blob: 1bbc1b8c71b293ea6a07966cc4f8d7e193d1f918 [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
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500164#define MULT(_d,c,a,t) \
165 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
166
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400167#ifdef HAVE_CAIRO_GL
168
169struct drm_surface_data {
170 struct surface_data data;
171 EGLImageKHR image;
172 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800173 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400174};
175
176static void
177drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400178{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400179 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800180 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400181
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800182 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400183 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800184 cairo_device_release(d->device);
185
186 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400187 wl_buffer_destroy(data->data.buffer);
188}
189
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500190EGLImageKHR
191display_get_image_for_drm_surface(struct display *display,
192 cairo_surface_t *surface)
193{
194 struct drm_surface_data *data;
195
196 data = cairo_surface_get_user_data (surface, &surface_data_key);
197
198 return data->image;
199}
200
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500201static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400202display_create_drm_surface(struct display *display,
203 struct rectangle *rectangle)
204{
205 struct drm_surface_data *data;
206 EGLDisplay dpy = display->dpy;
207 cairo_surface_t *surface;
208 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400209 EGLint name, stride;
210
211 EGLint image_attribs[] = {
212 EGL_WIDTH, 0,
213 EGL_HEIGHT, 0,
214 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
215 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
216 EGL_NONE
217 };
218
219 data = malloc(sizeof *data);
220 if (data == NULL)
221 return NULL;
222
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800223 data->display = display;
224
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400225 image_attribs[1] = rectangle->width;
226 image_attribs[3] = rectangle->height;
227 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800228
229 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400230 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400231 glBindTexture(GL_TEXTURE_2D, data->texture);
232 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800233 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400234
235 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
236
237 visual = wl_display_get_premultiplied_argb_visual(display->display);
238 data->data.buffer =
239 wl_drm_create_buffer(display->drm, name, rectangle->width,
240 rectangle->height, stride, visual);
241
242 surface = cairo_gl_surface_create_for_texture(display->device,
243 CAIRO_CONTENT_COLOR_ALPHA,
244 data->texture,
245 rectangle->width,
246 rectangle->height);
247
248 cairo_surface_set_user_data (surface, &surface_data_key,
249 data, drm_surface_data_destroy);
250
251 return surface;
252}
253
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500254static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400255display_create_drm_surface_from_file(struct display *display,
256 const char *filename,
257 struct rectangle *rect)
258{
259 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400260 GdkPixbuf *pixbuf;
261 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400262 int stride, i;
263 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800264 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400265
266 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400267 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400268 FALSE, &error);
269 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400270 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400271
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400272 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
273 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
274 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400276 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400277
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400278
279 stride = gdk_pixbuf_get_rowstride(pixbuf);
280 pixels = gdk_pixbuf_get_pixels(pixbuf);
281
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400282 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400283 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400284 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400285 while (p < end) {
286 unsigned int t;
287
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400288 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
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500329static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330shm_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
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500338static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339display_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
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500397static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398display_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
Yuval Fledel45568f62010-12-06 09:18:12 -0500456 if (display->drm) {
457 return display_create_drm_surface(display, rectangle);
458 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500460 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461}
462
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500463static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464display_create_surface_from_file(struct display *display,
465 const char *filename,
466 struct rectangle *rectangle)
467{
468#ifdef HAVE_CAIRO_GL
Yuval Fledel45568f62010-12-06 09:18:12 -0500469 if (display->drm) {
470 return display_create_drm_surface_from_file(display, filename, rectangle);
471 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500473 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400474}
Yuval Fledel45568f62010-12-06 09:18:12 -0500475 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400476 const char *filename;
477 int hotspot_x, hotspot_y;
478} pointer_images[] = {
479 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
480 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
481 { DATADIR "/wayland/bottom_side.png", 16, 20 },
482 { DATADIR "/wayland/grabbing.png", 20, 17 },
483 { DATADIR "/wayland/left_ptr.png", 10, 5 },
484 { DATADIR "/wayland/left_side.png", 10, 20 },
485 { DATADIR "/wayland/right_side.png", 30, 19 },
486 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
487 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
488 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400489 { DATADIR "/wayland/xterm.png", 15, 15 },
490 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400491};
492
493static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400494create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400495{
496 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400497 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400498 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400499
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400500 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400501 display->pointer_surfaces =
502 malloc(count * sizeof *display->pointer_surfaces);
503 rect.width = width;
504 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400505 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400506 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507 display_create_surface_from_file(display,
508 pointer_images[i].filename,
509 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400510 }
511
512}
513
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400514cairo_surface_t *
515display_get_pointer_surface(struct display *display, int pointer,
516 int *width, int *height,
517 int *hotspot_x, int *hotspot_y)
518{
519 cairo_surface_t *surface;
520
521 surface = display->pointer_surfaces[pointer];
522 *width = cairo_gl_surface_get_width(surface);
523 *height = cairo_gl_surface_get_height(surface);
524 *hotspot_x = pointer_images[pointer].hotspot_x;
525 *hotspot_y = pointer_images[pointer].hotspot_y;
526
527 return cairo_surface_reference(surface);
528}
529
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400530
531static void
532window_attach_surface(struct window *window);
533
534static void
535free_surface(void *data)
536{
537 struct window *window = data;
538
539 cairo_surface_destroy(window->pending_surface);
540 window->pending_surface = NULL;
541 if (window->cairo_surface)
542 window_attach_surface(window);
543}
544
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500545static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500546window_attach_surface(struct window *window)
547{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400548 struct display *display = window->display;
549 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500550
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500551 if (window->pending_surface != NULL)
552 return;
553
Kristian Høgsberg09531622010-06-14 23:22:15 -0400554 window->pending_surface = window->cairo_surface;
555 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500556
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400557 buffer = display_get_buffer_for_surface(display,
558 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400559 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500560
561 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400562 window->allocation.x,
563 window->allocation.y,
564 window->allocation.width,
565 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400566
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400567 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500568}
569
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500570void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400571window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500572{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400573 if (window->cairo_surface)
574 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500575}
576
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400577void
578window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500580 cairo_surface_reference(surface);
581
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400582 if (window->cairo_surface != NULL)
583 cairo_surface_destroy(window->cairo_surface);
584
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500585 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400586}
587
588void
589window_create_surface(struct window *window)
590{
591 cairo_surface_t *surface;
592
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593 switch (window->buffer_type) {
594#ifdef HAVE_CAIRO_GL
595 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400596 surface = display_create_surface(window->display,
597 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598 break;
599#endif
600 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400601 surface = display_create_shm_surface(window->display,
602 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800604 default:
605 surface = NULL;
606 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400608
609 window_set_surface(window, surface);
610 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611}
612
613static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500614window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500615{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500616 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500617 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400618 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400619 cairo_surface_t *frame;
620 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500621
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400622 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400624 width = window->allocation.width;
625 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500626
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500627 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500628 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500629 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
630
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500631 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500632
Kristian Høgsberg09531622010-06-14 23:22:15 -0400633 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400634 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400635 cairo_paint(cr);
636
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400637 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400638 tile_mask(cr, window->display->shadow,
639 shadow_dx, shadow_dy, width, height,
640 window->margin + 10 - shadow_dx,
641 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500642
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400643 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400644 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400645 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400646 frame = window->display->inactive_frame;
647
648 tile_source(cr, frame, 0, 0, width, height,
649 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500650
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500651 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
652 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500653 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400654 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500655 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
656 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
657 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400658 if (window->keyboard_device)
659 cairo_set_source_rgb(cr, 0, 0, 0);
660 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400661 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400662 cairo_show_text(cr, window->title);
663
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500664 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400665
666 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500667}
668
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400669void
670display_flush_cairo_device(struct display *display)
671{
672 cairo_device_flush (display->device);
673}
674
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500675void
676window_draw(struct window *window)
677{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400678 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400679 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500680 else
681 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500682}
683
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400684cairo_surface_t *
685window_get_surface(struct window *window)
686{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400687 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400688}
689
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400690enum window_location {
691 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400692 WINDOW_RESIZING_TOP = 1,
693 WINDOW_RESIZING_BOTTOM = 2,
694 WINDOW_RESIZING_LEFT = 4,
695 WINDOW_RESIZING_TOP_LEFT = 5,
696 WINDOW_RESIZING_BOTTOM_LEFT = 6,
697 WINDOW_RESIZING_RIGHT = 8,
698 WINDOW_RESIZING_TOP_RIGHT = 9,
699 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
700 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701 WINDOW_EXTERIOR = 16,
702 WINDOW_TITLEBAR = 17,
703 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400704};
705
706static int
707get_pointer_location(struct window *window, int32_t x, int32_t y)
708{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400709 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400710 const int grip_size = 8;
711
712 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400713 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400714 else if (window->margin <= x && x < window->margin + grip_size)
715 hlocation = WINDOW_RESIZING_LEFT;
716 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400717 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400718 else if (x < window->allocation.width - window->margin)
719 hlocation = WINDOW_RESIZING_RIGHT;
720 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400721 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400722
723 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400724 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400725 else if (window->margin <= y && y < window->margin + grip_size)
726 vlocation = WINDOW_RESIZING_TOP;
727 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400729 else if (y < window->allocation.height - window->margin)
730 vlocation = WINDOW_RESIZING_BOTTOM;
731 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400732 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400733
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 location = vlocation | hlocation;
735 if (location & WINDOW_EXTERIOR)
736 location = WINDOW_EXTERIOR;
737 if (location == WINDOW_INTERIOR && y < window->margin + 50)
738 location = WINDOW_TITLEBAR;
739 else if (location == WINDOW_INTERIOR)
740 location = WINDOW_CLIENT_AREA;
741
742 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400743}
744
745static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400746set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400747{
748 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400749 struct wl_buffer *buffer;
750 cairo_surface_t *surface;
751 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400752
753 location = get_pointer_location(input->pointer_focus,
754 input->sx, input->sy);
755 switch (location) {
756 case WINDOW_RESIZING_TOP:
757 pointer = POINTER_TOP;
758 break;
759 case WINDOW_RESIZING_BOTTOM:
760 pointer = POINTER_BOTTOM;
761 break;
762 case WINDOW_RESIZING_LEFT:
763 pointer = POINTER_LEFT;
764 break;
765 case WINDOW_RESIZING_RIGHT:
766 pointer = POINTER_RIGHT;
767 break;
768 case WINDOW_RESIZING_TOP_LEFT:
769 pointer = POINTER_TOP_LEFT;
770 break;
771 case WINDOW_RESIZING_TOP_RIGHT:
772 pointer = POINTER_TOP_RIGHT;
773 break;
774 case WINDOW_RESIZING_BOTTOM_LEFT:
775 pointer = POINTER_BOTTOM_LEFT;
776 break;
777 case WINDOW_RESIZING_BOTTOM_RIGHT:
778 pointer = POINTER_BOTTOM_RIGHT;
779 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400780 case WINDOW_EXTERIOR:
781 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400782 if (input->current_pointer_image == POINTER_DEFAULT)
783 return;
784
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400785 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400786 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400787 return;
788 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400789 break;
790 }
791
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400792 if (pointer == input->current_pointer_image)
793 return;
794
795 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400796 surface = display->pointer_surfaces[pointer];
797 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400798 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400799 pointer_images[pointer].hotspot_x,
800 pointer_images[pointer].hotspot_y);
801}
802
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500803static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500804window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400805 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500806 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500807{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400808 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400809 struct window *window = input->pointer_focus;
810 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400811
812 input->x = x;
813 input->y = y;
814 input->sx = sx;
815 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400816
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400817 location = get_pointer_location(window, input->sx, input->sy);
818
819 if (window->motion_handler)
820 pointer = (*window->motion_handler)(window, input, time,
821 x, y, sx, sy,
822 window->user_data);
823
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400824 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500825}
826
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400827static void
828window_handle_button(void *data,
829 struct wl_input_device *input_device,
830 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500831{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400832 struct input *input = data;
833 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400834 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400835
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400836 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500837
838 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400839 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400840 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400841 wl_shell_move(window->display->shell,
842 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500843 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400844 case WINDOW_RESIZING_TOP:
845 case WINDOW_RESIZING_BOTTOM:
846 case WINDOW_RESIZING_LEFT:
847 case WINDOW_RESIZING_RIGHT:
848 case WINDOW_RESIZING_TOP_LEFT:
849 case WINDOW_RESIZING_TOP_RIGHT:
850 case WINDOW_RESIZING_BOTTOM_LEFT:
851 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400852 wl_shell_resize(window->display->shell,
853 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400854 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500855 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400856 case WINDOW_CLIENT_AREA:
857 if (window->button_handler)
858 (*window->button_handler)(window,
859 input, time,
860 button, state,
861 window->user_data);
862 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500863 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400864 } else {
865 if (window->button_handler)
866 (*window->button_handler)(window,
867 input, time,
868 button, state,
869 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500870 }
871}
872
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500873static void
874window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400875 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500876{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400877 struct input *input = data;
878 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400879 struct display *d = window->display;
880 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500881
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400882 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400883 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500884 return;
885
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400886 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400887 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400888 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
889 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500890
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400891 sym = XkbKeySymEntry(d->xkb, code, level, 0);
892
893 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400894 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400895 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400896 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500897
898 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400899 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400900 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500901}
902
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500903static void
904window_handle_pointer_focus(void *data,
905 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400906 uint32_t time, struct wl_surface *surface,
907 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500908{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400909 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400910 struct window *window;
911 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400912
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400913 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400914 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400915 window = input->pointer_focus;
916
917 pointer = POINTER_LEFT_PTR;
918 if (window->motion_handler)
919 pointer = (*window->motion_handler)(window,
920 input, time,
921 x, y, sx, sy,
922 window->user_data);
923
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400924 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400925 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400926 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400927 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400928 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500929}
930
931static void
932window_handle_keyboard_focus(void *data,
933 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400934 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500935 struct wl_surface *surface,
936 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500937{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400938 struct input *input = data;
939 struct window *window = input->keyboard_focus;
940 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500941 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500942
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400943 window = input->keyboard_focus;
944 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500945 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400946 if (window->keyboard_focus_handler)
947 (*window->keyboard_focus_handler)(window, NULL,
948 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500949 }
950
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400951 if (surface)
952 input->keyboard_focus = wl_surface_get_user_data(surface);
953 else
954 input->keyboard_focus = NULL;
955
956 end = keys->data + keys->size;
957 for (k = keys->data; k < end; k++)
958 input->modifiers |= d->xkb->map->modmap[*k];
959
960 window = input->keyboard_focus;
961 if (window) {
962 window->keyboard_device = input;
963 if (window->keyboard_focus_handler)
964 (*window->keyboard_focus_handler)(window,
965 window->keyboard_device,
966 window->user_data);
967 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500968}
969
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500970static const struct wl_input_device_listener input_device_listener = {
971 window_handle_motion,
972 window_handle_button,
973 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500974 window_handle_pointer_focus,
975 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500976};
977
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400978void
979input_get_position(struct input *input, int32_t *x, int32_t *y)
980{
981 *x = input->sx;
982 *y = input->sy;
983}
984
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400985struct wl_input_device *
986input_get_input_device(struct input *input)
987{
988 return input->input_device;
989}
990
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400991struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -0500992window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400993{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400994 cairo_device_flush (window->display->device);
995
Kristian Høgsberg41da9082010-11-30 14:01:07 -0500996 return wl_shell_create_drag(window->display->shell);
997}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400998
Kristian Høgsberg41da9082010-11-30 14:01:07 -0500999void
1000window_activate_drag(struct wl_drag *drag, struct window *window,
1001 struct input *input, uint32_t time)
1002{
1003 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001004}
1005
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001006static void
1007handle_configure(void *data, struct wl_shell *shell,
1008 uint32_t time, uint32_t edges,
1009 struct wl_surface *surface,
1010 int32_t x, int32_t y, int32_t width, int32_t height)
1011{
1012 struct window *window = wl_surface_get_user_data(surface);
1013
1014 window->resize_edges = edges;
1015 window->pending_allocation.x = x;
1016 window->pending_allocation.y = y;
1017 window->pending_allocation.width = width;
1018 window->pending_allocation.height = height;
1019
Joel Teichroeb88b272d2010-11-30 10:22:13 -08001020 if (edges & WINDOW_TITLEBAR) {
1021 window->allocation.x = window->pending_allocation.x;
1022 window->allocation.y = window->pending_allocation.y;
1023 } else if (edges & WINDOW_RESIZING_MASK) {
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 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001030}
1031
1032static const struct wl_shell_listener shell_listener = {
1033 handle_configure,
1034};
1035
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001036void
1037window_get_child_rectangle(struct window *window,
1038 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001039{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001040 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001041 *rectangle = window->allocation;
1042 } else {
1043 rectangle->x = window->margin + 10;
1044 rectangle->y = window->margin + 50;
1045 rectangle->width = window->allocation.width - 20 - window->margin * 2;
1046 rectangle->height = window->allocation.height - 60 - window->margin * 2;
1047 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001048}
1049
1050void
Kristian Høgsberg22106762008-12-08 13:50:07 -05001051window_set_child_size(struct window *window,
1052 struct rectangle *rectangle)
1053{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001054 int32_t width, height;
1055
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001056 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001057 width = rectangle->width + 20 + window->margin * 2;
1058 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001059
1060 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1061 window->allocation.x +=
1062 window->allocation.width - width;
1063 if (window->resize_edges & WINDOW_RESIZING_TOP)
1064 window->allocation.y +=
1065 window->allocation.height - height;
1066
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001067 window->allocation.width = width;
1068 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001069 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001070}
1071
1072void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001073window_copy_image(struct window *window,
1074 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001075{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001076 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001077}
1078
1079void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001080window_copy_surface(struct window *window,
1081 struct rectangle *rectangle,
1082 cairo_surface_t *surface)
1083{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001084 cairo_t *cr;
1085
1086 cr = cairo_create (window->cairo_surface);
1087
1088 cairo_set_source_surface (cr,
1089 surface,
1090 rectangle->x, rectangle->y);
1091
1092 cairo_paint (cr);
1093 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001094}
1095
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001096static gboolean
1097idle_redraw(void *data)
1098{
1099 struct window *window = data;
1100
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001101 if (window->resize_edges)
1102 window->allocation = window->pending_allocation;
1103
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001104 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001105
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001106 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001107 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001108
1109 return FALSE;
1110}
1111
1112void
1113window_schedule_redraw(struct window *window)
1114{
1115 if (!window->redraw_scheduled) {
1116 g_idle_add(idle_redraw, window);
1117 window->redraw_scheduled = 1;
1118 }
1119}
1120
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001121void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001122window_set_fullscreen(struct window *window, int fullscreen)
1123{
1124 window->fullscreen = fullscreen;
1125 if (window->fullscreen) {
1126 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001127 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001128 } else {
1129 window->allocation = window->saved_allocation;
1130 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001131}
1132
1133void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001134window_set_decoration(struct window *window, int decoration)
1135{
1136 window->decoration = decoration;
1137}
1138
1139void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001140window_set_user_data(struct window *window, void *data)
1141{
1142 window->user_data = data;
1143}
1144
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001145void *
1146window_get_user_data(struct window *window)
1147{
1148 return window->user_data;
1149}
1150
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001151void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001152window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001153 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001154{
1155 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001156}
1157
1158void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001159window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001160 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001161{
1162 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001163}
1164
1165void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001166window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001167 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001168{
1169 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001170}
1171
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001172void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001173window_set_button_handler(struct window *window,
1174 window_button_handler_t handler)
1175{
1176 window->button_handler = handler;
1177}
1178
1179void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001180window_set_motion_handler(struct window *window,
1181 window_motion_handler_t handler)
1182{
1183 window->motion_handler = handler;
1184}
1185
1186void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001187window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001188 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001189{
1190 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001191}
1192
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001193void
1194window_move(struct window *window, int32_t x, int32_t y)
1195{
1196 window->allocation.x = x;
1197 window->allocation.y = y;
1198
1199 wl_surface_map(window->surface,
1200 window->allocation.x - window->margin,
1201 window->allocation.y - window->margin,
1202 window->allocation.width,
1203 window->allocation.height);
1204}
1205
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001206void
1207window_damage(struct window *window, int32_t x, int32_t y,
1208 int32_t width, int32_t height)
1209{
1210 wl_surface_damage(window->surface, x, y, width, height);
1211}
1212
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001213struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001214window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001215 int32_t x, int32_t y, int32_t width, int32_t height)
1216{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001217 struct window *window;
1218
1219 window = malloc(sizeof *window);
1220 if (window == NULL)
1221 return NULL;
1222
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001223 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001224 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001225 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001226 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001227 window->allocation.x = x;
1228 window->allocation.y = y;
1229 window->allocation.width = width;
1230 window->allocation.height = height;
1231 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001232 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001233 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001234
Yuval Fledel45568f62010-12-06 09:18:12 -05001235 if (display->drm)
1236 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1237 else
1238 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001239
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,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001275 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001276{
1277 struct display *display = data;
1278
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001279 display->screen_allocation.x = x;
1280 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001281 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) {
Kristian Høgsberg8857a962010-11-22 13:02:21 -05001333 if (d->drag_offer_handler) {
1334 offer = wl_drag_offer_create(display, id);
1335 d->drag_offer_handler(offer, d);
1336 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001337 }
1338}
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
Yuval Fledel45568f62010-12-06 09:18:12 -05001392static int
1393init_drm(struct display *d)
1394{
1395 EGLint major, minor;
1396 int fd;
1397 drm_magic_t magic;
1398
1399 fd = open(d->device_name, O_RDWR);
1400 if (fd < 0) {
1401 fprintf(stderr, "drm open failed: %m\n");
1402 return -1;
1403 }
1404
1405 if (drmGetMagic(fd, &magic)) {
1406 fprintf(stderr, "DRI2: failed to get drm magic");
1407 return -1;
1408 }
1409
1410 /* Wait for authenticated event */
1411 wl_drm_authenticate(d->drm, magic);
1412 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1413 while (!d->authenticated)
1414 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1415
1416 d->dpy = eglGetDRMDisplayMESA(fd);
1417 if (!eglInitialize(d->dpy, &major, &minor)) {
1418 fprintf(stderr, "failed to initialize display\n");
1419 return -1;
1420 }
1421
1422 if (!eglBindAPI(EGL_OPENGL_API)) {
1423 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1424 return -1;
1425 }
1426
1427 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1428 if (d->ctx == NULL) {
1429 fprintf(stderr, "failed to create context\n");
1430 return -1;
1431 }
1432
1433 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1434 fprintf(stderr, "faile to make context current\n");
1435 return -1;
1436 }
1437
1438#ifdef HAVE_CAIRO_GL
1439 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1440 if (d->device == NULL) {
1441 fprintf(stderr, "failed to get cairo drm device\n");
1442 return -1;
1443 }
1444#endif
1445
1446 return 0;
1447}
1448
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001449struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001450display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001451{
1452 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001453 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001454 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001455 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001456
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001457 g_type_init();
1458
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001459 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001460 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001461 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001462
1463 xkb_option_group = g_option_group_new("xkb",
1464 "Keyboard options",
1465 "Show all XKB options",
1466 NULL, NULL);
1467 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1468 g_option_context_add_group (context, xkb_option_group);
1469
1470 if (!g_option_context_parse(context, argc, argv, &error)) {
1471 fprintf(stderr, "option parsing failed: %s\n", error->message);
1472 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001473 }
1474
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001475
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001476 d = malloc(sizeof *d);
1477 if (d == NULL)
1478 return NULL;
1479
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001480 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001481 if (d->display == NULL) {
1482 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001483 return NULL;
1484 }
1485
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001486 wl_list_init(&d->input_list);
1487
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001488 /* Set up listener so we'll catch all events. */
1489 wl_display_add_global_listener(d->display,
1490 display_handle_global, d);
1491
1492 /* Process connection events. */
1493 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1494
Yuval Fledel45568f62010-12-06 09:18:12 -05001495 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001496 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001497
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001498 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001499
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001500 display_render_frame(d);
1501
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001502 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001503 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001504 g_source_attach(d->source, NULL);
1505
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001506 wl_list_init(&d->window_list);
1507
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001508 init_xkb(d);
1509
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001510 return d;
1511}
1512
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001513struct wl_display *
1514display_get_display(struct display *display)
1515{
1516 return display->display;
1517}
1518
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001519struct wl_compositor *
1520display_get_compositor(struct display *display)
1521{
1522 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001523}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001524
1525EGLDisplay
1526display_get_egl_display(struct display *d)
1527{
1528 return d->dpy;
1529}
1530
1531void
1532display_run(struct display *d)
1533{
1534 g_main_loop_run(d->loop);
1535}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001536
1537void
1538display_set_drag_offer_handler(struct display *display,
1539 display_drag_offer_handler_t handler)
1540{
1541 display->drag_offer_handler = handler;
1542}