blob: 1c8b9adf3f57a9191927ebe5d90b28fb1d92d6c3 [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;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001317 struct display *d = window->display;
1318 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;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001321 if (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 window = input->keyboard_focus;
1398 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001399 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001400 if (window->keyboard_focus_handler)
1401 (*window->keyboard_focus_handler)(window, NULL,
1402 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001403 }
1404
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001405 if (surface)
1406 input->keyboard_focus = wl_surface_get_user_data(surface);
1407 else
1408 input->keyboard_focus = NULL;
1409
1410 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001411 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001412 for (k = keys->data; k < end; k++)
1413 input->modifiers |= d->xkb->map->modmap[*k];
1414
1415 window = input->keyboard_focus;
1416 if (window) {
1417 window->keyboard_device = input;
1418 if (window->keyboard_focus_handler)
1419 (*window->keyboard_focus_handler)(window,
1420 window->keyboard_device,
1421 window->user_data);
1422 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001423}
1424
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001425static const struct wl_input_device_listener input_device_listener = {
1426 window_handle_motion,
1427 window_handle_button,
1428 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001429 window_handle_pointer_focus,
1430 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001431};
1432
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001433void
1434input_get_position(struct input *input, int32_t *x, int32_t *y)
1435{
1436 *x = input->sx;
1437 *y = input->sy;
1438}
1439
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001440struct wl_input_device *
1441input_get_input_device(struct input *input)
1442{
1443 return input->input_device;
1444}
1445
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001446uint32_t
1447input_get_modifiers(struct input *input)
1448{
1449 return input->modifiers;
1450}
1451
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001452struct data_offer {
1453 struct wl_data_offer *offer;
1454 struct input *input;
1455 struct wl_array types;
1456 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001457
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001458 struct task io_task;
1459 int fd;
1460 data_func_t func;
1461 int32_t x, y;
1462 void *user_data;
1463};
1464
1465static void
1466data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1467{
1468 struct data_offer *offer = data;
1469 char **p;
1470
1471 p = wl_array_add(&offer->types, sizeof *p);
1472 *p = strdup(type);
1473}
1474
1475static const struct wl_data_offer_listener data_offer_listener = {
1476 data_offer_offer,
1477};
1478
1479static void
1480data_offer_destroy(struct data_offer *offer)
1481{
1482 char **p;
1483
1484 offer->refcount--;
1485 if (offer->refcount == 0) {
1486 wl_data_offer_destroy(offer->offer);
1487 for (p = offer->types.data; *p; p++)
1488 free(*p);
1489 wl_array_release(&offer->types);
1490 free(offer);
1491 }
1492}
1493
1494static void
1495data_device_data_offer(void *data,
1496 struct wl_data_device *data_device, uint32_t id)
1497{
1498 struct data_offer *offer;
1499
1500 offer = malloc(sizeof *offer);
1501
1502 wl_array_init(&offer->types);
1503 offer->refcount = 1;
1504 offer->input = data;
1505
1506 /* FIXME: Generate typesafe wrappers for this */
1507 offer->offer = (struct wl_data_offer *)
1508 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1509 id, &wl_data_offer_interface);
1510
1511 wl_data_offer_add_listener(offer->offer,
1512 &data_offer_listener, offer);
1513}
1514
1515static void
1516data_device_enter(void *data, struct wl_data_device *data_device,
1517 uint32_t time, struct wl_surface *surface,
1518 int32_t x, int32_t y, struct wl_data_offer *offer)
1519{
1520 struct input *input = data;
1521 struct window *window;
1522 char **p;
1523
1524 input->drag_offer = wl_data_offer_get_user_data(offer);
1525 window = wl_surface_get_user_data(surface);
1526 input->pointer_focus = window;
1527
1528 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1529 *p = NULL;
1530
1531 window = input->pointer_focus;
1532 if (window->data_handler)
1533 window->data_handler(window, input, time, x, y,
1534 input->drag_offer->types.data,
1535 window->user_data);
1536}
1537
1538static void
1539data_device_leave(void *data, struct wl_data_device *data_device)
1540{
1541 struct input *input = data;
1542
1543 data_offer_destroy(input->drag_offer);
1544 input->drag_offer = NULL;
1545}
1546
1547static void
1548data_device_motion(void *data, struct wl_data_device *data_device,
1549 uint32_t time, int32_t x, int32_t y)
1550{
1551 struct input *input = data;
1552 struct window *window = input->pointer_focus;
1553
1554 input->sx = x;
1555 input->sy = y;
1556
1557 if (window->data_handler)
1558 window->data_handler(window, input, time, x, y,
1559 input->drag_offer->types.data,
1560 window->user_data);
1561}
1562
1563static void
1564data_device_drop(void *data, struct wl_data_device *data_device)
1565{
1566 struct input *input = data;
1567 struct window *window = input->pointer_focus;
1568
1569 if (window->drop_handler)
1570 window->drop_handler(window, input,
1571 input->sx, input->sy, window->user_data);
1572}
1573
1574static void
1575data_device_selection(void *data,
1576 struct wl_data_device *wl_data_device,
1577 struct wl_data_offer *offer)
1578{
1579 struct input *input = data;
1580 char **p;
1581
1582 if (input->selection_offer)
1583 data_offer_destroy(input->selection_offer);
1584
1585 input->selection_offer = wl_data_offer_get_user_data(offer);
1586 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1587 *p = NULL;
1588}
1589
1590static const struct wl_data_device_listener data_device_listener = {
1591 data_device_data_offer,
1592 data_device_enter,
1593 data_device_leave,
1594 data_device_motion,
1595 data_device_drop,
1596 data_device_selection
1597};
1598
1599struct wl_data_device *
1600input_get_data_device(struct input *input)
1601{
1602 return input->data_device;
1603}
1604
1605void
1606input_set_selection(struct input *input,
1607 struct wl_data_source *source, uint32_t time)
1608{
1609 wl_data_device_set_selection(input->data_device, source, time);
1610}
1611
1612void
1613input_accept(struct input *input, uint32_t time, const char *type)
1614{
1615 wl_data_offer_accept(input->drag_offer->offer, time, type);
1616}
1617
1618static void
1619offer_io_func(struct task *task, uint32_t events)
1620{
1621 struct data_offer *offer =
1622 container_of(task, struct data_offer, io_task);
1623 unsigned int len;
1624 char buffer[4096];
1625
1626 len = read(offer->fd, buffer, sizeof buffer);
1627 offer->func(buffer, len,
1628 offer->x, offer->y, offer->user_data);
1629
1630 if (len == 0) {
1631 close(offer->fd);
1632 data_offer_destroy(offer);
1633 }
1634}
1635
1636static void
1637data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1638 data_func_t func, void *user_data)
1639{
1640 int p[2];
1641
1642 pipe2(p, O_CLOEXEC);
1643 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1644 close(p[1]);
1645
1646 offer->io_task.run = offer_io_func;
1647 offer->fd = p[0];
1648 offer->func = func;
1649 offer->refcount++;
1650 offer->user_data = user_data;
1651
1652 display_watch_fd(offer->input->display,
1653 offer->fd, EPOLLIN, &offer->io_task);
1654}
1655
1656void
1657input_receive_drag_data(struct input *input, const char *mime_type,
1658 data_func_t func, void *data)
1659{
1660 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1661 input->drag_offer->x = input->sx;
1662 input->drag_offer->y = input->sy;
1663}
1664
1665int
1666input_receive_selection_data(struct input *input, const char *mime_type,
1667 data_func_t func, void *data)
1668{
1669 char **p;
1670
1671 if (input->selection_offer == NULL)
1672 return -1;
1673
1674 for (p = input->selection_offer->types.data; *p; p++)
1675 if (strcmp(mime_type, *p) == 0)
1676 break;
1677
1678 if (*p == NULL)
1679 return -1;
1680
1681 data_offer_receive_data(input->selection_offer,
1682 mime_type, func, data);
1683 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001684}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001685
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001686void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001687window_move(struct window *window, struct input *input, uint32_t time)
1688{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001689 if (window->display->shell)
1690 wl_shell_move(window->display->shell,
1691 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001692}
1693
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001694static void
1695handle_configure(void *data, struct wl_shell *shell,
1696 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001697 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001698{
1699 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001700 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001701
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001702 /* FIXME: this is probably the wrong place to check for width
1703 * or height <= 0, but it prevents the compositor from crashing
1704 */
1705 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001706 return;
1707
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001708 window->resize_edges = edges;
1709
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001710 if (window->resize_handler) {
1711 child_width = width - 20 - window->margin * 2;
1712 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001713
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001714 (*window->resize_handler)(window,
1715 child_width, child_height,
1716 window->user_data);
1717 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001718 window->allocation.width = width;
1719 window->allocation.height = height;
1720
1721 if (window->redraw_handler)
1722 window_schedule_redraw(window);
1723 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001724}
1725
1726static const struct wl_shell_listener shell_listener = {
1727 handle_configure,
1728};
1729
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001730void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001731window_get_allocation(struct window *window,
1732 struct rectangle *allocation)
1733{
1734 *allocation = window->allocation;
1735}
1736
1737void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001738window_get_child_allocation(struct window *window,
1739 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001740{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001741 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001742 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001743 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001744 allocation->x = window->margin + 10;
1745 allocation->y = window->margin + 50;
1746 allocation->width =
1747 window->allocation.width - 20 - window->margin * 2;
1748 allocation->height =
1749 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001750 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001751}
1752
1753void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001754window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001755{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001756 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001757 window->allocation.x = 20 + window->margin;
1758 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001759 window->allocation.width = width + 20 + window->margin * 2;
1760 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001761 } else {
1762 window->allocation.x = 0;
1763 window->allocation.y = 0;
1764 window->allocation.width = width;
1765 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001766 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001767}
1768
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001769static void
1770idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001771{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001772 struct window *window =
1773 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001774
1775 window->redraw_handler(window, window->user_data);
1776 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001777}
1778
1779void
1780window_schedule_redraw(struct window *window)
1781{
1782 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001783 window->redraw_task.run = idle_redraw;
1784 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001785 window->redraw_scheduled = 1;
1786 }
1787}
1788
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001789void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001790window_set_custom(struct window *window)
1791{
1792 window->type = TYPE_CUSTOM;
1793}
1794
1795void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001796window_set_fullscreen(struct window *window, int fullscreen)
1797{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001798 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001799 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001800
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001801 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001802 return;
1803
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001804 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001805 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001806 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001807 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001808 width = output->allocation.width;
1809 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001810 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001811 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001812 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001813 width = window->saved_allocation.width - 20 - window->margin * 2;
1814 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001815 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001816 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001817
1818 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001819}
1820
1821void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001822window_set_decoration(struct window *window, int decoration)
1823{
1824 window->decoration = decoration;
1825}
1826
1827void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001828window_set_user_data(struct window *window, void *data)
1829{
1830 window->user_data = data;
1831}
1832
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001833void *
1834window_get_user_data(struct window *window)
1835{
1836 return window->user_data;
1837}
1838
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001839void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001840window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001841 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001842{
1843 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001844}
1845
1846void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001847window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001848 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001849{
1850 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001851}
1852
1853void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001854window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001855 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001856{
1857 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001858}
1859
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001860void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001861window_set_button_handler(struct window *window,
1862 window_button_handler_t handler)
1863{
1864 window->button_handler = handler;
1865}
1866
1867void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001868window_set_motion_handler(struct window *window,
1869 window_motion_handler_t handler)
1870{
1871 window->motion_handler = handler;
1872}
1873
1874void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001875window_set_enter_handler(struct window *window,
1876 window_enter_handler_t handler)
1877{
1878 window->enter_handler = handler;
1879}
1880
1881void
1882window_set_leave_handler(struct window *window,
1883 window_leave_handler_t handler)
1884{
1885 window->leave_handler = handler;
1886}
1887
1888void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001889window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001890 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001891{
1892 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001893}
1894
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001895void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001896window_set_item_focus_handler(struct window *window,
1897 window_item_focus_handler_t handler)
1898{
1899 window->item_focus_handler = handler;
1900}
1901
1902void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001903window_set_data_handler(struct window *window, window_data_handler_t handler)
1904{
1905 window->data_handler = handler;
1906}
1907
1908void
1909window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1910{
1911 window->drop_handler = handler;
1912}
1913
1914void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001915window_set_transparent(struct window *window, int transparent)
1916{
1917 window->transparent = transparent;
1918}
1919
1920void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001921window_set_title(struct window *window, const char *title)
1922{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001923 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001924 window->title = strdup(title);
1925}
1926
1927const char *
1928window_get_title(struct window *window)
1929{
1930 return window->title;
1931}
1932
1933void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001934display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1935 int32_t x, int32_t y, int32_t width, int32_t height)
1936{
1937 struct wl_buffer *buffer;
1938
1939 buffer = display_get_buffer_for_surface(display, cairo_surface);
1940
1941 wl_buffer_damage(buffer, x, y, width, height);
1942}
1943
1944void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001945window_damage(struct window *window, int32_t x, int32_t y,
1946 int32_t width, int32_t height)
1947{
1948 wl_surface_damage(window->surface, x, y, width, height);
1949}
1950
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001951static struct window *
1952window_create_internal(struct display *display, struct window *parent,
1953 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001954{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001955 struct window *window;
1956
1957 window = malloc(sizeof *window);
1958 if (window == NULL)
1959 return NULL;
1960
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001961 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001962 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001963 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001964 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001965 window->allocation.x = 0;
1966 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001967 window->allocation.width = width;
1968 window->allocation.height = height;
1969 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001970 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001971 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001972 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001973 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001974
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001975 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001976#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001977 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1978 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001979#else
1980 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1981#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001982 else
1983 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001984
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001985 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001986 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001987
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001988 return window;
1989}
1990
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001991struct window *
1992window_create(struct display *display, int32_t width, int32_t height)
1993{
1994 struct window *window;
1995
1996 window = window_create_internal(display, NULL, width, height);
1997 if (!window)
1998 return NULL;
1999
2000 return window;
2001}
2002
2003struct window *
2004window_create_transient(struct display *display, struct window *parent,
2005 int32_t x, int32_t y, int32_t width, int32_t height)
2006{
2007 struct window *window;
2008
2009 window = window_create_internal(parent->display,
2010 parent, width, height);
2011 if (!window)
2012 return NULL;
2013
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002014 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002015 window->x = x;
2016 window->y = y;
2017
2018 return window;
2019}
2020
2021void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002022window_set_buffer_type(struct window *window, enum window_buffer_type type)
2023{
2024 window->buffer_type = type;
2025}
2026
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002027
2028static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002029display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002030 struct wl_output *wl_output,
2031 int x, int y,
2032 int physical_width,
2033 int physical_height,
2034 int subpixel,
2035 const char *make,
2036 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002037{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002038 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002039
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002040 output->allocation.x = x;
2041 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002042}
2043
2044static void
2045display_handle_mode(void *data,
2046 struct wl_output *wl_output,
2047 uint32_t flags,
2048 int width,
2049 int height,
2050 int refresh)
2051{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002052 struct output *output = data;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002053
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002054 if (flags & WL_OUTPUT_MODE_CURRENT) {
2055 output->allocation.width = width;
2056 output->allocation.height = height;
2057 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002058}
2059
2060static const struct wl_output_listener output_listener = {
2061 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002062 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002063};
2064
2065static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002066display_add_output(struct display *d, uint32_t id)
2067{
2068 struct output *output;
2069
2070 output = malloc(sizeof *output);
2071 if (output == NULL)
2072 return;
2073
2074 memset(output, 0, sizeof *output);
2075 output->display = d;
2076 output->output =
2077 wl_display_bind(d->display, id, &wl_output_interface);
2078 wl_list_insert(d->output_list.prev, &output->link);
2079
2080 wl_output_add_listener(output->output, &output_listener, output);
2081}
2082
2083void
2084output_get_allocation(struct output *output, struct rectangle *allocation)
2085{
2086 *allocation = output->allocation;
2087}
2088
2089static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002090display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002091{
2092 struct input *input;
2093
2094 input = malloc(sizeof *input);
2095 if (input == NULL)
2096 return;
2097
2098 memset(input, 0, sizeof *input);
2099 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002100 input->input_device =
2101 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002102 input->pointer_focus = NULL;
2103 input->keyboard_focus = NULL;
2104 wl_list_insert(d->input_list.prev, &input->link);
2105
2106 wl_input_device_add_listener(input->input_device,
2107 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002108 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002109
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002110 input->data_device =
2111 wl_data_device_manager_get_data_device(d->data_device_manager,
2112 input->input_device);
2113 wl_data_device_add_listener(input->data_device,
2114 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002115}
2116
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002117static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002118display_handle_global(struct wl_display *display, uint32_t id,
2119 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002120{
2121 struct display *d = data;
2122
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002123 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002124 d->compositor =
2125 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002126 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002127 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002128 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002129 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002130 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002131 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002132 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002133 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002134 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002135 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2136 d->data_device_manager =
2137 wl_display_bind(display, id,
2138 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002139 }
2140}
2141
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002142static void
2143display_render_frame(struct display *d)
2144{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002145 int radius = 8;
2146 cairo_t *cr;
2147
2148 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2149 cr = cairo_create(d->shadow);
2150 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2151 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2152 rounded_rect(cr, 16, 16, 112, 112, radius);
2153 cairo_fill(cr);
2154 cairo_destroy(cr);
2155 blur_surface(d->shadow, 64);
2156
2157 d->active_frame =
2158 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2159 cr = cairo_create(d->active_frame);
2160 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2161 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2162 rounded_rect(cr, 16, 16, 112, 112, radius);
2163 cairo_fill(cr);
2164 cairo_destroy(cr);
2165
2166 d->inactive_frame =
2167 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2168 cr = cairo_create(d->inactive_frame);
2169 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2170 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2171 rounded_rect(cr, 16, 16, 112, 112, radius);
2172 cairo_fill(cr);
2173 cairo_destroy(cr);
2174}
2175
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002176static void
2177init_xkb(struct display *d)
2178{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002179 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002180
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002181 names.rules = "evdev";
2182 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002183 names.layout = option_xkb_layout;
2184 names.variant = option_xkb_variant;
2185 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002186
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002187 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002188 if (!d->xkb) {
2189 fprintf(stderr, "Failed to compile keymap\n");
2190 exit(1);
2191 }
2192}
2193
Yuval Fledel45568f62010-12-06 09:18:12 -05002194static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002195init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002196{
2197 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002198 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002199
2200 static const EGLint premul_argb_cfg_attribs[] = {
2201 EGL_SURFACE_TYPE,
2202 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2203 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002204 EGL_RED_SIZE, 1,
2205 EGL_GREEN_SIZE, 1,
2206 EGL_BLUE_SIZE, 1,
2207 EGL_ALPHA_SIZE, 1,
2208 EGL_DEPTH_SIZE, 1,
2209 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2210 EGL_NONE
2211 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002212
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002213 static const EGLint rgb_cfg_attribs[] = {
2214 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2215 EGL_RED_SIZE, 1,
2216 EGL_GREEN_SIZE, 1,
2217 EGL_BLUE_SIZE, 1,
2218 EGL_ALPHA_SIZE, 0,
2219 EGL_DEPTH_SIZE, 1,
2220 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2221 EGL_NONE
2222 };
2223
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002224 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002225 if (!eglInitialize(d->dpy, &major, &minor)) {
2226 fprintf(stderr, "failed to initialize display\n");
2227 return -1;
2228 }
2229
2230 if (!eglBindAPI(EGL_OPENGL_API)) {
2231 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2232 return -1;
2233 }
2234
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002235 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002236 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002237 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002238 return -1;
2239 }
2240
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002241 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2242 &d->rgb_config, 1, &n) || n != 1) {
2243 fprintf(stderr, "failed to choose rgb config\n");
2244 return -1;
2245 }
2246
Benjamin Franzke0c991632011-09-27 21:57:31 +02002247 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2248 if (d->rgb_ctx == NULL) {
2249 fprintf(stderr, "failed to create context\n");
2250 return -1;
2251 }
2252 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2253 EGL_NO_CONTEXT, NULL);
2254 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002255 fprintf(stderr, "failed to create context\n");
2256 return -1;
2257 }
2258
Benjamin Franzke0c991632011-09-27 21:57:31 +02002259 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002260 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002261 return -1;
2262 }
2263
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002264#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002265 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2266 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002267 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002268 return -1;
2269 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002270 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2271 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2272 fprintf(stderr, "failed to get cairo egl argb device\n");
2273 return -1;
2274 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002275#endif
2276
2277 return 0;
2278}
2279
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002280static int
2281event_mask_update(uint32_t mask, void *data)
2282{
2283 struct display *d = data;
2284
2285 d->mask = mask;
2286
2287 return 0;
2288}
2289
2290static void
2291handle_display_data(struct task *task, uint32_t events)
2292{
2293 struct display *display =
2294 container_of(task, struct display, display_task);
2295
2296 wl_display_iterate(display->display, display->mask);
2297}
2298
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002299struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002300display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002301{
2302 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002303 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002304 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002305 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002306
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002307 g_type_init();
2308
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002309 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002310 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002311 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002312
2313 xkb_option_group = g_option_group_new("xkb",
2314 "Keyboard options",
2315 "Show all XKB options",
2316 NULL, NULL);
2317 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2318 g_option_context_add_group (context, xkb_option_group);
2319
2320 if (!g_option_context_parse(context, argc, argv, &error)) {
2321 fprintf(stderr, "option parsing failed: %s\n", error->message);
2322 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002323 }
2324
Tim Wiederhake748f6722011-01-23 23:25:25 +01002325 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002326
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002327 d = malloc(sizeof *d);
2328 if (d == NULL)
2329 return NULL;
2330
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002331 memset(d, 0, sizeof *d);
2332
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002333 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002334 if (d->display == NULL) {
2335 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002336 return NULL;
2337 }
2338
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002339 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2340 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2341 d->display_task.run = handle_display_data;
2342 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2343
2344 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002345 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002346 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002347
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002348 /* Set up listener so we'll catch all events. */
2349 wl_display_add_global_listener(d->display,
2350 display_handle_global, d);
2351
2352 /* Process connection events. */
2353 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002354 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002355 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002356
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002357 d->image_target_texture_2d =
2358 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2359 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2360 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2361
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002362 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002363
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002364 display_render_frame(d);
2365
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002366 wl_list_init(&d->window_list);
2367
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002368 init_xkb(d);
2369
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002370 return d;
2371}
2372
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002373struct wl_display *
2374display_get_display(struct display *display)
2375{
2376 return display->display;
2377}
2378
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002379struct output *
2380display_get_output(struct display *display)
2381{
2382 return container_of(display->output_list.next, struct output, link);
2383}
2384
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002385struct wl_compositor *
2386display_get_compositor(struct display *display)
2387{
2388 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002389}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002390
2391EGLDisplay
2392display_get_egl_display(struct display *d)
2393{
2394 return d->dpy;
2395}
2396
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002397struct wl_data_source *
2398display_create_data_source(struct display *display)
2399{
2400 return wl_data_device_manager_create_data_source(display->data_device_manager);
2401}
2402
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002403EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002404display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002405{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002406 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002407}
2408
Benjamin Franzke0c991632011-09-27 21:57:31 +02002409EGLConfig
2410display_get_argb_egl_config(struct display *d)
2411{
2412 return d->premultiplied_argb_config;
2413}
2414
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002415struct wl_shell *
2416display_get_shell(struct display *display)
2417{
2418 return display->shell;
2419}
2420
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002421int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002422display_acquire_window_surface(struct display *display,
2423 struct window *window,
2424 EGLContext ctx)
2425{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002426#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002427 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002428 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002429
2430 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002431 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002432 device = cairo_surface_get_device(window->cairo_surface);
2433 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002434 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002435
Benjamin Franzke0c991632011-09-27 21:57:31 +02002436 if (!ctx) {
2437 if (device == display->rgb_device)
2438 ctx = display->rgb_ctx;
2439 else if (device == display->argb_device)
2440 ctx = display->argb_ctx;
2441 else
2442 assert(0);
2443 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002444
2445 data = cairo_surface_get_user_data(window->cairo_surface,
2446 &surface_data_key);
2447
Benjamin Franzke0c991632011-09-27 21:57:31 +02002448 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002449 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2450 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002451
2452 return 0;
2453#else
2454 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002455#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002456}
2457
2458void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002459display_release_window_surface(struct display *display,
2460 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002461{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002462#ifdef HAVE_CAIRO_EGL
2463 cairo_device_t *device;
2464
2465 device = cairo_surface_get_device(window->cairo_surface);
2466 if (!device)
2467 return;
2468
2469 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002470 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002471 cairo_device_release(device);
2472#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002473}
2474
2475void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002476display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002477{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002478 wl_list_insert(&display->deferred_list, &task->link);
2479}
2480
2481void
2482display_watch_fd(struct display *display,
2483 int fd, uint32_t events, struct task *task)
2484{
2485 struct epoll_event ep;
2486
2487 ep.events = events;
2488 ep.data.ptr = task;
2489 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2490}
2491
2492void
2493display_run(struct display *display)
2494{
2495 struct task *task;
2496 struct epoll_event ep[16];
2497 int i, count;
2498
2499 while (1) {
2500 while (display->mask & WL_DISPLAY_WRITABLE)
2501 wl_display_iterate(display->display,
2502 WL_DISPLAY_WRITABLE);
2503
2504 count = epoll_wait(display->epoll_fd,
2505 ep, ARRAY_LENGTH(ep), -1);
2506 for (i = 0; i < count; i++) {
2507 task = ep[i].data.ptr;
2508 task->run(task, ep[i].events);
2509 }
2510
2511 while (!wl_list_empty(&display->deferred_list)) {
2512 task = container_of(display->deferred_list.next,
2513 struct task, link);
2514 wl_list_remove(&task->link);
2515 task->run(task, 0);
2516 }
2517 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002518}