blob: bead68f6e88658a2049a56b417839f6cec67327c [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øgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Pekka Paalanen826d7952011-12-15 10:14:07 +020081 int running;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050085 struct wl_list output_list;
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,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500119 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400122 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400125 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500126
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;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500775 case TYPE_MENU:
776 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400777 case TYPE_CUSTOM:
778 break;
779 }
780}
781
782static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500783window_attach_surface(struct window *window)
784{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400785 struct display *display = window->display;
786 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400787 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000788#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000790#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500791 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400793 if (display->shell)
794 window_set_type(window);
795
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100796 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
799 data = cairo_surface_get_user_data(window->cairo_surface,
800 &surface_data_key);
801
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802 cairo_gl_surface_swapbuffers(window->cairo_surface);
803 wl_egl_window_get_attached_size(data->window,
804 &window->server_allocation.width,
805 &window->server_allocation.height);
806 break;
807 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000808#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200810 window_get_resize_dx_dy(window, &x, &y);
811
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 if (window->pending_surface != NULL)
813 return;
814
815 window->pending_surface = window->cairo_surface;
816 window->cairo_surface = NULL;
817
818 buffer =
819 display_get_buffer_for_surface(display,
820 window->pending_surface);
821
822 wl_surface_attach(window->surface, buffer, x, y);
823 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400824 cb = wl_display_sync(display->display);
825 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000827 default:
828 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500830
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500831 wl_surface_damage(window->surface, 0, 0,
832 window->allocation.width,
833 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500834}
835
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500836void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400837window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500838{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100839 if (window->cairo_surface) {
840 switch (window->buffer_type) {
841 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
842 case WINDOW_BUFFER_TYPE_SHM:
843 display_surface_damage(window->display,
844 window->cairo_surface,
845 0, 0,
846 window->allocation.width,
847 window->allocation.height);
848 break;
849 default:
850 break;
851 }
852 window_attach_surface(window);
853 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500854}
855
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400856void
857window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400858{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500859 cairo_surface_reference(surface);
860
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400861 if (window->cairo_surface != NULL)
862 cairo_surface_destroy(window->cairo_surface);
863
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500864 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400865}
866
Benjamin Franzke22d54812011-07-16 19:50:32 +0000867#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100868static void
869window_resize_cairo_window_surface(struct window *window)
870{
871 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200872 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100873
874 data = cairo_surface_get_user_data(window->cairo_surface,
875 &surface_data_key);
876
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200877 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100878 wl_egl_window_resize(data->window,
879 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200880 window->allocation.height,
881 x,y);
882
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100883 cairo_gl_surface_set_size(window->cairo_surface,
884 window->allocation.width,
885 window->allocation.height);
886}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000887#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100888
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400889struct display *
890window_get_display(struct window *window)
891{
892 return window->display;
893}
894
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400895void
896window_create_surface(struct window *window)
897{
898 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400899 uint32_t flags = 0;
900
901 if (!window->transparent)
902 flags = SURFACE_OPAQUE;
903
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400904 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500905#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
907 if (window->cairo_surface) {
908 window_resize_cairo_window_surface(window);
909 return;
910 }
911 surface = display_create_surface(window->display,
912 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400913 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500915 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400916 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100917 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400918 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400919 break;
920#endif
921 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400922 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400923 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400924 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800925 default:
926 surface = NULL;
927 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400929
930 window_set_surface(window, surface);
931 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932}
933
934static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500935window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500936{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500937 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500938 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400939 cairo_surface_t *frame;
940 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500941
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400943
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400944 width = window->allocation.width;
945 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500946
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500947 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500948
Kristian Høgsberg09531622010-06-14 23:22:15 -0400949 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400950 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400951 cairo_paint(cr);
952
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400953 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400954 tile_mask(cr, window->display->shadow,
955 shadow_dx, shadow_dy, width, height,
956 window->margin + 10 - shadow_dx,
957 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500958
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400959 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400960 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400961 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400962 frame = window->display->inactive_frame;
963
964 tile_source(cr, frame, 0, 0, width, height,
965 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500966
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500967 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
968 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500969 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400970 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 if (window->keyboard_device)
972 cairo_set_source_rgb(cr, 0, 0, 0);
973 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400974 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400975 cairo_show_text(cr, window->title);
976
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500977 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500978}
979
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400980void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500981window_destroy(struct window *window)
982{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200983 struct display *display = window->display;
984 struct input *input;
985
986 if (window->redraw_scheduled)
987 wl_list_remove(&window->redraw_task.link);
988
989 wl_list_for_each(input, &display->input_list, link) {
990 if (input->pointer_focus == window)
991 input->pointer_focus = NULL;
992 if (input->keyboard_focus == window)
993 input->keyboard_focus = NULL;
994 }
995
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200996 if (window->shell_surface)
997 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500998 wl_surface_destroy(window->surface);
999 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001000
1001 if (window->cairo_surface != NULL)
1002 cairo_surface_destroy(window->cairo_surface);
1003 if (window->pending_surface != NULL)
1004 cairo_surface_destroy(window->pending_surface);
1005
1006 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001007 free(window);
1008}
1009
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010static struct item *
1011window_find_item(struct window *window, int32_t x, int32_t y)
1012{
1013 struct item *item;
1014
1015 wl_list_for_each(item, &window->item_list, link) {
1016 if (item->allocation.x <= x &&
1017 x < item->allocation.x + item->allocation.width &&
1018 item->allocation.y <= y &&
1019 y < item->allocation.y + item->allocation.height) {
1020 return item;
1021 }
1022 }
1023
1024 return NULL;
1025}
1026
1027struct item *
1028window_add_item(struct window *window, void *data)
1029{
1030 struct item *item;
1031
1032 item = malloc(sizeof *item);
1033 memset(item, 0, sizeof *item);
1034 item->user_data = data;
1035 wl_list_insert(window->item_list.prev, &item->link);
1036
1037 return item;
1038}
1039
1040void
1041window_for_each_item(struct window *window, item_func_t func, void *data)
1042{
1043 struct item *item;
1044
1045 wl_list_for_each(item, &window->item_list, link)
1046 func(item, data);
1047}
1048
1049struct item *
1050window_get_focus_item(struct window *window)
1051{
1052 return window->focus_item;
1053}
1054
1055void
1056item_get_allocation(struct item *item, struct rectangle *allocation)
1057{
1058 *allocation = item->allocation;
1059}
1060
1061void
1062item_set_allocation(struct item *item,
1063 int32_t x, int32_t y, int32_t width, int32_t height)
1064{
1065 item->allocation.x = x;
1066 item->allocation.y = y;
1067 item->allocation.width = width;
1068 item->allocation.height = height;
1069}
1070
1071void *
1072item_get_user_data(struct item *item)
1073{
1074 return item->user_data;
1075}
1076
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001077void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001078window_draw(struct window *window)
1079{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001080 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001081 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001082 else
1083 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001084}
1085
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001086cairo_surface_t *
1087window_get_surface(struct window *window)
1088{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001089 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001090}
1091
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001092struct wl_surface *
1093window_get_wl_surface(struct window *window)
1094{
1095 return window->surface;
1096}
1097
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001098struct wl_shell_surface *
1099window_get_wl_shell_surface(struct window *window)
1100{
1101 return window->shell_surface;
1102}
1103
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104static int
1105get_pointer_location(struct window *window, int32_t x, int32_t y)
1106{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108 const int grip_size = 8;
1109
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001110 if (!window->decoration)
1111 return WINDOW_CLIENT_AREA;
1112
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001113 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115 else if (window->margin <= x && x < window->margin + grip_size)
1116 hlocation = WINDOW_RESIZING_LEFT;
1117 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119 else if (x < window->allocation.width - window->margin)
1120 hlocation = WINDOW_RESIZING_RIGHT;
1121 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001122 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001123
1124 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 else if (window->margin <= y && y < window->margin + grip_size)
1127 vlocation = WINDOW_RESIZING_TOP;
1128 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001129 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001130 else if (y < window->allocation.height - window->margin)
1131 vlocation = WINDOW_RESIZING_BOTTOM;
1132 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001133 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001134
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135 location = vlocation | hlocation;
1136 if (location & WINDOW_EXTERIOR)
1137 location = WINDOW_EXTERIOR;
1138 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1139 location = WINDOW_TITLEBAR;
1140 else if (location == WINDOW_INTERIOR)
1141 location = WINDOW_CLIENT_AREA;
1142
1143 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144}
1145
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001146static int
1147input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001148{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001149 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001150
1151 location = get_pointer_location(input->pointer_focus,
1152 input->sx, input->sy);
1153 switch (location) {
1154 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001155 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001156 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001157 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001159 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001160 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001161 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001162 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001163 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001164 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001165 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001166 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001167 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001169 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001170 case WINDOW_EXTERIOR:
1171 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001172 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001174 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001175 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001176}
1177
1178void
1179input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1180{
1181 struct display *display = input->display;
1182 struct wl_buffer *buffer;
1183 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001184
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001185 if (pointer == input->current_pointer_image)
1186 return;
1187
1188 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001189 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001190
1191 if (!surface)
1192 return;
1193
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001194 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001195 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001196 pointer_images[pointer].hotspot_x,
1197 pointer_images[pointer].hotspot_y);
1198}
1199
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001200static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001201window_set_focus_item(struct window *window, struct item *focus)
1202{
1203 void *data;
1204
1205 if (focus == window->focus_item)
1206 return;
1207
1208 window->focus_item = focus;
1209 data = focus ? focus->user_data : NULL;
1210 if (window->item_focus_handler)
1211 window->item_focus_handler(window, focus, data);
1212}
1213
1214static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001215window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001216 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001217 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001218{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001219 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001220 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001221 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001222 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001223
1224 input->x = x;
1225 input->y = y;
1226 input->sx = sx;
1227 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001228
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001229 if (!window->focus_item || !window->item_grab_button) {
1230 item = window_find_item(window, sx, sy);
1231 window_set_focus_item(window, item);
1232 }
1233
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234 if (window->motion_handler)
1235 pointer = (*window->motion_handler)(window, input, time,
1236 x, y, sx, sy,
1237 window->user_data);
1238
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001239 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001240 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001241}
1242
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001243static void
1244window_handle_button(void *data,
1245 struct wl_input_device *input_device,
1246 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001247{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001248 struct input *input = data;
1249 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001250 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001252
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001253 if (window->focus_item && window->item_grab_button == 0 && state)
1254 window->item_grab_button = button;
1255
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001257
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001258 if (window->display->shell &&
1259 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001262 if (!window->shell_surface)
1263 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001264 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001265 wl_shell_surface_move(window->shell_surface,
1266 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001267 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001268 case WINDOW_RESIZING_TOP:
1269 case WINDOW_RESIZING_BOTTOM:
1270 case WINDOW_RESIZING_LEFT:
1271 case WINDOW_RESIZING_RIGHT:
1272 case WINDOW_RESIZING_TOP_LEFT:
1273 case WINDOW_RESIZING_TOP_RIGHT:
1274 case WINDOW_RESIZING_BOTTOM_LEFT:
1275 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001276 if (!window->shell_surface)
1277 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001278 wl_shell_surface_resize(window->shell_surface,
1279 input_device, time,
1280 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001281 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001282 case WINDOW_CLIENT_AREA:
1283 if (window->button_handler)
1284 (*window->button_handler)(window,
1285 input, time,
1286 button, state,
1287 window->user_data);
1288 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001289 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001290 } else {
1291 if (window->button_handler)
1292 (*window->button_handler)(window,
1293 input, time,
1294 button, state,
1295 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001296 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001297
1298 if (window->focus_item &&
1299 window->item_grab_button == button && !state) {
1300 window->item_grab_button = 0;
1301 item = window_find_item(window, input->sx, input->sy);
1302 window_set_focus_item(window, item);
1303 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001304}
1305
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001306static void
1307window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001308 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001309{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001310 struct input *input = data;
1311 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001312 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001313 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001314
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001315 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001316 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001317 return;
1318
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001319 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001320 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001321 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1322 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001323
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001324 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1325
1326 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001328 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001329 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001330
1331 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001332 (*window->key_handler)(window, input, time, key, sym, state,
1333 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001334}
1335
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001336static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02001337remove_pointer_focus(struct input *input, uint32_t time)
1338{
1339 struct window *window = input->pointer_focus;
1340
1341 if (!window)
1342 return;
1343
1344 window_set_focus_item(window, NULL);
1345
1346 if (window->leave_handler)
1347 window->leave_handler(window, input, time, window->user_data);
1348 input->pointer_focus = NULL;
1349 input->current_pointer_image = POINTER_UNSET;
1350}
1351
1352static void
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001353window_handle_pointer_focus(void *data,
1354 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001355 uint32_t time, struct wl_surface *surface,
1356 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001357{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001358 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001359 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001360 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001361 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001362
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001363 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001364 if (window && window->surface != surface)
1365 remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001366
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001367 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001368 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001369 window = input->pointer_focus;
1370
Kristian Høgsberg59826582011-01-20 11:56:57 -05001371 input->x = x;
1372 input->y = y;
1373 input->sx = sx;
1374 input->sy = sy;
1375
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001376 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001377 if (window->enter_handler)
1378 pointer = window->enter_handler(window, input,
1379 time, sx, sy,
1380 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001381
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001382 item = window_find_item(window, x, y);
1383 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001384
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001385 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001386 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001387 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001388}
1389
1390static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02001391remove_keyboard_focus(struct input *input)
1392{
1393 struct window *window = input->keyboard_focus;
1394
1395 if (!window)
1396 return;
1397
1398 window->keyboard_device = NULL;
1399 if (window->keyboard_focus_handler)
1400 (*window->keyboard_focus_handler)(window, NULL,
1401 window->user_data);
1402
1403 input->keyboard_focus = NULL;
1404}
1405
1406static void
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001407window_handle_keyboard_focus(void *data,
1408 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001409 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001410 struct wl_surface *surface,
1411 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001412{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001414 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001415 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001416 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001417
Pekka Paalanene1207c72011-12-16 12:02:09 +02001418 remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001419
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001420 if (surface)
1421 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001422
1423 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001424 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001425 for (k = keys->data; k < end; k++)
1426 input->modifiers |= d->xkb->map->modmap[*k];
1427
1428 window = input->keyboard_focus;
1429 if (window) {
1430 window->keyboard_device = input;
1431 if (window->keyboard_focus_handler)
1432 (*window->keyboard_focus_handler)(window,
1433 window->keyboard_device,
1434 window->user_data);
1435 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001436}
1437
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001438static const struct wl_input_device_listener input_device_listener = {
1439 window_handle_motion,
1440 window_handle_button,
1441 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001442 window_handle_pointer_focus,
1443 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001444};
1445
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446void
1447input_get_position(struct input *input, int32_t *x, int32_t *y)
1448{
1449 *x = input->sx;
1450 *y = input->sy;
1451}
1452
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001453struct wl_input_device *
1454input_get_input_device(struct input *input)
1455{
1456 return input->input_device;
1457}
1458
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001459uint32_t
1460input_get_modifiers(struct input *input)
1461{
1462 return input->modifiers;
1463}
1464
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001465struct data_offer {
1466 struct wl_data_offer *offer;
1467 struct input *input;
1468 struct wl_array types;
1469 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001470
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001471 struct task io_task;
1472 int fd;
1473 data_func_t func;
1474 int32_t x, y;
1475 void *user_data;
1476};
1477
1478static void
1479data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1480{
1481 struct data_offer *offer = data;
1482 char **p;
1483
1484 p = wl_array_add(&offer->types, sizeof *p);
1485 *p = strdup(type);
1486}
1487
1488static const struct wl_data_offer_listener data_offer_listener = {
1489 data_offer_offer,
1490};
1491
1492static void
1493data_offer_destroy(struct data_offer *offer)
1494{
1495 char **p;
1496
1497 offer->refcount--;
1498 if (offer->refcount == 0) {
1499 wl_data_offer_destroy(offer->offer);
1500 for (p = offer->types.data; *p; p++)
1501 free(*p);
1502 wl_array_release(&offer->types);
1503 free(offer);
1504 }
1505}
1506
1507static void
1508data_device_data_offer(void *data,
1509 struct wl_data_device *data_device, uint32_t id)
1510{
1511 struct data_offer *offer;
1512
1513 offer = malloc(sizeof *offer);
1514
1515 wl_array_init(&offer->types);
1516 offer->refcount = 1;
1517 offer->input = data;
1518
1519 /* FIXME: Generate typesafe wrappers for this */
1520 offer->offer = (struct wl_data_offer *)
1521 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1522 id, &wl_data_offer_interface);
1523
1524 wl_data_offer_add_listener(offer->offer,
1525 &data_offer_listener, offer);
1526}
1527
1528static void
1529data_device_enter(void *data, struct wl_data_device *data_device,
1530 uint32_t time, struct wl_surface *surface,
1531 int32_t x, int32_t y, struct wl_data_offer *offer)
1532{
1533 struct input *input = data;
1534 struct window *window;
1535 char **p;
1536
1537 input->drag_offer = wl_data_offer_get_user_data(offer);
1538 window = wl_surface_get_user_data(surface);
1539 input->pointer_focus = window;
1540
1541 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1542 *p = NULL;
1543
1544 window = input->pointer_focus;
1545 if (window->data_handler)
1546 window->data_handler(window, input, time, x, y,
1547 input->drag_offer->types.data,
1548 window->user_data);
1549}
1550
1551static void
1552data_device_leave(void *data, struct wl_data_device *data_device)
1553{
1554 struct input *input = data;
1555
1556 data_offer_destroy(input->drag_offer);
1557 input->drag_offer = NULL;
1558}
1559
1560static void
1561data_device_motion(void *data, struct wl_data_device *data_device,
1562 uint32_t time, int32_t x, int32_t y)
1563{
1564 struct input *input = data;
1565 struct window *window = input->pointer_focus;
1566
1567 input->sx = x;
1568 input->sy = y;
1569
1570 if (window->data_handler)
1571 window->data_handler(window, input, time, x, y,
1572 input->drag_offer->types.data,
1573 window->user_data);
1574}
1575
1576static void
1577data_device_drop(void *data, struct wl_data_device *data_device)
1578{
1579 struct input *input = data;
1580 struct window *window = input->pointer_focus;
1581
1582 if (window->drop_handler)
1583 window->drop_handler(window, input,
1584 input->sx, input->sy, window->user_data);
1585}
1586
1587static void
1588data_device_selection(void *data,
1589 struct wl_data_device *wl_data_device,
1590 struct wl_data_offer *offer)
1591{
1592 struct input *input = data;
1593 char **p;
1594
1595 if (input->selection_offer)
1596 data_offer_destroy(input->selection_offer);
1597
1598 input->selection_offer = wl_data_offer_get_user_data(offer);
1599 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1600 *p = NULL;
1601}
1602
1603static const struct wl_data_device_listener data_device_listener = {
1604 data_device_data_offer,
1605 data_device_enter,
1606 data_device_leave,
1607 data_device_motion,
1608 data_device_drop,
1609 data_device_selection
1610};
1611
1612struct wl_data_device *
1613input_get_data_device(struct input *input)
1614{
1615 return input->data_device;
1616}
1617
1618void
1619input_set_selection(struct input *input,
1620 struct wl_data_source *source, uint32_t time)
1621{
1622 wl_data_device_set_selection(input->data_device, source, time);
1623}
1624
1625void
1626input_accept(struct input *input, uint32_t time, const char *type)
1627{
1628 wl_data_offer_accept(input->drag_offer->offer, time, type);
1629}
1630
1631static void
1632offer_io_func(struct task *task, uint32_t events)
1633{
1634 struct data_offer *offer =
1635 container_of(task, struct data_offer, io_task);
1636 unsigned int len;
1637 char buffer[4096];
1638
1639 len = read(offer->fd, buffer, sizeof buffer);
1640 offer->func(buffer, len,
1641 offer->x, offer->y, offer->user_data);
1642
1643 if (len == 0) {
1644 close(offer->fd);
1645 data_offer_destroy(offer);
1646 }
1647}
1648
1649static void
1650data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1651 data_func_t func, void *user_data)
1652{
1653 int p[2];
1654
1655 pipe2(p, O_CLOEXEC);
1656 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1657 close(p[1]);
1658
1659 offer->io_task.run = offer_io_func;
1660 offer->fd = p[0];
1661 offer->func = func;
1662 offer->refcount++;
1663 offer->user_data = user_data;
1664
1665 display_watch_fd(offer->input->display,
1666 offer->fd, EPOLLIN, &offer->io_task);
1667}
1668
1669void
1670input_receive_drag_data(struct input *input, const char *mime_type,
1671 data_func_t func, void *data)
1672{
1673 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1674 input->drag_offer->x = input->sx;
1675 input->drag_offer->y = input->sy;
1676}
1677
1678int
1679input_receive_selection_data(struct input *input, const char *mime_type,
1680 data_func_t func, void *data)
1681{
1682 char **p;
1683
1684 if (input->selection_offer == NULL)
1685 return -1;
1686
1687 for (p = input->selection_offer->types.data; *p; p++)
1688 if (strcmp(mime_type, *p) == 0)
1689 break;
1690
1691 if (*p == NULL)
1692 return -1;
1693
1694 data_offer_receive_data(input->selection_offer,
1695 mime_type, func, data);
1696 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001697}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001698
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001699void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001700window_move(struct window *window, struct input *input, uint32_t time)
1701{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001702 if (!window->shell_surface)
1703 return;
1704
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001705 wl_shell_surface_move(window->shell_surface,
1706 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001707}
1708
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001709static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001710handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001711 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001713{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001714 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001715 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001716
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001717 /* FIXME: this is probably the wrong place to check for width
1718 * or height <= 0, but it prevents the compositor from crashing
1719 */
1720 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001721 return;
1722
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001723 window->resize_edges = edges;
1724
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001725 if (window->resize_handler) {
1726 child_width = width - 20 - window->margin * 2;
1727 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001728
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001729 (*window->resize_handler)(window,
1730 child_width, child_height,
1731 window->user_data);
1732 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001733 window->allocation.width = width;
1734 window->allocation.height = height;
1735
1736 if (window->redraw_handler)
1737 window_schedule_redraw(window);
1738 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001739}
1740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001742 handle_configure,
1743};
1744
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001745void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001746window_get_allocation(struct window *window,
1747 struct rectangle *allocation)
1748{
1749 *allocation = window->allocation;
1750}
1751
1752void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001753window_get_child_allocation(struct window *window,
1754 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001755{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001756 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001757 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001758 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001759 allocation->x = window->margin + 10;
1760 allocation->y = window->margin + 50;
1761 allocation->width =
1762 window->allocation.width - 20 - window->margin * 2;
1763 allocation->height =
1764 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001765 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001766}
1767
1768void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001769window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001770{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001771 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001772 window->allocation.x = 20 + window->margin;
1773 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001774 window->allocation.width = width + 20 + window->margin * 2;
1775 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001776 } else {
1777 window->allocation.x = 0;
1778 window->allocation.y = 0;
1779 window->allocation.width = width;
1780 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001781 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001782}
1783
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001784static void
1785idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001786{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001787 struct window *window =
1788 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001789
1790 window->redraw_handler(window, window->user_data);
1791 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001792}
1793
1794void
1795window_schedule_redraw(struct window *window)
1796{
1797 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001798 window->redraw_task.run = idle_redraw;
1799 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001800 window->redraw_scheduled = 1;
1801 }
1802}
1803
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001804void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001805window_set_custom(struct window *window)
1806{
1807 window->type = TYPE_CUSTOM;
1808}
1809
1810void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001811window_set_fullscreen(struct window *window, int fullscreen)
1812{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001813 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001814 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001815
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001816 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001817 return;
1818
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001819 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001820 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001821 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001822 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001823 width = output->allocation.width;
1824 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001825 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001826 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001827 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001828 width = window->saved_allocation.width - 20 - window->margin * 2;
1829 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001830 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001831 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001832
1833 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001834}
1835
1836void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001837window_set_decoration(struct window *window, int decoration)
1838{
1839 window->decoration = decoration;
1840}
1841
1842void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001843window_set_user_data(struct window *window, void *data)
1844{
1845 window->user_data = data;
1846}
1847
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001848void *
1849window_get_user_data(struct window *window)
1850{
1851 return window->user_data;
1852}
1853
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001854void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001855window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001856 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001857{
1858 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001859}
1860
1861void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001862window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001863 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001864{
1865 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001866}
1867
1868void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001869window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001870 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001871{
1872 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001873}
1874
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001875void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001876window_set_button_handler(struct window *window,
1877 window_button_handler_t handler)
1878{
1879 window->button_handler = handler;
1880}
1881
1882void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001883window_set_motion_handler(struct window *window,
1884 window_motion_handler_t handler)
1885{
1886 window->motion_handler = handler;
1887}
1888
1889void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001890window_set_enter_handler(struct window *window,
1891 window_enter_handler_t handler)
1892{
1893 window->enter_handler = handler;
1894}
1895
1896void
1897window_set_leave_handler(struct window *window,
1898 window_leave_handler_t handler)
1899{
1900 window->leave_handler = handler;
1901}
1902
1903void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001904window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001905 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001906{
1907 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001908}
1909
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001910void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001911window_set_item_focus_handler(struct window *window,
1912 window_item_focus_handler_t handler)
1913{
1914 window->item_focus_handler = handler;
1915}
1916
1917void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001918window_set_data_handler(struct window *window, window_data_handler_t handler)
1919{
1920 window->data_handler = handler;
1921}
1922
1923void
1924window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1925{
1926 window->drop_handler = handler;
1927}
1928
1929void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001930window_set_transparent(struct window *window, int transparent)
1931{
1932 window->transparent = transparent;
1933}
1934
1935void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001936window_set_title(struct window *window, const char *title)
1937{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001938 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001939 window->title = strdup(title);
1940}
1941
1942const char *
1943window_get_title(struct window *window)
1944{
1945 return window->title;
1946}
1947
1948void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001949display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1950 int32_t x, int32_t y, int32_t width, int32_t height)
1951{
1952 struct wl_buffer *buffer;
1953
1954 buffer = display_get_buffer_for_surface(display, cairo_surface);
1955
1956 wl_buffer_damage(buffer, x, y, width, height);
1957}
1958
1959void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001960window_damage(struct window *window, int32_t x, int32_t y,
1961 int32_t width, int32_t height)
1962{
1963 wl_surface_damage(window->surface, x, y, width, height);
1964}
1965
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001966static struct window *
1967window_create_internal(struct display *display, struct window *parent,
1968 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001969{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001970 struct window *window;
1971
1972 window = malloc(sizeof *window);
1973 if (window == NULL)
1974 return NULL;
1975
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001976 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001977 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001978 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001979 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001980 if (display->shell) {
1981 window->shell_surface =
1982 wl_shell_get_shell_surface(display->shell,
1983 window->surface);
1984 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001985 window->allocation.x = 0;
1986 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001987 window->allocation.width = width;
1988 window->allocation.height = height;
1989 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001990 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001991 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001992 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001993 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001994
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001995 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001996#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001997 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1998 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001999#else
2000 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2001#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002002 else
2003 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002004
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002005 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002006 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002007
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002008 if (window->shell_surface) {
2009 wl_shell_surface_set_user_data(window->shell_surface, window);
2010 wl_shell_surface_add_listener(window->shell_surface,
2011 &shell_surface_listener, window);
2012 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002013
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002014 return window;
2015}
2016
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002017struct window *
2018window_create(struct display *display, int32_t width, int32_t height)
2019{
2020 struct window *window;
2021
2022 window = window_create_internal(display, NULL, width, height);
2023 if (!window)
2024 return NULL;
2025
2026 return window;
2027}
2028
2029struct window *
2030window_create_transient(struct display *display, struct window *parent,
2031 int32_t x, int32_t y, int32_t width, int32_t height)
2032{
2033 struct window *window;
2034
2035 window = window_create_internal(parent->display,
2036 parent, width, height);
2037 if (!window)
2038 return NULL;
2039
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002040 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002041 window->x = x;
2042 window->y = y;
2043
2044 return window;
2045}
2046
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002047struct menu {
2048 struct window *window;
2049 const char **entries;
2050 int current;
2051 int count;
2052};
2053
2054static int
2055menu_set_item(struct window *window, struct menu *menu, int sy)
2056{
2057 int next;
2058
2059 next = (sy - 8) / 20;
2060 if (menu->current != next) {
2061 menu->current = next;
2062 window_schedule_redraw(window);
2063 }
2064
2065 return POINTER_LEFT_PTR;
2066}
2067
2068static int
2069menu_motion_handler(struct window *window,
2070 struct input *input, uint32_t time,
2071 int32_t x, int32_t y,
2072 int32_t sx, int32_t sy, void *data)
2073{
2074 return menu_set_item(window, data, sy);
2075}
2076
2077static int
2078menu_enter_handler(struct window *window,
2079 struct input *input, uint32_t time,
2080 int32_t x, int32_t y, void *data)
2081{
2082 return menu_set_item(window, data, y);
2083}
2084
2085static void
2086menu_leave_handler(struct window *window,
2087 struct input *input, uint32_t time, void *data)
2088{
2089 menu_set_item(window, data, -200);
2090}
2091
2092static void
2093menu_button_handler(struct window *window,
2094 struct input *input, uint32_t time,
2095 int button, int state, void *data)
2096
2097{
2098 struct menu *menu = data;
2099
2100 /* Either relase after press-drag-release or click-motion-click. */
2101 if (state == 0 && 0 <= menu->current && menu->current < menu->count)
2102 window_destroy(window);
2103}
2104
2105static void
2106menu_redraw_handler(struct window *window, void *data)
2107{
2108 cairo_t *cr;
2109 const int32_t r = 3, margin = 3;
2110 struct menu *menu = data;
2111 int32_t width, height, i;
2112
2113 width = 200;
2114 height = menu->count * 20 + margin * 2;
2115 window_set_child_size(window, width, height);
2116 window_create_surface(window);
2117
2118 cr = cairo_create(window->cairo_surface);
2119 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2120 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2121 cairo_paint(cr);
2122
2123 width = window->allocation.width;
2124 height = window->allocation.height;
2125 rounded_rect(cr, 0, 0, width, height, r);
2126 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2127 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2128 cairo_fill(cr);
2129
2130 for (i = 0; i < menu->count; i++) {
2131 if (i == menu->current) {
2132 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2133 cairo_rectangle(cr, margin, i * 20 + margin,
2134 width - 2 * margin, 20);
2135 cairo_fill(cr);
2136 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2137 cairo_move_to(cr, 10, i * 20 + 16);
2138 cairo_show_text(cr, menu->entries[i]);
2139 } else {
2140 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2141 cairo_move_to(cr, 10, i * 20 + 16);
2142 cairo_show_text(cr, menu->entries[i]);
2143 }
2144 }
2145
2146 cairo_destroy(cr);
2147 window_flush(window);
2148}
2149
2150struct window *
2151window_create_menu(struct display *display, struct window *parent,
2152 int32_t x, int32_t y, const char **entries, int count)
2153{
2154 struct window *window;
2155 struct menu *menu;
2156
2157 menu = malloc(sizeof *menu);
2158 if (!menu)
2159 return NULL;
2160
2161 window = window_create_internal(parent->display, parent, 0, 0);
2162 if (!window)
2163 return NULL;
2164
2165 menu->window = window;
2166 menu->entries = entries;
2167 menu->count = count;
2168 window->decoration = 0;
2169 window->type = TYPE_MENU;
2170 window->x = x;
2171 window->y = y;
2172
2173 wl_shell_surface_set_transient(window->shell_surface,
2174 window->parent->shell_surface,
2175 window->x, window->y, 0);
2176
2177 window_set_motion_handler(window, menu_motion_handler);
2178 window_set_enter_handler(window, menu_enter_handler);
2179 window_set_leave_handler(window, menu_leave_handler);
2180 window_set_button_handler(window, menu_button_handler);
2181 window_set_redraw_handler(window, menu_redraw_handler);
2182 window_set_user_data(window, menu);
2183
2184 return window;
2185}
2186
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002187void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002188window_set_buffer_type(struct window *window, enum window_buffer_type type)
2189{
2190 window->buffer_type = type;
2191}
2192
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002193
2194static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002195display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002196 struct wl_output *wl_output,
2197 int x, int y,
2198 int physical_width,
2199 int physical_height,
2200 int subpixel,
2201 const char *make,
2202 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002203{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002204 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002205
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002206 output->allocation.x = x;
2207 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002208}
2209
2210static void
2211display_handle_mode(void *data,
2212 struct wl_output *wl_output,
2213 uint32_t flags,
2214 int width,
2215 int height,
2216 int refresh)
2217{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002218 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002219 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002220
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002221 if (flags & WL_OUTPUT_MODE_CURRENT) {
2222 output->allocation.width = width;
2223 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002224 if (display->output_configure_handler)
2225 (*display->output_configure_handler)(
2226 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002227 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002228}
2229
2230static const struct wl_output_listener output_listener = {
2231 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002232 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002233};
2234
2235static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002236display_add_output(struct display *d, uint32_t id)
2237{
2238 struct output *output;
2239
2240 output = malloc(sizeof *output);
2241 if (output == NULL)
2242 return;
2243
2244 memset(output, 0, sizeof *output);
2245 output->display = d;
2246 output->output =
2247 wl_display_bind(d->display, id, &wl_output_interface);
2248 wl_list_insert(d->output_list.prev, &output->link);
2249
2250 wl_output_add_listener(output->output, &output_listener, output);
2251}
2252
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002253static void
2254output_destroy(struct output *output)
2255{
2256 if (output->destroy_handler)
2257 (*output->destroy_handler)(output, output->user_data);
2258
2259 wl_output_destroy(output->output);
2260 wl_list_remove(&output->link);
2261 free(output);
2262}
2263
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002264void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002265display_set_output_configure_handler(struct display *display,
2266 display_output_handler_t handler)
2267{
2268 struct output *output;
2269
2270 display->output_configure_handler = handler;
2271 if (!handler)
2272 return;
2273
2274 wl_list_for_each(output, &display->output_list, link)
2275 (*display->output_configure_handler)(output,
2276 display->user_data);
2277}
2278
2279void
2280output_set_user_data(struct output *output, void *data)
2281{
2282 output->user_data = data;
2283}
2284
2285void *
2286output_get_user_data(struct output *output)
2287{
2288 return output->user_data;
2289}
2290
2291void
2292output_set_destroy_handler(struct output *output,
2293 display_output_handler_t handler)
2294{
2295 output->destroy_handler = handler;
2296 /* FIXME: implement this, once we have way to remove outputs */
2297}
2298
2299void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002300output_get_allocation(struct output *output, struct rectangle *allocation)
2301{
2302 *allocation = output->allocation;
2303}
2304
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002305struct wl_output *
2306output_get_wl_output(struct output *output)
2307{
2308 return output->output;
2309}
2310
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002311static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002312display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002313{
2314 struct input *input;
2315
2316 input = malloc(sizeof *input);
2317 if (input == NULL)
2318 return;
2319
2320 memset(input, 0, sizeof *input);
2321 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002322 input->input_device =
2323 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002324 input->pointer_focus = NULL;
2325 input->keyboard_focus = NULL;
2326 wl_list_insert(d->input_list.prev, &input->link);
2327
2328 wl_input_device_add_listener(input->input_device,
2329 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002330 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002331
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002332 input->data_device =
2333 wl_data_device_manager_get_data_device(d->data_device_manager,
2334 input->input_device);
2335 wl_data_device_add_listener(input->data_device,
2336 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002337}
2338
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002339static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002340input_destroy(struct input *input)
2341{
2342 remove_keyboard_focus(input);
2343 remove_pointer_focus(input, 0);
2344
2345 if (input->drag_offer)
2346 data_offer_destroy(input->drag_offer);
2347
2348 if (input->selection_offer)
2349 data_offer_destroy(input->selection_offer);
2350
2351 wl_data_device_destroy(input->data_device);
2352 wl_list_remove(&input->link);
2353 wl_input_device_destroy(input->input_device);
2354 free(input);
2355}
2356
2357static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002358display_handle_global(struct wl_display *display, uint32_t id,
2359 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002360{
2361 struct display *d = data;
2362
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002363 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002364 d->compositor =
2365 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002366 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002367 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002368 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002369 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002370 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002371 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002372 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002373 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002374 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2375 d->data_device_manager =
2376 wl_display_bind(display, id,
2377 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002378 }
2379}
2380
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002381static void
2382display_render_frame(struct display *d)
2383{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002384 int radius = 8;
2385 cairo_t *cr;
2386
2387 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2388 cr = cairo_create(d->shadow);
2389 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2390 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2391 rounded_rect(cr, 16, 16, 112, 112, radius);
2392 cairo_fill(cr);
2393 cairo_destroy(cr);
2394 blur_surface(d->shadow, 64);
2395
2396 d->active_frame =
2397 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2398 cr = cairo_create(d->active_frame);
2399 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2400 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2401 rounded_rect(cr, 16, 16, 112, 112, radius);
2402 cairo_fill(cr);
2403 cairo_destroy(cr);
2404
2405 d->inactive_frame =
2406 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2407 cr = cairo_create(d->inactive_frame);
2408 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2409 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2410 rounded_rect(cr, 16, 16, 112, 112, radius);
2411 cairo_fill(cr);
2412 cairo_destroy(cr);
2413}
2414
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002415static void
2416init_xkb(struct display *d)
2417{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002418 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002419
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002420 names.rules = "evdev";
2421 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002422 names.layout = option_xkb_layout;
2423 names.variant = option_xkb_variant;
2424 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002425
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002426 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002427 if (!d->xkb) {
2428 fprintf(stderr, "Failed to compile keymap\n");
2429 exit(1);
2430 }
2431}
2432
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002433static void
2434fini_xkb(struct display *display)
2435{
2436 xkb_free_keymap(display->xkb);
2437}
2438
Yuval Fledel45568f62010-12-06 09:18:12 -05002439static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002440init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002441{
2442 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002443 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002444
2445 static const EGLint premul_argb_cfg_attribs[] = {
2446 EGL_SURFACE_TYPE,
2447 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2448 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002449 EGL_RED_SIZE, 1,
2450 EGL_GREEN_SIZE, 1,
2451 EGL_BLUE_SIZE, 1,
2452 EGL_ALPHA_SIZE, 1,
2453 EGL_DEPTH_SIZE, 1,
2454 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2455 EGL_NONE
2456 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002457
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002458 static const EGLint rgb_cfg_attribs[] = {
2459 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2460 EGL_RED_SIZE, 1,
2461 EGL_GREEN_SIZE, 1,
2462 EGL_BLUE_SIZE, 1,
2463 EGL_ALPHA_SIZE, 0,
2464 EGL_DEPTH_SIZE, 1,
2465 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2466 EGL_NONE
2467 };
2468
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002469 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002470 if (!eglInitialize(d->dpy, &major, &minor)) {
2471 fprintf(stderr, "failed to initialize display\n");
2472 return -1;
2473 }
2474
2475 if (!eglBindAPI(EGL_OPENGL_API)) {
2476 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2477 return -1;
2478 }
2479
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002480 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002481 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002482 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002483 return -1;
2484 }
2485
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002486 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2487 &d->rgb_config, 1, &n) || n != 1) {
2488 fprintf(stderr, "failed to choose rgb config\n");
2489 return -1;
2490 }
2491
Benjamin Franzke0c991632011-09-27 21:57:31 +02002492 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2493 if (d->rgb_ctx == NULL) {
2494 fprintf(stderr, "failed to create context\n");
2495 return -1;
2496 }
2497 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2498 EGL_NO_CONTEXT, NULL);
2499 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002500 fprintf(stderr, "failed to create context\n");
2501 return -1;
2502 }
2503
Benjamin Franzke0c991632011-09-27 21:57:31 +02002504 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002505 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002506 return -1;
2507 }
2508
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002509#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002510 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2511 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002512 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002513 return -1;
2514 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002515 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2516 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2517 fprintf(stderr, "failed to get cairo egl argb device\n");
2518 return -1;
2519 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002520#endif
2521
2522 return 0;
2523}
2524
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002525static void
2526fini_egl(struct display *display)
2527{
2528#ifdef HAVE_CAIRO_EGL
2529 cairo_device_destroy(display->argb_device);
2530 cairo_device_destroy(display->rgb_device);
2531#endif
2532
2533 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2534 EGL_NO_CONTEXT);
2535
2536 eglTerminate(display->dpy);
2537 eglReleaseThread();
2538}
2539
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002540static int
2541event_mask_update(uint32_t mask, void *data)
2542{
2543 struct display *d = data;
2544
2545 d->mask = mask;
2546
2547 return 0;
2548}
2549
2550static void
2551handle_display_data(struct task *task, uint32_t events)
2552{
2553 struct display *display =
2554 container_of(task, struct display, display_task);
2555
2556 wl_display_iterate(display->display, display->mask);
2557}
2558
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002559struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002560display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002561{
2562 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002563 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002564 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002565 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002566
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002567 g_type_init();
2568
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002569 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002570 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002571 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002572
2573 xkb_option_group = g_option_group_new("xkb",
2574 "Keyboard options",
2575 "Show all XKB options",
2576 NULL, NULL);
2577 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2578 g_option_context_add_group (context, xkb_option_group);
2579
2580 if (!g_option_context_parse(context, argc, argv, &error)) {
2581 fprintf(stderr, "option parsing failed: %s\n", error->message);
2582 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002583 }
2584
Tim Wiederhake748f6722011-01-23 23:25:25 +01002585 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002586
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002587 d = malloc(sizeof *d);
2588 if (d == NULL)
2589 return NULL;
2590
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002591 memset(d, 0, sizeof *d);
2592
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002593 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002594 if (d->display == NULL) {
2595 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002596 return NULL;
2597 }
2598
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002599 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2600 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2601 d->display_task.run = handle_display_data;
2602 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2603
2604 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002605 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002606 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002607
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002608 /* Set up listener so we'll catch all events. */
2609 wl_display_add_global_listener(d->display,
2610 display_handle_global, d);
2611
2612 /* Process connection events. */
2613 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002614 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002615 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002616
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002617 d->image_target_texture_2d =
2618 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2619 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2620 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2621
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002622 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002623
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002624 display_render_frame(d);
2625
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002626 wl_list_init(&d->window_list);
2627
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002628 init_xkb(d);
2629
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002630 return d;
2631}
2632
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002633static void
2634display_destroy_outputs(struct display *display)
2635{
2636 struct output *tmp;
2637 struct output *output;
2638
2639 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2640 output_destroy(output);
2641}
2642
Pekka Paalanene1207c72011-12-16 12:02:09 +02002643static void
2644display_destroy_inputs(struct display *display)
2645{
2646 struct input *tmp;
2647 struct input *input;
2648
2649 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2650 input_destroy(input);
2651}
2652
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002653void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002654display_destroy(struct display *display)
2655{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002656 if (!wl_list_empty(&display->window_list))
2657 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2658
2659 if (!wl_list_empty(&display->deferred_list))
2660 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2661
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002662 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002663 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002664
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002665 fini_xkb(display);
2666 fini_egl(display);
2667
Pekka Paalanenc2052982011-12-16 11:41:32 +02002668 if (display->shell)
2669 wl_shell_destroy(display->shell);
2670
2671 if (display->shm)
2672 wl_shm_destroy(display->shm);
2673
2674 if (display->data_device_manager)
2675 wl_data_device_manager_destroy(display->data_device_manager);
2676
2677 wl_compositor_destroy(display->compositor);
2678
2679 close(display->epoll_fd);
2680
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002681 wl_display_flush(display->display);
2682 wl_display_destroy(display->display);
2683 free(display);
2684}
2685
2686void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002687display_set_user_data(struct display *display, void *data)
2688{
2689 display->user_data = data;
2690}
2691
2692void *
2693display_get_user_data(struct display *display)
2694{
2695 return display->user_data;
2696}
2697
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002698struct wl_display *
2699display_get_display(struct display *display)
2700{
2701 return display->display;
2702}
2703
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002704struct output *
2705display_get_output(struct display *display)
2706{
2707 return container_of(display->output_list.next, struct output, link);
2708}
2709
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002710struct wl_compositor *
2711display_get_compositor(struct display *display)
2712{
2713 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002714}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002715
2716EGLDisplay
2717display_get_egl_display(struct display *d)
2718{
2719 return d->dpy;
2720}
2721
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002722struct wl_data_source *
2723display_create_data_source(struct display *display)
2724{
2725 return wl_data_device_manager_create_data_source(display->data_device_manager);
2726}
2727
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002728EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002729display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002730{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002731 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002732}
2733
Benjamin Franzke0c991632011-09-27 21:57:31 +02002734EGLConfig
2735display_get_argb_egl_config(struct display *d)
2736{
2737 return d->premultiplied_argb_config;
2738}
2739
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002740struct wl_shell *
2741display_get_shell(struct display *display)
2742{
2743 return display->shell;
2744}
2745
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002746int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002747display_acquire_window_surface(struct display *display,
2748 struct window *window,
2749 EGLContext ctx)
2750{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002751#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002752 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002753 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002754
2755 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002756 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002757 device = cairo_surface_get_device(window->cairo_surface);
2758 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002759 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002760
Benjamin Franzke0c991632011-09-27 21:57:31 +02002761 if (!ctx) {
2762 if (device == display->rgb_device)
2763 ctx = display->rgb_ctx;
2764 else if (device == display->argb_device)
2765 ctx = display->argb_ctx;
2766 else
2767 assert(0);
2768 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002769
2770 data = cairo_surface_get_user_data(window->cairo_surface,
2771 &surface_data_key);
2772
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002773 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002774 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002775 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2776 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002777
2778 return 0;
2779#else
2780 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002781#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002782}
2783
2784void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002785display_release_window_surface(struct display *display,
2786 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002787{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002788#ifdef HAVE_CAIRO_EGL
2789 cairo_device_t *device;
2790
2791 device = cairo_surface_get_device(window->cairo_surface);
2792 if (!device)
2793 return;
2794
2795 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002796 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002797 cairo_device_release(device);
2798#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002799}
2800
2801void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002802display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002803{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002804 wl_list_insert(&display->deferred_list, &task->link);
2805}
2806
2807void
2808display_watch_fd(struct display *display,
2809 int fd, uint32_t events, struct task *task)
2810{
2811 struct epoll_event ep;
2812
2813 ep.events = events;
2814 ep.data.ptr = task;
2815 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2816}
2817
2818void
2819display_run(struct display *display)
2820{
2821 struct task *task;
2822 struct epoll_event ep[16];
2823 int i, count;
2824
Pekka Paalanen826d7952011-12-15 10:14:07 +02002825 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002826 while (1) {
2827 while (display->mask & WL_DISPLAY_WRITABLE)
2828 wl_display_iterate(display->display,
2829 WL_DISPLAY_WRITABLE);
2830
Pekka Paalanen826d7952011-12-15 10:14:07 +02002831 if (!display->running)
2832 break;
2833
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002834 count = epoll_wait(display->epoll_fd,
2835 ep, ARRAY_LENGTH(ep), -1);
2836 for (i = 0; i < count; i++) {
2837 task = ep[i].data.ptr;
2838 task->run(task, ep[i].events);
2839 }
2840
2841 while (!wl_list_empty(&display->deferred_list)) {
2842 task = container_of(display->deferred_list.next,
2843 struct task, link);
2844 wl_list_remove(&task->link);
2845 task->run(task, 0);
2846 }
2847 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002848}
Pekka Paalanen826d7952011-12-15 10:14:07 +02002849
2850void
2851display_exit(struct display *display)
2852{
2853 display->running = 0;
2854}