blob: 45bee61cf50c5f2338cb9d1af857783784b53a52 [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
46#ifdef HAVE_CAIRO_GL
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øgsberg83fc0612010-08-04 22:44:55 -040089 struct rectangle allocation, saved_allocation, pending_allocation;
90 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øgsberg78231c82008-11-08 15:06:01 -0500100
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500102 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500103
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500104 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400105 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500106 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400107 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500108 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400109 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500111 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400112 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500113};
114
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400115struct input {
116 struct display *display;
117 struct wl_input_device *input_device;
118 struct window *pointer_focus;
119 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400120 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400121 uint32_t modifiers;
122 int32_t x, y, sx, sy;
123 struct wl_list link;
124};
125
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400126enum {
127 POINTER_DEFAULT = 100,
128 POINTER_UNSET
129};
130
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400131const char *option_xkb_layout = "us";
132const char *option_xkb_variant = "";
133const char *option_xkb_options = "";
134
135static const GOptionEntry xkb_option_entries[] = {
136 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
137 &option_xkb_layout, "XKB Layout" },
138 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
139 &option_xkb_variant, "XKB Variant" },
140 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
141 &option_xkb_options, "XKB Options" },
142 { NULL }
143};
144
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500145static void
146rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
147{
148 cairo_move_to(cr, x0, y0 + radius);
149 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
150 cairo_line_to(cr, x1 - radius, y0);
151 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
152 cairo_line_to(cr, x1, y1 - radius);
153 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
154 cairo_line_to(cr, x0 + radius, y1);
155 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
156 cairo_close_path(cr);
157}
158
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400159static const cairo_user_data_key_t surface_data_key;
160struct surface_data {
161 struct wl_buffer *buffer;
162};
163
164#ifdef HAVE_CAIRO_GL
165
166struct drm_surface_data {
167 struct surface_data data;
168 EGLImageKHR image;
169 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800170 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400171};
172
173static void
174drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400175{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400176 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800177 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400178
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800179 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400180 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800181 cairo_device_release(d->device);
182
183 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400184 wl_buffer_destroy(data->data.buffer);
185}
186
187cairo_surface_t *
188display_create_drm_surface(struct display *display,
189 struct rectangle *rectangle)
190{
191 struct drm_surface_data *data;
192 EGLDisplay dpy = display->dpy;
193 cairo_surface_t *surface;
194 struct wl_visual *visual;
195 struct wl_buffer *buffer;
196 EGLint name, stride;
197
198 EGLint image_attribs[] = {
199 EGL_WIDTH, 0,
200 EGL_HEIGHT, 0,
201 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
202 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
203 EGL_NONE
204 };
205
206 data = malloc(sizeof *data);
207 if (data == NULL)
208 return NULL;
209
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800210 data->display = display;
211
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400212 image_attribs[1] = rectangle->width;
213 image_attribs[3] = rectangle->height;
214 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800215
216 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400217 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400218 glBindTexture(GL_TEXTURE_2D, data->texture);
219 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800220 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400221
222 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
223
224 visual = wl_display_get_premultiplied_argb_visual(display->display);
225 data->data.buffer =
226 wl_drm_create_buffer(display->drm, name, rectangle->width,
227 rectangle->height, stride, visual);
228
229 surface = cairo_gl_surface_create_for_texture(display->device,
230 CAIRO_CONTENT_COLOR_ALPHA,
231 data->texture,
232 rectangle->width,
233 rectangle->height);
234
235 cairo_surface_set_user_data (surface, &surface_data_key,
236 data, drm_surface_data_destroy);
237
238 return surface;
239}
240
241cairo_surface_t *
242display_create_drm_surface_from_file(struct display *display,
243 const char *filename,
244 struct rectangle *rect)
245{
246 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400247 GdkPixbuf *pixbuf;
248 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400249 int stride, i;
250 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800251 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400252
253 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400254 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400255 FALSE, &error);
256 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400257 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400258
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400259 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
260 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
261 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400262 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400263 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400264
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400265
266 stride = gdk_pixbuf_get_rowstride(pixbuf);
267 pixels = gdk_pixbuf_get_pixels(pixbuf);
268
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400269 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400270 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400271 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400272 while (p < end) {
273 unsigned int t;
274
275#define MULT(d,c,a,t) \
276 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
277
278 MULT(p[0], p[0], p[3], t);
279 MULT(p[1], p[1], p[3], t);
280 MULT(p[2], p[2], p[3], t);
281 p += 4;
282
283 }
284 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400286 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800287 data = cairo_surface_get_user_data(surface, &surface_data_key);
288
289 cairo_device_acquire(display->device);
290 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800291 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
292 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800293 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400294
295 gdk_pixbuf_unref(pixbuf);
296
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400297 return surface;
298}
299
300#endif
301
302struct wl_buffer *
303display_get_buffer_for_surface(struct display *display,
304 cairo_surface_t *surface)
305{
306 struct surface_data *data;
307
308 data = cairo_surface_get_user_data (surface, &surface_data_key);
309
310 return data->buffer;
311}
312
313struct shm_surface_data {
314 struct surface_data data;
315 void *map;
316 size_t length;
317};
318
319void
320shm_surface_data_destroy(void *p)
321{
322 struct shm_surface_data *data = p;
323
324 wl_buffer_destroy(data->data.buffer);
325 munmap(data->map, data->length);
326}
327
328cairo_surface_t *
329display_create_shm_surface(struct display *display,
330 struct rectangle *rectangle)
331{
332 struct shm_surface_data *data;
333 cairo_surface_t *surface;
334 struct wl_visual *visual;
335 int stride, alloc, fd;
336 char filename[] = "/tmp/wayland-shm-XXXXXX";
337
338 data = malloc(sizeof *data);
339 if (data == NULL)
340 return NULL;
341
342 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
343 rectangle->width);
344 data->length = stride * rectangle->height;
345 fd = mkstemp(filename);
346 if (fd < 0) {
347 fprintf(stderr, "open %s failed: %m", filename);
348 return NULL;
349 }
350 if (ftruncate(fd, data->length) < 0) {
351 fprintf(stderr, "ftruncate failed: %m");
352 close(fd);
353 return NULL;
354 }
355
356 data->map = mmap(NULL, data->length,
357 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
358 unlink(filename);
359
360 if (data->map == MAP_FAILED) {
361 fprintf(stderr, "mmap failed: %m");
362 close(fd);
363 return NULL;
364 }
365
366 surface = cairo_image_surface_create_for_data (data->map,
367 CAIRO_FORMAT_ARGB32,
368 rectangle->width,
369 rectangle->height,
370 stride);
371
372 cairo_surface_set_user_data (surface, &surface_data_key,
373 data, shm_surface_data_destroy);
374
375 visual = wl_display_get_premultiplied_argb_visual(display->display);
376 data->data.buffer = wl_shm_create_buffer(display->shm,
377 fd,
378 rectangle->width,
379 rectangle->height,
380 stride, visual);
381
382 close(fd);
383
384 return surface;
385}
386
387cairo_surface_t *
388display_create_shm_surface_from_file(struct display *display,
389 const char *filename,
390 struct rectangle *rect)
391{
392 cairo_surface_t *surface;
393 GdkPixbuf *pixbuf;
394 GError *error = NULL;
395 int stride, i;
396 unsigned char *pixels, *p, *end, *dest_data;
397 int dest_stride;
398 uint32_t *d;
399
400 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
401 rect->width, rect->height,
402 FALSE, &error);
403 if (error != NULL)
404 return NULL;
405
406 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
407 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
408 gdk_pixbuf_unref(pixbuf);
409 return NULL;
410 }
411
412 stride = gdk_pixbuf_get_rowstride(pixbuf);
413 pixels = gdk_pixbuf_get_pixels(pixbuf);
414
415 surface = display_create_shm_surface(display, rect);
416 dest_data = cairo_image_surface_get_data (surface);
417 dest_stride = cairo_image_surface_get_stride (surface);
418
419 for (i = 0; i < rect->height; i++) {
420 d = (uint32_t *) (dest_data + i * dest_stride);
421 p = pixels + i * stride;
422 end = p + rect->width * 4;
423 while (p < end) {
424 unsigned int t;
425 unsigned char a, r, g, b;
426
427#define MULT(_d,c,a,t) \
428 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
429
430 a = p[3];
431 MULT(r, p[0], a, t);
432 MULT(g, p[1], a, t);
433 MULT(b, p[2], a, t);
434 p += 4;
435 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
436 }
437 }
438
439 gdk_pixbuf_unref(pixbuf);
440
441 return surface;
442}
443
444cairo_surface_t *
445display_create_surface(struct display *display,
446 struct rectangle *rectangle)
447{
448#ifdef HAVE_CAIRO_GL
449 display_create_drm_surface(display, rectangle);
450#else
451 display_create_shm_surface(display, rectangle);
452#endif
453}
454
455cairo_surface_t *
456display_create_surface_from_file(struct display *display,
457 const char *filename,
458 struct rectangle *rectangle)
459{
460#ifdef HAVE_CAIRO_GL
461 display_create_drm_surface_from_file(display, filename, rectangle);
462#else
463 display_create_shm_surface_from_file(display, filename, rectangle);
464#endif
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465}
466
467static const struct {
468 const char *filename;
469 int hotspot_x, hotspot_y;
470} pointer_images[] = {
471 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
472 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
473 { DATADIR "/wayland/bottom_side.png", 16, 20 },
474 { DATADIR "/wayland/grabbing.png", 20, 17 },
475 { DATADIR "/wayland/left_ptr.png", 10, 5 },
476 { DATADIR "/wayland/left_side.png", 10, 20 },
477 { DATADIR "/wayland/right_side.png", 30, 19 },
478 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
479 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
480 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400481 { DATADIR "/wayland/xterm.png", 15, 15 },
482 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400483};
484
485static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400486create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400487{
488 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400489 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400490 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400491
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400492 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400493 display->pointer_surfaces =
494 malloc(count * sizeof *display->pointer_surfaces);
495 rect.width = width;
496 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400497 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400498 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499 display_create_surface_from_file(display,
500 pointer_images[i].filename,
501 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400502 }
503
504}
505
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400506cairo_surface_t *
507display_get_pointer_surface(struct display *display, int pointer,
508 int *width, int *height,
509 int *hotspot_x, int *hotspot_y)
510{
511 cairo_surface_t *surface;
512
513 surface = display->pointer_surfaces[pointer];
514 *width = cairo_gl_surface_get_width(surface);
515 *height = cairo_gl_surface_get_height(surface);
516 *hotspot_x = pointer_images[pointer].hotspot_x;
517 *hotspot_y = pointer_images[pointer].hotspot_y;
518
519 return cairo_surface_reference(surface);
520}
521
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400522
523static void
524window_attach_surface(struct window *window);
525
526static void
527free_surface(void *data)
528{
529 struct window *window = data;
530
531 cairo_surface_destroy(window->pending_surface);
532 window->pending_surface = NULL;
533 if (window->cairo_surface)
534 window_attach_surface(window);
535}
536
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500537static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500538window_attach_surface(struct window *window)
539{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400540 struct display *display = window->display;
541 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500542
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500543 if (window->pending_surface != NULL)
544 return;
545
Kristian Høgsberg09531622010-06-14 23:22:15 -0400546 window->pending_surface = window->cairo_surface;
547 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500548
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400549 buffer = display_get_buffer_for_surface(display,
550 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400551 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500552
553 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400554 window->allocation.x,
555 window->allocation.y,
556 window->allocation.width,
557 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400558
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400559 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500560}
561
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500562void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400563window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500564{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400565 if (window->cairo_surface)
566 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500567}
568
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400569void
570window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400572 if (window->cairo_surface != NULL)
573 cairo_surface_destroy(window->cairo_surface);
574
575 window->cairo_surface = cairo_surface_reference(surface);
576}
577
578void
579window_create_surface(struct window *window)
580{
581 cairo_surface_t *surface;
582
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583 switch (window->buffer_type) {
584#ifdef HAVE_CAIRO_GL
585 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400586 surface = display_create_surface(window->display,
587 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400588 break;
589#endif
590 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400591 surface = display_create_shm_surface(window->display,
592 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593 break;
594 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400595
596 window_set_surface(window, surface);
597 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598}
599
600static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500601window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500602{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500603 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500604 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400605 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400606 cairo_surface_t *frame;
607 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500608
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400609 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400610
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400611 width = window->allocation.width;
612 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500613
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500614 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500615 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500616 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
617
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500618 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500619
Kristian Høgsberg09531622010-06-14 23:22:15 -0400620 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400621 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400622 cairo_paint(cr);
623
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400624 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400625 tile_mask(cr, window->display->shadow,
626 shadow_dx, shadow_dy, width, height,
627 window->margin + 10 - shadow_dx,
628 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500629
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400630 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400631 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400632 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400633 frame = window->display->inactive_frame;
634
635 tile_source(cr, frame, 0, 0, width, height,
636 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500637
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500638 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
639 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500640 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400641 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500642 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
643 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
644 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400645 if (window->keyboard_device)
646 cairo_set_source_rgb(cr, 0, 0, 0);
647 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400648 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400649 cairo_show_text(cr, window->title);
650
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500651 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400652
653 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500654}
655
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400656void
657display_flush_cairo_device(struct display *display)
658{
659 cairo_device_flush (display->device);
660}
661
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500662void
663window_draw(struct window *window)
664{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400665 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400666 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500667 else
668 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500669}
670
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400671cairo_surface_t *
672window_get_surface(struct window *window)
673{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400674 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400675}
676
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400677enum window_location {
678 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400679 WINDOW_RESIZING_TOP = 1,
680 WINDOW_RESIZING_BOTTOM = 2,
681 WINDOW_RESIZING_LEFT = 4,
682 WINDOW_RESIZING_TOP_LEFT = 5,
683 WINDOW_RESIZING_BOTTOM_LEFT = 6,
684 WINDOW_RESIZING_RIGHT = 8,
685 WINDOW_RESIZING_TOP_RIGHT = 9,
686 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
687 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688 WINDOW_EXTERIOR = 16,
689 WINDOW_TITLEBAR = 17,
690 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691};
692
693static int
694get_pointer_location(struct window *window, int32_t x, int32_t y)
695{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697 const int grip_size = 8;
698
699 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701 else if (window->margin <= x && x < window->margin + grip_size)
702 hlocation = WINDOW_RESIZING_LEFT;
703 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400704 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400705 else if (x < window->allocation.width - window->margin)
706 hlocation = WINDOW_RESIZING_RIGHT;
707 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709
710 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400711 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400712 else if (window->margin <= y && y < window->margin + grip_size)
713 vlocation = WINDOW_RESIZING_TOP;
714 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400715 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400716 else if (y < window->allocation.height - window->margin)
717 vlocation = WINDOW_RESIZING_BOTTOM;
718 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400719 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400720
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400721 location = vlocation | hlocation;
722 if (location & WINDOW_EXTERIOR)
723 location = WINDOW_EXTERIOR;
724 if (location == WINDOW_INTERIOR && y < window->margin + 50)
725 location = WINDOW_TITLEBAR;
726 else if (location == WINDOW_INTERIOR)
727 location = WINDOW_CLIENT_AREA;
728
729 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400730}
731
732static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400733set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400734{
735 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400736 struct wl_buffer *buffer;
737 cairo_surface_t *surface;
738 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400739
740 location = get_pointer_location(input->pointer_focus,
741 input->sx, input->sy);
742 switch (location) {
743 case WINDOW_RESIZING_TOP:
744 pointer = POINTER_TOP;
745 break;
746 case WINDOW_RESIZING_BOTTOM:
747 pointer = POINTER_BOTTOM;
748 break;
749 case WINDOW_RESIZING_LEFT:
750 pointer = POINTER_LEFT;
751 break;
752 case WINDOW_RESIZING_RIGHT:
753 pointer = POINTER_RIGHT;
754 break;
755 case WINDOW_RESIZING_TOP_LEFT:
756 pointer = POINTER_TOP_LEFT;
757 break;
758 case WINDOW_RESIZING_TOP_RIGHT:
759 pointer = POINTER_TOP_RIGHT;
760 break;
761 case WINDOW_RESIZING_BOTTOM_LEFT:
762 pointer = POINTER_BOTTOM_LEFT;
763 break;
764 case WINDOW_RESIZING_BOTTOM_RIGHT:
765 pointer = POINTER_BOTTOM_RIGHT;
766 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400767 case WINDOW_EXTERIOR:
768 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400769 if (input->current_pointer_image == POINTER_DEFAULT)
770 return;
771
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400772 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400773 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400774 return;
775 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400776 break;
777 }
778
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400779 if (pointer == input->current_pointer_image)
780 return;
781
782 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400783 surface = display->pointer_surfaces[pointer];
784 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400785 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400786 pointer_images[pointer].hotspot_x,
787 pointer_images[pointer].hotspot_y);
788}
789
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500790static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500791window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400792 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500793 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500794{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400795 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400796 struct window *window = input->pointer_focus;
797 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400798
799 input->x = x;
800 input->y = y;
801 input->sx = sx;
802 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400803
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400804 location = get_pointer_location(window, input->sx, input->sy);
805
806 if (window->motion_handler)
807 pointer = (*window->motion_handler)(window, input, time,
808 x, y, sx, sy,
809 window->user_data);
810
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400811 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500812}
813
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400814static void
815window_handle_button(void *data,
816 struct wl_input_device *input_device,
817 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500818{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400819 struct input *input = data;
820 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400821 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400822
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400823 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500824
825 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400826 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400827 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400828 wl_shell_move(window->display->shell,
829 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500830 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400831 case WINDOW_RESIZING_TOP:
832 case WINDOW_RESIZING_BOTTOM:
833 case WINDOW_RESIZING_LEFT:
834 case WINDOW_RESIZING_RIGHT:
835 case WINDOW_RESIZING_TOP_LEFT:
836 case WINDOW_RESIZING_TOP_RIGHT:
837 case WINDOW_RESIZING_BOTTOM_LEFT:
838 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400839 wl_shell_resize(window->display->shell,
840 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400841 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500842 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400843 case WINDOW_CLIENT_AREA:
844 if (window->button_handler)
845 (*window->button_handler)(window,
846 input, time,
847 button, state,
848 window->user_data);
849 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500850 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400851 } else {
852 if (window->button_handler)
853 (*window->button_handler)(window,
854 input, time,
855 button, state,
856 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500857 }
858}
859
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500860static void
861window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400862 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500863{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400864 struct input *input = data;
865 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400866 struct display *d = window->display;
867 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500868
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400869 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400870 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500871 return;
872
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400873 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400874 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400875 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
876 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500877
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400878 sym = XkbKeySymEntry(d->xkb, code, level, 0);
879
880 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400881 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400882 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400883 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500884
885 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400886 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400887 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500888}
889
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500890static void
891window_handle_pointer_focus(void *data,
892 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400893 uint32_t time, struct wl_surface *surface,
894 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500895{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400896 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400897 struct window *window;
898 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400899
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400900 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400901 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400902 window = input->pointer_focus;
903
904 pointer = POINTER_LEFT_PTR;
905 if (window->motion_handler)
906 pointer = (*window->motion_handler)(window,
907 input, time,
908 x, y, sx, sy,
909 window->user_data);
910
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400911 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400912 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400913 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400914 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400915 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500916}
917
918static void
919window_handle_keyboard_focus(void *data,
920 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400921 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500922 struct wl_surface *surface,
923 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500924{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400925 struct input *input = data;
926 struct window *window = input->keyboard_focus;
927 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500928 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500929
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400930 window = input->keyboard_focus;
931 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500932 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400933 if (window->keyboard_focus_handler)
934 (*window->keyboard_focus_handler)(window, NULL,
935 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500936 }
937
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400938 if (surface)
939 input->keyboard_focus = wl_surface_get_user_data(surface);
940 else
941 input->keyboard_focus = NULL;
942
943 end = keys->data + keys->size;
944 for (k = keys->data; k < end; k++)
945 input->modifiers |= d->xkb->map->modmap[*k];
946
947 window = input->keyboard_focus;
948 if (window) {
949 window->keyboard_device = input;
950 if (window->keyboard_focus_handler)
951 (*window->keyboard_focus_handler)(window,
952 window->keyboard_device,
953 window->user_data);
954 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500955}
956
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500957static const struct wl_input_device_listener input_device_listener = {
958 window_handle_motion,
959 window_handle_button,
960 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500961 window_handle_pointer_focus,
962 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500963};
964
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400965void
966input_get_position(struct input *input, int32_t *x, int32_t *y)
967{
968 *x = input->sx;
969 *y = input->sy;
970}
971
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400972struct wl_input_device *
973input_get_input_device(struct input *input)
974{
975 return input->input_device;
976}
977
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400978struct wl_drag *
979window_start_drag(struct window *window, struct input *input, uint32_t time,
980 const struct wl_drag_listener *listener, void *data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400981{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400982 struct wl_drag *drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400983
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400984 cairo_device_flush (window->display->device);
985
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400986 drag = wl_shell_create_drag(window->display->shell);
987 wl_drag_offer(drag, "text/plain");
988 wl_drag_offer(drag, "text/html");
989 wl_drag_activate(drag, window->surface, input->input_device, time);
990 wl_drag_add_listener(drag, listener, data);
991
992 return drag;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400993}
994
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400995static void
996handle_configure(void *data, struct wl_shell *shell,
997 uint32_t time, uint32_t edges,
998 struct wl_surface *surface,
999 int32_t x, int32_t y, int32_t width, int32_t height)
1000{
1001 struct window *window = wl_surface_get_user_data(surface);
1002
1003 window->resize_edges = edges;
1004 window->pending_allocation.x = x;
1005 window->pending_allocation.y = y;
1006 window->pending_allocation.width = width;
1007 window->pending_allocation.height = height;
1008
1009 if (!(edges & 15))
1010 return;
1011
1012 if (window->resize_handler)
1013 (*window->resize_handler)(window,
1014 window->user_data);
1015 else if (window->redraw_handler)
1016 window_schedule_redraw(window);
1017}
1018
1019static const struct wl_shell_listener shell_listener = {
1020 handle_configure,
1021};
1022
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001023void
1024window_get_child_rectangle(struct window *window,
1025 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001026{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001027 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001028 *rectangle = window->allocation;
1029 } else {
1030 rectangle->x = window->margin + 10;
1031 rectangle->y = window->margin + 50;
1032 rectangle->width = window->allocation.width - 20 - window->margin * 2;
1033 rectangle->height = window->allocation.height - 60 - window->margin * 2;
1034 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001035}
1036
1037void
Kristian Høgsberg22106762008-12-08 13:50:07 -05001038window_set_child_size(struct window *window,
1039 struct rectangle *rectangle)
1040{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001041 int32_t width, height;
1042
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001043 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001044 width = rectangle->width + 20 + window->margin * 2;
1045 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001046
1047 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1048 window->allocation.x +=
1049 window->allocation.width - width;
1050 if (window->resize_edges & WINDOW_RESIZING_TOP)
1051 window->allocation.y +=
1052 window->allocation.height - height;
1053
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001054 window->allocation.width = width;
1055 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001056 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001057}
1058
1059void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001060window_copy_image(struct window *window,
1061 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001062{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001063 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001064}
1065
1066void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001067window_copy_surface(struct window *window,
1068 struct rectangle *rectangle,
1069 cairo_surface_t *surface)
1070{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001071 cairo_t *cr;
1072
1073 cr = cairo_create (window->cairo_surface);
1074
1075 cairo_set_source_surface (cr,
1076 surface,
1077 rectangle->x, rectangle->y);
1078
1079 cairo_paint (cr);
1080 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001081}
1082
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001083static gboolean
1084idle_redraw(void *data)
1085{
1086 struct window *window = data;
1087
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001088 if (window->resize_edges)
1089 window->allocation = window->pending_allocation;
1090
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001091 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001092
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001093 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001094 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001095
1096 return FALSE;
1097}
1098
1099void
1100window_schedule_redraw(struct window *window)
1101{
1102 if (!window->redraw_scheduled) {
1103 g_idle_add(idle_redraw, window);
1104 window->redraw_scheduled = 1;
1105 }
1106}
1107
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001108void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001109window_set_fullscreen(struct window *window, int fullscreen)
1110{
1111 window->fullscreen = fullscreen;
1112 if (window->fullscreen) {
1113 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001114 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001115 } else {
1116 window->allocation = window->saved_allocation;
1117 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001118}
1119
1120void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001121window_set_decoration(struct window *window, int decoration)
1122{
1123 window->decoration = decoration;
1124}
1125
1126void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001127window_set_user_data(struct window *window, void *data)
1128{
1129 window->user_data = data;
1130}
1131
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001132void *
1133window_get_user_data(struct window *window)
1134{
1135 return window->user_data;
1136}
1137
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001138void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001139window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001140 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001141{
1142 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001143}
1144
1145void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001146window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001147 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001148{
1149 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001150}
1151
1152void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001153window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001154 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001155{
1156 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001157}
1158
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001159void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001160window_set_button_handler(struct window *window,
1161 window_button_handler_t handler)
1162{
1163 window->button_handler = handler;
1164}
1165
1166void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001167window_set_motion_handler(struct window *window,
1168 window_motion_handler_t handler)
1169{
1170 window->motion_handler = handler;
1171}
1172
1173void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001174window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001175 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001176{
1177 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001178}
1179
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001180void
1181window_move(struct window *window, int32_t x, int32_t y)
1182{
1183 window->allocation.x = x;
1184 window->allocation.y = y;
1185
1186 wl_surface_map(window->surface,
1187 window->allocation.x - window->margin,
1188 window->allocation.y - window->margin,
1189 window->allocation.width,
1190 window->allocation.height);
1191}
1192
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001193void
1194window_damage(struct window *window, int32_t x, int32_t y,
1195 int32_t width, int32_t height)
1196{
1197 wl_surface_damage(window->surface, x, y, width, height);
1198}
1199
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001200struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001201window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001202 int32_t x, int32_t y, int32_t width, int32_t height)
1203{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001204 struct window *window;
1205
1206 window = malloc(sizeof *window);
1207 if (window == NULL)
1208 return NULL;
1209
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001210 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001211 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001212 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001213 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001214 window->allocation.x = x;
1215 window->allocation.y = y;
1216 window->allocation.width = width;
1217 window->allocation.height = height;
1218 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001219 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001220 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001221
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001222#ifdef HAVE_CAIRO_GL
1223 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1224#else
1225 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1226#endif
1227
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001228 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001229 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001230
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001231 return window;
1232}
1233
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001234void
1235window_set_buffer_type(struct window *window, enum window_buffer_type type)
1236{
1237 window->buffer_type = type;
1238}
1239
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001240static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001241drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001242{
1243 struct display *d = data;
1244
1245 d->device_name = strdup(device);
1246}
1247
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001248static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1249{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001250 struct display *d = data;
1251
1252 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001253}
1254
1255static const struct wl_drm_listener drm_listener = {
1256 drm_handle_device,
1257 drm_handle_authenticated
1258};
1259
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001260static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001261display_handle_geometry(void *data,
1262 struct wl_output *output,
1263 int32_t width, int32_t height)
1264{
1265 struct display *display = data;
1266
1267 display->screen_allocation.x = 0;
1268 display->screen_allocation.y = 0;
1269 display->screen_allocation.width = width;
1270 display->screen_allocation.height = height;
1271}
1272
1273static const struct wl_output_listener output_listener = {
1274 display_handle_geometry,
1275};
1276
1277static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001278display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001279{
1280 struct input *input;
1281
1282 input = malloc(sizeof *input);
1283 if (input == NULL)
1284 return;
1285
1286 memset(input, 0, sizeof *input);
1287 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001288 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001289 input->pointer_focus = NULL;
1290 input->keyboard_focus = NULL;
1291 wl_list_insert(d->input_list.prev, &input->link);
1292
1293 wl_input_device_add_listener(input->input_device,
1294 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001296}
1297
1298static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001299display_handle_global(struct wl_display *display, uint32_t id,
1300 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001301{
1302 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001303 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001304
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001305 if (strcmp(interface, "compositor") == 0) {
1306 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001307 } else if (strcmp(interface, "output") == 0) {
1308 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001309 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001310 } else if (strcmp(interface, "input_device") == 0) {
1311 display_add_input(d, id);
1312 } else if (strcmp(interface, "shell") == 0) {
1313 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001314 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001315 } else if (strcmp(interface, "drm") == 0) {
1316 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001317 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001318 } else if (strcmp(interface, "shm") == 0) {
1319 d->shm = wl_shm_create(display, id);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001320 } else if (strcmp(interface, "drag_offer") == 0) {
1321 offer = wl_drag_offer_create(display, id);
1322 d->drag_offer_handler(offer, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001323 }
1324}
1325
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001326static const char socket_name[] = "\0wayland";
1327
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001328static void
1329display_render_frame(struct display *d)
1330{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001331 int radius = 8;
1332 cairo_t *cr;
1333
1334 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1335 cr = cairo_create(d->shadow);
1336 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1337 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1338 rounded_rect(cr, 16, 16, 112, 112, radius);
1339 cairo_fill(cr);
1340 cairo_destroy(cr);
1341 blur_surface(d->shadow, 64);
1342
1343 d->active_frame =
1344 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1345 cr = cairo_create(d->active_frame);
1346 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1347 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1348 rounded_rect(cr, 16, 16, 112, 112, radius);
1349 cairo_fill(cr);
1350 cairo_destroy(cr);
1351
1352 d->inactive_frame =
1353 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1354 cr = cairo_create(d->inactive_frame);
1355 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1356 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1357 rounded_rect(cr, 16, 16, 112, 112, radius);
1358 cairo_fill(cr);
1359 cairo_destroy(cr);
1360}
1361
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001362static void
1363init_xkb(struct display *d)
1364{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001365 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001366
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001367 names.rules = "evdev";
1368 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001369 names.layout = option_xkb_layout;
1370 names.variant = option_xkb_variant;
1371 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001372
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001373 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001374 if (!d->xkb) {
1375 fprintf(stderr, "Failed to compile keymap\n");
1376 exit(1);
1377 }
1378}
1379
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001380struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001381display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001382{
1383 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001384 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001385 int fd;
1386 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001387 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001388 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001389 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001390
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001391 g_type_init();
1392
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001393 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001394 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001395 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001396
1397 xkb_option_group = g_option_group_new("xkb",
1398 "Keyboard options",
1399 "Show all XKB options",
1400 NULL, NULL);
1401 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1402 g_option_context_add_group (context, xkb_option_group);
1403
1404 if (!g_option_context_parse(context, argc, argv, &error)) {
1405 fprintf(stderr, "option parsing failed: %s\n", error->message);
1406 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001407 }
1408
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001409
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001410 d = malloc(sizeof *d);
1411 if (d == NULL)
1412 return NULL;
1413
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001414 d->display = wl_display_create(socket_name, sizeof socket_name);
1415 if (d->display == NULL) {
1416 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001417 return NULL;
1418 }
1419
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001420 wl_list_init(&d->input_list);
1421
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001422 /* Set up listener so we'll catch all events. */
1423 wl_display_add_global_listener(d->display,
1424 display_handle_global, d);
1425
1426 /* Process connection events. */
1427 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1428
1429 fd = open(d->device_name, O_RDWR);
1430 if (fd < 0) {
1431 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001432 return NULL;
1433 }
1434
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001435 if (drmGetMagic(fd, &magic)) {
1436 fprintf(stderr, "DRI2: failed to get drm magic");
1437 return NULL;
1438 }
1439
1440 /* Wait for authenticated event */
1441 wl_drm_authenticate(d->drm, magic);
1442 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1443 while (!d->authenticated)
1444 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1445
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001446 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001447 if (!eglInitialize(d->dpy, &major, &minor)) {
1448 fprintf(stderr, "failed to initialize display\n");
1449 return NULL;
1450 }
1451
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001452 eglBindAPI(EGL_OPENGL_API);
1453
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001454 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001455 if (d->ctx == NULL) {
1456 fprintf(stderr, "failed to create context\n");
1457 return NULL;
1458 }
1459
1460 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1461 fprintf(stderr, "faile to make context current\n");
1462 return NULL;
1463 }
1464
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001465#ifdef HAVE_CAIRO_GL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001466 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001467 if (d->device == NULL) {
1468 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001469 return NULL;
1470 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001471#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001472
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001473 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001474
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001475 display_render_frame(d);
1476
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001477 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001478 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001479 g_source_attach(d->source, NULL);
1480
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001481 wl_list_init(&d->window_list);
1482
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001483 init_xkb(d);
1484
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001485 return d;
1486}
1487
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001488struct wl_display *
1489display_get_display(struct display *display)
1490{
1491 return display->display;
1492}
1493
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001494struct wl_compositor *
1495display_get_compositor(struct display *display)
1496{
1497 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001498}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001499
1500EGLDisplay
1501display_get_egl_display(struct display *d)
1502{
1503 return d->dpy;
1504}
1505
1506void
1507display_run(struct display *d)
1508{
1509 g_main_loop_run(d->loop);
1510}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001511
1512void
1513display_set_drag_offer_handler(struct display *display,
1514 display_drag_offer_handler_t handler)
1515{
1516 display->drag_offer_handler = handler;
1517}