blob: a42ade0cdfd24eefea3165e721014ff62bd67833 [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øgsberg2d6b7c12010-06-25 16:51:57 -0400687 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688 cairo_surface_t *frame;
689 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500690
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400691 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400693 width = window->allocation.width;
694 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500695
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500696 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500697 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500698 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
699
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500700 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500701
Kristian Høgsberg09531622010-06-14 23:22:15 -0400702 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400703 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400704 cairo_paint(cr);
705
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400706 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400707 tile_mask(cr, window->display->shadow,
708 shadow_dx, shadow_dy, width, height,
709 window->margin + 10 - shadow_dx,
710 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500711
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400712 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400713 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400714 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400715 frame = window->display->inactive_frame;
716
717 tile_source(cr, frame, 0, 0, width, height,
718 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500719
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500720 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
721 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500722 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400723 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500724 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
725 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
726 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400727 if (window->keyboard_device)
728 cairo_set_source_rgb(cr, 0, 0, 0);
729 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400730 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400731 cairo_show_text(cr, window->title);
732
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500733 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400734
735 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500736}
737
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400738void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500739window_destroy(struct window *window)
740{
741 wl_surface_destroy(window->surface);
742 wl_list_remove(&window->link);
743 free(window);
744}
745
746void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400747display_flush_cairo_device(struct display *display)
748{
749 cairo_device_flush (display->device);
750}
751
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500752void
753window_draw(struct window *window)
754{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500755 if (window->parent)
756 window_draw_menu(window);
757 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400758 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500759 else
760 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500761}
762
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400763cairo_surface_t *
764window_get_surface(struct window *window)
765{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400766 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400767}
768
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400769static int
770get_pointer_location(struct window *window, int32_t x, int32_t y)
771{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400772 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400773 const int grip_size = 8;
774
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500775 if (!window->decoration)
776 return WINDOW_CLIENT_AREA;
777
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400778 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400779 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400780 else if (window->margin <= x && x < window->margin + grip_size)
781 hlocation = WINDOW_RESIZING_LEFT;
782 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400783 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400784 else if (x < window->allocation.width - window->margin)
785 hlocation = WINDOW_RESIZING_RIGHT;
786 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400787 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400788
789 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400790 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400791 else if (window->margin <= y && y < window->margin + grip_size)
792 vlocation = WINDOW_RESIZING_TOP;
793 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400794 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400795 else if (y < window->allocation.height - window->margin)
796 vlocation = WINDOW_RESIZING_BOTTOM;
797 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400798 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400799
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400800 location = vlocation | hlocation;
801 if (location & WINDOW_EXTERIOR)
802 location = WINDOW_EXTERIOR;
803 if (location == WINDOW_INTERIOR && y < window->margin + 50)
804 location = WINDOW_TITLEBAR;
805 else if (location == WINDOW_INTERIOR)
806 location = WINDOW_CLIENT_AREA;
807
808 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400809}
810
811static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400812set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400813{
814 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400815 struct wl_buffer *buffer;
816 cairo_surface_t *surface;
817 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400818
819 location = get_pointer_location(input->pointer_focus,
820 input->sx, input->sy);
821 switch (location) {
822 case WINDOW_RESIZING_TOP:
823 pointer = POINTER_TOP;
824 break;
825 case WINDOW_RESIZING_BOTTOM:
826 pointer = POINTER_BOTTOM;
827 break;
828 case WINDOW_RESIZING_LEFT:
829 pointer = POINTER_LEFT;
830 break;
831 case WINDOW_RESIZING_RIGHT:
832 pointer = POINTER_RIGHT;
833 break;
834 case WINDOW_RESIZING_TOP_LEFT:
835 pointer = POINTER_TOP_LEFT;
836 break;
837 case WINDOW_RESIZING_TOP_RIGHT:
838 pointer = POINTER_TOP_RIGHT;
839 break;
840 case WINDOW_RESIZING_BOTTOM_LEFT:
841 pointer = POINTER_BOTTOM_LEFT;
842 break;
843 case WINDOW_RESIZING_BOTTOM_RIGHT:
844 pointer = POINTER_BOTTOM_RIGHT;
845 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400846 case WINDOW_EXTERIOR:
847 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400848 if (input->current_pointer_image == POINTER_DEFAULT)
849 return;
850
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400851 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400852 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400853 return;
854 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400855 break;
856 }
857
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400858 if (pointer == input->current_pointer_image)
859 return;
860
861 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400862 surface = display->pointer_surfaces[pointer];
863 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400864 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400865 pointer_images[pointer].hotspot_x,
866 pointer_images[pointer].hotspot_y);
867}
868
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500869static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500870window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400871 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500872 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500873{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400874 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400875 struct window *window = input->pointer_focus;
876 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400877
878 input->x = x;
879 input->y = y;
880 input->sx = sx;
881 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400882
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400883 location = get_pointer_location(window, input->sx, input->sy);
884
885 if (window->motion_handler)
886 pointer = (*window->motion_handler)(window, input, time,
887 x, y, sx, sy,
888 window->user_data);
889
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400890 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500891}
892
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400893static void
894window_handle_button(void *data,
895 struct wl_input_device *input_device,
896 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500897{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400898 struct input *input = data;
899 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400900 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400901
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400902 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500903
904 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400905 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400906 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400907 wl_shell_move(window->display->shell,
908 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500909 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400910 case WINDOW_RESIZING_TOP:
911 case WINDOW_RESIZING_BOTTOM:
912 case WINDOW_RESIZING_LEFT:
913 case WINDOW_RESIZING_RIGHT:
914 case WINDOW_RESIZING_TOP_LEFT:
915 case WINDOW_RESIZING_TOP_RIGHT:
916 case WINDOW_RESIZING_BOTTOM_LEFT:
917 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400918 wl_shell_resize(window->display->shell,
919 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400920 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500921 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400922 case WINDOW_CLIENT_AREA:
923 if (window->button_handler)
924 (*window->button_handler)(window,
925 input, time,
926 button, state,
927 window->user_data);
928 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500929 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930 } else {
931 if (window->button_handler)
932 (*window->button_handler)(window,
933 input, time,
934 button, state,
935 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500936 }
937}
938
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500939static void
940window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400941 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500942{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400943 struct input *input = data;
944 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400945 struct display *d = window->display;
946 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500947
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400948 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400949 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500950 return;
951
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400952 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500953 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400954 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
955 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500956
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400957 sym = XkbKeySymEntry(d->xkb, code, level, 0);
958
959 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400960 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400961 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400962 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500963
964 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500965 (*window->key_handler)(window, input, time, key, sym, state,
966 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500967}
968
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500969static void
970window_handle_pointer_focus(void *data,
971 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400972 uint32_t time, struct wl_surface *surface,
973 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500974{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400975 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400976 struct window *window;
977 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400978
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400979 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400980 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400981 window = input->pointer_focus;
982
Kristian Høgsberg59826582011-01-20 11:56:57 -0500983 input->x = x;
984 input->y = y;
985 input->sx = sx;
986 input->sy = sy;
987
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400988 pointer = POINTER_LEFT_PTR;
989 if (window->motion_handler)
990 pointer = (*window->motion_handler)(window,
991 input, time,
992 x, y, sx, sy,
993 window->user_data);
994
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400995 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400996 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400997 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400998 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400999 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001000}
1001
1002static void
1003window_handle_keyboard_focus(void *data,
1004 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001005 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001006 struct wl_surface *surface,
1007 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001008{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001009 struct input *input = data;
1010 struct window *window = input->keyboard_focus;
1011 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001012 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001013
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001014 window = input->keyboard_focus;
1015 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001016 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001017 if (window->keyboard_focus_handler)
1018 (*window->keyboard_focus_handler)(window, NULL,
1019 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001020 }
1021
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001022 if (surface)
1023 input->keyboard_focus = wl_surface_get_user_data(surface);
1024 else
1025 input->keyboard_focus = NULL;
1026
1027 end = keys->data + keys->size;
1028 for (k = keys->data; k < end; k++)
1029 input->modifiers |= d->xkb->map->modmap[*k];
1030
1031 window = input->keyboard_focus;
1032 if (window) {
1033 window->keyboard_device = input;
1034 if (window->keyboard_focus_handler)
1035 (*window->keyboard_focus_handler)(window,
1036 window->keyboard_device,
1037 window->user_data);
1038 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001039}
1040
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001041static const struct wl_input_device_listener input_device_listener = {
1042 window_handle_motion,
1043 window_handle_button,
1044 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001045 window_handle_pointer_focus,
1046 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001047};
1048
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001049void
1050input_get_position(struct input *input, int32_t *x, int32_t *y)
1051{
1052 *x = input->sx;
1053 *y = input->sy;
1054}
1055
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001056struct wl_input_device *
1057input_get_input_device(struct input *input)
1058{
1059 return input->input_device;
1060}
1061
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001062uint32_t
1063input_get_modifiers(struct input *input)
1064{
1065 return input->modifiers;
1066}
1067
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001068struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001069window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001070{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001071 cairo_device_flush (window->display->device);
1072
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001073 return wl_shell_create_drag(window->display->shell);
1074}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001075
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001076void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001077window_move(struct window *window, struct input *input, uint32_t time)
1078{
1079 wl_shell_move(window->display->shell,
1080 window->surface, input->input_device, time);
1081}
1082
1083void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001084window_activate_drag(struct wl_drag *drag, struct window *window,
1085 struct input *input, uint32_t time)
1086{
1087 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001088}
1089
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001090static void
1091handle_configure(void *data, struct wl_shell *shell,
1092 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001093 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001094{
1095 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001096 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001097
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 {
1106 window->resize_edges = edges;
1107 window->allocation.width = width;
1108 window->allocation.height = height;
1109
1110 if (window->redraw_handler)
1111 window_schedule_redraw(window);
1112 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001113}
1114
1115static const struct wl_shell_listener shell_listener = {
1116 handle_configure,
1117};
1118
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001119void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001120window_get_child_allocation(struct window *window,
1121 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001122{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001123 if (window->fullscreen && !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001124 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001125 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001126 allocation->x = window->margin + 10;
1127 allocation->y = window->margin + 50;
1128 allocation->width =
1129 window->allocation.width - 20 - window->margin * 2;
1130 allocation->height =
1131 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001132 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001133}
1134
1135void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001136window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001137{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001138 if (!window->fullscreen) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001139 window->allocation.width = width + 20 + window->margin * 2;
1140 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001141 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001142}
1143
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001144static gboolean
1145idle_redraw(void *data)
1146{
1147 struct window *window = data;
1148
1149 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001150
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001151 window->redraw_scheduled = 0;
1152
1153 return FALSE;
1154}
1155
1156void
1157window_schedule_redraw(struct window *window)
1158{
1159 if (!window->redraw_scheduled) {
1160 g_idle_add(idle_redraw, window);
1161 window->redraw_scheduled = 1;
1162 }
1163}
1164
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001165void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001166window_set_fullscreen(struct window *window, int fullscreen)
1167{
1168 window->fullscreen = fullscreen;
1169 if (window->fullscreen) {
1170 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001171 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001172 } else {
1173 window->allocation = window->saved_allocation;
1174 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001175}
1176
1177void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001178window_set_decoration(struct window *window, int decoration)
1179{
1180 window->decoration = decoration;
1181}
1182
1183void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001184window_set_user_data(struct window *window, void *data)
1185{
1186 window->user_data = data;
1187}
1188
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001189void *
1190window_get_user_data(struct window *window)
1191{
1192 return window->user_data;
1193}
1194
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001195void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001196window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001197 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001198{
1199 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001200}
1201
1202void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001203window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001204 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001205{
1206 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001207}
1208
1209void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001210window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001211 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001212{
1213 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001214}
1215
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001216void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001217window_set_button_handler(struct window *window,
1218 window_button_handler_t handler)
1219{
1220 window->button_handler = handler;
1221}
1222
1223void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001224window_set_motion_handler(struct window *window,
1225 window_motion_handler_t handler)
1226{
1227 window->motion_handler = handler;
1228}
1229
1230void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001231window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001232 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001233{
1234 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001235}
1236
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001237void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001238window_damage(struct window *window, int32_t x, int32_t y,
1239 int32_t width, int32_t height)
1240{
1241 wl_surface_damage(window->surface, x, y, width, height);
1242}
1243
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001244static struct window *
1245window_create_internal(struct display *display, struct window *parent,
1246 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001247{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001248 struct window *window;
1249
1250 window = malloc(sizeof *window);
1251 if (window == NULL)
1252 return NULL;
1253
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001254 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001255 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001256 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001257 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001258 window->allocation.x = 0;
1259 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001260 window->allocation.width = width;
1261 window->allocation.height = height;
1262 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001263 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001264 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001265
Yuval Fledel45568f62010-12-06 09:18:12 -05001266 if (display->drm)
1267 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1268 else
1269 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001270
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001271 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001272 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001273
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001274 return window;
1275}
1276
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001277void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001278window_set_title(struct window *window, const char *title)
1279{
1280 window->title = strdup(title);
1281}
1282
1283struct window *
1284window_create(struct display *display, int32_t width, int32_t height)
1285{
1286 struct window *window;
1287
1288 window = window_create_internal(display, NULL, width, height);
1289 if (!window)
1290 return NULL;
1291
1292 return window;
1293}
1294
1295struct window *
1296window_create_transient(struct display *display, struct window *parent,
1297 int32_t x, int32_t y, int32_t width, int32_t height)
1298{
1299 struct window *window;
1300
1301 window = window_create_internal(parent->display,
1302 parent, width, height);
1303 if (!window)
1304 return NULL;
1305
1306 window->x = x;
1307 window->y = y;
1308
1309 return window;
1310}
1311
1312void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001313window_set_buffer_type(struct window *window, enum window_buffer_type type)
1314{
1315 window->buffer_type = type;
1316}
1317
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001318static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001319drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001320{
1321 struct display *d = data;
1322
1323 d->device_name = strdup(device);
1324}
1325
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001326static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1327{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001328 struct display *d = data;
1329
1330 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001331}
1332
1333static const struct wl_drm_listener drm_listener = {
1334 drm_handle_device,
1335 drm_handle_authenticated
1336};
1337
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001338static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001339display_handle_geometry(void *data,
1340 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001341 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001342{
1343 struct display *display = data;
1344
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001345 display->screen_allocation.x = x;
1346 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001347 display->screen_allocation.width = width;
1348 display->screen_allocation.height = height;
1349}
1350
1351static const struct wl_output_listener output_listener = {
1352 display_handle_geometry,
1353};
1354
1355static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001356display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001357{
1358 struct input *input;
1359
1360 input = malloc(sizeof *input);
1361 if (input == NULL)
1362 return;
1363
1364 memset(input, 0, sizeof *input);
1365 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001366 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001367 input->pointer_focus = NULL;
1368 input->keyboard_focus = NULL;
1369 wl_list_insert(d->input_list.prev, &input->link);
1370
1371 wl_input_device_add_listener(input->input_device,
1372 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001373 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001374}
1375
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001376struct selection_offer {
1377 struct display *display;
1378 struct wl_selection_offer *offer;
1379 struct wl_array types;
1380 struct input *input;
1381};
1382
1383int
1384input_offers_mime_type(struct input *input, const char *type)
1385{
1386 struct selection_offer *offer = input->offer;
1387 char **p, **end;
1388
1389 if (offer == NULL)
1390 return 0;
1391
1392 end = offer->types.data + offer->types.size;
1393 for (p = offer->types.data; p < end; p++)
1394 if (strcmp(*p, type) == 0)
1395 return 1;
1396
1397 return 0;
1398}
1399
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001400void
1401input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001402{
1403 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001404
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001405 /* FIXME: A number of things can go wrong here: the object may
1406 * not be the current selection offer any more (which could
1407 * still work, but the source may have gone away or just
1408 * destroyed its wl_selection) or the offer may not have the
1409 * requested type after all (programmer/client error,
1410 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001411 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001412}
1413
1414static void
1415selection_offer_offer(void *data,
1416 struct wl_selection_offer *selection_offer,
1417 const char *type)
1418{
1419 struct selection_offer *offer = data;
1420
1421 char **p;
1422
1423 p = wl_array_add(&offer->types, sizeof *p);
1424 if (p)
1425 *p = strdup(type);
1426};
1427
1428static void
1429selection_offer_keyboard_focus(void *data,
1430 struct wl_selection_offer *selection_offer,
1431 struct wl_input_device *input_device)
1432{
1433 struct selection_offer *offer = data;
1434 struct input *input;
1435 char **p, **end;
1436
1437 if (input_device == NULL) {
1438 printf("selection offer retracted %p\n", selection_offer);
1439 input = offer->input;
1440 input->offer = NULL;
1441 wl_selection_offer_destroy(selection_offer);
1442 wl_array_release(&offer->types);
1443 free(offer);
1444 return;
1445 }
1446
1447 input = wl_input_device_get_user_data(input_device);
1448 printf("new selection offer %p:", selection_offer);
1449
1450 offer->input = input;
1451 input->offer = offer;
1452 end = offer->types.data + offer->types.size;
1453 for (p = offer->types.data; p < end; p++)
1454 printf(" %s", *p);
1455
1456 printf("\n");
1457}
1458
1459struct wl_selection_offer_listener selection_offer_listener = {
1460 selection_offer_offer,
1461 selection_offer_keyboard_focus
1462};
1463
1464static void
1465add_selection_offer(struct display *d, uint32_t id)
1466{
1467 struct selection_offer *offer;
1468
1469 offer = malloc(sizeof *offer);
1470 if (offer == NULL)
1471 return;
1472
1473 offer->offer = wl_selection_offer_create(d->display, id);
1474 offer->display = d;
1475 wl_array_init(&offer->types);
1476 offer->input = NULL;
1477
1478 wl_selection_offer_add_listener(offer->offer,
1479 &selection_offer_listener, offer);
1480}
1481
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001482static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001483display_handle_global(struct wl_display *display, uint32_t id,
1484 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001485{
1486 struct display *d = data;
1487
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001488 if (strcmp(interface, "compositor") == 0) {
1489 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001490 } else if (strcmp(interface, "output") == 0) {
1491 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001492 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001493 } else if (strcmp(interface, "input_device") == 0) {
1494 display_add_input(d, id);
1495 } else if (strcmp(interface, "shell") == 0) {
1496 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001497 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001498 } else if (strcmp(interface, "drm") == 0) {
1499 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001500 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001501 } else if (strcmp(interface, "shm") == 0) {
1502 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001503 } else if (strcmp(interface, "selection_offer") == 0) {
1504 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001505 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001506 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001507 }
1508}
1509
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001510static void
1511display_render_frame(struct display *d)
1512{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001513 int radius = 8;
1514 cairo_t *cr;
1515
1516 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1517 cr = cairo_create(d->shadow);
1518 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1519 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1520 rounded_rect(cr, 16, 16, 112, 112, radius);
1521 cairo_fill(cr);
1522 cairo_destroy(cr);
1523 blur_surface(d->shadow, 64);
1524
1525 d->active_frame =
1526 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1527 cr = cairo_create(d->active_frame);
1528 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1529 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1530 rounded_rect(cr, 16, 16, 112, 112, radius);
1531 cairo_fill(cr);
1532 cairo_destroy(cr);
1533
1534 d->inactive_frame =
1535 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1536 cr = cairo_create(d->inactive_frame);
1537 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1538 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1539 rounded_rect(cr, 16, 16, 112, 112, radius);
1540 cairo_fill(cr);
1541 cairo_destroy(cr);
1542}
1543
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001544static void
1545init_xkb(struct display *d)
1546{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001547 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001548
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001549 names.rules = "evdev";
1550 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001551 names.layout = option_xkb_layout;
1552 names.variant = option_xkb_variant;
1553 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001554
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001555 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001556 if (!d->xkb) {
1557 fprintf(stderr, "Failed to compile keymap\n");
1558 exit(1);
1559 }
1560}
1561
Yuval Fledel45568f62010-12-06 09:18:12 -05001562static int
1563init_drm(struct display *d)
1564{
1565 EGLint major, minor;
1566 int fd;
1567 drm_magic_t magic;
1568
1569 fd = open(d->device_name, O_RDWR);
1570 if (fd < 0) {
1571 fprintf(stderr, "drm open failed: %m\n");
1572 return -1;
1573 }
1574
1575 if (drmGetMagic(fd, &magic)) {
1576 fprintf(stderr, "DRI2: failed to get drm magic");
1577 return -1;
1578 }
1579
1580 /* Wait for authenticated event */
1581 wl_drm_authenticate(d->drm, magic);
1582 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1583 while (!d->authenticated)
1584 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1585
1586 d->dpy = eglGetDRMDisplayMESA(fd);
1587 if (!eglInitialize(d->dpy, &major, &minor)) {
1588 fprintf(stderr, "failed to initialize display\n");
1589 return -1;
1590 }
1591
1592 if (!eglBindAPI(EGL_OPENGL_API)) {
1593 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1594 return -1;
1595 }
1596
1597 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1598 if (d->ctx == NULL) {
1599 fprintf(stderr, "failed to create context\n");
1600 return -1;
1601 }
1602
1603 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1604 fprintf(stderr, "faile to make context current\n");
1605 return -1;
1606 }
1607
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001608#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001609 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1610 if (d->device == NULL) {
1611 fprintf(stderr, "failed to get cairo drm device\n");
1612 return -1;
1613 }
1614#endif
1615
1616 return 0;
1617}
1618
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001619struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001620display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001621{
1622 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001623 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001624 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001625 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001626
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001627 g_type_init();
1628
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001629 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001630 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001631 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001632
1633 xkb_option_group = g_option_group_new("xkb",
1634 "Keyboard options",
1635 "Show all XKB options",
1636 NULL, NULL);
1637 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1638 g_option_context_add_group (context, xkb_option_group);
1639
1640 if (!g_option_context_parse(context, argc, argv, &error)) {
1641 fprintf(stderr, "option parsing failed: %s\n", error->message);
1642 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001643 }
1644
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
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001650 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001651 if (d->display == NULL) {
1652 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001653 return NULL;
1654 }
1655
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001656 wl_list_init(&d->input_list);
1657
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001658 /* Set up listener so we'll catch all events. */
1659 wl_display_add_global_listener(d->display,
1660 display_handle_global, d);
1661
1662 /* Process connection events. */
1663 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1664
Yuval Fledel45568f62010-12-06 09:18:12 -05001665 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001666 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001667
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001668 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001669
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001670 display_render_frame(d);
1671
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001672 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001673 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001674 g_source_attach(d->source, NULL);
1675
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001676 wl_list_init(&d->window_list);
1677
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001678 init_xkb(d);
1679
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001680 return d;
1681}
1682
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001683struct wl_display *
1684display_get_display(struct display *display)
1685{
1686 return display->display;
1687}
1688
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001689struct wl_compositor *
1690display_get_compositor(struct display *display)
1691{
1692 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001693}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001694
1695EGLDisplay
1696display_get_egl_display(struct display *d)
1697{
1698 return d->dpy;
1699}
1700
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001701struct wl_shell *
1702display_get_shell(struct display *display)
1703{
1704 return display->shell;
1705}
1706
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001707void
1708display_run(struct display *d)
1709{
1710 g_main_loop_run(d->loop);
1711}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001712
1713void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001714display_set_global_handler(struct display *display,
1715 display_global_handler_t handler)
1716{
1717 display->global_handler = handler;
1718}