blob: 3be93b9c51f021e1ae93ed3b492bdce02546ce2c [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[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500661 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
662 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
663 { DATADIR "/weston/bottom_side.png", 16, 20 },
664 { DATADIR "/weston/grabbing.png", 20, 17 },
665 { DATADIR "/weston/left_ptr.png", 10, 5 },
666 { DATADIR "/weston/left_side.png", 10, 20 },
667 { DATADIR "/weston/right_side.png", 30, 19 },
668 { DATADIR "/weston/top_left_corner.png", 8, 8 },
669 { DATADIR "/weston/top_right_corner.png", 26, 8 },
670 { DATADIR "/weston/top_side.png", 18, 8 },
671 { DATADIR "/weston/xterm.png", 15, 15 },
672 { DATADIR "/weston/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
Pekka Paalanen325bb602011-12-19 10:31:45 +0200700static void
701destroy_pointer_surfaces(struct display *display)
702{
703 int i, count;
704
705 count = ARRAY_LENGTH(pointer_images);
706 for (i = 0; i < count; ++i) {
707 if (display->pointer_surfaces[i])
708 cairo_surface_destroy(display->pointer_surfaces[i]);
709 }
710 free(display->pointer_surfaces);
711}
712
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400713cairo_surface_t *
714display_get_pointer_surface(struct display *display, int pointer,
715 int *width, int *height,
716 int *hotspot_x, int *hotspot_y)
717{
718 cairo_surface_t *surface;
719
720 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500721#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400722 *width = cairo_gl_surface_get_width(surface);
723 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000724#else
725 *width = cairo_image_surface_get_width(surface);
726 *height = cairo_image_surface_get_height(surface);
727#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728 *hotspot_x = pointer_images[pointer].hotspot_x;
729 *hotspot_y = pointer_images[pointer].hotspot_y;
730
731 return cairo_surface_reference(surface);
732}
733
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400734static void
735window_attach_surface(struct window *window);
736
737static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400738free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400739{
740 struct window *window = data;
741
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400742 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400743 cairo_surface_destroy(window->pending_surface);
744 window->pending_surface = NULL;
745 if (window->cairo_surface)
746 window_attach_surface(window);
747}
748
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400749static const struct wl_callback_listener free_surface_listener = {
750 free_surface
751};
752
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500753static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200754window_get_resize_dx_dy(struct window *window, int *x, int *y)
755{
756 if (window->resize_edges & WINDOW_RESIZING_LEFT)
757 *x = window->server_allocation.width - window->allocation.width;
758 else
759 *x = 0;
760
761 if (window->resize_edges & WINDOW_RESIZING_TOP)
762 *y = window->server_allocation.height -
763 window->allocation.height;
764 else
765 *y = 0;
766
767 window->resize_edges = 0;
768}
769
770static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400771window_set_type(struct window *window)
772{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200773 if (!window->shell_surface)
774 return;
775
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400776 switch (window->type) {
777 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200778 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400779 break;
780 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200781 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400782 break;
783 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200784 wl_shell_surface_set_transient(window->shell_surface,
785 window->parent->shell_surface,
786 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400787 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500788 case TYPE_MENU:
789 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400790 case TYPE_CUSTOM:
791 break;
792 }
793}
794
795static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500796window_attach_surface(struct window *window)
797{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400798 struct display *display = window->display;
799 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400800 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000801#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000803#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500804 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400806 if (display->shell)
807 window_set_type(window);
808
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
812 data = cairo_surface_get_user_data(window->cairo_surface,
813 &surface_data_key);
814
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 cairo_gl_surface_swapbuffers(window->cairo_surface);
816 wl_egl_window_get_attached_size(data->window,
817 &window->server_allocation.width,
818 &window->server_allocation.height);
819 break;
820 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000821#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200823 window_get_resize_dx_dy(window, &x, &y);
824
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 if (window->pending_surface != NULL)
826 return;
827
828 window->pending_surface = window->cairo_surface;
829 window->cairo_surface = NULL;
830
831 buffer =
832 display_get_buffer_for_surface(display,
833 window->pending_surface);
834
835 wl_surface_attach(window->surface, buffer, x, y);
836 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400837 cb = wl_display_sync(display->display);
838 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000840 default:
841 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500843
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500844 wl_surface_damage(window->surface, 0, 0,
845 window->allocation.width,
846 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500847}
848
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500849void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400850window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500851{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100852 if (window->cairo_surface) {
853 switch (window->buffer_type) {
854 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
855 case WINDOW_BUFFER_TYPE_SHM:
856 display_surface_damage(window->display,
857 window->cairo_surface,
858 0, 0,
859 window->allocation.width,
860 window->allocation.height);
861 break;
862 default:
863 break;
864 }
865 window_attach_surface(window);
866 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500867}
868
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400869void
870window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400871{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500872 cairo_surface_reference(surface);
873
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400874 if (window->cairo_surface != NULL)
875 cairo_surface_destroy(window->cairo_surface);
876
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500877 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400878}
879
Benjamin Franzke22d54812011-07-16 19:50:32 +0000880#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100881static void
882window_resize_cairo_window_surface(struct window *window)
883{
884 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200885 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100886
887 data = cairo_surface_get_user_data(window->cairo_surface,
888 &surface_data_key);
889
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200890 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100891 wl_egl_window_resize(data->window,
892 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200893 window->allocation.height,
894 x,y);
895
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896 cairo_gl_surface_set_size(window->cairo_surface,
897 window->allocation.width,
898 window->allocation.height);
899}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000900#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400902struct display *
903window_get_display(struct window *window)
904{
905 return window->display;
906}
907
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400908void
909window_create_surface(struct window *window)
910{
911 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 uint32_t flags = 0;
913
914 if (!window->transparent)
915 flags = SURFACE_OPAQUE;
916
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400917 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500918#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100919 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
920 if (window->cairo_surface) {
921 window_resize_cairo_window_surface(window);
922 return;
923 }
924 surface = display_create_surface(window->display,
925 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400926 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100927 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500928 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400929 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100930 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400931 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932 break;
933#endif
934 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400935 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400936 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800938 default:
939 surface = NULL;
940 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942
943 window_set_surface(window, surface);
944 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945}
946
947static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500948window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500949{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500950 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500951 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400952 cairo_surface_t *frame;
953 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500954
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400957 width = window->allocation.width;
958 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500959
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500960 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500961
Kristian Høgsberg09531622010-06-14 23:22:15 -0400962 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400963 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400964 cairo_paint(cr);
965
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400966 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400967 tile_mask(cr, window->display->shadow,
968 shadow_dx, shadow_dy, width, height,
969 window->margin + 10 - shadow_dx,
970 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500971
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400973 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400974 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400975 frame = window->display->inactive_frame;
976
977 tile_source(cr, frame, 0, 0, width, height,
978 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500979
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500980 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
981 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500982 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400983 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400984 if (window->keyboard_device)
985 cairo_set_source_rgb(cr, 0, 0, 0);
986 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400987 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400988 cairo_show_text(cr, window->title);
989
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500990 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500991}
992
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400993void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500994window_destroy(struct window *window)
995{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200996 struct display *display = window->display;
997 struct input *input;
998
999 if (window->redraw_scheduled)
1000 wl_list_remove(&window->redraw_task.link);
1001
1002 wl_list_for_each(input, &display->input_list, link) {
1003 if (input->pointer_focus == window)
1004 input->pointer_focus = NULL;
1005 if (input->keyboard_focus == window)
1006 input->keyboard_focus = NULL;
1007 }
1008
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001009 if (window->shell_surface)
1010 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001011 wl_surface_destroy(window->surface);
1012 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001013
1014 if (window->cairo_surface != NULL)
1015 cairo_surface_destroy(window->cairo_surface);
1016 if (window->pending_surface != NULL)
1017 cairo_surface_destroy(window->pending_surface);
1018
1019 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001020 free(window);
1021}
1022
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001023static struct item *
1024window_find_item(struct window *window, int32_t x, int32_t y)
1025{
1026 struct item *item;
1027
1028 wl_list_for_each(item, &window->item_list, link) {
1029 if (item->allocation.x <= x &&
1030 x < item->allocation.x + item->allocation.width &&
1031 item->allocation.y <= y &&
1032 y < item->allocation.y + item->allocation.height) {
1033 return item;
1034 }
1035 }
1036
1037 return NULL;
1038}
1039
1040struct item *
1041window_add_item(struct window *window, void *data)
1042{
1043 struct item *item;
1044
1045 item = malloc(sizeof *item);
1046 memset(item, 0, sizeof *item);
1047 item->user_data = data;
1048 wl_list_insert(window->item_list.prev, &item->link);
1049
1050 return item;
1051}
1052
1053void
1054window_for_each_item(struct window *window, item_func_t func, void *data)
1055{
1056 struct item *item;
1057
1058 wl_list_for_each(item, &window->item_list, link)
1059 func(item, data);
1060}
1061
1062struct item *
1063window_get_focus_item(struct window *window)
1064{
1065 return window->focus_item;
1066}
1067
1068void
1069item_get_allocation(struct item *item, struct rectangle *allocation)
1070{
1071 *allocation = item->allocation;
1072}
1073
1074void
1075item_set_allocation(struct item *item,
1076 int32_t x, int32_t y, int32_t width, int32_t height)
1077{
1078 item->allocation.x = x;
1079 item->allocation.y = y;
1080 item->allocation.width = width;
1081 item->allocation.height = height;
1082}
1083
1084void *
1085item_get_user_data(struct item *item)
1086{
1087 return item->user_data;
1088}
1089
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001090void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001091window_draw(struct window *window)
1092{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001093 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001094 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001095 else
1096 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001097}
1098
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001099cairo_surface_t *
1100window_get_surface(struct window *window)
1101{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001102 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001103}
1104
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001105struct wl_surface *
1106window_get_wl_surface(struct window *window)
1107{
1108 return window->surface;
1109}
1110
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001111struct wl_shell_surface *
1112window_get_wl_shell_surface(struct window *window)
1113{
1114 return window->shell_surface;
1115}
1116
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001117static int
1118get_pointer_location(struct window *window, int32_t x, int32_t y)
1119{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001120 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001121 const int grip_size = 8;
1122
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001123 if (!window->decoration)
1124 return WINDOW_CLIENT_AREA;
1125
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001128 else if (window->margin <= x && x < window->margin + grip_size)
1129 hlocation = WINDOW_RESIZING_LEFT;
1130 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132 else if (x < window->allocation.width - window->margin)
1133 hlocation = WINDOW_RESIZING_RIGHT;
1134 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001136
1137 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001138 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139 else if (window->margin <= y && y < window->margin + grip_size)
1140 vlocation = WINDOW_RESIZING_TOP;
1141 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143 else if (y < window->allocation.height - window->margin)
1144 vlocation = WINDOW_RESIZING_BOTTOM;
1145 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001146 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001147
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001148 location = vlocation | hlocation;
1149 if (location & WINDOW_EXTERIOR)
1150 location = WINDOW_EXTERIOR;
1151 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1152 location = WINDOW_TITLEBAR;
1153 else if (location == WINDOW_INTERIOR)
1154 location = WINDOW_CLIENT_AREA;
1155
1156 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001157}
1158
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001159static int
1160input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001162 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001163
1164 location = get_pointer_location(input->pointer_focus,
1165 input->sx, input->sy);
1166 switch (location) {
1167 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001168 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001170 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001172 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001174 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001175 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001176 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001177 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001178 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001179 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001180 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001181 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001182 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001183 case WINDOW_EXTERIOR:
1184 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001185 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001186 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001187 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001188 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001189}
1190
1191void
1192input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1193{
1194 struct display *display = input->display;
1195 struct wl_buffer *buffer;
1196 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001198 if (pointer == input->current_pointer_image)
1199 return;
1200
1201 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001202 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001203
1204 if (!surface)
1205 return;
1206
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001207 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001208 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 pointer_images[pointer].hotspot_x,
1210 pointer_images[pointer].hotspot_y);
1211}
1212
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001213static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001214window_set_focus_item(struct window *window, struct item *focus)
1215{
1216 void *data;
1217
1218 if (focus == window->focus_item)
1219 return;
1220
1221 window->focus_item = focus;
1222 data = focus ? focus->user_data : NULL;
1223 if (window->item_focus_handler)
1224 window->item_focus_handler(window, focus, data);
1225}
1226
1227static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001228input_handle_motion(void *data, struct wl_input_device *input_device,
1229 uint32_t time,
1230 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001231{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001232 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001233 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001234 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001235 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001236
1237 input->x = x;
1238 input->y = y;
1239 input->sx = sx;
1240 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001241
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001242 if (!window->focus_item || !window->item_grab_button) {
1243 item = window_find_item(window, sx, sy);
1244 window_set_focus_item(window, item);
1245 }
1246
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 if (window->motion_handler)
1248 pointer = (*window->motion_handler)(window, input, time,
1249 x, y, sx, sy,
1250 window->user_data);
1251
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001252 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001253 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001254}
1255
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001256static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001257input_handle_button(void *data,
1258 struct wl_input_device *input_device,
1259 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001260{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001261 struct input *input = data;
1262 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001263 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001265
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001266 if (window->focus_item && window->item_grab_button == 0 && state)
1267 window->item_grab_button = button;
1268
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001270
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001271 if (window->display->shell &&
1272 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001273 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001274 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001275 if (!window->shell_surface)
1276 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001277 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001278 wl_shell_surface_move(window->shell_surface,
1279 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001280 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001281 case WINDOW_RESIZING_TOP:
1282 case WINDOW_RESIZING_BOTTOM:
1283 case WINDOW_RESIZING_LEFT:
1284 case WINDOW_RESIZING_RIGHT:
1285 case WINDOW_RESIZING_TOP_LEFT:
1286 case WINDOW_RESIZING_TOP_RIGHT:
1287 case WINDOW_RESIZING_BOTTOM_LEFT:
1288 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001289 if (!window->shell_surface)
1290 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001291 wl_shell_surface_resize(window->shell_surface,
1292 input_device, time,
1293 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001294 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 case WINDOW_CLIENT_AREA:
1296 if (window->button_handler)
1297 (*window->button_handler)(window,
1298 input, time,
1299 button, state,
1300 window->user_data);
1301 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001302 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001303 } else {
1304 if (window->button_handler)
1305 (*window->button_handler)(window,
1306 input, time,
1307 button, state,
1308 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001309 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001310
1311 if (window->focus_item &&
1312 window->item_grab_button == button && !state) {
1313 window->item_grab_button = 0;
1314 item = window_find_item(window, input->sx, input->sy);
1315 window_set_focus_item(window, item);
1316 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001317}
1318
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001319static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001320input_handle_key(void *data, struct wl_input_device *input_device,
1321 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001322{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323 struct input *input = data;
1324 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001325 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001326 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001327
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001328 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001329 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001330 return;
1331
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001332 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001333 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001334 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1335 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001336
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001337 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1338
1339 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001340 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001341 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001342 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001343
1344 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001345 (*window->key_handler)(window, input, time, key, sym, state,
1346 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001347}
1348
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001349static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001350input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001351{
1352 struct window *window = input->pointer_focus;
1353
1354 if (!window)
1355 return;
1356
1357 window_set_focus_item(window, NULL);
1358
1359 if (window->leave_handler)
1360 window->leave_handler(window, input, time, window->user_data);
1361 input->pointer_focus = NULL;
1362 input->current_pointer_image = POINTER_UNSET;
1363}
1364
1365static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001366input_handle_pointer_focus(void *data,
1367 struct wl_input_device *input_device,
1368 uint32_t time, struct wl_surface *surface,
1369 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001370{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001371 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001372 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001373 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001374 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001375
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001376 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001377 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001378 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001379
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001380 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001381 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001382 window = input->pointer_focus;
1383
Kristian Høgsberg59826582011-01-20 11:56:57 -05001384 input->x = x;
1385 input->y = y;
1386 input->sx = sx;
1387 input->sy = sy;
1388
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001389 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001390 if (window->enter_handler)
1391 pointer = window->enter_handler(window, input,
1392 time, sx, sy,
1393 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001394
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001395 item = window_find_item(window, x, y);
1396 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001397
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001398 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001399 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001400 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001401}
1402
1403static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001404input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001405{
1406 struct window *window = input->keyboard_focus;
1407
1408 if (!window)
1409 return;
1410
1411 window->keyboard_device = NULL;
1412 if (window->keyboard_focus_handler)
1413 (*window->keyboard_focus_handler)(window, NULL,
1414 window->user_data);
1415
1416 input->keyboard_focus = NULL;
1417}
1418
1419static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001420input_handle_keyboard_focus(void *data,
1421 struct wl_input_device *input_device,
1422 uint32_t time,
1423 struct wl_surface *surface,
1424 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001425{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001426 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001427 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001428 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001429 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001430
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001431 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001432
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001433 if (surface)
1434 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001435
1436 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001437 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001438 for (k = keys->data; k < end; k++)
1439 input->modifiers |= d->xkb->map->modmap[*k];
1440
1441 window = input->keyboard_focus;
1442 if (window) {
1443 window->keyboard_device = input;
1444 if (window->keyboard_focus_handler)
1445 (*window->keyboard_focus_handler)(window,
1446 window->keyboard_device,
1447 window->user_data);
1448 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001449}
1450
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001451static void
1452input_handle_touch_down(void *data,
1453 struct wl_input_device *wl_input_device,
1454 uint32_t time, struct wl_surface *surface,
1455 int32_t id, int32_t x, int32_t y)
1456{
1457}
1458
1459static void
1460input_handle_touch_up(void *data,
1461 struct wl_input_device *wl_input_device,
1462 uint32_t time, int32_t id)
1463{
1464}
1465
1466static void
1467input_handle_touch_motion(void *data,
1468 struct wl_input_device *wl_input_device,
1469 uint32_t time, int32_t id, int32_t x, int32_t y)
1470{
1471}
1472
1473static void
1474input_handle_touch_frame(void *data,
1475 struct wl_input_device *wl_input_device)
1476{
1477}
1478
1479static void
1480input_handle_touch_cancel(void *data,
1481 struct wl_input_device *wl_input_device)
1482{
1483}
1484
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001485static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001486 input_handle_motion,
1487 input_handle_button,
1488 input_handle_key,
1489 input_handle_pointer_focus,
1490 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001491 input_handle_touch_down,
1492 input_handle_touch_up,
1493 input_handle_touch_motion,
1494 input_handle_touch_frame,
1495 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001496};
1497
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001498void
1499input_get_position(struct input *input, int32_t *x, int32_t *y)
1500{
1501 *x = input->sx;
1502 *y = input->sy;
1503}
1504
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001505struct wl_input_device *
1506input_get_input_device(struct input *input)
1507{
1508 return input->input_device;
1509}
1510
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001511uint32_t
1512input_get_modifiers(struct input *input)
1513{
1514 return input->modifiers;
1515}
1516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001517struct data_offer {
1518 struct wl_data_offer *offer;
1519 struct input *input;
1520 struct wl_array types;
1521 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001522
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001523 struct task io_task;
1524 int fd;
1525 data_func_t func;
1526 int32_t x, y;
1527 void *user_data;
1528};
1529
1530static void
1531data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1532{
1533 struct data_offer *offer = data;
1534 char **p;
1535
1536 p = wl_array_add(&offer->types, sizeof *p);
1537 *p = strdup(type);
1538}
1539
1540static const struct wl_data_offer_listener data_offer_listener = {
1541 data_offer_offer,
1542};
1543
1544static void
1545data_offer_destroy(struct data_offer *offer)
1546{
1547 char **p;
1548
1549 offer->refcount--;
1550 if (offer->refcount == 0) {
1551 wl_data_offer_destroy(offer->offer);
1552 for (p = offer->types.data; *p; p++)
1553 free(*p);
1554 wl_array_release(&offer->types);
1555 free(offer);
1556 }
1557}
1558
1559static void
1560data_device_data_offer(void *data,
1561 struct wl_data_device *data_device, uint32_t id)
1562{
1563 struct data_offer *offer;
1564
1565 offer = malloc(sizeof *offer);
1566
1567 wl_array_init(&offer->types);
1568 offer->refcount = 1;
1569 offer->input = data;
1570
1571 /* FIXME: Generate typesafe wrappers for this */
1572 offer->offer = (struct wl_data_offer *)
1573 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1574 id, &wl_data_offer_interface);
1575
1576 wl_data_offer_add_listener(offer->offer,
1577 &data_offer_listener, offer);
1578}
1579
1580static void
1581data_device_enter(void *data, struct wl_data_device *data_device,
1582 uint32_t time, struct wl_surface *surface,
1583 int32_t x, int32_t y, struct wl_data_offer *offer)
1584{
1585 struct input *input = data;
1586 struct window *window;
1587 char **p;
1588
1589 input->drag_offer = wl_data_offer_get_user_data(offer);
1590 window = wl_surface_get_user_data(surface);
1591 input->pointer_focus = window;
1592
1593 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1594 *p = NULL;
1595
1596 window = input->pointer_focus;
1597 if (window->data_handler)
1598 window->data_handler(window, input, time, x, y,
1599 input->drag_offer->types.data,
1600 window->user_data);
1601}
1602
1603static void
1604data_device_leave(void *data, struct wl_data_device *data_device)
1605{
1606 struct input *input = data;
1607
1608 data_offer_destroy(input->drag_offer);
1609 input->drag_offer = NULL;
1610}
1611
1612static void
1613data_device_motion(void *data, struct wl_data_device *data_device,
1614 uint32_t time, int32_t x, int32_t y)
1615{
1616 struct input *input = data;
1617 struct window *window = input->pointer_focus;
1618
1619 input->sx = x;
1620 input->sy = y;
1621
1622 if (window->data_handler)
1623 window->data_handler(window, input, time, x, y,
1624 input->drag_offer->types.data,
1625 window->user_data);
1626}
1627
1628static void
1629data_device_drop(void *data, struct wl_data_device *data_device)
1630{
1631 struct input *input = data;
1632 struct window *window = input->pointer_focus;
1633
1634 if (window->drop_handler)
1635 window->drop_handler(window, input,
1636 input->sx, input->sy, window->user_data);
1637}
1638
1639static void
1640data_device_selection(void *data,
1641 struct wl_data_device *wl_data_device,
1642 struct wl_data_offer *offer)
1643{
1644 struct input *input = data;
1645 char **p;
1646
1647 if (input->selection_offer)
1648 data_offer_destroy(input->selection_offer);
1649
1650 input->selection_offer = wl_data_offer_get_user_data(offer);
1651 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1652 *p = NULL;
1653}
1654
1655static const struct wl_data_device_listener data_device_listener = {
1656 data_device_data_offer,
1657 data_device_enter,
1658 data_device_leave,
1659 data_device_motion,
1660 data_device_drop,
1661 data_device_selection
1662};
1663
1664struct wl_data_device *
1665input_get_data_device(struct input *input)
1666{
1667 return input->data_device;
1668}
1669
1670void
1671input_set_selection(struct input *input,
1672 struct wl_data_source *source, uint32_t time)
1673{
1674 wl_data_device_set_selection(input->data_device, source, time);
1675}
1676
1677void
1678input_accept(struct input *input, uint32_t time, const char *type)
1679{
1680 wl_data_offer_accept(input->drag_offer->offer, time, type);
1681}
1682
1683static void
1684offer_io_func(struct task *task, uint32_t events)
1685{
1686 struct data_offer *offer =
1687 container_of(task, struct data_offer, io_task);
1688 unsigned int len;
1689 char buffer[4096];
1690
1691 len = read(offer->fd, buffer, sizeof buffer);
1692 offer->func(buffer, len,
1693 offer->x, offer->y, offer->user_data);
1694
1695 if (len == 0) {
1696 close(offer->fd);
1697 data_offer_destroy(offer);
1698 }
1699}
1700
1701static void
1702data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1703 data_func_t func, void *user_data)
1704{
1705 int p[2];
1706
1707 pipe2(p, O_CLOEXEC);
1708 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1709 close(p[1]);
1710
1711 offer->io_task.run = offer_io_func;
1712 offer->fd = p[0];
1713 offer->func = func;
1714 offer->refcount++;
1715 offer->user_data = user_data;
1716
1717 display_watch_fd(offer->input->display,
1718 offer->fd, EPOLLIN, &offer->io_task);
1719}
1720
1721void
1722input_receive_drag_data(struct input *input, const char *mime_type,
1723 data_func_t func, void *data)
1724{
1725 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1726 input->drag_offer->x = input->sx;
1727 input->drag_offer->y = input->sy;
1728}
1729
1730int
1731input_receive_selection_data(struct input *input, const char *mime_type,
1732 data_func_t func, void *data)
1733{
1734 char **p;
1735
1736 if (input->selection_offer == NULL)
1737 return -1;
1738
1739 for (p = input->selection_offer->types.data; *p; p++)
1740 if (strcmp(mime_type, *p) == 0)
1741 break;
1742
1743 if (*p == NULL)
1744 return -1;
1745
1746 data_offer_receive_data(input->selection_offer,
1747 mime_type, func, data);
1748 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001749}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001750
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001751int
1752input_receive_selection_data_to_fd(struct input *input,
1753 const char *mime_type, int fd)
1754{
1755 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1756
1757 return 0;
1758}
1759
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001760void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001761window_move(struct window *window, struct input *input, uint32_t time)
1762{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001763 if (!window->shell_surface)
1764 return;
1765
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001766 wl_shell_surface_move(window->shell_surface,
1767 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001768}
1769
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001770static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001771handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001772 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001773 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001774{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001775 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001776 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001777
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001778 /* FIXME: this is probably the wrong place to check for width
1779 * or height <= 0, but it prevents the compositor from crashing
1780 */
1781 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001782 return;
1783
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001784 window->resize_edges = edges;
1785
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001786 if (window->resize_handler) {
1787 child_width = width - 20 - window->margin * 2;
1788 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001789
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001790 (*window->resize_handler)(window,
1791 child_width, child_height,
1792 window->user_data);
1793 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001794 window->allocation.width = width;
1795 window->allocation.height = height;
1796
1797 if (window->redraw_handler)
1798 window_schedule_redraw(window);
1799 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001800}
1801
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001802static void
1803handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1804{
1805 struct window *window = data;
1806
1807 /* FIXME: Need more context in this event, at least the input
1808 * device. Or just use wl_callback. */
1809
1810 window_destroy(window);
1811}
1812
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001813static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001814 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001815 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001816};
1817
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001818void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001819window_get_allocation(struct window *window,
1820 struct rectangle *allocation)
1821{
1822 *allocation = window->allocation;
1823}
1824
1825void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001826window_get_child_allocation(struct window *window,
1827 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001828{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001829 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001830 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001831 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001832 allocation->x = window->margin + 10;
1833 allocation->y = window->margin + 50;
1834 allocation->width =
1835 window->allocation.width - 20 - window->margin * 2;
1836 allocation->height =
1837 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001838 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001839}
1840
1841void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001842window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001843{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001844 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001845 window->allocation.x = 20 + window->margin;
1846 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001847 window->allocation.width = width + 20 + window->margin * 2;
1848 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001849 } else {
1850 window->allocation.x = 0;
1851 window->allocation.y = 0;
1852 window->allocation.width = width;
1853 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001854 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001855}
1856
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001857static void
1858idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001859{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001860 struct window *window =
1861 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001862
1863 window->redraw_handler(window, window->user_data);
1864 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001865}
1866
1867void
1868window_schedule_redraw(struct window *window)
1869{
1870 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001871 window->redraw_task.run = idle_redraw;
1872 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001873 window->redraw_scheduled = 1;
1874 }
1875}
1876
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001877void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001878window_set_custom(struct window *window)
1879{
1880 window->type = TYPE_CUSTOM;
1881}
1882
1883void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001884window_set_fullscreen(struct window *window, int fullscreen)
1885{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001886 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001887 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001888
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001889 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001890 return;
1891
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001892 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001893 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001894 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001895 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001896 width = output->allocation.width;
1897 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001898 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001899 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001900 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001901 width = window->saved_allocation.width - 20 - window->margin * 2;
1902 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001903 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001904 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001905
1906 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001907}
1908
1909void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001910window_set_decoration(struct window *window, int decoration)
1911{
1912 window->decoration = decoration;
1913}
1914
1915void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001916window_set_user_data(struct window *window, void *data)
1917{
1918 window->user_data = data;
1919}
1920
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001921void *
1922window_get_user_data(struct window *window)
1923{
1924 return window->user_data;
1925}
1926
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001927void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001928window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001929 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001930{
1931 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001932}
1933
1934void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001935window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001936 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001937{
1938 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001939}
1940
1941void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001942window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001943 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001944{
1945 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001946}
1947
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001948void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001949window_set_button_handler(struct window *window,
1950 window_button_handler_t handler)
1951{
1952 window->button_handler = handler;
1953}
1954
1955void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001956window_set_motion_handler(struct window *window,
1957 window_motion_handler_t handler)
1958{
1959 window->motion_handler = handler;
1960}
1961
1962void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001963window_set_enter_handler(struct window *window,
1964 window_enter_handler_t handler)
1965{
1966 window->enter_handler = handler;
1967}
1968
1969void
1970window_set_leave_handler(struct window *window,
1971 window_leave_handler_t handler)
1972{
1973 window->leave_handler = handler;
1974}
1975
1976void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001977window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001978 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001979{
1980 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001981}
1982
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001983void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001984window_set_item_focus_handler(struct window *window,
1985 window_item_focus_handler_t handler)
1986{
1987 window->item_focus_handler = handler;
1988}
1989
1990void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001991window_set_data_handler(struct window *window, window_data_handler_t handler)
1992{
1993 window->data_handler = handler;
1994}
1995
1996void
1997window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1998{
1999 window->drop_handler = handler;
2000}
2001
2002void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002003window_set_transparent(struct window *window, int transparent)
2004{
2005 window->transparent = transparent;
2006}
2007
2008void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002009window_set_title(struct window *window, const char *title)
2010{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002011 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002012 window->title = strdup(title);
2013}
2014
2015const char *
2016window_get_title(struct window *window)
2017{
2018 return window->title;
2019}
2020
2021void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002022display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2023 int32_t x, int32_t y, int32_t width, int32_t height)
2024{
2025 struct wl_buffer *buffer;
2026
2027 buffer = display_get_buffer_for_surface(display, cairo_surface);
2028
2029 wl_buffer_damage(buffer, x, y, width, height);
2030}
2031
2032void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002033window_damage(struct window *window, int32_t x, int32_t y,
2034 int32_t width, int32_t height)
2035{
2036 wl_surface_damage(window->surface, x, y, width, height);
2037}
2038
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002039static struct window *
2040window_create_internal(struct display *display, struct window *parent,
2041 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002042{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002043 struct window *window;
2044
2045 window = malloc(sizeof *window);
2046 if (window == NULL)
2047 return NULL;
2048
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002049 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002050 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002051 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002052 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002053 if (display->shell) {
2054 window->shell_surface =
2055 wl_shell_get_shell_surface(display->shell,
2056 window->surface);
2057 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002058 window->allocation.x = 0;
2059 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002060 window->allocation.width = width;
2061 window->allocation.height = height;
2062 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002063 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002064 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002065 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002066 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002067
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002068 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002069#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002070 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2071 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002072#else
2073 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2074#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002075 else
2076 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002077
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002078 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002079 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002080
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002081 if (window->shell_surface) {
2082 wl_shell_surface_set_user_data(window->shell_surface, window);
2083 wl_shell_surface_add_listener(window->shell_surface,
2084 &shell_surface_listener, window);
2085 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002087 return window;
2088}
2089
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002090struct window *
2091window_create(struct display *display, int32_t width, int32_t height)
2092{
2093 struct window *window;
2094
2095 window = window_create_internal(display, NULL, width, height);
2096 if (!window)
2097 return NULL;
2098
2099 return window;
2100}
2101
2102struct window *
2103window_create_transient(struct display *display, struct window *parent,
2104 int32_t x, int32_t y, int32_t width, int32_t height)
2105{
2106 struct window *window;
2107
2108 window = window_create_internal(parent->display,
2109 parent, width, height);
2110 if (!window)
2111 return NULL;
2112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002113 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002114 window->x = x;
2115 window->y = y;
2116
2117 return window;
2118}
2119
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002120struct menu {
2121 struct window *window;
2122 const char **entries;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002123 uint32_t time;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002124 int current;
2125 int count;
2126};
2127
2128static int
2129menu_set_item(struct window *window, struct menu *menu, int sy)
2130{
2131 int next;
2132
2133 next = (sy - 8) / 20;
2134 if (menu->current != next) {
2135 menu->current = next;
2136 window_schedule_redraw(window);
2137 }
2138
2139 return POINTER_LEFT_PTR;
2140}
2141
2142static int
2143menu_motion_handler(struct window *window,
2144 struct input *input, uint32_t time,
2145 int32_t x, int32_t y,
2146 int32_t sx, int32_t sy, void *data)
2147{
2148 return menu_set_item(window, data, sy);
2149}
2150
2151static int
2152menu_enter_handler(struct window *window,
2153 struct input *input, uint32_t time,
2154 int32_t x, int32_t y, void *data)
2155{
2156 return menu_set_item(window, data, y);
2157}
2158
2159static void
2160menu_leave_handler(struct window *window,
2161 struct input *input, uint32_t time, void *data)
2162{
2163 menu_set_item(window, data, -200);
2164}
2165
2166static void
2167menu_button_handler(struct window *window,
2168 struct input *input, uint32_t time,
2169 int button, int state, void *data)
2170
2171{
2172 struct menu *menu = data;
2173
2174 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002175 if (state == 0 && time - menu->time > 500)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002176 window_destroy(window);
2177}
2178
2179static void
2180menu_redraw_handler(struct window *window, void *data)
2181{
2182 cairo_t *cr;
2183 const int32_t r = 3, margin = 3;
2184 struct menu *menu = data;
2185 int32_t width, height, i;
2186
2187 width = 200;
2188 height = menu->count * 20 + margin * 2;
2189 window_set_child_size(window, width, height);
2190 window_create_surface(window);
2191
2192 cr = cairo_create(window->cairo_surface);
2193 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2194 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2195 cairo_paint(cr);
2196
2197 width = window->allocation.width;
2198 height = window->allocation.height;
2199 rounded_rect(cr, 0, 0, width, height, r);
2200 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2201 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2202 cairo_fill(cr);
2203
2204 for (i = 0; i < menu->count; i++) {
2205 if (i == menu->current) {
2206 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2207 cairo_rectangle(cr, margin, i * 20 + margin,
2208 width - 2 * margin, 20);
2209 cairo_fill(cr);
2210 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2211 cairo_move_to(cr, 10, i * 20 + 16);
2212 cairo_show_text(cr, menu->entries[i]);
2213 } else {
2214 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2215 cairo_move_to(cr, 10, i * 20 + 16);
2216 cairo_show_text(cr, menu->entries[i]);
2217 }
2218 }
2219
2220 cairo_destroy(cr);
2221 window_flush(window);
2222}
2223
2224struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002225window_create_menu(struct display *display,
2226 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002227 int32_t x, int32_t y, const char **entries, int count)
2228{
2229 struct window *window;
2230 struct menu *menu;
2231
2232 menu = malloc(sizeof *menu);
2233 if (!menu)
2234 return NULL;
2235
2236 window = window_create_internal(parent->display, parent, 0, 0);
2237 if (!window)
2238 return NULL;
2239
2240 menu->window = window;
2241 menu->entries = entries;
2242 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002243 menu->time = time;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002244 window->decoration = 0;
2245 window->type = TYPE_MENU;
2246 window->x = x;
2247 window->y = y;
2248
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002249 wl_shell_surface_set_popup(window->shell_surface,
2250 input->input_device, time,
2251 window->parent->shell_surface,
2252 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002253
2254 window_set_motion_handler(window, menu_motion_handler);
2255 window_set_enter_handler(window, menu_enter_handler);
2256 window_set_leave_handler(window, menu_leave_handler);
2257 window_set_button_handler(window, menu_button_handler);
2258 window_set_redraw_handler(window, menu_redraw_handler);
2259 window_set_user_data(window, menu);
2260
2261 return window;
2262}
2263
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002264void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002265window_set_buffer_type(struct window *window, enum window_buffer_type type)
2266{
2267 window->buffer_type = type;
2268}
2269
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002270
2271static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002272display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002273 struct wl_output *wl_output,
2274 int x, int y,
2275 int physical_width,
2276 int physical_height,
2277 int subpixel,
2278 const char *make,
2279 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002280{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002281 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002282
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002283 output->allocation.x = x;
2284 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002285}
2286
2287static void
2288display_handle_mode(void *data,
2289 struct wl_output *wl_output,
2290 uint32_t flags,
2291 int width,
2292 int height,
2293 int refresh)
2294{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002295 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002296 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002297
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002298 if (flags & WL_OUTPUT_MODE_CURRENT) {
2299 output->allocation.width = width;
2300 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002301 if (display->output_configure_handler)
2302 (*display->output_configure_handler)(
2303 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002304 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002305}
2306
2307static const struct wl_output_listener output_listener = {
2308 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002309 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002310};
2311
2312static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002313display_add_output(struct display *d, uint32_t id)
2314{
2315 struct output *output;
2316
2317 output = malloc(sizeof *output);
2318 if (output == NULL)
2319 return;
2320
2321 memset(output, 0, sizeof *output);
2322 output->display = d;
2323 output->output =
2324 wl_display_bind(d->display, id, &wl_output_interface);
2325 wl_list_insert(d->output_list.prev, &output->link);
2326
2327 wl_output_add_listener(output->output, &output_listener, output);
2328}
2329
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002330static void
2331output_destroy(struct output *output)
2332{
2333 if (output->destroy_handler)
2334 (*output->destroy_handler)(output, output->user_data);
2335
2336 wl_output_destroy(output->output);
2337 wl_list_remove(&output->link);
2338 free(output);
2339}
2340
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002341void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002342display_set_output_configure_handler(struct display *display,
2343 display_output_handler_t handler)
2344{
2345 struct output *output;
2346
2347 display->output_configure_handler = handler;
2348 if (!handler)
2349 return;
2350
2351 wl_list_for_each(output, &display->output_list, link)
2352 (*display->output_configure_handler)(output,
2353 display->user_data);
2354}
2355
2356void
2357output_set_user_data(struct output *output, void *data)
2358{
2359 output->user_data = data;
2360}
2361
2362void *
2363output_get_user_data(struct output *output)
2364{
2365 return output->user_data;
2366}
2367
2368void
2369output_set_destroy_handler(struct output *output,
2370 display_output_handler_t handler)
2371{
2372 output->destroy_handler = handler;
2373 /* FIXME: implement this, once we have way to remove outputs */
2374}
2375
2376void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002377output_get_allocation(struct output *output, struct rectangle *allocation)
2378{
2379 *allocation = output->allocation;
2380}
2381
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002382struct wl_output *
2383output_get_wl_output(struct output *output)
2384{
2385 return output->output;
2386}
2387
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002388static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002389display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002390{
2391 struct input *input;
2392
2393 input = malloc(sizeof *input);
2394 if (input == NULL)
2395 return;
2396
2397 memset(input, 0, sizeof *input);
2398 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002399 input->input_device =
2400 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002401 input->pointer_focus = NULL;
2402 input->keyboard_focus = NULL;
2403 wl_list_insert(d->input_list.prev, &input->link);
2404
2405 wl_input_device_add_listener(input->input_device,
2406 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002407 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002408
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002409 input->data_device =
2410 wl_data_device_manager_get_data_device(d->data_device_manager,
2411 input->input_device);
2412 wl_data_device_add_listener(input->data_device,
2413 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002414}
2415
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002416static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002417input_destroy(struct input *input)
2418{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002419 input_remove_keyboard_focus(input);
2420 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002421
2422 if (input->drag_offer)
2423 data_offer_destroy(input->drag_offer);
2424
2425 if (input->selection_offer)
2426 data_offer_destroy(input->selection_offer);
2427
2428 wl_data_device_destroy(input->data_device);
2429 wl_list_remove(&input->link);
2430 wl_input_device_destroy(input->input_device);
2431 free(input);
2432}
2433
2434static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002435display_handle_global(struct wl_display *display, uint32_t id,
2436 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002437{
2438 struct display *d = data;
2439
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002440 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002441 d->compositor =
2442 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002443 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002444 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002445 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002446 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002447 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002448 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002449 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002450 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002451 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2452 d->data_device_manager =
2453 wl_display_bind(display, id,
2454 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002455 }
2456}
2457
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002458static void
2459display_render_frame(struct display *d)
2460{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002461 int radius = 8;
2462 cairo_t *cr;
2463
2464 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2465 cr = cairo_create(d->shadow);
2466 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2467 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2468 rounded_rect(cr, 16, 16, 112, 112, radius);
2469 cairo_fill(cr);
2470 cairo_destroy(cr);
2471 blur_surface(d->shadow, 64);
2472
2473 d->active_frame =
2474 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2475 cr = cairo_create(d->active_frame);
2476 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2477 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2478 rounded_rect(cr, 16, 16, 112, 112, radius);
2479 cairo_fill(cr);
2480 cairo_destroy(cr);
2481
2482 d->inactive_frame =
2483 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2484 cr = cairo_create(d->inactive_frame);
2485 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2486 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2487 rounded_rect(cr, 16, 16, 112, 112, radius);
2488 cairo_fill(cr);
2489 cairo_destroy(cr);
2490}
2491
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002492static void
2493init_xkb(struct display *d)
2494{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002495 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002496
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002497 names.rules = "evdev";
2498 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002499 names.layout = option_xkb_layout;
2500 names.variant = option_xkb_variant;
2501 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002502
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002503 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002504 if (!d->xkb) {
2505 fprintf(stderr, "Failed to compile keymap\n");
2506 exit(1);
2507 }
2508}
2509
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002510static void
2511fini_xkb(struct display *display)
2512{
2513 xkb_free_keymap(display->xkb);
2514}
2515
Yuval Fledel45568f62010-12-06 09:18:12 -05002516static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002517init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002518{
2519 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002520 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002521
2522 static const EGLint premul_argb_cfg_attribs[] = {
2523 EGL_SURFACE_TYPE,
2524 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2525 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002526 EGL_RED_SIZE, 1,
2527 EGL_GREEN_SIZE, 1,
2528 EGL_BLUE_SIZE, 1,
2529 EGL_ALPHA_SIZE, 1,
2530 EGL_DEPTH_SIZE, 1,
2531 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2532 EGL_NONE
2533 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002534
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002535 static const EGLint rgb_cfg_attribs[] = {
2536 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2537 EGL_RED_SIZE, 1,
2538 EGL_GREEN_SIZE, 1,
2539 EGL_BLUE_SIZE, 1,
2540 EGL_ALPHA_SIZE, 0,
2541 EGL_DEPTH_SIZE, 1,
2542 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2543 EGL_NONE
2544 };
2545
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002546 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002547 if (!eglInitialize(d->dpy, &major, &minor)) {
2548 fprintf(stderr, "failed to initialize display\n");
2549 return -1;
2550 }
2551
2552 if (!eglBindAPI(EGL_OPENGL_API)) {
2553 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2554 return -1;
2555 }
2556
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002557 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002558 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002559 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002560 return -1;
2561 }
2562
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002563 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2564 &d->rgb_config, 1, &n) || n != 1) {
2565 fprintf(stderr, "failed to choose rgb config\n");
2566 return -1;
2567 }
2568
Benjamin Franzke0c991632011-09-27 21:57:31 +02002569 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2570 if (d->rgb_ctx == NULL) {
2571 fprintf(stderr, "failed to create context\n");
2572 return -1;
2573 }
2574 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2575 EGL_NO_CONTEXT, NULL);
2576 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002577 fprintf(stderr, "failed to create context\n");
2578 return -1;
2579 }
2580
Benjamin Franzke0c991632011-09-27 21:57:31 +02002581 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002582 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002583 return -1;
2584 }
2585
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002586#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002587 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2588 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002589 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002590 return -1;
2591 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002592 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2593 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2594 fprintf(stderr, "failed to get cairo egl argb device\n");
2595 return -1;
2596 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002597#endif
2598
2599 return 0;
2600}
2601
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002602static void
2603fini_egl(struct display *display)
2604{
2605#ifdef HAVE_CAIRO_EGL
2606 cairo_device_destroy(display->argb_device);
2607 cairo_device_destroy(display->rgb_device);
2608#endif
2609
2610 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2611 EGL_NO_CONTEXT);
2612
2613 eglTerminate(display->dpy);
2614 eglReleaseThread();
2615}
2616
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002617static int
2618event_mask_update(uint32_t mask, void *data)
2619{
2620 struct display *d = data;
2621
2622 d->mask = mask;
2623
2624 return 0;
2625}
2626
2627static void
2628handle_display_data(struct task *task, uint32_t events)
2629{
2630 struct display *display =
2631 container_of(task, struct display, display_task);
2632
2633 wl_display_iterate(display->display, display->mask);
2634}
2635
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002636struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002637display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002638{
2639 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002640 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002641 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002642 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002643
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002644 g_type_init();
2645
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002646 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002647 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002648 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002649
2650 xkb_option_group = g_option_group_new("xkb",
2651 "Keyboard options",
2652 "Show all XKB options",
2653 NULL, NULL);
2654 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2655 g_option_context_add_group (context, xkb_option_group);
2656
2657 if (!g_option_context_parse(context, argc, argv, &error)) {
2658 fprintf(stderr, "option parsing failed: %s\n", error->message);
2659 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002660 }
2661
Tim Wiederhake748f6722011-01-23 23:25:25 +01002662 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002663
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002664 d = malloc(sizeof *d);
2665 if (d == NULL)
2666 return NULL;
2667
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002668 memset(d, 0, sizeof *d);
2669
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002670 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002671 if (d->display == NULL) {
2672 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002673 return NULL;
2674 }
2675
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002676 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2677 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2678 d->display_task.run = handle_display_data;
2679 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2680
2681 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002682 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002683 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002684
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002685 /* Set up listener so we'll catch all events. */
2686 wl_display_add_global_listener(d->display,
2687 display_handle_global, d);
2688
2689 /* Process connection events. */
2690 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002691 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002692 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002693
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002694 d->image_target_texture_2d =
2695 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2696 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2697 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2698
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002699 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002700
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002701 display_render_frame(d);
2702
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002703 wl_list_init(&d->window_list);
2704
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002705 init_xkb(d);
2706
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002707 return d;
2708}
2709
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002710static void
2711display_destroy_outputs(struct display *display)
2712{
2713 struct output *tmp;
2714 struct output *output;
2715
2716 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2717 output_destroy(output);
2718}
2719
Pekka Paalanene1207c72011-12-16 12:02:09 +02002720static void
2721display_destroy_inputs(struct display *display)
2722{
2723 struct input *tmp;
2724 struct input *input;
2725
2726 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2727 input_destroy(input);
2728}
2729
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002730void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002731display_destroy(struct display *display)
2732{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002733 if (!wl_list_empty(&display->window_list))
2734 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2735
2736 if (!wl_list_empty(&display->deferred_list))
2737 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2738
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002739 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002740 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002741
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002742 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002743
2744 cairo_surface_destroy(display->active_frame);
2745 cairo_surface_destroy(display->inactive_frame);
2746 cairo_surface_destroy(display->shadow);
2747 destroy_pointer_surfaces(display);
2748
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002749 fini_egl(display);
2750
Pekka Paalanenc2052982011-12-16 11:41:32 +02002751 if (display->shell)
2752 wl_shell_destroy(display->shell);
2753
2754 if (display->shm)
2755 wl_shm_destroy(display->shm);
2756
2757 if (display->data_device_manager)
2758 wl_data_device_manager_destroy(display->data_device_manager);
2759
2760 wl_compositor_destroy(display->compositor);
2761
2762 close(display->epoll_fd);
2763
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002764 wl_display_flush(display->display);
2765 wl_display_destroy(display->display);
2766 free(display);
2767}
2768
2769void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002770display_set_user_data(struct display *display, void *data)
2771{
2772 display->user_data = data;
2773}
2774
2775void *
2776display_get_user_data(struct display *display)
2777{
2778 return display->user_data;
2779}
2780
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002781struct wl_display *
2782display_get_display(struct display *display)
2783{
2784 return display->display;
2785}
2786
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002787struct output *
2788display_get_output(struct display *display)
2789{
2790 return container_of(display->output_list.next, struct output, link);
2791}
2792
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002793struct wl_compositor *
2794display_get_compositor(struct display *display)
2795{
2796 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002797}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002798
2799EGLDisplay
2800display_get_egl_display(struct display *d)
2801{
2802 return d->dpy;
2803}
2804
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002805struct wl_data_source *
2806display_create_data_source(struct display *display)
2807{
2808 return wl_data_device_manager_create_data_source(display->data_device_manager);
2809}
2810
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002811EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002812display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002813{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002814 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002815}
2816
Benjamin Franzke0c991632011-09-27 21:57:31 +02002817EGLConfig
2818display_get_argb_egl_config(struct display *d)
2819{
2820 return d->premultiplied_argb_config;
2821}
2822
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002823struct wl_shell *
2824display_get_shell(struct display *display)
2825{
2826 return display->shell;
2827}
2828
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002829int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002830display_acquire_window_surface(struct display *display,
2831 struct window *window,
2832 EGLContext ctx)
2833{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002834#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002835 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002836 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002837
2838 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002839 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002840 device = cairo_surface_get_device(window->cairo_surface);
2841 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002842 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002843
Benjamin Franzke0c991632011-09-27 21:57:31 +02002844 if (!ctx) {
2845 if (device == display->rgb_device)
2846 ctx = display->rgb_ctx;
2847 else if (device == display->argb_device)
2848 ctx = display->argb_ctx;
2849 else
2850 assert(0);
2851 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002852
2853 data = cairo_surface_get_user_data(window->cairo_surface,
2854 &surface_data_key);
2855
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002856 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002857 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002858 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2859 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002860
2861 return 0;
2862#else
2863 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002864#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002865}
2866
2867void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002868display_release_window_surface(struct display *display,
2869 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002870{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002871#ifdef HAVE_CAIRO_EGL
2872 cairo_device_t *device;
2873
2874 device = cairo_surface_get_device(window->cairo_surface);
2875 if (!device)
2876 return;
2877
2878 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002879 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002880 cairo_device_release(device);
2881#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002882}
2883
2884void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002885display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002886{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002887 wl_list_insert(&display->deferred_list, &task->link);
2888}
2889
2890void
2891display_watch_fd(struct display *display,
2892 int fd, uint32_t events, struct task *task)
2893{
2894 struct epoll_event ep;
2895
2896 ep.events = events;
2897 ep.data.ptr = task;
2898 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2899}
2900
2901void
2902display_run(struct display *display)
2903{
2904 struct task *task;
2905 struct epoll_event ep[16];
2906 int i, count;
2907
Pekka Paalanen826d7952011-12-15 10:14:07 +02002908 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002909 while (1) {
2910 while (display->mask & WL_DISPLAY_WRITABLE)
2911 wl_display_iterate(display->display,
2912 WL_DISPLAY_WRITABLE);
2913
Pekka Paalanen826d7952011-12-15 10:14:07 +02002914 if (!display->running)
2915 break;
2916
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002917 count = epoll_wait(display->epoll_fd,
2918 ep, ARRAY_LENGTH(ep), -1);
2919 for (i = 0; i < count; i++) {
2920 task = ep[i].data.ptr;
2921 task->run(task, ep[i].events);
2922 }
2923
2924 while (!wl_list_empty(&display->deferred_list)) {
2925 task = container_of(display->deferred_list.next,
2926 struct task, link);
2927 wl_list_remove(&task->link);
2928 task->run(task, 0);
2929 }
2930 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002931}
Pekka Paalanen826d7952011-12-15 10:14:07 +02002932
2933void
2934display_exit(struct display *display)
2935{
2936 display->running = 0;
2937}