blob: 3abc2b0ba1e264c94f3a48746611f3ea6b7670b6 [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
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500187EGLImageKHR
188display_get_image_for_drm_surface(struct display *display,
189 cairo_surface_t *surface)
190{
191 struct drm_surface_data *data;
192
193 data = cairo_surface_get_user_data (surface, &surface_data_key);
194
195 return data->image;
196}
197
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400198cairo_surface_t *
199display_create_drm_surface(struct display *display,
200 struct rectangle *rectangle)
201{
202 struct drm_surface_data *data;
203 EGLDisplay dpy = display->dpy;
204 cairo_surface_t *surface;
205 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400206 EGLint name, stride;
207
208 EGLint image_attribs[] = {
209 EGL_WIDTH, 0,
210 EGL_HEIGHT, 0,
211 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
212 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
213 EGL_NONE
214 };
215
216 data = malloc(sizeof *data);
217 if (data == NULL)
218 return NULL;
219
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800220 data->display = display;
221
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400222 image_attribs[1] = rectangle->width;
223 image_attribs[3] = rectangle->height;
224 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800225
226 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400227 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400228 glBindTexture(GL_TEXTURE_2D, data->texture);
229 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800230 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400231
232 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
233
234 visual = wl_display_get_premultiplied_argb_visual(display->display);
235 data->data.buffer =
236 wl_drm_create_buffer(display->drm, name, rectangle->width,
237 rectangle->height, stride, visual);
238
239 surface = cairo_gl_surface_create_for_texture(display->device,
240 CAIRO_CONTENT_COLOR_ALPHA,
241 data->texture,
242 rectangle->width,
243 rectangle->height);
244
245 cairo_surface_set_user_data (surface, &surface_data_key,
246 data, drm_surface_data_destroy);
247
248 return surface;
249}
250
251cairo_surface_t *
252display_create_drm_surface_from_file(struct display *display,
253 const char *filename,
254 struct rectangle *rect)
255{
256 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400257 GdkPixbuf *pixbuf;
258 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400259 int stride, i;
260 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800261 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400262
263 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400264 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400265 FALSE, &error);
266 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400267 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400268
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400269 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
270 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
271 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400273 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400274
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400275
276 stride = gdk_pixbuf_get_rowstride(pixbuf);
277 pixels = gdk_pixbuf_get_pixels(pixbuf);
278
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400279 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400280 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400281 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400282 while (p < end) {
283 unsigned int t;
284
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500285#define MULT(_d,c,a,t) \
286 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400287
288 MULT(p[0], p[0], p[3], t);
289 MULT(p[1], p[1], p[3], t);
290 MULT(p[2], p[2], p[3], t);
291 p += 4;
292
293 }
294 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400295
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400296 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800297 data = cairo_surface_get_user_data(surface, &surface_data_key);
298
299 cairo_device_acquire(display->device);
300 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800301 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
302 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800303 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400304
305 gdk_pixbuf_unref(pixbuf);
306
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400307 return surface;
308}
309
310#endif
311
312struct wl_buffer *
313display_get_buffer_for_surface(struct display *display,
314 cairo_surface_t *surface)
315{
316 struct surface_data *data;
317
318 data = cairo_surface_get_user_data (surface, &surface_data_key);
319
320 return data->buffer;
321}
322
323struct shm_surface_data {
324 struct surface_data data;
325 void *map;
326 size_t length;
327};
328
329void
330shm_surface_data_destroy(void *p)
331{
332 struct shm_surface_data *data = p;
333
334 wl_buffer_destroy(data->data.buffer);
335 munmap(data->map, data->length);
336}
337
338cairo_surface_t *
339display_create_shm_surface(struct display *display,
340 struct rectangle *rectangle)
341{
342 struct shm_surface_data *data;
343 cairo_surface_t *surface;
344 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800345 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400346 char filename[] = "/tmp/wayland-shm-XXXXXX";
347
348 data = malloc(sizeof *data);
349 if (data == NULL)
350 return NULL;
351
352 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
353 rectangle->width);
354 data->length = stride * rectangle->height;
355 fd = mkstemp(filename);
356 if (fd < 0) {
357 fprintf(stderr, "open %s failed: %m", filename);
358 return NULL;
359 }
360 if (ftruncate(fd, data->length) < 0) {
361 fprintf(stderr, "ftruncate failed: %m");
362 close(fd);
363 return NULL;
364 }
365
366 data->map = mmap(NULL, data->length,
367 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
368 unlink(filename);
369
370 if (data->map == MAP_FAILED) {
371 fprintf(stderr, "mmap failed: %m");
372 close(fd);
373 return NULL;
374 }
375
376 surface = cairo_image_surface_create_for_data (data->map,
377 CAIRO_FORMAT_ARGB32,
378 rectangle->width,
379 rectangle->height,
380 stride);
381
382 cairo_surface_set_user_data (surface, &surface_data_key,
383 data, shm_surface_data_destroy);
384
385 visual = wl_display_get_premultiplied_argb_visual(display->display);
386 data->data.buffer = wl_shm_create_buffer(display->shm,
387 fd,
388 rectangle->width,
389 rectangle->height,
390 stride, visual);
391
392 close(fd);
393
394 return surface;
395}
396
397cairo_surface_t *
398display_create_shm_surface_from_file(struct display *display,
399 const char *filename,
400 struct rectangle *rect)
401{
402 cairo_surface_t *surface;
403 GdkPixbuf *pixbuf;
404 GError *error = NULL;
405 int stride, i;
406 unsigned char *pixels, *p, *end, *dest_data;
407 int dest_stride;
408 uint32_t *d;
409
410 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
411 rect->width, rect->height,
412 FALSE, &error);
413 if (error != NULL)
414 return NULL;
415
416 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
417 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
418 gdk_pixbuf_unref(pixbuf);
419 return NULL;
420 }
421
422 stride = gdk_pixbuf_get_rowstride(pixbuf);
423 pixels = gdk_pixbuf_get_pixels(pixbuf);
424
425 surface = display_create_shm_surface(display, rect);
426 dest_data = cairo_image_surface_get_data (surface);
427 dest_stride = cairo_image_surface_get_stride (surface);
428
429 for (i = 0; i < rect->height; i++) {
430 d = (uint32_t *) (dest_data + i * dest_stride);
431 p = pixels + i * stride;
432 end = p + rect->width * 4;
433 while (p < end) {
434 unsigned int t;
435 unsigned char a, r, g, b;
436
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 a = p[3];
438 MULT(r, p[0], a, t);
439 MULT(g, p[1], a, t);
440 MULT(b, p[2], a, t);
441 p += 4;
442 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
443 }
444 }
445
446 gdk_pixbuf_unref(pixbuf);
447
448 return surface;
449}
450
451cairo_surface_t *
452display_create_surface(struct display *display,
453 struct rectangle *rectangle)
454{
455#ifdef HAVE_CAIRO_GL
Bryce Harrington156d0572010-11-19 12:14:39 -0800456 return display_create_drm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457#else
Bryce Harrington156d0572010-11-19 12:14:39 -0800458 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459#endif
460}
461
462cairo_surface_t *
463display_create_surface_from_file(struct display *display,
464 const char *filename,
465 struct rectangle *rectangle)
466{
467#ifdef HAVE_CAIRO_GL
Bryce Harrington156d0572010-11-19 12:14:39 -0800468 return display_create_drm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469#else
Bryce Harrington156d0572010-11-19 12:14:39 -0800470 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400471#endif
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400472}
473
474static const struct {
475 const char *filename;
476 int hotspot_x, hotspot_y;
477} pointer_images[] = {
478 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
479 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
480 { DATADIR "/wayland/bottom_side.png", 16, 20 },
481 { DATADIR "/wayland/grabbing.png", 20, 17 },
482 { DATADIR "/wayland/left_ptr.png", 10, 5 },
483 { DATADIR "/wayland/left_side.png", 10, 20 },
484 { DATADIR "/wayland/right_side.png", 30, 19 },
485 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
486 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
487 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400488 { DATADIR "/wayland/xterm.png", 15, 15 },
489 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400490};
491
492static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400493create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494{
495 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400496 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400497 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400498
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400499 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400500 display->pointer_surfaces =
501 malloc(count * sizeof *display->pointer_surfaces);
502 rect.width = width;
503 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400504 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400505 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506 display_create_surface_from_file(display,
507 pointer_images[i].filename,
508 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400509 }
510
511}
512
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400513cairo_surface_t *
514display_get_pointer_surface(struct display *display, int pointer,
515 int *width, int *height,
516 int *hotspot_x, int *hotspot_y)
517{
518 cairo_surface_t *surface;
519
520 surface = display->pointer_surfaces[pointer];
521 *width = cairo_gl_surface_get_width(surface);
522 *height = cairo_gl_surface_get_height(surface);
523 *hotspot_x = pointer_images[pointer].hotspot_x;
524 *hotspot_y = pointer_images[pointer].hotspot_y;
525
526 return cairo_surface_reference(surface);
527}
528
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400529
530static void
531window_attach_surface(struct window *window);
532
533static void
534free_surface(void *data)
535{
536 struct window *window = data;
537
538 cairo_surface_destroy(window->pending_surface);
539 window->pending_surface = NULL;
540 if (window->cairo_surface)
541 window_attach_surface(window);
542}
543
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500544static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500545window_attach_surface(struct window *window)
546{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400547 struct display *display = window->display;
548 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500549
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500550 if (window->pending_surface != NULL)
551 return;
552
Kristian Høgsberg09531622010-06-14 23:22:15 -0400553 window->pending_surface = window->cairo_surface;
554 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500555
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400556 buffer = display_get_buffer_for_surface(display,
557 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400558 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500559
560 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400561 window->allocation.x,
562 window->allocation.y,
563 window->allocation.width,
564 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400565
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400566 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500567}
568
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500569void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400570window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500571{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400572 if (window->cairo_surface)
573 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500574}
575
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400576void
577window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500579 cairo_surface_reference(surface);
580
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400581 if (window->cairo_surface != NULL)
582 cairo_surface_destroy(window->cairo_surface);
583
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500584 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400585}
586
587void
588window_create_surface(struct window *window)
589{
590 cairo_surface_t *surface;
591
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592 switch (window->buffer_type) {
593#ifdef HAVE_CAIRO_GL
594 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400595 surface = display_create_surface(window->display,
596 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597 break;
598#endif
599 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400600 surface = display_create_shm_surface(window->display,
601 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800603 default:
604 surface = NULL;
605 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400607
608 window_set_surface(window, surface);
609 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400610}
611
612static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500613window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500614{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500615 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500616 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400617 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400618 cairo_surface_t *frame;
619 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500620
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400621 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400623 width = window->allocation.width;
624 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500625
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500626 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500627 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500628 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
629
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500630 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500631
Kristian Høgsberg09531622010-06-14 23:22:15 -0400632 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400633 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400634 cairo_paint(cr);
635
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400636 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400637 tile_mask(cr, window->display->shadow,
638 shadow_dx, shadow_dy, width, height,
639 window->margin + 10 - shadow_dx,
640 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500641
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400642 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400643 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400644 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400645 frame = window->display->inactive_frame;
646
647 tile_source(cr, frame, 0, 0, width, height,
648 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500649
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500650 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
651 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500652 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400653 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500654 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
655 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
656 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400657 if (window->keyboard_device)
658 cairo_set_source_rgb(cr, 0, 0, 0);
659 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400660 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400661 cairo_show_text(cr, window->title);
662
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500663 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400664
665 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500666}
667
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400668void
669display_flush_cairo_device(struct display *display)
670{
671 cairo_device_flush (display->device);
672}
673
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500674void
675window_draw(struct window *window)
676{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400677 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400678 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500679 else
680 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500681}
682
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400683cairo_surface_t *
684window_get_surface(struct window *window)
685{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400686 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400687}
688
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689enum window_location {
690 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400691 WINDOW_RESIZING_TOP = 1,
692 WINDOW_RESIZING_BOTTOM = 2,
693 WINDOW_RESIZING_LEFT = 4,
694 WINDOW_RESIZING_TOP_LEFT = 5,
695 WINDOW_RESIZING_BOTTOM_LEFT = 6,
696 WINDOW_RESIZING_RIGHT = 8,
697 WINDOW_RESIZING_TOP_RIGHT = 9,
698 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
699 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 WINDOW_EXTERIOR = 16,
701 WINDOW_TITLEBAR = 17,
702 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400703};
704
705static int
706get_pointer_location(struct window *window, int32_t x, int32_t y)
707{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709 const int grip_size = 8;
710
711 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400712 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400713 else if (window->margin <= x && x < window->margin + grip_size)
714 hlocation = WINDOW_RESIZING_LEFT;
715 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400716 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400717 else if (x < window->allocation.width - window->margin)
718 hlocation = WINDOW_RESIZING_RIGHT;
719 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400720 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400721
722 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400723 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400724 else if (window->margin <= y && y < window->margin + grip_size)
725 vlocation = WINDOW_RESIZING_TOP;
726 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400727 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400728 else if (y < window->allocation.height - window->margin)
729 vlocation = WINDOW_RESIZING_BOTTOM;
730 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400731 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400732
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733 location = vlocation | hlocation;
734 if (location & WINDOW_EXTERIOR)
735 location = WINDOW_EXTERIOR;
736 if (location == WINDOW_INTERIOR && y < window->margin + 50)
737 location = WINDOW_TITLEBAR;
738 else if (location == WINDOW_INTERIOR)
739 location = WINDOW_CLIENT_AREA;
740
741 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400742}
743
744static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400745set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400746{
747 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400748 struct wl_buffer *buffer;
749 cairo_surface_t *surface;
750 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400751
752 location = get_pointer_location(input->pointer_focus,
753 input->sx, input->sy);
754 switch (location) {
755 case WINDOW_RESIZING_TOP:
756 pointer = POINTER_TOP;
757 break;
758 case WINDOW_RESIZING_BOTTOM:
759 pointer = POINTER_BOTTOM;
760 break;
761 case WINDOW_RESIZING_LEFT:
762 pointer = POINTER_LEFT;
763 break;
764 case WINDOW_RESIZING_RIGHT:
765 pointer = POINTER_RIGHT;
766 break;
767 case WINDOW_RESIZING_TOP_LEFT:
768 pointer = POINTER_TOP_LEFT;
769 break;
770 case WINDOW_RESIZING_TOP_RIGHT:
771 pointer = POINTER_TOP_RIGHT;
772 break;
773 case WINDOW_RESIZING_BOTTOM_LEFT:
774 pointer = POINTER_BOTTOM_LEFT;
775 break;
776 case WINDOW_RESIZING_BOTTOM_RIGHT:
777 pointer = POINTER_BOTTOM_RIGHT;
778 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400779 case WINDOW_EXTERIOR:
780 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400781 if (input->current_pointer_image == POINTER_DEFAULT)
782 return;
783
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400784 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400785 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400786 return;
787 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400788 break;
789 }
790
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400791 if (pointer == input->current_pointer_image)
792 return;
793
794 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400795 surface = display->pointer_surfaces[pointer];
796 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400797 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400798 pointer_images[pointer].hotspot_x,
799 pointer_images[pointer].hotspot_y);
800}
801
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500802static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500803window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400804 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500805 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500806{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400807 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400808 struct window *window = input->pointer_focus;
809 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400810
811 input->x = x;
812 input->y = y;
813 input->sx = sx;
814 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400815
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400816 location = get_pointer_location(window, input->sx, input->sy);
817
818 if (window->motion_handler)
819 pointer = (*window->motion_handler)(window, input, time,
820 x, y, sx, sy,
821 window->user_data);
822
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400823 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500824}
825
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400826static void
827window_handle_button(void *data,
828 struct wl_input_device *input_device,
829 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500830{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400831 struct input *input = data;
832 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400833 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400834
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400835 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500836
837 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400838 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400839 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400840 wl_shell_move(window->display->shell,
841 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500842 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400843 case WINDOW_RESIZING_TOP:
844 case WINDOW_RESIZING_BOTTOM:
845 case WINDOW_RESIZING_LEFT:
846 case WINDOW_RESIZING_RIGHT:
847 case WINDOW_RESIZING_TOP_LEFT:
848 case WINDOW_RESIZING_TOP_RIGHT:
849 case WINDOW_RESIZING_BOTTOM_LEFT:
850 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400851 wl_shell_resize(window->display->shell,
852 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400853 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500854 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400855 case WINDOW_CLIENT_AREA:
856 if (window->button_handler)
857 (*window->button_handler)(window,
858 input, time,
859 button, state,
860 window->user_data);
861 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500862 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400863 } else {
864 if (window->button_handler)
865 (*window->button_handler)(window,
866 input, time,
867 button, state,
868 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500869 }
870}
871
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500872static void
873window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400874 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500875{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400876 struct input *input = data;
877 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400878 struct display *d = window->display;
879 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500880
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400881 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400882 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500883 return;
884
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400885 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400886 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400887 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
888 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500889
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400890 sym = XkbKeySymEntry(d->xkb, code, level, 0);
891
892 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400893 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400894 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400895 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500896
897 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400898 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400899 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500900}
901
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500902static void
903window_handle_pointer_focus(void *data,
904 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400905 uint32_t time, struct wl_surface *surface,
906 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500907{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400908 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400909 struct window *window;
910 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400911
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400912 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400913 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400914 window = input->pointer_focus;
915
916 pointer = POINTER_LEFT_PTR;
917 if (window->motion_handler)
918 pointer = (*window->motion_handler)(window,
919 input, time,
920 x, y, sx, sy,
921 window->user_data);
922
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400923 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400924 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400925 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400926 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400927 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500928}
929
930static void
931window_handle_keyboard_focus(void *data,
932 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400933 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500934 struct wl_surface *surface,
935 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500936{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400937 struct input *input = data;
938 struct window *window = input->keyboard_focus;
939 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500940 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500941
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400942 window = input->keyboard_focus;
943 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500944 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400945 if (window->keyboard_focus_handler)
946 (*window->keyboard_focus_handler)(window, NULL,
947 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500948 }
949
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400950 if (surface)
951 input->keyboard_focus = wl_surface_get_user_data(surface);
952 else
953 input->keyboard_focus = NULL;
954
955 end = keys->data + keys->size;
956 for (k = keys->data; k < end; k++)
957 input->modifiers |= d->xkb->map->modmap[*k];
958
959 window = input->keyboard_focus;
960 if (window) {
961 window->keyboard_device = input;
962 if (window->keyboard_focus_handler)
963 (*window->keyboard_focus_handler)(window,
964 window->keyboard_device,
965 window->user_data);
966 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500967}
968
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500969static const struct wl_input_device_listener input_device_listener = {
970 window_handle_motion,
971 window_handle_button,
972 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500973 window_handle_pointer_focus,
974 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500975};
976
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400977void
978input_get_position(struct input *input, int32_t *x, int32_t *y)
979{
980 *x = input->sx;
981 *y = input->sy;
982}
983
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400984struct wl_input_device *
985input_get_input_device(struct input *input)
986{
987 return input->input_device;
988}
989
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400990struct wl_drag *
991window_start_drag(struct window *window, struct input *input, uint32_t time,
992 const struct wl_drag_listener *listener, void *data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400993{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400994 struct wl_drag *drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400995
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400996 cairo_device_flush (window->display->device);
997
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400998 drag = wl_shell_create_drag(window->display->shell);
999 wl_drag_offer(drag, "text/plain");
1000 wl_drag_offer(drag, "text/html");
1001 wl_drag_activate(drag, window->surface, input->input_device, time);
1002 wl_drag_add_listener(drag, listener, data);
1003
1004 return drag;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001005}
1006
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001007static void
1008handle_configure(void *data, struct wl_shell *shell,
1009 uint32_t time, uint32_t edges,
1010 struct wl_surface *surface,
1011 int32_t x, int32_t y, int32_t width, int32_t height)
1012{
1013 struct window *window = wl_surface_get_user_data(surface);
1014
1015 window->resize_edges = edges;
1016 window->pending_allocation.x = x;
1017 window->pending_allocation.y = y;
1018 window->pending_allocation.width = width;
1019 window->pending_allocation.height = height;
1020
1021 if (!(edges & 15))
1022 return;
1023
1024 if (window->resize_handler)
1025 (*window->resize_handler)(window,
1026 window->user_data);
1027 else if (window->redraw_handler)
1028 window_schedule_redraw(window);
1029}
1030
1031static const struct wl_shell_listener shell_listener = {
1032 handle_configure,
1033};
1034
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001035void
1036window_get_child_rectangle(struct window *window,
1037 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001038{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001039 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001040 *rectangle = window->allocation;
1041 } else {
1042 rectangle->x = window->margin + 10;
1043 rectangle->y = window->margin + 50;
1044 rectangle->width = window->allocation.width - 20 - window->margin * 2;
1045 rectangle->height = window->allocation.height - 60 - window->margin * 2;
1046 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001047}
1048
1049void
Kristian Høgsberg22106762008-12-08 13:50:07 -05001050window_set_child_size(struct window *window,
1051 struct rectangle *rectangle)
1052{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001053 int32_t width, height;
1054
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001055 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001056 width = rectangle->width + 20 + window->margin * 2;
1057 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001058
1059 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1060 window->allocation.x +=
1061 window->allocation.width - width;
1062 if (window->resize_edges & WINDOW_RESIZING_TOP)
1063 window->allocation.y +=
1064 window->allocation.height - height;
1065
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001066 window->allocation.width = width;
1067 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001068 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001069}
1070
1071void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001072window_copy_image(struct window *window,
1073 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001074{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001075 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001076}
1077
1078void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001079window_copy_surface(struct window *window,
1080 struct rectangle *rectangle,
1081 cairo_surface_t *surface)
1082{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001083 cairo_t *cr;
1084
1085 cr = cairo_create (window->cairo_surface);
1086
1087 cairo_set_source_surface (cr,
1088 surface,
1089 rectangle->x, rectangle->y);
1090
1091 cairo_paint (cr);
1092 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001093}
1094
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001095static gboolean
1096idle_redraw(void *data)
1097{
1098 struct window *window = data;
1099
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001100 if (window->resize_edges)
1101 window->allocation = window->pending_allocation;
1102
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001103 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001104
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001105 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001106 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001107
1108 return FALSE;
1109}
1110
1111void
1112window_schedule_redraw(struct window *window)
1113{
1114 if (!window->redraw_scheduled) {
1115 g_idle_add(idle_redraw, window);
1116 window->redraw_scheduled = 1;
1117 }
1118}
1119
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001120void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001121window_set_fullscreen(struct window *window, int fullscreen)
1122{
1123 window->fullscreen = fullscreen;
1124 if (window->fullscreen) {
1125 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001126 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001127 } else {
1128 window->allocation = window->saved_allocation;
1129 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001130}
1131
1132void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001133window_set_decoration(struct window *window, int decoration)
1134{
1135 window->decoration = decoration;
1136}
1137
1138void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001139window_set_user_data(struct window *window, void *data)
1140{
1141 window->user_data = data;
1142}
1143
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001144void *
1145window_get_user_data(struct window *window)
1146{
1147 return window->user_data;
1148}
1149
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001150void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001151window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001152 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001153{
1154 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001155}
1156
1157void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001158window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001159 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001160{
1161 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001162}
1163
1164void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001165window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001166 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001167{
1168 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001169}
1170
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001171void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001172window_set_button_handler(struct window *window,
1173 window_button_handler_t handler)
1174{
1175 window->button_handler = handler;
1176}
1177
1178void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001179window_set_motion_handler(struct window *window,
1180 window_motion_handler_t handler)
1181{
1182 window->motion_handler = handler;
1183}
1184
1185void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001186window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001187 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001188{
1189 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001190}
1191
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001192void
1193window_move(struct window *window, int32_t x, int32_t y)
1194{
1195 window->allocation.x = x;
1196 window->allocation.y = y;
1197
1198 wl_surface_map(window->surface,
1199 window->allocation.x - window->margin,
1200 window->allocation.y - window->margin,
1201 window->allocation.width,
1202 window->allocation.height);
1203}
1204
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001205void
1206window_damage(struct window *window, int32_t x, int32_t y,
1207 int32_t width, int32_t height)
1208{
1209 wl_surface_damage(window->surface, x, y, width, height);
1210}
1211
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001212struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001213window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001214 int32_t x, int32_t y, int32_t width, int32_t height)
1215{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001216 struct window *window;
1217
1218 window = malloc(sizeof *window);
1219 if (window == NULL)
1220 return NULL;
1221
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001222 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001223 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001224 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001225 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001226 window->allocation.x = x;
1227 window->allocation.y = y;
1228 window->allocation.width = width;
1229 window->allocation.height = height;
1230 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001231 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001232 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001233
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001234#ifdef HAVE_CAIRO_GL
1235 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1236#else
1237 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1238#endif
1239
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001240 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001241 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001242
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001243 return window;
1244}
1245
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001246void
1247window_set_buffer_type(struct window *window, enum window_buffer_type type)
1248{
1249 window->buffer_type = type;
1250}
1251
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001252static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001253drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001254{
1255 struct display *d = data;
1256
1257 d->device_name = strdup(device);
1258}
1259
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001260static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1261{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001262 struct display *d = data;
1263
1264 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001265}
1266
1267static const struct wl_drm_listener drm_listener = {
1268 drm_handle_device,
1269 drm_handle_authenticated
1270};
1271
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001272static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001273display_handle_geometry(void *data,
1274 struct wl_output *output,
1275 int32_t width, int32_t height)
1276{
1277 struct display *display = data;
1278
1279 display->screen_allocation.x = 0;
1280 display->screen_allocation.y = 0;
1281 display->screen_allocation.width = width;
1282 display->screen_allocation.height = height;
1283}
1284
1285static const struct wl_output_listener output_listener = {
1286 display_handle_geometry,
1287};
1288
1289static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001290display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001291{
1292 struct input *input;
1293
1294 input = malloc(sizeof *input);
1295 if (input == NULL)
1296 return;
1297
1298 memset(input, 0, sizeof *input);
1299 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001300 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001301 input->pointer_focus = NULL;
1302 input->keyboard_focus = NULL;
1303 wl_list_insert(d->input_list.prev, &input->link);
1304
1305 wl_input_device_add_listener(input->input_device,
1306 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001308}
1309
1310static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001311display_handle_global(struct wl_display *display, uint32_t id,
1312 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001313{
1314 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001315 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001316
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001317 if (strcmp(interface, "compositor") == 0) {
1318 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001319 } else if (strcmp(interface, "output") == 0) {
1320 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001321 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001322 } else if (strcmp(interface, "input_device") == 0) {
1323 display_add_input(d, id);
1324 } else if (strcmp(interface, "shell") == 0) {
1325 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001326 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001327 } else if (strcmp(interface, "drm") == 0) {
1328 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001329 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001330 } else if (strcmp(interface, "shm") == 0) {
1331 d->shm = wl_shm_create(display, id);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001332 } else if (strcmp(interface, "drag_offer") == 0) {
1333 offer = wl_drag_offer_create(display, id);
1334 d->drag_offer_handler(offer, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001335 }
1336}
1337
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001338static const char socket_name[] = "\0wayland";
1339
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001340static void
1341display_render_frame(struct display *d)
1342{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001343 int radius = 8;
1344 cairo_t *cr;
1345
1346 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1347 cr = cairo_create(d->shadow);
1348 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1349 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1350 rounded_rect(cr, 16, 16, 112, 112, radius);
1351 cairo_fill(cr);
1352 cairo_destroy(cr);
1353 blur_surface(d->shadow, 64);
1354
1355 d->active_frame =
1356 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1357 cr = cairo_create(d->active_frame);
1358 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1359 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1360 rounded_rect(cr, 16, 16, 112, 112, radius);
1361 cairo_fill(cr);
1362 cairo_destroy(cr);
1363
1364 d->inactive_frame =
1365 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1366 cr = cairo_create(d->inactive_frame);
1367 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1368 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1369 rounded_rect(cr, 16, 16, 112, 112, radius);
1370 cairo_fill(cr);
1371 cairo_destroy(cr);
1372}
1373
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001374static void
1375init_xkb(struct display *d)
1376{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001377 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001378
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001379 names.rules = "evdev";
1380 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001381 names.layout = option_xkb_layout;
1382 names.variant = option_xkb_variant;
1383 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001384
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001385 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001386 if (!d->xkb) {
1387 fprintf(stderr, "Failed to compile keymap\n");
1388 exit(1);
1389 }
1390}
1391
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001392struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001393display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001394{
1395 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001396 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001397 int fd;
1398 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001399 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001400 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001401 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001402
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001403 g_type_init();
1404
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001405 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001406 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001407 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001408
1409 xkb_option_group = g_option_group_new("xkb",
1410 "Keyboard options",
1411 "Show all XKB options",
1412 NULL, NULL);
1413 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1414 g_option_context_add_group (context, xkb_option_group);
1415
1416 if (!g_option_context_parse(context, argc, argv, &error)) {
1417 fprintf(stderr, "option parsing failed: %s\n", error->message);
1418 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001419 }
1420
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001421
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001422 d = malloc(sizeof *d);
1423 if (d == NULL)
1424 return NULL;
1425
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001426 d->display = wl_display_create(socket_name, sizeof socket_name);
1427 if (d->display == NULL) {
1428 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001429 return NULL;
1430 }
1431
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001432 wl_list_init(&d->input_list);
1433
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001434 /* Set up listener so we'll catch all events. */
1435 wl_display_add_global_listener(d->display,
1436 display_handle_global, d);
1437
1438 /* Process connection events. */
1439 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1440
1441 fd = open(d->device_name, O_RDWR);
1442 if (fd < 0) {
1443 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001444 return NULL;
1445 }
1446
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001447 if (drmGetMagic(fd, &magic)) {
1448 fprintf(stderr, "DRI2: failed to get drm magic");
1449 return NULL;
1450 }
1451
1452 /* Wait for authenticated event */
1453 wl_drm_authenticate(d->drm, magic);
1454 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1455 while (!d->authenticated)
1456 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1457
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001458 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001459 if (!eglInitialize(d->dpy, &major, &minor)) {
1460 fprintf(stderr, "failed to initialize display\n");
1461 return NULL;
1462 }
1463
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001464 eglBindAPI(EGL_OPENGL_API);
1465
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001466 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001467 if (d->ctx == NULL) {
1468 fprintf(stderr, "failed to create context\n");
1469 return NULL;
1470 }
1471
1472 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1473 fprintf(stderr, "faile to make context current\n");
1474 return NULL;
1475 }
1476
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001477#ifdef HAVE_CAIRO_GL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001478 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001479 if (d->device == NULL) {
1480 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001481 return NULL;
1482 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001483#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001484
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001485 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001486
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001487 display_render_frame(d);
1488
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001489 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001490 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001491 g_source_attach(d->source, NULL);
1492
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001493 wl_list_init(&d->window_list);
1494
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001495 init_xkb(d);
1496
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001497 return d;
1498}
1499
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001500struct wl_display *
1501display_get_display(struct display *display)
1502{
1503 return display->display;
1504}
1505
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001506struct wl_compositor *
1507display_get_compositor(struct display *display)
1508{
1509 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001510}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001511
1512EGLDisplay
1513display_get_egl_display(struct display *d)
1514{
1515 return d->dpy;
1516}
1517
1518void
1519display_run(struct display *d)
1520{
1521 g_main_loop_run(d->loop);
1522}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001523
1524void
1525display_set_drag_offer_handler(struct display *display,
1526 display_drag_offer_handler_t handler)
1527{
1528 display->drag_offer_handler = handler;
1529}