blob: e192ead7b349f9007de2a6dfcc62ea48a3b5f4e7 [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>
Benjamin Franzke0c991632011-09-27 21:57:31 +020032#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <time.h>
34#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050035#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040036#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040039
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050040#include <wayland-egl.h>
41
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042#include <GL/gl.h>
43#include <EGL/egl.h>
44#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040045
Kristian Høgsberg8def2642011-01-14 17:41:33 -050046#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040047#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040048#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050049
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040050#include <X11/extensions/XKBcommon.h>
51
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050052#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050053#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050054#include "wayland-client.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040055#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050057#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050058
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050060 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050061 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040062 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040063 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Kristian Høgsberg478d9262010-06-08 20:34:11 -040081 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040082 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040084 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040085 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040086 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040087
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050088 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
89 PFNEGLCREATEIMAGEKHRPROC create_image;
90 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050091};
92
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040094 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040095 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040096 TYPE_TRANSIENT,
97 TYPE_CUSTOM
98};
99
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500100struct window {
101 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500102 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500103 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500104 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500105 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500106 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400107 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400108 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400109 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500110 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500111 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400112 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400113 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400114 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400115 struct input *grab_device;
116 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500117 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400118 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500119
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400120 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500121 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500122
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500123 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400124 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500125 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400126 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500127 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400128 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400129 window_enter_handler_t enter_handler;
130 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400131 window_item_focus_handler_t item_focus_handler;
132
133 struct wl_list item_list;
134 struct item *focus_item;
135 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400136
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500137 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400138 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500139};
140
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400141struct item {
142 struct wl_list link;
143 struct rectangle allocation;
144 void *user_data;
145};
146
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147struct input {
148 struct display *display;
149 struct wl_input_device *input_device;
150 struct window *pointer_focus;
151 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500152 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400153 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400154 uint32_t modifiers;
155 int32_t x, y, sx, sy;
156 struct wl_list link;
157};
158
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400159enum {
160 POINTER_DEFAULT = 100,
161 POINTER_UNSET
162};
163
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500164enum window_location {
165 WINDOW_INTERIOR = 0,
166 WINDOW_RESIZING_TOP = 1,
167 WINDOW_RESIZING_BOTTOM = 2,
168 WINDOW_RESIZING_LEFT = 4,
169 WINDOW_RESIZING_TOP_LEFT = 5,
170 WINDOW_RESIZING_BOTTOM_LEFT = 6,
171 WINDOW_RESIZING_RIGHT = 8,
172 WINDOW_RESIZING_TOP_RIGHT = 9,
173 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
174 WINDOW_RESIZING_MASK = 15,
175 WINDOW_EXTERIOR = 16,
176 WINDOW_TITLEBAR = 17,
177 WINDOW_CLIENT_AREA = 18,
178};
179
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400180const char *option_xkb_layout = "us";
181const char *option_xkb_variant = "";
182const char *option_xkb_options = "";
183
184static const GOptionEntry xkb_option_entries[] = {
185 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
186 &option_xkb_layout, "XKB Layout" },
187 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
188 &option_xkb_variant, "XKB Variant" },
189 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
190 &option_xkb_options, "XKB Options" },
191 { NULL }
192};
193
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400194static const cairo_user_data_key_t surface_data_key;
195struct surface_data {
196 struct wl_buffer *buffer;
197};
198
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500199#define MULT(_d,c,a,t) \
200 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
201
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500202#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400203
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100204struct egl_window_surface_data {
205 struct display *display;
206 struct wl_surface *surface;
207 struct wl_egl_window *window;
208 EGLSurface surf;
209};
210
211static void
212egl_window_surface_data_destroy(void *p)
213{
214 struct egl_window_surface_data *data = p;
215 struct display *d = data->display;
216
217 eglDestroySurface(d->dpy, data->surf);
218 wl_egl_window_destroy(data->window);
219 data->surface = NULL;
220
221 free(p);
222}
223
224static cairo_surface_t *
225display_create_egl_window_surface(struct display *display,
226 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400227 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100228 struct rectangle *rectangle)
229{
230 cairo_surface_t *cairo_surface;
231 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400232 EGLConfig config;
233 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200234 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100235
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400236 static const EGLint premul_attribs[] = {
237 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
238 EGL_NONE
239 };
240
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100241 data = malloc(sizeof *data);
242 if (data == NULL)
243 return NULL;
244
245 data->display = display;
246 data->surface = surface;
247
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400248 if (flags & SURFACE_OPAQUE) {
249 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200250 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200251 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400252 } else {
253 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200254 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400255 attribs = premul_attribs;
256 }
257
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400258 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100259 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400260 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100261
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400262 data->surf = eglCreateWindowSurface(display->dpy, config,
263 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100264
Benjamin Franzke0c991632011-09-27 21:57:31 +0200265 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100266 data->surf,
267 rectangle->width,
268 rectangle->height);
269
270 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
271 data, egl_window_surface_data_destroy);
272
273 return cairo_surface;
274}
275
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500276struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400277 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200278 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400279 EGLImageKHR image;
280 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800281 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500282 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283};
284
285static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500286egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400287{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500288 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800289 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200293 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800294
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500295 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400296 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500297 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500298 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400299}
300
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500301EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500302display_get_image_for_egl_image_surface(struct display *display,
303 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500304{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500305 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500306
307 data = cairo_surface_get_user_data (surface, &surface_data_key);
308
309 return data->image;
310}
311
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500312static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400314 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500317 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318 EGLDisplay dpy = display->dpy;
319 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400320 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200321 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322
323 data = malloc(sizeof *data);
324 if (data == NULL)
325 return NULL;
326
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327 data->display = display;
328
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400329 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400330 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000332 free(data);
333 return NULL;
334 }
335
Benjamin Franzke0c991632011-09-27 21:57:31 +0200336 if (flags & SURFACE_OPAQUE) {
337 data->device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400338 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200339 content = CAIRO_CONTENT_COLOR;
340 } else {
341 data->device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400342 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200343 content = CAIRO_CONTENT_COLOR_ALPHA;
344 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400345
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500346 data->image = display->create_image(dpy, NULL,
347 EGL_NATIVE_PIXMAP_KHR,
348 (EGLClientBuffer) data->pixmap,
349 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500351 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 free(data);
353 return NULL;
354 }
355
356 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400357 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500358
Benjamin Franzke0c991632011-09-27 21:57:31 +0200359 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500362 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200363 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400364
Benjamin Franzke0c991632011-09-27 21:57:31 +0200365 surface = cairo_gl_surface_create_for_texture(data->device,
366 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400367 data->texture,
368 rectangle->width,
369 rectangle->height);
370
371 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500372 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400373
374 return surface;
375}
376
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500377static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500378display_create_egl_image_surface_from_file(struct display *display,
379 const char *filename,
380 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400381{
382 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400383 GdkPixbuf *pixbuf;
384 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400385 int stride, i;
386 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500387 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400388
389 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400391 FALSE, &error);
392 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400394
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400395 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
396 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500397 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400399 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400400
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400401
402 stride = gdk_pixbuf_get_rowstride(pixbuf);
403 pixels = gdk_pixbuf_get_pixels(pixbuf);
404
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400406 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400408 while (p < end) {
409 unsigned int t;
410
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400411 MULT(p[0], p[0], p[3], t);
412 MULT(p[1], p[1], p[3], t);
413 MULT(p[2], p[2], p[3], t);
414 p += 4;
415
416 }
417 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400418
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200419 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000420 if (surface == NULL) {
421 g_object_unref(pixbuf);
422 return NULL;
423 }
424
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800425 data = cairo_surface_get_user_data(surface, &surface_data_key);
426
Benjamin Franzke0c991632011-09-27 21:57:31 +0200427 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800428 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800429 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
430 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200431 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500433 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400434
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435 return surface;
436}
437
438#endif
439
440struct wl_buffer *
441display_get_buffer_for_surface(struct display *display,
442 cairo_surface_t *surface)
443{
444 struct surface_data *data;
445
446 data = cairo_surface_get_user_data (surface, &surface_data_key);
447
448 return data->buffer;
449}
450
451struct shm_surface_data {
452 struct surface_data data;
453 void *map;
454 size_t length;
455};
456
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500457static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400458shm_surface_data_destroy(void *p)
459{
460 struct shm_surface_data *data = p;
461
462 wl_buffer_destroy(data->data.buffer);
463 munmap(data->map, data->length);
464}
465
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500466static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400468 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469{
470 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400471 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400472 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800473 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400474 char filename[] = "/tmp/wayland-shm-XXXXXX";
475
476 data = malloc(sizeof *data);
477 if (data == NULL)
478 return NULL;
479
480 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
481 rectangle->width);
482 data->length = stride * rectangle->height;
483 fd = mkstemp(filename);
484 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000485 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400486 return NULL;
487 }
488 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000489 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490 close(fd);
491 return NULL;
492 }
493
494 data->map = mmap(NULL, data->length,
495 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
496 unlink(filename);
497
498 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000499 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500 close(fd);
501 return NULL;
502 }
503
504 surface = cairo_image_surface_create_for_data (data->map,
505 CAIRO_FORMAT_ARGB32,
506 rectangle->width,
507 rectangle->height,
508 stride);
509
510 cairo_surface_set_user_data (surface, &surface_data_key,
511 data, shm_surface_data_destroy);
512
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400513 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400514 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400515 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400516 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400517
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518 data->data.buffer = wl_shm_create_buffer(display->shm,
519 fd,
520 rectangle->width,
521 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400522 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523
524 close(fd);
525
526 return surface;
527}
528
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500529static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530display_create_shm_surface_from_file(struct display *display,
531 const char *filename,
532 struct rectangle *rect)
533{
534 cairo_surface_t *surface;
535 GdkPixbuf *pixbuf;
536 GError *error = NULL;
537 int stride, i;
538 unsigned char *pixels, *p, *end, *dest_data;
539 int dest_stride;
540 uint32_t *d;
541
542 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
543 rect->width, rect->height,
544 FALSE, &error);
545 if (error != NULL)
546 return NULL;
547
548 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
549 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500550 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 return NULL;
552 }
553
554 stride = gdk_pixbuf_get_rowstride(pixbuf);
555 pixels = gdk_pixbuf_get_pixels(pixbuf);
556
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000558 if (surface == NULL) {
559 g_object_unref(pixbuf);
560 return NULL;
561 }
562
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563 dest_data = cairo_image_surface_get_data (surface);
564 dest_stride = cairo_image_surface_get_stride (surface);
565
566 for (i = 0; i < rect->height; i++) {
567 d = (uint32_t *) (dest_data + i * dest_stride);
568 p = pixels + i * stride;
569 end = p + rect->width * 4;
570 while (p < end) {
571 unsigned int t;
572 unsigned char a, r, g, b;
573
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574 a = p[3];
575 MULT(r, p[0], a, t);
576 MULT(g, p[1], a, t);
577 MULT(b, p[2], a, t);
578 p += 4;
579 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
580 }
581 }
582
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500583 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584
585 return surface;
586}
587
nobled7b87cb02011-02-01 18:51:47 +0000588static int
589check_size(struct rectangle *rect)
590{
591 if (rect->width && rect->height)
592 return 0;
593
594 fprintf(stderr, "tried to create surface of "
595 "width: %d, height: %d\n", rect->width, rect->height);
596 return -1;
597}
598
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400599cairo_surface_t *
600display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100601 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400602 struct rectangle *rectangle,
603 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604{
nobled7b87cb02011-02-01 18:51:47 +0000605 if (check_size(rectangle) < 0)
606 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500607#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500608 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100609 if (surface)
610 return display_create_egl_window_surface(display,
611 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400612 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100613 rectangle);
614 else
615 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400616 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500618 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400620 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621}
622
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500623static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624display_create_surface_from_file(struct display *display,
625 const char *filename,
626 struct rectangle *rectangle)
627{
nobled7b87cb02011-02-01 18:51:47 +0000628 if (check_size(rectangle) < 0)
629 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500630#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500631 if (display->dpy) {
632 return display_create_egl_image_surface_from_file(display,
633 filename,
634 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500635 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500637 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400638}
Yuval Fledel45568f62010-12-06 09:18:12 -0500639 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400640 const char *filename;
641 int hotspot_x, hotspot_y;
642} pointer_images[] = {
643 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
644 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
645 { DATADIR "/wayland/bottom_side.png", 16, 20 },
646 { DATADIR "/wayland/grabbing.png", 20, 17 },
647 { DATADIR "/wayland/left_ptr.png", 10, 5 },
648 { DATADIR "/wayland/left_side.png", 10, 20 },
649 { DATADIR "/wayland/right_side.png", 30, 19 },
650 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
651 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
652 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400653 { DATADIR "/wayland/xterm.png", 15, 15 },
654 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400655};
656
657static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400658create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400659{
660 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400661 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400662 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400663
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400665 display->pointer_surfaces =
666 malloc(count * sizeof *display->pointer_surfaces);
667 rect.width = width;
668 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400670 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671 display_create_surface_from_file(display,
672 pointer_images[i].filename,
673 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100674 if (!display->pointer_surfaces[i]) {
675 fprintf(stderr, "Error loading pointer image: %s\n",
676 pointer_images[i].filename);
677 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678 }
679
680}
681
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400682cairo_surface_t *
683display_get_pointer_surface(struct display *display, int pointer,
684 int *width, int *height,
685 int *hotspot_x, int *hotspot_y)
686{
687 cairo_surface_t *surface;
688
689 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500690#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691 *width = cairo_gl_surface_get_width(surface);
692 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000693#else
694 *width = cairo_image_surface_get_width(surface);
695 *height = cairo_image_surface_get_height(surface);
696#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400697 *hotspot_x = pointer_images[pointer].hotspot_x;
698 *hotspot_y = pointer_images[pointer].hotspot_y;
699
700 return cairo_surface_reference(surface);
701}
702
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400703
704static void
705window_attach_surface(struct window *window);
706
707static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400708free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400709{
710 struct window *window = data;
711
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400712 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400713 cairo_surface_destroy(window->pending_surface);
714 window->pending_surface = NULL;
715 if (window->cairo_surface)
716 window_attach_surface(window);
717}
718
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400719static const struct wl_callback_listener free_surface_listener = {
720 free_surface
721};
722
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500723static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200724window_get_resize_dx_dy(struct window *window, int *x, int *y)
725{
726 if (window->resize_edges & WINDOW_RESIZING_LEFT)
727 *x = window->server_allocation.width - window->allocation.width;
728 else
729 *x = 0;
730
731 if (window->resize_edges & WINDOW_RESIZING_TOP)
732 *y = window->server_allocation.height -
733 window->allocation.height;
734 else
735 *y = 0;
736
737 window->resize_edges = 0;
738}
739
740static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400741window_set_type(struct window *window)
742{
743 struct display *display = window->display;
744
745 switch (window->type) {
746 case TYPE_FULLSCREEN:
747 wl_shell_set_fullscreen(display->shell, window->surface);
748 break;
749 case TYPE_TOPLEVEL:
750 wl_shell_set_toplevel(display->shell, window->surface);
751 break;
752 case TYPE_TRANSIENT:
753 wl_shell_set_transient(display->shell, window->surface,
754 window->parent->surface,
755 window->x, window->y, 0);
756 break;
757 case TYPE_CUSTOM:
758 break;
759 }
760}
761
762static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500763window_attach_surface(struct window *window)
764{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400765 struct display *display = window->display;
766 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400767 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000768#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100769 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000770#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500771 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400773 if (display->shell)
774 window_set_type(window);
775
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100776 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000777#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
779 data = cairo_surface_get_user_data(window->cairo_surface,
780 &surface_data_key);
781
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100782 cairo_gl_surface_swapbuffers(window->cairo_surface);
783 wl_egl_window_get_attached_size(data->window,
784 &window->server_allocation.width,
785 &window->server_allocation.height);
786 break;
787 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000788#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200790 window_get_resize_dx_dy(window, &x, &y);
791
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 if (window->pending_surface != NULL)
793 return;
794
795 window->pending_surface = window->cairo_surface;
796 window->cairo_surface = NULL;
797
798 buffer =
799 display_get_buffer_for_surface(display,
800 window->pending_surface);
801
802 wl_surface_attach(window->surface, buffer, x, y);
803 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400804 cb = wl_display_sync(display->display);
805 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100806 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000807 default:
808 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500810
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500811 wl_surface_damage(window->surface, 0, 0,
812 window->allocation.width,
813 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500814}
815
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500816void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400817window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500818{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100819 if (window->cairo_surface) {
820 switch (window->buffer_type) {
821 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
822 case WINDOW_BUFFER_TYPE_SHM:
823 display_surface_damage(window->display,
824 window->cairo_surface,
825 0, 0,
826 window->allocation.width,
827 window->allocation.height);
828 break;
829 default:
830 break;
831 }
832 window_attach_surface(window);
833 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500834}
835
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836void
837window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500839 cairo_surface_reference(surface);
840
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400841 if (window->cairo_surface != NULL)
842 cairo_surface_destroy(window->cairo_surface);
843
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500844 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400845}
846
Benjamin Franzke22d54812011-07-16 19:50:32 +0000847#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848static void
849window_resize_cairo_window_surface(struct window *window)
850{
851 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200852 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853
854 data = cairo_surface_get_user_data(window->cairo_surface,
855 &surface_data_key);
856
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200857 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100858 wl_egl_window_resize(data->window,
859 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200860 window->allocation.height,
861 x,y);
862
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 cairo_gl_surface_set_size(window->cairo_surface,
864 window->allocation.width,
865 window->allocation.height);
866}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000867#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100868
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400869struct display *
870window_get_display(struct window *window)
871{
872 return window->display;
873}
874
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400875void
876window_create_surface(struct window *window)
877{
878 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400879 uint32_t flags = 0;
880
881 if (!window->transparent)
882 flags = SURFACE_OPAQUE;
883
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500885#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100886 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
887 if (window->cairo_surface) {
888 window_resize_cairo_window_surface(window);
889 return;
890 }
891 surface = display_create_surface(window->display,
892 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400893 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500895 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400898 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400899 break;
900#endif
901 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400902 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400903 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400904 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800905 default:
906 surface = NULL;
907 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400909
910 window_set_surface(window, surface);
911 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400912}
913
914static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500915window_draw_menu(struct window *window)
916{
917 cairo_t *cr;
918 int width, height, r = 5;
919
920 window_create_surface(window);
921
922 cr = cairo_create(window->cairo_surface);
923 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
924 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
925 cairo_paint(cr);
926
927 width = window->allocation.width;
928 height = window->allocation.height;
929 rounded_rect(cr, r, r, width - r, height - r, r);
930 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
931 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
932 cairo_fill(cr);
933 cairo_destroy(cr);
934}
935
936static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500937window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500938{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500939 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500940 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 cairo_surface_t *frame;
942 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500943
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400944 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400946 width = window->allocation.width;
947 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500948
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500949 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500950
Kristian Høgsberg09531622010-06-14 23:22:15 -0400951 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400952 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400953 cairo_paint(cr);
954
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400955 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400956 tile_mask(cr, window->display->shadow,
957 shadow_dx, shadow_dy, width, height,
958 window->margin + 10 - shadow_dx,
959 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500960
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400961 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400962 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400963 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400964 frame = window->display->inactive_frame;
965
966 tile_source(cr, frame, 0, 0, width, height,
967 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500968
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500969 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
970 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500971 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400973 if (window->keyboard_device)
974 cairo_set_source_rgb(cr, 0, 0, 0);
975 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400976 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400977 cairo_show_text(cr, window->title);
978
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500979 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400980
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100981 /* FIXME: this breakes gears, fix cairo? */
982#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400983 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100984#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500985}
986
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400987void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500988window_destroy(struct window *window)
989{
990 wl_surface_destroy(window->surface);
991 wl_list_remove(&window->link);
992 free(window);
993}
994
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995static struct item *
996window_find_item(struct window *window, int32_t x, int32_t y)
997{
998 struct item *item;
999
1000 wl_list_for_each(item, &window->item_list, link) {
1001 if (item->allocation.x <= x &&
1002 x < item->allocation.x + item->allocation.width &&
1003 item->allocation.y <= y &&
1004 y < item->allocation.y + item->allocation.height) {
1005 return item;
1006 }
1007 }
1008
1009 return NULL;
1010}
1011
1012struct item *
1013window_add_item(struct window *window, void *data)
1014{
1015 struct item *item;
1016
1017 item = malloc(sizeof *item);
1018 memset(item, 0, sizeof *item);
1019 item->user_data = data;
1020 wl_list_insert(window->item_list.prev, &item->link);
1021
1022 return item;
1023}
1024
1025void
1026window_for_each_item(struct window *window, item_func_t func, void *data)
1027{
1028 struct item *item;
1029
1030 wl_list_for_each(item, &window->item_list, link)
1031 func(item, data);
1032}
1033
1034struct item *
1035window_get_focus_item(struct window *window)
1036{
1037 return window->focus_item;
1038}
1039
1040void
1041item_get_allocation(struct item *item, struct rectangle *allocation)
1042{
1043 *allocation = item->allocation;
1044}
1045
1046void
1047item_set_allocation(struct item *item,
1048 int32_t x, int32_t y, int32_t width, int32_t height)
1049{
1050 item->allocation.x = x;
1051 item->allocation.y = y;
1052 item->allocation.width = width;
1053 item->allocation.height = height;
1054}
1055
1056void *
1057item_get_user_data(struct item *item)
1058{
1059 return item->user_data;
1060}
1061
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001062void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001063window_draw(struct window *window)
1064{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001065 if (window->parent)
1066 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001067 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001068 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001069 else
1070 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001071}
1072
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001073cairo_surface_t *
1074window_get_surface(struct window *window)
1075{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001076 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001077}
1078
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001079struct wl_surface *
1080window_get_wl_surface(struct window *window)
1081{
1082 return window->surface;
1083}
1084
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001085static int
1086get_pointer_location(struct window *window, int32_t x, int32_t y)
1087{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001088 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001089 const int grip_size = 8;
1090
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001091 if (!window->decoration)
1092 return WINDOW_CLIENT_AREA;
1093
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001094 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001095 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001096 else if (window->margin <= x && x < window->margin + grip_size)
1097 hlocation = WINDOW_RESIZING_LEFT;
1098 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001099 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001100 else if (x < window->allocation.width - window->margin)
1101 hlocation = WINDOW_RESIZING_RIGHT;
1102 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104
1105 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001106 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107 else if (window->margin <= y && y < window->margin + grip_size)
1108 vlocation = WINDOW_RESIZING_TOP;
1109 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111 else if (y < window->allocation.height - window->margin)
1112 vlocation = WINDOW_RESIZING_BOTTOM;
1113 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001116 location = vlocation | hlocation;
1117 if (location & WINDOW_EXTERIOR)
1118 location = WINDOW_EXTERIOR;
1119 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1120 location = WINDOW_TITLEBAR;
1121 else if (location == WINDOW_INTERIOR)
1122 location = WINDOW_CLIENT_AREA;
1123
1124 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001125}
1126
1127static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001128set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001129{
1130 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 struct wl_buffer *buffer;
1132 cairo_surface_t *surface;
1133 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001134
1135 location = get_pointer_location(input->pointer_focus,
1136 input->sx, input->sy);
1137 switch (location) {
1138 case WINDOW_RESIZING_TOP:
1139 pointer = POINTER_TOP;
1140 break;
1141 case WINDOW_RESIZING_BOTTOM:
1142 pointer = POINTER_BOTTOM;
1143 break;
1144 case WINDOW_RESIZING_LEFT:
1145 pointer = POINTER_LEFT;
1146 break;
1147 case WINDOW_RESIZING_RIGHT:
1148 pointer = POINTER_RIGHT;
1149 break;
1150 case WINDOW_RESIZING_TOP_LEFT:
1151 pointer = POINTER_TOP_LEFT;
1152 break;
1153 case WINDOW_RESIZING_TOP_RIGHT:
1154 pointer = POINTER_TOP_RIGHT;
1155 break;
1156 case WINDOW_RESIZING_BOTTOM_LEFT:
1157 pointer = POINTER_BOTTOM_LEFT;
1158 break;
1159 case WINDOW_RESIZING_BOTTOM_RIGHT:
1160 pointer = POINTER_BOTTOM_RIGHT;
1161 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001162 case WINDOW_EXTERIOR:
1163 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001164 if (input->current_pointer_image == POINTER_DEFAULT)
1165 return;
1166
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001167 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001168 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169 return;
1170 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171 break;
1172 }
1173
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001174 if (pointer == input->current_pointer_image)
1175 return;
1176
1177 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001178 surface = display->pointer_surfaces[pointer];
1179 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001180 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001181 pointer_images[pointer].hotspot_x,
1182 pointer_images[pointer].hotspot_y);
1183}
1184
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001185static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001186window_set_focus_item(struct window *window, struct item *focus)
1187{
1188 void *data;
1189
1190 if (focus == window->focus_item)
1191 return;
1192
1193 window->focus_item = focus;
1194 data = focus ? focus->user_data : NULL;
1195 if (window->item_focus_handler)
1196 window->item_focus_handler(window, focus, data);
1197}
1198
1199static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001200window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001201 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001202 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001203{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001204 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001205 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001206 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001207 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001208
1209 input->x = x;
1210 input->y = y;
1211 input->sx = sx;
1212 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001214 if (!window->focus_item || !window->item_grab_button) {
1215 item = window_find_item(window, sx, sy);
1216 window_set_focus_item(window, item);
1217 }
1218
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001219 if (window->motion_handler)
1220 pointer = (*window->motion_handler)(window, input, time,
1221 x, y, sx, sy,
1222 window->user_data);
1223
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001224 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001225}
1226
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001227static void
1228window_handle_button(void *data,
1229 struct wl_input_device *input_device,
1230 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001231{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001232 struct input *input = data;
1233 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001234 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001236
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001237 if (window->focus_item && window->item_grab_button == 0 && state)
1238 window->item_grab_button = button;
1239
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001241
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001242 if (window->display->shell &&
1243 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001244 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001246 wl_shell_move(window->display->shell,
1247 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001248 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001249 case WINDOW_RESIZING_TOP:
1250 case WINDOW_RESIZING_BOTTOM:
1251 case WINDOW_RESIZING_LEFT:
1252 case WINDOW_RESIZING_RIGHT:
1253 case WINDOW_RESIZING_TOP_LEFT:
1254 case WINDOW_RESIZING_TOP_RIGHT:
1255 case WINDOW_RESIZING_BOTTOM_LEFT:
1256 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001257 wl_shell_resize(window->display->shell,
1258 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001260 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 case WINDOW_CLIENT_AREA:
1262 if (window->button_handler)
1263 (*window->button_handler)(window,
1264 input, time,
1265 button, state,
1266 window->user_data);
1267 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001268 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 } else {
1270 if (window->button_handler)
1271 (*window->button_handler)(window,
1272 input, time,
1273 button, state,
1274 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001275 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001276
1277 if (window->focus_item &&
1278 window->item_grab_button == button && !state) {
1279 window->item_grab_button = 0;
1280 item = window_find_item(window, input->sx, input->sy);
1281 window_set_focus_item(window, item);
1282 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001283}
1284
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001285static void
1286window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001287 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001288{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001289 struct input *input = data;
1290 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001291 struct display *d = window->display;
1292 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001293
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001294 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001295 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001296 return;
1297
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001298 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001299 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001300 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1301 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001302
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001303 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1304
1305 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001306 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001307 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001308 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001309
1310 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001311 (*window->key_handler)(window, input, time, key, sym, state,
1312 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001313}
1314
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001315static void
1316window_handle_pointer_focus(void *data,
1317 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001318 uint32_t time, struct wl_surface *surface,
1319 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001320{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001321 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001322 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001323 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001324 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001325
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001326 window = input->pointer_focus;
1327 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001328 window_set_focus_item(window, NULL);
1329
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001330 if (window->leave_handler)
1331 window->leave_handler(window, input,
1332 time, window->user_data);
1333 input->pointer_focus = NULL;
1334 input->current_pointer_image = POINTER_UNSET;
1335 }
1336
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001337 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001338 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001339 window = input->pointer_focus;
1340
Kristian Høgsberg59826582011-01-20 11:56:57 -05001341 input->x = x;
1342 input->y = y;
1343 input->sx = sx;
1344 input->sy = sy;
1345
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001346 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001347 if (window->enter_handler)
1348 pointer = window->enter_handler(window, input,
1349 time, sx, sy,
1350 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001351
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001352 item = window_find_item(window, x, y);
1353 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001354
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001355 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001356 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001357}
1358
1359static void
1360window_handle_keyboard_focus(void *data,
1361 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001362 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001363 struct wl_surface *surface,
1364 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001365{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001366 struct input *input = data;
1367 struct window *window = input->keyboard_focus;
1368 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001369 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001370
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001371 window = input->keyboard_focus;
1372 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001373 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001374 if (window->keyboard_focus_handler)
1375 (*window->keyboard_focus_handler)(window, NULL,
1376 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001377 }
1378
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001379 if (surface)
1380 input->keyboard_focus = wl_surface_get_user_data(surface);
1381 else
1382 input->keyboard_focus = NULL;
1383
1384 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001385 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001386 for (k = keys->data; k < end; k++)
1387 input->modifiers |= d->xkb->map->modmap[*k];
1388
1389 window = input->keyboard_focus;
1390 if (window) {
1391 window->keyboard_device = input;
1392 if (window->keyboard_focus_handler)
1393 (*window->keyboard_focus_handler)(window,
1394 window->keyboard_device,
1395 window->user_data);
1396 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001397}
1398
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001399static const struct wl_input_device_listener input_device_listener = {
1400 window_handle_motion,
1401 window_handle_button,
1402 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001403 window_handle_pointer_focus,
1404 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001405};
1406
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001407void
1408input_get_position(struct input *input, int32_t *x, int32_t *y)
1409{
1410 *x = input->sx;
1411 *y = input->sy;
1412}
1413
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001414struct wl_input_device *
1415input_get_input_device(struct input *input)
1416{
1417 return input->input_device;
1418}
1419
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001420uint32_t
1421input_get_modifiers(struct input *input)
1422{
1423 return input->modifiers;
1424}
1425
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001426struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001427window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001428{
Benjamin Franzke0c991632011-09-27 21:57:31 +02001429 cairo_device_flush (window->display->rgb_device);
1430 cairo_device_flush (window->display->argb_device);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001431
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001432 return wl_shell_create_drag(window->display->shell);
1433}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001434
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001435void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001436window_move(struct window *window, struct input *input, uint32_t time)
1437{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001438 if (window->display->shell)
1439 wl_shell_move(window->display->shell,
1440 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001441}
1442
1443void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001444window_activate_drag(struct wl_drag *drag, struct window *window,
1445 struct input *input, uint32_t time)
1446{
1447 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001448}
1449
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001450static void
1451handle_configure(void *data, struct wl_shell *shell,
1452 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001453 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001454{
1455 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001456 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001457
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001458 /* FIXME: this is probably the wrong place to check for width
1459 * or height <= 0, but it prevents the compositor from crashing
1460 */
1461 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001462 return;
1463
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001464 window->resize_edges = edges;
1465
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001466 if (window->resize_handler) {
1467 child_width = width - 20 - window->margin * 2;
1468 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001469
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001470 (*window->resize_handler)(window,
1471 child_width, child_height,
1472 window->user_data);
1473 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001474 window->allocation.width = width;
1475 window->allocation.height = height;
1476
1477 if (window->redraw_handler)
1478 window_schedule_redraw(window);
1479 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001480}
1481
1482static const struct wl_shell_listener shell_listener = {
1483 handle_configure,
1484};
1485
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001486void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001487window_get_allocation(struct window *window,
1488 struct rectangle *allocation)
1489{
1490 *allocation = window->allocation;
1491}
1492
1493void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001494window_get_child_allocation(struct window *window,
1495 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001496{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001497 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001498 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001499 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001500 allocation->x = window->margin + 10;
1501 allocation->y = window->margin + 50;
1502 allocation->width =
1503 window->allocation.width - 20 - window->margin * 2;
1504 allocation->height =
1505 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001506 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001507}
1508
1509void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001510window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001511{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001512 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001513 window->allocation.x = 20 + window->margin;
1514 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001515 window->allocation.width = width + 20 + window->margin * 2;
1516 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001517 } else {
1518 window->allocation.x = 0;
1519 window->allocation.y = 0;
1520 window->allocation.width = width;
1521 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001522 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001523}
1524
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001525static void
1526idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001527{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001528 struct window *window =
1529 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001530
1531 window->redraw_handler(window, window->user_data);
1532 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001533}
1534
1535void
1536window_schedule_redraw(struct window *window)
1537{
1538 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001539 window->redraw_task.run = idle_redraw;
1540 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001541 window->redraw_scheduled = 1;
1542 }
1543}
1544
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001545void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001546window_set_custom(struct window *window)
1547{
1548 window->type = TYPE_CUSTOM;
1549}
1550
1551void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001552window_set_fullscreen(struct window *window, int fullscreen)
1553{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001554 int32_t width, height;
1555
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001556 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001557 return;
1558
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001559 if (fullscreen) {
1560 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001561 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001562 width = window->display->screen_allocation.width;
1563 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001564 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001565 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001566 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001567 width = window->saved_allocation.width - 20 - window->margin * 2;
1568 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001569 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001570 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001571
1572 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001573}
1574
1575void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001576window_set_decoration(struct window *window, int decoration)
1577{
1578 window->decoration = decoration;
1579}
1580
1581void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001582window_set_user_data(struct window *window, void *data)
1583{
1584 window->user_data = data;
1585}
1586
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001587void *
1588window_get_user_data(struct window *window)
1589{
1590 return window->user_data;
1591}
1592
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001593void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001594window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001595 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001596{
1597 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001598}
1599
1600void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001601window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001602 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001603{
1604 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001605}
1606
1607void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001608window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001609 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001610{
1611 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001612}
1613
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001614void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001615window_set_button_handler(struct window *window,
1616 window_button_handler_t handler)
1617{
1618 window->button_handler = handler;
1619}
1620
1621void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001622window_set_motion_handler(struct window *window,
1623 window_motion_handler_t handler)
1624{
1625 window->motion_handler = handler;
1626}
1627
1628void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001629window_set_enter_handler(struct window *window,
1630 window_enter_handler_t handler)
1631{
1632 window->enter_handler = handler;
1633}
1634
1635void
1636window_set_leave_handler(struct window *window,
1637 window_leave_handler_t handler)
1638{
1639 window->leave_handler = handler;
1640}
1641
1642void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001643window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001644 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001645{
1646 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001647}
1648
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001649void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001650window_set_item_focus_handler(struct window *window,
1651 window_item_focus_handler_t handler)
1652{
1653 window->item_focus_handler = handler;
1654}
1655
1656void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001657window_set_transparent(struct window *window, int transparent)
1658{
1659 window->transparent = transparent;
1660}
1661
1662void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001663window_set_title(struct window *window, const char *title)
1664{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001665 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001666 window->title = strdup(title);
1667}
1668
1669const char *
1670window_get_title(struct window *window)
1671{
1672 return window->title;
1673}
1674
1675void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001676display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1677 int32_t x, int32_t y, int32_t width, int32_t height)
1678{
1679 struct wl_buffer *buffer;
1680
1681 buffer = display_get_buffer_for_surface(display, cairo_surface);
1682
1683 wl_buffer_damage(buffer, x, y, width, height);
1684}
1685
1686void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001687window_damage(struct window *window, int32_t x, int32_t y,
1688 int32_t width, int32_t height)
1689{
1690 wl_surface_damage(window->surface, x, y, width, height);
1691}
1692
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001693static struct window *
1694window_create_internal(struct display *display, struct window *parent,
1695 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001696{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001697 struct window *window;
1698
1699 window = malloc(sizeof *window);
1700 if (window == NULL)
1701 return NULL;
1702
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001703 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001704 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001705 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001706 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001707 window->allocation.x = 0;
1708 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001709 window->allocation.width = width;
1710 window->allocation.height = height;
1711 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001712 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001713 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001714 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001715 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001716
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001717 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001718#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001719 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1720 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001721#else
1722 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1723#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001724 else
1725 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001726
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001727 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001728 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001729
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001730 return window;
1731}
1732
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001733struct window *
1734window_create(struct display *display, int32_t width, int32_t height)
1735{
1736 struct window *window;
1737
1738 window = window_create_internal(display, NULL, width, height);
1739 if (!window)
1740 return NULL;
1741
1742 return window;
1743}
1744
1745struct window *
1746window_create_transient(struct display *display, struct window *parent,
1747 int32_t x, int32_t y, int32_t width, int32_t height)
1748{
1749 struct window *window;
1750
1751 window = window_create_internal(parent->display,
1752 parent, width, height);
1753 if (!window)
1754 return NULL;
1755
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001756 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001757 window->x = x;
1758 window->y = y;
1759
1760 return window;
1761}
1762
1763void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001764window_set_buffer_type(struct window *window, enum window_buffer_type type)
1765{
1766 window->buffer_type = type;
1767}
1768
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001769
1770static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001771display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001772 struct wl_output *wl_output,
1773 int x, int y,
1774 int physical_width,
1775 int physical_height,
1776 int subpixel,
1777 const char *make,
1778 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001779{
1780 struct display *display = data;
1781
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001782 display->screen_allocation.x = x;
1783 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001784}
1785
1786static void
1787display_handle_mode(void *data,
1788 struct wl_output *wl_output,
1789 uint32_t flags,
1790 int width,
1791 int height,
1792 int refresh)
1793{
1794 struct display *display = data;
1795
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001796 display->screen_allocation.width = width;
1797 display->screen_allocation.height = height;
1798}
1799
1800static const struct wl_output_listener output_listener = {
1801 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001802 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001803};
1804
1805static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001806display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001807{
1808 struct input *input;
1809
1810 input = malloc(sizeof *input);
1811 if (input == NULL)
1812 return;
1813
1814 memset(input, 0, sizeof *input);
1815 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001816 input->input_device =
1817 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001818 input->pointer_focus = NULL;
1819 input->keyboard_focus = NULL;
1820 wl_list_insert(d->input_list.prev, &input->link);
1821
1822 wl_input_device_add_listener(input->input_device,
1823 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001824 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001825}
1826
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001827struct selection_offer {
1828 struct display *display;
1829 struct wl_selection_offer *offer;
1830 struct wl_array types;
1831 struct input *input;
1832};
1833
1834int
1835input_offers_mime_type(struct input *input, const char *type)
1836{
1837 struct selection_offer *offer = input->offer;
1838 char **p, **end;
1839
1840 if (offer == NULL)
1841 return 0;
1842
1843 end = offer->types.data + offer->types.size;
1844 for (p = offer->types.data; p < end; p++)
1845 if (strcmp(*p, type) == 0)
1846 return 1;
1847
1848 return 0;
1849}
1850
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001851void
1852input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001853{
1854 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001855
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001856 /* FIXME: A number of things can go wrong here: the object may
1857 * not be the current selection offer any more (which could
1858 * still work, but the source may have gone away or just
1859 * destroyed its wl_selection) or the offer may not have the
1860 * requested type after all (programmer/client error,
1861 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001862 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001863}
1864
1865static void
1866selection_offer_offer(void *data,
1867 struct wl_selection_offer *selection_offer,
1868 const char *type)
1869{
1870 struct selection_offer *offer = data;
1871
1872 char **p;
1873
1874 p = wl_array_add(&offer->types, sizeof *p);
1875 if (p)
1876 *p = strdup(type);
1877};
1878
1879static void
1880selection_offer_keyboard_focus(void *data,
1881 struct wl_selection_offer *selection_offer,
1882 struct wl_input_device *input_device)
1883{
1884 struct selection_offer *offer = data;
1885 struct input *input;
1886 char **p, **end;
1887
1888 if (input_device == NULL) {
1889 printf("selection offer retracted %p\n", selection_offer);
1890 input = offer->input;
1891 input->offer = NULL;
1892 wl_selection_offer_destroy(selection_offer);
1893 wl_array_release(&offer->types);
1894 free(offer);
1895 return;
1896 }
1897
1898 input = wl_input_device_get_user_data(input_device);
1899 printf("new selection offer %p:", selection_offer);
1900
1901 offer->input = input;
1902 input->offer = offer;
1903 end = offer->types.data + offer->types.size;
1904 for (p = offer->types.data; p < end; p++)
1905 printf(" %s", *p);
1906
1907 printf("\n");
1908}
1909
1910struct wl_selection_offer_listener selection_offer_listener = {
1911 selection_offer_offer,
1912 selection_offer_keyboard_focus
1913};
1914
1915static void
1916add_selection_offer(struct display *d, uint32_t id)
1917{
1918 struct selection_offer *offer;
1919
1920 offer = malloc(sizeof *offer);
1921 if (offer == NULL)
1922 return;
1923
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001924 offer->offer =
1925 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001926 offer->display = d;
1927 wl_array_init(&offer->types);
1928 offer->input = NULL;
1929
1930 wl_selection_offer_add_listener(offer->offer,
1931 &selection_offer_listener, offer);
1932}
1933
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001934static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001935display_handle_global(struct wl_display *display, uint32_t id,
1936 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001937{
1938 struct display *d = data;
1939
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001940 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001941 d->compositor =
1942 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001943 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001944 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001945 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001946 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001947 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001948 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001949 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001950 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001951 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001952 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001953 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001954 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001955 }
1956}
1957
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001958static void
1959display_render_frame(struct display *d)
1960{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001961 int radius = 8;
1962 cairo_t *cr;
1963
1964 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1965 cr = cairo_create(d->shadow);
1966 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1967 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1968 rounded_rect(cr, 16, 16, 112, 112, radius);
1969 cairo_fill(cr);
1970 cairo_destroy(cr);
1971 blur_surface(d->shadow, 64);
1972
1973 d->active_frame =
1974 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1975 cr = cairo_create(d->active_frame);
1976 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1977 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1978 rounded_rect(cr, 16, 16, 112, 112, radius);
1979 cairo_fill(cr);
1980 cairo_destroy(cr);
1981
1982 d->inactive_frame =
1983 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1984 cr = cairo_create(d->inactive_frame);
1985 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1986 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1987 rounded_rect(cr, 16, 16, 112, 112, radius);
1988 cairo_fill(cr);
1989 cairo_destroy(cr);
1990}
1991
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001992static void
1993init_xkb(struct display *d)
1994{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001995 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001996
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001997 names.rules = "evdev";
1998 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001999 names.layout = option_xkb_layout;
2000 names.variant = option_xkb_variant;
2001 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002002
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002003 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002004 if (!d->xkb) {
2005 fprintf(stderr, "Failed to compile keymap\n");
2006 exit(1);
2007 }
2008}
2009
Yuval Fledel45568f62010-12-06 09:18:12 -05002010static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002011init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002012{
2013 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002014 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002015
2016 static const EGLint premul_argb_cfg_attribs[] = {
2017 EGL_SURFACE_TYPE,
2018 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2019 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002020 EGL_RED_SIZE, 1,
2021 EGL_GREEN_SIZE, 1,
2022 EGL_BLUE_SIZE, 1,
2023 EGL_ALPHA_SIZE, 1,
2024 EGL_DEPTH_SIZE, 1,
2025 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2026 EGL_NONE
2027 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002028
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002029 static const EGLint rgb_cfg_attribs[] = {
2030 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2031 EGL_RED_SIZE, 1,
2032 EGL_GREEN_SIZE, 1,
2033 EGL_BLUE_SIZE, 1,
2034 EGL_ALPHA_SIZE, 0,
2035 EGL_DEPTH_SIZE, 1,
2036 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2037 EGL_NONE
2038 };
2039
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002040 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002041 if (!eglInitialize(d->dpy, &major, &minor)) {
2042 fprintf(stderr, "failed to initialize display\n");
2043 return -1;
2044 }
2045
2046 if (!eglBindAPI(EGL_OPENGL_API)) {
2047 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2048 return -1;
2049 }
2050
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002051 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002052 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002053 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002054 return -1;
2055 }
2056
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002057 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2058 &d->rgb_config, 1, &n) || n != 1) {
2059 fprintf(stderr, "failed to choose rgb config\n");
2060 return -1;
2061 }
2062
Benjamin Franzke0c991632011-09-27 21:57:31 +02002063 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2064 if (d->rgb_ctx == NULL) {
2065 fprintf(stderr, "failed to create context\n");
2066 return -1;
2067 }
2068 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2069 EGL_NO_CONTEXT, NULL);
2070 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002071 fprintf(stderr, "failed to create context\n");
2072 return -1;
2073 }
2074
Benjamin Franzke0c991632011-09-27 21:57:31 +02002075 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002076 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002077 return -1;
2078 }
2079
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002080#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002081 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2082 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002083 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002084 return -1;
2085 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002086 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2087 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2088 fprintf(stderr, "failed to get cairo egl argb device\n");
2089 return -1;
2090 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002091#endif
2092
2093 return 0;
2094}
2095
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002096static int
2097event_mask_update(uint32_t mask, void *data)
2098{
2099 struct display *d = data;
2100
2101 d->mask = mask;
2102
2103 return 0;
2104}
2105
2106static void
2107handle_display_data(struct task *task, uint32_t events)
2108{
2109 struct display *display =
2110 container_of(task, struct display, display_task);
2111
2112 wl_display_iterate(display->display, display->mask);
2113}
2114
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002115struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002116display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002117{
2118 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002119 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002120 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002121 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002122
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002123 g_type_init();
2124
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002125 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002126 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002127 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002128
2129 xkb_option_group = g_option_group_new("xkb",
2130 "Keyboard options",
2131 "Show all XKB options",
2132 NULL, NULL);
2133 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2134 g_option_context_add_group (context, xkb_option_group);
2135
2136 if (!g_option_context_parse(context, argc, argv, &error)) {
2137 fprintf(stderr, "option parsing failed: %s\n", error->message);
2138 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002139 }
2140
Tim Wiederhake748f6722011-01-23 23:25:25 +01002141 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002142
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002143 d = malloc(sizeof *d);
2144 if (d == NULL)
2145 return NULL;
2146
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002147 memset(d, 0, sizeof *d);
2148
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002149 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002150 if (d->display == NULL) {
2151 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002152 return NULL;
2153 }
2154
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002155 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2156 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2157 d->display_task.run = handle_display_data;
2158 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2159
2160 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002161 wl_list_init(&d->input_list);
2162
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002163 /* Set up listener so we'll catch all events. */
2164 wl_display_add_global_listener(d->display,
2165 display_handle_global, d);
2166
2167 /* Process connection events. */
2168 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002169 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002170 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002171
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002172 d->image_target_texture_2d =
2173 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2174 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2175 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2176
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002177 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002178
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002179 display_render_frame(d);
2180
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002181 wl_list_init(&d->window_list);
2182
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002183 init_xkb(d);
2184
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002185 return d;
2186}
2187
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002188struct wl_display *
2189display_get_display(struct display *display)
2190{
2191 return display->display;
2192}
2193
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002194struct wl_compositor *
2195display_get_compositor(struct display *display)
2196{
2197 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002198}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002199
2200EGLDisplay
2201display_get_egl_display(struct display *d)
2202{
2203 return d->dpy;
2204}
2205
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002206EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002207display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002208{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002209 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002210}
2211
Benjamin Franzke0c991632011-09-27 21:57:31 +02002212EGLConfig
2213display_get_argb_egl_config(struct display *d)
2214{
2215 return d->premultiplied_argb_config;
2216}
2217
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002218struct wl_shell *
2219display_get_shell(struct display *display)
2220{
2221 return display->shell;
2222}
2223
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002224int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002225display_acquire_window_surface(struct display *display,
2226 struct window *window,
2227 EGLContext ctx)
2228{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002229#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002230 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002231 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002232
2233 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002234 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002235 device = cairo_surface_get_device(window->cairo_surface);
2236 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002237 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002238
Benjamin Franzke0c991632011-09-27 21:57:31 +02002239 if (!ctx) {
2240 if (device == display->rgb_device)
2241 ctx = display->rgb_ctx;
2242 else if (device == display->argb_device)
2243 ctx = display->argb_ctx;
2244 else
2245 assert(0);
2246 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002247
2248 data = cairo_surface_get_user_data(window->cairo_surface,
2249 &surface_data_key);
2250
Benjamin Franzke0c991632011-09-27 21:57:31 +02002251 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002252 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2253 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002254
2255 return 0;
2256#else
2257 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002258#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002259}
2260
2261void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002262display_release_window_surface(struct display *display,
2263 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002264{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002265#ifdef HAVE_CAIRO_EGL
2266 cairo_device_t *device;
2267
2268 device = cairo_surface_get_device(window->cairo_surface);
2269 if (!device)
2270 return;
2271
2272 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002273 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002274 cairo_device_release(device);
2275#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002276}
2277
2278void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002279display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002280{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002281 wl_list_insert(&display->deferred_list, &task->link);
2282}
2283
2284void
2285display_watch_fd(struct display *display,
2286 int fd, uint32_t events, struct task *task)
2287{
2288 struct epoll_event ep;
2289
2290 ep.events = events;
2291 ep.data.ptr = task;
2292 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2293}
2294
2295void
2296display_run(struct display *display)
2297{
2298 struct task *task;
2299 struct epoll_event ep[16];
2300 int i, count;
2301
2302 while (1) {
2303 while (display->mask & WL_DISPLAY_WRITABLE)
2304 wl_display_iterate(display->display,
2305 WL_DISPLAY_WRITABLE);
2306
2307 count = epoll_wait(display->epoll_fd,
2308 ep, ARRAY_LENGTH(ep), -1);
2309 for (i = 0; i < count; i++) {
2310 task = ep[i].data.ptr;
2311 task->run(task, ep[i].events);
2312 }
2313
2314 while (!wl_list_empty(&display->deferred_list)) {
2315 task = container_of(display->deferred_list.next,
2316 struct task, link);
2317 wl_list_remove(&task->link);
2318 task->run(task, 0);
2319 }
2320 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002321}