blob: 0f242500d8dcb9d5185584635d6a8274146cfdea [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øgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct wl_output *output;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020069 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 EGLContext rgb_ctx;
71 EGLContext argb_ctx;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040074
75 int display_fd;
76 uint32_t mask;
77 struct task display_task;
78
79 int epoll_fd;
80 struct wl_list deferred_list;
81
Kristian Høgsberg478d9262010-06-08 20:34:11 -040082 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040083 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050084 struct wl_list output_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040085 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050093};
94
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040095enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040096 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040097 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040098 TYPE_TRANSIENT,
99 TYPE_CUSTOM
100};
101
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102struct window {
103 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500104 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200106 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500107 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500108 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400110 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400111 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400112 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500113 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500114 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400116 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400117 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400118 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400120 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500121
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400122 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500123 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500124
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500125 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400126 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500127 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400128 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500129 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400130 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400131 window_enter_handler_t enter_handler;
132 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400133 window_item_focus_handler_t item_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400134 window_data_handler_t data_handler;
135 window_drop_handler_t drop_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400136
137 struct wl_list item_list;
138 struct item *focus_item;
139 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400140
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500141 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400142 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500143};
144
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400145struct item {
146 struct wl_list link;
147 struct rectangle allocation;
148 void *user_data;
149};
150
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400151struct input {
152 struct display *display;
153 struct wl_input_device *input_device;
154 struct window *pointer_focus;
155 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400156 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400157 uint32_t modifiers;
158 int32_t x, y, sx, sy;
159 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400160
161 struct wl_data_device *data_device;
162 struct data_offer *drag_offer;
163 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164};
165
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500166struct output {
167 struct display *display;
168 struct wl_output *output;
169 struct rectangle allocation;
170 struct wl_list link;
171};
172
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400173enum {
174 POINTER_DEFAULT = 100,
175 POINTER_UNSET
176};
177
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500178enum window_location {
179 WINDOW_INTERIOR = 0,
180 WINDOW_RESIZING_TOP = 1,
181 WINDOW_RESIZING_BOTTOM = 2,
182 WINDOW_RESIZING_LEFT = 4,
183 WINDOW_RESIZING_TOP_LEFT = 5,
184 WINDOW_RESIZING_BOTTOM_LEFT = 6,
185 WINDOW_RESIZING_RIGHT = 8,
186 WINDOW_RESIZING_TOP_RIGHT = 9,
187 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
188 WINDOW_RESIZING_MASK = 15,
189 WINDOW_EXTERIOR = 16,
190 WINDOW_TITLEBAR = 17,
191 WINDOW_CLIENT_AREA = 18,
192};
193
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400194const char *option_xkb_layout = "us";
195const char *option_xkb_variant = "";
196const char *option_xkb_options = "";
197
198static const GOptionEntry xkb_option_entries[] = {
199 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
200 &option_xkb_layout, "XKB Layout" },
201 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
202 &option_xkb_variant, "XKB Variant" },
203 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
204 &option_xkb_options, "XKB Options" },
205 { NULL }
206};
207
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400208static const cairo_user_data_key_t surface_data_key;
209struct surface_data {
210 struct wl_buffer *buffer;
211};
212
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500213#define MULT(_d,c,a,t) \
214 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
215
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500216#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400217
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100218struct egl_window_surface_data {
219 struct display *display;
220 struct wl_surface *surface;
221 struct wl_egl_window *window;
222 EGLSurface surf;
223};
224
225static void
226egl_window_surface_data_destroy(void *p)
227{
228 struct egl_window_surface_data *data = p;
229 struct display *d = data->display;
230
231 eglDestroySurface(d->dpy, data->surf);
232 wl_egl_window_destroy(data->window);
233 data->surface = NULL;
234
235 free(p);
236}
237
238static cairo_surface_t *
239display_create_egl_window_surface(struct display *display,
240 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400241 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100242 struct rectangle *rectangle)
243{
244 cairo_surface_t *cairo_surface;
245 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400246 EGLConfig config;
247 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200248 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100249
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400250 static const EGLint premul_attribs[] = {
251 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
252 EGL_NONE
253 };
254
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100255 data = malloc(sizeof *data);
256 if (data == NULL)
257 return NULL;
258
259 data->display = display;
260 data->surface = surface;
261
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400262 if (flags & SURFACE_OPAQUE) {
263 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200264 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200265 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400266 } else {
267 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200268 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400269 attribs = premul_attribs;
270 }
271
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400272 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100273 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400274 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 data->surf = eglCreateWindowSurface(display->dpy, config,
277 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278
Benjamin Franzke0c991632011-09-27 21:57:31 +0200279 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100280 data->surf,
281 rectangle->width,
282 rectangle->height);
283
284 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
285 data, egl_window_surface_data_destroy);
286
287 return cairo_surface;
288}
289
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500290struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400291 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200292 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400293 EGLImageKHR image;
294 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800295 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500296 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400297};
298
299static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400301{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500302 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800303 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400304
Benjamin Franzke0c991632011-09-27 21:57:31 +0200305 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400306 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200307 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800308
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500309 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500311 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313}
314
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500315EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316display_get_image_for_egl_image_surface(struct display *display,
317 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500318{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500320
321 data = cairo_surface_get_user_data (surface, &surface_data_key);
322
323 return data->image;
324}
325
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500326static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500327display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400328 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 EGLDisplay dpy = display->dpy;
333 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200334 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
336 data = malloc(sizeof *data);
337 if (data == NULL)
338 return NULL;
339
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800340 data->display = display;
341
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400342 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400343 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500344 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000345 free(data);
346 return NULL;
347 }
348
Benjamin Franzke0c991632011-09-27 21:57:31 +0200349 if (flags & SURFACE_OPAQUE) {
350 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200351 content = CAIRO_CONTENT_COLOR;
352 } else {
353 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200354 content = CAIRO_CONTENT_COLOR_ALPHA;
355 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400356
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500357 data->image = display->create_image(dpy, NULL,
358 EGL_NATIVE_PIXMAP_KHR,
359 (EGLClientBuffer) data->pixmap,
360 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500361 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500362 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363 free(data);
364 return NULL;
365 }
366
367 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400368 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500369
Benjamin Franzke0c991632011-09-27 21:57:31 +0200370 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400371 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400372 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500373 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400375
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 surface = cairo_gl_surface_create_for_texture(data->device,
377 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400378 data->texture,
379 rectangle->width,
380 rectangle->height);
381
382 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400384
385 return surface;
386}
387
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500388static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500389display_create_egl_image_surface_from_file(struct display *display,
390 const char *filename,
391 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392{
393 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400394 GdkPixbuf *pixbuf;
395 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400396 int stride, i;
397 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500398 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400399
400 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400402 FALSE, &error);
403 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400405
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400406 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
407 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500408 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400410 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400411
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400412
413 stride = gdk_pixbuf_get_rowstride(pixbuf);
414 pixels = gdk_pixbuf_get_pixels(pixbuf);
415
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400417 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400419 while (p < end) {
420 unsigned int t;
421
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400422 MULT(p[0], p[0], p[3], t);
423 MULT(p[1], p[1], p[3], t);
424 MULT(p[2], p[2], p[3], t);
425 p += 4;
426
427 }
428 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400429
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200430 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000431 if (surface == NULL) {
432 g_object_unref(pixbuf);
433 return NULL;
434 }
435
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800436 data = cairo_surface_get_user_data(surface, &surface_data_key);
437
Benjamin Franzke0c991632011-09-27 21:57:31 +0200438 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800439 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800440 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
441 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200442 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400443
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500444 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400445
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400446 return surface;
447}
448
449#endif
450
451struct wl_buffer *
452display_get_buffer_for_surface(struct display *display,
453 cairo_surface_t *surface)
454{
455 struct surface_data *data;
456
457 data = cairo_surface_get_user_data (surface, &surface_data_key);
458
459 return data->buffer;
460}
461
462struct shm_surface_data {
463 struct surface_data data;
464 void *map;
465 size_t length;
466};
467
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500468static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469shm_surface_data_destroy(void *p)
470{
471 struct shm_surface_data *data = p;
472
473 wl_buffer_destroy(data->data.buffer);
474 munmap(data->map, data->length);
475}
476
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500477static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400478display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400479 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400480{
481 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400482 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400483 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800484 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485 char filename[] = "/tmp/wayland-shm-XXXXXX";
486
487 data = malloc(sizeof *data);
488 if (data == NULL)
489 return NULL;
490
491 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
492 rectangle->width);
493 data->length = stride * rectangle->height;
494 fd = mkstemp(filename);
495 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000496 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497 return NULL;
498 }
499 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000500 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501 close(fd);
502 return NULL;
503 }
504
505 data->map = mmap(NULL, data->length,
506 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
507 unlink(filename);
508
509 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000510 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511 close(fd);
512 return NULL;
513 }
514
515 surface = cairo_image_surface_create_for_data (data->map,
516 CAIRO_FORMAT_ARGB32,
517 rectangle->width,
518 rectangle->height,
519 stride);
520
521 cairo_surface_set_user_data (surface, &surface_data_key,
522 data, shm_surface_data_destroy);
523
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400524 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400525 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400526 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400527 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400528
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529 data->data.buffer = wl_shm_create_buffer(display->shm,
530 fd,
531 rectangle->width,
532 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400533 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534
535 close(fd);
536
537 return surface;
538}
539
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500540static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541display_create_shm_surface_from_file(struct display *display,
542 const char *filename,
543 struct rectangle *rect)
544{
545 cairo_surface_t *surface;
546 GdkPixbuf *pixbuf;
547 GError *error = NULL;
548 int stride, i;
549 unsigned char *pixels, *p, *end, *dest_data;
550 int dest_stride;
551 uint32_t *d;
552
553 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
554 rect->width, rect->height,
555 FALSE, &error);
556 if (error != NULL)
557 return NULL;
558
559 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
560 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500561 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562 return NULL;
563 }
564
565 stride = gdk_pixbuf_get_rowstride(pixbuf);
566 pixels = gdk_pixbuf_get_pixels(pixbuf);
567
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400568 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000569 if (surface == NULL) {
570 g_object_unref(pixbuf);
571 return NULL;
572 }
573
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574 dest_data = cairo_image_surface_get_data (surface);
575 dest_stride = cairo_image_surface_get_stride (surface);
576
577 for (i = 0; i < rect->height; i++) {
578 d = (uint32_t *) (dest_data + i * dest_stride);
579 p = pixels + i * stride;
580 end = p + rect->width * 4;
581 while (p < end) {
582 unsigned int t;
583 unsigned char a, r, g, b;
584
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585 a = p[3];
586 MULT(r, p[0], a, t);
587 MULT(g, p[1], a, t);
588 MULT(b, p[2], a, t);
589 p += 4;
590 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
591 }
592 }
593
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500594 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595
596 return surface;
597}
598
nobled7b87cb02011-02-01 18:51:47 +0000599static int
600check_size(struct rectangle *rect)
601{
602 if (rect->width && rect->height)
603 return 0;
604
605 fprintf(stderr, "tried to create surface of "
606 "width: %d, height: %d\n", rect->width, rect->height);
607 return -1;
608}
609
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400610cairo_surface_t *
611display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100612 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400613 struct rectangle *rectangle,
614 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615{
nobled7b87cb02011-02-01 18:51:47 +0000616 if (check_size(rectangle) < 0)
617 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500618#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500619 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620 if (surface)
621 return display_create_egl_window_surface(display,
622 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400623 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100624 rectangle);
625 else
626 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400627 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100628 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500629 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400631 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632}
633
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500634static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635display_create_surface_from_file(struct display *display,
636 const char *filename,
637 struct rectangle *rectangle)
638{
nobled7b87cb02011-02-01 18:51:47 +0000639 if (check_size(rectangle) < 0)
640 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500642 if (display->dpy) {
643 return display_create_egl_image_surface_from_file(display,
644 filename,
645 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500646 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500648 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400649}
Yuval Fledel45568f62010-12-06 09:18:12 -0500650 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400651 const char *filename;
652 int hotspot_x, hotspot_y;
653} pointer_images[] = {
654 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
655 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
656 { DATADIR "/wayland/bottom_side.png", 16, 20 },
657 { DATADIR "/wayland/grabbing.png", 20, 17 },
658 { DATADIR "/wayland/left_ptr.png", 10, 5 },
659 { DATADIR "/wayland/left_side.png", 10, 20 },
660 { DATADIR "/wayland/right_side.png", 30, 19 },
661 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
662 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
663 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400664 { DATADIR "/wayland/xterm.png", 15, 15 },
665 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400666};
667
668static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400669create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670{
671 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400673 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400675 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400676 display->pointer_surfaces =
677 malloc(count * sizeof *display->pointer_surfaces);
678 rect.width = width;
679 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400680 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400681 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682 display_create_surface_from_file(display,
683 pointer_images[i].filename,
684 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100685 if (!display->pointer_surfaces[i]) {
686 fprintf(stderr, "Error loading pointer image: %s\n",
687 pointer_images[i].filename);
688 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689 }
690
691}
692
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400693cairo_surface_t *
694display_get_pointer_surface(struct display *display, int pointer,
695 int *width, int *height,
696 int *hotspot_x, int *hotspot_y)
697{
698 cairo_surface_t *surface;
699
700 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500701#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 *width = cairo_gl_surface_get_width(surface);
703 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000704#else
705 *width = cairo_image_surface_get_width(surface);
706 *height = cairo_image_surface_get_height(surface);
707#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 *hotspot_x = pointer_images[pointer].hotspot_x;
709 *hotspot_y = pointer_images[pointer].hotspot_y;
710
711 return cairo_surface_reference(surface);
712}
713
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400714static void
715window_attach_surface(struct window *window);
716
717static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400718free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400719{
720 struct window *window = data;
721
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400722 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400723 cairo_surface_destroy(window->pending_surface);
724 window->pending_surface = NULL;
725 if (window->cairo_surface)
726 window_attach_surface(window);
727}
728
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400729static const struct wl_callback_listener free_surface_listener = {
730 free_surface
731};
732
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500733static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200734window_get_resize_dx_dy(struct window *window, int *x, int *y)
735{
736 if (window->resize_edges & WINDOW_RESIZING_LEFT)
737 *x = window->server_allocation.width - window->allocation.width;
738 else
739 *x = 0;
740
741 if (window->resize_edges & WINDOW_RESIZING_TOP)
742 *y = window->server_allocation.height -
743 window->allocation.height;
744 else
745 *y = 0;
746
747 window->resize_edges = 0;
748}
749
750static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400751window_set_type(struct window *window)
752{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400753 switch (window->type) {
754 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200755 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400756 break;
757 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200758 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400759 break;
760 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200761 wl_shell_surface_set_transient(window->shell_surface,
762 window->parent->shell_surface,
763 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400764 break;
765 case TYPE_CUSTOM:
766 break;
767 }
768}
769
770static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500771window_attach_surface(struct window *window)
772{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400773 struct display *display = window->display;
774 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400775 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000776#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100777 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000778#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500779 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400781 if (display->shell)
782 window_set_type(window);
783
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000785#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
787 data = cairo_surface_get_user_data(window->cairo_surface,
788 &surface_data_key);
789
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790 cairo_gl_surface_swapbuffers(window->cairo_surface);
791 wl_egl_window_get_attached_size(data->window,
792 &window->server_allocation.width,
793 &window->server_allocation.height);
794 break;
795 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000796#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100797 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200798 window_get_resize_dx_dy(window, &x, &y);
799
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 if (window->pending_surface != NULL)
801 return;
802
803 window->pending_surface = window->cairo_surface;
804 window->cairo_surface = NULL;
805
806 buffer =
807 display_get_buffer_for_surface(display,
808 window->pending_surface);
809
810 wl_surface_attach(window->surface, buffer, x, y);
811 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400812 cb = wl_display_sync(display->display);
813 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815 default:
816 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500818
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500819 wl_surface_damage(window->surface, 0, 0,
820 window->allocation.width,
821 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500822}
823
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500824void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400825window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500826{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100827 if (window->cairo_surface) {
828 switch (window->buffer_type) {
829 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
830 case WINDOW_BUFFER_TYPE_SHM:
831 display_surface_damage(window->display,
832 window->cairo_surface,
833 0, 0,
834 window->allocation.width,
835 window->allocation.height);
836 break;
837 default:
838 break;
839 }
840 window_attach_surface(window);
841 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500842}
843
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400844void
845window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500847 cairo_surface_reference(surface);
848
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400849 if (window->cairo_surface != NULL)
850 cairo_surface_destroy(window->cairo_surface);
851
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500852 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400853}
854
Benjamin Franzke22d54812011-07-16 19:50:32 +0000855#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856static void
857window_resize_cairo_window_surface(struct window *window)
858{
859 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200860 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861
862 data = cairo_surface_get_user_data(window->cairo_surface,
863 &surface_data_key);
864
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200865 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100866 wl_egl_window_resize(data->window,
867 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200868 window->allocation.height,
869 x,y);
870
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100871 cairo_gl_surface_set_size(window->cairo_surface,
872 window->allocation.width,
873 window->allocation.height);
874}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000875#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400877struct display *
878window_get_display(struct window *window)
879{
880 return window->display;
881}
882
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400883void
884window_create_surface(struct window *window)
885{
886 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400887 uint32_t flags = 0;
888
889 if (!window->transparent)
890 flags = SURFACE_OPAQUE;
891
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400892 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500893#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
895 if (window->cairo_surface) {
896 window_resize_cairo_window_surface(window);
897 return;
898 }
899 surface = display_create_surface(window->display,
900 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400901 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500903 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400904 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400906 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400907 break;
908#endif
909 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400910 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400911 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400912 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800913 default:
914 surface = NULL;
915 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400916 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400917
918 window_set_surface(window, surface);
919 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400920}
921
922static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500923window_draw_menu(struct window *window)
924{
925 cairo_t *cr;
926 int width, height, r = 5;
927
928 window_create_surface(window);
929
930 cr = cairo_create(window->cairo_surface);
931 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
932 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
933 cairo_paint(cr);
934
935 width = window->allocation.width;
936 height = window->allocation.height;
937 rounded_rect(cr, r, r, width - r, height - r, r);
938 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
939 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
940 cairo_fill(cr);
941 cairo_destroy(cr);
942}
943
944static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500945window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500946{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500947 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500948 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400949 cairo_surface_t *frame;
950 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500951
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400952 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400954 width = window->allocation.width;
955 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500956
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500957 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500958
Kristian Høgsberg09531622010-06-14 23:22:15 -0400959 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400960 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400961 cairo_paint(cr);
962
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400963 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400964 tile_mask(cr, window->display->shadow,
965 shadow_dx, shadow_dy, width, height,
966 window->margin + 10 - shadow_dx,
967 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500968
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400969 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400970 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400972 frame = window->display->inactive_frame;
973
974 tile_source(cr, frame, 0, 0, width, height,
975 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500976
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500977 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
978 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500979 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400980 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400981 if (window->keyboard_device)
982 cairo_set_source_rgb(cr, 0, 0, 0);
983 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400984 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400985 cairo_show_text(cr, window->title);
986
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500987 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400988
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100989 /* FIXME: this breakes gears, fix cairo? */
990#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400991 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100992#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500993}
994
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400995void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500996window_destroy(struct window *window)
997{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200998 struct display *display = window->display;
999 struct input *input;
1000
1001 if (window->redraw_scheduled)
1002 wl_list_remove(&window->redraw_task.link);
1003
1004 wl_list_for_each(input, &display->input_list, link) {
1005 if (input->pointer_focus == window)
1006 input->pointer_focus = NULL;
1007 if (input->keyboard_focus == window)
1008 input->keyboard_focus = NULL;
1009 }
1010
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001011 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001012 wl_surface_destroy(window->surface);
1013 wl_list_remove(&window->link);
1014 free(window);
1015}
1016
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017static struct item *
1018window_find_item(struct window *window, int32_t x, int32_t y)
1019{
1020 struct item *item;
1021
1022 wl_list_for_each(item, &window->item_list, link) {
1023 if (item->allocation.x <= x &&
1024 x < item->allocation.x + item->allocation.width &&
1025 item->allocation.y <= y &&
1026 y < item->allocation.y + item->allocation.height) {
1027 return item;
1028 }
1029 }
1030
1031 return NULL;
1032}
1033
1034struct item *
1035window_add_item(struct window *window, void *data)
1036{
1037 struct item *item;
1038
1039 item = malloc(sizeof *item);
1040 memset(item, 0, sizeof *item);
1041 item->user_data = data;
1042 wl_list_insert(window->item_list.prev, &item->link);
1043
1044 return item;
1045}
1046
1047void
1048window_for_each_item(struct window *window, item_func_t func, void *data)
1049{
1050 struct item *item;
1051
1052 wl_list_for_each(item, &window->item_list, link)
1053 func(item, data);
1054}
1055
1056struct item *
1057window_get_focus_item(struct window *window)
1058{
1059 return window->focus_item;
1060}
1061
1062void
1063item_get_allocation(struct item *item, struct rectangle *allocation)
1064{
1065 *allocation = item->allocation;
1066}
1067
1068void
1069item_set_allocation(struct item *item,
1070 int32_t x, int32_t y, int32_t width, int32_t height)
1071{
1072 item->allocation.x = x;
1073 item->allocation.y = y;
1074 item->allocation.width = width;
1075 item->allocation.height = height;
1076}
1077
1078void *
1079item_get_user_data(struct item *item)
1080{
1081 return item->user_data;
1082}
1083
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001084void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001085window_draw(struct window *window)
1086{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001087 if (window->parent)
1088 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001089 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001090 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001091 else
1092 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001093}
1094
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001095cairo_surface_t *
1096window_get_surface(struct window *window)
1097{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001098 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001099}
1100
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001101struct wl_surface *
1102window_get_wl_surface(struct window *window)
1103{
1104 return window->surface;
1105}
1106
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001107struct wl_shell_surface *
1108window_get_wl_shell_surface(struct window *window)
1109{
1110 return window->shell_surface;
1111}
1112
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001113static int
1114get_pointer_location(struct window *window, int32_t x, int32_t y)
1115{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001116 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001117 const int grip_size = 8;
1118
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001119 if (!window->decoration)
1120 return WINDOW_CLIENT_AREA;
1121
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001122 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001123 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 else if (window->margin <= x && x < window->margin + grip_size)
1125 hlocation = WINDOW_RESIZING_LEFT;
1126 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001128 else if (x < window->allocation.width - window->margin)
1129 hlocation = WINDOW_RESIZING_RIGHT;
1130 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132
1133 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001134 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001135 else if (window->margin <= y && y < window->margin + grip_size)
1136 vlocation = WINDOW_RESIZING_TOP;
1137 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001138 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139 else if (y < window->allocation.height - window->margin)
1140 vlocation = WINDOW_RESIZING_BOTTOM;
1141 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001144 location = vlocation | hlocation;
1145 if (location & WINDOW_EXTERIOR)
1146 location = WINDOW_EXTERIOR;
1147 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1148 location = WINDOW_TITLEBAR;
1149 else if (location == WINDOW_INTERIOR)
1150 location = WINDOW_CLIENT_AREA;
1151
1152 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001153}
1154
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001155void
1156input_set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001157{
1158 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001159 struct wl_buffer *buffer;
1160 cairo_surface_t *surface;
1161 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001162
1163 location = get_pointer_location(input->pointer_focus,
1164 input->sx, input->sy);
1165 switch (location) {
1166 case WINDOW_RESIZING_TOP:
1167 pointer = POINTER_TOP;
1168 break;
1169 case WINDOW_RESIZING_BOTTOM:
1170 pointer = POINTER_BOTTOM;
1171 break;
1172 case WINDOW_RESIZING_LEFT:
1173 pointer = POINTER_LEFT;
1174 break;
1175 case WINDOW_RESIZING_RIGHT:
1176 pointer = POINTER_RIGHT;
1177 break;
1178 case WINDOW_RESIZING_TOP_LEFT:
1179 pointer = POINTER_TOP_LEFT;
1180 break;
1181 case WINDOW_RESIZING_TOP_RIGHT:
1182 pointer = POINTER_TOP_RIGHT;
1183 break;
1184 case WINDOW_RESIZING_BOTTOM_LEFT:
1185 pointer = POINTER_BOTTOM_LEFT;
1186 break;
1187 case WINDOW_RESIZING_BOTTOM_RIGHT:
1188 pointer = POINTER_BOTTOM_RIGHT;
1189 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001190 case WINDOW_EXTERIOR:
1191 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001192 if (input->current_pointer_image == POINTER_DEFAULT)
1193 return;
1194
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001195 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001196 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197 return;
1198 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001199 break;
1200 }
1201
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001202 if (pointer == input->current_pointer_image)
1203 return;
1204
1205 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001206 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001207
1208 if (!surface)
1209 return;
1210
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001211 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001212 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213 pointer_images[pointer].hotspot_x,
1214 pointer_images[pointer].hotspot_y);
1215}
1216
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001217static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001218window_set_focus_item(struct window *window, struct item *focus)
1219{
1220 void *data;
1221
1222 if (focus == window->focus_item)
1223 return;
1224
1225 window->focus_item = focus;
1226 data = focus ? focus->user_data : NULL;
1227 if (window->item_focus_handler)
1228 window->item_focus_handler(window, focus, data);
1229}
1230
1231static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001232window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001233 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001234 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001235{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001236 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001237 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001238 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001239 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001240
1241 input->x = x;
1242 input->y = y;
1243 input->sx = sx;
1244 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001246 if (!window->focus_item || !window->item_grab_button) {
1247 item = window_find_item(window, sx, sy);
1248 window_set_focus_item(window, item);
1249 }
1250
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001251 if (window->motion_handler)
1252 pointer = (*window->motion_handler)(window, input, time,
1253 x, y, sx, sy,
1254 window->user_data);
1255
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001256 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001257}
1258
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001259static void
1260window_handle_button(void *data,
1261 struct wl_input_device *input_device,
1262 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001263{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001264 struct input *input = data;
1265 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001266 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001268
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001269 if (window->focus_item && window->item_grab_button == 0 && state)
1270 window->item_grab_button = button;
1271
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001273
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001274 if (window->display->shell &&
1275 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 case WINDOW_TITLEBAR:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001278 wl_shell_surface_move(window->shell_surface,
1279 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001280 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001281 case WINDOW_RESIZING_TOP:
1282 case WINDOW_RESIZING_BOTTOM:
1283 case WINDOW_RESIZING_LEFT:
1284 case WINDOW_RESIZING_RIGHT:
1285 case WINDOW_RESIZING_TOP_LEFT:
1286 case WINDOW_RESIZING_TOP_RIGHT:
1287 case WINDOW_RESIZING_BOTTOM_LEFT:
1288 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001289 wl_shell_surface_resize(window->shell_surface,
1290 input_device, time,
1291 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001292 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001293 case WINDOW_CLIENT_AREA:
1294 if (window->button_handler)
1295 (*window->button_handler)(window,
1296 input, time,
1297 button, state,
1298 window->user_data);
1299 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001300 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301 } else {
1302 if (window->button_handler)
1303 (*window->button_handler)(window,
1304 input, time,
1305 button, state,
1306 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001307 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001308
1309 if (window->focus_item &&
1310 window->item_grab_button == button && !state) {
1311 window->item_grab_button = 0;
1312 item = window_find_item(window, input->sx, input->sy);
1313 window_set_focus_item(window, item);
1314 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001315}
1316
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001317static void
1318window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001319 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001320{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001321 struct input *input = data;
1322 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001323 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001324 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001325
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001326 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001327 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001328 return;
1329
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001330 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001331 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001332 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1333 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001334
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001335 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1336
1337 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001338 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001339 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001340 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001341
1342 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001343 (*window->key_handler)(window, input, time, key, sym, state,
1344 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001345}
1346
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001347static void
1348window_handle_pointer_focus(void *data,
1349 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001350 uint32_t time, struct wl_surface *surface,
1351 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001352{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001353 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001354 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001355 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001356 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001357
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001358 window = input->pointer_focus;
1359 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001360 window_set_focus_item(window, NULL);
1361
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001362 if (window->leave_handler)
1363 window->leave_handler(window, input,
1364 time, window->user_data);
1365 input->pointer_focus = NULL;
1366 input->current_pointer_image = POINTER_UNSET;
1367 }
1368
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001369 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001370 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001371 window = input->pointer_focus;
1372
Kristian Høgsberg59826582011-01-20 11:56:57 -05001373 input->x = x;
1374 input->y = y;
1375 input->sx = sx;
1376 input->sy = sy;
1377
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001378 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001379 if (window->enter_handler)
1380 pointer = window->enter_handler(window, input,
1381 time, sx, sy,
1382 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001383
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001384 item = window_find_item(window, x, y);
1385 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001386
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001387 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001388 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001389}
1390
1391static void
1392window_handle_keyboard_focus(void *data,
1393 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001394 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001395 struct wl_surface *surface,
1396 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001397{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001398 struct input *input = data;
1399 struct window *window = input->keyboard_focus;
1400 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001401 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001402
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001403 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001404 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001405 if (window->keyboard_focus_handler)
1406 (*window->keyboard_focus_handler)(window, NULL,
1407 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001408 }
1409
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001410 if (surface)
1411 input->keyboard_focus = wl_surface_get_user_data(surface);
1412 else
1413 input->keyboard_focus = NULL;
1414
1415 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001416 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001417 for (k = keys->data; k < end; k++)
1418 input->modifiers |= d->xkb->map->modmap[*k];
1419
1420 window = input->keyboard_focus;
1421 if (window) {
1422 window->keyboard_device = input;
1423 if (window->keyboard_focus_handler)
1424 (*window->keyboard_focus_handler)(window,
1425 window->keyboard_device,
1426 window->user_data);
1427 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001428}
1429
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001430static const struct wl_input_device_listener input_device_listener = {
1431 window_handle_motion,
1432 window_handle_button,
1433 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001434 window_handle_pointer_focus,
1435 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001436};
1437
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001438void
1439input_get_position(struct input *input, int32_t *x, int32_t *y)
1440{
1441 *x = input->sx;
1442 *y = input->sy;
1443}
1444
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001445struct wl_input_device *
1446input_get_input_device(struct input *input)
1447{
1448 return input->input_device;
1449}
1450
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001451uint32_t
1452input_get_modifiers(struct input *input)
1453{
1454 return input->modifiers;
1455}
1456
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001457struct data_offer {
1458 struct wl_data_offer *offer;
1459 struct input *input;
1460 struct wl_array types;
1461 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001462
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001463 struct task io_task;
1464 int fd;
1465 data_func_t func;
1466 int32_t x, y;
1467 void *user_data;
1468};
1469
1470static void
1471data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1472{
1473 struct data_offer *offer = data;
1474 char **p;
1475
1476 p = wl_array_add(&offer->types, sizeof *p);
1477 *p = strdup(type);
1478}
1479
1480static const struct wl_data_offer_listener data_offer_listener = {
1481 data_offer_offer,
1482};
1483
1484static void
1485data_offer_destroy(struct data_offer *offer)
1486{
1487 char **p;
1488
1489 offer->refcount--;
1490 if (offer->refcount == 0) {
1491 wl_data_offer_destroy(offer->offer);
1492 for (p = offer->types.data; *p; p++)
1493 free(*p);
1494 wl_array_release(&offer->types);
1495 free(offer);
1496 }
1497}
1498
1499static void
1500data_device_data_offer(void *data,
1501 struct wl_data_device *data_device, uint32_t id)
1502{
1503 struct data_offer *offer;
1504
1505 offer = malloc(sizeof *offer);
1506
1507 wl_array_init(&offer->types);
1508 offer->refcount = 1;
1509 offer->input = data;
1510
1511 /* FIXME: Generate typesafe wrappers for this */
1512 offer->offer = (struct wl_data_offer *)
1513 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1514 id, &wl_data_offer_interface);
1515
1516 wl_data_offer_add_listener(offer->offer,
1517 &data_offer_listener, offer);
1518}
1519
1520static void
1521data_device_enter(void *data, struct wl_data_device *data_device,
1522 uint32_t time, struct wl_surface *surface,
1523 int32_t x, int32_t y, struct wl_data_offer *offer)
1524{
1525 struct input *input = data;
1526 struct window *window;
1527 char **p;
1528
1529 input->drag_offer = wl_data_offer_get_user_data(offer);
1530 window = wl_surface_get_user_data(surface);
1531 input->pointer_focus = window;
1532
1533 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1534 *p = NULL;
1535
1536 window = input->pointer_focus;
1537 if (window->data_handler)
1538 window->data_handler(window, input, time, x, y,
1539 input->drag_offer->types.data,
1540 window->user_data);
1541}
1542
1543static void
1544data_device_leave(void *data, struct wl_data_device *data_device)
1545{
1546 struct input *input = data;
1547
1548 data_offer_destroy(input->drag_offer);
1549 input->drag_offer = NULL;
1550}
1551
1552static void
1553data_device_motion(void *data, struct wl_data_device *data_device,
1554 uint32_t time, int32_t x, int32_t y)
1555{
1556 struct input *input = data;
1557 struct window *window = input->pointer_focus;
1558
1559 input->sx = x;
1560 input->sy = y;
1561
1562 if (window->data_handler)
1563 window->data_handler(window, input, time, x, y,
1564 input->drag_offer->types.data,
1565 window->user_data);
1566}
1567
1568static void
1569data_device_drop(void *data, struct wl_data_device *data_device)
1570{
1571 struct input *input = data;
1572 struct window *window = input->pointer_focus;
1573
1574 if (window->drop_handler)
1575 window->drop_handler(window, input,
1576 input->sx, input->sy, window->user_data);
1577}
1578
1579static void
1580data_device_selection(void *data,
1581 struct wl_data_device *wl_data_device,
1582 struct wl_data_offer *offer)
1583{
1584 struct input *input = data;
1585 char **p;
1586
1587 if (input->selection_offer)
1588 data_offer_destroy(input->selection_offer);
1589
1590 input->selection_offer = wl_data_offer_get_user_data(offer);
1591 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1592 *p = NULL;
1593}
1594
1595static const struct wl_data_device_listener data_device_listener = {
1596 data_device_data_offer,
1597 data_device_enter,
1598 data_device_leave,
1599 data_device_motion,
1600 data_device_drop,
1601 data_device_selection
1602};
1603
1604struct wl_data_device *
1605input_get_data_device(struct input *input)
1606{
1607 return input->data_device;
1608}
1609
1610void
1611input_set_selection(struct input *input,
1612 struct wl_data_source *source, uint32_t time)
1613{
1614 wl_data_device_set_selection(input->data_device, source, time);
1615}
1616
1617void
1618input_accept(struct input *input, uint32_t time, const char *type)
1619{
1620 wl_data_offer_accept(input->drag_offer->offer, time, type);
1621}
1622
1623static void
1624offer_io_func(struct task *task, uint32_t events)
1625{
1626 struct data_offer *offer =
1627 container_of(task, struct data_offer, io_task);
1628 unsigned int len;
1629 char buffer[4096];
1630
1631 len = read(offer->fd, buffer, sizeof buffer);
1632 offer->func(buffer, len,
1633 offer->x, offer->y, offer->user_data);
1634
1635 if (len == 0) {
1636 close(offer->fd);
1637 data_offer_destroy(offer);
1638 }
1639}
1640
1641static void
1642data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1643 data_func_t func, void *user_data)
1644{
1645 int p[2];
1646
1647 pipe2(p, O_CLOEXEC);
1648 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1649 close(p[1]);
1650
1651 offer->io_task.run = offer_io_func;
1652 offer->fd = p[0];
1653 offer->func = func;
1654 offer->refcount++;
1655 offer->user_data = user_data;
1656
1657 display_watch_fd(offer->input->display,
1658 offer->fd, EPOLLIN, &offer->io_task);
1659}
1660
1661void
1662input_receive_drag_data(struct input *input, const char *mime_type,
1663 data_func_t func, void *data)
1664{
1665 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1666 input->drag_offer->x = input->sx;
1667 input->drag_offer->y = input->sy;
1668}
1669
1670int
1671input_receive_selection_data(struct input *input, const char *mime_type,
1672 data_func_t func, void *data)
1673{
1674 char **p;
1675
1676 if (input->selection_offer == NULL)
1677 return -1;
1678
1679 for (p = input->selection_offer->types.data; *p; p++)
1680 if (strcmp(mime_type, *p) == 0)
1681 break;
1682
1683 if (*p == NULL)
1684 return -1;
1685
1686 data_offer_receive_data(input->selection_offer,
1687 mime_type, func, data);
1688 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001689}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001690
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001691void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001692window_move(struct window *window, struct input *input, uint32_t time)
1693{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001694 wl_shell_surface_move(window->shell_surface,
1695 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001696}
1697
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001698static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001699handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001700 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001701 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001702{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001703 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001704 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001705
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001706 /* FIXME: this is probably the wrong place to check for width
1707 * or height <= 0, but it prevents the compositor from crashing
1708 */
1709 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001710 return;
1711
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001712 window->resize_edges = edges;
1713
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001714 if (window->resize_handler) {
1715 child_width = width - 20 - window->margin * 2;
1716 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001717
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001718 (*window->resize_handler)(window,
1719 child_width, child_height,
1720 window->user_data);
1721 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001722 window->allocation.width = width;
1723 window->allocation.height = height;
1724
1725 if (window->redraw_handler)
1726 window_schedule_redraw(window);
1727 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001728}
1729
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001730static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001731 handle_configure,
1732};
1733
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001734void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001735window_get_allocation(struct window *window,
1736 struct rectangle *allocation)
1737{
1738 *allocation = window->allocation;
1739}
1740
1741void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001742window_get_child_allocation(struct window *window,
1743 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001744{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001745 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001746 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001747 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001748 allocation->x = window->margin + 10;
1749 allocation->y = window->margin + 50;
1750 allocation->width =
1751 window->allocation.width - 20 - window->margin * 2;
1752 allocation->height =
1753 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001754 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001755}
1756
1757void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001758window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001759{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001760 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001761 window->allocation.x = 20 + window->margin;
1762 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001763 window->allocation.width = width + 20 + window->margin * 2;
1764 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001765 } else {
1766 window->allocation.x = 0;
1767 window->allocation.y = 0;
1768 window->allocation.width = width;
1769 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001770 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001771}
1772
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001773static void
1774idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001775{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001776 struct window *window =
1777 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001778
1779 window->redraw_handler(window, window->user_data);
1780 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001781}
1782
1783void
1784window_schedule_redraw(struct window *window)
1785{
1786 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001787 window->redraw_task.run = idle_redraw;
1788 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001789 window->redraw_scheduled = 1;
1790 }
1791}
1792
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001793void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001794window_set_custom(struct window *window)
1795{
1796 window->type = TYPE_CUSTOM;
1797}
1798
1799void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001800window_set_fullscreen(struct window *window, int fullscreen)
1801{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001802 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001803 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001804
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001805 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001806 return;
1807
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001808 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001809 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001810 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001811 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001812 width = output->allocation.width;
1813 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001814 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001815 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001816 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001817 width = window->saved_allocation.width - 20 - window->margin * 2;
1818 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001819 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001820 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001821
1822 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001823}
1824
1825void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001826window_set_decoration(struct window *window, int decoration)
1827{
1828 window->decoration = decoration;
1829}
1830
1831void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001832window_set_user_data(struct window *window, void *data)
1833{
1834 window->user_data = data;
1835}
1836
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001837void *
1838window_get_user_data(struct window *window)
1839{
1840 return window->user_data;
1841}
1842
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001843void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001844window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001845 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001846{
1847 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001848}
1849
1850void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001851window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001852 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001853{
1854 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001855}
1856
1857void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001858window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001859 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001860{
1861 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001862}
1863
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001864void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001865window_set_button_handler(struct window *window,
1866 window_button_handler_t handler)
1867{
1868 window->button_handler = handler;
1869}
1870
1871void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001872window_set_motion_handler(struct window *window,
1873 window_motion_handler_t handler)
1874{
1875 window->motion_handler = handler;
1876}
1877
1878void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001879window_set_enter_handler(struct window *window,
1880 window_enter_handler_t handler)
1881{
1882 window->enter_handler = handler;
1883}
1884
1885void
1886window_set_leave_handler(struct window *window,
1887 window_leave_handler_t handler)
1888{
1889 window->leave_handler = handler;
1890}
1891
1892void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001893window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001894 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001895{
1896 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001897}
1898
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001899void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001900window_set_item_focus_handler(struct window *window,
1901 window_item_focus_handler_t handler)
1902{
1903 window->item_focus_handler = handler;
1904}
1905
1906void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001907window_set_data_handler(struct window *window, window_data_handler_t handler)
1908{
1909 window->data_handler = handler;
1910}
1911
1912void
1913window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1914{
1915 window->drop_handler = handler;
1916}
1917
1918void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001919window_set_transparent(struct window *window, int transparent)
1920{
1921 window->transparent = transparent;
1922}
1923
1924void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001925window_set_title(struct window *window, const char *title)
1926{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001927 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001928 window->title = strdup(title);
1929}
1930
1931const char *
1932window_get_title(struct window *window)
1933{
1934 return window->title;
1935}
1936
1937void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001938display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1939 int32_t x, int32_t y, int32_t width, int32_t height)
1940{
1941 struct wl_buffer *buffer;
1942
1943 buffer = display_get_buffer_for_surface(display, cairo_surface);
1944
1945 wl_buffer_damage(buffer, x, y, width, height);
1946}
1947
1948void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001949window_damage(struct window *window, int32_t x, int32_t y,
1950 int32_t width, int32_t height)
1951{
1952 wl_surface_damage(window->surface, x, y, width, height);
1953}
1954
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001955static struct window *
1956window_create_internal(struct display *display, struct window *parent,
1957 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001958{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001959 struct window *window;
1960
1961 window = malloc(sizeof *window);
1962 if (window == NULL)
1963 return NULL;
1964
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001965 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001966 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001967 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001968 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001969 window->shell_surface = wl_shell_get_shell_surface(display->shell,
1970 window->surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001971 window->allocation.x = 0;
1972 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001973 window->allocation.width = width;
1974 window->allocation.height = height;
1975 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001976 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001977 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001978 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001979 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001980
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001981 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001982#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001983 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1984 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001985#else
1986 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1987#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001988 else
1989 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001990
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001991 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001992 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001993
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001994 wl_shell_surface_add_listener(window->shell_surface,
1995 &shell_surface_listener, window);
1996
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001997 return window;
1998}
1999
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002000struct window *
2001window_create(struct display *display, int32_t width, int32_t height)
2002{
2003 struct window *window;
2004
2005 window = window_create_internal(display, NULL, width, height);
2006 if (!window)
2007 return NULL;
2008
2009 return window;
2010}
2011
2012struct window *
2013window_create_transient(struct display *display, struct window *parent,
2014 int32_t x, int32_t y, int32_t width, int32_t height)
2015{
2016 struct window *window;
2017
2018 window = window_create_internal(parent->display,
2019 parent, width, height);
2020 if (!window)
2021 return NULL;
2022
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002023 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002024 window->x = x;
2025 window->y = y;
2026
2027 return window;
2028}
2029
2030void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002031window_set_buffer_type(struct window *window, enum window_buffer_type type)
2032{
2033 window->buffer_type = type;
2034}
2035
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002036
2037static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002038display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002039 struct wl_output *wl_output,
2040 int x, int y,
2041 int physical_width,
2042 int physical_height,
2043 int subpixel,
2044 const char *make,
2045 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002046{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002047 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002048
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002049 output->allocation.x = x;
2050 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002051}
2052
2053static void
2054display_handle_mode(void *data,
2055 struct wl_output *wl_output,
2056 uint32_t flags,
2057 int width,
2058 int height,
2059 int refresh)
2060{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002061 struct output *output = data;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002062
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002063 if (flags & WL_OUTPUT_MODE_CURRENT) {
2064 output->allocation.width = width;
2065 output->allocation.height = height;
2066 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002067}
2068
2069static const struct wl_output_listener output_listener = {
2070 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002071 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002072};
2073
2074static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002075display_add_output(struct display *d, uint32_t id)
2076{
2077 struct output *output;
2078
2079 output = malloc(sizeof *output);
2080 if (output == NULL)
2081 return;
2082
2083 memset(output, 0, sizeof *output);
2084 output->display = d;
2085 output->output =
2086 wl_display_bind(d->display, id, &wl_output_interface);
2087 wl_list_insert(d->output_list.prev, &output->link);
2088
2089 wl_output_add_listener(output->output, &output_listener, output);
2090}
2091
2092void
2093output_get_allocation(struct output *output, struct rectangle *allocation)
2094{
2095 *allocation = output->allocation;
2096}
2097
2098static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002099display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002100{
2101 struct input *input;
2102
2103 input = malloc(sizeof *input);
2104 if (input == NULL)
2105 return;
2106
2107 memset(input, 0, sizeof *input);
2108 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002109 input->input_device =
2110 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002111 input->pointer_focus = NULL;
2112 input->keyboard_focus = NULL;
2113 wl_list_insert(d->input_list.prev, &input->link);
2114
2115 wl_input_device_add_listener(input->input_device,
2116 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002117 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002118
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002119 input->data_device =
2120 wl_data_device_manager_get_data_device(d->data_device_manager,
2121 input->input_device);
2122 wl_data_device_add_listener(input->data_device,
2123 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002124}
2125
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002126static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002127display_handle_global(struct wl_display *display, uint32_t id,
2128 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002129{
2130 struct display *d = data;
2131
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002132 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002133 d->compositor =
2134 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002135 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002136 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002137 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002138 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002139 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002140 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002141 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002142 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002143 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2144 d->data_device_manager =
2145 wl_display_bind(display, id,
2146 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002147 }
2148}
2149
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002150static void
2151display_render_frame(struct display *d)
2152{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002153 int radius = 8;
2154 cairo_t *cr;
2155
2156 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2157 cr = cairo_create(d->shadow);
2158 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2159 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2160 rounded_rect(cr, 16, 16, 112, 112, radius);
2161 cairo_fill(cr);
2162 cairo_destroy(cr);
2163 blur_surface(d->shadow, 64);
2164
2165 d->active_frame =
2166 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2167 cr = cairo_create(d->active_frame);
2168 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2169 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2170 rounded_rect(cr, 16, 16, 112, 112, radius);
2171 cairo_fill(cr);
2172 cairo_destroy(cr);
2173
2174 d->inactive_frame =
2175 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2176 cr = cairo_create(d->inactive_frame);
2177 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2178 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2179 rounded_rect(cr, 16, 16, 112, 112, radius);
2180 cairo_fill(cr);
2181 cairo_destroy(cr);
2182}
2183
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002184static void
2185init_xkb(struct display *d)
2186{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002187 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002188
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002189 names.rules = "evdev";
2190 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002191 names.layout = option_xkb_layout;
2192 names.variant = option_xkb_variant;
2193 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002194
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002195 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002196 if (!d->xkb) {
2197 fprintf(stderr, "Failed to compile keymap\n");
2198 exit(1);
2199 }
2200}
2201
Yuval Fledel45568f62010-12-06 09:18:12 -05002202static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002203init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002204{
2205 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002206 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002207
2208 static const EGLint premul_argb_cfg_attribs[] = {
2209 EGL_SURFACE_TYPE,
2210 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2211 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002212 EGL_RED_SIZE, 1,
2213 EGL_GREEN_SIZE, 1,
2214 EGL_BLUE_SIZE, 1,
2215 EGL_ALPHA_SIZE, 1,
2216 EGL_DEPTH_SIZE, 1,
2217 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2218 EGL_NONE
2219 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002220
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002221 static const EGLint rgb_cfg_attribs[] = {
2222 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2223 EGL_RED_SIZE, 1,
2224 EGL_GREEN_SIZE, 1,
2225 EGL_BLUE_SIZE, 1,
2226 EGL_ALPHA_SIZE, 0,
2227 EGL_DEPTH_SIZE, 1,
2228 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2229 EGL_NONE
2230 };
2231
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002232 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002233 if (!eglInitialize(d->dpy, &major, &minor)) {
2234 fprintf(stderr, "failed to initialize display\n");
2235 return -1;
2236 }
2237
2238 if (!eglBindAPI(EGL_OPENGL_API)) {
2239 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2240 return -1;
2241 }
2242
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002243 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002244 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002245 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002246 return -1;
2247 }
2248
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002249 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2250 &d->rgb_config, 1, &n) || n != 1) {
2251 fprintf(stderr, "failed to choose rgb config\n");
2252 return -1;
2253 }
2254
Benjamin Franzke0c991632011-09-27 21:57:31 +02002255 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2256 if (d->rgb_ctx == NULL) {
2257 fprintf(stderr, "failed to create context\n");
2258 return -1;
2259 }
2260 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2261 EGL_NO_CONTEXT, NULL);
2262 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002263 fprintf(stderr, "failed to create context\n");
2264 return -1;
2265 }
2266
Benjamin Franzke0c991632011-09-27 21:57:31 +02002267 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002268 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002269 return -1;
2270 }
2271
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002272#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002273 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2274 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002275 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002276 return -1;
2277 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002278 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2279 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2280 fprintf(stderr, "failed to get cairo egl argb device\n");
2281 return -1;
2282 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002283#endif
2284
2285 return 0;
2286}
2287
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002288static int
2289event_mask_update(uint32_t mask, void *data)
2290{
2291 struct display *d = data;
2292
2293 d->mask = mask;
2294
2295 return 0;
2296}
2297
2298static void
2299handle_display_data(struct task *task, uint32_t events)
2300{
2301 struct display *display =
2302 container_of(task, struct display, display_task);
2303
2304 wl_display_iterate(display->display, display->mask);
2305}
2306
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002307struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002308display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002309{
2310 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002311 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002312 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002313 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002314
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002315 g_type_init();
2316
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002317 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002318 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002319 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002320
2321 xkb_option_group = g_option_group_new("xkb",
2322 "Keyboard options",
2323 "Show all XKB options",
2324 NULL, NULL);
2325 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2326 g_option_context_add_group (context, xkb_option_group);
2327
2328 if (!g_option_context_parse(context, argc, argv, &error)) {
2329 fprintf(stderr, "option parsing failed: %s\n", error->message);
2330 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002331 }
2332
Tim Wiederhake748f6722011-01-23 23:25:25 +01002333 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002334
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002335 d = malloc(sizeof *d);
2336 if (d == NULL)
2337 return NULL;
2338
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002339 memset(d, 0, sizeof *d);
2340
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002341 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002342 if (d->display == NULL) {
2343 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002344 return NULL;
2345 }
2346
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002347 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2348 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2349 d->display_task.run = handle_display_data;
2350 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2351
2352 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002353 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002354 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002355
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002356 /* Set up listener so we'll catch all events. */
2357 wl_display_add_global_listener(d->display,
2358 display_handle_global, d);
2359
2360 /* Process connection events. */
2361 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002362 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002363 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002364
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002365 d->image_target_texture_2d =
2366 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2367 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2368 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2369
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002370 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002371
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002372 display_render_frame(d);
2373
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002374 wl_list_init(&d->window_list);
2375
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002376 init_xkb(d);
2377
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002378 return d;
2379}
2380
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002381struct wl_display *
2382display_get_display(struct display *display)
2383{
2384 return display->display;
2385}
2386
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002387struct output *
2388display_get_output(struct display *display)
2389{
2390 return container_of(display->output_list.next, struct output, link);
2391}
2392
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002393struct wl_compositor *
2394display_get_compositor(struct display *display)
2395{
2396 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002397}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002398
2399EGLDisplay
2400display_get_egl_display(struct display *d)
2401{
2402 return d->dpy;
2403}
2404
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002405struct wl_data_source *
2406display_create_data_source(struct display *display)
2407{
2408 return wl_data_device_manager_create_data_source(display->data_device_manager);
2409}
2410
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002411EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002412display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002413{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002414 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002415}
2416
Benjamin Franzke0c991632011-09-27 21:57:31 +02002417EGLConfig
2418display_get_argb_egl_config(struct display *d)
2419{
2420 return d->premultiplied_argb_config;
2421}
2422
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002423struct wl_shell *
2424display_get_shell(struct display *display)
2425{
2426 return display->shell;
2427}
2428
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002429int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002430display_acquire_window_surface(struct display *display,
2431 struct window *window,
2432 EGLContext ctx)
2433{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002434#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002435 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002436 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002437
2438 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002439 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002440 device = cairo_surface_get_device(window->cairo_surface);
2441 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002442 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002443
Benjamin Franzke0c991632011-09-27 21:57:31 +02002444 if (!ctx) {
2445 if (device == display->rgb_device)
2446 ctx = display->rgb_ctx;
2447 else if (device == display->argb_device)
2448 ctx = display->argb_ctx;
2449 else
2450 assert(0);
2451 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002452
2453 data = cairo_surface_get_user_data(window->cairo_surface,
2454 &surface_data_key);
2455
Benjamin Franzke0c991632011-09-27 21:57:31 +02002456 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002457 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2458 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002459
2460 return 0;
2461#else
2462 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002463#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002464}
2465
2466void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002467display_release_window_surface(struct display *display,
2468 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002469{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002470#ifdef HAVE_CAIRO_EGL
2471 cairo_device_t *device;
2472
2473 device = cairo_surface_get_device(window->cairo_surface);
2474 if (!device)
2475 return;
2476
2477 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002478 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002479 cairo_device_release(device);
2480#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002481}
2482
2483void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002484display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002485{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002486 wl_list_insert(&display->deferred_list, &task->link);
2487}
2488
2489void
2490display_watch_fd(struct display *display,
2491 int fd, uint32_t events, struct task *task)
2492{
2493 struct epoll_event ep;
2494
2495 ep.events = events;
2496 ep.data.ptr = task;
2497 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2498}
2499
2500void
2501display_run(struct display *display)
2502{
2503 struct task *task;
2504 struct epoll_event ep[16];
2505 int i, count;
2506
2507 while (1) {
2508 while (display->mask & WL_DISPLAY_WRITABLE)
2509 wl_display_iterate(display->display,
2510 WL_DISPLAY_WRITABLE);
2511
2512 count = epoll_wait(display->epoll_fd,
2513 ep, ARRAY_LENGTH(ep), -1);
2514 for (i = 0; i < count; i++) {
2515 task = ep[i].data.ptr;
2516 task->run(task, ep[i].events);
2517 }
2518
2519 while (!wl_list_empty(&display->deferred_list)) {
2520 task = container_of(display->deferred_list.next,
2521 struct task, link);
2522 wl_list_remove(&task->link);
2523 task->run(task, 0);
2524 }
2525 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002526}