blob: da9055c1311b0ee3de2e1ad18498a5a9d17d1741 [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
Kristian Høgsbergb46df052011-01-18 09:17:57 -050082 display_global_handler_t global_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083};
84
85struct window {
86 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050087 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050089 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050090 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050091 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040092 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040093 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050094 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050095 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050096 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040097 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040098 struct input *grab_device;
99 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400101 enum window_buffer_type buffer_type;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500102 int mapped;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500103
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400104 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500105 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500106
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500107 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400108 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500109 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500111 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400112 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400113
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500114 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400115 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116};
117
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400118struct input {
119 struct display *display;
120 struct wl_input_device *input_device;
121 struct window *pointer_focus;
122 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500123 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400124 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 uint32_t modifiers;
126 int32_t x, y, sx, sy;
127 struct wl_list link;
128};
129
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400130enum {
131 POINTER_DEFAULT = 100,
132 POINTER_UNSET
133};
134
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500135enum window_location {
136 WINDOW_INTERIOR = 0,
137 WINDOW_RESIZING_TOP = 1,
138 WINDOW_RESIZING_BOTTOM = 2,
139 WINDOW_RESIZING_LEFT = 4,
140 WINDOW_RESIZING_TOP_LEFT = 5,
141 WINDOW_RESIZING_BOTTOM_LEFT = 6,
142 WINDOW_RESIZING_RIGHT = 8,
143 WINDOW_RESIZING_TOP_RIGHT = 9,
144 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
145 WINDOW_RESIZING_MASK = 15,
146 WINDOW_EXTERIOR = 16,
147 WINDOW_TITLEBAR = 17,
148 WINDOW_CLIENT_AREA = 18,
149};
150
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400151const char *option_xkb_layout = "us";
152const char *option_xkb_variant = "";
153const char *option_xkb_options = "";
154
155static const GOptionEntry xkb_option_entries[] = {
156 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
157 &option_xkb_layout, "XKB Layout" },
158 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_variant, "XKB Variant" },
160 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_options, "XKB Options" },
162 { NULL }
163};
164
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500165static void
166rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
167{
168 cairo_move_to(cr, x0, y0 + radius);
169 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
170 cairo_line_to(cr, x1 - radius, y0);
171 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
172 cairo_line_to(cr, x1, y1 - radius);
173 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
174 cairo_line_to(cr, x0 + radius, y1);
175 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
176 cairo_close_path(cr);
177}
178
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400179static const cairo_user_data_key_t surface_data_key;
180struct surface_data {
181 struct wl_buffer *buffer;
182};
183
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500184#define MULT(_d,c,a,t) \
185 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
186
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500187#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400188
189struct drm_surface_data {
190 struct surface_data data;
191 EGLImageKHR image;
192 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800193 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400194};
195
196static void
197drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400198{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400199 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800200 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400201
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800202 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400203 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800204 cairo_device_release(d->device);
205
206 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400207 wl_buffer_destroy(data->data.buffer);
208}
209
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500210EGLImageKHR
211display_get_image_for_drm_surface(struct display *display,
212 cairo_surface_t *surface)
213{
214 struct drm_surface_data *data;
215
216 data = cairo_surface_get_user_data (surface, &surface_data_key);
217
218 return data->image;
219}
220
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500221static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400222display_create_drm_surface(struct display *display,
223 struct rectangle *rectangle)
224{
225 struct drm_surface_data *data;
226 EGLDisplay dpy = display->dpy;
227 cairo_surface_t *surface;
228 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400229 EGLint name, stride;
230
231 EGLint image_attribs[] = {
232 EGL_WIDTH, 0,
233 EGL_HEIGHT, 0,
234 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
235 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
236 EGL_NONE
237 };
238
239 data = malloc(sizeof *data);
240 if (data == NULL)
241 return NULL;
242
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800243 data->display = display;
244
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400245 image_attribs[1] = rectangle->width;
246 image_attribs[3] = rectangle->height;
247 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800248
249 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400250 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251 glBindTexture(GL_TEXTURE_2D, data->texture);
252 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800253 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400254
255 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
256
257 visual = wl_display_get_premultiplied_argb_visual(display->display);
258 data->data.buffer =
259 wl_drm_create_buffer(display->drm, name, rectangle->width,
260 rectangle->height, stride, visual);
261
262 surface = cairo_gl_surface_create_for_texture(display->device,
263 CAIRO_CONTENT_COLOR_ALPHA,
264 data->texture,
265 rectangle->width,
266 rectangle->height);
267
268 cairo_surface_set_user_data (surface, &surface_data_key,
269 data, drm_surface_data_destroy);
270
271 return surface;
272}
273
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500274static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275display_create_drm_surface_from_file(struct display *display,
276 const char *filename,
277 struct rectangle *rect)
278{
279 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400280 GdkPixbuf *pixbuf;
281 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400282 int stride, i;
283 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800284 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285
286 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400287 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400288 FALSE, &error);
289 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400291
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400292 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
293 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500294 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400295 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400296 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400297
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400298
299 stride = gdk_pixbuf_get_rowstride(pixbuf);
300 pixels = gdk_pixbuf_get_pixels(pixbuf);
301
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400302 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400303 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400304 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400305 while (p < end) {
306 unsigned int t;
307
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400308 MULT(p[0], p[0], p[3], t);
309 MULT(p[1], p[1], p[3], t);
310 MULT(p[2], p[2], p[3], t);
311 p += 4;
312
313 }
314 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400315
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 data = cairo_surface_get_user_data(surface, &surface_data_key);
318
319 cairo_device_acquire(display->device);
320 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800321 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
322 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400324
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500325 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400326
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327 return surface;
328}
329
330#endif
331
332struct wl_buffer *
333display_get_buffer_for_surface(struct display *display,
334 cairo_surface_t *surface)
335{
336 struct surface_data *data;
337
338 data = cairo_surface_get_user_data (surface, &surface_data_key);
339
340 return data->buffer;
341}
342
343struct shm_surface_data {
344 struct surface_data data;
345 void *map;
346 size_t length;
347};
348
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500349static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350shm_surface_data_destroy(void *p)
351{
352 struct shm_surface_data *data = p;
353
354 wl_buffer_destroy(data->data.buffer);
355 munmap(data->map, data->length);
356}
357
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500358static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359display_create_shm_surface(struct display *display,
360 struct rectangle *rectangle)
361{
362 struct shm_surface_data *data;
363 cairo_surface_t *surface;
364 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800365 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400366 char filename[] = "/tmp/wayland-shm-XXXXXX";
367
368 data = malloc(sizeof *data);
369 if (data == NULL)
370 return NULL;
371
372 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
373 rectangle->width);
374 data->length = stride * rectangle->height;
375 fd = mkstemp(filename);
376 if (fd < 0) {
377 fprintf(stderr, "open %s failed: %m", filename);
378 return NULL;
379 }
380 if (ftruncate(fd, data->length) < 0) {
381 fprintf(stderr, "ftruncate failed: %m");
382 close(fd);
383 return NULL;
384 }
385
386 data->map = mmap(NULL, data->length,
387 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
388 unlink(filename);
389
390 if (data->map == MAP_FAILED) {
391 fprintf(stderr, "mmap failed: %m");
392 close(fd);
393 return NULL;
394 }
395
396 surface = cairo_image_surface_create_for_data (data->map,
397 CAIRO_FORMAT_ARGB32,
398 rectangle->width,
399 rectangle->height,
400 stride);
401
402 cairo_surface_set_user_data (surface, &surface_data_key,
403 data, shm_surface_data_destroy);
404
405 visual = wl_display_get_premultiplied_argb_visual(display->display);
406 data->data.buffer = wl_shm_create_buffer(display->shm,
407 fd,
408 rectangle->width,
409 rectangle->height,
410 stride, visual);
411
412 close(fd);
413
414 return surface;
415}
416
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500417static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418display_create_shm_surface_from_file(struct display *display,
419 const char *filename,
420 struct rectangle *rect)
421{
422 cairo_surface_t *surface;
423 GdkPixbuf *pixbuf;
424 GError *error = NULL;
425 int stride, i;
426 unsigned char *pixels, *p, *end, *dest_data;
427 int dest_stride;
428 uint32_t *d;
429
430 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
431 rect->width, rect->height,
432 FALSE, &error);
433 if (error != NULL)
434 return NULL;
435
436 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
437 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500438 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439 return NULL;
440 }
441
442 stride = gdk_pixbuf_get_rowstride(pixbuf);
443 pixels = gdk_pixbuf_get_pixels(pixbuf);
444
445 surface = display_create_shm_surface(display, rect);
446 dest_data = cairo_image_surface_get_data (surface);
447 dest_stride = cairo_image_surface_get_stride (surface);
448
449 for (i = 0; i < rect->height; i++) {
450 d = (uint32_t *) (dest_data + i * dest_stride);
451 p = pixels + i * stride;
452 end = p + rect->width * 4;
453 while (p < end) {
454 unsigned int t;
455 unsigned char a, r, g, b;
456
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457 a = p[3];
458 MULT(r, p[0], a, t);
459 MULT(g, p[1], a, t);
460 MULT(b, p[2], a, t);
461 p += 4;
462 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
463 }
464 }
465
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500466 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467
468 return surface;
469}
470
471cairo_surface_t *
472display_create_surface(struct display *display,
473 struct rectangle *rectangle)
474{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500475#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500476 if (display->drm) {
477 return display_create_drm_surface(display, rectangle);
478 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500480 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400481}
482
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500483static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400484display_create_surface_from_file(struct display *display,
485 const char *filename,
486 struct rectangle *rectangle)
487{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500488#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500489 if (display->drm) {
490 return display_create_drm_surface_from_file(display, filename, rectangle);
491 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500493 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494}
Yuval Fledel45568f62010-12-06 09:18:12 -0500495 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400496 const char *filename;
497 int hotspot_x, hotspot_y;
498} pointer_images[] = {
499 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
500 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
501 { DATADIR "/wayland/bottom_side.png", 16, 20 },
502 { DATADIR "/wayland/grabbing.png", 20, 17 },
503 { DATADIR "/wayland/left_ptr.png", 10, 5 },
504 { DATADIR "/wayland/left_side.png", 10, 20 },
505 { DATADIR "/wayland/right_side.png", 30, 19 },
506 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
507 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
508 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400509 { DATADIR "/wayland/xterm.png", 15, 15 },
510 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400511};
512
513static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400514create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400515{
516 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400517 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400518 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400519
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400520 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400521 display->pointer_surfaces =
522 malloc(count * sizeof *display->pointer_surfaces);
523 rect.width = width;
524 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400525 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400526 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527 display_create_surface_from_file(display,
528 pointer_images[i].filename,
529 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400530 }
531
532}
533
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400534cairo_surface_t *
535display_get_pointer_surface(struct display *display, int pointer,
536 int *width, int *height,
537 int *hotspot_x, int *hotspot_y)
538{
539 cairo_surface_t *surface;
540
541 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500542#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400543 *width = cairo_gl_surface_get_width(surface);
544 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000545#else
546 *width = cairo_image_surface_get_width(surface);
547 *height = cairo_image_surface_get_height(surface);
548#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400549 *hotspot_x = pointer_images[pointer].hotspot_x;
550 *hotspot_y = pointer_images[pointer].hotspot_y;
551
552 return cairo_surface_reference(surface);
553}
554
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400555
556static void
557window_attach_surface(struct window *window);
558
559static void
560free_surface(void *data)
561{
562 struct window *window = data;
563
564 cairo_surface_destroy(window->pending_surface);
565 window->pending_surface = NULL;
566 if (window->cairo_surface)
567 window_attach_surface(window);
568}
569
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500570static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500571window_attach_surface(struct window *window)
572{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400573 struct display *display = window->display;
574 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500575 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500576
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500577 if (window->pending_surface != NULL)
578 return;
579
Kristian Høgsberg09531622010-06-14 23:22:15 -0400580 window->pending_surface = window->cairo_surface;
581 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500582
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400583 buffer = display_get_buffer_for_surface(display,
584 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500585 if (window->resize_edges & WINDOW_RESIZING_LEFT)
586 x = window->server_allocation.width -
587 window->allocation.width;
588 else
589 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500590
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500591 if (window->resize_edges & WINDOW_RESIZING_TOP)
592 y = window->server_allocation.height -
593 window->allocation.height;
594 else
595 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400596
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500597 window->server_allocation = window->allocation;
598 window->resize_edges = 0;
599 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400600 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500601
602 if (!window->mapped) {
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500603 if (!window->parent)
604 wl_surface_map_toplevel(window->surface);
605 else
606 wl_surface_map_transient(window->surface,
607 window->parent->surface,
608 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500609 window->mapped = 1;
610 }
611
612 wl_surface_damage(window->surface, 0, 0,
613 window->allocation.width,
614 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500615}
616
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500617void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400618window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500619{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400620 if (window->cairo_surface)
621 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500622}
623
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400624void
625window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500627 cairo_surface_reference(surface);
628
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400629 if (window->cairo_surface != NULL)
630 cairo_surface_destroy(window->cairo_surface);
631
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500632 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400633}
634
635void
636window_create_surface(struct window *window)
637{
638 cairo_surface_t *surface;
639
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400643 surface = display_create_surface(window->display,
644 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645 break;
646#endif
647 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400648 surface = display_create_shm_surface(window->display,
649 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800651 default:
652 surface = NULL;
653 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400655
656 window_set_surface(window, surface);
657 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658}
659
660static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500661window_draw_menu(struct window *window)
662{
663 cairo_t *cr;
664 int width, height, r = 5;
665
666 window_create_surface(window);
667
668 cr = cairo_create(window->cairo_surface);
669 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
670 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
671 cairo_paint(cr);
672
673 width = window->allocation.width;
674 height = window->allocation.height;
675 rounded_rect(cr, r, r, width - r, height - r, r);
676 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
677 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
678 cairo_fill(cr);
679 cairo_destroy(cr);
680}
681
682static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500683window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500684{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500685 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500686 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400687 cairo_surface_t *frame;
688 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500689
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400690 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400692 width = window->allocation.width;
693 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500694
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500695 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500696
Kristian Høgsberg09531622010-06-14 23:22:15 -0400697 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400698 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400699 cairo_paint(cr);
700
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400701 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 tile_mask(cr, window->display->shadow,
703 shadow_dx, shadow_dy, width, height,
704 window->margin + 10 - shadow_dx,
705 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500706
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400707 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400709 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400710 frame = window->display->inactive_frame;
711
712 tile_source(cr, frame, 0, 0, width, height,
713 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500714
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500715 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
716 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500717 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400718 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400719 if (window->keyboard_device)
720 cairo_set_source_rgb(cr, 0, 0, 0);
721 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400722 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400723 cairo_show_text(cr, window->title);
724
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500725 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400726
727 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500728}
729
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400730void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500731window_destroy(struct window *window)
732{
733 wl_surface_destroy(window->surface);
734 wl_list_remove(&window->link);
735 free(window);
736}
737
738void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400739display_flush_cairo_device(struct display *display)
740{
741 cairo_device_flush (display->device);
742}
743
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500744void
745window_draw(struct window *window)
746{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500747 if (window->parent)
748 window_draw_menu(window);
749 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400750 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500751 else
752 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500753}
754
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400755cairo_surface_t *
756window_get_surface(struct window *window)
757{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400758 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400759}
760
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400761static int
762get_pointer_location(struct window *window, int32_t x, int32_t y)
763{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400764 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400765 const int grip_size = 8;
766
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500767 if (!window->decoration)
768 return WINDOW_CLIENT_AREA;
769
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400770 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400771 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400772 else if (window->margin <= x && x < window->margin + grip_size)
773 hlocation = WINDOW_RESIZING_LEFT;
774 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400775 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400776 else if (x < window->allocation.width - window->margin)
777 hlocation = WINDOW_RESIZING_RIGHT;
778 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400779 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400780
781 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400782 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400783 else if (window->margin <= y && y < window->margin + grip_size)
784 vlocation = WINDOW_RESIZING_TOP;
785 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400786 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400787 else if (y < window->allocation.height - window->margin)
788 vlocation = WINDOW_RESIZING_BOTTOM;
789 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400790 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400791
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400792 location = vlocation | hlocation;
793 if (location & WINDOW_EXTERIOR)
794 location = WINDOW_EXTERIOR;
795 if (location == WINDOW_INTERIOR && y < window->margin + 50)
796 location = WINDOW_TITLEBAR;
797 else if (location == WINDOW_INTERIOR)
798 location = WINDOW_CLIENT_AREA;
799
800 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400801}
802
803static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400804set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400805{
806 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400807 struct wl_buffer *buffer;
808 cairo_surface_t *surface;
809 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400810
811 location = get_pointer_location(input->pointer_focus,
812 input->sx, input->sy);
813 switch (location) {
814 case WINDOW_RESIZING_TOP:
815 pointer = POINTER_TOP;
816 break;
817 case WINDOW_RESIZING_BOTTOM:
818 pointer = POINTER_BOTTOM;
819 break;
820 case WINDOW_RESIZING_LEFT:
821 pointer = POINTER_LEFT;
822 break;
823 case WINDOW_RESIZING_RIGHT:
824 pointer = POINTER_RIGHT;
825 break;
826 case WINDOW_RESIZING_TOP_LEFT:
827 pointer = POINTER_TOP_LEFT;
828 break;
829 case WINDOW_RESIZING_TOP_RIGHT:
830 pointer = POINTER_TOP_RIGHT;
831 break;
832 case WINDOW_RESIZING_BOTTOM_LEFT:
833 pointer = POINTER_BOTTOM_LEFT;
834 break;
835 case WINDOW_RESIZING_BOTTOM_RIGHT:
836 pointer = POINTER_BOTTOM_RIGHT;
837 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400838 case WINDOW_EXTERIOR:
839 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400840 if (input->current_pointer_image == POINTER_DEFAULT)
841 return;
842
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400843 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400844 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400845 return;
846 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400847 break;
848 }
849
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400850 if (pointer == input->current_pointer_image)
851 return;
852
853 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400854 surface = display->pointer_surfaces[pointer];
855 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400856 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400857 pointer_images[pointer].hotspot_x,
858 pointer_images[pointer].hotspot_y);
859}
860
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500861static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500862window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400863 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500864 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500865{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400866 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400867 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500868 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400869
870 input->x = x;
871 input->y = y;
872 input->sx = sx;
873 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400874
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400875 if (window->motion_handler)
876 pointer = (*window->motion_handler)(window, input, time,
877 x, y, sx, sy,
878 window->user_data);
879
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400880 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500881}
882
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400883static void
884window_handle_button(void *data,
885 struct wl_input_device *input_device,
886 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500887{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400888 struct input *input = data;
889 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400890 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400891
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400892 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500893
894 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400895 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400896 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400897 wl_shell_move(window->display->shell,
898 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500899 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400900 case WINDOW_RESIZING_TOP:
901 case WINDOW_RESIZING_BOTTOM:
902 case WINDOW_RESIZING_LEFT:
903 case WINDOW_RESIZING_RIGHT:
904 case WINDOW_RESIZING_TOP_LEFT:
905 case WINDOW_RESIZING_TOP_RIGHT:
906 case WINDOW_RESIZING_BOTTOM_LEFT:
907 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400908 wl_shell_resize(window->display->shell,
909 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400910 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500911 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400912 case WINDOW_CLIENT_AREA:
913 if (window->button_handler)
914 (*window->button_handler)(window,
915 input, time,
916 button, state,
917 window->user_data);
918 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500919 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400920 } else {
921 if (window->button_handler)
922 (*window->button_handler)(window,
923 input, time,
924 button, state,
925 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500926 }
927}
928
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500929static void
930window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400931 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500932{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400933 struct input *input = data;
934 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400935 struct display *d = window->display;
936 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500937
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400938 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400939 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500940 return;
941
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400942 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500943 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400944 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
945 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500946
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400947 sym = XkbKeySymEntry(d->xkb, code, level, 0);
948
949 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400950 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400951 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400952 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500953
954 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500955 (*window->key_handler)(window, input, time, key, sym, state,
956 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500957}
958
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500959static void
960window_handle_pointer_focus(void *data,
961 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400962 uint32_t time, struct wl_surface *surface,
963 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500964{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400965 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400966 struct window *window;
967 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400968
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400969 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400970 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400971 window = input->pointer_focus;
972
Kristian Høgsberg59826582011-01-20 11:56:57 -0500973 input->x = x;
974 input->y = y;
975 input->sx = sx;
976 input->sy = sy;
977
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400978 pointer = POINTER_LEFT_PTR;
979 if (window->motion_handler)
980 pointer = (*window->motion_handler)(window,
981 input, time,
982 x, y, sx, sy,
983 window->user_data);
984
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400985 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400986 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400987 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400988 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400989 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500990}
991
992static void
993window_handle_keyboard_focus(void *data,
994 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400995 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500996 struct wl_surface *surface,
997 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500998{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999 struct input *input = data;
1000 struct window *window = input->keyboard_focus;
1001 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001002 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001003
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001004 window = input->keyboard_focus;
1005 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001006 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001007 if (window->keyboard_focus_handler)
1008 (*window->keyboard_focus_handler)(window, NULL,
1009 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001010 }
1011
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001012 if (surface)
1013 input->keyboard_focus = wl_surface_get_user_data(surface);
1014 else
1015 input->keyboard_focus = NULL;
1016
1017 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001018 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001019 for (k = keys->data; k < end; k++)
1020 input->modifiers |= d->xkb->map->modmap[*k];
1021
1022 window = input->keyboard_focus;
1023 if (window) {
1024 window->keyboard_device = input;
1025 if (window->keyboard_focus_handler)
1026 (*window->keyboard_focus_handler)(window,
1027 window->keyboard_device,
1028 window->user_data);
1029 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001030}
1031
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001032static const struct wl_input_device_listener input_device_listener = {
1033 window_handle_motion,
1034 window_handle_button,
1035 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001036 window_handle_pointer_focus,
1037 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001038};
1039
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001040void
1041input_get_position(struct input *input, int32_t *x, int32_t *y)
1042{
1043 *x = input->sx;
1044 *y = input->sy;
1045}
1046
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001047struct wl_input_device *
1048input_get_input_device(struct input *input)
1049{
1050 return input->input_device;
1051}
1052
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001053uint32_t
1054input_get_modifiers(struct input *input)
1055{
1056 return input->modifiers;
1057}
1058
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001059struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001060window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001061{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001062 cairo_device_flush (window->display->device);
1063
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001064 return wl_shell_create_drag(window->display->shell);
1065}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001066
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001067void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001068window_move(struct window *window, struct input *input, uint32_t time)
1069{
1070 wl_shell_move(window->display->shell,
1071 window->surface, input->input_device, time);
1072}
1073
1074void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001075window_activate_drag(struct wl_drag *drag, struct window *window,
1076 struct input *input, uint32_t time)
1077{
1078 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001079}
1080
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001081static void
1082handle_configure(void *data, struct wl_shell *shell,
1083 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001084 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001085{
1086 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001087 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001088
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001089 /* FIXME this is probably the wrong place to check for width or
1090 height <= 0, but it prevents the compositor from crashing
1091 */
1092 if(width <= 0 || height <= 0)
1093 return;
1094
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001095 window->resize_edges = edges;
1096
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001097 if (window->resize_handler) {
1098 child_width = width - 20 - window->margin * 2;
1099 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001100
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001101 (*window->resize_handler)(window,
1102 child_width, child_height,
1103 window->user_data);
1104 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001105 window->allocation.width = width;
1106 window->allocation.height = height;
1107
1108 if (window->redraw_handler)
1109 window_schedule_redraw(window);
1110 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001111}
1112
1113static const struct wl_shell_listener shell_listener = {
1114 handle_configure,
1115};
1116
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001117void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001118window_get_child_allocation(struct window *window,
1119 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001120{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001121 if (window->fullscreen && !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001122 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001123 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001124 allocation->x = window->margin + 10;
1125 allocation->y = window->margin + 50;
1126 allocation->width =
1127 window->allocation.width - 20 - window->margin * 2;
1128 allocation->height =
1129 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001130 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001131}
1132
1133void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001134window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001135{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001136 if (!window->fullscreen) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001137 window->allocation.width = width + 20 + window->margin * 2;
1138 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001139 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001140}
1141
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001142static gboolean
1143idle_redraw(void *data)
1144{
1145 struct window *window = data;
1146
1147 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001148
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001149 window->redraw_scheduled = 0;
1150
1151 return FALSE;
1152}
1153
1154void
1155window_schedule_redraw(struct window *window)
1156{
1157 if (!window->redraw_scheduled) {
1158 g_idle_add(idle_redraw, window);
1159 window->redraw_scheduled = 1;
1160 }
1161}
1162
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001163void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001164window_set_fullscreen(struct window *window, int fullscreen)
1165{
1166 window->fullscreen = fullscreen;
1167 if (window->fullscreen) {
1168 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001169 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001170 } else {
1171 window->allocation = window->saved_allocation;
1172 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001173}
1174
1175void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001176window_set_decoration(struct window *window, int decoration)
1177{
1178 window->decoration = decoration;
1179}
1180
1181void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001182window_set_user_data(struct window *window, void *data)
1183{
1184 window->user_data = data;
1185}
1186
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001187void *
1188window_get_user_data(struct window *window)
1189{
1190 return window->user_data;
1191}
1192
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001193void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001194window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001195 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001196{
1197 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001198}
1199
1200void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001201window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001202 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001203{
1204 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001205}
1206
1207void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001208window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001209 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001210{
1211 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001212}
1213
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001214void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001215window_set_button_handler(struct window *window,
1216 window_button_handler_t handler)
1217{
1218 window->button_handler = handler;
1219}
1220
1221void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001222window_set_motion_handler(struct window *window,
1223 window_motion_handler_t handler)
1224{
1225 window->motion_handler = handler;
1226}
1227
1228void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001229window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001230 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001231{
1232 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001233}
1234
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001235void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001236window_set_title(struct window *window, const char *title)
1237{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001238 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001239 window->title = strdup(title);
1240}
1241
1242const char *
1243window_get_title(struct window *window)
1244{
1245 return window->title;
1246}
1247
1248void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001249window_damage(struct window *window, int32_t x, int32_t y,
1250 int32_t width, int32_t height)
1251{
1252 wl_surface_damage(window->surface, x, y, width, height);
1253}
1254
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001255static struct window *
1256window_create_internal(struct display *display, struct window *parent,
1257 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001258{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001259 struct window *window;
1260
1261 window = malloc(sizeof *window);
1262 if (window == NULL)
1263 return NULL;
1264
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001265 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001266 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001267 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001268 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001269 window->allocation.x = 0;
1270 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001271 window->allocation.width = width;
1272 window->allocation.height = height;
1273 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001274 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001275 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001276
Yuval Fledel45568f62010-12-06 09:18:12 -05001277 if (display->drm)
1278 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1279 else
1280 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001281
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001282 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001283 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001284
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001285 return window;
1286}
1287
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001288struct window *
1289window_create(struct display *display, int32_t width, int32_t height)
1290{
1291 struct window *window;
1292
1293 window = window_create_internal(display, NULL, width, height);
1294 if (!window)
1295 return NULL;
1296
1297 return window;
1298}
1299
1300struct window *
1301window_create_transient(struct display *display, struct window *parent,
1302 int32_t x, int32_t y, int32_t width, int32_t height)
1303{
1304 struct window *window;
1305
1306 window = window_create_internal(parent->display,
1307 parent, width, height);
1308 if (!window)
1309 return NULL;
1310
1311 window->x = x;
1312 window->y = y;
1313
1314 return window;
1315}
1316
1317void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001318window_set_buffer_type(struct window *window, enum window_buffer_type type)
1319{
1320 window->buffer_type = type;
1321}
1322
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001323static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001324drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001325{
1326 struct display *d = data;
1327
1328 d->device_name = strdup(device);
1329}
1330
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001331static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1332{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001333 struct display *d = data;
1334
1335 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001336}
1337
1338static const struct wl_drm_listener drm_listener = {
1339 drm_handle_device,
1340 drm_handle_authenticated
1341};
1342
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001343static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001344display_handle_geometry(void *data,
1345 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001346 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001347{
1348 struct display *display = data;
1349
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001350 display->screen_allocation.x = x;
1351 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001352 display->screen_allocation.width = width;
1353 display->screen_allocation.height = height;
1354}
1355
1356static const struct wl_output_listener output_listener = {
1357 display_handle_geometry,
1358};
1359
1360static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001361display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001362{
1363 struct input *input;
1364
1365 input = malloc(sizeof *input);
1366 if (input == NULL)
1367 return;
1368
1369 memset(input, 0, sizeof *input);
1370 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001371 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001372 input->pointer_focus = NULL;
1373 input->keyboard_focus = NULL;
1374 wl_list_insert(d->input_list.prev, &input->link);
1375
1376 wl_input_device_add_listener(input->input_device,
1377 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001378 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001379}
1380
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001381struct selection_offer {
1382 struct display *display;
1383 struct wl_selection_offer *offer;
1384 struct wl_array types;
1385 struct input *input;
1386};
1387
1388int
1389input_offers_mime_type(struct input *input, const char *type)
1390{
1391 struct selection_offer *offer = input->offer;
1392 char **p, **end;
1393
1394 if (offer == NULL)
1395 return 0;
1396
1397 end = offer->types.data + offer->types.size;
1398 for (p = offer->types.data; p < end; p++)
1399 if (strcmp(*p, type) == 0)
1400 return 1;
1401
1402 return 0;
1403}
1404
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001405void
1406input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001407{
1408 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001409
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001410 /* FIXME: A number of things can go wrong here: the object may
1411 * not be the current selection offer any more (which could
1412 * still work, but the source may have gone away or just
1413 * destroyed its wl_selection) or the offer may not have the
1414 * requested type after all (programmer/client error,
1415 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001416 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001417}
1418
1419static void
1420selection_offer_offer(void *data,
1421 struct wl_selection_offer *selection_offer,
1422 const char *type)
1423{
1424 struct selection_offer *offer = data;
1425
1426 char **p;
1427
1428 p = wl_array_add(&offer->types, sizeof *p);
1429 if (p)
1430 *p = strdup(type);
1431};
1432
1433static void
1434selection_offer_keyboard_focus(void *data,
1435 struct wl_selection_offer *selection_offer,
1436 struct wl_input_device *input_device)
1437{
1438 struct selection_offer *offer = data;
1439 struct input *input;
1440 char **p, **end;
1441
1442 if (input_device == NULL) {
1443 printf("selection offer retracted %p\n", selection_offer);
1444 input = offer->input;
1445 input->offer = NULL;
1446 wl_selection_offer_destroy(selection_offer);
1447 wl_array_release(&offer->types);
1448 free(offer);
1449 return;
1450 }
1451
1452 input = wl_input_device_get_user_data(input_device);
1453 printf("new selection offer %p:", selection_offer);
1454
1455 offer->input = input;
1456 input->offer = offer;
1457 end = offer->types.data + offer->types.size;
1458 for (p = offer->types.data; p < end; p++)
1459 printf(" %s", *p);
1460
1461 printf("\n");
1462}
1463
1464struct wl_selection_offer_listener selection_offer_listener = {
1465 selection_offer_offer,
1466 selection_offer_keyboard_focus
1467};
1468
1469static void
1470add_selection_offer(struct display *d, uint32_t id)
1471{
1472 struct selection_offer *offer;
1473
1474 offer = malloc(sizeof *offer);
1475 if (offer == NULL)
1476 return;
1477
1478 offer->offer = wl_selection_offer_create(d->display, id);
1479 offer->display = d;
1480 wl_array_init(&offer->types);
1481 offer->input = NULL;
1482
1483 wl_selection_offer_add_listener(offer->offer,
1484 &selection_offer_listener, offer);
1485}
1486
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001487static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001488display_handle_global(struct wl_display *display, uint32_t id,
1489 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001490{
1491 struct display *d = data;
1492
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001493 if (strcmp(interface, "compositor") == 0) {
1494 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001495 } else if (strcmp(interface, "output") == 0) {
1496 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001497 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001498 } else if (strcmp(interface, "input_device") == 0) {
1499 display_add_input(d, id);
1500 } else if (strcmp(interface, "shell") == 0) {
1501 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001502 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001503 } else if (strcmp(interface, "drm") == 0) {
1504 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001505 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001506 } else if (strcmp(interface, "shm") == 0) {
1507 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001508 } else if (strcmp(interface, "selection_offer") == 0) {
1509 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001510 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001511 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001512 }
1513}
1514
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001515static void
1516display_render_frame(struct display *d)
1517{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001518 int radius = 8;
1519 cairo_t *cr;
1520
1521 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1522 cr = cairo_create(d->shadow);
1523 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1524 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1525 rounded_rect(cr, 16, 16, 112, 112, radius);
1526 cairo_fill(cr);
1527 cairo_destroy(cr);
1528 blur_surface(d->shadow, 64);
1529
1530 d->active_frame =
1531 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1532 cr = cairo_create(d->active_frame);
1533 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1534 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1535 rounded_rect(cr, 16, 16, 112, 112, radius);
1536 cairo_fill(cr);
1537 cairo_destroy(cr);
1538
1539 d->inactive_frame =
1540 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1541 cr = cairo_create(d->inactive_frame);
1542 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1543 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1544 rounded_rect(cr, 16, 16, 112, 112, radius);
1545 cairo_fill(cr);
1546 cairo_destroy(cr);
1547}
1548
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001549static void
1550init_xkb(struct display *d)
1551{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001552 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001553
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001554 names.rules = "evdev";
1555 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001556 names.layout = option_xkb_layout;
1557 names.variant = option_xkb_variant;
1558 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001559
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001560 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001561 if (!d->xkb) {
1562 fprintf(stderr, "Failed to compile keymap\n");
1563 exit(1);
1564 }
1565}
1566
Yuval Fledel45568f62010-12-06 09:18:12 -05001567static int
1568init_drm(struct display *d)
1569{
1570 EGLint major, minor;
1571 int fd;
1572 drm_magic_t magic;
1573
1574 fd = open(d->device_name, O_RDWR);
1575 if (fd < 0) {
1576 fprintf(stderr, "drm open failed: %m\n");
1577 return -1;
1578 }
1579
1580 if (drmGetMagic(fd, &magic)) {
1581 fprintf(stderr, "DRI2: failed to get drm magic");
1582 return -1;
1583 }
1584
1585 /* Wait for authenticated event */
1586 wl_drm_authenticate(d->drm, magic);
1587 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1588 while (!d->authenticated)
1589 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1590
1591 d->dpy = eglGetDRMDisplayMESA(fd);
1592 if (!eglInitialize(d->dpy, &major, &minor)) {
1593 fprintf(stderr, "failed to initialize display\n");
1594 return -1;
1595 }
1596
1597 if (!eglBindAPI(EGL_OPENGL_API)) {
1598 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1599 return -1;
1600 }
1601
1602 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1603 if (d->ctx == NULL) {
1604 fprintf(stderr, "failed to create context\n");
1605 return -1;
1606 }
1607
1608 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1609 fprintf(stderr, "faile to make context current\n");
1610 return -1;
1611 }
1612
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001613#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001614 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1615 if (d->device == NULL) {
1616 fprintf(stderr, "failed to get cairo drm device\n");
1617 return -1;
1618 }
1619#endif
1620
1621 return 0;
1622}
1623
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001624struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001625display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001626{
1627 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001628 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001629 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001630 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001631
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001632 g_type_init();
1633
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001634 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001635 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001636 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001637
1638 xkb_option_group = g_option_group_new("xkb",
1639 "Keyboard options",
1640 "Show all XKB options",
1641 NULL, NULL);
1642 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1643 g_option_context_add_group (context, xkb_option_group);
1644
1645 if (!g_option_context_parse(context, argc, argv, &error)) {
1646 fprintf(stderr, "option parsing failed: %s\n", error->message);
1647 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001648 }
1649
Tim Wiederhake748f6722011-01-23 23:25:25 +01001650 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001651
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001652 d = malloc(sizeof *d);
1653 if (d == NULL)
1654 return NULL;
1655
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001656 memset(d, 0, sizeof *d);
1657
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001658 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001659 if (d->display == NULL) {
1660 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001661 return NULL;
1662 }
1663
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001664 wl_list_init(&d->input_list);
1665
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001666 /* Set up listener so we'll catch all events. */
1667 wl_display_add_global_listener(d->display,
1668 display_handle_global, d);
1669
1670 /* Process connection events. */
1671 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1672
Yuval Fledel45568f62010-12-06 09:18:12 -05001673 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001674 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001675
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001676 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001677
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001678 display_render_frame(d);
1679
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001680 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001681 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001682 g_source_attach(d->source, NULL);
1683
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001684 wl_list_init(&d->window_list);
1685
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001686 init_xkb(d);
1687
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001688 return d;
1689}
1690
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001691struct wl_display *
1692display_get_display(struct display *display)
1693{
1694 return display->display;
1695}
1696
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001697struct wl_compositor *
1698display_get_compositor(struct display *display)
1699{
1700 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001701}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001702
1703EGLDisplay
1704display_get_egl_display(struct display *d)
1705{
1706 return d->dpy;
1707}
1708
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001709struct wl_shell *
1710display_get_shell(struct display *display)
1711{
1712 return display->shell;
1713}
1714
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001715void
1716display_run(struct display *d)
1717{
1718 g_main_loop_run(d->loop);
1719}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001720
1721void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001722display_set_global_handler(struct display *display,
1723 display_global_handler_t handler)
1724{
1725 display->global_handler = handler;
1726}