blob: e0693da147266632b602909a0ab9783794e4463d [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{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200753 if (!window->shell_surface)
754 return;
755
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400756 switch (window->type) {
757 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200758 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400759 break;
760 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200761 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400762 break;
763 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200764 wl_shell_surface_set_transient(window->shell_surface,
765 window->parent->shell_surface,
766 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400767 break;
768 case TYPE_CUSTOM:
769 break;
770 }
771}
772
773static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500774window_attach_surface(struct window *window)
775{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400776 struct display *display = window->display;
777 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400778 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000779#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000781#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500782 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400784 if (display->shell)
785 window_set_type(window);
786
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000788#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
790 data = cairo_surface_get_user_data(window->cairo_surface,
791 &surface_data_key);
792
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793 cairo_gl_surface_swapbuffers(window->cairo_surface);
794 wl_egl_window_get_attached_size(data->window,
795 &window->server_allocation.width,
796 &window->server_allocation.height);
797 break;
798 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000799#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200801 window_get_resize_dx_dy(window, &x, &y);
802
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100803 if (window->pending_surface != NULL)
804 return;
805
806 window->pending_surface = window->cairo_surface;
807 window->cairo_surface = NULL;
808
809 buffer =
810 display_get_buffer_for_surface(display,
811 window->pending_surface);
812
813 wl_surface_attach(window->surface, buffer, x, y);
814 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400815 cb = wl_display_sync(display->display);
816 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818 default:
819 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500821
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500822 wl_surface_damage(window->surface, 0, 0,
823 window->allocation.width,
824 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500825}
826
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500827void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400828window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500829{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100830 if (window->cairo_surface) {
831 switch (window->buffer_type) {
832 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
833 case WINDOW_BUFFER_TYPE_SHM:
834 display_surface_damage(window->display,
835 window->cairo_surface,
836 0, 0,
837 window->allocation.width,
838 window->allocation.height);
839 break;
840 default:
841 break;
842 }
843 window_attach_surface(window);
844 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500845}
846
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400847void
848window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400849{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500850 cairo_surface_reference(surface);
851
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400852 if (window->cairo_surface != NULL)
853 cairo_surface_destroy(window->cairo_surface);
854
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500855 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400856}
857
Benjamin Franzke22d54812011-07-16 19:50:32 +0000858#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859static void
860window_resize_cairo_window_surface(struct window *window)
861{
862 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200863 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864
865 data = cairo_surface_get_user_data(window->cairo_surface,
866 &surface_data_key);
867
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200868 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100869 wl_egl_window_resize(data->window,
870 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200871 window->allocation.height,
872 x,y);
873
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874 cairo_gl_surface_set_size(window->cairo_surface,
875 window->allocation.width,
876 window->allocation.height);
877}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000878#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100879
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400880struct display *
881window_get_display(struct window *window)
882{
883 return window->display;
884}
885
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886void
887window_create_surface(struct window *window)
888{
889 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400890 uint32_t flags = 0;
891
892 if (!window->transparent)
893 flags = SURFACE_OPAQUE;
894
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500896#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
898 if (window->cairo_surface) {
899 window_resize_cairo_window_surface(window);
900 return;
901 }
902 surface = display_create_surface(window->display,
903 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400904 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500906 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400907 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400909 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400910 break;
911#endif
912 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400913 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400914 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400915 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800916 default:
917 surface = NULL;
918 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400919 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920
921 window_set_surface(window, surface);
922 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923}
924
925static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500926window_draw_menu(struct window *window)
927{
928 cairo_t *cr;
929 int width, height, r = 5;
930
931 window_create_surface(window);
932
933 cr = cairo_create(window->cairo_surface);
934 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
935 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
936 cairo_paint(cr);
937
938 width = window->allocation.width;
939 height = window->allocation.height;
940 rounded_rect(cr, r, r, width - r, height - r, r);
941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
942 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
943 cairo_fill(cr);
944 cairo_destroy(cr);
945}
946
947static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500948window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500949{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500950 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500951 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400952 cairo_surface_t *frame;
953 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500954
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400957 width = window->allocation.width;
958 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500959
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500960 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500961
Kristian Høgsberg09531622010-06-14 23:22:15 -0400962 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400963 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400964 cairo_paint(cr);
965
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400966 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400967 tile_mask(cr, window->display->shadow,
968 shadow_dx, shadow_dy, width, height,
969 window->margin + 10 - shadow_dx,
970 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500971
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400973 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400974 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400975 frame = window->display->inactive_frame;
976
977 tile_source(cr, frame, 0, 0, width, height,
978 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500979
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500980 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
981 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500982 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400983 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400984 if (window->keyboard_device)
985 cairo_set_source_rgb(cr, 0, 0, 0);
986 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400987 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400988 cairo_show_text(cr, window->title);
989
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500990 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400991
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100992 /* FIXME: this breakes gears, fix cairo? */
993#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400994 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100995#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500996}
997
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400998void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500999window_destroy(struct window *window)
1000{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001001 struct display *display = window->display;
1002 struct input *input;
1003
1004 if (window->redraw_scheduled)
1005 wl_list_remove(&window->redraw_task.link);
1006
1007 wl_list_for_each(input, &display->input_list, link) {
1008 if (input->pointer_focus == window)
1009 input->pointer_focus = NULL;
1010 if (input->keyboard_focus == window)
1011 input->keyboard_focus = NULL;
1012 }
1013
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001014 if (window->shell_surface)
1015 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001016 wl_surface_destroy(window->surface);
1017 wl_list_remove(&window->link);
1018 free(window);
1019}
1020
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021static struct item *
1022window_find_item(struct window *window, int32_t x, int32_t y)
1023{
1024 struct item *item;
1025
1026 wl_list_for_each(item, &window->item_list, link) {
1027 if (item->allocation.x <= x &&
1028 x < item->allocation.x + item->allocation.width &&
1029 item->allocation.y <= y &&
1030 y < item->allocation.y + item->allocation.height) {
1031 return item;
1032 }
1033 }
1034
1035 return NULL;
1036}
1037
1038struct item *
1039window_add_item(struct window *window, void *data)
1040{
1041 struct item *item;
1042
1043 item = malloc(sizeof *item);
1044 memset(item, 0, sizeof *item);
1045 item->user_data = data;
1046 wl_list_insert(window->item_list.prev, &item->link);
1047
1048 return item;
1049}
1050
1051void
1052window_for_each_item(struct window *window, item_func_t func, void *data)
1053{
1054 struct item *item;
1055
1056 wl_list_for_each(item, &window->item_list, link)
1057 func(item, data);
1058}
1059
1060struct item *
1061window_get_focus_item(struct window *window)
1062{
1063 return window->focus_item;
1064}
1065
1066void
1067item_get_allocation(struct item *item, struct rectangle *allocation)
1068{
1069 *allocation = item->allocation;
1070}
1071
1072void
1073item_set_allocation(struct item *item,
1074 int32_t x, int32_t y, int32_t width, int32_t height)
1075{
1076 item->allocation.x = x;
1077 item->allocation.y = y;
1078 item->allocation.width = width;
1079 item->allocation.height = height;
1080}
1081
1082void *
1083item_get_user_data(struct item *item)
1084{
1085 return item->user_data;
1086}
1087
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001088void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001089window_draw(struct window *window)
1090{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001091 if (window->parent)
1092 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001093 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001094 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001095 else
1096 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001097}
1098
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001099cairo_surface_t *
1100window_get_surface(struct window *window)
1101{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001102 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001103}
1104
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001105struct wl_surface *
1106window_get_wl_surface(struct window *window)
1107{
1108 return window->surface;
1109}
1110
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001111struct wl_shell_surface *
1112window_get_wl_shell_surface(struct window *window)
1113{
1114 return window->shell_surface;
1115}
1116
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001117static int
1118get_pointer_location(struct window *window, int32_t x, int32_t y)
1119{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001120 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001121 const int grip_size = 8;
1122
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001123 if (!window->decoration)
1124 return WINDOW_CLIENT_AREA;
1125
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001128 else if (window->margin <= x && x < window->margin + grip_size)
1129 hlocation = WINDOW_RESIZING_LEFT;
1130 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132 else if (x < window->allocation.width - window->margin)
1133 hlocation = WINDOW_RESIZING_RIGHT;
1134 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001136
1137 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001138 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139 else if (window->margin <= y && y < window->margin + grip_size)
1140 vlocation = WINDOW_RESIZING_TOP;
1141 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143 else if (y < window->allocation.height - window->margin)
1144 vlocation = WINDOW_RESIZING_BOTTOM;
1145 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001146 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001147
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001148 location = vlocation | hlocation;
1149 if (location & WINDOW_EXTERIOR)
1150 location = WINDOW_EXTERIOR;
1151 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1152 location = WINDOW_TITLEBAR;
1153 else if (location == WINDOW_INTERIOR)
1154 location = WINDOW_CLIENT_AREA;
1155
1156 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001157}
1158
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001159void
1160input_set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161{
1162 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001163 struct wl_buffer *buffer;
1164 cairo_surface_t *surface;
1165 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001166
1167 location = get_pointer_location(input->pointer_focus,
1168 input->sx, input->sy);
1169 switch (location) {
1170 case WINDOW_RESIZING_TOP:
1171 pointer = POINTER_TOP;
1172 break;
1173 case WINDOW_RESIZING_BOTTOM:
1174 pointer = POINTER_BOTTOM;
1175 break;
1176 case WINDOW_RESIZING_LEFT:
1177 pointer = POINTER_LEFT;
1178 break;
1179 case WINDOW_RESIZING_RIGHT:
1180 pointer = POINTER_RIGHT;
1181 break;
1182 case WINDOW_RESIZING_TOP_LEFT:
1183 pointer = POINTER_TOP_LEFT;
1184 break;
1185 case WINDOW_RESIZING_TOP_RIGHT:
1186 pointer = POINTER_TOP_RIGHT;
1187 break;
1188 case WINDOW_RESIZING_BOTTOM_LEFT:
1189 pointer = POINTER_BOTTOM_LEFT;
1190 break;
1191 case WINDOW_RESIZING_BOTTOM_RIGHT:
1192 pointer = POINTER_BOTTOM_RIGHT;
1193 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001194 case WINDOW_EXTERIOR:
1195 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001196 if (input->current_pointer_image == POINTER_DEFAULT)
1197 return;
1198
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001199 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001200 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001201 return;
1202 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203 break;
1204 }
1205
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001206 if (pointer == input->current_pointer_image)
1207 return;
1208
1209 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001210 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001211
1212 if (!surface)
1213 return;
1214
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001215 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001216 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217 pointer_images[pointer].hotspot_x,
1218 pointer_images[pointer].hotspot_y);
1219}
1220
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001221static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001222window_set_focus_item(struct window *window, struct item *focus)
1223{
1224 void *data;
1225
1226 if (focus == window->focus_item)
1227 return;
1228
1229 window->focus_item = focus;
1230 data = focus ? focus->user_data : NULL;
1231 if (window->item_focus_handler)
1232 window->item_focus_handler(window, focus, data);
1233}
1234
1235static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001236window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001237 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001238 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001239{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001240 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001241 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001242 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001243 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001244
1245 input->x = x;
1246 input->y = y;
1247 input->sx = sx;
1248 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001250 if (!window->focus_item || !window->item_grab_button) {
1251 item = window_find_item(window, sx, sy);
1252 window_set_focus_item(window, item);
1253 }
1254
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001255 if (window->motion_handler)
1256 pointer = (*window->motion_handler)(window, input, time,
1257 x, y, sx, sy,
1258 window->user_data);
1259
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001260 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001261}
1262
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001263static void
1264window_handle_button(void *data,
1265 struct wl_input_device *input_device,
1266 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001267{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001268 struct input *input = data;
1269 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001270 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001272
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001273 if (window->focus_item && window->item_grab_button == 0 && state)
1274 window->item_grab_button = button;
1275
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001277
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001278 if (window->display->shell &&
1279 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001282 if (!window->shell_surface)
1283 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001284 wl_shell_surface_move(window->shell_surface,
1285 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001286 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001287 case WINDOW_RESIZING_TOP:
1288 case WINDOW_RESIZING_BOTTOM:
1289 case WINDOW_RESIZING_LEFT:
1290 case WINDOW_RESIZING_RIGHT:
1291 case WINDOW_RESIZING_TOP_LEFT:
1292 case WINDOW_RESIZING_TOP_RIGHT:
1293 case WINDOW_RESIZING_BOTTOM_LEFT:
1294 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001295 if (!window->shell_surface)
1296 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001297 wl_shell_surface_resize(window->shell_surface,
1298 input_device, time,
1299 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001300 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301 case WINDOW_CLIENT_AREA:
1302 if (window->button_handler)
1303 (*window->button_handler)(window,
1304 input, time,
1305 button, state,
1306 window->user_data);
1307 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001308 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001309 } else {
1310 if (window->button_handler)
1311 (*window->button_handler)(window,
1312 input, time,
1313 button, state,
1314 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001315 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001316
1317 if (window->focus_item &&
1318 window->item_grab_button == button && !state) {
1319 window->item_grab_button = 0;
1320 item = window_find_item(window, input->sx, input->sy);
1321 window_set_focus_item(window, item);
1322 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001323}
1324
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001325static void
1326window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001328{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001329 struct input *input = data;
1330 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001331 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001332 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001333
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001334 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001335 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001336 return;
1337
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001338 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001339 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001340 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1341 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001342
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001343 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1344
1345 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001346 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001347 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001348 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001349
1350 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001351 (*window->key_handler)(window, input, time, key, sym, state,
1352 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001353}
1354
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001355static void
1356window_handle_pointer_focus(void *data,
1357 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001358 uint32_t time, struct wl_surface *surface,
1359 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001360{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001361 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001362 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001363 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001364 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001365
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001366 window = input->pointer_focus;
1367 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001368 window_set_focus_item(window, NULL);
1369
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001370 if (window->leave_handler)
1371 window->leave_handler(window, input,
1372 time, window->user_data);
1373 input->pointer_focus = NULL;
1374 input->current_pointer_image = POINTER_UNSET;
1375 }
1376
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001377 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001378 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001379 window = input->pointer_focus;
1380
Kristian Høgsberg59826582011-01-20 11:56:57 -05001381 input->x = x;
1382 input->y = y;
1383 input->sx = sx;
1384 input->sy = sy;
1385
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001386 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001387 if (window->enter_handler)
1388 pointer = window->enter_handler(window, input,
1389 time, sx, sy,
1390 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001391
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001392 item = window_find_item(window, x, y);
1393 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001394
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001395 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001396 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001397}
1398
1399static void
1400window_handle_keyboard_focus(void *data,
1401 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001402 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001403 struct wl_surface *surface,
1404 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001405{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001406 struct input *input = data;
1407 struct window *window = input->keyboard_focus;
1408 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001409 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001410
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001411 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001412 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 if (window->keyboard_focus_handler)
1414 (*window->keyboard_focus_handler)(window, NULL,
1415 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001416 }
1417
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001418 if (surface)
1419 input->keyboard_focus = wl_surface_get_user_data(surface);
1420 else
1421 input->keyboard_focus = NULL;
1422
1423 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001424 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001425 for (k = keys->data; k < end; k++)
1426 input->modifiers |= d->xkb->map->modmap[*k];
1427
1428 window = input->keyboard_focus;
1429 if (window) {
1430 window->keyboard_device = input;
1431 if (window->keyboard_focus_handler)
1432 (*window->keyboard_focus_handler)(window,
1433 window->keyboard_device,
1434 window->user_data);
1435 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001436}
1437
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001438static const struct wl_input_device_listener input_device_listener = {
1439 window_handle_motion,
1440 window_handle_button,
1441 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001442 window_handle_pointer_focus,
1443 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001444};
1445
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446void
1447input_get_position(struct input *input, int32_t *x, int32_t *y)
1448{
1449 *x = input->sx;
1450 *y = input->sy;
1451}
1452
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001453struct wl_input_device *
1454input_get_input_device(struct input *input)
1455{
1456 return input->input_device;
1457}
1458
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001459uint32_t
1460input_get_modifiers(struct input *input)
1461{
1462 return input->modifiers;
1463}
1464
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001465struct data_offer {
1466 struct wl_data_offer *offer;
1467 struct input *input;
1468 struct wl_array types;
1469 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001470
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001471 struct task io_task;
1472 int fd;
1473 data_func_t func;
1474 int32_t x, y;
1475 void *user_data;
1476};
1477
1478static void
1479data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1480{
1481 struct data_offer *offer = data;
1482 char **p;
1483
1484 p = wl_array_add(&offer->types, sizeof *p);
1485 *p = strdup(type);
1486}
1487
1488static const struct wl_data_offer_listener data_offer_listener = {
1489 data_offer_offer,
1490};
1491
1492static void
1493data_offer_destroy(struct data_offer *offer)
1494{
1495 char **p;
1496
1497 offer->refcount--;
1498 if (offer->refcount == 0) {
1499 wl_data_offer_destroy(offer->offer);
1500 for (p = offer->types.data; *p; p++)
1501 free(*p);
1502 wl_array_release(&offer->types);
1503 free(offer);
1504 }
1505}
1506
1507static void
1508data_device_data_offer(void *data,
1509 struct wl_data_device *data_device, uint32_t id)
1510{
1511 struct data_offer *offer;
1512
1513 offer = malloc(sizeof *offer);
1514
1515 wl_array_init(&offer->types);
1516 offer->refcount = 1;
1517 offer->input = data;
1518
1519 /* FIXME: Generate typesafe wrappers for this */
1520 offer->offer = (struct wl_data_offer *)
1521 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1522 id, &wl_data_offer_interface);
1523
1524 wl_data_offer_add_listener(offer->offer,
1525 &data_offer_listener, offer);
1526}
1527
1528static void
1529data_device_enter(void *data, struct wl_data_device *data_device,
1530 uint32_t time, struct wl_surface *surface,
1531 int32_t x, int32_t y, struct wl_data_offer *offer)
1532{
1533 struct input *input = data;
1534 struct window *window;
1535 char **p;
1536
1537 input->drag_offer = wl_data_offer_get_user_data(offer);
1538 window = wl_surface_get_user_data(surface);
1539 input->pointer_focus = window;
1540
1541 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1542 *p = NULL;
1543
1544 window = input->pointer_focus;
1545 if (window->data_handler)
1546 window->data_handler(window, input, time, x, y,
1547 input->drag_offer->types.data,
1548 window->user_data);
1549}
1550
1551static void
1552data_device_leave(void *data, struct wl_data_device *data_device)
1553{
1554 struct input *input = data;
1555
1556 data_offer_destroy(input->drag_offer);
1557 input->drag_offer = NULL;
1558}
1559
1560static void
1561data_device_motion(void *data, struct wl_data_device *data_device,
1562 uint32_t time, int32_t x, int32_t y)
1563{
1564 struct input *input = data;
1565 struct window *window = input->pointer_focus;
1566
1567 input->sx = x;
1568 input->sy = y;
1569
1570 if (window->data_handler)
1571 window->data_handler(window, input, time, x, y,
1572 input->drag_offer->types.data,
1573 window->user_data);
1574}
1575
1576static void
1577data_device_drop(void *data, struct wl_data_device *data_device)
1578{
1579 struct input *input = data;
1580 struct window *window = input->pointer_focus;
1581
1582 if (window->drop_handler)
1583 window->drop_handler(window, input,
1584 input->sx, input->sy, window->user_data);
1585}
1586
1587static void
1588data_device_selection(void *data,
1589 struct wl_data_device *wl_data_device,
1590 struct wl_data_offer *offer)
1591{
1592 struct input *input = data;
1593 char **p;
1594
1595 if (input->selection_offer)
1596 data_offer_destroy(input->selection_offer);
1597
1598 input->selection_offer = wl_data_offer_get_user_data(offer);
1599 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1600 *p = NULL;
1601}
1602
1603static const struct wl_data_device_listener data_device_listener = {
1604 data_device_data_offer,
1605 data_device_enter,
1606 data_device_leave,
1607 data_device_motion,
1608 data_device_drop,
1609 data_device_selection
1610};
1611
1612struct wl_data_device *
1613input_get_data_device(struct input *input)
1614{
1615 return input->data_device;
1616}
1617
1618void
1619input_set_selection(struct input *input,
1620 struct wl_data_source *source, uint32_t time)
1621{
1622 wl_data_device_set_selection(input->data_device, source, time);
1623}
1624
1625void
1626input_accept(struct input *input, uint32_t time, const char *type)
1627{
1628 wl_data_offer_accept(input->drag_offer->offer, time, type);
1629}
1630
1631static void
1632offer_io_func(struct task *task, uint32_t events)
1633{
1634 struct data_offer *offer =
1635 container_of(task, struct data_offer, io_task);
1636 unsigned int len;
1637 char buffer[4096];
1638
1639 len = read(offer->fd, buffer, sizeof buffer);
1640 offer->func(buffer, len,
1641 offer->x, offer->y, offer->user_data);
1642
1643 if (len == 0) {
1644 close(offer->fd);
1645 data_offer_destroy(offer);
1646 }
1647}
1648
1649static void
1650data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1651 data_func_t func, void *user_data)
1652{
1653 int p[2];
1654
1655 pipe2(p, O_CLOEXEC);
1656 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1657 close(p[1]);
1658
1659 offer->io_task.run = offer_io_func;
1660 offer->fd = p[0];
1661 offer->func = func;
1662 offer->refcount++;
1663 offer->user_data = user_data;
1664
1665 display_watch_fd(offer->input->display,
1666 offer->fd, EPOLLIN, &offer->io_task);
1667}
1668
1669void
1670input_receive_drag_data(struct input *input, const char *mime_type,
1671 data_func_t func, void *data)
1672{
1673 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1674 input->drag_offer->x = input->sx;
1675 input->drag_offer->y = input->sy;
1676}
1677
1678int
1679input_receive_selection_data(struct input *input, const char *mime_type,
1680 data_func_t func, void *data)
1681{
1682 char **p;
1683
1684 if (input->selection_offer == NULL)
1685 return -1;
1686
1687 for (p = input->selection_offer->types.data; *p; p++)
1688 if (strcmp(mime_type, *p) == 0)
1689 break;
1690
1691 if (*p == NULL)
1692 return -1;
1693
1694 data_offer_receive_data(input->selection_offer,
1695 mime_type, func, data);
1696 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001697}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001698
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001699void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001700window_move(struct window *window, struct input *input, uint32_t time)
1701{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001702 if (!window->shell_surface)
1703 return;
1704
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001705 wl_shell_surface_move(window->shell_surface,
1706 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001707}
1708
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001709static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001710handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001711 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001713{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001714 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001715 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001716
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001717 /* FIXME: this is probably the wrong place to check for width
1718 * or height <= 0, but it prevents the compositor from crashing
1719 */
1720 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001721 return;
1722
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001723 window->resize_edges = edges;
1724
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001725 if (window->resize_handler) {
1726 child_width = width - 20 - window->margin * 2;
1727 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001728
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001729 (*window->resize_handler)(window,
1730 child_width, child_height,
1731 window->user_data);
1732 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001733 window->allocation.width = width;
1734 window->allocation.height = height;
1735
1736 if (window->redraw_handler)
1737 window_schedule_redraw(window);
1738 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001739}
1740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001742 handle_configure,
1743};
1744
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001745void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001746window_get_allocation(struct window *window,
1747 struct rectangle *allocation)
1748{
1749 *allocation = window->allocation;
1750}
1751
1752void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001753window_get_child_allocation(struct window *window,
1754 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001755{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001756 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001757 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001758 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001759 allocation->x = window->margin + 10;
1760 allocation->y = window->margin + 50;
1761 allocation->width =
1762 window->allocation.width - 20 - window->margin * 2;
1763 allocation->height =
1764 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001765 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001766}
1767
1768void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001769window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001770{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001771 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001772 window->allocation.x = 20 + window->margin;
1773 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001774 window->allocation.width = width + 20 + window->margin * 2;
1775 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001776 } else {
1777 window->allocation.x = 0;
1778 window->allocation.y = 0;
1779 window->allocation.width = width;
1780 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001781 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001782}
1783
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001784static void
1785idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001786{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001787 struct window *window =
1788 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001789
1790 window->redraw_handler(window, window->user_data);
1791 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001792}
1793
1794void
1795window_schedule_redraw(struct window *window)
1796{
1797 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001798 window->redraw_task.run = idle_redraw;
1799 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001800 window->redraw_scheduled = 1;
1801 }
1802}
1803
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001804void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001805window_set_custom(struct window *window)
1806{
1807 window->type = TYPE_CUSTOM;
1808}
1809
1810void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001811window_set_fullscreen(struct window *window, int fullscreen)
1812{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001813 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001814 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001815
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001816 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001817 return;
1818
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001819 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001820 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001821 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001822 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001823 width = output->allocation.width;
1824 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001825 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001826 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001827 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001828 width = window->saved_allocation.width - 20 - window->margin * 2;
1829 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001830 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001831 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001832
1833 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001834}
1835
1836void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001837window_set_decoration(struct window *window, int decoration)
1838{
1839 window->decoration = decoration;
1840}
1841
1842void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001843window_set_user_data(struct window *window, void *data)
1844{
1845 window->user_data = data;
1846}
1847
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001848void *
1849window_get_user_data(struct window *window)
1850{
1851 return window->user_data;
1852}
1853
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001854void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001855window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001856 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001857{
1858 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001859}
1860
1861void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001862window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001863 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001864{
1865 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001866}
1867
1868void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001869window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001870 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001871{
1872 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001873}
1874
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001875void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001876window_set_button_handler(struct window *window,
1877 window_button_handler_t handler)
1878{
1879 window->button_handler = handler;
1880}
1881
1882void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001883window_set_motion_handler(struct window *window,
1884 window_motion_handler_t handler)
1885{
1886 window->motion_handler = handler;
1887}
1888
1889void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001890window_set_enter_handler(struct window *window,
1891 window_enter_handler_t handler)
1892{
1893 window->enter_handler = handler;
1894}
1895
1896void
1897window_set_leave_handler(struct window *window,
1898 window_leave_handler_t handler)
1899{
1900 window->leave_handler = handler;
1901}
1902
1903void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001904window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001905 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001906{
1907 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001908}
1909
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001910void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001911window_set_item_focus_handler(struct window *window,
1912 window_item_focus_handler_t handler)
1913{
1914 window->item_focus_handler = handler;
1915}
1916
1917void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001918window_set_data_handler(struct window *window, window_data_handler_t handler)
1919{
1920 window->data_handler = handler;
1921}
1922
1923void
1924window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1925{
1926 window->drop_handler = handler;
1927}
1928
1929void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001930window_set_transparent(struct window *window, int transparent)
1931{
1932 window->transparent = transparent;
1933}
1934
1935void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001936window_set_title(struct window *window, const char *title)
1937{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001938 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001939 window->title = strdup(title);
1940}
1941
1942const char *
1943window_get_title(struct window *window)
1944{
1945 return window->title;
1946}
1947
1948void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001949display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1950 int32_t x, int32_t y, int32_t width, int32_t height)
1951{
1952 struct wl_buffer *buffer;
1953
1954 buffer = display_get_buffer_for_surface(display, cairo_surface);
1955
1956 wl_buffer_damage(buffer, x, y, width, height);
1957}
1958
1959void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001960window_damage(struct window *window, int32_t x, int32_t y,
1961 int32_t width, int32_t height)
1962{
1963 wl_surface_damage(window->surface, x, y, width, height);
1964}
1965
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001966static struct window *
1967window_create_internal(struct display *display, struct window *parent,
1968 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001969{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001970 struct window *window;
1971
1972 window = malloc(sizeof *window);
1973 if (window == NULL)
1974 return NULL;
1975
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001976 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001977 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001978 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001979 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001980 if (display->shell) {
1981 window->shell_surface =
1982 wl_shell_get_shell_surface(display->shell,
1983 window->surface);
1984 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001985 window->allocation.x = 0;
1986 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001987 window->allocation.width = width;
1988 window->allocation.height = height;
1989 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001990 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001991 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001992 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001993 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001994
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001995 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001996#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001997 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1998 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001999#else
2000 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2001#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002002 else
2003 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002004
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002005 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002006 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002007
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002008 if (window->shell_surface) {
2009 wl_shell_surface_set_user_data(window->shell_surface, window);
2010 wl_shell_surface_add_listener(window->shell_surface,
2011 &shell_surface_listener, window);
2012 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002013
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002014 return window;
2015}
2016
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002017struct window *
2018window_create(struct display *display, int32_t width, int32_t height)
2019{
2020 struct window *window;
2021
2022 window = window_create_internal(display, NULL, width, height);
2023 if (!window)
2024 return NULL;
2025
2026 return window;
2027}
2028
2029struct window *
2030window_create_transient(struct display *display, struct window *parent,
2031 int32_t x, int32_t y, int32_t width, int32_t height)
2032{
2033 struct window *window;
2034
2035 window = window_create_internal(parent->display,
2036 parent, width, height);
2037 if (!window)
2038 return NULL;
2039
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002040 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002041 window->x = x;
2042 window->y = y;
2043
2044 return window;
2045}
2046
2047void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002048window_set_buffer_type(struct window *window, enum window_buffer_type type)
2049{
2050 window->buffer_type = type;
2051}
2052
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002053
2054static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002055display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002056 struct wl_output *wl_output,
2057 int x, int y,
2058 int physical_width,
2059 int physical_height,
2060 int subpixel,
2061 const char *make,
2062 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002063{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002064 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002065
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002066 output->allocation.x = x;
2067 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002068}
2069
2070static void
2071display_handle_mode(void *data,
2072 struct wl_output *wl_output,
2073 uint32_t flags,
2074 int width,
2075 int height,
2076 int refresh)
2077{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002078 struct output *output = data;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002079
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002080 if (flags & WL_OUTPUT_MODE_CURRENT) {
2081 output->allocation.width = width;
2082 output->allocation.height = height;
2083 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002084}
2085
2086static const struct wl_output_listener output_listener = {
2087 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002088 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002089};
2090
2091static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002092display_add_output(struct display *d, uint32_t id)
2093{
2094 struct output *output;
2095
2096 output = malloc(sizeof *output);
2097 if (output == NULL)
2098 return;
2099
2100 memset(output, 0, sizeof *output);
2101 output->display = d;
2102 output->output =
2103 wl_display_bind(d->display, id, &wl_output_interface);
2104 wl_list_insert(d->output_list.prev, &output->link);
2105
2106 wl_output_add_listener(output->output, &output_listener, output);
2107}
2108
2109void
2110output_get_allocation(struct output *output, struct rectangle *allocation)
2111{
2112 *allocation = output->allocation;
2113}
2114
2115static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002116display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002117{
2118 struct input *input;
2119
2120 input = malloc(sizeof *input);
2121 if (input == NULL)
2122 return;
2123
2124 memset(input, 0, sizeof *input);
2125 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002126 input->input_device =
2127 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002128 input->pointer_focus = NULL;
2129 input->keyboard_focus = NULL;
2130 wl_list_insert(d->input_list.prev, &input->link);
2131
2132 wl_input_device_add_listener(input->input_device,
2133 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002134 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002135
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002136 input->data_device =
2137 wl_data_device_manager_get_data_device(d->data_device_manager,
2138 input->input_device);
2139 wl_data_device_add_listener(input->data_device,
2140 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002141}
2142
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002143static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002144display_handle_global(struct wl_display *display, uint32_t id,
2145 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002146{
2147 struct display *d = data;
2148
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002149 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002150 d->compositor =
2151 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002152 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002153 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002154 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002155 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002156 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002157 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002158 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002159 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002160 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2161 d->data_device_manager =
2162 wl_display_bind(display, id,
2163 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002164 }
2165}
2166
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002167static void
2168display_render_frame(struct display *d)
2169{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002170 int radius = 8;
2171 cairo_t *cr;
2172
2173 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2174 cr = cairo_create(d->shadow);
2175 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2176 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2177 rounded_rect(cr, 16, 16, 112, 112, radius);
2178 cairo_fill(cr);
2179 cairo_destroy(cr);
2180 blur_surface(d->shadow, 64);
2181
2182 d->active_frame =
2183 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2184 cr = cairo_create(d->active_frame);
2185 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2186 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2187 rounded_rect(cr, 16, 16, 112, 112, radius);
2188 cairo_fill(cr);
2189 cairo_destroy(cr);
2190
2191 d->inactive_frame =
2192 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2193 cr = cairo_create(d->inactive_frame);
2194 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2195 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2196 rounded_rect(cr, 16, 16, 112, 112, radius);
2197 cairo_fill(cr);
2198 cairo_destroy(cr);
2199}
2200
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002201static void
2202init_xkb(struct display *d)
2203{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002204 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002205
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002206 names.rules = "evdev";
2207 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002208 names.layout = option_xkb_layout;
2209 names.variant = option_xkb_variant;
2210 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002211
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002212 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002213 if (!d->xkb) {
2214 fprintf(stderr, "Failed to compile keymap\n");
2215 exit(1);
2216 }
2217}
2218
Yuval Fledel45568f62010-12-06 09:18:12 -05002219static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002220init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002221{
2222 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002223 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002224
2225 static const EGLint premul_argb_cfg_attribs[] = {
2226 EGL_SURFACE_TYPE,
2227 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2228 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002229 EGL_RED_SIZE, 1,
2230 EGL_GREEN_SIZE, 1,
2231 EGL_BLUE_SIZE, 1,
2232 EGL_ALPHA_SIZE, 1,
2233 EGL_DEPTH_SIZE, 1,
2234 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2235 EGL_NONE
2236 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002237
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002238 static const EGLint rgb_cfg_attribs[] = {
2239 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2240 EGL_RED_SIZE, 1,
2241 EGL_GREEN_SIZE, 1,
2242 EGL_BLUE_SIZE, 1,
2243 EGL_ALPHA_SIZE, 0,
2244 EGL_DEPTH_SIZE, 1,
2245 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2246 EGL_NONE
2247 };
2248
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002249 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002250 if (!eglInitialize(d->dpy, &major, &minor)) {
2251 fprintf(stderr, "failed to initialize display\n");
2252 return -1;
2253 }
2254
2255 if (!eglBindAPI(EGL_OPENGL_API)) {
2256 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2257 return -1;
2258 }
2259
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002260 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002261 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002262 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002263 return -1;
2264 }
2265
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002266 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2267 &d->rgb_config, 1, &n) || n != 1) {
2268 fprintf(stderr, "failed to choose rgb config\n");
2269 return -1;
2270 }
2271
Benjamin Franzke0c991632011-09-27 21:57:31 +02002272 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2273 if (d->rgb_ctx == NULL) {
2274 fprintf(stderr, "failed to create context\n");
2275 return -1;
2276 }
2277 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2278 EGL_NO_CONTEXT, NULL);
2279 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002280 fprintf(stderr, "failed to create context\n");
2281 return -1;
2282 }
2283
Benjamin Franzke0c991632011-09-27 21:57:31 +02002284 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002285 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002286 return -1;
2287 }
2288
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002289#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002290 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2291 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002292 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002293 return -1;
2294 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002295 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2296 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2297 fprintf(stderr, "failed to get cairo egl argb device\n");
2298 return -1;
2299 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002300#endif
2301
2302 return 0;
2303}
2304
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002305static int
2306event_mask_update(uint32_t mask, void *data)
2307{
2308 struct display *d = data;
2309
2310 d->mask = mask;
2311
2312 return 0;
2313}
2314
2315static void
2316handle_display_data(struct task *task, uint32_t events)
2317{
2318 struct display *display =
2319 container_of(task, struct display, display_task);
2320
2321 wl_display_iterate(display->display, display->mask);
2322}
2323
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002324struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002325display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002326{
2327 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002328 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002329 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002330 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002331
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002332 g_type_init();
2333
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002334 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002335 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002336 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002337
2338 xkb_option_group = g_option_group_new("xkb",
2339 "Keyboard options",
2340 "Show all XKB options",
2341 NULL, NULL);
2342 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2343 g_option_context_add_group (context, xkb_option_group);
2344
2345 if (!g_option_context_parse(context, argc, argv, &error)) {
2346 fprintf(stderr, "option parsing failed: %s\n", error->message);
2347 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002348 }
2349
Tim Wiederhake748f6722011-01-23 23:25:25 +01002350 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002351
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002352 d = malloc(sizeof *d);
2353 if (d == NULL)
2354 return NULL;
2355
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002356 memset(d, 0, sizeof *d);
2357
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002358 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002359 if (d->display == NULL) {
2360 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002361 return NULL;
2362 }
2363
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002364 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2365 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2366 d->display_task.run = handle_display_data;
2367 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2368
2369 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002370 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002371 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002372
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002373 /* Set up listener so we'll catch all events. */
2374 wl_display_add_global_listener(d->display,
2375 display_handle_global, d);
2376
2377 /* Process connection events. */
2378 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002379 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002380 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002381
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002382 d->image_target_texture_2d =
2383 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2384 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2385 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2386
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002387 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002388
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002389 display_render_frame(d);
2390
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002391 wl_list_init(&d->window_list);
2392
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002393 init_xkb(d);
2394
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002395 return d;
2396}
2397
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002398struct wl_display *
2399display_get_display(struct display *display)
2400{
2401 return display->display;
2402}
2403
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002404struct output *
2405display_get_output(struct display *display)
2406{
2407 return container_of(display->output_list.next, struct output, link);
2408}
2409
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002410struct wl_compositor *
2411display_get_compositor(struct display *display)
2412{
2413 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002414}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002415
2416EGLDisplay
2417display_get_egl_display(struct display *d)
2418{
2419 return d->dpy;
2420}
2421
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002422struct wl_data_source *
2423display_create_data_source(struct display *display)
2424{
2425 return wl_data_device_manager_create_data_source(display->data_device_manager);
2426}
2427
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002428EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002429display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002430{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002431 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002432}
2433
Benjamin Franzke0c991632011-09-27 21:57:31 +02002434EGLConfig
2435display_get_argb_egl_config(struct display *d)
2436{
2437 return d->premultiplied_argb_config;
2438}
2439
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002440struct wl_shell *
2441display_get_shell(struct display *display)
2442{
2443 return display->shell;
2444}
2445
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002446int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002447display_acquire_window_surface(struct display *display,
2448 struct window *window,
2449 EGLContext ctx)
2450{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002451#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002452 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002453 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002454
2455 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002456 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002457 device = cairo_surface_get_device(window->cairo_surface);
2458 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002459 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002460
Benjamin Franzke0c991632011-09-27 21:57:31 +02002461 if (!ctx) {
2462 if (device == display->rgb_device)
2463 ctx = display->rgb_ctx;
2464 else if (device == display->argb_device)
2465 ctx = display->argb_ctx;
2466 else
2467 assert(0);
2468 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002469
2470 data = cairo_surface_get_user_data(window->cairo_surface,
2471 &surface_data_key);
2472
Benjamin Franzke0c991632011-09-27 21:57:31 +02002473 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002474 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2475 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002476
2477 return 0;
2478#else
2479 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002480#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002481}
2482
2483void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002484display_release_window_surface(struct display *display,
2485 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002486{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002487#ifdef HAVE_CAIRO_EGL
2488 cairo_device_t *device;
2489
2490 device = cairo_surface_get_device(window->cairo_surface);
2491 if (!device)
2492 return;
2493
2494 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002495 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002496 cairo_device_release(device);
2497#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002498}
2499
2500void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002501display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002502{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002503 wl_list_insert(&display->deferred_list, &task->link);
2504}
2505
2506void
2507display_watch_fd(struct display *display,
2508 int fd, uint32_t events, struct task *task)
2509{
2510 struct epoll_event ep;
2511
2512 ep.events = events;
2513 ep.data.ptr = task;
2514 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2515}
2516
2517void
2518display_run(struct display *display)
2519{
2520 struct task *task;
2521 struct epoll_event ep[16];
2522 int i, count;
2523
2524 while (1) {
2525 while (display->mask & WL_DISPLAY_WRITABLE)
2526 wl_display_iterate(display->display,
2527 WL_DISPLAY_WRITABLE);
2528
2529 count = epoll_wait(display->epoll_fd,
2530 ep, ARRAY_LENGTH(ep), -1);
2531 for (i = 0; i < count; i++) {
2532 task = ep[i].data.ptr;
2533 task->run(task, ep[i].events);
2534 }
2535
2536 while (!wl_list_empty(&display->deferred_list)) {
2537 task = container_of(display->deferred_list.next,
2538 struct task, link);
2539 wl_list_remove(&task->link);
2540 task->run(task, 0);
2541 }
2542 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002543}