blob: 9d5c9d1e7328e45f1e9c3eaf57edc89d02ae46d0 [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
Kristian Høgsberg8def2642011-01-14 17:41:33 -050046#ifdef HAVE_CAIRO_EGL
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øgsbergb46df052011-01-18 09:17:57 -050083 display_global_handler_t global_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084};
85
86struct window {
87 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050089 const char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050090 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040091 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040092 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050093 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050094 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050095 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040096 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040097 struct input *grab_device;
98 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050099 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400100 enum window_buffer_type buffer_type;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500101 int mapped;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500102
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400103 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500104 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500105
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500106 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400107 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500108 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400109 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500110 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400111 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500113 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400114 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500115};
116
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400117struct input {
118 struct display *display;
119 struct wl_input_device *input_device;
120 struct window *pointer_focus;
121 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400122 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 uint32_t modifiers;
124 int32_t x, y, sx, sy;
125 struct wl_list link;
126};
127
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400128enum {
129 POINTER_DEFAULT = 100,
130 POINTER_UNSET
131};
132
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500133enum window_location {
134 WINDOW_INTERIOR = 0,
135 WINDOW_RESIZING_TOP = 1,
136 WINDOW_RESIZING_BOTTOM = 2,
137 WINDOW_RESIZING_LEFT = 4,
138 WINDOW_RESIZING_TOP_LEFT = 5,
139 WINDOW_RESIZING_BOTTOM_LEFT = 6,
140 WINDOW_RESIZING_RIGHT = 8,
141 WINDOW_RESIZING_TOP_RIGHT = 9,
142 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
143 WINDOW_RESIZING_MASK = 15,
144 WINDOW_EXTERIOR = 16,
145 WINDOW_TITLEBAR = 17,
146 WINDOW_CLIENT_AREA = 18,
147};
148
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400149const char *option_xkb_layout = "us";
150const char *option_xkb_variant = "";
151const char *option_xkb_options = "";
152
153static const GOptionEntry xkb_option_entries[] = {
154 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
155 &option_xkb_layout, "XKB Layout" },
156 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
157 &option_xkb_variant, "XKB Variant" },
158 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_options, "XKB Options" },
160 { NULL }
161};
162
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500163static void
164rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
165{
166 cairo_move_to(cr, x0, y0 + radius);
167 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
168 cairo_line_to(cr, x1 - radius, y0);
169 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
170 cairo_line_to(cr, x1, y1 - radius);
171 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
172 cairo_line_to(cr, x0 + radius, y1);
173 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
174 cairo_close_path(cr);
175}
176
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400177static const cairo_user_data_key_t surface_data_key;
178struct surface_data {
179 struct wl_buffer *buffer;
180};
181
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500182#define MULT(_d,c,a,t) \
183 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
184
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500185#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400186
187struct drm_surface_data {
188 struct surface_data data;
189 EGLImageKHR image;
190 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800191 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400192};
193
194static void
195drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400196{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400197 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800198 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400199
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800200 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400201 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800202 cairo_device_release(d->device);
203
204 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400205 wl_buffer_destroy(data->data.buffer);
206}
207
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500208EGLImageKHR
209display_get_image_for_drm_surface(struct display *display,
210 cairo_surface_t *surface)
211{
212 struct drm_surface_data *data;
213
214 data = cairo_surface_get_user_data (surface, &surface_data_key);
215
216 return data->image;
217}
218
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500219static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400220display_create_drm_surface(struct display *display,
221 struct rectangle *rectangle)
222{
223 struct drm_surface_data *data;
224 EGLDisplay dpy = display->dpy;
225 cairo_surface_t *surface;
226 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400227 EGLint name, stride;
228
229 EGLint image_attribs[] = {
230 EGL_WIDTH, 0,
231 EGL_HEIGHT, 0,
232 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
233 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
234 EGL_NONE
235 };
236
237 data = malloc(sizeof *data);
238 if (data == NULL)
239 return NULL;
240
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800241 data->display = display;
242
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400243 image_attribs[1] = rectangle->width;
244 image_attribs[3] = rectangle->height;
245 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800246
247 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400248 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400249 glBindTexture(GL_TEXTURE_2D, data->texture);
250 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800251 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400252
253 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
254
255 visual = wl_display_get_premultiplied_argb_visual(display->display);
256 data->data.buffer =
257 wl_drm_create_buffer(display->drm, name, rectangle->width,
258 rectangle->height, stride, visual);
259
260 surface = cairo_gl_surface_create_for_texture(display->device,
261 CAIRO_CONTENT_COLOR_ALPHA,
262 data->texture,
263 rectangle->width,
264 rectangle->height);
265
266 cairo_surface_set_user_data (surface, &surface_data_key,
267 data, drm_surface_data_destroy);
268
269 return surface;
270}
271
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500272static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400273display_create_drm_surface_from_file(struct display *display,
274 const char *filename,
275 struct rectangle *rect)
276{
277 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400278 GdkPixbuf *pixbuf;
279 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400280 int stride, i;
281 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800282 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400283
284 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400285 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400286 FALSE, &error);
287 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400288 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400289
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400290 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
291 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
292 gdk_pixbuf_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400293 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400294 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400295
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400296
297 stride = gdk_pixbuf_get_rowstride(pixbuf);
298 pixels = gdk_pixbuf_get_pixels(pixbuf);
299
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400300 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400301 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400302 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400303 while (p < end) {
304 unsigned int t;
305
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400306 MULT(p[0], p[0], p[3], t);
307 MULT(p[1], p[1], p[3], t);
308 MULT(p[2], p[2], p[3], t);
309 p += 4;
310
311 }
312 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400313
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800315 data = cairo_surface_get_user_data(surface, &surface_data_key);
316
317 cairo_device_acquire(display->device);
318 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800319 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
320 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800321 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400322
323 gdk_pixbuf_unref(pixbuf);
324
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 return surface;
326}
327
328#endif
329
330struct wl_buffer *
331display_get_buffer_for_surface(struct display *display,
332 cairo_surface_t *surface)
333{
334 struct surface_data *data;
335
336 data = cairo_surface_get_user_data (surface, &surface_data_key);
337
338 return data->buffer;
339}
340
341struct shm_surface_data {
342 struct surface_data data;
343 void *map;
344 size_t length;
345};
346
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500347static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400348shm_surface_data_destroy(void *p)
349{
350 struct shm_surface_data *data = p;
351
352 wl_buffer_destroy(data->data.buffer);
353 munmap(data->map, data->length);
354}
355
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500356static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357display_create_shm_surface(struct display *display,
358 struct rectangle *rectangle)
359{
360 struct shm_surface_data *data;
361 cairo_surface_t *surface;
362 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800363 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400364 char filename[] = "/tmp/wayland-shm-XXXXXX";
365
366 data = malloc(sizeof *data);
367 if (data == NULL)
368 return NULL;
369
370 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
371 rectangle->width);
372 data->length = stride * rectangle->height;
373 fd = mkstemp(filename);
374 if (fd < 0) {
375 fprintf(stderr, "open %s failed: %m", filename);
376 return NULL;
377 }
378 if (ftruncate(fd, data->length) < 0) {
379 fprintf(stderr, "ftruncate failed: %m");
380 close(fd);
381 return NULL;
382 }
383
384 data->map = mmap(NULL, data->length,
385 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
386 unlink(filename);
387
388 if (data->map == MAP_FAILED) {
389 fprintf(stderr, "mmap failed: %m");
390 close(fd);
391 return NULL;
392 }
393
394 surface = cairo_image_surface_create_for_data (data->map,
395 CAIRO_FORMAT_ARGB32,
396 rectangle->width,
397 rectangle->height,
398 stride);
399
400 cairo_surface_set_user_data (surface, &surface_data_key,
401 data, shm_surface_data_destroy);
402
403 visual = wl_display_get_premultiplied_argb_visual(display->display);
404 data->data.buffer = wl_shm_create_buffer(display->shm,
405 fd,
406 rectangle->width,
407 rectangle->height,
408 stride, visual);
409
410 close(fd);
411
412 return surface;
413}
414
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500415static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416display_create_shm_surface_from_file(struct display *display,
417 const char *filename,
418 struct rectangle *rect)
419{
420 cairo_surface_t *surface;
421 GdkPixbuf *pixbuf;
422 GError *error = NULL;
423 int stride, i;
424 unsigned char *pixels, *p, *end, *dest_data;
425 int dest_stride;
426 uint32_t *d;
427
428 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
429 rect->width, rect->height,
430 FALSE, &error);
431 if (error != NULL)
432 return NULL;
433
434 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
435 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
436 gdk_pixbuf_unref(pixbuf);
437 return NULL;
438 }
439
440 stride = gdk_pixbuf_get_rowstride(pixbuf);
441 pixels = gdk_pixbuf_get_pixels(pixbuf);
442
443 surface = display_create_shm_surface(display, rect);
444 dest_data = cairo_image_surface_get_data (surface);
445 dest_stride = cairo_image_surface_get_stride (surface);
446
447 for (i = 0; i < rect->height; i++) {
448 d = (uint32_t *) (dest_data + i * dest_stride);
449 p = pixels + i * stride;
450 end = p + rect->width * 4;
451 while (p < end) {
452 unsigned int t;
453 unsigned char a, r, g, b;
454
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400455 a = p[3];
456 MULT(r, p[0], a, t);
457 MULT(g, p[1], a, t);
458 MULT(b, p[2], a, t);
459 p += 4;
460 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
461 }
462 }
463
464 gdk_pixbuf_unref(pixbuf);
465
466 return surface;
467}
468
469cairo_surface_t *
470display_create_surface(struct display *display,
471 struct rectangle *rectangle)
472{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500473#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500474 if (display->drm) {
475 return display_create_drm_surface(display, rectangle);
476 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500478 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479}
480
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500481static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400482display_create_surface_from_file(struct display *display,
483 const char *filename,
484 struct rectangle *rectangle)
485{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500486#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500487 if (display->drm) {
488 return display_create_drm_surface_from_file(display, filename, rectangle);
489 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500491 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400492}
Yuval Fledel45568f62010-12-06 09:18:12 -0500493 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494 const char *filename;
495 int hotspot_x, hotspot_y;
496} pointer_images[] = {
497 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
498 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
499 { DATADIR "/wayland/bottom_side.png", 16, 20 },
500 { DATADIR "/wayland/grabbing.png", 20, 17 },
501 { DATADIR "/wayland/left_ptr.png", 10, 5 },
502 { DATADIR "/wayland/left_side.png", 10, 20 },
503 { DATADIR "/wayland/right_side.png", 30, 19 },
504 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
505 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
506 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400507 { DATADIR "/wayland/xterm.png", 15, 15 },
508 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400509};
510
511static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400512create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400513{
514 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400515 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400516 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400517
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400518 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400519 display->pointer_surfaces =
520 malloc(count * sizeof *display->pointer_surfaces);
521 rect.width = width;
522 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400523 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400524 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525 display_create_surface_from_file(display,
526 pointer_images[i].filename,
527 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400528 }
529
530}
531
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400532cairo_surface_t *
533display_get_pointer_surface(struct display *display, int pointer,
534 int *width, int *height,
535 int *hotspot_x, int *hotspot_y)
536{
537 cairo_surface_t *surface;
538
539 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500540#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400541 *width = cairo_gl_surface_get_width(surface);
542 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000543#else
544 *width = cairo_image_surface_get_width(surface);
545 *height = cairo_image_surface_get_height(surface);
546#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400547 *hotspot_x = pointer_images[pointer].hotspot_x;
548 *hotspot_y = pointer_images[pointer].hotspot_y;
549
550 return cairo_surface_reference(surface);
551}
552
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400553
554static void
555window_attach_surface(struct window *window);
556
557static void
558free_surface(void *data)
559{
560 struct window *window = data;
561
562 cairo_surface_destroy(window->pending_surface);
563 window->pending_surface = NULL;
564 if (window->cairo_surface)
565 window_attach_surface(window);
566}
567
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500568static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500569window_attach_surface(struct window *window)
570{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400571 struct display *display = window->display;
572 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500573 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500574
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500575 if (window->pending_surface != NULL)
576 return;
577
Kristian Høgsberg09531622010-06-14 23:22:15 -0400578 window->pending_surface = window->cairo_surface;
579 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500580
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400581 buffer = display_get_buffer_for_surface(display,
582 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500583 if (window->resize_edges & WINDOW_RESIZING_LEFT)
584 x = window->server_allocation.width -
585 window->allocation.width;
586 else
587 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500588
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500589 if (window->resize_edges & WINDOW_RESIZING_TOP)
590 y = window->server_allocation.height -
591 window->allocation.height;
592 else
593 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400594
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500595 window->server_allocation = window->allocation;
596 window->resize_edges = 0;
597 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400598 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500599
600 if (!window->mapped) {
601 wl_surface_map_toplevel(window->surface);
602 window->mapped = 1;
603 }
604
605 wl_surface_damage(window->surface, 0, 0,
606 window->allocation.width,
607 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500608}
609
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500610void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400611window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500612{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400613 if (window->cairo_surface)
614 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500615}
616
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400617void
618window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500620 cairo_surface_reference(surface);
621
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400622 if (window->cairo_surface != NULL)
623 cairo_surface_destroy(window->cairo_surface);
624
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500625 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400626}
627
628void
629window_create_surface(struct window *window)
630{
631 cairo_surface_t *surface;
632
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500634#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400636 surface = display_create_surface(window->display,
637 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638 break;
639#endif
640 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400641 surface = display_create_shm_surface(window->display,
642 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400643 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800644 default:
645 surface = NULL;
646 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400648
649 window_set_surface(window, surface);
650 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651}
652
653static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500654window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500655{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500656 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500657 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400658 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400659 cairo_surface_t *frame;
660 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500661
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400662 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400663
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400664 width = window->allocation.width;
665 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500666
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500667 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500668 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500669 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
670
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500671 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500672
Kristian Høgsberg09531622010-06-14 23:22:15 -0400673 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400674 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400675 cairo_paint(cr);
676
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400677 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678 tile_mask(cr, window->display->shadow,
679 shadow_dx, shadow_dy, width, height,
680 window->margin + 10 - shadow_dx,
681 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500682
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400683 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400684 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400685 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400686 frame = window->display->inactive_frame;
687
688 tile_source(cr, frame, 0, 0, width, height,
689 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500690
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500691 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
692 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500693 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400694 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500695 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
696 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
697 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400698 if (window->keyboard_device)
699 cairo_set_source_rgb(cr, 0, 0, 0);
700 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400701 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400702 cairo_show_text(cr, window->title);
703
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500704 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400705
706 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500707}
708
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400709void
710display_flush_cairo_device(struct display *display)
711{
712 cairo_device_flush (display->device);
713}
714
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500715void
716window_draw(struct window *window)
717{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400718 if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400719 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500720 else
721 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500722}
723
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400724cairo_surface_t *
725window_get_surface(struct window *window)
726{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400727 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400728}
729
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400730static int
731get_pointer_location(struct window *window, int32_t x, int32_t y)
732{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400734 const int grip_size = 8;
735
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500736 if (!window->decoration)
737 return WINDOW_CLIENT_AREA;
738
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400739 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400740 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400741 else if (window->margin <= x && x < window->margin + grip_size)
742 hlocation = WINDOW_RESIZING_LEFT;
743 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400744 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400745 else if (x < window->allocation.width - window->margin)
746 hlocation = WINDOW_RESIZING_RIGHT;
747 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400748 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400749
750 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400751 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400752 else if (window->margin <= y && y < window->margin + grip_size)
753 vlocation = WINDOW_RESIZING_TOP;
754 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400755 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400756 else if (y < window->allocation.height - window->margin)
757 vlocation = WINDOW_RESIZING_BOTTOM;
758 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400759 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400760
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400761 location = vlocation | hlocation;
762 if (location & WINDOW_EXTERIOR)
763 location = WINDOW_EXTERIOR;
764 if (location == WINDOW_INTERIOR && y < window->margin + 50)
765 location = WINDOW_TITLEBAR;
766 else if (location == WINDOW_INTERIOR)
767 location = WINDOW_CLIENT_AREA;
768
769 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400770}
771
772static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400773set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400774{
775 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400776 struct wl_buffer *buffer;
777 cairo_surface_t *surface;
778 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400779
780 location = get_pointer_location(input->pointer_focus,
781 input->sx, input->sy);
782 switch (location) {
783 case WINDOW_RESIZING_TOP:
784 pointer = POINTER_TOP;
785 break;
786 case WINDOW_RESIZING_BOTTOM:
787 pointer = POINTER_BOTTOM;
788 break;
789 case WINDOW_RESIZING_LEFT:
790 pointer = POINTER_LEFT;
791 break;
792 case WINDOW_RESIZING_RIGHT:
793 pointer = POINTER_RIGHT;
794 break;
795 case WINDOW_RESIZING_TOP_LEFT:
796 pointer = POINTER_TOP_LEFT;
797 break;
798 case WINDOW_RESIZING_TOP_RIGHT:
799 pointer = POINTER_TOP_RIGHT;
800 break;
801 case WINDOW_RESIZING_BOTTOM_LEFT:
802 pointer = POINTER_BOTTOM_LEFT;
803 break;
804 case WINDOW_RESIZING_BOTTOM_RIGHT:
805 pointer = POINTER_BOTTOM_RIGHT;
806 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400807 case WINDOW_EXTERIOR:
808 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400809 if (input->current_pointer_image == POINTER_DEFAULT)
810 return;
811
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400812 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400813 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400814 return;
815 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400816 break;
817 }
818
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400819 if (pointer == input->current_pointer_image)
820 return;
821
822 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400823 surface = display->pointer_surfaces[pointer];
824 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400825 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400826 pointer_images[pointer].hotspot_x,
827 pointer_images[pointer].hotspot_y);
828}
829
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500830static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500831window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400832 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500833 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500834{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400835 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400836 struct window *window = input->pointer_focus;
837 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400838
839 input->x = x;
840 input->y = y;
841 input->sx = sx;
842 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400843
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400844 location = get_pointer_location(window, input->sx, input->sy);
845
846 if (window->motion_handler)
847 pointer = (*window->motion_handler)(window, input, time,
848 x, y, sx, sy,
849 window->user_data);
850
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400851 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500852}
853
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400854static void
855window_handle_button(void *data,
856 struct wl_input_device *input_device,
857 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500858{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400859 struct input *input = data;
860 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400861 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400862
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400863 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500864
865 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400866 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400867 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400868 wl_shell_move(window->display->shell,
869 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500870 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400871 case WINDOW_RESIZING_TOP:
872 case WINDOW_RESIZING_BOTTOM:
873 case WINDOW_RESIZING_LEFT:
874 case WINDOW_RESIZING_RIGHT:
875 case WINDOW_RESIZING_TOP_LEFT:
876 case WINDOW_RESIZING_TOP_RIGHT:
877 case WINDOW_RESIZING_BOTTOM_LEFT:
878 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400879 wl_shell_resize(window->display->shell,
880 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400881 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500882 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400883 case WINDOW_CLIENT_AREA:
884 if (window->button_handler)
885 (*window->button_handler)(window,
886 input, time,
887 button, state,
888 window->user_data);
889 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500890 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400891 } else {
892 if (window->button_handler)
893 (*window->button_handler)(window,
894 input, time,
895 button, state,
896 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500897 }
898}
899
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500900static void
901window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400902 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500903{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400904 struct input *input = data;
905 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400906 struct display *d = window->display;
907 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500908
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400909 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400910 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500911 return;
912
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400913 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400914 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400915 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
916 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500917
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400918 sym = XkbKeySymEntry(d->xkb, code, level, 0);
919
920 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400921 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400922 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400923 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500924
925 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400926 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400927 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500928}
929
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500930static void
931window_handle_pointer_focus(void *data,
932 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400933 uint32_t time, struct wl_surface *surface,
934 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500935{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400936 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400937 struct window *window;
938 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400939
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400940 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400941 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400942 window = input->pointer_focus;
943
944 pointer = POINTER_LEFT_PTR;
945 if (window->motion_handler)
946 pointer = (*window->motion_handler)(window,
947 input, time,
948 x, y, sx, sy,
949 window->user_data);
950
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400951 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400952 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400953 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400954 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400955 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500956}
957
958static void
959window_handle_keyboard_focus(void *data,
960 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400961 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500962 struct wl_surface *surface,
963 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500964{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400965 struct input *input = data;
966 struct window *window = input->keyboard_focus;
967 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500968 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500969
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400970 window = input->keyboard_focus;
971 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500972 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400973 if (window->keyboard_focus_handler)
974 (*window->keyboard_focus_handler)(window, NULL,
975 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500976 }
977
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400978 if (surface)
979 input->keyboard_focus = wl_surface_get_user_data(surface);
980 else
981 input->keyboard_focus = NULL;
982
983 end = keys->data + keys->size;
984 for (k = keys->data; k < end; k++)
985 input->modifiers |= d->xkb->map->modmap[*k];
986
987 window = input->keyboard_focus;
988 if (window) {
989 window->keyboard_device = input;
990 if (window->keyboard_focus_handler)
991 (*window->keyboard_focus_handler)(window,
992 window->keyboard_device,
993 window->user_data);
994 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500995}
996
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500997static const struct wl_input_device_listener input_device_listener = {
998 window_handle_motion,
999 window_handle_button,
1000 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001001 window_handle_pointer_focus,
1002 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001003};
1004
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001005void
1006input_get_position(struct input *input, int32_t *x, int32_t *y)
1007{
1008 *x = input->sx;
1009 *y = input->sy;
1010}
1011
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001012struct wl_input_device *
1013input_get_input_device(struct input *input)
1014{
1015 return input->input_device;
1016}
1017
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001018struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001019window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001020{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001021 cairo_device_flush (window->display->device);
1022
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001023 return wl_shell_create_drag(window->display->shell);
1024}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001025
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001026void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001027window_move(struct window *window, struct input *input, uint32_t time)
1028{
1029 wl_shell_move(window->display->shell,
1030 window->surface, input->input_device, time);
1031}
1032
1033void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001034window_activate_drag(struct wl_drag *drag, struct window *window,
1035 struct input *input, uint32_t time)
1036{
1037 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001038}
1039
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001040static void
1041handle_configure(void *data, struct wl_shell *shell,
1042 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001043 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001044{
1045 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001046 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001047
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001048 if (window->resize_handler) {
1049 child_width = width - 20 - window->margin * 2;
1050 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001051
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001052 (*window->resize_handler)(window,
1053 child_width, child_height,
1054 window->user_data);
1055 } else {
1056 window->resize_edges = edges;
1057 window->allocation.width = width;
1058 window->allocation.height = height;
1059
1060 if (window->redraw_handler)
1061 window_schedule_redraw(window);
1062 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001063}
1064
1065static const struct wl_shell_listener shell_listener = {
1066 handle_configure,
1067};
1068
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001069void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001070window_get_child_allocation(struct window *window,
1071 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001072{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001073 if (window->fullscreen && !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001074 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001075 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001076 allocation->x = window->margin + 10;
1077 allocation->y = window->margin + 50;
1078 allocation->width =
1079 window->allocation.width - 20 - window->margin * 2;
1080 allocation->height =
1081 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001082 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001083}
1084
1085void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001086window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001087{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001088 if (!window->fullscreen) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001089 window->allocation.width = width + 20 + window->margin * 2;
1090 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001091 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001092}
1093
1094void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001095window_copy_image(struct window *window,
1096 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001097{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001098 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001099}
1100
1101void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001102window_copy_surface(struct window *window,
1103 struct rectangle *rectangle,
1104 cairo_surface_t *surface)
1105{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001106 cairo_t *cr;
1107
1108 cr = cairo_create (window->cairo_surface);
1109
1110 cairo_set_source_surface (cr,
1111 surface,
1112 rectangle->x, rectangle->y);
1113
1114 cairo_paint (cr);
1115 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001116}
1117
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001118static gboolean
1119idle_redraw(void *data)
1120{
1121 struct window *window = data;
1122
1123 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001124
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001125 window->redraw_scheduled = 0;
1126
1127 return FALSE;
1128}
1129
1130void
1131window_schedule_redraw(struct window *window)
1132{
1133 if (!window->redraw_scheduled) {
1134 g_idle_add(idle_redraw, window);
1135 window->redraw_scheduled = 1;
1136 }
1137}
1138
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001139void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001140window_set_fullscreen(struct window *window, int fullscreen)
1141{
1142 window->fullscreen = fullscreen;
1143 if (window->fullscreen) {
1144 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001145 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001146 } else {
1147 window->allocation = window->saved_allocation;
1148 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001149}
1150
1151void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001152window_set_decoration(struct window *window, int decoration)
1153{
1154 window->decoration = decoration;
1155}
1156
1157void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001158window_set_user_data(struct window *window, void *data)
1159{
1160 window->user_data = data;
1161}
1162
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001163void *
1164window_get_user_data(struct window *window)
1165{
1166 return window->user_data;
1167}
1168
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001169void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001170window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001171 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001172{
1173 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001174}
1175
1176void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001177window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001178 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001179{
1180 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001181}
1182
1183void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001184window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001185 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001186{
1187 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001188}
1189
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001190void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001191window_set_button_handler(struct window *window,
1192 window_button_handler_t handler)
1193{
1194 window->button_handler = handler;
1195}
1196
1197void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001198window_set_motion_handler(struct window *window,
1199 window_motion_handler_t handler)
1200{
1201 window->motion_handler = handler;
1202}
1203
1204void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001205window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001206 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001207{
1208 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001209}
1210
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001211void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001212window_damage(struct window *window, int32_t x, int32_t y,
1213 int32_t width, int32_t height)
1214{
1215 wl_surface_damage(window->surface, x, y, width, height);
1216}
1217
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001218struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001219window_create(struct display *display, const char *title,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001220 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001221{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001222 struct window *window;
1223
1224 window = malloc(sizeof *window);
1225 if (window == NULL)
1226 return NULL;
1227
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001228 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001229 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001230 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001231 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001232 window->allocation.x = 0;
1233 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001234 window->allocation.width = width;
1235 window->allocation.height = height;
1236 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001237 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001238 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001239
Yuval Fledel45568f62010-12-06 09:18:12 -05001240 if (display->drm)
1241 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1242 else
1243 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001244
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001245 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001246 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001247
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001248 return window;
1249}
1250
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001251void
1252window_set_buffer_type(struct window *window, enum window_buffer_type type)
1253{
1254 window->buffer_type = type;
1255}
1256
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001257static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001258drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001259{
1260 struct display *d = data;
1261
1262 d->device_name = strdup(device);
1263}
1264
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001265static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1266{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001267 struct display *d = data;
1268
1269 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001270}
1271
1272static const struct wl_drm_listener drm_listener = {
1273 drm_handle_device,
1274 drm_handle_authenticated
1275};
1276
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001277static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001278display_handle_geometry(void *data,
1279 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001280 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001281{
1282 struct display *display = data;
1283
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001284 display->screen_allocation.x = x;
1285 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001286 display->screen_allocation.width = width;
1287 display->screen_allocation.height = height;
1288}
1289
1290static const struct wl_output_listener output_listener = {
1291 display_handle_geometry,
1292};
1293
1294static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001295display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001296{
1297 struct input *input;
1298
1299 input = malloc(sizeof *input);
1300 if (input == NULL)
1301 return;
1302
1303 memset(input, 0, sizeof *input);
1304 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001305 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001306 input->pointer_focus = NULL;
1307 input->keyboard_focus = NULL;
1308 wl_list_insert(d->input_list.prev, &input->link);
1309
1310 wl_input_device_add_listener(input->input_device,
1311 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001312 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001313}
1314
1315static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001316display_handle_global(struct wl_display *display, uint32_t id,
1317 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001318{
1319 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001320 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001321
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001322 if (strcmp(interface, "compositor") == 0) {
1323 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001324 } else if (strcmp(interface, "output") == 0) {
1325 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001326 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001327 } else if (strcmp(interface, "input_device") == 0) {
1328 display_add_input(d, id);
1329 } else if (strcmp(interface, "shell") == 0) {
1330 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001331 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001332 } else if (strcmp(interface, "drm") == 0) {
1333 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001334 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001335 } else if (strcmp(interface, "shm") == 0) {
1336 d->shm = wl_shm_create(display, id);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001337 } else if (strcmp(interface, "drag_offer") == 0) {
Kristian Høgsberg8857a962010-11-22 13:02:21 -05001338 if (d->drag_offer_handler) {
1339 offer = wl_drag_offer_create(display, id);
1340 d->drag_offer_handler(offer, d);
1341 }
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001342 } else if (d->global_handler) {
1343 d->global_handler(d, interface, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001344 }
1345}
1346
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001347static void
1348display_render_frame(struct display *d)
1349{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001350 int radius = 8;
1351 cairo_t *cr;
1352
1353 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1354 cr = cairo_create(d->shadow);
1355 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1356 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1357 rounded_rect(cr, 16, 16, 112, 112, radius);
1358 cairo_fill(cr);
1359 cairo_destroy(cr);
1360 blur_surface(d->shadow, 64);
1361
1362 d->active_frame =
1363 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1364 cr = cairo_create(d->active_frame);
1365 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1366 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1367 rounded_rect(cr, 16, 16, 112, 112, radius);
1368 cairo_fill(cr);
1369 cairo_destroy(cr);
1370
1371 d->inactive_frame =
1372 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1373 cr = cairo_create(d->inactive_frame);
1374 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1375 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1376 rounded_rect(cr, 16, 16, 112, 112, radius);
1377 cairo_fill(cr);
1378 cairo_destroy(cr);
1379}
1380
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001381static void
1382init_xkb(struct display *d)
1383{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001384 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001385
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001386 names.rules = "evdev";
1387 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001388 names.layout = option_xkb_layout;
1389 names.variant = option_xkb_variant;
1390 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001391
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001392 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001393 if (!d->xkb) {
1394 fprintf(stderr, "Failed to compile keymap\n");
1395 exit(1);
1396 }
1397}
1398
Yuval Fledel45568f62010-12-06 09:18:12 -05001399static int
1400init_drm(struct display *d)
1401{
1402 EGLint major, minor;
1403 int fd;
1404 drm_magic_t magic;
1405
1406 fd = open(d->device_name, O_RDWR);
1407 if (fd < 0) {
1408 fprintf(stderr, "drm open failed: %m\n");
1409 return -1;
1410 }
1411
1412 if (drmGetMagic(fd, &magic)) {
1413 fprintf(stderr, "DRI2: failed to get drm magic");
1414 return -1;
1415 }
1416
1417 /* Wait for authenticated event */
1418 wl_drm_authenticate(d->drm, magic);
1419 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1420 while (!d->authenticated)
1421 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1422
1423 d->dpy = eglGetDRMDisplayMESA(fd);
1424 if (!eglInitialize(d->dpy, &major, &minor)) {
1425 fprintf(stderr, "failed to initialize display\n");
1426 return -1;
1427 }
1428
1429 if (!eglBindAPI(EGL_OPENGL_API)) {
1430 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1431 return -1;
1432 }
1433
1434 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1435 if (d->ctx == NULL) {
1436 fprintf(stderr, "failed to create context\n");
1437 return -1;
1438 }
1439
1440 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1441 fprintf(stderr, "faile to make context current\n");
1442 return -1;
1443 }
1444
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001445#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001446 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1447 if (d->device == NULL) {
1448 fprintf(stderr, "failed to get cairo drm device\n");
1449 return -1;
1450 }
1451#endif
1452
1453 return 0;
1454}
1455
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001456struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001457display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001458{
1459 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001460 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001461 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001462 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001463
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001464 g_type_init();
1465
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001466 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001467 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001468 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001469
1470 xkb_option_group = g_option_group_new("xkb",
1471 "Keyboard options",
1472 "Show all XKB options",
1473 NULL, NULL);
1474 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1475 g_option_context_add_group (context, xkb_option_group);
1476
1477 if (!g_option_context_parse(context, argc, argv, &error)) {
1478 fprintf(stderr, "option parsing failed: %s\n", error->message);
1479 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001480 }
1481
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001482
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001483 d = malloc(sizeof *d);
1484 if (d == NULL)
1485 return NULL;
1486
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001487 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001488 if (d->display == NULL) {
1489 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001490 return NULL;
1491 }
1492
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001493 wl_list_init(&d->input_list);
1494
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001495 /* Set up listener so we'll catch all events. */
1496 wl_display_add_global_listener(d->display,
1497 display_handle_global, d);
1498
1499 /* Process connection events. */
1500 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1501
Yuval Fledel45568f62010-12-06 09:18:12 -05001502 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001503 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001504
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001505 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001506
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001507 display_render_frame(d);
1508
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001509 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001510 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001511 g_source_attach(d->source, NULL);
1512
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001513 wl_list_init(&d->window_list);
1514
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001515 init_xkb(d);
1516
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001517 return d;
1518}
1519
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001520struct wl_display *
1521display_get_display(struct display *display)
1522{
1523 return display->display;
1524}
1525
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001526struct wl_compositor *
1527display_get_compositor(struct display *display)
1528{
1529 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001530}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001531
1532EGLDisplay
1533display_get_egl_display(struct display *d)
1534{
1535 return d->dpy;
1536}
1537
1538void
1539display_run(struct display *d)
1540{
1541 g_main_loop_run(d->loop);
1542}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001543
1544void
1545display_set_drag_offer_handler(struct display *display,
1546 display_drag_offer_handler_t handler)
1547{
1548 display->drag_offer_handler = handler;
1549}
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001550
1551void
1552display_set_global_handler(struct display *display,
1553 display_global_handler_t handler)
1554{
1555 display->global_handler = handler;
1556}