blob: 6f1c65b4bf56679277c325a2c369eeac584bb0d1 [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øgsberg9a13dab2012-01-08 15:18:19 -0500155 widget_focus_handler_t focus_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 void *user_data;
157};
158
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400159struct input {
160 struct display *display;
161 struct wl_input_device *input_device;
162 struct window *pointer_focus;
163 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400164 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165 uint32_t modifiers;
166 int32_t x, y, sx, sy;
167 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168
169 struct wl_data_device *data_device;
170 struct data_offer *drag_offer;
171 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172};
173
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500174struct output {
175 struct display *display;
176 struct wl_output *output;
177 struct rectangle allocation;
178 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200179
180 display_output_handler_t destroy_handler;
181 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500182};
183
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500184struct menu {
185 struct window *window;
186 const char **entries;
187 uint32_t time;
188 int current;
189 int count;
190 menu_func_t func;
191};
192
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400193enum {
194 POINTER_DEFAULT = 100,
195 POINTER_UNSET
196};
197
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500198enum window_location {
199 WINDOW_INTERIOR = 0,
200 WINDOW_RESIZING_TOP = 1,
201 WINDOW_RESIZING_BOTTOM = 2,
202 WINDOW_RESIZING_LEFT = 4,
203 WINDOW_RESIZING_TOP_LEFT = 5,
204 WINDOW_RESIZING_BOTTOM_LEFT = 6,
205 WINDOW_RESIZING_RIGHT = 8,
206 WINDOW_RESIZING_TOP_RIGHT = 9,
207 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
208 WINDOW_RESIZING_MASK = 15,
209 WINDOW_EXTERIOR = 16,
210 WINDOW_TITLEBAR = 17,
211 WINDOW_CLIENT_AREA = 18,
212};
213
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400214const char *option_xkb_layout = "us";
215const char *option_xkb_variant = "";
216const char *option_xkb_options = "";
217
218static const GOptionEntry xkb_option_entries[] = {
219 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
220 &option_xkb_layout, "XKB Layout" },
221 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
222 &option_xkb_variant, "XKB Variant" },
223 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
224 &option_xkb_options, "XKB Options" },
225 { NULL }
226};
227
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400228static const cairo_user_data_key_t surface_data_key;
229struct surface_data {
230 struct wl_buffer *buffer;
231};
232
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500233#define MULT(_d,c,a,t) \
234 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
235
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500236#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400237
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100238struct egl_window_surface_data {
239 struct display *display;
240 struct wl_surface *surface;
241 struct wl_egl_window *window;
242 EGLSurface surf;
243};
244
245static void
246egl_window_surface_data_destroy(void *p)
247{
248 struct egl_window_surface_data *data = p;
249 struct display *d = data->display;
250
251 eglDestroySurface(d->dpy, data->surf);
252 wl_egl_window_destroy(data->window);
253 data->surface = NULL;
254
255 free(p);
256}
257
258static cairo_surface_t *
259display_create_egl_window_surface(struct display *display,
260 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400261 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100262 struct rectangle *rectangle)
263{
264 cairo_surface_t *cairo_surface;
265 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400266 EGLConfig config;
267 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200268 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100269
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400270 static const EGLint premul_attribs[] = {
271 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
272 EGL_NONE
273 };
274
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275 data = malloc(sizeof *data);
276 if (data == NULL)
277 return NULL;
278
279 data->display = display;
280 data->surface = surface;
281
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400282 if (flags & SURFACE_OPAQUE) {
283 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200284 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200285 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 } else {
287 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 attribs = premul_attribs;
290 }
291
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400292 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100293 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 data->surf = eglCreateWindowSurface(display->dpy, config,
297 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Benjamin Franzke0c991632011-09-27 21:57:31 +0200299 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300 data->surf,
301 rectangle->width,
302 rectangle->height);
303
304 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
305 data, egl_window_surface_data_destroy);
306
307 return cairo_surface;
308}
309
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200312 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 EGLImageKHR image;
314 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800315 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317};
318
319static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324
Benjamin Franzke0c991632011-09-27 21:57:31 +0200325 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800328
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500329 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500331 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500332 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333}
334
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500335EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336display_get_image_for_egl_image_surface(struct display *display,
337 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340
341 data = cairo_surface_get_user_data (surface, &surface_data_key);
342
343 return data->image;
344}
345
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500346static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400348 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 EGLDisplay dpy = display->dpy;
353 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200354 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355
356 data = malloc(sizeof *data);
357 if (data == NULL)
358 return NULL;
359
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800360 data->display = display;
361
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400362 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500364 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000365 free(data);
366 return NULL;
367 }
368
Benjamin Franzke0c991632011-09-27 21:57:31 +0200369 if (flags & SURFACE_OPAQUE) {
370 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200371 content = CAIRO_CONTENT_COLOR;
372 } else {
373 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 content = CAIRO_CONTENT_COLOR_ALPHA;
375 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500377 data->image = display->create_image(dpy, NULL,
378 EGL_NATIVE_PIXMAP_KHR,
379 (EGLClientBuffer) data->pixmap,
380 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500382 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 free(data);
384 return NULL;
385 }
386
387 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400388 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500389
Benjamin Franzke0c991632011-09-27 21:57:31 +0200390 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500393 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 surface = cairo_gl_surface_create_for_texture(data->device,
397 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 data->texture,
399 rectangle->width,
400 rectangle->height);
401
402 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500403 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404
405 return surface;
406}
407
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500408static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500409display_create_egl_image_surface_from_file(struct display *display,
410 const char *filename,
411 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412{
413 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400414 GdkPixbuf *pixbuf;
415 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400416 int stride, i;
417 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500418 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
420 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400422 FALSE, &error);
423 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400426 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
427 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500428 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400430 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432
433 stride = gdk_pixbuf_get_rowstride(pixbuf);
434 pixels = gdk_pixbuf_get_pixels(pixbuf);
435
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400437 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 while (p < end) {
440 unsigned int t;
441
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400442 MULT(p[0], p[0], p[3], t);
443 MULT(p[1], p[1], p[3], t);
444 MULT(p[2], p[2], p[3], t);
445 p += 4;
446
447 }
448 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400449
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200450 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000451 if (surface == NULL) {
452 g_object_unref(pixbuf);
453 return NULL;
454 }
455
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800456 data = cairo_surface_get_user_data(surface, &surface_data_key);
457
Benjamin Franzke0c991632011-09-27 21:57:31 +0200458 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800459 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800460 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
461 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200462 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400463
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500464 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466 return surface;
467}
468
469#endif
470
471struct wl_buffer *
472display_get_buffer_for_surface(struct display *display,
473 cairo_surface_t *surface)
474{
475 struct surface_data *data;
476
477 data = cairo_surface_get_user_data (surface, &surface_data_key);
478
479 return data->buffer;
480}
481
482struct shm_surface_data {
483 struct surface_data data;
484 void *map;
485 size_t length;
486};
487
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500488static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489shm_surface_data_destroy(void *p)
490{
491 struct shm_surface_data *data = p;
492
493 wl_buffer_destroy(data->data.buffer);
494 munmap(data->map, data->length);
495}
496
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500497static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400499 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500{
501 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400502 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400503 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800504 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505 char filename[] = "/tmp/wayland-shm-XXXXXX";
506
507 data = malloc(sizeof *data);
508 if (data == NULL)
509 return NULL;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rectangle->width);
513 data->length = stride * rectangle->height;
514 fd = mkstemp(filename);
515 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000516 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517 return NULL;
518 }
519 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000520 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521 close(fd);
522 return NULL;
523 }
524
525 data->map = mmap(NULL, data->length,
526 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
527 unlink(filename);
528
529 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000530 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531 close(fd);
532 return NULL;
533 }
534
535 surface = cairo_image_surface_create_for_data (data->map,
536 CAIRO_FORMAT_ARGB32,
537 rectangle->width,
538 rectangle->height,
539 stride);
540
541 cairo_surface_set_user_data (surface, &surface_data_key,
542 data, shm_surface_data_destroy);
543
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400545 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400547 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 data->data.buffer = wl_shm_create_buffer(display->shm,
550 fd,
551 rectangle->width,
552 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400553 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554
555 close(fd);
556
557 return surface;
558}
559
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500560static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561display_create_shm_surface_from_file(struct display *display,
562 const char *filename,
563 struct rectangle *rect)
564{
565 cairo_surface_t *surface;
566 GdkPixbuf *pixbuf;
567 GError *error = NULL;
568 int stride, i;
569 unsigned char *pixels, *p, *end, *dest_data;
570 int dest_stride;
571 uint32_t *d;
572
573 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
574 rect->width, rect->height,
575 FALSE, &error);
576 if (error != NULL)
577 return NULL;
578
579 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
580 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500581 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582 return NULL;
583 }
584
585 stride = gdk_pixbuf_get_rowstride(pixbuf);
586 pixels = gdk_pixbuf_get_pixels(pixbuf);
587
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400588 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000589 if (surface == NULL) {
590 g_object_unref(pixbuf);
591 return NULL;
592 }
593
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400594 dest_data = cairo_image_surface_get_data (surface);
595 dest_stride = cairo_image_surface_get_stride (surface);
596
597 for (i = 0; i < rect->height; i++) {
598 d = (uint32_t *) (dest_data + i * dest_stride);
599 p = pixels + i * stride;
600 end = p + rect->width * 4;
601 while (p < end) {
602 unsigned int t;
603 unsigned char a, r, g, b;
604
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605 a = p[3];
606 MULT(r, p[0], a, t);
607 MULT(g, p[1], a, t);
608 MULT(b, p[2], a, t);
609 p += 4;
610 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
611 }
612 }
613
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500614 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615
616 return surface;
617}
618
nobled7b87cb02011-02-01 18:51:47 +0000619static int
620check_size(struct rectangle *rect)
621{
622 if (rect->width && rect->height)
623 return 0;
624
625 fprintf(stderr, "tried to create surface of "
626 "width: %d, height: %d\n", rect->width, rect->height);
627 return -1;
628}
629
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630cairo_surface_t *
631display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400633 struct rectangle *rectangle,
634 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635{
nobled7b87cb02011-02-01 18:51:47 +0000636 if (check_size(rectangle) < 0)
637 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500638#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500639 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100640 if (surface)
641 return display_create_egl_window_surface(display,
642 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400643 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644 rectangle);
645 else
646 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400647 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500649 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400651 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652}
653
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500654static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655display_create_surface_from_file(struct display *display,
656 const char *filename,
657 struct rectangle *rectangle)
658{
nobled7b87cb02011-02-01 18:51:47 +0000659 if (check_size(rectangle) < 0)
660 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500661#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500662 if (display->dpy) {
663 return display_create_egl_image_surface_from_file(display,
664 filename,
665 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500666 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669}
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671 const char *filename;
672 int hotspot_x, hotspot_y;
673} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500674 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
675 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
676 { DATADIR "/weston/bottom_side.png", 16, 20 },
677 { DATADIR "/weston/grabbing.png", 20, 17 },
678 { DATADIR "/weston/left_ptr.png", 10, 5 },
679 { DATADIR "/weston/left_side.png", 10, 20 },
680 { DATADIR "/weston/right_side.png", 30, 19 },
681 { DATADIR "/weston/top_left_corner.png", 8, 8 },
682 { DATADIR "/weston/top_right_corner.png", 26, 8 },
683 { DATADIR "/weston/top_side.png", 18, 8 },
684 { DATADIR "/weston/xterm.png", 15, 15 },
685 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400686};
687
688static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400690{
691 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400693 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 display->pointer_surfaces =
697 malloc(count * sizeof *display->pointer_surfaces);
698 rect.width = width;
699 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400700 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702 display_create_surface_from_file(display,
703 pointer_images[i].filename,
704 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100705 if (!display->pointer_surfaces[i]) {
706 fprintf(stderr, "Error loading pointer image: %s\n",
707 pointer_images[i].filename);
708 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709 }
710
711}
712
Pekka Paalanen325bb602011-12-19 10:31:45 +0200713static void
714destroy_pointer_surfaces(struct display *display)
715{
716 int i, count;
717
718 count = ARRAY_LENGTH(pointer_images);
719 for (i = 0; i < count; ++i) {
720 if (display->pointer_surfaces[i])
721 cairo_surface_destroy(display->pointer_surfaces[i]);
722 }
723 free(display->pointer_surfaces);
724}
725
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400726cairo_surface_t *
727display_get_pointer_surface(struct display *display, int pointer,
728 int *width, int *height,
729 int *hotspot_x, int *hotspot_y)
730{
731 cairo_surface_t *surface;
732
733 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500734#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400735 *width = cairo_gl_surface_get_width(surface);
736 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000737#else
738 *width = cairo_image_surface_get_width(surface);
739 *height = cairo_image_surface_get_height(surface);
740#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400741 *hotspot_x = pointer_images[pointer].hotspot_x;
742 *hotspot_y = pointer_images[pointer].hotspot_y;
743
744 return cairo_surface_reference(surface);
745}
746
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400747static void
748window_attach_surface(struct window *window);
749
750static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400751free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400752{
753 struct window *window = data;
754
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400755 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756 cairo_surface_destroy(window->pending_surface);
757 window->pending_surface = NULL;
758 if (window->cairo_surface)
759 window_attach_surface(window);
760}
761
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762static const struct wl_callback_listener free_surface_listener = {
763 free_surface
764};
765
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500766static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200767window_get_resize_dx_dy(struct window *window, int *x, int *y)
768{
769 if (window->resize_edges & WINDOW_RESIZING_LEFT)
770 *x = window->server_allocation.width - window->allocation.width;
771 else
772 *x = 0;
773
774 if (window->resize_edges & WINDOW_RESIZING_TOP)
775 *y = window->server_allocation.height -
776 window->allocation.height;
777 else
778 *y = 0;
779
780 window->resize_edges = 0;
781}
782
783static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400784window_set_type(struct window *window)
785{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200786 if (!window->shell_surface)
787 return;
788
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400789 switch (window->type) {
790 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200791 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400792 break;
793 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200794 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400795 break;
796 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 wl_shell_surface_set_transient(window->shell_surface,
798 window->parent->shell_surface,
799 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400800 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500801 case TYPE_MENU:
802 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400803 case TYPE_CUSTOM:
804 break;
805 }
806}
807
808static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500809window_attach_surface(struct window *window)
810{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400811 struct display *display = window->display;
812 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400813 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000816#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500817 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400819 if (display->shell)
820 window_set_type(window);
821
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
825 data = cairo_surface_get_user_data(window->cairo_surface,
826 &surface_data_key);
827
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 cairo_gl_surface_swapbuffers(window->cairo_surface);
829 wl_egl_window_get_attached_size(data->window,
830 &window->server_allocation.width,
831 &window->server_allocation.height);
832 break;
833 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200836 window_get_resize_dx_dy(window, &x, &y);
837
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 if (window->pending_surface != NULL)
839 return;
840
841 window->pending_surface = window->cairo_surface;
842 window->cairo_surface = NULL;
843
844 buffer =
845 display_get_buffer_for_surface(display,
846 window->pending_surface);
847
848 wl_surface_attach(window->surface, buffer, x, y);
849 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400850 cb = wl_display_sync(display->display);
851 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000853 default:
854 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500856
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500857 wl_surface_damage(window->surface, 0, 0,
858 window->allocation.width,
859 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500860}
861
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500862void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400863window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500864{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100865 if (window->cairo_surface) {
866 switch (window->buffer_type) {
867 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
868 case WINDOW_BUFFER_TYPE_SHM:
869 display_surface_damage(window->display,
870 window->cairo_surface,
871 0, 0,
872 window->allocation.width,
873 window->allocation.height);
874 break;
875 default:
876 break;
877 }
878 window_attach_surface(window);
879 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500880}
881
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882void
883window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500885 cairo_surface_reference(surface);
886
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400887 if (window->cairo_surface != NULL)
888 cairo_surface_destroy(window->cairo_surface);
889
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500890 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891}
892
Benjamin Franzke22d54812011-07-16 19:50:32 +0000893#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894static void
895window_resize_cairo_window_surface(struct window *window)
896{
897 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200898 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899
900 data = cairo_surface_get_user_data(window->cairo_surface,
901 &surface_data_key);
902
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200903 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904 wl_egl_window_resize(data->window,
905 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200906 window->allocation.height,
907 x,y);
908
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909 cairo_gl_surface_set_size(window->cairo_surface,
910 window->allocation.width,
911 window->allocation.height);
912}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000913#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400915struct display *
916window_get_display(struct window *window)
917{
918 return window->display;
919}
920
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921void
922window_create_surface(struct window *window)
923{
924 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400925 uint32_t flags = 0;
926
927 if (!window->transparent)
928 flags = SURFACE_OPAQUE;
929
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400930 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500931#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100932 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
933 if (window->cairo_surface) {
934 window_resize_cairo_window_surface(window);
935 return;
936 }
937 surface = display_create_surface(window->display,
938 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400939 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100940 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500941 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100943 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400944 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945 break;
946#endif
947 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400948 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400949 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400950 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800951 default:
952 surface = NULL;
953 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400954 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955
956 window_set_surface(window, surface);
957 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400958}
959
960static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500961window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500962{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500963 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500964 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400965 cairo_surface_t *frame;
966 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500967
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400968 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400969
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400970 width = window->allocation.width;
971 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500972
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500973 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500974
Kristian Høgsberg09531622010-06-14 23:22:15 -0400975 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400976 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400977 cairo_paint(cr);
978
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400979 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400980 tile_mask(cr, window->display->shadow,
981 shadow_dx, shadow_dy, width, height,
982 window->margin + 10 - shadow_dx,
983 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500984
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400985 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400986 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400987 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400988 frame = window->display->inactive_frame;
989
990 tile_source(cr, frame, 0, 0, width, height,
991 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500992
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500993 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
994 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500995 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400996 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400997 if (window->keyboard_device)
998 cairo_set_source_rgb(cr, 0, 0, 0);
999 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001000 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001001 cairo_show_text(cr, window->title);
1002
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001003 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001004}
1005
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001006void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001007window_destroy(struct window *window)
1008{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001009 struct display *display = window->display;
1010 struct input *input;
1011
1012 if (window->redraw_scheduled)
1013 wl_list_remove(&window->redraw_task.link);
1014
1015 wl_list_for_each(input, &display->input_list, link) {
1016 if (input->pointer_focus == window)
1017 input->pointer_focus = NULL;
1018 if (input->keyboard_focus == window)
1019 input->keyboard_focus = NULL;
1020 }
1021
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001022 if (window->shell_surface)
1023 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001024 wl_surface_destroy(window->surface);
1025 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001026
1027 if (window->cairo_surface != NULL)
1028 cairo_surface_destroy(window->cairo_surface);
1029 if (window->pending_surface != NULL)
1030 cairo_surface_destroy(window->pending_surface);
1031
1032 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001033 free(window);
1034}
1035
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001036static struct widget *
1037window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001038{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 wl_list_for_each(widget, &window->widget_list, link) {
1042 if (widget->allocation.x <= x &&
1043 x < widget->allocation.x + widget->allocation.width &&
1044 widget->allocation.y <= y &&
1045 y < widget->allocation.y + widget->allocation.height) {
1046 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001047 }
1048 }
1049
1050 return NULL;
1051}
1052
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001053struct widget *
1054window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001056 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058 widget = malloc(sizeof *widget);
1059 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001060 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001061 widget->user_data = data;
1062 wl_list_insert(window->widget_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001063
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001064 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065}
1066
1067void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 wl_list_for_each(widget, &window->widget_list, link)
1073 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001074}
1075
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001076struct widget *
1077window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001078{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001079 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
1082void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001083widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001084{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086}
1087
1088void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089widget_set_allocation(struct widget *widget,
1090 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 widget->allocation.x = x;
1093 widget->allocation.y = y;
1094 widget->allocation.width = width;
1095 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001096}
1097
1098void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001101 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001104void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001105widget_set_focus_handler(struct widget *widget, widget_focus_handler_t handler)
1106{
1107 widget->focus_handler = handler;
1108}
1109
1110void
1111widget_schedule_redraw(struct widget *widget)
1112{
1113 window_schedule_redraw(widget->window);
1114}
1115
1116void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001117window_draw(struct window *window)
1118{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001119 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001120 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001121 else
1122 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001123}
1124
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001125cairo_surface_t *
1126window_get_surface(struct window *window)
1127{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001128 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001129}
1130
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001131struct wl_surface *
1132window_get_wl_surface(struct window *window)
1133{
1134 return window->surface;
1135}
1136
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001137struct wl_shell_surface *
1138window_get_wl_shell_surface(struct window *window)
1139{
1140 return window->shell_surface;
1141}
1142
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001143static int
1144get_pointer_location(struct window *window, int32_t x, int32_t y)
1145{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001146 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001147 const int grip_size = 8;
1148
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001149 if (!window->decoration)
1150 return WINDOW_CLIENT_AREA;
1151
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001152 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001153 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 else if (window->margin <= x && x < window->margin + grip_size)
1155 hlocation = WINDOW_RESIZING_LEFT;
1156 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001157 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158 else if (x < window->allocation.width - window->margin)
1159 hlocation = WINDOW_RESIZING_RIGHT;
1160 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001161 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001162
1163 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001164 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001165 else if (window->margin <= y && y < window->margin + grip_size)
1166 vlocation = WINDOW_RESIZING_TOP;
1167 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001168 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169 else if (y < window->allocation.height - window->margin)
1170 vlocation = WINDOW_RESIZING_BOTTOM;
1171 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001172 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001174 location = vlocation | hlocation;
1175 if (location & WINDOW_EXTERIOR)
1176 location = WINDOW_EXTERIOR;
1177 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1178 location = WINDOW_TITLEBAR;
1179 else if (location == WINDOW_INTERIOR)
1180 location = WINDOW_CLIENT_AREA;
1181
1182 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001183}
1184
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001185static int
1186input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001187{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001188 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001189
1190 location = get_pointer_location(input->pointer_focus,
1191 input->sx, input->sy);
1192 switch (location) {
1193 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001194 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001195 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001196 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001198 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001199 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001200 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001201 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001202 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001204 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001205 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001206 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001207 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001208 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001209 case WINDOW_EXTERIOR:
1210 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001211 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001212 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001213 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001214 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001215}
1216
1217void
1218input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1219{
1220 struct display *display = input->display;
1221 struct wl_buffer *buffer;
1222 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001224 if (pointer == input->current_pointer_image)
1225 return;
1226
1227 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001229
1230 if (!surface)
1231 return;
1232
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001233 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001234 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235 pointer_images[pointer].hotspot_x,
1236 pointer_images[pointer].hotspot_y);
1237}
1238
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001239static void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001240window_set_focus_widget(struct window *window, struct widget *focus)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001241{
1242 void *data;
1243
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001244 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001245 return;
1246
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001247 window->focus_widget = focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001248 data = focus ? focus->user_data : NULL;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001249 if (focus && focus->focus_handler)
1250 focus->focus_handler(focus, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001251}
1252
1253static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001254input_handle_motion(void *data, struct wl_input_device *input_device,
1255 uint32_t time,
1256 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001257{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001258 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001260 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001261 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001262
1263 input->x = x;
1264 input->y = y;
1265 input->sx = sx;
1266 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001268 if (!window->focus_widget || !window->widget_grab_button) {
1269 widget = window_find_widget(window, sx, sy);
1270 window_set_focus_widget(window, widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001271 }
1272
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 if (window->motion_handler)
1274 pointer = (*window->motion_handler)(window, input, time,
1275 x, y, sx, sy,
1276 window->user_data);
1277
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001278 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001279 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001280}
1281
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001282static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001283window_menu_func(struct window *window, int index, void *data)
1284{
1285 switch (index) {
1286 case 0: /* close */
1287 if (window->close_handler)
1288 window->close_handler(window->parent,
1289 window->user_data);
1290 else
1291 exit(0);
1292 break;
1293 case 1: /* fullscreen */
1294 /* we don't have a way to get out of fullscreen for now */
1295 window_set_fullscreen(window, 1);
1296 break;
1297 case 2: /* rotate */
1298 case 3: /* scale */
1299 break;
1300 }
1301}
1302
1303
1304static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001305input_handle_button(void *data,
1306 struct wl_input_device *input_device,
1307 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001308{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001309 struct input *input = data;
1310 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001311 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001312 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001313 int32_t x, y;
1314 static const char *entries[] = {
1315 "Close", "Fullscreen", "Rotate", "Scale"
1316 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001317
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001318 if (window->focus_widget && window->widget_grab_button == 0 && state)
1319 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001320
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001321 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001322
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001323 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001325 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001326 if (!window->shell_surface)
1327 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001328 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001329 wl_shell_surface_move(window->shell_surface,
1330 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001331 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001332 case WINDOW_RESIZING_TOP:
1333 case WINDOW_RESIZING_BOTTOM:
1334 case WINDOW_RESIZING_LEFT:
1335 case WINDOW_RESIZING_RIGHT:
1336 case WINDOW_RESIZING_TOP_LEFT:
1337 case WINDOW_RESIZING_TOP_RIGHT:
1338 case WINDOW_RESIZING_BOTTOM_LEFT:
1339 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001340 if (!window->shell_surface)
1341 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001342 wl_shell_surface_resize(window->shell_surface,
1343 input_device, time,
1344 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001345 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001346 case WINDOW_CLIENT_AREA:
1347 if (window->button_handler)
1348 (*window->button_handler)(window,
1349 input, time,
1350 button, state,
1351 window->user_data);
1352 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001353 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001354 } else if (button == BTN_RIGHT && state == 1) {
1355 switch (location) {
1356 default:
1357 input_get_position(input, &x, &y);
1358 window->menu = window_create_menu(window->display,
1359 input, time,
1360 window,
1361 x - 10, y - 10,
1362 window_menu_func,
1363 entries, 4);
1364 window_schedule_redraw(window->menu);
1365 break;
1366 case WINDOW_CLIENT_AREA:
1367 if (window->button_handler)
1368 (*window->button_handler)(window,
1369 input, time,
1370 button, state,
1371 window->user_data);
1372 break;
1373 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001374 } else {
1375 if (window->button_handler)
1376 (*window->button_handler)(window,
1377 input, time,
1378 button, state,
1379 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001380 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001381
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001382 if (window->focus_widget &&
1383 window->widget_grab_button == button && !state) {
1384 window->widget_grab_button = 0;
1385 widget = window_find_widget(window, input->sx, input->sy);
1386 window_set_focus_widget(window, widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001387 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001388}
1389
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001390static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001391input_handle_key(void *data, struct wl_input_device *input_device,
1392 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001393{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001394 struct input *input = data;
1395 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001396 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001397 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001398
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001399 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001400 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001401 return;
1402
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001403 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001404 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001405 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1406 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001407
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001408 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1409
1410 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001411 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001412 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001414
1415 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001416 (*window->key_handler)(window, input, time, key, sym, state,
1417 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001418}
1419
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001420static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001421input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001422{
1423 struct window *window = input->pointer_focus;
1424
1425 if (!window)
1426 return;
1427
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001428 window_set_focus_widget(window, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001429
1430 if (window->leave_handler)
1431 window->leave_handler(window, input, time, window->user_data);
1432 input->pointer_focus = NULL;
1433 input->current_pointer_image = POINTER_UNSET;
1434}
1435
1436static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001437input_handle_pointer_focus(void *data,
1438 struct wl_input_device *input_device,
1439 uint32_t time, struct wl_surface *surface,
1440 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001441{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001442 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001443 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001444 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001445 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001446
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001447 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001448 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001449 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001450
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001451 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001452 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001453 window = input->pointer_focus;
1454
Kristian Høgsberg59826582011-01-20 11:56:57 -05001455 input->x = x;
1456 input->y = y;
1457 input->sx = sx;
1458 input->sy = sy;
1459
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001460 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001461 if (window->enter_handler)
1462 pointer = window->enter_handler(window, input,
1463 time, sx, sy,
1464 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001465
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001466 widget = window_find_widget(window, x, y);
1467 window_set_focus_widget(window, widget);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001468
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001469 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001470 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001471 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001472}
1473
1474static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001475input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001476{
1477 struct window *window = input->keyboard_focus;
1478
1479 if (!window)
1480 return;
1481
1482 window->keyboard_device = NULL;
1483 if (window->keyboard_focus_handler)
1484 (*window->keyboard_focus_handler)(window, NULL,
1485 window->user_data);
1486
1487 input->keyboard_focus = NULL;
1488}
1489
1490static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001491input_handle_keyboard_focus(void *data,
1492 struct wl_input_device *input_device,
1493 uint32_t time,
1494 struct wl_surface *surface,
1495 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001496{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001497 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001498 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001499 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001500 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001501
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001502 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001503
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001504 if (surface)
1505 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001506
1507 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001508 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001509 for (k = keys->data; k < end; k++)
1510 input->modifiers |= d->xkb->map->modmap[*k];
1511
1512 window = input->keyboard_focus;
1513 if (window) {
1514 window->keyboard_device = input;
1515 if (window->keyboard_focus_handler)
1516 (*window->keyboard_focus_handler)(window,
1517 window->keyboard_device,
1518 window->user_data);
1519 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001520}
1521
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001522static void
1523input_handle_touch_down(void *data,
1524 struct wl_input_device *wl_input_device,
1525 uint32_t time, struct wl_surface *surface,
1526 int32_t id, int32_t x, int32_t y)
1527{
1528}
1529
1530static void
1531input_handle_touch_up(void *data,
1532 struct wl_input_device *wl_input_device,
1533 uint32_t time, int32_t id)
1534{
1535}
1536
1537static void
1538input_handle_touch_motion(void *data,
1539 struct wl_input_device *wl_input_device,
1540 uint32_t time, int32_t id, int32_t x, int32_t y)
1541{
1542}
1543
1544static void
1545input_handle_touch_frame(void *data,
1546 struct wl_input_device *wl_input_device)
1547{
1548}
1549
1550static void
1551input_handle_touch_cancel(void *data,
1552 struct wl_input_device *wl_input_device)
1553{
1554}
1555
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001556static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001557 input_handle_motion,
1558 input_handle_button,
1559 input_handle_key,
1560 input_handle_pointer_focus,
1561 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001562 input_handle_touch_down,
1563 input_handle_touch_up,
1564 input_handle_touch_motion,
1565 input_handle_touch_frame,
1566 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001567};
1568
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001569void
1570input_get_position(struct input *input, int32_t *x, int32_t *y)
1571{
1572 *x = input->sx;
1573 *y = input->sy;
1574}
1575
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001576struct wl_input_device *
1577input_get_input_device(struct input *input)
1578{
1579 return input->input_device;
1580}
1581
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001582uint32_t
1583input_get_modifiers(struct input *input)
1584{
1585 return input->modifiers;
1586}
1587
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001588struct data_offer {
1589 struct wl_data_offer *offer;
1590 struct input *input;
1591 struct wl_array types;
1592 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001593
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001594 struct task io_task;
1595 int fd;
1596 data_func_t func;
1597 int32_t x, y;
1598 void *user_data;
1599};
1600
1601static void
1602data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1603{
1604 struct data_offer *offer = data;
1605 char **p;
1606
1607 p = wl_array_add(&offer->types, sizeof *p);
1608 *p = strdup(type);
1609}
1610
1611static const struct wl_data_offer_listener data_offer_listener = {
1612 data_offer_offer,
1613};
1614
1615static void
1616data_offer_destroy(struct data_offer *offer)
1617{
1618 char **p;
1619
1620 offer->refcount--;
1621 if (offer->refcount == 0) {
1622 wl_data_offer_destroy(offer->offer);
1623 for (p = offer->types.data; *p; p++)
1624 free(*p);
1625 wl_array_release(&offer->types);
1626 free(offer);
1627 }
1628}
1629
1630static void
1631data_device_data_offer(void *data,
1632 struct wl_data_device *data_device, uint32_t id)
1633{
1634 struct data_offer *offer;
1635
1636 offer = malloc(sizeof *offer);
1637
1638 wl_array_init(&offer->types);
1639 offer->refcount = 1;
1640 offer->input = data;
1641
1642 /* FIXME: Generate typesafe wrappers for this */
1643 offer->offer = (struct wl_data_offer *)
1644 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1645 id, &wl_data_offer_interface);
1646
1647 wl_data_offer_add_listener(offer->offer,
1648 &data_offer_listener, offer);
1649}
1650
1651static void
1652data_device_enter(void *data, struct wl_data_device *data_device,
1653 uint32_t time, struct wl_surface *surface,
1654 int32_t x, int32_t y, struct wl_data_offer *offer)
1655{
1656 struct input *input = data;
1657 struct window *window;
1658 char **p;
1659
1660 input->drag_offer = wl_data_offer_get_user_data(offer);
1661 window = wl_surface_get_user_data(surface);
1662 input->pointer_focus = window;
1663
1664 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1665 *p = NULL;
1666
1667 window = input->pointer_focus;
1668 if (window->data_handler)
1669 window->data_handler(window, input, time, x, y,
1670 input->drag_offer->types.data,
1671 window->user_data);
1672}
1673
1674static void
1675data_device_leave(void *data, struct wl_data_device *data_device)
1676{
1677 struct input *input = data;
1678
1679 data_offer_destroy(input->drag_offer);
1680 input->drag_offer = NULL;
1681}
1682
1683static void
1684data_device_motion(void *data, struct wl_data_device *data_device,
1685 uint32_t time, int32_t x, int32_t y)
1686{
1687 struct input *input = data;
1688 struct window *window = input->pointer_focus;
1689
1690 input->sx = x;
1691 input->sy = y;
1692
1693 if (window->data_handler)
1694 window->data_handler(window, input, time, x, y,
1695 input->drag_offer->types.data,
1696 window->user_data);
1697}
1698
1699static void
1700data_device_drop(void *data, struct wl_data_device *data_device)
1701{
1702 struct input *input = data;
1703 struct window *window = input->pointer_focus;
1704
1705 if (window->drop_handler)
1706 window->drop_handler(window, input,
1707 input->sx, input->sy, window->user_data);
1708}
1709
1710static void
1711data_device_selection(void *data,
1712 struct wl_data_device *wl_data_device,
1713 struct wl_data_offer *offer)
1714{
1715 struct input *input = data;
1716 char **p;
1717
1718 if (input->selection_offer)
1719 data_offer_destroy(input->selection_offer);
1720
1721 input->selection_offer = wl_data_offer_get_user_data(offer);
1722 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1723 *p = NULL;
1724}
1725
1726static const struct wl_data_device_listener data_device_listener = {
1727 data_device_data_offer,
1728 data_device_enter,
1729 data_device_leave,
1730 data_device_motion,
1731 data_device_drop,
1732 data_device_selection
1733};
1734
1735struct wl_data_device *
1736input_get_data_device(struct input *input)
1737{
1738 return input->data_device;
1739}
1740
1741void
1742input_set_selection(struct input *input,
1743 struct wl_data_source *source, uint32_t time)
1744{
1745 wl_data_device_set_selection(input->data_device, source, time);
1746}
1747
1748void
1749input_accept(struct input *input, uint32_t time, const char *type)
1750{
1751 wl_data_offer_accept(input->drag_offer->offer, time, type);
1752}
1753
1754static void
1755offer_io_func(struct task *task, uint32_t events)
1756{
1757 struct data_offer *offer =
1758 container_of(task, struct data_offer, io_task);
1759 unsigned int len;
1760 char buffer[4096];
1761
1762 len = read(offer->fd, buffer, sizeof buffer);
1763 offer->func(buffer, len,
1764 offer->x, offer->y, offer->user_data);
1765
1766 if (len == 0) {
1767 close(offer->fd);
1768 data_offer_destroy(offer);
1769 }
1770}
1771
1772static void
1773data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1774 data_func_t func, void *user_data)
1775{
1776 int p[2];
1777
1778 pipe2(p, O_CLOEXEC);
1779 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1780 close(p[1]);
1781
1782 offer->io_task.run = offer_io_func;
1783 offer->fd = p[0];
1784 offer->func = func;
1785 offer->refcount++;
1786 offer->user_data = user_data;
1787
1788 display_watch_fd(offer->input->display,
1789 offer->fd, EPOLLIN, &offer->io_task);
1790}
1791
1792void
1793input_receive_drag_data(struct input *input, const char *mime_type,
1794 data_func_t func, void *data)
1795{
1796 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1797 input->drag_offer->x = input->sx;
1798 input->drag_offer->y = input->sy;
1799}
1800
1801int
1802input_receive_selection_data(struct input *input, const char *mime_type,
1803 data_func_t func, void *data)
1804{
1805 char **p;
1806
1807 if (input->selection_offer == NULL)
1808 return -1;
1809
1810 for (p = input->selection_offer->types.data; *p; p++)
1811 if (strcmp(mime_type, *p) == 0)
1812 break;
1813
1814 if (*p == NULL)
1815 return -1;
1816
1817 data_offer_receive_data(input->selection_offer,
1818 mime_type, func, data);
1819 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001820}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001821
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001822int
1823input_receive_selection_data_to_fd(struct input *input,
1824 const char *mime_type, int fd)
1825{
1826 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1827
1828 return 0;
1829}
1830
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001831void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001832window_move(struct window *window, struct input *input, uint32_t time)
1833{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001834 if (!window->shell_surface)
1835 return;
1836
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001837 wl_shell_surface_move(window->shell_surface,
1838 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001839}
1840
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001841static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001842handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001843 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001844 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001845{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001846 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001847 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001848
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001849 /* FIXME: this is probably the wrong place to check for width
1850 * or height <= 0, but it prevents the compositor from crashing
1851 */
1852 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001853 return;
1854
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001855 window->resize_edges = edges;
1856
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001857 if (window->resize_handler) {
1858 child_width = width - 20 - window->margin * 2;
1859 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001860
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001861 (*window->resize_handler)(window,
1862 child_width, child_height,
1863 window->user_data);
1864 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001865 window->allocation.width = width;
1866 window->allocation.height = height;
1867
1868 if (window->redraw_handler)
1869 window_schedule_redraw(window);
1870 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001871}
1872
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001873static void
1874handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1875{
1876 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001877 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001878 /* FIXME: Need more context in this event, at least the input
1879 * device. Or just use wl_callback. */
1880
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001881 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001882 window_destroy(window);
1883}
1884
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001885static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001886 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001887 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001888};
1889
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001890void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001891window_get_allocation(struct window *window,
1892 struct rectangle *allocation)
1893{
1894 *allocation = window->allocation;
1895}
1896
1897void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001898window_get_child_allocation(struct window *window,
1899 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001900{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001901 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001902 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001903 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001904 allocation->x = window->margin + 10;
1905 allocation->y = window->margin + 50;
1906 allocation->width =
1907 window->allocation.width - 20 - window->margin * 2;
1908 allocation->height =
1909 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001910 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001911}
1912
1913void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001914window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001915{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001916 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001917 window->allocation.x = 20 + window->margin;
1918 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001919 window->allocation.width = width + 20 + window->margin * 2;
1920 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001921 } else {
1922 window->allocation.x = 0;
1923 window->allocation.y = 0;
1924 window->allocation.width = width;
1925 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001926 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001927}
1928
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001929static void
1930idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001931{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001932 struct window *window =
1933 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001934
1935 window->redraw_handler(window, window->user_data);
1936 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001937}
1938
1939void
1940window_schedule_redraw(struct window *window)
1941{
1942 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001943 window->redraw_task.run = idle_redraw;
1944 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001945 window->redraw_scheduled = 1;
1946 }
1947}
1948
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001949void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001950window_set_custom(struct window *window)
1951{
1952 window->type = TYPE_CUSTOM;
1953}
1954
1955void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001956window_set_fullscreen(struct window *window, int fullscreen)
1957{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001958 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001959 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001960
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001961 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001962 return;
1963
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001964 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001965 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001966 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001967 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001968 width = output->allocation.width;
1969 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001970 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001971 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001972 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001973 width = window->saved_allocation.width - 20 - window->margin * 2;
1974 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001975 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001976 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001977
1978 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001979}
1980
1981void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001982window_set_decoration(struct window *window, int decoration)
1983{
1984 window->decoration = decoration;
1985}
1986
1987void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001988window_set_user_data(struct window *window, void *data)
1989{
1990 window->user_data = data;
1991}
1992
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001993void *
1994window_get_user_data(struct window *window)
1995{
1996 return window->user_data;
1997}
1998
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001999void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002000window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002001 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002002{
2003 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002004}
2005
2006void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002007window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002008 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002009{
2010 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002011}
2012
2013void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002014window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002015 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002016{
2017 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002018}
2019
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002020void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002021window_set_button_handler(struct window *window,
2022 window_button_handler_t handler)
2023{
2024 window->button_handler = handler;
2025}
2026
2027void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002028window_set_motion_handler(struct window *window,
2029 window_motion_handler_t handler)
2030{
2031 window->motion_handler = handler;
2032}
2033
2034void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04002035window_set_enter_handler(struct window *window,
2036 window_enter_handler_t handler)
2037{
2038 window->enter_handler = handler;
2039}
2040
2041void
2042window_set_leave_handler(struct window *window,
2043 window_leave_handler_t handler)
2044{
2045 window->leave_handler = handler;
2046}
2047
2048void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002049window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002050 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002051{
2052 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002053}
2054
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002055void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002056window_set_data_handler(struct window *window, window_data_handler_t handler)
2057{
2058 window->data_handler = handler;
2059}
2060
2061void
2062window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2063{
2064 window->drop_handler = handler;
2065}
2066
2067void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002068window_set_close_handler(struct window *window,
2069 window_close_handler_t handler)
2070{
2071 window->close_handler = handler;
2072}
2073
2074void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002075window_set_transparent(struct window *window, int transparent)
2076{
2077 window->transparent = transparent;
2078}
2079
2080void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002081window_set_title(struct window *window, const char *title)
2082{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002083 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002084 window->title = strdup(title);
2085}
2086
2087const char *
2088window_get_title(struct window *window)
2089{
2090 return window->title;
2091}
2092
2093void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002094display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2095 int32_t x, int32_t y, int32_t width, int32_t height)
2096{
2097 struct wl_buffer *buffer;
2098
2099 buffer = display_get_buffer_for_surface(display, cairo_surface);
2100
2101 wl_buffer_damage(buffer, x, y, width, height);
2102}
2103
2104void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002105window_damage(struct window *window, int32_t x, int32_t y,
2106 int32_t width, int32_t height)
2107{
2108 wl_surface_damage(window->surface, x, y, width, height);
2109}
2110
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002111static struct window *
2112window_create_internal(struct display *display, struct window *parent,
2113 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002114{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002115 struct window *window;
2116
2117 window = malloc(sizeof *window);
2118 if (window == NULL)
2119 return NULL;
2120
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002121 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002122 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002123 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002124 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002125 if (display->shell) {
2126 window->shell_surface =
2127 wl_shell_get_shell_surface(display->shell,
2128 window->surface);
2129 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002130 window->allocation.x = 0;
2131 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002132 window->allocation.width = width;
2133 window->allocation.height = height;
2134 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002135 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002136 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002137 window->transparent = 1;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002138 wl_list_init(&window->widget_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002139
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002140 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002141#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002142 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2143 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002144#else
2145 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2146#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002147 else
2148 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002149
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002150 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002151 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002152
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002153 if (window->shell_surface) {
2154 wl_shell_surface_set_user_data(window->shell_surface, window);
2155 wl_shell_surface_add_listener(window->shell_surface,
2156 &shell_surface_listener, window);
2157 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002158
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002159 return window;
2160}
2161
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002162struct window *
2163window_create(struct display *display, int32_t width, int32_t height)
2164{
2165 struct window *window;
2166
2167 window = window_create_internal(display, NULL, width, height);
2168 if (!window)
2169 return NULL;
2170
2171 return window;
2172}
2173
2174struct window *
2175window_create_transient(struct display *display, struct window *parent,
2176 int32_t x, int32_t y, int32_t width, int32_t height)
2177{
2178 struct window *window;
2179
2180 window = window_create_internal(parent->display,
2181 parent, width, height);
2182 if (!window)
2183 return NULL;
2184
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002185 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002186 window->x = x;
2187 window->y = y;
2188
2189 return window;
2190}
2191
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002192static int
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002193menu_set_widget(struct window *window, struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002194{
2195 int next;
2196
2197 next = (sy - 8) / 20;
2198 if (menu->current != next) {
2199 menu->current = next;
2200 window_schedule_redraw(window);
2201 }
2202
2203 return POINTER_LEFT_PTR;
2204}
2205
2206static int
2207menu_motion_handler(struct window *window,
2208 struct input *input, uint32_t time,
2209 int32_t x, int32_t y,
2210 int32_t sx, int32_t sy, void *data)
2211{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002212 return menu_set_widget(window, data, sy);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002213}
2214
2215static int
2216menu_enter_handler(struct window *window,
2217 struct input *input, uint32_t time,
2218 int32_t x, int32_t y, void *data)
2219{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002220 return menu_set_widget(window, data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002221}
2222
2223static void
2224menu_leave_handler(struct window *window,
2225 struct input *input, uint32_t time, void *data)
2226{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002227 menu_set_widget(window, data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002228}
2229
2230static void
2231menu_button_handler(struct window *window,
2232 struct input *input, uint32_t time,
2233 int button, int state, void *data)
2234
2235{
2236 struct menu *menu = data;
2237
2238 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002239 if (state == 0 && time - menu->time > 500) {
2240 menu->func(window->parent,
2241 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002242 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002243 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002244}
2245
2246static void
2247menu_redraw_handler(struct window *window, void *data)
2248{
2249 cairo_t *cr;
2250 const int32_t r = 3, margin = 3;
2251 struct menu *menu = data;
2252 int32_t width, height, i;
2253
2254 width = 200;
2255 height = menu->count * 20 + margin * 2;
2256 window_set_child_size(window, width, height);
2257 window_create_surface(window);
2258
2259 cr = cairo_create(window->cairo_surface);
2260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2261 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2262 cairo_paint(cr);
2263
2264 width = window->allocation.width;
2265 height = window->allocation.height;
2266 rounded_rect(cr, 0, 0, width, height, r);
2267 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2268 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2269 cairo_fill(cr);
2270
2271 for (i = 0; i < menu->count; i++) {
2272 if (i == menu->current) {
2273 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2274 cairo_rectangle(cr, margin, i * 20 + margin,
2275 width - 2 * margin, 20);
2276 cairo_fill(cr);
2277 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2278 cairo_move_to(cr, 10, i * 20 + 16);
2279 cairo_show_text(cr, menu->entries[i]);
2280 } else {
2281 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2282 cairo_move_to(cr, 10, i * 20 + 16);
2283 cairo_show_text(cr, menu->entries[i]);
2284 }
2285 }
2286
2287 cairo_destroy(cr);
2288 window_flush(window);
2289}
2290
2291struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002292window_create_menu(struct display *display,
2293 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002294 int32_t x, int32_t y,
2295 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002296{
2297 struct window *window;
2298 struct menu *menu;
2299
2300 menu = malloc(sizeof *menu);
2301 if (!menu)
2302 return NULL;
2303
2304 window = window_create_internal(parent->display, parent, 0, 0);
2305 if (!window)
2306 return NULL;
2307
2308 menu->window = window;
2309 menu->entries = entries;
2310 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002311 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002312 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002313 window->decoration = 0;
2314 window->type = TYPE_MENU;
2315 window->x = x;
2316 window->y = y;
2317
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002318 wl_shell_surface_set_popup(window->shell_surface,
2319 input->input_device, time,
2320 window->parent->shell_surface,
2321 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002322
2323 window_set_motion_handler(window, menu_motion_handler);
2324 window_set_enter_handler(window, menu_enter_handler);
2325 window_set_leave_handler(window, menu_leave_handler);
2326 window_set_button_handler(window, menu_button_handler);
2327 window_set_redraw_handler(window, menu_redraw_handler);
2328 window_set_user_data(window, menu);
2329
2330 return window;
2331}
2332
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002333void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002334window_set_buffer_type(struct window *window, enum window_buffer_type type)
2335{
2336 window->buffer_type = type;
2337}
2338
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002339
2340static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002341display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002342 struct wl_output *wl_output,
2343 int x, int y,
2344 int physical_width,
2345 int physical_height,
2346 int subpixel,
2347 const char *make,
2348 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002349{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002350 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002351
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002352 output->allocation.x = x;
2353 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002354}
2355
2356static void
2357display_handle_mode(void *data,
2358 struct wl_output *wl_output,
2359 uint32_t flags,
2360 int width,
2361 int height,
2362 int refresh)
2363{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002364 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002365 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002366
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002367 if (flags & WL_OUTPUT_MODE_CURRENT) {
2368 output->allocation.width = width;
2369 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002370 if (display->output_configure_handler)
2371 (*display->output_configure_handler)(
2372 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002373 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002374}
2375
2376static const struct wl_output_listener output_listener = {
2377 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002378 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002379};
2380
2381static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002382display_add_output(struct display *d, uint32_t id)
2383{
2384 struct output *output;
2385
2386 output = malloc(sizeof *output);
2387 if (output == NULL)
2388 return;
2389
2390 memset(output, 0, sizeof *output);
2391 output->display = d;
2392 output->output =
2393 wl_display_bind(d->display, id, &wl_output_interface);
2394 wl_list_insert(d->output_list.prev, &output->link);
2395
2396 wl_output_add_listener(output->output, &output_listener, output);
2397}
2398
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002399static void
2400output_destroy(struct output *output)
2401{
2402 if (output->destroy_handler)
2403 (*output->destroy_handler)(output, output->user_data);
2404
2405 wl_output_destroy(output->output);
2406 wl_list_remove(&output->link);
2407 free(output);
2408}
2409
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002410void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002411display_set_output_configure_handler(struct display *display,
2412 display_output_handler_t handler)
2413{
2414 struct output *output;
2415
2416 display->output_configure_handler = handler;
2417 if (!handler)
2418 return;
2419
2420 wl_list_for_each(output, &display->output_list, link)
2421 (*display->output_configure_handler)(output,
2422 display->user_data);
2423}
2424
2425void
2426output_set_user_data(struct output *output, void *data)
2427{
2428 output->user_data = data;
2429}
2430
2431void *
2432output_get_user_data(struct output *output)
2433{
2434 return output->user_data;
2435}
2436
2437void
2438output_set_destroy_handler(struct output *output,
2439 display_output_handler_t handler)
2440{
2441 output->destroy_handler = handler;
2442 /* FIXME: implement this, once we have way to remove outputs */
2443}
2444
2445void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002446output_get_allocation(struct output *output, struct rectangle *allocation)
2447{
2448 *allocation = output->allocation;
2449}
2450
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002451struct wl_output *
2452output_get_wl_output(struct output *output)
2453{
2454 return output->output;
2455}
2456
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002457static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002458display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002459{
2460 struct input *input;
2461
2462 input = malloc(sizeof *input);
2463 if (input == NULL)
2464 return;
2465
2466 memset(input, 0, sizeof *input);
2467 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002468 input->input_device =
2469 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002470 input->pointer_focus = NULL;
2471 input->keyboard_focus = NULL;
2472 wl_list_insert(d->input_list.prev, &input->link);
2473
2474 wl_input_device_add_listener(input->input_device,
2475 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002476 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002477
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002478 input->data_device =
2479 wl_data_device_manager_get_data_device(d->data_device_manager,
2480 input->input_device);
2481 wl_data_device_add_listener(input->data_device,
2482 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002483}
2484
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002485static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002486input_destroy(struct input *input)
2487{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002488 input_remove_keyboard_focus(input);
2489 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002490
2491 if (input->drag_offer)
2492 data_offer_destroy(input->drag_offer);
2493
2494 if (input->selection_offer)
2495 data_offer_destroy(input->selection_offer);
2496
2497 wl_data_device_destroy(input->data_device);
2498 wl_list_remove(&input->link);
2499 wl_input_device_destroy(input->input_device);
2500 free(input);
2501}
2502
2503static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002504display_handle_global(struct wl_display *display, uint32_t id,
2505 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002506{
2507 struct display *d = data;
2508
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002509 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002510 d->compositor =
2511 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002512 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002513 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002514 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002515 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002516 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002517 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002518 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002519 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002520 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2521 d->data_device_manager =
2522 wl_display_bind(display, id,
2523 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002524 }
2525}
2526
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002527static void
2528display_render_frame(struct display *d)
2529{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002530 int radius = 8;
2531 cairo_t *cr;
2532
2533 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2534 cr = cairo_create(d->shadow);
2535 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2536 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2537 rounded_rect(cr, 16, 16, 112, 112, radius);
2538 cairo_fill(cr);
2539 cairo_destroy(cr);
2540 blur_surface(d->shadow, 64);
2541
2542 d->active_frame =
2543 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2544 cr = cairo_create(d->active_frame);
2545 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2546 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2547 rounded_rect(cr, 16, 16, 112, 112, radius);
2548 cairo_fill(cr);
2549 cairo_destroy(cr);
2550
2551 d->inactive_frame =
2552 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2553 cr = cairo_create(d->inactive_frame);
2554 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2555 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2556 rounded_rect(cr, 16, 16, 112, 112, radius);
2557 cairo_fill(cr);
2558 cairo_destroy(cr);
2559}
2560
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002561static void
2562init_xkb(struct display *d)
2563{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002564 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002565
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002566 names.rules = "evdev";
2567 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002568 names.layout = option_xkb_layout;
2569 names.variant = option_xkb_variant;
2570 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002571
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002572 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002573 if (!d->xkb) {
2574 fprintf(stderr, "Failed to compile keymap\n");
2575 exit(1);
2576 }
2577}
2578
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002579static void
2580fini_xkb(struct display *display)
2581{
2582 xkb_free_keymap(display->xkb);
2583}
2584
Yuval Fledel45568f62010-12-06 09:18:12 -05002585static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002586init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002587{
2588 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002589 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002590
2591 static const EGLint premul_argb_cfg_attribs[] = {
2592 EGL_SURFACE_TYPE,
2593 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2594 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002595 EGL_RED_SIZE, 1,
2596 EGL_GREEN_SIZE, 1,
2597 EGL_BLUE_SIZE, 1,
2598 EGL_ALPHA_SIZE, 1,
2599 EGL_DEPTH_SIZE, 1,
2600 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2601 EGL_NONE
2602 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002603
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002604 static const EGLint rgb_cfg_attribs[] = {
2605 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2606 EGL_RED_SIZE, 1,
2607 EGL_GREEN_SIZE, 1,
2608 EGL_BLUE_SIZE, 1,
2609 EGL_ALPHA_SIZE, 0,
2610 EGL_DEPTH_SIZE, 1,
2611 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2612 EGL_NONE
2613 };
2614
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002615 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002616 if (!eglInitialize(d->dpy, &major, &minor)) {
2617 fprintf(stderr, "failed to initialize display\n");
2618 return -1;
2619 }
2620
2621 if (!eglBindAPI(EGL_OPENGL_API)) {
2622 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2623 return -1;
2624 }
2625
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002626 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002627 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002628 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002629 return -1;
2630 }
2631
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002632 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2633 &d->rgb_config, 1, &n) || n != 1) {
2634 fprintf(stderr, "failed to choose rgb config\n");
2635 return -1;
2636 }
2637
Benjamin Franzke0c991632011-09-27 21:57:31 +02002638 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2639 if (d->rgb_ctx == NULL) {
2640 fprintf(stderr, "failed to create context\n");
2641 return -1;
2642 }
2643 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2644 EGL_NO_CONTEXT, NULL);
2645 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002646 fprintf(stderr, "failed to create context\n");
2647 return -1;
2648 }
2649
Benjamin Franzke0c991632011-09-27 21:57:31 +02002650 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002651 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002652 return -1;
2653 }
2654
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002655#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002656 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2657 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002658 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002659 return -1;
2660 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002661 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2662 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2663 fprintf(stderr, "failed to get cairo egl argb device\n");
2664 return -1;
2665 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002666#endif
2667
2668 return 0;
2669}
2670
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002671static void
2672fini_egl(struct display *display)
2673{
2674#ifdef HAVE_CAIRO_EGL
2675 cairo_device_destroy(display->argb_device);
2676 cairo_device_destroy(display->rgb_device);
2677#endif
2678
2679 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2680 EGL_NO_CONTEXT);
2681
2682 eglTerminate(display->dpy);
2683 eglReleaseThread();
2684}
2685
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002686static int
2687event_mask_update(uint32_t mask, void *data)
2688{
2689 struct display *d = data;
2690
2691 d->mask = mask;
2692
2693 return 0;
2694}
2695
2696static void
2697handle_display_data(struct task *task, uint32_t events)
2698{
2699 struct display *display =
2700 container_of(task, struct display, display_task);
2701
2702 wl_display_iterate(display->display, display->mask);
2703}
2704
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002705struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002706display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002707{
2708 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002709 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002710 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002711 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002712
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002713 g_type_init();
2714
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002715 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002716 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002717 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002718
2719 xkb_option_group = g_option_group_new("xkb",
2720 "Keyboard options",
2721 "Show all XKB options",
2722 NULL, NULL);
2723 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2724 g_option_context_add_group (context, xkb_option_group);
2725
2726 if (!g_option_context_parse(context, argc, argv, &error)) {
2727 fprintf(stderr, "option parsing failed: %s\n", error->message);
2728 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002729 }
2730
Tim Wiederhake748f6722011-01-23 23:25:25 +01002731 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002732
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002733 d = malloc(sizeof *d);
2734 if (d == NULL)
2735 return NULL;
2736
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002737 memset(d, 0, sizeof *d);
2738
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002739 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002740 if (d->display == NULL) {
2741 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002742 return NULL;
2743 }
2744
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002745 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2746 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2747 d->display_task.run = handle_display_data;
2748 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2749
2750 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002751 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002752 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002753
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002754 /* Set up listener so we'll catch all events. */
2755 wl_display_add_global_listener(d->display,
2756 display_handle_global, d);
2757
2758 /* Process connection events. */
2759 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002760 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002761 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002762
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002763 d->image_target_texture_2d =
2764 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2765 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2766 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2767
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002768 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002769
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002770 display_render_frame(d);
2771
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002772 wl_list_init(&d->window_list);
2773
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002774 init_xkb(d);
2775
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002776 return d;
2777}
2778
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002779static void
2780display_destroy_outputs(struct display *display)
2781{
2782 struct output *tmp;
2783 struct output *output;
2784
2785 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2786 output_destroy(output);
2787}
2788
Pekka Paalanene1207c72011-12-16 12:02:09 +02002789static void
2790display_destroy_inputs(struct display *display)
2791{
2792 struct input *tmp;
2793 struct input *input;
2794
2795 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2796 input_destroy(input);
2797}
2798
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002799void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002800display_destroy(struct display *display)
2801{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002802 if (!wl_list_empty(&display->window_list))
2803 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2804
2805 if (!wl_list_empty(&display->deferred_list))
2806 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2807
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002808 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002809 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002810
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002811 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002812
2813 cairo_surface_destroy(display->active_frame);
2814 cairo_surface_destroy(display->inactive_frame);
2815 cairo_surface_destroy(display->shadow);
2816 destroy_pointer_surfaces(display);
2817
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002818 fini_egl(display);
2819
Pekka Paalanenc2052982011-12-16 11:41:32 +02002820 if (display->shell)
2821 wl_shell_destroy(display->shell);
2822
2823 if (display->shm)
2824 wl_shm_destroy(display->shm);
2825
2826 if (display->data_device_manager)
2827 wl_data_device_manager_destroy(display->data_device_manager);
2828
2829 wl_compositor_destroy(display->compositor);
2830
2831 close(display->epoll_fd);
2832
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002833 wl_display_flush(display->display);
2834 wl_display_destroy(display->display);
2835 free(display);
2836}
2837
2838void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002839display_set_user_data(struct display *display, void *data)
2840{
2841 display->user_data = data;
2842}
2843
2844void *
2845display_get_user_data(struct display *display)
2846{
2847 return display->user_data;
2848}
2849
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002850struct wl_display *
2851display_get_display(struct display *display)
2852{
2853 return display->display;
2854}
2855
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002856struct output *
2857display_get_output(struct display *display)
2858{
2859 return container_of(display->output_list.next, struct output, link);
2860}
2861
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002862struct wl_compositor *
2863display_get_compositor(struct display *display)
2864{
2865 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002866}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002867
2868EGLDisplay
2869display_get_egl_display(struct display *d)
2870{
2871 return d->dpy;
2872}
2873
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002874struct wl_data_source *
2875display_create_data_source(struct display *display)
2876{
2877 return wl_data_device_manager_create_data_source(display->data_device_manager);
2878}
2879
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002880EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002881display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002882{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002883 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002884}
2885
Benjamin Franzke0c991632011-09-27 21:57:31 +02002886EGLConfig
2887display_get_argb_egl_config(struct display *d)
2888{
2889 return d->premultiplied_argb_config;
2890}
2891
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002892struct wl_shell *
2893display_get_shell(struct display *display)
2894{
2895 return display->shell;
2896}
2897
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002898int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002899display_acquire_window_surface(struct display *display,
2900 struct window *window,
2901 EGLContext ctx)
2902{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002903#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002904 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002905 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002906
2907 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002908 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002909 device = cairo_surface_get_device(window->cairo_surface);
2910 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002911 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002912
Benjamin Franzke0c991632011-09-27 21:57:31 +02002913 if (!ctx) {
2914 if (device == display->rgb_device)
2915 ctx = display->rgb_ctx;
2916 else if (device == display->argb_device)
2917 ctx = display->argb_ctx;
2918 else
2919 assert(0);
2920 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002921
2922 data = cairo_surface_get_user_data(window->cairo_surface,
2923 &surface_data_key);
2924
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002925 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002926 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002927 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2928 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002929
2930 return 0;
2931#else
2932 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002933#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002934}
2935
2936void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002937display_release_window_surface(struct display *display,
2938 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002939{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002940#ifdef HAVE_CAIRO_EGL
2941 cairo_device_t *device;
2942
2943 device = cairo_surface_get_device(window->cairo_surface);
2944 if (!device)
2945 return;
2946
2947 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002948 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002949 cairo_device_release(device);
2950#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002951}
2952
2953void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002954display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002955{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002956 wl_list_insert(&display->deferred_list, &task->link);
2957}
2958
2959void
2960display_watch_fd(struct display *display,
2961 int fd, uint32_t events, struct task *task)
2962{
2963 struct epoll_event ep;
2964
2965 ep.events = events;
2966 ep.data.ptr = task;
2967 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2968}
2969
2970void
2971display_run(struct display *display)
2972{
2973 struct task *task;
2974 struct epoll_event ep[16];
2975 int i, count;
2976
Pekka Paalanen826d7952011-12-15 10:14:07 +02002977 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002978 while (1) {
2979 while (display->mask & WL_DISPLAY_WRITABLE)
2980 wl_display_iterate(display->display,
2981 WL_DISPLAY_WRITABLE);
2982
Pekka Paalanen826d7952011-12-15 10:14:07 +02002983 if (!display->running)
2984 break;
2985
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002986 count = epoll_wait(display->epoll_fd,
2987 ep, ARRAY_LENGTH(ep), -1);
2988 for (i = 0; i < count; i++) {
2989 task = ep[i].data.ptr;
2990 task->run(task, ep[i].events);
2991 }
2992
2993 while (!wl_list_empty(&display->deferred_list)) {
2994 task = container_of(display->deferred_list.next,
2995 struct task, link);
2996 wl_list_remove(&task->link);
2997 task->run(task, 0);
2998 }
2999 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003000}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003001
3002void
3003display_exit(struct display *display)
3004{
3005 display->running = 0;
3006}