blob: 22cbd28754dfc38fe74a367a4f28226a957ee64e [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øgsberg82da52b2010-12-17 09:53:12 -050089 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040091 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050092 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050093 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050094 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040096 struct input *grab_device;
97 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050098 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040099 enum window_buffer_type buffer_type;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500100 int mapped;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500101
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500103 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500104
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500105 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400106 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500107 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400108 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500109 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400111
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500112 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400113 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500114};
115
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400116struct input {
117 struct display *display;
118 struct wl_input_device *input_device;
119 struct window *pointer_focus;
120 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400121 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122 uint32_t modifiers;
123 int32_t x, y, sx, sy;
124 struct wl_list link;
125};
126
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400127enum {
128 POINTER_DEFAULT = 100,
129 POINTER_UNSET
130};
131
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500132enum window_location {
133 WINDOW_INTERIOR = 0,
134 WINDOW_RESIZING_TOP = 1,
135 WINDOW_RESIZING_BOTTOM = 2,
136 WINDOW_RESIZING_LEFT = 4,
137 WINDOW_RESIZING_TOP_LEFT = 5,
138 WINDOW_RESIZING_BOTTOM_LEFT = 6,
139 WINDOW_RESIZING_RIGHT = 8,
140 WINDOW_RESIZING_TOP_RIGHT = 9,
141 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
142 WINDOW_RESIZING_MASK = 15,
143 WINDOW_EXTERIOR = 16,
144 WINDOW_TITLEBAR = 17,
145 WINDOW_CLIENT_AREA = 18,
146};
147
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400148const char *option_xkb_layout = "us";
149const char *option_xkb_variant = "";
150const char *option_xkb_options = "";
151
152static const GOptionEntry xkb_option_entries[] = {
153 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
154 &option_xkb_layout, "XKB Layout" },
155 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
156 &option_xkb_variant, "XKB Variant" },
157 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
158 &option_xkb_options, "XKB Options" },
159 { NULL }
160};
161
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500162static void
163rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
164{
165 cairo_move_to(cr, x0, y0 + radius);
166 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
167 cairo_line_to(cr, x1 - radius, y0);
168 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
169 cairo_line_to(cr, x1, y1 - radius);
170 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
171 cairo_line_to(cr, x0 + radius, y1);
172 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
173 cairo_close_path(cr);
174}
175
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400176static const cairo_user_data_key_t surface_data_key;
177struct surface_data {
178 struct wl_buffer *buffer;
179};
180
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500181#define MULT(_d,c,a,t) \
182 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
183
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400184#ifdef HAVE_CAIRO_GL
185
186struct drm_surface_data {
187 struct surface_data data;
188 EGLImageKHR image;
189 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800190 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191};
192
193static void
194drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400195{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400196 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800197 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400198
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800199 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400200 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800201 cairo_device_release(d->device);
202
203 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400204 wl_buffer_destroy(data->data.buffer);
205}
206
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500207EGLImageKHR
208display_get_image_for_drm_surface(struct display *display,
209 cairo_surface_t *surface)
210{
211 struct drm_surface_data *data;
212
213 data = cairo_surface_get_user_data (surface, &surface_data_key);
214
215 return data->image;
216}
217
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500218static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400219display_create_drm_surface(struct display *display,
220 struct rectangle *rectangle)
221{
222 struct drm_surface_data *data;
223 EGLDisplay dpy = display->dpy;
224 cairo_surface_t *surface;
225 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400226 EGLint name, stride;
227
228 EGLint image_attribs[] = {
229 EGL_WIDTH, 0,
230 EGL_HEIGHT, 0,
231 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
232 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
233 EGL_NONE
234 };
235
236 data = malloc(sizeof *data);
237 if (data == NULL)
238 return NULL;
239
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800240 data->display = display;
241
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400242 image_attribs[1] = rectangle->width;
243 image_attribs[3] = rectangle->height;
244 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800245
246 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400248 glBindTexture(GL_TEXTURE_2D, data->texture);
249 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800250 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251
252 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
253
254 visual = wl_display_get_premultiplied_argb_visual(display->display);
255 data->data.buffer =
256 wl_drm_create_buffer(display->drm, name, rectangle->width,
257 rectangle->height, stride, visual);
258
259 surface = cairo_gl_surface_create_for_texture(display->device,
260 CAIRO_CONTENT_COLOR_ALPHA,
261 data->texture,
262 rectangle->width,
263 rectangle->height);
264
265 cairo_surface_set_user_data (surface, &surface_data_key,
266 data, drm_surface_data_destroy);
267
268 return surface;
269}
270
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500271static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272display_create_drm_surface_from_file(struct display *display,
273 const char *filename,
274 struct rectangle *rect)
275{
276 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400277 GdkPixbuf *pixbuf;
278 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400279 int stride, i;
280 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800281 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400282
283 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400284 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285 FALSE, &error);
286 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400287 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400288
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400289 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
290 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
291 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400293 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400294
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400295
296 stride = gdk_pixbuf_get_rowstride(pixbuf);
297 pixels = gdk_pixbuf_get_pixels(pixbuf);
298
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400299 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400300 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400302 while (p < end) {
303 unsigned int t;
304
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400305 MULT(p[0], p[0], p[3], t);
306 MULT(p[1], p[1], p[3], t);
307 MULT(p[2], p[2], p[3], t);
308 p += 4;
309
310 }
311 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400312
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800314 data = cairo_surface_get_user_data(surface, &surface_data_key);
315
316 cairo_device_acquire(display->device);
317 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800318 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
319 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800320 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321
322 gdk_pixbuf_unref(pixbuf);
323
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324 return surface;
325}
326
327#endif
328
329struct wl_buffer *
330display_get_buffer_for_surface(struct display *display,
331 cairo_surface_t *surface)
332{
333 struct surface_data *data;
334
335 data = cairo_surface_get_user_data (surface, &surface_data_key);
336
337 return data->buffer;
338}
339
340struct shm_surface_data {
341 struct surface_data data;
342 void *map;
343 size_t length;
344};
345
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500346static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400347shm_surface_data_destroy(void *p)
348{
349 struct shm_surface_data *data = p;
350
351 wl_buffer_destroy(data->data.buffer);
352 munmap(data->map, data->length);
353}
354
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500355static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356display_create_shm_surface(struct display *display,
357 struct rectangle *rectangle)
358{
359 struct shm_surface_data *data;
360 cairo_surface_t *surface;
361 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800362 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400363 char filename[] = "/tmp/wayland-shm-XXXXXX";
364
365 data = malloc(sizeof *data);
366 if (data == NULL)
367 return NULL;
368
369 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
370 rectangle->width);
371 data->length = stride * rectangle->height;
372 fd = mkstemp(filename);
373 if (fd < 0) {
374 fprintf(stderr, "open %s failed: %m", filename);
375 return NULL;
376 }
377 if (ftruncate(fd, data->length) < 0) {
378 fprintf(stderr, "ftruncate failed: %m");
379 close(fd);
380 return NULL;
381 }
382
383 data->map = mmap(NULL, data->length,
384 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
385 unlink(filename);
386
387 if (data->map == MAP_FAILED) {
388 fprintf(stderr, "mmap failed: %m");
389 close(fd);
390 return NULL;
391 }
392
393 surface = cairo_image_surface_create_for_data (data->map,
394 CAIRO_FORMAT_ARGB32,
395 rectangle->width,
396 rectangle->height,
397 stride);
398
399 cairo_surface_set_user_data (surface, &surface_data_key,
400 data, shm_surface_data_destroy);
401
402 visual = wl_display_get_premultiplied_argb_visual(display->display);
403 data->data.buffer = wl_shm_create_buffer(display->shm,
404 fd,
405 rectangle->width,
406 rectangle->height,
407 stride, visual);
408
409 close(fd);
410
411 return surface;
412}
413
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500414static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415display_create_shm_surface_from_file(struct display *display,
416 const char *filename,
417 struct rectangle *rect)
418{
419 cairo_surface_t *surface;
420 GdkPixbuf *pixbuf;
421 GError *error = NULL;
422 int stride, i;
423 unsigned char *pixels, *p, *end, *dest_data;
424 int dest_stride;
425 uint32_t *d;
426
427 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
428 rect->width, rect->height,
429 FALSE, &error);
430 if (error != NULL)
431 return NULL;
432
433 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
434 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
435 gdk_pixbuf_unref(pixbuf);
436 return NULL;
437 }
438
439 stride = gdk_pixbuf_get_rowstride(pixbuf);
440 pixels = gdk_pixbuf_get_pixels(pixbuf);
441
442 surface = display_create_shm_surface(display, rect);
443 dest_data = cairo_image_surface_get_data (surface);
444 dest_stride = cairo_image_surface_get_stride (surface);
445
446 for (i = 0; i < rect->height; i++) {
447 d = (uint32_t *) (dest_data + i * dest_stride);
448 p = pixels + i * stride;
449 end = p + rect->width * 4;
450 while (p < end) {
451 unsigned int t;
452 unsigned char a, r, g, b;
453
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454 a = p[3];
455 MULT(r, p[0], a, t);
456 MULT(g, p[1], a, t);
457 MULT(b, p[2], a, t);
458 p += 4;
459 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
460 }
461 }
462
463 gdk_pixbuf_unref(pixbuf);
464
465 return surface;
466}
467
468cairo_surface_t *
469display_create_surface(struct display *display,
470 struct rectangle *rectangle)
471{
472#ifdef HAVE_CAIRO_GL
Yuval Fledel45568f62010-12-06 09:18:12 -0500473 if (display->drm) {
474 return display_create_drm_surface(display, rectangle);
475 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400476#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500477 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400478}
479
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500480static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400481display_create_surface_from_file(struct display *display,
482 const char *filename,
483 struct rectangle *rectangle)
484{
485#ifdef HAVE_CAIRO_GL
Yuval Fledel45568f62010-12-06 09:18:12 -0500486 if (display->drm) {
487 return display_create_drm_surface_from_file(display, filename, rectangle);
488 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500490 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400491}
Yuval Fledel45568f62010-12-06 09:18:12 -0500492 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400493 const char *filename;
494 int hotspot_x, hotspot_y;
495} pointer_images[] = {
496 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
497 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
498 { DATADIR "/wayland/bottom_side.png", 16, 20 },
499 { DATADIR "/wayland/grabbing.png", 20, 17 },
500 { DATADIR "/wayland/left_ptr.png", 10, 5 },
501 { DATADIR "/wayland/left_side.png", 10, 20 },
502 { DATADIR "/wayland/right_side.png", 30, 19 },
503 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
504 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
505 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400506 { DATADIR "/wayland/xterm.png", 15, 15 },
507 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400508};
509
510static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400511create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400512{
513 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400514 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400515 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400516
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400517 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400518 display->pointer_surfaces =
519 malloc(count * sizeof *display->pointer_surfaces);
520 rect.width = width;
521 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400522 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400523 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 display_create_surface_from_file(display,
525 pointer_images[i].filename,
526 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400527 }
528
529}
530
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400531cairo_surface_t *
532display_get_pointer_surface(struct display *display, int pointer,
533 int *width, int *height,
534 int *hotspot_x, int *hotspot_y)
535{
536 cairo_surface_t *surface;
537
538 surface = display->pointer_surfaces[pointer];
539 *width = cairo_gl_surface_get_width(surface);
540 *height = cairo_gl_surface_get_height(surface);
541 *hotspot_x = pointer_images[pointer].hotspot_x;
542 *hotspot_y = pointer_images[pointer].hotspot_y;
543
544 return cairo_surface_reference(surface);
545}
546
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400547
548static void
549window_attach_surface(struct window *window);
550
551static void
552free_surface(void *data)
553{
554 struct window *window = data;
555
556 cairo_surface_destroy(window->pending_surface);
557 window->pending_surface = NULL;
558 if (window->cairo_surface)
559 window_attach_surface(window);
560}
561
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500562static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500563window_attach_surface(struct window *window)
564{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400565 struct display *display = window->display;
566 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500567 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500568
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500569 if (window->pending_surface != NULL)
570 return;
571
Kristian Høgsberg09531622010-06-14 23:22:15 -0400572 window->pending_surface = window->cairo_surface;
573 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500574
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400575 buffer = display_get_buffer_for_surface(display,
576 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500577 if (window->resize_edges & WINDOW_RESIZING_LEFT)
578 x = window->server_allocation.width -
579 window->allocation.width;
580 else
581 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500582
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500583 if (window->resize_edges & WINDOW_RESIZING_TOP)
584 y = window->server_allocation.height -
585 window->allocation.height;
586 else
587 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400588
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500589 window->server_allocation = window->allocation;
590 window->resize_edges = 0;
591 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400592 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500593
594 if (!window->mapped) {
595 wl_surface_map_toplevel(window->surface);
596 window->mapped = 1;
597 }
598
599 wl_surface_damage(window->surface, 0, 0,
600 window->allocation.width,
601 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500602}
603
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500604void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400605window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500606{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400607 if (window->cairo_surface)
608 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500609}
610
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400611void
612window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500614 cairo_surface_reference(surface);
615
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400616 if (window->cairo_surface != NULL)
617 cairo_surface_destroy(window->cairo_surface);
618
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500619 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400620}
621
622void
623window_create_surface(struct window *window)
624{
625 cairo_surface_t *surface;
626
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627 switch (window->buffer_type) {
628#ifdef HAVE_CAIRO_GL
629 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400630 surface = display_create_surface(window->display,
631 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632 break;
633#endif
634 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400635 surface = display_create_shm_surface(window->display,
636 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800638 default:
639 surface = NULL;
640 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400641 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400642
643 window_set_surface(window, surface);
644 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645}
646
647static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500648window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500649{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500650 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500651 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400652 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400653 cairo_surface_t *frame;
654 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500655
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400656 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400658 width = window->allocation.width;
659 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500660
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500661 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500662 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500663 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
664
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500665 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500666
Kristian Høgsberg09531622010-06-14 23:22:15 -0400667 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400668 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400669 cairo_paint(cr);
670
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400671 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672 tile_mask(cr, window->display->shadow,
673 shadow_dx, shadow_dy, width, height,
674 window->margin + 10 - shadow_dx,
675 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500676
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400677 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400679 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680 frame = window->display->inactive_frame;
681
682 tile_source(cr, frame, 0, 0, width, height,
683 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500684
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500685 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
686 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500687 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400688 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500689 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
690 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
691 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400692 if (window->keyboard_device)
693 cairo_set_source_rgb(cr, 0, 0, 0);
694 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400695 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400696 cairo_show_text(cr, window->title);
697
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500698 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400699
700 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500701}
702
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400703void
704display_flush_cairo_device(struct display *display)
705{
706 cairo_device_flush (display->device);
707}
708
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500709void
710window_draw(struct window *window)
711{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400712 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400713 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500714 else
715 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500716}
717
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400718cairo_surface_t *
719window_get_surface(struct window *window)
720{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400721 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400722}
723
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400724static int
725get_pointer_location(struct window *window, int32_t x, int32_t y)
726{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400727 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400728 const int grip_size = 8;
729
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500730 if (!window->decoration)
731 return WINDOW_CLIENT_AREA;
732
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400733 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400735 else if (window->margin <= x && x < window->margin + grip_size)
736 hlocation = WINDOW_RESIZING_LEFT;
737 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400738 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400739 else if (x < window->allocation.width - window->margin)
740 hlocation = WINDOW_RESIZING_RIGHT;
741 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400743
744 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400745 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400746 else if (window->margin <= y && y < window->margin + grip_size)
747 vlocation = WINDOW_RESIZING_TOP;
748 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400749 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400750 else if (y < window->allocation.height - window->margin)
751 vlocation = WINDOW_RESIZING_BOTTOM;
752 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400753 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400754
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400755 location = vlocation | hlocation;
756 if (location & WINDOW_EXTERIOR)
757 location = WINDOW_EXTERIOR;
758 if (location == WINDOW_INTERIOR && y < window->margin + 50)
759 location = WINDOW_TITLEBAR;
760 else if (location == WINDOW_INTERIOR)
761 location = WINDOW_CLIENT_AREA;
762
763 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400764}
765
766static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400767set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400768{
769 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400770 struct wl_buffer *buffer;
771 cairo_surface_t *surface;
772 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400773
774 location = get_pointer_location(input->pointer_focus,
775 input->sx, input->sy);
776 switch (location) {
777 case WINDOW_RESIZING_TOP:
778 pointer = POINTER_TOP;
779 break;
780 case WINDOW_RESIZING_BOTTOM:
781 pointer = POINTER_BOTTOM;
782 break;
783 case WINDOW_RESIZING_LEFT:
784 pointer = POINTER_LEFT;
785 break;
786 case WINDOW_RESIZING_RIGHT:
787 pointer = POINTER_RIGHT;
788 break;
789 case WINDOW_RESIZING_TOP_LEFT:
790 pointer = POINTER_TOP_LEFT;
791 break;
792 case WINDOW_RESIZING_TOP_RIGHT:
793 pointer = POINTER_TOP_RIGHT;
794 break;
795 case WINDOW_RESIZING_BOTTOM_LEFT:
796 pointer = POINTER_BOTTOM_LEFT;
797 break;
798 case WINDOW_RESIZING_BOTTOM_RIGHT:
799 pointer = POINTER_BOTTOM_RIGHT;
800 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400801 case WINDOW_EXTERIOR:
802 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400803 if (input->current_pointer_image == POINTER_DEFAULT)
804 return;
805
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400806 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400807 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400808 return;
809 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400810 break;
811 }
812
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400813 if (pointer == input->current_pointer_image)
814 return;
815
816 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400817 surface = display->pointer_surfaces[pointer];
818 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400819 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400820 pointer_images[pointer].hotspot_x,
821 pointer_images[pointer].hotspot_y);
822}
823
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500824static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500825window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400826 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500827 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500828{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400829 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400830 struct window *window = input->pointer_focus;
831 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400832
833 input->x = x;
834 input->y = y;
835 input->sx = sx;
836 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400837
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400838 location = get_pointer_location(window, input->sx, input->sy);
839
840 if (window->motion_handler)
841 pointer = (*window->motion_handler)(window, input, time,
842 x, y, sx, sy,
843 window->user_data);
844
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400845 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500846}
847
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400848static void
849window_handle_button(void *data,
850 struct wl_input_device *input_device,
851 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500852{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400853 struct input *input = data;
854 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400855 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400856
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400857 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500858
859 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400860 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400861 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400862 wl_shell_move(window->display->shell,
863 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500864 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400865 case WINDOW_RESIZING_TOP:
866 case WINDOW_RESIZING_BOTTOM:
867 case WINDOW_RESIZING_LEFT:
868 case WINDOW_RESIZING_RIGHT:
869 case WINDOW_RESIZING_TOP_LEFT:
870 case WINDOW_RESIZING_TOP_RIGHT:
871 case WINDOW_RESIZING_BOTTOM_LEFT:
872 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400873 wl_shell_resize(window->display->shell,
874 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400875 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500876 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400877 case WINDOW_CLIENT_AREA:
878 if (window->button_handler)
879 (*window->button_handler)(window,
880 input, time,
881 button, state,
882 window->user_data);
883 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500884 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400885 } else {
886 if (window->button_handler)
887 (*window->button_handler)(window,
888 input, time,
889 button, state,
890 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500891 }
892}
893
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500894static void
895window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400896 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500897{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400898 struct input *input = data;
899 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400900 struct display *d = window->display;
901 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500902
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400903 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400904 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500905 return;
906
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400907 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400908 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400909 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
910 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500911
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400912 sym = XkbKeySymEntry(d->xkb, code, level, 0);
913
914 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400915 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400916 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400917 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500918
919 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400920 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400921 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500922}
923
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500924static void
925window_handle_pointer_focus(void *data,
926 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400927 uint32_t time, struct wl_surface *surface,
928 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500929{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400930 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400931 struct window *window;
932 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400933
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400934 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400935 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400936 window = input->pointer_focus;
937
938 pointer = POINTER_LEFT_PTR;
939 if (window->motion_handler)
940 pointer = (*window->motion_handler)(window,
941 input, time,
942 x, y, sx, sy,
943 window->user_data);
944
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400945 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400946 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400947 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400948 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400949 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500950}
951
952static void
953window_handle_keyboard_focus(void *data,
954 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400955 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500956 struct wl_surface *surface,
957 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500958{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400959 struct input *input = data;
960 struct window *window = input->keyboard_focus;
961 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500962 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500963
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400964 window = input->keyboard_focus;
965 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500966 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400967 if (window->keyboard_focus_handler)
968 (*window->keyboard_focus_handler)(window, NULL,
969 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500970 }
971
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400972 if (surface)
973 input->keyboard_focus = wl_surface_get_user_data(surface);
974 else
975 input->keyboard_focus = NULL;
976
977 end = keys->data + keys->size;
978 for (k = keys->data; k < end; k++)
979 input->modifiers |= d->xkb->map->modmap[*k];
980
981 window = input->keyboard_focus;
982 if (window) {
983 window->keyboard_device = input;
984 if (window->keyboard_focus_handler)
985 (*window->keyboard_focus_handler)(window,
986 window->keyboard_device,
987 window->user_data);
988 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500989}
990
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500991static const struct wl_input_device_listener input_device_listener = {
992 window_handle_motion,
993 window_handle_button,
994 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500995 window_handle_pointer_focus,
996 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500997};
998
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400999void
1000input_get_position(struct input *input, int32_t *x, int32_t *y)
1001{
1002 *x = input->sx;
1003 *y = input->sy;
1004}
1005
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001006struct wl_input_device *
1007input_get_input_device(struct input *input)
1008{
1009 return input->input_device;
1010}
1011
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001012struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001013window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001014{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001015 cairo_device_flush (window->display->device);
1016
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001017 return wl_shell_create_drag(window->display->shell);
1018}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001019
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001020void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001021window_move(struct window *window, struct input *input, uint32_t time)
1022{
1023 wl_shell_move(window->display->shell,
1024 window->surface, input->input_device, time);
1025}
1026
1027void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001028window_activate_drag(struct wl_drag *drag, struct window *window,
1029 struct input *input, uint32_t time)
1030{
1031 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001032}
1033
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001034static void
1035handle_configure(void *data, struct wl_shell *shell,
1036 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001037 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001038{
1039 struct window *window = wl_surface_get_user_data(surface);
1040
1041 window->resize_edges = edges;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001042 window->allocation.width = width;
1043 window->allocation.height = height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001044
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001045 if (window->resize_handler)
1046 (*window->resize_handler)(window, window->user_data);
1047 if (window->redraw_handler)
1048 window_schedule_redraw(window);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001049}
1050
1051static const struct wl_shell_listener shell_listener = {
1052 handle_configure,
1053};
1054
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001055void
1056window_get_child_rectangle(struct window *window,
1057 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001058{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001059 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001060 *rectangle = window->allocation;
1061 } else {
1062 rectangle->x = window->margin + 10;
1063 rectangle->y = window->margin + 50;
1064 rectangle->width = window->allocation.width - 20 - window->margin * 2;
1065 rectangle->height = window->allocation.height - 60 - window->margin * 2;
1066 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001067}
1068
1069void
Kristian Høgsberg22106762008-12-08 13:50:07 -05001070window_set_child_size(struct window *window,
1071 struct rectangle *rectangle)
1072{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001073 if (!window->fullscreen) {
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001074 window->allocation.width =
1075 rectangle->width + 20 + window->margin * 2;
1076 window->allocation.height =
1077 rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001078 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001079}
1080
1081void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001082window_copy_image(struct window *window,
1083 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001084{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001085 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001086}
1087
1088void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001089window_copy_surface(struct window *window,
1090 struct rectangle *rectangle,
1091 cairo_surface_t *surface)
1092{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001093 cairo_t *cr;
1094
1095 cr = cairo_create (window->cairo_surface);
1096
1097 cairo_set_source_surface (cr,
1098 surface,
1099 rectangle->x, rectangle->y);
1100
1101 cairo_paint (cr);
1102 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001103}
1104
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001105static gboolean
1106idle_redraw(void *data)
1107{
1108 struct window *window = data;
1109
1110 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001111
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001112 window->redraw_scheduled = 0;
1113
1114 return FALSE;
1115}
1116
1117void
1118window_schedule_redraw(struct window *window)
1119{
1120 if (!window->redraw_scheduled) {
1121 g_idle_add(idle_redraw, window);
1122 window->redraw_scheduled = 1;
1123 }
1124}
1125
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001126void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001127window_set_fullscreen(struct window *window, int fullscreen)
1128{
1129 window->fullscreen = fullscreen;
1130 if (window->fullscreen) {
1131 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001132 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001133 } else {
1134 window->allocation = window->saved_allocation;
1135 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001136}
1137
1138void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001139window_set_decoration(struct window *window, int decoration)
1140{
1141 window->decoration = decoration;
1142}
1143
1144void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001145window_set_user_data(struct window *window, void *data)
1146{
1147 window->user_data = data;
1148}
1149
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001150void *
1151window_get_user_data(struct window *window)
1152{
1153 return window->user_data;
1154}
1155
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001156void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001157window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001158 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001159{
1160 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001161}
1162
1163void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001164window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001165 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001166{
1167 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001168}
1169
1170void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001171window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001172 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001173{
1174 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001175}
1176
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001177void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001178window_set_button_handler(struct window *window,
1179 window_button_handler_t handler)
1180{
1181 window->button_handler = handler;
1182}
1183
1184void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001185window_set_motion_handler(struct window *window,
1186 window_motion_handler_t handler)
1187{
1188 window->motion_handler = handler;
1189}
1190
1191void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001192window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001193 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001194{
1195 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001196}
1197
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001198void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001199window_damage(struct window *window, int32_t x, int32_t y,
1200 int32_t width, int32_t height)
1201{
1202 wl_surface_damage(window->surface, x, y, width, height);
1203}
1204
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001205struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001206window_create(struct display *display, const char *title,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001207 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001208{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001209 struct window *window;
1210
1211 window = malloc(sizeof *window);
1212 if (window == NULL)
1213 return NULL;
1214
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001215 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001216 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001217 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001218 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001219 window->allocation.x = 0;
1220 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001221 window->allocation.width = width;
1222 window->allocation.height = height;
1223 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001224 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001225 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001226
Yuval Fledel45568f62010-12-06 09:18:12 -05001227 if (display->drm)
1228 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1229 else
1230 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001231
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001232 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001233 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001234
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001235 return window;
1236}
1237
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001238void
1239window_set_buffer_type(struct window *window, enum window_buffer_type type)
1240{
1241 window->buffer_type = type;
1242}
1243
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001244static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001245drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001246{
1247 struct display *d = data;
1248
1249 d->device_name = strdup(device);
1250}
1251
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001252static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1253{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001254 struct display *d = data;
1255
1256 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001257}
1258
1259static const struct wl_drm_listener drm_listener = {
1260 drm_handle_device,
1261 drm_handle_authenticated
1262};
1263
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001264static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001265display_handle_geometry(void *data,
1266 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001267 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001268{
1269 struct display *display = data;
1270
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001271 display->screen_allocation.x = x;
1272 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001273 display->screen_allocation.width = width;
1274 display->screen_allocation.height = height;
1275}
1276
1277static const struct wl_output_listener output_listener = {
1278 display_handle_geometry,
1279};
1280
1281static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001282display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001283{
1284 struct input *input;
1285
1286 input = malloc(sizeof *input);
1287 if (input == NULL)
1288 return;
1289
1290 memset(input, 0, sizeof *input);
1291 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001292 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001293 input->pointer_focus = NULL;
1294 input->keyboard_focus = NULL;
1295 wl_list_insert(d->input_list.prev, &input->link);
1296
1297 wl_input_device_add_listener(input->input_device,
1298 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001299 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001300}
1301
1302static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001303display_handle_global(struct wl_display *display, uint32_t id,
1304 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001305{
1306 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001307 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001308
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001309 if (strcmp(interface, "compositor") == 0) {
1310 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001311 } else if (strcmp(interface, "output") == 0) {
1312 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001313 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001314 } else if (strcmp(interface, "input_device") == 0) {
1315 display_add_input(d, id);
1316 } else if (strcmp(interface, "shell") == 0) {
1317 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001318 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001319 } else if (strcmp(interface, "drm") == 0) {
1320 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001321 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001322 } else if (strcmp(interface, "shm") == 0) {
1323 d->shm = wl_shm_create(display, id);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001324 } else if (strcmp(interface, "drag_offer") == 0) {
Kristian Høgsberg8857a962010-11-22 13:02:21 -05001325 if (d->drag_offer_handler) {
1326 offer = wl_drag_offer_create(display, id);
1327 d->drag_offer_handler(offer, d);
1328 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001329 }
1330}
1331
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001332static void
1333display_render_frame(struct display *d)
1334{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001335 int radius = 8;
1336 cairo_t *cr;
1337
1338 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1339 cr = cairo_create(d->shadow);
1340 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1341 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1342 rounded_rect(cr, 16, 16, 112, 112, radius);
1343 cairo_fill(cr);
1344 cairo_destroy(cr);
1345 blur_surface(d->shadow, 64);
1346
1347 d->active_frame =
1348 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1349 cr = cairo_create(d->active_frame);
1350 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1351 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1352 rounded_rect(cr, 16, 16, 112, 112, radius);
1353 cairo_fill(cr);
1354 cairo_destroy(cr);
1355
1356 d->inactive_frame =
1357 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1358 cr = cairo_create(d->inactive_frame);
1359 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1360 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1361 rounded_rect(cr, 16, 16, 112, 112, radius);
1362 cairo_fill(cr);
1363 cairo_destroy(cr);
1364}
1365
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001366static void
1367init_xkb(struct display *d)
1368{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001369 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001370
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001371 names.rules = "evdev";
1372 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001373 names.layout = option_xkb_layout;
1374 names.variant = option_xkb_variant;
1375 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001376
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001377 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001378 if (!d->xkb) {
1379 fprintf(stderr, "Failed to compile keymap\n");
1380 exit(1);
1381 }
1382}
1383
Yuval Fledel45568f62010-12-06 09:18:12 -05001384static int
1385init_drm(struct display *d)
1386{
1387 EGLint major, minor;
1388 int fd;
1389 drm_magic_t magic;
1390
1391 fd = open(d->device_name, O_RDWR);
1392 if (fd < 0) {
1393 fprintf(stderr, "drm open failed: %m\n");
1394 return -1;
1395 }
1396
1397 if (drmGetMagic(fd, &magic)) {
1398 fprintf(stderr, "DRI2: failed to get drm magic");
1399 return -1;
1400 }
1401
1402 /* Wait for authenticated event */
1403 wl_drm_authenticate(d->drm, magic);
1404 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1405 while (!d->authenticated)
1406 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1407
1408 d->dpy = eglGetDRMDisplayMESA(fd);
1409 if (!eglInitialize(d->dpy, &major, &minor)) {
1410 fprintf(stderr, "failed to initialize display\n");
1411 return -1;
1412 }
1413
1414 if (!eglBindAPI(EGL_OPENGL_API)) {
1415 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1416 return -1;
1417 }
1418
1419 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1420 if (d->ctx == NULL) {
1421 fprintf(stderr, "failed to create context\n");
1422 return -1;
1423 }
1424
1425 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1426 fprintf(stderr, "faile to make context current\n");
1427 return -1;
1428 }
1429
1430#ifdef HAVE_CAIRO_GL
1431 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1432 if (d->device == NULL) {
1433 fprintf(stderr, "failed to get cairo drm device\n");
1434 return -1;
1435 }
1436#endif
1437
1438 return 0;
1439}
1440
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001441struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001442display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001443{
1444 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001445 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001446 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001447 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001448
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001449 g_type_init();
1450
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001451 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001452 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001453 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001454
1455 xkb_option_group = g_option_group_new("xkb",
1456 "Keyboard options",
1457 "Show all XKB options",
1458 NULL, NULL);
1459 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1460 g_option_context_add_group (context, xkb_option_group);
1461
1462 if (!g_option_context_parse(context, argc, argv, &error)) {
1463 fprintf(stderr, "option parsing failed: %s\n", error->message);
1464 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001465 }
1466
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001467
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001468 d = malloc(sizeof *d);
1469 if (d == NULL)
1470 return NULL;
1471
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001472 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001473 if (d->display == NULL) {
1474 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001475 return NULL;
1476 }
1477
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001478 wl_list_init(&d->input_list);
1479
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001480 /* Set up listener so we'll catch all events. */
1481 wl_display_add_global_listener(d->display,
1482 display_handle_global, d);
1483
1484 /* Process connection events. */
1485 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1486
Yuval Fledel45568f62010-12-06 09:18:12 -05001487 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001488 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001489
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001490 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001491
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001492 display_render_frame(d);
1493
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001494 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001495 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001496 g_source_attach(d->source, NULL);
1497
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001498 wl_list_init(&d->window_list);
1499
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001500 init_xkb(d);
1501
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001502 return d;
1503}
1504
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001505struct wl_display *
1506display_get_display(struct display *display)
1507{
1508 return display->display;
1509}
1510
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001511struct wl_compositor *
1512display_get_compositor(struct display *display)
1513{
1514 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001515}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001516
1517EGLDisplay
1518display_get_egl_display(struct display *d)
1519{
1520 return d->dpy;
1521}
1522
1523void
1524display_run(struct display *d)
1525{
1526 g_main_loop_run(d->loop);
1527}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001528
1529void
1530display_set_drag_offer_handler(struct display *display,
1531 display_drag_offer_handler_t handler)
1532{
1533 display->drag_offer_handler = handler;
1534}