blob: 1e317c70332f4cc2201dd65aef722e4a46d748be [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>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020053#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040054#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050057
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050058struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050059 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050060 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040061 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040062 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050063 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040065 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040066 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020067 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020068 EGLContext rgb_ctx;
69 EGLContext argb_ctx;
70 cairo_device_t *rgb_device;
71 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040072
73 int display_fd;
74 uint32_t mask;
75 struct task display_task;
76
77 int epoll_fd;
78 struct wl_list deferred_list;
79
Kristian Høgsberg478d9262010-06-08 20:34:11 -040080 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040081 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040082 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040083 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040084 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040085 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040086
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050087 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
88 PFNEGLCREATEIMAGEKHRPROC create_image;
89 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050090};
91
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040092enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040094 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040095 TYPE_TRANSIENT,
96 TYPE_CUSTOM
97};
98
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050099struct window {
100 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500101 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500102 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500103 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500104 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500105 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400106 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400107 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400108 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500109 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500110 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400111 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400113 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400114 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500115 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400116 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500117
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400118 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500119 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500120
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500121 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400122 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500123 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400124 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500125 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400126 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400127 window_enter_handler_t enter_handler;
128 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400129 window_item_focus_handler_t item_focus_handler;
130
131 struct wl_list item_list;
132 struct item *focus_item;
133 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400134
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500135 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400136 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137};
138
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400139struct item {
140 struct wl_list link;
141 struct rectangle allocation;
142 void *user_data;
143};
144
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400145struct input {
146 struct display *display;
147 struct wl_input_device *input_device;
148 struct window *pointer_focus;
149 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500150 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400151 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400152 uint32_t modifiers;
153 int32_t x, y, sx, sy;
154 struct wl_list link;
155};
156
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400157enum {
158 POINTER_DEFAULT = 100,
159 POINTER_UNSET
160};
161
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500162enum window_location {
163 WINDOW_INTERIOR = 0,
164 WINDOW_RESIZING_TOP = 1,
165 WINDOW_RESIZING_BOTTOM = 2,
166 WINDOW_RESIZING_LEFT = 4,
167 WINDOW_RESIZING_TOP_LEFT = 5,
168 WINDOW_RESIZING_BOTTOM_LEFT = 6,
169 WINDOW_RESIZING_RIGHT = 8,
170 WINDOW_RESIZING_TOP_RIGHT = 9,
171 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
172 WINDOW_RESIZING_MASK = 15,
173 WINDOW_EXTERIOR = 16,
174 WINDOW_TITLEBAR = 17,
175 WINDOW_CLIENT_AREA = 18,
176};
177
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400178const char *option_xkb_layout = "us";
179const char *option_xkb_variant = "";
180const char *option_xkb_options = "";
181
182static const GOptionEntry xkb_option_entries[] = {
183 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
184 &option_xkb_layout, "XKB Layout" },
185 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
186 &option_xkb_variant, "XKB Variant" },
187 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
188 &option_xkb_options, "XKB Options" },
189 { NULL }
190};
191
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400192static const cairo_user_data_key_t surface_data_key;
193struct surface_data {
194 struct wl_buffer *buffer;
195};
196
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500197#define MULT(_d,c,a,t) \
198 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
199
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500200#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400201
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100202struct egl_window_surface_data {
203 struct display *display;
204 struct wl_surface *surface;
205 struct wl_egl_window *window;
206 EGLSurface surf;
207};
208
209static void
210egl_window_surface_data_destroy(void *p)
211{
212 struct egl_window_surface_data *data = p;
213 struct display *d = data->display;
214
215 eglDestroySurface(d->dpy, data->surf);
216 wl_egl_window_destroy(data->window);
217 data->surface = NULL;
218
219 free(p);
220}
221
222static cairo_surface_t *
223display_create_egl_window_surface(struct display *display,
224 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400225 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100226 struct rectangle *rectangle)
227{
228 cairo_surface_t *cairo_surface;
229 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400230 EGLConfig config;
231 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200232 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100233
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400234 static const EGLint premul_attribs[] = {
235 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
236 EGL_NONE
237 };
238
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100239 data = malloc(sizeof *data);
240 if (data == NULL)
241 return NULL;
242
243 data->display = display;
244 data->surface = surface;
245
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400246 if (flags & SURFACE_OPAQUE) {
247 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200248 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200249 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400250 } else {
251 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200252 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400253 attribs = premul_attribs;
254 }
255
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400256 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100257 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400258 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100259
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400260 data->surf = eglCreateWindowSurface(display->dpy, config,
261 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100262
Benjamin Franzke0c991632011-09-27 21:57:31 +0200263 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100264 data->surf,
265 rectangle->width,
266 rectangle->height);
267
268 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
269 data, egl_window_surface_data_destroy);
270
271 return cairo_surface;
272}
273
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500274struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200276 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400277 EGLImageKHR image;
278 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800279 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500280 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400281};
282
283static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500284egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500286 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800287 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400288
Benjamin Franzke0c991632011-09-27 21:57:31 +0200289 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800292
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500293 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400294 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500295 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500296 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400297}
298
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500299EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300display_get_image_for_egl_image_surface(struct display *display,
301 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500302{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500303 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500304
305 data = cairo_surface_get_user_data (surface, &surface_data_key);
306
307 return data->image;
308}
309
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500310static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500311display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400312 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 EGLDisplay dpy = display->dpy;
317 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200318 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319
320 data = malloc(sizeof *data);
321 if (data == NULL)
322 return NULL;
323
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800324 data->display = display;
325
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400326 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400327 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500328 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000329 free(data);
330 return NULL;
331 }
332
Benjamin Franzke0c991632011-09-27 21:57:31 +0200333 if (flags & SURFACE_OPAQUE) {
334 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200335 content = CAIRO_CONTENT_COLOR;
336 } else {
337 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200338 content = CAIRO_CONTENT_COLOR_ALPHA;
339 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400340
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500341 data->image = display->create_image(dpy, NULL,
342 EGL_NATIVE_PIXMAP_KHR,
343 (EGLClientBuffer) data->pixmap,
344 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500346 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347 free(data);
348 return NULL;
349 }
350
351 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400352 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353
Benjamin Franzke0c991632011-09-27 21:57:31 +0200354 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500357 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200358 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359
Benjamin Franzke0c991632011-09-27 21:57:31 +0200360 surface = cairo_gl_surface_create_for_texture(data->device,
361 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400362 data->texture,
363 rectangle->width,
364 rectangle->height);
365
366 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500367 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400368
369 return surface;
370}
371
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500372static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500373display_create_egl_image_surface_from_file(struct display *display,
374 const char *filename,
375 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400376{
377 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400378 GdkPixbuf *pixbuf;
379 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400380 int stride, i;
381 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400383
384 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400386 FALSE, &error);
387 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400389
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400390 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
391 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500392 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400394 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400395
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400396
397 stride = gdk_pixbuf_get_rowstride(pixbuf);
398 pixels = gdk_pixbuf_get_pixels(pixbuf);
399
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400401 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400403 while (p < end) {
404 unsigned int t;
405
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400406 MULT(p[0], p[0], p[3], t);
407 MULT(p[1], p[1], p[3], t);
408 MULT(p[2], p[2], p[3], t);
409 p += 4;
410
411 }
412 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200414 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000415 if (surface == NULL) {
416 g_object_unref(pixbuf);
417 return NULL;
418 }
419
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800420 data = cairo_surface_get_user_data(surface, &surface_data_key);
421
Benjamin Franzke0c991632011-09-27 21:57:31 +0200422 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800423 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800424 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
425 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200426 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400427
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500428 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400429
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430 return surface;
431}
432
433#endif
434
435struct wl_buffer *
436display_get_buffer_for_surface(struct display *display,
437 cairo_surface_t *surface)
438{
439 struct surface_data *data;
440
441 data = cairo_surface_get_user_data (surface, &surface_data_key);
442
443 return data->buffer;
444}
445
446struct shm_surface_data {
447 struct surface_data data;
448 void *map;
449 size_t length;
450};
451
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500452static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400453shm_surface_data_destroy(void *p)
454{
455 struct shm_surface_data *data = p;
456
457 wl_buffer_destroy(data->data.buffer);
458 munmap(data->map, data->length);
459}
460
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500461static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400462display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400463 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464{
465 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400466 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400467 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800468 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469 char filename[] = "/tmp/wayland-shm-XXXXXX";
470
471 data = malloc(sizeof *data);
472 if (data == NULL)
473 return NULL;
474
475 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
476 rectangle->width);
477 data->length = stride * rectangle->height;
478 fd = mkstemp(filename);
479 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000480 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400481 return NULL;
482 }
483 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000484 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485 close(fd);
486 return NULL;
487 }
488
489 data->map = mmap(NULL, data->length,
490 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
491 unlink(filename);
492
493 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000494 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400495 close(fd);
496 return NULL;
497 }
498
499 surface = cairo_image_surface_create_for_data (data->map,
500 CAIRO_FORMAT_ARGB32,
501 rectangle->width,
502 rectangle->height,
503 stride);
504
505 cairo_surface_set_user_data (surface, &surface_data_key,
506 data, shm_surface_data_destroy);
507
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400508 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400510 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400511 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400512
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513 data->data.buffer = wl_shm_create_buffer(display->shm,
514 fd,
515 rectangle->width,
516 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400517 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518
519 close(fd);
520
521 return surface;
522}
523
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500524static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525display_create_shm_surface_from_file(struct display *display,
526 const char *filename,
527 struct rectangle *rect)
528{
529 cairo_surface_t *surface;
530 GdkPixbuf *pixbuf;
531 GError *error = NULL;
532 int stride, i;
533 unsigned char *pixels, *p, *end, *dest_data;
534 int dest_stride;
535 uint32_t *d;
536
537 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
538 rect->width, rect->height,
539 FALSE, &error);
540 if (error != NULL)
541 return NULL;
542
543 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
544 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500545 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546 return NULL;
547 }
548
549 stride = gdk_pixbuf_get_rowstride(pixbuf);
550 pixels = gdk_pixbuf_get_pixels(pixbuf);
551
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000553 if (surface == NULL) {
554 g_object_unref(pixbuf);
555 return NULL;
556 }
557
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400558 dest_data = cairo_image_surface_get_data (surface);
559 dest_stride = cairo_image_surface_get_stride (surface);
560
561 for (i = 0; i < rect->height; i++) {
562 d = (uint32_t *) (dest_data + i * dest_stride);
563 p = pixels + i * stride;
564 end = p + rect->width * 4;
565 while (p < end) {
566 unsigned int t;
567 unsigned char a, r, g, b;
568
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569 a = p[3];
570 MULT(r, p[0], a, t);
571 MULT(g, p[1], a, t);
572 MULT(b, p[2], a, t);
573 p += 4;
574 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
575 }
576 }
577
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500578 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579
580 return surface;
581}
582
nobled7b87cb02011-02-01 18:51:47 +0000583static int
584check_size(struct rectangle *rect)
585{
586 if (rect->width && rect->height)
587 return 0;
588
589 fprintf(stderr, "tried to create surface of "
590 "width: %d, height: %d\n", rect->width, rect->height);
591 return -1;
592}
593
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400594cairo_surface_t *
595display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400597 struct rectangle *rectangle,
598 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400599{
nobled7b87cb02011-02-01 18:51:47 +0000600 if (check_size(rectangle) < 0)
601 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500602#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500603 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604 if (surface)
605 return display_create_egl_window_surface(display,
606 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400607 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100608 rectangle);
609 else
610 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400611 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100612 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500613 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400615 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616}
617
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500618static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619display_create_surface_from_file(struct display *display,
620 const char *filename,
621 struct rectangle *rectangle)
622{
nobled7b87cb02011-02-01 18:51:47 +0000623 if (check_size(rectangle) < 0)
624 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500625#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500626 if (display->dpy) {
627 return display_create_egl_image_surface_from_file(display,
628 filename,
629 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500630 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500632 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400633}
Yuval Fledel45568f62010-12-06 09:18:12 -0500634 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635 const char *filename;
636 int hotspot_x, hotspot_y;
637} pointer_images[] = {
638 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
639 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
640 { DATADIR "/wayland/bottom_side.png", 16, 20 },
641 { DATADIR "/wayland/grabbing.png", 20, 17 },
642 { DATADIR "/wayland/left_ptr.png", 10, 5 },
643 { DATADIR "/wayland/left_side.png", 10, 20 },
644 { DATADIR "/wayland/right_side.png", 30, 19 },
645 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
646 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
647 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400648 { DATADIR "/wayland/xterm.png", 15, 15 },
649 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650};
651
652static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400653create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400654{
655 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400657 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400659 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400660 display->pointer_surfaces =
661 malloc(count * sizeof *display->pointer_surfaces);
662 rect.width = width;
663 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400665 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400666 display_create_surface_from_file(display,
667 pointer_images[i].filename,
668 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100669 if (!display->pointer_surfaces[i]) {
670 fprintf(stderr, "Error loading pointer image: %s\n",
671 pointer_images[i].filename);
672 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673 }
674
675}
676
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400677cairo_surface_t *
678display_get_pointer_surface(struct display *display, int pointer,
679 int *width, int *height,
680 int *hotspot_x, int *hotspot_y)
681{
682 cairo_surface_t *surface;
683
684 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500685#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400686 *width = cairo_gl_surface_get_width(surface);
687 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000688#else
689 *width = cairo_image_surface_get_width(surface);
690 *height = cairo_image_surface_get_height(surface);
691#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692 *hotspot_x = pointer_images[pointer].hotspot_x;
693 *hotspot_y = pointer_images[pointer].hotspot_y;
694
695 return cairo_surface_reference(surface);
696}
697
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400698
699static void
700window_attach_surface(struct window *window);
701
702static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400703free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400704{
705 struct window *window = data;
706
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400707 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400708 cairo_surface_destroy(window->pending_surface);
709 window->pending_surface = NULL;
710 if (window->cairo_surface)
711 window_attach_surface(window);
712}
713
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400714static const struct wl_callback_listener free_surface_listener = {
715 free_surface
716};
717
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500718static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200719window_get_resize_dx_dy(struct window *window, int *x, int *y)
720{
721 if (window->resize_edges & WINDOW_RESIZING_LEFT)
722 *x = window->server_allocation.width - window->allocation.width;
723 else
724 *x = 0;
725
726 if (window->resize_edges & WINDOW_RESIZING_TOP)
727 *y = window->server_allocation.height -
728 window->allocation.height;
729 else
730 *y = 0;
731
732 window->resize_edges = 0;
733}
734
735static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400736window_set_type(struct window *window)
737{
738 struct display *display = window->display;
739
740 switch (window->type) {
741 case TYPE_FULLSCREEN:
742 wl_shell_set_fullscreen(display->shell, window->surface);
743 break;
744 case TYPE_TOPLEVEL:
745 wl_shell_set_toplevel(display->shell, window->surface);
746 break;
747 case TYPE_TRANSIENT:
748 wl_shell_set_transient(display->shell, window->surface,
749 window->parent->surface,
750 window->x, window->y, 0);
751 break;
752 case TYPE_CUSTOM:
753 break;
754 }
755}
756
757static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500758window_attach_surface(struct window *window)
759{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400760 struct display *display = window->display;
761 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000763#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100764 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000765#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500766 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100767
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400768 if (display->shell)
769 window_set_type(window);
770
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100771 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000772#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100773 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
774 data = cairo_surface_get_user_data(window->cairo_surface,
775 &surface_data_key);
776
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100777 cairo_gl_surface_swapbuffers(window->cairo_surface);
778 wl_egl_window_get_attached_size(data->window,
779 &window->server_allocation.width,
780 &window->server_allocation.height);
781 break;
782 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000783#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200785 window_get_resize_dx_dy(window, &x, &y);
786
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 if (window->pending_surface != NULL)
788 return;
789
790 window->pending_surface = window->cairo_surface;
791 window->cairo_surface = NULL;
792
793 buffer =
794 display_get_buffer_for_surface(display,
795 window->pending_surface);
796
797 wl_surface_attach(window->surface, buffer, x, y);
798 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400799 cb = wl_display_sync(display->display);
800 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000802 default:
803 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100804 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500805
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500806 wl_surface_damage(window->surface, 0, 0,
807 window->allocation.width,
808 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500809}
810
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500811void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400812window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500813{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100814 if (window->cairo_surface) {
815 switch (window->buffer_type) {
816 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
817 case WINDOW_BUFFER_TYPE_SHM:
818 display_surface_damage(window->display,
819 window->cairo_surface,
820 0, 0,
821 window->allocation.width,
822 window->allocation.height);
823 break;
824 default:
825 break;
826 }
827 window_attach_surface(window);
828 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500829}
830
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831void
832window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500834 cairo_surface_reference(surface);
835
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836 if (window->cairo_surface != NULL)
837 cairo_surface_destroy(window->cairo_surface);
838
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500839 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400840}
841
Benjamin Franzke22d54812011-07-16 19:50:32 +0000842#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843static void
844window_resize_cairo_window_surface(struct window *window)
845{
846 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200847 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848
849 data = cairo_surface_get_user_data(window->cairo_surface,
850 &surface_data_key);
851
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200852 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 wl_egl_window_resize(data->window,
854 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200855 window->allocation.height,
856 x,y);
857
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100858 cairo_gl_surface_set_size(window->cairo_surface,
859 window->allocation.width,
860 window->allocation.height);
861}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000862#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400864struct display *
865window_get_display(struct window *window)
866{
867 return window->display;
868}
869
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400870void
871window_create_surface(struct window *window)
872{
873 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400874 uint32_t flags = 0;
875
876 if (!window->transparent)
877 flags = SURFACE_OPAQUE;
878
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500880#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100881 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
882 if (window->cairo_surface) {
883 window_resize_cairo_window_surface(window);
884 return;
885 }
886 surface = display_create_surface(window->display,
887 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400888 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100889 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500890 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400893 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894 break;
895#endif
896 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400898 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400899 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800900 default:
901 surface = NULL;
902 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400904
905 window_set_surface(window, surface);
906 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400907}
908
909static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500910window_draw_menu(struct window *window)
911{
912 cairo_t *cr;
913 int width, height, r = 5;
914
915 window_create_surface(window);
916
917 cr = cairo_create(window->cairo_surface);
918 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
919 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
920 cairo_paint(cr);
921
922 width = window->allocation.width;
923 height = window->allocation.height;
924 rounded_rect(cr, r, r, width - r, height - r, r);
925 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
926 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
927 cairo_fill(cr);
928 cairo_destroy(cr);
929}
930
931static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500932window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500933{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500934 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500935 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400936 cairo_surface_t *frame;
937 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500938
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400939 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400941 width = window->allocation.width;
942 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500943
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500944 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500945
Kristian Høgsberg09531622010-06-14 23:22:15 -0400946 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400947 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400948 cairo_paint(cr);
949
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400950 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400951 tile_mask(cr, window->display->shadow,
952 shadow_dx, shadow_dy, width, height,
953 window->margin + 10 - shadow_dx,
954 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500955
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400956 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400957 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400958 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400959 frame = window->display->inactive_frame;
960
961 tile_source(cr, frame, 0, 0, width, height,
962 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500963
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500964 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
965 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500966 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400967 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400968 if (window->keyboard_device)
969 cairo_set_source_rgb(cr, 0, 0, 0);
970 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400971 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 cairo_show_text(cr, window->title);
973
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500974 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400975
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100976 /* FIXME: this breakes gears, fix cairo? */
977#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400978 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100979#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500980}
981
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400982void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500983window_destroy(struct window *window)
984{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200985 struct display *display = window->display;
986 struct input *input;
987
988 if (window->redraw_scheduled)
989 wl_list_remove(&window->redraw_task.link);
990
991 wl_list_for_each(input, &display->input_list, link) {
992 if (input->pointer_focus == window)
993 input->pointer_focus = NULL;
994 if (input->keyboard_focus == window)
995 input->keyboard_focus = NULL;
996 }
997
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500998 wl_surface_destroy(window->surface);
999 wl_list_remove(&window->link);
1000 free(window);
1001}
1002
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001003static struct item *
1004window_find_item(struct window *window, int32_t x, int32_t y)
1005{
1006 struct item *item;
1007
1008 wl_list_for_each(item, &window->item_list, link) {
1009 if (item->allocation.x <= x &&
1010 x < item->allocation.x + item->allocation.width &&
1011 item->allocation.y <= y &&
1012 y < item->allocation.y + item->allocation.height) {
1013 return item;
1014 }
1015 }
1016
1017 return NULL;
1018}
1019
1020struct item *
1021window_add_item(struct window *window, void *data)
1022{
1023 struct item *item;
1024
1025 item = malloc(sizeof *item);
1026 memset(item, 0, sizeof *item);
1027 item->user_data = data;
1028 wl_list_insert(window->item_list.prev, &item->link);
1029
1030 return item;
1031}
1032
1033void
1034window_for_each_item(struct window *window, item_func_t func, void *data)
1035{
1036 struct item *item;
1037
1038 wl_list_for_each(item, &window->item_list, link)
1039 func(item, data);
1040}
1041
1042struct item *
1043window_get_focus_item(struct window *window)
1044{
1045 return window->focus_item;
1046}
1047
1048void
1049item_get_allocation(struct item *item, struct rectangle *allocation)
1050{
1051 *allocation = item->allocation;
1052}
1053
1054void
1055item_set_allocation(struct item *item,
1056 int32_t x, int32_t y, int32_t width, int32_t height)
1057{
1058 item->allocation.x = x;
1059 item->allocation.y = y;
1060 item->allocation.width = width;
1061 item->allocation.height = height;
1062}
1063
1064void *
1065item_get_user_data(struct item *item)
1066{
1067 return item->user_data;
1068}
1069
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001070void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001071window_draw(struct window *window)
1072{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001073 if (window->parent)
1074 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001075 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001076 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001077 else
1078 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001079}
1080
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001081cairo_surface_t *
1082window_get_surface(struct window *window)
1083{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001084 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001085}
1086
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001087struct wl_surface *
1088window_get_wl_surface(struct window *window)
1089{
1090 return window->surface;
1091}
1092
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001093static int
1094get_pointer_location(struct window *window, int32_t x, int32_t y)
1095{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001096 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001097 const int grip_size = 8;
1098
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001099 if (!window->decoration)
1100 return WINDOW_CLIENT_AREA;
1101
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001102 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104 else if (window->margin <= x && x < window->margin + grip_size)
1105 hlocation = WINDOW_RESIZING_LEFT;
1106 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108 else if (x < window->allocation.width - window->margin)
1109 hlocation = WINDOW_RESIZING_RIGHT;
1110 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001112
1113 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115 else if (window->margin <= y && y < window->margin + grip_size)
1116 vlocation = WINDOW_RESIZING_TOP;
1117 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119 else if (y < window->allocation.height - window->margin)
1120 vlocation = WINDOW_RESIZING_BOTTOM;
1121 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001122 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001123
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001124 location = vlocation | hlocation;
1125 if (location & WINDOW_EXTERIOR)
1126 location = WINDOW_EXTERIOR;
1127 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1128 location = WINDOW_TITLEBAR;
1129 else if (location == WINDOW_INTERIOR)
1130 location = WINDOW_CLIENT_AREA;
1131
1132 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001133}
1134
1135static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001136set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001137{
1138 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001139 struct wl_buffer *buffer;
1140 cairo_surface_t *surface;
1141 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001142
1143 location = get_pointer_location(input->pointer_focus,
1144 input->sx, input->sy);
1145 switch (location) {
1146 case WINDOW_RESIZING_TOP:
1147 pointer = POINTER_TOP;
1148 break;
1149 case WINDOW_RESIZING_BOTTOM:
1150 pointer = POINTER_BOTTOM;
1151 break;
1152 case WINDOW_RESIZING_LEFT:
1153 pointer = POINTER_LEFT;
1154 break;
1155 case WINDOW_RESIZING_RIGHT:
1156 pointer = POINTER_RIGHT;
1157 break;
1158 case WINDOW_RESIZING_TOP_LEFT:
1159 pointer = POINTER_TOP_LEFT;
1160 break;
1161 case WINDOW_RESIZING_TOP_RIGHT:
1162 pointer = POINTER_TOP_RIGHT;
1163 break;
1164 case WINDOW_RESIZING_BOTTOM_LEFT:
1165 pointer = POINTER_BOTTOM_LEFT;
1166 break;
1167 case WINDOW_RESIZING_BOTTOM_RIGHT:
1168 pointer = POINTER_BOTTOM_RIGHT;
1169 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001170 case WINDOW_EXTERIOR:
1171 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001172 if (input->current_pointer_image == POINTER_DEFAULT)
1173 return;
1174
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001175 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001176 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001177 return;
1178 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001179 break;
1180 }
1181
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001182 if (pointer == input->current_pointer_image)
1183 return;
1184
1185 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001186 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001187
1188 if (!surface)
1189 return;
1190
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001191 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001192 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193 pointer_images[pointer].hotspot_x,
1194 pointer_images[pointer].hotspot_y);
1195}
1196
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001197static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001198window_set_focus_item(struct window *window, struct item *focus)
1199{
1200 void *data;
1201
1202 if (focus == window->focus_item)
1203 return;
1204
1205 window->focus_item = focus;
1206 data = focus ? focus->user_data : NULL;
1207 if (window->item_focus_handler)
1208 window->item_focus_handler(window, focus, data);
1209}
1210
1211static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001212window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001213 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001214 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001215{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001216 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001217 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001218 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001219 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001220
1221 input->x = x;
1222 input->y = y;
1223 input->sx = sx;
1224 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001226 if (!window->focus_item || !window->item_grab_button) {
1227 item = window_find_item(window, sx, sy);
1228 window_set_focus_item(window, item);
1229 }
1230
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001231 if (window->motion_handler)
1232 pointer = (*window->motion_handler)(window, input, time,
1233 x, y, sx, sy,
1234 window->user_data);
1235
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001236 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001237}
1238
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001239static void
1240window_handle_button(void *data,
1241 struct wl_input_device *input_device,
1242 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001243{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001244 struct input *input = data;
1245 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001246 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001247 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001248
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001249 if (window->focus_item && window->item_grab_button == 0 && state)
1250 window->item_grab_button = button;
1251
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001253
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001254 if (window->display->shell &&
1255 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001257 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001258 wl_shell_move(window->display->shell,
1259 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001260 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001261 case WINDOW_RESIZING_TOP:
1262 case WINDOW_RESIZING_BOTTOM:
1263 case WINDOW_RESIZING_LEFT:
1264 case WINDOW_RESIZING_RIGHT:
1265 case WINDOW_RESIZING_TOP_LEFT:
1266 case WINDOW_RESIZING_TOP_RIGHT:
1267 case WINDOW_RESIZING_BOTTOM_LEFT:
1268 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001269 wl_shell_resize(window->display->shell,
1270 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001272 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 case WINDOW_CLIENT_AREA:
1274 if (window->button_handler)
1275 (*window->button_handler)(window,
1276 input, time,
1277 button, state,
1278 window->user_data);
1279 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001280 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 } else {
1282 if (window->button_handler)
1283 (*window->button_handler)(window,
1284 input, time,
1285 button, state,
1286 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001287 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001288
1289 if (window->focus_item &&
1290 window->item_grab_button == button && !state) {
1291 window->item_grab_button = 0;
1292 item = window_find_item(window, input->sx, input->sy);
1293 window_set_focus_item(window, item);
1294 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001295}
1296
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001297static void
1298window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001299 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001300{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001301 struct input *input = data;
1302 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001303 struct display *d = window->display;
1304 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001305
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001306 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001307 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001308 return;
1309
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001310 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001311 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001312 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1313 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001314
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001315 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1316
1317 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001318 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001319 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001320 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001321
1322 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001323 (*window->key_handler)(window, input, time, key, sym, state,
1324 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001325}
1326
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001327static void
1328window_handle_pointer_focus(void *data,
1329 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001330 uint32_t time, struct wl_surface *surface,
1331 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001332{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001333 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001334 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001335 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001336 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001337
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001338 window = input->pointer_focus;
1339 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001340 window_set_focus_item(window, NULL);
1341
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001342 if (window->leave_handler)
1343 window->leave_handler(window, input,
1344 time, window->user_data);
1345 input->pointer_focus = NULL;
1346 input->current_pointer_image = POINTER_UNSET;
1347 }
1348
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001349 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001350 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001351 window = input->pointer_focus;
1352
Kristian Høgsberg59826582011-01-20 11:56:57 -05001353 input->x = x;
1354 input->y = y;
1355 input->sx = sx;
1356 input->sy = sy;
1357
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001358 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001359 if (window->enter_handler)
1360 pointer = window->enter_handler(window, input,
1361 time, sx, sy,
1362 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001363
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001364 item = window_find_item(window, x, y);
1365 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001366
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001367 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001368 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001369}
1370
1371static void
1372window_handle_keyboard_focus(void *data,
1373 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001374 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001375 struct wl_surface *surface,
1376 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001377{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001378 struct input *input = data;
1379 struct window *window = input->keyboard_focus;
1380 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001381 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001382
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001383 window = input->keyboard_focus;
1384 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001385 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001386 if (window->keyboard_focus_handler)
1387 (*window->keyboard_focus_handler)(window, NULL,
1388 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001389 }
1390
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001391 if (surface)
1392 input->keyboard_focus = wl_surface_get_user_data(surface);
1393 else
1394 input->keyboard_focus = NULL;
1395
1396 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001397 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001398 for (k = keys->data; k < end; k++)
1399 input->modifiers |= d->xkb->map->modmap[*k];
1400
1401 window = input->keyboard_focus;
1402 if (window) {
1403 window->keyboard_device = input;
1404 if (window->keyboard_focus_handler)
1405 (*window->keyboard_focus_handler)(window,
1406 window->keyboard_device,
1407 window->user_data);
1408 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001409}
1410
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001411static const struct wl_input_device_listener input_device_listener = {
1412 window_handle_motion,
1413 window_handle_button,
1414 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001415 window_handle_pointer_focus,
1416 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001417};
1418
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001419void
1420input_get_position(struct input *input, int32_t *x, int32_t *y)
1421{
1422 *x = input->sx;
1423 *y = input->sy;
1424}
1425
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001426struct wl_input_device *
1427input_get_input_device(struct input *input)
1428{
1429 return input->input_device;
1430}
1431
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001432uint32_t
1433input_get_modifiers(struct input *input)
1434{
1435 return input->modifiers;
1436}
1437
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001438struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001439window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001440{
Benjamin Franzke0c991632011-09-27 21:57:31 +02001441 cairo_device_flush (window->display->rgb_device);
1442 cairo_device_flush (window->display->argb_device);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001443
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001444 return wl_shell_create_drag(window->display->shell);
1445}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001446
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001447void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001448window_move(struct window *window, struct input *input, uint32_t time)
1449{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001450 if (window->display->shell)
1451 wl_shell_move(window->display->shell,
1452 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001453}
1454
1455void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001456window_activate_drag(struct wl_drag *drag, struct window *window,
1457 struct input *input, uint32_t time)
1458{
1459 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001460}
1461
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001462static void
1463handle_configure(void *data, struct wl_shell *shell,
1464 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001465 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001466{
1467 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001468 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001469
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001470 /* FIXME: this is probably the wrong place to check for width
1471 * or height <= 0, but it prevents the compositor from crashing
1472 */
1473 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001474 return;
1475
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001476 window->resize_edges = edges;
1477
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001478 if (window->resize_handler) {
1479 child_width = width - 20 - window->margin * 2;
1480 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001481
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001482 (*window->resize_handler)(window,
1483 child_width, child_height,
1484 window->user_data);
1485 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001486 window->allocation.width = width;
1487 window->allocation.height = height;
1488
1489 if (window->redraw_handler)
1490 window_schedule_redraw(window);
1491 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001492}
1493
1494static const struct wl_shell_listener shell_listener = {
1495 handle_configure,
1496};
1497
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001498void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001499window_get_allocation(struct window *window,
1500 struct rectangle *allocation)
1501{
1502 *allocation = window->allocation;
1503}
1504
1505void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001506window_get_child_allocation(struct window *window,
1507 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001508{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001509 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001510 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001511 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001512 allocation->x = window->margin + 10;
1513 allocation->y = window->margin + 50;
1514 allocation->width =
1515 window->allocation.width - 20 - window->margin * 2;
1516 allocation->height =
1517 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001518 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001519}
1520
1521void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001522window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001523{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001524 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001525 window->allocation.x = 20 + window->margin;
1526 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001527 window->allocation.width = width + 20 + window->margin * 2;
1528 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001529 } else {
1530 window->allocation.x = 0;
1531 window->allocation.y = 0;
1532 window->allocation.width = width;
1533 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001534 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001535}
1536
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001537static void
1538idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001539{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001540 struct window *window =
1541 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001542
1543 window->redraw_handler(window, window->user_data);
1544 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001545}
1546
1547void
1548window_schedule_redraw(struct window *window)
1549{
1550 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001551 window->redraw_task.run = idle_redraw;
1552 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001553 window->redraw_scheduled = 1;
1554 }
1555}
1556
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001557void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001558window_set_custom(struct window *window)
1559{
1560 window->type = TYPE_CUSTOM;
1561}
1562
1563void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001564window_set_fullscreen(struct window *window, int fullscreen)
1565{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001566 int32_t width, height;
1567
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001568 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001569 return;
1570
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001571 if (fullscreen) {
1572 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001573 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001574 width = window->display->screen_allocation.width;
1575 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001576 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001577 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001578 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001579 width = window->saved_allocation.width - 20 - window->margin * 2;
1580 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001581 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001582 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001583
1584 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001585}
1586
1587void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001588window_set_decoration(struct window *window, int decoration)
1589{
1590 window->decoration = decoration;
1591}
1592
1593void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001594window_set_user_data(struct window *window, void *data)
1595{
1596 window->user_data = data;
1597}
1598
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001599void *
1600window_get_user_data(struct window *window)
1601{
1602 return window->user_data;
1603}
1604
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001605void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001606window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001607 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001608{
1609 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001610}
1611
1612void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001613window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001614 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001615{
1616 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001617}
1618
1619void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001620window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001621 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001622{
1623 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001624}
1625
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001626void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001627window_set_button_handler(struct window *window,
1628 window_button_handler_t handler)
1629{
1630 window->button_handler = handler;
1631}
1632
1633void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001634window_set_motion_handler(struct window *window,
1635 window_motion_handler_t handler)
1636{
1637 window->motion_handler = handler;
1638}
1639
1640void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001641window_set_enter_handler(struct window *window,
1642 window_enter_handler_t handler)
1643{
1644 window->enter_handler = handler;
1645}
1646
1647void
1648window_set_leave_handler(struct window *window,
1649 window_leave_handler_t handler)
1650{
1651 window->leave_handler = handler;
1652}
1653
1654void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001655window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001656 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001657{
1658 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001659}
1660
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001661void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662window_set_item_focus_handler(struct window *window,
1663 window_item_focus_handler_t handler)
1664{
1665 window->item_focus_handler = handler;
1666}
1667
1668void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001669window_set_transparent(struct window *window, int transparent)
1670{
1671 window->transparent = transparent;
1672}
1673
1674void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001675window_set_title(struct window *window, const char *title)
1676{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001677 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001678 window->title = strdup(title);
1679}
1680
1681const char *
1682window_get_title(struct window *window)
1683{
1684 return window->title;
1685}
1686
1687void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001688display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1689 int32_t x, int32_t y, int32_t width, int32_t height)
1690{
1691 struct wl_buffer *buffer;
1692
1693 buffer = display_get_buffer_for_surface(display, cairo_surface);
1694
1695 wl_buffer_damage(buffer, x, y, width, height);
1696}
1697
1698void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001699window_damage(struct window *window, int32_t x, int32_t y,
1700 int32_t width, int32_t height)
1701{
1702 wl_surface_damage(window->surface, x, y, width, height);
1703}
1704
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001705static struct window *
1706window_create_internal(struct display *display, struct window *parent,
1707 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001708{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001709 struct window *window;
1710
1711 window = malloc(sizeof *window);
1712 if (window == NULL)
1713 return NULL;
1714
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001715 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001716 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001717 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001718 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001719 window->allocation.x = 0;
1720 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001721 window->allocation.width = width;
1722 window->allocation.height = height;
1723 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001724 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001725 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001726 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001727 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001728
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001729 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001730#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001731 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1732 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001733#else
1734 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1735#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001736 else
1737 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001738
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001739 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001740 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001741
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001742 return window;
1743}
1744
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001745struct window *
1746window_create(struct display *display, int32_t width, int32_t height)
1747{
1748 struct window *window;
1749
1750 window = window_create_internal(display, NULL, width, height);
1751 if (!window)
1752 return NULL;
1753
1754 return window;
1755}
1756
1757struct window *
1758window_create_transient(struct display *display, struct window *parent,
1759 int32_t x, int32_t y, int32_t width, int32_t height)
1760{
1761 struct window *window;
1762
1763 window = window_create_internal(parent->display,
1764 parent, width, height);
1765 if (!window)
1766 return NULL;
1767
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001768 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001769 window->x = x;
1770 window->y = y;
1771
1772 return window;
1773}
1774
1775void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001776window_set_buffer_type(struct window *window, enum window_buffer_type type)
1777{
1778 window->buffer_type = type;
1779}
1780
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001781
1782static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001783display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001784 struct wl_output *wl_output,
1785 int x, int y,
1786 int physical_width,
1787 int physical_height,
1788 int subpixel,
1789 const char *make,
1790 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001791{
1792 struct display *display = data;
1793
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001794 display->screen_allocation.x = x;
1795 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001796}
1797
1798static void
1799display_handle_mode(void *data,
1800 struct wl_output *wl_output,
1801 uint32_t flags,
1802 int width,
1803 int height,
1804 int refresh)
1805{
1806 struct display *display = data;
1807
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001808 display->screen_allocation.width = width;
1809 display->screen_allocation.height = height;
1810}
1811
1812static const struct wl_output_listener output_listener = {
1813 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001814 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001815};
1816
1817static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001818display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001819{
1820 struct input *input;
1821
1822 input = malloc(sizeof *input);
1823 if (input == NULL)
1824 return;
1825
1826 memset(input, 0, sizeof *input);
1827 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001828 input->input_device =
1829 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001830 input->pointer_focus = NULL;
1831 input->keyboard_focus = NULL;
1832 wl_list_insert(d->input_list.prev, &input->link);
1833
1834 wl_input_device_add_listener(input->input_device,
1835 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001836 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001837}
1838
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001839struct selection_offer {
1840 struct display *display;
1841 struct wl_selection_offer *offer;
1842 struct wl_array types;
1843 struct input *input;
1844};
1845
1846int
1847input_offers_mime_type(struct input *input, const char *type)
1848{
1849 struct selection_offer *offer = input->offer;
1850 char **p, **end;
1851
1852 if (offer == NULL)
1853 return 0;
1854
1855 end = offer->types.data + offer->types.size;
1856 for (p = offer->types.data; p < end; p++)
1857 if (strcmp(*p, type) == 0)
1858 return 1;
1859
1860 return 0;
1861}
1862
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001863void
1864input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001865{
1866 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001867
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001868 /* FIXME: A number of things can go wrong here: the object may
1869 * not be the current selection offer any more (which could
1870 * still work, but the source may have gone away or just
1871 * destroyed its wl_selection) or the offer may not have the
1872 * requested type after all (programmer/client error,
1873 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001874 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001875}
1876
1877static void
1878selection_offer_offer(void *data,
1879 struct wl_selection_offer *selection_offer,
1880 const char *type)
1881{
1882 struct selection_offer *offer = data;
1883
1884 char **p;
1885
1886 p = wl_array_add(&offer->types, sizeof *p);
1887 if (p)
1888 *p = strdup(type);
1889};
1890
1891static void
1892selection_offer_keyboard_focus(void *data,
1893 struct wl_selection_offer *selection_offer,
1894 struct wl_input_device *input_device)
1895{
1896 struct selection_offer *offer = data;
1897 struct input *input;
1898 char **p, **end;
1899
1900 if (input_device == NULL) {
1901 printf("selection offer retracted %p\n", selection_offer);
1902 input = offer->input;
1903 input->offer = NULL;
1904 wl_selection_offer_destroy(selection_offer);
1905 wl_array_release(&offer->types);
1906 free(offer);
1907 return;
1908 }
1909
1910 input = wl_input_device_get_user_data(input_device);
1911 printf("new selection offer %p:", selection_offer);
1912
1913 offer->input = input;
1914 input->offer = offer;
1915 end = offer->types.data + offer->types.size;
1916 for (p = offer->types.data; p < end; p++)
1917 printf(" %s", *p);
1918
1919 printf("\n");
1920}
1921
1922struct wl_selection_offer_listener selection_offer_listener = {
1923 selection_offer_offer,
1924 selection_offer_keyboard_focus
1925};
1926
1927static void
1928add_selection_offer(struct display *d, uint32_t id)
1929{
1930 struct selection_offer *offer;
1931
1932 offer = malloc(sizeof *offer);
1933 if (offer == NULL)
1934 return;
1935
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001936 offer->offer =
1937 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001938 offer->display = d;
1939 wl_array_init(&offer->types);
1940 offer->input = NULL;
1941
1942 wl_selection_offer_add_listener(offer->offer,
1943 &selection_offer_listener, offer);
1944}
1945
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001946static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001947display_handle_global(struct wl_display *display, uint32_t id,
1948 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001949{
1950 struct display *d = data;
1951
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001952 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001953 d->compositor =
1954 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001955 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001956 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001957 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001958 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001959 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001960 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001961 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001962 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001963 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001964 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001965 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001966 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001967 }
1968}
1969
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001970static void
1971display_render_frame(struct display *d)
1972{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001973 int radius = 8;
1974 cairo_t *cr;
1975
1976 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1977 cr = cairo_create(d->shadow);
1978 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1979 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1980 rounded_rect(cr, 16, 16, 112, 112, radius);
1981 cairo_fill(cr);
1982 cairo_destroy(cr);
1983 blur_surface(d->shadow, 64);
1984
1985 d->active_frame =
1986 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1987 cr = cairo_create(d->active_frame);
1988 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1989 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1990 rounded_rect(cr, 16, 16, 112, 112, radius);
1991 cairo_fill(cr);
1992 cairo_destroy(cr);
1993
1994 d->inactive_frame =
1995 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1996 cr = cairo_create(d->inactive_frame);
1997 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1998 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1999 rounded_rect(cr, 16, 16, 112, 112, radius);
2000 cairo_fill(cr);
2001 cairo_destroy(cr);
2002}
2003
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002004static void
2005init_xkb(struct display *d)
2006{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002007 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002008
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002009 names.rules = "evdev";
2010 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002011 names.layout = option_xkb_layout;
2012 names.variant = option_xkb_variant;
2013 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002014
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002015 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002016 if (!d->xkb) {
2017 fprintf(stderr, "Failed to compile keymap\n");
2018 exit(1);
2019 }
2020}
2021
Yuval Fledel45568f62010-12-06 09:18:12 -05002022static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002023init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002024{
2025 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002026 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002027
2028 static const EGLint premul_argb_cfg_attribs[] = {
2029 EGL_SURFACE_TYPE,
2030 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2031 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002032 EGL_RED_SIZE, 1,
2033 EGL_GREEN_SIZE, 1,
2034 EGL_BLUE_SIZE, 1,
2035 EGL_ALPHA_SIZE, 1,
2036 EGL_DEPTH_SIZE, 1,
2037 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2038 EGL_NONE
2039 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002040
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002041 static const EGLint rgb_cfg_attribs[] = {
2042 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2043 EGL_RED_SIZE, 1,
2044 EGL_GREEN_SIZE, 1,
2045 EGL_BLUE_SIZE, 1,
2046 EGL_ALPHA_SIZE, 0,
2047 EGL_DEPTH_SIZE, 1,
2048 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2049 EGL_NONE
2050 };
2051
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002052 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002053 if (!eglInitialize(d->dpy, &major, &minor)) {
2054 fprintf(stderr, "failed to initialize display\n");
2055 return -1;
2056 }
2057
2058 if (!eglBindAPI(EGL_OPENGL_API)) {
2059 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2060 return -1;
2061 }
2062
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002063 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002064 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002065 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002066 return -1;
2067 }
2068
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002069 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2070 &d->rgb_config, 1, &n) || n != 1) {
2071 fprintf(stderr, "failed to choose rgb config\n");
2072 return -1;
2073 }
2074
Benjamin Franzke0c991632011-09-27 21:57:31 +02002075 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2076 if (d->rgb_ctx == NULL) {
2077 fprintf(stderr, "failed to create context\n");
2078 return -1;
2079 }
2080 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2081 EGL_NO_CONTEXT, NULL);
2082 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002083 fprintf(stderr, "failed to create context\n");
2084 return -1;
2085 }
2086
Benjamin Franzke0c991632011-09-27 21:57:31 +02002087 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002088 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002089 return -1;
2090 }
2091
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002092#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002093 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2094 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002095 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002096 return -1;
2097 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002098 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2099 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2100 fprintf(stderr, "failed to get cairo egl argb device\n");
2101 return -1;
2102 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002103#endif
2104
2105 return 0;
2106}
2107
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002108static int
2109event_mask_update(uint32_t mask, void *data)
2110{
2111 struct display *d = data;
2112
2113 d->mask = mask;
2114
2115 return 0;
2116}
2117
2118static void
2119handle_display_data(struct task *task, uint32_t events)
2120{
2121 struct display *display =
2122 container_of(task, struct display, display_task);
2123
2124 wl_display_iterate(display->display, display->mask);
2125}
2126
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002127struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002128display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002129{
2130 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002131 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002132 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002133 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002134
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002135 g_type_init();
2136
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002137 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002138 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002139 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002140
2141 xkb_option_group = g_option_group_new("xkb",
2142 "Keyboard options",
2143 "Show all XKB options",
2144 NULL, NULL);
2145 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2146 g_option_context_add_group (context, xkb_option_group);
2147
2148 if (!g_option_context_parse(context, argc, argv, &error)) {
2149 fprintf(stderr, "option parsing failed: %s\n", error->message);
2150 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002151 }
2152
Tim Wiederhake748f6722011-01-23 23:25:25 +01002153 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002154
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002155 d = malloc(sizeof *d);
2156 if (d == NULL)
2157 return NULL;
2158
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002159 memset(d, 0, sizeof *d);
2160
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002161 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002162 if (d->display == NULL) {
2163 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002164 return NULL;
2165 }
2166
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002167 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2168 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2169 d->display_task.run = handle_display_data;
2170 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2171
2172 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002173 wl_list_init(&d->input_list);
2174
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002175 /* Set up listener so we'll catch all events. */
2176 wl_display_add_global_listener(d->display,
2177 display_handle_global, d);
2178
2179 /* Process connection events. */
2180 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002181 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002182 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002183
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002184 d->image_target_texture_2d =
2185 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2186 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2187 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2188
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002189 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002190
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002191 display_render_frame(d);
2192
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002193 wl_list_init(&d->window_list);
2194
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002195 init_xkb(d);
2196
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002197 return d;
2198}
2199
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002200struct wl_display *
2201display_get_display(struct display *display)
2202{
2203 return display->display;
2204}
2205
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002206struct wl_compositor *
2207display_get_compositor(struct display *display)
2208{
2209 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002210}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002211
2212EGLDisplay
2213display_get_egl_display(struct display *d)
2214{
2215 return d->dpy;
2216}
2217
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002218EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002219display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002220{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002221 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002222}
2223
Benjamin Franzke0c991632011-09-27 21:57:31 +02002224EGLConfig
2225display_get_argb_egl_config(struct display *d)
2226{
2227 return d->premultiplied_argb_config;
2228}
2229
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002230struct wl_shell *
2231display_get_shell(struct display *display)
2232{
2233 return display->shell;
2234}
2235
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002236int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002237display_acquire_window_surface(struct display *display,
2238 struct window *window,
2239 EGLContext ctx)
2240{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002241#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002242 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002243 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002244
2245 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002246 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002247 device = cairo_surface_get_device(window->cairo_surface);
2248 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002249 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002250
Benjamin Franzke0c991632011-09-27 21:57:31 +02002251 if (!ctx) {
2252 if (device == display->rgb_device)
2253 ctx = display->rgb_ctx;
2254 else if (device == display->argb_device)
2255 ctx = display->argb_ctx;
2256 else
2257 assert(0);
2258 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002259
2260 data = cairo_surface_get_user_data(window->cairo_surface,
2261 &surface_data_key);
2262
Benjamin Franzke0c991632011-09-27 21:57:31 +02002263 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002264 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2265 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002266
2267 return 0;
2268#else
2269 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002270#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002271}
2272
2273void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002274display_release_window_surface(struct display *display,
2275 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002276{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002277#ifdef HAVE_CAIRO_EGL
2278 cairo_device_t *device;
2279
2280 device = cairo_surface_get_device(window->cairo_surface);
2281 if (!device)
2282 return;
2283
2284 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002285 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002286 cairo_device_release(device);
2287#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002288}
2289
2290void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002291display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002292{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002293 wl_list_insert(&display->deferred_list, &task->link);
2294}
2295
2296void
2297display_watch_fd(struct display *display,
2298 int fd, uint32_t events, struct task *task)
2299{
2300 struct epoll_event ep;
2301
2302 ep.events = events;
2303 ep.data.ptr = task;
2304 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2305}
2306
2307void
2308display_run(struct display *display)
2309{
2310 struct task *task;
2311 struct epoll_event ep[16];
2312 int i, count;
2313
2314 while (1) {
2315 while (display->mask & WL_DISPLAY_WRITABLE)
2316 wl_display_iterate(display->display,
2317 WL_DISPLAY_WRITABLE);
2318
2319 count = epoll_wait(display->epoll_fd,
2320 ep, ARRAY_LENGTH(ep), -1);
2321 for (i = 0; i < count; i++) {
2322 task = ep[i].data.ptr;
2323 task->run(task, ep[i].events);
2324 }
2325
2326 while (!wl_list_empty(&display->deferred_list)) {
2327 task = container_of(display->deferred_list.next,
2328 struct task, link);
2329 wl_list_remove(&task->link);
2330 task->run(task, 0);
2331 }
2332 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002333}