blob: c81aed30e03b0fd5ba86f1bfbfc49c9da1841460 [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øgsbergc51f7992012-01-08 15:09:53 -05001115widget_set_allocation(struct widget *widget,
1116 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001117{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001118 widget->allocation.x = x;
1119 widget->allocation.y = y;
1120 widget->allocation.width = width;
1121 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001122}
1123
1124void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001125widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001126{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001127 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001128}
1129
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001130void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001131widget_set_resize_handler(struct widget *widget,
1132 widget_resize_handler_t handler)
1133{
1134 widget->resize_handler = handler;
1135}
1136
1137void
1138widget_set_redraw_handler(struct widget *widget,
1139 widget_redraw_handler_t handler)
1140{
1141 widget->redraw_handler = handler;
1142}
1143
1144void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001145widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001146{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001147 widget->enter_handler = handler;
1148}
1149
1150void
1151widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1152{
1153 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001154}
1155
1156void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001157widget_set_motion_handler(struct widget *widget,
1158 widget_motion_handler_t handler)
1159{
1160 widget->motion_handler = handler;
1161}
1162
1163void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001164widget_set_button_handler(struct widget *widget,
1165 widget_button_handler_t handler)
1166{
1167 widget->button_handler = handler;
1168}
1169
1170void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001171widget_schedule_redraw(struct widget *widget)
1172{
1173 window_schedule_redraw(widget->window);
1174}
1175
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001176cairo_surface_t *
1177window_get_surface(struct window *window)
1178{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001179 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001180}
1181
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001182struct wl_surface *
1183window_get_wl_surface(struct window *window)
1184{
1185 return window->surface;
1186}
1187
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001188struct wl_shell_surface *
1189window_get_wl_shell_surface(struct window *window)
1190{
1191 return window->shell_surface;
1192}
1193
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001194static int
1195get_pointer_location(struct window *window, int32_t x, int32_t y)
1196{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001197 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001198 const int grip_size = 8;
1199
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001200 if (!window->decoration)
1201 return WINDOW_CLIENT_AREA;
1202
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001204 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001205 else if (window->margin <= x && x < window->margin + grip_size)
1206 hlocation = WINDOW_RESIZING_LEFT;
1207 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 else if (x < window->allocation.width - window->margin)
1210 hlocation = WINDOW_RESIZING_RIGHT;
1211 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001212 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213
1214 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001215 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001216 else if (window->margin <= y && y < window->margin + grip_size)
1217 vlocation = WINDOW_RESIZING_TOP;
1218 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001219 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001220 else if (y < window->allocation.height - window->margin)
1221 vlocation = WINDOW_RESIZING_BOTTOM;
1222 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001224
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001225 location = vlocation | hlocation;
1226 if (location & WINDOW_EXTERIOR)
1227 location = WINDOW_EXTERIOR;
1228 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1229 location = WINDOW_TITLEBAR;
1230 else if (location == WINDOW_INTERIOR)
1231 location = WINDOW_CLIENT_AREA;
1232
1233 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234}
1235
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001236static int
1237input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001239 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240
1241 location = get_pointer_location(input->pointer_focus,
1242 input->sx, input->sy);
1243 switch (location) {
1244 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001245 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001246 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001247 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001249 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001251 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001253 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001255 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001257 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001259 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001260 case WINDOW_EXTERIOR:
1261 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001262 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001264 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001266}
1267
1268void
1269input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1270{
1271 struct display *display = input->display;
1272 struct wl_buffer *buffer;
1273 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001275 if (pointer == input->current_pointer_image)
1276 return;
1277
1278 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001279 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001280
1281 if (!surface)
1282 return;
1283
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001284 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001285 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286 pointer_images[pointer].hotspot_x,
1287 pointer_images[pointer].hotspot_y);
1288}
1289
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001290static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001291window_set_focus_widget(struct window *window, struct widget *focus,
1292 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001294 struct widget *old;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001295 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001296
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001297 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001298 return;
1299
Kristian Høgsbergee143232012-01-09 08:42:24 -05001300 old = window->focus_widget;
1301 if (old) {
1302 if (old->leave_handler)
1303 old->leave_handler(old, input, old->user_data);
1304 window->focus_widget = NULL;
1305 }
1306
1307 if (focus) {
1308 if (focus->enter_handler)
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001309 pointer = focus->enter_handler(focus, input, time,
1310 x, y, focus->user_data);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001311 window->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001312
1313 pointer = input_get_pointer_image_for_location(input, pointer);
1314 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001315 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001316}
1317
1318static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001319input_handle_motion(void *data, struct wl_input_device *input_device,
1320 uint32_t time,
1321 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001322{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001324 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001325 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001326 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327
1328 input->x = x;
1329 input->y = y;
1330 input->sx = sx;
1331 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001332
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001333 if (!window->focus_widget || !window->widget_grab_button) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001334 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001335 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001336 }
1337
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001338 widget = window->focus_widget;
1339 if (widget && widget->motion_handler)
1340 pointer = widget->motion_handler(widget, input, time, sx, sy,
1341 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001342
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001343 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001344 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001345}
1346
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001347static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001348window_menu_func(struct window *window, int index, void *data)
1349{
1350 switch (index) {
1351 case 0: /* close */
1352 if (window->close_handler)
1353 window->close_handler(window->parent,
1354 window->user_data);
1355 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001356 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001357 break;
1358 case 1: /* fullscreen */
1359 /* we don't have a way to get out of fullscreen for now */
1360 window_set_fullscreen(window, 1);
1361 break;
1362 case 2: /* rotate */
1363 case 3: /* scale */
1364 break;
1365 }
1366}
1367
Kristian Høgsberg27ff0862012-01-10 14:28:58 -05001368static void
1369break_grab(struct window *window)
1370{
1371 window->focus_widget = NULL;
1372 window->widget_grab_button = 0;
1373}
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001374
1375static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001376input_handle_button(void *data,
1377 struct wl_input_device *input_device,
1378 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001379{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001380 struct input *input = data;
1381 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001382 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001383 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001384 int32_t x, y;
1385 static const char *entries[] = {
1386 "Close", "Fullscreen", "Rotate", "Scale"
1387 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001388
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001389 if (window->focus_widget && window->widget_grab_button == 0 && state)
1390 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001391
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001392 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001393
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001394 widget = window->focus_widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001395 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001396 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001397 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001398 if (!window->shell_surface)
1399 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001400 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg27ff0862012-01-10 14:28:58 -05001401 break_grab(window);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001402 wl_shell_surface_move(window->shell_surface,
1403 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001404 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001405 case WINDOW_RESIZING_TOP:
1406 case WINDOW_RESIZING_BOTTOM:
1407 case WINDOW_RESIZING_LEFT:
1408 case WINDOW_RESIZING_RIGHT:
1409 case WINDOW_RESIZING_TOP_LEFT:
1410 case WINDOW_RESIZING_TOP_RIGHT:
1411 case WINDOW_RESIZING_BOTTOM_LEFT:
1412 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001413 if (!window->shell_surface)
1414 break;
Kristian Høgsberg27ff0862012-01-10 14:28:58 -05001415 break_grab(window);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001416 wl_shell_surface_resize(window->shell_surface,
1417 input_device, time,
1418 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001419 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001420 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001421 if (widget && widget->button_handler)
1422 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001423 input, time,
1424 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001425 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001426 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001427 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001428 } else if (button == BTN_RIGHT && state == 1) {
1429 switch (location) {
1430 default:
1431 input_get_position(input, &x, &y);
1432 window->menu = window_create_menu(window->display,
1433 input, time,
1434 window,
1435 x - 10, y - 10,
1436 window_menu_func,
1437 entries, 4);
1438 window_schedule_redraw(window->menu);
1439 break;
1440 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øgsberg4f7dcd62012-01-06 21:59:05 -05001443 input, time,
1444 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001445 widget->user_data);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001446 break;
1447 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001448 } else {
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001449 if (widget && widget->button_handler)
1450 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001451 input, time,
1452 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001453 widget->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001454 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001455
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001456 if (window->focus_widget &&
1457 window->widget_grab_button == button && !state) {
1458 window->widget_grab_button = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001459 widget = widget_find_widget(window->widget,
1460 input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001461 window_set_focus_widget(window, widget, input, time,
1462 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001463 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001464}
1465
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001466static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001467input_handle_key(void *data, struct wl_input_device *input_device,
1468 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001469{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001470 struct input *input = data;
1471 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001472 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001473 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001474
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001475 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001476 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001477 return;
1478
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001479 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001480 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001481 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1482 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001483
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001484 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1485
1486 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001487 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001488 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001489 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001490
1491 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001492 (*window->key_handler)(window, input, time, key, sym, state,
1493 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001494}
1495
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001496static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001497input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001498{
1499 struct window *window = input->pointer_focus;
1500
1501 if (!window)
1502 return;
1503
Kristian Høgsbergee143232012-01-09 08:42:24 -05001504 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001505
Pekka Paalanene1207c72011-12-16 12:02:09 +02001506 input->pointer_focus = NULL;
1507 input->current_pointer_image = POINTER_UNSET;
1508}
1509
1510static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001511input_handle_pointer_focus(void *data,
1512 struct wl_input_device *input_device,
1513 uint32_t time, struct wl_surface *surface,
1514 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001515{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001516 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001517 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001518 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001519 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001520
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001521 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001522 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001523 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001524
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001525 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001526 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001527 window = input->pointer_focus;
1528
Kristian Høgsberg59826582011-01-20 11:56:57 -05001529 input->x = x;
1530 input->y = y;
1531 input->sx = sx;
1532 input->sy = sy;
1533
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001534 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001535 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001536 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001537
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001538 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001539 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001540 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001541}
1542
1543static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001544input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001545{
1546 struct window *window = input->keyboard_focus;
1547
1548 if (!window)
1549 return;
1550
1551 window->keyboard_device = NULL;
1552 if (window->keyboard_focus_handler)
1553 (*window->keyboard_focus_handler)(window, NULL,
1554 window->user_data);
1555
1556 input->keyboard_focus = NULL;
1557}
1558
1559static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001560input_handle_keyboard_focus(void *data,
1561 struct wl_input_device *input_device,
1562 uint32_t time,
1563 struct wl_surface *surface,
1564 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001565{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001566 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001567 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001568 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001569 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001570
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001571 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001572
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001573 if (surface)
1574 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001575
1576 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001577 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001578 for (k = keys->data; k < end; k++)
1579 input->modifiers |= d->xkb->map->modmap[*k];
1580
1581 window = input->keyboard_focus;
1582 if (window) {
1583 window->keyboard_device = input;
1584 if (window->keyboard_focus_handler)
1585 (*window->keyboard_focus_handler)(window,
1586 window->keyboard_device,
1587 window->user_data);
1588 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001589}
1590
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001591static void
1592input_handle_touch_down(void *data,
1593 struct wl_input_device *wl_input_device,
1594 uint32_t time, struct wl_surface *surface,
1595 int32_t id, int32_t x, int32_t y)
1596{
1597}
1598
1599static void
1600input_handle_touch_up(void *data,
1601 struct wl_input_device *wl_input_device,
1602 uint32_t time, int32_t id)
1603{
1604}
1605
1606static void
1607input_handle_touch_motion(void *data,
1608 struct wl_input_device *wl_input_device,
1609 uint32_t time, int32_t id, int32_t x, int32_t y)
1610{
1611}
1612
1613static void
1614input_handle_touch_frame(void *data,
1615 struct wl_input_device *wl_input_device)
1616{
1617}
1618
1619static void
1620input_handle_touch_cancel(void *data,
1621 struct wl_input_device *wl_input_device)
1622{
1623}
1624
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001625static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001626 input_handle_motion,
1627 input_handle_button,
1628 input_handle_key,
1629 input_handle_pointer_focus,
1630 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001631 input_handle_touch_down,
1632 input_handle_touch_up,
1633 input_handle_touch_motion,
1634 input_handle_touch_frame,
1635 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001636};
1637
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001638void
1639input_get_position(struct input *input, int32_t *x, int32_t *y)
1640{
1641 *x = input->sx;
1642 *y = input->sy;
1643}
1644
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001645struct wl_input_device *
1646input_get_input_device(struct input *input)
1647{
1648 return input->input_device;
1649}
1650
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001651uint32_t
1652input_get_modifiers(struct input *input)
1653{
1654 return input->modifiers;
1655}
1656
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001657struct data_offer {
1658 struct wl_data_offer *offer;
1659 struct input *input;
1660 struct wl_array types;
1661 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001662
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001663 struct task io_task;
1664 int fd;
1665 data_func_t func;
1666 int32_t x, y;
1667 void *user_data;
1668};
1669
1670static void
1671data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1672{
1673 struct data_offer *offer = data;
1674 char **p;
1675
1676 p = wl_array_add(&offer->types, sizeof *p);
1677 *p = strdup(type);
1678}
1679
1680static const struct wl_data_offer_listener data_offer_listener = {
1681 data_offer_offer,
1682};
1683
1684static void
1685data_offer_destroy(struct data_offer *offer)
1686{
1687 char **p;
1688
1689 offer->refcount--;
1690 if (offer->refcount == 0) {
1691 wl_data_offer_destroy(offer->offer);
1692 for (p = offer->types.data; *p; p++)
1693 free(*p);
1694 wl_array_release(&offer->types);
1695 free(offer);
1696 }
1697}
1698
1699static void
1700data_device_data_offer(void *data,
1701 struct wl_data_device *data_device, uint32_t id)
1702{
1703 struct data_offer *offer;
1704
1705 offer = malloc(sizeof *offer);
1706
1707 wl_array_init(&offer->types);
1708 offer->refcount = 1;
1709 offer->input = data;
1710
1711 /* FIXME: Generate typesafe wrappers for this */
1712 offer->offer = (struct wl_data_offer *)
1713 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1714 id, &wl_data_offer_interface);
1715
1716 wl_data_offer_add_listener(offer->offer,
1717 &data_offer_listener, offer);
1718}
1719
1720static void
1721data_device_enter(void *data, struct wl_data_device *data_device,
1722 uint32_t time, struct wl_surface *surface,
1723 int32_t x, int32_t y, struct wl_data_offer *offer)
1724{
1725 struct input *input = data;
1726 struct window *window;
1727 char **p;
1728
1729 input->drag_offer = wl_data_offer_get_user_data(offer);
1730 window = wl_surface_get_user_data(surface);
1731 input->pointer_focus = window;
1732
1733 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1734 *p = NULL;
1735
1736 window = input->pointer_focus;
1737 if (window->data_handler)
1738 window->data_handler(window, input, time, x, y,
1739 input->drag_offer->types.data,
1740 window->user_data);
1741}
1742
1743static void
1744data_device_leave(void *data, struct wl_data_device *data_device)
1745{
1746 struct input *input = data;
1747
1748 data_offer_destroy(input->drag_offer);
1749 input->drag_offer = NULL;
1750}
1751
1752static void
1753data_device_motion(void *data, struct wl_data_device *data_device,
1754 uint32_t time, int32_t x, int32_t y)
1755{
1756 struct input *input = data;
1757 struct window *window = input->pointer_focus;
1758
1759 input->sx = x;
1760 input->sy = y;
1761
1762 if (window->data_handler)
1763 window->data_handler(window, input, time, x, y,
1764 input->drag_offer->types.data,
1765 window->user_data);
1766}
1767
1768static void
1769data_device_drop(void *data, struct wl_data_device *data_device)
1770{
1771 struct input *input = data;
1772 struct window *window = input->pointer_focus;
1773
1774 if (window->drop_handler)
1775 window->drop_handler(window, input,
1776 input->sx, input->sy, window->user_data);
1777}
1778
1779static void
1780data_device_selection(void *data,
1781 struct wl_data_device *wl_data_device,
1782 struct wl_data_offer *offer)
1783{
1784 struct input *input = data;
1785 char **p;
1786
1787 if (input->selection_offer)
1788 data_offer_destroy(input->selection_offer);
1789
1790 input->selection_offer = wl_data_offer_get_user_data(offer);
1791 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1792 *p = NULL;
1793}
1794
1795static const struct wl_data_device_listener data_device_listener = {
1796 data_device_data_offer,
1797 data_device_enter,
1798 data_device_leave,
1799 data_device_motion,
1800 data_device_drop,
1801 data_device_selection
1802};
1803
1804struct wl_data_device *
1805input_get_data_device(struct input *input)
1806{
1807 return input->data_device;
1808}
1809
1810void
1811input_set_selection(struct input *input,
1812 struct wl_data_source *source, uint32_t time)
1813{
1814 wl_data_device_set_selection(input->data_device, source, time);
1815}
1816
1817void
1818input_accept(struct input *input, uint32_t time, const char *type)
1819{
1820 wl_data_offer_accept(input->drag_offer->offer, time, type);
1821}
1822
1823static void
1824offer_io_func(struct task *task, uint32_t events)
1825{
1826 struct data_offer *offer =
1827 container_of(task, struct data_offer, io_task);
1828 unsigned int len;
1829 char buffer[4096];
1830
1831 len = read(offer->fd, buffer, sizeof buffer);
1832 offer->func(buffer, len,
1833 offer->x, offer->y, offer->user_data);
1834
1835 if (len == 0) {
1836 close(offer->fd);
1837 data_offer_destroy(offer);
1838 }
1839}
1840
1841static void
1842data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1843 data_func_t func, void *user_data)
1844{
1845 int p[2];
1846
1847 pipe2(p, O_CLOEXEC);
1848 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1849 close(p[1]);
1850
1851 offer->io_task.run = offer_io_func;
1852 offer->fd = p[0];
1853 offer->func = func;
1854 offer->refcount++;
1855 offer->user_data = user_data;
1856
1857 display_watch_fd(offer->input->display,
1858 offer->fd, EPOLLIN, &offer->io_task);
1859}
1860
1861void
1862input_receive_drag_data(struct input *input, const char *mime_type,
1863 data_func_t func, void *data)
1864{
1865 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1866 input->drag_offer->x = input->sx;
1867 input->drag_offer->y = input->sy;
1868}
1869
1870int
1871input_receive_selection_data(struct input *input, const char *mime_type,
1872 data_func_t func, void *data)
1873{
1874 char **p;
1875
1876 if (input->selection_offer == NULL)
1877 return -1;
1878
1879 for (p = input->selection_offer->types.data; *p; p++)
1880 if (strcmp(mime_type, *p) == 0)
1881 break;
1882
1883 if (*p == NULL)
1884 return -1;
1885
1886 data_offer_receive_data(input->selection_offer,
1887 mime_type, func, data);
1888 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001889}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001890
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001891int
1892input_receive_selection_data_to_fd(struct input *input,
1893 const char *mime_type, int fd)
1894{
1895 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1896
1897 return 0;
1898}
1899
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001900void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001901window_move(struct window *window, struct input *input, uint32_t time)
1902{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001903 if (!window->shell_surface)
1904 return;
1905
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001906 wl_shell_surface_move(window->shell_surface,
1907 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001908}
1909
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001910static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001911window_resize(struct window *window, int32_t width, int32_t height)
1912{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001913 struct rectangle allocation;
1914
1915 if (window->decoration) {
1916 allocation.x = 20;
1917 allocation.y = 60;
1918 allocation.width = width - 20 - window->margin * 2;
1919 allocation.height = height - 60 - window->margin * 2;
1920 } else {
1921 allocation.x = 0;
1922 allocation.y = 0;
1923 allocation.width = width;
1924 allocation.height = height;
1925 }
1926
1927 window->allocation.width = width;
1928 window->allocation.height = height;
1929
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001930 widget_set_allocation(window->widget, allocation.x, allocation.y,
1931 allocation.width, allocation.height);
1932
1933 if (window->widget->resize_handler)
1934 window->widget->resize_handler(window->widget,
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001935 allocation.width,
1936 allocation.height,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001937 window->widget->user_data);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001938
1939 window_schedule_redraw(window);
1940}
1941
1942static void
1943idle_resize(struct task *task, uint32_t events)
1944{
1945 struct window *window =
1946 container_of(task, struct window, resize_task);
1947
1948 window_resize(window,
1949 window->allocation.width, window->allocation.height);
1950 window->resize_scheduled = 0;
1951}
1952
1953void
1954window_schedule_resize(struct window *window, int width, int height)
1955{
1956 if (!window->resize_scheduled) {
1957 window->allocation.width = width;
1958 window->allocation.height = height;
1959 window->resize_task.run = idle_resize;
1960 display_defer(window->display, &window->resize_task);
1961 window->resize_scheduled = 1;
1962 }
1963}
1964
1965static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001966handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001967 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001968 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001969{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001970 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001971
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001972 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001973 return;
1974
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001975 window->resize_edges = edges;
1976
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001977 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001978}
1979
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001980static void
1981handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1982{
1983 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001984 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001985 /* FIXME: Need more context in this event, at least the input
1986 * device. Or just use wl_callback. */
1987
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001988 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001989 window_destroy(window);
1990}
1991
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001992static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001993 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001994 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001995};
1996
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001997void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001998window_get_allocation(struct window *window,
1999 struct rectangle *allocation)
2000{
2001 *allocation = window->allocation;
2002}
2003
2004void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002005window_get_child_allocation(struct window *window,
2006 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002007{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002008 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002009 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002010 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002011 allocation->x = window->margin + 10;
2012 allocation->y = window->margin + 50;
2013 allocation->width =
2014 window->allocation.width - 20 - window->margin * 2;
2015 allocation->height =
2016 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002017 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002018}
2019
2020void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002021window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05002022{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002023 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002024 window->allocation.x = 20 + window->margin;
2025 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002026 window->allocation.width = width + 20 + window->margin * 2;
2027 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002028 } else {
2029 window->allocation.x = 0;
2030 window->allocation.y = 0;
2031 window->allocation.width = width;
2032 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002033 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05002034}
2035
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002036static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002037widget_redraw(struct widget *widget)
2038{
2039 struct widget *child;
2040
2041 if (widget->redraw_handler)
2042 widget->redraw_handler(widget, widget->user_data);
2043 wl_list_for_each(child, &widget->child_list, link)
2044 widget_redraw(child);
2045}
2046
2047static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002048idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002049{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002050 struct window *window =
2051 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002052
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002053 window_create_surface(window);
2054 if (window->decoration)
2055 window_draw_decorations(window);
2056
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002057 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002058
2059 window_flush(window);
2060
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002061 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002062}
2063
2064void
2065window_schedule_redraw(struct window *window)
2066{
2067 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002068 window->redraw_task.run = idle_redraw;
2069 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002070 window->redraw_scheduled = 1;
2071 }
2072}
2073
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002074void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002075window_set_custom(struct window *window)
2076{
2077 window->type = TYPE_CUSTOM;
2078}
2079
2080void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002081window_set_fullscreen(struct window *window, int fullscreen)
2082{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002083 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002084 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002085
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002086 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002087 return;
2088
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002089 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002090 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002091 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002092 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002093 width = output->allocation.width;
2094 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002095 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002096 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002097 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002098 width = window->saved_allocation.width - 20 - window->margin * 2;
2099 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002100 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002101 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002102
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002103 window_resize(window, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002104}
2105
2106void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002107window_set_decoration(struct window *window, int decoration)
2108{
2109 window->decoration = decoration;
2110}
2111
2112void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002113window_set_user_data(struct window *window, void *data)
2114{
2115 window->user_data = data;
2116}
2117
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002118void *
2119window_get_user_data(struct window *window)
2120{
2121 return window->user_data;
2122}
2123
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002124void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002125window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002126 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002127{
2128 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002129}
2130
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002131void
2132window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002133 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002134{
2135 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002136}
2137
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002138void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002139window_set_data_handler(struct window *window, window_data_handler_t handler)
2140{
2141 window->data_handler = handler;
2142}
2143
2144void
2145window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2146{
2147 window->drop_handler = handler;
2148}
2149
2150void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002151window_set_close_handler(struct window *window,
2152 window_close_handler_t handler)
2153{
2154 window->close_handler = handler;
2155}
2156
2157void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002158window_set_transparent(struct window *window, int transparent)
2159{
2160 window->transparent = transparent;
2161}
2162
2163void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002164window_set_title(struct window *window, const char *title)
2165{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002166 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002167 window->title = strdup(title);
2168}
2169
2170const char *
2171window_get_title(struct window *window)
2172{
2173 return window->title;
2174}
2175
2176void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002177display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2178 int32_t x, int32_t y, int32_t width, int32_t height)
2179{
2180 struct wl_buffer *buffer;
2181
2182 buffer = display_get_buffer_for_surface(display, cairo_surface);
2183
2184 wl_buffer_damage(buffer, x, y, width, height);
2185}
2186
2187void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002188window_damage(struct window *window, int32_t x, int32_t y,
2189 int32_t width, int32_t height)
2190{
2191 wl_surface_damage(window->surface, x, y, width, height);
2192}
2193
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002194static struct window *
2195window_create_internal(struct display *display, struct window *parent,
2196 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002197{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002198 struct window *window;
2199
2200 window = malloc(sizeof *window);
2201 if (window == NULL)
2202 return NULL;
2203
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002204 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002205 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002206 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002207 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002208 if (display->shell) {
2209 window->shell_surface =
2210 wl_shell_get_shell_surface(display->shell,
2211 window->surface);
2212 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002213 window->allocation.x = 0;
2214 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002215 window->allocation.width = width;
2216 window->allocation.height = height;
2217 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002218 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002219 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002220 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002221
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002222 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002223#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002224 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2225 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002226#else
2227 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2228#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002229 else
2230 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002231
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002232 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002233 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002234
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002235 if (window->shell_surface) {
2236 wl_shell_surface_set_user_data(window->shell_surface, window);
2237 wl_shell_surface_add_listener(window->shell_surface,
2238 &shell_surface_listener, window);
2239 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002240
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002241 return window;
2242}
2243
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002244struct window *
2245window_create(struct display *display, int32_t width, int32_t height)
2246{
2247 struct window *window;
2248
2249 window = window_create_internal(display, NULL, width, height);
2250 if (!window)
2251 return NULL;
2252
2253 return window;
2254}
2255
2256struct window *
2257window_create_transient(struct display *display, struct window *parent,
2258 int32_t x, int32_t y, int32_t width, int32_t height)
2259{
2260 struct window *window;
2261
2262 window = window_create_internal(parent->display,
2263 parent, width, height);
2264 if (!window)
2265 return NULL;
2266
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002267 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002268 window->x = x;
2269 window->y = y;
2270
2271 return window;
2272}
2273
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002274static int
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002275menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002276{
2277 int next;
2278
2279 next = (sy - 8) / 20;
2280 if (menu->current != next) {
2281 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002282 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002283 }
2284
2285 return POINTER_LEFT_PTR;
2286}
2287
2288static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002289menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002290 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002291 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002292{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002293 return menu_set_item(data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002294}
2295
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002296static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002297menu_enter_handler(struct widget *widget,
2298 struct input *input, uint32_t time,
2299 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002300{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002301 return menu_set_item(data, y);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002302}
2303
2304static void
2305menu_leave_handler(struct widget *widget, struct input *input, void *data)
2306{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002307 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002308}
2309
2310static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002311menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002312 struct input *input, uint32_t time,
2313 int button, int state, void *data)
2314
2315{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002316 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002317
2318 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002319 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002320 menu->func(menu->window->parent,
2321 menu->current, menu->window->parent->user_data);
2322 window_destroy(widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002323 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002324}
2325
2326static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002327menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002328{
2329 cairo_t *cr;
2330 const int32_t r = 3, margin = 3;
2331 struct menu *menu = data;
2332 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002333 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002334
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002335 cr = cairo_create(window->cairo_surface);
2336 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2337 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2338 cairo_paint(cr);
2339
2340 width = window->allocation.width;
2341 height = window->allocation.height;
2342 rounded_rect(cr, 0, 0, width, height, r);
2343 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2344 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2345 cairo_fill(cr);
2346
2347 for (i = 0; i < menu->count; i++) {
2348 if (i == menu->current) {
2349 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2350 cairo_rectangle(cr, margin, i * 20 + margin,
2351 width - 2 * margin, 20);
2352 cairo_fill(cr);
2353 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2354 cairo_move_to(cr, 10, i * 20 + 16);
2355 cairo_show_text(cr, menu->entries[i]);
2356 } else {
2357 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2358 cairo_move_to(cr, 10, i * 20 + 16);
2359 cairo_show_text(cr, menu->entries[i]);
2360 }
2361 }
2362
2363 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002364}
2365
2366struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002367window_create_menu(struct display *display,
2368 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002369 int32_t x, int32_t y,
2370 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002371{
2372 struct window *window;
2373 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002374 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002375
2376 menu = malloc(sizeof *menu);
2377 if (!menu)
2378 return NULL;
2379
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002380 window = window_create_internal(parent->display, parent,
2381 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002382 if (!window)
2383 return NULL;
2384
2385 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002386 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002387 menu->entries = entries;
2388 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002389 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002390 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002391 window->decoration = 0;
2392 window->type = TYPE_MENU;
2393 window->x = x;
2394 window->y = y;
2395
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002396 wl_shell_surface_set_popup(window->shell_surface,
2397 input->input_device, time,
2398 window->parent->shell_surface,
2399 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002400
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002401 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002402 widget_set_enter_handler(menu->widget, menu_enter_handler);
2403 widget_set_leave_handler(menu->widget, menu_leave_handler);
2404 widget_set_motion_handler(menu->widget, menu_motion_handler);
2405 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002406
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002407 return window;
2408}
2409
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002410void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002411window_set_buffer_type(struct window *window, enum window_buffer_type type)
2412{
2413 window->buffer_type = type;
2414}
2415
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002416
2417static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002418display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002419 struct wl_output *wl_output,
2420 int x, int y,
2421 int physical_width,
2422 int physical_height,
2423 int subpixel,
2424 const char *make,
2425 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002426{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002427 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002428
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002429 output->allocation.x = x;
2430 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002431}
2432
2433static void
2434display_handle_mode(void *data,
2435 struct wl_output *wl_output,
2436 uint32_t flags,
2437 int width,
2438 int height,
2439 int refresh)
2440{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002441 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002442 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002443
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002444 if (flags & WL_OUTPUT_MODE_CURRENT) {
2445 output->allocation.width = width;
2446 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002447 if (display->output_configure_handler)
2448 (*display->output_configure_handler)(
2449 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002450 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002451}
2452
2453static const struct wl_output_listener output_listener = {
2454 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002455 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002456};
2457
2458static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002459display_add_output(struct display *d, uint32_t id)
2460{
2461 struct output *output;
2462
2463 output = malloc(sizeof *output);
2464 if (output == NULL)
2465 return;
2466
2467 memset(output, 0, sizeof *output);
2468 output->display = d;
2469 output->output =
2470 wl_display_bind(d->display, id, &wl_output_interface);
2471 wl_list_insert(d->output_list.prev, &output->link);
2472
2473 wl_output_add_listener(output->output, &output_listener, output);
2474}
2475
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002476static void
2477output_destroy(struct output *output)
2478{
2479 if (output->destroy_handler)
2480 (*output->destroy_handler)(output, output->user_data);
2481
2482 wl_output_destroy(output->output);
2483 wl_list_remove(&output->link);
2484 free(output);
2485}
2486
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002487void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002488display_set_output_configure_handler(struct display *display,
2489 display_output_handler_t handler)
2490{
2491 struct output *output;
2492
2493 display->output_configure_handler = handler;
2494 if (!handler)
2495 return;
2496
2497 wl_list_for_each(output, &display->output_list, link)
2498 (*display->output_configure_handler)(output,
2499 display->user_data);
2500}
2501
2502void
2503output_set_user_data(struct output *output, void *data)
2504{
2505 output->user_data = data;
2506}
2507
2508void *
2509output_get_user_data(struct output *output)
2510{
2511 return output->user_data;
2512}
2513
2514void
2515output_set_destroy_handler(struct output *output,
2516 display_output_handler_t handler)
2517{
2518 output->destroy_handler = handler;
2519 /* FIXME: implement this, once we have way to remove outputs */
2520}
2521
2522void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002523output_get_allocation(struct output *output, struct rectangle *allocation)
2524{
2525 *allocation = output->allocation;
2526}
2527
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002528struct wl_output *
2529output_get_wl_output(struct output *output)
2530{
2531 return output->output;
2532}
2533
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002534static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002535display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002536{
2537 struct input *input;
2538
2539 input = malloc(sizeof *input);
2540 if (input == NULL)
2541 return;
2542
2543 memset(input, 0, sizeof *input);
2544 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002545 input->input_device =
2546 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002547 input->pointer_focus = NULL;
2548 input->keyboard_focus = NULL;
2549 wl_list_insert(d->input_list.prev, &input->link);
2550
2551 wl_input_device_add_listener(input->input_device,
2552 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002553 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002554
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002555 input->data_device =
2556 wl_data_device_manager_get_data_device(d->data_device_manager,
2557 input->input_device);
2558 wl_data_device_add_listener(input->data_device,
2559 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002560}
2561
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002562static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002563input_destroy(struct input *input)
2564{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002565 input_remove_keyboard_focus(input);
2566 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002567
2568 if (input->drag_offer)
2569 data_offer_destroy(input->drag_offer);
2570
2571 if (input->selection_offer)
2572 data_offer_destroy(input->selection_offer);
2573
2574 wl_data_device_destroy(input->data_device);
2575 wl_list_remove(&input->link);
2576 wl_input_device_destroy(input->input_device);
2577 free(input);
2578}
2579
2580static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002581display_handle_global(struct wl_display *display, uint32_t id,
2582 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002583{
2584 struct display *d = data;
2585
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002586 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002587 d->compositor =
2588 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002589 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002590 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002591 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002592 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002593 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002594 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002595 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002596 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002597 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2598 d->data_device_manager =
2599 wl_display_bind(display, id,
2600 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002601 }
2602}
2603
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002604static void
2605display_render_frame(struct display *d)
2606{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002607 int radius = 8;
2608 cairo_t *cr;
2609
2610 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2611 cr = cairo_create(d->shadow);
2612 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2613 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2614 rounded_rect(cr, 16, 16, 112, 112, radius);
2615 cairo_fill(cr);
2616 cairo_destroy(cr);
2617 blur_surface(d->shadow, 64);
2618
2619 d->active_frame =
2620 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2621 cr = cairo_create(d->active_frame);
2622 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2623 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2624 rounded_rect(cr, 16, 16, 112, 112, radius);
2625 cairo_fill(cr);
2626 cairo_destroy(cr);
2627
2628 d->inactive_frame =
2629 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2630 cr = cairo_create(d->inactive_frame);
2631 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2632 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2633 rounded_rect(cr, 16, 16, 112, 112, radius);
2634 cairo_fill(cr);
2635 cairo_destroy(cr);
2636}
2637
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002638static void
2639init_xkb(struct display *d)
2640{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002641 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002642
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002643 names.rules = "evdev";
2644 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002645 names.layout = option_xkb_layout;
2646 names.variant = option_xkb_variant;
2647 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002648
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002649 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002650 if (!d->xkb) {
2651 fprintf(stderr, "Failed to compile keymap\n");
2652 exit(1);
2653 }
2654}
2655
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002656static void
2657fini_xkb(struct display *display)
2658{
2659 xkb_free_keymap(display->xkb);
2660}
2661
Yuval Fledel45568f62010-12-06 09:18:12 -05002662static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002663init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002664{
2665 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002666 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002667
2668 static const EGLint premul_argb_cfg_attribs[] = {
2669 EGL_SURFACE_TYPE,
2670 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2671 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002672 EGL_RED_SIZE, 1,
2673 EGL_GREEN_SIZE, 1,
2674 EGL_BLUE_SIZE, 1,
2675 EGL_ALPHA_SIZE, 1,
2676 EGL_DEPTH_SIZE, 1,
2677 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2678 EGL_NONE
2679 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002680
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002681 static const EGLint rgb_cfg_attribs[] = {
2682 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2683 EGL_RED_SIZE, 1,
2684 EGL_GREEN_SIZE, 1,
2685 EGL_BLUE_SIZE, 1,
2686 EGL_ALPHA_SIZE, 0,
2687 EGL_DEPTH_SIZE, 1,
2688 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2689 EGL_NONE
2690 };
2691
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002692 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002693 if (!eglInitialize(d->dpy, &major, &minor)) {
2694 fprintf(stderr, "failed to initialize display\n");
2695 return -1;
2696 }
2697
2698 if (!eglBindAPI(EGL_OPENGL_API)) {
2699 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2700 return -1;
2701 }
2702
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002703 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002704 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002705 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002706 return -1;
2707 }
2708
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002709 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2710 &d->rgb_config, 1, &n) || n != 1) {
2711 fprintf(stderr, "failed to choose rgb config\n");
2712 return -1;
2713 }
2714
Benjamin Franzke0c991632011-09-27 21:57:31 +02002715 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2716 if (d->rgb_ctx == NULL) {
2717 fprintf(stderr, "failed to create context\n");
2718 return -1;
2719 }
2720 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2721 EGL_NO_CONTEXT, NULL);
2722 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002723 fprintf(stderr, "failed to create context\n");
2724 return -1;
2725 }
2726
Benjamin Franzke0c991632011-09-27 21:57:31 +02002727 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002728 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002729 return -1;
2730 }
2731
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002732#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002733 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2734 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002735 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002736 return -1;
2737 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002738 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2739 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2740 fprintf(stderr, "failed to get cairo egl argb device\n");
2741 return -1;
2742 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002743#endif
2744
2745 return 0;
2746}
2747
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002748static void
2749fini_egl(struct display *display)
2750{
2751#ifdef HAVE_CAIRO_EGL
2752 cairo_device_destroy(display->argb_device);
2753 cairo_device_destroy(display->rgb_device);
2754#endif
2755
2756 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2757 EGL_NO_CONTEXT);
2758
2759 eglTerminate(display->dpy);
2760 eglReleaseThread();
2761}
2762
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002763static int
2764event_mask_update(uint32_t mask, void *data)
2765{
2766 struct display *d = data;
2767
2768 d->mask = mask;
2769
2770 return 0;
2771}
2772
2773static void
2774handle_display_data(struct task *task, uint32_t events)
2775{
2776 struct display *display =
2777 container_of(task, struct display, display_task);
2778
2779 wl_display_iterate(display->display, display->mask);
2780}
2781
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002782struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002783display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002784{
2785 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002786 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002787 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002788 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002789
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002790 g_type_init();
2791
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002792 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002793 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002794 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002795
2796 xkb_option_group = g_option_group_new("xkb",
2797 "Keyboard options",
2798 "Show all XKB options",
2799 NULL, NULL);
2800 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2801 g_option_context_add_group (context, xkb_option_group);
2802
2803 if (!g_option_context_parse(context, argc, argv, &error)) {
2804 fprintf(stderr, "option parsing failed: %s\n", error->message);
2805 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002806 }
2807
Tim Wiederhake748f6722011-01-23 23:25:25 +01002808 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002809
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002810 d = malloc(sizeof *d);
2811 if (d == NULL)
2812 return NULL;
2813
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002814 memset(d, 0, sizeof *d);
2815
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002816 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002817 if (d->display == NULL) {
2818 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002819 return NULL;
2820 }
2821
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002822 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2823 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2824 d->display_task.run = handle_display_data;
2825 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2826
2827 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002828 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002829 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002830
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002831 /* Set up listener so we'll catch all events. */
2832 wl_display_add_global_listener(d->display,
2833 display_handle_global, d);
2834
2835 /* Process connection events. */
2836 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002837 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002838 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002839
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002840 d->image_target_texture_2d =
2841 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2842 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2843 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2844
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002845 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002846
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002847 display_render_frame(d);
2848
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002849 wl_list_init(&d->window_list);
2850
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002851 init_xkb(d);
2852
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002853 return d;
2854}
2855
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002856static void
2857display_destroy_outputs(struct display *display)
2858{
2859 struct output *tmp;
2860 struct output *output;
2861
2862 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2863 output_destroy(output);
2864}
2865
Pekka Paalanene1207c72011-12-16 12:02:09 +02002866static void
2867display_destroy_inputs(struct display *display)
2868{
2869 struct input *tmp;
2870 struct input *input;
2871
2872 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2873 input_destroy(input);
2874}
2875
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002876void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002877display_destroy(struct display *display)
2878{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002879 if (!wl_list_empty(&display->window_list))
2880 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2881
2882 if (!wl_list_empty(&display->deferred_list))
2883 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2884
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002885 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002886 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002887
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002888 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002889
2890 cairo_surface_destroy(display->active_frame);
2891 cairo_surface_destroy(display->inactive_frame);
2892 cairo_surface_destroy(display->shadow);
2893 destroy_pointer_surfaces(display);
2894
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002895 fini_egl(display);
2896
Pekka Paalanenc2052982011-12-16 11:41:32 +02002897 if (display->shell)
2898 wl_shell_destroy(display->shell);
2899
2900 if (display->shm)
2901 wl_shm_destroy(display->shm);
2902
2903 if (display->data_device_manager)
2904 wl_data_device_manager_destroy(display->data_device_manager);
2905
2906 wl_compositor_destroy(display->compositor);
2907
2908 close(display->epoll_fd);
2909
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002910 wl_display_flush(display->display);
2911 wl_display_destroy(display->display);
2912 free(display);
2913}
2914
2915void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002916display_set_user_data(struct display *display, void *data)
2917{
2918 display->user_data = data;
2919}
2920
2921void *
2922display_get_user_data(struct display *display)
2923{
2924 return display->user_data;
2925}
2926
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002927struct wl_display *
2928display_get_display(struct display *display)
2929{
2930 return display->display;
2931}
2932
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002933struct output *
2934display_get_output(struct display *display)
2935{
2936 return container_of(display->output_list.next, struct output, link);
2937}
2938
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002939struct wl_compositor *
2940display_get_compositor(struct display *display)
2941{
2942 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002943}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002944
2945EGLDisplay
2946display_get_egl_display(struct display *d)
2947{
2948 return d->dpy;
2949}
2950
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002951struct wl_data_source *
2952display_create_data_source(struct display *display)
2953{
2954 return wl_data_device_manager_create_data_source(display->data_device_manager);
2955}
2956
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002957EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002958display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002959{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002960 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002961}
2962
Benjamin Franzke0c991632011-09-27 21:57:31 +02002963EGLConfig
2964display_get_argb_egl_config(struct display *d)
2965{
2966 return d->premultiplied_argb_config;
2967}
2968
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002969struct wl_shell *
2970display_get_shell(struct display *display)
2971{
2972 return display->shell;
2973}
2974
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002975int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002976display_acquire_window_surface(struct display *display,
2977 struct window *window,
2978 EGLContext ctx)
2979{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002980#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002981 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002982 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002983
2984 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002985 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002986 device = cairo_surface_get_device(window->cairo_surface);
2987 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002988 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002989
Benjamin Franzke0c991632011-09-27 21:57:31 +02002990 if (!ctx) {
2991 if (device == display->rgb_device)
2992 ctx = display->rgb_ctx;
2993 else if (device == display->argb_device)
2994 ctx = display->argb_ctx;
2995 else
2996 assert(0);
2997 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002998
2999 data = cairo_surface_get_user_data(window->cairo_surface,
3000 &surface_data_key);
3001
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003002 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003003 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003004 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3005 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003006
3007 return 0;
3008#else
3009 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003010#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003011}
3012
3013void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003014display_release_window_surface(struct display *display,
3015 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003016{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003017#ifdef HAVE_CAIRO_EGL
3018 cairo_device_t *device;
3019
3020 device = cairo_surface_get_device(window->cairo_surface);
3021 if (!device)
3022 return;
3023
3024 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003025 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003026 cairo_device_release(device);
3027#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003028}
3029
3030void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003031display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003032{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003033 wl_list_insert(&display->deferred_list, &task->link);
3034}
3035
3036void
3037display_watch_fd(struct display *display,
3038 int fd, uint32_t events, struct task *task)
3039{
3040 struct epoll_event ep;
3041
3042 ep.events = events;
3043 ep.data.ptr = task;
3044 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3045}
3046
3047void
3048display_run(struct display *display)
3049{
3050 struct task *task;
3051 struct epoll_event ep[16];
3052 int i, count;
3053
Pekka Paalanen826d7952011-12-15 10:14:07 +02003054 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003055 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003056 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003057
3058 while (!wl_list_empty(&display->deferred_list)) {
3059 task = container_of(display->deferred_list.next,
3060 struct task, link);
3061 wl_list_remove(&task->link);
3062 task->run(task, 0);
3063 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003064
3065 if (!display->running)
3066 break;
3067
3068 wl_display_flush(display->display);
3069
3070 count = epoll_wait(display->epoll_fd,
3071 ep, ARRAY_LENGTH(ep), -1);
3072 for (i = 0; i < count; i++) {
3073 task = ep[i].data.ptr;
3074 task->run(task, ep[i].events);
3075 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003076 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003077}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003078
3079void
3080display_exit(struct display *display)
3081{
3082 display->running = 0;
3083}