blob: 32c546d7fad627cf7f46dba4bc74c46c93776eaf [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øgsberg43c28ee2009-01-26 23:42:46 -050067 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040069 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020070 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020071 EGLContext rgb_ctx;
72 EGLContext argb_ctx;
73 cairo_device_t *rgb_device;
74 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040075
76 int display_fd;
77 uint32_t mask;
78 struct task display_task;
79
80 int epoll_fd;
81 struct wl_list deferred_list;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_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øgsberg7d804062010-09-07 21:50:06 -0400165enum {
166 POINTER_DEFAULT = 100,
167 POINTER_UNSET
168};
169
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500170enum window_location {
171 WINDOW_INTERIOR = 0,
172 WINDOW_RESIZING_TOP = 1,
173 WINDOW_RESIZING_BOTTOM = 2,
174 WINDOW_RESIZING_LEFT = 4,
175 WINDOW_RESIZING_TOP_LEFT = 5,
176 WINDOW_RESIZING_BOTTOM_LEFT = 6,
177 WINDOW_RESIZING_RIGHT = 8,
178 WINDOW_RESIZING_TOP_RIGHT = 9,
179 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
180 WINDOW_RESIZING_MASK = 15,
181 WINDOW_EXTERIOR = 16,
182 WINDOW_TITLEBAR = 17,
183 WINDOW_CLIENT_AREA = 18,
184};
185
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400186const char *option_xkb_layout = "us";
187const char *option_xkb_variant = "";
188const char *option_xkb_options = "";
189
190static const GOptionEntry xkb_option_entries[] = {
191 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
192 &option_xkb_layout, "XKB Layout" },
193 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
194 &option_xkb_variant, "XKB Variant" },
195 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
196 &option_xkb_options, "XKB Options" },
197 { NULL }
198};
199
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400200static const cairo_user_data_key_t surface_data_key;
201struct surface_data {
202 struct wl_buffer *buffer;
203};
204
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500205#define MULT(_d,c,a,t) \
206 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
207
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500208#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400209
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100210struct egl_window_surface_data {
211 struct display *display;
212 struct wl_surface *surface;
213 struct wl_egl_window *window;
214 EGLSurface surf;
215};
216
217static void
218egl_window_surface_data_destroy(void *p)
219{
220 struct egl_window_surface_data *data = p;
221 struct display *d = data->display;
222
223 eglDestroySurface(d->dpy, data->surf);
224 wl_egl_window_destroy(data->window);
225 data->surface = NULL;
226
227 free(p);
228}
229
230static cairo_surface_t *
231display_create_egl_window_surface(struct display *display,
232 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400233 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100234 struct rectangle *rectangle)
235{
236 cairo_surface_t *cairo_surface;
237 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400238 EGLConfig config;
239 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200240 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100241
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400242 static const EGLint premul_attribs[] = {
243 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
244 EGL_NONE
245 };
246
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100247 data = malloc(sizeof *data);
248 if (data == NULL)
249 return NULL;
250
251 data->display = display;
252 data->surface = surface;
253
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400254 if (flags & SURFACE_OPAQUE) {
255 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200256 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200257 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400258 } else {
259 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200260 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400261 attribs = premul_attribs;
262 }
263
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400264 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100265 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400266 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100267
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 data->surf = eglCreateWindowSurface(display->dpy, config,
269 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100270
Benjamin Franzke0c991632011-09-27 21:57:31 +0200271 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100272 data->surf,
273 rectangle->width,
274 rectangle->height);
275
276 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
277 data, egl_window_surface_data_destroy);
278
279 return cairo_surface;
280}
281
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500282struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200284 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400285 EGLImageKHR image;
286 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800287 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500288 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400289};
290
291static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500292egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400293{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500294 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800295 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400296
Benjamin Franzke0c991632011-09-27 21:57:31 +0200297 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400298 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200299 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800300
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500301 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400302 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500303 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500304 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400305}
306
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500307EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500308display_get_image_for_egl_image_surface(struct display *display,
309 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500310{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500311 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500312
313 data = cairo_surface_get_user_data (surface, &surface_data_key);
314
315 return data->image;
316}
317
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500318static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400320 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324 EGLDisplay dpy = display->dpy;
325 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327
328 data = malloc(sizeof *data);
329 if (data == NULL)
330 return NULL;
331
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800332 data->display = display;
333
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400334 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400335 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000337 free(data);
338 return NULL;
339 }
340
Benjamin Franzke0c991632011-09-27 21:57:31 +0200341 if (flags & SURFACE_OPAQUE) {
342 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200343 content = CAIRO_CONTENT_COLOR;
344 } else {
345 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200346 content = CAIRO_CONTENT_COLOR_ALPHA;
347 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400348
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500349 data->image = display->create_image(dpy, NULL,
350 EGL_NATIVE_PIXMAP_KHR,
351 (EGLClientBuffer) data->pixmap,
352 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500354 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500355 free(data);
356 return NULL;
357 }
358
359 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400360 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500361
Benjamin Franzke0c991632011-09-27 21:57:31 +0200362 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400363 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400364 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500365 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200366 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400367
Benjamin Franzke0c991632011-09-27 21:57:31 +0200368 surface = cairo_gl_surface_create_for_texture(data->device,
369 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400370 data->texture,
371 rectangle->width,
372 rectangle->height);
373
374 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500375 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400376
377 return surface;
378}
379
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500380static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381display_create_egl_image_surface_from_file(struct display *display,
382 const char *filename,
383 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400384{
385 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400386 GdkPixbuf *pixbuf;
387 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400388 int stride, i;
389 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400391
392 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400394 FALSE, &error);
395 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400397
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400398 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
399 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500400 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400402 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400403
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400404
405 stride = gdk_pixbuf_get_rowstride(pixbuf);
406 pixels = gdk_pixbuf_get_pixels(pixbuf);
407
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400409 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400411 while (p < end) {
412 unsigned int t;
413
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400414 MULT(p[0], p[0], p[3], t);
415 MULT(p[1], p[1], p[3], t);
416 MULT(p[2], p[2], p[3], t);
417 p += 4;
418
419 }
420 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200422 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000423 if (surface == NULL) {
424 g_object_unref(pixbuf);
425 return NULL;
426 }
427
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800428 data = cairo_surface_get_user_data(surface, &surface_data_key);
429
Benjamin Franzke0c991632011-09-27 21:57:31 +0200430 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800431 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800432 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
433 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200434 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400435
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500436 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400437
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 return surface;
439}
440
441#endif
442
443struct wl_buffer *
444display_get_buffer_for_surface(struct display *display,
445 cairo_surface_t *surface)
446{
447 struct surface_data *data;
448
449 data = cairo_surface_get_user_data (surface, &surface_data_key);
450
451 return data->buffer;
452}
453
454struct shm_surface_data {
455 struct surface_data data;
456 void *map;
457 size_t length;
458};
459
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500460static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461shm_surface_data_destroy(void *p)
462{
463 struct shm_surface_data *data = p;
464
465 wl_buffer_destroy(data->data.buffer);
466 munmap(data->map, data->length);
467}
468
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500469static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400470display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400471 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472{
473 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400474 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400475 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800476 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477 char filename[] = "/tmp/wayland-shm-XXXXXX";
478
479 data = malloc(sizeof *data);
480 if (data == NULL)
481 return NULL;
482
483 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
484 rectangle->width);
485 data->length = stride * rectangle->height;
486 fd = mkstemp(filename);
487 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000488 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489 return NULL;
490 }
491 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000492 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400493 close(fd);
494 return NULL;
495 }
496
497 data->map = mmap(NULL, data->length,
498 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
499 unlink(filename);
500
501 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000502 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503 close(fd);
504 return NULL;
505 }
506
507 surface = cairo_image_surface_create_for_data (data->map,
508 CAIRO_FORMAT_ARGB32,
509 rectangle->width,
510 rectangle->height,
511 stride);
512
513 cairo_surface_set_user_data (surface, &surface_data_key,
514 data, shm_surface_data_destroy);
515
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400516 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400517 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400518 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400519 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521 data->data.buffer = wl_shm_create_buffer(display->shm,
522 fd,
523 rectangle->width,
524 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400525 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526
527 close(fd);
528
529 return surface;
530}
531
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500532static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533display_create_shm_surface_from_file(struct display *display,
534 const char *filename,
535 struct rectangle *rect)
536{
537 cairo_surface_t *surface;
538 GdkPixbuf *pixbuf;
539 GError *error = NULL;
540 int stride, i;
541 unsigned char *pixels, *p, *end, *dest_data;
542 int dest_stride;
543 uint32_t *d;
544
545 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
546 rect->width, rect->height,
547 FALSE, &error);
548 if (error != NULL)
549 return NULL;
550
551 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
552 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500553 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554 return NULL;
555 }
556
557 stride = gdk_pixbuf_get_rowstride(pixbuf);
558 pixels = gdk_pixbuf_get_pixels(pixbuf);
559
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400560 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000561 if (surface == NULL) {
562 g_object_unref(pixbuf);
563 return NULL;
564 }
565
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566 dest_data = cairo_image_surface_get_data (surface);
567 dest_stride = cairo_image_surface_get_stride (surface);
568
569 for (i = 0; i < rect->height; i++) {
570 d = (uint32_t *) (dest_data + i * dest_stride);
571 p = pixels + i * stride;
572 end = p + rect->width * 4;
573 while (p < end) {
574 unsigned int t;
575 unsigned char a, r, g, b;
576
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400577 a = p[3];
578 MULT(r, p[0], a, t);
579 MULT(g, p[1], a, t);
580 MULT(b, p[2], a, t);
581 p += 4;
582 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
583 }
584 }
585
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500586 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587
588 return surface;
589}
590
nobled7b87cb02011-02-01 18:51:47 +0000591static int
592check_size(struct rectangle *rect)
593{
594 if (rect->width && rect->height)
595 return 0;
596
597 fprintf(stderr, "tried to create surface of "
598 "width: %d, height: %d\n", rect->width, rect->height);
599 return -1;
600}
601
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602cairo_surface_t *
603display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400605 struct rectangle *rectangle,
606 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607{
nobled7b87cb02011-02-01 18:51:47 +0000608 if (check_size(rectangle) < 0)
609 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500610#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500611 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100612 if (surface)
613 return display_create_egl_window_surface(display,
614 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400615 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100616 rectangle);
617 else
618 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400619 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500621 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400623 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624}
625
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500626static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627display_create_surface_from_file(struct display *display,
628 const char *filename,
629 struct rectangle *rectangle)
630{
nobled7b87cb02011-02-01 18:51:47 +0000631 if (check_size(rectangle) < 0)
632 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500633#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500634 if (display->dpy) {
635 return display_create_egl_image_surface_from_file(display,
636 filename,
637 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500638 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500640 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400641}
Yuval Fledel45568f62010-12-06 09:18:12 -0500642 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400643 const char *filename;
644 int hotspot_x, hotspot_y;
645} pointer_images[] = {
646 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
647 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
648 { DATADIR "/wayland/bottom_side.png", 16, 20 },
649 { DATADIR "/wayland/grabbing.png", 20, 17 },
650 { DATADIR "/wayland/left_ptr.png", 10, 5 },
651 { DATADIR "/wayland/left_side.png", 10, 20 },
652 { DATADIR "/wayland/right_side.png", 30, 19 },
653 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
654 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
655 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400656 { DATADIR "/wayland/xterm.png", 15, 15 },
657 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658};
659
660static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400661create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400662{
663 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400665 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400666
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400667 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400668 display->pointer_surfaces =
669 malloc(count * sizeof *display->pointer_surfaces);
670 rect.width = width;
671 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400673 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674 display_create_surface_from_file(display,
675 pointer_images[i].filename,
676 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100677 if (!display->pointer_surfaces[i]) {
678 fprintf(stderr, "Error loading pointer image: %s\n",
679 pointer_images[i].filename);
680 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400681 }
682
683}
684
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400685cairo_surface_t *
686display_get_pointer_surface(struct display *display, int pointer,
687 int *width, int *height,
688 int *hotspot_x, int *hotspot_y)
689{
690 cairo_surface_t *surface;
691
692 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500693#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400694 *width = cairo_gl_surface_get_width(surface);
695 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000696#else
697 *width = cairo_image_surface_get_width(surface);
698 *height = cairo_image_surface_get_height(surface);
699#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 *hotspot_x = pointer_images[pointer].hotspot_x;
701 *hotspot_y = pointer_images[pointer].hotspot_y;
702
703 return cairo_surface_reference(surface);
704}
705
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400706static void
707window_attach_surface(struct window *window);
708
709static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400710free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400711{
712 struct window *window = data;
713
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400714 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400715 cairo_surface_destroy(window->pending_surface);
716 window->pending_surface = NULL;
717 if (window->cairo_surface)
718 window_attach_surface(window);
719}
720
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400721static const struct wl_callback_listener free_surface_listener = {
722 free_surface
723};
724
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500725static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200726window_get_resize_dx_dy(struct window *window, int *x, int *y)
727{
728 if (window->resize_edges & WINDOW_RESIZING_LEFT)
729 *x = window->server_allocation.width - window->allocation.width;
730 else
731 *x = 0;
732
733 if (window->resize_edges & WINDOW_RESIZING_TOP)
734 *y = window->server_allocation.height -
735 window->allocation.height;
736 else
737 *y = 0;
738
739 window->resize_edges = 0;
740}
741
742static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400743window_set_type(struct window *window)
744{
745 struct display *display = window->display;
746
747 switch (window->type) {
748 case TYPE_FULLSCREEN:
749 wl_shell_set_fullscreen(display->shell, window->surface);
750 break;
751 case TYPE_TOPLEVEL:
752 wl_shell_set_toplevel(display->shell, window->surface);
753 break;
754 case TYPE_TRANSIENT:
755 wl_shell_set_transient(display->shell, window->surface,
756 window->parent->surface,
757 window->x, window->y, 0);
758 break;
759 case TYPE_CUSTOM:
760 break;
761 }
762}
763
764static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500765window_attach_surface(struct window *window)
766{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400767 struct display *display = window->display;
768 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400769 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000770#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100771 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000772#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500773 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100774
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400775 if (display->shell)
776 window_set_type(window);
777
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000779#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
781 data = cairo_surface_get_user_data(window->cairo_surface,
782 &surface_data_key);
783
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 cairo_gl_surface_swapbuffers(window->cairo_surface);
785 wl_egl_window_get_attached_size(data->window,
786 &window->server_allocation.width,
787 &window->server_allocation.height);
788 break;
789 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000790#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200792 window_get_resize_dx_dy(window, &x, &y);
793
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 if (window->pending_surface != NULL)
795 return;
796
797 window->pending_surface = window->cairo_surface;
798 window->cairo_surface = NULL;
799
800 buffer =
801 display_get_buffer_for_surface(display,
802 window->pending_surface);
803
804 wl_surface_attach(window->surface, buffer, x, y);
805 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400806 cb = wl_display_sync(display->display);
807 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100808 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000809 default:
810 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500812
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500813 wl_surface_damage(window->surface, 0, 0,
814 window->allocation.width,
815 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500816}
817
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500818void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400819window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500820{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100821 if (window->cairo_surface) {
822 switch (window->buffer_type) {
823 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
824 case WINDOW_BUFFER_TYPE_SHM:
825 display_surface_damage(window->display,
826 window->cairo_surface,
827 0, 0,
828 window->allocation.width,
829 window->allocation.height);
830 break;
831 default:
832 break;
833 }
834 window_attach_surface(window);
835 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500836}
837
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400838void
839window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500841 cairo_surface_reference(surface);
842
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400843 if (window->cairo_surface != NULL)
844 cairo_surface_destroy(window->cairo_surface);
845
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500846 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400847}
848
Benjamin Franzke22d54812011-07-16 19:50:32 +0000849#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850static void
851window_resize_cairo_window_surface(struct window *window)
852{
853 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200854 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855
856 data = cairo_surface_get_user_data(window->cairo_surface,
857 &surface_data_key);
858
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200859 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 wl_egl_window_resize(data->window,
861 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200862 window->allocation.height,
863 x,y);
864
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100865 cairo_gl_surface_set_size(window->cairo_surface,
866 window->allocation.width,
867 window->allocation.height);
868}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000869#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100870
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400871struct display *
872window_get_display(struct window *window)
873{
874 return window->display;
875}
876
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400877void
878window_create_surface(struct window *window)
879{
880 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400881 uint32_t flags = 0;
882
883 if (!window->transparent)
884 flags = SURFACE_OPAQUE;
885
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400886 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500887#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100888 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
889 if (window->cairo_surface) {
890 window_resize_cairo_window_surface(window);
891 return;
892 }
893 surface = display_create_surface(window->display,
894 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400895 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500897 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400898 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400900 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400901 break;
902#endif
903 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400904 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400905 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400906 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800907 default:
908 surface = NULL;
909 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400910 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400911
912 window_set_surface(window, surface);
913 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914}
915
916static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500917window_draw_menu(struct window *window)
918{
919 cairo_t *cr;
920 int width, height, r = 5;
921
922 window_create_surface(window);
923
924 cr = cairo_create(window->cairo_surface);
925 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
926 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
927 cairo_paint(cr);
928
929 width = window->allocation.width;
930 height = window->allocation.height;
931 rounded_rect(cr, r, r, width - r, height - r, r);
932 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
933 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
934 cairo_fill(cr);
935 cairo_destroy(cr);
936}
937
938static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500939window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500940{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500941 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500942 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400943 cairo_surface_t *frame;
944 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500945
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400946 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400948 width = window->allocation.width;
949 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500950
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500951 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500952
Kristian Høgsberg09531622010-06-14 23:22:15 -0400953 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400954 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400955 cairo_paint(cr);
956
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400957 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400958 tile_mask(cr, window->display->shadow,
959 shadow_dx, shadow_dy, width, height,
960 window->margin + 10 - shadow_dx,
961 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500962
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400963 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400964 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400965 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400966 frame = window->display->inactive_frame;
967
968 tile_source(cr, frame, 0, 0, width, height,
969 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500970
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500971 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
972 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500973 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400974 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400975 if (window->keyboard_device)
976 cairo_set_source_rgb(cr, 0, 0, 0);
977 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400978 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400979 cairo_show_text(cr, window->title);
980
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500981 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400982
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100983 /* FIXME: this breakes gears, fix cairo? */
984#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400985 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100986#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500987}
988
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400989void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500990window_destroy(struct window *window)
991{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200992 struct display *display = window->display;
993 struct input *input;
994
995 if (window->redraw_scheduled)
996 wl_list_remove(&window->redraw_task.link);
997
998 wl_list_for_each(input, &display->input_list, link) {
999 if (input->pointer_focus == window)
1000 input->pointer_focus = NULL;
1001 if (input->keyboard_focus == window)
1002 input->keyboard_focus = NULL;
1003 }
1004
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001005 wl_surface_destroy(window->surface);
1006 wl_list_remove(&window->link);
1007 free(window);
1008}
1009
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010static struct item *
1011window_find_item(struct window *window, int32_t x, int32_t y)
1012{
1013 struct item *item;
1014
1015 wl_list_for_each(item, &window->item_list, link) {
1016 if (item->allocation.x <= x &&
1017 x < item->allocation.x + item->allocation.width &&
1018 item->allocation.y <= y &&
1019 y < item->allocation.y + item->allocation.height) {
1020 return item;
1021 }
1022 }
1023
1024 return NULL;
1025}
1026
1027struct item *
1028window_add_item(struct window *window, void *data)
1029{
1030 struct item *item;
1031
1032 item = malloc(sizeof *item);
1033 memset(item, 0, sizeof *item);
1034 item->user_data = data;
1035 wl_list_insert(window->item_list.prev, &item->link);
1036
1037 return item;
1038}
1039
1040void
1041window_for_each_item(struct window *window, item_func_t func, void *data)
1042{
1043 struct item *item;
1044
1045 wl_list_for_each(item, &window->item_list, link)
1046 func(item, data);
1047}
1048
1049struct item *
1050window_get_focus_item(struct window *window)
1051{
1052 return window->focus_item;
1053}
1054
1055void
1056item_get_allocation(struct item *item, struct rectangle *allocation)
1057{
1058 *allocation = item->allocation;
1059}
1060
1061void
1062item_set_allocation(struct item *item,
1063 int32_t x, int32_t y, int32_t width, int32_t height)
1064{
1065 item->allocation.x = x;
1066 item->allocation.y = y;
1067 item->allocation.width = width;
1068 item->allocation.height = height;
1069}
1070
1071void *
1072item_get_user_data(struct item *item)
1073{
1074 return item->user_data;
1075}
1076
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001077void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001078window_draw(struct window *window)
1079{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001080 if (window->parent)
1081 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001082 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001083 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001084 else
1085 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001086}
1087
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001088cairo_surface_t *
1089window_get_surface(struct window *window)
1090{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001091 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001092}
1093
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001094struct wl_surface *
1095window_get_wl_surface(struct window *window)
1096{
1097 return window->surface;
1098}
1099
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001100static int
1101get_pointer_location(struct window *window, int32_t x, int32_t y)
1102{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104 const int grip_size = 8;
1105
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001106 if (!window->decoration)
1107 return WINDOW_CLIENT_AREA;
1108
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111 else if (window->margin <= x && x < window->margin + grip_size)
1112 hlocation = WINDOW_RESIZING_LEFT;
1113 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115 else if (x < window->allocation.width - window->margin)
1116 hlocation = WINDOW_RESIZING_RIGHT;
1117 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119
1120 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001121 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001122 else if (window->margin <= y && y < window->margin + grip_size)
1123 vlocation = WINDOW_RESIZING_TOP;
1124 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 else if (y < window->allocation.height - window->margin)
1127 vlocation = WINDOW_RESIZING_BOTTOM;
1128 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001129 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001130
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 location = vlocation | hlocation;
1132 if (location & WINDOW_EXTERIOR)
1133 location = WINDOW_EXTERIOR;
1134 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1135 location = WINDOW_TITLEBAR;
1136 else if (location == WINDOW_INTERIOR)
1137 location = WINDOW_CLIENT_AREA;
1138
1139 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140}
1141
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001142void
1143input_set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144{
1145 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001146 struct wl_buffer *buffer;
1147 cairo_surface_t *surface;
1148 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001149
1150 location = get_pointer_location(input->pointer_focus,
1151 input->sx, input->sy);
1152 switch (location) {
1153 case WINDOW_RESIZING_TOP:
1154 pointer = POINTER_TOP;
1155 break;
1156 case WINDOW_RESIZING_BOTTOM:
1157 pointer = POINTER_BOTTOM;
1158 break;
1159 case WINDOW_RESIZING_LEFT:
1160 pointer = POINTER_LEFT;
1161 break;
1162 case WINDOW_RESIZING_RIGHT:
1163 pointer = POINTER_RIGHT;
1164 break;
1165 case WINDOW_RESIZING_TOP_LEFT:
1166 pointer = POINTER_TOP_LEFT;
1167 break;
1168 case WINDOW_RESIZING_TOP_RIGHT:
1169 pointer = POINTER_TOP_RIGHT;
1170 break;
1171 case WINDOW_RESIZING_BOTTOM_LEFT:
1172 pointer = POINTER_BOTTOM_LEFT;
1173 break;
1174 case WINDOW_RESIZING_BOTTOM_RIGHT:
1175 pointer = POINTER_BOTTOM_RIGHT;
1176 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001177 case WINDOW_EXTERIOR:
1178 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001179 if (input->current_pointer_image == POINTER_DEFAULT)
1180 return;
1181
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001182 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001183 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001184 return;
1185 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001186 break;
1187 }
1188
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001189 if (pointer == input->current_pointer_image)
1190 return;
1191
1192 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001193 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001194
1195 if (!surface)
1196 return;
1197
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001198 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001199 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001200 pointer_images[pointer].hotspot_x,
1201 pointer_images[pointer].hotspot_y);
1202}
1203
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001204static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001205window_set_focus_item(struct window *window, struct item *focus)
1206{
1207 void *data;
1208
1209 if (focus == window->focus_item)
1210 return;
1211
1212 window->focus_item = focus;
1213 data = focus ? focus->user_data : NULL;
1214 if (window->item_focus_handler)
1215 window->item_focus_handler(window, focus, data);
1216}
1217
1218static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001219window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001220 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001221 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001222{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001223 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001224 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001225 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001226 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001227
1228 input->x = x;
1229 input->y = y;
1230 input->sx = sx;
1231 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001233 if (!window->focus_item || !window->item_grab_button) {
1234 item = window_find_item(window, sx, sy);
1235 window_set_focus_item(window, item);
1236 }
1237
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001238 if (window->motion_handler)
1239 pointer = (*window->motion_handler)(window, input, time,
1240 x, y, sx, sy,
1241 window->user_data);
1242
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001243 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001244}
1245
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001246static void
1247window_handle_button(void *data,
1248 struct wl_input_device *input_device,
1249 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001250{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001251 struct input *input = data;
1252 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001253 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001255
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001256 if (window->focus_item && window->item_grab_button == 0 && state)
1257 window->item_grab_button = button;
1258
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001260
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001261 if (window->display->shell &&
1262 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001264 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001265 wl_shell_move(window->display->shell,
1266 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001267 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001268 case WINDOW_RESIZING_TOP:
1269 case WINDOW_RESIZING_BOTTOM:
1270 case WINDOW_RESIZING_LEFT:
1271 case WINDOW_RESIZING_RIGHT:
1272 case WINDOW_RESIZING_TOP_LEFT:
1273 case WINDOW_RESIZING_TOP_RIGHT:
1274 case WINDOW_RESIZING_BOTTOM_LEFT:
1275 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001276 wl_shell_resize(window->display->shell,
1277 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001279 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001280 case WINDOW_CLIENT_AREA:
1281 if (window->button_handler)
1282 (*window->button_handler)(window,
1283 input, time,
1284 button, state,
1285 window->user_data);
1286 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001287 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001288 } else {
1289 if (window->button_handler)
1290 (*window->button_handler)(window,
1291 input, time,
1292 button, state,
1293 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001294 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001295
1296 if (window->focus_item &&
1297 window->item_grab_button == button && !state) {
1298 window->item_grab_button = 0;
1299 item = window_find_item(window, input->sx, input->sy);
1300 window_set_focus_item(window, item);
1301 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001302}
1303
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001304static void
1305window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001306 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001307{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001308 struct input *input = data;
1309 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001310 struct display *d = window->display;
1311 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001312
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001313 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001314 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001315 return;
1316
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001317 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001318 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001319 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1320 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001321
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001322 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1323
1324 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001325 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001326 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001328
1329 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001330 (*window->key_handler)(window, input, time, key, sym, state,
1331 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001332}
1333
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001334static void
1335window_handle_pointer_focus(void *data,
1336 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001337 uint32_t time, struct wl_surface *surface,
1338 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001339{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001340 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001341 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001342 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001343 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001344
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001345 window = input->pointer_focus;
1346 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001347 window_set_focus_item(window, NULL);
1348
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001349 if (window->leave_handler)
1350 window->leave_handler(window, input,
1351 time, window->user_data);
1352 input->pointer_focus = NULL;
1353 input->current_pointer_image = POINTER_UNSET;
1354 }
1355
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001356 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001357 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001358 window = input->pointer_focus;
1359
Kristian Høgsberg59826582011-01-20 11:56:57 -05001360 input->x = x;
1361 input->y = y;
1362 input->sx = sx;
1363 input->sy = sy;
1364
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001365 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001366 if (window->enter_handler)
1367 pointer = window->enter_handler(window, input,
1368 time, sx, sy,
1369 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001370
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001371 item = window_find_item(window, x, y);
1372 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001373
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001374 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001375 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001376}
1377
1378static void
1379window_handle_keyboard_focus(void *data,
1380 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001381 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001382 struct wl_surface *surface,
1383 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001384{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001385 struct input *input = data;
1386 struct window *window = input->keyboard_focus;
1387 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001388 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001389
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001390 window = input->keyboard_focus;
1391 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001392 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001393 if (window->keyboard_focus_handler)
1394 (*window->keyboard_focus_handler)(window, NULL,
1395 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001396 }
1397
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001398 if (surface)
1399 input->keyboard_focus = wl_surface_get_user_data(surface);
1400 else
1401 input->keyboard_focus = NULL;
1402
1403 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001404 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001405 for (k = keys->data; k < end; k++)
1406 input->modifiers |= d->xkb->map->modmap[*k];
1407
1408 window = input->keyboard_focus;
1409 if (window) {
1410 window->keyboard_device = input;
1411 if (window->keyboard_focus_handler)
1412 (*window->keyboard_focus_handler)(window,
1413 window->keyboard_device,
1414 window->user_data);
1415 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001416}
1417
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001418static const struct wl_input_device_listener input_device_listener = {
1419 window_handle_motion,
1420 window_handle_button,
1421 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001422 window_handle_pointer_focus,
1423 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001424};
1425
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001426void
1427input_get_position(struct input *input, int32_t *x, int32_t *y)
1428{
1429 *x = input->sx;
1430 *y = input->sy;
1431}
1432
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001433struct wl_input_device *
1434input_get_input_device(struct input *input)
1435{
1436 return input->input_device;
1437}
1438
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001439uint32_t
1440input_get_modifiers(struct input *input)
1441{
1442 return input->modifiers;
1443}
1444
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001445struct data_offer {
1446 struct wl_data_offer *offer;
1447 struct input *input;
1448 struct wl_array types;
1449 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001450
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001451 struct task io_task;
1452 int fd;
1453 data_func_t func;
1454 int32_t x, y;
1455 void *user_data;
1456};
1457
1458static void
1459data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1460{
1461 struct data_offer *offer = data;
1462 char **p;
1463
1464 p = wl_array_add(&offer->types, sizeof *p);
1465 *p = strdup(type);
1466}
1467
1468static const struct wl_data_offer_listener data_offer_listener = {
1469 data_offer_offer,
1470};
1471
1472static void
1473data_offer_destroy(struct data_offer *offer)
1474{
1475 char **p;
1476
1477 offer->refcount--;
1478 if (offer->refcount == 0) {
1479 wl_data_offer_destroy(offer->offer);
1480 for (p = offer->types.data; *p; p++)
1481 free(*p);
1482 wl_array_release(&offer->types);
1483 free(offer);
1484 }
1485}
1486
1487static void
1488data_device_data_offer(void *data,
1489 struct wl_data_device *data_device, uint32_t id)
1490{
1491 struct data_offer *offer;
1492
1493 offer = malloc(sizeof *offer);
1494
1495 wl_array_init(&offer->types);
1496 offer->refcount = 1;
1497 offer->input = data;
1498
1499 /* FIXME: Generate typesafe wrappers for this */
1500 offer->offer = (struct wl_data_offer *)
1501 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1502 id, &wl_data_offer_interface);
1503
1504 wl_data_offer_add_listener(offer->offer,
1505 &data_offer_listener, offer);
1506}
1507
1508static void
1509data_device_enter(void *data, struct wl_data_device *data_device,
1510 uint32_t time, struct wl_surface *surface,
1511 int32_t x, int32_t y, struct wl_data_offer *offer)
1512{
1513 struct input *input = data;
1514 struct window *window;
1515 char **p;
1516
1517 input->drag_offer = wl_data_offer_get_user_data(offer);
1518 window = wl_surface_get_user_data(surface);
1519 input->pointer_focus = window;
1520
1521 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1522 *p = NULL;
1523
1524 window = input->pointer_focus;
1525 if (window->data_handler)
1526 window->data_handler(window, input, time, x, y,
1527 input->drag_offer->types.data,
1528 window->user_data);
1529}
1530
1531static void
1532data_device_leave(void *data, struct wl_data_device *data_device)
1533{
1534 struct input *input = data;
1535
1536 data_offer_destroy(input->drag_offer);
1537 input->drag_offer = NULL;
1538}
1539
1540static void
1541data_device_motion(void *data, struct wl_data_device *data_device,
1542 uint32_t time, int32_t x, int32_t y)
1543{
1544 struct input *input = data;
1545 struct window *window = input->pointer_focus;
1546
1547 input->sx = x;
1548 input->sy = y;
1549
1550 if (window->data_handler)
1551 window->data_handler(window, input, time, x, y,
1552 input->drag_offer->types.data,
1553 window->user_data);
1554}
1555
1556static void
1557data_device_drop(void *data, struct wl_data_device *data_device)
1558{
1559 struct input *input = data;
1560 struct window *window = input->pointer_focus;
1561
1562 if (window->drop_handler)
1563 window->drop_handler(window, input,
1564 input->sx, input->sy, window->user_data);
1565}
1566
1567static void
1568data_device_selection(void *data,
1569 struct wl_data_device *wl_data_device,
1570 struct wl_data_offer *offer)
1571{
1572 struct input *input = data;
1573 char **p;
1574
1575 if (input->selection_offer)
1576 data_offer_destroy(input->selection_offer);
1577
1578 input->selection_offer = wl_data_offer_get_user_data(offer);
1579 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1580 *p = NULL;
1581}
1582
1583static const struct wl_data_device_listener data_device_listener = {
1584 data_device_data_offer,
1585 data_device_enter,
1586 data_device_leave,
1587 data_device_motion,
1588 data_device_drop,
1589 data_device_selection
1590};
1591
1592struct wl_data_device *
1593input_get_data_device(struct input *input)
1594{
1595 return input->data_device;
1596}
1597
1598void
1599input_set_selection(struct input *input,
1600 struct wl_data_source *source, uint32_t time)
1601{
1602 wl_data_device_set_selection(input->data_device, source, time);
1603}
1604
1605void
1606input_accept(struct input *input, uint32_t time, const char *type)
1607{
1608 wl_data_offer_accept(input->drag_offer->offer, time, type);
1609}
1610
1611static void
1612offer_io_func(struct task *task, uint32_t events)
1613{
1614 struct data_offer *offer =
1615 container_of(task, struct data_offer, io_task);
1616 unsigned int len;
1617 char buffer[4096];
1618
1619 len = read(offer->fd, buffer, sizeof buffer);
1620 offer->func(buffer, len,
1621 offer->x, offer->y, offer->user_data);
1622
1623 if (len == 0) {
1624 close(offer->fd);
1625 data_offer_destroy(offer);
1626 }
1627}
1628
1629static void
1630data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1631 data_func_t func, void *user_data)
1632{
1633 int p[2];
1634
1635 pipe2(p, O_CLOEXEC);
1636 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1637 close(p[1]);
1638
1639 offer->io_task.run = offer_io_func;
1640 offer->fd = p[0];
1641 offer->func = func;
1642 offer->refcount++;
1643 offer->user_data = user_data;
1644
1645 display_watch_fd(offer->input->display,
1646 offer->fd, EPOLLIN, &offer->io_task);
1647}
1648
1649void
1650input_receive_drag_data(struct input *input, const char *mime_type,
1651 data_func_t func, void *data)
1652{
1653 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1654 input->drag_offer->x = input->sx;
1655 input->drag_offer->y = input->sy;
1656}
1657
1658int
1659input_receive_selection_data(struct input *input, const char *mime_type,
1660 data_func_t func, void *data)
1661{
1662 char **p;
1663
1664 if (input->selection_offer == NULL)
1665 return -1;
1666
1667 for (p = input->selection_offer->types.data; *p; p++)
1668 if (strcmp(mime_type, *p) == 0)
1669 break;
1670
1671 if (*p == NULL)
1672 return -1;
1673
1674 data_offer_receive_data(input->selection_offer,
1675 mime_type, func, data);
1676 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001677}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001678
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001679void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001680window_move(struct window *window, struct input *input, uint32_t time)
1681{
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001682 if (window->display->shell)
1683 wl_shell_move(window->display->shell,
1684 window->surface, input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001685}
1686
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001687static void
1688handle_configure(void *data, struct wl_shell *shell,
1689 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001690 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001691{
1692 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001693 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001694
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001695 /* FIXME: this is probably the wrong place to check for width
1696 * or height <= 0, but it prevents the compositor from crashing
1697 */
1698 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001699 return;
1700
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001701 window->resize_edges = edges;
1702
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001703 if (window->resize_handler) {
1704 child_width = width - 20 - window->margin * 2;
1705 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001706
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001707 (*window->resize_handler)(window,
1708 child_width, child_height,
1709 window->user_data);
1710 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001711 window->allocation.width = width;
1712 window->allocation.height = height;
1713
1714 if (window->redraw_handler)
1715 window_schedule_redraw(window);
1716 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001717}
1718
1719static const struct wl_shell_listener shell_listener = {
1720 handle_configure,
1721};
1722
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001723void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001724window_get_allocation(struct window *window,
1725 struct rectangle *allocation)
1726{
1727 *allocation = window->allocation;
1728}
1729
1730void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001731window_get_child_allocation(struct window *window,
1732 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001733{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001734 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001735 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001736 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001737 allocation->x = window->margin + 10;
1738 allocation->y = window->margin + 50;
1739 allocation->width =
1740 window->allocation.width - 20 - window->margin * 2;
1741 allocation->height =
1742 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001743 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001744}
1745
1746void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001747window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001748{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001749 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001750 window->allocation.x = 20 + window->margin;
1751 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001752 window->allocation.width = width + 20 + window->margin * 2;
1753 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001754 } else {
1755 window->allocation.x = 0;
1756 window->allocation.y = 0;
1757 window->allocation.width = width;
1758 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001759 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001760}
1761
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001762static void
1763idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001764{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001765 struct window *window =
1766 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001767
1768 window->redraw_handler(window, window->user_data);
1769 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001770}
1771
1772void
1773window_schedule_redraw(struct window *window)
1774{
1775 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001776 window->redraw_task.run = idle_redraw;
1777 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001778 window->redraw_scheduled = 1;
1779 }
1780}
1781
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001782void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001783window_set_custom(struct window *window)
1784{
1785 window->type = TYPE_CUSTOM;
1786}
1787
1788void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001789window_set_fullscreen(struct window *window, int fullscreen)
1790{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001791 int32_t width, height;
1792
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001793 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001794 return;
1795
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001796 if (fullscreen) {
1797 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001798 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001799 width = window->display->screen_allocation.width;
1800 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001801 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001802 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001803 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001804 width = window->saved_allocation.width - 20 - window->margin * 2;
1805 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001806 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001807 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001808
1809 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001810}
1811
1812void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001813window_set_decoration(struct window *window, int decoration)
1814{
1815 window->decoration = decoration;
1816}
1817
1818void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001819window_set_user_data(struct window *window, void *data)
1820{
1821 window->user_data = data;
1822}
1823
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001824void *
1825window_get_user_data(struct window *window)
1826{
1827 return window->user_data;
1828}
1829
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001830void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001831window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001832 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001833{
1834 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001835}
1836
1837void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001838window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001839 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001840{
1841 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001842}
1843
1844void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001845window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001846 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001847{
1848 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001849}
1850
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001851void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001852window_set_button_handler(struct window *window,
1853 window_button_handler_t handler)
1854{
1855 window->button_handler = handler;
1856}
1857
1858void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001859window_set_motion_handler(struct window *window,
1860 window_motion_handler_t handler)
1861{
1862 window->motion_handler = handler;
1863}
1864
1865void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001866window_set_enter_handler(struct window *window,
1867 window_enter_handler_t handler)
1868{
1869 window->enter_handler = handler;
1870}
1871
1872void
1873window_set_leave_handler(struct window *window,
1874 window_leave_handler_t handler)
1875{
1876 window->leave_handler = handler;
1877}
1878
1879void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001880window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001881 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001882{
1883 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001884}
1885
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001886void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001887window_set_item_focus_handler(struct window *window,
1888 window_item_focus_handler_t handler)
1889{
1890 window->item_focus_handler = handler;
1891}
1892
1893void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001894window_set_data_handler(struct window *window, window_data_handler_t handler)
1895{
1896 window->data_handler = handler;
1897}
1898
1899void
1900window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1901{
1902 window->drop_handler = handler;
1903}
1904
1905void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001906window_set_transparent(struct window *window, int transparent)
1907{
1908 window->transparent = transparent;
1909}
1910
1911void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001912window_set_title(struct window *window, const char *title)
1913{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001914 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001915 window->title = strdup(title);
1916}
1917
1918const char *
1919window_get_title(struct window *window)
1920{
1921 return window->title;
1922}
1923
1924void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001925display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1926 int32_t x, int32_t y, int32_t width, int32_t height)
1927{
1928 struct wl_buffer *buffer;
1929
1930 buffer = display_get_buffer_for_surface(display, cairo_surface);
1931
1932 wl_buffer_damage(buffer, x, y, width, height);
1933}
1934
1935void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001936window_damage(struct window *window, int32_t x, int32_t y,
1937 int32_t width, int32_t height)
1938{
1939 wl_surface_damage(window->surface, x, y, width, height);
1940}
1941
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001942static struct window *
1943window_create_internal(struct display *display, struct window *parent,
1944 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001945{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001946 struct window *window;
1947
1948 window = malloc(sizeof *window);
1949 if (window == NULL)
1950 return NULL;
1951
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001952 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001953 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001954 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001955 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001956 window->allocation.x = 0;
1957 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001958 window->allocation.width = width;
1959 window->allocation.height = height;
1960 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001961 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001962 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001963 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001964 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001965
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001966 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001967#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001968 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1969 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001970#else
1971 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1972#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001973 else
1974 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001975
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001976 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001977 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001978
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001979 return window;
1980}
1981
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001982struct window *
1983window_create(struct display *display, int32_t width, int32_t height)
1984{
1985 struct window *window;
1986
1987 window = window_create_internal(display, NULL, width, height);
1988 if (!window)
1989 return NULL;
1990
1991 return window;
1992}
1993
1994struct window *
1995window_create_transient(struct display *display, struct window *parent,
1996 int32_t x, int32_t y, int32_t width, int32_t height)
1997{
1998 struct window *window;
1999
2000 window = window_create_internal(parent->display,
2001 parent, width, height);
2002 if (!window)
2003 return NULL;
2004
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002005 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002006 window->x = x;
2007 window->y = y;
2008
2009 return window;
2010}
2011
2012void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002013window_set_buffer_type(struct window *window, enum window_buffer_type type)
2014{
2015 window->buffer_type = type;
2016}
2017
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002018
2019static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002020display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002021 struct wl_output *wl_output,
2022 int x, int y,
2023 int physical_width,
2024 int physical_height,
2025 int subpixel,
2026 const char *make,
2027 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002028{
2029 struct display *display = data;
2030
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05002031 display->screen_allocation.x = x;
2032 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002033}
2034
2035static void
2036display_handle_mode(void *data,
2037 struct wl_output *wl_output,
2038 uint32_t flags,
2039 int width,
2040 int height,
2041 int refresh)
2042{
2043 struct display *display = data;
2044
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002045 display->screen_allocation.width = width;
2046 display->screen_allocation.height = height;
2047}
2048
2049static const struct wl_output_listener output_listener = {
2050 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002051 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002052};
2053
2054static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002055display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002056{
2057 struct input *input;
2058
2059 input = malloc(sizeof *input);
2060 if (input == NULL)
2061 return;
2062
2063 memset(input, 0, sizeof *input);
2064 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002065 input->input_device =
2066 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002067 input->pointer_focus = NULL;
2068 input->keyboard_focus = NULL;
2069 wl_list_insert(d->input_list.prev, &input->link);
2070
2071 wl_input_device_add_listener(input->input_device,
2072 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002073 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002074
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002075 input->data_device =
2076 wl_data_device_manager_get_data_device(d->data_device_manager,
2077 input->input_device);
2078 wl_data_device_add_listener(input->data_device,
2079 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002080}
2081
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002082static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002083display_handle_global(struct wl_display *display, uint32_t id,
2084 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002085{
2086 struct display *d = data;
2087
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002088 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002089 d->compositor =
2090 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002091 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002092 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002093 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002094 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002095 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002096 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002097 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002098 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002099 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002100 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002101 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2102 d->data_device_manager =
2103 wl_display_bind(display, id,
2104 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002105 }
2106}
2107
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002108static void
2109display_render_frame(struct display *d)
2110{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002111 int radius = 8;
2112 cairo_t *cr;
2113
2114 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2115 cr = cairo_create(d->shadow);
2116 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2117 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2118 rounded_rect(cr, 16, 16, 112, 112, radius);
2119 cairo_fill(cr);
2120 cairo_destroy(cr);
2121 blur_surface(d->shadow, 64);
2122
2123 d->active_frame =
2124 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2125 cr = cairo_create(d->active_frame);
2126 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2127 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2128 rounded_rect(cr, 16, 16, 112, 112, radius);
2129 cairo_fill(cr);
2130 cairo_destroy(cr);
2131
2132 d->inactive_frame =
2133 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2134 cr = cairo_create(d->inactive_frame);
2135 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2136 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2137 rounded_rect(cr, 16, 16, 112, 112, radius);
2138 cairo_fill(cr);
2139 cairo_destroy(cr);
2140}
2141
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002142static void
2143init_xkb(struct display *d)
2144{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002145 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002146
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002147 names.rules = "evdev";
2148 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002149 names.layout = option_xkb_layout;
2150 names.variant = option_xkb_variant;
2151 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002152
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002153 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002154 if (!d->xkb) {
2155 fprintf(stderr, "Failed to compile keymap\n");
2156 exit(1);
2157 }
2158}
2159
Yuval Fledel45568f62010-12-06 09:18:12 -05002160static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002161init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002162{
2163 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002164 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002165
2166 static const EGLint premul_argb_cfg_attribs[] = {
2167 EGL_SURFACE_TYPE,
2168 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2169 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002170 EGL_RED_SIZE, 1,
2171 EGL_GREEN_SIZE, 1,
2172 EGL_BLUE_SIZE, 1,
2173 EGL_ALPHA_SIZE, 1,
2174 EGL_DEPTH_SIZE, 1,
2175 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2176 EGL_NONE
2177 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002178
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002179 static const EGLint rgb_cfg_attribs[] = {
2180 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2181 EGL_RED_SIZE, 1,
2182 EGL_GREEN_SIZE, 1,
2183 EGL_BLUE_SIZE, 1,
2184 EGL_ALPHA_SIZE, 0,
2185 EGL_DEPTH_SIZE, 1,
2186 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2187 EGL_NONE
2188 };
2189
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002190 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002191 if (!eglInitialize(d->dpy, &major, &minor)) {
2192 fprintf(stderr, "failed to initialize display\n");
2193 return -1;
2194 }
2195
2196 if (!eglBindAPI(EGL_OPENGL_API)) {
2197 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2198 return -1;
2199 }
2200
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002201 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002202 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002203 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002204 return -1;
2205 }
2206
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002207 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2208 &d->rgb_config, 1, &n) || n != 1) {
2209 fprintf(stderr, "failed to choose rgb config\n");
2210 return -1;
2211 }
2212
Benjamin Franzke0c991632011-09-27 21:57:31 +02002213 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2214 if (d->rgb_ctx == NULL) {
2215 fprintf(stderr, "failed to create context\n");
2216 return -1;
2217 }
2218 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2219 EGL_NO_CONTEXT, NULL);
2220 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002221 fprintf(stderr, "failed to create context\n");
2222 return -1;
2223 }
2224
Benjamin Franzke0c991632011-09-27 21:57:31 +02002225 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002226 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002227 return -1;
2228 }
2229
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002230#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002231 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2232 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002233 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002234 return -1;
2235 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002236 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2237 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2238 fprintf(stderr, "failed to get cairo egl argb device\n");
2239 return -1;
2240 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002241#endif
2242
2243 return 0;
2244}
2245
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002246static int
2247event_mask_update(uint32_t mask, void *data)
2248{
2249 struct display *d = data;
2250
2251 d->mask = mask;
2252
2253 return 0;
2254}
2255
2256static void
2257handle_display_data(struct task *task, uint32_t events)
2258{
2259 struct display *display =
2260 container_of(task, struct display, display_task);
2261
2262 wl_display_iterate(display->display, display->mask);
2263}
2264
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002265struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002266display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002267{
2268 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002269 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002270 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002271 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002272
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002273 g_type_init();
2274
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002275 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002276 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002277 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002278
2279 xkb_option_group = g_option_group_new("xkb",
2280 "Keyboard options",
2281 "Show all XKB options",
2282 NULL, NULL);
2283 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2284 g_option_context_add_group (context, xkb_option_group);
2285
2286 if (!g_option_context_parse(context, argc, argv, &error)) {
2287 fprintf(stderr, "option parsing failed: %s\n", error->message);
2288 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002289 }
2290
Tim Wiederhake748f6722011-01-23 23:25:25 +01002291 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002292
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002293 d = malloc(sizeof *d);
2294 if (d == NULL)
2295 return NULL;
2296
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002297 memset(d, 0, sizeof *d);
2298
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002299 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002300 if (d->display == NULL) {
2301 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002302 return NULL;
2303 }
2304
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002305 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2306 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2307 d->display_task.run = handle_display_data;
2308 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2309
2310 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002311 wl_list_init(&d->input_list);
2312
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002313 /* Set up listener so we'll catch all events. */
2314 wl_display_add_global_listener(d->display,
2315 display_handle_global, d);
2316
2317 /* Process connection events. */
2318 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002319 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002320 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002321
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002322 d->image_target_texture_2d =
2323 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2324 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2325 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2326
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002327 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002328
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002329 display_render_frame(d);
2330
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002331 wl_list_init(&d->window_list);
2332
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002333 init_xkb(d);
2334
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002335 return d;
2336}
2337
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002338struct wl_display *
2339display_get_display(struct display *display)
2340{
2341 return display->display;
2342}
2343
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002344struct wl_compositor *
2345display_get_compositor(struct display *display)
2346{
2347 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002348}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002349
2350EGLDisplay
2351display_get_egl_display(struct display *d)
2352{
2353 return d->dpy;
2354}
2355
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002356struct wl_data_source *
2357display_create_data_source(struct display *display)
2358{
2359 return wl_data_device_manager_create_data_source(display->data_device_manager);
2360}
2361
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002362EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002363display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002364{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002365 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002366}
2367
Benjamin Franzke0c991632011-09-27 21:57:31 +02002368EGLConfig
2369display_get_argb_egl_config(struct display *d)
2370{
2371 return d->premultiplied_argb_config;
2372}
2373
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002374struct wl_shell *
2375display_get_shell(struct display *display)
2376{
2377 return display->shell;
2378}
2379
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002380int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002381display_acquire_window_surface(struct display *display,
2382 struct window *window,
2383 EGLContext ctx)
2384{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002385#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002386 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002387 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002388
2389 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002390 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002391 device = cairo_surface_get_device(window->cairo_surface);
2392 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002393 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002394
Benjamin Franzke0c991632011-09-27 21:57:31 +02002395 if (!ctx) {
2396 if (device == display->rgb_device)
2397 ctx = display->rgb_ctx;
2398 else if (device == display->argb_device)
2399 ctx = display->argb_ctx;
2400 else
2401 assert(0);
2402 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002403
2404 data = cairo_surface_get_user_data(window->cairo_surface,
2405 &surface_data_key);
2406
Benjamin Franzke0c991632011-09-27 21:57:31 +02002407 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002408 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2409 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002410
2411 return 0;
2412#else
2413 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002414#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002415}
2416
2417void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002418display_release_window_surface(struct display *display,
2419 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002420{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002421#ifdef HAVE_CAIRO_EGL
2422 cairo_device_t *device;
2423
2424 device = cairo_surface_get_device(window->cairo_surface);
2425 if (!device)
2426 return;
2427
2428 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002429 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002430 cairo_device_release(device);
2431#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002432}
2433
2434void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002435display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002436{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002437 wl_list_insert(&display->deferred_list, &task->link);
2438}
2439
2440void
2441display_watch_fd(struct display *display,
2442 int fd, uint32_t events, struct task *task)
2443{
2444 struct epoll_event ep;
2445
2446 ep.events = events;
2447 ep.data.ptr = task;
2448 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2449}
2450
2451void
2452display_run(struct display *display)
2453{
2454 struct task *task;
2455 struct epoll_event ep[16];
2456 int i, count;
2457
2458 while (1) {
2459 while (display->mask & WL_DISPLAY_WRITABLE)
2460 wl_display_iterate(display->display,
2461 WL_DISPLAY_WRITABLE);
2462
2463 count = epoll_wait(display->epoll_fd,
2464 ep, ARRAY_LENGTH(ep), -1);
2465 for (i = 0; i < count; i++) {
2466 task = ep[i].data.ptr;
2467 task->run(task, ep[i].events);
2468 }
2469
2470 while (!wl_list_empty(&display->deferred_list)) {
2471 task = container_of(display->deferred_list.next,
2472 struct task, link);
2473 wl_list_remove(&task->link);
2474 task->run(task, 0);
2475 }
2476 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002477}