blob: 0e95f8e9711eba1c483b6f08736cf6b65c2e923e [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øgsberg43c28ee2009-01-26 23:42:46 -050065 struct wl_output *output;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020069 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 EGLContext rgb_ctx;
71 EGLContext argb_ctx;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040074
75 int display_fd;
76 uint32_t mask;
77 struct task display_task;
78
79 int epoll_fd;
80 struct wl_list deferred_list;
81
Pekka Paalanen826d7952011-12-15 10:14:07 +020082 int running;
83
Kristian Høgsberg478d9262010-06-08 20:34:11 -040084 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040085 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050086 struct wl_list output_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040088 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040089 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040090 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040091
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050092 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
93 PFNEGLCREATEIMAGEKHRPROC create_image;
94 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020095
96 display_output_handler_t output_configure_handler;
97
98 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050099};
100
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400101enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400103 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500105 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400106 TYPE_CUSTOM
107};
108
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500109struct window {
110 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500111 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500112 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500114 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500115 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500116 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400117 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400118 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400119 struct task redraw_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øgsberga85fe3c2010-06-08 14:08:30 -0400129 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500130 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500131
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500132 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400133 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500134 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400135 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500136 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400137 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400138 window_enter_handler_t enter_handler;
139 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140 window_item_focus_handler_t item_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400141 window_data_handler_t data_handler;
142 window_drop_handler_t drop_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400143
144 struct wl_list item_list;
145 struct item *focus_item;
146 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400147
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500148 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400149 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500150};
151
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400152struct item {
153 struct wl_list link;
154 struct rectangle allocation;
155 void *user_data;
156};
157
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400158struct input {
159 struct display *display;
160 struct wl_input_device *input_device;
161 struct window *pointer_focus;
162 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400163 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164 uint32_t modifiers;
165 int32_t x, y, sx, sy;
166 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400167
168 struct wl_data_device *data_device;
169 struct data_offer *drag_offer;
170 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171};
172
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500173struct output {
174 struct display *display;
175 struct wl_output *output;
176 struct rectangle allocation;
177 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200178
179 display_output_handler_t destroy_handler;
180 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500181};
182
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400183enum {
184 POINTER_DEFAULT = 100,
185 POINTER_UNSET
186};
187
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500188enum window_location {
189 WINDOW_INTERIOR = 0,
190 WINDOW_RESIZING_TOP = 1,
191 WINDOW_RESIZING_BOTTOM = 2,
192 WINDOW_RESIZING_LEFT = 4,
193 WINDOW_RESIZING_TOP_LEFT = 5,
194 WINDOW_RESIZING_BOTTOM_LEFT = 6,
195 WINDOW_RESIZING_RIGHT = 8,
196 WINDOW_RESIZING_TOP_RIGHT = 9,
197 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
198 WINDOW_RESIZING_MASK = 15,
199 WINDOW_EXTERIOR = 16,
200 WINDOW_TITLEBAR = 17,
201 WINDOW_CLIENT_AREA = 18,
202};
203
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400204const char *option_xkb_layout = "us";
205const char *option_xkb_variant = "";
206const char *option_xkb_options = "";
207
208static const GOptionEntry xkb_option_entries[] = {
209 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
210 &option_xkb_layout, "XKB Layout" },
211 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
212 &option_xkb_variant, "XKB Variant" },
213 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
214 &option_xkb_options, "XKB Options" },
215 { NULL }
216};
217
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400218static const cairo_user_data_key_t surface_data_key;
219struct surface_data {
220 struct wl_buffer *buffer;
221};
222
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500223#define MULT(_d,c,a,t) \
224 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
225
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500226#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400227
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100228struct egl_window_surface_data {
229 struct display *display;
230 struct wl_surface *surface;
231 struct wl_egl_window *window;
232 EGLSurface surf;
233};
234
235static void
236egl_window_surface_data_destroy(void *p)
237{
238 struct egl_window_surface_data *data = p;
239 struct display *d = data->display;
240
241 eglDestroySurface(d->dpy, data->surf);
242 wl_egl_window_destroy(data->window);
243 data->surface = NULL;
244
245 free(p);
246}
247
248static cairo_surface_t *
249display_create_egl_window_surface(struct display *display,
250 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400251 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100252 struct rectangle *rectangle)
253{
254 cairo_surface_t *cairo_surface;
255 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400256 EGLConfig config;
257 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200258 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100259
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400260 static const EGLint premul_attribs[] = {
261 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
262 EGL_NONE
263 };
264
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100265 data = malloc(sizeof *data);
266 if (data == NULL)
267 return NULL;
268
269 data->display = display;
270 data->surface = surface;
271
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400272 if (flags & SURFACE_OPAQUE) {
273 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200274 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200275 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 } else {
277 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200278 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400279 attribs = premul_attribs;
280 }
281
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400282 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100283 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400284 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100285
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 data->surf = eglCreateWindowSurface(display->dpy, config,
287 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100288
Benjamin Franzke0c991632011-09-27 21:57:31 +0200289 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100290 data->surf,
291 rectangle->width,
292 rectangle->height);
293
294 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
295 data, egl_window_surface_data_destroy);
296
297 return cairo_surface;
298}
299
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200302 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400303 EGLImageKHR image;
304 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800305 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500306 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400307};
308
309static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400311{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800313 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314
Benjamin Franzke0c991632011-09-27 21:57:31 +0200315 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200317 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800318
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500319 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500321 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323}
324
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500325EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500326display_get_image_for_egl_image_surface(struct display *display,
327 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500328{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500330
331 data = cairo_surface_get_user_data (surface, &surface_data_key);
332
333 return data->image;
334}
335
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500336static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400338 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342 EGLDisplay dpy = display->dpy;
343 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200344 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345
346 data = malloc(sizeof *data);
347 if (data == NULL)
348 return NULL;
349
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800350 data->display = display;
351
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400352 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400353 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000355 free(data);
356 return NULL;
357 }
358
Benjamin Franzke0c991632011-09-27 21:57:31 +0200359 if (flags & SURFACE_OPAQUE) {
360 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200361 content = CAIRO_CONTENT_COLOR;
362 } else {
363 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200364 content = CAIRO_CONTENT_COLOR_ALPHA;
365 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500367 data->image = display->create_image(dpy, NULL,
368 EGL_NATIVE_PIXMAP_KHR,
369 (EGLClientBuffer) data->pixmap,
370 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500371 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500372 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500373 free(data);
374 return NULL;
375 }
376
377 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400378 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500379
Benjamin Franzke0c991632011-09-27 21:57:31 +0200380 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400381 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400382 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500383 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385
Benjamin Franzke0c991632011-09-27 21:57:31 +0200386 surface = cairo_gl_surface_create_for_texture(data->device,
387 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388 data->texture,
389 rectangle->width,
390 rectangle->height);
391
392 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500393 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394
395 return surface;
396}
397
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500398static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500399display_create_egl_image_surface_from_file(struct display *display,
400 const char *filename,
401 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402{
403 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400404 GdkPixbuf *pixbuf;
405 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400406 int stride, i;
407 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500408 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400409
410 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400412 FALSE, &error);
413 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400416 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
417 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500418 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400420 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400422
423 stride = gdk_pixbuf_get_rowstride(pixbuf);
424 pixels = gdk_pixbuf_get_pixels(pixbuf);
425
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400427 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 while (p < end) {
430 unsigned int t;
431
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432 MULT(p[0], p[0], p[3], t);
433 MULT(p[1], p[1], p[3], t);
434 MULT(p[2], p[2], p[3], t);
435 p += 4;
436
437 }
438 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400439
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200440 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000441 if (surface == NULL) {
442 g_object_unref(pixbuf);
443 return NULL;
444 }
445
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800446 data = cairo_surface_get_user_data(surface, &surface_data_key);
447
Benjamin Franzke0c991632011-09-27 21:57:31 +0200448 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800449 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800450 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
451 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200452 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400453
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500454 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400455
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400456 return surface;
457}
458
459#endif
460
461struct wl_buffer *
462display_get_buffer_for_surface(struct display *display,
463 cairo_surface_t *surface)
464{
465 struct surface_data *data;
466
467 data = cairo_surface_get_user_data (surface, &surface_data_key);
468
469 return data->buffer;
470}
471
472struct shm_surface_data {
473 struct surface_data data;
474 void *map;
475 size_t length;
476};
477
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500478static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479shm_surface_data_destroy(void *p)
480{
481 struct shm_surface_data *data = p;
482
483 wl_buffer_destroy(data->data.buffer);
484 munmap(data->map, data->length);
485}
486
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500487static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400489 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490{
491 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400492 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400493 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800494 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400495 char filename[] = "/tmp/wayland-shm-XXXXXX";
496
497 data = malloc(sizeof *data);
498 if (data == NULL)
499 return NULL;
500
501 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
502 rectangle->width);
503 data->length = stride * rectangle->height;
504 fd = mkstemp(filename);
505 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000506 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507 return NULL;
508 }
509 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000510 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511 close(fd);
512 return NULL;
513 }
514
515 data->map = mmap(NULL, data->length,
516 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
517 unlink(filename);
518
519 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000520 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521 close(fd);
522 return NULL;
523 }
524
525 surface = cairo_image_surface_create_for_data (data->map,
526 CAIRO_FORMAT_ARGB32,
527 rectangle->width,
528 rectangle->height,
529 stride);
530
531 cairo_surface_set_user_data (surface, &surface_data_key,
532 data, shm_surface_data_destroy);
533
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400534 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400535 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400536 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400537 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400538
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539 data->data.buffer = wl_shm_create_buffer(display->shm,
540 fd,
541 rectangle->width,
542 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400543 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
545 close(fd);
546
547 return surface;
548}
549
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500550static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551display_create_shm_surface_from_file(struct display *display,
552 const char *filename,
553 struct rectangle *rect)
554{
555 cairo_surface_t *surface;
556 GdkPixbuf *pixbuf;
557 GError *error = NULL;
558 int stride, i;
559 unsigned char *pixels, *p, *end, *dest_data;
560 int dest_stride;
561 uint32_t *d;
562
563 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
564 rect->width, rect->height,
565 FALSE, &error);
566 if (error != NULL)
567 return NULL;
568
569 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
570 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500571 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400572 return NULL;
573 }
574
575 stride = gdk_pixbuf_get_rowstride(pixbuf);
576 pixels = gdk_pixbuf_get_pixels(pixbuf);
577
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400578 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000579 if (surface == NULL) {
580 g_object_unref(pixbuf);
581 return NULL;
582 }
583
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584 dest_data = cairo_image_surface_get_data (surface);
585 dest_stride = cairo_image_surface_get_stride (surface);
586
587 for (i = 0; i < rect->height; i++) {
588 d = (uint32_t *) (dest_data + i * dest_stride);
589 p = pixels + i * stride;
590 end = p + rect->width * 4;
591 while (p < end) {
592 unsigned int t;
593 unsigned char a, r, g, b;
594
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595 a = p[3];
596 MULT(r, p[0], a, t);
597 MULT(g, p[1], a, t);
598 MULT(b, p[2], a, t);
599 p += 4;
600 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
601 }
602 }
603
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500604 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605
606 return surface;
607}
608
nobled7b87cb02011-02-01 18:51:47 +0000609static int
610check_size(struct rectangle *rect)
611{
612 if (rect->width && rect->height)
613 return 0;
614
615 fprintf(stderr, "tried to create surface of "
616 "width: %d, height: %d\n", rect->width, rect->height);
617 return -1;
618}
619
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620cairo_surface_t *
621display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400623 struct rectangle *rectangle,
624 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625{
nobled7b87cb02011-02-01 18:51:47 +0000626 if (check_size(rectangle) < 0)
627 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500628#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500629 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630 if (surface)
631 return display_create_egl_window_surface(display,
632 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400633 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 rectangle);
635 else
636 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400637 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500639 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400641 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642}
643
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500644static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645display_create_surface_from_file(struct display *display,
646 const char *filename,
647 struct rectangle *rectangle)
648{
nobled7b87cb02011-02-01 18:51:47 +0000649 if (check_size(rectangle) < 0)
650 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500651#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500652 if (display->dpy) {
653 return display_create_egl_image_surface_from_file(display,
654 filename,
655 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500656 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500658 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400659}
Yuval Fledel45568f62010-12-06 09:18:12 -0500660 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400661 const char *filename;
662 int hotspot_x, hotspot_y;
663} pointer_images[] = {
664 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
665 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
666 { DATADIR "/wayland/bottom_side.png", 16, 20 },
667 { DATADIR "/wayland/grabbing.png", 20, 17 },
668 { DATADIR "/wayland/left_ptr.png", 10, 5 },
669 { DATADIR "/wayland/left_side.png", 10, 20 },
670 { DATADIR "/wayland/right_side.png", 30, 19 },
671 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
672 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
673 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400674 { DATADIR "/wayland/xterm.png", 15, 15 },
675 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400676};
677
678static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400679create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400680{
681 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400682 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400683 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400684
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400685 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400686 display->pointer_surfaces =
687 malloc(count * sizeof *display->pointer_surfaces);
688 rect.width = width;
689 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400690 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692 display_create_surface_from_file(display,
693 pointer_images[i].filename,
694 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100695 if (!display->pointer_surfaces[i]) {
696 fprintf(stderr, "Error loading pointer image: %s\n",
697 pointer_images[i].filename);
698 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699 }
700
701}
702
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703cairo_surface_t *
704display_get_pointer_surface(struct display *display, int pointer,
705 int *width, int *height,
706 int *hotspot_x, int *hotspot_y)
707{
708 cairo_surface_t *surface;
709
710 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500711#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400712 *width = cairo_gl_surface_get_width(surface);
713 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000714#else
715 *width = cairo_image_surface_get_width(surface);
716 *height = cairo_image_surface_get_height(surface);
717#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400718 *hotspot_x = pointer_images[pointer].hotspot_x;
719 *hotspot_y = pointer_images[pointer].hotspot_y;
720
721 return cairo_surface_reference(surface);
722}
723
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400724static void
725window_attach_surface(struct window *window);
726
727static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400728free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400729{
730 struct window *window = data;
731
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400732 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400733 cairo_surface_destroy(window->pending_surface);
734 window->pending_surface = NULL;
735 if (window->cairo_surface)
736 window_attach_surface(window);
737}
738
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400739static const struct wl_callback_listener free_surface_listener = {
740 free_surface
741};
742
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500743static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200744window_get_resize_dx_dy(struct window *window, int *x, int *y)
745{
746 if (window->resize_edges & WINDOW_RESIZING_LEFT)
747 *x = window->server_allocation.width - window->allocation.width;
748 else
749 *x = 0;
750
751 if (window->resize_edges & WINDOW_RESIZING_TOP)
752 *y = window->server_allocation.height -
753 window->allocation.height;
754 else
755 *y = 0;
756
757 window->resize_edges = 0;
758}
759
760static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400761window_set_type(struct window *window)
762{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200763 if (!window->shell_surface)
764 return;
765
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400766 switch (window->type) {
767 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200768 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400769 break;
770 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200771 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400772 break;
773 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200774 wl_shell_surface_set_transient(window->shell_surface,
775 window->parent->shell_surface,
776 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400777 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500778 case TYPE_MENU:
779 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400780 case TYPE_CUSTOM:
781 break;
782 }
783}
784
785static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500786window_attach_surface(struct window *window)
787{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400788 struct display *display = window->display;
789 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400790 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000791#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500794 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400796 if (display->shell)
797 window_set_type(window);
798
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000800#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
802 data = cairo_surface_get_user_data(window->cairo_surface,
803 &surface_data_key);
804
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805 cairo_gl_surface_swapbuffers(window->cairo_surface);
806 wl_egl_window_get_attached_size(data->window,
807 &window->server_allocation.width,
808 &window->server_allocation.height);
809 break;
810 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000811#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200813 window_get_resize_dx_dy(window, &x, &y);
814
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 if (window->pending_surface != NULL)
816 return;
817
818 window->pending_surface = window->cairo_surface;
819 window->cairo_surface = NULL;
820
821 buffer =
822 display_get_buffer_for_surface(display,
823 window->pending_surface);
824
825 wl_surface_attach(window->surface, buffer, x, y);
826 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400827 cb = wl_display_sync(display->display);
828 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830 default:
831 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100832 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500833
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500834 wl_surface_damage(window->surface, 0, 0,
835 window->allocation.width,
836 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500837}
838
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500839void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400840window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500841{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100842 if (window->cairo_surface) {
843 switch (window->buffer_type) {
844 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
845 case WINDOW_BUFFER_TYPE_SHM:
846 display_surface_damage(window->display,
847 window->cairo_surface,
848 0, 0,
849 window->allocation.width,
850 window->allocation.height);
851 break;
852 default:
853 break;
854 }
855 window_attach_surface(window);
856 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500857}
858
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400859void
860window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500862 cairo_surface_reference(surface);
863
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400864 if (window->cairo_surface != NULL)
865 cairo_surface_destroy(window->cairo_surface);
866
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500867 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400868}
869
Benjamin Franzke22d54812011-07-16 19:50:32 +0000870#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100871static void
872window_resize_cairo_window_surface(struct window *window)
873{
874 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200875 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876
877 data = cairo_surface_get_user_data(window->cairo_surface,
878 &surface_data_key);
879
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200880 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100881 wl_egl_window_resize(data->window,
882 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200883 window->allocation.height,
884 x,y);
885
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100886 cairo_gl_surface_set_size(window->cairo_surface,
887 window->allocation.width,
888 window->allocation.height);
889}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000890#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100891
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400892struct display *
893window_get_display(struct window *window)
894{
895 return window->display;
896}
897
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400898void
899window_create_surface(struct window *window)
900{
901 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400902 uint32_t flags = 0;
903
904 if (!window->transparent)
905 flags = SURFACE_OPAQUE;
906
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400907 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500908#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
910 if (window->cairo_surface) {
911 window_resize_cairo_window_surface(window);
912 return;
913 }
914 surface = display_create_surface(window->display,
915 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400916 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100917 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500918 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400919 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100920 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400921 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922 break;
923#endif
924 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400925 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400926 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400927 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800928 default:
929 surface = NULL;
930 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400932
933 window_set_surface(window, surface);
934 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400935}
936
937static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500938window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500939{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500940 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500941 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400942 cairo_surface_t *frame;
943 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500944
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400945 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400946
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400947 width = window->allocation.width;
948 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500949
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500950 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500951
Kristian Høgsberg09531622010-06-14 23:22:15 -0400952 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400953 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400954 cairo_paint(cr);
955
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400956 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400957 tile_mask(cr, window->display->shadow,
958 shadow_dx, shadow_dy, width, height,
959 window->margin + 10 - shadow_dx,
960 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500961
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400962 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400963 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400964 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400965 frame = window->display->inactive_frame;
966
967 tile_source(cr, frame, 0, 0, width, height,
968 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500969
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500970 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
971 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500972 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400973 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400974 if (window->keyboard_device)
975 cairo_set_source_rgb(cr, 0, 0, 0);
976 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400977 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400978 cairo_show_text(cr, window->title);
979
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500980 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500981}
982
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400983void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500984window_destroy(struct window *window)
985{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200986 struct display *display = window->display;
987 struct input *input;
988
989 if (window->redraw_scheduled)
990 wl_list_remove(&window->redraw_task.link);
991
992 wl_list_for_each(input, &display->input_list, link) {
993 if (input->pointer_focus == window)
994 input->pointer_focus = NULL;
995 if (input->keyboard_focus == window)
996 input->keyboard_focus = NULL;
997 }
998
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200999 if (window->shell_surface)
1000 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001001 wl_surface_destroy(window->surface);
1002 wl_list_remove(&window->link);
1003 free(window);
1004}
1005
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001006static struct item *
1007window_find_item(struct window *window, int32_t x, int32_t y)
1008{
1009 struct item *item;
1010
1011 wl_list_for_each(item, &window->item_list, link) {
1012 if (item->allocation.x <= x &&
1013 x < item->allocation.x + item->allocation.width &&
1014 item->allocation.y <= y &&
1015 y < item->allocation.y + item->allocation.height) {
1016 return item;
1017 }
1018 }
1019
1020 return NULL;
1021}
1022
1023struct item *
1024window_add_item(struct window *window, void *data)
1025{
1026 struct item *item;
1027
1028 item = malloc(sizeof *item);
1029 memset(item, 0, sizeof *item);
1030 item->user_data = data;
1031 wl_list_insert(window->item_list.prev, &item->link);
1032
1033 return item;
1034}
1035
1036void
1037window_for_each_item(struct window *window, item_func_t func, void *data)
1038{
1039 struct item *item;
1040
1041 wl_list_for_each(item, &window->item_list, link)
1042 func(item, data);
1043}
1044
1045struct item *
1046window_get_focus_item(struct window *window)
1047{
1048 return window->focus_item;
1049}
1050
1051void
1052item_get_allocation(struct item *item, struct rectangle *allocation)
1053{
1054 *allocation = item->allocation;
1055}
1056
1057void
1058item_set_allocation(struct item *item,
1059 int32_t x, int32_t y, int32_t width, int32_t height)
1060{
1061 item->allocation.x = x;
1062 item->allocation.y = y;
1063 item->allocation.width = width;
1064 item->allocation.height = height;
1065}
1066
1067void *
1068item_get_user_data(struct item *item)
1069{
1070 return item->user_data;
1071}
1072
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001073void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001074window_draw(struct window *window)
1075{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001076 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001077 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001078 else
1079 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001080}
1081
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001082cairo_surface_t *
1083window_get_surface(struct window *window)
1084{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001085 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001086}
1087
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001088struct wl_surface *
1089window_get_wl_surface(struct window *window)
1090{
1091 return window->surface;
1092}
1093
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001094struct wl_shell_surface *
1095window_get_wl_shell_surface(struct window *window)
1096{
1097 return window->shell_surface;
1098}
1099
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001100static int
1101get_pointer_location(struct window *window, int32_t x, int32_t y)
1102{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104 const int grip_size = 8;
1105
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001106 if (!window->decoration)
1107 return WINDOW_CLIENT_AREA;
1108
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111 else if (window->margin <= x && x < window->margin + grip_size)
1112 hlocation = WINDOW_RESIZING_LEFT;
1113 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115 else if (x < window->allocation.width - window->margin)
1116 hlocation = WINDOW_RESIZING_RIGHT;
1117 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119
1120 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001121 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001122 else if (window->margin <= y && y < window->margin + grip_size)
1123 vlocation = WINDOW_RESIZING_TOP;
1124 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 else if (y < window->allocation.height - window->margin)
1127 vlocation = WINDOW_RESIZING_BOTTOM;
1128 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001129 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001130
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 location = vlocation | hlocation;
1132 if (location & WINDOW_EXTERIOR)
1133 location = WINDOW_EXTERIOR;
1134 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1135 location = WINDOW_TITLEBAR;
1136 else if (location == WINDOW_INTERIOR)
1137 location = WINDOW_CLIENT_AREA;
1138
1139 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140}
1141
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001142static int
1143input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001145 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001146
1147 location = get_pointer_location(input->pointer_focus,
1148 input->sx, input->sy);
1149 switch (location) {
1150 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001151 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001152 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001153 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001155 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001156 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001157 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001159 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001160 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001161 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001162 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001163 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001164 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001165 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001166 case WINDOW_EXTERIOR:
1167 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001168 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001170 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001172}
1173
1174void
1175input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1176{
1177 struct display *display = input->display;
1178 struct wl_buffer *buffer;
1179 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001180
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001181 if (pointer == input->current_pointer_image)
1182 return;
1183
1184 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001185 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001186
1187 if (!surface)
1188 return;
1189
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001190 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001191 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001192 pointer_images[pointer].hotspot_x,
1193 pointer_images[pointer].hotspot_y);
1194}
1195
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001196static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001197window_set_focus_item(struct window *window, struct item *focus)
1198{
1199 void *data;
1200
1201 if (focus == window->focus_item)
1202 return;
1203
1204 window->focus_item = focus;
1205 data = focus ? focus->user_data : NULL;
1206 if (window->item_focus_handler)
1207 window->item_focus_handler(window, focus, data);
1208}
1209
1210static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001211window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001212 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001213 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001214{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001215 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001217 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001218 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001219
1220 input->x = x;
1221 input->y = y;
1222 input->sx = sx;
1223 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001224
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001225 if (!window->focus_item || !window->item_grab_button) {
1226 item = window_find_item(window, sx, sy);
1227 window_set_focus_item(window, item);
1228 }
1229
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001230 if (window->motion_handler)
1231 pointer = (*window->motion_handler)(window, input, time,
1232 x, y, sx, sy,
1233 window->user_data);
1234
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001235 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001236 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001237}
1238
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001239static void
1240window_handle_button(void *data,
1241 struct wl_input_device *input_device,
1242 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001243{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001244 struct input *input = data;
1245 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001246 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001247 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001248
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001249 if (window->focus_item && window->item_grab_button == 0 && state)
1250 window->item_grab_button = button;
1251
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001253
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001254 if (window->display->shell &&
1255 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001257 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001258 if (!window->shell_surface)
1259 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001260 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001261 wl_shell_surface_move(window->shell_surface,
1262 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001263 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001264 case WINDOW_RESIZING_TOP:
1265 case WINDOW_RESIZING_BOTTOM:
1266 case WINDOW_RESIZING_LEFT:
1267 case WINDOW_RESIZING_RIGHT:
1268 case WINDOW_RESIZING_TOP_LEFT:
1269 case WINDOW_RESIZING_TOP_RIGHT:
1270 case WINDOW_RESIZING_BOTTOM_LEFT:
1271 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001272 if (!window->shell_surface)
1273 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001274 wl_shell_surface_resize(window->shell_surface,
1275 input_device, time,
1276 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001277 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001278 case WINDOW_CLIENT_AREA:
1279 if (window->button_handler)
1280 (*window->button_handler)(window,
1281 input, time,
1282 button, state,
1283 window->user_data);
1284 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001285 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001286 } else {
1287 if (window->button_handler)
1288 (*window->button_handler)(window,
1289 input, time,
1290 button, state,
1291 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001292 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293
1294 if (window->focus_item &&
1295 window->item_grab_button == button && !state) {
1296 window->item_grab_button = 0;
1297 item = window_find_item(window, input->sx, input->sy);
1298 window_set_focus_item(window, item);
1299 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001300}
1301
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001302static void
1303window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001304 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001305{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001306 struct input *input = data;
1307 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001308 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001309 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001310
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001311 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001312 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001313 return;
1314
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001315 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001316 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001317 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1318 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001319
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001320 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1321
1322 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001324 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001325 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001326
1327 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001328 (*window->key_handler)(window, input, time, key, sym, state,
1329 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001330}
1331
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001332static void
1333window_handle_pointer_focus(void *data,
1334 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001335 uint32_t time, struct wl_surface *surface,
1336 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001337{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001338 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001339 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001340 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001341 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001342
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001343 window = input->pointer_focus;
1344 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001345 window_set_focus_item(window, NULL);
1346
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001347 if (window->leave_handler)
1348 window->leave_handler(window, input,
1349 time, window->user_data);
1350 input->pointer_focus = NULL;
1351 input->current_pointer_image = POINTER_UNSET;
1352 }
1353
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001354 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001355 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001356 window = input->pointer_focus;
1357
Kristian Høgsberg59826582011-01-20 11:56:57 -05001358 input->x = x;
1359 input->y = y;
1360 input->sx = sx;
1361 input->sy = sy;
1362
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001363 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001364 if (window->enter_handler)
1365 pointer = window->enter_handler(window, input,
1366 time, sx, sy,
1367 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001368
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001369 item = window_find_item(window, x, y);
1370 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001371
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001372 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001373 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001374 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001375}
1376
1377static void
1378window_handle_keyboard_focus(void *data,
1379 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001380 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001381 struct wl_surface *surface,
1382 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001383{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001384 struct input *input = data;
1385 struct window *window = input->keyboard_focus;
1386 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001387 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001388
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001389 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001390 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001391 if (window->keyboard_focus_handler)
1392 (*window->keyboard_focus_handler)(window, NULL,
1393 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001394 }
1395
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001396 if (surface)
1397 input->keyboard_focus = wl_surface_get_user_data(surface);
1398 else
1399 input->keyboard_focus = NULL;
1400
1401 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001402 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001403 for (k = keys->data; k < end; k++)
1404 input->modifiers |= d->xkb->map->modmap[*k];
1405
1406 window = input->keyboard_focus;
1407 if (window) {
1408 window->keyboard_device = input;
1409 if (window->keyboard_focus_handler)
1410 (*window->keyboard_focus_handler)(window,
1411 window->keyboard_device,
1412 window->user_data);
1413 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001414}
1415
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001416static const struct wl_input_device_listener input_device_listener = {
1417 window_handle_motion,
1418 window_handle_button,
1419 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001420 window_handle_pointer_focus,
1421 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001422};
1423
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001424void
1425input_get_position(struct input *input, int32_t *x, int32_t *y)
1426{
1427 *x = input->sx;
1428 *y = input->sy;
1429}
1430
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001431struct wl_input_device *
1432input_get_input_device(struct input *input)
1433{
1434 return input->input_device;
1435}
1436
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001437uint32_t
1438input_get_modifiers(struct input *input)
1439{
1440 return input->modifiers;
1441}
1442
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001443struct data_offer {
1444 struct wl_data_offer *offer;
1445 struct input *input;
1446 struct wl_array types;
1447 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001448
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001449 struct task io_task;
1450 int fd;
1451 data_func_t func;
1452 int32_t x, y;
1453 void *user_data;
1454};
1455
1456static void
1457data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1458{
1459 struct data_offer *offer = data;
1460 char **p;
1461
1462 p = wl_array_add(&offer->types, sizeof *p);
1463 *p = strdup(type);
1464}
1465
1466static const struct wl_data_offer_listener data_offer_listener = {
1467 data_offer_offer,
1468};
1469
1470static void
1471data_offer_destroy(struct data_offer *offer)
1472{
1473 char **p;
1474
1475 offer->refcount--;
1476 if (offer->refcount == 0) {
1477 wl_data_offer_destroy(offer->offer);
1478 for (p = offer->types.data; *p; p++)
1479 free(*p);
1480 wl_array_release(&offer->types);
1481 free(offer);
1482 }
1483}
1484
1485static void
1486data_device_data_offer(void *data,
1487 struct wl_data_device *data_device, uint32_t id)
1488{
1489 struct data_offer *offer;
1490
1491 offer = malloc(sizeof *offer);
1492
1493 wl_array_init(&offer->types);
1494 offer->refcount = 1;
1495 offer->input = data;
1496
1497 /* FIXME: Generate typesafe wrappers for this */
1498 offer->offer = (struct wl_data_offer *)
1499 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1500 id, &wl_data_offer_interface);
1501
1502 wl_data_offer_add_listener(offer->offer,
1503 &data_offer_listener, offer);
1504}
1505
1506static void
1507data_device_enter(void *data, struct wl_data_device *data_device,
1508 uint32_t time, struct wl_surface *surface,
1509 int32_t x, int32_t y, struct wl_data_offer *offer)
1510{
1511 struct input *input = data;
1512 struct window *window;
1513 char **p;
1514
1515 input->drag_offer = wl_data_offer_get_user_data(offer);
1516 window = wl_surface_get_user_data(surface);
1517 input->pointer_focus = window;
1518
1519 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1520 *p = NULL;
1521
1522 window = input->pointer_focus;
1523 if (window->data_handler)
1524 window->data_handler(window, input, time, x, y,
1525 input->drag_offer->types.data,
1526 window->user_data);
1527}
1528
1529static void
1530data_device_leave(void *data, struct wl_data_device *data_device)
1531{
1532 struct input *input = data;
1533
1534 data_offer_destroy(input->drag_offer);
1535 input->drag_offer = NULL;
1536}
1537
1538static void
1539data_device_motion(void *data, struct wl_data_device *data_device,
1540 uint32_t time, int32_t x, int32_t y)
1541{
1542 struct input *input = data;
1543 struct window *window = input->pointer_focus;
1544
1545 input->sx = x;
1546 input->sy = y;
1547
1548 if (window->data_handler)
1549 window->data_handler(window, input, time, x, y,
1550 input->drag_offer->types.data,
1551 window->user_data);
1552}
1553
1554static void
1555data_device_drop(void *data, struct wl_data_device *data_device)
1556{
1557 struct input *input = data;
1558 struct window *window = input->pointer_focus;
1559
1560 if (window->drop_handler)
1561 window->drop_handler(window, input,
1562 input->sx, input->sy, window->user_data);
1563}
1564
1565static void
1566data_device_selection(void *data,
1567 struct wl_data_device *wl_data_device,
1568 struct wl_data_offer *offer)
1569{
1570 struct input *input = data;
1571 char **p;
1572
1573 if (input->selection_offer)
1574 data_offer_destroy(input->selection_offer);
1575
1576 input->selection_offer = wl_data_offer_get_user_data(offer);
1577 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1578 *p = NULL;
1579}
1580
1581static const struct wl_data_device_listener data_device_listener = {
1582 data_device_data_offer,
1583 data_device_enter,
1584 data_device_leave,
1585 data_device_motion,
1586 data_device_drop,
1587 data_device_selection
1588};
1589
1590struct wl_data_device *
1591input_get_data_device(struct input *input)
1592{
1593 return input->data_device;
1594}
1595
1596void
1597input_set_selection(struct input *input,
1598 struct wl_data_source *source, uint32_t time)
1599{
1600 wl_data_device_set_selection(input->data_device, source, time);
1601}
1602
1603void
1604input_accept(struct input *input, uint32_t time, const char *type)
1605{
1606 wl_data_offer_accept(input->drag_offer->offer, time, type);
1607}
1608
1609static void
1610offer_io_func(struct task *task, uint32_t events)
1611{
1612 struct data_offer *offer =
1613 container_of(task, struct data_offer, io_task);
1614 unsigned int len;
1615 char buffer[4096];
1616
1617 len = read(offer->fd, buffer, sizeof buffer);
1618 offer->func(buffer, len,
1619 offer->x, offer->y, offer->user_data);
1620
1621 if (len == 0) {
1622 close(offer->fd);
1623 data_offer_destroy(offer);
1624 }
1625}
1626
1627static void
1628data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1629 data_func_t func, void *user_data)
1630{
1631 int p[2];
1632
1633 pipe2(p, O_CLOEXEC);
1634 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1635 close(p[1]);
1636
1637 offer->io_task.run = offer_io_func;
1638 offer->fd = p[0];
1639 offer->func = func;
1640 offer->refcount++;
1641 offer->user_data = user_data;
1642
1643 display_watch_fd(offer->input->display,
1644 offer->fd, EPOLLIN, &offer->io_task);
1645}
1646
1647void
1648input_receive_drag_data(struct input *input, const char *mime_type,
1649 data_func_t func, void *data)
1650{
1651 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1652 input->drag_offer->x = input->sx;
1653 input->drag_offer->y = input->sy;
1654}
1655
1656int
1657input_receive_selection_data(struct input *input, const char *mime_type,
1658 data_func_t func, void *data)
1659{
1660 char **p;
1661
1662 if (input->selection_offer == NULL)
1663 return -1;
1664
1665 for (p = input->selection_offer->types.data; *p; p++)
1666 if (strcmp(mime_type, *p) == 0)
1667 break;
1668
1669 if (*p == NULL)
1670 return -1;
1671
1672 data_offer_receive_data(input->selection_offer,
1673 mime_type, func, data);
1674 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001675}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001676
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001677void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001678window_move(struct window *window, struct input *input, uint32_t time)
1679{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001680 if (!window->shell_surface)
1681 return;
1682
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001683 wl_shell_surface_move(window->shell_surface,
1684 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001685}
1686
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001687static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001688handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001689 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001690 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001691{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001692 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001693 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001694
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001695 /* FIXME: this is probably the wrong place to check for width
1696 * or height <= 0, but it prevents the compositor from crashing
1697 */
1698 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001699 return;
1700
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001701 window->resize_edges = edges;
1702
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001703 if (window->resize_handler) {
1704 child_width = width - 20 - window->margin * 2;
1705 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001706
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001707 (*window->resize_handler)(window,
1708 child_width, child_height,
1709 window->user_data);
1710 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001711 window->allocation.width = width;
1712 window->allocation.height = height;
1713
1714 if (window->redraw_handler)
1715 window_schedule_redraw(window);
1716 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001717}
1718
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001719static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001720 handle_configure,
1721};
1722
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001723void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001724window_get_allocation(struct window *window,
1725 struct rectangle *allocation)
1726{
1727 *allocation = window->allocation;
1728}
1729
1730void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001731window_get_child_allocation(struct window *window,
1732 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001733{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001734 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001735 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001736 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001737 allocation->x = window->margin + 10;
1738 allocation->y = window->margin + 50;
1739 allocation->width =
1740 window->allocation.width - 20 - window->margin * 2;
1741 allocation->height =
1742 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001743 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001744}
1745
1746void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001747window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001748{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001749 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001750 window->allocation.x = 20 + window->margin;
1751 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001752 window->allocation.width = width + 20 + window->margin * 2;
1753 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001754 } else {
1755 window->allocation.x = 0;
1756 window->allocation.y = 0;
1757 window->allocation.width = width;
1758 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001759 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001760}
1761
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001762static void
1763idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001764{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001765 struct window *window =
1766 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001767
1768 window->redraw_handler(window, window->user_data);
1769 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001770}
1771
1772void
1773window_schedule_redraw(struct window *window)
1774{
1775 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001776 window->redraw_task.run = idle_redraw;
1777 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001778 window->redraw_scheduled = 1;
1779 }
1780}
1781
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001782void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001783window_set_custom(struct window *window)
1784{
1785 window->type = TYPE_CUSTOM;
1786}
1787
1788void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001789window_set_fullscreen(struct window *window, int fullscreen)
1790{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001791 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001792 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001793
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001794 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001795 return;
1796
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001797 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001798 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001799 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001800 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001801 width = output->allocation.width;
1802 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001803 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001804 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001805 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001806 width = window->saved_allocation.width - 20 - window->margin * 2;
1807 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001808 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001809 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001810
1811 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001812}
1813
1814void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001815window_set_decoration(struct window *window, int decoration)
1816{
1817 window->decoration = decoration;
1818}
1819
1820void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001821window_set_user_data(struct window *window, void *data)
1822{
1823 window->user_data = data;
1824}
1825
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001826void *
1827window_get_user_data(struct window *window)
1828{
1829 return window->user_data;
1830}
1831
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001832void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001833window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001834 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001835{
1836 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001837}
1838
1839void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001840window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001841 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001842{
1843 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001844}
1845
1846void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001847window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001848 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001849{
1850 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001851}
1852
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001853void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001854window_set_button_handler(struct window *window,
1855 window_button_handler_t handler)
1856{
1857 window->button_handler = handler;
1858}
1859
1860void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001861window_set_motion_handler(struct window *window,
1862 window_motion_handler_t handler)
1863{
1864 window->motion_handler = handler;
1865}
1866
1867void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001868window_set_enter_handler(struct window *window,
1869 window_enter_handler_t handler)
1870{
1871 window->enter_handler = handler;
1872}
1873
1874void
1875window_set_leave_handler(struct window *window,
1876 window_leave_handler_t handler)
1877{
1878 window->leave_handler = handler;
1879}
1880
1881void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001882window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001883 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001884{
1885 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001886}
1887
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001888void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001889window_set_item_focus_handler(struct window *window,
1890 window_item_focus_handler_t handler)
1891{
1892 window->item_focus_handler = handler;
1893}
1894
1895void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001896window_set_data_handler(struct window *window, window_data_handler_t handler)
1897{
1898 window->data_handler = handler;
1899}
1900
1901void
1902window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1903{
1904 window->drop_handler = handler;
1905}
1906
1907void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001908window_set_transparent(struct window *window, int transparent)
1909{
1910 window->transparent = transparent;
1911}
1912
1913void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001914window_set_title(struct window *window, const char *title)
1915{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001916 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001917 window->title = strdup(title);
1918}
1919
1920const char *
1921window_get_title(struct window *window)
1922{
1923 return window->title;
1924}
1925
1926void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001927display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1928 int32_t x, int32_t y, int32_t width, int32_t height)
1929{
1930 struct wl_buffer *buffer;
1931
1932 buffer = display_get_buffer_for_surface(display, cairo_surface);
1933
1934 wl_buffer_damage(buffer, x, y, width, height);
1935}
1936
1937void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001938window_damage(struct window *window, int32_t x, int32_t y,
1939 int32_t width, int32_t height)
1940{
1941 wl_surface_damage(window->surface, x, y, width, height);
1942}
1943
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001944static struct window *
1945window_create_internal(struct display *display, struct window *parent,
1946 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001947{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001948 struct window *window;
1949
1950 window = malloc(sizeof *window);
1951 if (window == NULL)
1952 return NULL;
1953
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001954 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001955 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001956 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001957 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001958 if (display->shell) {
1959 window->shell_surface =
1960 wl_shell_get_shell_surface(display->shell,
1961 window->surface);
1962 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001963 window->allocation.x = 0;
1964 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001965 window->allocation.width = width;
1966 window->allocation.height = height;
1967 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001968 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001969 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001970 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001971 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001972
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001973 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001974#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001975 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1976 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001977#else
1978 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1979#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001980 else
1981 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001982
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001983 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001984 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001985
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001986 if (window->shell_surface) {
1987 wl_shell_surface_set_user_data(window->shell_surface, window);
1988 wl_shell_surface_add_listener(window->shell_surface,
1989 &shell_surface_listener, window);
1990 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001991
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001992 return window;
1993}
1994
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001995struct window *
1996window_create(struct display *display, int32_t width, int32_t height)
1997{
1998 struct window *window;
1999
2000 window = window_create_internal(display, NULL, width, height);
2001 if (!window)
2002 return NULL;
2003
2004 return window;
2005}
2006
2007struct window *
2008window_create_transient(struct display *display, struct window *parent,
2009 int32_t x, int32_t y, int32_t width, int32_t height)
2010{
2011 struct window *window;
2012
2013 window = window_create_internal(parent->display,
2014 parent, width, height);
2015 if (!window)
2016 return NULL;
2017
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002018 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002019 window->x = x;
2020 window->y = y;
2021
2022 return window;
2023}
2024
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002025struct menu {
2026 struct window *window;
2027 const char **entries;
2028 int current;
2029 int count;
2030};
2031
2032static int
2033menu_set_item(struct window *window, struct menu *menu, int sy)
2034{
2035 int next;
2036
2037 next = (sy - 8) / 20;
2038 if (menu->current != next) {
2039 menu->current = next;
2040 window_schedule_redraw(window);
2041 }
2042
2043 return POINTER_LEFT_PTR;
2044}
2045
2046static int
2047menu_motion_handler(struct window *window,
2048 struct input *input, uint32_t time,
2049 int32_t x, int32_t y,
2050 int32_t sx, int32_t sy, void *data)
2051{
2052 return menu_set_item(window, data, sy);
2053}
2054
2055static int
2056menu_enter_handler(struct window *window,
2057 struct input *input, uint32_t time,
2058 int32_t x, int32_t y, void *data)
2059{
2060 return menu_set_item(window, data, y);
2061}
2062
2063static void
2064menu_leave_handler(struct window *window,
2065 struct input *input, uint32_t time, void *data)
2066{
2067 menu_set_item(window, data, -200);
2068}
2069
2070static void
2071menu_button_handler(struct window *window,
2072 struct input *input, uint32_t time,
2073 int button, int state, void *data)
2074
2075{
2076 struct menu *menu = data;
2077
2078 /* Either relase after press-drag-release or click-motion-click. */
2079 if (state == 0 && 0 <= menu->current && menu->current < menu->count)
2080 window_destroy(window);
2081}
2082
2083static void
2084menu_redraw_handler(struct window *window, void *data)
2085{
2086 cairo_t *cr;
2087 const int32_t r = 3, margin = 3;
2088 struct menu *menu = data;
2089 int32_t width, height, i;
2090
2091 width = 200;
2092 height = menu->count * 20 + margin * 2;
2093 window_set_child_size(window, width, height);
2094 window_create_surface(window);
2095
2096 cr = cairo_create(window->cairo_surface);
2097 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2098 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2099 cairo_paint(cr);
2100
2101 width = window->allocation.width;
2102 height = window->allocation.height;
2103 rounded_rect(cr, 0, 0, width, height, r);
2104 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2105 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2106 cairo_fill(cr);
2107
2108 for (i = 0; i < menu->count; i++) {
2109 if (i == menu->current) {
2110 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2111 cairo_rectangle(cr, margin, i * 20 + margin,
2112 width - 2 * margin, 20);
2113 cairo_fill(cr);
2114 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2115 cairo_move_to(cr, 10, i * 20 + 16);
2116 cairo_show_text(cr, menu->entries[i]);
2117 } else {
2118 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2119 cairo_move_to(cr, 10, i * 20 + 16);
2120 cairo_show_text(cr, menu->entries[i]);
2121 }
2122 }
2123
2124 cairo_destroy(cr);
2125 window_flush(window);
2126}
2127
2128struct window *
2129window_create_menu(struct display *display, struct window *parent,
2130 int32_t x, int32_t y, const char **entries, int count)
2131{
2132 struct window *window;
2133 struct menu *menu;
2134
2135 menu = malloc(sizeof *menu);
2136 if (!menu)
2137 return NULL;
2138
2139 window = window_create_internal(parent->display, parent, 0, 0);
2140 if (!window)
2141 return NULL;
2142
2143 menu->window = window;
2144 menu->entries = entries;
2145 menu->count = count;
2146 window->decoration = 0;
2147 window->type = TYPE_MENU;
2148 window->x = x;
2149 window->y = y;
2150
2151 wl_shell_surface_set_transient(window->shell_surface,
2152 window->parent->shell_surface,
2153 window->x, window->y, 0);
2154
2155 window_set_motion_handler(window, menu_motion_handler);
2156 window_set_enter_handler(window, menu_enter_handler);
2157 window_set_leave_handler(window, menu_leave_handler);
2158 window_set_button_handler(window, menu_button_handler);
2159 window_set_redraw_handler(window, menu_redraw_handler);
2160 window_set_user_data(window, menu);
2161
2162 return window;
2163}
2164
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002165void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002166window_set_buffer_type(struct window *window, enum window_buffer_type type)
2167{
2168 window->buffer_type = type;
2169}
2170
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002171
2172static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002173display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002174 struct wl_output *wl_output,
2175 int x, int y,
2176 int physical_width,
2177 int physical_height,
2178 int subpixel,
2179 const char *make,
2180 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002181{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002182 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002183
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002184 output->allocation.x = x;
2185 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002186}
2187
2188static void
2189display_handle_mode(void *data,
2190 struct wl_output *wl_output,
2191 uint32_t flags,
2192 int width,
2193 int height,
2194 int refresh)
2195{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002196 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002197 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002198
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002199 if (flags & WL_OUTPUT_MODE_CURRENT) {
2200 output->allocation.width = width;
2201 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002202 if (display->output_configure_handler)
2203 (*display->output_configure_handler)(
2204 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002205 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002206}
2207
2208static const struct wl_output_listener output_listener = {
2209 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002210 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002211};
2212
2213static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002214display_add_output(struct display *d, uint32_t id)
2215{
2216 struct output *output;
2217
2218 output = malloc(sizeof *output);
2219 if (output == NULL)
2220 return;
2221
2222 memset(output, 0, sizeof *output);
2223 output->display = d;
2224 output->output =
2225 wl_display_bind(d->display, id, &wl_output_interface);
2226 wl_list_insert(d->output_list.prev, &output->link);
2227
2228 wl_output_add_listener(output->output, &output_listener, output);
2229}
2230
2231void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002232display_set_output_configure_handler(struct display *display,
2233 display_output_handler_t handler)
2234{
2235 struct output *output;
2236
2237 display->output_configure_handler = handler;
2238 if (!handler)
2239 return;
2240
2241 wl_list_for_each(output, &display->output_list, link)
2242 (*display->output_configure_handler)(output,
2243 display->user_data);
2244}
2245
2246void
2247output_set_user_data(struct output *output, void *data)
2248{
2249 output->user_data = data;
2250}
2251
2252void *
2253output_get_user_data(struct output *output)
2254{
2255 return output->user_data;
2256}
2257
2258void
2259output_set_destroy_handler(struct output *output,
2260 display_output_handler_t handler)
2261{
2262 output->destroy_handler = handler;
2263 /* FIXME: implement this, once we have way to remove outputs */
2264}
2265
2266void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002267output_get_allocation(struct output *output, struct rectangle *allocation)
2268{
2269 *allocation = output->allocation;
2270}
2271
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002272struct wl_output *
2273output_get_wl_output(struct output *output)
2274{
2275 return output->output;
2276}
2277
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002278static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002279display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002280{
2281 struct input *input;
2282
2283 input = malloc(sizeof *input);
2284 if (input == NULL)
2285 return;
2286
2287 memset(input, 0, sizeof *input);
2288 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002289 input->input_device =
2290 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002291 input->pointer_focus = NULL;
2292 input->keyboard_focus = NULL;
2293 wl_list_insert(d->input_list.prev, &input->link);
2294
2295 wl_input_device_add_listener(input->input_device,
2296 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002297 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002298
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002299 input->data_device =
2300 wl_data_device_manager_get_data_device(d->data_device_manager,
2301 input->input_device);
2302 wl_data_device_add_listener(input->data_device,
2303 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002304}
2305
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002306static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002307display_handle_global(struct wl_display *display, uint32_t id,
2308 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002309{
2310 struct display *d = data;
2311
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002312 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002313 d->compositor =
2314 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002315 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002316 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002317 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002318 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002319 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002320 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002321 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002322 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002323 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2324 d->data_device_manager =
2325 wl_display_bind(display, id,
2326 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002327 }
2328}
2329
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002330static void
2331display_render_frame(struct display *d)
2332{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002333 int radius = 8;
2334 cairo_t *cr;
2335
2336 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2337 cr = cairo_create(d->shadow);
2338 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2339 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2340 rounded_rect(cr, 16, 16, 112, 112, radius);
2341 cairo_fill(cr);
2342 cairo_destroy(cr);
2343 blur_surface(d->shadow, 64);
2344
2345 d->active_frame =
2346 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2347 cr = cairo_create(d->active_frame);
2348 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2349 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2350 rounded_rect(cr, 16, 16, 112, 112, radius);
2351 cairo_fill(cr);
2352 cairo_destroy(cr);
2353
2354 d->inactive_frame =
2355 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2356 cr = cairo_create(d->inactive_frame);
2357 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2358 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2359 rounded_rect(cr, 16, 16, 112, 112, radius);
2360 cairo_fill(cr);
2361 cairo_destroy(cr);
2362}
2363
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002364static void
2365init_xkb(struct display *d)
2366{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002367 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002368
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002369 names.rules = "evdev";
2370 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002371 names.layout = option_xkb_layout;
2372 names.variant = option_xkb_variant;
2373 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002374
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002375 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002376 if (!d->xkb) {
2377 fprintf(stderr, "Failed to compile keymap\n");
2378 exit(1);
2379 }
2380}
2381
Yuval Fledel45568f62010-12-06 09:18:12 -05002382static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002383init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002384{
2385 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002386 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002387
2388 static const EGLint premul_argb_cfg_attribs[] = {
2389 EGL_SURFACE_TYPE,
2390 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2391 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002392 EGL_RED_SIZE, 1,
2393 EGL_GREEN_SIZE, 1,
2394 EGL_BLUE_SIZE, 1,
2395 EGL_ALPHA_SIZE, 1,
2396 EGL_DEPTH_SIZE, 1,
2397 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2398 EGL_NONE
2399 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002400
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002401 static const EGLint rgb_cfg_attribs[] = {
2402 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2403 EGL_RED_SIZE, 1,
2404 EGL_GREEN_SIZE, 1,
2405 EGL_BLUE_SIZE, 1,
2406 EGL_ALPHA_SIZE, 0,
2407 EGL_DEPTH_SIZE, 1,
2408 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2409 EGL_NONE
2410 };
2411
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002412 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002413 if (!eglInitialize(d->dpy, &major, &minor)) {
2414 fprintf(stderr, "failed to initialize display\n");
2415 return -1;
2416 }
2417
2418 if (!eglBindAPI(EGL_OPENGL_API)) {
2419 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2420 return -1;
2421 }
2422
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002423 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002424 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002425 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002426 return -1;
2427 }
2428
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002429 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2430 &d->rgb_config, 1, &n) || n != 1) {
2431 fprintf(stderr, "failed to choose rgb config\n");
2432 return -1;
2433 }
2434
Benjamin Franzke0c991632011-09-27 21:57:31 +02002435 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2436 if (d->rgb_ctx == NULL) {
2437 fprintf(stderr, "failed to create context\n");
2438 return -1;
2439 }
2440 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2441 EGL_NO_CONTEXT, NULL);
2442 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002443 fprintf(stderr, "failed to create context\n");
2444 return -1;
2445 }
2446
Benjamin Franzke0c991632011-09-27 21:57:31 +02002447 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002448 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002449 return -1;
2450 }
2451
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002452#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002453 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2454 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002455 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002456 return -1;
2457 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002458 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2459 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2460 fprintf(stderr, "failed to get cairo egl argb device\n");
2461 return -1;
2462 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002463#endif
2464
2465 return 0;
2466}
2467
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002468static int
2469event_mask_update(uint32_t mask, void *data)
2470{
2471 struct display *d = data;
2472
2473 d->mask = mask;
2474
2475 return 0;
2476}
2477
2478static void
2479handle_display_data(struct task *task, uint32_t events)
2480{
2481 struct display *display =
2482 container_of(task, struct display, display_task);
2483
2484 wl_display_iterate(display->display, display->mask);
2485}
2486
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002487struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002488display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002489{
2490 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002491 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002492 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002493 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002494
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002495 g_type_init();
2496
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002497 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002498 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002499 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002500
2501 xkb_option_group = g_option_group_new("xkb",
2502 "Keyboard options",
2503 "Show all XKB options",
2504 NULL, NULL);
2505 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2506 g_option_context_add_group (context, xkb_option_group);
2507
2508 if (!g_option_context_parse(context, argc, argv, &error)) {
2509 fprintf(stderr, "option parsing failed: %s\n", error->message);
2510 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002511 }
2512
Tim Wiederhake748f6722011-01-23 23:25:25 +01002513 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002514
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002515 d = malloc(sizeof *d);
2516 if (d == NULL)
2517 return NULL;
2518
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002519 memset(d, 0, sizeof *d);
2520
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002521 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002522 if (d->display == NULL) {
2523 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002524 return NULL;
2525 }
2526
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002527 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2528 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2529 d->display_task.run = handle_display_data;
2530 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2531
2532 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002533 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002534 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002535
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002536 /* Set up listener so we'll catch all events. */
2537 wl_display_add_global_listener(d->display,
2538 display_handle_global, d);
2539
2540 /* Process connection events. */
2541 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002542 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002543 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002544
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002545 d->image_target_texture_2d =
2546 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2547 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2548 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2549
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002550 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002551
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002552 display_render_frame(d);
2553
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002554 wl_list_init(&d->window_list);
2555
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002556 init_xkb(d);
2557
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002558 return d;
2559}
2560
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002561void
2562display_set_user_data(struct display *display, void *data)
2563{
2564 display->user_data = data;
2565}
2566
2567void *
2568display_get_user_data(struct display *display)
2569{
2570 return display->user_data;
2571}
2572
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002573struct wl_display *
2574display_get_display(struct display *display)
2575{
2576 return display->display;
2577}
2578
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002579struct output *
2580display_get_output(struct display *display)
2581{
2582 return container_of(display->output_list.next, struct output, link);
2583}
2584
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002585struct wl_compositor *
2586display_get_compositor(struct display *display)
2587{
2588 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002589}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002590
2591EGLDisplay
2592display_get_egl_display(struct display *d)
2593{
2594 return d->dpy;
2595}
2596
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002597struct wl_data_source *
2598display_create_data_source(struct display *display)
2599{
2600 return wl_data_device_manager_create_data_source(display->data_device_manager);
2601}
2602
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002603EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002604display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002605{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002606 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002607}
2608
Benjamin Franzke0c991632011-09-27 21:57:31 +02002609EGLConfig
2610display_get_argb_egl_config(struct display *d)
2611{
2612 return d->premultiplied_argb_config;
2613}
2614
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002615struct wl_shell *
2616display_get_shell(struct display *display)
2617{
2618 return display->shell;
2619}
2620
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002621int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002622display_acquire_window_surface(struct display *display,
2623 struct window *window,
2624 EGLContext ctx)
2625{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002626#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002627 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002628 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002629
2630 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002631 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002632 device = cairo_surface_get_device(window->cairo_surface);
2633 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002634 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002635
Benjamin Franzke0c991632011-09-27 21:57:31 +02002636 if (!ctx) {
2637 if (device == display->rgb_device)
2638 ctx = display->rgb_ctx;
2639 else if (device == display->argb_device)
2640 ctx = display->argb_ctx;
2641 else
2642 assert(0);
2643 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002644
2645 data = cairo_surface_get_user_data(window->cairo_surface,
2646 &surface_data_key);
2647
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002648 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002649 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002650 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2651 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002652
2653 return 0;
2654#else
2655 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002656#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002657}
2658
2659void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002660display_release_window_surface(struct display *display,
2661 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002662{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002663#ifdef HAVE_CAIRO_EGL
2664 cairo_device_t *device;
2665
2666 device = cairo_surface_get_device(window->cairo_surface);
2667 if (!device)
2668 return;
2669
2670 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002671 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002672 cairo_device_release(device);
2673#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002674}
2675
2676void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002677display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002678{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002679 wl_list_insert(&display->deferred_list, &task->link);
2680}
2681
2682void
2683display_watch_fd(struct display *display,
2684 int fd, uint32_t events, struct task *task)
2685{
2686 struct epoll_event ep;
2687
2688 ep.events = events;
2689 ep.data.ptr = task;
2690 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2691}
2692
2693void
2694display_run(struct display *display)
2695{
2696 struct task *task;
2697 struct epoll_event ep[16];
2698 int i, count;
2699
Pekka Paalanen826d7952011-12-15 10:14:07 +02002700 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002701 while (1) {
2702 while (display->mask & WL_DISPLAY_WRITABLE)
2703 wl_display_iterate(display->display,
2704 WL_DISPLAY_WRITABLE);
2705
Pekka Paalanen826d7952011-12-15 10:14:07 +02002706 if (!display->running)
2707 break;
2708
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002709 count = epoll_wait(display->epoll_fd,
2710 ep, ARRAY_LENGTH(ep), -1);
2711 for (i = 0; i < count; i++) {
2712 task = ep[i].data.ptr;
2713 task->run(task, ep[i].events);
2714 }
2715
2716 while (!wl_list_empty(&display->deferred_list)) {
2717 task = container_of(display->deferred_list.next,
2718 struct task, link);
2719 wl_list_remove(&task->link);
2720 task->run(task, 0);
2721 }
2722 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002723}
Pekka Paalanen826d7952011-12-15 10:14:07 +02002724
2725void
2726display_exit(struct display *display)
2727{
2728 display->running = 0;
2729}