blob: 09e9b70fe5d75a57109ca5a8aab70a881f44420c [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
Kristian Høgsberg478d9262010-06-08 20:34:11 -040082 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040083 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050084 struct wl_list output_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040085 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500119 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400122 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400125 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500126
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400127 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500128 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500129
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500130 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400131 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500132 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400133 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500134 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400135 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400136 window_enter_handler_t enter_handler;
137 window_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400138 window_item_focus_handler_t item_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400139 window_data_handler_t data_handler;
140 window_drop_handler_t drop_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400141
142 struct wl_list item_list;
143 struct item *focus_item;
144 uint32_t item_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400145
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500146 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400147 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500148};
149
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400150struct item {
151 struct wl_list link;
152 struct rectangle allocation;
153 void *user_data;
154};
155
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400156struct input {
157 struct display *display;
158 struct wl_input_device *input_device;
159 struct window *pointer_focus;
160 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400161 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400162 uint32_t modifiers;
163 int32_t x, y, sx, sy;
164 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400165
166 struct wl_data_device *data_device;
167 struct data_offer *drag_offer;
168 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400169};
170
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500171struct output {
172 struct display *display;
173 struct wl_output *output;
174 struct rectangle allocation;
175 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200176
177 display_output_handler_t destroy_handler;
178 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500179};
180
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400181enum {
182 POINTER_DEFAULT = 100,
183 POINTER_UNSET
184};
185
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500186enum window_location {
187 WINDOW_INTERIOR = 0,
188 WINDOW_RESIZING_TOP = 1,
189 WINDOW_RESIZING_BOTTOM = 2,
190 WINDOW_RESIZING_LEFT = 4,
191 WINDOW_RESIZING_TOP_LEFT = 5,
192 WINDOW_RESIZING_BOTTOM_LEFT = 6,
193 WINDOW_RESIZING_RIGHT = 8,
194 WINDOW_RESIZING_TOP_RIGHT = 9,
195 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
196 WINDOW_RESIZING_MASK = 15,
197 WINDOW_EXTERIOR = 16,
198 WINDOW_TITLEBAR = 17,
199 WINDOW_CLIENT_AREA = 18,
200};
201
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400202const char *option_xkb_layout = "us";
203const char *option_xkb_variant = "";
204const char *option_xkb_options = "";
205
206static const GOptionEntry xkb_option_entries[] = {
207 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
208 &option_xkb_layout, "XKB Layout" },
209 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
210 &option_xkb_variant, "XKB Variant" },
211 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
212 &option_xkb_options, "XKB Options" },
213 { NULL }
214};
215
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400216static const cairo_user_data_key_t surface_data_key;
217struct surface_data {
218 struct wl_buffer *buffer;
219};
220
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500221#define MULT(_d,c,a,t) \
222 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
223
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500224#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400225
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100226struct egl_window_surface_data {
227 struct display *display;
228 struct wl_surface *surface;
229 struct wl_egl_window *window;
230 EGLSurface surf;
231};
232
233static void
234egl_window_surface_data_destroy(void *p)
235{
236 struct egl_window_surface_data *data = p;
237 struct display *d = data->display;
238
239 eglDestroySurface(d->dpy, data->surf);
240 wl_egl_window_destroy(data->window);
241 data->surface = NULL;
242
243 free(p);
244}
245
246static cairo_surface_t *
247display_create_egl_window_surface(struct display *display,
248 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400249 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100250 struct rectangle *rectangle)
251{
252 cairo_surface_t *cairo_surface;
253 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400254 EGLConfig config;
255 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200256 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100257
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400258 static const EGLint premul_attribs[] = {
259 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
260 EGL_NONE
261 };
262
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100263 data = malloc(sizeof *data);
264 if (data == NULL)
265 return NULL;
266
267 data->display = display;
268 data->surface = surface;
269
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400270 if (flags & SURFACE_OPAQUE) {
271 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200272 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200273 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400274 } else {
275 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200276 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400277 attribs = premul_attribs;
278 }
279
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400280 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100281 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400282 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100283
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400284 data->surf = eglCreateWindowSurface(display->dpy, config,
285 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100286
Benjamin Franzke0c991632011-09-27 21:57:31 +0200287 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100288 data->surf,
289 rectangle->width,
290 rectangle->height);
291
292 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
293 data, egl_window_surface_data_destroy);
294
295 return cairo_surface;
296}
297
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500298struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400299 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200300 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 EGLImageKHR image;
302 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800303 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500304 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400305};
306
307static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500308egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400309{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800311 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312
Benjamin Franzke0c991632011-09-27 21:57:31 +0200313 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200315 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800316
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500317 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500319 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321}
322
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500323EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324display_get_image_for_egl_image_surface(struct display *display,
325 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500326{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500327 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500328
329 data = cairo_surface_get_user_data (surface, &surface_data_key);
330
331 return data->image;
332}
333
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500334static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400336 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400338{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340 EGLDisplay dpy = display->dpy;
341 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200342 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400343
344 data = malloc(sizeof *data);
345 if (data == NULL)
346 return NULL;
347
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800348 data->display = display;
349
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400350 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400351 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000353 free(data);
354 return NULL;
355 }
356
Benjamin Franzke0c991632011-09-27 21:57:31 +0200357 if (flags & SURFACE_OPAQUE) {
358 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200359 content = CAIRO_CONTENT_COLOR;
360 } else {
361 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200362 content = CAIRO_CONTENT_COLOR_ALPHA;
363 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500365 data->image = display->create_image(dpy, NULL,
366 EGL_NATIVE_PIXMAP_KHR,
367 (EGLClientBuffer) data->pixmap,
368 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500369 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500370 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500371 free(data);
372 return NULL;
373 }
374
375 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400376 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500377
Benjamin Franzke0c991632011-09-27 21:57:31 +0200378 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400379 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400380 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500381 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200382 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400383
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 surface = cairo_gl_surface_create_for_texture(data->device,
385 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400386 data->texture,
387 rectangle->width,
388 rectangle->height);
389
390 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500391 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392
393 return surface;
394}
395
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500396static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500397display_create_egl_image_surface_from_file(struct display *display,
398 const char *filename,
399 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400{
401 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400402 GdkPixbuf *pixbuf;
403 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400404 int stride, i;
405 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500406 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400407
408 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400410 FALSE, &error);
411 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400414 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
415 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500416 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400418 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400420
421 stride = gdk_pixbuf_get_rowstride(pixbuf);
422 pixels = gdk_pixbuf_get_pixels(pixbuf);
423
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400425 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400427 while (p < end) {
428 unsigned int t;
429
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400430 MULT(p[0], p[0], p[3], t);
431 MULT(p[1], p[1], p[3], t);
432 MULT(p[2], p[2], p[3], t);
433 p += 4;
434
435 }
436 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400437
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200438 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000439 if (surface == NULL) {
440 g_object_unref(pixbuf);
441 return NULL;
442 }
443
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800444 data = cairo_surface_get_user_data(surface, &surface_data_key);
445
Benjamin Franzke0c991632011-09-27 21:57:31 +0200446 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800447 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800448 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
449 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200450 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400451
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500452 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400453
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454 return surface;
455}
456
457#endif
458
459struct wl_buffer *
460display_get_buffer_for_surface(struct display *display,
461 cairo_surface_t *surface)
462{
463 struct surface_data *data;
464
465 data = cairo_surface_get_user_data (surface, &surface_data_key);
466
467 return data->buffer;
468}
469
470struct shm_surface_data {
471 struct surface_data data;
472 void *map;
473 size_t length;
474};
475
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500476static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477shm_surface_data_destroy(void *p)
478{
479 struct shm_surface_data *data = p;
480
481 wl_buffer_destroy(data->data.buffer);
482 munmap(data->map, data->length);
483}
484
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500485static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400486display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400487 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488{
489 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400490 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400491 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800492 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400493 char filename[] = "/tmp/wayland-shm-XXXXXX";
494
495 data = malloc(sizeof *data);
496 if (data == NULL)
497 return NULL;
498
499 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
500 rectangle->width);
501 data->length = stride * rectangle->height;
502 fd = mkstemp(filename);
503 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000504 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505 return NULL;
506 }
507 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000508 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509 close(fd);
510 return NULL;
511 }
512
513 data->map = mmap(NULL, data->length,
514 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
515 unlink(filename);
516
517 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000518 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519 close(fd);
520 return NULL;
521 }
522
523 surface = cairo_image_surface_create_for_data (data->map,
524 CAIRO_FORMAT_ARGB32,
525 rectangle->width,
526 rectangle->height,
527 stride);
528
529 cairo_surface_set_user_data (surface, &surface_data_key,
530 data, shm_surface_data_destroy);
531
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400532 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400533 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400534 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400535 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400536
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537 data->data.buffer = wl_shm_create_buffer(display->shm,
538 fd,
539 rectangle->width,
540 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400541 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542
543 close(fd);
544
545 return surface;
546}
547
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500548static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549display_create_shm_surface_from_file(struct display *display,
550 const char *filename,
551 struct rectangle *rect)
552{
553 cairo_surface_t *surface;
554 GdkPixbuf *pixbuf;
555 GError *error = NULL;
556 int stride, i;
557 unsigned char *pixels, *p, *end, *dest_data;
558 int dest_stride;
559 uint32_t *d;
560
561 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
562 rect->width, rect->height,
563 FALSE, &error);
564 if (error != NULL)
565 return NULL;
566
567 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
568 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500569 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400570 return NULL;
571 }
572
573 stride = gdk_pixbuf_get_rowstride(pixbuf);
574 pixels = gdk_pixbuf_get_pixels(pixbuf);
575
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400576 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000577 if (surface == NULL) {
578 g_object_unref(pixbuf);
579 return NULL;
580 }
581
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582 dest_data = cairo_image_surface_get_data (surface);
583 dest_stride = cairo_image_surface_get_stride (surface);
584
585 for (i = 0; i < rect->height; i++) {
586 d = (uint32_t *) (dest_data + i * dest_stride);
587 p = pixels + i * stride;
588 end = p + rect->width * 4;
589 while (p < end) {
590 unsigned int t;
591 unsigned char a, r, g, b;
592
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593 a = p[3];
594 MULT(r, p[0], a, t);
595 MULT(g, p[1], a, t);
596 MULT(b, p[2], a, t);
597 p += 4;
598 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
599 }
600 }
601
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500602 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603
604 return surface;
605}
606
nobled7b87cb02011-02-01 18:51:47 +0000607static int
608check_size(struct rectangle *rect)
609{
610 if (rect->width && rect->height)
611 return 0;
612
613 fprintf(stderr, "tried to create surface of "
614 "width: %d, height: %d\n", rect->width, rect->height);
615 return -1;
616}
617
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618cairo_surface_t *
619display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400621 struct rectangle *rectangle,
622 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623{
nobled7b87cb02011-02-01 18:51:47 +0000624 if (check_size(rectangle) < 0)
625 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500626#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500627 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100628 if (surface)
629 return display_create_egl_window_surface(display,
630 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400631 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632 rectangle);
633 else
634 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400635 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500637 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400639 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640}
641
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500642static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400643display_create_surface_from_file(struct display *display,
644 const char *filename,
645 struct rectangle *rectangle)
646{
nobled7b87cb02011-02-01 18:51:47 +0000647 if (check_size(rectangle) < 0)
648 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500649#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500650 if (display->dpy) {
651 return display_create_egl_image_surface_from_file(display,
652 filename,
653 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500654 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500656 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400657}
Yuval Fledel45568f62010-12-06 09:18:12 -0500658 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400659 const char *filename;
660 int hotspot_x, hotspot_y;
661} pointer_images[] = {
662 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
663 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
664 { DATADIR "/wayland/bottom_side.png", 16, 20 },
665 { DATADIR "/wayland/grabbing.png", 20, 17 },
666 { DATADIR "/wayland/left_ptr.png", 10, 5 },
667 { DATADIR "/wayland/left_side.png", 10, 20 },
668 { DATADIR "/wayland/right_side.png", 30, 19 },
669 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
670 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
671 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672 { DATADIR "/wayland/xterm.png", 15, 15 },
673 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674};
675
676static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400677create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678{
679 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400680 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400681 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400682
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400683 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400684 display->pointer_surfaces =
685 malloc(count * sizeof *display->pointer_surfaces);
686 rect.width = width;
687 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690 display_create_surface_from_file(display,
691 pointer_images[i].filename,
692 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100693 if (!display->pointer_surfaces[i]) {
694 fprintf(stderr, "Error loading pointer image: %s\n",
695 pointer_images[i].filename);
696 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697 }
698
699}
700
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701cairo_surface_t *
702display_get_pointer_surface(struct display *display, int pointer,
703 int *width, int *height,
704 int *hotspot_x, int *hotspot_y)
705{
706 cairo_surface_t *surface;
707
708 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500709#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400710 *width = cairo_gl_surface_get_width(surface);
711 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000712#else
713 *width = cairo_image_surface_get_width(surface);
714 *height = cairo_image_surface_get_height(surface);
715#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400716 *hotspot_x = pointer_images[pointer].hotspot_x;
717 *hotspot_y = pointer_images[pointer].hotspot_y;
718
719 return cairo_surface_reference(surface);
720}
721
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400722static void
723window_attach_surface(struct window *window);
724
725static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400726free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400727{
728 struct window *window = data;
729
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400730 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400731 cairo_surface_destroy(window->pending_surface);
732 window->pending_surface = NULL;
733 if (window->cairo_surface)
734 window_attach_surface(window);
735}
736
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400737static const struct wl_callback_listener free_surface_listener = {
738 free_surface
739};
740
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500741static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200742window_get_resize_dx_dy(struct window *window, int *x, int *y)
743{
744 if (window->resize_edges & WINDOW_RESIZING_LEFT)
745 *x = window->server_allocation.width - window->allocation.width;
746 else
747 *x = 0;
748
749 if (window->resize_edges & WINDOW_RESIZING_TOP)
750 *y = window->server_allocation.height -
751 window->allocation.height;
752 else
753 *y = 0;
754
755 window->resize_edges = 0;
756}
757
758static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400759window_set_type(struct window *window)
760{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200761 if (!window->shell_surface)
762 return;
763
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400764 switch (window->type) {
765 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200766 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400767 break;
768 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200769 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400770 break;
771 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200772 wl_shell_surface_set_transient(window->shell_surface,
773 window->parent->shell_surface,
774 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400775 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500776 case TYPE_MENU:
777 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400778 case TYPE_CUSTOM:
779 break;
780 }
781}
782
783static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500784window_attach_surface(struct window *window)
785{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400786 struct display *display = window->display;
787 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400788 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000789#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000791#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500792 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 if (display->shell)
795 window_set_type(window);
796
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100797 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000798#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
800 data = cairo_surface_get_user_data(window->cairo_surface,
801 &surface_data_key);
802
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100803 cairo_gl_surface_swapbuffers(window->cairo_surface);
804 wl_egl_window_get_attached_size(data->window,
805 &window->server_allocation.width,
806 &window->server_allocation.height);
807 break;
808 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000809#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100810 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200811 window_get_resize_dx_dy(window, &x, &y);
812
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 if (window->pending_surface != NULL)
814 return;
815
816 window->pending_surface = window->cairo_surface;
817 window->cairo_surface = NULL;
818
819 buffer =
820 display_get_buffer_for_surface(display,
821 window->pending_surface);
822
823 wl_surface_attach(window->surface, buffer, x, y);
824 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400825 cb = wl_display_sync(display->display);
826 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000828 default:
829 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500831
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500832 wl_surface_damage(window->surface, 0, 0,
833 window->allocation.width,
834 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500835}
836
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500837void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400838window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500839{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100840 if (window->cairo_surface) {
841 switch (window->buffer_type) {
842 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
843 case WINDOW_BUFFER_TYPE_SHM:
844 display_surface_damage(window->display,
845 window->cairo_surface,
846 0, 0,
847 window->allocation.width,
848 window->allocation.height);
849 break;
850 default:
851 break;
852 }
853 window_attach_surface(window);
854 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500855}
856
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400857void
858window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500860 cairo_surface_reference(surface);
861
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862 if (window->cairo_surface != NULL)
863 cairo_surface_destroy(window->cairo_surface);
864
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500865 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400866}
867
Benjamin Franzke22d54812011-07-16 19:50:32 +0000868#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100869static void
870window_resize_cairo_window_surface(struct window *window)
871{
872 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200873 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874
875 data = cairo_surface_get_user_data(window->cairo_surface,
876 &surface_data_key);
877
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200878 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100879 wl_egl_window_resize(data->window,
880 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200881 window->allocation.height,
882 x,y);
883
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100884 cairo_gl_surface_set_size(window->cairo_surface,
885 window->allocation.width,
886 window->allocation.height);
887}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000888#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100889
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400890struct display *
891window_get_display(struct window *window)
892{
893 return window->display;
894}
895
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896void
897window_create_surface(struct window *window)
898{
899 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400900 uint32_t flags = 0;
901
902 if (!window->transparent)
903 flags = SURFACE_OPAQUE;
904
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400905 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500906#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100907 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
908 if (window->cairo_surface) {
909 window_resize_cairo_window_surface(window);
910 return;
911 }
912 surface = display_create_surface(window->display,
913 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400914 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500916 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400917 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100918 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400919 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400920 break;
921#endif
922 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400923 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400924 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400925 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800926 default:
927 surface = NULL;
928 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400930
931 window_set_surface(window, surface);
932 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933}
934
935static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500936window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500937{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500938 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500939 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400940 cairo_surface_t *frame;
941 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500942
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400943 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400945 width = window->allocation.width;
946 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500947
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500948 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500949
Kristian Høgsberg09531622010-06-14 23:22:15 -0400950 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400951 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400952 cairo_paint(cr);
953
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400954 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400955 tile_mask(cr, window->display->shadow,
956 shadow_dx, shadow_dy, width, height,
957 window->margin + 10 - shadow_dx,
958 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500959
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400960 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400961 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400962 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400963 frame = window->display->inactive_frame;
964
965 tile_source(cr, frame, 0, 0, width, height,
966 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500967
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500968 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
969 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500970 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 if (window->keyboard_device)
973 cairo_set_source_rgb(cr, 0, 0, 0);
974 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400975 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400976 cairo_show_text(cr, window->title);
977
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500978 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500979}
980
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400981void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500982window_destroy(struct window *window)
983{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200984 struct display *display = window->display;
985 struct input *input;
986
987 if (window->redraw_scheduled)
988 wl_list_remove(&window->redraw_task.link);
989
990 wl_list_for_each(input, &display->input_list, link) {
991 if (input->pointer_focus == window)
992 input->pointer_focus = NULL;
993 if (input->keyboard_focus == window)
994 input->keyboard_focus = NULL;
995 }
996
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200997 if (window->shell_surface)
998 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500999 wl_surface_destroy(window->surface);
1000 wl_list_remove(&window->link);
1001 free(window);
1002}
1003
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004static struct item *
1005window_find_item(struct window *window, int32_t x, int32_t y)
1006{
1007 struct item *item;
1008
1009 wl_list_for_each(item, &window->item_list, link) {
1010 if (item->allocation.x <= x &&
1011 x < item->allocation.x + item->allocation.width &&
1012 item->allocation.y <= y &&
1013 y < item->allocation.y + item->allocation.height) {
1014 return item;
1015 }
1016 }
1017
1018 return NULL;
1019}
1020
1021struct item *
1022window_add_item(struct window *window, void *data)
1023{
1024 struct item *item;
1025
1026 item = malloc(sizeof *item);
1027 memset(item, 0, sizeof *item);
1028 item->user_data = data;
1029 wl_list_insert(window->item_list.prev, &item->link);
1030
1031 return item;
1032}
1033
1034void
1035window_for_each_item(struct window *window, item_func_t func, void *data)
1036{
1037 struct item *item;
1038
1039 wl_list_for_each(item, &window->item_list, link)
1040 func(item, data);
1041}
1042
1043struct item *
1044window_get_focus_item(struct window *window)
1045{
1046 return window->focus_item;
1047}
1048
1049void
1050item_get_allocation(struct item *item, struct rectangle *allocation)
1051{
1052 *allocation = item->allocation;
1053}
1054
1055void
1056item_set_allocation(struct item *item,
1057 int32_t x, int32_t y, int32_t width, int32_t height)
1058{
1059 item->allocation.x = x;
1060 item->allocation.y = y;
1061 item->allocation.width = width;
1062 item->allocation.height = height;
1063}
1064
1065void *
1066item_get_user_data(struct item *item)
1067{
1068 return item->user_data;
1069}
1070
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001071void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001072window_draw(struct window *window)
1073{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001074 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001075 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001076 else
1077 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001078}
1079
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001080cairo_surface_t *
1081window_get_surface(struct window *window)
1082{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001083 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001084}
1085
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001086struct wl_surface *
1087window_get_wl_surface(struct window *window)
1088{
1089 return window->surface;
1090}
1091
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001092struct wl_shell_surface *
1093window_get_wl_shell_surface(struct window *window)
1094{
1095 return window->shell_surface;
1096}
1097
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001098static int
1099get_pointer_location(struct window *window, int32_t x, int32_t y)
1100{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001101 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001102 const int grip_size = 8;
1103
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001104 if (!window->decoration)
1105 return WINDOW_CLIENT_AREA;
1106
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001108 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109 else if (window->margin <= x && x < window->margin + grip_size)
1110 hlocation = WINDOW_RESIZING_LEFT;
1111 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001112 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001113 else if (x < window->allocation.width - window->margin)
1114 hlocation = WINDOW_RESIZING_RIGHT;
1115 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001116 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001117
1118 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001119 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001120 else if (window->margin <= y && y < window->margin + grip_size)
1121 vlocation = WINDOW_RESIZING_TOP;
1122 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001123 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 else if (y < window->allocation.height - window->margin)
1125 vlocation = WINDOW_RESIZING_BOTTOM;
1126 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001128
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001129 location = vlocation | hlocation;
1130 if (location & WINDOW_EXTERIOR)
1131 location = WINDOW_EXTERIOR;
1132 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1133 location = WINDOW_TITLEBAR;
1134 else if (location == WINDOW_INTERIOR)
1135 location = WINDOW_CLIENT_AREA;
1136
1137 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001138}
1139
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001140static int
1141input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001142{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144
1145 location = get_pointer_location(input->pointer_focus,
1146 input->sx, input->sy);
1147 switch (location) {
1148 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001149 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001150 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001151 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001152 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001153 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001155 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001156 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001157 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001159 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001160 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001161 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001162 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001163 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001164 case WINDOW_EXTERIOR:
1165 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001166 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001167 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001168 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001170}
1171
1172void
1173input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1174{
1175 struct display *display = input->display;
1176 struct wl_buffer *buffer;
1177 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001178
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001179 if (pointer == input->current_pointer_image)
1180 return;
1181
1182 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001183 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001184
1185 if (!surface)
1186 return;
1187
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001188 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001189 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001190 pointer_images[pointer].hotspot_x,
1191 pointer_images[pointer].hotspot_y);
1192}
1193
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001194static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001195window_set_focus_item(struct window *window, struct item *focus)
1196{
1197 void *data;
1198
1199 if (focus == window->focus_item)
1200 return;
1201
1202 window->focus_item = focus;
1203 data = focus ? focus->user_data : NULL;
1204 if (window->item_focus_handler)
1205 window->item_focus_handler(window, focus, data);
1206}
1207
1208static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001209window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001210 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001211 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001212{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001213 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001214 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001215 struct item *item;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001216 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001217
1218 input->x = x;
1219 input->y = y;
1220 input->sx = sx;
1221 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001222
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001223 if (!window->focus_item || !window->item_grab_button) {
1224 item = window_find_item(window, sx, sy);
1225 window_set_focus_item(window, item);
1226 }
1227
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228 if (window->motion_handler)
1229 pointer = (*window->motion_handler)(window, input, time,
1230 x, y, sx, sy,
1231 window->user_data);
1232
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001233 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001234 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001235}
1236
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001237static void
1238window_handle_button(void *data,
1239 struct wl_input_device *input_device,
1240 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001241{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001242 struct input *input = data;
1243 struct window *window = input->pointer_focus;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001244 struct item *item;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001246
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001247 if (window->focus_item && window->item_grab_button == 0 && state)
1248 window->item_grab_button = button;
1249
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001251
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -04001252 if (window->display->shell &&
1253 button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001255 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001256 if (!window->shell_surface)
1257 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001258 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001259 wl_shell_surface_move(window->shell_surface,
1260 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001261 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001262 case WINDOW_RESIZING_TOP:
1263 case WINDOW_RESIZING_BOTTOM:
1264 case WINDOW_RESIZING_LEFT:
1265 case WINDOW_RESIZING_RIGHT:
1266 case WINDOW_RESIZING_TOP_LEFT:
1267 case WINDOW_RESIZING_TOP_RIGHT:
1268 case WINDOW_RESIZING_BOTTOM_LEFT:
1269 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001270 if (!window->shell_surface)
1271 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001272 wl_shell_surface_resize(window->shell_surface,
1273 input_device, time,
1274 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001275 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001276 case WINDOW_CLIENT_AREA:
1277 if (window->button_handler)
1278 (*window->button_handler)(window,
1279 input, time,
1280 button, state,
1281 window->user_data);
1282 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001283 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001284 } else {
1285 if (window->button_handler)
1286 (*window->button_handler)(window,
1287 input, time,
1288 button, state,
1289 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001290 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001291
1292 if (window->focus_item &&
1293 window->item_grab_button == button && !state) {
1294 window->item_grab_button = 0;
1295 item = window_find_item(window, input->sx, input->sy);
1296 window_set_focus_item(window, item);
1297 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001298}
1299
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001300static void
1301window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001302 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001303{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001304 struct input *input = data;
1305 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001306 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001307 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001308
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001309 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001310 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001311 return;
1312
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001313 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001314 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001315 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1316 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001317
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001318 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1319
1320 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001321 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001322 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001324
1325 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001326 (*window->key_handler)(window, input, time, key, sym, state,
1327 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001328}
1329
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001330static void
1331window_handle_pointer_focus(void *data,
1332 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001333 uint32_t time, struct wl_surface *surface,
1334 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001335{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001336 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001337 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001338 struct item *item;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001339 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001340
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001341 window = input->pointer_focus;
1342 if (window && window->surface != surface) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001343 window_set_focus_item(window, NULL);
1344
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001345 if (window->leave_handler)
1346 window->leave_handler(window, input,
1347 time, window->user_data);
1348 input->pointer_focus = NULL;
1349 input->current_pointer_image = POINTER_UNSET;
1350 }
1351
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001352 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001353 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001354 window = input->pointer_focus;
1355
Kristian Høgsberg59826582011-01-20 11:56:57 -05001356 input->x = x;
1357 input->y = y;
1358 input->sx = sx;
1359 input->sy = sy;
1360
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001361 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001362 if (window->enter_handler)
1363 pointer = window->enter_handler(window, input,
1364 time, sx, sy,
1365 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001366
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001367 item = window_find_item(window, x, y);
1368 window_set_focus_item(window, item);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001369
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001370 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001371 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001372 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001373}
1374
1375static void
1376window_handle_keyboard_focus(void *data,
1377 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001378 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001379 struct wl_surface *surface,
1380 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001381{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001382 struct input *input = data;
1383 struct window *window = input->keyboard_focus;
1384 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001385 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001386
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001387 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001388 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001389 if (window->keyboard_focus_handler)
1390 (*window->keyboard_focus_handler)(window, NULL,
1391 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001392 }
1393
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001394 if (surface)
1395 input->keyboard_focus = wl_surface_get_user_data(surface);
1396 else
1397 input->keyboard_focus = NULL;
1398
1399 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001400 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001401 for (k = keys->data; k < end; k++)
1402 input->modifiers |= d->xkb->map->modmap[*k];
1403
1404 window = input->keyboard_focus;
1405 if (window) {
1406 window->keyboard_device = input;
1407 if (window->keyboard_focus_handler)
1408 (*window->keyboard_focus_handler)(window,
1409 window->keyboard_device,
1410 window->user_data);
1411 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001412}
1413
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001414static const struct wl_input_device_listener input_device_listener = {
1415 window_handle_motion,
1416 window_handle_button,
1417 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001418 window_handle_pointer_focus,
1419 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001420};
1421
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001422void
1423input_get_position(struct input *input, int32_t *x, int32_t *y)
1424{
1425 *x = input->sx;
1426 *y = input->sy;
1427}
1428
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001429struct wl_input_device *
1430input_get_input_device(struct input *input)
1431{
1432 return input->input_device;
1433}
1434
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001435uint32_t
1436input_get_modifiers(struct input *input)
1437{
1438 return input->modifiers;
1439}
1440
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001441struct data_offer {
1442 struct wl_data_offer *offer;
1443 struct input *input;
1444 struct wl_array types;
1445 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001446
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001447 struct task io_task;
1448 int fd;
1449 data_func_t func;
1450 int32_t x, y;
1451 void *user_data;
1452};
1453
1454static void
1455data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1456{
1457 struct data_offer *offer = data;
1458 char **p;
1459
1460 p = wl_array_add(&offer->types, sizeof *p);
1461 *p = strdup(type);
1462}
1463
1464static const struct wl_data_offer_listener data_offer_listener = {
1465 data_offer_offer,
1466};
1467
1468static void
1469data_offer_destroy(struct data_offer *offer)
1470{
1471 char **p;
1472
1473 offer->refcount--;
1474 if (offer->refcount == 0) {
1475 wl_data_offer_destroy(offer->offer);
1476 for (p = offer->types.data; *p; p++)
1477 free(*p);
1478 wl_array_release(&offer->types);
1479 free(offer);
1480 }
1481}
1482
1483static void
1484data_device_data_offer(void *data,
1485 struct wl_data_device *data_device, uint32_t id)
1486{
1487 struct data_offer *offer;
1488
1489 offer = malloc(sizeof *offer);
1490
1491 wl_array_init(&offer->types);
1492 offer->refcount = 1;
1493 offer->input = data;
1494
1495 /* FIXME: Generate typesafe wrappers for this */
1496 offer->offer = (struct wl_data_offer *)
1497 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1498 id, &wl_data_offer_interface);
1499
1500 wl_data_offer_add_listener(offer->offer,
1501 &data_offer_listener, offer);
1502}
1503
1504static void
1505data_device_enter(void *data, struct wl_data_device *data_device,
1506 uint32_t time, struct wl_surface *surface,
1507 int32_t x, int32_t y, struct wl_data_offer *offer)
1508{
1509 struct input *input = data;
1510 struct window *window;
1511 char **p;
1512
1513 input->drag_offer = wl_data_offer_get_user_data(offer);
1514 window = wl_surface_get_user_data(surface);
1515 input->pointer_focus = window;
1516
1517 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1518 *p = NULL;
1519
1520 window = input->pointer_focus;
1521 if (window->data_handler)
1522 window->data_handler(window, input, time, x, y,
1523 input->drag_offer->types.data,
1524 window->user_data);
1525}
1526
1527static void
1528data_device_leave(void *data, struct wl_data_device *data_device)
1529{
1530 struct input *input = data;
1531
1532 data_offer_destroy(input->drag_offer);
1533 input->drag_offer = NULL;
1534}
1535
1536static void
1537data_device_motion(void *data, struct wl_data_device *data_device,
1538 uint32_t time, int32_t x, int32_t y)
1539{
1540 struct input *input = data;
1541 struct window *window = input->pointer_focus;
1542
1543 input->sx = x;
1544 input->sy = y;
1545
1546 if (window->data_handler)
1547 window->data_handler(window, input, time, x, y,
1548 input->drag_offer->types.data,
1549 window->user_data);
1550}
1551
1552static void
1553data_device_drop(void *data, struct wl_data_device *data_device)
1554{
1555 struct input *input = data;
1556 struct window *window = input->pointer_focus;
1557
1558 if (window->drop_handler)
1559 window->drop_handler(window, input,
1560 input->sx, input->sy, window->user_data);
1561}
1562
1563static void
1564data_device_selection(void *data,
1565 struct wl_data_device *wl_data_device,
1566 struct wl_data_offer *offer)
1567{
1568 struct input *input = data;
1569 char **p;
1570
1571 if (input->selection_offer)
1572 data_offer_destroy(input->selection_offer);
1573
1574 input->selection_offer = wl_data_offer_get_user_data(offer);
1575 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1576 *p = NULL;
1577}
1578
1579static const struct wl_data_device_listener data_device_listener = {
1580 data_device_data_offer,
1581 data_device_enter,
1582 data_device_leave,
1583 data_device_motion,
1584 data_device_drop,
1585 data_device_selection
1586};
1587
1588struct wl_data_device *
1589input_get_data_device(struct input *input)
1590{
1591 return input->data_device;
1592}
1593
1594void
1595input_set_selection(struct input *input,
1596 struct wl_data_source *source, uint32_t time)
1597{
1598 wl_data_device_set_selection(input->data_device, source, time);
1599}
1600
1601void
1602input_accept(struct input *input, uint32_t time, const char *type)
1603{
1604 wl_data_offer_accept(input->drag_offer->offer, time, type);
1605}
1606
1607static void
1608offer_io_func(struct task *task, uint32_t events)
1609{
1610 struct data_offer *offer =
1611 container_of(task, struct data_offer, io_task);
1612 unsigned int len;
1613 char buffer[4096];
1614
1615 len = read(offer->fd, buffer, sizeof buffer);
1616 offer->func(buffer, len,
1617 offer->x, offer->y, offer->user_data);
1618
1619 if (len == 0) {
1620 close(offer->fd);
1621 data_offer_destroy(offer);
1622 }
1623}
1624
1625static void
1626data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1627 data_func_t func, void *user_data)
1628{
1629 int p[2];
1630
1631 pipe2(p, O_CLOEXEC);
1632 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1633 close(p[1]);
1634
1635 offer->io_task.run = offer_io_func;
1636 offer->fd = p[0];
1637 offer->func = func;
1638 offer->refcount++;
1639 offer->user_data = user_data;
1640
1641 display_watch_fd(offer->input->display,
1642 offer->fd, EPOLLIN, &offer->io_task);
1643}
1644
1645void
1646input_receive_drag_data(struct input *input, const char *mime_type,
1647 data_func_t func, void *data)
1648{
1649 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1650 input->drag_offer->x = input->sx;
1651 input->drag_offer->y = input->sy;
1652}
1653
1654int
1655input_receive_selection_data(struct input *input, const char *mime_type,
1656 data_func_t func, void *data)
1657{
1658 char **p;
1659
1660 if (input->selection_offer == NULL)
1661 return -1;
1662
1663 for (p = input->selection_offer->types.data; *p; p++)
1664 if (strcmp(mime_type, *p) == 0)
1665 break;
1666
1667 if (*p == NULL)
1668 return -1;
1669
1670 data_offer_receive_data(input->selection_offer,
1671 mime_type, func, data);
1672 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001673}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001674
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001675void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001676window_move(struct window *window, struct input *input, uint32_t time)
1677{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001678 if (!window->shell_surface)
1679 return;
1680
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001681 wl_shell_surface_move(window->shell_surface,
1682 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001683}
1684
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001685static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001686handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001687 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001688 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001689{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001690 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001691 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001692
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001693 /* FIXME: this is probably the wrong place to check for width
1694 * or height <= 0, but it prevents the compositor from crashing
1695 */
1696 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001697 return;
1698
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001699 window->resize_edges = edges;
1700
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001701 if (window->resize_handler) {
1702 child_width = width - 20 - window->margin * 2;
1703 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001704
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001705 (*window->resize_handler)(window,
1706 child_width, child_height,
1707 window->user_data);
1708 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001709 window->allocation.width = width;
1710 window->allocation.height = height;
1711
1712 if (window->redraw_handler)
1713 window_schedule_redraw(window);
1714 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001715}
1716
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001717static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001718 handle_configure,
1719};
1720
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001721void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001722window_get_allocation(struct window *window,
1723 struct rectangle *allocation)
1724{
1725 *allocation = window->allocation;
1726}
1727
1728void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001729window_get_child_allocation(struct window *window,
1730 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001731{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001732 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001733 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001734 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001735 allocation->x = window->margin + 10;
1736 allocation->y = window->margin + 50;
1737 allocation->width =
1738 window->allocation.width - 20 - window->margin * 2;
1739 allocation->height =
1740 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001741 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001742}
1743
1744void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001745window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001746{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001747 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001748 window->allocation.x = 20 + window->margin;
1749 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001750 window->allocation.width = width + 20 + window->margin * 2;
1751 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001752 } else {
1753 window->allocation.x = 0;
1754 window->allocation.y = 0;
1755 window->allocation.width = width;
1756 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001757 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001758}
1759
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001760static void
1761idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001762{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001763 struct window *window =
1764 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001765
1766 window->redraw_handler(window, window->user_data);
1767 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001768}
1769
1770void
1771window_schedule_redraw(struct window *window)
1772{
1773 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001774 window->redraw_task.run = idle_redraw;
1775 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001776 window->redraw_scheduled = 1;
1777 }
1778}
1779
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001780void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001781window_set_custom(struct window *window)
1782{
1783 window->type = TYPE_CUSTOM;
1784}
1785
1786void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001787window_set_fullscreen(struct window *window, int fullscreen)
1788{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001789 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001790 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001791
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001792 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001793 return;
1794
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001795 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001796 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001797 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001798 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001799 width = output->allocation.width;
1800 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001801 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001802 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001803 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001804 width = window->saved_allocation.width - 20 - window->margin * 2;
1805 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001806 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001807 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001808
1809 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001810}
1811
1812void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001813window_set_decoration(struct window *window, int decoration)
1814{
1815 window->decoration = decoration;
1816}
1817
1818void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001819window_set_user_data(struct window *window, void *data)
1820{
1821 window->user_data = data;
1822}
1823
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001824void *
1825window_get_user_data(struct window *window)
1826{
1827 return window->user_data;
1828}
1829
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001830void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001831window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001832 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001833{
1834 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001835}
1836
1837void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001838window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001839 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001840{
1841 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001842}
1843
1844void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001845window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001846 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001847{
1848 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001849}
1850
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001851void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001852window_set_button_handler(struct window *window,
1853 window_button_handler_t handler)
1854{
1855 window->button_handler = handler;
1856}
1857
1858void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001859window_set_motion_handler(struct window *window,
1860 window_motion_handler_t handler)
1861{
1862 window->motion_handler = handler;
1863}
1864
1865void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001866window_set_enter_handler(struct window *window,
1867 window_enter_handler_t handler)
1868{
1869 window->enter_handler = handler;
1870}
1871
1872void
1873window_set_leave_handler(struct window *window,
1874 window_leave_handler_t handler)
1875{
1876 window->leave_handler = handler;
1877}
1878
1879void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001880window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001881 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001882{
1883 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001884}
1885
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001886void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001887window_set_item_focus_handler(struct window *window,
1888 window_item_focus_handler_t handler)
1889{
1890 window->item_focus_handler = handler;
1891}
1892
1893void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001894window_set_data_handler(struct window *window, window_data_handler_t handler)
1895{
1896 window->data_handler = handler;
1897}
1898
1899void
1900window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1901{
1902 window->drop_handler = handler;
1903}
1904
1905void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001906window_set_transparent(struct window *window, int transparent)
1907{
1908 window->transparent = transparent;
1909}
1910
1911void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001912window_set_title(struct window *window, const char *title)
1913{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001914 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001915 window->title = strdup(title);
1916}
1917
1918const char *
1919window_get_title(struct window *window)
1920{
1921 return window->title;
1922}
1923
1924void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001925display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1926 int32_t x, int32_t y, int32_t width, int32_t height)
1927{
1928 struct wl_buffer *buffer;
1929
1930 buffer = display_get_buffer_for_surface(display, cairo_surface);
1931
1932 wl_buffer_damage(buffer, x, y, width, height);
1933}
1934
1935void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001936window_damage(struct window *window, int32_t x, int32_t y,
1937 int32_t width, int32_t height)
1938{
1939 wl_surface_damage(window->surface, x, y, width, height);
1940}
1941
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001942static struct window *
1943window_create_internal(struct display *display, struct window *parent,
1944 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001945{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001946 struct window *window;
1947
1948 window = malloc(sizeof *window);
1949 if (window == NULL)
1950 return NULL;
1951
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001952 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001953 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001954 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001955 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001956 if (display->shell) {
1957 window->shell_surface =
1958 wl_shell_get_shell_surface(display->shell,
1959 window->surface);
1960 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001961 window->allocation.x = 0;
1962 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001963 window->allocation.width = width;
1964 window->allocation.height = height;
1965 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001966 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001967 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001968 window->transparent = 1;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001969 wl_list_init(&window->item_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001970
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001971 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001972#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001973 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1974 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001975#else
1976 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1977#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001978 else
1979 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001980
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001981 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001982 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001983
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001984 if (window->shell_surface) {
1985 wl_shell_surface_set_user_data(window->shell_surface, window);
1986 wl_shell_surface_add_listener(window->shell_surface,
1987 &shell_surface_listener, window);
1988 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001989
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001990 return window;
1991}
1992
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001993struct window *
1994window_create(struct display *display, int32_t width, int32_t height)
1995{
1996 struct window *window;
1997
1998 window = window_create_internal(display, NULL, width, height);
1999 if (!window)
2000 return NULL;
2001
2002 return window;
2003}
2004
2005struct window *
2006window_create_transient(struct display *display, struct window *parent,
2007 int32_t x, int32_t y, int32_t width, int32_t height)
2008{
2009 struct window *window;
2010
2011 window = window_create_internal(parent->display,
2012 parent, width, height);
2013 if (!window)
2014 return NULL;
2015
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002016 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002017 window->x = x;
2018 window->y = y;
2019
2020 return window;
2021}
2022
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002023struct menu {
2024 struct window *window;
2025 const char **entries;
2026 int current;
2027 int count;
2028};
2029
2030static int
2031menu_set_item(struct window *window, struct menu *menu, int sy)
2032{
2033 int next;
2034
2035 next = (sy - 8) / 20;
2036 if (menu->current != next) {
2037 menu->current = next;
2038 window_schedule_redraw(window);
2039 }
2040
2041 return POINTER_LEFT_PTR;
2042}
2043
2044static int
2045menu_motion_handler(struct window *window,
2046 struct input *input, uint32_t time,
2047 int32_t x, int32_t y,
2048 int32_t sx, int32_t sy, void *data)
2049{
2050 return menu_set_item(window, data, sy);
2051}
2052
2053static int
2054menu_enter_handler(struct window *window,
2055 struct input *input, uint32_t time,
2056 int32_t x, int32_t y, void *data)
2057{
2058 return menu_set_item(window, data, y);
2059}
2060
2061static void
2062menu_leave_handler(struct window *window,
2063 struct input *input, uint32_t time, void *data)
2064{
2065 menu_set_item(window, data, -200);
2066}
2067
2068static void
2069menu_button_handler(struct window *window,
2070 struct input *input, uint32_t time,
2071 int button, int state, void *data)
2072
2073{
2074 struct menu *menu = data;
2075
2076 /* Either relase after press-drag-release or click-motion-click. */
2077 if (state == 0 && 0 <= menu->current && menu->current < menu->count)
2078 window_destroy(window);
2079}
2080
2081static void
2082menu_redraw_handler(struct window *window, void *data)
2083{
2084 cairo_t *cr;
2085 const int32_t r = 3, margin = 3;
2086 struct menu *menu = data;
2087 int32_t width, height, i;
2088
2089 width = 200;
2090 height = menu->count * 20 + margin * 2;
2091 window_set_child_size(window, width, height);
2092 window_create_surface(window);
2093
2094 cr = cairo_create(window->cairo_surface);
2095 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2096 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2097 cairo_paint(cr);
2098
2099 width = window->allocation.width;
2100 height = window->allocation.height;
2101 rounded_rect(cr, 0, 0, width, height, r);
2102 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2103 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2104 cairo_fill(cr);
2105
2106 for (i = 0; i < menu->count; i++) {
2107 if (i == menu->current) {
2108 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2109 cairo_rectangle(cr, margin, i * 20 + margin,
2110 width - 2 * margin, 20);
2111 cairo_fill(cr);
2112 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2113 cairo_move_to(cr, 10, i * 20 + 16);
2114 cairo_show_text(cr, menu->entries[i]);
2115 } else {
2116 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2117 cairo_move_to(cr, 10, i * 20 + 16);
2118 cairo_show_text(cr, menu->entries[i]);
2119 }
2120 }
2121
2122 cairo_destroy(cr);
2123 window_flush(window);
2124}
2125
2126struct window *
2127window_create_menu(struct display *display, struct window *parent,
2128 int32_t x, int32_t y, const char **entries, int count)
2129{
2130 struct window *window;
2131 struct menu *menu;
2132
2133 menu = malloc(sizeof *menu);
2134 if (!menu)
2135 return NULL;
2136
2137 window = window_create_internal(parent->display, parent, 0, 0);
2138 if (!window)
2139 return NULL;
2140
2141 menu->window = window;
2142 menu->entries = entries;
2143 menu->count = count;
2144 window->decoration = 0;
2145 window->type = TYPE_MENU;
2146 window->x = x;
2147 window->y = y;
2148
2149 wl_shell_surface_set_transient(window->shell_surface,
2150 window->parent->shell_surface,
2151 window->x, window->y, 0);
2152
2153 window_set_motion_handler(window, menu_motion_handler);
2154 window_set_enter_handler(window, menu_enter_handler);
2155 window_set_leave_handler(window, menu_leave_handler);
2156 window_set_button_handler(window, menu_button_handler);
2157 window_set_redraw_handler(window, menu_redraw_handler);
2158 window_set_user_data(window, menu);
2159
2160 return window;
2161}
2162
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002163void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002164window_set_buffer_type(struct window *window, enum window_buffer_type type)
2165{
2166 window->buffer_type = type;
2167}
2168
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002169
2170static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002171display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002172 struct wl_output *wl_output,
2173 int x, int y,
2174 int physical_width,
2175 int physical_height,
2176 int subpixel,
2177 const char *make,
2178 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002179{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002180 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002181
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002182 output->allocation.x = x;
2183 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002184}
2185
2186static void
2187display_handle_mode(void *data,
2188 struct wl_output *wl_output,
2189 uint32_t flags,
2190 int width,
2191 int height,
2192 int refresh)
2193{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002194 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002195 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002196
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002197 if (flags & WL_OUTPUT_MODE_CURRENT) {
2198 output->allocation.width = width;
2199 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002200 if (display->output_configure_handler)
2201 (*display->output_configure_handler)(
2202 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002203 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002204}
2205
2206static const struct wl_output_listener output_listener = {
2207 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002208 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002209};
2210
2211static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002212display_add_output(struct display *d, uint32_t id)
2213{
2214 struct output *output;
2215
2216 output = malloc(sizeof *output);
2217 if (output == NULL)
2218 return;
2219
2220 memset(output, 0, sizeof *output);
2221 output->display = d;
2222 output->output =
2223 wl_display_bind(d->display, id, &wl_output_interface);
2224 wl_list_insert(d->output_list.prev, &output->link);
2225
2226 wl_output_add_listener(output->output, &output_listener, output);
2227}
2228
2229void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002230display_set_output_configure_handler(struct display *display,
2231 display_output_handler_t handler)
2232{
2233 struct output *output;
2234
2235 display->output_configure_handler = handler;
2236 if (!handler)
2237 return;
2238
2239 wl_list_for_each(output, &display->output_list, link)
2240 (*display->output_configure_handler)(output,
2241 display->user_data);
2242}
2243
2244void
2245output_set_user_data(struct output *output, void *data)
2246{
2247 output->user_data = data;
2248}
2249
2250void *
2251output_get_user_data(struct output *output)
2252{
2253 return output->user_data;
2254}
2255
2256void
2257output_set_destroy_handler(struct output *output,
2258 display_output_handler_t handler)
2259{
2260 output->destroy_handler = handler;
2261 /* FIXME: implement this, once we have way to remove outputs */
2262}
2263
2264void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002265output_get_allocation(struct output *output, struct rectangle *allocation)
2266{
2267 *allocation = output->allocation;
2268}
2269
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002270struct wl_output *
2271output_get_wl_output(struct output *output)
2272{
2273 return output->output;
2274}
2275
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002276static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002277display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002278{
2279 struct input *input;
2280
2281 input = malloc(sizeof *input);
2282 if (input == NULL)
2283 return;
2284
2285 memset(input, 0, sizeof *input);
2286 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002287 input->input_device =
2288 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002289 input->pointer_focus = NULL;
2290 input->keyboard_focus = NULL;
2291 wl_list_insert(d->input_list.prev, &input->link);
2292
2293 wl_input_device_add_listener(input->input_device,
2294 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002295 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002296
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002297 input->data_device =
2298 wl_data_device_manager_get_data_device(d->data_device_manager,
2299 input->input_device);
2300 wl_data_device_add_listener(input->data_device,
2301 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002302}
2303
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002304static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002305display_handle_global(struct wl_display *display, uint32_t id,
2306 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002307{
2308 struct display *d = data;
2309
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002310 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002311 d->compositor =
2312 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002313 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002314 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002315 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002316 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002317 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002318 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002319 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002320 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002321 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2322 d->data_device_manager =
2323 wl_display_bind(display, id,
2324 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002325 }
2326}
2327
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002328static void
2329display_render_frame(struct display *d)
2330{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002331 int radius = 8;
2332 cairo_t *cr;
2333
2334 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2335 cr = cairo_create(d->shadow);
2336 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2337 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2338 rounded_rect(cr, 16, 16, 112, 112, radius);
2339 cairo_fill(cr);
2340 cairo_destroy(cr);
2341 blur_surface(d->shadow, 64);
2342
2343 d->active_frame =
2344 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2345 cr = cairo_create(d->active_frame);
2346 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2347 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2348 rounded_rect(cr, 16, 16, 112, 112, radius);
2349 cairo_fill(cr);
2350 cairo_destroy(cr);
2351
2352 d->inactive_frame =
2353 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2354 cr = cairo_create(d->inactive_frame);
2355 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2356 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2357 rounded_rect(cr, 16, 16, 112, 112, radius);
2358 cairo_fill(cr);
2359 cairo_destroy(cr);
2360}
2361
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002362static void
2363init_xkb(struct display *d)
2364{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002365 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002366
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002367 names.rules = "evdev";
2368 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002369 names.layout = option_xkb_layout;
2370 names.variant = option_xkb_variant;
2371 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002372
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002373 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002374 if (!d->xkb) {
2375 fprintf(stderr, "Failed to compile keymap\n");
2376 exit(1);
2377 }
2378}
2379
Yuval Fledel45568f62010-12-06 09:18:12 -05002380static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002381init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002382{
2383 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002384 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002385
2386 static const EGLint premul_argb_cfg_attribs[] = {
2387 EGL_SURFACE_TYPE,
2388 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2389 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002390 EGL_RED_SIZE, 1,
2391 EGL_GREEN_SIZE, 1,
2392 EGL_BLUE_SIZE, 1,
2393 EGL_ALPHA_SIZE, 1,
2394 EGL_DEPTH_SIZE, 1,
2395 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2396 EGL_NONE
2397 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002398
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002399 static const EGLint rgb_cfg_attribs[] = {
2400 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2401 EGL_RED_SIZE, 1,
2402 EGL_GREEN_SIZE, 1,
2403 EGL_BLUE_SIZE, 1,
2404 EGL_ALPHA_SIZE, 0,
2405 EGL_DEPTH_SIZE, 1,
2406 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2407 EGL_NONE
2408 };
2409
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002410 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002411 if (!eglInitialize(d->dpy, &major, &minor)) {
2412 fprintf(stderr, "failed to initialize display\n");
2413 return -1;
2414 }
2415
2416 if (!eglBindAPI(EGL_OPENGL_API)) {
2417 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2418 return -1;
2419 }
2420
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002421 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002422 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002423 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002424 return -1;
2425 }
2426
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002427 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2428 &d->rgb_config, 1, &n) || n != 1) {
2429 fprintf(stderr, "failed to choose rgb config\n");
2430 return -1;
2431 }
2432
Benjamin Franzke0c991632011-09-27 21:57:31 +02002433 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2434 if (d->rgb_ctx == NULL) {
2435 fprintf(stderr, "failed to create context\n");
2436 return -1;
2437 }
2438 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2439 EGL_NO_CONTEXT, NULL);
2440 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002441 fprintf(stderr, "failed to create context\n");
2442 return -1;
2443 }
2444
Benjamin Franzke0c991632011-09-27 21:57:31 +02002445 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002446 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002447 return -1;
2448 }
2449
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002450#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002451 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2452 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002453 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002454 return -1;
2455 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002456 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2457 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2458 fprintf(stderr, "failed to get cairo egl argb device\n");
2459 return -1;
2460 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002461#endif
2462
2463 return 0;
2464}
2465
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002466static int
2467event_mask_update(uint32_t mask, void *data)
2468{
2469 struct display *d = data;
2470
2471 d->mask = mask;
2472
2473 return 0;
2474}
2475
2476static void
2477handle_display_data(struct task *task, uint32_t events)
2478{
2479 struct display *display =
2480 container_of(task, struct display, display_task);
2481
2482 wl_display_iterate(display->display, display->mask);
2483}
2484
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002485struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002486display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002487{
2488 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002489 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002490 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002491 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002492
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002493 g_type_init();
2494
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002495 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002496 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002497 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002498
2499 xkb_option_group = g_option_group_new("xkb",
2500 "Keyboard options",
2501 "Show all XKB options",
2502 NULL, NULL);
2503 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2504 g_option_context_add_group (context, xkb_option_group);
2505
2506 if (!g_option_context_parse(context, argc, argv, &error)) {
2507 fprintf(stderr, "option parsing failed: %s\n", error->message);
2508 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002509 }
2510
Tim Wiederhake748f6722011-01-23 23:25:25 +01002511 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002512
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002513 d = malloc(sizeof *d);
2514 if (d == NULL)
2515 return NULL;
2516
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002517 memset(d, 0, sizeof *d);
2518
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002519 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002520 if (d->display == NULL) {
2521 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002522 return NULL;
2523 }
2524
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002525 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2526 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2527 d->display_task.run = handle_display_data;
2528 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2529
2530 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002531 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002532 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002533
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002534 /* Set up listener so we'll catch all events. */
2535 wl_display_add_global_listener(d->display,
2536 display_handle_global, d);
2537
2538 /* Process connection events. */
2539 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002540 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002541 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002542
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002543 d->image_target_texture_2d =
2544 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2545 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2546 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2547
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002548 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002549
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002550 display_render_frame(d);
2551
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002552 wl_list_init(&d->window_list);
2553
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002554 init_xkb(d);
2555
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002556 return d;
2557}
2558
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002559void
2560display_set_user_data(struct display *display, void *data)
2561{
2562 display->user_data = data;
2563}
2564
2565void *
2566display_get_user_data(struct display *display)
2567{
2568 return display->user_data;
2569}
2570
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002571struct wl_display *
2572display_get_display(struct display *display)
2573{
2574 return display->display;
2575}
2576
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002577struct output *
2578display_get_output(struct display *display)
2579{
2580 return container_of(display->output_list.next, struct output, link);
2581}
2582
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002583struct wl_compositor *
2584display_get_compositor(struct display *display)
2585{
2586 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002587}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002588
2589EGLDisplay
2590display_get_egl_display(struct display *d)
2591{
2592 return d->dpy;
2593}
2594
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002595struct wl_data_source *
2596display_create_data_source(struct display *display)
2597{
2598 return wl_data_device_manager_create_data_source(display->data_device_manager);
2599}
2600
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002601EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002602display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002603{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002604 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002605}
2606
Benjamin Franzke0c991632011-09-27 21:57:31 +02002607EGLConfig
2608display_get_argb_egl_config(struct display *d)
2609{
2610 return d->premultiplied_argb_config;
2611}
2612
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002613struct wl_shell *
2614display_get_shell(struct display *display)
2615{
2616 return display->shell;
2617}
2618
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002619int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002620display_acquire_window_surface(struct display *display,
2621 struct window *window,
2622 EGLContext ctx)
2623{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002624#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002625 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002626 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002627
2628 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002629 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002630 device = cairo_surface_get_device(window->cairo_surface);
2631 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002632 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002633
Benjamin Franzke0c991632011-09-27 21:57:31 +02002634 if (!ctx) {
2635 if (device == display->rgb_device)
2636 ctx = display->rgb_ctx;
2637 else if (device == display->argb_device)
2638 ctx = display->argb_ctx;
2639 else
2640 assert(0);
2641 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002642
2643 data = cairo_surface_get_user_data(window->cairo_surface,
2644 &surface_data_key);
2645
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002646 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002647 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002648 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2649 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002650
2651 return 0;
2652#else
2653 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002654#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002655}
2656
2657void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002658display_release_window_surface(struct display *display,
2659 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002660{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002661#ifdef HAVE_CAIRO_EGL
2662 cairo_device_t *device;
2663
2664 device = cairo_surface_get_device(window->cairo_surface);
2665 if (!device)
2666 return;
2667
2668 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002669 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002670 cairo_device_release(device);
2671#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002672}
2673
2674void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002675display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002676{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002677 wl_list_insert(&display->deferred_list, &task->link);
2678}
2679
2680void
2681display_watch_fd(struct display *display,
2682 int fd, uint32_t events, struct task *task)
2683{
2684 struct epoll_event ep;
2685
2686 ep.events = events;
2687 ep.data.ptr = task;
2688 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2689}
2690
2691void
2692display_run(struct display *display)
2693{
2694 struct task *task;
2695 struct epoll_event ep[16];
2696 int i, count;
2697
2698 while (1) {
2699 while (display->mask & WL_DISPLAY_WRITABLE)
2700 wl_display_iterate(display->display,
2701 WL_DISPLAY_WRITABLE);
2702
2703 count = epoll_wait(display->epoll_fd,
2704 ep, ARRAY_LENGTH(ep), -1);
2705 for (i = 0; i < count; i++) {
2706 task = ep[i].data.ptr;
2707 task->run(task, ep[i].events);
2708 }
2709
2710 while (!wl_list_empty(&display->deferred_list)) {
2711 task = container_of(display->deferred_list.next,
2712 struct task, link);
2713 wl_list_remove(&task->link);
2714 task->run(task, 0);
2715 }
2716 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002717}