blob: 59ea9754b8dcc70dffd4de2de3a1374bf2528cff [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øgsberg640609a2010-08-09 22:11:47 -040037#include <xf86drm.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040039
40#define EGL_EGLEXT_PROTOTYPES 1
41#define GL_GLEXT_PROTOTYPES 1
42#include <GL/gl.h>
43#include <EGL/egl.h>
44#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040045
Kristian Høgsberg8def2642011-01-14 17:41:33 -050046#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040047#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040048#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050049
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040050#include <X11/extensions/XKBcommon.h>
51
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050052#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050053#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050054#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050055#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -040064 struct wl_drm *drm;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040065 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040068 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040069 EGLDisplay dpy;
70 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010071 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040073 GMainLoop *loop;
74 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040075 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040076 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040077 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040078 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040079 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040080 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040081
Kristian Høgsbergb46df052011-01-18 09:17:57 -050082 display_global_handler_t global_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083};
84
85struct window {
86 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050087 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050089 const char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050090 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050091 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040092 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040093 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050094 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050095 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050096 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040097 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040098 struct input *grab_device;
99 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400101 enum window_buffer_type buffer_type;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500102 int mapped;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500103
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400104 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500105 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500106
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500107 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400108 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500109 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500111 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400112 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400113
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500114 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400115 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116};
117
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400118struct input {
119 struct display *display;
120 struct wl_input_device *input_device;
121 struct window *pointer_focus;
122 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500123 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400124 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 uint32_t modifiers;
126 int32_t x, y, sx, sy;
127 struct wl_list link;
128};
129
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400130enum {
131 POINTER_DEFAULT = 100,
132 POINTER_UNSET
133};
134
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500135enum window_location {
136 WINDOW_INTERIOR = 0,
137 WINDOW_RESIZING_TOP = 1,
138 WINDOW_RESIZING_BOTTOM = 2,
139 WINDOW_RESIZING_LEFT = 4,
140 WINDOW_RESIZING_TOP_LEFT = 5,
141 WINDOW_RESIZING_BOTTOM_LEFT = 6,
142 WINDOW_RESIZING_RIGHT = 8,
143 WINDOW_RESIZING_TOP_RIGHT = 9,
144 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
145 WINDOW_RESIZING_MASK = 15,
146 WINDOW_EXTERIOR = 16,
147 WINDOW_TITLEBAR = 17,
148 WINDOW_CLIENT_AREA = 18,
149};
150
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400151const char *option_xkb_layout = "us";
152const char *option_xkb_variant = "";
153const char *option_xkb_options = "";
154
155static const GOptionEntry xkb_option_entries[] = {
156 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
157 &option_xkb_layout, "XKB Layout" },
158 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_variant, "XKB Variant" },
160 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_options, "XKB Options" },
162 { NULL }
163};
164
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500165static void
166rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
167{
168 cairo_move_to(cr, x0, y0 + radius);
169 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
170 cairo_line_to(cr, x1 - radius, y0);
171 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
172 cairo_line_to(cr, x1, y1 - radius);
173 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
174 cairo_line_to(cr, x0 + radius, y1);
175 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
176 cairo_close_path(cr);
177}
178
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400179static const cairo_user_data_key_t surface_data_key;
180struct surface_data {
181 struct wl_buffer *buffer;
182};
183
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500184#define MULT(_d,c,a,t) \
185 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
186
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500187#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400188
189struct drm_surface_data {
190 struct surface_data data;
191 EGLImageKHR image;
192 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800193 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400194};
195
196static void
197drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400198{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400199 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800200 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400201
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800202 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400203 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800204 cairo_device_release(d->device);
205
206 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400207 wl_buffer_destroy(data->data.buffer);
208}
209
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500210EGLImageKHR
211display_get_image_for_drm_surface(struct display *display,
212 cairo_surface_t *surface)
213{
214 struct drm_surface_data *data;
215
216 data = cairo_surface_get_user_data (surface, &surface_data_key);
217
218 return data->image;
219}
220
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500221static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400222display_create_drm_surface(struct display *display,
223 struct rectangle *rectangle)
224{
225 struct drm_surface_data *data;
226 EGLDisplay dpy = display->dpy;
227 cairo_surface_t *surface;
228 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400229 EGLint name, stride;
230
231 EGLint image_attribs[] = {
232 EGL_WIDTH, 0,
233 EGL_HEIGHT, 0,
234 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
235 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
236 EGL_NONE
237 };
238
239 data = malloc(sizeof *data);
240 if (data == NULL)
241 return NULL;
242
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800243 data->display = display;
244
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400245 image_attribs[1] = rectangle->width;
246 image_attribs[3] = rectangle->height;
247 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800248
249 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400250 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251 glBindTexture(GL_TEXTURE_2D, data->texture);
252 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800253 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400254
255 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
256
257 visual = wl_display_get_premultiplied_argb_visual(display->display);
258 data->data.buffer =
259 wl_drm_create_buffer(display->drm, name, rectangle->width,
260 rectangle->height, stride, visual);
261
262 surface = cairo_gl_surface_create_for_texture(display->device,
263 CAIRO_CONTENT_COLOR_ALPHA,
264 data->texture,
265 rectangle->width,
266 rectangle->height);
267
268 cairo_surface_set_user_data (surface, &surface_data_key,
269 data, drm_surface_data_destroy);
270
271 return surface;
272}
273
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500274static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275display_create_drm_surface_from_file(struct display *display,
276 const char *filename,
277 struct rectangle *rect)
278{
279 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400280 GdkPixbuf *pixbuf;
281 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400282 int stride, i;
283 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800284 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285
286 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400287 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400288 FALSE, &error);
289 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400291
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400292 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
293 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
294 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400295 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400296 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400297
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400298
299 stride = gdk_pixbuf_get_rowstride(pixbuf);
300 pixels = gdk_pixbuf_get_pixels(pixbuf);
301
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400302 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400303 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400304 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400305 while (p < end) {
306 unsigned int t;
307
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400308 MULT(p[0], p[0], p[3], t);
309 MULT(p[1], p[1], p[3], t);
310 MULT(p[2], p[2], p[3], t);
311 p += 4;
312
313 }
314 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400315
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 data = cairo_surface_get_user_data(surface, &surface_data_key);
318
319 cairo_device_acquire(display->device);
320 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800321 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
322 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400324
325 gdk_pixbuf_unref(pixbuf);
326
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327 return surface;
328}
329
330#endif
331
332struct wl_buffer *
333display_get_buffer_for_surface(struct display *display,
334 cairo_surface_t *surface)
335{
336 struct surface_data *data;
337
338 data = cairo_surface_get_user_data (surface, &surface_data_key);
339
340 return data->buffer;
341}
342
343struct shm_surface_data {
344 struct surface_data data;
345 void *map;
346 size_t length;
347};
348
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500349static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350shm_surface_data_destroy(void *p)
351{
352 struct shm_surface_data *data = p;
353
354 wl_buffer_destroy(data->data.buffer);
355 munmap(data->map, data->length);
356}
357
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500358static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359display_create_shm_surface(struct display *display,
360 struct rectangle *rectangle)
361{
362 struct shm_surface_data *data;
363 cairo_surface_t *surface;
364 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800365 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400366 char filename[] = "/tmp/wayland-shm-XXXXXX";
367
368 data = malloc(sizeof *data);
369 if (data == NULL)
370 return NULL;
371
372 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
373 rectangle->width);
374 data->length = stride * rectangle->height;
375 fd = mkstemp(filename);
376 if (fd < 0) {
377 fprintf(stderr, "open %s failed: %m", filename);
378 return NULL;
379 }
380 if (ftruncate(fd, data->length) < 0) {
381 fprintf(stderr, "ftruncate failed: %m");
382 close(fd);
383 return NULL;
384 }
385
386 data->map = mmap(NULL, data->length,
387 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
388 unlink(filename);
389
390 if (data->map == MAP_FAILED) {
391 fprintf(stderr, "mmap failed: %m");
392 close(fd);
393 return NULL;
394 }
395
396 surface = cairo_image_surface_create_for_data (data->map,
397 CAIRO_FORMAT_ARGB32,
398 rectangle->width,
399 rectangle->height,
400 stride);
401
402 cairo_surface_set_user_data (surface, &surface_data_key,
403 data, shm_surface_data_destroy);
404
405 visual = wl_display_get_premultiplied_argb_visual(display->display);
406 data->data.buffer = wl_shm_create_buffer(display->shm,
407 fd,
408 rectangle->width,
409 rectangle->height,
410 stride, visual);
411
412 close(fd);
413
414 return surface;
415}
416
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500417static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418display_create_shm_surface_from_file(struct display *display,
419 const char *filename,
420 struct rectangle *rect)
421{
422 cairo_surface_t *surface;
423 GdkPixbuf *pixbuf;
424 GError *error = NULL;
425 int stride, i;
426 unsigned char *pixels, *p, *end, *dest_data;
427 int dest_stride;
428 uint32_t *d;
429
430 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
431 rect->width, rect->height,
432 FALSE, &error);
433 if (error != NULL)
434 return NULL;
435
436 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
437 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
438 gdk_pixbuf_unref(pixbuf);
439 return NULL;
440 }
441
442 stride = gdk_pixbuf_get_rowstride(pixbuf);
443 pixels = gdk_pixbuf_get_pixels(pixbuf);
444
445 surface = display_create_shm_surface(display, rect);
446 dest_data = cairo_image_surface_get_data (surface);
447 dest_stride = cairo_image_surface_get_stride (surface);
448
449 for (i = 0; i < rect->height; i++) {
450 d = (uint32_t *) (dest_data + i * dest_stride);
451 p = pixels + i * stride;
452 end = p + rect->width * 4;
453 while (p < end) {
454 unsigned int t;
455 unsigned char a, r, g, b;
456
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457 a = p[3];
458 MULT(r, p[0], a, t);
459 MULT(g, p[1], a, t);
460 MULT(b, p[2], a, t);
461 p += 4;
462 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
463 }
464 }
465
466 gdk_pixbuf_unref(pixbuf);
467
468 return surface;
469}
470
471cairo_surface_t *
472display_create_surface(struct display *display,
473 struct rectangle *rectangle)
474{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500475#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500476 if (display->drm) {
477 return display_create_drm_surface(display, rectangle);
478 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500480 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400481}
482
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500483static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400484display_create_surface_from_file(struct display *display,
485 const char *filename,
486 struct rectangle *rectangle)
487{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500488#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500489 if (display->drm) {
490 return display_create_drm_surface_from_file(display, filename, rectangle);
491 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500493 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494}
Yuval Fledel45568f62010-12-06 09:18:12 -0500495 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400496 const char *filename;
497 int hotspot_x, hotspot_y;
498} pointer_images[] = {
499 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
500 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
501 { DATADIR "/wayland/bottom_side.png", 16, 20 },
502 { DATADIR "/wayland/grabbing.png", 20, 17 },
503 { DATADIR "/wayland/left_ptr.png", 10, 5 },
504 { DATADIR "/wayland/left_side.png", 10, 20 },
505 { DATADIR "/wayland/right_side.png", 30, 19 },
506 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
507 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
508 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400509 { DATADIR "/wayland/xterm.png", 15, 15 },
510 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400511};
512
513static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400514create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400515{
516 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400517 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400518 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400519
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400520 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400521 display->pointer_surfaces =
522 malloc(count * sizeof *display->pointer_surfaces);
523 rect.width = width;
524 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400525 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400526 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527 display_create_surface_from_file(display,
528 pointer_images[i].filename,
529 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400530 }
531
532}
533
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400534cairo_surface_t *
535display_get_pointer_surface(struct display *display, int pointer,
536 int *width, int *height,
537 int *hotspot_x, int *hotspot_y)
538{
539 cairo_surface_t *surface;
540
541 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500542#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400543 *width = cairo_gl_surface_get_width(surface);
544 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000545#else
546 *width = cairo_image_surface_get_width(surface);
547 *height = cairo_image_surface_get_height(surface);
548#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400549 *hotspot_x = pointer_images[pointer].hotspot_x;
550 *hotspot_y = pointer_images[pointer].hotspot_y;
551
552 return cairo_surface_reference(surface);
553}
554
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400555
556static void
557window_attach_surface(struct window *window);
558
559static void
560free_surface(void *data)
561{
562 struct window *window = data;
563
564 cairo_surface_destroy(window->pending_surface);
565 window->pending_surface = NULL;
566 if (window->cairo_surface)
567 window_attach_surface(window);
568}
569
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500570static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500571window_attach_surface(struct window *window)
572{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400573 struct display *display = window->display;
574 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500575 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500576
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500577 if (window->pending_surface != NULL)
578 return;
579
Kristian Høgsberg09531622010-06-14 23:22:15 -0400580 window->pending_surface = window->cairo_surface;
581 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500582
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400583 buffer = display_get_buffer_for_surface(display,
584 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500585 if (window->resize_edges & WINDOW_RESIZING_LEFT)
586 x = window->server_allocation.width -
587 window->allocation.width;
588 else
589 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500590
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500591 if (window->resize_edges & WINDOW_RESIZING_TOP)
592 y = window->server_allocation.height -
593 window->allocation.height;
594 else
595 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400596
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500597 window->server_allocation = window->allocation;
598 window->resize_edges = 0;
599 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400600 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500601
602 if (!window->mapped) {
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500603 if (!window->parent)
604 wl_surface_map_toplevel(window->surface);
605 else
606 wl_surface_map_transient(window->surface,
607 window->parent->surface,
608 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500609 window->mapped = 1;
610 }
611
612 wl_surface_damage(window->surface, 0, 0,
613 window->allocation.width,
614 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500615}
616
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500617void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400618window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500619{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400620 if (window->cairo_surface)
621 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500622}
623
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400624void
625window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500627 cairo_surface_reference(surface);
628
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400629 if (window->cairo_surface != NULL)
630 cairo_surface_destroy(window->cairo_surface);
631
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500632 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400633}
634
635void
636window_create_surface(struct window *window)
637{
638 cairo_surface_t *surface;
639
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400643 surface = display_create_surface(window->display,
644 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645 break;
646#endif
647 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400648 surface = display_create_shm_surface(window->display,
649 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800651 default:
652 surface = NULL;
653 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400655
656 window_set_surface(window, surface);
657 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658}
659
660static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500661window_draw_menu(struct window *window)
662{
663 cairo_t *cr;
664 int width, height, r = 5;
665
666 window_create_surface(window);
667
668 cr = cairo_create(window->cairo_surface);
669 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
670 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
671 cairo_paint(cr);
672
673 width = window->allocation.width;
674 height = window->allocation.height;
675 rounded_rect(cr, r, r, width - r, height - r, r);
676 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
677 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
678 cairo_fill(cr);
679 cairo_destroy(cr);
680}
681
682static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500683window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500684{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500685 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500686 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400687 cairo_surface_t *frame;
688 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500689
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400690 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400692 width = window->allocation.width;
693 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500694
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500695 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500696
Kristian Høgsberg09531622010-06-14 23:22:15 -0400697 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400698 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400699 cairo_paint(cr);
700
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400701 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 tile_mask(cr, window->display->shadow,
703 shadow_dx, shadow_dy, width, height,
704 window->margin + 10 - shadow_dx,
705 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500706
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400707 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400709 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400710 frame = window->display->inactive_frame;
711
712 tile_source(cr, frame, 0, 0, width, height,
713 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500714
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500715 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
716 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500717 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400718 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400719 if (window->keyboard_device)
720 cairo_set_source_rgb(cr, 0, 0, 0);
721 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400722 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400723 cairo_show_text(cr, window->title);
724
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500725 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400726
727 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500728}
729
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400730void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500731window_destroy(struct window *window)
732{
733 wl_surface_destroy(window->surface);
734 wl_list_remove(&window->link);
735 free(window);
736}
737
738void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400739display_flush_cairo_device(struct display *display)
740{
741 cairo_device_flush (display->device);
742}
743
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500744void
745window_draw(struct window *window)
746{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500747 if (window->parent)
748 window_draw_menu(window);
749 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400750 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500751 else
752 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500753}
754
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400755cairo_surface_t *
756window_get_surface(struct window *window)
757{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400758 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400759}
760
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400761static int
762get_pointer_location(struct window *window, int32_t x, int32_t y)
763{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400764 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400765 const int grip_size = 8;
766
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500767 if (!window->decoration)
768 return WINDOW_CLIENT_AREA;
769
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400770 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400771 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400772 else if (window->margin <= x && x < window->margin + grip_size)
773 hlocation = WINDOW_RESIZING_LEFT;
774 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400775 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400776 else if (x < window->allocation.width - window->margin)
777 hlocation = WINDOW_RESIZING_RIGHT;
778 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400779 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400780
781 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400782 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400783 else if (window->margin <= y && y < window->margin + grip_size)
784 vlocation = WINDOW_RESIZING_TOP;
785 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400786 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400787 else if (y < window->allocation.height - window->margin)
788 vlocation = WINDOW_RESIZING_BOTTOM;
789 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400790 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400791
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400792 location = vlocation | hlocation;
793 if (location & WINDOW_EXTERIOR)
794 location = WINDOW_EXTERIOR;
795 if (location == WINDOW_INTERIOR && y < window->margin + 50)
796 location = WINDOW_TITLEBAR;
797 else if (location == WINDOW_INTERIOR)
798 location = WINDOW_CLIENT_AREA;
799
800 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400801}
802
803static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400804set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400805{
806 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400807 struct wl_buffer *buffer;
808 cairo_surface_t *surface;
809 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400810
811 location = get_pointer_location(input->pointer_focus,
812 input->sx, input->sy);
813 switch (location) {
814 case WINDOW_RESIZING_TOP:
815 pointer = POINTER_TOP;
816 break;
817 case WINDOW_RESIZING_BOTTOM:
818 pointer = POINTER_BOTTOM;
819 break;
820 case WINDOW_RESIZING_LEFT:
821 pointer = POINTER_LEFT;
822 break;
823 case WINDOW_RESIZING_RIGHT:
824 pointer = POINTER_RIGHT;
825 break;
826 case WINDOW_RESIZING_TOP_LEFT:
827 pointer = POINTER_TOP_LEFT;
828 break;
829 case WINDOW_RESIZING_TOP_RIGHT:
830 pointer = POINTER_TOP_RIGHT;
831 break;
832 case WINDOW_RESIZING_BOTTOM_LEFT:
833 pointer = POINTER_BOTTOM_LEFT;
834 break;
835 case WINDOW_RESIZING_BOTTOM_RIGHT:
836 pointer = POINTER_BOTTOM_RIGHT;
837 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400838 case WINDOW_EXTERIOR:
839 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400840 if (input->current_pointer_image == POINTER_DEFAULT)
841 return;
842
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400843 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400844 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400845 return;
846 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400847 break;
848 }
849
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400850 if (pointer == input->current_pointer_image)
851 return;
852
853 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400854 surface = display->pointer_surfaces[pointer];
855 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400856 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400857 pointer_images[pointer].hotspot_x,
858 pointer_images[pointer].hotspot_y);
859}
860
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500861static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500862window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400863 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500864 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500865{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400866 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400867 struct window *window = input->pointer_focus;
868 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400869
870 input->x = x;
871 input->y = y;
872 input->sx = sx;
873 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400874
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400875 location = get_pointer_location(window, input->sx, input->sy);
876
877 if (window->motion_handler)
878 pointer = (*window->motion_handler)(window, input, time,
879 x, y, sx, sy,
880 window->user_data);
881
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400882 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500883}
884
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400885static void
886window_handle_button(void *data,
887 struct wl_input_device *input_device,
888 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500889{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400890 struct input *input = data;
891 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400892 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400893
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400894 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500895
896 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400897 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400898 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400899 wl_shell_move(window->display->shell,
900 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500901 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400902 case WINDOW_RESIZING_TOP:
903 case WINDOW_RESIZING_BOTTOM:
904 case WINDOW_RESIZING_LEFT:
905 case WINDOW_RESIZING_RIGHT:
906 case WINDOW_RESIZING_TOP_LEFT:
907 case WINDOW_RESIZING_TOP_RIGHT:
908 case WINDOW_RESIZING_BOTTOM_LEFT:
909 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400910 wl_shell_resize(window->display->shell,
911 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400912 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500913 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400914 case WINDOW_CLIENT_AREA:
915 if (window->button_handler)
916 (*window->button_handler)(window,
917 input, time,
918 button, state,
919 window->user_data);
920 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500921 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400922 } else {
923 if (window->button_handler)
924 (*window->button_handler)(window,
925 input, time,
926 button, state,
927 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500928 }
929}
930
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500931static void
932window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400933 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500934{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400935 struct input *input = data;
936 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400937 struct display *d = window->display;
938 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500939
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400940 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400941 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500942 return;
943
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400944 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500945 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400946 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
947 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500948
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400949 sym = XkbKeySymEntry(d->xkb, code, level, 0);
950
951 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400952 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400953 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400954 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500955
956 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500957 (*window->key_handler)(window, input, time, key, sym, state,
958 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500959}
960
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500961static void
962window_handle_pointer_focus(void *data,
963 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400964 uint32_t time, struct wl_surface *surface,
965 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500966{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400967 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400968 struct window *window;
969 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400970
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400971 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400972 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400973 window = input->pointer_focus;
974
Kristian Høgsberg59826582011-01-20 11:56:57 -0500975 input->x = x;
976 input->y = y;
977 input->sx = sx;
978 input->sy = sy;
979
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400980 pointer = POINTER_LEFT_PTR;
981 if (window->motion_handler)
982 pointer = (*window->motion_handler)(window,
983 input, time,
984 x, y, sx, sy,
985 window->user_data);
986
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400987 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400988 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400989 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400990 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400991 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500992}
993
994static void
995window_handle_keyboard_focus(void *data,
996 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400997 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500998 struct wl_surface *surface,
999 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001000{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001001 struct input *input = data;
1002 struct window *window = input->keyboard_focus;
1003 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001004 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001005
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001006 window = input->keyboard_focus;
1007 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001008 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001009 if (window->keyboard_focus_handler)
1010 (*window->keyboard_focus_handler)(window, NULL,
1011 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001012 }
1013
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001014 if (surface)
1015 input->keyboard_focus = wl_surface_get_user_data(surface);
1016 else
1017 input->keyboard_focus = NULL;
1018
1019 end = keys->data + keys->size;
1020 for (k = keys->data; k < end; k++)
1021 input->modifiers |= d->xkb->map->modmap[*k];
1022
1023 window = input->keyboard_focus;
1024 if (window) {
1025 window->keyboard_device = input;
1026 if (window->keyboard_focus_handler)
1027 (*window->keyboard_focus_handler)(window,
1028 window->keyboard_device,
1029 window->user_data);
1030 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001031}
1032
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001033static const struct wl_input_device_listener input_device_listener = {
1034 window_handle_motion,
1035 window_handle_button,
1036 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001037 window_handle_pointer_focus,
1038 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001039};
1040
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001041void
1042input_get_position(struct input *input, int32_t *x, int32_t *y)
1043{
1044 *x = input->sx;
1045 *y = input->sy;
1046}
1047
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001048struct wl_input_device *
1049input_get_input_device(struct input *input)
1050{
1051 return input->input_device;
1052}
1053
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001054uint32_t
1055input_get_modifiers(struct input *input)
1056{
1057 return input->modifiers;
1058}
1059
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001060struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001061window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001062{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001063 cairo_device_flush (window->display->device);
1064
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001065 return wl_shell_create_drag(window->display->shell);
1066}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001067
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001068void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001069window_move(struct window *window, struct input *input, uint32_t time)
1070{
1071 wl_shell_move(window->display->shell,
1072 window->surface, input->input_device, time);
1073}
1074
1075void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001076window_activate_drag(struct wl_drag *drag, struct window *window,
1077 struct input *input, uint32_t time)
1078{
1079 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001080}
1081
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001082static void
1083handle_configure(void *data, struct wl_shell *shell,
1084 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001085 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001086{
1087 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001088 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001089
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001090 /* FIXME this is probably the wrong place to check for width or
1091 height <= 0, but it prevents the compositor from crashing
1092 */
1093 if(width <= 0 || height <= 0)
1094 return;
1095
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001096 window->resize_edges = edges;
1097
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001098 if (window->resize_handler) {
1099 child_width = width - 20 - window->margin * 2;
1100 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001101
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001102 (*window->resize_handler)(window,
1103 child_width, child_height,
1104 window->user_data);
1105 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001106 window->allocation.width = width;
1107 window->allocation.height = height;
1108
1109 if (window->redraw_handler)
1110 window_schedule_redraw(window);
1111 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001112}
1113
1114static const struct wl_shell_listener shell_listener = {
1115 handle_configure,
1116};
1117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001118void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001119window_get_child_allocation(struct window *window,
1120 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001121{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001122 if (window->fullscreen && !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001123 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001124 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001125 allocation->x = window->margin + 10;
1126 allocation->y = window->margin + 50;
1127 allocation->width =
1128 window->allocation.width - 20 - window->margin * 2;
1129 allocation->height =
1130 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001131 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001132}
1133
1134void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001135window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001136{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001137 if (!window->fullscreen) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001138 window->allocation.width = width + 20 + window->margin * 2;
1139 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001140 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001141}
1142
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001143static gboolean
1144idle_redraw(void *data)
1145{
1146 struct window *window = data;
1147
1148 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001149
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001150 window->redraw_scheduled = 0;
1151
1152 return FALSE;
1153}
1154
1155void
1156window_schedule_redraw(struct window *window)
1157{
1158 if (!window->redraw_scheduled) {
1159 g_idle_add(idle_redraw, window);
1160 window->redraw_scheduled = 1;
1161 }
1162}
1163
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001164void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001165window_set_fullscreen(struct window *window, int fullscreen)
1166{
1167 window->fullscreen = fullscreen;
1168 if (window->fullscreen) {
1169 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001170 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001171 } else {
1172 window->allocation = window->saved_allocation;
1173 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001174}
1175
1176void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001177window_set_decoration(struct window *window, int decoration)
1178{
1179 window->decoration = decoration;
1180}
1181
1182void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001183window_set_user_data(struct window *window, void *data)
1184{
1185 window->user_data = data;
1186}
1187
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001188void *
1189window_get_user_data(struct window *window)
1190{
1191 return window->user_data;
1192}
1193
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001194void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001195window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001196 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001197{
1198 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001199}
1200
1201void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001202window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001203 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001204{
1205 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001206}
1207
1208void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001209window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001210 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001211{
1212 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001213}
1214
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001215void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216window_set_button_handler(struct window *window,
1217 window_button_handler_t handler)
1218{
1219 window->button_handler = handler;
1220}
1221
1222void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223window_set_motion_handler(struct window *window,
1224 window_motion_handler_t handler)
1225{
1226 window->motion_handler = handler;
1227}
1228
1229void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001230window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001231 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001232{
1233 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001234}
1235
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001236void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001237window_damage(struct window *window, int32_t x, int32_t y,
1238 int32_t width, int32_t height)
1239{
1240 wl_surface_damage(window->surface, x, y, width, height);
1241}
1242
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001243static struct window *
1244window_create_internal(struct display *display, struct window *parent,
1245 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001246{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001247 struct window *window;
1248
1249 window = malloc(sizeof *window);
1250 if (window == NULL)
1251 return NULL;
1252
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001253 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001254 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001255 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001256 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001257 window->allocation.x = 0;
1258 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001259 window->allocation.width = width;
1260 window->allocation.height = height;
1261 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001262 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001263 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001264
Yuval Fledel45568f62010-12-06 09:18:12 -05001265 if (display->drm)
1266 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1267 else
1268 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001269
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001270 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001271 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001272
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001273 return window;
1274}
1275
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001276void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001277window_set_title(struct window *window, const char *title)
1278{
1279 window->title = strdup(title);
1280}
1281
1282struct window *
1283window_create(struct display *display, int32_t width, int32_t height)
1284{
1285 struct window *window;
1286
1287 window = window_create_internal(display, NULL, width, height);
1288 if (!window)
1289 return NULL;
1290
1291 return window;
1292}
1293
1294struct window *
1295window_create_transient(struct display *display, struct window *parent,
1296 int32_t x, int32_t y, int32_t width, int32_t height)
1297{
1298 struct window *window;
1299
1300 window = window_create_internal(parent->display,
1301 parent, width, height);
1302 if (!window)
1303 return NULL;
1304
1305 window->x = x;
1306 window->y = y;
1307
1308 return window;
1309}
1310
1311void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001312window_set_buffer_type(struct window *window, enum window_buffer_type type)
1313{
1314 window->buffer_type = type;
1315}
1316
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001317static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001318drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001319{
1320 struct display *d = data;
1321
1322 d->device_name = strdup(device);
1323}
1324
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001325static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1326{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001327 struct display *d = data;
1328
1329 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001330}
1331
1332static const struct wl_drm_listener drm_listener = {
1333 drm_handle_device,
1334 drm_handle_authenticated
1335};
1336
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001337static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001338display_handle_geometry(void *data,
1339 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001340 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001341{
1342 struct display *display = data;
1343
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001344 display->screen_allocation.x = x;
1345 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001346 display->screen_allocation.width = width;
1347 display->screen_allocation.height = height;
1348}
1349
1350static const struct wl_output_listener output_listener = {
1351 display_handle_geometry,
1352};
1353
1354static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001355display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001356{
1357 struct input *input;
1358
1359 input = malloc(sizeof *input);
1360 if (input == NULL)
1361 return;
1362
1363 memset(input, 0, sizeof *input);
1364 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001365 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001366 input->pointer_focus = NULL;
1367 input->keyboard_focus = NULL;
1368 wl_list_insert(d->input_list.prev, &input->link);
1369
1370 wl_input_device_add_listener(input->input_device,
1371 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001372 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001373}
1374
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001375struct selection_offer {
1376 struct display *display;
1377 struct wl_selection_offer *offer;
1378 struct wl_array types;
1379 struct input *input;
1380};
1381
1382int
1383input_offers_mime_type(struct input *input, const char *type)
1384{
1385 struct selection_offer *offer = input->offer;
1386 char **p, **end;
1387
1388 if (offer == NULL)
1389 return 0;
1390
1391 end = offer->types.data + offer->types.size;
1392 for (p = offer->types.data; p < end; p++)
1393 if (strcmp(*p, type) == 0)
1394 return 1;
1395
1396 return 0;
1397}
1398
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001399void
1400input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001401{
1402 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001403
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001404 /* FIXME: A number of things can go wrong here: the object may
1405 * not be the current selection offer any more (which could
1406 * still work, but the source may have gone away or just
1407 * destroyed its wl_selection) or the offer may not have the
1408 * requested type after all (programmer/client error,
1409 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001410 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001411}
1412
1413static void
1414selection_offer_offer(void *data,
1415 struct wl_selection_offer *selection_offer,
1416 const char *type)
1417{
1418 struct selection_offer *offer = data;
1419
1420 char **p;
1421
1422 p = wl_array_add(&offer->types, sizeof *p);
1423 if (p)
1424 *p = strdup(type);
1425};
1426
1427static void
1428selection_offer_keyboard_focus(void *data,
1429 struct wl_selection_offer *selection_offer,
1430 struct wl_input_device *input_device)
1431{
1432 struct selection_offer *offer = data;
1433 struct input *input;
1434 char **p, **end;
1435
1436 if (input_device == NULL) {
1437 printf("selection offer retracted %p\n", selection_offer);
1438 input = offer->input;
1439 input->offer = NULL;
1440 wl_selection_offer_destroy(selection_offer);
1441 wl_array_release(&offer->types);
1442 free(offer);
1443 return;
1444 }
1445
1446 input = wl_input_device_get_user_data(input_device);
1447 printf("new selection offer %p:", selection_offer);
1448
1449 offer->input = input;
1450 input->offer = offer;
1451 end = offer->types.data + offer->types.size;
1452 for (p = offer->types.data; p < end; p++)
1453 printf(" %s", *p);
1454
1455 printf("\n");
1456}
1457
1458struct wl_selection_offer_listener selection_offer_listener = {
1459 selection_offer_offer,
1460 selection_offer_keyboard_focus
1461};
1462
1463static void
1464add_selection_offer(struct display *d, uint32_t id)
1465{
1466 struct selection_offer *offer;
1467
1468 offer = malloc(sizeof *offer);
1469 if (offer == NULL)
1470 return;
1471
1472 offer->offer = wl_selection_offer_create(d->display, id);
1473 offer->display = d;
1474 wl_array_init(&offer->types);
1475 offer->input = NULL;
1476
1477 wl_selection_offer_add_listener(offer->offer,
1478 &selection_offer_listener, offer);
1479}
1480
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001481static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001482display_handle_global(struct wl_display *display, uint32_t id,
1483 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001484{
1485 struct display *d = data;
1486
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001487 if (strcmp(interface, "compositor") == 0) {
1488 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001489 } else if (strcmp(interface, "output") == 0) {
1490 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001491 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001492 } else if (strcmp(interface, "input_device") == 0) {
1493 display_add_input(d, id);
1494 } else if (strcmp(interface, "shell") == 0) {
1495 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001496 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001497 } else if (strcmp(interface, "drm") == 0) {
1498 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001499 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001500 } else if (strcmp(interface, "shm") == 0) {
1501 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001502 } else if (strcmp(interface, "selection_offer") == 0) {
1503 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001504 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001505 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001506 }
1507}
1508
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001509static void
1510display_render_frame(struct display *d)
1511{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001512 int radius = 8;
1513 cairo_t *cr;
1514
1515 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1516 cr = cairo_create(d->shadow);
1517 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1518 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1519 rounded_rect(cr, 16, 16, 112, 112, radius);
1520 cairo_fill(cr);
1521 cairo_destroy(cr);
1522 blur_surface(d->shadow, 64);
1523
1524 d->active_frame =
1525 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1526 cr = cairo_create(d->active_frame);
1527 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1528 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1529 rounded_rect(cr, 16, 16, 112, 112, radius);
1530 cairo_fill(cr);
1531 cairo_destroy(cr);
1532
1533 d->inactive_frame =
1534 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1535 cr = cairo_create(d->inactive_frame);
1536 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1537 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1538 rounded_rect(cr, 16, 16, 112, 112, radius);
1539 cairo_fill(cr);
1540 cairo_destroy(cr);
1541}
1542
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001543static void
1544init_xkb(struct display *d)
1545{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001546 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001547
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001548 names.rules = "evdev";
1549 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001550 names.layout = option_xkb_layout;
1551 names.variant = option_xkb_variant;
1552 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001553
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001554 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001555 if (!d->xkb) {
1556 fprintf(stderr, "Failed to compile keymap\n");
1557 exit(1);
1558 }
1559}
1560
Yuval Fledel45568f62010-12-06 09:18:12 -05001561static int
1562init_drm(struct display *d)
1563{
1564 EGLint major, minor;
1565 int fd;
1566 drm_magic_t magic;
1567
1568 fd = open(d->device_name, O_RDWR);
1569 if (fd < 0) {
1570 fprintf(stderr, "drm open failed: %m\n");
1571 return -1;
1572 }
1573
1574 if (drmGetMagic(fd, &magic)) {
1575 fprintf(stderr, "DRI2: failed to get drm magic");
1576 return -1;
1577 }
1578
1579 /* Wait for authenticated event */
1580 wl_drm_authenticate(d->drm, magic);
1581 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1582 while (!d->authenticated)
1583 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1584
1585 d->dpy = eglGetDRMDisplayMESA(fd);
1586 if (!eglInitialize(d->dpy, &major, &minor)) {
1587 fprintf(stderr, "failed to initialize display\n");
1588 return -1;
1589 }
1590
1591 if (!eglBindAPI(EGL_OPENGL_API)) {
1592 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1593 return -1;
1594 }
1595
1596 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1597 if (d->ctx == NULL) {
1598 fprintf(stderr, "failed to create context\n");
1599 return -1;
1600 }
1601
1602 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1603 fprintf(stderr, "faile to make context current\n");
1604 return -1;
1605 }
1606
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001607#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001608 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1609 if (d->device == NULL) {
1610 fprintf(stderr, "failed to get cairo drm device\n");
1611 return -1;
1612 }
1613#endif
1614
1615 return 0;
1616}
1617
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001618struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001619display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001620{
1621 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001622 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001623 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001624 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001625
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001626 g_type_init();
1627
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001628 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001629 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001630 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001631
1632 xkb_option_group = g_option_group_new("xkb",
1633 "Keyboard options",
1634 "Show all XKB options",
1635 NULL, NULL);
1636 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1637 g_option_context_add_group (context, xkb_option_group);
1638
1639 if (!g_option_context_parse(context, argc, argv, &error)) {
1640 fprintf(stderr, "option parsing failed: %s\n", error->message);
1641 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001642 }
1643
Tim Wiederhake748f6722011-01-23 23:25:25 +01001644 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001645
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001646 d = malloc(sizeof *d);
1647 if (d == NULL)
1648 return NULL;
1649
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001650 memset(d, 0, sizeof *d);
1651
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001652 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001653 if (d->display == NULL) {
1654 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001655 return NULL;
1656 }
1657
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001658 wl_list_init(&d->input_list);
1659
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001660 /* Set up listener so we'll catch all events. */
1661 wl_display_add_global_listener(d->display,
1662 display_handle_global, d);
1663
1664 /* Process connection events. */
1665 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1666
Yuval Fledel45568f62010-12-06 09:18:12 -05001667 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001668 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001669
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001670 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001671
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001672 display_render_frame(d);
1673
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001674 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001675 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001676 g_source_attach(d->source, NULL);
1677
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001678 wl_list_init(&d->window_list);
1679
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001680 init_xkb(d);
1681
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001682 return d;
1683}
1684
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001685struct wl_display *
1686display_get_display(struct display *display)
1687{
1688 return display->display;
1689}
1690
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001691struct wl_compositor *
1692display_get_compositor(struct display *display)
1693{
1694 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001695}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001696
1697EGLDisplay
1698display_get_egl_display(struct display *d)
1699{
1700 return d->dpy;
1701}
1702
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001703struct wl_shell *
1704display_get_shell(struct display *display)
1705{
1706 return display->shell;
1707}
1708
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001709void
1710display_run(struct display *d)
1711{
1712 g_main_loop_run(d->loop);
1713}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001714
1715void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001716display_set_global_handler(struct display *display,
1717 display_global_handler_t handler)
1718{
1719 display->global_handler = handler;
1720}