blob: 70642a4554468c927e530a20ce27d8bc6e1e6fa0 [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øgsberg87a57bb2012-01-09 10:34:35 -0500110 struct widget *widget;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500111 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200112 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500113 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500114 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400116 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400117 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400118 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500119 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500120 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400122 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400123 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400124 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500125 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400126 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500127
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500128 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500129
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500130 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400131 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500132 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500133 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400134 window_data_handler_t data_handler;
135 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500136 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400137
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500138 struct wl_list widget_list;
139 struct widget *focus_widget;
140 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400141
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500142 struct window *menu;
143
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500144 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400145 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500146};
147
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500148struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500149 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400150 struct wl_list link;
151 struct rectangle allocation;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500152 widget_enter_handler_t enter_handler;
153 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500154 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500155 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 void *user_data;
157};
158
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400159struct input {
160 struct display *display;
161 struct wl_input_device *input_device;
162 struct window *pointer_focus;
163 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400164 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165 uint32_t modifiers;
166 int32_t x, y, sx, sy;
167 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168
169 struct wl_data_device *data_device;
170 struct data_offer *drag_offer;
171 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172};
173
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500174struct output {
175 struct display *display;
176 struct wl_output *output;
177 struct rectangle allocation;
178 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200179
180 display_output_handler_t destroy_handler;
181 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500182};
183
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500184struct menu {
185 struct window *window;
186 const char **entries;
187 uint32_t time;
188 int current;
189 int count;
190 menu_func_t func;
191};
192
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400193enum {
194 POINTER_DEFAULT = 100,
195 POINTER_UNSET
196};
197
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500198enum window_location {
199 WINDOW_INTERIOR = 0,
200 WINDOW_RESIZING_TOP = 1,
201 WINDOW_RESIZING_BOTTOM = 2,
202 WINDOW_RESIZING_LEFT = 4,
203 WINDOW_RESIZING_TOP_LEFT = 5,
204 WINDOW_RESIZING_BOTTOM_LEFT = 6,
205 WINDOW_RESIZING_RIGHT = 8,
206 WINDOW_RESIZING_TOP_RIGHT = 9,
207 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
208 WINDOW_RESIZING_MASK = 15,
209 WINDOW_EXTERIOR = 16,
210 WINDOW_TITLEBAR = 17,
211 WINDOW_CLIENT_AREA = 18,
212};
213
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400214const char *option_xkb_layout = "us";
215const char *option_xkb_variant = "";
216const char *option_xkb_options = "";
217
218static const GOptionEntry xkb_option_entries[] = {
219 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
220 &option_xkb_layout, "XKB Layout" },
221 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
222 &option_xkb_variant, "XKB Variant" },
223 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
224 &option_xkb_options, "XKB Options" },
225 { NULL }
226};
227
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400228static const cairo_user_data_key_t surface_data_key;
229struct surface_data {
230 struct wl_buffer *buffer;
231};
232
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500233#define MULT(_d,c,a,t) \
234 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
235
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500236#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400237
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100238struct egl_window_surface_data {
239 struct display *display;
240 struct wl_surface *surface;
241 struct wl_egl_window *window;
242 EGLSurface surf;
243};
244
245static void
246egl_window_surface_data_destroy(void *p)
247{
248 struct egl_window_surface_data *data = p;
249 struct display *d = data->display;
250
251 eglDestroySurface(d->dpy, data->surf);
252 wl_egl_window_destroy(data->window);
253 data->surface = NULL;
254
255 free(p);
256}
257
258static cairo_surface_t *
259display_create_egl_window_surface(struct display *display,
260 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400261 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100262 struct rectangle *rectangle)
263{
264 cairo_surface_t *cairo_surface;
265 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400266 EGLConfig config;
267 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200268 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100269
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400270 static const EGLint premul_attribs[] = {
271 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
272 EGL_NONE
273 };
274
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275 data = malloc(sizeof *data);
276 if (data == NULL)
277 return NULL;
278
279 data->display = display;
280 data->surface = surface;
281
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400282 if (flags & SURFACE_OPAQUE) {
283 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200284 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200285 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 } else {
287 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 attribs = premul_attribs;
290 }
291
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400292 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100293 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 data->surf = eglCreateWindowSurface(display->dpy, config,
297 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Benjamin Franzke0c991632011-09-27 21:57:31 +0200299 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300 data->surf,
301 rectangle->width,
302 rectangle->height);
303
304 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
305 data, egl_window_surface_data_destroy);
306
307 return cairo_surface;
308}
309
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200312 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 EGLImageKHR image;
314 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800315 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317};
318
319static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324
Benjamin Franzke0c991632011-09-27 21:57:31 +0200325 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800328
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500329 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500331 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500332 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333}
334
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500335EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336display_get_image_for_egl_image_surface(struct display *display,
337 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340
341 data = cairo_surface_get_user_data (surface, &surface_data_key);
342
343 return data->image;
344}
345
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500346static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400348 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 EGLDisplay dpy = display->dpy;
353 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200354 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355
356 data = malloc(sizeof *data);
357 if (data == NULL)
358 return NULL;
359
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800360 data->display = display;
361
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400362 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500364 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000365 free(data);
366 return NULL;
367 }
368
Benjamin Franzke0c991632011-09-27 21:57:31 +0200369 if (flags & SURFACE_OPAQUE) {
370 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200371 content = CAIRO_CONTENT_COLOR;
372 } else {
373 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 content = CAIRO_CONTENT_COLOR_ALPHA;
375 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500377 data->image = display->create_image(dpy, NULL,
378 EGL_NATIVE_PIXMAP_KHR,
379 (EGLClientBuffer) data->pixmap,
380 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500382 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 free(data);
384 return NULL;
385 }
386
387 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400388 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500389
Benjamin Franzke0c991632011-09-27 21:57:31 +0200390 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500393 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 surface = cairo_gl_surface_create_for_texture(data->device,
397 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 data->texture,
399 rectangle->width,
400 rectangle->height);
401
402 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500403 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404
405 return surface;
406}
407
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500408static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500409display_create_egl_image_surface_from_file(struct display *display,
410 const char *filename,
411 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412{
413 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400414 GdkPixbuf *pixbuf;
415 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400416 int stride, i;
417 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500418 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
420 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400422 FALSE, &error);
423 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400426 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
427 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500428 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400430 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432
433 stride = gdk_pixbuf_get_rowstride(pixbuf);
434 pixels = gdk_pixbuf_get_pixels(pixbuf);
435
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400437 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 while (p < end) {
440 unsigned int t;
441
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400442 MULT(p[0], p[0], p[3], t);
443 MULT(p[1], p[1], p[3], t);
444 MULT(p[2], p[2], p[3], t);
445 p += 4;
446
447 }
448 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400449
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200450 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000451 if (surface == NULL) {
452 g_object_unref(pixbuf);
453 return NULL;
454 }
455
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800456 data = cairo_surface_get_user_data(surface, &surface_data_key);
457
Benjamin Franzke0c991632011-09-27 21:57:31 +0200458 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800459 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800460 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
461 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200462 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400463
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500464 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466 return surface;
467}
468
469#endif
470
471struct wl_buffer *
472display_get_buffer_for_surface(struct display *display,
473 cairo_surface_t *surface)
474{
475 struct surface_data *data;
476
477 data = cairo_surface_get_user_data (surface, &surface_data_key);
478
479 return data->buffer;
480}
481
482struct shm_surface_data {
483 struct surface_data data;
484 void *map;
485 size_t length;
486};
487
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500488static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489shm_surface_data_destroy(void *p)
490{
491 struct shm_surface_data *data = p;
492
493 wl_buffer_destroy(data->data.buffer);
494 munmap(data->map, data->length);
495}
496
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500497static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400499 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500{
501 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400502 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400503 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800504 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505 char filename[] = "/tmp/wayland-shm-XXXXXX";
506
507 data = malloc(sizeof *data);
508 if (data == NULL)
509 return NULL;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rectangle->width);
513 data->length = stride * rectangle->height;
514 fd = mkstemp(filename);
515 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000516 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517 return NULL;
518 }
519 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000520 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521 close(fd);
522 return NULL;
523 }
524
525 data->map = mmap(NULL, data->length,
526 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
527 unlink(filename);
528
529 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000530 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531 close(fd);
532 return NULL;
533 }
534
535 surface = cairo_image_surface_create_for_data (data->map,
536 CAIRO_FORMAT_ARGB32,
537 rectangle->width,
538 rectangle->height,
539 stride);
540
541 cairo_surface_set_user_data (surface, &surface_data_key,
542 data, shm_surface_data_destroy);
543
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400545 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400547 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 data->data.buffer = wl_shm_create_buffer(display->shm,
550 fd,
551 rectangle->width,
552 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400553 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554
555 close(fd);
556
557 return surface;
558}
559
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500560static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561display_create_shm_surface_from_file(struct display *display,
562 const char *filename,
563 struct rectangle *rect)
564{
565 cairo_surface_t *surface;
566 GdkPixbuf *pixbuf;
567 GError *error = NULL;
568 int stride, i;
569 unsigned char *pixels, *p, *end, *dest_data;
570 int dest_stride;
571 uint32_t *d;
572
573 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
574 rect->width, rect->height,
575 FALSE, &error);
576 if (error != NULL)
577 return NULL;
578
579 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
580 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500581 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582 return NULL;
583 }
584
585 stride = gdk_pixbuf_get_rowstride(pixbuf);
586 pixels = gdk_pixbuf_get_pixels(pixbuf);
587
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400588 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000589 if (surface == NULL) {
590 g_object_unref(pixbuf);
591 return NULL;
592 }
593
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400594 dest_data = cairo_image_surface_get_data (surface);
595 dest_stride = cairo_image_surface_get_stride (surface);
596
597 for (i = 0; i < rect->height; i++) {
598 d = (uint32_t *) (dest_data + i * dest_stride);
599 p = pixels + i * stride;
600 end = p + rect->width * 4;
601 while (p < end) {
602 unsigned int t;
603 unsigned char a, r, g, b;
604
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605 a = p[3];
606 MULT(r, p[0], a, t);
607 MULT(g, p[1], a, t);
608 MULT(b, p[2], a, t);
609 p += 4;
610 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
611 }
612 }
613
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500614 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615
616 return surface;
617}
618
nobled7b87cb02011-02-01 18:51:47 +0000619static int
620check_size(struct rectangle *rect)
621{
622 if (rect->width && rect->height)
623 return 0;
624
625 fprintf(stderr, "tried to create surface of "
626 "width: %d, height: %d\n", rect->width, rect->height);
627 return -1;
628}
629
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630cairo_surface_t *
631display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400633 struct rectangle *rectangle,
634 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635{
nobled7b87cb02011-02-01 18:51:47 +0000636 if (check_size(rectangle) < 0)
637 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500638#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500639 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100640 if (surface)
641 return display_create_egl_window_surface(display,
642 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400643 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644 rectangle);
645 else
646 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400647 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500649 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400651 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652}
653
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500654static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655display_create_surface_from_file(struct display *display,
656 const char *filename,
657 struct rectangle *rectangle)
658{
nobled7b87cb02011-02-01 18:51:47 +0000659 if (check_size(rectangle) < 0)
660 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500661#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500662 if (display->dpy) {
663 return display_create_egl_image_surface_from_file(display,
664 filename,
665 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500666 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669}
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671 const char *filename;
672 int hotspot_x, hotspot_y;
673} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500674 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
675 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
676 { DATADIR "/weston/bottom_side.png", 16, 20 },
677 { DATADIR "/weston/grabbing.png", 20, 17 },
678 { DATADIR "/weston/left_ptr.png", 10, 5 },
679 { DATADIR "/weston/left_side.png", 10, 20 },
680 { DATADIR "/weston/right_side.png", 30, 19 },
681 { DATADIR "/weston/top_left_corner.png", 8, 8 },
682 { DATADIR "/weston/top_right_corner.png", 26, 8 },
683 { DATADIR "/weston/top_side.png", 18, 8 },
684 { DATADIR "/weston/xterm.png", 15, 15 },
685 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400686};
687
688static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400690{
691 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400693 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 display->pointer_surfaces =
697 malloc(count * sizeof *display->pointer_surfaces);
698 rect.width = width;
699 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400700 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702 display_create_surface_from_file(display,
703 pointer_images[i].filename,
704 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100705 if (!display->pointer_surfaces[i]) {
706 fprintf(stderr, "Error loading pointer image: %s\n",
707 pointer_images[i].filename);
708 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709 }
710
711}
712
Pekka Paalanen325bb602011-12-19 10:31:45 +0200713static void
714destroy_pointer_surfaces(struct display *display)
715{
716 int i, count;
717
718 count = ARRAY_LENGTH(pointer_images);
719 for (i = 0; i < count; ++i) {
720 if (display->pointer_surfaces[i])
721 cairo_surface_destroy(display->pointer_surfaces[i]);
722 }
723 free(display->pointer_surfaces);
724}
725
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400726cairo_surface_t *
727display_get_pointer_surface(struct display *display, int pointer,
728 int *width, int *height,
729 int *hotspot_x, int *hotspot_y)
730{
731 cairo_surface_t *surface;
732
733 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500734#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400735 *width = cairo_gl_surface_get_width(surface);
736 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000737#else
738 *width = cairo_image_surface_get_width(surface);
739 *height = cairo_image_surface_get_height(surface);
740#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400741 *hotspot_x = pointer_images[pointer].hotspot_x;
742 *hotspot_y = pointer_images[pointer].hotspot_y;
743
744 return cairo_surface_reference(surface);
745}
746
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400747static void
748window_attach_surface(struct window *window);
749
750static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400751free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400752{
753 struct window *window = data;
754
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400755 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756 cairo_surface_destroy(window->pending_surface);
757 window->pending_surface = NULL;
758 if (window->cairo_surface)
759 window_attach_surface(window);
760}
761
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762static const struct wl_callback_listener free_surface_listener = {
763 free_surface
764};
765
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500766static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200767window_get_resize_dx_dy(struct window *window, int *x, int *y)
768{
769 if (window->resize_edges & WINDOW_RESIZING_LEFT)
770 *x = window->server_allocation.width - window->allocation.width;
771 else
772 *x = 0;
773
774 if (window->resize_edges & WINDOW_RESIZING_TOP)
775 *y = window->server_allocation.height -
776 window->allocation.height;
777 else
778 *y = 0;
779
780 window->resize_edges = 0;
781}
782
783static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400784window_set_type(struct window *window)
785{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200786 if (!window->shell_surface)
787 return;
788
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400789 switch (window->type) {
790 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200791 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400792 break;
793 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200794 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400795 break;
796 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 wl_shell_surface_set_transient(window->shell_surface,
798 window->parent->shell_surface,
799 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400800 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500801 case TYPE_MENU:
802 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400803 case TYPE_CUSTOM:
804 break;
805 }
806}
807
808static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500809window_attach_surface(struct window *window)
810{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400811 struct display *display = window->display;
812 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400813 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000816#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500817 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400819 if (display->shell)
820 window_set_type(window);
821
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
825 data = cairo_surface_get_user_data(window->cairo_surface,
826 &surface_data_key);
827
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 cairo_gl_surface_swapbuffers(window->cairo_surface);
829 wl_egl_window_get_attached_size(data->window,
830 &window->server_allocation.width,
831 &window->server_allocation.height);
832 break;
833 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200836 window_get_resize_dx_dy(window, &x, &y);
837
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 if (window->pending_surface != NULL)
839 return;
840
841 window->pending_surface = window->cairo_surface;
842 window->cairo_surface = NULL;
843
844 buffer =
845 display_get_buffer_for_surface(display,
846 window->pending_surface);
847
848 wl_surface_attach(window->surface, buffer, x, y);
849 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400850 cb = wl_display_sync(display->display);
851 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000853 default:
854 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500856
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500857 wl_surface_damage(window->surface, 0, 0,
858 window->allocation.width,
859 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500860}
861
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500862void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400863window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500864{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100865 if (window->cairo_surface) {
866 switch (window->buffer_type) {
867 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
868 case WINDOW_BUFFER_TYPE_SHM:
869 display_surface_damage(window->display,
870 window->cairo_surface,
871 0, 0,
872 window->allocation.width,
873 window->allocation.height);
874 break;
875 default:
876 break;
877 }
878 window_attach_surface(window);
879 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500880}
881
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882void
883window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500885 cairo_surface_reference(surface);
886
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400887 if (window->cairo_surface != NULL)
888 cairo_surface_destroy(window->cairo_surface);
889
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500890 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891}
892
Benjamin Franzke22d54812011-07-16 19:50:32 +0000893#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894static void
895window_resize_cairo_window_surface(struct window *window)
896{
897 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200898 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899
900 data = cairo_surface_get_user_data(window->cairo_surface,
901 &surface_data_key);
902
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200903 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904 wl_egl_window_resize(data->window,
905 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200906 window->allocation.height,
907 x,y);
908
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909 cairo_gl_surface_set_size(window->cairo_surface,
910 window->allocation.width,
911 window->allocation.height);
912}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000913#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400915struct display *
916window_get_display(struct window *window)
917{
918 return window->display;
919}
920
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500921struct widget *
922window_get_widget(struct window *window)
923{
924 return window->widget;
925}
926
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400927void
928window_create_surface(struct window *window)
929{
930 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400931 uint32_t flags = 0;
932
933 if (!window->transparent)
934 flags = SURFACE_OPAQUE;
935
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500937#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
939 if (window->cairo_surface) {
940 window_resize_cairo_window_surface(window);
941 return;
942 }
943 surface = display_create_surface(window->display,
944 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100946 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500947 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400948 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100949 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400950 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951 break;
952#endif
953 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400954 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400955 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800957 default:
958 surface = NULL;
959 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400961
962 window_set_surface(window, surface);
963 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400964}
965
966static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500967window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500968{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500969 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500970 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400971 cairo_surface_t *frame;
972 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500973
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400974 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400975
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400976 width = window->allocation.width;
977 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500978
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500979 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500980
Kristian Høgsberg09531622010-06-14 23:22:15 -0400981 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400982 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400983 cairo_paint(cr);
984
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400985 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400986 tile_mask(cr, window->display->shadow,
987 shadow_dx, shadow_dy, width, height,
988 window->margin + 10 - shadow_dx,
989 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500990
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400991 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400992 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400993 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400994 frame = window->display->inactive_frame;
995
996 tile_source(cr, frame, 0, 0, width, height,
997 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500998
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500999 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1000 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001001 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001002 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001003 if (window->keyboard_device)
1004 cairo_set_source_rgb(cr, 0, 0, 0);
1005 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001006 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001007 cairo_show_text(cr, window->title);
1008
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001009 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001010}
1011
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001012void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001013window_destroy(struct window *window)
1014{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001015 struct display *display = window->display;
1016 struct input *input;
1017
1018 if (window->redraw_scheduled)
1019 wl_list_remove(&window->redraw_task.link);
1020
1021 wl_list_for_each(input, &display->input_list, link) {
1022 if (input->pointer_focus == window)
1023 input->pointer_focus = NULL;
1024 if (input->keyboard_focus == window)
1025 input->keyboard_focus = NULL;
1026 }
1027
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001028 if (window->shell_surface)
1029 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001030 wl_surface_destroy(window->surface);
1031 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001032
1033 if (window->cairo_surface != NULL)
1034 cairo_surface_destroy(window->cairo_surface);
1035 if (window->pending_surface != NULL)
1036 cairo_surface_destroy(window->pending_surface);
1037
1038 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001039 free(window);
1040}
1041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042static struct widget *
1043window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001044{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001045 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001046
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001047 wl_list_for_each(widget, &window->widget_list, link) {
1048 if (widget->allocation.x <= x &&
1049 x < widget->allocation.x + widget->allocation.width &&
1050 widget->allocation.y <= y &&
1051 y < widget->allocation.y + widget->allocation.height) {
1052 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001053 }
1054 }
1055
1056 return NULL;
1057}
1058
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059struct widget *
1060window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001062 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001063
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001064 widget = malloc(sizeof *widget);
1065 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001066 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001067 widget->user_data = data;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05001068 wl_list_insert(&window->widget_list, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071}
1072
1073void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001076 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001078 wl_list_for_each(widget, &window->widget_list, link)
1079 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001082struct widget *
1083window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001084{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086}
1087
1088void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001090{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001091 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092}
1093
1094void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001095widget_set_allocation(struct widget *widget,
1096 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 widget->allocation.x = x;
1099 widget->allocation.y = y;
1100 widget->allocation.width = width;
1101 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
1104void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001105widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001106{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001107 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001108}
1109
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001110void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001111widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001112{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001113 widget->enter_handler = handler;
1114}
1115
1116void
1117widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1118{
1119 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001120}
1121
1122void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001123widget_set_motion_handler(struct widget *widget,
1124 widget_motion_handler_t handler)
1125{
1126 widget->motion_handler = handler;
1127}
1128
1129void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001130widget_set_button_handler(struct widget *widget,
1131 widget_button_handler_t handler)
1132{
1133 widget->button_handler = handler;
1134}
1135
1136void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001137widget_schedule_redraw(struct widget *widget)
1138{
1139 window_schedule_redraw(widget->window);
1140}
1141
1142void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001143window_draw(struct window *window)
1144{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001145 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001146 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001147 else
1148 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001149}
1150
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151cairo_surface_t *
1152window_get_surface(struct window *window)
1153{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001154 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001155}
1156
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001157struct wl_surface *
1158window_get_wl_surface(struct window *window)
1159{
1160 return window->surface;
1161}
1162
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163struct wl_shell_surface *
1164window_get_wl_shell_surface(struct window *window)
1165{
1166 return window->shell_surface;
1167}
1168
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169static int
1170get_pointer_location(struct window *window, int32_t x, int32_t y)
1171{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001172 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173 const int grip_size = 8;
1174
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001175 if (!window->decoration)
1176 return WINDOW_CLIENT_AREA;
1177
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001178 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001179 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001180 else if (window->margin <= x && x < window->margin + grip_size)
1181 hlocation = WINDOW_RESIZING_LEFT;
1182 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001183 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001184 else if (x < window->allocation.width - window->margin)
1185 hlocation = WINDOW_RESIZING_RIGHT;
1186 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001187 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001188
1189 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001190 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001191 else if (window->margin <= y && y < window->margin + grip_size)
1192 vlocation = WINDOW_RESIZING_TOP;
1193 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001194 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001195 else if (y < window->allocation.height - window->margin)
1196 vlocation = WINDOW_RESIZING_BOTTOM;
1197 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001198 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001199
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001200 location = vlocation | hlocation;
1201 if (location & WINDOW_EXTERIOR)
1202 location = WINDOW_EXTERIOR;
1203 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1204 location = WINDOW_TITLEBAR;
1205 else if (location == WINDOW_INTERIOR)
1206 location = WINDOW_CLIENT_AREA;
1207
1208 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209}
1210
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001211static int
1212input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001214 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001215
1216 location = get_pointer_location(input->pointer_focus,
1217 input->sx, input->sy);
1218 switch (location) {
1219 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001220 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001222 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001224 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001226 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001227 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001228 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001230 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001231 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001232 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001233 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001234 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 case WINDOW_EXTERIOR:
1236 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001237 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001239 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001241}
1242
1243void
1244input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1245{
1246 struct display *display = input->display;
1247 struct wl_buffer *buffer;
1248 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001250 if (pointer == input->current_pointer_image)
1251 return;
1252
1253 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001255
1256 if (!surface)
1257 return;
1258
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001260 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261 pointer_images[pointer].hotspot_x,
1262 pointer_images[pointer].hotspot_y);
1263}
1264
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001265static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001266window_set_focus_widget(struct window *window, struct widget *focus,
1267 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001268{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001269 struct widget *old;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001270
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001271 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001272 return;
1273
Kristian Høgsbergee143232012-01-09 08:42:24 -05001274 old = window->focus_widget;
1275 if (old) {
1276 if (old->leave_handler)
1277 old->leave_handler(old, input, old->user_data);
1278 window->focus_widget = NULL;
1279 }
1280
1281 if (focus) {
1282 if (focus->enter_handler)
1283 focus->enter_handler(focus, input, time,
1284 x, y, focus->user_data);
1285 window->focus_widget = focus;
1286 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001287}
1288
1289static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001290input_handle_motion(void *data, struct wl_input_device *input_device,
1291 uint32_t time,
1292 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001293{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001294 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001296 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001297 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001298
1299 input->x = x;
1300 input->y = y;
1301 input->sx = sx;
1302 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001304 if (!window->focus_widget || !window->widget_grab_button) {
1305 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001306 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001307 }
1308
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001309 widget = window->focus_widget;
1310 if (widget && widget->motion_handler)
1311 pointer = widget->motion_handler(widget, input, time, sx, sy,
1312 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001313
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001315 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001316}
1317
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001318static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001319window_menu_func(struct window *window, int index, void *data)
1320{
1321 switch (index) {
1322 case 0: /* close */
1323 if (window->close_handler)
1324 window->close_handler(window->parent,
1325 window->user_data);
1326 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001327 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001328 break;
1329 case 1: /* fullscreen */
1330 /* we don't have a way to get out of fullscreen for now */
1331 window_set_fullscreen(window, 1);
1332 break;
1333 case 2: /* rotate */
1334 case 3: /* scale */
1335 break;
1336 }
1337}
1338
1339
1340static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001341input_handle_button(void *data,
1342 struct wl_input_device *input_device,
1343 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001344{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001345 struct input *input = data;
1346 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001347 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001348 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001349 int32_t x, y;
1350 static const char *entries[] = {
1351 "Close", "Fullscreen", "Rotate", "Scale"
1352 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001353
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001354 if (window->focus_widget && window->widget_grab_button == 0 && state)
1355 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001356
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001357 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001358
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001359 widget = window->focus_widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001360 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001361 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001362 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001363 if (!window->shell_surface)
1364 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001365 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001366 wl_shell_surface_move(window->shell_surface,
1367 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001368 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001369 case WINDOW_RESIZING_TOP:
1370 case WINDOW_RESIZING_BOTTOM:
1371 case WINDOW_RESIZING_LEFT:
1372 case WINDOW_RESIZING_RIGHT:
1373 case WINDOW_RESIZING_TOP_LEFT:
1374 case WINDOW_RESIZING_TOP_RIGHT:
1375 case WINDOW_RESIZING_BOTTOM_LEFT:
1376 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001377 if (!window->shell_surface)
1378 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001379 wl_shell_surface_resize(window->shell_surface,
1380 input_device, time,
1381 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001382 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001383 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001384 if (widget && widget->button_handler)
1385 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001386 input, time,
1387 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001388 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001389 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001390 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001391 } else if (button == BTN_RIGHT && state == 1) {
1392 switch (location) {
1393 default:
1394 input_get_position(input, &x, &y);
1395 window->menu = window_create_menu(window->display,
1396 input, time,
1397 window,
1398 x - 10, y - 10,
1399 window_menu_func,
1400 entries, 4);
1401 window_schedule_redraw(window->menu);
1402 break;
1403 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001404 if (widget && widget->button_handler)
1405 (*widget->button_handler)(widget,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001406 input, time,
1407 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001408 widget->user_data);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001409 break;
1410 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001411 } else {
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001412 if (widget && widget->button_handler)
1413 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001414 input, time,
1415 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001416 widget->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001417 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001418
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001419 if (window->focus_widget &&
1420 window->widget_grab_button == button && !state) {
1421 window->widget_grab_button = 0;
1422 widget = window_find_widget(window, input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001423 window_set_focus_widget(window, widget, input, time,
1424 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001425 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001426}
1427
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001428static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001429input_handle_key(void *data, struct wl_input_device *input_device,
1430 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001431{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001432 struct input *input = data;
1433 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001434 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001435 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001436
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001437 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001438 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001439 return;
1440
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001441 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001442 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001443 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1444 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001445
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001446 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1447
1448 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001449 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001450 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001451 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001452
1453 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001454 (*window->key_handler)(window, input, time, key, sym, state,
1455 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001456}
1457
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001458static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001459input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001460{
1461 struct window *window = input->pointer_focus;
1462
1463 if (!window)
1464 return;
1465
Kristian Høgsbergee143232012-01-09 08:42:24 -05001466 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001467
Pekka Paalanene1207c72011-12-16 12:02:09 +02001468 input->pointer_focus = NULL;
1469 input->current_pointer_image = POINTER_UNSET;
1470}
1471
1472static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001473input_handle_pointer_focus(void *data,
1474 struct wl_input_device *input_device,
1475 uint32_t time, struct wl_surface *surface,
1476 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001477{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001478 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001479 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001480 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001481 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001482
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001483 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001484 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001485 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001486
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001487 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001488 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001489 window = input->pointer_focus;
1490
Kristian Høgsberg59826582011-01-20 11:56:57 -05001491 input->x = x;
1492 input->y = y;
1493 input->sx = sx;
1494 input->sy = sy;
1495
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001496 pointer = POINTER_LEFT_PTR;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001497 widget = window_find_widget(window, x, y);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001498 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001499
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001500 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001501 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001502 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001503}
1504
1505static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001506input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001507{
1508 struct window *window = input->keyboard_focus;
1509
1510 if (!window)
1511 return;
1512
1513 window->keyboard_device = NULL;
1514 if (window->keyboard_focus_handler)
1515 (*window->keyboard_focus_handler)(window, NULL,
1516 window->user_data);
1517
1518 input->keyboard_focus = NULL;
1519}
1520
1521static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001522input_handle_keyboard_focus(void *data,
1523 struct wl_input_device *input_device,
1524 uint32_t time,
1525 struct wl_surface *surface,
1526 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001527{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001528 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001529 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001530 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001531 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001532
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001533 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001534
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001535 if (surface)
1536 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001537
1538 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001539 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001540 for (k = keys->data; k < end; k++)
1541 input->modifiers |= d->xkb->map->modmap[*k];
1542
1543 window = input->keyboard_focus;
1544 if (window) {
1545 window->keyboard_device = input;
1546 if (window->keyboard_focus_handler)
1547 (*window->keyboard_focus_handler)(window,
1548 window->keyboard_device,
1549 window->user_data);
1550 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001551}
1552
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001553static void
1554input_handle_touch_down(void *data,
1555 struct wl_input_device *wl_input_device,
1556 uint32_t time, struct wl_surface *surface,
1557 int32_t id, int32_t x, int32_t y)
1558{
1559}
1560
1561static void
1562input_handle_touch_up(void *data,
1563 struct wl_input_device *wl_input_device,
1564 uint32_t time, int32_t id)
1565{
1566}
1567
1568static void
1569input_handle_touch_motion(void *data,
1570 struct wl_input_device *wl_input_device,
1571 uint32_t time, int32_t id, int32_t x, int32_t y)
1572{
1573}
1574
1575static void
1576input_handle_touch_frame(void *data,
1577 struct wl_input_device *wl_input_device)
1578{
1579}
1580
1581static void
1582input_handle_touch_cancel(void *data,
1583 struct wl_input_device *wl_input_device)
1584{
1585}
1586
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001587static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001588 input_handle_motion,
1589 input_handle_button,
1590 input_handle_key,
1591 input_handle_pointer_focus,
1592 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001593 input_handle_touch_down,
1594 input_handle_touch_up,
1595 input_handle_touch_motion,
1596 input_handle_touch_frame,
1597 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001598};
1599
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001600void
1601input_get_position(struct input *input, int32_t *x, int32_t *y)
1602{
1603 *x = input->sx;
1604 *y = input->sy;
1605}
1606
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001607struct wl_input_device *
1608input_get_input_device(struct input *input)
1609{
1610 return input->input_device;
1611}
1612
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001613uint32_t
1614input_get_modifiers(struct input *input)
1615{
1616 return input->modifiers;
1617}
1618
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001619struct data_offer {
1620 struct wl_data_offer *offer;
1621 struct input *input;
1622 struct wl_array types;
1623 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001624
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001625 struct task io_task;
1626 int fd;
1627 data_func_t func;
1628 int32_t x, y;
1629 void *user_data;
1630};
1631
1632static void
1633data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1634{
1635 struct data_offer *offer = data;
1636 char **p;
1637
1638 p = wl_array_add(&offer->types, sizeof *p);
1639 *p = strdup(type);
1640}
1641
1642static const struct wl_data_offer_listener data_offer_listener = {
1643 data_offer_offer,
1644};
1645
1646static void
1647data_offer_destroy(struct data_offer *offer)
1648{
1649 char **p;
1650
1651 offer->refcount--;
1652 if (offer->refcount == 0) {
1653 wl_data_offer_destroy(offer->offer);
1654 for (p = offer->types.data; *p; p++)
1655 free(*p);
1656 wl_array_release(&offer->types);
1657 free(offer);
1658 }
1659}
1660
1661static void
1662data_device_data_offer(void *data,
1663 struct wl_data_device *data_device, uint32_t id)
1664{
1665 struct data_offer *offer;
1666
1667 offer = malloc(sizeof *offer);
1668
1669 wl_array_init(&offer->types);
1670 offer->refcount = 1;
1671 offer->input = data;
1672
1673 /* FIXME: Generate typesafe wrappers for this */
1674 offer->offer = (struct wl_data_offer *)
1675 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1676 id, &wl_data_offer_interface);
1677
1678 wl_data_offer_add_listener(offer->offer,
1679 &data_offer_listener, offer);
1680}
1681
1682static void
1683data_device_enter(void *data, struct wl_data_device *data_device,
1684 uint32_t time, struct wl_surface *surface,
1685 int32_t x, int32_t y, struct wl_data_offer *offer)
1686{
1687 struct input *input = data;
1688 struct window *window;
1689 char **p;
1690
1691 input->drag_offer = wl_data_offer_get_user_data(offer);
1692 window = wl_surface_get_user_data(surface);
1693 input->pointer_focus = window;
1694
1695 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1696 *p = NULL;
1697
1698 window = input->pointer_focus;
1699 if (window->data_handler)
1700 window->data_handler(window, input, time, x, y,
1701 input->drag_offer->types.data,
1702 window->user_data);
1703}
1704
1705static void
1706data_device_leave(void *data, struct wl_data_device *data_device)
1707{
1708 struct input *input = data;
1709
1710 data_offer_destroy(input->drag_offer);
1711 input->drag_offer = NULL;
1712}
1713
1714static void
1715data_device_motion(void *data, struct wl_data_device *data_device,
1716 uint32_t time, int32_t x, int32_t y)
1717{
1718 struct input *input = data;
1719 struct window *window = input->pointer_focus;
1720
1721 input->sx = x;
1722 input->sy = y;
1723
1724 if (window->data_handler)
1725 window->data_handler(window, input, time, x, y,
1726 input->drag_offer->types.data,
1727 window->user_data);
1728}
1729
1730static void
1731data_device_drop(void *data, struct wl_data_device *data_device)
1732{
1733 struct input *input = data;
1734 struct window *window = input->pointer_focus;
1735
1736 if (window->drop_handler)
1737 window->drop_handler(window, input,
1738 input->sx, input->sy, window->user_data);
1739}
1740
1741static void
1742data_device_selection(void *data,
1743 struct wl_data_device *wl_data_device,
1744 struct wl_data_offer *offer)
1745{
1746 struct input *input = data;
1747 char **p;
1748
1749 if (input->selection_offer)
1750 data_offer_destroy(input->selection_offer);
1751
1752 input->selection_offer = wl_data_offer_get_user_data(offer);
1753 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1754 *p = NULL;
1755}
1756
1757static const struct wl_data_device_listener data_device_listener = {
1758 data_device_data_offer,
1759 data_device_enter,
1760 data_device_leave,
1761 data_device_motion,
1762 data_device_drop,
1763 data_device_selection
1764};
1765
1766struct wl_data_device *
1767input_get_data_device(struct input *input)
1768{
1769 return input->data_device;
1770}
1771
1772void
1773input_set_selection(struct input *input,
1774 struct wl_data_source *source, uint32_t time)
1775{
1776 wl_data_device_set_selection(input->data_device, source, time);
1777}
1778
1779void
1780input_accept(struct input *input, uint32_t time, const char *type)
1781{
1782 wl_data_offer_accept(input->drag_offer->offer, time, type);
1783}
1784
1785static void
1786offer_io_func(struct task *task, uint32_t events)
1787{
1788 struct data_offer *offer =
1789 container_of(task, struct data_offer, io_task);
1790 unsigned int len;
1791 char buffer[4096];
1792
1793 len = read(offer->fd, buffer, sizeof buffer);
1794 offer->func(buffer, len,
1795 offer->x, offer->y, offer->user_data);
1796
1797 if (len == 0) {
1798 close(offer->fd);
1799 data_offer_destroy(offer);
1800 }
1801}
1802
1803static void
1804data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1805 data_func_t func, void *user_data)
1806{
1807 int p[2];
1808
1809 pipe2(p, O_CLOEXEC);
1810 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1811 close(p[1]);
1812
1813 offer->io_task.run = offer_io_func;
1814 offer->fd = p[0];
1815 offer->func = func;
1816 offer->refcount++;
1817 offer->user_data = user_data;
1818
1819 display_watch_fd(offer->input->display,
1820 offer->fd, EPOLLIN, &offer->io_task);
1821}
1822
1823void
1824input_receive_drag_data(struct input *input, const char *mime_type,
1825 data_func_t func, void *data)
1826{
1827 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1828 input->drag_offer->x = input->sx;
1829 input->drag_offer->y = input->sy;
1830}
1831
1832int
1833input_receive_selection_data(struct input *input, const char *mime_type,
1834 data_func_t func, void *data)
1835{
1836 char **p;
1837
1838 if (input->selection_offer == NULL)
1839 return -1;
1840
1841 for (p = input->selection_offer->types.data; *p; p++)
1842 if (strcmp(mime_type, *p) == 0)
1843 break;
1844
1845 if (*p == NULL)
1846 return -1;
1847
1848 data_offer_receive_data(input->selection_offer,
1849 mime_type, func, data);
1850 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001851}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001852
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001853int
1854input_receive_selection_data_to_fd(struct input *input,
1855 const char *mime_type, int fd)
1856{
1857 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1858
1859 return 0;
1860}
1861
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001862void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001863window_move(struct window *window, struct input *input, uint32_t time)
1864{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001865 if (!window->shell_surface)
1866 return;
1867
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001868 wl_shell_surface_move(window->shell_surface,
1869 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001870}
1871
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001872static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001873handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001874 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001875 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001876{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001877 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001878 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001879
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001880 /* FIXME: this is probably the wrong place to check for width
1881 * or height <= 0, but it prevents the compositor from crashing
1882 */
1883 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001884 return;
1885
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001886 window->resize_edges = edges;
1887
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001888 if (window->resize_handler) {
1889 child_width = width - 20 - window->margin * 2;
1890 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001891
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001892 (*window->resize_handler)(window,
1893 child_width, child_height,
1894 window->user_data);
1895 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001896 window->allocation.width = width;
1897 window->allocation.height = height;
1898
1899 if (window->redraw_handler)
1900 window_schedule_redraw(window);
1901 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001902}
1903
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001904static void
1905handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1906{
1907 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001908 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001909 /* FIXME: Need more context in this event, at least the input
1910 * device. Or just use wl_callback. */
1911
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001912 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001913 window_destroy(window);
1914}
1915
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001916static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001917 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001918 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001919};
1920
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001921void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001922window_get_allocation(struct window *window,
1923 struct rectangle *allocation)
1924{
1925 *allocation = window->allocation;
1926}
1927
1928void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001929window_get_child_allocation(struct window *window,
1930 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001931{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001932 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001933 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001934 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001935 allocation->x = window->margin + 10;
1936 allocation->y = window->margin + 50;
1937 allocation->width =
1938 window->allocation.width - 20 - window->margin * 2;
1939 allocation->height =
1940 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001941 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001942}
1943
1944void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001945window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001946{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001947 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001948 window->allocation.x = 20 + window->margin;
1949 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001950 window->allocation.width = width + 20 + window->margin * 2;
1951 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001952 } else {
1953 window->allocation.x = 0;
1954 window->allocation.y = 0;
1955 window->allocation.width = width;
1956 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001957 }
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05001958
1959 window->widget->allocation = window->allocation;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001960}
1961
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001962static void
1963idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001964{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001965 struct window *window =
1966 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001967
1968 window->redraw_handler(window, window->user_data);
1969 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001970}
1971
1972void
1973window_schedule_redraw(struct window *window)
1974{
1975 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001976 window->redraw_task.run = idle_redraw;
1977 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001978 window->redraw_scheduled = 1;
1979 }
1980}
1981
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001982void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001983window_set_custom(struct window *window)
1984{
1985 window->type = TYPE_CUSTOM;
1986}
1987
1988void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001989window_set_fullscreen(struct window *window, int fullscreen)
1990{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001991 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001992 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001993
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001994 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001995 return;
1996
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001997 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001998 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001999 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002000 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002001 width = output->allocation.width;
2002 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002003 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002004 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002005 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002006 width = window->saved_allocation.width - 20 - window->margin * 2;
2007 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002008 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002009 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002010
2011 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002012}
2013
2014void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002015window_set_decoration(struct window *window, int decoration)
2016{
2017 window->decoration = decoration;
2018}
2019
2020void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002021window_set_user_data(struct window *window, void *data)
2022{
2023 window->user_data = data;
2024}
2025
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002026void *
2027window_get_user_data(struct window *window)
2028{
2029 return window->user_data;
2030}
2031
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002032void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002033window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002034 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002035{
2036 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002037}
2038
2039void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002040window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002041 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002042{
2043 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002044}
2045
2046void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002047window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002048 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002049{
2050 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002051}
2052
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002053void
2054window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002055 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002056{
2057 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002058}
2059
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002060void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061window_set_data_handler(struct window *window, window_data_handler_t handler)
2062{
2063 window->data_handler = handler;
2064}
2065
2066void
2067window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2068{
2069 window->drop_handler = handler;
2070}
2071
2072void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002073window_set_close_handler(struct window *window,
2074 window_close_handler_t handler)
2075{
2076 window->close_handler = handler;
2077}
2078
2079void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002080window_set_transparent(struct window *window, int transparent)
2081{
2082 window->transparent = transparent;
2083}
2084
2085void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002086window_set_title(struct window *window, const char *title)
2087{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002088 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002089 window->title = strdup(title);
2090}
2091
2092const char *
2093window_get_title(struct window *window)
2094{
2095 return window->title;
2096}
2097
2098void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002099display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2100 int32_t x, int32_t y, int32_t width, int32_t height)
2101{
2102 struct wl_buffer *buffer;
2103
2104 buffer = display_get_buffer_for_surface(display, cairo_surface);
2105
2106 wl_buffer_damage(buffer, x, y, width, height);
2107}
2108
2109void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002110window_damage(struct window *window, int32_t x, int32_t y,
2111 int32_t width, int32_t height)
2112{
2113 wl_surface_damage(window->surface, x, y, width, height);
2114}
2115
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002116static struct window *
2117window_create_internal(struct display *display, struct window *parent,
2118 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002119{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002120 struct window *window;
2121
2122 window = malloc(sizeof *window);
2123 if (window == NULL)
2124 return NULL;
2125
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002126 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002127 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002128 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002129 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002130 if (display->shell) {
2131 window->shell_surface =
2132 wl_shell_get_shell_surface(display->shell,
2133 window->surface);
2134 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002135 window->allocation.x = 0;
2136 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002137 window->allocation.width = width;
2138 window->allocation.height = height;
2139 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002140 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002141 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002142 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002143
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002144 wl_list_init(&window->widget_list);
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002145 window->widget = window_add_widget(window, window);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002146
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002147 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002148#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002149 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2150 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002151#else
2152 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2153#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002154 else
2155 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002156
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002157 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002158 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002159
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002160 if (window->shell_surface) {
2161 wl_shell_surface_set_user_data(window->shell_surface, window);
2162 wl_shell_surface_add_listener(window->shell_surface,
2163 &shell_surface_listener, window);
2164 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002165
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002166 return window;
2167}
2168
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002169struct window *
2170window_create(struct display *display, int32_t width, int32_t height)
2171{
2172 struct window *window;
2173
2174 window = window_create_internal(display, NULL, width, height);
2175 if (!window)
2176 return NULL;
2177
2178 return window;
2179}
2180
2181struct window *
2182window_create_transient(struct display *display, struct window *parent,
2183 int32_t x, int32_t y, int32_t width, int32_t height)
2184{
2185 struct window *window;
2186
2187 window = window_create_internal(parent->display,
2188 parent, width, height);
2189 if (!window)
2190 return NULL;
2191
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002192 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002193 window->x = x;
2194 window->y = y;
2195
2196 return window;
2197}
2198
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002199static int
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002200menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002201{
2202 int next;
2203
2204 next = (sy - 8) / 20;
2205 if (menu->current != next) {
2206 menu->current = next;
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002207 widget_schedule_redraw(menu->window->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002208 }
2209
2210 return POINTER_LEFT_PTR;
2211}
2212
2213static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002214menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002215 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002216 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002217{
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002218 struct window *window = data;
2219 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002220
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002221 return menu_set_item(menu, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002222}
2223
2224static void
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002225menu_enter_handler(struct widget *widget,
2226 struct input *input, uint32_t time,
2227 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002228{
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002229 struct window *window = data;
2230 struct menu *menu = window_get_user_data(window);
2231
2232 menu_set_item(menu, y);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002233}
2234
2235static void
2236menu_leave_handler(struct widget *widget, struct input *input, void *data)
2237{
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002238 struct window *window = data;
2239 struct menu *menu = window_get_user_data(window);
2240
2241 menu_set_item(menu, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002242}
2243
2244static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002245menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002246 struct input *input, uint32_t time,
2247 int button, int state, void *data)
2248
2249{
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002250 struct window *window = data;
2251 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002252
2253 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002254 if (state == 0 && time - menu->time > 500) {
2255 menu->func(window->parent,
2256 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002257 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002258 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002259}
2260
2261static void
2262menu_redraw_handler(struct window *window, void *data)
2263{
2264 cairo_t *cr;
2265 const int32_t r = 3, margin = 3;
2266 struct menu *menu = data;
2267 int32_t width, height, i;
2268
2269 width = 200;
2270 height = menu->count * 20 + margin * 2;
2271 window_set_child_size(window, width, height);
2272 window_create_surface(window);
2273
2274 cr = cairo_create(window->cairo_surface);
2275 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2276 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2277 cairo_paint(cr);
2278
2279 width = window->allocation.width;
2280 height = window->allocation.height;
2281 rounded_rect(cr, 0, 0, width, height, r);
2282 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2283 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2284 cairo_fill(cr);
2285
2286 for (i = 0; i < menu->count; i++) {
2287 if (i == menu->current) {
2288 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2289 cairo_rectangle(cr, margin, i * 20 + margin,
2290 width - 2 * margin, 20);
2291 cairo_fill(cr);
2292 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2293 cairo_move_to(cr, 10, i * 20 + 16);
2294 cairo_show_text(cr, menu->entries[i]);
2295 } else {
2296 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2297 cairo_move_to(cr, 10, i * 20 + 16);
2298 cairo_show_text(cr, menu->entries[i]);
2299 }
2300 }
2301
2302 cairo_destroy(cr);
2303 window_flush(window);
2304}
2305
2306struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002307window_create_menu(struct display *display,
2308 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002309 int32_t x, int32_t y,
2310 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002311{
2312 struct window *window;
2313 struct menu *menu;
2314
2315 menu = malloc(sizeof *menu);
2316 if (!menu)
2317 return NULL;
2318
2319 window = window_create_internal(parent->display, parent, 0, 0);
2320 if (!window)
2321 return NULL;
2322
2323 menu->window = window;
2324 menu->entries = entries;
2325 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002326 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002327 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002328 window->decoration = 0;
2329 window->type = TYPE_MENU;
2330 window->x = x;
2331 window->y = y;
2332
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002333 wl_shell_surface_set_popup(window->shell_surface,
2334 input->input_device, time,
2335 window->parent->shell_surface,
2336 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002337
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002338 window_set_redraw_handler(window, menu_redraw_handler);
2339 window_set_user_data(window, menu);
2340
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002341 widget_set_enter_handler(window->widget, menu_enter_handler);
2342 widget_set_leave_handler(window->widget, menu_leave_handler);
2343 widget_set_motion_handler(window->widget, menu_motion_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002344 widget_set_button_handler(window->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002345
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002346 return window;
2347}
2348
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002349void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002350window_set_buffer_type(struct window *window, enum window_buffer_type type)
2351{
2352 window->buffer_type = type;
2353}
2354
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002355
2356static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002357display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002358 struct wl_output *wl_output,
2359 int x, int y,
2360 int physical_width,
2361 int physical_height,
2362 int subpixel,
2363 const char *make,
2364 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002365{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002366 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002367
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002368 output->allocation.x = x;
2369 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002370}
2371
2372static void
2373display_handle_mode(void *data,
2374 struct wl_output *wl_output,
2375 uint32_t flags,
2376 int width,
2377 int height,
2378 int refresh)
2379{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002380 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002381 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002382
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002383 if (flags & WL_OUTPUT_MODE_CURRENT) {
2384 output->allocation.width = width;
2385 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002386 if (display->output_configure_handler)
2387 (*display->output_configure_handler)(
2388 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002389 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002390}
2391
2392static const struct wl_output_listener output_listener = {
2393 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002394 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002395};
2396
2397static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002398display_add_output(struct display *d, uint32_t id)
2399{
2400 struct output *output;
2401
2402 output = malloc(sizeof *output);
2403 if (output == NULL)
2404 return;
2405
2406 memset(output, 0, sizeof *output);
2407 output->display = d;
2408 output->output =
2409 wl_display_bind(d->display, id, &wl_output_interface);
2410 wl_list_insert(d->output_list.prev, &output->link);
2411
2412 wl_output_add_listener(output->output, &output_listener, output);
2413}
2414
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002415static void
2416output_destroy(struct output *output)
2417{
2418 if (output->destroy_handler)
2419 (*output->destroy_handler)(output, output->user_data);
2420
2421 wl_output_destroy(output->output);
2422 wl_list_remove(&output->link);
2423 free(output);
2424}
2425
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002426void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002427display_set_output_configure_handler(struct display *display,
2428 display_output_handler_t handler)
2429{
2430 struct output *output;
2431
2432 display->output_configure_handler = handler;
2433 if (!handler)
2434 return;
2435
2436 wl_list_for_each(output, &display->output_list, link)
2437 (*display->output_configure_handler)(output,
2438 display->user_data);
2439}
2440
2441void
2442output_set_user_data(struct output *output, void *data)
2443{
2444 output->user_data = data;
2445}
2446
2447void *
2448output_get_user_data(struct output *output)
2449{
2450 return output->user_data;
2451}
2452
2453void
2454output_set_destroy_handler(struct output *output,
2455 display_output_handler_t handler)
2456{
2457 output->destroy_handler = handler;
2458 /* FIXME: implement this, once we have way to remove outputs */
2459}
2460
2461void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002462output_get_allocation(struct output *output, struct rectangle *allocation)
2463{
2464 *allocation = output->allocation;
2465}
2466
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002467struct wl_output *
2468output_get_wl_output(struct output *output)
2469{
2470 return output->output;
2471}
2472
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002473static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002474display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002475{
2476 struct input *input;
2477
2478 input = malloc(sizeof *input);
2479 if (input == NULL)
2480 return;
2481
2482 memset(input, 0, sizeof *input);
2483 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002484 input->input_device =
2485 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002486 input->pointer_focus = NULL;
2487 input->keyboard_focus = NULL;
2488 wl_list_insert(d->input_list.prev, &input->link);
2489
2490 wl_input_device_add_listener(input->input_device,
2491 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002492 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002493
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002494 input->data_device =
2495 wl_data_device_manager_get_data_device(d->data_device_manager,
2496 input->input_device);
2497 wl_data_device_add_listener(input->data_device,
2498 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002499}
2500
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002501static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002502input_destroy(struct input *input)
2503{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002504 input_remove_keyboard_focus(input);
2505 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002506
2507 if (input->drag_offer)
2508 data_offer_destroy(input->drag_offer);
2509
2510 if (input->selection_offer)
2511 data_offer_destroy(input->selection_offer);
2512
2513 wl_data_device_destroy(input->data_device);
2514 wl_list_remove(&input->link);
2515 wl_input_device_destroy(input->input_device);
2516 free(input);
2517}
2518
2519static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002520display_handle_global(struct wl_display *display, uint32_t id,
2521 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002522{
2523 struct display *d = data;
2524
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002525 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002526 d->compositor =
2527 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002528 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002529 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002530 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002531 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002532 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002533 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002534 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002535 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002536 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2537 d->data_device_manager =
2538 wl_display_bind(display, id,
2539 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002540 }
2541}
2542
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002543static void
2544display_render_frame(struct display *d)
2545{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002546 int radius = 8;
2547 cairo_t *cr;
2548
2549 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2550 cr = cairo_create(d->shadow);
2551 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2552 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2553 rounded_rect(cr, 16, 16, 112, 112, radius);
2554 cairo_fill(cr);
2555 cairo_destroy(cr);
2556 blur_surface(d->shadow, 64);
2557
2558 d->active_frame =
2559 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2560 cr = cairo_create(d->active_frame);
2561 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2562 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2563 rounded_rect(cr, 16, 16, 112, 112, radius);
2564 cairo_fill(cr);
2565 cairo_destroy(cr);
2566
2567 d->inactive_frame =
2568 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2569 cr = cairo_create(d->inactive_frame);
2570 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2571 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2572 rounded_rect(cr, 16, 16, 112, 112, radius);
2573 cairo_fill(cr);
2574 cairo_destroy(cr);
2575}
2576
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002577static void
2578init_xkb(struct display *d)
2579{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002580 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002581
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002582 names.rules = "evdev";
2583 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002584 names.layout = option_xkb_layout;
2585 names.variant = option_xkb_variant;
2586 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002587
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002588 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002589 if (!d->xkb) {
2590 fprintf(stderr, "Failed to compile keymap\n");
2591 exit(1);
2592 }
2593}
2594
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002595static void
2596fini_xkb(struct display *display)
2597{
2598 xkb_free_keymap(display->xkb);
2599}
2600
Yuval Fledel45568f62010-12-06 09:18:12 -05002601static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002602init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002603{
2604 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002605 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002606
2607 static const EGLint premul_argb_cfg_attribs[] = {
2608 EGL_SURFACE_TYPE,
2609 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2610 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002611 EGL_RED_SIZE, 1,
2612 EGL_GREEN_SIZE, 1,
2613 EGL_BLUE_SIZE, 1,
2614 EGL_ALPHA_SIZE, 1,
2615 EGL_DEPTH_SIZE, 1,
2616 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2617 EGL_NONE
2618 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002619
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002620 static const EGLint rgb_cfg_attribs[] = {
2621 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2622 EGL_RED_SIZE, 1,
2623 EGL_GREEN_SIZE, 1,
2624 EGL_BLUE_SIZE, 1,
2625 EGL_ALPHA_SIZE, 0,
2626 EGL_DEPTH_SIZE, 1,
2627 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2628 EGL_NONE
2629 };
2630
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002631 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002632 if (!eglInitialize(d->dpy, &major, &minor)) {
2633 fprintf(stderr, "failed to initialize display\n");
2634 return -1;
2635 }
2636
2637 if (!eglBindAPI(EGL_OPENGL_API)) {
2638 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2639 return -1;
2640 }
2641
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002642 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002643 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002644 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002645 return -1;
2646 }
2647
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002648 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2649 &d->rgb_config, 1, &n) || n != 1) {
2650 fprintf(stderr, "failed to choose rgb config\n");
2651 return -1;
2652 }
2653
Benjamin Franzke0c991632011-09-27 21:57:31 +02002654 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2655 if (d->rgb_ctx == NULL) {
2656 fprintf(stderr, "failed to create context\n");
2657 return -1;
2658 }
2659 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2660 EGL_NO_CONTEXT, NULL);
2661 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002662 fprintf(stderr, "failed to create context\n");
2663 return -1;
2664 }
2665
Benjamin Franzke0c991632011-09-27 21:57:31 +02002666 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002667 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002668 return -1;
2669 }
2670
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002671#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002672 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2673 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002674 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002675 return -1;
2676 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002677 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2678 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2679 fprintf(stderr, "failed to get cairo egl argb device\n");
2680 return -1;
2681 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002682#endif
2683
2684 return 0;
2685}
2686
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002687static void
2688fini_egl(struct display *display)
2689{
2690#ifdef HAVE_CAIRO_EGL
2691 cairo_device_destroy(display->argb_device);
2692 cairo_device_destroy(display->rgb_device);
2693#endif
2694
2695 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2696 EGL_NO_CONTEXT);
2697
2698 eglTerminate(display->dpy);
2699 eglReleaseThread();
2700}
2701
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002702static int
2703event_mask_update(uint32_t mask, void *data)
2704{
2705 struct display *d = data;
2706
2707 d->mask = mask;
2708
2709 return 0;
2710}
2711
2712static void
2713handle_display_data(struct task *task, uint32_t events)
2714{
2715 struct display *display =
2716 container_of(task, struct display, display_task);
2717
2718 wl_display_iterate(display->display, display->mask);
2719}
2720
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002721struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002722display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002723{
2724 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002725 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002726 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002727 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002728
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002729 g_type_init();
2730
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002731 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002732 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002733 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002734
2735 xkb_option_group = g_option_group_new("xkb",
2736 "Keyboard options",
2737 "Show all XKB options",
2738 NULL, NULL);
2739 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2740 g_option_context_add_group (context, xkb_option_group);
2741
2742 if (!g_option_context_parse(context, argc, argv, &error)) {
2743 fprintf(stderr, "option parsing failed: %s\n", error->message);
2744 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002745 }
2746
Tim Wiederhake748f6722011-01-23 23:25:25 +01002747 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002748
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002749 d = malloc(sizeof *d);
2750 if (d == NULL)
2751 return NULL;
2752
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002753 memset(d, 0, sizeof *d);
2754
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002755 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002756 if (d->display == NULL) {
2757 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002758 return NULL;
2759 }
2760
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002761 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2762 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2763 d->display_task.run = handle_display_data;
2764 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2765
2766 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002767 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002768 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002769
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002770 /* Set up listener so we'll catch all events. */
2771 wl_display_add_global_listener(d->display,
2772 display_handle_global, d);
2773
2774 /* Process connection events. */
2775 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002776 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002777 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002778
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002779 d->image_target_texture_2d =
2780 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2781 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2782 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2783
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002784 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002785
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002786 display_render_frame(d);
2787
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002788 wl_list_init(&d->window_list);
2789
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002790 init_xkb(d);
2791
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002792 return d;
2793}
2794
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002795static void
2796display_destroy_outputs(struct display *display)
2797{
2798 struct output *tmp;
2799 struct output *output;
2800
2801 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2802 output_destroy(output);
2803}
2804
Pekka Paalanene1207c72011-12-16 12:02:09 +02002805static void
2806display_destroy_inputs(struct display *display)
2807{
2808 struct input *tmp;
2809 struct input *input;
2810
2811 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2812 input_destroy(input);
2813}
2814
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002815void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002816display_destroy(struct display *display)
2817{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002818 if (!wl_list_empty(&display->window_list))
2819 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2820
2821 if (!wl_list_empty(&display->deferred_list))
2822 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2823
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002824 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002825 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002826
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002827 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002828
2829 cairo_surface_destroy(display->active_frame);
2830 cairo_surface_destroy(display->inactive_frame);
2831 cairo_surface_destroy(display->shadow);
2832 destroy_pointer_surfaces(display);
2833
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002834 fini_egl(display);
2835
Pekka Paalanenc2052982011-12-16 11:41:32 +02002836 if (display->shell)
2837 wl_shell_destroy(display->shell);
2838
2839 if (display->shm)
2840 wl_shm_destroy(display->shm);
2841
2842 if (display->data_device_manager)
2843 wl_data_device_manager_destroy(display->data_device_manager);
2844
2845 wl_compositor_destroy(display->compositor);
2846
2847 close(display->epoll_fd);
2848
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002849 wl_display_flush(display->display);
2850 wl_display_destroy(display->display);
2851 free(display);
2852}
2853
2854void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002855display_set_user_data(struct display *display, void *data)
2856{
2857 display->user_data = data;
2858}
2859
2860void *
2861display_get_user_data(struct display *display)
2862{
2863 return display->user_data;
2864}
2865
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002866struct wl_display *
2867display_get_display(struct display *display)
2868{
2869 return display->display;
2870}
2871
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002872struct output *
2873display_get_output(struct display *display)
2874{
2875 return container_of(display->output_list.next, struct output, link);
2876}
2877
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002878struct wl_compositor *
2879display_get_compositor(struct display *display)
2880{
2881 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002882}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002883
2884EGLDisplay
2885display_get_egl_display(struct display *d)
2886{
2887 return d->dpy;
2888}
2889
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002890struct wl_data_source *
2891display_create_data_source(struct display *display)
2892{
2893 return wl_data_device_manager_create_data_source(display->data_device_manager);
2894}
2895
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002896EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002897display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002898{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002899 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002900}
2901
Benjamin Franzke0c991632011-09-27 21:57:31 +02002902EGLConfig
2903display_get_argb_egl_config(struct display *d)
2904{
2905 return d->premultiplied_argb_config;
2906}
2907
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002908struct wl_shell *
2909display_get_shell(struct display *display)
2910{
2911 return display->shell;
2912}
2913
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002914int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002915display_acquire_window_surface(struct display *display,
2916 struct window *window,
2917 EGLContext ctx)
2918{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002919#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002920 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002921 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002922
2923 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002924 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002925 device = cairo_surface_get_device(window->cairo_surface);
2926 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002927 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002928
Benjamin Franzke0c991632011-09-27 21:57:31 +02002929 if (!ctx) {
2930 if (device == display->rgb_device)
2931 ctx = display->rgb_ctx;
2932 else if (device == display->argb_device)
2933 ctx = display->argb_ctx;
2934 else
2935 assert(0);
2936 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002937
2938 data = cairo_surface_get_user_data(window->cairo_surface,
2939 &surface_data_key);
2940
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002941 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002942 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002943 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2944 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002945
2946 return 0;
2947#else
2948 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002949#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002950}
2951
2952void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002953display_release_window_surface(struct display *display,
2954 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002955{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002956#ifdef HAVE_CAIRO_EGL
2957 cairo_device_t *device;
2958
2959 device = cairo_surface_get_device(window->cairo_surface);
2960 if (!device)
2961 return;
2962
2963 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002964 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002965 cairo_device_release(device);
2966#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002967}
2968
2969void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002970display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002971{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002972 wl_list_insert(&display->deferred_list, &task->link);
2973}
2974
2975void
2976display_watch_fd(struct display *display,
2977 int fd, uint32_t events, struct task *task)
2978{
2979 struct epoll_event ep;
2980
2981 ep.events = events;
2982 ep.data.ptr = task;
2983 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2984}
2985
2986void
2987display_run(struct display *display)
2988{
2989 struct task *task;
2990 struct epoll_event ep[16];
2991 int i, count;
2992
Pekka Paalanen826d7952011-12-15 10:14:07 +02002993 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002994 while (1) {
2995 while (display->mask & WL_DISPLAY_WRITABLE)
2996 wl_display_iterate(display->display,
2997 WL_DISPLAY_WRITABLE);
2998
Pekka Paalanen826d7952011-12-15 10:14:07 +02002999 if (!display->running)
3000 break;
3001
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003002 count = epoll_wait(display->epoll_fd,
3003 ep, ARRAY_LENGTH(ep), -1);
3004 for (i = 0; i < count; i++) {
3005 task = ep[i].data.ptr;
3006 task->run(task, ep[i].events);
3007 }
3008
3009 while (!wl_list_empty(&display->deferred_list)) {
3010 task = container_of(display->deferred_list.next,
3011 struct task, link);
3012 wl_list_remove(&task->link);
3013 task->run(task, 0);
3014 }
3015 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003016}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003017
3018void
3019display_exit(struct display *display)
3020{
3021 display->running = 0;
3022}