blob: 16322ff62508a44f3a80bb2478aeff7345d0752c [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 *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400117 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500118
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400119 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500120 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500121
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500122 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400123 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500124 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400125 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500126 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400127 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400128 window_enter_handler_t enter_handler;
129 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400130 window_item_focus_handler_t item_focus_handler;
131
132 struct wl_list item_list;
133 struct item *focus_item;
134 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400135
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500136 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400137 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500138};
139
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140struct item {
141 struct wl_list link;
142 struct rectangle allocation;
143 void *user_data;
144};
145
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400146struct input {
147 struct display *display;
148 struct wl_input_device *input_device;
149 struct window *pointer_focus;
150 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500151 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400152 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400153 uint32_t modifiers;
154 int32_t x, y, sx, sy;
155 struct wl_list link;
156};
157
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400158enum {
159 POINTER_DEFAULT = 100,
160 POINTER_UNSET
161};
162
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500163enum window_location {
164 WINDOW_INTERIOR = 0,
165 WINDOW_RESIZING_TOP = 1,
166 WINDOW_RESIZING_BOTTOM = 2,
167 WINDOW_RESIZING_LEFT = 4,
168 WINDOW_RESIZING_TOP_LEFT = 5,
169 WINDOW_RESIZING_BOTTOM_LEFT = 6,
170 WINDOW_RESIZING_RIGHT = 8,
171 WINDOW_RESIZING_TOP_RIGHT = 9,
172 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
173 WINDOW_RESIZING_MASK = 15,
174 WINDOW_EXTERIOR = 16,
175 WINDOW_TITLEBAR = 17,
176 WINDOW_CLIENT_AREA = 18,
177};
178
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400179const char *option_xkb_layout = "us";
180const char *option_xkb_variant = "";
181const char *option_xkb_options = "";
182
183static const GOptionEntry xkb_option_entries[] = {
184 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
185 &option_xkb_layout, "XKB Layout" },
186 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
187 &option_xkb_variant, "XKB Variant" },
188 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
189 &option_xkb_options, "XKB Options" },
190 { NULL }
191};
192
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400193static const cairo_user_data_key_t surface_data_key;
194struct surface_data {
195 struct wl_buffer *buffer;
196};
197
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500198#define MULT(_d,c,a,t) \
199 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
200
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500201#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400202
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100203struct egl_window_surface_data {
204 struct display *display;
205 struct wl_surface *surface;
206 struct wl_egl_window *window;
207 EGLSurface surf;
208};
209
210static void
211egl_window_surface_data_destroy(void *p)
212{
213 struct egl_window_surface_data *data = p;
214 struct display *d = data->display;
215
216 eglDestroySurface(d->dpy, data->surf);
217 wl_egl_window_destroy(data->window);
218 data->surface = NULL;
219
220 free(p);
221}
222
223static cairo_surface_t *
224display_create_egl_window_surface(struct display *display,
225 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400226 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100227 struct rectangle *rectangle)
228{
229 cairo_surface_t *cairo_surface;
230 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400231 EGLConfig config;
232 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200233 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100234
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400235 static const EGLint premul_attribs[] = {
236 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
237 EGL_NONE
238 };
239
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100240 data = malloc(sizeof *data);
241 if (data == NULL)
242 return NULL;
243
244 data->display = display;
245 data->surface = surface;
246
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400247 if (flags & SURFACE_OPAQUE) {
248 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200249 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200250 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400251 } else {
252 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200253 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400254 attribs = premul_attribs;
255 }
256
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400257 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100258 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400259 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100260
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400261 data->surf = eglCreateWindowSurface(display->dpy, config,
262 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100263
Benjamin Franzke0c991632011-09-27 21:57:31 +0200264 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100265 data->surf,
266 rectangle->width,
267 rectangle->height);
268
269 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
270 data, egl_window_surface_data_destroy);
271
272 return cairo_surface;
273}
274
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400276 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200277 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400278 EGLImageKHR image;
279 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800280 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500281 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400282};
283
284static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500285egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400286{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500287 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800288 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400289
Benjamin Franzke0c991632011-09-27 21:57:31 +0200290 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400291 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200292 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800293
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500294 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400295 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500296 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500297 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400298}
299
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500300EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500301display_get_image_for_egl_image_surface(struct display *display,
302 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500303{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500304 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500305
306 data = cairo_surface_get_user_data (surface, &surface_data_key);
307
308 return data->image;
309}
310
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500311static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400313 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500314 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317 EGLDisplay dpy = display->dpy;
318 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400319 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200320 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321
322 data = malloc(sizeof *data);
323 if (data == NULL)
324 return NULL;
325
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326 data->display = display;
327
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400328 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400329 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500330 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000331 free(data);
332 return NULL;
333 }
334
Benjamin Franzke0c991632011-09-27 21:57:31 +0200335 if (flags & SURFACE_OPAQUE) {
336 data->device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400337 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200338 content = CAIRO_CONTENT_COLOR;
339 } else {
340 data->device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400341 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200342 content = CAIRO_CONTENT_COLOR_ALPHA;
343 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400344
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500345 data->image = display->create_image(dpy, NULL,
346 EGL_NATIVE_PIXMAP_KHR,
347 (EGLClientBuffer) data->pixmap,
348 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500350 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 free(data);
352 return NULL;
353 }
354
355 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400356 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500357
Benjamin Franzke0c991632011-09-27 21:57:31 +0200358 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500361 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200362 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400363
Benjamin Franzke0c991632011-09-27 21:57:31 +0200364 surface = cairo_gl_surface_create_for_texture(data->device,
365 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400366 data->texture,
367 rectangle->width,
368 rectangle->height);
369
370 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500371 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400372
373 return surface;
374}
375
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500376static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500377display_create_egl_image_surface_from_file(struct display *display,
378 const char *filename,
379 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400380{
381 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400382 GdkPixbuf *pixbuf;
383 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400384 int stride, i;
385 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500386 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400387
388 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400390 FALSE, &error);
391 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400393
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400394 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
395 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500396 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400398 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400399
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400400
401 stride = gdk_pixbuf_get_rowstride(pixbuf);
402 pixels = gdk_pixbuf_get_pixels(pixbuf);
403
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400405 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400407 while (p < end) {
408 unsigned int t;
409
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400410 MULT(p[0], p[0], p[3], t);
411 MULT(p[1], p[1], p[3], t);
412 MULT(p[2], p[2], p[3], t);
413 p += 4;
414
415 }
416 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200418 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000419 if (surface == NULL) {
420 g_object_unref(pixbuf);
421 return NULL;
422 }
423
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800424 data = cairo_surface_get_user_data(surface, &surface_data_key);
425
Benjamin Franzke0c991632011-09-27 21:57:31 +0200426 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800427 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800428 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
429 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200430 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500432 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400433
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434 return surface;
435}
436
437#endif
438
439struct wl_buffer *
440display_get_buffer_for_surface(struct display *display,
441 cairo_surface_t *surface)
442{
443 struct surface_data *data;
444
445 data = cairo_surface_get_user_data (surface, &surface_data_key);
446
447 return data->buffer;
448}
449
450struct shm_surface_data {
451 struct surface_data data;
452 void *map;
453 size_t length;
454};
455
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500456static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457shm_surface_data_destroy(void *p)
458{
459 struct shm_surface_data *data = p;
460
461 wl_buffer_destroy(data->data.buffer);
462 munmap(data->map, data->length);
463}
464
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500465static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400467 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468{
469 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400470 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400471 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800472 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 char filename[] = "/tmp/wayland-shm-XXXXXX";
474
475 data = malloc(sizeof *data);
476 if (data == NULL)
477 return NULL;
478
479 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
480 rectangle->width);
481 data->length = stride * rectangle->height;
482 fd = mkstemp(filename);
483 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000484 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485 return NULL;
486 }
487 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000488 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489 close(fd);
490 return NULL;
491 }
492
493 data->map = mmap(NULL, data->length,
494 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
495 unlink(filename);
496
497 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000498 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499 close(fd);
500 return NULL;
501 }
502
503 surface = cairo_image_surface_create_for_data (data->map,
504 CAIRO_FORMAT_ARGB32,
505 rectangle->width,
506 rectangle->height,
507 stride);
508
509 cairo_surface_set_user_data (surface, &surface_data_key,
510 data, shm_surface_data_destroy);
511
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400512 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400513 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400514 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400515 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400516
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517 data->data.buffer = wl_shm_create_buffer(display->shm,
518 fd,
519 rectangle->width,
520 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400521 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522
523 close(fd);
524
525 return surface;
526}
527
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500528static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529display_create_shm_surface_from_file(struct display *display,
530 const char *filename,
531 struct rectangle *rect)
532{
533 cairo_surface_t *surface;
534 GdkPixbuf *pixbuf;
535 GError *error = NULL;
536 int stride, i;
537 unsigned char *pixels, *p, *end, *dest_data;
538 int dest_stride;
539 uint32_t *d;
540
541 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
542 rect->width, rect->height,
543 FALSE, &error);
544 if (error != NULL)
545 return NULL;
546
547 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
548 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500549 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 return NULL;
551 }
552
553 stride = gdk_pixbuf_get_rowstride(pixbuf);
554 pixels = gdk_pixbuf_get_pixels(pixbuf);
555
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400556 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000557 if (surface == NULL) {
558 g_object_unref(pixbuf);
559 return NULL;
560 }
561
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562 dest_data = cairo_image_surface_get_data (surface);
563 dest_stride = cairo_image_surface_get_stride (surface);
564
565 for (i = 0; i < rect->height; i++) {
566 d = (uint32_t *) (dest_data + i * dest_stride);
567 p = pixels + i * stride;
568 end = p + rect->width * 4;
569 while (p < end) {
570 unsigned int t;
571 unsigned char a, r, g, b;
572
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573 a = p[3];
574 MULT(r, p[0], a, t);
575 MULT(g, p[1], a, t);
576 MULT(b, p[2], a, t);
577 p += 4;
578 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
579 }
580 }
581
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500582 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583
584 return surface;
585}
586
nobled7b87cb02011-02-01 18:51:47 +0000587static int
588check_size(struct rectangle *rect)
589{
590 if (rect->width && rect->height)
591 return 0;
592
593 fprintf(stderr, "tried to create surface of "
594 "width: %d, height: %d\n", rect->width, rect->height);
595 return -1;
596}
597
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598cairo_surface_t *
599display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400601 struct rectangle *rectangle,
602 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603{
nobled7b87cb02011-02-01 18:51:47 +0000604 if (check_size(rectangle) < 0)
605 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500606#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500607 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100608 if (surface)
609 return display_create_egl_window_surface(display,
610 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400611 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100612 rectangle);
613 else
614 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400615 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100616 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500617 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400619 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620}
621
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500622static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623display_create_surface_from_file(struct display *display,
624 const char *filename,
625 struct rectangle *rectangle)
626{
nobled7b87cb02011-02-01 18:51:47 +0000627 if (check_size(rectangle) < 0)
628 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500629#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500630 if (display->dpy) {
631 return display_create_egl_image_surface_from_file(display,
632 filename,
633 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500634 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500636 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400637}
Yuval Fledel45568f62010-12-06 09:18:12 -0500638 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400639 const char *filename;
640 int hotspot_x, hotspot_y;
641} pointer_images[] = {
642 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
643 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
644 { DATADIR "/wayland/bottom_side.png", 16, 20 },
645 { DATADIR "/wayland/grabbing.png", 20, 17 },
646 { DATADIR "/wayland/left_ptr.png", 10, 5 },
647 { DATADIR "/wayland/left_side.png", 10, 20 },
648 { DATADIR "/wayland/right_side.png", 30, 19 },
649 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
650 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
651 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400652 { DATADIR "/wayland/xterm.png", 15, 15 },
653 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400654};
655
656static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400657create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658{
659 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400660 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400661 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400662
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400663 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400664 display->pointer_surfaces =
665 malloc(count * sizeof *display->pointer_surfaces);
666 rect.width = width;
667 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400669 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670 display_create_surface_from_file(display,
671 pointer_images[i].filename,
672 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100673 if (!display->pointer_surfaces[i]) {
674 fprintf(stderr, "Error loading pointer image: %s\n",
675 pointer_images[i].filename);
676 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400677 }
678
679}
680
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400681cairo_surface_t *
682display_get_pointer_surface(struct display *display, int pointer,
683 int *width, int *height,
684 int *hotspot_x, int *hotspot_y)
685{
686 cairo_surface_t *surface;
687
688 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500689#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400690 *width = cairo_gl_surface_get_width(surface);
691 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000692#else
693 *width = cairo_image_surface_get_width(surface);
694 *height = cairo_image_surface_get_height(surface);
695#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 *hotspot_x = pointer_images[pointer].hotspot_x;
697 *hotspot_y = pointer_images[pointer].hotspot_y;
698
699 return cairo_surface_reference(surface);
700}
701
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400702
703static void
704window_attach_surface(struct window *window);
705
706static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400707free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400708{
709 struct window *window = data;
710
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400711 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400712 cairo_surface_destroy(window->pending_surface);
713 window->pending_surface = NULL;
714 if (window->cairo_surface)
715 window_attach_surface(window);
716}
717
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400718static const struct wl_callback_listener free_surface_listener = {
719 free_surface
720};
721
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500722static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200723window_get_resize_dx_dy(struct window *window, int *x, int *y)
724{
725 if (window->resize_edges & WINDOW_RESIZING_LEFT)
726 *x = window->server_allocation.width - window->allocation.width;
727 else
728 *x = 0;
729
730 if (window->resize_edges & WINDOW_RESIZING_TOP)
731 *y = window->server_allocation.height -
732 window->allocation.height;
733 else
734 *y = 0;
735
736 window->resize_edges = 0;
737}
738
739static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400740window_set_type(struct window *window)
741{
742 struct display *display = window->display;
743
744 switch (window->type) {
745 case TYPE_FULLSCREEN:
746 wl_shell_set_fullscreen(display->shell, window->surface);
747 break;
748 case TYPE_TOPLEVEL:
749 wl_shell_set_toplevel(display->shell, window->surface);
750 break;
751 case TYPE_TRANSIENT:
752 wl_shell_set_transient(display->shell, window->surface,
753 window->parent->surface,
754 window->x, window->y, 0);
755 break;
756 case TYPE_CUSTOM:
757 break;
758 }
759}
760
761static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500762window_attach_surface(struct window *window)
763{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400764 struct display *display = window->display;
765 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400766 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000767#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100768 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000769#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500770 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100771
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400772 if (display->shell)
773 window_set_type(window);
774
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000776#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100777 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
778 data = cairo_surface_get_user_data(window->cairo_surface,
779 &surface_data_key);
780
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781 cairo_gl_surface_swapbuffers(window->cairo_surface);
782 wl_egl_window_get_attached_size(data->window,
783 &window->server_allocation.width,
784 &window->server_allocation.height);
785 break;
786 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000787#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789 window_get_resize_dx_dy(window, &x, &y);
790
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791 if (window->pending_surface != NULL)
792 return;
793
794 window->pending_surface = window->cairo_surface;
795 window->cairo_surface = NULL;
796
797 buffer =
798 display_get_buffer_for_surface(display,
799 window->pending_surface);
800
801 wl_surface_attach(window->surface, buffer, x, y);
802 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400803 cb = wl_display_sync(display->display);
804 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000806 default:
807 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100808 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500809
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500810 wl_surface_damage(window->surface, 0, 0,
811 window->allocation.width,
812 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500813}
814
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500815void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400816window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500817{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100818 if (window->cairo_surface) {
819 switch (window->buffer_type) {
820 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
821 case WINDOW_BUFFER_TYPE_SHM:
822 display_surface_damage(window->display,
823 window->cairo_surface,
824 0, 0,
825 window->allocation.width,
826 window->allocation.height);
827 break;
828 default:
829 break;
830 }
831 window_attach_surface(window);
832 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500833}
834
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400835void
836window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500838 cairo_surface_reference(surface);
839
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400840 if (window->cairo_surface != NULL)
841 cairo_surface_destroy(window->cairo_surface);
842
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500843 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400844}
845
Benjamin Franzke22d54812011-07-16 19:50:32 +0000846#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847static void
848window_resize_cairo_window_surface(struct window *window)
849{
850 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200851 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852
853 data = cairo_surface_get_user_data(window->cairo_surface,
854 &surface_data_key);
855
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200856 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100857 wl_egl_window_resize(data->window,
858 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200859 window->allocation.height,
860 x,y);
861
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100862 cairo_gl_surface_set_size(window->cairo_surface,
863 window->allocation.width,
864 window->allocation.height);
865}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000866#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100867
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400868struct display *
869window_get_display(struct window *window)
870{
871 return window->display;
872}
873
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400874void
875window_create_surface(struct window *window)
876{
877 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400878 uint32_t flags = 0;
879
880 if (!window->transparent)
881 flags = SURFACE_OPAQUE;
882
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500884#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100885 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
886 if (window->cairo_surface) {
887 window_resize_cairo_window_surface(window);
888 return;
889 }
890 surface = display_create_surface(window->display,
891 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400892 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500894 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400895 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400897 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400898 break;
899#endif
900 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400901 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400902 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800904 default:
905 surface = NULL;
906 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400907 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400908
909 window_set_surface(window, surface);
910 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400911}
912
913static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500914window_draw_menu(struct window *window)
915{
916 cairo_t *cr;
917 int width, height, r = 5;
918
919 window_create_surface(window);
920
921 cr = cairo_create(window->cairo_surface);
922 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
923 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
924 cairo_paint(cr);
925
926 width = window->allocation.width;
927 height = window->allocation.height;
928 rounded_rect(cr, r, r, width - r, height - r, r);
929 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
930 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
931 cairo_fill(cr);
932 cairo_destroy(cr);
933}
934
935static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500936window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500937{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500938 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500939 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400940 cairo_surface_t *frame;
941 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500942
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400943 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400945 width = window->allocation.width;
946 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500947
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500948 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500949
Kristian Høgsberg09531622010-06-14 23:22:15 -0400950 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400951 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400952 cairo_paint(cr);
953
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400954 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400955 tile_mask(cr, window->display->shadow,
956 shadow_dx, shadow_dy, width, height,
957 window->margin + 10 - shadow_dx,
958 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500959
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400960 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400961 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400962 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400963 frame = window->display->inactive_frame;
964
965 tile_source(cr, frame, 0, 0, width, height,
966 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500967
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500968 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
969 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500970 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 if (window->keyboard_device)
973 cairo_set_source_rgb(cr, 0, 0, 0);
974 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400975 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400976 cairo_show_text(cr, window->title);
977
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500978 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400979
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100980 /* FIXME: this breakes gears, fix cairo? */
981#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400982 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100983#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500984}
985
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400986void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500987window_destroy(struct window *window)
988{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200989 struct display *display = window->display;
990 struct input *input;
991
992 if (window->redraw_scheduled)
993 wl_list_remove(&window->redraw_task.link);
994
995 wl_list_for_each(input, &display->input_list, link) {
996 if (input->pointer_focus == window)
997 input->pointer_focus = NULL;
998 if (input->keyboard_focus == window)
999 input->keyboard_focus = NULL;
1000 }
1001
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002 wl_surface_destroy(window->surface);
1003 wl_list_remove(&window->link);
1004 free(window);
1005}
1006
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007static struct item *
1008window_find_item(struct window *window, int32_t x, int32_t y)
1009{
1010 struct item *item;
1011
1012 wl_list_for_each(item, &window->item_list, link) {
1013 if (item->allocation.x <= x &&
1014 x < item->allocation.x + item->allocation.width &&
1015 item->allocation.y <= y &&
1016 y < item->allocation.y + item->allocation.height) {
1017 return item;
1018 }
1019 }
1020
1021 return NULL;
1022}
1023
1024struct item *
1025window_add_item(struct window *window, void *data)
1026{
1027 struct item *item;
1028
1029 item = malloc(sizeof *item);
1030 memset(item, 0, sizeof *item);
1031 item->user_data = data;
1032 wl_list_insert(window->item_list.prev, &item->link);
1033
1034 return item;
1035}
1036
1037void
1038window_for_each_item(struct window *window, item_func_t func, void *data)
1039{
1040 struct item *item;
1041
1042 wl_list_for_each(item, &window->item_list, link)
1043 func(item, data);
1044}
1045
1046struct item *
1047window_get_focus_item(struct window *window)
1048{
1049 return window->focus_item;
1050}
1051
1052void
1053item_get_allocation(struct item *item, struct rectangle *allocation)
1054{
1055 *allocation = item->allocation;
1056}
1057
1058void
1059item_set_allocation(struct item *item,
1060 int32_t x, int32_t y, int32_t width, int32_t height)
1061{
1062 item->allocation.x = x;
1063 item->allocation.y = y;
1064 item->allocation.width = width;
1065 item->allocation.height = height;
1066}
1067
1068void *
1069item_get_user_data(struct item *item)
1070{
1071 return item->user_data;
1072}
1073
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001074void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001075window_draw(struct window *window)
1076{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001077 if (window->parent)
1078 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001079 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001080 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001081 else
1082 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001083}
1084
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001085cairo_surface_t *
1086window_get_surface(struct window *window)
1087{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001088 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001089}
1090
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001091struct wl_surface *
1092window_get_wl_surface(struct window *window)
1093{
1094 return window->surface;
1095}
1096
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001097static int
1098get_pointer_location(struct window *window, int32_t x, int32_t y)
1099{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001100 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001101 const int grip_size = 8;
1102
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001103 if (!window->decoration)
1104 return WINDOW_CLIENT_AREA;
1105
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001106 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108 else if (window->margin <= x && x < window->margin + grip_size)
1109 hlocation = WINDOW_RESIZING_LEFT;
1110 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001112 else if (x < window->allocation.width - window->margin)
1113 hlocation = WINDOW_RESIZING_RIGHT;
1114 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001115 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116
1117 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119 else if (window->margin <= y && y < window->margin + grip_size)
1120 vlocation = WINDOW_RESIZING_TOP;
1121 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001122 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001123 else if (y < window->allocation.height - window->margin)
1124 vlocation = WINDOW_RESIZING_BOTTOM;
1125 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001126 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001127
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001128 location = vlocation | hlocation;
1129 if (location & WINDOW_EXTERIOR)
1130 location = WINDOW_EXTERIOR;
1131 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1132 location = WINDOW_TITLEBAR;
1133 else if (location == WINDOW_INTERIOR)
1134 location = WINDOW_CLIENT_AREA;
1135
1136 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001137}
1138
1139static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001140set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141{
1142 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143 struct wl_buffer *buffer;
1144 cairo_surface_t *surface;
1145 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001146
1147 location = get_pointer_location(input->pointer_focus,
1148 input->sx, input->sy);
1149 switch (location) {
1150 case WINDOW_RESIZING_TOP:
1151 pointer = POINTER_TOP;
1152 break;
1153 case WINDOW_RESIZING_BOTTOM:
1154 pointer = POINTER_BOTTOM;
1155 break;
1156 case WINDOW_RESIZING_LEFT:
1157 pointer = POINTER_LEFT;
1158 break;
1159 case WINDOW_RESIZING_RIGHT:
1160 pointer = POINTER_RIGHT;
1161 break;
1162 case WINDOW_RESIZING_TOP_LEFT:
1163 pointer = POINTER_TOP_LEFT;
1164 break;
1165 case WINDOW_RESIZING_TOP_RIGHT:
1166 pointer = POINTER_TOP_RIGHT;
1167 break;
1168 case WINDOW_RESIZING_BOTTOM_LEFT:
1169 pointer = POINTER_BOTTOM_LEFT;
1170 break;
1171 case WINDOW_RESIZING_BOTTOM_RIGHT:
1172 pointer = POINTER_BOTTOM_RIGHT;
1173 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001174 case WINDOW_EXTERIOR:
1175 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001176 if (input->current_pointer_image == POINTER_DEFAULT)
1177 return;
1178
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001179 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001180 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001181 return;
1182 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001183 break;
1184 }
1185
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001186 if (pointer == input->current_pointer_image)
1187 return;
1188
1189 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001190 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001191
1192 if (!surface)
1193 return;
1194
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001196 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197 pointer_images[pointer].hotspot_x,
1198 pointer_images[pointer].hotspot_y);
1199}
1200
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001201static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001202window_set_focus_item(struct window *window, struct item *focus)
1203{
1204 void *data;
1205
1206 if (focus == window->focus_item)
1207 return;
1208
1209 window->focus_item = focus;
1210 data = focus ? focus->user_data : NULL;
1211 if (window->item_focus_handler)
1212 window->item_focus_handler(window, focus, data);
1213}
1214
1215static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001216window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001217 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001218 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001219{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001220 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001221 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001222 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001223 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001224
1225 input->x = x;
1226 input->y = y;
1227 input->sx = sx;
1228 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001230 if (!window->focus_item || !window->item_grab_button) {
1231 item = window_find_item(window, sx, sy);
1232 window_set_focus_item(window, item);
1233 }
1234
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 if (window->motion_handler)
1236 pointer = (*window->motion_handler)(window, input, time,
1237 x, y, sx, sy,
1238 window->user_data);
1239
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001240 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001241}
1242
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001243static void
1244window_handle_button(void *data,
1245 struct wl_input_device *input_device,
1246 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001247{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001248 struct input *input = data;
1249 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001250 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001252
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001253 if (window->focus_item && window->item_grab_button == 0 && state)
1254 window->item_grab_button = button;
1255
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001257
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001258 if (window->display->shell &&
1259 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001262 wl_shell_move(window->display->shell,
1263 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001264 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001265 case WINDOW_RESIZING_TOP:
1266 case WINDOW_RESIZING_BOTTOM:
1267 case WINDOW_RESIZING_LEFT:
1268 case WINDOW_RESIZING_RIGHT:
1269 case WINDOW_RESIZING_TOP_LEFT:
1270 case WINDOW_RESIZING_TOP_RIGHT:
1271 case WINDOW_RESIZING_BOTTOM_LEFT:
1272 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001273 wl_shell_resize(window->display->shell,
1274 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001276 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 case WINDOW_CLIENT_AREA:
1278 if (window->button_handler)
1279 (*window->button_handler)(window,
1280 input, time,
1281 button, state,
1282 window->user_data);
1283 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001284 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001285 } else {
1286 if (window->button_handler)
1287 (*window->button_handler)(window,
1288 input, time,
1289 button, state,
1290 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001291 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001292
1293 if (window->focus_item &&
1294 window->item_grab_button == button && !state) {
1295 window->item_grab_button = 0;
1296 item = window_find_item(window, input->sx, input->sy);
1297 window_set_focus_item(window, item);
1298 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001299}
1300
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001301static void
1302window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001303 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001304{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001305 struct input *input = data;
1306 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001307 struct display *d = window->display;
1308 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001309
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001310 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001311 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001312 return;
1313
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001314 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001315 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001316 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1317 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001318
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001319 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1320
1321 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001322 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001323 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001324 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001325
1326 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001327 (*window->key_handler)(window, input, time, key, sym, state,
1328 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001329}
1330
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001331static void
1332window_handle_pointer_focus(void *data,
1333 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001334 uint32_t time, struct wl_surface *surface,
1335 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001336{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001337 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001338 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001339 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001340 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001341
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001342 window = input->pointer_focus;
1343 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001344 window_set_focus_item(window, NULL);
1345
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001346 if (window->leave_handler)
1347 window->leave_handler(window, input,
1348 time, window->user_data);
1349 input->pointer_focus = NULL;
1350 input->current_pointer_image = POINTER_UNSET;
1351 }
1352
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001353 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001354 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001355 window = input->pointer_focus;
1356
Kristian Høgsberg59826582011-01-20 11:56:57 -05001357 input->x = x;
1358 input->y = y;
1359 input->sx = sx;
1360 input->sy = sy;
1361
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001362 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001363 if (window->enter_handler)
1364 pointer = window->enter_handler(window, input,
1365 time, sx, sy,
1366 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001367
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001368 item = window_find_item(window, x, y);
1369 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001370
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001371 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001372 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001373}
1374
1375static void
1376window_handle_keyboard_focus(void *data,
1377 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001378 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001379 struct wl_surface *surface,
1380 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001381{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001382 struct input *input = data;
1383 struct window *window = input->keyboard_focus;
1384 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001385 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001386
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001387 window = input->keyboard_focus;
1388 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001389 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001390 if (window->keyboard_focus_handler)
1391 (*window->keyboard_focus_handler)(window, NULL,
1392 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001393 }
1394
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001395 if (surface)
1396 input->keyboard_focus = wl_surface_get_user_data(surface);
1397 else
1398 input->keyboard_focus = NULL;
1399
1400 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001401 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001402 for (k = keys->data; k < end; k++)
1403 input->modifiers |= d->xkb->map->modmap[*k];
1404
1405 window = input->keyboard_focus;
1406 if (window) {
1407 window->keyboard_device = input;
1408 if (window->keyboard_focus_handler)
1409 (*window->keyboard_focus_handler)(window,
1410 window->keyboard_device,
1411 window->user_data);
1412 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001413}
1414
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001415static const struct wl_input_device_listener input_device_listener = {
1416 window_handle_motion,
1417 window_handle_button,
1418 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001419 window_handle_pointer_focus,
1420 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001421};
1422
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001423void
1424input_get_position(struct input *input, int32_t *x, int32_t *y)
1425{
1426 *x = input->sx;
1427 *y = input->sy;
1428}
1429
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001430struct wl_input_device *
1431input_get_input_device(struct input *input)
1432{
1433 return input->input_device;
1434}
1435
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001436uint32_t
1437input_get_modifiers(struct input *input)
1438{
1439 return input->modifiers;
1440}
1441
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001442struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001443window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001444{
Benjamin Franzke0c991632011-09-27 21:57:31 +02001445 cairo_device_flush (window->display->rgb_device);
1446 cairo_device_flush (window->display->argb_device);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001447
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001448 return wl_shell_create_drag(window->display->shell);
1449}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001450
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001451void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001452window_move(struct window *window, struct input *input, uint32_t time)
1453{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001454 if (window->display->shell)
1455 wl_shell_move(window->display->shell,
1456 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001457}
1458
1459void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001460window_activate_drag(struct wl_drag *drag, struct window *window,
1461 struct input *input, uint32_t time)
1462{
1463 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001464}
1465
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001466static void
1467handle_configure(void *data, struct wl_shell *shell,
1468 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001469 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001470{
1471 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001472 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001473
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001474 /* FIXME: this is probably the wrong place to check for width
1475 * or height <= 0, but it prevents the compositor from crashing
1476 */
1477 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001478 return;
1479
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001480 window->resize_edges = edges;
1481
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001482 if (window->resize_handler) {
1483 child_width = width - 20 - window->margin * 2;
1484 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001485
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001486 (*window->resize_handler)(window,
1487 child_width, child_height,
1488 window->user_data);
1489 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001490 window->allocation.width = width;
1491 window->allocation.height = height;
1492
1493 if (window->redraw_handler)
1494 window_schedule_redraw(window);
1495 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001496}
1497
1498static const struct wl_shell_listener shell_listener = {
1499 handle_configure,
1500};
1501
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001502void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001503window_get_allocation(struct window *window,
1504 struct rectangle *allocation)
1505{
1506 *allocation = window->allocation;
1507}
1508
1509void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001510window_get_child_allocation(struct window *window,
1511 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001512{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001513 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001514 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001515 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001516 allocation->x = window->margin + 10;
1517 allocation->y = window->margin + 50;
1518 allocation->width =
1519 window->allocation.width - 20 - window->margin * 2;
1520 allocation->height =
1521 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001522 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001523}
1524
1525void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001526window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001527{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001528 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001529 window->allocation.x = 20 + window->margin;
1530 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001531 window->allocation.width = width + 20 + window->margin * 2;
1532 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001533 } else {
1534 window->allocation.x = 0;
1535 window->allocation.y = 0;
1536 window->allocation.width = width;
1537 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001538 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001539}
1540
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001541static void
1542idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001543{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001544 struct window *window =
1545 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001546
1547 window->redraw_handler(window, window->user_data);
1548 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001549}
1550
1551void
1552window_schedule_redraw(struct window *window)
1553{
1554 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001555 window->redraw_task.run = idle_redraw;
1556 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001557 window->redraw_scheduled = 1;
1558 }
1559}
1560
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001561void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001562window_set_custom(struct window *window)
1563{
1564 window->type = TYPE_CUSTOM;
1565}
1566
1567void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001568window_set_fullscreen(struct window *window, int fullscreen)
1569{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001570 int32_t width, height;
1571
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001572 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001573 return;
1574
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001575 if (fullscreen) {
1576 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001577 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001578 width = window->display->screen_allocation.width;
1579 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001580 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001581 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001582 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001583 width = window->saved_allocation.width - 20 - window->margin * 2;
1584 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001585 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001586 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001587
1588 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001589}
1590
1591void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001592window_set_decoration(struct window *window, int decoration)
1593{
1594 window->decoration = decoration;
1595}
1596
1597void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001598window_set_user_data(struct window *window, void *data)
1599{
1600 window->user_data = data;
1601}
1602
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001603void *
1604window_get_user_data(struct window *window)
1605{
1606 return window->user_data;
1607}
1608
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001609void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001610window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001611 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001612{
1613 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001614}
1615
1616void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001617window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001618 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001619{
1620 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001621}
1622
1623void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001624window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001625 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001626{
1627 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001628}
1629
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001630void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001631window_set_button_handler(struct window *window,
1632 window_button_handler_t handler)
1633{
1634 window->button_handler = handler;
1635}
1636
1637void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001638window_set_motion_handler(struct window *window,
1639 window_motion_handler_t handler)
1640{
1641 window->motion_handler = handler;
1642}
1643
1644void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001645window_set_enter_handler(struct window *window,
1646 window_enter_handler_t handler)
1647{
1648 window->enter_handler = handler;
1649}
1650
1651void
1652window_set_leave_handler(struct window *window,
1653 window_leave_handler_t handler)
1654{
1655 window->leave_handler = handler;
1656}
1657
1658void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001659window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001660 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001661{
1662 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001663}
1664
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001665void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666window_set_item_focus_handler(struct window *window,
1667 window_item_focus_handler_t handler)
1668{
1669 window->item_focus_handler = handler;
1670}
1671
1672void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001673window_set_transparent(struct window *window, int transparent)
1674{
1675 window->transparent = transparent;
1676}
1677
1678void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001679window_set_title(struct window *window, const char *title)
1680{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001681 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001682 window->title = strdup(title);
1683}
1684
1685const char *
1686window_get_title(struct window *window)
1687{
1688 return window->title;
1689}
1690
1691void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001692display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1693 int32_t x, int32_t y, int32_t width, int32_t height)
1694{
1695 struct wl_buffer *buffer;
1696
1697 buffer = display_get_buffer_for_surface(display, cairo_surface);
1698
1699 wl_buffer_damage(buffer, x, y, width, height);
1700}
1701
1702void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001703window_damage(struct window *window, int32_t x, int32_t y,
1704 int32_t width, int32_t height)
1705{
1706 wl_surface_damage(window->surface, x, y, width, height);
1707}
1708
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001709static struct window *
1710window_create_internal(struct display *display, struct window *parent,
1711 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001712{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001713 struct window *window;
1714
1715 window = malloc(sizeof *window);
1716 if (window == NULL)
1717 return NULL;
1718
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001719 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001720 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001721 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001722 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001723 window->allocation.x = 0;
1724 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001725 window->allocation.width = width;
1726 window->allocation.height = height;
1727 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001728 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001729 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001730 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001732
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001733 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001734#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001735 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1736 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001737#else
1738 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1739#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001740 else
1741 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001742
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001744 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001745
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001746 return window;
1747}
1748
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001749struct window *
1750window_create(struct display *display, int32_t width, int32_t height)
1751{
1752 struct window *window;
1753
1754 window = window_create_internal(display, NULL, width, height);
1755 if (!window)
1756 return NULL;
1757
1758 return window;
1759}
1760
1761struct window *
1762window_create_transient(struct display *display, struct window *parent,
1763 int32_t x, int32_t y, int32_t width, int32_t height)
1764{
1765 struct window *window;
1766
1767 window = window_create_internal(parent->display,
1768 parent, width, height);
1769 if (!window)
1770 return NULL;
1771
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001772 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001773 window->x = x;
1774 window->y = y;
1775
1776 return window;
1777}
1778
1779void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001780window_set_buffer_type(struct window *window, enum window_buffer_type type)
1781{
1782 window->buffer_type = type;
1783}
1784
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001785
1786static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001787display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001788 struct wl_output *wl_output,
1789 int x, int y,
1790 int physical_width,
1791 int physical_height,
1792 int subpixel,
1793 const char *make,
1794 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001795{
1796 struct display *display = data;
1797
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001798 display->screen_allocation.x = x;
1799 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001800}
1801
1802static void
1803display_handle_mode(void *data,
1804 struct wl_output *wl_output,
1805 uint32_t flags,
1806 int width,
1807 int height,
1808 int refresh)
1809{
1810 struct display *display = data;
1811
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001812 display->screen_allocation.width = width;
1813 display->screen_allocation.height = height;
1814}
1815
1816static const struct wl_output_listener output_listener = {
1817 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001818 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001819};
1820
1821static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001822display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001823{
1824 struct input *input;
1825
1826 input = malloc(sizeof *input);
1827 if (input == NULL)
1828 return;
1829
1830 memset(input, 0, sizeof *input);
1831 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001832 input->input_device =
1833 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001834 input->pointer_focus = NULL;
1835 input->keyboard_focus = NULL;
1836 wl_list_insert(d->input_list.prev, &input->link);
1837
1838 wl_input_device_add_listener(input->input_device,
1839 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001840 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001841}
1842
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001843struct selection_offer {
1844 struct display *display;
1845 struct wl_selection_offer *offer;
1846 struct wl_array types;
1847 struct input *input;
1848};
1849
1850int
1851input_offers_mime_type(struct input *input, const char *type)
1852{
1853 struct selection_offer *offer = input->offer;
1854 char **p, **end;
1855
1856 if (offer == NULL)
1857 return 0;
1858
1859 end = offer->types.data + offer->types.size;
1860 for (p = offer->types.data; p < end; p++)
1861 if (strcmp(*p, type) == 0)
1862 return 1;
1863
1864 return 0;
1865}
1866
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001867void
1868input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001869{
1870 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001871
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001872 /* FIXME: A number of things can go wrong here: the object may
1873 * not be the current selection offer any more (which could
1874 * still work, but the source may have gone away or just
1875 * destroyed its wl_selection) or the offer may not have the
1876 * requested type after all (programmer/client error,
1877 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001878 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001879}
1880
1881static void
1882selection_offer_offer(void *data,
1883 struct wl_selection_offer *selection_offer,
1884 const char *type)
1885{
1886 struct selection_offer *offer = data;
1887
1888 char **p;
1889
1890 p = wl_array_add(&offer->types, sizeof *p);
1891 if (p)
1892 *p = strdup(type);
1893};
1894
1895static void
1896selection_offer_keyboard_focus(void *data,
1897 struct wl_selection_offer *selection_offer,
1898 struct wl_input_device *input_device)
1899{
1900 struct selection_offer *offer = data;
1901 struct input *input;
1902 char **p, **end;
1903
1904 if (input_device == NULL) {
1905 printf("selection offer retracted %p\n", selection_offer);
1906 input = offer->input;
1907 input->offer = NULL;
1908 wl_selection_offer_destroy(selection_offer);
1909 wl_array_release(&offer->types);
1910 free(offer);
1911 return;
1912 }
1913
1914 input = wl_input_device_get_user_data(input_device);
1915 printf("new selection offer %p:", selection_offer);
1916
1917 offer->input = input;
1918 input->offer = offer;
1919 end = offer->types.data + offer->types.size;
1920 for (p = offer->types.data; p < end; p++)
1921 printf(" %s", *p);
1922
1923 printf("\n");
1924}
1925
1926struct wl_selection_offer_listener selection_offer_listener = {
1927 selection_offer_offer,
1928 selection_offer_keyboard_focus
1929};
1930
1931static void
1932add_selection_offer(struct display *d, uint32_t id)
1933{
1934 struct selection_offer *offer;
1935
1936 offer = malloc(sizeof *offer);
1937 if (offer == NULL)
1938 return;
1939
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001940 offer->offer =
1941 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001942 offer->display = d;
1943 wl_array_init(&offer->types);
1944 offer->input = NULL;
1945
1946 wl_selection_offer_add_listener(offer->offer,
1947 &selection_offer_listener, offer);
1948}
1949
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001950static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001951display_handle_global(struct wl_display *display, uint32_t id,
1952 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001953{
1954 struct display *d = data;
1955
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001956 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001957 d->compositor =
1958 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001959 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001960 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001961 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001962 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001963 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001964 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001965 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001966 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001967 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001968 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001969 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001970 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001971 }
1972}
1973
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001974static void
1975display_render_frame(struct display *d)
1976{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001977 int radius = 8;
1978 cairo_t *cr;
1979
1980 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1981 cr = cairo_create(d->shadow);
1982 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1983 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1984 rounded_rect(cr, 16, 16, 112, 112, radius);
1985 cairo_fill(cr);
1986 cairo_destroy(cr);
1987 blur_surface(d->shadow, 64);
1988
1989 d->active_frame =
1990 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1991 cr = cairo_create(d->active_frame);
1992 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1993 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1994 rounded_rect(cr, 16, 16, 112, 112, radius);
1995 cairo_fill(cr);
1996 cairo_destroy(cr);
1997
1998 d->inactive_frame =
1999 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2000 cr = cairo_create(d->inactive_frame);
2001 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2002 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2003 rounded_rect(cr, 16, 16, 112, 112, radius);
2004 cairo_fill(cr);
2005 cairo_destroy(cr);
2006}
2007
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002008static void
2009init_xkb(struct display *d)
2010{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002011 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002012
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002013 names.rules = "evdev";
2014 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002015 names.layout = option_xkb_layout;
2016 names.variant = option_xkb_variant;
2017 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002018
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002019 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002020 if (!d->xkb) {
2021 fprintf(stderr, "Failed to compile keymap\n");
2022 exit(1);
2023 }
2024}
2025
Yuval Fledel45568f62010-12-06 09:18:12 -05002026static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002027init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002028{
2029 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002030 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002031
2032 static const EGLint premul_argb_cfg_attribs[] = {
2033 EGL_SURFACE_TYPE,
2034 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2035 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002036 EGL_RED_SIZE, 1,
2037 EGL_GREEN_SIZE, 1,
2038 EGL_BLUE_SIZE, 1,
2039 EGL_ALPHA_SIZE, 1,
2040 EGL_DEPTH_SIZE, 1,
2041 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2042 EGL_NONE
2043 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002044
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002045 static const EGLint rgb_cfg_attribs[] = {
2046 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2047 EGL_RED_SIZE, 1,
2048 EGL_GREEN_SIZE, 1,
2049 EGL_BLUE_SIZE, 1,
2050 EGL_ALPHA_SIZE, 0,
2051 EGL_DEPTH_SIZE, 1,
2052 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2053 EGL_NONE
2054 };
2055
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002056 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002057 if (!eglInitialize(d->dpy, &major, &minor)) {
2058 fprintf(stderr, "failed to initialize display\n");
2059 return -1;
2060 }
2061
2062 if (!eglBindAPI(EGL_OPENGL_API)) {
2063 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2064 return -1;
2065 }
2066
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002067 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002068 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002069 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002070 return -1;
2071 }
2072
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002073 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2074 &d->rgb_config, 1, &n) || n != 1) {
2075 fprintf(stderr, "failed to choose rgb config\n");
2076 return -1;
2077 }
2078
Benjamin Franzke0c991632011-09-27 21:57:31 +02002079 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2080 if (d->rgb_ctx == NULL) {
2081 fprintf(stderr, "failed to create context\n");
2082 return -1;
2083 }
2084 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2085 EGL_NO_CONTEXT, NULL);
2086 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002087 fprintf(stderr, "failed to create context\n");
2088 return -1;
2089 }
2090
Benjamin Franzke0c991632011-09-27 21:57:31 +02002091 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002092 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002093 return -1;
2094 }
2095
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002096#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002097 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2098 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002099 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002100 return -1;
2101 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002102 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2103 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2104 fprintf(stderr, "failed to get cairo egl argb device\n");
2105 return -1;
2106 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002107#endif
2108
2109 return 0;
2110}
2111
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002112static int
2113event_mask_update(uint32_t mask, void *data)
2114{
2115 struct display *d = data;
2116
2117 d->mask = mask;
2118
2119 return 0;
2120}
2121
2122static void
2123handle_display_data(struct task *task, uint32_t events)
2124{
2125 struct display *display =
2126 container_of(task, struct display, display_task);
2127
2128 wl_display_iterate(display->display, display->mask);
2129}
2130
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002131struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002132display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002133{
2134 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002135 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002136 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002137 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002138
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002139 g_type_init();
2140
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002141 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002142 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002143 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002144
2145 xkb_option_group = g_option_group_new("xkb",
2146 "Keyboard options",
2147 "Show all XKB options",
2148 NULL, NULL);
2149 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2150 g_option_context_add_group (context, xkb_option_group);
2151
2152 if (!g_option_context_parse(context, argc, argv, &error)) {
2153 fprintf(stderr, "option parsing failed: %s\n", error->message);
2154 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002155 }
2156
Tim Wiederhake748f6722011-01-23 23:25:25 +01002157 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002158
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002159 d = malloc(sizeof *d);
2160 if (d == NULL)
2161 return NULL;
2162
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002163 memset(d, 0, sizeof *d);
2164
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002165 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002166 if (d->display == NULL) {
2167 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002168 return NULL;
2169 }
2170
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002171 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2172 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2173 d->display_task.run = handle_display_data;
2174 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2175
2176 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002177 wl_list_init(&d->input_list);
2178
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002179 /* Set up listener so we'll catch all events. */
2180 wl_display_add_global_listener(d->display,
2181 display_handle_global, d);
2182
2183 /* Process connection events. */
2184 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002185 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002186 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002187
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002188 d->image_target_texture_2d =
2189 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2190 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2191 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2192
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002193 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002194
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002195 display_render_frame(d);
2196
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002197 wl_list_init(&d->window_list);
2198
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002199 init_xkb(d);
2200
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002201 return d;
2202}
2203
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002204struct wl_display *
2205display_get_display(struct display *display)
2206{
2207 return display->display;
2208}
2209
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002210struct wl_compositor *
2211display_get_compositor(struct display *display)
2212{
2213 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002214}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002215
2216EGLDisplay
2217display_get_egl_display(struct display *d)
2218{
2219 return d->dpy;
2220}
2221
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002222EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002223display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002224{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002225 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002226}
2227
Benjamin Franzke0c991632011-09-27 21:57:31 +02002228EGLConfig
2229display_get_argb_egl_config(struct display *d)
2230{
2231 return d->premultiplied_argb_config;
2232}
2233
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002234struct wl_shell *
2235display_get_shell(struct display *display)
2236{
2237 return display->shell;
2238}
2239
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002240int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002241display_acquire_window_surface(struct display *display,
2242 struct window *window,
2243 EGLContext ctx)
2244{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002245#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002246 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002247 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002248
2249 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002250 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002251 device = cairo_surface_get_device(window->cairo_surface);
2252 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002253 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002254
Benjamin Franzke0c991632011-09-27 21:57:31 +02002255 if (!ctx) {
2256 if (device == display->rgb_device)
2257 ctx = display->rgb_ctx;
2258 else if (device == display->argb_device)
2259 ctx = display->argb_ctx;
2260 else
2261 assert(0);
2262 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002263
2264 data = cairo_surface_get_user_data(window->cairo_surface,
2265 &surface_data_key);
2266
Benjamin Franzke0c991632011-09-27 21:57:31 +02002267 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002268 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2269 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002270
2271 return 0;
2272#else
2273 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002274#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002275}
2276
2277void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002278display_release_window_surface(struct display *display,
2279 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002280{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002281#ifdef HAVE_CAIRO_EGL
2282 cairo_device_t *device;
2283
2284 device = cairo_surface_get_device(window->cairo_surface);
2285 if (!device)
2286 return;
2287
2288 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002289 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002290 cairo_device_release(device);
2291#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002292}
2293
2294void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002295display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002296{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002297 wl_list_insert(&display->deferred_list, &task->link);
2298}
2299
2300void
2301display_watch_fd(struct display *display,
2302 int fd, uint32_t events, struct task *task)
2303{
2304 struct epoll_event ep;
2305
2306 ep.events = events;
2307 ep.data.ptr = task;
2308 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2309}
2310
2311void
2312display_run(struct display *display)
2313{
2314 struct task *task;
2315 struct epoll_event ep[16];
2316 int i, count;
2317
2318 while (1) {
2319 while (display->mask & WL_DISPLAY_WRITABLE)
2320 wl_display_iterate(display->display,
2321 WL_DISPLAY_WRITABLE);
2322
2323 count = epoll_wait(display->epoll_fd,
2324 ep, ARRAY_LENGTH(ep), -1);
2325 for (i = 0; i < count; i++) {
2326 task = ep[i].data.ptr;
2327 task->run(task, ep[i].events);
2328 }
2329
2330 while (!wl_list_empty(&display->deferred_list)) {
2331 task = container_of(display->deferred_list.next,
2332 struct task, link);
2333 wl_list_remove(&task->link);
2334 task->run(task, 0);
2335 }
2336 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002337}