blob: 492d9517d69e3a2d99fac9dd49ee7a5e25ca0e93 [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øgsbergb67e94b2012-01-10 12:23:19 -0500118 int resize_scheduled;
119 struct task resize_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500121 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400122 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400123 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400124 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500126 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400127 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500128
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500129 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500130
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500132 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400133 window_data_handler_t data_handler;
134 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500135 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400136
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500137 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500138 struct widget *focus_widget;
139 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400140
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500141 struct window *menu;
142
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500143 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400144 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500145};
146
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500147struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500148 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500149 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400150 struct wl_list link;
151 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500152 widget_resize_handler_t resize_handler;
153 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500154 widget_enter_handler_t enter_handler;
155 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500156 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500157 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400158 void *user_data;
159};
160
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400161struct input {
162 struct display *display;
163 struct wl_input_device *input_device;
164 struct window *pointer_focus;
165 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400166 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400167 uint32_t modifiers;
168 int32_t x, y, sx, sy;
169 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400170
171 struct wl_data_device *data_device;
172 struct data_offer *drag_offer;
173 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174};
175
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500176struct output {
177 struct display *display;
178 struct wl_output *output;
179 struct rectangle allocation;
180 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200181
182 display_output_handler_t destroy_handler;
183 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500184};
185
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500186struct menu {
187 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500188 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500189 const char **entries;
190 uint32_t time;
191 int current;
192 int count;
193 menu_func_t func;
194};
195
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400196enum {
197 POINTER_DEFAULT = 100,
198 POINTER_UNSET
199};
200
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500201enum window_location {
202 WINDOW_INTERIOR = 0,
203 WINDOW_RESIZING_TOP = 1,
204 WINDOW_RESIZING_BOTTOM = 2,
205 WINDOW_RESIZING_LEFT = 4,
206 WINDOW_RESIZING_TOP_LEFT = 5,
207 WINDOW_RESIZING_BOTTOM_LEFT = 6,
208 WINDOW_RESIZING_RIGHT = 8,
209 WINDOW_RESIZING_TOP_RIGHT = 9,
210 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
211 WINDOW_RESIZING_MASK = 15,
212 WINDOW_EXTERIOR = 16,
213 WINDOW_TITLEBAR = 17,
214 WINDOW_CLIENT_AREA = 18,
215};
216
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400217const char *option_xkb_layout = "us";
218const char *option_xkb_variant = "";
219const char *option_xkb_options = "";
220
221static const GOptionEntry xkb_option_entries[] = {
222 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
223 &option_xkb_layout, "XKB Layout" },
224 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
225 &option_xkb_variant, "XKB Variant" },
226 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
227 &option_xkb_options, "XKB Options" },
228 { NULL }
229};
230
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400231static const cairo_user_data_key_t surface_data_key;
232struct surface_data {
233 struct wl_buffer *buffer;
234};
235
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500236#define MULT(_d,c,a,t) \
237 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
238
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500239#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400240
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100241struct egl_window_surface_data {
242 struct display *display;
243 struct wl_surface *surface;
244 struct wl_egl_window *window;
245 EGLSurface surf;
246};
247
248static void
249egl_window_surface_data_destroy(void *p)
250{
251 struct egl_window_surface_data *data = p;
252 struct display *d = data->display;
253
254 eglDestroySurface(d->dpy, data->surf);
255 wl_egl_window_destroy(data->window);
256 data->surface = NULL;
257
258 free(p);
259}
260
261static cairo_surface_t *
262display_create_egl_window_surface(struct display *display,
263 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400264 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100265 struct rectangle *rectangle)
266{
267 cairo_surface_t *cairo_surface;
268 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400269 EGLConfig config;
270 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200271 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100272
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 static const EGLint premul_attribs[] = {
274 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
275 EGL_NONE
276 };
277
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278 data = malloc(sizeof *data);
279 if (data == NULL)
280 return NULL;
281
282 data->display = display;
283 data->surface = surface;
284
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400285 if (flags & SURFACE_OPAQUE) {
286 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200287 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200288 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 } else {
290 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400292 attribs = premul_attribs;
293 }
294
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400295 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400299 data->surf = eglCreateWindowSurface(display->dpy, config,
300 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Benjamin Franzke0c991632011-09-27 21:57:31 +0200302 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100303 data->surf,
304 rectangle->width,
305 rectangle->height);
306
307 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
308 data, egl_window_surface_data_destroy);
309
310 return cairo_surface;
311}
312
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200315 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 EGLImageKHR image;
317 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800318 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320};
321
322static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400324{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500325 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327
Benjamin Franzke0c991632011-09-27 21:57:31 +0200328 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200330 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800331
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500332 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500334 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336}
337
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339display_get_image_for_egl_image_surface(struct display *display,
340 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500343
344 data = cairo_surface_get_user_data (surface, &surface_data_key);
345
346 return data->image;
347}
348
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500349static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400351 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355 EGLDisplay dpy = display->dpy;
356 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200357 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358
359 data = malloc(sizeof *data);
360 if (data == NULL)
361 return NULL;
362
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800363 data->display = display;
364
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400365 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500367 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000368 free(data);
369 return NULL;
370 }
371
Benjamin Franzke0c991632011-09-27 21:57:31 +0200372 if (flags & SURFACE_OPAQUE) {
373 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 content = CAIRO_CONTENT_COLOR;
375 } else {
376 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200377 content = CAIRO_CONTENT_COLOR_ALPHA;
378 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500380 data->image = display->create_image(dpy, NULL,
381 EGL_NATIVE_PIXMAP_KHR,
382 (EGLClientBuffer) data->pixmap,
383 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500385 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500386 free(data);
387 return NULL;
388 }
389
390 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400391 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500396 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398
Benjamin Franzke0c991632011-09-27 21:57:31 +0200399 surface = cairo_gl_surface_create_for_texture(data->device,
400 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401 data->texture,
402 rectangle->width,
403 rectangle->height);
404
405 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500406 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407
408 return surface;
409}
410
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500411static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500412display_create_egl_image_surface_from_file(struct display *display,
413 const char *filename,
414 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415{
416 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417 GdkPixbuf *pixbuf;
418 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400419 int stride, i;
420 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500421 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400422
423 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425 FALSE, &error);
426 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400428
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
430 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500431 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400432 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400434
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400435
436 stride = gdk_pixbuf_get_rowstride(pixbuf);
437 pixels = gdk_pixbuf_get_pixels(pixbuf);
438
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400440 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400441 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400442 while (p < end) {
443 unsigned int t;
444
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400445 MULT(p[0], p[0], p[3], t);
446 MULT(p[1], p[1], p[3], t);
447 MULT(p[2], p[2], p[3], t);
448 p += 4;
449
450 }
451 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400452
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200453 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000454 if (surface == NULL) {
455 g_object_unref(pixbuf);
456 return NULL;
457 }
458
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800459 data = cairo_surface_get_user_data(surface, &surface_data_key);
460
Benjamin Franzke0c991632011-09-27 21:57:31 +0200461 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800462 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800463 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
464 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200465 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400466
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500467 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400468
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469 return surface;
470}
471
472#endif
473
474struct wl_buffer *
475display_get_buffer_for_surface(struct display *display,
476 cairo_surface_t *surface)
477{
478 struct surface_data *data;
479
480 data = cairo_surface_get_user_data (surface, &surface_data_key);
481
482 return data->buffer;
483}
484
485struct shm_surface_data {
486 struct surface_data data;
487 void *map;
488 size_t length;
489};
490
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500491static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492shm_surface_data_destroy(void *p)
493{
494 struct shm_surface_data *data = p;
495
496 wl_buffer_destroy(data->data.buffer);
497 munmap(data->map, data->length);
498}
499
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500500static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503{
504 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400505 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400506 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800507 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508 char filename[] = "/tmp/wayland-shm-XXXXXX";
509
510 data = malloc(sizeof *data);
511 if (data == NULL)
512 return NULL;
513
514 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
515 rectangle->width);
516 data->length = stride * rectangle->height;
517 fd = mkstemp(filename);
518 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000519 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520 return NULL;
521 }
522 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000523 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 close(fd);
525 return NULL;
526 }
527
528 data->map = mmap(NULL, data->length,
529 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
530 unlink(filename);
531
532 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000533 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534 close(fd);
535 return NULL;
536 }
537
538 surface = cairo_image_surface_create_for_data (data->map,
539 CAIRO_FORMAT_ARGB32,
540 rectangle->width,
541 rectangle->height,
542 stride);
543
544 cairo_surface_set_user_data (surface, &surface_data_key,
545 data, shm_surface_data_destroy);
546
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400548 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400550 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400552 data->data.buffer = wl_shm_create_buffer(display->shm,
553 fd,
554 rectangle->width,
555 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
558 close(fd);
559
560 return surface;
561}
562
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500563static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400564display_create_shm_surface_from_file(struct display *display,
565 const char *filename,
566 struct rectangle *rect)
567{
568 cairo_surface_t *surface;
569 GdkPixbuf *pixbuf;
570 GError *error = NULL;
571 int stride, i;
572 unsigned char *pixels, *p, *end, *dest_data;
573 int dest_stride;
574 uint32_t *d;
575
576 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
577 rect->width, rect->height,
578 FALSE, &error);
579 if (error != NULL)
580 return NULL;
581
582 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
583 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500584 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585 return NULL;
586 }
587
588 stride = gdk_pixbuf_get_rowstride(pixbuf);
589 pixels = gdk_pixbuf_get_pixels(pixbuf);
590
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400591 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000592 if (surface == NULL) {
593 g_object_unref(pixbuf);
594 return NULL;
595 }
596
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597 dest_data = cairo_image_surface_get_data (surface);
598 dest_stride = cairo_image_surface_get_stride (surface);
599
600 for (i = 0; i < rect->height; i++) {
601 d = (uint32_t *) (dest_data + i * dest_stride);
602 p = pixels + i * stride;
603 end = p + rect->width * 4;
604 while (p < end) {
605 unsigned int t;
606 unsigned char a, r, g, b;
607
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608 a = p[3];
609 MULT(r, p[0], a, t);
610 MULT(g, p[1], a, t);
611 MULT(b, p[2], a, t);
612 p += 4;
613 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
614 }
615 }
616
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500617 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618
619 return surface;
620}
621
nobled7b87cb02011-02-01 18:51:47 +0000622static int
623check_size(struct rectangle *rect)
624{
625 if (rect->width && rect->height)
626 return 0;
627
628 fprintf(stderr, "tried to create surface of "
629 "width: %d, height: %d\n", rect->width, rect->height);
630 return -1;
631}
632
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633cairo_surface_t *
634display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400636 struct rectangle *rectangle,
637 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638{
nobled7b87cb02011-02-01 18:51:47 +0000639 if (check_size(rectangle) < 0)
640 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500642 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643 if (surface)
644 return display_create_egl_window_surface(display,
645 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400646 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 rectangle);
648 else
649 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400650 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500652 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400653#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400654 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655}
656
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500657static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658display_create_surface_from_file(struct display *display,
659 const char *filename,
660 struct rectangle *rectangle)
661{
nobled7b87cb02011-02-01 18:51:47 +0000662 if (check_size(rectangle) < 0)
663 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500664#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500665 if (display->dpy) {
666 return display_create_egl_image_surface_from_file(display,
667 filename,
668 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500671 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672}
Yuval Fledel45568f62010-12-06 09:18:12 -0500673 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674 const char *filename;
675 int hotspot_x, hotspot_y;
676} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500677 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
678 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
679 { DATADIR "/weston/bottom_side.png", 16, 20 },
680 { DATADIR "/weston/grabbing.png", 20, 17 },
681 { DATADIR "/weston/left_ptr.png", 10, 5 },
682 { DATADIR "/weston/left_side.png", 10, 20 },
683 { DATADIR "/weston/right_side.png", 30, 19 },
684 { DATADIR "/weston/top_left_corner.png", 8, 8 },
685 { DATADIR "/weston/top_right_corner.png", 26, 8 },
686 { DATADIR "/weston/top_side.png", 18, 8 },
687 { DATADIR "/weston/xterm.png", 15, 15 },
688 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689};
690
691static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693{
694 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400698 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400699 display->pointer_surfaces =
700 malloc(count * sizeof *display->pointer_surfaces);
701 rect.width = width;
702 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400703 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400704 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705 display_create_surface_from_file(display,
706 pointer_images[i].filename,
707 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100708 if (!display->pointer_surfaces[i]) {
709 fprintf(stderr, "Error loading pointer image: %s\n",
710 pointer_images[i].filename);
711 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400712 }
713
714}
715
Pekka Paalanen325bb602011-12-19 10:31:45 +0200716static void
717destroy_pointer_surfaces(struct display *display)
718{
719 int i, count;
720
721 count = ARRAY_LENGTH(pointer_images);
722 for (i = 0; i < count; ++i) {
723 if (display->pointer_surfaces[i])
724 cairo_surface_destroy(display->pointer_surfaces[i]);
725 }
726 free(display->pointer_surfaces);
727}
728
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400729cairo_surface_t *
730display_get_pointer_surface(struct display *display, int pointer,
731 int *width, int *height,
732 int *hotspot_x, int *hotspot_y)
733{
734 cairo_surface_t *surface;
735
736 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500737#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400738 *width = cairo_gl_surface_get_width(surface);
739 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000740#else
741 *width = cairo_image_surface_get_width(surface);
742 *height = cairo_image_surface_get_height(surface);
743#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400744 *hotspot_x = pointer_images[pointer].hotspot_x;
745 *hotspot_y = pointer_images[pointer].hotspot_y;
746
747 return cairo_surface_reference(surface);
748}
749
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400750static void
751window_attach_surface(struct window *window);
752
753static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400755{
756 struct window *window = data;
757
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400758 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400759 cairo_surface_destroy(window->pending_surface);
760 window->pending_surface = NULL;
761 if (window->cairo_surface)
762 window_attach_surface(window);
763}
764
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400765static const struct wl_callback_listener free_surface_listener = {
766 free_surface
767};
768
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500769static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200770window_get_resize_dx_dy(struct window *window, int *x, int *y)
771{
772 if (window->resize_edges & WINDOW_RESIZING_LEFT)
773 *x = window->server_allocation.width - window->allocation.width;
774 else
775 *x = 0;
776
777 if (window->resize_edges & WINDOW_RESIZING_TOP)
778 *y = window->server_allocation.height -
779 window->allocation.height;
780 else
781 *y = 0;
782
783 window->resize_edges = 0;
784}
785
786static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400787window_set_type(struct window *window)
788{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200789 if (!window->shell_surface)
790 return;
791
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400792 switch (window->type) {
793 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200794 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400795 break;
796 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400798 break;
799 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200800 wl_shell_surface_set_transient(window->shell_surface,
801 window->parent->shell_surface,
802 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400803 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500804 case TYPE_MENU:
805 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400806 case TYPE_CUSTOM:
807 break;
808 }
809}
810
811static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500812window_attach_surface(struct window *window)
813{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400814 struct display *display = window->display;
815 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400816 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000817#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000819#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500820 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400822 if (display->shell)
823 window_set_type(window);
824
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000826#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
828 data = cairo_surface_get_user_data(window->cairo_surface,
829 &surface_data_key);
830
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 cairo_gl_surface_swapbuffers(window->cairo_surface);
832 wl_egl_window_get_attached_size(data->window,
833 &window->server_allocation.width,
834 &window->server_allocation.height);
835 break;
836 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000837#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200839 window_get_resize_dx_dy(window, &x, &y);
840
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 if (window->pending_surface != NULL)
842 return;
843
844 window->pending_surface = window->cairo_surface;
845 window->cairo_surface = NULL;
846
847 buffer =
848 display_get_buffer_for_surface(display,
849 window->pending_surface);
850
851 wl_surface_attach(window->surface, buffer, x, y);
852 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400853 cb = wl_display_sync(display->display);
854 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000856 default:
857 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100858 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500859
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500860 wl_surface_damage(window->surface, 0, 0,
861 window->allocation.width,
862 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500863}
864
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500865void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400866window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500867{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100868 if (window->cairo_surface) {
869 switch (window->buffer_type) {
870 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
871 case WINDOW_BUFFER_TYPE_SHM:
872 display_surface_damage(window->display,
873 window->cairo_surface,
874 0, 0,
875 window->allocation.width,
876 window->allocation.height);
877 break;
878 default:
879 break;
880 }
881 window_attach_surface(window);
882 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500883}
884
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400885void
886window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400887{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500888 cairo_surface_reference(surface);
889
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890 if (window->cairo_surface != NULL)
891 cairo_surface_destroy(window->cairo_surface);
892
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500893 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894}
895
Benjamin Franzke22d54812011-07-16 19:50:32 +0000896#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897static void
898window_resize_cairo_window_surface(struct window *window)
899{
900 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200901 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902
903 data = cairo_surface_get_user_data(window->cairo_surface,
904 &surface_data_key);
905
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200906 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100907 wl_egl_window_resize(data->window,
908 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200909 window->allocation.height,
910 x,y);
911
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912 cairo_gl_surface_set_size(window->cairo_surface,
913 window->allocation.width,
914 window->allocation.height);
915}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000916#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100917
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400918struct display *
919window_get_display(struct window *window)
920{
921 return window->display;
922}
923
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400924void
925window_create_surface(struct window *window)
926{
927 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400928 uint32_t flags = 0;
929
930 if (!window->transparent)
931 flags = SURFACE_OPAQUE;
932
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500934#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100935 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
936 if (window->cairo_surface) {
937 window_resize_cairo_window_surface(window);
938 return;
939 }
940 surface = display_create_surface(window->display,
941 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400942 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100943 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500944 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400945 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100946 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400947 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400948 break;
949#endif
950 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400951 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400952 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800954 default:
955 surface = NULL;
956 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400958
959 window_set_surface(window, surface);
960 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961}
962
963static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500964window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500965{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500966 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500967 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400968 cairo_surface_t *frame;
969 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500970
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 width = window->allocation.width;
972 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500973
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500974 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500975
Kristian Høgsberg09531622010-06-14 23:22:15 -0400976 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400977 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400978 cairo_paint(cr);
979
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400980 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400981 tile_mask(cr, window->display->shadow,
982 shadow_dx, shadow_dy, width, height,
983 window->margin + 10 - shadow_dx,
984 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500985
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400986 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400988 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400989 frame = window->display->inactive_frame;
990
991 tile_source(cr, frame, 0, 0, width, height,
992 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500993
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500994 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
995 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500996 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400997 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400998 if (window->keyboard_device)
999 cairo_set_source_rgb(cr, 0, 0, 0);
1000 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001001 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001002 cairo_show_text(cr, window->title);
1003
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001004 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001005}
1006
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001007void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001008window_destroy(struct window *window)
1009{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001010 struct display *display = window->display;
1011 struct input *input;
1012
1013 if (window->redraw_scheduled)
1014 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001015 if (window->resize_scheduled)
1016 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001017
1018 wl_list_for_each(input, &display->input_list, link) {
1019 if (input->pointer_focus == window)
1020 input->pointer_focus = NULL;
1021 if (input->keyboard_focus == window)
1022 input->keyboard_focus = NULL;
1023 }
1024
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001025 if (window->shell_surface)
1026 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001027 wl_surface_destroy(window->surface);
1028 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001029
1030 if (window->cairo_surface != NULL)
1031 cairo_surface_destroy(window->cairo_surface);
1032 if (window->pending_surface != NULL)
1033 cairo_surface_destroy(window->pending_surface);
1034
1035 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001036 free(window);
1037}
1038
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001040widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001042 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001043
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001044 wl_list_for_each(child, &widget->child_list, link) {
1045 target = widget_find_widget(child, x, y);
1046 if (target)
1047 return target;
1048 }
1049
1050 if (widget->allocation.x <= x &&
1051 x < widget->allocation.x + widget->allocation.width &&
1052 widget->allocation.y <= y &&
1053 y < widget->allocation.y + widget->allocation.height) {
1054 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055 }
1056
1057 return NULL;
1058}
1059
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001060static struct widget *
1061widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001065 widget = malloc(sizeof *widget);
1066 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001067 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001069 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001070 wl_list_init(&widget->child_list);
1071
1072 return widget;
1073}
1074
1075struct widget *
1076window_add_widget(struct window *window, void *data)
1077{
1078 window->widget = widget_create(window, data);
1079 wl_list_init(&window->widget->link);
1080
1081 return window->widget;
1082}
1083
1084struct widget *
1085widget_add_widget(struct widget *parent, void *data)
1086{
1087 struct widget *widget;
1088
1089 widget = widget_create(parent->window, data);
1090 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
1095void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001096widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001098 wl_list_remove(&widget->link);
1099 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100}
1101
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102struct widget *
1103window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001104{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001105 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001106}
1107
1108void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001109widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001110{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001111 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001112}
1113
1114void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001115widget_set_size(struct widget *widget, int32_t width, int32_t height)
1116{
1117 struct window *window = widget->window;
1118
1119 widget->allocation.width = width;
1120 widget->allocation.height = height;
1121
1122 window->allocation.x = 0;
1123 window->allocation.y = 0;
1124 if (widget->window->decoration) {
1125 window->allocation.width = width + 20 + widget->window->margin;
1126 window->allocation.height =
1127 height + 60 + widget->window->margin;
1128 } else {
1129 window->allocation.width = width;
1130 window->allocation.height = height;
1131 }
1132}
1133
1134void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001135widget_set_allocation(struct widget *widget,
1136 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001137{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001138 widget->allocation.x = x;
1139 widget->allocation.y = y;
1140 widget->allocation.width = width;
1141 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001142}
1143
1144void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001145widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001146{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001147 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001148}
1149
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001150void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001151widget_set_resize_handler(struct widget *widget,
1152 widget_resize_handler_t handler)
1153{
1154 widget->resize_handler = handler;
1155}
1156
1157void
1158widget_set_redraw_handler(struct widget *widget,
1159 widget_redraw_handler_t handler)
1160{
1161 widget->redraw_handler = handler;
1162}
1163
1164void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001165widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001166{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001167 widget->enter_handler = handler;
1168}
1169
1170void
1171widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1172{
1173 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001174}
1175
1176void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001177widget_set_motion_handler(struct widget *widget,
1178 widget_motion_handler_t handler)
1179{
1180 widget->motion_handler = handler;
1181}
1182
1183void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001184widget_set_button_handler(struct widget *widget,
1185 widget_button_handler_t handler)
1186{
1187 widget->button_handler = handler;
1188}
1189
1190void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001191widget_schedule_redraw(struct widget *widget)
1192{
1193 window_schedule_redraw(widget->window);
1194}
1195
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001196cairo_surface_t *
1197window_get_surface(struct window *window)
1198{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001199 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001200}
1201
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001202struct wl_surface *
1203window_get_wl_surface(struct window *window)
1204{
1205 return window->surface;
1206}
1207
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001208struct wl_shell_surface *
1209window_get_wl_shell_surface(struct window *window)
1210{
1211 return window->shell_surface;
1212}
1213
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001214static int
1215get_pointer_location(struct window *window, int32_t x, int32_t y)
1216{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001217 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001218 const int grip_size = 8;
1219
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001220 if (!window->decoration)
1221 return WINDOW_CLIENT_AREA;
1222
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001224 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 else if (window->margin <= x && x < window->margin + grip_size)
1226 hlocation = WINDOW_RESIZING_LEFT;
1227 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229 else if (x < window->allocation.width - window->margin)
1230 hlocation = WINDOW_RESIZING_RIGHT;
1231 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001232 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001233
1234 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236 else if (window->margin <= y && y < window->margin + grip_size)
1237 vlocation = WINDOW_RESIZING_TOP;
1238 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001239 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240 else if (y < window->allocation.height - window->margin)
1241 vlocation = WINDOW_RESIZING_BOTTOM;
1242 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001243 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001244
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245 location = vlocation | hlocation;
1246 if (location & WINDOW_EXTERIOR)
1247 location = WINDOW_EXTERIOR;
1248 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1249 location = WINDOW_TITLEBAR;
1250 else if (location == WINDOW_INTERIOR)
1251 location = WINDOW_CLIENT_AREA;
1252
1253 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254}
1255
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001256static int
1257input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260
1261 location = get_pointer_location(input->pointer_focus,
1262 input->sx, input->sy);
1263 switch (location) {
1264 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001265 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001267 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001269 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001271 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001273 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001275 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001277 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001279 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001280 case WINDOW_EXTERIOR:
1281 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001284 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286}
1287
1288void
1289input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1290{
1291 struct display *display = input->display;
1292 struct wl_buffer *buffer;
1293 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001295 if (pointer == input->current_pointer_image)
1296 return;
1297
1298 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001299 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001300
1301 if (!surface)
1302 return;
1303
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001304 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001305 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001306 pointer_images[pointer].hotspot_x,
1307 pointer_images[pointer].hotspot_y);
1308}
1309
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001310static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001311window_set_focus_widget(struct window *window, struct widget *focus,
1312 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001313{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001314 struct widget *old;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001315 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001316
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001317 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001318 return;
1319
Kristian Høgsbergee143232012-01-09 08:42:24 -05001320 old = window->focus_widget;
1321 if (old) {
1322 if (old->leave_handler)
1323 old->leave_handler(old, input, old->user_data);
1324 window->focus_widget = NULL;
1325 }
1326
1327 if (focus) {
1328 if (focus->enter_handler)
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001329 pointer = focus->enter_handler(focus, input, time,
1330 x, y, focus->user_data);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001331 window->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001332
1333 pointer = input_get_pointer_image_for_location(input, pointer);
1334 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001335 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001336}
1337
1338static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001339input_handle_motion(void *data, struct wl_input_device *input_device,
1340 uint32_t time,
1341 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001342{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001343 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001344 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001345 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001346 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001347
1348 input->x = x;
1349 input->y = y;
1350 input->sx = sx;
1351 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001352
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001353 if (!window->focus_widget || !window->widget_grab_button) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001354 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001355 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001356 }
1357
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001358 widget = window->focus_widget;
1359 if (widget && widget->motion_handler)
1360 pointer = widget->motion_handler(widget, input, time, sx, sy,
1361 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001362
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001363 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001364 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001365}
1366
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001367static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001368window_menu_func(struct window *window, int index, void *data)
1369{
1370 switch (index) {
1371 case 0: /* close */
1372 if (window->close_handler)
1373 window->close_handler(window->parent,
1374 window->user_data);
1375 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001376 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001377 break;
1378 case 1: /* fullscreen */
1379 /* we don't have a way to get out of fullscreen for now */
1380 window_set_fullscreen(window, 1);
1381 break;
1382 case 2: /* rotate */
1383 case 3: /* scale */
1384 break;
1385 }
1386}
1387
Kristian Høgsberg27ff0862012-01-10 14:28:58 -05001388static void
1389break_grab(struct window *window)
1390{
1391 window->focus_widget = NULL;
1392 window->widget_grab_button = 0;
1393}
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001394
1395static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001396input_handle_button(void *data,
1397 struct wl_input_device *input_device,
1398 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001399{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001400 struct input *input = data;
1401 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001402 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001403 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001404 int32_t x, y;
1405 static const char *entries[] = {
1406 "Close", "Fullscreen", "Rotate", "Scale"
1407 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001408
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001409 if (window->focus_widget && window->widget_grab_button == 0 && state)
1410 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001411
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001412 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001413
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001414 widget = window->focus_widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001415 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001416 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001417 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001418 if (!window->shell_surface)
1419 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001420 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg27ff0862012-01-10 14:28:58 -05001421 break_grab(window);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001422 wl_shell_surface_move(window->shell_surface,
1423 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001424 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001425 case WINDOW_RESIZING_TOP:
1426 case WINDOW_RESIZING_BOTTOM:
1427 case WINDOW_RESIZING_LEFT:
1428 case WINDOW_RESIZING_RIGHT:
1429 case WINDOW_RESIZING_TOP_LEFT:
1430 case WINDOW_RESIZING_TOP_RIGHT:
1431 case WINDOW_RESIZING_BOTTOM_LEFT:
1432 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001433 if (!window->shell_surface)
1434 break;
Kristian Høgsberg27ff0862012-01-10 14:28:58 -05001435 break_grab(window);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001436 wl_shell_surface_resize(window->shell_surface,
1437 input_device, time,
1438 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001439 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001440 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001441 if (widget && widget->button_handler)
1442 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001443 input, time,
1444 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001445 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001447 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001448 } else if (button == BTN_RIGHT && state == 1) {
1449 switch (location) {
1450 default:
1451 input_get_position(input, &x, &y);
1452 window->menu = window_create_menu(window->display,
1453 input, time,
1454 window,
1455 x - 10, y - 10,
1456 window_menu_func,
1457 entries, 4);
1458 window_schedule_redraw(window->menu);
1459 break;
1460 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001461 if (widget && widget->button_handler)
1462 (*widget->button_handler)(widget,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001463 input, time,
1464 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001465 widget->user_data);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001466 break;
1467 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001468 } else {
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001469 if (widget && widget->button_handler)
1470 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001471 input, time,
1472 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001473 widget->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001474 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001475
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001476 if (window->focus_widget &&
1477 window->widget_grab_button == button && !state) {
1478 window->widget_grab_button = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001479 widget = widget_find_widget(window->widget,
1480 input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001481 window_set_focus_widget(window, widget, input, time,
1482 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001483 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001484}
1485
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001486static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001487input_handle_key(void *data, struct wl_input_device *input_device,
1488 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001489{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001490 struct input *input = data;
1491 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001492 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001493 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001494
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001495 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001496 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001497 return;
1498
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001499 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001500 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001501 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1502 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001503
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001504 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1505
1506 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001507 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001508 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001509 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001510
1511 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001512 (*window->key_handler)(window, input, time, key, sym, state,
1513 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001514}
1515
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001516static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001517input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001518{
1519 struct window *window = input->pointer_focus;
1520
1521 if (!window)
1522 return;
1523
Kristian Høgsbergee143232012-01-09 08:42:24 -05001524 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001525
Pekka Paalanene1207c72011-12-16 12:02:09 +02001526 input->pointer_focus = NULL;
1527 input->current_pointer_image = POINTER_UNSET;
1528}
1529
1530static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001531input_handle_pointer_focus(void *data,
1532 struct wl_input_device *input_device,
1533 uint32_t time, struct wl_surface *surface,
1534 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001535{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001536 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001537 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001538 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001539 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001540
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001541 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001542 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001543 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001544
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001545 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001546 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001547 window = input->pointer_focus;
1548
Kristian Høgsberg59826582011-01-20 11:56:57 -05001549 input->x = x;
1550 input->y = y;
1551 input->sx = sx;
1552 input->sy = sy;
1553
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001554 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001555 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001556 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001557
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001558 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001559 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001560 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001561}
1562
1563static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001564input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001565{
1566 struct window *window = input->keyboard_focus;
1567
1568 if (!window)
1569 return;
1570
1571 window->keyboard_device = NULL;
1572 if (window->keyboard_focus_handler)
1573 (*window->keyboard_focus_handler)(window, NULL,
1574 window->user_data);
1575
1576 input->keyboard_focus = NULL;
1577}
1578
1579static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001580input_handle_keyboard_focus(void *data,
1581 struct wl_input_device *input_device,
1582 uint32_t time,
1583 struct wl_surface *surface,
1584 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001585{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001586 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001587 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001588 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001589 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001590
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001591 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001592
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001593 if (surface)
1594 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001595
1596 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001597 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001598 for (k = keys->data; k < end; k++)
1599 input->modifiers |= d->xkb->map->modmap[*k];
1600
1601 window = input->keyboard_focus;
1602 if (window) {
1603 window->keyboard_device = input;
1604 if (window->keyboard_focus_handler)
1605 (*window->keyboard_focus_handler)(window,
1606 window->keyboard_device,
1607 window->user_data);
1608 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001609}
1610
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001611static void
1612input_handle_touch_down(void *data,
1613 struct wl_input_device *wl_input_device,
1614 uint32_t time, struct wl_surface *surface,
1615 int32_t id, int32_t x, int32_t y)
1616{
1617}
1618
1619static void
1620input_handle_touch_up(void *data,
1621 struct wl_input_device *wl_input_device,
1622 uint32_t time, int32_t id)
1623{
1624}
1625
1626static void
1627input_handle_touch_motion(void *data,
1628 struct wl_input_device *wl_input_device,
1629 uint32_t time, int32_t id, int32_t x, int32_t y)
1630{
1631}
1632
1633static void
1634input_handle_touch_frame(void *data,
1635 struct wl_input_device *wl_input_device)
1636{
1637}
1638
1639static void
1640input_handle_touch_cancel(void *data,
1641 struct wl_input_device *wl_input_device)
1642{
1643}
1644
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001645static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001646 input_handle_motion,
1647 input_handle_button,
1648 input_handle_key,
1649 input_handle_pointer_focus,
1650 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001651 input_handle_touch_down,
1652 input_handle_touch_up,
1653 input_handle_touch_motion,
1654 input_handle_touch_frame,
1655 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001656};
1657
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001658void
1659input_get_position(struct input *input, int32_t *x, int32_t *y)
1660{
1661 *x = input->sx;
1662 *y = input->sy;
1663}
1664
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001665struct wl_input_device *
1666input_get_input_device(struct input *input)
1667{
1668 return input->input_device;
1669}
1670
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001671uint32_t
1672input_get_modifiers(struct input *input)
1673{
1674 return input->modifiers;
1675}
1676
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001677struct data_offer {
1678 struct wl_data_offer *offer;
1679 struct input *input;
1680 struct wl_array types;
1681 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001682
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001683 struct task io_task;
1684 int fd;
1685 data_func_t func;
1686 int32_t x, y;
1687 void *user_data;
1688};
1689
1690static void
1691data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1692{
1693 struct data_offer *offer = data;
1694 char **p;
1695
1696 p = wl_array_add(&offer->types, sizeof *p);
1697 *p = strdup(type);
1698}
1699
1700static const struct wl_data_offer_listener data_offer_listener = {
1701 data_offer_offer,
1702};
1703
1704static void
1705data_offer_destroy(struct data_offer *offer)
1706{
1707 char **p;
1708
1709 offer->refcount--;
1710 if (offer->refcount == 0) {
1711 wl_data_offer_destroy(offer->offer);
1712 for (p = offer->types.data; *p; p++)
1713 free(*p);
1714 wl_array_release(&offer->types);
1715 free(offer);
1716 }
1717}
1718
1719static void
1720data_device_data_offer(void *data,
1721 struct wl_data_device *data_device, uint32_t id)
1722{
1723 struct data_offer *offer;
1724
1725 offer = malloc(sizeof *offer);
1726
1727 wl_array_init(&offer->types);
1728 offer->refcount = 1;
1729 offer->input = data;
1730
1731 /* FIXME: Generate typesafe wrappers for this */
1732 offer->offer = (struct wl_data_offer *)
1733 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1734 id, &wl_data_offer_interface);
1735
1736 wl_data_offer_add_listener(offer->offer,
1737 &data_offer_listener, offer);
1738}
1739
1740static void
1741data_device_enter(void *data, struct wl_data_device *data_device,
1742 uint32_t time, struct wl_surface *surface,
1743 int32_t x, int32_t y, struct wl_data_offer *offer)
1744{
1745 struct input *input = data;
1746 struct window *window;
1747 char **p;
1748
1749 input->drag_offer = wl_data_offer_get_user_data(offer);
1750 window = wl_surface_get_user_data(surface);
1751 input->pointer_focus = window;
1752
1753 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1754 *p = NULL;
1755
1756 window = input->pointer_focus;
1757 if (window->data_handler)
1758 window->data_handler(window, input, time, x, y,
1759 input->drag_offer->types.data,
1760 window->user_data);
1761}
1762
1763static void
1764data_device_leave(void *data, struct wl_data_device *data_device)
1765{
1766 struct input *input = data;
1767
1768 data_offer_destroy(input->drag_offer);
1769 input->drag_offer = NULL;
1770}
1771
1772static void
1773data_device_motion(void *data, struct wl_data_device *data_device,
1774 uint32_t time, int32_t x, int32_t y)
1775{
1776 struct input *input = data;
1777 struct window *window = input->pointer_focus;
1778
1779 input->sx = x;
1780 input->sy = y;
1781
1782 if (window->data_handler)
1783 window->data_handler(window, input, time, x, y,
1784 input->drag_offer->types.data,
1785 window->user_data);
1786}
1787
1788static void
1789data_device_drop(void *data, struct wl_data_device *data_device)
1790{
1791 struct input *input = data;
1792 struct window *window = input->pointer_focus;
1793
1794 if (window->drop_handler)
1795 window->drop_handler(window, input,
1796 input->sx, input->sy, window->user_data);
1797}
1798
1799static void
1800data_device_selection(void *data,
1801 struct wl_data_device *wl_data_device,
1802 struct wl_data_offer *offer)
1803{
1804 struct input *input = data;
1805 char **p;
1806
1807 if (input->selection_offer)
1808 data_offer_destroy(input->selection_offer);
1809
1810 input->selection_offer = wl_data_offer_get_user_data(offer);
1811 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1812 *p = NULL;
1813}
1814
1815static const struct wl_data_device_listener data_device_listener = {
1816 data_device_data_offer,
1817 data_device_enter,
1818 data_device_leave,
1819 data_device_motion,
1820 data_device_drop,
1821 data_device_selection
1822};
1823
1824struct wl_data_device *
1825input_get_data_device(struct input *input)
1826{
1827 return input->data_device;
1828}
1829
1830void
1831input_set_selection(struct input *input,
1832 struct wl_data_source *source, uint32_t time)
1833{
1834 wl_data_device_set_selection(input->data_device, source, time);
1835}
1836
1837void
1838input_accept(struct input *input, uint32_t time, const char *type)
1839{
1840 wl_data_offer_accept(input->drag_offer->offer, time, type);
1841}
1842
1843static void
1844offer_io_func(struct task *task, uint32_t events)
1845{
1846 struct data_offer *offer =
1847 container_of(task, struct data_offer, io_task);
1848 unsigned int len;
1849 char buffer[4096];
1850
1851 len = read(offer->fd, buffer, sizeof buffer);
1852 offer->func(buffer, len,
1853 offer->x, offer->y, offer->user_data);
1854
1855 if (len == 0) {
1856 close(offer->fd);
1857 data_offer_destroy(offer);
1858 }
1859}
1860
1861static void
1862data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1863 data_func_t func, void *user_data)
1864{
1865 int p[2];
1866
1867 pipe2(p, O_CLOEXEC);
1868 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1869 close(p[1]);
1870
1871 offer->io_task.run = offer_io_func;
1872 offer->fd = p[0];
1873 offer->func = func;
1874 offer->refcount++;
1875 offer->user_data = user_data;
1876
1877 display_watch_fd(offer->input->display,
1878 offer->fd, EPOLLIN, &offer->io_task);
1879}
1880
1881void
1882input_receive_drag_data(struct input *input, const char *mime_type,
1883 data_func_t func, void *data)
1884{
1885 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1886 input->drag_offer->x = input->sx;
1887 input->drag_offer->y = input->sy;
1888}
1889
1890int
1891input_receive_selection_data(struct input *input, const char *mime_type,
1892 data_func_t func, void *data)
1893{
1894 char **p;
1895
1896 if (input->selection_offer == NULL)
1897 return -1;
1898
1899 for (p = input->selection_offer->types.data; *p; p++)
1900 if (strcmp(mime_type, *p) == 0)
1901 break;
1902
1903 if (*p == NULL)
1904 return -1;
1905
1906 data_offer_receive_data(input->selection_offer,
1907 mime_type, func, data);
1908 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001909}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001910
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001911int
1912input_receive_selection_data_to_fd(struct input *input,
1913 const char *mime_type, int fd)
1914{
1915 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1916
1917 return 0;
1918}
1919
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001920void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001921window_move(struct window *window, struct input *input, uint32_t time)
1922{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001923 if (!window->shell_surface)
1924 return;
1925
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001926 wl_shell_surface_move(window->shell_surface,
1927 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001928}
1929
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001930static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001931window_resize(struct window *window, int32_t width, int32_t height)
1932{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001933 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001934 struct widget *widget;
1935 int decoration_width, decoration_height;
1936
1937 decoration_width = 20 + window->margin * 2;
1938 decoration_height = 60 + window->margin * 2;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001939
1940 if (window->decoration) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001941 allocation.x = 10 + window->margin;
1942 allocation.y = 50 + window->margin;
1943 allocation.width = width - decoration_width;
1944 allocation.height = height - decoration_height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001945 } else {
1946 allocation.x = 0;
1947 allocation.y = 0;
1948 allocation.width = width;
1949 allocation.height = height;
1950 }
1951
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001952 widget = window->widget;
1953 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001954 allocation.width, allocation.height);
1955
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001956 if (widget->resize_handler)
1957 widget->resize_handler(widget,
1958 allocation.width,
1959 allocation.height,
1960 widget->user_data);
1961
1962 if (window->decoration) {
1963 window->allocation.x = 0;
1964 window->allocation.y = 0;
1965 window->allocation.width =
1966 widget->allocation.width + decoration_width;
1967 window->allocation.height =
1968 widget->allocation.height + decoration_height;
1969 } else {
1970 window->allocation = widget->allocation;
1971 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001972
1973 window_schedule_redraw(window);
1974}
1975
1976static void
1977idle_resize(struct task *task, uint32_t events)
1978{
1979 struct window *window =
1980 container_of(task, struct window, resize_task);
1981
1982 window_resize(window,
1983 window->allocation.width, window->allocation.height);
1984 window->resize_scheduled = 0;
1985}
1986
1987void
1988window_schedule_resize(struct window *window, int width, int height)
1989{
1990 if (!window->resize_scheduled) {
1991 window->allocation.width = width;
1992 window->allocation.height = height;
1993 window->resize_task.run = idle_resize;
1994 display_defer(window->display, &window->resize_task);
1995 window->resize_scheduled = 1;
1996 }
1997}
1998
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001999void
2000widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2001{
2002 struct window *window = widget->window;
2003
2004 if (widget->window->decoration) {
2005 window_schedule_resize(window,
2006 width + 20 + 2 * window->margin,
2007 height + 60 + 2 * window->margin);
2008
2009 } else {
2010 window_schedule_resize(window, width, height);
2011 }
2012}
2013
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002014static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002015handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002016 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002017 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002018{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002019 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002020
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002021 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002022 return;
2023
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002024 window->resize_edges = edges;
2025
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002026 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002027}
2028
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002029static void
2030handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2031{
2032 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002033 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002034 /* FIXME: Need more context in this event, at least the input
2035 * device. Or just use wl_callback. */
2036
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002037 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002038 window_destroy(window);
2039}
2040
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002041static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002042 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002043 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002044};
2045
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002046void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002047window_get_allocation(struct window *window,
2048 struct rectangle *allocation)
2049{
2050 *allocation = window->allocation;
2051}
2052
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002053static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002054widget_redraw(struct widget *widget)
2055{
2056 struct widget *child;
2057
2058 if (widget->redraw_handler)
2059 widget->redraw_handler(widget, widget->user_data);
2060 wl_list_for_each(child, &widget->child_list, link)
2061 widget_redraw(child);
2062}
2063
2064static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002065idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002066{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002067 struct window *window =
2068 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002069
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002070 window_create_surface(window);
2071 if (window->decoration)
2072 window_draw_decorations(window);
2073
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002074 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002075
2076 window_flush(window);
2077
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002078 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002079}
2080
2081void
2082window_schedule_redraw(struct window *window)
2083{
2084 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002085 window->redraw_task.run = idle_redraw;
2086 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002087 window->redraw_scheduled = 1;
2088 }
2089}
2090
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002091void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002092window_set_custom(struct window *window)
2093{
2094 window->type = TYPE_CUSTOM;
2095}
2096
2097void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002098window_set_fullscreen(struct window *window, int fullscreen)
2099{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002100 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002101 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002102
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002103 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002104 return;
2105
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002106 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002107 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002108 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002109 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002110 width = output->allocation.width;
2111 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002112 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002113 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002114 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002115 width = window->saved_allocation.width - 20 - window->margin * 2;
2116 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002117 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002118 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002119
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002120 window_resize(window, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002121}
2122
2123void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002124window_set_decoration(struct window *window, int decoration)
2125{
2126 window->decoration = decoration;
2127}
2128
2129void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002130window_set_user_data(struct window *window, void *data)
2131{
2132 window->user_data = data;
2133}
2134
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002135void *
2136window_get_user_data(struct window *window)
2137{
2138 return window->user_data;
2139}
2140
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002141void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002142window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002143 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002144{
2145 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002146}
2147
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002148void
2149window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002150 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002151{
2152 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002153}
2154
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002155void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002156window_set_data_handler(struct window *window, window_data_handler_t handler)
2157{
2158 window->data_handler = handler;
2159}
2160
2161void
2162window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2163{
2164 window->drop_handler = handler;
2165}
2166
2167void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002168window_set_close_handler(struct window *window,
2169 window_close_handler_t handler)
2170{
2171 window->close_handler = handler;
2172}
2173
2174void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002175window_set_transparent(struct window *window, int transparent)
2176{
2177 window->transparent = transparent;
2178}
2179
2180void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002181window_set_title(struct window *window, const char *title)
2182{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002183 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002184 window->title = strdup(title);
2185}
2186
2187const char *
2188window_get_title(struct window *window)
2189{
2190 return window->title;
2191}
2192
2193void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002194display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2195 int32_t x, int32_t y, int32_t width, int32_t height)
2196{
2197 struct wl_buffer *buffer;
2198
2199 buffer = display_get_buffer_for_surface(display, cairo_surface);
2200
2201 wl_buffer_damage(buffer, x, y, width, height);
2202}
2203
2204void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002205window_damage(struct window *window, int32_t x, int32_t y,
2206 int32_t width, int32_t height)
2207{
2208 wl_surface_damage(window->surface, x, y, width, height);
2209}
2210
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002211static struct window *
2212window_create_internal(struct display *display, struct window *parent,
2213 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002214{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002215 struct window *window;
2216
2217 window = malloc(sizeof *window);
2218 if (window == NULL)
2219 return NULL;
2220
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002221 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002222 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002223 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002224 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002225 if (display->shell) {
2226 window->shell_surface =
2227 wl_shell_get_shell_surface(display->shell,
2228 window->surface);
2229 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002230 window->allocation.x = 0;
2231 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002232 window->allocation.width = width;
2233 window->allocation.height = height;
2234 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002235 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002236 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002237 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002238
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002239 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002240#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002241 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2242 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002243#else
2244 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2245#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002246 else
2247 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002248
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002249 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002250 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002251
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002252 if (window->shell_surface) {
2253 wl_shell_surface_set_user_data(window->shell_surface, window);
2254 wl_shell_surface_add_listener(window->shell_surface,
2255 &shell_surface_listener, window);
2256 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002257
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002258 return window;
2259}
2260
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002261struct window *
2262window_create(struct display *display, int32_t width, int32_t height)
2263{
2264 struct window *window;
2265
2266 window = window_create_internal(display, NULL, width, height);
2267 if (!window)
2268 return NULL;
2269
2270 return window;
2271}
2272
2273struct window *
2274window_create_transient(struct display *display, struct window *parent,
2275 int32_t x, int32_t y, int32_t width, int32_t height)
2276{
2277 struct window *window;
2278
2279 window = window_create_internal(parent->display,
2280 parent, width, height);
2281 if (!window)
2282 return NULL;
2283
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002284 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002285 window->x = x;
2286 window->y = y;
2287
2288 return window;
2289}
2290
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002291static int
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002292menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002293{
2294 int next;
2295
2296 next = (sy - 8) / 20;
2297 if (menu->current != next) {
2298 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002299 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002300 }
2301
2302 return POINTER_LEFT_PTR;
2303}
2304
2305static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002306menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002307 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002308 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002309{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002310 return menu_set_item(data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002311}
2312
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002313static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002314menu_enter_handler(struct widget *widget,
2315 struct input *input, uint32_t time,
2316 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002317{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002318 return menu_set_item(data, y);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002319}
2320
2321static void
2322menu_leave_handler(struct widget *widget, struct input *input, void *data)
2323{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002324 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002325}
2326
2327static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002328menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002329 struct input *input, uint32_t time,
2330 int button, int state, void *data)
2331
2332{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002333 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002334
2335 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002336 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002337 menu->func(menu->window->parent,
2338 menu->current, menu->window->parent->user_data);
2339 window_destroy(widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002340 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002341}
2342
2343static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002344menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002345{
2346 cairo_t *cr;
2347 const int32_t r = 3, margin = 3;
2348 struct menu *menu = data;
2349 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002350 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002351
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002352 cr = cairo_create(window->cairo_surface);
2353 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2354 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2355 cairo_paint(cr);
2356
2357 width = window->allocation.width;
2358 height = window->allocation.height;
2359 rounded_rect(cr, 0, 0, width, height, r);
2360 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2361 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2362 cairo_fill(cr);
2363
2364 for (i = 0; i < menu->count; i++) {
2365 if (i == menu->current) {
2366 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2367 cairo_rectangle(cr, margin, i * 20 + margin,
2368 width - 2 * margin, 20);
2369 cairo_fill(cr);
2370 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2371 cairo_move_to(cr, 10, i * 20 + 16);
2372 cairo_show_text(cr, menu->entries[i]);
2373 } else {
2374 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2375 cairo_move_to(cr, 10, i * 20 + 16);
2376 cairo_show_text(cr, menu->entries[i]);
2377 }
2378 }
2379
2380 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002381}
2382
2383struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002384window_create_menu(struct display *display,
2385 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002386 int32_t x, int32_t y,
2387 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002388{
2389 struct window *window;
2390 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002391 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002392
2393 menu = malloc(sizeof *menu);
2394 if (!menu)
2395 return NULL;
2396
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002397 window = window_create_internal(parent->display, parent,
2398 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002399 if (!window)
2400 return NULL;
2401
2402 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002403 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002404 menu->entries = entries;
2405 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002406 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002407 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002408 window->decoration = 0;
2409 window->type = TYPE_MENU;
2410 window->x = x;
2411 window->y = y;
2412
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002413 wl_shell_surface_set_popup(window->shell_surface,
2414 input->input_device, time,
2415 window->parent->shell_surface,
2416 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002417
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002418 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002419 widget_set_enter_handler(menu->widget, menu_enter_handler);
2420 widget_set_leave_handler(menu->widget, menu_leave_handler);
2421 widget_set_motion_handler(menu->widget, menu_motion_handler);
2422 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002423
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002424 return window;
2425}
2426
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002427void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002428window_set_buffer_type(struct window *window, enum window_buffer_type type)
2429{
2430 window->buffer_type = type;
2431}
2432
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002433
2434static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002435display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002436 struct wl_output *wl_output,
2437 int x, int y,
2438 int physical_width,
2439 int physical_height,
2440 int subpixel,
2441 const char *make,
2442 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002443{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002444 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002445
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002446 output->allocation.x = x;
2447 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002448}
2449
2450static void
2451display_handle_mode(void *data,
2452 struct wl_output *wl_output,
2453 uint32_t flags,
2454 int width,
2455 int height,
2456 int refresh)
2457{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002458 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002459 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002460
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002461 if (flags & WL_OUTPUT_MODE_CURRENT) {
2462 output->allocation.width = width;
2463 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002464 if (display->output_configure_handler)
2465 (*display->output_configure_handler)(
2466 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002467 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002468}
2469
2470static const struct wl_output_listener output_listener = {
2471 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002472 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002473};
2474
2475static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002476display_add_output(struct display *d, uint32_t id)
2477{
2478 struct output *output;
2479
2480 output = malloc(sizeof *output);
2481 if (output == NULL)
2482 return;
2483
2484 memset(output, 0, sizeof *output);
2485 output->display = d;
2486 output->output =
2487 wl_display_bind(d->display, id, &wl_output_interface);
2488 wl_list_insert(d->output_list.prev, &output->link);
2489
2490 wl_output_add_listener(output->output, &output_listener, output);
2491}
2492
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002493static void
2494output_destroy(struct output *output)
2495{
2496 if (output->destroy_handler)
2497 (*output->destroy_handler)(output, output->user_data);
2498
2499 wl_output_destroy(output->output);
2500 wl_list_remove(&output->link);
2501 free(output);
2502}
2503
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002504void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002505display_set_output_configure_handler(struct display *display,
2506 display_output_handler_t handler)
2507{
2508 struct output *output;
2509
2510 display->output_configure_handler = handler;
2511 if (!handler)
2512 return;
2513
2514 wl_list_for_each(output, &display->output_list, link)
2515 (*display->output_configure_handler)(output,
2516 display->user_data);
2517}
2518
2519void
2520output_set_user_data(struct output *output, void *data)
2521{
2522 output->user_data = data;
2523}
2524
2525void *
2526output_get_user_data(struct output *output)
2527{
2528 return output->user_data;
2529}
2530
2531void
2532output_set_destroy_handler(struct output *output,
2533 display_output_handler_t handler)
2534{
2535 output->destroy_handler = handler;
2536 /* FIXME: implement this, once we have way to remove outputs */
2537}
2538
2539void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002540output_get_allocation(struct output *output, struct rectangle *allocation)
2541{
2542 *allocation = output->allocation;
2543}
2544
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002545struct wl_output *
2546output_get_wl_output(struct output *output)
2547{
2548 return output->output;
2549}
2550
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002551static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002552display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002553{
2554 struct input *input;
2555
2556 input = malloc(sizeof *input);
2557 if (input == NULL)
2558 return;
2559
2560 memset(input, 0, sizeof *input);
2561 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002562 input->input_device =
2563 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002564 input->pointer_focus = NULL;
2565 input->keyboard_focus = NULL;
2566 wl_list_insert(d->input_list.prev, &input->link);
2567
2568 wl_input_device_add_listener(input->input_device,
2569 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002570 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002571
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002572 input->data_device =
2573 wl_data_device_manager_get_data_device(d->data_device_manager,
2574 input->input_device);
2575 wl_data_device_add_listener(input->data_device,
2576 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002577}
2578
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002579static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002580input_destroy(struct input *input)
2581{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002582 input_remove_keyboard_focus(input);
2583 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002584
2585 if (input->drag_offer)
2586 data_offer_destroy(input->drag_offer);
2587
2588 if (input->selection_offer)
2589 data_offer_destroy(input->selection_offer);
2590
2591 wl_data_device_destroy(input->data_device);
2592 wl_list_remove(&input->link);
2593 wl_input_device_destroy(input->input_device);
2594 free(input);
2595}
2596
2597static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002598display_handle_global(struct wl_display *display, uint32_t id,
2599 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002600{
2601 struct display *d = data;
2602
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002603 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002604 d->compositor =
2605 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002606 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002607 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002608 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002609 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002610 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002611 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002612 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002613 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002614 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2615 d->data_device_manager =
2616 wl_display_bind(display, id,
2617 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002618 }
2619}
2620
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002621static void
2622display_render_frame(struct display *d)
2623{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002624 int radius = 8;
2625 cairo_t *cr;
2626
2627 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2628 cr = cairo_create(d->shadow);
2629 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2630 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2631 rounded_rect(cr, 16, 16, 112, 112, radius);
2632 cairo_fill(cr);
2633 cairo_destroy(cr);
2634 blur_surface(d->shadow, 64);
2635
2636 d->active_frame =
2637 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2638 cr = cairo_create(d->active_frame);
2639 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2640 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2641 rounded_rect(cr, 16, 16, 112, 112, radius);
2642 cairo_fill(cr);
2643 cairo_destroy(cr);
2644
2645 d->inactive_frame =
2646 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2647 cr = cairo_create(d->inactive_frame);
2648 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2649 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2650 rounded_rect(cr, 16, 16, 112, 112, radius);
2651 cairo_fill(cr);
2652 cairo_destroy(cr);
2653}
2654
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002655static void
2656init_xkb(struct display *d)
2657{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002658 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002659
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002660 names.rules = "evdev";
2661 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002662 names.layout = option_xkb_layout;
2663 names.variant = option_xkb_variant;
2664 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002665
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002666 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002667 if (!d->xkb) {
2668 fprintf(stderr, "Failed to compile keymap\n");
2669 exit(1);
2670 }
2671}
2672
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002673static void
2674fini_xkb(struct display *display)
2675{
2676 xkb_free_keymap(display->xkb);
2677}
2678
Yuval Fledel45568f62010-12-06 09:18:12 -05002679static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002680init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002681{
2682 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002683 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002684
2685 static const EGLint premul_argb_cfg_attribs[] = {
2686 EGL_SURFACE_TYPE,
2687 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2688 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002689 EGL_RED_SIZE, 1,
2690 EGL_GREEN_SIZE, 1,
2691 EGL_BLUE_SIZE, 1,
2692 EGL_ALPHA_SIZE, 1,
2693 EGL_DEPTH_SIZE, 1,
2694 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2695 EGL_NONE
2696 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002697
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002698 static const EGLint rgb_cfg_attribs[] = {
2699 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2700 EGL_RED_SIZE, 1,
2701 EGL_GREEN_SIZE, 1,
2702 EGL_BLUE_SIZE, 1,
2703 EGL_ALPHA_SIZE, 0,
2704 EGL_DEPTH_SIZE, 1,
2705 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2706 EGL_NONE
2707 };
2708
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002709 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002710 if (!eglInitialize(d->dpy, &major, &minor)) {
2711 fprintf(stderr, "failed to initialize display\n");
2712 return -1;
2713 }
2714
2715 if (!eglBindAPI(EGL_OPENGL_API)) {
2716 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2717 return -1;
2718 }
2719
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002720 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002721 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002722 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002723 return -1;
2724 }
2725
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002726 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2727 &d->rgb_config, 1, &n) || n != 1) {
2728 fprintf(stderr, "failed to choose rgb config\n");
2729 return -1;
2730 }
2731
Benjamin Franzke0c991632011-09-27 21:57:31 +02002732 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2733 if (d->rgb_ctx == NULL) {
2734 fprintf(stderr, "failed to create context\n");
2735 return -1;
2736 }
2737 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2738 EGL_NO_CONTEXT, NULL);
2739 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002740 fprintf(stderr, "failed to create context\n");
2741 return -1;
2742 }
2743
Benjamin Franzke0c991632011-09-27 21:57:31 +02002744 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002745 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002746 return -1;
2747 }
2748
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002749#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002750 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2751 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002752 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002753 return -1;
2754 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002755 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2756 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2757 fprintf(stderr, "failed to get cairo egl argb device\n");
2758 return -1;
2759 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002760#endif
2761
2762 return 0;
2763}
2764
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002765static void
2766fini_egl(struct display *display)
2767{
2768#ifdef HAVE_CAIRO_EGL
2769 cairo_device_destroy(display->argb_device);
2770 cairo_device_destroy(display->rgb_device);
2771#endif
2772
2773 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2774 EGL_NO_CONTEXT);
2775
2776 eglTerminate(display->dpy);
2777 eglReleaseThread();
2778}
2779
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002780static int
2781event_mask_update(uint32_t mask, void *data)
2782{
2783 struct display *d = data;
2784
2785 d->mask = mask;
2786
2787 return 0;
2788}
2789
2790static void
2791handle_display_data(struct task *task, uint32_t events)
2792{
2793 struct display *display =
2794 container_of(task, struct display, display_task);
2795
2796 wl_display_iterate(display->display, display->mask);
2797}
2798
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002799struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002800display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002801{
2802 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002803 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002804 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002805 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002806
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002807 g_type_init();
2808
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002809 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002810 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002811 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002812
2813 xkb_option_group = g_option_group_new("xkb",
2814 "Keyboard options",
2815 "Show all XKB options",
2816 NULL, NULL);
2817 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2818 g_option_context_add_group (context, xkb_option_group);
2819
2820 if (!g_option_context_parse(context, argc, argv, &error)) {
2821 fprintf(stderr, "option parsing failed: %s\n", error->message);
2822 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002823 }
2824
Tim Wiederhake748f6722011-01-23 23:25:25 +01002825 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002826
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002827 d = malloc(sizeof *d);
2828 if (d == NULL)
2829 return NULL;
2830
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002831 memset(d, 0, sizeof *d);
2832
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002833 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002834 if (d->display == NULL) {
2835 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002836 return NULL;
2837 }
2838
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002839 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2840 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2841 d->display_task.run = handle_display_data;
2842 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2843
2844 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002845 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002846 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002847
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002848 /* Set up listener so we'll catch all events. */
2849 wl_display_add_global_listener(d->display,
2850 display_handle_global, d);
2851
2852 /* Process connection events. */
2853 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002854 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002855 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002856
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002857 d->image_target_texture_2d =
2858 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2859 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2860 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2861
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002862 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002863
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002864 display_render_frame(d);
2865
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002866 wl_list_init(&d->window_list);
2867
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002868 init_xkb(d);
2869
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002870 return d;
2871}
2872
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002873static void
2874display_destroy_outputs(struct display *display)
2875{
2876 struct output *tmp;
2877 struct output *output;
2878
2879 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2880 output_destroy(output);
2881}
2882
Pekka Paalanene1207c72011-12-16 12:02:09 +02002883static void
2884display_destroy_inputs(struct display *display)
2885{
2886 struct input *tmp;
2887 struct input *input;
2888
2889 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2890 input_destroy(input);
2891}
2892
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002893void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002894display_destroy(struct display *display)
2895{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002896 if (!wl_list_empty(&display->window_list))
2897 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2898
2899 if (!wl_list_empty(&display->deferred_list))
2900 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2901
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002902 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002903 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002904
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002905 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002906
2907 cairo_surface_destroy(display->active_frame);
2908 cairo_surface_destroy(display->inactive_frame);
2909 cairo_surface_destroy(display->shadow);
2910 destroy_pointer_surfaces(display);
2911
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002912 fini_egl(display);
2913
Pekka Paalanenc2052982011-12-16 11:41:32 +02002914 if (display->shell)
2915 wl_shell_destroy(display->shell);
2916
2917 if (display->shm)
2918 wl_shm_destroy(display->shm);
2919
2920 if (display->data_device_manager)
2921 wl_data_device_manager_destroy(display->data_device_manager);
2922
2923 wl_compositor_destroy(display->compositor);
2924
2925 close(display->epoll_fd);
2926
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002927 wl_display_flush(display->display);
2928 wl_display_destroy(display->display);
2929 free(display);
2930}
2931
2932void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002933display_set_user_data(struct display *display, void *data)
2934{
2935 display->user_data = data;
2936}
2937
2938void *
2939display_get_user_data(struct display *display)
2940{
2941 return display->user_data;
2942}
2943
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002944struct wl_display *
2945display_get_display(struct display *display)
2946{
2947 return display->display;
2948}
2949
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002950struct output *
2951display_get_output(struct display *display)
2952{
2953 return container_of(display->output_list.next, struct output, link);
2954}
2955
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002956struct wl_compositor *
2957display_get_compositor(struct display *display)
2958{
2959 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002960}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002961
2962EGLDisplay
2963display_get_egl_display(struct display *d)
2964{
2965 return d->dpy;
2966}
2967
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002968struct wl_data_source *
2969display_create_data_source(struct display *display)
2970{
2971 return wl_data_device_manager_create_data_source(display->data_device_manager);
2972}
2973
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002974EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002975display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002976{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002977 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002978}
2979
Benjamin Franzke0c991632011-09-27 21:57:31 +02002980EGLConfig
2981display_get_argb_egl_config(struct display *d)
2982{
2983 return d->premultiplied_argb_config;
2984}
2985
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002986struct wl_shell *
2987display_get_shell(struct display *display)
2988{
2989 return display->shell;
2990}
2991
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002992int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002993display_acquire_window_surface(struct display *display,
2994 struct window *window,
2995 EGLContext ctx)
2996{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002997#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002998 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002999 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003000
3001 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003002 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003003 device = cairo_surface_get_device(window->cairo_surface);
3004 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003005 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003006
Benjamin Franzke0c991632011-09-27 21:57:31 +02003007 if (!ctx) {
3008 if (device == display->rgb_device)
3009 ctx = display->rgb_ctx;
3010 else if (device == display->argb_device)
3011 ctx = display->argb_ctx;
3012 else
3013 assert(0);
3014 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003015
3016 data = cairo_surface_get_user_data(window->cairo_surface,
3017 &surface_data_key);
3018
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003019 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003020 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003021 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3022 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003023
3024 return 0;
3025#else
3026 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003027#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003028}
3029
3030void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003031display_release_window_surface(struct display *display,
3032 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003033{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003034#ifdef HAVE_CAIRO_EGL
3035 cairo_device_t *device;
3036
3037 device = cairo_surface_get_device(window->cairo_surface);
3038 if (!device)
3039 return;
3040
3041 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003042 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003043 cairo_device_release(device);
3044#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003045}
3046
3047void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003048display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003049{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003050 wl_list_insert(&display->deferred_list, &task->link);
3051}
3052
3053void
3054display_watch_fd(struct display *display,
3055 int fd, uint32_t events, struct task *task)
3056{
3057 struct epoll_event ep;
3058
3059 ep.events = events;
3060 ep.data.ptr = task;
3061 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3062}
3063
3064void
3065display_run(struct display *display)
3066{
3067 struct task *task;
3068 struct epoll_event ep[16];
3069 int i, count;
3070
Pekka Paalanen826d7952011-12-15 10:14:07 +02003071 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003072 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003073 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003074
3075 while (!wl_list_empty(&display->deferred_list)) {
3076 task = container_of(display->deferred_list.next,
3077 struct task, link);
3078 wl_list_remove(&task->link);
3079 task->run(task, 0);
3080 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003081
3082 if (!display->running)
3083 break;
3084
3085 wl_display_flush(display->display);
3086
3087 count = epoll_wait(display->epoll_fd,
3088 ep, ARRAY_LENGTH(ep), -1);
3089 for (i = 0; i < count; i++) {
3090 task = ep[i].data.ptr;
3091 task->run(task, ep[i].events);
3092 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003093 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003094}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003095
3096void
3097display_exit(struct display *display)
3098{
3099 display->running = 0;
3100}