blob: e8e3508d2333fcce32968b6373ef13cc7e6134c6 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct wl_output *output;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020069 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 EGLContext rgb_ctx;
71 EGLContext argb_ctx;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040074
75 int display_fd;
76 uint32_t mask;
77 struct task display_task;
78
79 int epoll_fd;
80 struct wl_list deferred_list;
81
Kristian Høgsberg478d9262010-06-08 20:34:11 -040082 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040083 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050084 struct wl_list output_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040085 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
103 TYPE_CUSTOM
104};
105
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500106struct window {
107 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500108 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500109 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500111 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500112 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500113 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400114 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400115 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400116 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500117 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500118 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400119 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400120 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400121 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500123 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400124 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500125
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400126 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500127 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500128
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500129 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400130 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400132 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500133 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400134 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400135 window_enter_handler_t enter_handler;
136 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400137 window_item_focus_handler_t item_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400138 window_data_handler_t data_handler;
139 window_drop_handler_t drop_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140
141 struct wl_list item_list;
142 struct item *focus_item;
143 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500145 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400146 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500147};
148
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400149struct item {
150 struct wl_list link;
151 struct rectangle allocation;
152 void *user_data;
153};
154
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400155struct input {
156 struct display *display;
157 struct wl_input_device *input_device;
158 struct window *pointer_focus;
159 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400160 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400161 uint32_t modifiers;
162 int32_t x, y, sx, sy;
163 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400164
165 struct wl_data_device *data_device;
166 struct data_offer *drag_offer;
167 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400168};
169
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500170struct output {
171 struct display *display;
172 struct wl_output *output;
173 struct rectangle allocation;
174 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200175
176 display_output_handler_t destroy_handler;
177 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500178};
179
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400180enum {
181 POINTER_DEFAULT = 100,
182 POINTER_UNSET
183};
184
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500185enum window_location {
186 WINDOW_INTERIOR = 0,
187 WINDOW_RESIZING_TOP = 1,
188 WINDOW_RESIZING_BOTTOM = 2,
189 WINDOW_RESIZING_LEFT = 4,
190 WINDOW_RESIZING_TOP_LEFT = 5,
191 WINDOW_RESIZING_BOTTOM_LEFT = 6,
192 WINDOW_RESIZING_RIGHT = 8,
193 WINDOW_RESIZING_TOP_RIGHT = 9,
194 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
195 WINDOW_RESIZING_MASK = 15,
196 WINDOW_EXTERIOR = 16,
197 WINDOW_TITLEBAR = 17,
198 WINDOW_CLIENT_AREA = 18,
199};
200
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400201const char *option_xkb_layout = "us";
202const char *option_xkb_variant = "";
203const char *option_xkb_options = "";
204
205static const GOptionEntry xkb_option_entries[] = {
206 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
207 &option_xkb_layout, "XKB Layout" },
208 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
209 &option_xkb_variant, "XKB Variant" },
210 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
211 &option_xkb_options, "XKB Options" },
212 { NULL }
213};
214
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400215static const cairo_user_data_key_t surface_data_key;
216struct surface_data {
217 struct wl_buffer *buffer;
218};
219
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500220#define MULT(_d,c,a,t) \
221 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
222
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500223#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400224
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100225struct egl_window_surface_data {
226 struct display *display;
227 struct wl_surface *surface;
228 struct wl_egl_window *window;
229 EGLSurface surf;
230};
231
232static void
233egl_window_surface_data_destroy(void *p)
234{
235 struct egl_window_surface_data *data = p;
236 struct display *d = data->display;
237
238 eglDestroySurface(d->dpy, data->surf);
239 wl_egl_window_destroy(data->window);
240 data->surface = NULL;
241
242 free(p);
243}
244
245static cairo_surface_t *
246display_create_egl_window_surface(struct display *display,
247 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400248 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100249 struct rectangle *rectangle)
250{
251 cairo_surface_t *cairo_surface;
252 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400253 EGLConfig config;
254 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200255 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100256
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400257 static const EGLint premul_attribs[] = {
258 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
259 EGL_NONE
260 };
261
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100262 data = malloc(sizeof *data);
263 if (data == NULL)
264 return NULL;
265
266 data->display = display;
267 data->surface = surface;
268
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400269 if (flags & SURFACE_OPAQUE) {
270 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200271 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200272 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 } else {
274 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200275 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 attribs = premul_attribs;
277 }
278
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400279 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100280 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400281 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100282
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400283 data->surf = eglCreateWindowSurface(display->dpy, config,
284 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100285
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100287 data->surf,
288 rectangle->width,
289 rectangle->height);
290
291 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
292 data, egl_window_surface_data_destroy);
293
294 return cairo_surface;
295}
296
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500297struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400298 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200299 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400300 EGLImageKHR image;
301 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800302 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500303 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400304};
305
306static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500307egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400308{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500309 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800310 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311
Benjamin Franzke0c991632011-09-27 21:57:31 +0200312 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200314 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800315
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500316 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500318 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320}
321
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500322EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323display_get_image_for_egl_image_surface(struct display *display,
324 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500325{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500326 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500327
328 data = cairo_surface_get_user_data (surface, &surface_data_key);
329
330 return data->image;
331}
332
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500333static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400335 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339 EGLDisplay dpy = display->dpy;
340 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200341 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342
343 data = malloc(sizeof *data);
344 if (data == NULL)
345 return NULL;
346
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800347 data->display = display;
348
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400349 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400350 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000352 free(data);
353 return NULL;
354 }
355
Benjamin Franzke0c991632011-09-27 21:57:31 +0200356 if (flags & SURFACE_OPAQUE) {
357 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200358 content = CAIRO_CONTENT_COLOR;
359 } else {
360 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200361 content = CAIRO_CONTENT_COLOR_ALPHA;
362 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500364 data->image = display->create_image(dpy, NULL,
365 EGL_NATIVE_PIXMAP_KHR,
366 (EGLClientBuffer) data->pixmap,
367 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500368 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500369 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500370 free(data);
371 return NULL;
372 }
373
374 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400375 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500376
Benjamin Franzke0c991632011-09-27 21:57:31 +0200377 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400378 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400379 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500380 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200381 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400382
Benjamin Franzke0c991632011-09-27 21:57:31 +0200383 surface = cairo_gl_surface_create_for_texture(data->device,
384 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385 data->texture,
386 rectangle->width,
387 rectangle->height);
388
389 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391
392 return surface;
393}
394
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500395static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500396display_create_egl_image_surface_from_file(struct display *display,
397 const char *filename,
398 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399{
400 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400401 GdkPixbuf *pixbuf;
402 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400403 int stride, i;
404 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500405 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400406
407 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400409 FALSE, &error);
410 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400412
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400413 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
414 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500415 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400417 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400418
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400419
420 stride = gdk_pixbuf_get_rowstride(pixbuf);
421 pixels = gdk_pixbuf_get_pixels(pixbuf);
422
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400424 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400426 while (p < end) {
427 unsigned int t;
428
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 MULT(p[0], p[0], p[3], t);
430 MULT(p[1], p[1], p[3], t);
431 MULT(p[2], p[2], p[3], t);
432 p += 4;
433
434 }
435 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400436
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200437 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000438 if (surface == NULL) {
439 g_object_unref(pixbuf);
440 return NULL;
441 }
442
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800443 data = cairo_surface_get_user_data(surface, &surface_data_key);
444
Benjamin Franzke0c991632011-09-27 21:57:31 +0200445 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800446 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800447 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
448 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200449 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400450
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500451 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400452
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400453 return surface;
454}
455
456#endif
457
458struct wl_buffer *
459display_get_buffer_for_surface(struct display *display,
460 cairo_surface_t *surface)
461{
462 struct surface_data *data;
463
464 data = cairo_surface_get_user_data (surface, &surface_data_key);
465
466 return data->buffer;
467}
468
469struct shm_surface_data {
470 struct surface_data data;
471 void *map;
472 size_t length;
473};
474
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500475static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400476shm_surface_data_destroy(void *p)
477{
478 struct shm_surface_data *data = p;
479
480 wl_buffer_destroy(data->data.buffer);
481 munmap(data->map, data->length);
482}
483
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500484static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400486 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400487{
488 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400489 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400490 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800491 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492 char filename[] = "/tmp/wayland-shm-XXXXXX";
493
494 data = malloc(sizeof *data);
495 if (data == NULL)
496 return NULL;
497
498 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
499 rectangle->width);
500 data->length = stride * rectangle->height;
501 fd = mkstemp(filename);
502 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000503 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400504 return NULL;
505 }
506 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000507 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508 close(fd);
509 return NULL;
510 }
511
512 data->map = mmap(NULL, data->length,
513 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
514 unlink(filename);
515
516 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000517 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518 close(fd);
519 return NULL;
520 }
521
522 surface = cairo_image_surface_create_for_data (data->map,
523 CAIRO_FORMAT_ARGB32,
524 rectangle->width,
525 rectangle->height,
526 stride);
527
528 cairo_surface_set_user_data (surface, &surface_data_key,
529 data, shm_surface_data_destroy);
530
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400531 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400532 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400533 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400534 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400535
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536 data->data.buffer = wl_shm_create_buffer(display->shm,
537 fd,
538 rectangle->width,
539 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400540 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
542 close(fd);
543
544 return surface;
545}
546
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500547static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548display_create_shm_surface_from_file(struct display *display,
549 const char *filename,
550 struct rectangle *rect)
551{
552 cairo_surface_t *surface;
553 GdkPixbuf *pixbuf;
554 GError *error = NULL;
555 int stride, i;
556 unsigned char *pixels, *p, *end, *dest_data;
557 int dest_stride;
558 uint32_t *d;
559
560 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
561 rect->width, rect->height,
562 FALSE, &error);
563 if (error != NULL)
564 return NULL;
565
566 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
567 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500568 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569 return NULL;
570 }
571
572 stride = gdk_pixbuf_get_rowstride(pixbuf);
573 pixels = gdk_pixbuf_get_pixels(pixbuf);
574
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400575 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000576 if (surface == NULL) {
577 g_object_unref(pixbuf);
578 return NULL;
579 }
580
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581 dest_data = cairo_image_surface_get_data (surface);
582 dest_stride = cairo_image_surface_get_stride (surface);
583
584 for (i = 0; i < rect->height; i++) {
585 d = (uint32_t *) (dest_data + i * dest_stride);
586 p = pixels + i * stride;
587 end = p + rect->width * 4;
588 while (p < end) {
589 unsigned int t;
590 unsigned char a, r, g, b;
591
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592 a = p[3];
593 MULT(r, p[0], a, t);
594 MULT(g, p[1], a, t);
595 MULT(b, p[2], a, t);
596 p += 4;
597 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
598 }
599 }
600
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500601 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602
603 return surface;
604}
605
nobled7b87cb02011-02-01 18:51:47 +0000606static int
607check_size(struct rectangle *rect)
608{
609 if (rect->width && rect->height)
610 return 0;
611
612 fprintf(stderr, "tried to create surface of "
613 "width: %d, height: %d\n", rect->width, rect->height);
614 return -1;
615}
616
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617cairo_surface_t *
618display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100619 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400620 struct rectangle *rectangle,
621 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622{
nobled7b87cb02011-02-01 18:51:47 +0000623 if (check_size(rectangle) < 0)
624 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500625#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500626 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100627 if (surface)
628 return display_create_egl_window_surface(display,
629 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400630 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 rectangle);
632 else
633 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400634 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500636 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400638 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639}
640
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500641static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642display_create_surface_from_file(struct display *display,
643 const char *filename,
644 struct rectangle *rectangle)
645{
nobled7b87cb02011-02-01 18:51:47 +0000646 if (check_size(rectangle) < 0)
647 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500648#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500649 if (display->dpy) {
650 return display_create_egl_image_surface_from_file(display,
651 filename,
652 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500653 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500655 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656}
Yuval Fledel45568f62010-12-06 09:18:12 -0500657 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658 const char *filename;
659 int hotspot_x, hotspot_y;
660} pointer_images[] = {
661 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
662 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
663 { DATADIR "/wayland/bottom_side.png", 16, 20 },
664 { DATADIR "/wayland/grabbing.png", 20, 17 },
665 { DATADIR "/wayland/left_ptr.png", 10, 5 },
666 { DATADIR "/wayland/left_side.png", 10, 20 },
667 { DATADIR "/wayland/right_side.png", 30, 19 },
668 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
669 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
670 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400671 { DATADIR "/wayland/xterm.png", 15, 15 },
672 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673};
674
675static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400676create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400677{
678 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400679 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400681
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400682 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400683 display->pointer_surfaces =
684 malloc(count * sizeof *display->pointer_surfaces);
685 rect.width = width;
686 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400687 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689 display_create_surface_from_file(display,
690 pointer_images[i].filename,
691 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100692 if (!display->pointer_surfaces[i]) {
693 fprintf(stderr, "Error loading pointer image: %s\n",
694 pointer_images[i].filename);
695 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696 }
697
698}
699
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700cairo_surface_t *
701display_get_pointer_surface(struct display *display, int pointer,
702 int *width, int *height,
703 int *hotspot_x, int *hotspot_y)
704{
705 cairo_surface_t *surface;
706
707 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500708#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400709 *width = cairo_gl_surface_get_width(surface);
710 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000711#else
712 *width = cairo_image_surface_get_width(surface);
713 *height = cairo_image_surface_get_height(surface);
714#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400715 *hotspot_x = pointer_images[pointer].hotspot_x;
716 *hotspot_y = pointer_images[pointer].hotspot_y;
717
718 return cairo_surface_reference(surface);
719}
720
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400721static void
722window_attach_surface(struct window *window);
723
724static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400725free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400726{
727 struct window *window = data;
728
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400729 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400730 cairo_surface_destroy(window->pending_surface);
731 window->pending_surface = NULL;
732 if (window->cairo_surface)
733 window_attach_surface(window);
734}
735
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400736static const struct wl_callback_listener free_surface_listener = {
737 free_surface
738};
739
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500740static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200741window_get_resize_dx_dy(struct window *window, int *x, int *y)
742{
743 if (window->resize_edges & WINDOW_RESIZING_LEFT)
744 *x = window->server_allocation.width - window->allocation.width;
745 else
746 *x = 0;
747
748 if (window->resize_edges & WINDOW_RESIZING_TOP)
749 *y = window->server_allocation.height -
750 window->allocation.height;
751 else
752 *y = 0;
753
754 window->resize_edges = 0;
755}
756
757static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400758window_set_type(struct window *window)
759{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200760 if (!window->shell_surface)
761 return;
762
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400763 switch (window->type) {
764 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200765 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400766 break;
767 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200768 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400769 break;
770 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200771 wl_shell_surface_set_transient(window->shell_surface,
772 window->parent->shell_surface,
773 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400774 break;
775 case TYPE_CUSTOM:
776 break;
777 }
778}
779
780static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500781window_attach_surface(struct window *window)
782{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400783 struct display *display = window->display;
784 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400785 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000786#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000788#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500789 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 if (display->shell)
792 window_set_type(window);
793
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000795#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100796 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
797 data = cairo_surface_get_user_data(window->cairo_surface,
798 &surface_data_key);
799
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 cairo_gl_surface_swapbuffers(window->cairo_surface);
801 wl_egl_window_get_attached_size(data->window,
802 &window->server_allocation.width,
803 &window->server_allocation.height);
804 break;
805 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000806#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100807 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200808 window_get_resize_dx_dy(window, &x, &y);
809
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100810 if (window->pending_surface != NULL)
811 return;
812
813 window->pending_surface = window->cairo_surface;
814 window->cairo_surface = NULL;
815
816 buffer =
817 display_get_buffer_for_surface(display,
818 window->pending_surface);
819
820 wl_surface_attach(window->surface, buffer, x, y);
821 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400822 cb = wl_display_sync(display->display);
823 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825 default:
826 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500828
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500829 wl_surface_damage(window->surface, 0, 0,
830 window->allocation.width,
831 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500832}
833
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500834void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400835window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500836{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100837 if (window->cairo_surface) {
838 switch (window->buffer_type) {
839 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
840 case WINDOW_BUFFER_TYPE_SHM:
841 display_surface_damage(window->display,
842 window->cairo_surface,
843 0, 0,
844 window->allocation.width,
845 window->allocation.height);
846 break;
847 default:
848 break;
849 }
850 window_attach_surface(window);
851 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500852}
853
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400854void
855window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500857 cairo_surface_reference(surface);
858
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400859 if (window->cairo_surface != NULL)
860 cairo_surface_destroy(window->cairo_surface);
861
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500862 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400863}
864
Benjamin Franzke22d54812011-07-16 19:50:32 +0000865#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100866static void
867window_resize_cairo_window_surface(struct window *window)
868{
869 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200870 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100871
872 data = cairo_surface_get_user_data(window->cairo_surface,
873 &surface_data_key);
874
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200875 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876 wl_egl_window_resize(data->window,
877 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200878 window->allocation.height,
879 x,y);
880
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100881 cairo_gl_surface_set_size(window->cairo_surface,
882 window->allocation.width,
883 window->allocation.height);
884}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000885#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100886
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400887struct display *
888window_get_display(struct window *window)
889{
890 return window->display;
891}
892
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400893void
894window_create_surface(struct window *window)
895{
896 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400897 uint32_t flags = 0;
898
899 if (!window->transparent)
900 flags = SURFACE_OPAQUE;
901
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400902 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500903#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
905 if (window->cairo_surface) {
906 window_resize_cairo_window_surface(window);
907 return;
908 }
909 surface = display_create_surface(window->display,
910 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400911 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500913 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400914 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400916 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400917 break;
918#endif
919 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400921 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800923 default:
924 surface = NULL;
925 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400926 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400927
928 window_set_surface(window, surface);
929 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400930}
931
932static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500933window_draw_menu(struct window *window)
934{
935 cairo_t *cr;
936 int width, height, r = 5;
937
938 window_create_surface(window);
939
940 cr = cairo_create(window->cairo_surface);
941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
942 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
943 cairo_paint(cr);
944
945 width = window->allocation.width;
946 height = window->allocation.height;
947 rounded_rect(cr, r, r, width - r, height - r, r);
948 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
949 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
950 cairo_fill(cr);
951 cairo_destroy(cr);
952}
953
954static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500955window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500956{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500957 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500958 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400959 cairo_surface_t *frame;
960 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500961
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400962 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400963
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400964 width = window->allocation.width;
965 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500966
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500967 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500968
Kristian Høgsberg09531622010-06-14 23:22:15 -0400969 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400970 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400971 cairo_paint(cr);
972
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400973 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400974 tile_mask(cr, window->display->shadow,
975 shadow_dx, shadow_dy, width, height,
976 window->margin + 10 - shadow_dx,
977 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500978
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400979 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400980 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400981 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400982 frame = window->display->inactive_frame;
983
984 tile_source(cr, frame, 0, 0, width, height,
985 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500986
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500987 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
988 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500989 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400990 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400991 if (window->keyboard_device)
992 cairo_set_source_rgb(cr, 0, 0, 0);
993 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400994 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400995 cairo_show_text(cr, window->title);
996
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500997 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400998
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100999 /* FIXME: this breakes gears, fix cairo? */
1000#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -04001001 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001002#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001003}
1004
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001005void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001006window_destroy(struct window *window)
1007{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001008 struct display *display = window->display;
1009 struct input *input;
1010
1011 if (window->redraw_scheduled)
1012 wl_list_remove(&window->redraw_task.link);
1013
1014 wl_list_for_each(input, &display->input_list, link) {
1015 if (input->pointer_focus == window)
1016 input->pointer_focus = NULL;
1017 if (input->keyboard_focus == window)
1018 input->keyboard_focus = NULL;
1019 }
1020
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001021 if (window->shell_surface)
1022 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001023 wl_surface_destroy(window->surface);
1024 wl_list_remove(&window->link);
1025 free(window);
1026}
1027
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028static struct item *
1029window_find_item(struct window *window, int32_t x, int32_t y)
1030{
1031 struct item *item;
1032
1033 wl_list_for_each(item, &window->item_list, link) {
1034 if (item->allocation.x <= x &&
1035 x < item->allocation.x + item->allocation.width &&
1036 item->allocation.y <= y &&
1037 y < item->allocation.y + item->allocation.height) {
1038 return item;
1039 }
1040 }
1041
1042 return NULL;
1043}
1044
1045struct item *
1046window_add_item(struct window *window, void *data)
1047{
1048 struct item *item;
1049
1050 item = malloc(sizeof *item);
1051 memset(item, 0, sizeof *item);
1052 item->user_data = data;
1053 wl_list_insert(window->item_list.prev, &item->link);
1054
1055 return item;
1056}
1057
1058void
1059window_for_each_item(struct window *window, item_func_t func, void *data)
1060{
1061 struct item *item;
1062
1063 wl_list_for_each(item, &window->item_list, link)
1064 func(item, data);
1065}
1066
1067struct item *
1068window_get_focus_item(struct window *window)
1069{
1070 return window->focus_item;
1071}
1072
1073void
1074item_get_allocation(struct item *item, struct rectangle *allocation)
1075{
1076 *allocation = item->allocation;
1077}
1078
1079void
1080item_set_allocation(struct item *item,
1081 int32_t x, int32_t y, int32_t width, int32_t height)
1082{
1083 item->allocation.x = x;
1084 item->allocation.y = y;
1085 item->allocation.width = width;
1086 item->allocation.height = height;
1087}
1088
1089void *
1090item_get_user_data(struct item *item)
1091{
1092 return item->user_data;
1093}
1094
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001095void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001096window_draw(struct window *window)
1097{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001098 if (window->parent)
1099 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001100 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001101 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001102 else
1103 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001104}
1105
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001106cairo_surface_t *
1107window_get_surface(struct window *window)
1108{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001109 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001110}
1111
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001112struct wl_surface *
1113window_get_wl_surface(struct window *window)
1114{
1115 return window->surface;
1116}
1117
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001118struct wl_shell_surface *
1119window_get_wl_shell_surface(struct window *window)
1120{
1121 return window->shell_surface;
1122}
1123
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124static int
1125get_pointer_location(struct window *window, int32_t x, int32_t y)
1126{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001128 const int grip_size = 8;
1129
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001130 if (!window->decoration)
1131 return WINDOW_CLIENT_AREA;
1132
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001133 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001134 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001135 else if (window->margin <= x && x < window->margin + grip_size)
1136 hlocation = WINDOW_RESIZING_LEFT;
1137 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001138 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139 else if (x < window->allocation.width - window->margin)
1140 hlocation = WINDOW_RESIZING_RIGHT;
1141 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143
1144 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001145 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001146 else if (window->margin <= y && y < window->margin + grip_size)
1147 vlocation = WINDOW_RESIZING_TOP;
1148 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001149 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001150 else if (y < window->allocation.height - window->margin)
1151 vlocation = WINDOW_RESIZING_BOTTOM;
1152 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001153 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001155 location = vlocation | hlocation;
1156 if (location & WINDOW_EXTERIOR)
1157 location = WINDOW_EXTERIOR;
1158 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1159 location = WINDOW_TITLEBAR;
1160 else if (location == WINDOW_INTERIOR)
1161 location = WINDOW_CLIENT_AREA;
1162
1163 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001164}
1165
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001166static int
1167input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001169 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001170
1171 location = get_pointer_location(input->pointer_focus,
1172 input->sx, input->sy);
1173 switch (location) {
1174 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001175 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001176 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001177 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001178 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001179 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001180 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001181 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001182 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001183 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001184 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001185 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001186 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001187 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001188 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001189 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001190 case WINDOW_EXTERIOR:
1191 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001192 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001194 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001195 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001196}
1197
1198void
1199input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1200{
1201 struct display *display = input->display;
1202 struct wl_buffer *buffer;
1203 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001204
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001205 if (pointer == input->current_pointer_image)
1206 return;
1207
1208 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001209 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001210
1211 if (!surface)
1212 return;
1213
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001214 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001215 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001216 pointer_images[pointer].hotspot_x,
1217 pointer_images[pointer].hotspot_y);
1218}
1219
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001220static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001221window_set_focus_item(struct window *window, struct item *focus)
1222{
1223 void *data;
1224
1225 if (focus == window->focus_item)
1226 return;
1227
1228 window->focus_item = focus;
1229 data = focus ? focus->user_data : NULL;
1230 if (window->item_focus_handler)
1231 window->item_focus_handler(window, focus, data);
1232}
1233
1234static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001235window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001236 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001237 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001238{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001239 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001240 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001241 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001242 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001243
1244 input->x = x;
1245 input->y = y;
1246 input->sx = sx;
1247 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001249 if (!window->focus_item || !window->item_grab_button) {
1250 item = window_find_item(window, sx, sy);
1251 window_set_focus_item(window, item);
1252 }
1253
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 if (window->motion_handler)
1255 pointer = (*window->motion_handler)(window, input, time,
1256 x, y, sx, sy,
1257 window->user_data);
1258
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001259 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001260 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001261}
1262
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001263static void
1264window_handle_button(void *data,
1265 struct wl_input_device *input_device,
1266 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001267{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001268 struct input *input = data;
1269 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001270 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001272
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001273 if (window->focus_item && window->item_grab_button == 0 && state)
1274 window->item_grab_button = button;
1275
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001277
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001278 if (window->display->shell &&
1279 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001282 if (!window->shell_surface)
1283 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001284 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001285 wl_shell_surface_move(window->shell_surface,
1286 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001287 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001288 case WINDOW_RESIZING_TOP:
1289 case WINDOW_RESIZING_BOTTOM:
1290 case WINDOW_RESIZING_LEFT:
1291 case WINDOW_RESIZING_RIGHT:
1292 case WINDOW_RESIZING_TOP_LEFT:
1293 case WINDOW_RESIZING_TOP_RIGHT:
1294 case WINDOW_RESIZING_BOTTOM_LEFT:
1295 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001296 if (!window->shell_surface)
1297 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001298 wl_shell_surface_resize(window->shell_surface,
1299 input_device, time,
1300 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001301 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001302 case WINDOW_CLIENT_AREA:
1303 if (window->button_handler)
1304 (*window->button_handler)(window,
1305 input, time,
1306 button, state,
1307 window->user_data);
1308 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001309 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001310 } else {
1311 if (window->button_handler)
1312 (*window->button_handler)(window,
1313 input, time,
1314 button, state,
1315 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001316 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001317
1318 if (window->focus_item &&
1319 window->item_grab_button == button && !state) {
1320 window->item_grab_button = 0;
1321 item = window_find_item(window, input->sx, input->sy);
1322 window_set_focus_item(window, item);
1323 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001324}
1325
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001326static void
1327window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001328 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001329{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001330 struct input *input = data;
1331 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001332 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001333 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001334
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001335 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001336 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001337 return;
1338
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001339 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001340 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001341 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1342 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001343
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001344 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1345
1346 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001347 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001348 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001349 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001350
1351 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001352 (*window->key_handler)(window, input, time, key, sym, state,
1353 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001354}
1355
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001356static void
1357window_handle_pointer_focus(void *data,
1358 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001359 uint32_t time, struct wl_surface *surface,
1360 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001361{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001362 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001363 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001364 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001365 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001366
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001367 window = input->pointer_focus;
1368 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001369 window_set_focus_item(window, NULL);
1370
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001371 if (window->leave_handler)
1372 window->leave_handler(window, input,
1373 time, window->user_data);
1374 input->pointer_focus = NULL;
1375 input->current_pointer_image = POINTER_UNSET;
1376 }
1377
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001378 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001379 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001380 window = input->pointer_focus;
1381
Kristian Høgsberg59826582011-01-20 11:56:57 -05001382 input->x = x;
1383 input->y = y;
1384 input->sx = sx;
1385 input->sy = sy;
1386
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001387 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001388 if (window->enter_handler)
1389 pointer = window->enter_handler(window, input,
1390 time, sx, sy,
1391 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001392
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001393 item = window_find_item(window, x, y);
1394 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001395
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001396 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001397 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001398 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001399}
1400
1401static void
1402window_handle_keyboard_focus(void *data,
1403 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001404 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001405 struct wl_surface *surface,
1406 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001407{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001408 struct input *input = data;
1409 struct window *window = input->keyboard_focus;
1410 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001411 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001412
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001414 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001415 if (window->keyboard_focus_handler)
1416 (*window->keyboard_focus_handler)(window, NULL,
1417 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001418 }
1419
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001420 if (surface)
1421 input->keyboard_focus = wl_surface_get_user_data(surface);
1422 else
1423 input->keyboard_focus = NULL;
1424
1425 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001426 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001427 for (k = keys->data; k < end; k++)
1428 input->modifiers |= d->xkb->map->modmap[*k];
1429
1430 window = input->keyboard_focus;
1431 if (window) {
1432 window->keyboard_device = input;
1433 if (window->keyboard_focus_handler)
1434 (*window->keyboard_focus_handler)(window,
1435 window->keyboard_device,
1436 window->user_data);
1437 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001438}
1439
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001440static const struct wl_input_device_listener input_device_listener = {
1441 window_handle_motion,
1442 window_handle_button,
1443 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001444 window_handle_pointer_focus,
1445 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001446};
1447
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001448void
1449input_get_position(struct input *input, int32_t *x, int32_t *y)
1450{
1451 *x = input->sx;
1452 *y = input->sy;
1453}
1454
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001455struct wl_input_device *
1456input_get_input_device(struct input *input)
1457{
1458 return input->input_device;
1459}
1460
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001461uint32_t
1462input_get_modifiers(struct input *input)
1463{
1464 return input->modifiers;
1465}
1466
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001467struct data_offer {
1468 struct wl_data_offer *offer;
1469 struct input *input;
1470 struct wl_array types;
1471 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001472
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001473 struct task io_task;
1474 int fd;
1475 data_func_t func;
1476 int32_t x, y;
1477 void *user_data;
1478};
1479
1480static void
1481data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1482{
1483 struct data_offer *offer = data;
1484 char **p;
1485
1486 p = wl_array_add(&offer->types, sizeof *p);
1487 *p = strdup(type);
1488}
1489
1490static const struct wl_data_offer_listener data_offer_listener = {
1491 data_offer_offer,
1492};
1493
1494static void
1495data_offer_destroy(struct data_offer *offer)
1496{
1497 char **p;
1498
1499 offer->refcount--;
1500 if (offer->refcount == 0) {
1501 wl_data_offer_destroy(offer->offer);
1502 for (p = offer->types.data; *p; p++)
1503 free(*p);
1504 wl_array_release(&offer->types);
1505 free(offer);
1506 }
1507}
1508
1509static void
1510data_device_data_offer(void *data,
1511 struct wl_data_device *data_device, uint32_t id)
1512{
1513 struct data_offer *offer;
1514
1515 offer = malloc(sizeof *offer);
1516
1517 wl_array_init(&offer->types);
1518 offer->refcount = 1;
1519 offer->input = data;
1520
1521 /* FIXME: Generate typesafe wrappers for this */
1522 offer->offer = (struct wl_data_offer *)
1523 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1524 id, &wl_data_offer_interface);
1525
1526 wl_data_offer_add_listener(offer->offer,
1527 &data_offer_listener, offer);
1528}
1529
1530static void
1531data_device_enter(void *data, struct wl_data_device *data_device,
1532 uint32_t time, struct wl_surface *surface,
1533 int32_t x, int32_t y, struct wl_data_offer *offer)
1534{
1535 struct input *input = data;
1536 struct window *window;
1537 char **p;
1538
1539 input->drag_offer = wl_data_offer_get_user_data(offer);
1540 window = wl_surface_get_user_data(surface);
1541 input->pointer_focus = window;
1542
1543 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1544 *p = NULL;
1545
1546 window = input->pointer_focus;
1547 if (window->data_handler)
1548 window->data_handler(window, input, time, x, y,
1549 input->drag_offer->types.data,
1550 window->user_data);
1551}
1552
1553static void
1554data_device_leave(void *data, struct wl_data_device *data_device)
1555{
1556 struct input *input = data;
1557
1558 data_offer_destroy(input->drag_offer);
1559 input->drag_offer = NULL;
1560}
1561
1562static void
1563data_device_motion(void *data, struct wl_data_device *data_device,
1564 uint32_t time, int32_t x, int32_t y)
1565{
1566 struct input *input = data;
1567 struct window *window = input->pointer_focus;
1568
1569 input->sx = x;
1570 input->sy = y;
1571
1572 if (window->data_handler)
1573 window->data_handler(window, input, time, x, y,
1574 input->drag_offer->types.data,
1575 window->user_data);
1576}
1577
1578static void
1579data_device_drop(void *data, struct wl_data_device *data_device)
1580{
1581 struct input *input = data;
1582 struct window *window = input->pointer_focus;
1583
1584 if (window->drop_handler)
1585 window->drop_handler(window, input,
1586 input->sx, input->sy, window->user_data);
1587}
1588
1589static void
1590data_device_selection(void *data,
1591 struct wl_data_device *wl_data_device,
1592 struct wl_data_offer *offer)
1593{
1594 struct input *input = data;
1595 char **p;
1596
1597 if (input->selection_offer)
1598 data_offer_destroy(input->selection_offer);
1599
1600 input->selection_offer = wl_data_offer_get_user_data(offer);
1601 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1602 *p = NULL;
1603}
1604
1605static const struct wl_data_device_listener data_device_listener = {
1606 data_device_data_offer,
1607 data_device_enter,
1608 data_device_leave,
1609 data_device_motion,
1610 data_device_drop,
1611 data_device_selection
1612};
1613
1614struct wl_data_device *
1615input_get_data_device(struct input *input)
1616{
1617 return input->data_device;
1618}
1619
1620void
1621input_set_selection(struct input *input,
1622 struct wl_data_source *source, uint32_t time)
1623{
1624 wl_data_device_set_selection(input->data_device, source, time);
1625}
1626
1627void
1628input_accept(struct input *input, uint32_t time, const char *type)
1629{
1630 wl_data_offer_accept(input->drag_offer->offer, time, type);
1631}
1632
1633static void
1634offer_io_func(struct task *task, uint32_t events)
1635{
1636 struct data_offer *offer =
1637 container_of(task, struct data_offer, io_task);
1638 unsigned int len;
1639 char buffer[4096];
1640
1641 len = read(offer->fd, buffer, sizeof buffer);
1642 offer->func(buffer, len,
1643 offer->x, offer->y, offer->user_data);
1644
1645 if (len == 0) {
1646 close(offer->fd);
1647 data_offer_destroy(offer);
1648 }
1649}
1650
1651static void
1652data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1653 data_func_t func, void *user_data)
1654{
1655 int p[2];
1656
1657 pipe2(p, O_CLOEXEC);
1658 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1659 close(p[1]);
1660
1661 offer->io_task.run = offer_io_func;
1662 offer->fd = p[0];
1663 offer->func = func;
1664 offer->refcount++;
1665 offer->user_data = user_data;
1666
1667 display_watch_fd(offer->input->display,
1668 offer->fd, EPOLLIN, &offer->io_task);
1669}
1670
1671void
1672input_receive_drag_data(struct input *input, const char *mime_type,
1673 data_func_t func, void *data)
1674{
1675 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1676 input->drag_offer->x = input->sx;
1677 input->drag_offer->y = input->sy;
1678}
1679
1680int
1681input_receive_selection_data(struct input *input, const char *mime_type,
1682 data_func_t func, void *data)
1683{
1684 char **p;
1685
1686 if (input->selection_offer == NULL)
1687 return -1;
1688
1689 for (p = input->selection_offer->types.data; *p; p++)
1690 if (strcmp(mime_type, *p) == 0)
1691 break;
1692
1693 if (*p == NULL)
1694 return -1;
1695
1696 data_offer_receive_data(input->selection_offer,
1697 mime_type, func, data);
1698 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001699}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001700
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001701void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001702window_move(struct window *window, struct input *input, uint32_t time)
1703{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001704 if (!window->shell_surface)
1705 return;
1706
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001707 wl_shell_surface_move(window->shell_surface,
1708 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001709}
1710
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001711static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001713 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001714 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001715{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001717 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001718
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001719 /* FIXME: this is probably the wrong place to check for width
1720 * or height <= 0, but it prevents the compositor from crashing
1721 */
1722 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001723 return;
1724
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001725 window->resize_edges = edges;
1726
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001727 if (window->resize_handler) {
1728 child_width = width - 20 - window->margin * 2;
1729 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001730
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001731 (*window->resize_handler)(window,
1732 child_width, child_height,
1733 window->user_data);
1734 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001735 window->allocation.width = width;
1736 window->allocation.height = height;
1737
1738 if (window->redraw_handler)
1739 window_schedule_redraw(window);
1740 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001741}
1742
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001743static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001744 handle_configure,
1745};
1746
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001747void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001748window_get_allocation(struct window *window,
1749 struct rectangle *allocation)
1750{
1751 *allocation = window->allocation;
1752}
1753
1754void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001755window_get_child_allocation(struct window *window,
1756 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001757{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001758 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001759 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001760 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001761 allocation->x = window->margin + 10;
1762 allocation->y = window->margin + 50;
1763 allocation->width =
1764 window->allocation.width - 20 - window->margin * 2;
1765 allocation->height =
1766 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001767 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001768}
1769
1770void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001771window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001772{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001773 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001774 window->allocation.x = 20 + window->margin;
1775 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001776 window->allocation.width = width + 20 + window->margin * 2;
1777 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001778 } else {
1779 window->allocation.x = 0;
1780 window->allocation.y = 0;
1781 window->allocation.width = width;
1782 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001783 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001784}
1785
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001786static void
1787idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001788{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001789 struct window *window =
1790 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001791
1792 window->redraw_handler(window, window->user_data);
1793 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001794}
1795
1796void
1797window_schedule_redraw(struct window *window)
1798{
1799 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001800 window->redraw_task.run = idle_redraw;
1801 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001802 window->redraw_scheduled = 1;
1803 }
1804}
1805
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001806void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001807window_set_custom(struct window *window)
1808{
1809 window->type = TYPE_CUSTOM;
1810}
1811
1812void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001813window_set_fullscreen(struct window *window, int fullscreen)
1814{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001815 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001816 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001817
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001818 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001819 return;
1820
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001821 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001822 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001823 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001824 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001825 width = output->allocation.width;
1826 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001827 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001828 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001829 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001830 width = window->saved_allocation.width - 20 - window->margin * 2;
1831 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001832 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001833 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001834
1835 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001836}
1837
1838void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001839window_set_decoration(struct window *window, int decoration)
1840{
1841 window->decoration = decoration;
1842}
1843
1844void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001845window_set_user_data(struct window *window, void *data)
1846{
1847 window->user_data = data;
1848}
1849
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001850void *
1851window_get_user_data(struct window *window)
1852{
1853 return window->user_data;
1854}
1855
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001856void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001857window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001858 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001859{
1860 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001861}
1862
1863void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001864window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001865 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001866{
1867 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001868}
1869
1870void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001871window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001872 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001873{
1874 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001875}
1876
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001877void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001878window_set_button_handler(struct window *window,
1879 window_button_handler_t handler)
1880{
1881 window->button_handler = handler;
1882}
1883
1884void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001885window_set_motion_handler(struct window *window,
1886 window_motion_handler_t handler)
1887{
1888 window->motion_handler = handler;
1889}
1890
1891void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001892window_set_enter_handler(struct window *window,
1893 window_enter_handler_t handler)
1894{
1895 window->enter_handler = handler;
1896}
1897
1898void
1899window_set_leave_handler(struct window *window,
1900 window_leave_handler_t handler)
1901{
1902 window->leave_handler = handler;
1903}
1904
1905void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001906window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001907 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001908{
1909 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001910}
1911
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001912void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001913window_set_item_focus_handler(struct window *window,
1914 window_item_focus_handler_t handler)
1915{
1916 window->item_focus_handler = handler;
1917}
1918
1919void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001920window_set_data_handler(struct window *window, window_data_handler_t handler)
1921{
1922 window->data_handler = handler;
1923}
1924
1925void
1926window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1927{
1928 window->drop_handler = handler;
1929}
1930
1931void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001932window_set_transparent(struct window *window, int transparent)
1933{
1934 window->transparent = transparent;
1935}
1936
1937void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001938window_set_title(struct window *window, const char *title)
1939{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001940 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001941 window->title = strdup(title);
1942}
1943
1944const char *
1945window_get_title(struct window *window)
1946{
1947 return window->title;
1948}
1949
1950void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001951display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1952 int32_t x, int32_t y, int32_t width, int32_t height)
1953{
1954 struct wl_buffer *buffer;
1955
1956 buffer = display_get_buffer_for_surface(display, cairo_surface);
1957
1958 wl_buffer_damage(buffer, x, y, width, height);
1959}
1960
1961void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001962window_damage(struct window *window, int32_t x, int32_t y,
1963 int32_t width, int32_t height)
1964{
1965 wl_surface_damage(window->surface, x, y, width, height);
1966}
1967
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001968static struct window *
1969window_create_internal(struct display *display, struct window *parent,
1970 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001971{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001972 struct window *window;
1973
1974 window = malloc(sizeof *window);
1975 if (window == NULL)
1976 return NULL;
1977
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001978 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001979 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001980 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001981 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001982 if (display->shell) {
1983 window->shell_surface =
1984 wl_shell_get_shell_surface(display->shell,
1985 window->surface);
1986 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001987 window->allocation.x = 0;
1988 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001989 window->allocation.width = width;
1990 window->allocation.height = height;
1991 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001992 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001993 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001994 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001995 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001996
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001997 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001998#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001999 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2000 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002001#else
2002 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2003#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002004 else
2005 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002006
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002007 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002008 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002009
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002010 if (window->shell_surface) {
2011 wl_shell_surface_set_user_data(window->shell_surface, window);
2012 wl_shell_surface_add_listener(window->shell_surface,
2013 &shell_surface_listener, window);
2014 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002015
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002016 return window;
2017}
2018
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002019struct window *
2020window_create(struct display *display, int32_t width, int32_t height)
2021{
2022 struct window *window;
2023
2024 window = window_create_internal(display, NULL, width, height);
2025 if (!window)
2026 return NULL;
2027
2028 return window;
2029}
2030
2031struct window *
2032window_create_transient(struct display *display, struct window *parent,
2033 int32_t x, int32_t y, int32_t width, int32_t height)
2034{
2035 struct window *window;
2036
2037 window = window_create_internal(parent->display,
2038 parent, width, height);
2039 if (!window)
2040 return NULL;
2041
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002042 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002043 window->x = x;
2044 window->y = y;
2045
2046 return window;
2047}
2048
2049void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002050window_set_buffer_type(struct window *window, enum window_buffer_type type)
2051{
2052 window->buffer_type = type;
2053}
2054
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002055
2056static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002057display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002058 struct wl_output *wl_output,
2059 int x, int y,
2060 int physical_width,
2061 int physical_height,
2062 int subpixel,
2063 const char *make,
2064 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002065{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002066 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002067
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002068 output->allocation.x = x;
2069 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002070}
2071
2072static void
2073display_handle_mode(void *data,
2074 struct wl_output *wl_output,
2075 uint32_t flags,
2076 int width,
2077 int height,
2078 int refresh)
2079{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002080 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002081 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002082
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002083 if (flags & WL_OUTPUT_MODE_CURRENT) {
2084 output->allocation.width = width;
2085 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002086 if (display->output_configure_handler)
2087 (*display->output_configure_handler)(
2088 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002089 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002090}
2091
2092static const struct wl_output_listener output_listener = {
2093 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002094 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002095};
2096
2097static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002098display_add_output(struct display *d, uint32_t id)
2099{
2100 struct output *output;
2101
2102 output = malloc(sizeof *output);
2103 if (output == NULL)
2104 return;
2105
2106 memset(output, 0, sizeof *output);
2107 output->display = d;
2108 output->output =
2109 wl_display_bind(d->display, id, &wl_output_interface);
2110 wl_list_insert(d->output_list.prev, &output->link);
2111
2112 wl_output_add_listener(output->output, &output_listener, output);
2113}
2114
2115void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002116display_set_output_configure_handler(struct display *display,
2117 display_output_handler_t handler)
2118{
2119 struct output *output;
2120
2121 display->output_configure_handler = handler;
2122 if (!handler)
2123 return;
2124
2125 wl_list_for_each(output, &display->output_list, link)
2126 (*display->output_configure_handler)(output,
2127 display->user_data);
2128}
2129
2130void
2131output_set_user_data(struct output *output, void *data)
2132{
2133 output->user_data = data;
2134}
2135
2136void *
2137output_get_user_data(struct output *output)
2138{
2139 return output->user_data;
2140}
2141
2142void
2143output_set_destroy_handler(struct output *output,
2144 display_output_handler_t handler)
2145{
2146 output->destroy_handler = handler;
2147 /* FIXME: implement this, once we have way to remove outputs */
2148}
2149
2150void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002151output_get_allocation(struct output *output, struct rectangle *allocation)
2152{
2153 *allocation = output->allocation;
2154}
2155
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002156struct wl_output *
2157output_get_wl_output(struct output *output)
2158{
2159 return output->output;
2160}
2161
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002162static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002163display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002164{
2165 struct input *input;
2166
2167 input = malloc(sizeof *input);
2168 if (input == NULL)
2169 return;
2170
2171 memset(input, 0, sizeof *input);
2172 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002173 input->input_device =
2174 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002175 input->pointer_focus = NULL;
2176 input->keyboard_focus = NULL;
2177 wl_list_insert(d->input_list.prev, &input->link);
2178
2179 wl_input_device_add_listener(input->input_device,
2180 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002181 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002182
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002183 input->data_device =
2184 wl_data_device_manager_get_data_device(d->data_device_manager,
2185 input->input_device);
2186 wl_data_device_add_listener(input->data_device,
2187 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002188}
2189
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002190static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002191display_handle_global(struct wl_display *display, uint32_t id,
2192 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002193{
2194 struct display *d = data;
2195
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002196 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002197 d->compositor =
2198 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002199 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002200 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002201 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002202 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002203 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002204 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002205 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002206 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002207 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2208 d->data_device_manager =
2209 wl_display_bind(display, id,
2210 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002211 }
2212}
2213
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002214static void
2215display_render_frame(struct display *d)
2216{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002217 int radius = 8;
2218 cairo_t *cr;
2219
2220 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2221 cr = cairo_create(d->shadow);
2222 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2223 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2224 rounded_rect(cr, 16, 16, 112, 112, radius);
2225 cairo_fill(cr);
2226 cairo_destroy(cr);
2227 blur_surface(d->shadow, 64);
2228
2229 d->active_frame =
2230 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2231 cr = cairo_create(d->active_frame);
2232 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2233 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2234 rounded_rect(cr, 16, 16, 112, 112, radius);
2235 cairo_fill(cr);
2236 cairo_destroy(cr);
2237
2238 d->inactive_frame =
2239 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2240 cr = cairo_create(d->inactive_frame);
2241 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2242 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2243 rounded_rect(cr, 16, 16, 112, 112, radius);
2244 cairo_fill(cr);
2245 cairo_destroy(cr);
2246}
2247
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002248static void
2249init_xkb(struct display *d)
2250{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002251 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002252
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002253 names.rules = "evdev";
2254 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002255 names.layout = option_xkb_layout;
2256 names.variant = option_xkb_variant;
2257 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002258
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002259 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002260 if (!d->xkb) {
2261 fprintf(stderr, "Failed to compile keymap\n");
2262 exit(1);
2263 }
2264}
2265
Yuval Fledel45568f62010-12-06 09:18:12 -05002266static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002267init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002268{
2269 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002270 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002271
2272 static const EGLint premul_argb_cfg_attribs[] = {
2273 EGL_SURFACE_TYPE,
2274 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2275 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002276 EGL_RED_SIZE, 1,
2277 EGL_GREEN_SIZE, 1,
2278 EGL_BLUE_SIZE, 1,
2279 EGL_ALPHA_SIZE, 1,
2280 EGL_DEPTH_SIZE, 1,
2281 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2282 EGL_NONE
2283 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002284
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002285 static const EGLint rgb_cfg_attribs[] = {
2286 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2287 EGL_RED_SIZE, 1,
2288 EGL_GREEN_SIZE, 1,
2289 EGL_BLUE_SIZE, 1,
2290 EGL_ALPHA_SIZE, 0,
2291 EGL_DEPTH_SIZE, 1,
2292 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2293 EGL_NONE
2294 };
2295
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002296 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002297 if (!eglInitialize(d->dpy, &major, &minor)) {
2298 fprintf(stderr, "failed to initialize display\n");
2299 return -1;
2300 }
2301
2302 if (!eglBindAPI(EGL_OPENGL_API)) {
2303 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2304 return -1;
2305 }
2306
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002307 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002308 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002309 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002310 return -1;
2311 }
2312
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002313 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2314 &d->rgb_config, 1, &n) || n != 1) {
2315 fprintf(stderr, "failed to choose rgb config\n");
2316 return -1;
2317 }
2318
Benjamin Franzke0c991632011-09-27 21:57:31 +02002319 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2320 if (d->rgb_ctx == NULL) {
2321 fprintf(stderr, "failed to create context\n");
2322 return -1;
2323 }
2324 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2325 EGL_NO_CONTEXT, NULL);
2326 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002327 fprintf(stderr, "failed to create context\n");
2328 return -1;
2329 }
2330
Benjamin Franzke0c991632011-09-27 21:57:31 +02002331 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002332 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002333 return -1;
2334 }
2335
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002336#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002337 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2338 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002339 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002340 return -1;
2341 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002342 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2343 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2344 fprintf(stderr, "failed to get cairo egl argb device\n");
2345 return -1;
2346 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002347#endif
2348
2349 return 0;
2350}
2351
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002352static int
2353event_mask_update(uint32_t mask, void *data)
2354{
2355 struct display *d = data;
2356
2357 d->mask = mask;
2358
2359 return 0;
2360}
2361
2362static void
2363handle_display_data(struct task *task, uint32_t events)
2364{
2365 struct display *display =
2366 container_of(task, struct display, display_task);
2367
2368 wl_display_iterate(display->display, display->mask);
2369}
2370
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002371struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002372display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002373{
2374 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002375 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002376 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002377 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002378
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002379 g_type_init();
2380
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002381 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002382 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002383 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002384
2385 xkb_option_group = g_option_group_new("xkb",
2386 "Keyboard options",
2387 "Show all XKB options",
2388 NULL, NULL);
2389 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2390 g_option_context_add_group (context, xkb_option_group);
2391
2392 if (!g_option_context_parse(context, argc, argv, &error)) {
2393 fprintf(stderr, "option parsing failed: %s\n", error->message);
2394 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002395 }
2396
Tim Wiederhake748f6722011-01-23 23:25:25 +01002397 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002398
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002399 d = malloc(sizeof *d);
2400 if (d == NULL)
2401 return NULL;
2402
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002403 memset(d, 0, sizeof *d);
2404
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002405 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002406 if (d->display == NULL) {
2407 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002408 return NULL;
2409 }
2410
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002411 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2412 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2413 d->display_task.run = handle_display_data;
2414 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2415
2416 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002417 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002418 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002419
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002420 /* Set up listener so we'll catch all events. */
2421 wl_display_add_global_listener(d->display,
2422 display_handle_global, d);
2423
2424 /* Process connection events. */
2425 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002426 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002427 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002428
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002429 d->image_target_texture_2d =
2430 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2431 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2432 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2433
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002434 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002435
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002436 display_render_frame(d);
2437
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002438 wl_list_init(&d->window_list);
2439
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002440 init_xkb(d);
2441
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002442 return d;
2443}
2444
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002445void
2446display_set_user_data(struct display *display, void *data)
2447{
2448 display->user_data = data;
2449}
2450
2451void *
2452display_get_user_data(struct display *display)
2453{
2454 return display->user_data;
2455}
2456
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002457struct wl_display *
2458display_get_display(struct display *display)
2459{
2460 return display->display;
2461}
2462
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002463struct output *
2464display_get_output(struct display *display)
2465{
2466 return container_of(display->output_list.next, struct output, link);
2467}
2468
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002469struct wl_compositor *
2470display_get_compositor(struct display *display)
2471{
2472 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002473}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002474
2475EGLDisplay
2476display_get_egl_display(struct display *d)
2477{
2478 return d->dpy;
2479}
2480
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002481struct wl_data_source *
2482display_create_data_source(struct display *display)
2483{
2484 return wl_data_device_manager_create_data_source(display->data_device_manager);
2485}
2486
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002487EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002488display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002489{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002490 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002491}
2492
Benjamin Franzke0c991632011-09-27 21:57:31 +02002493EGLConfig
2494display_get_argb_egl_config(struct display *d)
2495{
2496 return d->premultiplied_argb_config;
2497}
2498
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002499struct wl_shell *
2500display_get_shell(struct display *display)
2501{
2502 return display->shell;
2503}
2504
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002505int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002506display_acquire_window_surface(struct display *display,
2507 struct window *window,
2508 EGLContext ctx)
2509{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002510#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002511 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002512 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002513
2514 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002515 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002516 device = cairo_surface_get_device(window->cairo_surface);
2517 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002518 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002519
Benjamin Franzke0c991632011-09-27 21:57:31 +02002520 if (!ctx) {
2521 if (device == display->rgb_device)
2522 ctx = display->rgb_ctx;
2523 else if (device == display->argb_device)
2524 ctx = display->argb_ctx;
2525 else
2526 assert(0);
2527 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002528
2529 data = cairo_surface_get_user_data(window->cairo_surface,
2530 &surface_data_key);
2531
Benjamin Franzke0c991632011-09-27 21:57:31 +02002532 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002533 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2534 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002535
2536 return 0;
2537#else
2538 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002539#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002540}
2541
2542void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002543display_release_window_surface(struct display *display,
2544 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002545{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002546#ifdef HAVE_CAIRO_EGL
2547 cairo_device_t *device;
2548
2549 device = cairo_surface_get_device(window->cairo_surface);
2550 if (!device)
2551 return;
2552
2553 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002554 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002555 cairo_device_release(device);
2556#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002557}
2558
2559void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002560display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002561{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002562 wl_list_insert(&display->deferred_list, &task->link);
2563}
2564
2565void
2566display_watch_fd(struct display *display,
2567 int fd, uint32_t events, struct task *task)
2568{
2569 struct epoll_event ep;
2570
2571 ep.events = events;
2572 ep.data.ptr = task;
2573 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2574}
2575
2576void
2577display_run(struct display *display)
2578{
2579 struct task *task;
2580 struct epoll_event ep[16];
2581 int i, count;
2582
2583 while (1) {
2584 while (display->mask & WL_DISPLAY_WRITABLE)
2585 wl_display_iterate(display->display,
2586 WL_DISPLAY_WRITABLE);
2587
2588 count = epoll_wait(display->epoll_fd,
2589 ep, ARRAY_LENGTH(ep), -1);
2590 for (i = 0; i < count; i++) {
2591 task = ep[i].data.ptr;
2592 task->run(task, ep[i].events);
2593 }
2594
2595 while (!wl_list_empty(&display->deferred_list)) {
2596 task = container_of(display->deferred_list.next,
2597 struct task, link);
2598 wl_list_remove(&task->link);
2599 task->run(task, 0);
2600 }
2601 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002602}