blob: a101204599efdccc431ea6c35cc9ff9771724a08 [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øgsberg4f7dcd62012-01-06 21:59:05 -0500140 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400141
142 struct wl_list item_list;
143 struct item *focus_item;
144 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400145
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500146 struct window *menu;
147
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500148 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400149 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500150};
151
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400152struct item {
153 struct wl_list link;
154 struct rectangle allocation;
155 void *user_data;
156};
157
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400158struct input {
159 struct display *display;
160 struct wl_input_device *input_device;
161 struct window *pointer_focus;
162 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400163 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164 uint32_t modifiers;
165 int32_t x, y, sx, sy;
166 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400167
168 struct wl_data_device *data_device;
169 struct data_offer *drag_offer;
170 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171};
172
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500173struct output {
174 struct display *display;
175 struct wl_output *output;
176 struct rectangle allocation;
177 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200178
179 display_output_handler_t destroy_handler;
180 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500181};
182
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500183struct menu {
184 struct window *window;
185 const char **entries;
186 uint32_t time;
187 int current;
188 int count;
189 menu_func_t func;
190};
191
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400192enum {
193 POINTER_DEFAULT = 100,
194 POINTER_UNSET
195};
196
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500197enum window_location {
198 WINDOW_INTERIOR = 0,
199 WINDOW_RESIZING_TOP = 1,
200 WINDOW_RESIZING_BOTTOM = 2,
201 WINDOW_RESIZING_LEFT = 4,
202 WINDOW_RESIZING_TOP_LEFT = 5,
203 WINDOW_RESIZING_BOTTOM_LEFT = 6,
204 WINDOW_RESIZING_RIGHT = 8,
205 WINDOW_RESIZING_TOP_RIGHT = 9,
206 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
207 WINDOW_RESIZING_MASK = 15,
208 WINDOW_EXTERIOR = 16,
209 WINDOW_TITLEBAR = 17,
210 WINDOW_CLIENT_AREA = 18,
211};
212
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400213const char *option_xkb_layout = "us";
214const char *option_xkb_variant = "";
215const char *option_xkb_options = "";
216
217static const GOptionEntry xkb_option_entries[] = {
218 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
219 &option_xkb_layout, "XKB Layout" },
220 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
221 &option_xkb_variant, "XKB Variant" },
222 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
223 &option_xkb_options, "XKB Options" },
224 { NULL }
225};
226
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400227static const cairo_user_data_key_t surface_data_key;
228struct surface_data {
229 struct wl_buffer *buffer;
230};
231
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500232#define MULT(_d,c,a,t) \
233 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
234
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500235#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400236
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100237struct egl_window_surface_data {
238 struct display *display;
239 struct wl_surface *surface;
240 struct wl_egl_window *window;
241 EGLSurface surf;
242};
243
244static void
245egl_window_surface_data_destroy(void *p)
246{
247 struct egl_window_surface_data *data = p;
248 struct display *d = data->display;
249
250 eglDestroySurface(d->dpy, data->surf);
251 wl_egl_window_destroy(data->window);
252 data->surface = NULL;
253
254 free(p);
255}
256
257static cairo_surface_t *
258display_create_egl_window_surface(struct display *display,
259 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400260 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100261 struct rectangle *rectangle)
262{
263 cairo_surface_t *cairo_surface;
264 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400265 EGLConfig config;
266 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200267 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100268
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400269 static const EGLint premul_attribs[] = {
270 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
271 EGL_NONE
272 };
273
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100274 data = malloc(sizeof *data);
275 if (data == NULL)
276 return NULL;
277
278 data->display = display;
279 data->surface = surface;
280
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400281 if (flags & SURFACE_OPAQUE) {
282 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200283 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200284 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400285 } else {
286 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200287 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400288 attribs = premul_attribs;
289 }
290
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400291 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100292 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100294
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 data->surf = eglCreateWindowSurface(display->dpy, config,
296 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Benjamin Franzke0c991632011-09-27 21:57:31 +0200298 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 data->surf,
300 rectangle->width,
301 rectangle->height);
302
303 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
304 data, egl_window_surface_data_destroy);
305
306 return cairo_surface;
307}
308
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500309struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200311 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 EGLImageKHR image;
313 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800314 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316};
317
318static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400320{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323
Benjamin Franzke0c991632011-09-27 21:57:31 +0200324 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500328 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500330 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332}
333
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500334EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335display_get_image_for_egl_image_surface(struct display *display,
336 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500339
340 data = cairo_surface_get_user_data (surface, &surface_data_key);
341
342 return data->image;
343}
344
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500345static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500346display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400347 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351 EGLDisplay dpy = display->dpy;
352 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200353 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354
355 data = malloc(sizeof *data);
356 if (data == NULL)
357 return NULL;
358
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800359 data->display = display;
360
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400361 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000364 free(data);
365 return NULL;
366 }
367
Benjamin Franzke0c991632011-09-27 21:57:31 +0200368 if (flags & SURFACE_OPAQUE) {
369 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200370 content = CAIRO_CONTENT_COLOR;
371 } else {
372 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 content = CAIRO_CONTENT_COLOR_ALPHA;
374 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500376 data->image = display->create_image(dpy, NULL,
377 EGL_NATIVE_PIXMAP_KHR,
378 (EGLClientBuffer) data->pixmap,
379 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500380 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500381 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 free(data);
383 return NULL;
384 }
385
386 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400387 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500388
Benjamin Franzke0c991632011-09-27 21:57:31 +0200389 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500392 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 surface = cairo_gl_surface_create_for_texture(data->device,
396 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397 data->texture,
398 rectangle->width,
399 rectangle->height);
400
401 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500402 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403
404 return surface;
405}
406
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500407static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500408display_create_egl_image_surface_from_file(struct display *display,
409 const char *filename,
410 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411{
412 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413 GdkPixbuf *pixbuf;
414 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400415 int stride, i;
416 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500417 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400418
419 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421 FALSE, &error);
422 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400425 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
426 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500427 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400430
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400431
432 stride = gdk_pixbuf_get_rowstride(pixbuf);
433 pixels = gdk_pixbuf_get_pixels(pixbuf);
434
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400436 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400438 while (p < end) {
439 unsigned int t;
440
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441 MULT(p[0], p[0], p[3], t);
442 MULT(p[1], p[1], p[3], t);
443 MULT(p[2], p[2], p[3], t);
444 p += 4;
445
446 }
447 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400448
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200449 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000450 if (surface == NULL) {
451 g_object_unref(pixbuf);
452 return NULL;
453 }
454
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800455 data = cairo_surface_get_user_data(surface, &surface_data_key);
456
Benjamin Franzke0c991632011-09-27 21:57:31 +0200457 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800458 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800459 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
460 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200461 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400462
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500463 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400464
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465 return surface;
466}
467
468#endif
469
470struct wl_buffer *
471display_get_buffer_for_surface(struct display *display,
472 cairo_surface_t *surface)
473{
474 struct surface_data *data;
475
476 data = cairo_surface_get_user_data (surface, &surface_data_key);
477
478 return data->buffer;
479}
480
481struct shm_surface_data {
482 struct surface_data data;
483 void *map;
484 size_t length;
485};
486
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500487static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488shm_surface_data_destroy(void *p)
489{
490 struct shm_surface_data *data = p;
491
492 wl_buffer_destroy(data->data.buffer);
493 munmap(data->map, data->length);
494}
495
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500496static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400498 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499{
500 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800503 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400504 char filename[] = "/tmp/wayland-shm-XXXXXX";
505
506 data = malloc(sizeof *data);
507 if (data == NULL)
508 return NULL;
509
510 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
511 rectangle->width);
512 data->length = stride * rectangle->height;
513 fd = mkstemp(filename);
514 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000515 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516 return NULL;
517 }
518 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000519 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520 close(fd);
521 return NULL;
522 }
523
524 data->map = mmap(NULL, data->length,
525 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
526 unlink(filename);
527
528 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000529 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530 close(fd);
531 return NULL;
532 }
533
534 surface = cairo_image_surface_create_for_data (data->map,
535 CAIRO_FORMAT_ARGB32,
536 rectangle->width,
537 rectangle->height,
538 stride);
539
540 cairo_surface_set_user_data (surface, &surface_data_key,
541 data, shm_surface_data_destroy);
542
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400543 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400544 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400545 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400546 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548 data->data.buffer = wl_shm_create_buffer(display->shm,
549 fd,
550 rectangle->width,
551 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400552 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553
554 close(fd);
555
556 return surface;
557}
558
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500559static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560display_create_shm_surface_from_file(struct display *display,
561 const char *filename,
562 struct rectangle *rect)
563{
564 cairo_surface_t *surface;
565 GdkPixbuf *pixbuf;
566 GError *error = NULL;
567 int stride, i;
568 unsigned char *pixels, *p, *end, *dest_data;
569 int dest_stride;
570 uint32_t *d;
571
572 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
573 rect->width, rect->height,
574 FALSE, &error);
575 if (error != NULL)
576 return NULL;
577
578 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
579 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500580 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581 return NULL;
582 }
583
584 stride = gdk_pixbuf_get_rowstride(pixbuf);
585 pixels = gdk_pixbuf_get_pixels(pixbuf);
586
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400587 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000588 if (surface == NULL) {
589 g_object_unref(pixbuf);
590 return NULL;
591 }
592
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593 dest_data = cairo_image_surface_get_data (surface);
594 dest_stride = cairo_image_surface_get_stride (surface);
595
596 for (i = 0; i < rect->height; i++) {
597 d = (uint32_t *) (dest_data + i * dest_stride);
598 p = pixels + i * stride;
599 end = p + rect->width * 4;
600 while (p < end) {
601 unsigned int t;
602 unsigned char a, r, g, b;
603
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604 a = p[3];
605 MULT(r, p[0], a, t);
606 MULT(g, p[1], a, t);
607 MULT(b, p[2], a, t);
608 p += 4;
609 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
610 }
611 }
612
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500613 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614
615 return surface;
616}
617
nobled7b87cb02011-02-01 18:51:47 +0000618static int
619check_size(struct rectangle *rect)
620{
621 if (rect->width && rect->height)
622 return 0;
623
624 fprintf(stderr, "tried to create surface of "
625 "width: %d, height: %d\n", rect->width, rect->height);
626 return -1;
627}
628
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629cairo_surface_t *
630display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400632 struct rectangle *rectangle,
633 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634{
nobled7b87cb02011-02-01 18:51:47 +0000635 if (check_size(rectangle) < 0)
636 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500637#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500638 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100639 if (surface)
640 return display_create_egl_window_surface(display,
641 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400642 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643 rectangle);
644 else
645 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400646 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500648 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400650 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651}
652
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500653static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654display_create_surface_from_file(struct display *display,
655 const char *filename,
656 struct rectangle *rectangle)
657{
nobled7b87cb02011-02-01 18:51:47 +0000658 if (check_size(rectangle) < 0)
659 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500660#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500661 if (display->dpy) {
662 return display_create_egl_image_surface_from_file(display,
663 filename,
664 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500665 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400666#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500667 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668}
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670 const char *filename;
671 int hotspot_x, hotspot_y;
672} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500673 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
674 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
675 { DATADIR "/weston/bottom_side.png", 16, 20 },
676 { DATADIR "/weston/grabbing.png", 20, 17 },
677 { DATADIR "/weston/left_ptr.png", 10, 5 },
678 { DATADIR "/weston/left_side.png", 10, 20 },
679 { DATADIR "/weston/right_side.png", 30, 19 },
680 { DATADIR "/weston/top_left_corner.png", 8, 8 },
681 { DATADIR "/weston/top_right_corner.png", 26, 8 },
682 { DATADIR "/weston/top_side.png", 18, 8 },
683 { DATADIR "/weston/xterm.png", 15, 15 },
684 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400685};
686
687static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689{
690 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 display->pointer_surfaces =
696 malloc(count * sizeof *display->pointer_surfaces);
697 rect.width = width;
698 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701 display_create_surface_from_file(display,
702 pointer_images[i].filename,
703 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100704 if (!display->pointer_surfaces[i]) {
705 fprintf(stderr, "Error loading pointer image: %s\n",
706 pointer_images[i].filename);
707 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400708 }
709
710}
711
Pekka Paalanen325bb602011-12-19 10:31:45 +0200712static void
713destroy_pointer_surfaces(struct display *display)
714{
715 int i, count;
716
717 count = ARRAY_LENGTH(pointer_images);
718 for (i = 0; i < count; ++i) {
719 if (display->pointer_surfaces[i])
720 cairo_surface_destroy(display->pointer_surfaces[i]);
721 }
722 free(display->pointer_surfaces);
723}
724
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400725cairo_surface_t *
726display_get_pointer_surface(struct display *display, int pointer,
727 int *width, int *height,
728 int *hotspot_x, int *hotspot_y)
729{
730 cairo_surface_t *surface;
731
732 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500733#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 *width = cairo_gl_surface_get_width(surface);
735 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000736#else
737 *width = cairo_image_surface_get_width(surface);
738 *height = cairo_image_surface_get_height(surface);
739#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400740 *hotspot_x = pointer_images[pointer].hotspot_x;
741 *hotspot_y = pointer_images[pointer].hotspot_y;
742
743 return cairo_surface_reference(surface);
744}
745
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400746static void
747window_attach_surface(struct window *window);
748
749static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400750free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400751{
752 struct window *window = data;
753
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400755 cairo_surface_destroy(window->pending_surface);
756 window->pending_surface = NULL;
757 if (window->cairo_surface)
758 window_attach_surface(window);
759}
760
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400761static const struct wl_callback_listener free_surface_listener = {
762 free_surface
763};
764
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500765static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200766window_get_resize_dx_dy(struct window *window, int *x, int *y)
767{
768 if (window->resize_edges & WINDOW_RESIZING_LEFT)
769 *x = window->server_allocation.width - window->allocation.width;
770 else
771 *x = 0;
772
773 if (window->resize_edges & WINDOW_RESIZING_TOP)
774 *y = window->server_allocation.height -
775 window->allocation.height;
776 else
777 *y = 0;
778
779 window->resize_edges = 0;
780}
781
782static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400783window_set_type(struct window *window)
784{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200785 if (!window->shell_surface)
786 return;
787
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400788 switch (window->type) {
789 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200790 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 break;
792 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200793 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 break;
795 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200796 wl_shell_surface_set_transient(window->shell_surface,
797 window->parent->shell_surface,
798 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400799 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500800 case TYPE_MENU:
801 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 case TYPE_CUSTOM:
803 break;
804 }
805}
806
807static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500808window_attach_surface(struct window *window)
809{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400810 struct display *display = window->display;
811 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400812 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500816 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400818 if (display->shell)
819 window_set_type(window);
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
832 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200835 window_get_resize_dx_dy(window, &x, &y);
836
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 if (window->pending_surface != NULL)
838 return;
839
840 window->pending_surface = window->cairo_surface;
841 window->cairo_surface = NULL;
842
843 buffer =
844 display_get_buffer_for_surface(display,
845 window->pending_surface);
846
847 wl_surface_attach(window->surface, buffer, x, y);
848 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400849 cb = wl_display_sync(display->display);
850 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000852 default:
853 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500855
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500856 wl_surface_damage(window->surface, 0, 0,
857 window->allocation.width,
858 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500859}
860
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500861void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400862window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100864 if (window->cairo_surface) {
865 switch (window->buffer_type) {
866 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
867 case WINDOW_BUFFER_TYPE_SHM:
868 display_surface_damage(window->display,
869 window->cairo_surface,
870 0, 0,
871 window->allocation.width,
872 window->allocation.height);
873 break;
874 default:
875 break;
876 }
877 window_attach_surface(window);
878 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500879}
880
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400881void
882window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500884 cairo_surface_reference(surface);
885
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886 if (window->cairo_surface != NULL)
887 cairo_surface_destroy(window->cairo_surface);
888
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500889 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890}
891
Benjamin Franzke22d54812011-07-16 19:50:32 +0000892#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893static void
894window_resize_cairo_window_surface(struct window *window)
895{
896 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898
899 data = cairo_surface_get_user_data(window->cairo_surface,
900 &surface_data_key);
901
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200902 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 wl_egl_window_resize(data->window,
904 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 window->allocation.height,
906 x,y);
907
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 cairo_gl_surface_set_size(window->cairo_surface,
909 window->allocation.width,
910 window->allocation.height);
911}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000912#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400914struct display *
915window_get_display(struct window *window)
916{
917 return window->display;
918}
919
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920void
921window_create_surface(struct window *window)
922{
923 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400924 uint32_t flags = 0;
925
926 if (!window->transparent)
927 flags = SURFACE_OPAQUE;
928
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500930#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100931 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
932 if (window->cairo_surface) {
933 window_resize_cairo_window_surface(window);
934 return;
935 }
936 surface = display_create_surface(window->display,
937 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400938 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500940 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400941 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944 break;
945#endif
946 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400947 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400948 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400949 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800950 default:
951 surface = NULL;
952 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400954
955 window_set_surface(window, surface);
956 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957}
958
959static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500960window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500961{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500962 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500963 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400964 cairo_surface_t *frame;
965 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500966
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400967 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400968
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400969 width = window->allocation.width;
970 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500971
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500972 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500973
Kristian Høgsberg09531622010-06-14 23:22:15 -0400974 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400975 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400976 cairo_paint(cr);
977
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400978 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400979 tile_mask(cr, window->display->shadow,
980 shadow_dx, shadow_dy, width, height,
981 window->margin + 10 - shadow_dx,
982 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500983
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400984 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400985 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400986 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 frame = window->display->inactive_frame;
988
989 tile_source(cr, frame, 0, 0, width, height,
990 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500991
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500992 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
993 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500994 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400995 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400996 if (window->keyboard_device)
997 cairo_set_source_rgb(cr, 0, 0, 0);
998 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400999 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001000 cairo_show_text(cr, window->title);
1001
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001002 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001003}
1004
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001005void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001006window_destroy(struct window *window)
1007{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001008 struct display *display = window->display;
1009 struct input *input;
1010
1011 if (window->redraw_scheduled)
1012 wl_list_remove(&window->redraw_task.link);
1013
1014 wl_list_for_each(input, &display->input_list, link) {
1015 if (input->pointer_focus == window)
1016 input->pointer_focus = NULL;
1017 if (input->keyboard_focus == window)
1018 input->keyboard_focus = NULL;
1019 }
1020
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001021 if (window->shell_surface)
1022 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001023 wl_surface_destroy(window->surface);
1024 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001025
1026 if (window->cairo_surface != NULL)
1027 cairo_surface_destroy(window->cairo_surface);
1028 if (window->pending_surface != NULL)
1029 cairo_surface_destroy(window->pending_surface);
1030
1031 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001032 free(window);
1033}
1034
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001035static struct item *
1036window_find_item(struct window *window, int32_t x, int32_t y)
1037{
1038 struct item *item;
1039
1040 wl_list_for_each(item, &window->item_list, link) {
1041 if (item->allocation.x <= x &&
1042 x < item->allocation.x + item->allocation.width &&
1043 item->allocation.y <= y &&
1044 y < item->allocation.y + item->allocation.height) {
1045 return item;
1046 }
1047 }
1048
1049 return NULL;
1050}
1051
1052struct item *
1053window_add_item(struct window *window, void *data)
1054{
1055 struct item *item;
1056
1057 item = malloc(sizeof *item);
1058 memset(item, 0, sizeof *item);
1059 item->user_data = data;
1060 wl_list_insert(window->item_list.prev, &item->link);
1061
1062 return item;
1063}
1064
1065void
1066window_for_each_item(struct window *window, item_func_t func, void *data)
1067{
1068 struct item *item;
1069
1070 wl_list_for_each(item, &window->item_list, link)
1071 func(item, data);
1072}
1073
1074struct item *
1075window_get_focus_item(struct window *window)
1076{
1077 return window->focus_item;
1078}
1079
1080void
1081item_get_allocation(struct item *item, struct rectangle *allocation)
1082{
1083 *allocation = item->allocation;
1084}
1085
1086void
1087item_set_allocation(struct item *item,
1088 int32_t x, int32_t y, int32_t width, int32_t height)
1089{
1090 item->allocation.x = x;
1091 item->allocation.y = y;
1092 item->allocation.width = width;
1093 item->allocation.height = height;
1094}
1095
1096void *
1097item_get_user_data(struct item *item)
1098{
1099 return item->user_data;
1100}
1101
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001102void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001103window_draw(struct window *window)
1104{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001105 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001106 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001107 else
1108 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001109}
1110
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001111cairo_surface_t *
1112window_get_surface(struct window *window)
1113{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001114 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001115}
1116
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001117struct wl_surface *
1118window_get_wl_surface(struct window *window)
1119{
1120 return window->surface;
1121}
1122
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001123struct wl_shell_surface *
1124window_get_wl_shell_surface(struct window *window)
1125{
1126 return window->shell_surface;
1127}
1128
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001129static int
1130get_pointer_location(struct window *window, int32_t x, int32_t y)
1131{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001132 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001133 const int grip_size = 8;
1134
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001135 if (!window->decoration)
1136 return WINDOW_CLIENT_AREA;
1137
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001138 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001139 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140 else if (window->margin <= x && x < window->margin + grip_size)
1141 hlocation = WINDOW_RESIZING_LEFT;
1142 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144 else if (x < window->allocation.width - window->margin)
1145 hlocation = WINDOW_RESIZING_RIGHT;
1146 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001147 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001148
1149 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001150 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001151 else if (window->margin <= y && y < window->margin + grip_size)
1152 vlocation = WINDOW_RESIZING_TOP;
1153 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001154 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001155 else if (y < window->allocation.height - window->margin)
1156 vlocation = WINDOW_RESIZING_BOTTOM;
1157 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001158 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001159
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001160 location = vlocation | hlocation;
1161 if (location & WINDOW_EXTERIOR)
1162 location = WINDOW_EXTERIOR;
1163 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1164 location = WINDOW_TITLEBAR;
1165 else if (location == WINDOW_INTERIOR)
1166 location = WINDOW_CLIENT_AREA;
1167
1168 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169}
1170
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001171static int
1172input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001174 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001175
1176 location = get_pointer_location(input->pointer_focus,
1177 input->sx, input->sy);
1178 switch (location) {
1179 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001180 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001181 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001182 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001183 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001184 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001185 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001186 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001187 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001188 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001189 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001190 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001191 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001192 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001194 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195 case WINDOW_EXTERIOR:
1196 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001197 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001198 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001199 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001200 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001201}
1202
1203void
1204input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1205{
1206 struct display *display = input->display;
1207 struct wl_buffer *buffer;
1208 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001210 if (pointer == input->current_pointer_image)
1211 return;
1212
1213 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001214 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001215
1216 if (!surface)
1217 return;
1218
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001219 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001220 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 pointer_images[pointer].hotspot_x,
1222 pointer_images[pointer].hotspot_y);
1223}
1224
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001225static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001226window_set_focus_item(struct window *window, struct item *focus)
1227{
1228 void *data;
1229
1230 if (focus == window->focus_item)
1231 return;
1232
1233 window->focus_item = focus;
1234 data = focus ? focus->user_data : NULL;
1235 if (window->item_focus_handler)
1236 window->item_focus_handler(window, focus, data);
1237}
1238
1239static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001240input_handle_motion(void *data, struct wl_input_device *input_device,
1241 uint32_t time,
1242 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001243{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001244 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001246 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001247 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001248
1249 input->x = x;
1250 input->y = y;
1251 input->sx = sx;
1252 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001253
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001254 if (!window->focus_item || !window->item_grab_button) {
1255 item = window_find_item(window, sx, sy);
1256 window_set_focus_item(window, item);
1257 }
1258
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 if (window->motion_handler)
1260 pointer = (*window->motion_handler)(window, input, time,
1261 x, y, sx, sy,
1262 window->user_data);
1263
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001264 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001265 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001266}
1267
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001268static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001269window_menu_func(struct window *window, int index, void *data)
1270{
1271 switch (index) {
1272 case 0: /* close */
1273 if (window->close_handler)
1274 window->close_handler(window->parent,
1275 window->user_data);
1276 else
1277 exit(0);
1278 break;
1279 case 1: /* fullscreen */
1280 /* we don't have a way to get out of fullscreen for now */
1281 window_set_fullscreen(window, 1);
1282 break;
1283 case 2: /* rotate */
1284 case 3: /* scale */
1285 break;
1286 }
1287}
1288
1289
1290static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001291input_handle_button(void *data,
1292 struct wl_input_device *input_device,
1293 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001294{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001295 struct input *input = data;
1296 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001297 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001298 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001299 int32_t x, y;
1300 static const char *entries[] = {
1301 "Close", "Fullscreen", "Rotate", "Scale"
1302 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001303
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001304 if (window->focus_item && window->item_grab_button == 0 && state)
1305 window->item_grab_button = button;
1306
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001308
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001309 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001311 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001312 if (!window->shell_surface)
1313 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001315 wl_shell_surface_move(window->shell_surface,
1316 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001317 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001318 case WINDOW_RESIZING_TOP:
1319 case WINDOW_RESIZING_BOTTOM:
1320 case WINDOW_RESIZING_LEFT:
1321 case WINDOW_RESIZING_RIGHT:
1322 case WINDOW_RESIZING_TOP_LEFT:
1323 case WINDOW_RESIZING_TOP_RIGHT:
1324 case WINDOW_RESIZING_BOTTOM_LEFT:
1325 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001326 if (!window->shell_surface)
1327 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001328 wl_shell_surface_resize(window->shell_surface,
1329 input_device, time,
1330 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001331 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001332 case WINDOW_CLIENT_AREA:
1333 if (window->button_handler)
1334 (*window->button_handler)(window,
1335 input, time,
1336 button, state,
1337 window->user_data);
1338 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001339 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001340 } else if (button == BTN_RIGHT && state == 1) {
1341 switch (location) {
1342 default:
1343 input_get_position(input, &x, &y);
1344 window->menu = window_create_menu(window->display,
1345 input, time,
1346 window,
1347 x - 10, y - 10,
1348 window_menu_func,
1349 entries, 4);
1350 window_schedule_redraw(window->menu);
1351 break;
1352 case WINDOW_CLIENT_AREA:
1353 if (window->button_handler)
1354 (*window->button_handler)(window,
1355 input, time,
1356 button, state,
1357 window->user_data);
1358 break;
1359 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001360 } else {
1361 if (window->button_handler)
1362 (*window->button_handler)(window,
1363 input, time,
1364 button, state,
1365 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001366 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001367
1368 if (window->focus_item &&
1369 window->item_grab_button == button && !state) {
1370 window->item_grab_button = 0;
1371 item = window_find_item(window, input->sx, input->sy);
1372 window_set_focus_item(window, item);
1373 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001374}
1375
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001376static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001377input_handle_key(void *data, struct wl_input_device *input_device,
1378 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001379{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001380 struct input *input = data;
1381 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001382 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001383 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001384
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001385 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001386 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001387 return;
1388
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001389 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001390 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001391 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1392 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001393
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001394 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1395
1396 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001397 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001398 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001399 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001400
1401 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001402 (*window->key_handler)(window, input, time, key, sym, state,
1403 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001404}
1405
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001406static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001407input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001408{
1409 struct window *window = input->pointer_focus;
1410
1411 if (!window)
1412 return;
1413
1414 window_set_focus_item(window, NULL);
1415
1416 if (window->leave_handler)
1417 window->leave_handler(window, input, time, window->user_data);
1418 input->pointer_focus = NULL;
1419 input->current_pointer_image = POINTER_UNSET;
1420}
1421
1422static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001423input_handle_pointer_focus(void *data,
1424 struct wl_input_device *input_device,
1425 uint32_t time, struct wl_surface *surface,
1426 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001427{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001428 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001429 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001430 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001431 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001432
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001433 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001434 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001435 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001436
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001437 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001438 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001439 window = input->pointer_focus;
1440
Kristian Høgsberg59826582011-01-20 11:56:57 -05001441 input->x = x;
1442 input->y = y;
1443 input->sx = sx;
1444 input->sy = sy;
1445
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001447 if (window->enter_handler)
1448 pointer = window->enter_handler(window, input,
1449 time, sx, sy,
1450 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001451
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001452 item = window_find_item(window, x, y);
1453 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001454
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001455 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001456 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001457 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001458}
1459
1460static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001461input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001462{
1463 struct window *window = input->keyboard_focus;
1464
1465 if (!window)
1466 return;
1467
1468 window->keyboard_device = NULL;
1469 if (window->keyboard_focus_handler)
1470 (*window->keyboard_focus_handler)(window, NULL,
1471 window->user_data);
1472
1473 input->keyboard_focus = NULL;
1474}
1475
1476static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001477input_handle_keyboard_focus(void *data,
1478 struct wl_input_device *input_device,
1479 uint32_t time,
1480 struct wl_surface *surface,
1481 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001482{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001483 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001484 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001485 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001486 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001487
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001488 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001489
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001490 if (surface)
1491 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001492
1493 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001494 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001495 for (k = keys->data; k < end; k++)
1496 input->modifiers |= d->xkb->map->modmap[*k];
1497
1498 window = input->keyboard_focus;
1499 if (window) {
1500 window->keyboard_device = input;
1501 if (window->keyboard_focus_handler)
1502 (*window->keyboard_focus_handler)(window,
1503 window->keyboard_device,
1504 window->user_data);
1505 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001506}
1507
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001508static void
1509input_handle_touch_down(void *data,
1510 struct wl_input_device *wl_input_device,
1511 uint32_t time, struct wl_surface *surface,
1512 int32_t id, int32_t x, int32_t y)
1513{
1514}
1515
1516static void
1517input_handle_touch_up(void *data,
1518 struct wl_input_device *wl_input_device,
1519 uint32_t time, int32_t id)
1520{
1521}
1522
1523static void
1524input_handle_touch_motion(void *data,
1525 struct wl_input_device *wl_input_device,
1526 uint32_t time, int32_t id, int32_t x, int32_t y)
1527{
1528}
1529
1530static void
1531input_handle_touch_frame(void *data,
1532 struct wl_input_device *wl_input_device)
1533{
1534}
1535
1536static void
1537input_handle_touch_cancel(void *data,
1538 struct wl_input_device *wl_input_device)
1539{
1540}
1541
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001542static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001543 input_handle_motion,
1544 input_handle_button,
1545 input_handle_key,
1546 input_handle_pointer_focus,
1547 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001548 input_handle_touch_down,
1549 input_handle_touch_up,
1550 input_handle_touch_motion,
1551 input_handle_touch_frame,
1552 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001553};
1554
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001555void
1556input_get_position(struct input *input, int32_t *x, int32_t *y)
1557{
1558 *x = input->sx;
1559 *y = input->sy;
1560}
1561
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001562struct wl_input_device *
1563input_get_input_device(struct input *input)
1564{
1565 return input->input_device;
1566}
1567
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001568uint32_t
1569input_get_modifiers(struct input *input)
1570{
1571 return input->modifiers;
1572}
1573
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001574struct data_offer {
1575 struct wl_data_offer *offer;
1576 struct input *input;
1577 struct wl_array types;
1578 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001579
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001580 struct task io_task;
1581 int fd;
1582 data_func_t func;
1583 int32_t x, y;
1584 void *user_data;
1585};
1586
1587static void
1588data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1589{
1590 struct data_offer *offer = data;
1591 char **p;
1592
1593 p = wl_array_add(&offer->types, sizeof *p);
1594 *p = strdup(type);
1595}
1596
1597static const struct wl_data_offer_listener data_offer_listener = {
1598 data_offer_offer,
1599};
1600
1601static void
1602data_offer_destroy(struct data_offer *offer)
1603{
1604 char **p;
1605
1606 offer->refcount--;
1607 if (offer->refcount == 0) {
1608 wl_data_offer_destroy(offer->offer);
1609 for (p = offer->types.data; *p; p++)
1610 free(*p);
1611 wl_array_release(&offer->types);
1612 free(offer);
1613 }
1614}
1615
1616static void
1617data_device_data_offer(void *data,
1618 struct wl_data_device *data_device, uint32_t id)
1619{
1620 struct data_offer *offer;
1621
1622 offer = malloc(sizeof *offer);
1623
1624 wl_array_init(&offer->types);
1625 offer->refcount = 1;
1626 offer->input = data;
1627
1628 /* FIXME: Generate typesafe wrappers for this */
1629 offer->offer = (struct wl_data_offer *)
1630 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1631 id, &wl_data_offer_interface);
1632
1633 wl_data_offer_add_listener(offer->offer,
1634 &data_offer_listener, offer);
1635}
1636
1637static void
1638data_device_enter(void *data, struct wl_data_device *data_device,
1639 uint32_t time, struct wl_surface *surface,
1640 int32_t x, int32_t y, struct wl_data_offer *offer)
1641{
1642 struct input *input = data;
1643 struct window *window;
1644 char **p;
1645
1646 input->drag_offer = wl_data_offer_get_user_data(offer);
1647 window = wl_surface_get_user_data(surface);
1648 input->pointer_focus = window;
1649
1650 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1651 *p = NULL;
1652
1653 window = input->pointer_focus;
1654 if (window->data_handler)
1655 window->data_handler(window, input, time, x, y,
1656 input->drag_offer->types.data,
1657 window->user_data);
1658}
1659
1660static void
1661data_device_leave(void *data, struct wl_data_device *data_device)
1662{
1663 struct input *input = data;
1664
1665 data_offer_destroy(input->drag_offer);
1666 input->drag_offer = NULL;
1667}
1668
1669static void
1670data_device_motion(void *data, struct wl_data_device *data_device,
1671 uint32_t time, int32_t x, int32_t y)
1672{
1673 struct input *input = data;
1674 struct window *window = input->pointer_focus;
1675
1676 input->sx = x;
1677 input->sy = y;
1678
1679 if (window->data_handler)
1680 window->data_handler(window, input, time, x, y,
1681 input->drag_offer->types.data,
1682 window->user_data);
1683}
1684
1685static void
1686data_device_drop(void *data, struct wl_data_device *data_device)
1687{
1688 struct input *input = data;
1689 struct window *window = input->pointer_focus;
1690
1691 if (window->drop_handler)
1692 window->drop_handler(window, input,
1693 input->sx, input->sy, window->user_data);
1694}
1695
1696static void
1697data_device_selection(void *data,
1698 struct wl_data_device *wl_data_device,
1699 struct wl_data_offer *offer)
1700{
1701 struct input *input = data;
1702 char **p;
1703
1704 if (input->selection_offer)
1705 data_offer_destroy(input->selection_offer);
1706
1707 input->selection_offer = wl_data_offer_get_user_data(offer);
1708 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1709 *p = NULL;
1710}
1711
1712static const struct wl_data_device_listener data_device_listener = {
1713 data_device_data_offer,
1714 data_device_enter,
1715 data_device_leave,
1716 data_device_motion,
1717 data_device_drop,
1718 data_device_selection
1719};
1720
1721struct wl_data_device *
1722input_get_data_device(struct input *input)
1723{
1724 return input->data_device;
1725}
1726
1727void
1728input_set_selection(struct input *input,
1729 struct wl_data_source *source, uint32_t time)
1730{
1731 wl_data_device_set_selection(input->data_device, source, time);
1732}
1733
1734void
1735input_accept(struct input *input, uint32_t time, const char *type)
1736{
1737 wl_data_offer_accept(input->drag_offer->offer, time, type);
1738}
1739
1740static void
1741offer_io_func(struct task *task, uint32_t events)
1742{
1743 struct data_offer *offer =
1744 container_of(task, struct data_offer, io_task);
1745 unsigned int len;
1746 char buffer[4096];
1747
1748 len = read(offer->fd, buffer, sizeof buffer);
1749 offer->func(buffer, len,
1750 offer->x, offer->y, offer->user_data);
1751
1752 if (len == 0) {
1753 close(offer->fd);
1754 data_offer_destroy(offer);
1755 }
1756}
1757
1758static void
1759data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1760 data_func_t func, void *user_data)
1761{
1762 int p[2];
1763
1764 pipe2(p, O_CLOEXEC);
1765 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1766 close(p[1]);
1767
1768 offer->io_task.run = offer_io_func;
1769 offer->fd = p[0];
1770 offer->func = func;
1771 offer->refcount++;
1772 offer->user_data = user_data;
1773
1774 display_watch_fd(offer->input->display,
1775 offer->fd, EPOLLIN, &offer->io_task);
1776}
1777
1778void
1779input_receive_drag_data(struct input *input, const char *mime_type,
1780 data_func_t func, void *data)
1781{
1782 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1783 input->drag_offer->x = input->sx;
1784 input->drag_offer->y = input->sy;
1785}
1786
1787int
1788input_receive_selection_data(struct input *input, const char *mime_type,
1789 data_func_t func, void *data)
1790{
1791 char **p;
1792
1793 if (input->selection_offer == NULL)
1794 return -1;
1795
1796 for (p = input->selection_offer->types.data; *p; p++)
1797 if (strcmp(mime_type, *p) == 0)
1798 break;
1799
1800 if (*p == NULL)
1801 return -1;
1802
1803 data_offer_receive_data(input->selection_offer,
1804 mime_type, func, data);
1805 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001806}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001807
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001808int
1809input_receive_selection_data_to_fd(struct input *input,
1810 const char *mime_type, int fd)
1811{
1812 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1813
1814 return 0;
1815}
1816
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001817void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001818window_move(struct window *window, struct input *input, uint32_t time)
1819{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001820 if (!window->shell_surface)
1821 return;
1822
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001823 wl_shell_surface_move(window->shell_surface,
1824 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001825}
1826
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001827static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001828handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001829 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001830 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001831{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001832 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001833 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001834
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001835 /* FIXME: this is probably the wrong place to check for width
1836 * or height <= 0, but it prevents the compositor from crashing
1837 */
1838 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001839 return;
1840
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001841 window->resize_edges = edges;
1842
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001843 if (window->resize_handler) {
1844 child_width = width - 20 - window->margin * 2;
1845 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001846
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001847 (*window->resize_handler)(window,
1848 child_width, child_height,
1849 window->user_data);
1850 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001851 window->allocation.width = width;
1852 window->allocation.height = height;
1853
1854 if (window->redraw_handler)
1855 window_schedule_redraw(window);
1856 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001857}
1858
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001859static void
1860handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1861{
1862 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001863 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001864 /* FIXME: Need more context in this event, at least the input
1865 * device. Or just use wl_callback. */
1866
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001867 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001868 window_destroy(window);
1869}
1870
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001871static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001872 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001873 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001874};
1875
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001876void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001877window_get_allocation(struct window *window,
1878 struct rectangle *allocation)
1879{
1880 *allocation = window->allocation;
1881}
1882
1883void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001884window_get_child_allocation(struct window *window,
1885 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001886{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001887 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001888 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001889 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001890 allocation->x = window->margin + 10;
1891 allocation->y = window->margin + 50;
1892 allocation->width =
1893 window->allocation.width - 20 - window->margin * 2;
1894 allocation->height =
1895 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001896 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001897}
1898
1899void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001900window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001901{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001902 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001903 window->allocation.x = 20 + window->margin;
1904 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001905 window->allocation.width = width + 20 + window->margin * 2;
1906 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001907 } else {
1908 window->allocation.x = 0;
1909 window->allocation.y = 0;
1910 window->allocation.width = width;
1911 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001912 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001913}
1914
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001915static void
1916idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001917{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001918 struct window *window =
1919 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001920
1921 window->redraw_handler(window, window->user_data);
1922 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001923}
1924
1925void
1926window_schedule_redraw(struct window *window)
1927{
1928 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001929 window->redraw_task.run = idle_redraw;
1930 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001931 window->redraw_scheduled = 1;
1932 }
1933}
1934
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001935void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001936window_set_custom(struct window *window)
1937{
1938 window->type = TYPE_CUSTOM;
1939}
1940
1941void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001942window_set_fullscreen(struct window *window, int fullscreen)
1943{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001944 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001945 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001946
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001947 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001948 return;
1949
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001950 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001951 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001952 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001953 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001954 width = output->allocation.width;
1955 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001956 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001957 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001958 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001959 width = window->saved_allocation.width - 20 - window->margin * 2;
1960 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001961 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001962 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001963
1964 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001965}
1966
1967void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001968window_set_decoration(struct window *window, int decoration)
1969{
1970 window->decoration = decoration;
1971}
1972
1973void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001974window_set_user_data(struct window *window, void *data)
1975{
1976 window->user_data = data;
1977}
1978
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001979void *
1980window_get_user_data(struct window *window)
1981{
1982 return window->user_data;
1983}
1984
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001985void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001986window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001987 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001988{
1989 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001990}
1991
1992void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001993window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001994 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001995{
1996 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001997}
1998
1999void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002000window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002001 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002002{
2003 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002004}
2005
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002006void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002007window_set_button_handler(struct window *window,
2008 window_button_handler_t handler)
2009{
2010 window->button_handler = handler;
2011}
2012
2013void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002014window_set_motion_handler(struct window *window,
2015 window_motion_handler_t handler)
2016{
2017 window->motion_handler = handler;
2018}
2019
2020void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04002021window_set_enter_handler(struct window *window,
2022 window_enter_handler_t handler)
2023{
2024 window->enter_handler = handler;
2025}
2026
2027void
2028window_set_leave_handler(struct window *window,
2029 window_leave_handler_t handler)
2030{
2031 window->leave_handler = handler;
2032}
2033
2034void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002035window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002036 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002037{
2038 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002039}
2040
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002041void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002042window_set_item_focus_handler(struct window *window,
2043 window_item_focus_handler_t handler)
2044{
2045 window->item_focus_handler = handler;
2046}
2047
2048void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002049window_set_data_handler(struct window *window, window_data_handler_t handler)
2050{
2051 window->data_handler = handler;
2052}
2053
2054void
2055window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2056{
2057 window->drop_handler = handler;
2058}
2059
2060void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002061window_set_close_handler(struct window *window,
2062 window_close_handler_t handler)
2063{
2064 window->close_handler = handler;
2065}
2066
2067void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002068window_set_transparent(struct window *window, int transparent)
2069{
2070 window->transparent = transparent;
2071}
2072
2073void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002074window_set_title(struct window *window, const char *title)
2075{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002076 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002077 window->title = strdup(title);
2078}
2079
2080const char *
2081window_get_title(struct window *window)
2082{
2083 return window->title;
2084}
2085
2086void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002087display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2088 int32_t x, int32_t y, int32_t width, int32_t height)
2089{
2090 struct wl_buffer *buffer;
2091
2092 buffer = display_get_buffer_for_surface(display, cairo_surface);
2093
2094 wl_buffer_damage(buffer, x, y, width, height);
2095}
2096
2097void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002098window_damage(struct window *window, int32_t x, int32_t y,
2099 int32_t width, int32_t height)
2100{
2101 wl_surface_damage(window->surface, x, y, width, height);
2102}
2103
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002104static struct window *
2105window_create_internal(struct display *display, struct window *parent,
2106 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002107{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002108 struct window *window;
2109
2110 window = malloc(sizeof *window);
2111 if (window == NULL)
2112 return NULL;
2113
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002114 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002115 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002116 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002117 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002118 if (display->shell) {
2119 window->shell_surface =
2120 wl_shell_get_shell_surface(display->shell,
2121 window->surface);
2122 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002123 window->allocation.x = 0;
2124 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002125 window->allocation.width = width;
2126 window->allocation.height = height;
2127 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002128 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002129 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002130 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002131 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002132
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002133 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002134#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002135 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2136 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002137#else
2138 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2139#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002140 else
2141 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002142
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002143 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002144 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002145
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002146 if (window->shell_surface) {
2147 wl_shell_surface_set_user_data(window->shell_surface, window);
2148 wl_shell_surface_add_listener(window->shell_surface,
2149 &shell_surface_listener, window);
2150 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002151
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002152 return window;
2153}
2154
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002155struct window *
2156window_create(struct display *display, int32_t width, int32_t height)
2157{
2158 struct window *window;
2159
2160 window = window_create_internal(display, NULL, width, height);
2161 if (!window)
2162 return NULL;
2163
2164 return window;
2165}
2166
2167struct window *
2168window_create_transient(struct display *display, struct window *parent,
2169 int32_t x, int32_t y, int32_t width, int32_t height)
2170{
2171 struct window *window;
2172
2173 window = window_create_internal(parent->display,
2174 parent, width, height);
2175 if (!window)
2176 return NULL;
2177
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002178 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002179 window->x = x;
2180 window->y = y;
2181
2182 return window;
2183}
2184
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002185static int
2186menu_set_item(struct window *window, struct menu *menu, int sy)
2187{
2188 int next;
2189
2190 next = (sy - 8) / 20;
2191 if (menu->current != next) {
2192 menu->current = next;
2193 window_schedule_redraw(window);
2194 }
2195
2196 return POINTER_LEFT_PTR;
2197}
2198
2199static int
2200menu_motion_handler(struct window *window,
2201 struct input *input, uint32_t time,
2202 int32_t x, int32_t y,
2203 int32_t sx, int32_t sy, void *data)
2204{
2205 return menu_set_item(window, data, sy);
2206}
2207
2208static int
2209menu_enter_handler(struct window *window,
2210 struct input *input, uint32_t time,
2211 int32_t x, int32_t y, void *data)
2212{
2213 return menu_set_item(window, data, y);
2214}
2215
2216static void
2217menu_leave_handler(struct window *window,
2218 struct input *input, uint32_t time, void *data)
2219{
2220 menu_set_item(window, data, -200);
2221}
2222
2223static void
2224menu_button_handler(struct window *window,
2225 struct input *input, uint32_t time,
2226 int button, int state, void *data)
2227
2228{
2229 struct menu *menu = data;
2230
2231 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002232 if (state == 0 && time - menu->time > 500) {
2233 menu->func(window->parent,
2234 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002235 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002236 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002237}
2238
2239static void
2240menu_redraw_handler(struct window *window, void *data)
2241{
2242 cairo_t *cr;
2243 const int32_t r = 3, margin = 3;
2244 struct menu *menu = data;
2245 int32_t width, height, i;
2246
2247 width = 200;
2248 height = menu->count * 20 + margin * 2;
2249 window_set_child_size(window, width, height);
2250 window_create_surface(window);
2251
2252 cr = cairo_create(window->cairo_surface);
2253 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2254 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2255 cairo_paint(cr);
2256
2257 width = window->allocation.width;
2258 height = window->allocation.height;
2259 rounded_rect(cr, 0, 0, width, height, r);
2260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2261 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2262 cairo_fill(cr);
2263
2264 for (i = 0; i < menu->count; i++) {
2265 if (i == menu->current) {
2266 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2267 cairo_rectangle(cr, margin, i * 20 + margin,
2268 width - 2 * margin, 20);
2269 cairo_fill(cr);
2270 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2271 cairo_move_to(cr, 10, i * 20 + 16);
2272 cairo_show_text(cr, menu->entries[i]);
2273 } else {
2274 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2275 cairo_move_to(cr, 10, i * 20 + 16);
2276 cairo_show_text(cr, menu->entries[i]);
2277 }
2278 }
2279
2280 cairo_destroy(cr);
2281 window_flush(window);
2282}
2283
2284struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002285window_create_menu(struct display *display,
2286 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002287 int32_t x, int32_t y,
2288 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002289{
2290 struct window *window;
2291 struct menu *menu;
2292
2293 menu = malloc(sizeof *menu);
2294 if (!menu)
2295 return NULL;
2296
2297 window = window_create_internal(parent->display, parent, 0, 0);
2298 if (!window)
2299 return NULL;
2300
2301 menu->window = window;
2302 menu->entries = entries;
2303 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002304 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002305 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002306 window->decoration = 0;
2307 window->type = TYPE_MENU;
2308 window->x = x;
2309 window->y = y;
2310
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002311 wl_shell_surface_set_popup(window->shell_surface,
2312 input->input_device, time,
2313 window->parent->shell_surface,
2314 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002315
2316 window_set_motion_handler(window, menu_motion_handler);
2317 window_set_enter_handler(window, menu_enter_handler);
2318 window_set_leave_handler(window, menu_leave_handler);
2319 window_set_button_handler(window, menu_button_handler);
2320 window_set_redraw_handler(window, menu_redraw_handler);
2321 window_set_user_data(window, menu);
2322
2323 return window;
2324}
2325
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002326void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002327window_set_buffer_type(struct window *window, enum window_buffer_type type)
2328{
2329 window->buffer_type = type;
2330}
2331
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002332
2333static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002334display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002335 struct wl_output *wl_output,
2336 int x, int y,
2337 int physical_width,
2338 int physical_height,
2339 int subpixel,
2340 const char *make,
2341 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002342{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002343 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002344
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002345 output->allocation.x = x;
2346 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002347}
2348
2349static void
2350display_handle_mode(void *data,
2351 struct wl_output *wl_output,
2352 uint32_t flags,
2353 int width,
2354 int height,
2355 int refresh)
2356{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002357 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002358 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002359
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002360 if (flags & WL_OUTPUT_MODE_CURRENT) {
2361 output->allocation.width = width;
2362 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002363 if (display->output_configure_handler)
2364 (*display->output_configure_handler)(
2365 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002366 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002367}
2368
2369static const struct wl_output_listener output_listener = {
2370 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002371 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002372};
2373
2374static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002375display_add_output(struct display *d, uint32_t id)
2376{
2377 struct output *output;
2378
2379 output = malloc(sizeof *output);
2380 if (output == NULL)
2381 return;
2382
2383 memset(output, 0, sizeof *output);
2384 output->display = d;
2385 output->output =
2386 wl_display_bind(d->display, id, &wl_output_interface);
2387 wl_list_insert(d->output_list.prev, &output->link);
2388
2389 wl_output_add_listener(output->output, &output_listener, output);
2390}
2391
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002392static void
2393output_destroy(struct output *output)
2394{
2395 if (output->destroy_handler)
2396 (*output->destroy_handler)(output, output->user_data);
2397
2398 wl_output_destroy(output->output);
2399 wl_list_remove(&output->link);
2400 free(output);
2401}
2402
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002403void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002404display_set_output_configure_handler(struct display *display,
2405 display_output_handler_t handler)
2406{
2407 struct output *output;
2408
2409 display->output_configure_handler = handler;
2410 if (!handler)
2411 return;
2412
2413 wl_list_for_each(output, &display->output_list, link)
2414 (*display->output_configure_handler)(output,
2415 display->user_data);
2416}
2417
2418void
2419output_set_user_data(struct output *output, void *data)
2420{
2421 output->user_data = data;
2422}
2423
2424void *
2425output_get_user_data(struct output *output)
2426{
2427 return output->user_data;
2428}
2429
2430void
2431output_set_destroy_handler(struct output *output,
2432 display_output_handler_t handler)
2433{
2434 output->destroy_handler = handler;
2435 /* FIXME: implement this, once we have way to remove outputs */
2436}
2437
2438void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002439output_get_allocation(struct output *output, struct rectangle *allocation)
2440{
2441 *allocation = output->allocation;
2442}
2443
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002444struct wl_output *
2445output_get_wl_output(struct output *output)
2446{
2447 return output->output;
2448}
2449
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002450static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002451display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002452{
2453 struct input *input;
2454
2455 input = malloc(sizeof *input);
2456 if (input == NULL)
2457 return;
2458
2459 memset(input, 0, sizeof *input);
2460 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002461 input->input_device =
2462 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002463 input->pointer_focus = NULL;
2464 input->keyboard_focus = NULL;
2465 wl_list_insert(d->input_list.prev, &input->link);
2466
2467 wl_input_device_add_listener(input->input_device,
2468 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002469 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002470
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002471 input->data_device =
2472 wl_data_device_manager_get_data_device(d->data_device_manager,
2473 input->input_device);
2474 wl_data_device_add_listener(input->data_device,
2475 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002476}
2477
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002478static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002479input_destroy(struct input *input)
2480{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002481 input_remove_keyboard_focus(input);
2482 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002483
2484 if (input->drag_offer)
2485 data_offer_destroy(input->drag_offer);
2486
2487 if (input->selection_offer)
2488 data_offer_destroy(input->selection_offer);
2489
2490 wl_data_device_destroy(input->data_device);
2491 wl_list_remove(&input->link);
2492 wl_input_device_destroy(input->input_device);
2493 free(input);
2494}
2495
2496static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002497display_handle_global(struct wl_display *display, uint32_t id,
2498 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002499{
2500 struct display *d = data;
2501
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002502 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002503 d->compositor =
2504 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002505 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002506 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002507 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002508 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002509 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002510 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002511 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002512 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002513 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2514 d->data_device_manager =
2515 wl_display_bind(display, id,
2516 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002517 }
2518}
2519
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002520static void
2521display_render_frame(struct display *d)
2522{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002523 int radius = 8;
2524 cairo_t *cr;
2525
2526 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2527 cr = cairo_create(d->shadow);
2528 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2529 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2530 rounded_rect(cr, 16, 16, 112, 112, radius);
2531 cairo_fill(cr);
2532 cairo_destroy(cr);
2533 blur_surface(d->shadow, 64);
2534
2535 d->active_frame =
2536 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2537 cr = cairo_create(d->active_frame);
2538 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2539 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2540 rounded_rect(cr, 16, 16, 112, 112, radius);
2541 cairo_fill(cr);
2542 cairo_destroy(cr);
2543
2544 d->inactive_frame =
2545 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2546 cr = cairo_create(d->inactive_frame);
2547 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2548 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2549 rounded_rect(cr, 16, 16, 112, 112, radius);
2550 cairo_fill(cr);
2551 cairo_destroy(cr);
2552}
2553
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002554static void
2555init_xkb(struct display *d)
2556{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002557 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002558
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002559 names.rules = "evdev";
2560 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002561 names.layout = option_xkb_layout;
2562 names.variant = option_xkb_variant;
2563 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002564
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002565 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002566 if (!d->xkb) {
2567 fprintf(stderr, "Failed to compile keymap\n");
2568 exit(1);
2569 }
2570}
2571
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002572static void
2573fini_xkb(struct display *display)
2574{
2575 xkb_free_keymap(display->xkb);
2576}
2577
Yuval Fledel45568f62010-12-06 09:18:12 -05002578static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002579init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002580{
2581 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002582 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002583
2584 static const EGLint premul_argb_cfg_attribs[] = {
2585 EGL_SURFACE_TYPE,
2586 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2587 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002588 EGL_RED_SIZE, 1,
2589 EGL_GREEN_SIZE, 1,
2590 EGL_BLUE_SIZE, 1,
2591 EGL_ALPHA_SIZE, 1,
2592 EGL_DEPTH_SIZE, 1,
2593 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2594 EGL_NONE
2595 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002596
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002597 static const EGLint rgb_cfg_attribs[] = {
2598 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2599 EGL_RED_SIZE, 1,
2600 EGL_GREEN_SIZE, 1,
2601 EGL_BLUE_SIZE, 1,
2602 EGL_ALPHA_SIZE, 0,
2603 EGL_DEPTH_SIZE, 1,
2604 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2605 EGL_NONE
2606 };
2607
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002608 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002609 if (!eglInitialize(d->dpy, &major, &minor)) {
2610 fprintf(stderr, "failed to initialize display\n");
2611 return -1;
2612 }
2613
2614 if (!eglBindAPI(EGL_OPENGL_API)) {
2615 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2616 return -1;
2617 }
2618
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002619 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002620 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002621 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002622 return -1;
2623 }
2624
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002625 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2626 &d->rgb_config, 1, &n) || n != 1) {
2627 fprintf(stderr, "failed to choose rgb config\n");
2628 return -1;
2629 }
2630
Benjamin Franzke0c991632011-09-27 21:57:31 +02002631 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2632 if (d->rgb_ctx == NULL) {
2633 fprintf(stderr, "failed to create context\n");
2634 return -1;
2635 }
2636 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2637 EGL_NO_CONTEXT, NULL);
2638 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002639 fprintf(stderr, "failed to create context\n");
2640 return -1;
2641 }
2642
Benjamin Franzke0c991632011-09-27 21:57:31 +02002643 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002644 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002645 return -1;
2646 }
2647
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002648#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002649 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2650 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002651 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002652 return -1;
2653 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002654 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2655 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2656 fprintf(stderr, "failed to get cairo egl argb device\n");
2657 return -1;
2658 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002659#endif
2660
2661 return 0;
2662}
2663
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002664static void
2665fini_egl(struct display *display)
2666{
2667#ifdef HAVE_CAIRO_EGL
2668 cairo_device_destroy(display->argb_device);
2669 cairo_device_destroy(display->rgb_device);
2670#endif
2671
2672 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2673 EGL_NO_CONTEXT);
2674
2675 eglTerminate(display->dpy);
2676 eglReleaseThread();
2677}
2678
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002679static int
2680event_mask_update(uint32_t mask, void *data)
2681{
2682 struct display *d = data;
2683
2684 d->mask = mask;
2685
2686 return 0;
2687}
2688
2689static void
2690handle_display_data(struct task *task, uint32_t events)
2691{
2692 struct display *display =
2693 container_of(task, struct display, display_task);
2694
2695 wl_display_iterate(display->display, display->mask);
2696}
2697
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002698struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002699display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002700{
2701 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002702 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002703 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002704 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002705
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002706 g_type_init();
2707
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002708 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002709 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002710 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002711
2712 xkb_option_group = g_option_group_new("xkb",
2713 "Keyboard options",
2714 "Show all XKB options",
2715 NULL, NULL);
2716 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2717 g_option_context_add_group (context, xkb_option_group);
2718
2719 if (!g_option_context_parse(context, argc, argv, &error)) {
2720 fprintf(stderr, "option parsing failed: %s\n", error->message);
2721 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002722 }
2723
Tim Wiederhake748f6722011-01-23 23:25:25 +01002724 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002725
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002726 d = malloc(sizeof *d);
2727 if (d == NULL)
2728 return NULL;
2729
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002730 memset(d, 0, sizeof *d);
2731
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002732 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002733 if (d->display == NULL) {
2734 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002735 return NULL;
2736 }
2737
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002738 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2739 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2740 d->display_task.run = handle_display_data;
2741 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2742
2743 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002744 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002745 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002746
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002747 /* Set up listener so we'll catch all events. */
2748 wl_display_add_global_listener(d->display,
2749 display_handle_global, d);
2750
2751 /* Process connection events. */
2752 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002753 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002754 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002755
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002756 d->image_target_texture_2d =
2757 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2758 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2759 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2760
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002761 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002762
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002763 display_render_frame(d);
2764
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002765 wl_list_init(&d->window_list);
2766
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002767 init_xkb(d);
2768
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002769 return d;
2770}
2771
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002772static void
2773display_destroy_outputs(struct display *display)
2774{
2775 struct output *tmp;
2776 struct output *output;
2777
2778 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2779 output_destroy(output);
2780}
2781
Pekka Paalanene1207c72011-12-16 12:02:09 +02002782static void
2783display_destroy_inputs(struct display *display)
2784{
2785 struct input *tmp;
2786 struct input *input;
2787
2788 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2789 input_destroy(input);
2790}
2791
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002792void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002793display_destroy(struct display *display)
2794{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002795 if (!wl_list_empty(&display->window_list))
2796 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2797
2798 if (!wl_list_empty(&display->deferred_list))
2799 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2800
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002801 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002802 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002803
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002804 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002805
2806 cairo_surface_destroy(display->active_frame);
2807 cairo_surface_destroy(display->inactive_frame);
2808 cairo_surface_destroy(display->shadow);
2809 destroy_pointer_surfaces(display);
2810
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002811 fini_egl(display);
2812
Pekka Paalanenc2052982011-12-16 11:41:32 +02002813 if (display->shell)
2814 wl_shell_destroy(display->shell);
2815
2816 if (display->shm)
2817 wl_shm_destroy(display->shm);
2818
2819 if (display->data_device_manager)
2820 wl_data_device_manager_destroy(display->data_device_manager);
2821
2822 wl_compositor_destroy(display->compositor);
2823
2824 close(display->epoll_fd);
2825
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002826 wl_display_flush(display->display);
2827 wl_display_destroy(display->display);
2828 free(display);
2829}
2830
2831void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002832display_set_user_data(struct display *display, void *data)
2833{
2834 display->user_data = data;
2835}
2836
2837void *
2838display_get_user_data(struct display *display)
2839{
2840 return display->user_data;
2841}
2842
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002843struct wl_display *
2844display_get_display(struct display *display)
2845{
2846 return display->display;
2847}
2848
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002849struct output *
2850display_get_output(struct display *display)
2851{
2852 return container_of(display->output_list.next, struct output, link);
2853}
2854
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002855struct wl_compositor *
2856display_get_compositor(struct display *display)
2857{
2858 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002859}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002860
2861EGLDisplay
2862display_get_egl_display(struct display *d)
2863{
2864 return d->dpy;
2865}
2866
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002867struct wl_data_source *
2868display_create_data_source(struct display *display)
2869{
2870 return wl_data_device_manager_create_data_source(display->data_device_manager);
2871}
2872
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002873EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002874display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002875{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002876 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002877}
2878
Benjamin Franzke0c991632011-09-27 21:57:31 +02002879EGLConfig
2880display_get_argb_egl_config(struct display *d)
2881{
2882 return d->premultiplied_argb_config;
2883}
2884
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002885struct wl_shell *
2886display_get_shell(struct display *display)
2887{
2888 return display->shell;
2889}
2890
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002891int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002892display_acquire_window_surface(struct display *display,
2893 struct window *window,
2894 EGLContext ctx)
2895{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002896#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002897 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002898 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002899
2900 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002901 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002902 device = cairo_surface_get_device(window->cairo_surface);
2903 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002904 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002905
Benjamin Franzke0c991632011-09-27 21:57:31 +02002906 if (!ctx) {
2907 if (device == display->rgb_device)
2908 ctx = display->rgb_ctx;
2909 else if (device == display->argb_device)
2910 ctx = display->argb_ctx;
2911 else
2912 assert(0);
2913 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002914
2915 data = cairo_surface_get_user_data(window->cairo_surface,
2916 &surface_data_key);
2917
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002918 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002919 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002920 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2921 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002922
2923 return 0;
2924#else
2925 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002926#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002927}
2928
2929void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002930display_release_window_surface(struct display *display,
2931 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002932{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002933#ifdef HAVE_CAIRO_EGL
2934 cairo_device_t *device;
2935
2936 device = cairo_surface_get_device(window->cairo_surface);
2937 if (!device)
2938 return;
2939
2940 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002941 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002942 cairo_device_release(device);
2943#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002944}
2945
2946void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002947display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002948{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002949 wl_list_insert(&display->deferred_list, &task->link);
2950}
2951
2952void
2953display_watch_fd(struct display *display,
2954 int fd, uint32_t events, struct task *task)
2955{
2956 struct epoll_event ep;
2957
2958 ep.events = events;
2959 ep.data.ptr = task;
2960 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2961}
2962
2963void
2964display_run(struct display *display)
2965{
2966 struct task *task;
2967 struct epoll_event ep[16];
2968 int i, count;
2969
Pekka Paalanen826d7952011-12-15 10:14:07 +02002970 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002971 while (1) {
2972 while (display->mask & WL_DISPLAY_WRITABLE)
2973 wl_display_iterate(display->display,
2974 WL_DISPLAY_WRITABLE);
2975
Pekka Paalanen826d7952011-12-15 10:14:07 +02002976 if (!display->running)
2977 break;
2978
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002979 count = epoll_wait(display->epoll_fd,
2980 ep, ARRAY_LENGTH(ep), -1);
2981 for (i = 0; i < count; i++) {
2982 task = ep[i].data.ptr;
2983 task->run(task, ep[i].events);
2984 }
2985
2986 while (!wl_list_empty(&display->deferred_list)) {
2987 task = container_of(display->deferred_list.next,
2988 struct task, link);
2989 wl_list_remove(&task->link);
2990 task->run(task, 0);
2991 }
2992 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002993}
Pekka Paalanen826d7952011-12-15 10:14:07 +02002994
2995void
2996display_exit(struct display *display)
2997{
2998 display->running = 0;
2999}