blob: 48b444aa035299e460246bc15417bc2bff82b667 [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øgsbergd0c3b9d2010-10-25 11:40:03 -040023#include "../config.h"
24
Kristian Høgsberg61017b12008-11-02 18:51:48 -050025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050029#include <fcntl.h>
30#include <unistd.h>
31#include <math.h>
32#include <time.h>
33#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050034#include <glib.h>
Kristian Høgsberg478d9262010-06-08 20:34:11 -040035#include <glib-object.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040036#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050039#include <wayland-egl.h>
40
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041#include <GL/gl.h>
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040044
Kristian Høgsberg8def2642011-01-14 17:41:33 -050045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040046#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040049#include <X11/extensions/XKBcommon.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050052#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050054#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040055#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050057#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050058
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050060 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050061 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040062 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040063 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040069 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010070 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040072 GMainLoop *loop;
73 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040074 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040075 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040076 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040077 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040078 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040079 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040080
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050081 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
82 PFNEGLCREATEIMAGEKHRPROC create_image;
83 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084};
85
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040086enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040087 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040088 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040089 TYPE_TRANSIENT,
90 TYPE_CUSTOM
91};
92
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050093struct window {
94 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050095 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050096 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050097 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050098 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050099 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400100 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400101 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500102 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500103 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400105 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400106 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400107 struct input *grab_device;
108 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500109 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400110 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500111
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500113 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500114
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500115 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500117 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400118 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500119 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400120 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400121 window_enter_handler_t enter_handler;
122 window_leave_handler_t leave_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400123
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500124 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400125 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500126};
127
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128struct input {
129 struct display *display;
130 struct wl_input_device *input_device;
131 struct window *pointer_focus;
132 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500133 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400134 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400135 uint32_t modifiers;
136 int32_t x, y, sx, sy;
137 struct wl_list link;
138};
139
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400140enum {
141 POINTER_DEFAULT = 100,
142 POINTER_UNSET
143};
144
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500145enum window_location {
146 WINDOW_INTERIOR = 0,
147 WINDOW_RESIZING_TOP = 1,
148 WINDOW_RESIZING_BOTTOM = 2,
149 WINDOW_RESIZING_LEFT = 4,
150 WINDOW_RESIZING_TOP_LEFT = 5,
151 WINDOW_RESIZING_BOTTOM_LEFT = 6,
152 WINDOW_RESIZING_RIGHT = 8,
153 WINDOW_RESIZING_TOP_RIGHT = 9,
154 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
155 WINDOW_RESIZING_MASK = 15,
156 WINDOW_EXTERIOR = 16,
157 WINDOW_TITLEBAR = 17,
158 WINDOW_CLIENT_AREA = 18,
159};
160
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400161const char *option_xkb_layout = "us";
162const char *option_xkb_variant = "";
163const char *option_xkb_options = "";
164
165static const GOptionEntry xkb_option_entries[] = {
166 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
167 &option_xkb_layout, "XKB Layout" },
168 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
169 &option_xkb_variant, "XKB Variant" },
170 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
171 &option_xkb_options, "XKB Options" },
172 { NULL }
173};
174
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400175static const cairo_user_data_key_t surface_data_key;
176struct surface_data {
177 struct wl_buffer *buffer;
178};
179
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500180#define MULT(_d,c,a,t) \
181 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
182
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500183#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400184
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100185struct egl_window_surface_data {
186 struct display *display;
187 struct wl_surface *surface;
188 struct wl_egl_window *window;
189 EGLSurface surf;
190};
191
192static void
193egl_window_surface_data_destroy(void *p)
194{
195 struct egl_window_surface_data *data = p;
196 struct display *d = data->display;
197
198 eglDestroySurface(d->dpy, data->surf);
199 wl_egl_window_destroy(data->window);
200 data->surface = NULL;
201
202 free(p);
203}
204
205static cairo_surface_t *
206display_create_egl_window_surface(struct display *display,
207 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400208 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100209 struct rectangle *rectangle)
210{
211 cairo_surface_t *cairo_surface;
212 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400213 EGLConfig config;
214 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100215
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400216 static const EGLint premul_attribs[] = {
217 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
218 EGL_NONE
219 };
220
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100221 data = malloc(sizeof *data);
222 if (data == NULL)
223 return NULL;
224
225 data->display = display;
226 data->surface = surface;
227
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400228 if (flags & SURFACE_OPAQUE) {
229 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200230 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400231 } else {
232 config = display->premultiplied_argb_config;
233 attribs = premul_attribs;
234 }
235
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400236 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100237 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400238 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100239
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400240 data->surf = eglCreateWindowSurface(display->dpy, config,
241 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100242
243 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
244 data->surf,
245 rectangle->width,
246 rectangle->height);
247
248 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
249 data, egl_window_surface_data_destroy);
250
251 return cairo_surface;
252}
253
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500254struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400255 struct surface_data data;
256 EGLImageKHR image;
257 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800258 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500259 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400260};
261
262static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500263egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400264{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500265 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800266 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400267
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800268 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400269 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800270 cairo_device_release(d->device);
271
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500272 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400273 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500274 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400276}
277
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500278EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500279display_get_image_for_egl_image_surface(struct display *display,
280 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500281{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500282 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500283
284 data = cairo_surface_get_user_data (surface, &surface_data_key);
285
286 return data->image;
287}
288
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500289static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500290display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400291 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500292 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400293{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500294 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400295 EGLDisplay dpy = display->dpy;
296 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400298
299 data = malloc(sizeof *data);
300 if (data == NULL)
301 return NULL;
302
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800303 data->display = display;
304
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400305 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400306 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500307 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000308 free(data);
309 return NULL;
310 }
311
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400312 if (flags & SURFACE_OPAQUE)
313 config = display->rgb_config;
314 else
315 config = display->premultiplied_argb_config;
316
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500317 data->image = display->create_image(dpy, NULL,
318 EGL_NATIVE_PIXMAP_KHR,
319 (EGLClientBuffer) data->pixmap,
320 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500322 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 free(data);
324 return NULL;
325 }
326
327 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400328 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500333 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800334 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336 surface = cairo_gl_surface_create_for_texture(display->device,
337 CAIRO_CONTENT_COLOR_ALPHA,
338 data->texture,
339 rectangle->width,
340 rectangle->height);
341
342 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500343 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400344
345 return surface;
346}
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349display_create_egl_image_surface_from_file(struct display *display,
350 const char *filename,
351 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352{
353 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400354 GdkPixbuf *pixbuf;
355 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400356 int stride, i;
357 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500358 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400359
360 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400362 FALSE, &error);
363 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400364 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400365
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400366 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
367 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500368 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400370 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400371
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372
373 stride = gdk_pixbuf_get_rowstride(pixbuf);
374 pixels = gdk_pixbuf_get_pixels(pixbuf);
375
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400376 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400377 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400378 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400379 while (p < end) {
380 unsigned int t;
381
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400382 MULT(p[0], p[0], p[3], t);
383 MULT(p[1], p[1], p[3], t);
384 MULT(p[2], p[2], p[3], t);
385 p += 4;
386
387 }
388 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400389
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200390 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000391 if (surface == NULL) {
392 g_object_unref(pixbuf);
393 return NULL;
394 }
395
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800396 data = cairo_surface_get_user_data(surface, &surface_data_key);
397
398 cairo_device_acquire(display->device);
399 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800400 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
401 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800402 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400403
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500404 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400405
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406 return surface;
407}
408
409#endif
410
411struct wl_buffer *
412display_get_buffer_for_surface(struct display *display,
413 cairo_surface_t *surface)
414{
415 struct surface_data *data;
416
417 data = cairo_surface_get_user_data (surface, &surface_data_key);
418
419 return data->buffer;
420}
421
422struct shm_surface_data {
423 struct surface_data data;
424 void *map;
425 size_t length;
426};
427
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500428static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429shm_surface_data_destroy(void *p)
430{
431 struct shm_surface_data *data = p;
432
433 wl_buffer_destroy(data->data.buffer);
434 munmap(data->map, data->length);
435}
436
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500437static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400439 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400440{
441 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400442 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400443 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800444 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445 char filename[] = "/tmp/wayland-shm-XXXXXX";
446
447 data = malloc(sizeof *data);
448 if (data == NULL)
449 return NULL;
450
451 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
452 rectangle->width);
453 data->length = stride * rectangle->height;
454 fd = mkstemp(filename);
455 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000456 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457 return NULL;
458 }
459 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000460 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461 close(fd);
462 return NULL;
463 }
464
465 data->map = mmap(NULL, data->length,
466 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
467 unlink(filename);
468
469 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000470 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400471 close(fd);
472 return NULL;
473 }
474
475 surface = cairo_image_surface_create_for_data (data->map,
476 CAIRO_FORMAT_ARGB32,
477 rectangle->width,
478 rectangle->height,
479 stride);
480
481 cairo_surface_set_user_data (surface, &surface_data_key,
482 data, shm_surface_data_destroy);
483
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400484 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400485 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400486 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400487 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400488
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489 data->data.buffer = wl_shm_create_buffer(display->shm,
490 fd,
491 rectangle->width,
492 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400493 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400494
495 close(fd);
496
497 return surface;
498}
499
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500500static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501display_create_shm_surface_from_file(struct display *display,
502 const char *filename,
503 struct rectangle *rect)
504{
505 cairo_surface_t *surface;
506 GdkPixbuf *pixbuf;
507 GError *error = NULL;
508 int stride, i;
509 unsigned char *pixels, *p, *end, *dest_data;
510 int dest_stride;
511 uint32_t *d;
512
513 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
514 rect->width, rect->height,
515 FALSE, &error);
516 if (error != NULL)
517 return NULL;
518
519 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
520 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500521 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522 return NULL;
523 }
524
525 stride = gdk_pixbuf_get_rowstride(pixbuf);
526 pixels = gdk_pixbuf_get_pixels(pixbuf);
527
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400528 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000529 if (surface == NULL) {
530 g_object_unref(pixbuf);
531 return NULL;
532 }
533
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534 dest_data = cairo_image_surface_get_data (surface);
535 dest_stride = cairo_image_surface_get_stride (surface);
536
537 for (i = 0; i < rect->height; i++) {
538 d = (uint32_t *) (dest_data + i * dest_stride);
539 p = pixels + i * stride;
540 end = p + rect->width * 4;
541 while (p < end) {
542 unsigned int t;
543 unsigned char a, r, g, b;
544
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400545 a = p[3];
546 MULT(r, p[0], a, t);
547 MULT(g, p[1], a, t);
548 MULT(b, p[2], a, t);
549 p += 4;
550 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
551 }
552 }
553
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500554 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555
556 return surface;
557}
558
nobled7b87cb02011-02-01 18:51:47 +0000559static int
560check_size(struct rectangle *rect)
561{
562 if (rect->width && rect->height)
563 return 0;
564
565 fprintf(stderr, "tried to create surface of "
566 "width: %d, height: %d\n", rect->width, rect->height);
567 return -1;
568}
569
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400570cairo_surface_t *
571display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100572 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400573 struct rectangle *rectangle,
574 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575{
nobled7b87cb02011-02-01 18:51:47 +0000576 if (check_size(rectangle) < 0)
577 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500578#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500579 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100580 if (surface)
581 return display_create_egl_window_surface(display,
582 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400583 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100584 rectangle);
585 else
586 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400587 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100588 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500589 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400591 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592}
593
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500594static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595display_create_surface_from_file(struct display *display,
596 const char *filename,
597 struct rectangle *rectangle)
598{
nobled7b87cb02011-02-01 18:51:47 +0000599 if (check_size(rectangle) < 0)
600 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500601#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500602 if (display->dpy) {
603 return display_create_egl_image_surface_from_file(display,
604 filename,
605 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500606 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500608 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400609}
Yuval Fledel45568f62010-12-06 09:18:12 -0500610 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400611 const char *filename;
612 int hotspot_x, hotspot_y;
613} pointer_images[] = {
614 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
615 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
616 { DATADIR "/wayland/bottom_side.png", 16, 20 },
617 { DATADIR "/wayland/grabbing.png", 20, 17 },
618 { DATADIR "/wayland/left_ptr.png", 10, 5 },
619 { DATADIR "/wayland/left_side.png", 10, 20 },
620 { DATADIR "/wayland/right_side.png", 30, 19 },
621 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
622 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
623 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400624 { DATADIR "/wayland/xterm.png", 15, 15 },
625 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400626};
627
628static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400629create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400630{
631 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400632 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400633 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400634
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400636 display->pointer_surfaces =
637 malloc(count * sizeof *display->pointer_surfaces);
638 rect.width = width;
639 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400640 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400641 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642 display_create_surface_from_file(display,
643 pointer_images[i].filename,
644 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400645 }
646
647}
648
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400649cairo_surface_t *
650display_get_pointer_surface(struct display *display, int pointer,
651 int *width, int *height,
652 int *hotspot_x, int *hotspot_y)
653{
654 cairo_surface_t *surface;
655
656 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500657#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400658 *width = cairo_gl_surface_get_width(surface);
659 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000660#else
661 *width = cairo_image_surface_get_width(surface);
662 *height = cairo_image_surface_get_height(surface);
663#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400664 *hotspot_x = pointer_images[pointer].hotspot_x;
665 *hotspot_y = pointer_images[pointer].hotspot_y;
666
667 return cairo_surface_reference(surface);
668}
669
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400670
671static void
672window_attach_surface(struct window *window);
673
674static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400675free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400676{
677 struct window *window = data;
678
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400679 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400680 cairo_surface_destroy(window->pending_surface);
681 window->pending_surface = NULL;
682 if (window->cairo_surface)
683 window_attach_surface(window);
684}
685
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400686static const struct wl_callback_listener free_surface_listener = {
687 free_surface
688};
689
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500690static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200691window_get_resize_dx_dy(struct window *window, int *x, int *y)
692{
693 if (window->resize_edges & WINDOW_RESIZING_LEFT)
694 *x = window->server_allocation.width - window->allocation.width;
695 else
696 *x = 0;
697
698 if (window->resize_edges & WINDOW_RESIZING_TOP)
699 *y = window->server_allocation.height -
700 window->allocation.height;
701 else
702 *y = 0;
703
704 window->resize_edges = 0;
705}
706
707static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500708window_attach_surface(struct window *window)
709{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400710 struct display *display = window->display;
711 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400712 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000713#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000715#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500716 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717
718 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000719#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
721 data = cairo_surface_get_user_data(window->cairo_surface,
722 &surface_data_key);
723
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724 cairo_gl_surface_swapbuffers(window->cairo_surface);
725 wl_egl_window_get_attached_size(data->window,
726 &window->server_allocation.width,
727 &window->server_allocation.height);
728 break;
729 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000730#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200732 window_get_resize_dx_dy(window, &x, &y);
733
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734 if (window->pending_surface != NULL)
735 return;
736
737 window->pending_surface = window->cairo_surface;
738 window->cairo_surface = NULL;
739
740 buffer =
741 display_get_buffer_for_surface(display,
742 window->pending_surface);
743
744 wl_surface_attach(window->surface, buffer, x, y);
745 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400746 cb = wl_display_sync(display->display);
747 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100748 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000749 default:
750 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100751 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500752
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400753 switch (window->type) {
754 case TYPE_FULLSCREEN:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400755 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400756 break;
757 case TYPE_TOPLEVEL:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400758 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400759 break;
760 case TYPE_TRANSIENT:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400761 wl_shell_set_transient(display->shell, window->surface,
762 window->parent->surface,
763 window->x, window->y, 0);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400764 break;
765 case TYPE_CUSTOM:
766 break;
767 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500768
769 wl_surface_damage(window->surface, 0, 0,
770 window->allocation.width,
771 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500772}
773
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500774void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400775window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500776{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100777 if (window->cairo_surface) {
778 switch (window->buffer_type) {
779 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
780 case WINDOW_BUFFER_TYPE_SHM:
781 display_surface_damage(window->display,
782 window->cairo_surface,
783 0, 0,
784 window->allocation.width,
785 window->allocation.height);
786 break;
787 default:
788 break;
789 }
790 window_attach_surface(window);
791 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500792}
793
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400794void
795window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500797 cairo_surface_reference(surface);
798
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400799 if (window->cairo_surface != NULL)
800 cairo_surface_destroy(window->cairo_surface);
801
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500802 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400803}
804
Benjamin Franzke22d54812011-07-16 19:50:32 +0000805#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100806static void
807window_resize_cairo_window_surface(struct window *window)
808{
809 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200810 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811
812 data = cairo_surface_get_user_data(window->cairo_surface,
813 &surface_data_key);
814
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200815 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 wl_egl_window_resize(data->window,
817 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200818 window->allocation.height,
819 x,y);
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 cairo_gl_surface_set_size(window->cairo_surface,
822 window->allocation.width,
823 window->allocation.height);
824}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400827void
828window_create_surface(struct window *window)
829{
830 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400831 uint32_t flags = 0;
832
833 if (!window->transparent)
834 flags = SURFACE_OPAQUE;
835
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400836 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500837#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
839 if (window->cairo_surface) {
840 window_resize_cairo_window_surface(window);
841 return;
842 }
843 surface = display_create_surface(window->display,
844 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500847 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400848 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400850 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400851 break;
852#endif
853 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400854 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400855 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800857 default:
858 surface = NULL;
859 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400860 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400861
862 window_set_surface(window, surface);
863 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400864}
865
866static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500867window_draw_menu(struct window *window)
868{
869 cairo_t *cr;
870 int width, height, r = 5;
871
872 window_create_surface(window);
873
874 cr = cairo_create(window->cairo_surface);
875 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
876 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
877 cairo_paint(cr);
878
879 width = window->allocation.width;
880 height = window->allocation.height;
881 rounded_rect(cr, r, r, width - r, height - r, r);
882 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
883 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
884 cairo_fill(cr);
885 cairo_destroy(cr);
886}
887
888static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500889window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500890{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500891 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500892 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400893 cairo_surface_t *frame;
894 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500895
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400897
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400898 width = window->allocation.width;
899 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500900
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500901 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500902
Kristian Høgsberg09531622010-06-14 23:22:15 -0400903 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400904 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400905 cairo_paint(cr);
906
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400907 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400908 tile_mask(cr, window->display->shadow,
909 shadow_dx, shadow_dy, width, height,
910 window->margin + 10 - shadow_dx,
911 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500912
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400913 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400914 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400915 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400916 frame = window->display->inactive_frame;
917
918 tile_source(cr, frame, 0, 0, width, height,
919 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500920
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500921 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
922 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500923 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400924 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400925 if (window->keyboard_device)
926 cairo_set_source_rgb(cr, 0, 0, 0);
927 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400928 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 cairo_show_text(cr, window->title);
930
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500931 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400932
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100933 /* FIXME: this breakes gears, fix cairo? */
934#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400935 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100936#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500937}
938
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400939void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500940window_destroy(struct window *window)
941{
942 wl_surface_destroy(window->surface);
943 wl_list_remove(&window->link);
944 free(window);
945}
946
947void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400948display_flush_cairo_device(struct display *display)
949{
950 cairo_device_flush (display->device);
951}
952
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500953void
954window_draw(struct window *window)
955{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500956 if (window->parent)
957 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400958 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400959 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500960 else
961 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500962}
963
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400964cairo_surface_t *
965window_get_surface(struct window *window)
966{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400967 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400968}
969
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100970struct wl_surface *
971window_get_wl_surface(struct window *window)
972{
973 return window->surface;
974}
975
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400976static int
977get_pointer_location(struct window *window, int32_t x, int32_t y)
978{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400979 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400980 const int grip_size = 8;
981
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500982 if (!window->decoration)
983 return WINDOW_CLIENT_AREA;
984
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400985 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400986 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400987 else if (window->margin <= x && x < window->margin + grip_size)
988 hlocation = WINDOW_RESIZING_LEFT;
989 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400990 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400991 else if (x < window->allocation.width - window->margin)
992 hlocation = WINDOW_RESIZING_RIGHT;
993 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400994 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400995
996 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400997 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400998 else if (window->margin <= y && y < window->margin + grip_size)
999 vlocation = WINDOW_RESIZING_TOP;
1000 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001001 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001002 else if (y < window->allocation.height - window->margin)
1003 vlocation = WINDOW_RESIZING_BOTTOM;
1004 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001005 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001006
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001007 location = vlocation | hlocation;
1008 if (location & WINDOW_EXTERIOR)
1009 location = WINDOW_EXTERIOR;
1010 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1011 location = WINDOW_TITLEBAR;
1012 else if (location == WINDOW_INTERIOR)
1013 location = WINDOW_CLIENT_AREA;
1014
1015 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001016}
1017
1018static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001019set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001020{
1021 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001022 struct wl_buffer *buffer;
1023 cairo_surface_t *surface;
1024 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001025
1026 location = get_pointer_location(input->pointer_focus,
1027 input->sx, input->sy);
1028 switch (location) {
1029 case WINDOW_RESIZING_TOP:
1030 pointer = POINTER_TOP;
1031 break;
1032 case WINDOW_RESIZING_BOTTOM:
1033 pointer = POINTER_BOTTOM;
1034 break;
1035 case WINDOW_RESIZING_LEFT:
1036 pointer = POINTER_LEFT;
1037 break;
1038 case WINDOW_RESIZING_RIGHT:
1039 pointer = POINTER_RIGHT;
1040 break;
1041 case WINDOW_RESIZING_TOP_LEFT:
1042 pointer = POINTER_TOP_LEFT;
1043 break;
1044 case WINDOW_RESIZING_TOP_RIGHT:
1045 pointer = POINTER_TOP_RIGHT;
1046 break;
1047 case WINDOW_RESIZING_BOTTOM_LEFT:
1048 pointer = POINTER_BOTTOM_LEFT;
1049 break;
1050 case WINDOW_RESIZING_BOTTOM_RIGHT:
1051 pointer = POINTER_BOTTOM_RIGHT;
1052 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001053 case WINDOW_EXTERIOR:
1054 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001055 if (input->current_pointer_image == POINTER_DEFAULT)
1056 return;
1057
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001058 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001059 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001060 return;
1061 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001062 break;
1063 }
1064
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001065 if (pointer == input->current_pointer_image)
1066 return;
1067
1068 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001069 surface = display->pointer_surfaces[pointer];
1070 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001071 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001072 pointer_images[pointer].hotspot_x,
1073 pointer_images[pointer].hotspot_y);
1074}
1075
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001076static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001077window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001078 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001079 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001080{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001081 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001082 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001083 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001084
1085 input->x = x;
1086 input->y = y;
1087 input->sx = sx;
1088 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001089
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001090 if (window->motion_handler)
1091 pointer = (*window->motion_handler)(window, input, time,
1092 x, y, sx, sy,
1093 window->user_data);
1094
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001095 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001096}
1097
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001098static void
1099window_handle_button(void *data,
1100 struct wl_input_device *input_device,
1101 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001102{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001103 struct input *input = data;
1104 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001105 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001106
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001108
1109 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001110 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001112 wl_shell_move(window->display->shell,
1113 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001114 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001115 case WINDOW_RESIZING_TOP:
1116 case WINDOW_RESIZING_BOTTOM:
1117 case WINDOW_RESIZING_LEFT:
1118 case WINDOW_RESIZING_RIGHT:
1119 case WINDOW_RESIZING_TOP_LEFT:
1120 case WINDOW_RESIZING_TOP_RIGHT:
1121 case WINDOW_RESIZING_BOTTOM_LEFT:
1122 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001123 wl_shell_resize(window->display->shell,
1124 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001125 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001126 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 case WINDOW_CLIENT_AREA:
1128 if (window->button_handler)
1129 (*window->button_handler)(window,
1130 input, time,
1131 button, state,
1132 window->user_data);
1133 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001134 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135 } else {
1136 if (window->button_handler)
1137 (*window->button_handler)(window,
1138 input, time,
1139 button, state,
1140 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001141 }
1142}
1143
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001144static void
1145window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001146 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001147{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001148 struct input *input = data;
1149 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001150 struct display *d = window->display;
1151 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001152
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001153 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001154 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001155 return;
1156
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001157 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001158 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001159 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1160 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001161
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001162 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1163
1164 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001165 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001166 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001167 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001168
1169 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001170 (*window->key_handler)(window, input, time, key, sym, state,
1171 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001172}
1173
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001174static void
1175window_handle_pointer_focus(void *data,
1176 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001177 uint32_t time, struct wl_surface *surface,
1178 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001179{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001180 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001181 struct window *window;
1182 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001183
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001184 window = input->pointer_focus;
1185 if (window && window->surface != surface) {
1186 if (window->leave_handler)
1187 window->leave_handler(window, input,
1188 time, window->user_data);
1189 input->pointer_focus = NULL;
1190 input->current_pointer_image = POINTER_UNSET;
1191 }
1192
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001194 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195 window = input->pointer_focus;
1196
Kristian Høgsberg59826582011-01-20 11:56:57 -05001197 input->x = x;
1198 input->y = y;
1199 input->sx = sx;
1200 input->sy = sy;
1201
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001202 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001203 if (window->enter_handler)
1204 pointer = window->enter_handler(window, input,
1205 time, sx, sy,
1206 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001207
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001208 set_pointer_image(input, time, pointer);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001209
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001210 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001211}
1212
1213static void
1214window_handle_keyboard_focus(void *data,
1215 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001216 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001217 struct wl_surface *surface,
1218 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001219{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001220 struct input *input = data;
1221 struct window *window = input->keyboard_focus;
1222 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001223 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001224
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001225 window = input->keyboard_focus;
1226 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001227 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001228 if (window->keyboard_focus_handler)
1229 (*window->keyboard_focus_handler)(window, NULL,
1230 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001231 }
1232
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001233 if (surface)
1234 input->keyboard_focus = wl_surface_get_user_data(surface);
1235 else
1236 input->keyboard_focus = NULL;
1237
1238 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001239 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001240 for (k = keys->data; k < end; k++)
1241 input->modifiers |= d->xkb->map->modmap[*k];
1242
1243 window = input->keyboard_focus;
1244 if (window) {
1245 window->keyboard_device = input;
1246 if (window->keyboard_focus_handler)
1247 (*window->keyboard_focus_handler)(window,
1248 window->keyboard_device,
1249 window->user_data);
1250 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001251}
1252
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001253static const struct wl_input_device_listener input_device_listener = {
1254 window_handle_motion,
1255 window_handle_button,
1256 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001257 window_handle_pointer_focus,
1258 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001259};
1260
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261void
1262input_get_position(struct input *input, int32_t *x, int32_t *y)
1263{
1264 *x = input->sx;
1265 *y = input->sy;
1266}
1267
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001268struct wl_input_device *
1269input_get_input_device(struct input *input)
1270{
1271 return input->input_device;
1272}
1273
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001274uint32_t
1275input_get_modifiers(struct input *input)
1276{
1277 return input->modifiers;
1278}
1279
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001280struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001281window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001282{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001283 cairo_device_flush (window->display->device);
1284
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001285 return wl_shell_create_drag(window->display->shell);
1286}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001287
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001288void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001289window_move(struct window *window, struct input *input, uint32_t time)
1290{
1291 wl_shell_move(window->display->shell,
1292 window->surface, input->input_device, time);
1293}
1294
1295void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001296window_activate_drag(struct wl_drag *drag, struct window *window,
1297 struct input *input, uint32_t time)
1298{
1299 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001300}
1301
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001302static void
1303handle_configure(void *data, struct wl_shell *shell,
1304 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001305 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001306{
1307 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001308 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001309
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001310 /* FIXME: this is probably the wrong place to check for width
1311 * or height <= 0, but it prevents the compositor from crashing
1312 */
1313 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001314 return;
1315
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001316 window->resize_edges = edges;
1317
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001318 if (window->resize_handler) {
1319 child_width = width - 20 - window->margin * 2;
1320 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001321
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001322 (*window->resize_handler)(window,
1323 child_width, child_height,
1324 window->user_data);
1325 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001326 window->allocation.width = width;
1327 window->allocation.height = height;
1328
1329 if (window->redraw_handler)
1330 window_schedule_redraw(window);
1331 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001332}
1333
1334static const struct wl_shell_listener shell_listener = {
1335 handle_configure,
1336};
1337
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001338void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001339window_get_allocation(struct window *window,
1340 struct rectangle *allocation)
1341{
1342 *allocation = window->allocation;
1343}
1344
1345void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001346window_get_child_allocation(struct window *window,
1347 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001348{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001349 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001350 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001351 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001352 allocation->x = window->margin + 10;
1353 allocation->y = window->margin + 50;
1354 allocation->width =
1355 window->allocation.width - 20 - window->margin * 2;
1356 allocation->height =
1357 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001358 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001359}
1360
1361void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001362window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001363{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001364 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001365 window->allocation.x = 20 + window->margin;
1366 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001367 window->allocation.width = width + 20 + window->margin * 2;
1368 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001369 } else {
1370 window->allocation.x = 0;
1371 window->allocation.y = 0;
1372 window->allocation.width = width;
1373 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001374 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001375}
1376
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001377static gboolean
1378idle_redraw(void *data)
1379{
1380 struct window *window = data;
1381
1382 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001383
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001384 window->redraw_scheduled = 0;
1385
1386 return FALSE;
1387}
1388
1389void
1390window_schedule_redraw(struct window *window)
1391{
1392 if (!window->redraw_scheduled) {
1393 g_idle_add(idle_redraw, window);
1394 window->redraw_scheduled = 1;
1395 }
1396}
1397
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001398void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001399window_set_custom(struct window *window)
1400{
1401 window->type = TYPE_CUSTOM;
1402}
1403
1404void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001405window_set_fullscreen(struct window *window, int fullscreen)
1406{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001407 int32_t width, height;
1408
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001409 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001410 return;
1411
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001412 if (fullscreen) {
1413 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001414 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001415 width = window->display->screen_allocation.width;
1416 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001417 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001418 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001419 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001420 width = window->saved_allocation.width - 20 - window->margin * 2;
1421 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001422 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001423 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001424
1425 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001426}
1427
1428void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001429window_set_decoration(struct window *window, int decoration)
1430{
1431 window->decoration = decoration;
1432}
1433
1434void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001435window_set_user_data(struct window *window, void *data)
1436{
1437 window->user_data = data;
1438}
1439
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001440void *
1441window_get_user_data(struct window *window)
1442{
1443 return window->user_data;
1444}
1445
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001446void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001447window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001448 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001449{
1450 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001451}
1452
1453void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001454window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001455 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001456{
1457 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001458}
1459
1460void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001461window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001462 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001463{
1464 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001465}
1466
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001467void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001468window_set_button_handler(struct window *window,
1469 window_button_handler_t handler)
1470{
1471 window->button_handler = handler;
1472}
1473
1474void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001475window_set_motion_handler(struct window *window,
1476 window_motion_handler_t handler)
1477{
1478 window->motion_handler = handler;
1479}
1480
1481void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001482window_set_enter_handler(struct window *window,
1483 window_enter_handler_t handler)
1484{
1485 window->enter_handler = handler;
1486}
1487
1488void
1489window_set_leave_handler(struct window *window,
1490 window_leave_handler_t handler)
1491{
1492 window->leave_handler = handler;
1493}
1494
1495void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001496window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001497 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001498{
1499 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001500}
1501
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001502void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001503window_set_transparent(struct window *window, int transparent)
1504{
1505 window->transparent = transparent;
1506}
1507
1508void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001509window_set_title(struct window *window, const char *title)
1510{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001511 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001512 window->title = strdup(title);
1513}
1514
1515const char *
1516window_get_title(struct window *window)
1517{
1518 return window->title;
1519}
1520
1521void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001522display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1523 int32_t x, int32_t y, int32_t width, int32_t height)
1524{
1525 struct wl_buffer *buffer;
1526
1527 buffer = display_get_buffer_for_surface(display, cairo_surface);
1528
1529 wl_buffer_damage(buffer, x, y, width, height);
1530}
1531
1532void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001533window_damage(struct window *window, int32_t x, int32_t y,
1534 int32_t width, int32_t height)
1535{
1536 wl_surface_damage(window->surface, x, y, width, height);
1537}
1538
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001539static struct window *
1540window_create_internal(struct display *display, struct window *parent,
1541 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001542{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001543 struct window *window;
1544
1545 window = malloc(sizeof *window);
1546 if (window == NULL)
1547 return NULL;
1548
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001549 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001550 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001551 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001552 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001553 window->allocation.x = 0;
1554 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001555 window->allocation.width = width;
1556 window->allocation.height = height;
1557 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001558 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001559 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001560 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001561
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001562 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001563#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001564 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1565 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001566#else
1567 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1568#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001569 else
1570 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001571
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001572 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001573 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001574
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001575 return window;
1576}
1577
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001578struct window *
1579window_create(struct display *display, int32_t width, int32_t height)
1580{
1581 struct window *window;
1582
1583 window = window_create_internal(display, NULL, width, height);
1584 if (!window)
1585 return NULL;
1586
1587 return window;
1588}
1589
1590struct window *
1591window_create_transient(struct display *display, struct window *parent,
1592 int32_t x, int32_t y, int32_t width, int32_t height)
1593{
1594 struct window *window;
1595
1596 window = window_create_internal(parent->display,
1597 parent, width, height);
1598 if (!window)
1599 return NULL;
1600
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001601 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001602 window->x = x;
1603 window->y = y;
1604
1605 return window;
1606}
1607
1608void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001609window_set_buffer_type(struct window *window, enum window_buffer_type type)
1610{
1611 window->buffer_type = type;
1612}
1613
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001614
1615static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001616display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001617 struct wl_output *wl_output,
1618 int x, int y,
1619 int physical_width,
1620 int physical_height,
1621 int subpixel,
1622 const char *make,
1623 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001624{
1625 struct display *display = data;
1626
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001627 display->screen_allocation.x = x;
1628 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001629}
1630
1631static void
1632display_handle_mode(void *data,
1633 struct wl_output *wl_output,
1634 uint32_t flags,
1635 int width,
1636 int height,
1637 int refresh)
1638{
1639 struct display *display = data;
1640
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001641 display->screen_allocation.width = width;
1642 display->screen_allocation.height = height;
1643}
1644
1645static const struct wl_output_listener output_listener = {
1646 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001647 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001648};
1649
1650static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001651display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001652{
1653 struct input *input;
1654
1655 input = malloc(sizeof *input);
1656 if (input == NULL)
1657 return;
1658
1659 memset(input, 0, sizeof *input);
1660 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001661 input->input_device =
1662 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001663 input->pointer_focus = NULL;
1664 input->keyboard_focus = NULL;
1665 wl_list_insert(d->input_list.prev, &input->link);
1666
1667 wl_input_device_add_listener(input->input_device,
1668 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001669 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001670}
1671
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001672struct selection_offer {
1673 struct display *display;
1674 struct wl_selection_offer *offer;
1675 struct wl_array types;
1676 struct input *input;
1677};
1678
1679int
1680input_offers_mime_type(struct input *input, const char *type)
1681{
1682 struct selection_offer *offer = input->offer;
1683 char **p, **end;
1684
1685 if (offer == NULL)
1686 return 0;
1687
1688 end = offer->types.data + offer->types.size;
1689 for (p = offer->types.data; p < end; p++)
1690 if (strcmp(*p, type) == 0)
1691 return 1;
1692
1693 return 0;
1694}
1695
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001696void
1697input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001698{
1699 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001700
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001701 /* FIXME: A number of things can go wrong here: the object may
1702 * not be the current selection offer any more (which could
1703 * still work, but the source may have gone away or just
1704 * destroyed its wl_selection) or the offer may not have the
1705 * requested type after all (programmer/client error,
1706 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001707 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001708}
1709
1710static void
1711selection_offer_offer(void *data,
1712 struct wl_selection_offer *selection_offer,
1713 const char *type)
1714{
1715 struct selection_offer *offer = data;
1716
1717 char **p;
1718
1719 p = wl_array_add(&offer->types, sizeof *p);
1720 if (p)
1721 *p = strdup(type);
1722};
1723
1724static void
1725selection_offer_keyboard_focus(void *data,
1726 struct wl_selection_offer *selection_offer,
1727 struct wl_input_device *input_device)
1728{
1729 struct selection_offer *offer = data;
1730 struct input *input;
1731 char **p, **end;
1732
1733 if (input_device == NULL) {
1734 printf("selection offer retracted %p\n", selection_offer);
1735 input = offer->input;
1736 input->offer = NULL;
1737 wl_selection_offer_destroy(selection_offer);
1738 wl_array_release(&offer->types);
1739 free(offer);
1740 return;
1741 }
1742
1743 input = wl_input_device_get_user_data(input_device);
1744 printf("new selection offer %p:", selection_offer);
1745
1746 offer->input = input;
1747 input->offer = offer;
1748 end = offer->types.data + offer->types.size;
1749 for (p = offer->types.data; p < end; p++)
1750 printf(" %s", *p);
1751
1752 printf("\n");
1753}
1754
1755struct wl_selection_offer_listener selection_offer_listener = {
1756 selection_offer_offer,
1757 selection_offer_keyboard_focus
1758};
1759
1760static void
1761add_selection_offer(struct display *d, uint32_t id)
1762{
1763 struct selection_offer *offer;
1764
1765 offer = malloc(sizeof *offer);
1766 if (offer == NULL)
1767 return;
1768
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001769 offer->offer =
1770 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001771 offer->display = d;
1772 wl_array_init(&offer->types);
1773 offer->input = NULL;
1774
1775 wl_selection_offer_add_listener(offer->offer,
1776 &selection_offer_listener, offer);
1777}
1778
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001779static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001780display_handle_global(struct wl_display *display, uint32_t id,
1781 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001782{
1783 struct display *d = data;
1784
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001785 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001786 d->compositor =
1787 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001788 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001789 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001790 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001791 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001792 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001793 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001794 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001795 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001796 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001797 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001798 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001799 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001800 }
1801}
1802
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001803static void
1804display_render_frame(struct display *d)
1805{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001806 int radius = 8;
1807 cairo_t *cr;
1808
1809 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1810 cr = cairo_create(d->shadow);
1811 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1812 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1813 rounded_rect(cr, 16, 16, 112, 112, radius);
1814 cairo_fill(cr);
1815 cairo_destroy(cr);
1816 blur_surface(d->shadow, 64);
1817
1818 d->active_frame =
1819 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1820 cr = cairo_create(d->active_frame);
1821 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1822 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1823 rounded_rect(cr, 16, 16, 112, 112, radius);
1824 cairo_fill(cr);
1825 cairo_destroy(cr);
1826
1827 d->inactive_frame =
1828 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1829 cr = cairo_create(d->inactive_frame);
1830 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1831 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1832 rounded_rect(cr, 16, 16, 112, 112, radius);
1833 cairo_fill(cr);
1834 cairo_destroy(cr);
1835}
1836
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001837static void
1838init_xkb(struct display *d)
1839{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001840 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001841
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001842 names.rules = "evdev";
1843 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001844 names.layout = option_xkb_layout;
1845 names.variant = option_xkb_variant;
1846 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001847
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001848 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001849 if (!d->xkb) {
1850 fprintf(stderr, "Failed to compile keymap\n");
1851 exit(1);
1852 }
1853}
1854
Yuval Fledel45568f62010-12-06 09:18:12 -05001855static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001856init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001857{
1858 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001859 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001860
1861 static const EGLint premul_argb_cfg_attribs[] = {
1862 EGL_SURFACE_TYPE,
1863 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1864 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001865 EGL_RED_SIZE, 1,
1866 EGL_GREEN_SIZE, 1,
1867 EGL_BLUE_SIZE, 1,
1868 EGL_ALPHA_SIZE, 1,
1869 EGL_DEPTH_SIZE, 1,
1870 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1871 EGL_NONE
1872 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001873
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001874 static const EGLint rgb_cfg_attribs[] = {
1875 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1876 EGL_RED_SIZE, 1,
1877 EGL_GREEN_SIZE, 1,
1878 EGL_BLUE_SIZE, 1,
1879 EGL_ALPHA_SIZE, 0,
1880 EGL_DEPTH_SIZE, 1,
1881 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1882 EGL_NONE
1883 };
1884
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001885 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001886 if (!eglInitialize(d->dpy, &major, &minor)) {
1887 fprintf(stderr, "failed to initialize display\n");
1888 return -1;
1889 }
1890
1891 if (!eglBindAPI(EGL_OPENGL_API)) {
1892 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1893 return -1;
1894 }
1895
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001896 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02001897 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001898 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001899 return -1;
1900 }
1901
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001902 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
1903 &d->rgb_config, 1, &n) || n != 1) {
1904 fprintf(stderr, "failed to choose rgb config\n");
1905 return -1;
1906 }
1907
1908 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001909 if (d->ctx == NULL) {
1910 fprintf(stderr, "failed to create context\n");
1911 return -1;
1912 }
1913
1914 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001915 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001916 return -1;
1917 }
1918
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001919#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001920 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001921 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001922 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001923 return -1;
1924 }
1925#endif
1926
1927 return 0;
1928}
1929
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001930struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04001931display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001932{
1933 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001934 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001935 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001936 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001937
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001938 g_type_init();
1939
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001940 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001941 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001942 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001943
1944 xkb_option_group = g_option_group_new("xkb",
1945 "Keyboard options",
1946 "Show all XKB options",
1947 NULL, NULL);
1948 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1949 g_option_context_add_group (context, xkb_option_group);
1950
1951 if (!g_option_context_parse(context, argc, argv, &error)) {
1952 fprintf(stderr, "option parsing failed: %s\n", error->message);
1953 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001954 }
1955
Tim Wiederhake748f6722011-01-23 23:25:25 +01001956 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001957
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001958 d = malloc(sizeof *d);
1959 if (d == NULL)
1960 return NULL;
1961
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001962 memset(d, 0, sizeof *d);
1963
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001964 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001965 if (d->display == NULL) {
1966 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001967 return NULL;
1968 }
1969
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001970 wl_list_init(&d->input_list);
1971
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001972 /* Set up listener so we'll catch all events. */
1973 wl_display_add_global_listener(d->display,
1974 display_handle_global, d);
1975
1976 /* Process connection events. */
1977 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001978 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001979 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001980
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001981 d->image_target_texture_2d =
1982 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1983 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1984 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1985
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001986 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001987
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001988 display_render_frame(d);
1989
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001990 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001991 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001992 g_source_attach(d->source, NULL);
1993
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001994 wl_list_init(&d->window_list);
1995
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001996 init_xkb(d);
1997
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001998 return d;
1999}
2000
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002001struct wl_display *
2002display_get_display(struct display *display)
2003{
2004 return display->display;
2005}
2006
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002007struct wl_compositor *
2008display_get_compositor(struct display *display)
2009{
2010 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002011}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002012
2013EGLDisplay
2014display_get_egl_display(struct display *d)
2015{
2016 return d->dpy;
2017}
2018
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002019EGLConfig
2020display_get_egl_config(struct display *d)
2021{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002022 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002023}
2024
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002025struct wl_shell *
2026display_get_shell(struct display *display)
2027{
2028 return display->shell;
2029}
2030
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002031void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002032display_acquire_window_surface(struct display *display,
2033 struct window *window,
2034 EGLContext ctx)
2035{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002036#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002037 struct egl_window_surface_data *data;
2038
2039 if (!window->cairo_surface)
2040 return;
2041
2042 if (!ctx)
2043 ctx = display->ctx;
2044
2045 data = cairo_surface_get_user_data(window->cairo_surface,
2046 &surface_data_key);
2047
2048 cairo_device_acquire(display->device);
2049 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2050 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002051#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002052}
2053
2054void
2055display_release(struct display *display)
2056{
2057 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2058 fprintf(stderr, "failed to make context current\n");
2059 cairo_device_release(display->device);
2060}
2061
2062void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002063display_run(struct display *d)
2064{
2065 g_main_loop_run(d->loop);
2066}