blob: 84d0418fb9b0979ea2658cc4b0ecef8967483d0f [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
82 display_drag_offer_handler_t drag_offer_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083};
84
85struct window {
86 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050087 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050088 const char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050089 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040091 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050092 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050093 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050094 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040096 struct input *grab_device;
97 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050098 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040099 enum window_buffer_type buffer_type;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500100 int mapped;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500101
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500103 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500104
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500105 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400106 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500107 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400108 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500109 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400111
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500112 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400113 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500114};
115
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400116struct input {
117 struct display *display;
118 struct wl_input_device *input_device;
119 struct window *pointer_focus;
120 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400121 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122 uint32_t modifiers;
123 int32_t x, y, sx, sy;
124 struct wl_list link;
125};
126
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400127enum {
128 POINTER_DEFAULT = 100,
129 POINTER_UNSET
130};
131
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500132enum window_location {
133 WINDOW_INTERIOR = 0,
134 WINDOW_RESIZING_TOP = 1,
135 WINDOW_RESIZING_BOTTOM = 2,
136 WINDOW_RESIZING_LEFT = 4,
137 WINDOW_RESIZING_TOP_LEFT = 5,
138 WINDOW_RESIZING_BOTTOM_LEFT = 6,
139 WINDOW_RESIZING_RIGHT = 8,
140 WINDOW_RESIZING_TOP_RIGHT = 9,
141 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
142 WINDOW_RESIZING_MASK = 15,
143 WINDOW_EXTERIOR = 16,
144 WINDOW_TITLEBAR = 17,
145 WINDOW_CLIENT_AREA = 18,
146};
147
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400148const char *option_xkb_layout = "us";
149const char *option_xkb_variant = "";
150const char *option_xkb_options = "";
151
152static const GOptionEntry xkb_option_entries[] = {
153 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
154 &option_xkb_layout, "XKB Layout" },
155 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
156 &option_xkb_variant, "XKB Variant" },
157 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
158 &option_xkb_options, "XKB Options" },
159 { NULL }
160};
161
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500162static void
163rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
164{
165 cairo_move_to(cr, x0, y0 + radius);
166 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
167 cairo_line_to(cr, x1 - radius, y0);
168 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
169 cairo_line_to(cr, x1, y1 - radius);
170 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
171 cairo_line_to(cr, x0 + radius, y1);
172 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
173 cairo_close_path(cr);
174}
175
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400176static const cairo_user_data_key_t surface_data_key;
177struct surface_data {
178 struct wl_buffer *buffer;
179};
180
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500181#define MULT(_d,c,a,t) \
182 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
183
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500184#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400185
186struct drm_surface_data {
187 struct surface_data data;
188 EGLImageKHR image;
189 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800190 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191};
192
193static void
194drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400195{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400196 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800197 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400198
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800199 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400200 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800201 cairo_device_release(d->device);
202
203 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400204 wl_buffer_destroy(data->data.buffer);
205}
206
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500207EGLImageKHR
208display_get_image_for_drm_surface(struct display *display,
209 cairo_surface_t *surface)
210{
211 struct drm_surface_data *data;
212
213 data = cairo_surface_get_user_data (surface, &surface_data_key);
214
215 return data->image;
216}
217
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500218static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400219display_create_drm_surface(struct display *display,
220 struct rectangle *rectangle)
221{
222 struct drm_surface_data *data;
223 EGLDisplay dpy = display->dpy;
224 cairo_surface_t *surface;
225 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400226 EGLint name, stride;
227
228 EGLint image_attribs[] = {
229 EGL_WIDTH, 0,
230 EGL_HEIGHT, 0,
231 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
232 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
233 EGL_NONE
234 };
235
236 data = malloc(sizeof *data);
237 if (data == NULL)
238 return NULL;
239
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800240 data->display = display;
241
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400242 image_attribs[1] = rectangle->width;
243 image_attribs[3] = rectangle->height;
244 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800245
246 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400248 glBindTexture(GL_TEXTURE_2D, data->texture);
249 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800250 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251
252 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
253
254 visual = wl_display_get_premultiplied_argb_visual(display->display);
255 data->data.buffer =
256 wl_drm_create_buffer(display->drm, name, rectangle->width,
257 rectangle->height, stride, visual);
258
259 surface = cairo_gl_surface_create_for_texture(display->device,
260 CAIRO_CONTENT_COLOR_ALPHA,
261 data->texture,
262 rectangle->width,
263 rectangle->height);
264
265 cairo_surface_set_user_data (surface, &surface_data_key,
266 data, drm_surface_data_destroy);
267
268 return surface;
269}
270
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500271static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272display_create_drm_surface_from_file(struct display *display,
273 const char *filename,
274 struct rectangle *rect)
275{
276 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400277 GdkPixbuf *pixbuf;
278 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400279 int stride, i;
280 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800281 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400282
283 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400284 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285 FALSE, &error);
286 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400287 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400288
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400289 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
290 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
291 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400293 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400294
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400295
296 stride = gdk_pixbuf_get_rowstride(pixbuf);
297 pixels = gdk_pixbuf_get_pixels(pixbuf);
298
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400299 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400300 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400302 while (p < end) {
303 unsigned int t;
304
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400305 MULT(p[0], p[0], p[3], t);
306 MULT(p[1], p[1], p[3], t);
307 MULT(p[2], p[2], p[3], t);
308 p += 4;
309
310 }
311 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400312
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800314 data = cairo_surface_get_user_data(surface, &surface_data_key);
315
316 cairo_device_acquire(display->device);
317 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800318 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
319 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800320 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321
322 gdk_pixbuf_unref(pixbuf);
323
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324 return surface;
325}
326
327#endif
328
329struct wl_buffer *
330display_get_buffer_for_surface(struct display *display,
331 cairo_surface_t *surface)
332{
333 struct surface_data *data;
334
335 data = cairo_surface_get_user_data (surface, &surface_data_key);
336
337 return data->buffer;
338}
339
340struct shm_surface_data {
341 struct surface_data data;
342 void *map;
343 size_t length;
344};
345
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500346static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400347shm_surface_data_destroy(void *p)
348{
349 struct shm_surface_data *data = p;
350
351 wl_buffer_destroy(data->data.buffer);
352 munmap(data->map, data->length);
353}
354
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500355static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356display_create_shm_surface(struct display *display,
357 struct rectangle *rectangle)
358{
359 struct shm_surface_data *data;
360 cairo_surface_t *surface;
361 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800362 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400363 char filename[] = "/tmp/wayland-shm-XXXXXX";
364
365 data = malloc(sizeof *data);
366 if (data == NULL)
367 return NULL;
368
369 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
370 rectangle->width);
371 data->length = stride * rectangle->height;
372 fd = mkstemp(filename);
373 if (fd < 0) {
374 fprintf(stderr, "open %s failed: %m", filename);
375 return NULL;
376 }
377 if (ftruncate(fd, data->length) < 0) {
378 fprintf(stderr, "ftruncate failed: %m");
379 close(fd);
380 return NULL;
381 }
382
383 data->map = mmap(NULL, data->length,
384 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
385 unlink(filename);
386
387 if (data->map == MAP_FAILED) {
388 fprintf(stderr, "mmap failed: %m");
389 close(fd);
390 return NULL;
391 }
392
393 surface = cairo_image_surface_create_for_data (data->map,
394 CAIRO_FORMAT_ARGB32,
395 rectangle->width,
396 rectangle->height,
397 stride);
398
399 cairo_surface_set_user_data (surface, &surface_data_key,
400 data, shm_surface_data_destroy);
401
402 visual = wl_display_get_premultiplied_argb_visual(display->display);
403 data->data.buffer = wl_shm_create_buffer(display->shm,
404 fd,
405 rectangle->width,
406 rectangle->height,
407 stride, visual);
408
409 close(fd);
410
411 return surface;
412}
413
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500414static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415display_create_shm_surface_from_file(struct display *display,
416 const char *filename,
417 struct rectangle *rect)
418{
419 cairo_surface_t *surface;
420 GdkPixbuf *pixbuf;
421 GError *error = NULL;
422 int stride, i;
423 unsigned char *pixels, *p, *end, *dest_data;
424 int dest_stride;
425 uint32_t *d;
426
427 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
428 rect->width, rect->height,
429 FALSE, &error);
430 if (error != NULL)
431 return NULL;
432
433 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
434 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
435 gdk_pixbuf_unref(pixbuf);
436 return NULL;
437 }
438
439 stride = gdk_pixbuf_get_rowstride(pixbuf);
440 pixels = gdk_pixbuf_get_pixels(pixbuf);
441
442 surface = display_create_shm_surface(display, rect);
443 dest_data = cairo_image_surface_get_data (surface);
444 dest_stride = cairo_image_surface_get_stride (surface);
445
446 for (i = 0; i < rect->height; i++) {
447 d = (uint32_t *) (dest_data + i * dest_stride);
448 p = pixels + i * stride;
449 end = p + rect->width * 4;
450 while (p < end) {
451 unsigned int t;
452 unsigned char a, r, g, b;
453
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454 a = p[3];
455 MULT(r, p[0], a, t);
456 MULT(g, p[1], a, t);
457 MULT(b, p[2], a, t);
458 p += 4;
459 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
460 }
461 }
462
463 gdk_pixbuf_unref(pixbuf);
464
465 return surface;
466}
467
468cairo_surface_t *
469display_create_surface(struct display *display,
470 struct rectangle *rectangle)
471{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500472#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500473 if (display->drm) {
474 return display_create_drm_surface(display, rectangle);
475 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400476#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500477 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400478}
479
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500480static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400481display_create_surface_from_file(struct display *display,
482 const char *filename,
483 struct rectangle *rectangle)
484{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500485#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500486 if (display->drm) {
487 return display_create_drm_surface_from_file(display, filename, rectangle);
488 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500490 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400491}
Yuval Fledel45568f62010-12-06 09:18:12 -0500492 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400493 const char *filename;
494 int hotspot_x, hotspot_y;
495} pointer_images[] = {
496 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
497 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
498 { DATADIR "/wayland/bottom_side.png", 16, 20 },
499 { DATADIR "/wayland/grabbing.png", 20, 17 },
500 { DATADIR "/wayland/left_ptr.png", 10, 5 },
501 { DATADIR "/wayland/left_side.png", 10, 20 },
502 { DATADIR "/wayland/right_side.png", 30, 19 },
503 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
504 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
505 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400506 { DATADIR "/wayland/xterm.png", 15, 15 },
507 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400508};
509
510static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400511create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400512{
513 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400514 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400515 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400516
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400517 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400518 display->pointer_surfaces =
519 malloc(count * sizeof *display->pointer_surfaces);
520 rect.width = width;
521 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400522 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400523 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 display_create_surface_from_file(display,
525 pointer_images[i].filename,
526 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400527 }
528
529}
530
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400531cairo_surface_t *
532display_get_pointer_surface(struct display *display, int pointer,
533 int *width, int *height,
534 int *hotspot_x, int *hotspot_y)
535{
536 cairo_surface_t *surface;
537
538 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500539#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400540 *width = cairo_gl_surface_get_width(surface);
541 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000542#else
543 *width = cairo_image_surface_get_width(surface);
544 *height = cairo_image_surface_get_height(surface);
545#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400546 *hotspot_x = pointer_images[pointer].hotspot_x;
547 *hotspot_y = pointer_images[pointer].hotspot_y;
548
549 return cairo_surface_reference(surface);
550}
551
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400552
553static void
554window_attach_surface(struct window *window);
555
556static void
557free_surface(void *data)
558{
559 struct window *window = data;
560
561 cairo_surface_destroy(window->pending_surface);
562 window->pending_surface = NULL;
563 if (window->cairo_surface)
564 window_attach_surface(window);
565}
566
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500567static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500568window_attach_surface(struct window *window)
569{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400570 struct display *display = window->display;
571 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500572 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500573
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500574 if (window->pending_surface != NULL)
575 return;
576
Kristian Høgsberg09531622010-06-14 23:22:15 -0400577 window->pending_surface = window->cairo_surface;
578 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500579
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400580 buffer = display_get_buffer_for_surface(display,
581 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500582 if (window->resize_edges & WINDOW_RESIZING_LEFT)
583 x = window->server_allocation.width -
584 window->allocation.width;
585 else
586 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500587
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500588 if (window->resize_edges & WINDOW_RESIZING_TOP)
589 y = window->server_allocation.height -
590 window->allocation.height;
591 else
592 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400593
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500594 window->server_allocation = window->allocation;
595 window->resize_edges = 0;
596 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400597 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500598
599 if (!window->mapped) {
600 wl_surface_map_toplevel(window->surface);
601 window->mapped = 1;
602 }
603
604 wl_surface_damage(window->surface, 0, 0,
605 window->allocation.width,
606 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500607}
608
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500609void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400610window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500611{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400612 if (window->cairo_surface)
613 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500614}
615
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400616void
617window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500619 cairo_surface_reference(surface);
620
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400621 if (window->cairo_surface != NULL)
622 cairo_surface_destroy(window->cairo_surface);
623
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500624 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400625}
626
627void
628window_create_surface(struct window *window)
629{
630 cairo_surface_t *surface;
631
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500633#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400635 surface = display_create_surface(window->display,
636 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637 break;
638#endif
639 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400640 surface = display_create_shm_surface(window->display,
641 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800643 default:
644 surface = NULL;
645 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400646 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400647
648 window_set_surface(window, surface);
649 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650}
651
652static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500653window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500654{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500655 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500656 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400657 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400658 cairo_surface_t *frame;
659 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500660
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400661 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400663 width = window->allocation.width;
664 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500665
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500666 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500667 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500668 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
669
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500670 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500671
Kristian Høgsberg09531622010-06-14 23:22:15 -0400672 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400673 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400674 cairo_paint(cr);
675
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400676 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400677 tile_mask(cr, window->display->shadow,
678 shadow_dx, shadow_dy, width, height,
679 window->margin + 10 - shadow_dx,
680 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500681
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400682 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400683 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400684 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400685 frame = window->display->inactive_frame;
686
687 tile_source(cr, frame, 0, 0, width, height,
688 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500689
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500690 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
691 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500692 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400693 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500694 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
695 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
696 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400697 if (window->keyboard_device)
698 cairo_set_source_rgb(cr, 0, 0, 0);
699 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400700 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400701 cairo_show_text(cr, window->title);
702
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500703 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400704
705 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500706}
707
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400708void
709display_flush_cairo_device(struct display *display)
710{
711 cairo_device_flush (display->device);
712}
713
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500714void
715window_draw(struct window *window)
716{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400717 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400718 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500719 else
720 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500721}
722
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400723cairo_surface_t *
724window_get_surface(struct window *window)
725{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400726 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400727}
728
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400729static int
730get_pointer_location(struct window *window, int32_t x, int32_t y)
731{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400732 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400733 const int grip_size = 8;
734
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500735 if (!window->decoration)
736 return WINDOW_CLIENT_AREA;
737
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400738 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400739 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400740 else if (window->margin <= x && x < window->margin + grip_size)
741 hlocation = WINDOW_RESIZING_LEFT;
742 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400743 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400744 else if (x < window->allocation.width - window->margin)
745 hlocation = WINDOW_RESIZING_RIGHT;
746 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400747 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400748
749 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400750 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400751 else if (window->margin <= y && y < window->margin + grip_size)
752 vlocation = WINDOW_RESIZING_TOP;
753 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400754 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400755 else if (y < window->allocation.height - window->margin)
756 vlocation = WINDOW_RESIZING_BOTTOM;
757 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400758 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400759
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400760 location = vlocation | hlocation;
761 if (location & WINDOW_EXTERIOR)
762 location = WINDOW_EXTERIOR;
763 if (location == WINDOW_INTERIOR && y < window->margin + 50)
764 location = WINDOW_TITLEBAR;
765 else if (location == WINDOW_INTERIOR)
766 location = WINDOW_CLIENT_AREA;
767
768 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400769}
770
771static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400772set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400773{
774 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400775 struct wl_buffer *buffer;
776 cairo_surface_t *surface;
777 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400778
779 location = get_pointer_location(input->pointer_focus,
780 input->sx, input->sy);
781 switch (location) {
782 case WINDOW_RESIZING_TOP:
783 pointer = POINTER_TOP;
784 break;
785 case WINDOW_RESIZING_BOTTOM:
786 pointer = POINTER_BOTTOM;
787 break;
788 case WINDOW_RESIZING_LEFT:
789 pointer = POINTER_LEFT;
790 break;
791 case WINDOW_RESIZING_RIGHT:
792 pointer = POINTER_RIGHT;
793 break;
794 case WINDOW_RESIZING_TOP_LEFT:
795 pointer = POINTER_TOP_LEFT;
796 break;
797 case WINDOW_RESIZING_TOP_RIGHT:
798 pointer = POINTER_TOP_RIGHT;
799 break;
800 case WINDOW_RESIZING_BOTTOM_LEFT:
801 pointer = POINTER_BOTTOM_LEFT;
802 break;
803 case WINDOW_RESIZING_BOTTOM_RIGHT:
804 pointer = POINTER_BOTTOM_RIGHT;
805 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400806 case WINDOW_EXTERIOR:
807 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400808 if (input->current_pointer_image == POINTER_DEFAULT)
809 return;
810
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400811 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400812 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400813 return;
814 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400815 break;
816 }
817
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400818 if (pointer == input->current_pointer_image)
819 return;
820
821 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400822 surface = display->pointer_surfaces[pointer];
823 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400824 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400825 pointer_images[pointer].hotspot_x,
826 pointer_images[pointer].hotspot_y);
827}
828
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500829static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500830window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400831 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500832 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500833{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400834 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400835 struct window *window = input->pointer_focus;
836 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400837
838 input->x = x;
839 input->y = y;
840 input->sx = sx;
841 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400842
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400843 location = get_pointer_location(window, input->sx, input->sy);
844
845 if (window->motion_handler)
846 pointer = (*window->motion_handler)(window, input, time,
847 x, y, sx, sy,
848 window->user_data);
849
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400850 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500851}
852
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400853static void
854window_handle_button(void *data,
855 struct wl_input_device *input_device,
856 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500857{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400858 struct input *input = data;
859 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400860 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400861
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400862 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500863
864 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400865 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400866 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400867 wl_shell_move(window->display->shell,
868 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500869 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400870 case WINDOW_RESIZING_TOP:
871 case WINDOW_RESIZING_BOTTOM:
872 case WINDOW_RESIZING_LEFT:
873 case WINDOW_RESIZING_RIGHT:
874 case WINDOW_RESIZING_TOP_LEFT:
875 case WINDOW_RESIZING_TOP_RIGHT:
876 case WINDOW_RESIZING_BOTTOM_LEFT:
877 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400878 wl_shell_resize(window->display->shell,
879 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400880 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500881 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400882 case WINDOW_CLIENT_AREA:
883 if (window->button_handler)
884 (*window->button_handler)(window,
885 input, time,
886 button, state,
887 window->user_data);
888 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500889 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400890 } else {
891 if (window->button_handler)
892 (*window->button_handler)(window,
893 input, time,
894 button, state,
895 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500896 }
897}
898
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500899static void
900window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400901 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500902{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400903 struct input *input = data;
904 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400905 struct display *d = window->display;
906 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500907
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400908 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400909 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500910 return;
911
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400912 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400913 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400914 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
915 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500916
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400917 sym = XkbKeySymEntry(d->xkb, code, level, 0);
918
919 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400920 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400921 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400922 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500923
924 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400925 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400926 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500927}
928
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500929static void
930window_handle_pointer_focus(void *data,
931 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400932 uint32_t time, struct wl_surface *surface,
933 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500934{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400935 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400936 struct window *window;
937 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400938
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400939 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400940 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 window = input->pointer_focus;
942
943 pointer = POINTER_LEFT_PTR;
944 if (window->motion_handler)
945 pointer = (*window->motion_handler)(window,
946 input, time,
947 x, y, sx, sy,
948 window->user_data);
949
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400950 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400951 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400952 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400953 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400954 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500955}
956
957static void
958window_handle_keyboard_focus(void *data,
959 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400960 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500961 struct wl_surface *surface,
962 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500963{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400964 struct input *input = data;
965 struct window *window = input->keyboard_focus;
966 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500967 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500968
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400969 window = input->keyboard_focus;
970 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500971 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400972 if (window->keyboard_focus_handler)
973 (*window->keyboard_focus_handler)(window, NULL,
974 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500975 }
976
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400977 if (surface)
978 input->keyboard_focus = wl_surface_get_user_data(surface);
979 else
980 input->keyboard_focus = NULL;
981
982 end = keys->data + keys->size;
983 for (k = keys->data; k < end; k++)
984 input->modifiers |= d->xkb->map->modmap[*k];
985
986 window = input->keyboard_focus;
987 if (window) {
988 window->keyboard_device = input;
989 if (window->keyboard_focus_handler)
990 (*window->keyboard_focus_handler)(window,
991 window->keyboard_device,
992 window->user_data);
993 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500994}
995
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500996static const struct wl_input_device_listener input_device_listener = {
997 window_handle_motion,
998 window_handle_button,
999 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001000 window_handle_pointer_focus,
1001 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001002};
1003
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001004void
1005input_get_position(struct input *input, int32_t *x, int32_t *y)
1006{
1007 *x = input->sx;
1008 *y = input->sy;
1009}
1010
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001011struct wl_input_device *
1012input_get_input_device(struct input *input)
1013{
1014 return input->input_device;
1015}
1016
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001017struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001018window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001019{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001020 cairo_device_flush (window->display->device);
1021
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001022 return wl_shell_create_drag(window->display->shell);
1023}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001024
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001025void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001026window_move(struct window *window, struct input *input, uint32_t time)
1027{
1028 wl_shell_move(window->display->shell,
1029 window->surface, input->input_device, time);
1030}
1031
1032void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001033window_activate_drag(struct wl_drag *drag, struct window *window,
1034 struct input *input, uint32_t time)
1035{
1036 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001037}
1038
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001039static void
1040handle_configure(void *data, struct wl_shell *shell,
1041 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001042 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001043{
1044 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001045 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001046
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001047 if (window->resize_handler) {
1048 child_width = width - 20 - window->margin * 2;
1049 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001050
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001051 (*window->resize_handler)(window,
1052 child_width, child_height,
1053 window->user_data);
1054 } else {
1055 window->resize_edges = edges;
1056 window->allocation.width = width;
1057 window->allocation.height = height;
1058
1059 if (window->redraw_handler)
1060 window_schedule_redraw(window);
1061 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001062}
1063
1064static const struct wl_shell_listener shell_listener = {
1065 handle_configure,
1066};
1067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001068void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001069window_get_child_allocation(struct window *window,
1070 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001071{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001072 if (window->fullscreen && !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001073 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001074 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001075 allocation->x = window->margin + 10;
1076 allocation->y = window->margin + 50;
1077 allocation->width =
1078 window->allocation.width - 20 - window->margin * 2;
1079 allocation->height =
1080 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001081 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001082}
1083
1084void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001085window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001086{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001087 if (!window->fullscreen) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001088 window->allocation.width = width + 20 + window->margin * 2;
1089 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001090 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001091}
1092
1093void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001094window_copy_image(struct window *window,
1095 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001096{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001097 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001098}
1099
1100void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001101window_copy_surface(struct window *window,
1102 struct rectangle *rectangle,
1103 cairo_surface_t *surface)
1104{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001105 cairo_t *cr;
1106
1107 cr = cairo_create (window->cairo_surface);
1108
1109 cairo_set_source_surface (cr,
1110 surface,
1111 rectangle->x, rectangle->y);
1112
1113 cairo_paint (cr);
1114 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001115}
1116
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001117static gboolean
1118idle_redraw(void *data)
1119{
1120 struct window *window = data;
1121
1122 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001123
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001124 window->redraw_scheduled = 0;
1125
1126 return FALSE;
1127}
1128
1129void
1130window_schedule_redraw(struct window *window)
1131{
1132 if (!window->redraw_scheduled) {
1133 g_idle_add(idle_redraw, window);
1134 window->redraw_scheduled = 1;
1135 }
1136}
1137
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001138void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001139window_set_fullscreen(struct window *window, int fullscreen)
1140{
1141 window->fullscreen = fullscreen;
1142 if (window->fullscreen) {
1143 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001144 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001145 } else {
1146 window->allocation = window->saved_allocation;
1147 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001148}
1149
1150void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151window_set_decoration(struct window *window, int decoration)
1152{
1153 window->decoration = decoration;
1154}
1155
1156void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001157window_set_user_data(struct window *window, void *data)
1158{
1159 window->user_data = data;
1160}
1161
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001162void *
1163window_get_user_data(struct window *window)
1164{
1165 return window->user_data;
1166}
1167
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001168void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001169window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001170 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001171{
1172 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001173}
1174
1175void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001176window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001177 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001178{
1179 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001180}
1181
1182void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001183window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001184 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001185{
1186 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001187}
1188
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001189void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001190window_set_button_handler(struct window *window,
1191 window_button_handler_t handler)
1192{
1193 window->button_handler = handler;
1194}
1195
1196void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001197window_set_motion_handler(struct window *window,
1198 window_motion_handler_t handler)
1199{
1200 window->motion_handler = handler;
1201}
1202
1203void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001204window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001205 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001206{
1207 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001208}
1209
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001210void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001211window_damage(struct window *window, int32_t x, int32_t y,
1212 int32_t width, int32_t height)
1213{
1214 wl_surface_damage(window->surface, x, y, width, height);
1215}
1216
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001217struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001218window_create(struct display *display, const char *title,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001219 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001220{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001221 struct window *window;
1222
1223 window = malloc(sizeof *window);
1224 if (window == NULL)
1225 return NULL;
1226
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001227 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001228 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001229 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001230 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001231 window->allocation.x = 0;
1232 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001233 window->allocation.width = width;
1234 window->allocation.height = height;
1235 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001236 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001237 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001238
Yuval Fledel45568f62010-12-06 09:18:12 -05001239 if (display->drm)
1240 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1241 else
1242 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001243
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001244 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001245 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001246
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001247 return window;
1248}
1249
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001250void
1251window_set_buffer_type(struct window *window, enum window_buffer_type type)
1252{
1253 window->buffer_type = type;
1254}
1255
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001256static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001257drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001258{
1259 struct display *d = data;
1260
1261 d->device_name = strdup(device);
1262}
1263
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001264static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1265{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001266 struct display *d = data;
1267
1268 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001269}
1270
1271static const struct wl_drm_listener drm_listener = {
1272 drm_handle_device,
1273 drm_handle_authenticated
1274};
1275
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001276static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001277display_handle_geometry(void *data,
1278 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001279 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001280{
1281 struct display *display = data;
1282
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001283 display->screen_allocation.x = x;
1284 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001285 display->screen_allocation.width = width;
1286 display->screen_allocation.height = height;
1287}
1288
1289static const struct wl_output_listener output_listener = {
1290 display_handle_geometry,
1291};
1292
1293static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001294display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001295{
1296 struct input *input;
1297
1298 input = malloc(sizeof *input);
1299 if (input == NULL)
1300 return;
1301
1302 memset(input, 0, sizeof *input);
1303 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001304 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001305 input->pointer_focus = NULL;
1306 input->keyboard_focus = NULL;
1307 wl_list_insert(d->input_list.prev, &input->link);
1308
1309 wl_input_device_add_listener(input->input_device,
1310 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001311 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001312}
1313
1314static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001315display_handle_global(struct wl_display *display, uint32_t id,
1316 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001317{
1318 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001319 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001320
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001321 if (strcmp(interface, "compositor") == 0) {
1322 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001323 } else if (strcmp(interface, "output") == 0) {
1324 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001325 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001326 } else if (strcmp(interface, "input_device") == 0) {
1327 display_add_input(d, id);
1328 } else if (strcmp(interface, "shell") == 0) {
1329 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001330 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001331 } else if (strcmp(interface, "drm") == 0) {
1332 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001333 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001334 } else if (strcmp(interface, "shm") == 0) {
1335 d->shm = wl_shm_create(display, id);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001336 } else if (strcmp(interface, "drag_offer") == 0) {
Kristian Høgsberg8857a962010-11-22 13:02:21 -05001337 if (d->drag_offer_handler) {
1338 offer = wl_drag_offer_create(display, id);
1339 d->drag_offer_handler(offer, d);
1340 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001341 }
1342}
1343
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001344static void
1345display_render_frame(struct display *d)
1346{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001347 int radius = 8;
1348 cairo_t *cr;
1349
1350 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1351 cr = cairo_create(d->shadow);
1352 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1353 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1354 rounded_rect(cr, 16, 16, 112, 112, radius);
1355 cairo_fill(cr);
1356 cairo_destroy(cr);
1357 blur_surface(d->shadow, 64);
1358
1359 d->active_frame =
1360 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1361 cr = cairo_create(d->active_frame);
1362 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1363 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1364 rounded_rect(cr, 16, 16, 112, 112, radius);
1365 cairo_fill(cr);
1366 cairo_destroy(cr);
1367
1368 d->inactive_frame =
1369 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1370 cr = cairo_create(d->inactive_frame);
1371 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1372 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1373 rounded_rect(cr, 16, 16, 112, 112, radius);
1374 cairo_fill(cr);
1375 cairo_destroy(cr);
1376}
1377
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001378static void
1379init_xkb(struct display *d)
1380{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001381 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001382
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001383 names.rules = "evdev";
1384 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001385 names.layout = option_xkb_layout;
1386 names.variant = option_xkb_variant;
1387 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001388
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001389 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001390 if (!d->xkb) {
1391 fprintf(stderr, "Failed to compile keymap\n");
1392 exit(1);
1393 }
1394}
1395
Yuval Fledel45568f62010-12-06 09:18:12 -05001396static int
1397init_drm(struct display *d)
1398{
1399 EGLint major, minor;
1400 int fd;
1401 drm_magic_t magic;
1402
1403 fd = open(d->device_name, O_RDWR);
1404 if (fd < 0) {
1405 fprintf(stderr, "drm open failed: %m\n");
1406 return -1;
1407 }
1408
1409 if (drmGetMagic(fd, &magic)) {
1410 fprintf(stderr, "DRI2: failed to get drm magic");
1411 return -1;
1412 }
1413
1414 /* Wait for authenticated event */
1415 wl_drm_authenticate(d->drm, magic);
1416 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1417 while (!d->authenticated)
1418 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1419
1420 d->dpy = eglGetDRMDisplayMESA(fd);
1421 if (!eglInitialize(d->dpy, &major, &minor)) {
1422 fprintf(stderr, "failed to initialize display\n");
1423 return -1;
1424 }
1425
1426 if (!eglBindAPI(EGL_OPENGL_API)) {
1427 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1428 return -1;
1429 }
1430
1431 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1432 if (d->ctx == NULL) {
1433 fprintf(stderr, "failed to create context\n");
1434 return -1;
1435 }
1436
1437 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1438 fprintf(stderr, "faile to make context current\n");
1439 return -1;
1440 }
1441
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001442#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001443 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1444 if (d->device == NULL) {
1445 fprintf(stderr, "failed to get cairo drm device\n");
1446 return -1;
1447 }
1448#endif
1449
1450 return 0;
1451}
1452
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001453struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001454display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001455{
1456 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001457 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001458 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001459 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001460
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001461 g_type_init();
1462
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001463 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001464 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001465 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001466
1467 xkb_option_group = g_option_group_new("xkb",
1468 "Keyboard options",
1469 "Show all XKB options",
1470 NULL, NULL);
1471 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1472 g_option_context_add_group (context, xkb_option_group);
1473
1474 if (!g_option_context_parse(context, argc, argv, &error)) {
1475 fprintf(stderr, "option parsing failed: %s\n", error->message);
1476 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001477 }
1478
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001479
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001480 d = malloc(sizeof *d);
1481 if (d == NULL)
1482 return NULL;
1483
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001484 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001485 if (d->display == NULL) {
1486 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001487 return NULL;
1488 }
1489
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001490 wl_list_init(&d->input_list);
1491
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001492 /* Set up listener so we'll catch all events. */
1493 wl_display_add_global_listener(d->display,
1494 display_handle_global, d);
1495
1496 /* Process connection events. */
1497 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1498
Yuval Fledel45568f62010-12-06 09:18:12 -05001499 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001500 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001501
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001502 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001503
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001504 display_render_frame(d);
1505
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001506 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001507 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001508 g_source_attach(d->source, NULL);
1509
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001510 wl_list_init(&d->window_list);
1511
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001512 init_xkb(d);
1513
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001514 return d;
1515}
1516
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001517struct wl_display *
1518display_get_display(struct display *display)
1519{
1520 return display->display;
1521}
1522
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001523struct wl_compositor *
1524display_get_compositor(struct display *display)
1525{
1526 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001527}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001528
1529EGLDisplay
1530display_get_egl_display(struct display *d)
1531{
1532 return d->dpy;
1533}
1534
1535void
1536display_run(struct display *d)
1537{
1538 g_main_loop_run(d->loop);
1539}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001540
1541void
1542display_set_drag_offer_handler(struct display *display,
1543 display_drag_offer_handler_t handler)
1544{
1545 display->drag_offer_handler = handler;
1546}