blob: 5324795dc19ad4a7af588501a4b3a6073885d619 [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;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500106 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500107 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500108 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400109 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400110 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400111 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500112 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500113 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400114 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400115 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400116 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400117 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500118 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400119 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500120
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500122 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500123
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500124 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500126 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400127 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500128 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400129 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400130 window_enter_handler_t enter_handler;
131 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400132 window_item_focus_handler_t item_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400133 window_data_handler_t data_handler;
134 window_drop_handler_t drop_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400135
136 struct wl_list item_list;
137 struct item *focus_item;
138 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400139
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500140 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400141 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500142};
143
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400144struct item {
145 struct wl_list link;
146 struct rectangle allocation;
147 void *user_data;
148};
149
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400150struct input {
151 struct display *display;
152 struct wl_input_device *input_device;
153 struct window *pointer_focus;
154 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400155 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400156 uint32_t modifiers;
157 int32_t x, y, sx, sy;
158 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400159
160 struct wl_data_device *data_device;
161 struct data_offer *drag_offer;
162 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400163};
164
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500165struct output {
166 struct display *display;
167 struct wl_output *output;
168 struct rectangle allocation;
169 struct wl_list link;
170};
171
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400172enum {
173 POINTER_DEFAULT = 100,
174 POINTER_UNSET
175};
176
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500177enum window_location {
178 WINDOW_INTERIOR = 0,
179 WINDOW_RESIZING_TOP = 1,
180 WINDOW_RESIZING_BOTTOM = 2,
181 WINDOW_RESIZING_LEFT = 4,
182 WINDOW_RESIZING_TOP_LEFT = 5,
183 WINDOW_RESIZING_BOTTOM_LEFT = 6,
184 WINDOW_RESIZING_RIGHT = 8,
185 WINDOW_RESIZING_TOP_RIGHT = 9,
186 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
187 WINDOW_RESIZING_MASK = 15,
188 WINDOW_EXTERIOR = 16,
189 WINDOW_TITLEBAR = 17,
190 WINDOW_CLIENT_AREA = 18,
191};
192
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400193const char *option_xkb_layout = "us";
194const char *option_xkb_variant = "";
195const char *option_xkb_options = "";
196
197static const GOptionEntry xkb_option_entries[] = {
198 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
199 &option_xkb_layout, "XKB Layout" },
200 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
201 &option_xkb_variant, "XKB Variant" },
202 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
203 &option_xkb_options, "XKB Options" },
204 { NULL }
205};
206
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400207static const cairo_user_data_key_t surface_data_key;
208struct surface_data {
209 struct wl_buffer *buffer;
210};
211
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500212#define MULT(_d,c,a,t) \
213 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
214
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500215#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400216
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100217struct egl_window_surface_data {
218 struct display *display;
219 struct wl_surface *surface;
220 struct wl_egl_window *window;
221 EGLSurface surf;
222};
223
224static void
225egl_window_surface_data_destroy(void *p)
226{
227 struct egl_window_surface_data *data = p;
228 struct display *d = data->display;
229
230 eglDestroySurface(d->dpy, data->surf);
231 wl_egl_window_destroy(data->window);
232 data->surface = NULL;
233
234 free(p);
235}
236
237static cairo_surface_t *
238display_create_egl_window_surface(struct display *display,
239 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400240 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100241 struct rectangle *rectangle)
242{
243 cairo_surface_t *cairo_surface;
244 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400245 EGLConfig config;
246 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200247 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100248
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400249 static const EGLint premul_attribs[] = {
250 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
251 EGL_NONE
252 };
253
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100254 data = malloc(sizeof *data);
255 if (data == NULL)
256 return NULL;
257
258 data->display = display;
259 data->surface = surface;
260
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400261 if (flags & SURFACE_OPAQUE) {
262 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200263 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200264 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400265 } else {
266 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200267 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 attribs = premul_attribs;
269 }
270
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400271 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100272 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100274
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400275 data->surf = eglCreateWindowSurface(display->dpy, config,
276 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100277
Benjamin Franzke0c991632011-09-27 21:57:31 +0200278 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100279 data->surf,
280 rectangle->width,
281 rectangle->height);
282
283 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
284 data, egl_window_surface_data_destroy);
285
286 return cairo_surface;
287}
288
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500289struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292 EGLImageKHR image;
293 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800294 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500295 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400296};
297
298static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500299egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400300{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500301 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800302 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400303
Benjamin Franzke0c991632011-09-27 21:57:31 +0200304 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400305 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200306 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800307
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500308 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400309 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500310 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500311 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312}
313
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500314EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315display_get_image_for_egl_image_surface(struct display *display,
316 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500317{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500319
320 data = cairo_surface_get_user_data (surface, &surface_data_key);
321
322 return data->image;
323}
324
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500325static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500326display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400327 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500328 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500330 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331 EGLDisplay dpy = display->dpy;
332 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200333 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334
335 data = malloc(sizeof *data);
336 if (data == NULL)
337 return NULL;
338
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800339 data->display = display;
340
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400341 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400342 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500343 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000344 free(data);
345 return NULL;
346 }
347
Benjamin Franzke0c991632011-09-27 21:57:31 +0200348 if (flags & SURFACE_OPAQUE) {
349 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200350 content = CAIRO_CONTENT_COLOR;
351 } else {
352 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200353 content = CAIRO_CONTENT_COLOR_ALPHA;
354 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400355
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500356 data->image = display->create_image(dpy, NULL,
357 EGL_NATIVE_PIXMAP_KHR,
358 (EGLClientBuffer) data->pixmap,
359 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500360 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500361 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500362 free(data);
363 return NULL;
364 }
365
366 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400367 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500368
Benjamin Franzke0c991632011-09-27 21:57:31 +0200369 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400370 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400371 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500372 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400374
Benjamin Franzke0c991632011-09-27 21:57:31 +0200375 surface = cairo_gl_surface_create_for_texture(data->device,
376 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400377 data->texture,
378 rectangle->width,
379 rectangle->height);
380
381 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400383
384 return surface;
385}
386
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500387static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500388display_create_egl_image_surface_from_file(struct display *display,
389 const char *filename,
390 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391{
392 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400393 GdkPixbuf *pixbuf;
394 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400395 int stride, i;
396 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500397 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400398
399 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400401 FALSE, &error);
402 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400404
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400405 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
406 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500407 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400409 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400410
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400411
412 stride = gdk_pixbuf_get_rowstride(pixbuf);
413 pixels = gdk_pixbuf_get_pixels(pixbuf);
414
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400416 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400418 while (p < end) {
419 unsigned int t;
420
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400421 MULT(p[0], p[0], p[3], t);
422 MULT(p[1], p[1], p[3], t);
423 MULT(p[2], p[2], p[3], t);
424 p += 4;
425
426 }
427 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400428
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200429 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000430 if (surface == NULL) {
431 g_object_unref(pixbuf);
432 return NULL;
433 }
434
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800435 data = cairo_surface_get_user_data(surface, &surface_data_key);
436
Benjamin Franzke0c991632011-09-27 21:57:31 +0200437 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800438 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800439 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
440 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200441 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400442
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500443 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400444
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445 return surface;
446}
447
448#endif
449
450struct wl_buffer *
451display_get_buffer_for_surface(struct display *display,
452 cairo_surface_t *surface)
453{
454 struct surface_data *data;
455
456 data = cairo_surface_get_user_data (surface, &surface_data_key);
457
458 return data->buffer;
459}
460
461struct shm_surface_data {
462 struct surface_data data;
463 void *map;
464 size_t length;
465};
466
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500467static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468shm_surface_data_destroy(void *p)
469{
470 struct shm_surface_data *data = p;
471
472 wl_buffer_destroy(data->data.buffer);
473 munmap(data->map, data->length);
474}
475
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500476static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400478 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479{
480 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400481 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400482 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800483 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400484 char filename[] = "/tmp/wayland-shm-XXXXXX";
485
486 data = malloc(sizeof *data);
487 if (data == NULL)
488 return NULL;
489
490 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
491 rectangle->width);
492 data->length = stride * rectangle->height;
493 fd = mkstemp(filename);
494 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000495 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496 return NULL;
497 }
498 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000499 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500 close(fd);
501 return NULL;
502 }
503
504 data->map = mmap(NULL, data->length,
505 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
506 unlink(filename);
507
508 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000509 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510 close(fd);
511 return NULL;
512 }
513
514 surface = cairo_image_surface_create_for_data (data->map,
515 CAIRO_FORMAT_ARGB32,
516 rectangle->width,
517 rectangle->height,
518 stride);
519
520 cairo_surface_set_user_data (surface, &surface_data_key,
521 data, shm_surface_data_destroy);
522
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400523 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400524 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400525 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400526 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400527
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528 data->data.buffer = wl_shm_create_buffer(display->shm,
529 fd,
530 rectangle->width,
531 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400532 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533
534 close(fd);
535
536 return surface;
537}
538
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500539static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540display_create_shm_surface_from_file(struct display *display,
541 const char *filename,
542 struct rectangle *rect)
543{
544 cairo_surface_t *surface;
545 GdkPixbuf *pixbuf;
546 GError *error = NULL;
547 int stride, i;
548 unsigned char *pixels, *p, *end, *dest_data;
549 int dest_stride;
550 uint32_t *d;
551
552 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
553 rect->width, rect->height,
554 FALSE, &error);
555 if (error != NULL)
556 return NULL;
557
558 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
559 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500560 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561 return NULL;
562 }
563
564 stride = gdk_pixbuf_get_rowstride(pixbuf);
565 pixels = gdk_pixbuf_get_pixels(pixbuf);
566
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400567 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000568 if (surface == NULL) {
569 g_object_unref(pixbuf);
570 return NULL;
571 }
572
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573 dest_data = cairo_image_surface_get_data (surface);
574 dest_stride = cairo_image_surface_get_stride (surface);
575
576 for (i = 0; i < rect->height; i++) {
577 d = (uint32_t *) (dest_data + i * dest_stride);
578 p = pixels + i * stride;
579 end = p + rect->width * 4;
580 while (p < end) {
581 unsigned int t;
582 unsigned char a, r, g, b;
583
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584 a = p[3];
585 MULT(r, p[0], a, t);
586 MULT(g, p[1], a, t);
587 MULT(b, p[2], a, t);
588 p += 4;
589 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
590 }
591 }
592
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500593 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400594
595 return surface;
596}
597
nobled7b87cb02011-02-01 18:51:47 +0000598static int
599check_size(struct rectangle *rect)
600{
601 if (rect->width && rect->height)
602 return 0;
603
604 fprintf(stderr, "tried to create surface of "
605 "width: %d, height: %d\n", rect->width, rect->height);
606 return -1;
607}
608
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400609cairo_surface_t *
610display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100611 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400612 struct rectangle *rectangle,
613 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614{
nobled7b87cb02011-02-01 18:51:47 +0000615 if (check_size(rectangle) < 0)
616 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500617#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500618 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100619 if (surface)
620 return display_create_egl_window_surface(display,
621 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400622 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 rectangle);
624 else
625 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400626 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100627 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500628 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400630 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631}
632
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500633static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634display_create_surface_from_file(struct display *display,
635 const char *filename,
636 struct rectangle *rectangle)
637{
nobled7b87cb02011-02-01 18:51:47 +0000638 if (check_size(rectangle) < 0)
639 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500640#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500641 if (display->dpy) {
642 return display_create_egl_image_surface_from_file(display,
643 filename,
644 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500645 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400646#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500647 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400648}
Yuval Fledel45568f62010-12-06 09:18:12 -0500649 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650 const char *filename;
651 int hotspot_x, hotspot_y;
652} pointer_images[] = {
653 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
654 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
655 { DATADIR "/wayland/bottom_side.png", 16, 20 },
656 { DATADIR "/wayland/grabbing.png", 20, 17 },
657 { DATADIR "/wayland/left_ptr.png", 10, 5 },
658 { DATADIR "/wayland/left_side.png", 10, 20 },
659 { DATADIR "/wayland/right_side.png", 30, 19 },
660 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
661 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
662 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400663 { DATADIR "/wayland/xterm.png", 15, 15 },
664 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400665};
666
667static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400668create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669{
670 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400675 display->pointer_surfaces =
676 malloc(count * sizeof *display->pointer_surfaces);
677 rect.width = width;
678 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400679 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681 display_create_surface_from_file(display,
682 pointer_images[i].filename,
683 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100684 if (!display->pointer_surfaces[i]) {
685 fprintf(stderr, "Error loading pointer image: %s\n",
686 pointer_images[i].filename);
687 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688 }
689
690}
691
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692cairo_surface_t *
693display_get_pointer_surface(struct display *display, int pointer,
694 int *width, int *height,
695 int *hotspot_x, int *hotspot_y)
696{
697 cairo_surface_t *surface;
698
699 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500700#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701 *width = cairo_gl_surface_get_width(surface);
702 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000703#else
704 *width = cairo_image_surface_get_width(surface);
705 *height = cairo_image_surface_get_height(surface);
706#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400707 *hotspot_x = pointer_images[pointer].hotspot_x;
708 *hotspot_y = pointer_images[pointer].hotspot_y;
709
710 return cairo_surface_reference(surface);
711}
712
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400713static void
714window_attach_surface(struct window *window);
715
716static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400717free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400718{
719 struct window *window = data;
720
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400721 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400722 cairo_surface_destroy(window->pending_surface);
723 window->pending_surface = NULL;
724 if (window->cairo_surface)
725 window_attach_surface(window);
726}
727
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400728static const struct wl_callback_listener free_surface_listener = {
729 free_surface
730};
731
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500732static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200733window_get_resize_dx_dy(struct window *window, int *x, int *y)
734{
735 if (window->resize_edges & WINDOW_RESIZING_LEFT)
736 *x = window->server_allocation.width - window->allocation.width;
737 else
738 *x = 0;
739
740 if (window->resize_edges & WINDOW_RESIZING_TOP)
741 *y = window->server_allocation.height -
742 window->allocation.height;
743 else
744 *y = 0;
745
746 window->resize_edges = 0;
747}
748
749static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400750window_set_type(struct window *window)
751{
752 struct display *display = window->display;
753
754 switch (window->type) {
755 case TYPE_FULLSCREEN:
756 wl_shell_set_fullscreen(display->shell, window->surface);
757 break;
758 case TYPE_TOPLEVEL:
759 wl_shell_set_toplevel(display->shell, window->surface);
760 break;
761 case TYPE_TRANSIENT:
762 wl_shell_set_transient(display->shell, window->surface,
763 window->parent->surface,
764 window->x, window->y, 0);
765 break;
766 case TYPE_CUSTOM:
767 break;
768 }
769}
770
771static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500772window_attach_surface(struct window *window)
773{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400774 struct display *display = window->display;
775 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400776 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000777#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000779#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500780 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400782 if (display->shell)
783 window_set_type(window);
784
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000786#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
788 data = cairo_surface_get_user_data(window->cairo_surface,
789 &surface_data_key);
790
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791 cairo_gl_surface_swapbuffers(window->cairo_surface);
792 wl_egl_window_get_attached_size(data->window,
793 &window->server_allocation.width,
794 &window->server_allocation.height);
795 break;
796 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200799 window_get_resize_dx_dy(window, &x, &y);
800
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801 if (window->pending_surface != NULL)
802 return;
803
804 window->pending_surface = window->cairo_surface;
805 window->cairo_surface = NULL;
806
807 buffer =
808 display_get_buffer_for_surface(display,
809 window->pending_surface);
810
811 wl_surface_attach(window->surface, buffer, x, y);
812 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400813 cb = wl_display_sync(display->display);
814 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000816 default:
817 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500819
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500820 wl_surface_damage(window->surface, 0, 0,
821 window->allocation.width,
822 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500823}
824
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500825void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400826window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500827{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100828 if (window->cairo_surface) {
829 switch (window->buffer_type) {
830 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
831 case WINDOW_BUFFER_TYPE_SHM:
832 display_surface_damage(window->display,
833 window->cairo_surface,
834 0, 0,
835 window->allocation.width,
836 window->allocation.height);
837 break;
838 default:
839 break;
840 }
841 window_attach_surface(window);
842 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500843}
844
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400845void
846window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500848 cairo_surface_reference(surface);
849
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400850 if (window->cairo_surface != NULL)
851 cairo_surface_destroy(window->cairo_surface);
852
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500853 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400854}
855
Benjamin Franzke22d54812011-07-16 19:50:32 +0000856#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100857static void
858window_resize_cairo_window_surface(struct window *window)
859{
860 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200861 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100862
863 data = cairo_surface_get_user_data(window->cairo_surface,
864 &surface_data_key);
865
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200866 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100867 wl_egl_window_resize(data->window,
868 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200869 window->allocation.height,
870 x,y);
871
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872 cairo_gl_surface_set_size(window->cairo_surface,
873 window->allocation.width,
874 window->allocation.height);
875}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000876#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400878struct display *
879window_get_display(struct window *window)
880{
881 return window->display;
882}
883
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400884void
885window_create_surface(struct window *window)
886{
887 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400888 uint32_t flags = 0;
889
890 if (!window->transparent)
891 flags = SURFACE_OPAQUE;
892
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400893 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500894#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
896 if (window->cairo_surface) {
897 window_resize_cairo_window_surface(window);
898 return;
899 }
900 surface = display_create_surface(window->display,
901 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400902 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500904 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400905 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400907 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908 break;
909#endif
910 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400911 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400913 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800914 default:
915 surface = NULL;
916 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400917 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400918
919 window_set_surface(window, surface);
920 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400921}
922
923static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500924window_draw_menu(struct window *window)
925{
926 cairo_t *cr;
927 int width, height, r = 5;
928
929 window_create_surface(window);
930
931 cr = cairo_create(window->cairo_surface);
932 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
933 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
934 cairo_paint(cr);
935
936 width = window->allocation.width;
937 height = window->allocation.height;
938 rounded_rect(cr, r, r, width - r, height - r, r);
939 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
940 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
941 cairo_fill(cr);
942 cairo_destroy(cr);
943}
944
945static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500946window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500947{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500948 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500949 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400950 cairo_surface_t *frame;
951 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500952
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400953 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400954
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400955 width = window->allocation.width;
956 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500957
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500958 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500959
Kristian Høgsberg09531622010-06-14 23:22:15 -0400960 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400961 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400962 cairo_paint(cr);
963
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400964 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400965 tile_mask(cr, window->display->shadow,
966 shadow_dx, shadow_dy, width, height,
967 window->margin + 10 - shadow_dx,
968 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500969
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400970 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400971 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400973 frame = window->display->inactive_frame;
974
975 tile_source(cr, frame, 0, 0, width, height,
976 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500977
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500978 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
979 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500980 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400981 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400982 if (window->keyboard_device)
983 cairo_set_source_rgb(cr, 0, 0, 0);
984 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400985 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400986 cairo_show_text(cr, window->title);
987
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500988 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400989
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100990 /* FIXME: this breakes gears, fix cairo? */
991#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400992 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100993#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500994}
995
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400996void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500997window_destroy(struct window *window)
998{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200999 struct display *display = window->display;
1000 struct input *input;
1001
1002 if (window->redraw_scheduled)
1003 wl_list_remove(&window->redraw_task.link);
1004
1005 wl_list_for_each(input, &display->input_list, link) {
1006 if (input->pointer_focus == window)
1007 input->pointer_focus = NULL;
1008 if (input->keyboard_focus == window)
1009 input->keyboard_focus = NULL;
1010 }
1011
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001012 wl_surface_destroy(window->surface);
1013 wl_list_remove(&window->link);
1014 free(window);
1015}
1016
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017static struct item *
1018window_find_item(struct window *window, int32_t x, int32_t y)
1019{
1020 struct item *item;
1021
1022 wl_list_for_each(item, &window->item_list, link) {
1023 if (item->allocation.x <= x &&
1024 x < item->allocation.x + item->allocation.width &&
1025 item->allocation.y <= y &&
1026 y < item->allocation.y + item->allocation.height) {
1027 return item;
1028 }
1029 }
1030
1031 return NULL;
1032}
1033
1034struct item *
1035window_add_item(struct window *window, void *data)
1036{
1037 struct item *item;
1038
1039 item = malloc(sizeof *item);
1040 memset(item, 0, sizeof *item);
1041 item->user_data = data;
1042 wl_list_insert(window->item_list.prev, &item->link);
1043
1044 return item;
1045}
1046
1047void
1048window_for_each_item(struct window *window, item_func_t func, void *data)
1049{
1050 struct item *item;
1051
1052 wl_list_for_each(item, &window->item_list, link)
1053 func(item, data);
1054}
1055
1056struct item *
1057window_get_focus_item(struct window *window)
1058{
1059 return window->focus_item;
1060}
1061
1062void
1063item_get_allocation(struct item *item, struct rectangle *allocation)
1064{
1065 *allocation = item->allocation;
1066}
1067
1068void
1069item_set_allocation(struct item *item,
1070 int32_t x, int32_t y, int32_t width, int32_t height)
1071{
1072 item->allocation.x = x;
1073 item->allocation.y = y;
1074 item->allocation.width = width;
1075 item->allocation.height = height;
1076}
1077
1078void *
1079item_get_user_data(struct item *item)
1080{
1081 return item->user_data;
1082}
1083
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001084void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001085window_draw(struct window *window)
1086{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001087 if (window->parent)
1088 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001089 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001090 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001091 else
1092 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001093}
1094
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001095cairo_surface_t *
1096window_get_surface(struct window *window)
1097{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001098 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001099}
1100
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001101struct wl_surface *
1102window_get_wl_surface(struct window *window)
1103{
1104 return window->surface;
1105}
1106
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107static int
1108get_pointer_location(struct window *window, int32_t x, int32_t y)
1109{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111 const int grip_size = 8;
1112
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001113 if (!window->decoration)
1114 return WINDOW_CLIENT_AREA;
1115
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001116 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001117 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001118 else if (window->margin <= x && x < window->margin + grip_size)
1119 hlocation = WINDOW_RESIZING_LEFT;
1120 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001121 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001122 else if (x < window->allocation.width - window->margin)
1123 hlocation = WINDOW_RESIZING_RIGHT;
1124 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126
1127 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001128 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001129 else if (window->margin <= y && y < window->margin + grip_size)
1130 vlocation = WINDOW_RESIZING_TOP;
1131 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001132 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001133 else if (y < window->allocation.height - window->margin)
1134 vlocation = WINDOW_RESIZING_BOTTOM;
1135 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001136 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001137
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001138 location = vlocation | hlocation;
1139 if (location & WINDOW_EXTERIOR)
1140 location = WINDOW_EXTERIOR;
1141 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1142 location = WINDOW_TITLEBAR;
1143 else if (location == WINDOW_INTERIOR)
1144 location = WINDOW_CLIENT_AREA;
1145
1146 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001147}
1148
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001149void
1150input_set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001151{
1152 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001153 struct wl_buffer *buffer;
1154 cairo_surface_t *surface;
1155 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001156
1157 location = get_pointer_location(input->pointer_focus,
1158 input->sx, input->sy);
1159 switch (location) {
1160 case WINDOW_RESIZING_TOP:
1161 pointer = POINTER_TOP;
1162 break;
1163 case WINDOW_RESIZING_BOTTOM:
1164 pointer = POINTER_BOTTOM;
1165 break;
1166 case WINDOW_RESIZING_LEFT:
1167 pointer = POINTER_LEFT;
1168 break;
1169 case WINDOW_RESIZING_RIGHT:
1170 pointer = POINTER_RIGHT;
1171 break;
1172 case WINDOW_RESIZING_TOP_LEFT:
1173 pointer = POINTER_TOP_LEFT;
1174 break;
1175 case WINDOW_RESIZING_TOP_RIGHT:
1176 pointer = POINTER_TOP_RIGHT;
1177 break;
1178 case WINDOW_RESIZING_BOTTOM_LEFT:
1179 pointer = POINTER_BOTTOM_LEFT;
1180 break;
1181 case WINDOW_RESIZING_BOTTOM_RIGHT:
1182 pointer = POINTER_BOTTOM_RIGHT;
1183 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001184 case WINDOW_EXTERIOR:
1185 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001186 if (input->current_pointer_image == POINTER_DEFAULT)
1187 return;
1188
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001189 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001190 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001191 return;
1192 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193 break;
1194 }
1195
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001196 if (pointer == input->current_pointer_image)
1197 return;
1198
1199 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001200 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001201
1202 if (!surface)
1203 return;
1204
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001205 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001206 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001207 pointer_images[pointer].hotspot_x,
1208 pointer_images[pointer].hotspot_y);
1209}
1210
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001211static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001212window_set_focus_item(struct window *window, struct item *focus)
1213{
1214 void *data;
1215
1216 if (focus == window->focus_item)
1217 return;
1218
1219 window->focus_item = focus;
1220 data = focus ? focus->user_data : NULL;
1221 if (window->item_focus_handler)
1222 window->item_focus_handler(window, focus, data);
1223}
1224
1225static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001226window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001227 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001228 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001229{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001230 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001231 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001232 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001233 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001234
1235 input->x = x;
1236 input->y = y;
1237 input->sx = sx;
1238 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001239
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001240 if (!window->focus_item || !window->item_grab_button) {
1241 item = window_find_item(window, sx, sy);
1242 window_set_focus_item(window, item);
1243 }
1244
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245 if (window->motion_handler)
1246 pointer = (*window->motion_handler)(window, input, time,
1247 x, y, sx, sy,
1248 window->user_data);
1249
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001250 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001251}
1252
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001253static void
1254window_handle_button(void *data,
1255 struct wl_input_device *input_device,
1256 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001257{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001258 struct input *input = data;
1259 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001260 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001262
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001263 if (window->focus_item && window->item_grab_button == 0 && state)
1264 window->item_grab_button = button;
1265
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001267
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001268 if (window->display->shell &&
1269 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001271 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001272 wl_shell_move(window->display->shell,
1273 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001274 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001275 case WINDOW_RESIZING_TOP:
1276 case WINDOW_RESIZING_BOTTOM:
1277 case WINDOW_RESIZING_LEFT:
1278 case WINDOW_RESIZING_RIGHT:
1279 case WINDOW_RESIZING_TOP_LEFT:
1280 case WINDOW_RESIZING_TOP_RIGHT:
1281 case WINDOW_RESIZING_BOTTOM_LEFT:
1282 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001283 wl_shell_resize(window->display->shell,
1284 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001286 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001287 case WINDOW_CLIENT_AREA:
1288 if (window->button_handler)
1289 (*window->button_handler)(window,
1290 input, time,
1291 button, state,
1292 window->user_data);
1293 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001294 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 } else {
1296 if (window->button_handler)
1297 (*window->button_handler)(window,
1298 input, time,
1299 button, state,
1300 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001301 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001302
1303 if (window->focus_item &&
1304 window->item_grab_button == button && !state) {
1305 window->item_grab_button = 0;
1306 item = window_find_item(window, input->sx, input->sy);
1307 window_set_focus_item(window, item);
1308 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001309}
1310
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001311static void
1312window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001313 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001314{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001315 struct input *input = data;
1316 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001317 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001318 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001319
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001320 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001321 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001322 return;
1323
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001324 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001325 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001326 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1327 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001328
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001329 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1330
1331 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001332 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001333 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001334 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001335
1336 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001337 (*window->key_handler)(window, input, time, key, sym, state,
1338 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001339}
1340
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001341static void
1342window_handle_pointer_focus(void *data,
1343 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001344 uint32_t time, struct wl_surface *surface,
1345 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001346{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001347 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001348 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001349 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001350 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001351
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001352 window = input->pointer_focus;
1353 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001354 window_set_focus_item(window, NULL);
1355
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001356 if (window->leave_handler)
1357 window->leave_handler(window, input,
1358 time, window->user_data);
1359 input->pointer_focus = NULL;
1360 input->current_pointer_image = POINTER_UNSET;
1361 }
1362
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001363 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001364 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001365 window = input->pointer_focus;
1366
Kristian Høgsberg59826582011-01-20 11:56:57 -05001367 input->x = x;
1368 input->y = y;
1369 input->sx = sx;
1370 input->sy = sy;
1371
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001372 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001373 if (window->enter_handler)
1374 pointer = window->enter_handler(window, input,
1375 time, sx, sy,
1376 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001377
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001378 item = window_find_item(window, x, y);
1379 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001380
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001381 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001382 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001383}
1384
1385static void
1386window_handle_keyboard_focus(void *data,
1387 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001388 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001389 struct wl_surface *surface,
1390 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001391{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001392 struct input *input = data;
1393 struct window *window = input->keyboard_focus;
1394 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001395 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001396
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001397 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001398 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001399 if (window->keyboard_focus_handler)
1400 (*window->keyboard_focus_handler)(window, NULL,
1401 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001402 }
1403
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001404 if (surface)
1405 input->keyboard_focus = wl_surface_get_user_data(surface);
1406 else
1407 input->keyboard_focus = NULL;
1408
1409 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001410 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001411 for (k = keys->data; k < end; k++)
1412 input->modifiers |= d->xkb->map->modmap[*k];
1413
1414 window = input->keyboard_focus;
1415 if (window) {
1416 window->keyboard_device = input;
1417 if (window->keyboard_focus_handler)
1418 (*window->keyboard_focus_handler)(window,
1419 window->keyboard_device,
1420 window->user_data);
1421 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001422}
1423
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001424static const struct wl_input_device_listener input_device_listener = {
1425 window_handle_motion,
1426 window_handle_button,
1427 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001428 window_handle_pointer_focus,
1429 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001430};
1431
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001432void
1433input_get_position(struct input *input, int32_t *x, int32_t *y)
1434{
1435 *x = input->sx;
1436 *y = input->sy;
1437}
1438
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001439struct wl_input_device *
1440input_get_input_device(struct input *input)
1441{
1442 return input->input_device;
1443}
1444
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001445uint32_t
1446input_get_modifiers(struct input *input)
1447{
1448 return input->modifiers;
1449}
1450
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001451struct data_offer {
1452 struct wl_data_offer *offer;
1453 struct input *input;
1454 struct wl_array types;
1455 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001456
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001457 struct task io_task;
1458 int fd;
1459 data_func_t func;
1460 int32_t x, y;
1461 void *user_data;
1462};
1463
1464static void
1465data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1466{
1467 struct data_offer *offer = data;
1468 char **p;
1469
1470 p = wl_array_add(&offer->types, sizeof *p);
1471 *p = strdup(type);
1472}
1473
1474static const struct wl_data_offer_listener data_offer_listener = {
1475 data_offer_offer,
1476};
1477
1478static void
1479data_offer_destroy(struct data_offer *offer)
1480{
1481 char **p;
1482
1483 offer->refcount--;
1484 if (offer->refcount == 0) {
1485 wl_data_offer_destroy(offer->offer);
1486 for (p = offer->types.data; *p; p++)
1487 free(*p);
1488 wl_array_release(&offer->types);
1489 free(offer);
1490 }
1491}
1492
1493static void
1494data_device_data_offer(void *data,
1495 struct wl_data_device *data_device, uint32_t id)
1496{
1497 struct data_offer *offer;
1498
1499 offer = malloc(sizeof *offer);
1500
1501 wl_array_init(&offer->types);
1502 offer->refcount = 1;
1503 offer->input = data;
1504
1505 /* FIXME: Generate typesafe wrappers for this */
1506 offer->offer = (struct wl_data_offer *)
1507 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1508 id, &wl_data_offer_interface);
1509
1510 wl_data_offer_add_listener(offer->offer,
1511 &data_offer_listener, offer);
1512}
1513
1514static void
1515data_device_enter(void *data, struct wl_data_device *data_device,
1516 uint32_t time, struct wl_surface *surface,
1517 int32_t x, int32_t y, struct wl_data_offer *offer)
1518{
1519 struct input *input = data;
1520 struct window *window;
1521 char **p;
1522
1523 input->drag_offer = wl_data_offer_get_user_data(offer);
1524 window = wl_surface_get_user_data(surface);
1525 input->pointer_focus = window;
1526
1527 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1528 *p = NULL;
1529
1530 window = input->pointer_focus;
1531 if (window->data_handler)
1532 window->data_handler(window, input, time, x, y,
1533 input->drag_offer->types.data,
1534 window->user_data);
1535}
1536
1537static void
1538data_device_leave(void *data, struct wl_data_device *data_device)
1539{
1540 struct input *input = data;
1541
1542 data_offer_destroy(input->drag_offer);
1543 input->drag_offer = NULL;
1544}
1545
1546static void
1547data_device_motion(void *data, struct wl_data_device *data_device,
1548 uint32_t time, int32_t x, int32_t y)
1549{
1550 struct input *input = data;
1551 struct window *window = input->pointer_focus;
1552
1553 input->sx = x;
1554 input->sy = y;
1555
1556 if (window->data_handler)
1557 window->data_handler(window, input, time, x, y,
1558 input->drag_offer->types.data,
1559 window->user_data);
1560}
1561
1562static void
1563data_device_drop(void *data, struct wl_data_device *data_device)
1564{
1565 struct input *input = data;
1566 struct window *window = input->pointer_focus;
1567
1568 if (window->drop_handler)
1569 window->drop_handler(window, input,
1570 input->sx, input->sy, window->user_data);
1571}
1572
1573static void
1574data_device_selection(void *data,
1575 struct wl_data_device *wl_data_device,
1576 struct wl_data_offer *offer)
1577{
1578 struct input *input = data;
1579 char **p;
1580
1581 if (input->selection_offer)
1582 data_offer_destroy(input->selection_offer);
1583
1584 input->selection_offer = wl_data_offer_get_user_data(offer);
1585 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1586 *p = NULL;
1587}
1588
1589static const struct wl_data_device_listener data_device_listener = {
1590 data_device_data_offer,
1591 data_device_enter,
1592 data_device_leave,
1593 data_device_motion,
1594 data_device_drop,
1595 data_device_selection
1596};
1597
1598struct wl_data_device *
1599input_get_data_device(struct input *input)
1600{
1601 return input->data_device;
1602}
1603
1604void
1605input_set_selection(struct input *input,
1606 struct wl_data_source *source, uint32_t time)
1607{
1608 wl_data_device_set_selection(input->data_device, source, time);
1609}
1610
1611void
1612input_accept(struct input *input, uint32_t time, const char *type)
1613{
1614 wl_data_offer_accept(input->drag_offer->offer, time, type);
1615}
1616
1617static void
1618offer_io_func(struct task *task, uint32_t events)
1619{
1620 struct data_offer *offer =
1621 container_of(task, struct data_offer, io_task);
1622 unsigned int len;
1623 char buffer[4096];
1624
1625 len = read(offer->fd, buffer, sizeof buffer);
1626 offer->func(buffer, len,
1627 offer->x, offer->y, offer->user_data);
1628
1629 if (len == 0) {
1630 close(offer->fd);
1631 data_offer_destroy(offer);
1632 }
1633}
1634
1635static void
1636data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1637 data_func_t func, void *user_data)
1638{
1639 int p[2];
1640
1641 pipe2(p, O_CLOEXEC);
1642 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1643 close(p[1]);
1644
1645 offer->io_task.run = offer_io_func;
1646 offer->fd = p[0];
1647 offer->func = func;
1648 offer->refcount++;
1649 offer->user_data = user_data;
1650
1651 display_watch_fd(offer->input->display,
1652 offer->fd, EPOLLIN, &offer->io_task);
1653}
1654
1655void
1656input_receive_drag_data(struct input *input, const char *mime_type,
1657 data_func_t func, void *data)
1658{
1659 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1660 input->drag_offer->x = input->sx;
1661 input->drag_offer->y = input->sy;
1662}
1663
1664int
1665input_receive_selection_data(struct input *input, const char *mime_type,
1666 data_func_t func, void *data)
1667{
1668 char **p;
1669
1670 if (input->selection_offer == NULL)
1671 return -1;
1672
1673 for (p = input->selection_offer->types.data; *p; p++)
1674 if (strcmp(mime_type, *p) == 0)
1675 break;
1676
1677 if (*p == NULL)
1678 return -1;
1679
1680 data_offer_receive_data(input->selection_offer,
1681 mime_type, func, data);
1682 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001683}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001684
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001685void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001686window_move(struct window *window, struct input *input, uint32_t time)
1687{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001688 if (window->display->shell)
1689 wl_shell_move(window->display->shell,
1690 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001691}
1692
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001693static void
1694handle_configure(void *data, struct wl_shell *shell,
1695 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001696 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001697{
1698 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001699 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001700
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001701 /* FIXME: this is probably the wrong place to check for width
1702 * or height <= 0, but it prevents the compositor from crashing
1703 */
1704 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001705 return;
1706
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001707 window->resize_edges = edges;
1708
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001709 if (window->resize_handler) {
1710 child_width = width - 20 - window->margin * 2;
1711 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001712
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001713 (*window->resize_handler)(window,
1714 child_width, child_height,
1715 window->user_data);
1716 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001717 window->allocation.width = width;
1718 window->allocation.height = height;
1719
1720 if (window->redraw_handler)
1721 window_schedule_redraw(window);
1722 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001723}
1724
1725static const struct wl_shell_listener shell_listener = {
1726 handle_configure,
1727};
1728
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001729void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001730window_get_allocation(struct window *window,
1731 struct rectangle *allocation)
1732{
1733 *allocation = window->allocation;
1734}
1735
1736void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001737window_get_child_allocation(struct window *window,
1738 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001739{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001740 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001741 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001742 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001743 allocation->x = window->margin + 10;
1744 allocation->y = window->margin + 50;
1745 allocation->width =
1746 window->allocation.width - 20 - window->margin * 2;
1747 allocation->height =
1748 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001749 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001750}
1751
1752void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001753window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001754{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001755 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001756 window->allocation.x = 20 + window->margin;
1757 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001758 window->allocation.width = width + 20 + window->margin * 2;
1759 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001760 } else {
1761 window->allocation.x = 0;
1762 window->allocation.y = 0;
1763 window->allocation.width = width;
1764 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001765 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001766}
1767
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001768static void
1769idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001770{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001771 struct window *window =
1772 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001773
1774 window->redraw_handler(window, window->user_data);
1775 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001776}
1777
1778void
1779window_schedule_redraw(struct window *window)
1780{
1781 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001782 window->redraw_task.run = idle_redraw;
1783 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001784 window->redraw_scheduled = 1;
1785 }
1786}
1787
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001788void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001789window_set_custom(struct window *window)
1790{
1791 window->type = TYPE_CUSTOM;
1792}
1793
1794void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001795window_set_fullscreen(struct window *window, int fullscreen)
1796{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001797 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001798 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001799
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001800 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001801 return;
1802
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001803 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001804 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001805 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001806 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001807 width = output->allocation.width;
1808 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001809 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001810 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001811 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001812 width = window->saved_allocation.width - 20 - window->margin * 2;
1813 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001814 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001815 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001816
1817 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001818}
1819
1820void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001821window_set_decoration(struct window *window, int decoration)
1822{
1823 window->decoration = decoration;
1824}
1825
1826void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001827window_set_user_data(struct window *window, void *data)
1828{
1829 window->user_data = data;
1830}
1831
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001832void *
1833window_get_user_data(struct window *window)
1834{
1835 return window->user_data;
1836}
1837
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001838void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001839window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001840 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001841{
1842 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001843}
1844
1845void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001846window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001847 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001848{
1849 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001850}
1851
1852void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001853window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001854 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001855{
1856 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001857}
1858
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001859void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001860window_set_button_handler(struct window *window,
1861 window_button_handler_t handler)
1862{
1863 window->button_handler = handler;
1864}
1865
1866void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001867window_set_motion_handler(struct window *window,
1868 window_motion_handler_t handler)
1869{
1870 window->motion_handler = handler;
1871}
1872
1873void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001874window_set_enter_handler(struct window *window,
1875 window_enter_handler_t handler)
1876{
1877 window->enter_handler = handler;
1878}
1879
1880void
1881window_set_leave_handler(struct window *window,
1882 window_leave_handler_t handler)
1883{
1884 window->leave_handler = handler;
1885}
1886
1887void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001888window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001889 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001890{
1891 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001892}
1893
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001894void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001895window_set_item_focus_handler(struct window *window,
1896 window_item_focus_handler_t handler)
1897{
1898 window->item_focus_handler = handler;
1899}
1900
1901void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001902window_set_data_handler(struct window *window, window_data_handler_t handler)
1903{
1904 window->data_handler = handler;
1905}
1906
1907void
1908window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1909{
1910 window->drop_handler = handler;
1911}
1912
1913void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001914window_set_transparent(struct window *window, int transparent)
1915{
1916 window->transparent = transparent;
1917}
1918
1919void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001920window_set_title(struct window *window, const char *title)
1921{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001922 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001923 window->title = strdup(title);
1924}
1925
1926const char *
1927window_get_title(struct window *window)
1928{
1929 return window->title;
1930}
1931
1932void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001933display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1934 int32_t x, int32_t y, int32_t width, int32_t height)
1935{
1936 struct wl_buffer *buffer;
1937
1938 buffer = display_get_buffer_for_surface(display, cairo_surface);
1939
1940 wl_buffer_damage(buffer, x, y, width, height);
1941}
1942
1943void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001944window_damage(struct window *window, int32_t x, int32_t y,
1945 int32_t width, int32_t height)
1946{
1947 wl_surface_damage(window->surface, x, y, width, height);
1948}
1949
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001950static struct window *
1951window_create_internal(struct display *display, struct window *parent,
1952 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001953{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001954 struct window *window;
1955
1956 window = malloc(sizeof *window);
1957 if (window == NULL)
1958 return NULL;
1959
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001960 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001961 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001962 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001963 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001964 window->allocation.x = 0;
1965 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001966 window->allocation.width = width;
1967 window->allocation.height = height;
1968 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001969 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001970 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001971 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001972 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001973
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001974 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001975#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001976 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1977 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001978#else
1979 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1980#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001981 else
1982 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001983
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001984 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001985 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001986
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001987 return window;
1988}
1989
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001990struct window *
1991window_create(struct display *display, int32_t width, int32_t height)
1992{
1993 struct window *window;
1994
1995 window = window_create_internal(display, NULL, width, height);
1996 if (!window)
1997 return NULL;
1998
1999 return window;
2000}
2001
2002struct window *
2003window_create_transient(struct display *display, struct window *parent,
2004 int32_t x, int32_t y, int32_t width, int32_t height)
2005{
2006 struct window *window;
2007
2008 window = window_create_internal(parent->display,
2009 parent, width, height);
2010 if (!window)
2011 return NULL;
2012
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002013 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002014 window->x = x;
2015 window->y = y;
2016
2017 return window;
2018}
2019
2020void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002021window_set_buffer_type(struct window *window, enum window_buffer_type type)
2022{
2023 window->buffer_type = type;
2024}
2025
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002026
2027static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002028display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002029 struct wl_output *wl_output,
2030 int x, int y,
2031 int physical_width,
2032 int physical_height,
2033 int subpixel,
2034 const char *make,
2035 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002036{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002037 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002038
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002039 output->allocation.x = x;
2040 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002041}
2042
2043static void
2044display_handle_mode(void *data,
2045 struct wl_output *wl_output,
2046 uint32_t flags,
2047 int width,
2048 int height,
2049 int refresh)
2050{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002051 struct output *output = data;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002052
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002053 if (flags & WL_OUTPUT_MODE_CURRENT) {
2054 output->allocation.width = width;
2055 output->allocation.height = height;
2056 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002057}
2058
2059static const struct wl_output_listener output_listener = {
2060 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002061 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002062};
2063
2064static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002065display_add_output(struct display *d, uint32_t id)
2066{
2067 struct output *output;
2068
2069 output = malloc(sizeof *output);
2070 if (output == NULL)
2071 return;
2072
2073 memset(output, 0, sizeof *output);
2074 output->display = d;
2075 output->output =
2076 wl_display_bind(d->display, id, &wl_output_interface);
2077 wl_list_insert(d->output_list.prev, &output->link);
2078
2079 wl_output_add_listener(output->output, &output_listener, output);
2080}
2081
2082void
2083output_get_allocation(struct output *output, struct rectangle *allocation)
2084{
2085 *allocation = output->allocation;
2086}
2087
2088static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002089display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002090{
2091 struct input *input;
2092
2093 input = malloc(sizeof *input);
2094 if (input == NULL)
2095 return;
2096
2097 memset(input, 0, sizeof *input);
2098 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002099 input->input_device =
2100 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002101 input->pointer_focus = NULL;
2102 input->keyboard_focus = NULL;
2103 wl_list_insert(d->input_list.prev, &input->link);
2104
2105 wl_input_device_add_listener(input->input_device,
2106 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002107 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002108
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002109 input->data_device =
2110 wl_data_device_manager_get_data_device(d->data_device_manager,
2111 input->input_device);
2112 wl_data_device_add_listener(input->data_device,
2113 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002114}
2115
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002116static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002117display_handle_global(struct wl_display *display, uint32_t id,
2118 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002119{
2120 struct display *d = data;
2121
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002122 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002123 d->compositor =
2124 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002125 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002126 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002127 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002128 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002129 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002130 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002131 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002132 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002133 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002134 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2135 d->data_device_manager =
2136 wl_display_bind(display, id,
2137 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002138 }
2139}
2140
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002141static void
2142display_render_frame(struct display *d)
2143{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002144 int radius = 8;
2145 cairo_t *cr;
2146
2147 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2148 cr = cairo_create(d->shadow);
2149 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2150 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2151 rounded_rect(cr, 16, 16, 112, 112, radius);
2152 cairo_fill(cr);
2153 cairo_destroy(cr);
2154 blur_surface(d->shadow, 64);
2155
2156 d->active_frame =
2157 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2158 cr = cairo_create(d->active_frame);
2159 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2160 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2161 rounded_rect(cr, 16, 16, 112, 112, radius);
2162 cairo_fill(cr);
2163 cairo_destroy(cr);
2164
2165 d->inactive_frame =
2166 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2167 cr = cairo_create(d->inactive_frame);
2168 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2169 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2170 rounded_rect(cr, 16, 16, 112, 112, radius);
2171 cairo_fill(cr);
2172 cairo_destroy(cr);
2173}
2174
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002175static void
2176init_xkb(struct display *d)
2177{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002178 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002179
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002180 names.rules = "evdev";
2181 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002182 names.layout = option_xkb_layout;
2183 names.variant = option_xkb_variant;
2184 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002185
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002186 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002187 if (!d->xkb) {
2188 fprintf(stderr, "Failed to compile keymap\n");
2189 exit(1);
2190 }
2191}
2192
Yuval Fledel45568f62010-12-06 09:18:12 -05002193static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002194init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002195{
2196 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002197 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002198
2199 static const EGLint premul_argb_cfg_attribs[] = {
2200 EGL_SURFACE_TYPE,
2201 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2202 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002203 EGL_RED_SIZE, 1,
2204 EGL_GREEN_SIZE, 1,
2205 EGL_BLUE_SIZE, 1,
2206 EGL_ALPHA_SIZE, 1,
2207 EGL_DEPTH_SIZE, 1,
2208 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2209 EGL_NONE
2210 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002211
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002212 static const EGLint rgb_cfg_attribs[] = {
2213 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2214 EGL_RED_SIZE, 1,
2215 EGL_GREEN_SIZE, 1,
2216 EGL_BLUE_SIZE, 1,
2217 EGL_ALPHA_SIZE, 0,
2218 EGL_DEPTH_SIZE, 1,
2219 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2220 EGL_NONE
2221 };
2222
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002223 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002224 if (!eglInitialize(d->dpy, &major, &minor)) {
2225 fprintf(stderr, "failed to initialize display\n");
2226 return -1;
2227 }
2228
2229 if (!eglBindAPI(EGL_OPENGL_API)) {
2230 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2231 return -1;
2232 }
2233
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002234 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002235 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002236 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002237 return -1;
2238 }
2239
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002240 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2241 &d->rgb_config, 1, &n) || n != 1) {
2242 fprintf(stderr, "failed to choose rgb config\n");
2243 return -1;
2244 }
2245
Benjamin Franzke0c991632011-09-27 21:57:31 +02002246 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2247 if (d->rgb_ctx == NULL) {
2248 fprintf(stderr, "failed to create context\n");
2249 return -1;
2250 }
2251 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2252 EGL_NO_CONTEXT, NULL);
2253 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002254 fprintf(stderr, "failed to create context\n");
2255 return -1;
2256 }
2257
Benjamin Franzke0c991632011-09-27 21:57:31 +02002258 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002259 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002260 return -1;
2261 }
2262
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002263#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002264 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2265 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002266 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002267 return -1;
2268 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002269 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2270 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2271 fprintf(stderr, "failed to get cairo egl argb device\n");
2272 return -1;
2273 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002274#endif
2275
2276 return 0;
2277}
2278
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002279static int
2280event_mask_update(uint32_t mask, void *data)
2281{
2282 struct display *d = data;
2283
2284 d->mask = mask;
2285
2286 return 0;
2287}
2288
2289static void
2290handle_display_data(struct task *task, uint32_t events)
2291{
2292 struct display *display =
2293 container_of(task, struct display, display_task);
2294
2295 wl_display_iterate(display->display, display->mask);
2296}
2297
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002298struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002299display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002300{
2301 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002302 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002303 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002304 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002305
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002306 g_type_init();
2307
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002308 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002309 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002310 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002311
2312 xkb_option_group = g_option_group_new("xkb",
2313 "Keyboard options",
2314 "Show all XKB options",
2315 NULL, NULL);
2316 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2317 g_option_context_add_group (context, xkb_option_group);
2318
2319 if (!g_option_context_parse(context, argc, argv, &error)) {
2320 fprintf(stderr, "option parsing failed: %s\n", error->message);
2321 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002322 }
2323
Tim Wiederhake748f6722011-01-23 23:25:25 +01002324 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002325
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002326 d = malloc(sizeof *d);
2327 if (d == NULL)
2328 return NULL;
2329
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002330 memset(d, 0, sizeof *d);
2331
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002332 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002333 if (d->display == NULL) {
2334 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002335 return NULL;
2336 }
2337
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002338 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2339 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2340 d->display_task.run = handle_display_data;
2341 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2342
2343 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002344 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002345 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002346
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002347 /* Set up listener so we'll catch all events. */
2348 wl_display_add_global_listener(d->display,
2349 display_handle_global, d);
2350
2351 /* Process connection events. */
2352 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002353 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002354 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002355
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002356 d->image_target_texture_2d =
2357 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2358 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2359 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2360
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002361 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002362
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002363 display_render_frame(d);
2364
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002365 wl_list_init(&d->window_list);
2366
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002367 init_xkb(d);
2368
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002369 return d;
2370}
2371
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002372struct wl_display *
2373display_get_display(struct display *display)
2374{
2375 return display->display;
2376}
2377
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002378struct output *
2379display_get_output(struct display *display)
2380{
2381 return container_of(display->output_list.next, struct output, link);
2382}
2383
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002384struct wl_compositor *
2385display_get_compositor(struct display *display)
2386{
2387 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002388}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002389
2390EGLDisplay
2391display_get_egl_display(struct display *d)
2392{
2393 return d->dpy;
2394}
2395
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002396struct wl_data_source *
2397display_create_data_source(struct display *display)
2398{
2399 return wl_data_device_manager_create_data_source(display->data_device_manager);
2400}
2401
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002402EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002403display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002404{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002405 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002406}
2407
Benjamin Franzke0c991632011-09-27 21:57:31 +02002408EGLConfig
2409display_get_argb_egl_config(struct display *d)
2410{
2411 return d->premultiplied_argb_config;
2412}
2413
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002414struct wl_shell *
2415display_get_shell(struct display *display)
2416{
2417 return display->shell;
2418}
2419
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002420int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002421display_acquire_window_surface(struct display *display,
2422 struct window *window,
2423 EGLContext ctx)
2424{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002425#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002426 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002427 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002428
2429 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002430 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002431 device = cairo_surface_get_device(window->cairo_surface);
2432 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002433 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002434
Benjamin Franzke0c991632011-09-27 21:57:31 +02002435 if (!ctx) {
2436 if (device == display->rgb_device)
2437 ctx = display->rgb_ctx;
2438 else if (device == display->argb_device)
2439 ctx = display->argb_ctx;
2440 else
2441 assert(0);
2442 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002443
2444 data = cairo_surface_get_user_data(window->cairo_surface,
2445 &surface_data_key);
2446
Benjamin Franzke0c991632011-09-27 21:57:31 +02002447 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002448 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2449 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002450
2451 return 0;
2452#else
2453 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002454#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002455}
2456
2457void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002458display_release_window_surface(struct display *display,
2459 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002460{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002461#ifdef HAVE_CAIRO_EGL
2462 cairo_device_t *device;
2463
2464 device = cairo_surface_get_device(window->cairo_surface);
2465 if (!device)
2466 return;
2467
2468 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002469 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002470 cairo_device_release(device);
2471#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002472}
2473
2474void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002475display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002476{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002477 wl_list_insert(&display->deferred_list, &task->link);
2478}
2479
2480void
2481display_watch_fd(struct display *display,
2482 int fd, uint32_t events, struct task *task)
2483{
2484 struct epoll_event ep;
2485
2486 ep.events = events;
2487 ep.data.ptr = task;
2488 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2489}
2490
2491void
2492display_run(struct display *display)
2493{
2494 struct task *task;
2495 struct epoll_event ep[16];
2496 int i, count;
2497
2498 while (1) {
2499 while (display->mask & WL_DISPLAY_WRITABLE)
2500 wl_display_iterate(display->display,
2501 WL_DISPLAY_WRITABLE);
2502
2503 count = epoll_wait(display->epoll_fd,
2504 ep, ARRAY_LENGTH(ep), -1);
2505 for (i = 0; i < count; i++) {
2506 task = ep[i].data.ptr;
2507 task->run(task, ep[i].events);
2508 }
2509
2510 while (!wl_list_empty(&display->deferred_list)) {
2511 task = container_of(display->deferred_list.next,
2512 struct task, link);
2513 wl_list_remove(&task->link);
2514 task->run(task, 0);
2515 }
2516 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002517}