blob: 1c94fcf39eea502c5a0e8b1467461ceb6b7092c9 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040023#include "../config.h"
24
Kristian Høgsberg61017b12008-11-02 18:51:48 -050025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050029#include <fcntl.h>
30#include <unistd.h>
31#include <math.h>
32#include <time.h>
33#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050034#include <glib.h>
Kristian Høgsberg478d9262010-06-08 20:34:11 -040035#include <glib-object.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040036#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050039#include <wayland-egl.h>
40
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041#include <GL/gl.h>
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040044
Kristian Høgsberg8def2642011-01-14 17:41:33 -050045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040046#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040049#include <X11/extensions/XKBcommon.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050052#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050054#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040055#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050057#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050058
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050060 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050061 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040062 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040063 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040069 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010070 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040072 GMainLoop *loop;
73 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040074 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040075 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040076 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040077 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040078 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040079 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040080
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050081 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
82 PFNEGLCREATEIMAGEKHRPROC create_image;
83 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084};
85
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040086enum {
87 TYPE_FULLSCREEN,
88 TYPE_TOPLEVEL,
89 TYPE_TRANSIENT,
90 TYPE_CUSTOM
91};
92
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050093struct window {
94 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050095 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050096 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050097 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050098 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050099 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400100 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400101 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500102 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500103 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400105 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400106 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400107 struct input *grab_device;
108 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500109 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400110 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500111
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500113 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500114
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500115 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500117 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400118 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500119 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400120 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400121 window_enter_handler_t enter_handler;
122 window_leave_handler_t leave_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400123
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500124 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400125 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500126};
127
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128struct input {
129 struct display *display;
130 struct wl_input_device *input_device;
131 struct window *pointer_focus;
132 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500133 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400134 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400135 uint32_t modifiers;
136 int32_t x, y, sx, sy;
137 struct wl_list link;
138};
139
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400140enum {
141 POINTER_DEFAULT = 100,
142 POINTER_UNSET
143};
144
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500145enum window_location {
146 WINDOW_INTERIOR = 0,
147 WINDOW_RESIZING_TOP = 1,
148 WINDOW_RESIZING_BOTTOM = 2,
149 WINDOW_RESIZING_LEFT = 4,
150 WINDOW_RESIZING_TOP_LEFT = 5,
151 WINDOW_RESIZING_BOTTOM_LEFT = 6,
152 WINDOW_RESIZING_RIGHT = 8,
153 WINDOW_RESIZING_TOP_RIGHT = 9,
154 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
155 WINDOW_RESIZING_MASK = 15,
156 WINDOW_EXTERIOR = 16,
157 WINDOW_TITLEBAR = 17,
158 WINDOW_CLIENT_AREA = 18,
159};
160
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400161const char *option_xkb_layout = "us";
162const char *option_xkb_variant = "";
163const char *option_xkb_options = "";
164
165static const GOptionEntry xkb_option_entries[] = {
166 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
167 &option_xkb_layout, "XKB Layout" },
168 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
169 &option_xkb_variant, "XKB Variant" },
170 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
171 &option_xkb_options, "XKB Options" },
172 { NULL }
173};
174
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500175static void
176rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
177{
178 cairo_move_to(cr, x0, y0 + radius);
179 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
180 cairo_line_to(cr, x1 - radius, y0);
181 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
182 cairo_line_to(cr, x1, y1 - radius);
183 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
184 cairo_line_to(cr, x0 + radius, y1);
185 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
186 cairo_close_path(cr);
187}
188
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400189static const cairo_user_data_key_t surface_data_key;
190struct surface_data {
191 struct wl_buffer *buffer;
192};
193
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500194#define MULT(_d,c,a,t) \
195 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
196
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500197#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400198
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100199struct egl_window_surface_data {
200 struct display *display;
201 struct wl_surface *surface;
202 struct wl_egl_window *window;
203 EGLSurface surf;
204};
205
206static void
207egl_window_surface_data_destroy(void *p)
208{
209 struct egl_window_surface_data *data = p;
210 struct display *d = data->display;
211
212 eglDestroySurface(d->dpy, data->surf);
213 wl_egl_window_destroy(data->window);
214 data->surface = NULL;
215
216 free(p);
217}
218
219static cairo_surface_t *
220display_create_egl_window_surface(struct display *display,
221 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400222 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100223 struct rectangle *rectangle)
224{
225 cairo_surface_t *cairo_surface;
226 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400227 EGLConfig config;
228 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100229
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400230 static const EGLint premul_attribs[] = {
231 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
232 EGL_NONE
233 };
234
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100235 data = malloc(sizeof *data);
236 if (data == NULL)
237 return NULL;
238
239 data->display = display;
240 data->surface = surface;
241
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400242 if (flags & SURFACE_OPAQUE) {
243 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200244 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400245 } else {
246 config = display->premultiplied_argb_config;
247 attribs = premul_attribs;
248 }
249
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400250 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100251 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400252 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100253
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400254 data->surf = eglCreateWindowSurface(display->dpy, config,
255 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100256
257 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
258 data->surf,
259 rectangle->width,
260 rectangle->height);
261
262 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
263 data, egl_window_surface_data_destroy);
264
265 return cairo_surface;
266}
267
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500268struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400269 struct surface_data data;
270 EGLImageKHR image;
271 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800272 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500273 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400274};
275
276static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500277egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400278{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500279 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800280 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400281
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800282 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800284 cairo_device_release(d->device);
285
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500286 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400287 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500288 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500289 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290}
291
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500292EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500293display_get_image_for_egl_image_surface(struct display *display,
294 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500295{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500296 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500297
298 data = cairo_surface_get_user_data (surface, &surface_data_key);
299
300 return data->image;
301}
302
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500303static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500304display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400305 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500306 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400307{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500308 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400309 EGLDisplay dpy = display->dpy;
310 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400311 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312
313 data = malloc(sizeof *data);
314 if (data == NULL)
315 return NULL;
316
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 data->display = display;
318
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400319 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400320 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000322 free(data);
323 return NULL;
324 }
325
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400326 if (flags & SURFACE_OPAQUE)
327 config = display->rgb_config;
328 else
329 config = display->premultiplied_argb_config;
330
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500331 data->image = display->create_image(dpy, NULL,
332 EGL_NATIVE_PIXMAP_KHR,
333 (EGLClientBuffer) data->pixmap,
334 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500336 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337 free(data);
338 return NULL;
339 }
340
341 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400342 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500343
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800344 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400346 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500347 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800348 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350 surface = cairo_gl_surface_create_for_texture(display->device,
351 CAIRO_CONTENT_COLOR_ALPHA,
352 data->texture,
353 rectangle->width,
354 rectangle->height);
355
356 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500357 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358
359 return surface;
360}
361
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500362static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363display_create_egl_image_surface_from_file(struct display *display,
364 const char *filename,
365 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400366{
367 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400368 GdkPixbuf *pixbuf;
369 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400370 int stride, i;
371 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500372 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400373
374 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400375 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400376 FALSE, &error);
377 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400378 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400379
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400380 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
381 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500382 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400383 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400384 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400385
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400386
387 stride = gdk_pixbuf_get_rowstride(pixbuf);
388 pixels = gdk_pixbuf_get_pixels(pixbuf);
389
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400391 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400393 while (p < end) {
394 unsigned int t;
395
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400396 MULT(p[0], p[0], p[3], t);
397 MULT(p[1], p[1], p[3], t);
398 MULT(p[2], p[2], p[3], t);
399 p += 4;
400
401 }
402 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400403
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200404 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000405 if (surface == NULL) {
406 g_object_unref(pixbuf);
407 return NULL;
408 }
409
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800410 data = cairo_surface_get_user_data(surface, &surface_data_key);
411
412 cairo_device_acquire(display->device);
413 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800414 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
415 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800416 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500418 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420 return surface;
421}
422
423#endif
424
425struct wl_buffer *
426display_get_buffer_for_surface(struct display *display,
427 cairo_surface_t *surface)
428{
429 struct surface_data *data;
430
431 data = cairo_surface_get_user_data (surface, &surface_data_key);
432
433 return data->buffer;
434}
435
436struct shm_surface_data {
437 struct surface_data data;
438 void *map;
439 size_t length;
440};
441
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500442static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443shm_surface_data_destroy(void *p)
444{
445 struct shm_surface_data *data = p;
446
447 wl_buffer_destroy(data->data.buffer);
448 munmap(data->map, data->length);
449}
450
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500451static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400452display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400453 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454{
455 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400456 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400457 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800458 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459 char filename[] = "/tmp/wayland-shm-XXXXXX";
460
461 data = malloc(sizeof *data);
462 if (data == NULL)
463 return NULL;
464
465 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
466 rectangle->width);
467 data->length = stride * rectangle->height;
468 fd = mkstemp(filename);
469 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000470 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400471 return NULL;
472 }
473 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000474 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400475 close(fd);
476 return NULL;
477 }
478
479 data->map = mmap(NULL, data->length,
480 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
481 unlink(filename);
482
483 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000484 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485 close(fd);
486 return NULL;
487 }
488
489 surface = cairo_image_surface_create_for_data (data->map,
490 CAIRO_FORMAT_ARGB32,
491 rectangle->width,
492 rectangle->height,
493 stride);
494
495 cairo_surface_set_user_data (surface, &surface_data_key,
496 data, shm_surface_data_destroy);
497
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400498 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400499 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400500 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503 data->data.buffer = wl_shm_create_buffer(display->shm,
504 fd,
505 rectangle->width,
506 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400507 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508
509 close(fd);
510
511 return surface;
512}
513
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500514static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400515display_create_shm_surface_from_file(struct display *display,
516 const char *filename,
517 struct rectangle *rect)
518{
519 cairo_surface_t *surface;
520 GdkPixbuf *pixbuf;
521 GError *error = NULL;
522 int stride, i;
523 unsigned char *pixels, *p, *end, *dest_data;
524 int dest_stride;
525 uint32_t *d;
526
527 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
528 rect->width, rect->height,
529 FALSE, &error);
530 if (error != NULL)
531 return NULL;
532
533 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
534 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500535 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536 return NULL;
537 }
538
539 stride = gdk_pixbuf_get_rowstride(pixbuf);
540 pixels = gdk_pixbuf_get_pixels(pixbuf);
541
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400542 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000543 if (surface == NULL) {
544 g_object_unref(pixbuf);
545 return NULL;
546 }
547
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548 dest_data = cairo_image_surface_get_data (surface);
549 dest_stride = cairo_image_surface_get_stride (surface);
550
551 for (i = 0; i < rect->height; i++) {
552 d = (uint32_t *) (dest_data + i * dest_stride);
553 p = pixels + i * stride;
554 end = p + rect->width * 4;
555 while (p < end) {
556 unsigned int t;
557 unsigned char a, r, g, b;
558
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559 a = p[3];
560 MULT(r, p[0], a, t);
561 MULT(g, p[1], a, t);
562 MULT(b, p[2], a, t);
563 p += 4;
564 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
565 }
566 }
567
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500568 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569
570 return surface;
571}
572
nobled7b87cb02011-02-01 18:51:47 +0000573static int
574check_size(struct rectangle *rect)
575{
576 if (rect->width && rect->height)
577 return 0;
578
579 fprintf(stderr, "tried to create surface of "
580 "width: %d, height: %d\n", rect->width, rect->height);
581 return -1;
582}
583
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584cairo_surface_t *
585display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100586 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400587 struct rectangle *rectangle,
588 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400589{
nobled7b87cb02011-02-01 18:51:47 +0000590 if (check_size(rectangle) < 0)
591 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500592#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500593 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100594 if (surface)
595 return display_create_egl_window_surface(display,
596 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400597 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100598 rectangle);
599 else
600 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400601 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500603 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400605 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606}
607
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500608static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400609display_create_surface_from_file(struct display *display,
610 const char *filename,
611 struct rectangle *rectangle)
612{
nobled7b87cb02011-02-01 18:51:47 +0000613 if (check_size(rectangle) < 0)
614 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500615#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500616 if (display->dpy) {
617 return display_create_egl_image_surface_from_file(display,
618 filename,
619 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500620 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500622 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400623}
Yuval Fledel45568f62010-12-06 09:18:12 -0500624 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400625 const char *filename;
626 int hotspot_x, hotspot_y;
627} pointer_images[] = {
628 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
629 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
630 { DATADIR "/wayland/bottom_side.png", 16, 20 },
631 { DATADIR "/wayland/grabbing.png", 20, 17 },
632 { DATADIR "/wayland/left_ptr.png", 10, 5 },
633 { DATADIR "/wayland/left_side.png", 10, 20 },
634 { DATADIR "/wayland/right_side.png", 30, 19 },
635 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
636 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
637 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400638 { DATADIR "/wayland/xterm.png", 15, 15 },
639 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400640};
641
642static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400643create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400644{
645 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400646 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400647 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400648
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400649 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400650 display->pointer_surfaces =
651 malloc(count * sizeof *display->pointer_surfaces);
652 rect.width = width;
653 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400654 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400655 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656 display_create_surface_from_file(display,
657 pointer_images[i].filename,
658 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400659 }
660
661}
662
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400663cairo_surface_t *
664display_get_pointer_surface(struct display *display, int pointer,
665 int *width, int *height,
666 int *hotspot_x, int *hotspot_y)
667{
668 cairo_surface_t *surface;
669
670 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500671#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672 *width = cairo_gl_surface_get_width(surface);
673 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000674#else
675 *width = cairo_image_surface_get_width(surface);
676 *height = cairo_image_surface_get_height(surface);
677#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678 *hotspot_x = pointer_images[pointer].hotspot_x;
679 *hotspot_y = pointer_images[pointer].hotspot_y;
680
681 return cairo_surface_reference(surface);
682}
683
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400684
685static void
686window_attach_surface(struct window *window);
687
688static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400689free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400690{
691 struct window *window = data;
692
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400693 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400694 cairo_surface_destroy(window->pending_surface);
695 window->pending_surface = NULL;
696 if (window->cairo_surface)
697 window_attach_surface(window);
698}
699
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400700static const struct wl_callback_listener free_surface_listener = {
701 free_surface
702};
703
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500704static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200705window_get_resize_dx_dy(struct window *window, int *x, int *y)
706{
707 if (window->resize_edges & WINDOW_RESIZING_LEFT)
708 *x = window->server_allocation.width - window->allocation.width;
709 else
710 *x = 0;
711
712 if (window->resize_edges & WINDOW_RESIZING_TOP)
713 *y = window->server_allocation.height -
714 window->allocation.height;
715 else
716 *y = 0;
717
718 window->resize_edges = 0;
719}
720
721static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500722window_attach_surface(struct window *window)
723{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400724 struct display *display = window->display;
725 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400726 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000727#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000729#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500730 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731
732 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000733#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
735 data = cairo_surface_get_user_data(window->cairo_surface,
736 &surface_data_key);
737
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 cairo_gl_surface_swapbuffers(window->cairo_surface);
739 wl_egl_window_get_attached_size(data->window,
740 &window->server_allocation.width,
741 &window->server_allocation.height);
742 break;
743 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000744#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100745 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200746 window_get_resize_dx_dy(window, &x, &y);
747
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100748 if (window->pending_surface != NULL)
749 return;
750
751 window->pending_surface = window->cairo_surface;
752 window->cairo_surface = NULL;
753
754 buffer =
755 display_get_buffer_for_surface(display,
756 window->pending_surface);
757
758 wl_surface_attach(window->surface, buffer, x, y);
759 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400760 cb = wl_display_sync(display->display);
761 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100762 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000763 default:
764 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100765 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500766
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400767 switch (window->type) {
768 case TYPE_FULLSCREEN:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400769 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400770 break;
771 case TYPE_TOPLEVEL:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400772 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400773 break;
774 case TYPE_TRANSIENT:
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400775 wl_shell_set_transient(display->shell, window->surface,
776 window->parent->surface,
777 window->x, window->y, 0);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400778 break;
779 case TYPE_CUSTOM:
780 break;
781 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500782
783 wl_surface_damage(window->surface, 0, 0,
784 window->allocation.width,
785 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500786}
787
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500788void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400789window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500790{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100791 if (window->cairo_surface) {
792 switch (window->buffer_type) {
793 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
794 case WINDOW_BUFFER_TYPE_SHM:
795 display_surface_damage(window->display,
796 window->cairo_surface,
797 0, 0,
798 window->allocation.width,
799 window->allocation.height);
800 break;
801 default:
802 break;
803 }
804 window_attach_surface(window);
805 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500806}
807
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400808void
809window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500811 cairo_surface_reference(surface);
812
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400813 if (window->cairo_surface != NULL)
814 cairo_surface_destroy(window->cairo_surface);
815
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500816 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400817}
818
Benjamin Franzke22d54812011-07-16 19:50:32 +0000819#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820static void
821window_resize_cairo_window_surface(struct window *window)
822{
823 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200824 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825
826 data = cairo_surface_get_user_data(window->cairo_surface,
827 &surface_data_key);
828
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200829 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 wl_egl_window_resize(data->window,
831 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200832 window->allocation.height,
833 x,y);
834
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 cairo_gl_surface_set_size(window->cairo_surface,
836 window->allocation.width,
837 window->allocation.height);
838}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000839#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400841void
842window_create_surface(struct window *window)
843{
844 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 uint32_t flags = 0;
846
847 if (!window->transparent)
848 flags = SURFACE_OPAQUE;
849
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500851#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
853 if (window->cairo_surface) {
854 window_resize_cairo_window_surface(window);
855 return;
856 }
857 surface = display_create_surface(window->display,
858 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400859 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500861 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400864 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865 break;
866#endif
867 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400868 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400869 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800871 default:
872 surface = NULL;
873 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400875
876 window_set_surface(window, surface);
877 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878}
879
880static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500881window_draw_menu(struct window *window)
882{
883 cairo_t *cr;
884 int width, height, r = 5;
885
886 window_create_surface(window);
887
888 cr = cairo_create(window->cairo_surface);
889 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
890 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
891 cairo_paint(cr);
892
893 width = window->allocation.width;
894 height = window->allocation.height;
895 rounded_rect(cr, r, r, width - r, height - r, r);
896 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
897 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
898 cairo_fill(cr);
899 cairo_destroy(cr);
900}
901
902static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500903window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500904{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500905 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500906 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400907 cairo_surface_t *frame;
908 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500909
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400910 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400911
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400912 width = window->allocation.width;
913 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500914
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500915 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500916
Kristian Høgsberg09531622010-06-14 23:22:15 -0400917 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400918 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400919 cairo_paint(cr);
920
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400921 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400922 tile_mask(cr, window->display->shadow,
923 shadow_dx, shadow_dy, width, height,
924 window->margin + 10 - shadow_dx,
925 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500926
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400927 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400928 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930 frame = window->display->inactive_frame;
931
932 tile_source(cr, frame, 0, 0, width, height,
933 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500934
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500935 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
936 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500937 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400938 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400939 if (window->keyboard_device)
940 cairo_set_source_rgb(cr, 0, 0, 0);
941 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400942 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400943 cairo_show_text(cr, window->title);
944
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500945 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400946
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100947 /* FIXME: this breakes gears, fix cairo? */
948#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400949 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100950#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500951}
952
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400953void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500954window_destroy(struct window *window)
955{
956 wl_surface_destroy(window->surface);
957 wl_list_remove(&window->link);
958 free(window);
959}
960
961void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400962display_flush_cairo_device(struct display *display)
963{
964 cairo_device_flush (display->device);
965}
966
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500967void
968window_draw(struct window *window)
969{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500970 if (window->parent)
971 window_draw_menu(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400972 else if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400973 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500974 else
975 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500976}
977
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400978cairo_surface_t *
979window_get_surface(struct window *window)
980{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400981 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400982}
983
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100984struct wl_surface *
985window_get_wl_surface(struct window *window)
986{
987 return window->surface;
988}
989
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400990static int
991get_pointer_location(struct window *window, int32_t x, int32_t y)
992{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400993 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400994 const int grip_size = 8;
995
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500996 if (!window->decoration)
997 return WINDOW_CLIENT_AREA;
998
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400999 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001000 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001001 else if (window->margin <= x && x < window->margin + grip_size)
1002 hlocation = WINDOW_RESIZING_LEFT;
1003 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001004 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001005 else if (x < window->allocation.width - window->margin)
1006 hlocation = WINDOW_RESIZING_RIGHT;
1007 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001008 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001009
1010 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001011 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001012 else if (window->margin <= y && y < window->margin + grip_size)
1013 vlocation = WINDOW_RESIZING_TOP;
1014 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001015 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001016 else if (y < window->allocation.height - window->margin)
1017 vlocation = WINDOW_RESIZING_BOTTOM;
1018 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001019 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001020
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001021 location = vlocation | hlocation;
1022 if (location & WINDOW_EXTERIOR)
1023 location = WINDOW_EXTERIOR;
1024 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1025 location = WINDOW_TITLEBAR;
1026 else if (location == WINDOW_INTERIOR)
1027 location = WINDOW_CLIENT_AREA;
1028
1029 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001030}
1031
1032static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001033set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001034{
1035 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001036 struct wl_buffer *buffer;
1037 cairo_surface_t *surface;
1038 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001039
1040 location = get_pointer_location(input->pointer_focus,
1041 input->sx, input->sy);
1042 switch (location) {
1043 case WINDOW_RESIZING_TOP:
1044 pointer = POINTER_TOP;
1045 break;
1046 case WINDOW_RESIZING_BOTTOM:
1047 pointer = POINTER_BOTTOM;
1048 break;
1049 case WINDOW_RESIZING_LEFT:
1050 pointer = POINTER_LEFT;
1051 break;
1052 case WINDOW_RESIZING_RIGHT:
1053 pointer = POINTER_RIGHT;
1054 break;
1055 case WINDOW_RESIZING_TOP_LEFT:
1056 pointer = POINTER_TOP_LEFT;
1057 break;
1058 case WINDOW_RESIZING_TOP_RIGHT:
1059 pointer = POINTER_TOP_RIGHT;
1060 break;
1061 case WINDOW_RESIZING_BOTTOM_LEFT:
1062 pointer = POINTER_BOTTOM_LEFT;
1063 break;
1064 case WINDOW_RESIZING_BOTTOM_RIGHT:
1065 pointer = POINTER_BOTTOM_RIGHT;
1066 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001067 case WINDOW_EXTERIOR:
1068 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001069 if (input->current_pointer_image == POINTER_DEFAULT)
1070 return;
1071
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001072 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001073 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001074 return;
1075 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001076 break;
1077 }
1078
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001079 if (pointer == input->current_pointer_image)
1080 return;
1081
1082 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001083 surface = display->pointer_surfaces[pointer];
1084 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001085 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001086 pointer_images[pointer].hotspot_x,
1087 pointer_images[pointer].hotspot_y);
1088}
1089
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001090static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001091window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001092 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001093 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001094{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001095 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001096 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001097 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001098
1099 input->x = x;
1100 input->y = y;
1101 input->sx = sx;
1102 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001103
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001104 if (window->motion_handler)
1105 pointer = (*window->motion_handler)(window, input, time,
1106 x, y, sx, sy,
1107 window->user_data);
1108
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001109 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001110}
1111
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001112static void
1113window_handle_button(void *data,
1114 struct wl_input_device *input_device,
1115 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001116{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001117 struct input *input = data;
1118 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001119 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001120
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001121 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001122
1123 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001126 wl_shell_move(window->display->shell,
1127 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001128 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001129 case WINDOW_RESIZING_TOP:
1130 case WINDOW_RESIZING_BOTTOM:
1131 case WINDOW_RESIZING_LEFT:
1132 case WINDOW_RESIZING_RIGHT:
1133 case WINDOW_RESIZING_TOP_LEFT:
1134 case WINDOW_RESIZING_TOP_RIGHT:
1135 case WINDOW_RESIZING_BOTTOM_LEFT:
1136 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001137 wl_shell_resize(window->display->shell,
1138 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001140 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001141 case WINDOW_CLIENT_AREA:
1142 if (window->button_handler)
1143 (*window->button_handler)(window,
1144 input, time,
1145 button, state,
1146 window->user_data);
1147 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001148 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001149 } else {
1150 if (window->button_handler)
1151 (*window->button_handler)(window,
1152 input, time,
1153 button, state,
1154 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001155 }
1156}
1157
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001158static void
1159window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001160 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001161{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001162 struct input *input = data;
1163 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001164 struct display *d = window->display;
1165 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001166
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001167 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001168 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001169 return;
1170
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001171 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001172 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001173 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1174 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001175
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001176 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1177
1178 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001179 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001180 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001181 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001182
1183 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001184 (*window->key_handler)(window, input, time, key, sym, state,
1185 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001186}
1187
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001188static void
1189window_handle_pointer_focus(void *data,
1190 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001191 uint32_t time, struct wl_surface *surface,
1192 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001193{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001194 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195 struct window *window;
1196 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001197
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001198 window = input->pointer_focus;
1199 if (window && window->surface != surface) {
1200 if (window->leave_handler)
1201 window->leave_handler(window, input,
1202 time, window->user_data);
1203 input->pointer_focus = NULL;
1204 input->current_pointer_image = POINTER_UNSET;
1205 }
1206
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001207 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001208 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001209 window = input->pointer_focus;
1210
Kristian Høgsberg59826582011-01-20 11:56:57 -05001211 input->x = x;
1212 input->y = y;
1213 input->sx = sx;
1214 input->sy = sy;
1215
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001217 if (window->enter_handler)
1218 pointer = window->enter_handler(window, input,
1219 time, sx, sy,
1220 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001221
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001222 set_pointer_image(input, time, pointer);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001223
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001224 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001225}
1226
1227static void
1228window_handle_keyboard_focus(void *data,
1229 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001230 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001231 struct wl_surface *surface,
1232 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001233{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001234 struct input *input = data;
1235 struct window *window = input->keyboard_focus;
1236 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001237 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001238
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001239 window = input->keyboard_focus;
1240 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001241 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001242 if (window->keyboard_focus_handler)
1243 (*window->keyboard_focus_handler)(window, NULL,
1244 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001245 }
1246
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001247 if (surface)
1248 input->keyboard_focus = wl_surface_get_user_data(surface);
1249 else
1250 input->keyboard_focus = NULL;
1251
1252 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001253 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001254 for (k = keys->data; k < end; k++)
1255 input->modifiers |= d->xkb->map->modmap[*k];
1256
1257 window = input->keyboard_focus;
1258 if (window) {
1259 window->keyboard_device = input;
1260 if (window->keyboard_focus_handler)
1261 (*window->keyboard_focus_handler)(window,
1262 window->keyboard_device,
1263 window->user_data);
1264 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001265}
1266
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001267static const struct wl_input_device_listener input_device_listener = {
1268 window_handle_motion,
1269 window_handle_button,
1270 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001271 window_handle_pointer_focus,
1272 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001273};
1274
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001275void
1276input_get_position(struct input *input, int32_t *x, int32_t *y)
1277{
1278 *x = input->sx;
1279 *y = input->sy;
1280}
1281
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001282struct wl_input_device *
1283input_get_input_device(struct input *input)
1284{
1285 return input->input_device;
1286}
1287
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001288uint32_t
1289input_get_modifiers(struct input *input)
1290{
1291 return input->modifiers;
1292}
1293
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001294struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001295window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001296{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001297 cairo_device_flush (window->display->device);
1298
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001299 return wl_shell_create_drag(window->display->shell);
1300}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001301
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001302void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001303window_move(struct window *window, struct input *input, uint32_t time)
1304{
1305 wl_shell_move(window->display->shell,
1306 window->surface, input->input_device, time);
1307}
1308
1309void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001310window_activate_drag(struct wl_drag *drag, struct window *window,
1311 struct input *input, uint32_t time)
1312{
1313 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001314}
1315
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001316static void
1317handle_configure(void *data, struct wl_shell *shell,
1318 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001319 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001320{
1321 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001322 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001323
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001324 /* FIXME: this is probably the wrong place to check for width
1325 * or height <= 0, but it prevents the compositor from crashing
1326 */
1327 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001328 return;
1329
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001330 window->resize_edges = edges;
1331
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001332 if (window->resize_handler) {
1333 child_width = width - 20 - window->margin * 2;
1334 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001335
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001336 (*window->resize_handler)(window,
1337 child_width, child_height,
1338 window->user_data);
1339 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001340 window->allocation.width = width;
1341 window->allocation.height = height;
1342
1343 if (window->redraw_handler)
1344 window_schedule_redraw(window);
1345 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001346}
1347
1348static const struct wl_shell_listener shell_listener = {
1349 handle_configure,
1350};
1351
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001352void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001353window_get_allocation(struct window *window,
1354 struct rectangle *allocation)
1355{
1356 *allocation = window->allocation;
1357}
1358
1359void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001360window_get_child_allocation(struct window *window,
1361 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001362{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001363 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001364 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001365 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001366 allocation->x = window->margin + 10;
1367 allocation->y = window->margin + 50;
1368 allocation->width =
1369 window->allocation.width - 20 - window->margin * 2;
1370 allocation->height =
1371 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001372 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001373}
1374
1375void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001376window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001377{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001378 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001379 window->allocation.x = 20 + window->margin;
1380 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001381 window->allocation.width = width + 20 + window->margin * 2;
1382 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001383 } else {
1384 window->allocation.x = 0;
1385 window->allocation.y = 0;
1386 window->allocation.width = width;
1387 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001388 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001389}
1390
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001391static gboolean
1392idle_redraw(void *data)
1393{
1394 struct window *window = data;
1395
1396 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001397
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001398 window->redraw_scheduled = 0;
1399
1400 return FALSE;
1401}
1402
1403void
1404window_schedule_redraw(struct window *window)
1405{
1406 if (!window->redraw_scheduled) {
1407 g_idle_add(idle_redraw, window);
1408 window->redraw_scheduled = 1;
1409 }
1410}
1411
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001412void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001413window_set_custom(struct window *window)
1414{
1415 window->type = TYPE_CUSTOM;
1416}
1417
1418void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001419window_set_fullscreen(struct window *window, int fullscreen)
1420{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001421 int32_t width, height;
1422
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001423 if (window->type == TYPE_FULLSCREEN)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001424 return;
1425
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001426 if (fullscreen) {
1427 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001428 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001429 width = window->display->screen_allocation.width;
1430 height = window->display->screen_allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001431 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001432 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001433 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001434 width = window->saved_allocation.width - 20 - window->margin * 2;
1435 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001436 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001437 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001438
1439 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001440}
1441
1442void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001443window_set_decoration(struct window *window, int decoration)
1444{
1445 window->decoration = decoration;
1446}
1447
1448void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001449window_set_user_data(struct window *window, void *data)
1450{
1451 window->user_data = data;
1452}
1453
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001454void *
1455window_get_user_data(struct window *window)
1456{
1457 return window->user_data;
1458}
1459
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001460void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001461window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001462 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001463{
1464 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001465}
1466
1467void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001468window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001469 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001470{
1471 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001472}
1473
1474void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001475window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001476 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001477{
1478 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001479}
1480
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001481void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001482window_set_button_handler(struct window *window,
1483 window_button_handler_t handler)
1484{
1485 window->button_handler = handler;
1486}
1487
1488void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001489window_set_motion_handler(struct window *window,
1490 window_motion_handler_t handler)
1491{
1492 window->motion_handler = handler;
1493}
1494
1495void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001496window_set_enter_handler(struct window *window,
1497 window_enter_handler_t handler)
1498{
1499 window->enter_handler = handler;
1500}
1501
1502void
1503window_set_leave_handler(struct window *window,
1504 window_leave_handler_t handler)
1505{
1506 window->leave_handler = handler;
1507}
1508
1509void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001510window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001511 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001512{
1513 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001514}
1515
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001516void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001517window_set_transparent(struct window *window, int transparent)
1518{
1519 window->transparent = transparent;
1520}
1521
1522void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001523window_set_title(struct window *window, const char *title)
1524{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001525 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001526 window->title = strdup(title);
1527}
1528
1529const char *
1530window_get_title(struct window *window)
1531{
1532 return window->title;
1533}
1534
1535void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001536display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1537 int32_t x, int32_t y, int32_t width, int32_t height)
1538{
1539 struct wl_buffer *buffer;
1540
1541 buffer = display_get_buffer_for_surface(display, cairo_surface);
1542
1543 wl_buffer_damage(buffer, x, y, width, height);
1544}
1545
1546void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001547window_damage(struct window *window, int32_t x, int32_t y,
1548 int32_t width, int32_t height)
1549{
1550 wl_surface_damage(window->surface, x, y, width, height);
1551}
1552
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001553static struct window *
1554window_create_internal(struct display *display, struct window *parent,
1555 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001556{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001557 struct window *window;
1558
1559 window = malloc(sizeof *window);
1560 if (window == NULL)
1561 return NULL;
1562
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001563 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001564 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001565 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001566 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001567 window->allocation.x = 0;
1568 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001569 window->allocation.width = width;
1570 window->allocation.height = height;
1571 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001572 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001573 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001574 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001575
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001576 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001577#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001578 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1579 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001580#else
1581 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1582#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001583 else
1584 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001585
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001586 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001587 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001588
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001589 return window;
1590}
1591
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001592struct window *
1593window_create(struct display *display, int32_t width, int32_t height)
1594{
1595 struct window *window;
1596
1597 window = window_create_internal(display, NULL, width, height);
1598 if (!window)
1599 return NULL;
1600
1601 return window;
1602}
1603
1604struct window *
1605window_create_transient(struct display *display, struct window *parent,
1606 int32_t x, int32_t y, int32_t width, int32_t height)
1607{
1608 struct window *window;
1609
1610 window = window_create_internal(parent->display,
1611 parent, width, height);
1612 if (!window)
1613 return NULL;
1614
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001615 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001616 window->x = x;
1617 window->y = y;
1618
1619 return window;
1620}
1621
1622void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001623window_set_buffer_type(struct window *window, enum window_buffer_type type)
1624{
1625 window->buffer_type = type;
1626}
1627
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001628
1629static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001630display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001631 struct wl_output *wl_output,
1632 int x, int y,
1633 int physical_width,
1634 int physical_height,
1635 int subpixel,
1636 const char *make,
1637 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001638{
1639 struct display *display = data;
1640
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001641 display->screen_allocation.x = x;
1642 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001643}
1644
1645static void
1646display_handle_mode(void *data,
1647 struct wl_output *wl_output,
1648 uint32_t flags,
1649 int width,
1650 int height,
1651 int refresh)
1652{
1653 struct display *display = data;
1654
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001655 display->screen_allocation.width = width;
1656 display->screen_allocation.height = height;
1657}
1658
1659static const struct wl_output_listener output_listener = {
1660 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001661 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001662};
1663
1664static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001665display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001666{
1667 struct input *input;
1668
1669 input = malloc(sizeof *input);
1670 if (input == NULL)
1671 return;
1672
1673 memset(input, 0, sizeof *input);
1674 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001675 input->input_device =
1676 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001677 input->pointer_focus = NULL;
1678 input->keyboard_focus = NULL;
1679 wl_list_insert(d->input_list.prev, &input->link);
1680
1681 wl_input_device_add_listener(input->input_device,
1682 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001683 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001684}
1685
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001686struct selection_offer {
1687 struct display *display;
1688 struct wl_selection_offer *offer;
1689 struct wl_array types;
1690 struct input *input;
1691};
1692
1693int
1694input_offers_mime_type(struct input *input, const char *type)
1695{
1696 struct selection_offer *offer = input->offer;
1697 char **p, **end;
1698
1699 if (offer == NULL)
1700 return 0;
1701
1702 end = offer->types.data + offer->types.size;
1703 for (p = offer->types.data; p < end; p++)
1704 if (strcmp(*p, type) == 0)
1705 return 1;
1706
1707 return 0;
1708}
1709
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001710void
1711input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001712{
1713 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001714
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001715 /* FIXME: A number of things can go wrong here: the object may
1716 * not be the current selection offer any more (which could
1717 * still work, but the source may have gone away or just
1718 * destroyed its wl_selection) or the offer may not have the
1719 * requested type after all (programmer/client error,
1720 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001721 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001722}
1723
1724static void
1725selection_offer_offer(void *data,
1726 struct wl_selection_offer *selection_offer,
1727 const char *type)
1728{
1729 struct selection_offer *offer = data;
1730
1731 char **p;
1732
1733 p = wl_array_add(&offer->types, sizeof *p);
1734 if (p)
1735 *p = strdup(type);
1736};
1737
1738static void
1739selection_offer_keyboard_focus(void *data,
1740 struct wl_selection_offer *selection_offer,
1741 struct wl_input_device *input_device)
1742{
1743 struct selection_offer *offer = data;
1744 struct input *input;
1745 char **p, **end;
1746
1747 if (input_device == NULL) {
1748 printf("selection offer retracted %p\n", selection_offer);
1749 input = offer->input;
1750 input->offer = NULL;
1751 wl_selection_offer_destroy(selection_offer);
1752 wl_array_release(&offer->types);
1753 free(offer);
1754 return;
1755 }
1756
1757 input = wl_input_device_get_user_data(input_device);
1758 printf("new selection offer %p:", selection_offer);
1759
1760 offer->input = input;
1761 input->offer = offer;
1762 end = offer->types.data + offer->types.size;
1763 for (p = offer->types.data; p < end; p++)
1764 printf(" %s", *p);
1765
1766 printf("\n");
1767}
1768
1769struct wl_selection_offer_listener selection_offer_listener = {
1770 selection_offer_offer,
1771 selection_offer_keyboard_focus
1772};
1773
1774static void
1775add_selection_offer(struct display *d, uint32_t id)
1776{
1777 struct selection_offer *offer;
1778
1779 offer = malloc(sizeof *offer);
1780 if (offer == NULL)
1781 return;
1782
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001783 offer->offer =
1784 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001785 offer->display = d;
1786 wl_array_init(&offer->types);
1787 offer->input = NULL;
1788
1789 wl_selection_offer_add_listener(offer->offer,
1790 &selection_offer_listener, offer);
1791}
1792
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001793static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001794display_handle_global(struct wl_display *display, uint32_t id,
1795 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001796{
1797 struct display *d = data;
1798
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001799 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001800 d->compositor =
1801 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001802 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001803 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001804 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001805 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001806 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001807 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001808 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001809 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001810 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001811 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001812 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001813 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001814 }
1815}
1816
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001817static void
1818display_render_frame(struct display *d)
1819{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001820 int radius = 8;
1821 cairo_t *cr;
1822
1823 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1824 cr = cairo_create(d->shadow);
1825 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1826 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1827 rounded_rect(cr, 16, 16, 112, 112, radius);
1828 cairo_fill(cr);
1829 cairo_destroy(cr);
1830 blur_surface(d->shadow, 64);
1831
1832 d->active_frame =
1833 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1834 cr = cairo_create(d->active_frame);
1835 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1836 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1837 rounded_rect(cr, 16, 16, 112, 112, radius);
1838 cairo_fill(cr);
1839 cairo_destroy(cr);
1840
1841 d->inactive_frame =
1842 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1843 cr = cairo_create(d->inactive_frame);
1844 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1845 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1846 rounded_rect(cr, 16, 16, 112, 112, radius);
1847 cairo_fill(cr);
1848 cairo_destroy(cr);
1849}
1850
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001851static void
1852init_xkb(struct display *d)
1853{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001854 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001855
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001856 names.rules = "evdev";
1857 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001858 names.layout = option_xkb_layout;
1859 names.variant = option_xkb_variant;
1860 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001861
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001862 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001863 if (!d->xkb) {
1864 fprintf(stderr, "Failed to compile keymap\n");
1865 exit(1);
1866 }
1867}
1868
Yuval Fledel45568f62010-12-06 09:18:12 -05001869static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001870init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001871{
1872 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001873 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001874
1875 static const EGLint premul_argb_cfg_attribs[] = {
1876 EGL_SURFACE_TYPE,
1877 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1878 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001879 EGL_RED_SIZE, 1,
1880 EGL_GREEN_SIZE, 1,
1881 EGL_BLUE_SIZE, 1,
1882 EGL_ALPHA_SIZE, 1,
1883 EGL_DEPTH_SIZE, 1,
1884 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1885 EGL_NONE
1886 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001887
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001888 static const EGLint rgb_cfg_attribs[] = {
1889 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1890 EGL_RED_SIZE, 1,
1891 EGL_GREEN_SIZE, 1,
1892 EGL_BLUE_SIZE, 1,
1893 EGL_ALPHA_SIZE, 0,
1894 EGL_DEPTH_SIZE, 1,
1895 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1896 EGL_NONE
1897 };
1898
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001899 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001900 if (!eglInitialize(d->dpy, &major, &minor)) {
1901 fprintf(stderr, "failed to initialize display\n");
1902 return -1;
1903 }
1904
1905 if (!eglBindAPI(EGL_OPENGL_API)) {
1906 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1907 return -1;
1908 }
1909
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001910 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02001911 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001912 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001913 return -1;
1914 }
1915
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001916 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
1917 &d->rgb_config, 1, &n) || n != 1) {
1918 fprintf(stderr, "failed to choose rgb config\n");
1919 return -1;
1920 }
1921
1922 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001923 if (d->ctx == NULL) {
1924 fprintf(stderr, "failed to create context\n");
1925 return -1;
1926 }
1927
1928 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001929 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001930 return -1;
1931 }
1932
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001933#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001934 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001935 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001936 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001937 return -1;
1938 }
1939#endif
1940
1941 return 0;
1942}
1943
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001944struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04001945display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001946{
1947 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001948 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001949 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001950 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001951
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001952 g_type_init();
1953
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001954 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001955 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001956 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001957
1958 xkb_option_group = g_option_group_new("xkb",
1959 "Keyboard options",
1960 "Show all XKB options",
1961 NULL, NULL);
1962 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1963 g_option_context_add_group (context, xkb_option_group);
1964
1965 if (!g_option_context_parse(context, argc, argv, &error)) {
1966 fprintf(stderr, "option parsing failed: %s\n", error->message);
1967 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001968 }
1969
Tim Wiederhake748f6722011-01-23 23:25:25 +01001970 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001971
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001972 d = malloc(sizeof *d);
1973 if (d == NULL)
1974 return NULL;
1975
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001976 memset(d, 0, sizeof *d);
1977
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001978 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001979 if (d->display == NULL) {
1980 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001981 return NULL;
1982 }
1983
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001984 wl_list_init(&d->input_list);
1985
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001986 /* Set up listener so we'll catch all events. */
1987 wl_display_add_global_listener(d->display,
1988 display_handle_global, d);
1989
1990 /* Process connection events. */
1991 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001992 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001993 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001994
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001995 d->image_target_texture_2d =
1996 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1997 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1998 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1999
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002000 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002001
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002002 display_render_frame(d);
2003
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002004 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002005 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002006 g_source_attach(d->source, NULL);
2007
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002008 wl_list_init(&d->window_list);
2009
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002010 init_xkb(d);
2011
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002012 return d;
2013}
2014
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002015struct wl_display *
2016display_get_display(struct display *display)
2017{
2018 return display->display;
2019}
2020
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002021struct wl_compositor *
2022display_get_compositor(struct display *display)
2023{
2024 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002025}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002026
2027EGLDisplay
2028display_get_egl_display(struct display *d)
2029{
2030 return d->dpy;
2031}
2032
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002033EGLConfig
2034display_get_egl_config(struct display *d)
2035{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002036 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002037}
2038
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002039struct wl_shell *
2040display_get_shell(struct display *display)
2041{
2042 return display->shell;
2043}
2044
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002045void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002046display_acquire_window_surface(struct display *display,
2047 struct window *window,
2048 EGLContext ctx)
2049{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002050#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002051 struct egl_window_surface_data *data;
2052
2053 if (!window->cairo_surface)
2054 return;
2055
2056 if (!ctx)
2057 ctx = display->ctx;
2058
2059 data = cairo_surface_get_user_data(window->cairo_surface,
2060 &surface_data_key);
2061
2062 cairo_device_acquire(display->device);
2063 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2064 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002065#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002066}
2067
2068void
2069display_release(struct display *display)
2070{
2071 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2072 fprintf(stderr, "failed to make context current\n");
2073 cairo_device_release(display->device);
2074}
2075
2076void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002077display_run(struct display *d)
2078{
2079 g_main_loop_run(d->loop);
2080}