blob: f720bf860d7f763648217c9e60bb0ad86ad6de63 [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øgsbergd0c3b9d2010-10-25 11:40:03 -0400201cairo_surface_t *
202display_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
254cairo_surface_t *
255display_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
329void
330shm_surface_data_destroy(void *p)
331{
332 struct shm_surface_data *data = p;
333
334 wl_buffer_destroy(data->data.buffer);
335 munmap(data->map, data->length);
336}
337
338cairo_surface_t *
339display_create_shm_surface(struct display *display,
340 struct rectangle *rectangle)
341{
342 struct shm_surface_data *data;
343 cairo_surface_t *surface;
344 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800345 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400346 char filename[] = "/tmp/wayland-shm-XXXXXX";
347
348 data = malloc(sizeof *data);
349 if (data == NULL)
350 return NULL;
351
352 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
353 rectangle->width);
354 data->length = stride * rectangle->height;
355 fd = mkstemp(filename);
356 if (fd < 0) {
357 fprintf(stderr, "open %s failed: %m", filename);
358 return NULL;
359 }
360 if (ftruncate(fd, data->length) < 0) {
361 fprintf(stderr, "ftruncate failed: %m");
362 close(fd);
363 return NULL;
364 }
365
366 data->map = mmap(NULL, data->length,
367 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
368 unlink(filename);
369
370 if (data->map == MAP_FAILED) {
371 fprintf(stderr, "mmap failed: %m");
372 close(fd);
373 return NULL;
374 }
375
376 surface = cairo_image_surface_create_for_data (data->map,
377 CAIRO_FORMAT_ARGB32,
378 rectangle->width,
379 rectangle->height,
380 stride);
381
382 cairo_surface_set_user_data (surface, &surface_data_key,
383 data, shm_surface_data_destroy);
384
385 visual = wl_display_get_premultiplied_argb_visual(display->display);
386 data->data.buffer = wl_shm_create_buffer(display->shm,
387 fd,
388 rectangle->width,
389 rectangle->height,
390 stride, visual);
391
392 close(fd);
393
394 return surface;
395}
396
397cairo_surface_t *
398display_create_shm_surface_from_file(struct display *display,
399 const char *filename,
400 struct rectangle *rect)
401{
402 cairo_surface_t *surface;
403 GdkPixbuf *pixbuf;
404 GError *error = NULL;
405 int stride, i;
406 unsigned char *pixels, *p, *end, *dest_data;
407 int dest_stride;
408 uint32_t *d;
409
410 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
411 rect->width, rect->height,
412 FALSE, &error);
413 if (error != NULL)
414 return NULL;
415
416 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
417 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
418 gdk_pixbuf_unref(pixbuf);
419 return NULL;
420 }
421
422 stride = gdk_pixbuf_get_rowstride(pixbuf);
423 pixels = gdk_pixbuf_get_pixels(pixbuf);
424
425 surface = display_create_shm_surface(display, rect);
426 dest_data = cairo_image_surface_get_data (surface);
427 dest_stride = cairo_image_surface_get_stride (surface);
428
429 for (i = 0; i < rect->height; i++) {
430 d = (uint32_t *) (dest_data + i * dest_stride);
431 p = pixels + i * stride;
432 end = p + rect->width * 4;
433 while (p < end) {
434 unsigned int t;
435 unsigned char a, r, g, b;
436
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 a = p[3];
438 MULT(r, p[0], a, t);
439 MULT(g, p[1], a, t);
440 MULT(b, p[2], a, t);
441 p += 4;
442 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
443 }
444 }
445
446 gdk_pixbuf_unref(pixbuf);
447
448 return surface;
449}
450
451cairo_surface_t *
452display_create_surface(struct display *display,
453 struct rectangle *rectangle)
454{
455#ifdef HAVE_CAIRO_GL
Bryce Harrington156d0572010-11-19 12:14:39 -0800456 return display_create_drm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457#else
Bryce Harrington156d0572010-11-19 12:14:39 -0800458 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459#endif
460}
461
462cairo_surface_t *
463display_create_surface_from_file(struct display *display,
464 const char *filename,
465 struct rectangle *rectangle)
466{
467#ifdef HAVE_CAIRO_GL
Bryce Harrington156d0572010-11-19 12:14:39 -0800468 return display_create_drm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469#else
Bryce Harrington156d0572010-11-19 12:14:39 -0800470 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400471#endif
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400472}
473
474static const struct {
475 const char *filename;
476 int hotspot_x, hotspot_y;
477} pointer_images[] = {
478 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
479 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
480 { DATADIR "/wayland/bottom_side.png", 16, 20 },
481 { DATADIR "/wayland/grabbing.png", 20, 17 },
482 { DATADIR "/wayland/left_ptr.png", 10, 5 },
483 { DATADIR "/wayland/left_side.png", 10, 20 },
484 { DATADIR "/wayland/right_side.png", 30, 19 },
485 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
486 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
487 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400488 { DATADIR "/wayland/xterm.png", 15, 15 },
489 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400490};
491
492static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400493create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494{
495 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400496 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400497 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400498
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400499 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400500 display->pointer_surfaces =
501 malloc(count * sizeof *display->pointer_surfaces);
502 rect.width = width;
503 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400504 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400505 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506 display_create_surface_from_file(display,
507 pointer_images[i].filename,
508 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400509 }
510
511}
512
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400513cairo_surface_t *
514display_get_pointer_surface(struct display *display, int pointer,
515 int *width, int *height,
516 int *hotspot_x, int *hotspot_y)
517{
518 cairo_surface_t *surface;
519
520 surface = display->pointer_surfaces[pointer];
521 *width = cairo_gl_surface_get_width(surface);
522 *height = cairo_gl_surface_get_height(surface);
523 *hotspot_x = pointer_images[pointer].hotspot_x;
524 *hotspot_y = pointer_images[pointer].hotspot_y;
525
526 return cairo_surface_reference(surface);
527}
528
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400529
530static void
531window_attach_surface(struct window *window);
532
533static void
534free_surface(void *data)
535{
536 struct window *window = data;
537
538 cairo_surface_destroy(window->pending_surface);
539 window->pending_surface = NULL;
540 if (window->cairo_surface)
541 window_attach_surface(window);
542}
543
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500544static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500545window_attach_surface(struct window *window)
546{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400547 struct display *display = window->display;
548 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500549
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500550 if (window->pending_surface != NULL)
551 return;
552
Kristian Høgsberg09531622010-06-14 23:22:15 -0400553 window->pending_surface = window->cairo_surface;
554 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500555
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400556 buffer = display_get_buffer_for_surface(display,
557 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400558 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500559
560 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400561 window->allocation.x,
562 window->allocation.y,
563 window->allocation.width,
564 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400565
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400566 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500567}
568
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500569void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400570window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500571{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400572 if (window->cairo_surface)
573 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500574}
575
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400576void
577window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500579 cairo_surface_reference(surface);
580
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400581 if (window->cairo_surface != NULL)
582 cairo_surface_destroy(window->cairo_surface);
583
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500584 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400585}
586
587void
588window_create_surface(struct window *window)
589{
590 cairo_surface_t *surface;
591
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592 switch (window->buffer_type) {
593#ifdef HAVE_CAIRO_GL
594 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400595 surface = display_create_surface(window->display,
596 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597 break;
598#endif
599 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400600 surface = display_create_shm_surface(window->display,
601 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800603 default:
604 surface = NULL;
605 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400607
608 window_set_surface(window, surface);
609 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400610}
611
612static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500613window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500614{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500615 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500616 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400617 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400618 cairo_surface_t *frame;
619 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500620
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400621 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400623 width = window->allocation.width;
624 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500625
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500626 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500627 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500628 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
629
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500630 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500631
Kristian Høgsberg09531622010-06-14 23:22:15 -0400632 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400633 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400634 cairo_paint(cr);
635
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400636 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400637 tile_mask(cr, window->display->shadow,
638 shadow_dx, shadow_dy, width, height,
639 window->margin + 10 - shadow_dx,
640 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500641
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400642 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400643 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400644 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400645 frame = window->display->inactive_frame;
646
647 tile_source(cr, frame, 0, 0, width, height,
648 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500649
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500650 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
651 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500652 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400653 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500654 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
655 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
656 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400657 if (window->keyboard_device)
658 cairo_set_source_rgb(cr, 0, 0, 0);
659 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400660 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400661 cairo_show_text(cr, window->title);
662
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500663 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400664
665 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500666}
667
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400668void
669display_flush_cairo_device(struct display *display)
670{
671 cairo_device_flush (display->device);
672}
673
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500674void
675window_draw(struct window *window)
676{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400677 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400678 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500679 else
680 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500681}
682
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400683cairo_surface_t *
684window_get_surface(struct window *window)
685{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400686 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400687}
688
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689enum window_location {
690 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400691 WINDOW_RESIZING_TOP = 1,
692 WINDOW_RESIZING_BOTTOM = 2,
693 WINDOW_RESIZING_LEFT = 4,
694 WINDOW_RESIZING_TOP_LEFT = 5,
695 WINDOW_RESIZING_BOTTOM_LEFT = 6,
696 WINDOW_RESIZING_RIGHT = 8,
697 WINDOW_RESIZING_TOP_RIGHT = 9,
698 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
699 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 WINDOW_EXTERIOR = 16,
701 WINDOW_TITLEBAR = 17,
702 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400703};
704
705static int
706get_pointer_location(struct window *window, int32_t x, int32_t y)
707{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709 const int grip_size = 8;
710
711 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400712 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400713 else if (window->margin <= x && x < window->margin + grip_size)
714 hlocation = WINDOW_RESIZING_LEFT;
715 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400716 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400717 else if (x < window->allocation.width - window->margin)
718 hlocation = WINDOW_RESIZING_RIGHT;
719 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400720 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400721
722 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400723 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400724 else if (window->margin <= y && y < window->margin + grip_size)
725 vlocation = WINDOW_RESIZING_TOP;
726 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400727 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400728 else if (y < window->allocation.height - window->margin)
729 vlocation = WINDOW_RESIZING_BOTTOM;
730 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400731 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400732
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733 location = vlocation | hlocation;
734 if (location & WINDOW_EXTERIOR)
735 location = WINDOW_EXTERIOR;
736 if (location == WINDOW_INTERIOR && y < window->margin + 50)
737 location = WINDOW_TITLEBAR;
738 else if (location == WINDOW_INTERIOR)
739 location = WINDOW_CLIENT_AREA;
740
741 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400742}
743
744static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400745set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400746{
747 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400748 struct wl_buffer *buffer;
749 cairo_surface_t *surface;
750 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400751
752 location = get_pointer_location(input->pointer_focus,
753 input->sx, input->sy);
754 switch (location) {
755 case WINDOW_RESIZING_TOP:
756 pointer = POINTER_TOP;
757 break;
758 case WINDOW_RESIZING_BOTTOM:
759 pointer = POINTER_BOTTOM;
760 break;
761 case WINDOW_RESIZING_LEFT:
762 pointer = POINTER_LEFT;
763 break;
764 case WINDOW_RESIZING_RIGHT:
765 pointer = POINTER_RIGHT;
766 break;
767 case WINDOW_RESIZING_TOP_LEFT:
768 pointer = POINTER_TOP_LEFT;
769 break;
770 case WINDOW_RESIZING_TOP_RIGHT:
771 pointer = POINTER_TOP_RIGHT;
772 break;
773 case WINDOW_RESIZING_BOTTOM_LEFT:
774 pointer = POINTER_BOTTOM_LEFT;
775 break;
776 case WINDOW_RESIZING_BOTTOM_RIGHT:
777 pointer = POINTER_BOTTOM_RIGHT;
778 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400779 case WINDOW_EXTERIOR:
780 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400781 if (input->current_pointer_image == POINTER_DEFAULT)
782 return;
783
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400784 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400785 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400786 return;
787 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400788 break;
789 }
790
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400791 if (pointer == input->current_pointer_image)
792 return;
793
794 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400795 surface = display->pointer_surfaces[pointer];
796 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400797 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400798 pointer_images[pointer].hotspot_x,
799 pointer_images[pointer].hotspot_y);
800}
801
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500802static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500803window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400804 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500805 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500806{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400807 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400808 struct window *window = input->pointer_focus;
809 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400810
811 input->x = x;
812 input->y = y;
813 input->sx = sx;
814 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400815
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400816 location = get_pointer_location(window, input->sx, input->sy);
817
818 if (window->motion_handler)
819 pointer = (*window->motion_handler)(window, input, time,
820 x, y, sx, sy,
821 window->user_data);
822
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400823 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500824}
825
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400826static void
827window_handle_button(void *data,
828 struct wl_input_device *input_device,
829 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500830{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400831 struct input *input = data;
832 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400833 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400834
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400835 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500836
837 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400838 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400839 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400840 wl_shell_move(window->display->shell,
841 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500842 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400843 case WINDOW_RESIZING_TOP:
844 case WINDOW_RESIZING_BOTTOM:
845 case WINDOW_RESIZING_LEFT:
846 case WINDOW_RESIZING_RIGHT:
847 case WINDOW_RESIZING_TOP_LEFT:
848 case WINDOW_RESIZING_TOP_RIGHT:
849 case WINDOW_RESIZING_BOTTOM_LEFT:
850 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400851 wl_shell_resize(window->display->shell,
852 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400853 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500854 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400855 case WINDOW_CLIENT_AREA:
856 if (window->button_handler)
857 (*window->button_handler)(window,
858 input, time,
859 button, state,
860 window->user_data);
861 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500862 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400863 } else {
864 if (window->button_handler)
865 (*window->button_handler)(window,
866 input, time,
867 button, state,
868 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500869 }
870}
871
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500872static void
873window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400874 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500875{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400876 struct input *input = data;
877 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400878 struct display *d = window->display;
879 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500880
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400881 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400882 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500883 return;
884
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400885 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400886 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400887 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
888 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500889
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400890 sym = XkbKeySymEntry(d->xkb, code, level, 0);
891
892 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400893 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400894 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400895 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500896
897 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400898 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400899 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500900}
901
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500902static void
903window_handle_pointer_focus(void *data,
904 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400905 uint32_t time, struct wl_surface *surface,
906 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500907{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400908 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400909 struct window *window;
910 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400911
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400912 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400913 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400914 window = input->pointer_focus;
915
916 pointer = POINTER_LEFT_PTR;
917 if (window->motion_handler)
918 pointer = (*window->motion_handler)(window,
919 input, time,
920 x, y, sx, sy,
921 window->user_data);
922
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400923 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400924 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400925 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400926 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400927 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500928}
929
930static void
931window_handle_keyboard_focus(void *data,
932 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400933 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500934 struct wl_surface *surface,
935 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500936{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400937 struct input *input = data;
938 struct window *window = input->keyboard_focus;
939 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500940 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500941
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400942 window = input->keyboard_focus;
943 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500944 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400945 if (window->keyboard_focus_handler)
946 (*window->keyboard_focus_handler)(window, NULL,
947 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500948 }
949
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400950 if (surface)
951 input->keyboard_focus = wl_surface_get_user_data(surface);
952 else
953 input->keyboard_focus = NULL;
954
955 end = keys->data + keys->size;
956 for (k = keys->data; k < end; k++)
957 input->modifiers |= d->xkb->map->modmap[*k];
958
959 window = input->keyboard_focus;
960 if (window) {
961 window->keyboard_device = input;
962 if (window->keyboard_focus_handler)
963 (*window->keyboard_focus_handler)(window,
964 window->keyboard_device,
965 window->user_data);
966 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500967}
968
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500969static const struct wl_input_device_listener input_device_listener = {
970 window_handle_motion,
971 window_handle_button,
972 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500973 window_handle_pointer_focus,
974 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500975};
976
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400977void
978input_get_position(struct input *input, int32_t *x, int32_t *y)
979{
980 *x = input->sx;
981 *y = input->sy;
982}
983
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400984struct wl_input_device *
985input_get_input_device(struct input *input)
986{
987 return input->input_device;
988}
989
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400990struct wl_drag *
991window_start_drag(struct window *window, struct input *input, uint32_t time,
992 const struct wl_drag_listener *listener, void *data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400993{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400994 struct wl_drag *drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400995
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400996 cairo_device_flush (window->display->device);
997
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400998 drag = wl_shell_create_drag(window->display->shell);
999 wl_drag_offer(drag, "text/plain");
1000 wl_drag_offer(drag, "text/html");
1001 wl_drag_activate(drag, window->surface, input->input_device, time);
1002 wl_drag_add_listener(drag, listener, data);
1003
1004 return drag;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001005}
1006
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001007static void
1008handle_configure(void *data, struct wl_shell *shell,
1009 uint32_t time, uint32_t edges,
1010 struct wl_surface *surface,
1011 int32_t x, int32_t y, int32_t width, int32_t height)
1012{
1013 struct window *window = wl_surface_get_user_data(surface);
1014
1015 window->resize_edges = edges;
1016 window->pending_allocation.x = x;
1017 window->pending_allocation.y = y;
1018 window->pending_allocation.width = width;
1019 window->pending_allocation.height = height;
1020
Joel Teichroeb88b272d2010-11-30 10:22:13 -08001021 if (edges & WINDOW_TITLEBAR) {
1022 window->allocation.x = window->pending_allocation.x;
1023 window->allocation.y = window->pending_allocation.y;
1024 } else if (edges & WINDOW_RESIZING_MASK) {
1025 if (window->resize_handler)
1026 (*window->resize_handler)(window,
1027 window->user_data);
1028 else if (window->redraw_handler)
1029 window_schedule_redraw(window);
1030 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001031}
1032
1033static const struct wl_shell_listener shell_listener = {
1034 handle_configure,
1035};
1036
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001037void
1038window_get_child_rectangle(struct window *window,
1039 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001040{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001041 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001042 *rectangle = window->allocation;
1043 } else {
1044 rectangle->x = window->margin + 10;
1045 rectangle->y = window->margin + 50;
1046 rectangle->width = window->allocation.width - 20 - window->margin * 2;
1047 rectangle->height = window->allocation.height - 60 - window->margin * 2;
1048 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001049}
1050
1051void
Kristian Høgsberg22106762008-12-08 13:50:07 -05001052window_set_child_size(struct window *window,
1053 struct rectangle *rectangle)
1054{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001055 int32_t width, height;
1056
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001057 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001058 width = rectangle->width + 20 + window->margin * 2;
1059 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001060
1061 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1062 window->allocation.x +=
1063 window->allocation.width - width;
1064 if (window->resize_edges & WINDOW_RESIZING_TOP)
1065 window->allocation.y +=
1066 window->allocation.height - height;
1067
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001068 window->allocation.width = width;
1069 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001070 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001071}
1072
1073void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001074window_copy_image(struct window *window,
1075 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001076{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001077 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001078}
1079
1080void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001081window_copy_surface(struct window *window,
1082 struct rectangle *rectangle,
1083 cairo_surface_t *surface)
1084{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001085 cairo_t *cr;
1086
1087 cr = cairo_create (window->cairo_surface);
1088
1089 cairo_set_source_surface (cr,
1090 surface,
1091 rectangle->x, rectangle->y);
1092
1093 cairo_paint (cr);
1094 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001095}
1096
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001097static gboolean
1098idle_redraw(void *data)
1099{
1100 struct window *window = data;
1101
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001102 if (window->resize_edges)
1103 window->allocation = window->pending_allocation;
1104
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001105 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001106
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001107 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001108 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001109
1110 return FALSE;
1111}
1112
1113void
1114window_schedule_redraw(struct window *window)
1115{
1116 if (!window->redraw_scheduled) {
1117 g_idle_add(idle_redraw, window);
1118 window->redraw_scheduled = 1;
1119 }
1120}
1121
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001122void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001123window_set_fullscreen(struct window *window, int fullscreen)
1124{
1125 window->fullscreen = fullscreen;
1126 if (window->fullscreen) {
1127 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001128 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001129 } else {
1130 window->allocation = window->saved_allocation;
1131 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001132}
1133
1134void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001135window_set_decoration(struct window *window, int decoration)
1136{
1137 window->decoration = decoration;
1138}
1139
1140void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001141window_set_user_data(struct window *window, void *data)
1142{
1143 window->user_data = data;
1144}
1145
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001146void *
1147window_get_user_data(struct window *window)
1148{
1149 return window->user_data;
1150}
1151
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001152void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001153window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001154 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001155{
1156 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001157}
1158
1159void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001160window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001161 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001162{
1163 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001164}
1165
1166void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001167window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001168 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001169{
1170 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001171}
1172
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001173void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001174window_set_button_handler(struct window *window,
1175 window_button_handler_t handler)
1176{
1177 window->button_handler = handler;
1178}
1179
1180void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001181window_set_motion_handler(struct window *window,
1182 window_motion_handler_t handler)
1183{
1184 window->motion_handler = handler;
1185}
1186
1187void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001188window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001189 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001190{
1191 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001192}
1193
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001194void
1195window_move(struct window *window, int32_t x, int32_t y)
1196{
1197 window->allocation.x = x;
1198 window->allocation.y = y;
1199
1200 wl_surface_map(window->surface,
1201 window->allocation.x - window->margin,
1202 window->allocation.y - window->margin,
1203 window->allocation.width,
1204 window->allocation.height);
1205}
1206
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001207void
1208window_damage(struct window *window, int32_t x, int32_t y,
1209 int32_t width, int32_t height)
1210{
1211 wl_surface_damage(window->surface, x, y, width, height);
1212}
1213
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001214struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001215window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001216 int32_t x, int32_t y, int32_t width, int32_t height)
1217{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001218 struct window *window;
1219
1220 window = malloc(sizeof *window);
1221 if (window == NULL)
1222 return NULL;
1223
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001224 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001225 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001226 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001227 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001228 window->allocation.x = x;
1229 window->allocation.y = y;
1230 window->allocation.width = width;
1231 window->allocation.height = height;
1232 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001233 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001234 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001235
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001236#ifdef HAVE_CAIRO_GL
1237 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1238#else
1239 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1240#endif
1241
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001242 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001243 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001244
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001245 return window;
1246}
1247
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001248void
1249window_set_buffer_type(struct window *window, enum window_buffer_type type)
1250{
1251 window->buffer_type = type;
1252}
1253
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001254static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001255drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001256{
1257 struct display *d = data;
1258
1259 d->device_name = strdup(device);
1260}
1261
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001262static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1263{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001264 struct display *d = data;
1265
1266 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001267}
1268
1269static const struct wl_drm_listener drm_listener = {
1270 drm_handle_device,
1271 drm_handle_authenticated
1272};
1273
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001274static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001275display_handle_geometry(void *data,
1276 struct wl_output *output,
1277 int32_t width, int32_t height)
1278{
1279 struct display *display = data;
1280
1281 display->screen_allocation.x = 0;
1282 display->screen_allocation.y = 0;
1283 display->screen_allocation.width = width;
1284 display->screen_allocation.height = height;
1285}
1286
1287static const struct wl_output_listener output_listener = {
1288 display_handle_geometry,
1289};
1290
1291static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001292display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001293{
1294 struct input *input;
1295
1296 input = malloc(sizeof *input);
1297 if (input == NULL)
1298 return;
1299
1300 memset(input, 0, sizeof *input);
1301 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001302 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001303 input->pointer_focus = NULL;
1304 input->keyboard_focus = NULL;
1305 wl_list_insert(d->input_list.prev, &input->link);
1306
1307 wl_input_device_add_listener(input->input_device,
1308 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001309 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001310}
1311
1312static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001313display_handle_global(struct wl_display *display, uint32_t id,
1314 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001315{
1316 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001317 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001318
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001319 if (strcmp(interface, "compositor") == 0) {
1320 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001321 } else if (strcmp(interface, "output") == 0) {
1322 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001323 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001324 } else if (strcmp(interface, "input_device") == 0) {
1325 display_add_input(d, id);
1326 } else if (strcmp(interface, "shell") == 0) {
1327 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001328 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001329 } else if (strcmp(interface, "drm") == 0) {
1330 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001331 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001332 } else if (strcmp(interface, "shm") == 0) {
1333 d->shm = wl_shm_create(display, id);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001334 } else if (strcmp(interface, "drag_offer") == 0) {
Kristian Høgsberg8857a962010-11-22 13:02:21 -05001335 if (d->drag_offer_handler) {
1336 offer = wl_drag_offer_create(display, id);
1337 d->drag_offer_handler(offer, d);
1338 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001339 }
1340}
1341
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001342static const char socket_name[] = "\0wayland";
1343
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001344static void
1345display_render_frame(struct display *d)
1346{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001347 int radius = 8;
1348 cairo_t *cr;
1349
1350 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1351 cr = cairo_create(d->shadow);
1352 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1353 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1354 rounded_rect(cr, 16, 16, 112, 112, radius);
1355 cairo_fill(cr);
1356 cairo_destroy(cr);
1357 blur_surface(d->shadow, 64);
1358
1359 d->active_frame =
1360 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1361 cr = cairo_create(d->active_frame);
1362 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1363 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1364 rounded_rect(cr, 16, 16, 112, 112, radius);
1365 cairo_fill(cr);
1366 cairo_destroy(cr);
1367
1368 d->inactive_frame =
1369 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1370 cr = cairo_create(d->inactive_frame);
1371 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1372 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1373 rounded_rect(cr, 16, 16, 112, 112, radius);
1374 cairo_fill(cr);
1375 cairo_destroy(cr);
1376}
1377
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001378static void
1379init_xkb(struct display *d)
1380{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001381 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001382
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001383 names.rules = "evdev";
1384 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001385 names.layout = option_xkb_layout;
1386 names.variant = option_xkb_variant;
1387 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001388
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001389 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001390 if (!d->xkb) {
1391 fprintf(stderr, "Failed to compile keymap\n");
1392 exit(1);
1393 }
1394}
1395
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001396struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001397display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001398{
1399 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001400 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001401 int fd;
1402 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001403 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001404 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001405 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001406
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001407 g_type_init();
1408
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001409 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001410 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001411 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001412
1413 xkb_option_group = g_option_group_new("xkb",
1414 "Keyboard options",
1415 "Show all XKB options",
1416 NULL, NULL);
1417 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1418 g_option_context_add_group (context, xkb_option_group);
1419
1420 if (!g_option_context_parse(context, argc, argv, &error)) {
1421 fprintf(stderr, "option parsing failed: %s\n", error->message);
1422 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001423 }
1424
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001425
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001426 d = malloc(sizeof *d);
1427 if (d == NULL)
1428 return NULL;
1429
Benjamin Franzke35fd2a82010-11-27 19:04:11 +01001430 d->display = wl_display_connect(socket_name, sizeof socket_name);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001431 if (d->display == NULL) {
1432 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001433 return NULL;
1434 }
1435
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001436 wl_list_init(&d->input_list);
1437
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001438 /* Set up listener so we'll catch all events. */
1439 wl_display_add_global_listener(d->display,
1440 display_handle_global, d);
1441
1442 /* Process connection events. */
1443 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1444
1445 fd = open(d->device_name, O_RDWR);
1446 if (fd < 0) {
1447 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001448 return NULL;
1449 }
1450
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001451 if (drmGetMagic(fd, &magic)) {
1452 fprintf(stderr, "DRI2: failed to get drm magic");
1453 return NULL;
1454 }
1455
1456 /* Wait for authenticated event */
1457 wl_drm_authenticate(d->drm, magic);
1458 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1459 while (!d->authenticated)
1460 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1461
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001462 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001463 if (!eglInitialize(d->dpy, &major, &minor)) {
1464 fprintf(stderr, "failed to initialize display\n");
1465 return NULL;
1466 }
1467
Darxus55973f22010-11-22 21:24:39 -05001468 if (!eglBindAPI(EGL_OPENGL_API)) {
1469 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1470 return NULL;
1471 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001472
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001473 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001474 if (d->ctx == NULL) {
1475 fprintf(stderr, "failed to create context\n");
1476 return NULL;
1477 }
1478
1479 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1480 fprintf(stderr, "faile to make context current\n");
1481 return NULL;
1482 }
1483
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001484#ifdef HAVE_CAIRO_GL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001485 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001486 if (d->device == NULL) {
1487 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001488 return NULL;
1489 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001490#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001491
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001492 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001493
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001494 display_render_frame(d);
1495
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001496 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001497 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001498 g_source_attach(d->source, NULL);
1499
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001500 wl_list_init(&d->window_list);
1501
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001502 init_xkb(d);
1503
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001504 return d;
1505}
1506
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001507struct wl_display *
1508display_get_display(struct display *display)
1509{
1510 return display->display;
1511}
1512
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001513struct wl_compositor *
1514display_get_compositor(struct display *display)
1515{
1516 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001517}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001518
1519EGLDisplay
1520display_get_egl_display(struct display *d)
1521{
1522 return d->dpy;
1523}
1524
1525void
1526display_run(struct display *d)
1527{
1528 g_main_loop_run(d->loop);
1529}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001530
1531void
1532display_set_drag_offer_handler(struct display *display,
1533 display_drag_offer_handler_t handler)
1534{
1535 display->drag_offer_handler = handler;
1536}