blob: e7a61ef9aaab598d3fd395f7264e43fcf4238c16 [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øgsbergda275dd2010-08-16 17:47:07 -040035#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040036#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040037#include <sys/epoll.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øgsbergb91cd102010-08-16 16:17:42 -040054#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050057
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050058struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050059 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050060 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040061 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040062 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050063 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040065 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040066 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020067 EGLConfig premultiplied_argb_config;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010069 cairo_device_t *device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040070
71 int display_fd;
72 uint32_t mask;
73 struct task display_task;
74
75 int epoll_fd;
76 struct wl_list deferred_list;
77
Kristian Høgsberg478d9262010-06-08 20:34:11 -040078 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040079 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040080 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040081 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040082 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040083 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040084
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050085 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
86 PFNEGLCREATEIMAGEKHRPROC create_image;
87 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050088};
89
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040090enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -040092 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093 TYPE_TRANSIENT,
94 TYPE_CUSTOM
95};
96
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097struct window {
98 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050099 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500101 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500102 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500103 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400104 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400105 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400106 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500107 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500108 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400109 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400111 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct input *grab_device;
113 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500114 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400115 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500116
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400117 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500118 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500119
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400121 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500122 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400123 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500124 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400125 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400126 window_enter_handler_t enter_handler;
127 window_leave_handler_t leave_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400128
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500129 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400130 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500131};
132
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400133struct input {
134 struct display *display;
135 struct wl_input_device *input_device;
136 struct window *pointer_focus;
137 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500138 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400139 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400140 uint32_t modifiers;
141 int32_t x, y, sx, sy;
142 struct wl_list link;
143};
144
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400145enum {
146 POINTER_DEFAULT = 100,
147 POINTER_UNSET
148};
149
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500150enum window_location {
151 WINDOW_INTERIOR = 0,
152 WINDOW_RESIZING_TOP = 1,
153 WINDOW_RESIZING_BOTTOM = 2,
154 WINDOW_RESIZING_LEFT = 4,
155 WINDOW_RESIZING_TOP_LEFT = 5,
156 WINDOW_RESIZING_BOTTOM_LEFT = 6,
157 WINDOW_RESIZING_RIGHT = 8,
158 WINDOW_RESIZING_TOP_RIGHT = 9,
159 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
160 WINDOW_RESIZING_MASK = 15,
161 WINDOW_EXTERIOR = 16,
162 WINDOW_TITLEBAR = 17,
163 WINDOW_CLIENT_AREA = 18,
164};
165
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400166const char *option_xkb_layout = "us";
167const char *option_xkb_variant = "";
168const char *option_xkb_options = "";
169
170static const GOptionEntry xkb_option_entries[] = {
171 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
172 &option_xkb_layout, "XKB Layout" },
173 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
174 &option_xkb_variant, "XKB Variant" },
175 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
176 &option_xkb_options, "XKB Options" },
177 { NULL }
178};
179
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400180static const cairo_user_data_key_t surface_data_key;
181struct surface_data {
182 struct wl_buffer *buffer;
183};
184
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500185#define MULT(_d,c,a,t) \
186 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
187
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500188#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400189
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100190struct egl_window_surface_data {
191 struct display *display;
192 struct wl_surface *surface;
193 struct wl_egl_window *window;
194 EGLSurface surf;
195};
196
197static void
198egl_window_surface_data_destroy(void *p)
199{
200 struct egl_window_surface_data *data = p;
201 struct display *d = data->display;
202
203 eglDestroySurface(d->dpy, data->surf);
204 wl_egl_window_destroy(data->window);
205 data->surface = NULL;
206
207 free(p);
208}
209
210static cairo_surface_t *
211display_create_egl_window_surface(struct display *display,
212 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400213 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100214 struct rectangle *rectangle)
215{
216 cairo_surface_t *cairo_surface;
217 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400218 EGLConfig config;
219 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100220
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400221 static const EGLint premul_attribs[] = {
222 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
223 EGL_NONE
224 };
225
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100226 data = malloc(sizeof *data);
227 if (data == NULL)
228 return NULL;
229
230 data->display = display;
231 data->surface = surface;
232
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400233 if (flags & SURFACE_OPAQUE) {
234 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200235 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400236 } else {
237 config = display->premultiplied_argb_config;
238 attribs = premul_attribs;
239 }
240
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400241 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100242 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400243 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100244
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400245 data->surf = eglCreateWindowSurface(display->dpy, config,
246 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100247
248 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
249 data->surf,
250 rectangle->width,
251 rectangle->height);
252
253 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
254 data, egl_window_surface_data_destroy);
255
256 return cairo_surface;
257}
258
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500259struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400260 struct surface_data data;
261 EGLImageKHR image;
262 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800263 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500264 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400265};
266
267static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500268egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400269{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500270 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800271 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800273 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400274 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800275 cairo_device_release(d->device);
276
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500277 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400278 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500279 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500280 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400281}
282
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500283EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500284display_get_image_for_egl_image_surface(struct display *display,
285 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500286{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500287 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500288
289 data = cairo_surface_get_user_data (surface, &surface_data_key);
290
291 return data->image;
292}
293
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500294static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500295display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500297 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400298{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500299 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400300 EGLDisplay dpy = display->dpy;
301 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400302 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400303
304 data = malloc(sizeof *data);
305 if (data == NULL)
306 return NULL;
307
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800308 data->display = display;
309
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400310 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400311 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000313 free(data);
314 return NULL;
315 }
316
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400317 if (flags & SURFACE_OPAQUE)
318 config = display->rgb_config;
319 else
320 config = display->premultiplied_argb_config;
321
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500322 data->image = display->create_image(dpy, NULL,
323 EGL_NATIVE_PIXMAP_KHR,
324 (EGLClientBuffer) data->pixmap,
325 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500326 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500327 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500328 free(data);
329 return NULL;
330 }
331
332 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400333 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800335 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500338 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800339 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400341 surface = cairo_gl_surface_create_for_texture(display->device,
342 CAIRO_CONTENT_COLOR_ALPHA,
343 data->texture,
344 rectangle->width,
345 rectangle->height);
346
347 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349
350 return surface;
351}
352
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500353static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354display_create_egl_image_surface_from_file(struct display *display,
355 const char *filename,
356 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357{
358 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400359 GdkPixbuf *pixbuf;
360 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400361 int stride, i;
362 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400364
365 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400366 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400367 FALSE, &error);
368 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400370
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400371 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
372 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500373 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400374 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400375 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400376
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400377
378 stride = gdk_pixbuf_get_rowstride(pixbuf);
379 pixels = gdk_pixbuf_get_pixels(pixbuf);
380
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400381 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400382 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400383 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400384 while (p < end) {
385 unsigned int t;
386
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400387 MULT(p[0], p[0], p[3], t);
388 MULT(p[1], p[1], p[3], t);
389 MULT(p[2], p[2], p[3], t);
390 p += 4;
391
392 }
393 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400394
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200395 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000396 if (surface == NULL) {
397 g_object_unref(pixbuf);
398 return NULL;
399 }
400
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800401 data = cairo_surface_get_user_data(surface, &surface_data_key);
402
403 cairo_device_acquire(display->device);
404 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800405 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
406 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800407 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400408
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500409 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400410
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411 return surface;
412}
413
414#endif
415
416struct wl_buffer *
417display_get_buffer_for_surface(struct display *display,
418 cairo_surface_t *surface)
419{
420 struct surface_data *data;
421
422 data = cairo_surface_get_user_data (surface, &surface_data_key);
423
424 return data->buffer;
425}
426
427struct shm_surface_data {
428 struct surface_data data;
429 void *map;
430 size_t length;
431};
432
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500433static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434shm_surface_data_destroy(void *p)
435{
436 struct shm_surface_data *data = p;
437
438 wl_buffer_destroy(data->data.buffer);
439 munmap(data->map, data->length);
440}
441
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500442static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400444 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445{
446 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400447 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400448 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800449 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400450 char filename[] = "/tmp/wayland-shm-XXXXXX";
451
452 data = malloc(sizeof *data);
453 if (data == NULL)
454 return NULL;
455
456 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
457 rectangle->width);
458 data->length = stride * rectangle->height;
459 fd = mkstemp(filename);
460 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000461 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400462 return NULL;
463 }
464 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000465 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466 close(fd);
467 return NULL;
468 }
469
470 data->map = mmap(NULL, data->length,
471 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
472 unlink(filename);
473
474 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000475 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400476 close(fd);
477 return NULL;
478 }
479
480 surface = cairo_image_surface_create_for_data (data->map,
481 CAIRO_FORMAT_ARGB32,
482 rectangle->width,
483 rectangle->height,
484 stride);
485
486 cairo_surface_set_user_data (surface, &surface_data_key,
487 data, shm_surface_data_destroy);
488
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400489 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400490 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400491 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400492 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400493
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400494 data->data.buffer = wl_shm_create_buffer(display->shm,
495 fd,
496 rectangle->width,
497 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400498 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499
500 close(fd);
501
502 return surface;
503}
504
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500505static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506display_create_shm_surface_from_file(struct display *display,
507 const char *filename,
508 struct rectangle *rect)
509{
510 cairo_surface_t *surface;
511 GdkPixbuf *pixbuf;
512 GError *error = NULL;
513 int stride, i;
514 unsigned char *pixels, *p, *end, *dest_data;
515 int dest_stride;
516 uint32_t *d;
517
518 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
519 rect->width, rect->height,
520 FALSE, &error);
521 if (error != NULL)
522 return NULL;
523
524 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
525 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500526 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527 return NULL;
528 }
529
530 stride = gdk_pixbuf_get_rowstride(pixbuf);
531 pixels = gdk_pixbuf_get_pixels(pixbuf);
532
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400533 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000534 if (surface == NULL) {
535 g_object_unref(pixbuf);
536 return NULL;
537 }
538
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539 dest_data = cairo_image_surface_get_data (surface);
540 dest_stride = cairo_image_surface_get_stride (surface);
541
542 for (i = 0; i < rect->height; i++) {
543 d = (uint32_t *) (dest_data + i * dest_stride);
544 p = pixels + i * stride;
545 end = p + rect->width * 4;
546 while (p < end) {
547 unsigned int t;
548 unsigned char a, r, g, b;
549
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 a = p[3];
551 MULT(r, p[0], a, t);
552 MULT(g, p[1], a, t);
553 MULT(b, p[2], a, t);
554 p += 4;
555 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
556 }
557 }
558
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500559 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
561 return surface;
562}
563
nobled7b87cb02011-02-01 18:51:47 +0000564static int
565check_size(struct rectangle *rect)
566{
567 if (rect->width && rect->height)
568 return 0;
569
570 fprintf(stderr, "tried to create surface of "
571 "width: %d, height: %d\n", rect->width, rect->height);
572 return -1;
573}
574
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575cairo_surface_t *
576display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100577 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400578 struct rectangle *rectangle,
579 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400580{
nobled7b87cb02011-02-01 18:51:47 +0000581 if (check_size(rectangle) < 0)
582 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500583#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500584 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100585 if (surface)
586 return display_create_egl_window_surface(display,
587 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400588 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100589 rectangle);
590 else
591 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400592 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100593 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500594 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400596 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597}
598
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500599static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600display_create_surface_from_file(struct display *display,
601 const char *filename,
602 struct rectangle *rectangle)
603{
nobled7b87cb02011-02-01 18:51:47 +0000604 if (check_size(rectangle) < 0)
605 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500606#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500607 if (display->dpy) {
608 return display_create_egl_image_surface_from_file(display,
609 filename,
610 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500611 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500613 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400614}
Yuval Fledel45568f62010-12-06 09:18:12 -0500615 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400616 const char *filename;
617 int hotspot_x, hotspot_y;
618} pointer_images[] = {
619 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
620 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
621 { DATADIR "/wayland/bottom_side.png", 16, 20 },
622 { DATADIR "/wayland/grabbing.png", 20, 17 },
623 { DATADIR "/wayland/left_ptr.png", 10, 5 },
624 { DATADIR "/wayland/left_side.png", 10, 20 },
625 { DATADIR "/wayland/right_side.png", 30, 19 },
626 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
627 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
628 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400629 { DATADIR "/wayland/xterm.png", 15, 15 },
630 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400631};
632
633static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400634create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635{
636 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400637 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400638 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400639
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400640 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400641 display->pointer_surfaces =
642 malloc(count * sizeof *display->pointer_surfaces);
643 rect.width = width;
644 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400645 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400646 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647 display_create_surface_from_file(display,
648 pointer_images[i].filename,
649 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650 }
651
652}
653
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400654cairo_surface_t *
655display_get_pointer_surface(struct display *display, int pointer,
656 int *width, int *height,
657 int *hotspot_x, int *hotspot_y)
658{
659 cairo_surface_t *surface;
660
661 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500662#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400663 *width = cairo_gl_surface_get_width(surface);
664 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000665#else
666 *width = cairo_image_surface_get_width(surface);
667 *height = cairo_image_surface_get_height(surface);
668#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400669 *hotspot_x = pointer_images[pointer].hotspot_x;
670 *hotspot_y = pointer_images[pointer].hotspot_y;
671
672 return cairo_surface_reference(surface);
673}
674
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400675
676static void
677window_attach_surface(struct window *window);
678
679static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400680free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400681{
682 struct window *window = data;
683
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400684 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400685 cairo_surface_destroy(window->pending_surface);
686 window->pending_surface = NULL;
687 if (window->cairo_surface)
688 window_attach_surface(window);
689}
690
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400691static const struct wl_callback_listener free_surface_listener = {
692 free_surface
693};
694
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500695static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200696window_get_resize_dx_dy(struct window *window, int *x, int *y)
697{
698 if (window->resize_edges & WINDOW_RESIZING_LEFT)
699 *x = window->server_allocation.width - window->allocation.width;
700 else
701 *x = 0;
702
703 if (window->resize_edges & WINDOW_RESIZING_TOP)
704 *y = window->server_allocation.height -
705 window->allocation.height;
706 else
707 *y = 0;
708
709 window->resize_edges = 0;
710}
711
712static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500713window_attach_surface(struct window *window)
714{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400715 struct display *display = window->display;
716 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400717 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000718#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100719 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000720#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500721 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722
723 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000724#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
726 data = cairo_surface_get_user_data(window->cairo_surface,
727 &surface_data_key);
728
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 cairo_gl_surface_swapbuffers(window->cairo_surface);
730 wl_egl_window_get_attached_size(data->window,
731 &window->server_allocation.width,
732 &window->server_allocation.height);
733 break;
734 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000735#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200737 window_get_resize_dx_dy(window, &x, &y);
738
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 if (window->pending_surface != NULL)
740 return;
741
742 window->pending_surface = window->cairo_surface;
743 window->cairo_surface = NULL;
744
745 buffer =
746 display_get_buffer_for_surface(display,
747 window->pending_surface);
748
749 wl_surface_attach(window->surface, buffer, x, y);
750 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400751 cb = wl_display_sync(display->display);
752 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100753 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000754 default:
755 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100756 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500757
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400758 switch (window->type) {
759 case TYPE_FULLSCREEN:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400760 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400761 break;
762 case TYPE_TOPLEVEL:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400763 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400764 break;
765 case TYPE_TRANSIENT:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400766 wl_shell_set_transient(display->shell, window->surface,
767 window->parent->surface,
768 window->x, window->y, 0);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400769 break;
770 case TYPE_CUSTOM:
771 break;
772 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500773
774 wl_surface_damage(window->surface, 0, 0,
775 window->allocation.width,
776 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500777}
778
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500779void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400780window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500781{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100782 if (window->cairo_surface) {
783 switch (window->buffer_type) {
784 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
785 case WINDOW_BUFFER_TYPE_SHM:
786 display_surface_damage(window->display,
787 window->cairo_surface,
788 0, 0,
789 window->allocation.width,
790 window->allocation.height);
791 break;
792 default:
793 break;
794 }
795 window_attach_surface(window);
796 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500797}
798
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400799void
800window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400801{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500802 cairo_surface_reference(surface);
803
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400804 if (window->cairo_surface != NULL)
805 cairo_surface_destroy(window->cairo_surface);
806
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500807 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400808}
809
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811static void
812window_resize_cairo_window_surface(struct window *window)
813{
814 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200815 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816
817 data = cairo_surface_get_user_data(window->cairo_surface,
818 &surface_data_key);
819
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200820 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 wl_egl_window_resize(data->window,
822 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200823 window->allocation.height,
824 x,y);
825
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 cairo_gl_surface_set_size(window->cairo_surface,
827 window->allocation.width,
828 window->allocation.height);
829}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400832void
833window_create_surface(struct window *window)
834{
835 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400836 uint32_t flags = 0;
837
838 if (!window->transparent)
839 flags = SURFACE_OPAQUE;
840
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500842#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
844 if (window->cairo_surface) {
845 window_resize_cairo_window_surface(window);
846 return;
847 }
848 surface = display_create_surface(window->display,
849 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400850 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500852 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400853 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400855 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856 break;
857#endif
858 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400859 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400860 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800862 default:
863 surface = NULL;
864 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400866
867 window_set_surface(window, surface);
868 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400869}
870
871static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500872window_draw_menu(struct window *window)
873{
874 cairo_t *cr;
875 int width, height, r = 5;
876
877 window_create_surface(window);
878
879 cr = cairo_create(window->cairo_surface);
880 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
881 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
882 cairo_paint(cr);
883
884 width = window->allocation.width;
885 height = window->allocation.height;
886 rounded_rect(cr, r, r, width - r, height - r, r);
887 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
888 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
889 cairo_fill(cr);
890 cairo_destroy(cr);
891}
892
893static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500894window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500895{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500896 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500897 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400898 cairo_surface_t *frame;
899 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500900
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400901 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400902
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400903 width = window->allocation.width;
904 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500905
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500906 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500907
Kristian Høgsberg09531622010-06-14 23:22:15 -0400908 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400909 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400910 cairo_paint(cr);
911
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400912 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400913 tile_mask(cr, window->display->shadow,
914 shadow_dx, shadow_dy, width, height,
915 window->margin + 10 - shadow_dx,
916 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500917
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400918 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400919 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400920 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400921 frame = window->display->inactive_frame;
922
923 tile_source(cr, frame, 0, 0, width, height,
924 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500925
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500926 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
927 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500928 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400930 if (window->keyboard_device)
931 cairo_set_source_rgb(cr, 0, 0, 0);
932 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400933 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400934 cairo_show_text(cr, window->title);
935
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500936 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400937
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100938 /* FIXME: this breakes gears, fix cairo? */
939#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400940 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100941#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500942}
943
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400944void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500945window_destroy(struct window *window)
946{
947 wl_surface_destroy(window->surface);
948 wl_list_remove(&window->link);
949 free(window);
950}
951
952void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400953display_flush_cairo_device(struct display *display)
954{
955 cairo_device_flush (display->device);
956}
957
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500958void
959window_draw(struct window *window)
960{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500961 if (window->parent)
962 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400963 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400964 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500965 else
966 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500967}
968
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400969cairo_surface_t *
970window_get_surface(struct window *window)
971{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400972 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400973}
974
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100975struct wl_surface *
976window_get_wl_surface(struct window *window)
977{
978 return window->surface;
979}
980
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400981static int
982get_pointer_location(struct window *window, int32_t x, int32_t y)
983{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400984 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400985 const int grip_size = 8;
986
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500987 if (!window->decoration)
988 return WINDOW_CLIENT_AREA;
989
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400990 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400991 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400992 else if (window->margin <= x && x < window->margin + grip_size)
993 hlocation = WINDOW_RESIZING_LEFT;
994 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400995 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400996 else if (x < window->allocation.width - window->margin)
997 hlocation = WINDOW_RESIZING_RIGHT;
998 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400999 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001000
1001 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001002 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001003 else if (window->margin <= y && y < window->margin + grip_size)
1004 vlocation = WINDOW_RESIZING_TOP;
1005 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001006 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001007 else if (y < window->allocation.height - window->margin)
1008 vlocation = WINDOW_RESIZING_BOTTOM;
1009 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001010 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001011
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001012 location = vlocation | hlocation;
1013 if (location & WINDOW_EXTERIOR)
1014 location = WINDOW_EXTERIOR;
1015 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1016 location = WINDOW_TITLEBAR;
1017 else if (location == WINDOW_INTERIOR)
1018 location = WINDOW_CLIENT_AREA;
1019
1020 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001021}
1022
1023static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001024set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001025{
1026 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001027 struct wl_buffer *buffer;
1028 cairo_surface_t *surface;
1029 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001030
1031 location = get_pointer_location(input->pointer_focus,
1032 input->sx, input->sy);
1033 switch (location) {
1034 case WINDOW_RESIZING_TOP:
1035 pointer = POINTER_TOP;
1036 break;
1037 case WINDOW_RESIZING_BOTTOM:
1038 pointer = POINTER_BOTTOM;
1039 break;
1040 case WINDOW_RESIZING_LEFT:
1041 pointer = POINTER_LEFT;
1042 break;
1043 case WINDOW_RESIZING_RIGHT:
1044 pointer = POINTER_RIGHT;
1045 break;
1046 case WINDOW_RESIZING_TOP_LEFT:
1047 pointer = POINTER_TOP_LEFT;
1048 break;
1049 case WINDOW_RESIZING_TOP_RIGHT:
1050 pointer = POINTER_TOP_RIGHT;
1051 break;
1052 case WINDOW_RESIZING_BOTTOM_LEFT:
1053 pointer = POINTER_BOTTOM_LEFT;
1054 break;
1055 case WINDOW_RESIZING_BOTTOM_RIGHT:
1056 pointer = POINTER_BOTTOM_RIGHT;
1057 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001058 case WINDOW_EXTERIOR:
1059 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001060 if (input->current_pointer_image == POINTER_DEFAULT)
1061 return;
1062
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001063 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001064 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001065 return;
1066 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001067 break;
1068 }
1069
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001070 if (pointer == input->current_pointer_image)
1071 return;
1072
1073 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001074 surface = display->pointer_surfaces[pointer];
1075 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001076 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001077 pointer_images[pointer].hotspot_x,
1078 pointer_images[pointer].hotspot_y);
1079}
1080
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001081static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001082window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001083 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001084 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001085{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001086 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001087 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001088 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001089
1090 input->x = x;
1091 input->y = y;
1092 input->sx = sx;
1093 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001094
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001095 if (window->motion_handler)
1096 pointer = (*window->motion_handler)(window, input, time,
1097 x, y, sx, sy,
1098 window->user_data);
1099
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001100 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001101}
1102
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001103static void
1104window_handle_button(void *data,
1105 struct wl_input_device *input_device,
1106 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001107{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001108 struct input *input = data;
1109 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001110 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001111
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001112 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001113
1114 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001116 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001117 wl_shell_move(window->display->shell,
1118 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001119 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001120 case WINDOW_RESIZING_TOP:
1121 case WINDOW_RESIZING_BOTTOM:
1122 case WINDOW_RESIZING_LEFT:
1123 case WINDOW_RESIZING_RIGHT:
1124 case WINDOW_RESIZING_TOP_LEFT:
1125 case WINDOW_RESIZING_TOP_RIGHT:
1126 case WINDOW_RESIZING_BOTTOM_LEFT:
1127 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001128 wl_shell_resize(window->display->shell,
1129 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001130 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001131 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001132 case WINDOW_CLIENT_AREA:
1133 if (window->button_handler)
1134 (*window->button_handler)(window,
1135 input, time,
1136 button, state,
1137 window->user_data);
1138 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001139 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001140 } else {
1141 if (window->button_handler)
1142 (*window->button_handler)(window,
1143 input, time,
1144 button, state,
1145 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001146 }
1147}
1148
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001149static void
1150window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001151 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001152{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001153 struct input *input = data;
1154 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001155 struct display *d = window->display;
1156 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001157
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001158 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001159 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001160 return;
1161
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001162 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001163 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001164 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1165 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001166
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001167 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1168
1169 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001170 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001171 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001172 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001173
1174 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001175 (*window->key_handler)(window, input, time, key, sym, state,
1176 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001177}
1178
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001179static void
1180window_handle_pointer_focus(void *data,
1181 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001182 uint32_t time, struct wl_surface *surface,
1183 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001184{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001185 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001186 struct window *window;
1187 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001188
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001189 window = input->pointer_focus;
1190 if (window && window->surface != surface) {
1191 if (window->leave_handler)
1192 window->leave_handler(window, input,
1193 time, window->user_data);
1194 input->pointer_focus = NULL;
1195 input->current_pointer_image = POINTER_UNSET;
1196 }
1197
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001198 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001199 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001200 window = input->pointer_focus;
1201
Kristian Høgsberg59826582011-01-20 11:56:57 -05001202 input->x = x;
1203 input->y = y;
1204 input->sx = sx;
1205 input->sy = sy;
1206
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001207 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001208 if (window->enter_handler)
1209 pointer = window->enter_handler(window, input,
1210 time, sx, sy,
1211 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001212
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001213 set_pointer_image(input, time, pointer);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001214
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001215 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001216}
1217
1218static void
1219window_handle_keyboard_focus(void *data,
1220 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001221 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001222 struct wl_surface *surface,
1223 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001224{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001225 struct input *input = data;
1226 struct window *window = input->keyboard_focus;
1227 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001228 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001229
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001230 window = input->keyboard_focus;
1231 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001232 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001233 if (window->keyboard_focus_handler)
1234 (*window->keyboard_focus_handler)(window, NULL,
1235 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001236 }
1237
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001238 if (surface)
1239 input->keyboard_focus = wl_surface_get_user_data(surface);
1240 else
1241 input->keyboard_focus = NULL;
1242
1243 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001244 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001245 for (k = keys->data; k < end; k++)
1246 input->modifiers |= d->xkb->map->modmap[*k];
1247
1248 window = input->keyboard_focus;
1249 if (window) {
1250 window->keyboard_device = input;
1251 if (window->keyboard_focus_handler)
1252 (*window->keyboard_focus_handler)(window,
1253 window->keyboard_device,
1254 window->user_data);
1255 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001256}
1257
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001258static const struct wl_input_device_listener input_device_listener = {
1259 window_handle_motion,
1260 window_handle_button,
1261 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001262 window_handle_pointer_focus,
1263 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001264};
1265
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001266void
1267input_get_position(struct input *input, int32_t *x, int32_t *y)
1268{
1269 *x = input->sx;
1270 *y = input->sy;
1271}
1272
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001273struct wl_input_device *
1274input_get_input_device(struct input *input)
1275{
1276 return input->input_device;
1277}
1278
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001279uint32_t
1280input_get_modifiers(struct input *input)
1281{
1282 return input->modifiers;
1283}
1284
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001285struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001286window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001287{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001288 cairo_device_flush (window->display->device);
1289
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001290 return wl_shell_create_drag(window->display->shell);
1291}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001292
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001293void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001294window_move(struct window *window, struct input *input, uint32_t time)
1295{
1296 wl_shell_move(window->display->shell,
1297 window->surface, input->input_device, time);
1298}
1299
1300void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001301window_activate_drag(struct wl_drag *drag, struct window *window,
1302 struct input *input, uint32_t time)
1303{
1304 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001305}
1306
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001307static void
1308handle_configure(void *data, struct wl_shell *shell,
1309 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001310 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001311{
1312 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001313 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001314
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001315 /* FIXME: this is probably the wrong place to check for width
1316 * or height <= 0, but it prevents the compositor from crashing
1317 */
1318 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001319 return;
1320
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001321 window->resize_edges = edges;
1322
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001323 if (window->resize_handler) {
1324 child_width = width - 20 - window->margin * 2;
1325 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001326
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001327 (*window->resize_handler)(window,
1328 child_width, child_height,
1329 window->user_data);
1330 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001331 window->allocation.width = width;
1332 window->allocation.height = height;
1333
1334 if (window->redraw_handler)
1335 window_schedule_redraw(window);
1336 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001337}
1338
1339static const struct wl_shell_listener shell_listener = {
1340 handle_configure,
1341};
1342
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001343void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001344window_get_allocation(struct window *window,
1345 struct rectangle *allocation)
1346{
1347 *allocation = window->allocation;
1348}
1349
1350void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001351window_get_child_allocation(struct window *window,
1352 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001353{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001354 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001355 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001356 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001357 allocation->x = window->margin + 10;
1358 allocation->y = window->margin + 50;
1359 allocation->width =
1360 window->allocation.width - 20 - window->margin * 2;
1361 allocation->height =
1362 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001363 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001364}
1365
1366void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001367window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001368{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001369 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001370 window->allocation.x = 20 + window->margin;
1371 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001372 window->allocation.width = width + 20 + window->margin * 2;
1373 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001374 } else {
1375 window->allocation.x = 0;
1376 window->allocation.y = 0;
1377 window->allocation.width = width;
1378 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001379 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001380}
1381
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001382static void
1383idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001384{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001385 struct window *window =
1386 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001387
1388 window->redraw_handler(window, window->user_data);
1389 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001390}
1391
1392void
1393window_schedule_redraw(struct window *window)
1394{
1395 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001396 window->redraw_task.run = idle_redraw;
1397 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001398 window->redraw_scheduled = 1;
1399 }
1400}
1401
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001402void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001403window_set_custom(struct window *window)
1404{
1405 window->type = TYPE_CUSTOM;
1406}
1407
1408void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001409window_set_fullscreen(struct window *window, int fullscreen)
1410{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001411 int32_t width, height;
1412
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001413 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001414 return;
1415
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001416 if (fullscreen) {
1417 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001418 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001419 width = window->display->screen_allocation.width;
1420 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001421 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001422 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001423 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001424 width = window->saved_allocation.width - 20 - window->margin * 2;
1425 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001426 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001427 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001428
1429 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001430}
1431
1432void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001433window_set_decoration(struct window *window, int decoration)
1434{
1435 window->decoration = decoration;
1436}
1437
1438void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001439window_set_user_data(struct window *window, void *data)
1440{
1441 window->user_data = data;
1442}
1443
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001444void *
1445window_get_user_data(struct window *window)
1446{
1447 return window->user_data;
1448}
1449
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001450void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001451window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001452 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001453{
1454 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001455}
1456
1457void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001458window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001459 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001460{
1461 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001462}
1463
1464void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001465window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001466 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001467{
1468 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001469}
1470
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001471void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001472window_set_button_handler(struct window *window,
1473 window_button_handler_t handler)
1474{
1475 window->button_handler = handler;
1476}
1477
1478void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001479window_set_motion_handler(struct window *window,
1480 window_motion_handler_t handler)
1481{
1482 window->motion_handler = handler;
1483}
1484
1485void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001486window_set_enter_handler(struct window *window,
1487 window_enter_handler_t handler)
1488{
1489 window->enter_handler = handler;
1490}
1491
1492void
1493window_set_leave_handler(struct window *window,
1494 window_leave_handler_t handler)
1495{
1496 window->leave_handler = handler;
1497}
1498
1499void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001500window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001501 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001502{
1503 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001504}
1505
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001506void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001507window_set_transparent(struct window *window, int transparent)
1508{
1509 window->transparent = transparent;
1510}
1511
1512void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001513window_set_title(struct window *window, const char *title)
1514{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001515 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001516 window->title = strdup(title);
1517}
1518
1519const char *
1520window_get_title(struct window *window)
1521{
1522 return window->title;
1523}
1524
1525void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001526display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1527 int32_t x, int32_t y, int32_t width, int32_t height)
1528{
1529 struct wl_buffer *buffer;
1530
1531 buffer = display_get_buffer_for_surface(display, cairo_surface);
1532
1533 wl_buffer_damage(buffer, x, y, width, height);
1534}
1535
1536void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001537window_damage(struct window *window, int32_t x, int32_t y,
1538 int32_t width, int32_t height)
1539{
1540 wl_surface_damage(window->surface, x, y, width, height);
1541}
1542
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001543static struct window *
1544window_create_internal(struct display *display, struct window *parent,
1545 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001546{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001547 struct window *window;
1548
1549 window = malloc(sizeof *window);
1550 if (window == NULL)
1551 return NULL;
1552
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001553 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001554 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001555 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001556 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001557 window->allocation.x = 0;
1558 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001559 window->allocation.width = width;
1560 window->allocation.height = height;
1561 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001562 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001563 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001564 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001565
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001566 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001567#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001568 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1569 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001570#else
1571 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1572#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001573 else
1574 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001575
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001576 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001577 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001578
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001579 return window;
1580}
1581
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001582struct window *
1583window_create(struct display *display, int32_t width, int32_t height)
1584{
1585 struct window *window;
1586
1587 window = window_create_internal(display, NULL, width, height);
1588 if (!window)
1589 return NULL;
1590
1591 return window;
1592}
1593
1594struct window *
1595window_create_transient(struct display *display, struct window *parent,
1596 int32_t x, int32_t y, int32_t width, int32_t height)
1597{
1598 struct window *window;
1599
1600 window = window_create_internal(parent->display,
1601 parent, width, height);
1602 if (!window)
1603 return NULL;
1604
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001605 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001606 window->x = x;
1607 window->y = y;
1608
1609 return window;
1610}
1611
1612void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001613window_set_buffer_type(struct window *window, enum window_buffer_type type)
1614{
1615 window->buffer_type = type;
1616}
1617
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001618
1619static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001620display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001621 struct wl_output *wl_output,
1622 int x, int y,
1623 int physical_width,
1624 int physical_height,
1625 int subpixel,
1626 const char *make,
1627 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001628{
1629 struct display *display = data;
1630
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001631 display->screen_allocation.x = x;
1632 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001633}
1634
1635static void
1636display_handle_mode(void *data,
1637 struct wl_output *wl_output,
1638 uint32_t flags,
1639 int width,
1640 int height,
1641 int refresh)
1642{
1643 struct display *display = data;
1644
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001645 display->screen_allocation.width = width;
1646 display->screen_allocation.height = height;
1647}
1648
1649static const struct wl_output_listener output_listener = {
1650 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001651 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001652};
1653
1654static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001655display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001656{
1657 struct input *input;
1658
1659 input = malloc(sizeof *input);
1660 if (input == NULL)
1661 return;
1662
1663 memset(input, 0, sizeof *input);
1664 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001665 input->input_device =
1666 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001667 input->pointer_focus = NULL;
1668 input->keyboard_focus = NULL;
1669 wl_list_insert(d->input_list.prev, &input->link);
1670
1671 wl_input_device_add_listener(input->input_device,
1672 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001673 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001674}
1675
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001676struct selection_offer {
1677 struct display *display;
1678 struct wl_selection_offer *offer;
1679 struct wl_array types;
1680 struct input *input;
1681};
1682
1683int
1684input_offers_mime_type(struct input *input, const char *type)
1685{
1686 struct selection_offer *offer = input->offer;
1687 char **p, **end;
1688
1689 if (offer == NULL)
1690 return 0;
1691
1692 end = offer->types.data + offer->types.size;
1693 for (p = offer->types.data; p < end; p++)
1694 if (strcmp(*p, type) == 0)
1695 return 1;
1696
1697 return 0;
1698}
1699
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001700void
1701input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001702{
1703 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001704
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001705 /* FIXME: A number of things can go wrong here: the object may
1706 * not be the current selection offer any more (which could
1707 * still work, but the source may have gone away or just
1708 * destroyed its wl_selection) or the offer may not have the
1709 * requested type after all (programmer/client error,
1710 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001711 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001712}
1713
1714static void
1715selection_offer_offer(void *data,
1716 struct wl_selection_offer *selection_offer,
1717 const char *type)
1718{
1719 struct selection_offer *offer = data;
1720
1721 char **p;
1722
1723 p = wl_array_add(&offer->types, sizeof *p);
1724 if (p)
1725 *p = strdup(type);
1726};
1727
1728static void
1729selection_offer_keyboard_focus(void *data,
1730 struct wl_selection_offer *selection_offer,
1731 struct wl_input_device *input_device)
1732{
1733 struct selection_offer *offer = data;
1734 struct input *input;
1735 char **p, **end;
1736
1737 if (input_device == NULL) {
1738 printf("selection offer retracted %p\n", selection_offer);
1739 input = offer->input;
1740 input->offer = NULL;
1741 wl_selection_offer_destroy(selection_offer);
1742 wl_array_release(&offer->types);
1743 free(offer);
1744 return;
1745 }
1746
1747 input = wl_input_device_get_user_data(input_device);
1748 printf("new selection offer %p:", selection_offer);
1749
1750 offer->input = input;
1751 input->offer = offer;
1752 end = offer->types.data + offer->types.size;
1753 for (p = offer->types.data; p < end; p++)
1754 printf(" %s", *p);
1755
1756 printf("\n");
1757}
1758
1759struct wl_selection_offer_listener selection_offer_listener = {
1760 selection_offer_offer,
1761 selection_offer_keyboard_focus
1762};
1763
1764static void
1765add_selection_offer(struct display *d, uint32_t id)
1766{
1767 struct selection_offer *offer;
1768
1769 offer = malloc(sizeof *offer);
1770 if (offer == NULL)
1771 return;
1772
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001773 offer->offer =
1774 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001775 offer->display = d;
1776 wl_array_init(&offer->types);
1777 offer->input = NULL;
1778
1779 wl_selection_offer_add_listener(offer->offer,
1780 &selection_offer_listener, offer);
1781}
1782
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001783static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001784display_handle_global(struct wl_display *display, uint32_t id,
1785 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001786{
1787 struct display *d = data;
1788
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001789 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001790 d->compositor =
1791 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001792 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001793 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001794 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001795 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001796 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001797 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001798 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001799 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001800 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001801 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001802 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001803 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001804 }
1805}
1806
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001807static void
1808display_render_frame(struct display *d)
1809{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001810 int radius = 8;
1811 cairo_t *cr;
1812
1813 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1814 cr = cairo_create(d->shadow);
1815 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1816 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1817 rounded_rect(cr, 16, 16, 112, 112, radius);
1818 cairo_fill(cr);
1819 cairo_destroy(cr);
1820 blur_surface(d->shadow, 64);
1821
1822 d->active_frame =
1823 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1824 cr = cairo_create(d->active_frame);
1825 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1826 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1827 rounded_rect(cr, 16, 16, 112, 112, radius);
1828 cairo_fill(cr);
1829 cairo_destroy(cr);
1830
1831 d->inactive_frame =
1832 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1833 cr = cairo_create(d->inactive_frame);
1834 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1835 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1836 rounded_rect(cr, 16, 16, 112, 112, radius);
1837 cairo_fill(cr);
1838 cairo_destroy(cr);
1839}
1840
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001841static void
1842init_xkb(struct display *d)
1843{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001844 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001845
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001846 names.rules = "evdev";
1847 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001848 names.layout = option_xkb_layout;
1849 names.variant = option_xkb_variant;
1850 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001851
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001852 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001853 if (!d->xkb) {
1854 fprintf(stderr, "Failed to compile keymap\n");
1855 exit(1);
1856 }
1857}
1858
Yuval Fledel45568f62010-12-06 09:18:12 -05001859static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001860init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001861{
1862 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001863 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001864
1865 static const EGLint premul_argb_cfg_attribs[] = {
1866 EGL_SURFACE_TYPE,
1867 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1868 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001869 EGL_RED_SIZE, 1,
1870 EGL_GREEN_SIZE, 1,
1871 EGL_BLUE_SIZE, 1,
1872 EGL_ALPHA_SIZE, 1,
1873 EGL_DEPTH_SIZE, 1,
1874 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1875 EGL_NONE
1876 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001877
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001878 static const EGLint rgb_cfg_attribs[] = {
1879 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1880 EGL_RED_SIZE, 1,
1881 EGL_GREEN_SIZE, 1,
1882 EGL_BLUE_SIZE, 1,
1883 EGL_ALPHA_SIZE, 0,
1884 EGL_DEPTH_SIZE, 1,
1885 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1886 EGL_NONE
1887 };
1888
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001889 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001890 if (!eglInitialize(d->dpy, &major, &minor)) {
1891 fprintf(stderr, "failed to initialize display\n");
1892 return -1;
1893 }
1894
1895 if (!eglBindAPI(EGL_OPENGL_API)) {
1896 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1897 return -1;
1898 }
1899
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001900 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02001901 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001902 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001903 return -1;
1904 }
1905
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001906 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
1907 &d->rgb_config, 1, &n) || n != 1) {
1908 fprintf(stderr, "failed to choose rgb config\n");
1909 return -1;
1910 }
1911
1912 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001913 if (d->ctx == NULL) {
1914 fprintf(stderr, "failed to create context\n");
1915 return -1;
1916 }
1917
1918 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001919 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001920 return -1;
1921 }
1922
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001923#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001924 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001925 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001926 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001927 return -1;
1928 }
1929#endif
1930
1931 return 0;
1932}
1933
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001934static int
1935event_mask_update(uint32_t mask, void *data)
1936{
1937 struct display *d = data;
1938
1939 d->mask = mask;
1940
1941 return 0;
1942}
1943
1944static void
1945handle_display_data(struct task *task, uint32_t events)
1946{
1947 struct display *display =
1948 container_of(task, struct display, display_task);
1949
1950 wl_display_iterate(display->display, display->mask);
1951}
1952
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001953struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04001954display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001955{
1956 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001957 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001958 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001959 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001960
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001961 g_type_init();
1962
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001963 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001964 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001965 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001966
1967 xkb_option_group = g_option_group_new("xkb",
1968 "Keyboard options",
1969 "Show all XKB options",
1970 NULL, NULL);
1971 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1972 g_option_context_add_group (context, xkb_option_group);
1973
1974 if (!g_option_context_parse(context, argc, argv, &error)) {
1975 fprintf(stderr, "option parsing failed: %s\n", error->message);
1976 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001977 }
1978
Tim Wiederhake748f6722011-01-23 23:25:25 +01001979 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001980
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001981 d = malloc(sizeof *d);
1982 if (d == NULL)
1983 return NULL;
1984
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001985 memset(d, 0, sizeof *d);
1986
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001987 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001988 if (d->display == NULL) {
1989 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001990 return NULL;
1991 }
1992
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001993 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1994 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
1995 d->display_task.run = handle_display_data;
1996 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
1997
1998 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001999 wl_list_init(&d->input_list);
2000
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002001 /* Set up listener so we'll catch all events. */
2002 wl_display_add_global_listener(d->display,
2003 display_handle_global, d);
2004
2005 /* Process connection events. */
2006 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002007 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002008 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002009
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002010 d->image_target_texture_2d =
2011 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2012 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2013 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2014
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002015 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002016
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002017 display_render_frame(d);
2018
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002019 wl_list_init(&d->window_list);
2020
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002021 init_xkb(d);
2022
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002023 return d;
2024}
2025
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002026struct wl_display *
2027display_get_display(struct display *display)
2028{
2029 return display->display;
2030}
2031
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002032struct wl_compositor *
2033display_get_compositor(struct display *display)
2034{
2035 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002036}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002037
2038EGLDisplay
2039display_get_egl_display(struct display *d)
2040{
2041 return d->dpy;
2042}
2043
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002044EGLConfig
2045display_get_egl_config(struct display *d)
2046{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002047 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002048}
2049
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002050struct wl_shell *
2051display_get_shell(struct display *display)
2052{
2053 return display->shell;
2054}
2055
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002056void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002057display_acquire_window_surface(struct display *display,
2058 struct window *window,
2059 EGLContext ctx)
2060{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002061#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002062 struct egl_window_surface_data *data;
2063
2064 if (!window->cairo_surface)
2065 return;
2066
2067 if (!ctx)
2068 ctx = display->ctx;
2069
2070 data = cairo_surface_get_user_data(window->cairo_surface,
2071 &surface_data_key);
2072
2073 cairo_device_acquire(display->device);
2074 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2075 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002076#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002077}
2078
2079void
2080display_release(struct display *display)
2081{
2082 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2083 fprintf(stderr, "failed to make context current\n");
2084 cairo_device_release(display->device);
2085}
2086
2087void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002088display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002089{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002090 wl_list_insert(&display->deferred_list, &task->link);
2091}
2092
2093void
2094display_watch_fd(struct display *display,
2095 int fd, uint32_t events, struct task *task)
2096{
2097 struct epoll_event ep;
2098
2099 ep.events = events;
2100 ep.data.ptr = task;
2101 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2102}
2103
2104void
2105display_run(struct display *display)
2106{
2107 struct task *task;
2108 struct epoll_event ep[16];
2109 int i, count;
2110
2111 while (1) {
2112 while (display->mask & WL_DISPLAY_WRITABLE)
2113 wl_display_iterate(display->display,
2114 WL_DISPLAY_WRITABLE);
2115
2116 count = epoll_wait(display->epoll_fd,
2117 ep, ARRAY_LENGTH(ep), -1);
2118 for (i = 0; i < count; i++) {
2119 task = ep[i].data.ptr;
2120 task->run(task, ep[i].events);
2121 }
2122
2123 while (!wl_list_empty(&display->deferred_list)) {
2124 task = container_of(display->deferred_list.next,
2125 struct task, link);
2126 wl_list_remove(&task->link);
2127 task->run(task, 0);
2128 }
2129 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002130}