blob: 8f5da49e752de27442c3cc4b228c28da7a16b6b0 [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);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674 }
675
676}
677
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678cairo_surface_t *
679display_get_pointer_surface(struct display *display, int pointer,
680 int *width, int *height,
681 int *hotspot_x, int *hotspot_y)
682{
683 cairo_surface_t *surface;
684
685 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500686#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400687 *width = cairo_gl_surface_get_width(surface);
688 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000689#else
690 *width = cairo_image_surface_get_width(surface);
691 *height = cairo_image_surface_get_height(surface);
692#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400693 *hotspot_x = pointer_images[pointer].hotspot_x;
694 *hotspot_y = pointer_images[pointer].hotspot_y;
695
696 return cairo_surface_reference(surface);
697}
698
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400699
700static void
701window_attach_surface(struct window *window);
702
703static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400704free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400705{
706 struct window *window = data;
707
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400708 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400709 cairo_surface_destroy(window->pending_surface);
710 window->pending_surface = NULL;
711 if (window->cairo_surface)
712 window_attach_surface(window);
713}
714
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400715static const struct wl_callback_listener free_surface_listener = {
716 free_surface
717};
718
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500719static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200720window_get_resize_dx_dy(struct window *window, int *x, int *y)
721{
722 if (window->resize_edges & WINDOW_RESIZING_LEFT)
723 *x = window->server_allocation.width - window->allocation.width;
724 else
725 *x = 0;
726
727 if (window->resize_edges & WINDOW_RESIZING_TOP)
728 *y = window->server_allocation.height -
729 window->allocation.height;
730 else
731 *y = 0;
732
733 window->resize_edges = 0;
734}
735
736static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400737window_set_type(struct window *window)
738{
739 struct display *display = window->display;
740
741 switch (window->type) {
742 case TYPE_FULLSCREEN:
743 wl_shell_set_fullscreen(display->shell, window->surface);
744 break;
745 case TYPE_TOPLEVEL:
746 wl_shell_set_toplevel(display->shell, window->surface);
747 break;
748 case TYPE_TRANSIENT:
749 wl_shell_set_transient(display->shell, window->surface,
750 window->parent->surface,
751 window->x, window->y, 0);
752 break;
753 case TYPE_CUSTOM:
754 break;
755 }
756}
757
758static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500759window_attach_surface(struct window *window)
760{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400761 struct display *display = window->display;
762 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400763 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000764#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100765 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000766#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500767 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100768
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400769 if (display->shell)
770 window_set_type(window);
771
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000773#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100774 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
775 data = cairo_surface_get_user_data(window->cairo_surface,
776 &surface_data_key);
777
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778 cairo_gl_surface_swapbuffers(window->cairo_surface);
779 wl_egl_window_get_attached_size(data->window,
780 &window->server_allocation.width,
781 &window->server_allocation.height);
782 break;
783 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000784#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 window_get_resize_dx_dy(window, &x, &y);
787
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 if (window->pending_surface != NULL)
789 return;
790
791 window->pending_surface = window->cairo_surface;
792 window->cairo_surface = NULL;
793
794 buffer =
795 display_get_buffer_for_surface(display,
796 window->pending_surface);
797
798 wl_surface_attach(window->surface, buffer, x, y);
799 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400800 cb = wl_display_sync(display->display);
801 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000803 default:
804 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500806
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500807 wl_surface_damage(window->surface, 0, 0,
808 window->allocation.width,
809 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500810}
811
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500812void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400813window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500814{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100815 if (window->cairo_surface) {
816 switch (window->buffer_type) {
817 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
818 case WINDOW_BUFFER_TYPE_SHM:
819 display_surface_damage(window->display,
820 window->cairo_surface,
821 0, 0,
822 window->allocation.width,
823 window->allocation.height);
824 break;
825 default:
826 break;
827 }
828 window_attach_surface(window);
829 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500830}
831
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400832void
833window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500835 cairo_surface_reference(surface);
836
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400837 if (window->cairo_surface != NULL)
838 cairo_surface_destroy(window->cairo_surface);
839
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500840 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400841}
842
Benjamin Franzke22d54812011-07-16 19:50:32 +0000843#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844static void
845window_resize_cairo_window_surface(struct window *window)
846{
847 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200848 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849
850 data = cairo_surface_get_user_data(window->cairo_surface,
851 &surface_data_key);
852
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200853 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 wl_egl_window_resize(data->window,
855 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200856 window->allocation.height,
857 x,y);
858
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 cairo_gl_surface_set_size(window->cairo_surface,
860 window->allocation.width,
861 window->allocation.height);
862}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000863#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400865void
866window_create_surface(struct window *window)
867{
868 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400869 uint32_t flags = 0;
870
871 if (!window->transparent)
872 flags = SURFACE_OPAQUE;
873
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500875#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
877 if (window->cairo_surface) {
878 window_resize_cairo_window_surface(window);
879 return;
880 }
881 surface = display_create_surface(window->display,
882 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400883 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100884 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500885 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400888 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889 break;
890#endif
891 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400893 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800895 default:
896 surface = NULL;
897 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400898 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400899
900 window_set_surface(window, surface);
901 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400902}
903
904static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500905window_draw_menu(struct window *window)
906{
907 cairo_t *cr;
908 int width, height, r = 5;
909
910 window_create_surface(window);
911
912 cr = cairo_create(window->cairo_surface);
913 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
914 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
915 cairo_paint(cr);
916
917 width = window->allocation.width;
918 height = window->allocation.height;
919 rounded_rect(cr, r, r, width - r, height - r, r);
920 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
921 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
922 cairo_fill(cr);
923 cairo_destroy(cr);
924}
925
926static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500927window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500928{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500929 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500930 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400931 cairo_surface_t *frame;
932 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500933
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400934 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400935
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400936 width = window->allocation.width;
937 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500938
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500939 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500940
Kristian Høgsberg09531622010-06-14 23:22:15 -0400941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400942 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400943 cairo_paint(cr);
944
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400945 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400946 tile_mask(cr, window->display->shadow,
947 shadow_dx, shadow_dy, width, height,
948 window->margin + 10 - shadow_dx,
949 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500950
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400951 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400952 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400953 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400954 frame = window->display->inactive_frame;
955
956 tile_source(cr, frame, 0, 0, width, height,
957 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500958
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500959 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
960 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500961 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400962 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400963 if (window->keyboard_device)
964 cairo_set_source_rgb(cr, 0, 0, 0);
965 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400966 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400967 cairo_show_text(cr, window->title);
968
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500969 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400970
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100971 /* FIXME: this breakes gears, fix cairo? */
972#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400973 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100974#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500975}
976
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400977void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500978window_destroy(struct window *window)
979{
980 wl_surface_destroy(window->surface);
981 wl_list_remove(&window->link);
982 free(window);
983}
984
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400985static struct item *
986window_find_item(struct window *window, int32_t x, int32_t y)
987{
988 struct item *item;
989
990 wl_list_for_each(item, &window->item_list, link) {
991 if (item->allocation.x <= x &&
992 x < item->allocation.x + item->allocation.width &&
993 item->allocation.y <= y &&
994 y < item->allocation.y + item->allocation.height) {
995 return item;
996 }
997 }
998
999 return NULL;
1000}
1001
1002struct item *
1003window_add_item(struct window *window, void *data)
1004{
1005 struct item *item;
1006
1007 item = malloc(sizeof *item);
1008 memset(item, 0, sizeof *item);
1009 item->user_data = data;
1010 wl_list_insert(window->item_list.prev, &item->link);
1011
1012 return item;
1013}
1014
1015void
1016window_for_each_item(struct window *window, item_func_t func, void *data)
1017{
1018 struct item *item;
1019
1020 wl_list_for_each(item, &window->item_list, link)
1021 func(item, data);
1022}
1023
1024struct item *
1025window_get_focus_item(struct window *window)
1026{
1027 return window->focus_item;
1028}
1029
1030void
1031item_get_allocation(struct item *item, struct rectangle *allocation)
1032{
1033 *allocation = item->allocation;
1034}
1035
1036void
1037item_set_allocation(struct item *item,
1038 int32_t x, int32_t y, int32_t width, int32_t height)
1039{
1040 item->allocation.x = x;
1041 item->allocation.y = y;
1042 item->allocation.width = width;
1043 item->allocation.height = height;
1044}
1045
1046void *
1047item_get_user_data(struct item *item)
1048{
1049 return item->user_data;
1050}
1051
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001052void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001053window_draw(struct window *window)
1054{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001055 if (window->parent)
1056 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001057 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001058 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001059 else
1060 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001061}
1062
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001063cairo_surface_t *
1064window_get_surface(struct window *window)
1065{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001066 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001067}
1068
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001069struct wl_surface *
1070window_get_wl_surface(struct window *window)
1071{
1072 return window->surface;
1073}
1074
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001075static int
1076get_pointer_location(struct window *window, int32_t x, int32_t y)
1077{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001078 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001079 const int grip_size = 8;
1080
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001081 if (!window->decoration)
1082 return WINDOW_CLIENT_AREA;
1083
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001084 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001085 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001086 else if (window->margin <= x && x < window->margin + grip_size)
1087 hlocation = WINDOW_RESIZING_LEFT;
1088 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001089 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001090 else if (x < window->allocation.width - window->margin)
1091 hlocation = WINDOW_RESIZING_RIGHT;
1092 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001093 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001094
1095 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001096 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001097 else if (window->margin <= y && y < window->margin + grip_size)
1098 vlocation = WINDOW_RESIZING_TOP;
1099 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001100 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001101 else if (y < window->allocation.height - window->margin)
1102 vlocation = WINDOW_RESIZING_BOTTOM;
1103 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001104 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001105
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001106 location = vlocation | hlocation;
1107 if (location & WINDOW_EXTERIOR)
1108 location = WINDOW_EXTERIOR;
1109 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1110 location = WINDOW_TITLEBAR;
1111 else if (location == WINDOW_INTERIOR)
1112 location = WINDOW_CLIENT_AREA;
1113
1114 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115}
1116
1117static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001118set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119{
1120 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001121 struct wl_buffer *buffer;
1122 cairo_surface_t *surface;
1123 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124
1125 location = get_pointer_location(input->pointer_focus,
1126 input->sx, input->sy);
1127 switch (location) {
1128 case WINDOW_RESIZING_TOP:
1129 pointer = POINTER_TOP;
1130 break;
1131 case WINDOW_RESIZING_BOTTOM:
1132 pointer = POINTER_BOTTOM;
1133 break;
1134 case WINDOW_RESIZING_LEFT:
1135 pointer = POINTER_LEFT;
1136 break;
1137 case WINDOW_RESIZING_RIGHT:
1138 pointer = POINTER_RIGHT;
1139 break;
1140 case WINDOW_RESIZING_TOP_LEFT:
1141 pointer = POINTER_TOP_LEFT;
1142 break;
1143 case WINDOW_RESIZING_TOP_RIGHT:
1144 pointer = POINTER_TOP_RIGHT;
1145 break;
1146 case WINDOW_RESIZING_BOTTOM_LEFT:
1147 pointer = POINTER_BOTTOM_LEFT;
1148 break;
1149 case WINDOW_RESIZING_BOTTOM_RIGHT:
1150 pointer = POINTER_BOTTOM_RIGHT;
1151 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001152 case WINDOW_EXTERIOR:
1153 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001154 if (input->current_pointer_image == POINTER_DEFAULT)
1155 return;
1156
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001157 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001158 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001159 return;
1160 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161 break;
1162 }
1163
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001164 if (pointer == input->current_pointer_image)
1165 return;
1166
1167 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001168 surface = display->pointer_surfaces[pointer];
1169 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001170 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171 pointer_images[pointer].hotspot_x,
1172 pointer_images[pointer].hotspot_y);
1173}
1174
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001175static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001176window_set_focus_item(struct window *window, struct item *focus)
1177{
1178 void *data;
1179
1180 if (focus == window->focus_item)
1181 return;
1182
1183 window->focus_item = focus;
1184 data = focus ? focus->user_data : NULL;
1185 if (window->item_focus_handler)
1186 window->item_focus_handler(window, focus, data);
1187}
1188
1189static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001190window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001191 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001192 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001193{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001194 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001196 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001197 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001198
1199 input->x = x;
1200 input->y = y;
1201 input->sx = sx;
1202 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001204 if (!window->focus_item || !window->item_grab_button) {
1205 item = window_find_item(window, sx, sy);
1206 window_set_focus_item(window, item);
1207 }
1208
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001209 if (window->motion_handler)
1210 pointer = (*window->motion_handler)(window, input, time,
1211 x, y, sx, sy,
1212 window->user_data);
1213
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001214 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001215}
1216
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001217static void
1218window_handle_button(void *data,
1219 struct wl_input_device *input_device,
1220 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001221{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001222 struct input *input = data;
1223 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001224 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001226
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001227 if (window->focus_item && window->item_grab_button == 0 && state)
1228 window->item_grab_button = button;
1229
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001230 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001231
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001232 if (window->display->shell &&
1233 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001236 wl_shell_move(window->display->shell,
1237 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001238 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001239 case WINDOW_RESIZING_TOP:
1240 case WINDOW_RESIZING_BOTTOM:
1241 case WINDOW_RESIZING_LEFT:
1242 case WINDOW_RESIZING_RIGHT:
1243 case WINDOW_RESIZING_TOP_LEFT:
1244 case WINDOW_RESIZING_TOP_RIGHT:
1245 case WINDOW_RESIZING_BOTTOM_LEFT:
1246 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001247 wl_shell_resize(window->display->shell,
1248 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001250 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001251 case WINDOW_CLIENT_AREA:
1252 if (window->button_handler)
1253 (*window->button_handler)(window,
1254 input, time,
1255 button, state,
1256 window->user_data);
1257 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001258 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 } else {
1260 if (window->button_handler)
1261 (*window->button_handler)(window,
1262 input, time,
1263 button, state,
1264 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001265 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001266
1267 if (window->focus_item &&
1268 window->item_grab_button == button && !state) {
1269 window->item_grab_button = 0;
1270 item = window_find_item(window, input->sx, input->sy);
1271 window_set_focus_item(window, item);
1272 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001273}
1274
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001275static void
1276window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001277 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001278{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001279 struct input *input = data;
1280 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001281 struct display *d = window->display;
1282 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001283
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001284 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001285 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001286 return;
1287
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001288 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001289 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001290 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1291 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001292
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001293 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1294
1295 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001296 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001297 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001298 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001299
1300 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001301 (*window->key_handler)(window, input, time, key, sym, state,
1302 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001303}
1304
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001305static void
1306window_handle_pointer_focus(void *data,
1307 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001308 uint32_t time, struct wl_surface *surface,
1309 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001310{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001311 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001312 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001313 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001314 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001315
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001316 window = input->pointer_focus;
1317 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001318 window_set_focus_item(window, NULL);
1319
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001320 if (window->leave_handler)
1321 window->leave_handler(window, input,
1322 time, window->user_data);
1323 input->pointer_focus = NULL;
1324 input->current_pointer_image = POINTER_UNSET;
1325 }
1326
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001327 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001328 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001329 window = input->pointer_focus;
1330
Kristian Høgsberg59826582011-01-20 11:56:57 -05001331 input->x = x;
1332 input->y = y;
1333 input->sx = sx;
1334 input->sy = sy;
1335
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001336 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001337 if (window->enter_handler)
1338 pointer = window->enter_handler(window, input,
1339 time, sx, sy,
1340 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001341
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001342 item = window_find_item(window, x, y);
1343 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001344
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001345 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001346 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001347}
1348
1349static void
1350window_handle_keyboard_focus(void *data,
1351 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001352 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001353 struct wl_surface *surface,
1354 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001355{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001356 struct input *input = data;
1357 struct window *window = input->keyboard_focus;
1358 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001359 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001360
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001361 window = input->keyboard_focus;
1362 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001363 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001364 if (window->keyboard_focus_handler)
1365 (*window->keyboard_focus_handler)(window, NULL,
1366 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001367 }
1368
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001369 if (surface)
1370 input->keyboard_focus = wl_surface_get_user_data(surface);
1371 else
1372 input->keyboard_focus = NULL;
1373
1374 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001375 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001376 for (k = keys->data; k < end; k++)
1377 input->modifiers |= d->xkb->map->modmap[*k];
1378
1379 window = input->keyboard_focus;
1380 if (window) {
1381 window->keyboard_device = input;
1382 if (window->keyboard_focus_handler)
1383 (*window->keyboard_focus_handler)(window,
1384 window->keyboard_device,
1385 window->user_data);
1386 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001387}
1388
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001389static const struct wl_input_device_listener input_device_listener = {
1390 window_handle_motion,
1391 window_handle_button,
1392 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001393 window_handle_pointer_focus,
1394 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001395};
1396
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001397void
1398input_get_position(struct input *input, int32_t *x, int32_t *y)
1399{
1400 *x = input->sx;
1401 *y = input->sy;
1402}
1403
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001404struct wl_input_device *
1405input_get_input_device(struct input *input)
1406{
1407 return input->input_device;
1408}
1409
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001410uint32_t
1411input_get_modifiers(struct input *input)
1412{
1413 return input->modifiers;
1414}
1415
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001416struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001417window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001418{
Benjamin Franzke0c991632011-09-27 21:57:31 +02001419 cairo_device_flush (window->display->rgb_device);
1420 cairo_device_flush (window->display->argb_device);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001421
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001422 return wl_shell_create_drag(window->display->shell);
1423}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001424
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001425void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001426window_move(struct window *window, struct input *input, uint32_t time)
1427{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001428 if (window->display->shell)
1429 wl_shell_move(window->display->shell,
1430 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001431}
1432
1433void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001434window_activate_drag(struct wl_drag *drag, struct window *window,
1435 struct input *input, uint32_t time)
1436{
1437 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001438}
1439
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001440static void
1441handle_configure(void *data, struct wl_shell *shell,
1442 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001443 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001444{
1445 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001446 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001447
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001448 /* FIXME: this is probably the wrong place to check for width
1449 * or height <= 0, but it prevents the compositor from crashing
1450 */
1451 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001452 return;
1453
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001454 window->resize_edges = edges;
1455
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001456 if (window->resize_handler) {
1457 child_width = width - 20 - window->margin * 2;
1458 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001459
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001460 (*window->resize_handler)(window,
1461 child_width, child_height,
1462 window->user_data);
1463 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001464 window->allocation.width = width;
1465 window->allocation.height = height;
1466
1467 if (window->redraw_handler)
1468 window_schedule_redraw(window);
1469 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001470}
1471
1472static const struct wl_shell_listener shell_listener = {
1473 handle_configure,
1474};
1475
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001476void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001477window_get_allocation(struct window *window,
1478 struct rectangle *allocation)
1479{
1480 *allocation = window->allocation;
1481}
1482
1483void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001484window_get_child_allocation(struct window *window,
1485 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001486{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001487 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001488 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001489 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001490 allocation->x = window->margin + 10;
1491 allocation->y = window->margin + 50;
1492 allocation->width =
1493 window->allocation.width - 20 - window->margin * 2;
1494 allocation->height =
1495 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001496 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001497}
1498
1499void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001500window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001501{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001502 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001503 window->allocation.x = 20 + window->margin;
1504 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001505 window->allocation.width = width + 20 + window->margin * 2;
1506 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001507 } else {
1508 window->allocation.x = 0;
1509 window->allocation.y = 0;
1510 window->allocation.width = width;
1511 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001512 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001513}
1514
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001515static void
1516idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001517{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001518 struct window *window =
1519 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001520
1521 window->redraw_handler(window, window->user_data);
1522 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001523}
1524
1525void
1526window_schedule_redraw(struct window *window)
1527{
1528 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001529 window->redraw_task.run = idle_redraw;
1530 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001531 window->redraw_scheduled = 1;
1532 }
1533}
1534
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001535void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001536window_set_custom(struct window *window)
1537{
1538 window->type = TYPE_CUSTOM;
1539}
1540
1541void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001542window_set_fullscreen(struct window *window, int fullscreen)
1543{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001544 int32_t width, height;
1545
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001546 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001547 return;
1548
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001549 if (fullscreen) {
1550 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001551 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001552 width = window->display->screen_allocation.width;
1553 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001554 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001555 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001556 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001557 width = window->saved_allocation.width - 20 - window->margin * 2;
1558 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001559 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001560 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001561
1562 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001563}
1564
1565void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001566window_set_decoration(struct window *window, int decoration)
1567{
1568 window->decoration = decoration;
1569}
1570
1571void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001572window_set_user_data(struct window *window, void *data)
1573{
1574 window->user_data = data;
1575}
1576
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001577void *
1578window_get_user_data(struct window *window)
1579{
1580 return window->user_data;
1581}
1582
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001583void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001584window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001585 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001586{
1587 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001588}
1589
1590void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001591window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001592 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001593{
1594 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001595}
1596
1597void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001598window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001599 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001600{
1601 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001602}
1603
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001604void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001605window_set_button_handler(struct window *window,
1606 window_button_handler_t handler)
1607{
1608 window->button_handler = handler;
1609}
1610
1611void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001612window_set_motion_handler(struct window *window,
1613 window_motion_handler_t handler)
1614{
1615 window->motion_handler = handler;
1616}
1617
1618void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001619window_set_enter_handler(struct window *window,
1620 window_enter_handler_t handler)
1621{
1622 window->enter_handler = handler;
1623}
1624
1625void
1626window_set_leave_handler(struct window *window,
1627 window_leave_handler_t handler)
1628{
1629 window->leave_handler = handler;
1630}
1631
1632void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001633window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001634 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001635{
1636 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001637}
1638
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001639void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001640window_set_item_focus_handler(struct window *window,
1641 window_item_focus_handler_t handler)
1642{
1643 window->item_focus_handler = handler;
1644}
1645
1646void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001647window_set_transparent(struct window *window, int transparent)
1648{
1649 window->transparent = transparent;
1650}
1651
1652void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001653window_set_title(struct window *window, const char *title)
1654{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001655 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001656 window->title = strdup(title);
1657}
1658
1659const char *
1660window_get_title(struct window *window)
1661{
1662 return window->title;
1663}
1664
1665void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001666display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1667 int32_t x, int32_t y, int32_t width, int32_t height)
1668{
1669 struct wl_buffer *buffer;
1670
1671 buffer = display_get_buffer_for_surface(display, cairo_surface);
1672
1673 wl_buffer_damage(buffer, x, y, width, height);
1674}
1675
1676void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001677window_damage(struct window *window, int32_t x, int32_t y,
1678 int32_t width, int32_t height)
1679{
1680 wl_surface_damage(window->surface, x, y, width, height);
1681}
1682
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001683static struct window *
1684window_create_internal(struct display *display, struct window *parent,
1685 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001686{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001687 struct window *window;
1688
1689 window = malloc(sizeof *window);
1690 if (window == NULL)
1691 return NULL;
1692
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001693 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001694 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001695 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001696 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001697 window->allocation.x = 0;
1698 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001699 window->allocation.width = width;
1700 window->allocation.height = height;
1701 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001702 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001703 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001704 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001706
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001707 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001708#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001709 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1710 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001711#else
1712 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1713#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001714 else
1715 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001716
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001717 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001718 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001719
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001720 return window;
1721}
1722
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001723struct window *
1724window_create(struct display *display, int32_t width, int32_t height)
1725{
1726 struct window *window;
1727
1728 window = window_create_internal(display, NULL, width, height);
1729 if (!window)
1730 return NULL;
1731
1732 return window;
1733}
1734
1735struct window *
1736window_create_transient(struct display *display, struct window *parent,
1737 int32_t x, int32_t y, int32_t width, int32_t height)
1738{
1739 struct window *window;
1740
1741 window = window_create_internal(parent->display,
1742 parent, width, height);
1743 if (!window)
1744 return NULL;
1745
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001746 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001747 window->x = x;
1748 window->y = y;
1749
1750 return window;
1751}
1752
1753void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001754window_set_buffer_type(struct window *window, enum window_buffer_type type)
1755{
1756 window->buffer_type = type;
1757}
1758
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001759
1760static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001761display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001762 struct wl_output *wl_output,
1763 int x, int y,
1764 int physical_width,
1765 int physical_height,
1766 int subpixel,
1767 const char *make,
1768 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001769{
1770 struct display *display = data;
1771
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001772 display->screen_allocation.x = x;
1773 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001774}
1775
1776static void
1777display_handle_mode(void *data,
1778 struct wl_output *wl_output,
1779 uint32_t flags,
1780 int width,
1781 int height,
1782 int refresh)
1783{
1784 struct display *display = data;
1785
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001786 display->screen_allocation.width = width;
1787 display->screen_allocation.height = height;
1788}
1789
1790static const struct wl_output_listener output_listener = {
1791 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001792 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001793};
1794
1795static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001796display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001797{
1798 struct input *input;
1799
1800 input = malloc(sizeof *input);
1801 if (input == NULL)
1802 return;
1803
1804 memset(input, 0, sizeof *input);
1805 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001806 input->input_device =
1807 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001808 input->pointer_focus = NULL;
1809 input->keyboard_focus = NULL;
1810 wl_list_insert(d->input_list.prev, &input->link);
1811
1812 wl_input_device_add_listener(input->input_device,
1813 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001814 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001815}
1816
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001817struct selection_offer {
1818 struct display *display;
1819 struct wl_selection_offer *offer;
1820 struct wl_array types;
1821 struct input *input;
1822};
1823
1824int
1825input_offers_mime_type(struct input *input, const char *type)
1826{
1827 struct selection_offer *offer = input->offer;
1828 char **p, **end;
1829
1830 if (offer == NULL)
1831 return 0;
1832
1833 end = offer->types.data + offer->types.size;
1834 for (p = offer->types.data; p < end; p++)
1835 if (strcmp(*p, type) == 0)
1836 return 1;
1837
1838 return 0;
1839}
1840
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001841void
1842input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001843{
1844 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001845
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001846 /* FIXME: A number of things can go wrong here: the object may
1847 * not be the current selection offer any more (which could
1848 * still work, but the source may have gone away or just
1849 * destroyed its wl_selection) or the offer may not have the
1850 * requested type after all (programmer/client error,
1851 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001852 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001853}
1854
1855static void
1856selection_offer_offer(void *data,
1857 struct wl_selection_offer *selection_offer,
1858 const char *type)
1859{
1860 struct selection_offer *offer = data;
1861
1862 char **p;
1863
1864 p = wl_array_add(&offer->types, sizeof *p);
1865 if (p)
1866 *p = strdup(type);
1867};
1868
1869static void
1870selection_offer_keyboard_focus(void *data,
1871 struct wl_selection_offer *selection_offer,
1872 struct wl_input_device *input_device)
1873{
1874 struct selection_offer *offer = data;
1875 struct input *input;
1876 char **p, **end;
1877
1878 if (input_device == NULL) {
1879 printf("selection offer retracted %p\n", selection_offer);
1880 input = offer->input;
1881 input->offer = NULL;
1882 wl_selection_offer_destroy(selection_offer);
1883 wl_array_release(&offer->types);
1884 free(offer);
1885 return;
1886 }
1887
1888 input = wl_input_device_get_user_data(input_device);
1889 printf("new selection offer %p:", selection_offer);
1890
1891 offer->input = input;
1892 input->offer = offer;
1893 end = offer->types.data + offer->types.size;
1894 for (p = offer->types.data; p < end; p++)
1895 printf(" %s", *p);
1896
1897 printf("\n");
1898}
1899
1900struct wl_selection_offer_listener selection_offer_listener = {
1901 selection_offer_offer,
1902 selection_offer_keyboard_focus
1903};
1904
1905static void
1906add_selection_offer(struct display *d, uint32_t id)
1907{
1908 struct selection_offer *offer;
1909
1910 offer = malloc(sizeof *offer);
1911 if (offer == NULL)
1912 return;
1913
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001914 offer->offer =
1915 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001916 offer->display = d;
1917 wl_array_init(&offer->types);
1918 offer->input = NULL;
1919
1920 wl_selection_offer_add_listener(offer->offer,
1921 &selection_offer_listener, offer);
1922}
1923
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001924static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001925display_handle_global(struct wl_display *display, uint32_t id,
1926 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001927{
1928 struct display *d = data;
1929
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001930 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001931 d->compositor =
1932 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001933 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001934 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001935 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001936 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001937 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001938 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001939 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001940 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001941 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001942 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001943 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001944 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001945 }
1946}
1947
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001948static void
1949display_render_frame(struct display *d)
1950{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001951 int radius = 8;
1952 cairo_t *cr;
1953
1954 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1955 cr = cairo_create(d->shadow);
1956 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1957 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1958 rounded_rect(cr, 16, 16, 112, 112, radius);
1959 cairo_fill(cr);
1960 cairo_destroy(cr);
1961 blur_surface(d->shadow, 64);
1962
1963 d->active_frame =
1964 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1965 cr = cairo_create(d->active_frame);
1966 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1967 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1968 rounded_rect(cr, 16, 16, 112, 112, radius);
1969 cairo_fill(cr);
1970 cairo_destroy(cr);
1971
1972 d->inactive_frame =
1973 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1974 cr = cairo_create(d->inactive_frame);
1975 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1976 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1977 rounded_rect(cr, 16, 16, 112, 112, radius);
1978 cairo_fill(cr);
1979 cairo_destroy(cr);
1980}
1981
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001982static void
1983init_xkb(struct display *d)
1984{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001985 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001986
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001987 names.rules = "evdev";
1988 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001989 names.layout = option_xkb_layout;
1990 names.variant = option_xkb_variant;
1991 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001992
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001993 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001994 if (!d->xkb) {
1995 fprintf(stderr, "Failed to compile keymap\n");
1996 exit(1);
1997 }
1998}
1999
Yuval Fledel45568f62010-12-06 09:18:12 -05002000static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002001init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002002{
2003 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002004 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002005
2006 static const EGLint premul_argb_cfg_attribs[] = {
2007 EGL_SURFACE_TYPE,
2008 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2009 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002010 EGL_RED_SIZE, 1,
2011 EGL_GREEN_SIZE, 1,
2012 EGL_BLUE_SIZE, 1,
2013 EGL_ALPHA_SIZE, 1,
2014 EGL_DEPTH_SIZE, 1,
2015 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2016 EGL_NONE
2017 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002018
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002019 static const EGLint rgb_cfg_attribs[] = {
2020 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2021 EGL_RED_SIZE, 1,
2022 EGL_GREEN_SIZE, 1,
2023 EGL_BLUE_SIZE, 1,
2024 EGL_ALPHA_SIZE, 0,
2025 EGL_DEPTH_SIZE, 1,
2026 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2027 EGL_NONE
2028 };
2029
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002030 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002031 if (!eglInitialize(d->dpy, &major, &minor)) {
2032 fprintf(stderr, "failed to initialize display\n");
2033 return -1;
2034 }
2035
2036 if (!eglBindAPI(EGL_OPENGL_API)) {
2037 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2038 return -1;
2039 }
2040
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002041 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002042 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002043 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002044 return -1;
2045 }
2046
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002047 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2048 &d->rgb_config, 1, &n) || n != 1) {
2049 fprintf(stderr, "failed to choose rgb config\n");
2050 return -1;
2051 }
2052
Benjamin Franzke0c991632011-09-27 21:57:31 +02002053 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2054 if (d->rgb_ctx == NULL) {
2055 fprintf(stderr, "failed to create context\n");
2056 return -1;
2057 }
2058 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2059 EGL_NO_CONTEXT, NULL);
2060 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002061 fprintf(stderr, "failed to create context\n");
2062 return -1;
2063 }
2064
Benjamin Franzke0c991632011-09-27 21:57:31 +02002065 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002066 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002067 return -1;
2068 }
2069
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002070#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002071 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2072 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002073 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002074 return -1;
2075 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002076 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2077 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2078 fprintf(stderr, "failed to get cairo egl argb device\n");
2079 return -1;
2080 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002081#endif
2082
2083 return 0;
2084}
2085
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002086static int
2087event_mask_update(uint32_t mask, void *data)
2088{
2089 struct display *d = data;
2090
2091 d->mask = mask;
2092
2093 return 0;
2094}
2095
2096static void
2097handle_display_data(struct task *task, uint32_t events)
2098{
2099 struct display *display =
2100 container_of(task, struct display, display_task);
2101
2102 wl_display_iterate(display->display, display->mask);
2103}
2104
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002105struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002106display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002107{
2108 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002109 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002110 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002111 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002112
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002113 g_type_init();
2114
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002115 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002116 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002117 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002118
2119 xkb_option_group = g_option_group_new("xkb",
2120 "Keyboard options",
2121 "Show all XKB options",
2122 NULL, NULL);
2123 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2124 g_option_context_add_group (context, xkb_option_group);
2125
2126 if (!g_option_context_parse(context, argc, argv, &error)) {
2127 fprintf(stderr, "option parsing failed: %s\n", error->message);
2128 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002129 }
2130
Tim Wiederhake748f6722011-01-23 23:25:25 +01002131 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002133 d = malloc(sizeof *d);
2134 if (d == NULL)
2135 return NULL;
2136
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002137 memset(d, 0, sizeof *d);
2138
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002139 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002140 if (d->display == NULL) {
2141 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002142 return NULL;
2143 }
2144
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002145 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2146 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2147 d->display_task.run = handle_display_data;
2148 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2149
2150 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002151 wl_list_init(&d->input_list);
2152
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002153 /* Set up listener so we'll catch all events. */
2154 wl_display_add_global_listener(d->display,
2155 display_handle_global, d);
2156
2157 /* Process connection events. */
2158 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002159 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002160 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002161
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002162 d->image_target_texture_2d =
2163 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2164 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2165 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2166
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002167 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002168
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002169 display_render_frame(d);
2170
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002171 wl_list_init(&d->window_list);
2172
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002173 init_xkb(d);
2174
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002175 return d;
2176}
2177
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002178struct wl_display *
2179display_get_display(struct display *display)
2180{
2181 return display->display;
2182}
2183
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002184struct wl_compositor *
2185display_get_compositor(struct display *display)
2186{
2187 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002188}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002189
2190EGLDisplay
2191display_get_egl_display(struct display *d)
2192{
2193 return d->dpy;
2194}
2195
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002196EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002197display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002198{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002199 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002200}
2201
Benjamin Franzke0c991632011-09-27 21:57:31 +02002202EGLConfig
2203display_get_argb_egl_config(struct display *d)
2204{
2205 return d->premultiplied_argb_config;
2206}
2207
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002208struct wl_shell *
2209display_get_shell(struct display *display)
2210{
2211 return display->shell;
2212}
2213
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002214void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002215display_acquire_window_surface(struct display *display,
2216 struct window *window,
2217 EGLContext ctx)
2218{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002219#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002220 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002221 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002222
2223 if (!window->cairo_surface)
2224 return;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002225 device = cairo_surface_get_device(window->cairo_surface);
2226 if (!device)
2227 return;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002228
Benjamin Franzke0c991632011-09-27 21:57:31 +02002229 if (!ctx) {
2230 if (device == display->rgb_device)
2231 ctx = display->rgb_ctx;
2232 else if (device == display->argb_device)
2233 ctx = display->argb_ctx;
2234 else
2235 assert(0);
2236 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002237
2238 data = cairo_surface_get_user_data(window->cairo_surface,
2239 &surface_data_key);
2240
Benjamin Franzke0c991632011-09-27 21:57:31 +02002241 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002242 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2243 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002244#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002245}
2246
2247void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002248display_release_window_surface(struct display *display,
2249 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002250{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002251#ifdef HAVE_CAIRO_EGL
2252 cairo_device_t *device;
2253
2254 device = cairo_surface_get_device(window->cairo_surface);
2255 if (!device)
2256 return;
2257
2258 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002259 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002260 cairo_device_release(device);
2261#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002262}
2263
2264void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002265display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002266{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002267 wl_list_insert(&display->deferred_list, &task->link);
2268}
2269
2270void
2271display_watch_fd(struct display *display,
2272 int fd, uint32_t events, struct task *task)
2273{
2274 struct epoll_event ep;
2275
2276 ep.events = events;
2277 ep.data.ptr = task;
2278 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2279}
2280
2281void
2282display_run(struct display *display)
2283{
2284 struct task *task;
2285 struct epoll_event ep[16];
2286 int i, count;
2287
2288 while (1) {
2289 while (display->mask & WL_DISPLAY_WRITABLE)
2290 wl_display_iterate(display->display,
2291 WL_DISPLAY_WRITABLE);
2292
2293 count = epoll_wait(display->epoll_fd,
2294 ep, ARRAY_LENGTH(ep), -1);
2295 for (i = 0; i < count; i++) {
2296 task = ep[i].data.ptr;
2297 task->run(task, ep[i].events);
2298 }
2299
2300 while (!wl_list_empty(&display->deferred_list)) {
2301 task = container_of(display->deferred_list.next,
2302 struct task, link);
2303 wl_list_remove(&task->link);
2304 task->run(task, 0);
2305 }
2306 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002307}