blob: 00850a07106b1ef6e5cef716053c1b82420fc183 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Pekka Paalanen826d7952011-12-15 10:14:07 +020081 int running;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050085 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500119 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400122 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400125 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500126
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500127 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500128
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500129 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400130 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400132 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500133 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400134 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400135 window_enter_handler_t enter_handler;
136 window_leave_handler_t leave_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400137 window_data_handler_t data_handler;
138 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500139 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500141 struct wl_list widget_list;
142 struct widget *focus_widget;
143 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400144
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500145 struct window *menu;
146
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500147 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400148 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500149};
150
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500151struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500152 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400153 struct wl_list link;
154 struct rectangle allocation;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500155 widget_enter_handler_t enter_handler;
156 widget_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400157 void *user_data;
158};
159
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400160struct input {
161 struct display *display;
162 struct wl_input_device *input_device;
163 struct window *pointer_focus;
164 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400165 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400166 uint32_t modifiers;
167 int32_t x, y, sx, sy;
168 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400169
170 struct wl_data_device *data_device;
171 struct data_offer *drag_offer;
172 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173};
174
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500175struct output {
176 struct display *display;
177 struct wl_output *output;
178 struct rectangle allocation;
179 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200180
181 display_output_handler_t destroy_handler;
182 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183};
184
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500185struct menu {
186 struct window *window;
187 const char **entries;
188 uint32_t time;
189 int current;
190 int count;
191 menu_func_t func;
192};
193
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400194enum {
195 POINTER_DEFAULT = 100,
196 POINTER_UNSET
197};
198
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500199enum window_location {
200 WINDOW_INTERIOR = 0,
201 WINDOW_RESIZING_TOP = 1,
202 WINDOW_RESIZING_BOTTOM = 2,
203 WINDOW_RESIZING_LEFT = 4,
204 WINDOW_RESIZING_TOP_LEFT = 5,
205 WINDOW_RESIZING_BOTTOM_LEFT = 6,
206 WINDOW_RESIZING_RIGHT = 8,
207 WINDOW_RESIZING_TOP_RIGHT = 9,
208 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
209 WINDOW_RESIZING_MASK = 15,
210 WINDOW_EXTERIOR = 16,
211 WINDOW_TITLEBAR = 17,
212 WINDOW_CLIENT_AREA = 18,
213};
214
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400215const char *option_xkb_layout = "us";
216const char *option_xkb_variant = "";
217const char *option_xkb_options = "";
218
219static const GOptionEntry xkb_option_entries[] = {
220 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
221 &option_xkb_layout, "XKB Layout" },
222 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
223 &option_xkb_variant, "XKB Variant" },
224 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
225 &option_xkb_options, "XKB Options" },
226 { NULL }
227};
228
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400229static const cairo_user_data_key_t surface_data_key;
230struct surface_data {
231 struct wl_buffer *buffer;
232};
233
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500234#define MULT(_d,c,a,t) \
235 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
236
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500237#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400238
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100239struct egl_window_surface_data {
240 struct display *display;
241 struct wl_surface *surface;
242 struct wl_egl_window *window;
243 EGLSurface surf;
244};
245
246static void
247egl_window_surface_data_destroy(void *p)
248{
249 struct egl_window_surface_data *data = p;
250 struct display *d = data->display;
251
252 eglDestroySurface(d->dpy, data->surf);
253 wl_egl_window_destroy(data->window);
254 data->surface = NULL;
255
256 free(p);
257}
258
259static cairo_surface_t *
260display_create_egl_window_surface(struct display *display,
261 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400262 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100263 struct rectangle *rectangle)
264{
265 cairo_surface_t *cairo_surface;
266 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400267 EGLConfig config;
268 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200269 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100270
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400271 static const EGLint premul_attribs[] = {
272 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
273 EGL_NONE
274 };
275
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100276 data = malloc(sizeof *data);
277 if (data == NULL)
278 return NULL;
279
280 data->display = display;
281 data->surface = surface;
282
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400283 if (flags & SURFACE_OPAQUE) {
284 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200285 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200286 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400287 } else {
288 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200289 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400290 attribs = premul_attribs;
291 }
292
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400293 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100294 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 data->surf = eglCreateWindowSurface(display->dpy, config,
298 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299
Benjamin Franzke0c991632011-09-27 21:57:31 +0200300 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301 data->surf,
302 rectangle->width,
303 rectangle->height);
304
305 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
306 data, egl_window_surface_data_destroy);
307
308 return cairo_surface;
309}
310
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500311struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200313 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 EGLImageKHR image;
315 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800316 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500317 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318};
319
320static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400322{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800324 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200328 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800329
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500330 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500332 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500333 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334}
335
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500336EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337display_get_image_for_egl_image_surface(struct display *display,
338 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500339{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500340 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341
342 data = cairo_surface_get_user_data (surface, &surface_data_key);
343
344 return data->image;
345}
346
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500347static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400349 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353 EGLDisplay dpy = display->dpy;
354 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200355 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356
357 data = malloc(sizeof *data);
358 if (data == NULL)
359 return NULL;
360
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800361 data->display = display;
362
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400363 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500365 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000366 free(data);
367 return NULL;
368 }
369
Benjamin Franzke0c991632011-09-27 21:57:31 +0200370 if (flags & SURFACE_OPAQUE) {
371 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200372 content = CAIRO_CONTENT_COLOR;
373 } else {
374 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200375 content = CAIRO_CONTENT_COLOR_ALPHA;
376 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500378 data->image = display->create_image(dpy, NULL,
379 EGL_NATIVE_PIXMAP_KHR,
380 (EGLClientBuffer) data->pixmap,
381 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500383 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384 free(data);
385 return NULL;
386 }
387
388 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400389 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500394 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 surface = cairo_gl_surface_create_for_texture(data->device,
398 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 data->texture,
400 rectangle->width,
401 rectangle->height);
402
403 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500404 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405
406 return surface;
407}
408
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500409static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410display_create_egl_image_surface_from_file(struct display *display,
411 const char *filename,
412 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413{
414 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415 GdkPixbuf *pixbuf;
416 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400417 int stride, i;
418 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500419 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400420
421 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400423 FALSE, &error);
424 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400426
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400427 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
428 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500429 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400431 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433
434 stride = gdk_pixbuf_get_rowstride(pixbuf);
435 pixels = gdk_pixbuf_get_pixels(pixbuf);
436
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400438 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400440 while (p < end) {
441 unsigned int t;
442
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400443 MULT(p[0], p[0], p[3], t);
444 MULT(p[1], p[1], p[3], t);
445 MULT(p[2], p[2], p[3], t);
446 p += 4;
447
448 }
449 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400450
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200451 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000452 if (surface == NULL) {
453 g_object_unref(pixbuf);
454 return NULL;
455 }
456
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800457 data = cairo_surface_get_user_data(surface, &surface_data_key);
458
Benjamin Franzke0c991632011-09-27 21:57:31 +0200459 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800460 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800461 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
462 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200463 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400464
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500465 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400466
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467 return surface;
468}
469
470#endif
471
472struct wl_buffer *
473display_get_buffer_for_surface(struct display *display,
474 cairo_surface_t *surface)
475{
476 struct surface_data *data;
477
478 data = cairo_surface_get_user_data (surface, &surface_data_key);
479
480 return data->buffer;
481}
482
483struct shm_surface_data {
484 struct surface_data data;
485 void *map;
486 size_t length;
487};
488
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500489static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490shm_surface_data_destroy(void *p)
491{
492 struct shm_surface_data *data = p;
493
494 wl_buffer_destroy(data->data.buffer);
495 munmap(data->map, data->length);
496}
497
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500498static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400500 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501{
502 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400503 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400504 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800505 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506 char filename[] = "/tmp/wayland-shm-XXXXXX";
507
508 data = malloc(sizeof *data);
509 if (data == NULL)
510 return NULL;
511
512 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
513 rectangle->width);
514 data->length = stride * rectangle->height;
515 fd = mkstemp(filename);
516 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000517 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518 return NULL;
519 }
520 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000521 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522 close(fd);
523 return NULL;
524 }
525
526 data->map = mmap(NULL, data->length,
527 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
528 unlink(filename);
529
530 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000531 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532 close(fd);
533 return NULL;
534 }
535
536 surface = cairo_image_surface_create_for_data (data->map,
537 CAIRO_FORMAT_ARGB32,
538 rectangle->width,
539 rectangle->height,
540 stride);
541
542 cairo_surface_set_user_data (surface, &surface_data_key,
543 data, shm_surface_data_destroy);
544
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400545 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400546 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400548 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 data->data.buffer = wl_shm_create_buffer(display->shm,
551 fd,
552 rectangle->width,
553 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400554 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555
556 close(fd);
557
558 return surface;
559}
560
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500561static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562display_create_shm_surface_from_file(struct display *display,
563 const char *filename,
564 struct rectangle *rect)
565{
566 cairo_surface_t *surface;
567 GdkPixbuf *pixbuf;
568 GError *error = NULL;
569 int stride, i;
570 unsigned char *pixels, *p, *end, *dest_data;
571 int dest_stride;
572 uint32_t *d;
573
574 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
575 rect->width, rect->height,
576 FALSE, &error);
577 if (error != NULL)
578 return NULL;
579
580 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
581 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500582 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583 return NULL;
584 }
585
586 stride = gdk_pixbuf_get_rowstride(pixbuf);
587 pixels = gdk_pixbuf_get_pixels(pixbuf);
588
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400589 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000590 if (surface == NULL) {
591 g_object_unref(pixbuf);
592 return NULL;
593 }
594
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595 dest_data = cairo_image_surface_get_data (surface);
596 dest_stride = cairo_image_surface_get_stride (surface);
597
598 for (i = 0; i < rect->height; i++) {
599 d = (uint32_t *) (dest_data + i * dest_stride);
600 p = pixels + i * stride;
601 end = p + rect->width * 4;
602 while (p < end) {
603 unsigned int t;
604 unsigned char a, r, g, b;
605
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606 a = p[3];
607 MULT(r, p[0], a, t);
608 MULT(g, p[1], a, t);
609 MULT(b, p[2], a, t);
610 p += 4;
611 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
612 }
613 }
614
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500615 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616
617 return surface;
618}
619
nobled7b87cb02011-02-01 18:51:47 +0000620static int
621check_size(struct rectangle *rect)
622{
623 if (rect->width && rect->height)
624 return 0;
625
626 fprintf(stderr, "tried to create surface of "
627 "width: %d, height: %d\n", rect->width, rect->height);
628 return -1;
629}
630
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631cairo_surface_t *
632display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100633 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400634 struct rectangle *rectangle,
635 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636{
nobled7b87cb02011-02-01 18:51:47 +0000637 if (check_size(rectangle) < 0)
638 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500639#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500640 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641 if (surface)
642 return display_create_egl_window_surface(display,
643 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400644 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645 rectangle);
646 else
647 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400648 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500650 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400652 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400653}
654
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500655static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656display_create_surface_from_file(struct display *display,
657 const char *filename,
658 struct rectangle *rectangle)
659{
nobled7b87cb02011-02-01 18:51:47 +0000660 if (check_size(rectangle) < 0)
661 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500662#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500663 if (display->dpy) {
664 return display_create_egl_image_surface_from_file(display,
665 filename,
666 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500667 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670}
Yuval Fledel45568f62010-12-06 09:18:12 -0500671 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672 const char *filename;
673 int hotspot_x, hotspot_y;
674} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500675 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
676 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
677 { DATADIR "/weston/bottom_side.png", 16, 20 },
678 { DATADIR "/weston/grabbing.png", 20, 17 },
679 { DATADIR "/weston/left_ptr.png", 10, 5 },
680 { DATADIR "/weston/left_side.png", 10, 20 },
681 { DATADIR "/weston/right_side.png", 30, 19 },
682 { DATADIR "/weston/top_left_corner.png", 8, 8 },
683 { DATADIR "/weston/top_right_corner.png", 26, 8 },
684 { DATADIR "/weston/top_side.png", 18, 8 },
685 { DATADIR "/weston/xterm.png", 15, 15 },
686 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400687};
688
689static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400690create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691{
692 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400694 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400697 display->pointer_surfaces =
698 malloc(count * sizeof *display->pointer_surfaces);
699 rect.width = width;
700 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400703 display_create_surface_from_file(display,
704 pointer_images[i].filename,
705 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100706 if (!display->pointer_surfaces[i]) {
707 fprintf(stderr, "Error loading pointer image: %s\n",
708 pointer_images[i].filename);
709 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400710 }
711
712}
713
Pekka Paalanen325bb602011-12-19 10:31:45 +0200714static void
715destroy_pointer_surfaces(struct display *display)
716{
717 int i, count;
718
719 count = ARRAY_LENGTH(pointer_images);
720 for (i = 0; i < count; ++i) {
721 if (display->pointer_surfaces[i])
722 cairo_surface_destroy(display->pointer_surfaces[i]);
723 }
724 free(display->pointer_surfaces);
725}
726
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400727cairo_surface_t *
728display_get_pointer_surface(struct display *display, int pointer,
729 int *width, int *height,
730 int *hotspot_x, int *hotspot_y)
731{
732 cairo_surface_t *surface;
733
734 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500735#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400736 *width = cairo_gl_surface_get_width(surface);
737 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000738#else
739 *width = cairo_image_surface_get_width(surface);
740 *height = cairo_image_surface_get_height(surface);
741#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 *hotspot_x = pointer_images[pointer].hotspot_x;
743 *hotspot_y = pointer_images[pointer].hotspot_y;
744
745 return cairo_surface_reference(surface);
746}
747
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400748static void
749window_attach_surface(struct window *window);
750
751static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400752free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400753{
754 struct window *window = data;
755
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400756 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400757 cairo_surface_destroy(window->pending_surface);
758 window->pending_surface = NULL;
759 if (window->cairo_surface)
760 window_attach_surface(window);
761}
762
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400763static const struct wl_callback_listener free_surface_listener = {
764 free_surface
765};
766
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500767static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200768window_get_resize_dx_dy(struct window *window, int *x, int *y)
769{
770 if (window->resize_edges & WINDOW_RESIZING_LEFT)
771 *x = window->server_allocation.width - window->allocation.width;
772 else
773 *x = 0;
774
775 if (window->resize_edges & WINDOW_RESIZING_TOP)
776 *y = window->server_allocation.height -
777 window->allocation.height;
778 else
779 *y = 0;
780
781 window->resize_edges = 0;
782}
783
784static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400785window_set_type(struct window *window)
786{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200787 if (!window->shell_surface)
788 return;
789
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400790 switch (window->type) {
791 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200792 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400793 break;
794 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200795 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400796 break;
797 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200798 wl_shell_surface_set_transient(window->shell_surface,
799 window->parent->shell_surface,
800 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400801 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500802 case TYPE_MENU:
803 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400804 case TYPE_CUSTOM:
805 break;
806 }
807}
808
809static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500810window_attach_surface(struct window *window)
811{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400812 struct display *display = window->display;
813 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400814 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000817#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500818 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400820 if (display->shell)
821 window_set_type(window);
822
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
826 data = cairo_surface_get_user_data(window->cairo_surface,
827 &surface_data_key);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 cairo_gl_surface_swapbuffers(window->cairo_surface);
830 wl_egl_window_get_attached_size(data->window,
831 &window->server_allocation.width,
832 &window->server_allocation.height);
833 break;
834 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000835#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200837 window_get_resize_dx_dy(window, &x, &y);
838
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 if (window->pending_surface != NULL)
840 return;
841
842 window->pending_surface = window->cairo_surface;
843 window->cairo_surface = NULL;
844
845 buffer =
846 display_get_buffer_for_surface(display,
847 window->pending_surface);
848
849 wl_surface_attach(window->surface, buffer, x, y);
850 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400851 cb = wl_display_sync(display->display);
852 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000854 default:
855 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500857
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500858 wl_surface_damage(window->surface, 0, 0,
859 window->allocation.width,
860 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500861}
862
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400864window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500865{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100866 if (window->cairo_surface) {
867 switch (window->buffer_type) {
868 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
869 case WINDOW_BUFFER_TYPE_SHM:
870 display_surface_damage(window->display,
871 window->cairo_surface,
872 0, 0,
873 window->allocation.width,
874 window->allocation.height);
875 break;
876 default:
877 break;
878 }
879 window_attach_surface(window);
880 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500881}
882
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400883void
884window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500886 cairo_surface_reference(surface);
887
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400888 if (window->cairo_surface != NULL)
889 cairo_surface_destroy(window->cairo_surface);
890
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500891 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892}
893
Benjamin Franzke22d54812011-07-16 19:50:32 +0000894#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895static void
896window_resize_cairo_window_surface(struct window *window)
897{
898 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200899 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900
901 data = cairo_surface_get_user_data(window->cairo_surface,
902 &surface_data_key);
903
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 wl_egl_window_resize(data->window,
906 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907 window->allocation.height,
908 x,y);
909
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 cairo_gl_surface_set_size(window->cairo_surface,
911 window->allocation.width,
912 window->allocation.height);
913}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000914#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400916struct display *
917window_get_display(struct window *window)
918{
919 return window->display;
920}
921
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400922void
923window_create_surface(struct window *window)
924{
925 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400926 uint32_t flags = 0;
927
928 if (!window->transparent)
929 flags = SURFACE_OPAQUE;
930
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500932#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100933 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
934 if (window->cairo_surface) {
935 window_resize_cairo_window_surface(window);
936 return;
937 }
938 surface = display_create_surface(window->display,
939 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400940 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100941 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500942 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400943 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400946 break;
947#endif
948 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400950 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800952 default:
953 surface = NULL;
954 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400955 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400956
957 window_set_surface(window, surface);
958 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400959}
960
961static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500962window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500963{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500964 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500965 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400966 cairo_surface_t *frame;
967 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500968
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400969 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400970
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 width = window->allocation.width;
972 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500973
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500974 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500975
Kristian Høgsberg09531622010-06-14 23:22:15 -0400976 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400977 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400978 cairo_paint(cr);
979
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400980 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400981 tile_mask(cr, window->display->shadow,
982 shadow_dx, shadow_dy, width, height,
983 window->margin + 10 - shadow_dx,
984 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500985
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400986 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400988 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400989 frame = window->display->inactive_frame;
990
991 tile_source(cr, frame, 0, 0, width, height,
992 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500993
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500994 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
995 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500996 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400997 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400998 if (window->keyboard_device)
999 cairo_set_source_rgb(cr, 0, 0, 0);
1000 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001001 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001002 cairo_show_text(cr, window->title);
1003
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001004 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001005}
1006
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001007void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001008window_destroy(struct window *window)
1009{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001010 struct display *display = window->display;
1011 struct input *input;
1012
1013 if (window->redraw_scheduled)
1014 wl_list_remove(&window->redraw_task.link);
1015
1016 wl_list_for_each(input, &display->input_list, link) {
1017 if (input->pointer_focus == window)
1018 input->pointer_focus = NULL;
1019 if (input->keyboard_focus == window)
1020 input->keyboard_focus = NULL;
1021 }
1022
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001023 if (window->shell_surface)
1024 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001025 wl_surface_destroy(window->surface);
1026 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001027
1028 if (window->cairo_surface != NULL)
1029 cairo_surface_destroy(window->cairo_surface);
1030 if (window->pending_surface != NULL)
1031 cairo_surface_destroy(window->pending_surface);
1032
1033 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001034 free(window);
1035}
1036
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001037static struct widget *
1038window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001040 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 wl_list_for_each(widget, &window->widget_list, link) {
1043 if (widget->allocation.x <= x &&
1044 x < widget->allocation.x + widget->allocation.width &&
1045 widget->allocation.y <= y &&
1046 y < widget->allocation.y + widget->allocation.height) {
1047 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048 }
1049 }
1050
1051 return NULL;
1052}
1053
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001054struct widget *
1055window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001057 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059 widget = malloc(sizeof *widget);
1060 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001061 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001062 widget->user_data = data;
1063 wl_list_insert(window->widget_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001065 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001066}
1067
1068void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 wl_list_for_each(widget, &window->widget_list, link)
1074 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075}
1076
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001077struct widget *
1078window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001079{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001080 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001081}
1082
1083void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
1089void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090widget_set_allocation(struct widget *widget,
1091 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001093 widget->allocation.x = x;
1094 widget->allocation.y = y;
1095 widget->allocation.width = width;
1096 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097}
1098
1099void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103}
1104
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001105void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001106widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001107{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001108 widget->enter_handler = handler;
1109}
1110
1111void
1112widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1113{
1114 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001115}
1116
1117void
1118widget_schedule_redraw(struct widget *widget)
1119{
1120 window_schedule_redraw(widget->window);
1121}
1122
1123void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001124window_draw(struct window *window)
1125{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001126 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001127 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001128 else
1129 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001130}
1131
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001132cairo_surface_t *
1133window_get_surface(struct window *window)
1134{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001135 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001136}
1137
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001138struct wl_surface *
1139window_get_wl_surface(struct window *window)
1140{
1141 return window->surface;
1142}
1143
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001144struct wl_shell_surface *
1145window_get_wl_shell_surface(struct window *window)
1146{
1147 return window->shell_surface;
1148}
1149
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001150static int
1151get_pointer_location(struct window *window, int32_t x, int32_t y)
1152{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001153 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 const int grip_size = 8;
1155
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001156 if (!window->decoration)
1157 return WINDOW_CLIENT_AREA;
1158
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001159 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001160 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161 else if (window->margin <= x && x < window->margin + grip_size)
1162 hlocation = WINDOW_RESIZING_LEFT;
1163 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001164 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001165 else if (x < window->allocation.width - window->margin)
1166 hlocation = WINDOW_RESIZING_RIGHT;
1167 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001168 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169
1170 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001171 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001172 else if (window->margin <= y && y < window->margin + grip_size)
1173 vlocation = WINDOW_RESIZING_TOP;
1174 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001175 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001176 else if (y < window->allocation.height - window->margin)
1177 vlocation = WINDOW_RESIZING_BOTTOM;
1178 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001179 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001180
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001181 location = vlocation | hlocation;
1182 if (location & WINDOW_EXTERIOR)
1183 location = WINDOW_EXTERIOR;
1184 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1185 location = WINDOW_TITLEBAR;
1186 else if (location == WINDOW_INTERIOR)
1187 location = WINDOW_CLIENT_AREA;
1188
1189 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001190}
1191
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001192static int
1193input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001194{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001196
1197 location = get_pointer_location(input->pointer_focus,
1198 input->sx, input->sy);
1199 switch (location) {
1200 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001201 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001202 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001203 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001204 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001205 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001206 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001207 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001208 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001209 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001210 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001211 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001212 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001213 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001214 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001215 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216 case WINDOW_EXTERIOR:
1217 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001218 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001219 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001220 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001222}
1223
1224void
1225input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1226{
1227 struct display *display = input->display;
1228 struct wl_buffer *buffer;
1229 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001230
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001231 if (pointer == input->current_pointer_image)
1232 return;
1233
1234 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001236
1237 if (!surface)
1238 return;
1239
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001240 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001241 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242 pointer_images[pointer].hotspot_x,
1243 pointer_images[pointer].hotspot_y);
1244}
1245
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001246static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001247window_set_focus_widget(struct window *window, struct widget *focus,
1248 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001249{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001250 struct widget *old;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001251
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001252 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001253 return;
1254
Kristian Høgsbergee143232012-01-09 08:42:24 -05001255 old = window->focus_widget;
1256 if (old) {
1257 if (old->leave_handler)
1258 old->leave_handler(old, input, old->user_data);
1259 window->focus_widget = NULL;
1260 }
1261
1262 if (focus) {
1263 if (focus->enter_handler)
1264 focus->enter_handler(focus, input, time,
1265 x, y, focus->user_data);
1266 window->focus_widget = focus;
1267 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001268}
1269
1270static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001271input_handle_motion(void *data, struct wl_input_device *input_device,
1272 uint32_t time,
1273 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001274{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001275 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001276 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001277 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001278 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001279
1280 input->x = x;
1281 input->y = y;
1282 input->sx = sx;
1283 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001285 if (!window->focus_widget || !window->widget_grab_button) {
1286 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001287 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001288 }
1289
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001290 if (window->motion_handler)
1291 pointer = (*window->motion_handler)(window, input, time,
1292 x, y, sx, sy,
1293 window->user_data);
1294
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001295 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001296 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001297}
1298
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001299static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001300window_menu_func(struct window *window, int index, void *data)
1301{
1302 switch (index) {
1303 case 0: /* close */
1304 if (window->close_handler)
1305 window->close_handler(window->parent,
1306 window->user_data);
1307 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001308 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001309 break;
1310 case 1: /* fullscreen */
1311 /* we don't have a way to get out of fullscreen for now */
1312 window_set_fullscreen(window, 1);
1313 break;
1314 case 2: /* rotate */
1315 case 3: /* scale */
1316 break;
1317 }
1318}
1319
1320
1321static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001322input_handle_button(void *data,
1323 struct wl_input_device *input_device,
1324 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001325{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001326 struct input *input = data;
1327 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001328 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001329 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001330 int32_t x, y;
1331 static const char *entries[] = {
1332 "Close", "Fullscreen", "Rotate", "Scale"
1333 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001334
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001335 if (window->focus_widget && window->widget_grab_button == 0 && state)
1336 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001337
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001338 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001339
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001340 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001341 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001342 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001343 if (!window->shell_surface)
1344 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001345 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001346 wl_shell_surface_move(window->shell_surface,
1347 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001348 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001349 case WINDOW_RESIZING_TOP:
1350 case WINDOW_RESIZING_BOTTOM:
1351 case WINDOW_RESIZING_LEFT:
1352 case WINDOW_RESIZING_RIGHT:
1353 case WINDOW_RESIZING_TOP_LEFT:
1354 case WINDOW_RESIZING_TOP_RIGHT:
1355 case WINDOW_RESIZING_BOTTOM_LEFT:
1356 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001357 if (!window->shell_surface)
1358 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001359 wl_shell_surface_resize(window->shell_surface,
1360 input_device, time,
1361 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001362 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001363 case WINDOW_CLIENT_AREA:
1364 if (window->button_handler)
1365 (*window->button_handler)(window,
1366 input, time,
1367 button, state,
1368 window->user_data);
1369 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001370 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001371 } else if (button == BTN_RIGHT && state == 1) {
1372 switch (location) {
1373 default:
1374 input_get_position(input, &x, &y);
1375 window->menu = window_create_menu(window->display,
1376 input, time,
1377 window,
1378 x - 10, y - 10,
1379 window_menu_func,
1380 entries, 4);
1381 window_schedule_redraw(window->menu);
1382 break;
1383 case WINDOW_CLIENT_AREA:
1384 if (window->button_handler)
1385 (*window->button_handler)(window,
1386 input, time,
1387 button, state,
1388 window->user_data);
1389 break;
1390 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001391 } else {
1392 if (window->button_handler)
1393 (*window->button_handler)(window,
1394 input, time,
1395 button, state,
1396 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001397 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001398
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001399 if (window->focus_widget &&
1400 window->widget_grab_button == button && !state) {
1401 window->widget_grab_button = 0;
1402 widget = window_find_widget(window, input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001403 window_set_focus_widget(window, widget, input, time,
1404 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001405 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001406}
1407
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001408static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001409input_handle_key(void *data, struct wl_input_device *input_device,
1410 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001411{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001412 struct input *input = data;
1413 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001414 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001415 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001416
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001417 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001418 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001419 return;
1420
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001421 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001422 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001423 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1424 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001425
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001426 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1427
1428 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001429 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001430 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001431 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001432
1433 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001434 (*window->key_handler)(window, input, time, key, sym, state,
1435 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001436}
1437
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001438static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001439input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001440{
1441 struct window *window = input->pointer_focus;
1442
1443 if (!window)
1444 return;
1445
Kristian Høgsbergee143232012-01-09 08:42:24 -05001446 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001447
1448 if (window->leave_handler)
1449 window->leave_handler(window, input, time, window->user_data);
1450 input->pointer_focus = NULL;
1451 input->current_pointer_image = POINTER_UNSET;
1452}
1453
1454static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001455input_handle_pointer_focus(void *data,
1456 struct wl_input_device *input_device,
1457 uint32_t time, struct wl_surface *surface,
1458 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001459{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001460 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001461 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001462 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001463 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001464
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001465 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001466 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001467 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001468
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001469 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001470 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001471 window = input->pointer_focus;
1472
Kristian Høgsberg59826582011-01-20 11:56:57 -05001473 input->x = x;
1474 input->y = y;
1475 input->sx = sx;
1476 input->sy = sy;
1477
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001478 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001479 if (window->enter_handler)
1480 pointer = window->enter_handler(window, input,
1481 time, sx, sy,
1482 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001483
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001484 widget = window_find_widget(window, x, y);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001485 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001486
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001487 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001488 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001489 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001490}
1491
1492static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001493input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001494{
1495 struct window *window = input->keyboard_focus;
1496
1497 if (!window)
1498 return;
1499
1500 window->keyboard_device = NULL;
1501 if (window->keyboard_focus_handler)
1502 (*window->keyboard_focus_handler)(window, NULL,
1503 window->user_data);
1504
1505 input->keyboard_focus = NULL;
1506}
1507
1508static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001509input_handle_keyboard_focus(void *data,
1510 struct wl_input_device *input_device,
1511 uint32_t time,
1512 struct wl_surface *surface,
1513 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001514{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001515 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001516 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001517 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001518 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001519
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001520 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001521
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001522 if (surface)
1523 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001524
1525 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001526 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001527 for (k = keys->data; k < end; k++)
1528 input->modifiers |= d->xkb->map->modmap[*k];
1529
1530 window = input->keyboard_focus;
1531 if (window) {
1532 window->keyboard_device = input;
1533 if (window->keyboard_focus_handler)
1534 (*window->keyboard_focus_handler)(window,
1535 window->keyboard_device,
1536 window->user_data);
1537 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001538}
1539
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001540static void
1541input_handle_touch_down(void *data,
1542 struct wl_input_device *wl_input_device,
1543 uint32_t time, struct wl_surface *surface,
1544 int32_t id, int32_t x, int32_t y)
1545{
1546}
1547
1548static void
1549input_handle_touch_up(void *data,
1550 struct wl_input_device *wl_input_device,
1551 uint32_t time, int32_t id)
1552{
1553}
1554
1555static void
1556input_handle_touch_motion(void *data,
1557 struct wl_input_device *wl_input_device,
1558 uint32_t time, int32_t id, int32_t x, int32_t y)
1559{
1560}
1561
1562static void
1563input_handle_touch_frame(void *data,
1564 struct wl_input_device *wl_input_device)
1565{
1566}
1567
1568static void
1569input_handle_touch_cancel(void *data,
1570 struct wl_input_device *wl_input_device)
1571{
1572}
1573
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001574static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001575 input_handle_motion,
1576 input_handle_button,
1577 input_handle_key,
1578 input_handle_pointer_focus,
1579 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001580 input_handle_touch_down,
1581 input_handle_touch_up,
1582 input_handle_touch_motion,
1583 input_handle_touch_frame,
1584 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001585};
1586
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001587void
1588input_get_position(struct input *input, int32_t *x, int32_t *y)
1589{
1590 *x = input->sx;
1591 *y = input->sy;
1592}
1593
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001594struct wl_input_device *
1595input_get_input_device(struct input *input)
1596{
1597 return input->input_device;
1598}
1599
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001600uint32_t
1601input_get_modifiers(struct input *input)
1602{
1603 return input->modifiers;
1604}
1605
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001606struct data_offer {
1607 struct wl_data_offer *offer;
1608 struct input *input;
1609 struct wl_array types;
1610 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001612 struct task io_task;
1613 int fd;
1614 data_func_t func;
1615 int32_t x, y;
1616 void *user_data;
1617};
1618
1619static void
1620data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1621{
1622 struct data_offer *offer = data;
1623 char **p;
1624
1625 p = wl_array_add(&offer->types, sizeof *p);
1626 *p = strdup(type);
1627}
1628
1629static const struct wl_data_offer_listener data_offer_listener = {
1630 data_offer_offer,
1631};
1632
1633static void
1634data_offer_destroy(struct data_offer *offer)
1635{
1636 char **p;
1637
1638 offer->refcount--;
1639 if (offer->refcount == 0) {
1640 wl_data_offer_destroy(offer->offer);
1641 for (p = offer->types.data; *p; p++)
1642 free(*p);
1643 wl_array_release(&offer->types);
1644 free(offer);
1645 }
1646}
1647
1648static void
1649data_device_data_offer(void *data,
1650 struct wl_data_device *data_device, uint32_t id)
1651{
1652 struct data_offer *offer;
1653
1654 offer = malloc(sizeof *offer);
1655
1656 wl_array_init(&offer->types);
1657 offer->refcount = 1;
1658 offer->input = data;
1659
1660 /* FIXME: Generate typesafe wrappers for this */
1661 offer->offer = (struct wl_data_offer *)
1662 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1663 id, &wl_data_offer_interface);
1664
1665 wl_data_offer_add_listener(offer->offer,
1666 &data_offer_listener, offer);
1667}
1668
1669static void
1670data_device_enter(void *data, struct wl_data_device *data_device,
1671 uint32_t time, struct wl_surface *surface,
1672 int32_t x, int32_t y, struct wl_data_offer *offer)
1673{
1674 struct input *input = data;
1675 struct window *window;
1676 char **p;
1677
1678 input->drag_offer = wl_data_offer_get_user_data(offer);
1679 window = wl_surface_get_user_data(surface);
1680 input->pointer_focus = window;
1681
1682 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1683 *p = NULL;
1684
1685 window = input->pointer_focus;
1686 if (window->data_handler)
1687 window->data_handler(window, input, time, x, y,
1688 input->drag_offer->types.data,
1689 window->user_data);
1690}
1691
1692static void
1693data_device_leave(void *data, struct wl_data_device *data_device)
1694{
1695 struct input *input = data;
1696
1697 data_offer_destroy(input->drag_offer);
1698 input->drag_offer = NULL;
1699}
1700
1701static void
1702data_device_motion(void *data, struct wl_data_device *data_device,
1703 uint32_t time, int32_t x, int32_t y)
1704{
1705 struct input *input = data;
1706 struct window *window = input->pointer_focus;
1707
1708 input->sx = x;
1709 input->sy = y;
1710
1711 if (window->data_handler)
1712 window->data_handler(window, input, time, x, y,
1713 input->drag_offer->types.data,
1714 window->user_data);
1715}
1716
1717static void
1718data_device_drop(void *data, struct wl_data_device *data_device)
1719{
1720 struct input *input = data;
1721 struct window *window = input->pointer_focus;
1722
1723 if (window->drop_handler)
1724 window->drop_handler(window, input,
1725 input->sx, input->sy, window->user_data);
1726}
1727
1728static void
1729data_device_selection(void *data,
1730 struct wl_data_device *wl_data_device,
1731 struct wl_data_offer *offer)
1732{
1733 struct input *input = data;
1734 char **p;
1735
1736 if (input->selection_offer)
1737 data_offer_destroy(input->selection_offer);
1738
1739 input->selection_offer = wl_data_offer_get_user_data(offer);
1740 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1741 *p = NULL;
1742}
1743
1744static const struct wl_data_device_listener data_device_listener = {
1745 data_device_data_offer,
1746 data_device_enter,
1747 data_device_leave,
1748 data_device_motion,
1749 data_device_drop,
1750 data_device_selection
1751};
1752
1753struct wl_data_device *
1754input_get_data_device(struct input *input)
1755{
1756 return input->data_device;
1757}
1758
1759void
1760input_set_selection(struct input *input,
1761 struct wl_data_source *source, uint32_t time)
1762{
1763 wl_data_device_set_selection(input->data_device, source, time);
1764}
1765
1766void
1767input_accept(struct input *input, uint32_t time, const char *type)
1768{
1769 wl_data_offer_accept(input->drag_offer->offer, time, type);
1770}
1771
1772static void
1773offer_io_func(struct task *task, uint32_t events)
1774{
1775 struct data_offer *offer =
1776 container_of(task, struct data_offer, io_task);
1777 unsigned int len;
1778 char buffer[4096];
1779
1780 len = read(offer->fd, buffer, sizeof buffer);
1781 offer->func(buffer, len,
1782 offer->x, offer->y, offer->user_data);
1783
1784 if (len == 0) {
1785 close(offer->fd);
1786 data_offer_destroy(offer);
1787 }
1788}
1789
1790static void
1791data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1792 data_func_t func, void *user_data)
1793{
1794 int p[2];
1795
1796 pipe2(p, O_CLOEXEC);
1797 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1798 close(p[1]);
1799
1800 offer->io_task.run = offer_io_func;
1801 offer->fd = p[0];
1802 offer->func = func;
1803 offer->refcount++;
1804 offer->user_data = user_data;
1805
1806 display_watch_fd(offer->input->display,
1807 offer->fd, EPOLLIN, &offer->io_task);
1808}
1809
1810void
1811input_receive_drag_data(struct input *input, const char *mime_type,
1812 data_func_t func, void *data)
1813{
1814 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1815 input->drag_offer->x = input->sx;
1816 input->drag_offer->y = input->sy;
1817}
1818
1819int
1820input_receive_selection_data(struct input *input, const char *mime_type,
1821 data_func_t func, void *data)
1822{
1823 char **p;
1824
1825 if (input->selection_offer == NULL)
1826 return -1;
1827
1828 for (p = input->selection_offer->types.data; *p; p++)
1829 if (strcmp(mime_type, *p) == 0)
1830 break;
1831
1832 if (*p == NULL)
1833 return -1;
1834
1835 data_offer_receive_data(input->selection_offer,
1836 mime_type, func, data);
1837 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001838}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001839
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001840int
1841input_receive_selection_data_to_fd(struct input *input,
1842 const char *mime_type, int fd)
1843{
1844 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1845
1846 return 0;
1847}
1848
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001849void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001850window_move(struct window *window, struct input *input, uint32_t time)
1851{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001852 if (!window->shell_surface)
1853 return;
1854
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001855 wl_shell_surface_move(window->shell_surface,
1856 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001857}
1858
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001859static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001860handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001861 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001862 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001863{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001864 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001865 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001866
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001867 /* FIXME: this is probably the wrong place to check for width
1868 * or height <= 0, but it prevents the compositor from crashing
1869 */
1870 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001871 return;
1872
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001873 window->resize_edges = edges;
1874
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001875 if (window->resize_handler) {
1876 child_width = width - 20 - window->margin * 2;
1877 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001878
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001879 (*window->resize_handler)(window,
1880 child_width, child_height,
1881 window->user_data);
1882 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001883 window->allocation.width = width;
1884 window->allocation.height = height;
1885
1886 if (window->redraw_handler)
1887 window_schedule_redraw(window);
1888 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001889}
1890
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001891static void
1892handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1893{
1894 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001895 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001896 /* FIXME: Need more context in this event, at least the input
1897 * device. Or just use wl_callback. */
1898
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001899 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001900 window_destroy(window);
1901}
1902
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001903static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001904 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001905 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001906};
1907
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001908void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001909window_get_allocation(struct window *window,
1910 struct rectangle *allocation)
1911{
1912 *allocation = window->allocation;
1913}
1914
1915void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001916window_get_child_allocation(struct window *window,
1917 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001918{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001919 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001920 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001921 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001922 allocation->x = window->margin + 10;
1923 allocation->y = window->margin + 50;
1924 allocation->width =
1925 window->allocation.width - 20 - window->margin * 2;
1926 allocation->height =
1927 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001928 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001929}
1930
1931void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001932window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001933{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001934 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001935 window->allocation.x = 20 + window->margin;
1936 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001937 window->allocation.width = width + 20 + window->margin * 2;
1938 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001939 } else {
1940 window->allocation.x = 0;
1941 window->allocation.y = 0;
1942 window->allocation.width = width;
1943 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001944 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001945}
1946
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001947static void
1948idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001949{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001950 struct window *window =
1951 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001952
1953 window->redraw_handler(window, window->user_data);
1954 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001955}
1956
1957void
1958window_schedule_redraw(struct window *window)
1959{
1960 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001961 window->redraw_task.run = idle_redraw;
1962 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001963 window->redraw_scheduled = 1;
1964 }
1965}
1966
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001967void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001968window_set_custom(struct window *window)
1969{
1970 window->type = TYPE_CUSTOM;
1971}
1972
1973void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001974window_set_fullscreen(struct window *window, int fullscreen)
1975{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001976 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001977 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001978
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001979 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001980 return;
1981
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001982 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001983 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001984 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001985 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001986 width = output->allocation.width;
1987 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001988 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001989 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001990 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001991 width = window->saved_allocation.width - 20 - window->margin * 2;
1992 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001993 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001994 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001995
1996 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001997}
1998
1999void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002000window_set_decoration(struct window *window, int decoration)
2001{
2002 window->decoration = decoration;
2003}
2004
2005void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002006window_set_user_data(struct window *window, void *data)
2007{
2008 window->user_data = data;
2009}
2010
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002011void *
2012window_get_user_data(struct window *window)
2013{
2014 return window->user_data;
2015}
2016
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002017void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002018window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002019 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002020{
2021 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002022}
2023
2024void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002025window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002026 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002027{
2028 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002029}
2030
2031void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002032window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002033 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002034{
2035 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002036}
2037
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002038void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002039window_set_button_handler(struct window *window,
2040 window_button_handler_t handler)
2041{
2042 window->button_handler = handler;
2043}
2044
2045void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002046window_set_motion_handler(struct window *window,
2047 window_motion_handler_t handler)
2048{
2049 window->motion_handler = handler;
2050}
2051
2052void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04002053window_set_enter_handler(struct window *window,
2054 window_enter_handler_t handler)
2055{
2056 window->enter_handler = handler;
2057}
2058
2059void
2060window_set_leave_handler(struct window *window,
2061 window_leave_handler_t handler)
2062{
2063 window->leave_handler = handler;
2064}
2065
2066void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002067window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002068 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002069{
2070 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002071}
2072
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002073void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002074window_set_data_handler(struct window *window, window_data_handler_t handler)
2075{
2076 window->data_handler = handler;
2077}
2078
2079void
2080window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2081{
2082 window->drop_handler = handler;
2083}
2084
2085void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002086window_set_close_handler(struct window *window,
2087 window_close_handler_t handler)
2088{
2089 window->close_handler = handler;
2090}
2091
2092void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002093window_set_transparent(struct window *window, int transparent)
2094{
2095 window->transparent = transparent;
2096}
2097
2098void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002099window_set_title(struct window *window, const char *title)
2100{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002101 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002102 window->title = strdup(title);
2103}
2104
2105const char *
2106window_get_title(struct window *window)
2107{
2108 return window->title;
2109}
2110
2111void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002112display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2113 int32_t x, int32_t y, int32_t width, int32_t height)
2114{
2115 struct wl_buffer *buffer;
2116
2117 buffer = display_get_buffer_for_surface(display, cairo_surface);
2118
2119 wl_buffer_damage(buffer, x, y, width, height);
2120}
2121
2122void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002123window_damage(struct window *window, int32_t x, int32_t y,
2124 int32_t width, int32_t height)
2125{
2126 wl_surface_damage(window->surface, x, y, width, height);
2127}
2128
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002129static struct window *
2130window_create_internal(struct display *display, struct window *parent,
2131 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002132{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002133 struct window *window;
2134
2135 window = malloc(sizeof *window);
2136 if (window == NULL)
2137 return NULL;
2138
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002139 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002140 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002141 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002142 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002143 if (display->shell) {
2144 window->shell_surface =
2145 wl_shell_get_shell_surface(display->shell,
2146 window->surface);
2147 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002148 window->allocation.x = 0;
2149 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002150 window->allocation.width = width;
2151 window->allocation.height = height;
2152 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002153 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002154 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002155 window->transparent = 1;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002156 wl_list_init(&window->widget_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002157
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002158 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002159#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002160 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2161 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002162#else
2163 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2164#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002165 else
2166 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002167
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002168 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002169 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002170
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002171 if (window->shell_surface) {
2172 wl_shell_surface_set_user_data(window->shell_surface, window);
2173 wl_shell_surface_add_listener(window->shell_surface,
2174 &shell_surface_listener, window);
2175 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002176
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002177 return window;
2178}
2179
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002180struct window *
2181window_create(struct display *display, int32_t width, int32_t height)
2182{
2183 struct window *window;
2184
2185 window = window_create_internal(display, NULL, width, height);
2186 if (!window)
2187 return NULL;
2188
2189 return window;
2190}
2191
2192struct window *
2193window_create_transient(struct display *display, struct window *parent,
2194 int32_t x, int32_t y, int32_t width, int32_t height)
2195{
2196 struct window *window;
2197
2198 window = window_create_internal(parent->display,
2199 parent, width, height);
2200 if (!window)
2201 return NULL;
2202
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002203 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002204 window->x = x;
2205 window->y = y;
2206
2207 return window;
2208}
2209
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002210static int
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002211menu_set_widget(struct window *window, struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002212{
2213 int next;
2214
2215 next = (sy - 8) / 20;
2216 if (menu->current != next) {
2217 menu->current = next;
2218 window_schedule_redraw(window);
2219 }
2220
2221 return POINTER_LEFT_PTR;
2222}
2223
2224static int
2225menu_motion_handler(struct window *window,
2226 struct input *input, uint32_t time,
2227 int32_t x, int32_t y,
2228 int32_t sx, int32_t sy, void *data)
2229{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002230 return menu_set_widget(window, data, sy);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002231}
2232
2233static int
2234menu_enter_handler(struct window *window,
2235 struct input *input, uint32_t time,
2236 int32_t x, int32_t y, void *data)
2237{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002238 return menu_set_widget(window, data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002239}
2240
2241static void
2242menu_leave_handler(struct window *window,
2243 struct input *input, uint32_t time, void *data)
2244{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002245 menu_set_widget(window, data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002246}
2247
2248static void
2249menu_button_handler(struct window *window,
2250 struct input *input, uint32_t time,
2251 int button, int state, void *data)
2252
2253{
2254 struct menu *menu = data;
2255
2256 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002257 if (state == 0 && time - menu->time > 500) {
2258 menu->func(window->parent,
2259 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002260 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002261 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002262}
2263
2264static void
2265menu_redraw_handler(struct window *window, void *data)
2266{
2267 cairo_t *cr;
2268 const int32_t r = 3, margin = 3;
2269 struct menu *menu = data;
2270 int32_t width, height, i;
2271
2272 width = 200;
2273 height = menu->count * 20 + margin * 2;
2274 window_set_child_size(window, width, height);
2275 window_create_surface(window);
2276
2277 cr = cairo_create(window->cairo_surface);
2278 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2279 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2280 cairo_paint(cr);
2281
2282 width = window->allocation.width;
2283 height = window->allocation.height;
2284 rounded_rect(cr, 0, 0, width, height, r);
2285 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2286 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2287 cairo_fill(cr);
2288
2289 for (i = 0; i < menu->count; i++) {
2290 if (i == menu->current) {
2291 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2292 cairo_rectangle(cr, margin, i * 20 + margin,
2293 width - 2 * margin, 20);
2294 cairo_fill(cr);
2295 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2296 cairo_move_to(cr, 10, i * 20 + 16);
2297 cairo_show_text(cr, menu->entries[i]);
2298 } else {
2299 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2300 cairo_move_to(cr, 10, i * 20 + 16);
2301 cairo_show_text(cr, menu->entries[i]);
2302 }
2303 }
2304
2305 cairo_destroy(cr);
2306 window_flush(window);
2307}
2308
2309struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002310window_create_menu(struct display *display,
2311 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002312 int32_t x, int32_t y,
2313 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002314{
2315 struct window *window;
2316 struct menu *menu;
2317
2318 menu = malloc(sizeof *menu);
2319 if (!menu)
2320 return NULL;
2321
2322 window = window_create_internal(parent->display, parent, 0, 0);
2323 if (!window)
2324 return NULL;
2325
2326 menu->window = window;
2327 menu->entries = entries;
2328 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002329 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002330 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331 window->decoration = 0;
2332 window->type = TYPE_MENU;
2333 window->x = x;
2334 window->y = y;
2335
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002336 wl_shell_surface_set_popup(window->shell_surface,
2337 input->input_device, time,
2338 window->parent->shell_surface,
2339 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002340
2341 window_set_motion_handler(window, menu_motion_handler);
2342 window_set_enter_handler(window, menu_enter_handler);
2343 window_set_leave_handler(window, menu_leave_handler);
2344 window_set_button_handler(window, menu_button_handler);
2345 window_set_redraw_handler(window, menu_redraw_handler);
2346 window_set_user_data(window, menu);
2347
2348 return window;
2349}
2350
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002351void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002352window_set_buffer_type(struct window *window, enum window_buffer_type type)
2353{
2354 window->buffer_type = type;
2355}
2356
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002357
2358static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002359display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002360 struct wl_output *wl_output,
2361 int x, int y,
2362 int physical_width,
2363 int physical_height,
2364 int subpixel,
2365 const char *make,
2366 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002367{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002368 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002369
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002370 output->allocation.x = x;
2371 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002372}
2373
2374static void
2375display_handle_mode(void *data,
2376 struct wl_output *wl_output,
2377 uint32_t flags,
2378 int width,
2379 int height,
2380 int refresh)
2381{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002382 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002383 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002384
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002385 if (flags & WL_OUTPUT_MODE_CURRENT) {
2386 output->allocation.width = width;
2387 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002388 if (display->output_configure_handler)
2389 (*display->output_configure_handler)(
2390 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002391 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002392}
2393
2394static const struct wl_output_listener output_listener = {
2395 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002396 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002397};
2398
2399static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002400display_add_output(struct display *d, uint32_t id)
2401{
2402 struct output *output;
2403
2404 output = malloc(sizeof *output);
2405 if (output == NULL)
2406 return;
2407
2408 memset(output, 0, sizeof *output);
2409 output->display = d;
2410 output->output =
2411 wl_display_bind(d->display, id, &wl_output_interface);
2412 wl_list_insert(d->output_list.prev, &output->link);
2413
2414 wl_output_add_listener(output->output, &output_listener, output);
2415}
2416
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002417static void
2418output_destroy(struct output *output)
2419{
2420 if (output->destroy_handler)
2421 (*output->destroy_handler)(output, output->user_data);
2422
2423 wl_output_destroy(output->output);
2424 wl_list_remove(&output->link);
2425 free(output);
2426}
2427
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002428void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002429display_set_output_configure_handler(struct display *display,
2430 display_output_handler_t handler)
2431{
2432 struct output *output;
2433
2434 display->output_configure_handler = handler;
2435 if (!handler)
2436 return;
2437
2438 wl_list_for_each(output, &display->output_list, link)
2439 (*display->output_configure_handler)(output,
2440 display->user_data);
2441}
2442
2443void
2444output_set_user_data(struct output *output, void *data)
2445{
2446 output->user_data = data;
2447}
2448
2449void *
2450output_get_user_data(struct output *output)
2451{
2452 return output->user_data;
2453}
2454
2455void
2456output_set_destroy_handler(struct output *output,
2457 display_output_handler_t handler)
2458{
2459 output->destroy_handler = handler;
2460 /* FIXME: implement this, once we have way to remove outputs */
2461}
2462
2463void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002464output_get_allocation(struct output *output, struct rectangle *allocation)
2465{
2466 *allocation = output->allocation;
2467}
2468
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002469struct wl_output *
2470output_get_wl_output(struct output *output)
2471{
2472 return output->output;
2473}
2474
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002475static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002476display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002477{
2478 struct input *input;
2479
2480 input = malloc(sizeof *input);
2481 if (input == NULL)
2482 return;
2483
2484 memset(input, 0, sizeof *input);
2485 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002486 input->input_device =
2487 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002488 input->pointer_focus = NULL;
2489 input->keyboard_focus = NULL;
2490 wl_list_insert(d->input_list.prev, &input->link);
2491
2492 wl_input_device_add_listener(input->input_device,
2493 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002494 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002495
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002496 input->data_device =
2497 wl_data_device_manager_get_data_device(d->data_device_manager,
2498 input->input_device);
2499 wl_data_device_add_listener(input->data_device,
2500 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002501}
2502
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002503static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002504input_destroy(struct input *input)
2505{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002506 input_remove_keyboard_focus(input);
2507 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002508
2509 if (input->drag_offer)
2510 data_offer_destroy(input->drag_offer);
2511
2512 if (input->selection_offer)
2513 data_offer_destroy(input->selection_offer);
2514
2515 wl_data_device_destroy(input->data_device);
2516 wl_list_remove(&input->link);
2517 wl_input_device_destroy(input->input_device);
2518 free(input);
2519}
2520
2521static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002522display_handle_global(struct wl_display *display, uint32_t id,
2523 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002524{
2525 struct display *d = data;
2526
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002527 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002528 d->compositor =
2529 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002530 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002531 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002532 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002533 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002534 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002535 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002536 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002537 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002538 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2539 d->data_device_manager =
2540 wl_display_bind(display, id,
2541 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002542 }
2543}
2544
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002545static void
2546display_render_frame(struct display *d)
2547{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002548 int radius = 8;
2549 cairo_t *cr;
2550
2551 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2552 cr = cairo_create(d->shadow);
2553 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2554 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2555 rounded_rect(cr, 16, 16, 112, 112, radius);
2556 cairo_fill(cr);
2557 cairo_destroy(cr);
2558 blur_surface(d->shadow, 64);
2559
2560 d->active_frame =
2561 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2562 cr = cairo_create(d->active_frame);
2563 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2564 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2565 rounded_rect(cr, 16, 16, 112, 112, radius);
2566 cairo_fill(cr);
2567 cairo_destroy(cr);
2568
2569 d->inactive_frame =
2570 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2571 cr = cairo_create(d->inactive_frame);
2572 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2573 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2574 rounded_rect(cr, 16, 16, 112, 112, radius);
2575 cairo_fill(cr);
2576 cairo_destroy(cr);
2577}
2578
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002579static void
2580init_xkb(struct display *d)
2581{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002582 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002583
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002584 names.rules = "evdev";
2585 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002586 names.layout = option_xkb_layout;
2587 names.variant = option_xkb_variant;
2588 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002589
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002590 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002591 if (!d->xkb) {
2592 fprintf(stderr, "Failed to compile keymap\n");
2593 exit(1);
2594 }
2595}
2596
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002597static void
2598fini_xkb(struct display *display)
2599{
2600 xkb_free_keymap(display->xkb);
2601}
2602
Yuval Fledel45568f62010-12-06 09:18:12 -05002603static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002604init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002605{
2606 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002607 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002608
2609 static const EGLint premul_argb_cfg_attribs[] = {
2610 EGL_SURFACE_TYPE,
2611 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2612 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002613 EGL_RED_SIZE, 1,
2614 EGL_GREEN_SIZE, 1,
2615 EGL_BLUE_SIZE, 1,
2616 EGL_ALPHA_SIZE, 1,
2617 EGL_DEPTH_SIZE, 1,
2618 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2619 EGL_NONE
2620 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002621
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002622 static const EGLint rgb_cfg_attribs[] = {
2623 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2624 EGL_RED_SIZE, 1,
2625 EGL_GREEN_SIZE, 1,
2626 EGL_BLUE_SIZE, 1,
2627 EGL_ALPHA_SIZE, 0,
2628 EGL_DEPTH_SIZE, 1,
2629 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2630 EGL_NONE
2631 };
2632
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002633 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002634 if (!eglInitialize(d->dpy, &major, &minor)) {
2635 fprintf(stderr, "failed to initialize display\n");
2636 return -1;
2637 }
2638
2639 if (!eglBindAPI(EGL_OPENGL_API)) {
2640 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2641 return -1;
2642 }
2643
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002644 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002645 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002646 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002647 return -1;
2648 }
2649
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002650 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2651 &d->rgb_config, 1, &n) || n != 1) {
2652 fprintf(stderr, "failed to choose rgb config\n");
2653 return -1;
2654 }
2655
Benjamin Franzke0c991632011-09-27 21:57:31 +02002656 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2657 if (d->rgb_ctx == NULL) {
2658 fprintf(stderr, "failed to create context\n");
2659 return -1;
2660 }
2661 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2662 EGL_NO_CONTEXT, NULL);
2663 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002664 fprintf(stderr, "failed to create context\n");
2665 return -1;
2666 }
2667
Benjamin Franzke0c991632011-09-27 21:57:31 +02002668 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002669 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002670 return -1;
2671 }
2672
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002673#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002674 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2675 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002676 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002677 return -1;
2678 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002679 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2680 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2681 fprintf(stderr, "failed to get cairo egl argb device\n");
2682 return -1;
2683 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002684#endif
2685
2686 return 0;
2687}
2688
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002689static void
2690fini_egl(struct display *display)
2691{
2692#ifdef HAVE_CAIRO_EGL
2693 cairo_device_destroy(display->argb_device);
2694 cairo_device_destroy(display->rgb_device);
2695#endif
2696
2697 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2698 EGL_NO_CONTEXT);
2699
2700 eglTerminate(display->dpy);
2701 eglReleaseThread();
2702}
2703
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002704static int
2705event_mask_update(uint32_t mask, void *data)
2706{
2707 struct display *d = data;
2708
2709 d->mask = mask;
2710
2711 return 0;
2712}
2713
2714static void
2715handle_display_data(struct task *task, uint32_t events)
2716{
2717 struct display *display =
2718 container_of(task, struct display, display_task);
2719
2720 wl_display_iterate(display->display, display->mask);
2721}
2722
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002723struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002724display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002725{
2726 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002727 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002728 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002729 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002730
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002731 g_type_init();
2732
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002733 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002734 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002735 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002736
2737 xkb_option_group = g_option_group_new("xkb",
2738 "Keyboard options",
2739 "Show all XKB options",
2740 NULL, NULL);
2741 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2742 g_option_context_add_group (context, xkb_option_group);
2743
2744 if (!g_option_context_parse(context, argc, argv, &error)) {
2745 fprintf(stderr, "option parsing failed: %s\n", error->message);
2746 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002747 }
2748
Tim Wiederhake748f6722011-01-23 23:25:25 +01002749 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002750
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002751 d = malloc(sizeof *d);
2752 if (d == NULL)
2753 return NULL;
2754
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002755 memset(d, 0, sizeof *d);
2756
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002757 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002758 if (d->display == NULL) {
2759 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002760 return NULL;
2761 }
2762
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002763 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2764 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2765 d->display_task.run = handle_display_data;
2766 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2767
2768 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002769 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002770 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002771
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002772 /* Set up listener so we'll catch all events. */
2773 wl_display_add_global_listener(d->display,
2774 display_handle_global, d);
2775
2776 /* Process connection events. */
2777 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002778 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002779 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002780
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002781 d->image_target_texture_2d =
2782 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2783 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2784 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2785
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002786 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002787
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002788 display_render_frame(d);
2789
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002790 wl_list_init(&d->window_list);
2791
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002792 init_xkb(d);
2793
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002794 return d;
2795}
2796
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002797static void
2798display_destroy_outputs(struct display *display)
2799{
2800 struct output *tmp;
2801 struct output *output;
2802
2803 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2804 output_destroy(output);
2805}
2806
Pekka Paalanene1207c72011-12-16 12:02:09 +02002807static void
2808display_destroy_inputs(struct display *display)
2809{
2810 struct input *tmp;
2811 struct input *input;
2812
2813 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2814 input_destroy(input);
2815}
2816
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002817void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002818display_destroy(struct display *display)
2819{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002820 if (!wl_list_empty(&display->window_list))
2821 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2822
2823 if (!wl_list_empty(&display->deferred_list))
2824 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2825
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002826 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002827 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002828
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002829 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002830
2831 cairo_surface_destroy(display->active_frame);
2832 cairo_surface_destroy(display->inactive_frame);
2833 cairo_surface_destroy(display->shadow);
2834 destroy_pointer_surfaces(display);
2835
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002836 fini_egl(display);
2837
Pekka Paalanenc2052982011-12-16 11:41:32 +02002838 if (display->shell)
2839 wl_shell_destroy(display->shell);
2840
2841 if (display->shm)
2842 wl_shm_destroy(display->shm);
2843
2844 if (display->data_device_manager)
2845 wl_data_device_manager_destroy(display->data_device_manager);
2846
2847 wl_compositor_destroy(display->compositor);
2848
2849 close(display->epoll_fd);
2850
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002851 wl_display_flush(display->display);
2852 wl_display_destroy(display->display);
2853 free(display);
2854}
2855
2856void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002857display_set_user_data(struct display *display, void *data)
2858{
2859 display->user_data = data;
2860}
2861
2862void *
2863display_get_user_data(struct display *display)
2864{
2865 return display->user_data;
2866}
2867
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002868struct wl_display *
2869display_get_display(struct display *display)
2870{
2871 return display->display;
2872}
2873
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002874struct output *
2875display_get_output(struct display *display)
2876{
2877 return container_of(display->output_list.next, struct output, link);
2878}
2879
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002880struct wl_compositor *
2881display_get_compositor(struct display *display)
2882{
2883 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002884}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002885
2886EGLDisplay
2887display_get_egl_display(struct display *d)
2888{
2889 return d->dpy;
2890}
2891
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002892struct wl_data_source *
2893display_create_data_source(struct display *display)
2894{
2895 return wl_data_device_manager_create_data_source(display->data_device_manager);
2896}
2897
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002898EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002899display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002900{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002901 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002902}
2903
Benjamin Franzke0c991632011-09-27 21:57:31 +02002904EGLConfig
2905display_get_argb_egl_config(struct display *d)
2906{
2907 return d->premultiplied_argb_config;
2908}
2909
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002910struct wl_shell *
2911display_get_shell(struct display *display)
2912{
2913 return display->shell;
2914}
2915
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002916int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002917display_acquire_window_surface(struct display *display,
2918 struct window *window,
2919 EGLContext ctx)
2920{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002921#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002922 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002923 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002924
2925 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002926 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002927 device = cairo_surface_get_device(window->cairo_surface);
2928 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002929 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002930
Benjamin Franzke0c991632011-09-27 21:57:31 +02002931 if (!ctx) {
2932 if (device == display->rgb_device)
2933 ctx = display->rgb_ctx;
2934 else if (device == display->argb_device)
2935 ctx = display->argb_ctx;
2936 else
2937 assert(0);
2938 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002939
2940 data = cairo_surface_get_user_data(window->cairo_surface,
2941 &surface_data_key);
2942
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002943 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002944 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002945 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2946 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002947
2948 return 0;
2949#else
2950 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002951#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002952}
2953
2954void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002955display_release_window_surface(struct display *display,
2956 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002957{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002958#ifdef HAVE_CAIRO_EGL
2959 cairo_device_t *device;
2960
2961 device = cairo_surface_get_device(window->cairo_surface);
2962 if (!device)
2963 return;
2964
2965 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002966 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002967 cairo_device_release(device);
2968#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002969}
2970
2971void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002972display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002973{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002974 wl_list_insert(&display->deferred_list, &task->link);
2975}
2976
2977void
2978display_watch_fd(struct display *display,
2979 int fd, uint32_t events, struct task *task)
2980{
2981 struct epoll_event ep;
2982
2983 ep.events = events;
2984 ep.data.ptr = task;
2985 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2986}
2987
2988void
2989display_run(struct display *display)
2990{
2991 struct task *task;
2992 struct epoll_event ep[16];
2993 int i, count;
2994
Pekka Paalanen826d7952011-12-15 10:14:07 +02002995 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002996 while (1) {
2997 while (display->mask & WL_DISPLAY_WRITABLE)
2998 wl_display_iterate(display->display,
2999 WL_DISPLAY_WRITABLE);
3000
Pekka Paalanen826d7952011-12-15 10:14:07 +02003001 if (!display->running)
3002 break;
3003
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003004 count = epoll_wait(display->epoll_fd,
3005 ep, ARRAY_LENGTH(ep), -1);
3006 for (i = 0; i < count; i++) {
3007 task = ep[i].data.ptr;
3008 task->run(task, ep[i].events);
3009 }
3010
3011 while (!wl_list_empty(&display->deferred_list)) {
3012 task = container_of(display->deferred_list.next,
3013 struct task, link);
3014 wl_list_remove(&task->link);
3015 task->run(task, 0);
3016 }
3017 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003018}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003019
3020void
3021display_exit(struct display *display)
3022{
3023 display->running = 0;
3024}