blob: c023e916603e660bd7b4ebee98eb4bc2939a4f6e [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ø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øgsberg58eec362011-01-19 14:27:42 -0500122 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400123 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400124 uint32_t modifiers;
125 int32_t x, y, sx, sy;
126 struct wl_list link;
127};
128
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400129enum {
130 POINTER_DEFAULT = 100,
131 POINTER_UNSET
132};
133
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500134enum window_location {
135 WINDOW_INTERIOR = 0,
136 WINDOW_RESIZING_TOP = 1,
137 WINDOW_RESIZING_BOTTOM = 2,
138 WINDOW_RESIZING_LEFT = 4,
139 WINDOW_RESIZING_TOP_LEFT = 5,
140 WINDOW_RESIZING_BOTTOM_LEFT = 6,
141 WINDOW_RESIZING_RIGHT = 8,
142 WINDOW_RESIZING_TOP_RIGHT = 9,
143 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
144 WINDOW_RESIZING_MASK = 15,
145 WINDOW_EXTERIOR = 16,
146 WINDOW_TITLEBAR = 17,
147 WINDOW_CLIENT_AREA = 18,
148};
149
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400150const char *option_xkb_layout = "us";
151const char *option_xkb_variant = "";
152const char *option_xkb_options = "";
153
154static const GOptionEntry xkb_option_entries[] = {
155 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
156 &option_xkb_layout, "XKB Layout" },
157 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
158 &option_xkb_variant, "XKB Variant" },
159 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
160 &option_xkb_options, "XKB Options" },
161 { NULL }
162};
163
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500164static void
165rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
166{
167 cairo_move_to(cr, x0, y0 + radius);
168 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
169 cairo_line_to(cr, x1 - radius, y0);
170 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
171 cairo_line_to(cr, x1, y1 - radius);
172 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
173 cairo_line_to(cr, x0 + radius, y1);
174 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
175 cairo_close_path(cr);
176}
177
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400178static const cairo_user_data_key_t surface_data_key;
179struct surface_data {
180 struct wl_buffer *buffer;
181};
182
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500183#define MULT(_d,c,a,t) \
184 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
185
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500186#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400187
188struct drm_surface_data {
189 struct surface_data data;
190 EGLImageKHR image;
191 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800192 struct display *display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400193};
194
195static void
196drm_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400197{
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400198 struct drm_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800199 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400200
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800201 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400202 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800203 cairo_device_release(d->device);
204
205 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400206 wl_buffer_destroy(data->data.buffer);
207}
208
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500209EGLImageKHR
210display_get_image_for_drm_surface(struct display *display,
211 cairo_surface_t *surface)
212{
213 struct drm_surface_data *data;
214
215 data = cairo_surface_get_user_data (surface, &surface_data_key);
216
217 return data->image;
218}
219
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500220static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400221display_create_drm_surface(struct display *display,
222 struct rectangle *rectangle)
223{
224 struct drm_surface_data *data;
225 EGLDisplay dpy = display->dpy;
226 cairo_surface_t *surface;
227 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400228 EGLint name, stride;
229
230 EGLint image_attribs[] = {
231 EGL_WIDTH, 0,
232 EGL_HEIGHT, 0,
233 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
234 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
235 EGL_NONE
236 };
237
238 data = malloc(sizeof *data);
239 if (data == NULL)
240 return NULL;
241
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800242 data->display = display;
243
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244 image_attribs[1] = rectangle->width;
245 image_attribs[3] = rectangle->height;
246 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800247
248 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400249 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400250 glBindTexture(GL_TEXTURE_2D, data->texture);
251 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800252 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400253
254 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
255
256 visual = wl_display_get_premultiplied_argb_visual(display->display);
257 data->data.buffer =
258 wl_drm_create_buffer(display->drm, name, rectangle->width,
259 rectangle->height, stride, visual);
260
261 surface = cairo_gl_surface_create_for_texture(display->device,
262 CAIRO_CONTENT_COLOR_ALPHA,
263 data->texture,
264 rectangle->width,
265 rectangle->height);
266
267 cairo_surface_set_user_data (surface, &surface_data_key,
268 data, drm_surface_data_destroy);
269
270 return surface;
271}
272
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500273static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400274display_create_drm_surface_from_file(struct display *display,
275 const char *filename,
276 struct rectangle *rect)
277{
278 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400279 GdkPixbuf *pixbuf;
280 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400281 int stride, i;
282 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800283 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400284
285 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400286 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400287 FALSE, &error);
288 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400289 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400290
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400291 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
292 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500293 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400294 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400295 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400296
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400297
298 stride = gdk_pixbuf_get_rowstride(pixbuf);
299 pixels = gdk_pixbuf_get_pixels(pixbuf);
300
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400302 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400303 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400304 while (p < end) {
305 unsigned int t;
306
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400307 MULT(p[0], p[0], p[3], t);
308 MULT(p[1], p[1], p[3], t);
309 MULT(p[2], p[2], p[3], t);
310 p += 4;
311
312 }
313 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400314
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315 surface = display_create_drm_surface(display, rect);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800316 data = cairo_surface_get_user_data(surface, &surface_data_key);
317
318 cairo_device_acquire(display->device);
319 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800320 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
321 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500324 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400325
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326 return surface;
327}
328
329#endif
330
331struct wl_buffer *
332display_get_buffer_for_surface(struct display *display,
333 cairo_surface_t *surface)
334{
335 struct surface_data *data;
336
337 data = cairo_surface_get_user_data (surface, &surface_data_key);
338
339 return data->buffer;
340}
341
342struct shm_surface_data {
343 struct surface_data data;
344 void *map;
345 size_t length;
346};
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349shm_surface_data_destroy(void *p)
350{
351 struct shm_surface_data *data = p;
352
353 wl_buffer_destroy(data->data.buffer);
354 munmap(data->map, data->length);
355}
356
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500357static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358display_create_shm_surface(struct display *display,
359 struct rectangle *rectangle)
360{
361 struct shm_surface_data *data;
362 cairo_surface_t *surface;
363 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800364 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400365 char filename[] = "/tmp/wayland-shm-XXXXXX";
366
367 data = malloc(sizeof *data);
368 if (data == NULL)
369 return NULL;
370
371 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
372 rectangle->width);
373 data->length = stride * rectangle->height;
374 fd = mkstemp(filename);
375 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000376 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400377 return NULL;
378 }
379 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000380 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400381 close(fd);
382 return NULL;
383 }
384
385 data->map = mmap(NULL, data->length,
386 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
387 unlink(filename);
388
389 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000390 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 close(fd);
392 return NULL;
393 }
394
395 surface = cairo_image_surface_create_for_data (data->map,
396 CAIRO_FORMAT_ARGB32,
397 rectangle->width,
398 rectangle->height,
399 stride);
400
401 cairo_surface_set_user_data (surface, &surface_data_key,
402 data, shm_surface_data_destroy);
403
404 visual = wl_display_get_premultiplied_argb_visual(display->display);
405 data->data.buffer = wl_shm_create_buffer(display->shm,
406 fd,
407 rectangle->width,
408 rectangle->height,
409 stride, visual);
410
411 close(fd);
412
413 return surface;
414}
415
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500416static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417display_create_shm_surface_from_file(struct display *display,
418 const char *filename,
419 struct rectangle *rect)
420{
421 cairo_surface_t *surface;
422 GdkPixbuf *pixbuf;
423 GError *error = NULL;
424 int stride, i;
425 unsigned char *pixels, *p, *end, *dest_data;
426 int dest_stride;
427 uint32_t *d;
428
429 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
430 rect->width, rect->height,
431 FALSE, &error);
432 if (error != NULL)
433 return NULL;
434
435 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
436 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500437 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 return NULL;
439 }
440
441 stride = gdk_pixbuf_get_rowstride(pixbuf);
442 pixels = gdk_pixbuf_get_pixels(pixbuf);
443
444 surface = display_create_shm_surface(display, rect);
445 dest_data = cairo_image_surface_get_data (surface);
446 dest_stride = cairo_image_surface_get_stride (surface);
447
448 for (i = 0; i < rect->height; i++) {
449 d = (uint32_t *) (dest_data + i * dest_stride);
450 p = pixels + i * stride;
451 end = p + rect->width * 4;
452 while (p < end) {
453 unsigned int t;
454 unsigned char a, r, g, b;
455
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400456 a = p[3];
457 MULT(r, p[0], a, t);
458 MULT(g, p[1], a, t);
459 MULT(b, p[2], a, t);
460 p += 4;
461 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
462 }
463 }
464
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500465 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466
467 return surface;
468}
469
470cairo_surface_t *
471display_create_surface(struct display *display,
472 struct rectangle *rectangle)
473{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500474#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500475 if (display->drm) {
476 return display_create_drm_surface(display, rectangle);
477 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400478#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500479 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400480}
481
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500482static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400483display_create_surface_from_file(struct display *display,
484 const char *filename,
485 struct rectangle *rectangle)
486{
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500487#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500488 if (display->drm) {
489 return display_create_drm_surface_from_file(display, filename, rectangle);
490 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500492 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400493}
Yuval Fledel45568f62010-12-06 09:18:12 -0500494 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400495 const char *filename;
496 int hotspot_x, hotspot_y;
497} pointer_images[] = {
498 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
499 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
500 { DATADIR "/wayland/bottom_side.png", 16, 20 },
501 { DATADIR "/wayland/grabbing.png", 20, 17 },
502 { DATADIR "/wayland/left_ptr.png", 10, 5 },
503 { DATADIR "/wayland/left_side.png", 10, 20 },
504 { DATADIR "/wayland/right_side.png", 30, 19 },
505 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
506 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
507 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400508 { DATADIR "/wayland/xterm.png", 15, 15 },
509 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400510};
511
512static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400513create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400514{
515 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400516 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400517 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400518
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400519 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400520 display->pointer_surfaces =
521 malloc(count * sizeof *display->pointer_surfaces);
522 rect.width = width;
523 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400524 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400525 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526 display_create_surface_from_file(display,
527 pointer_images[i].filename,
528 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400529 }
530
531}
532
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400533cairo_surface_t *
534display_get_pointer_surface(struct display *display, int pointer,
535 int *width, int *height,
536 int *hotspot_x, int *hotspot_y)
537{
538 cairo_surface_t *surface;
539
540 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500541#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400542 *width = cairo_gl_surface_get_width(surface);
543 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000544#else
545 *width = cairo_image_surface_get_width(surface);
546 *height = cairo_image_surface_get_height(surface);
547#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400548 *hotspot_x = pointer_images[pointer].hotspot_x;
549 *hotspot_y = pointer_images[pointer].hotspot_y;
550
551 return cairo_surface_reference(surface);
552}
553
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400554
555static void
556window_attach_surface(struct window *window);
557
558static void
559free_surface(void *data)
560{
561 struct window *window = data;
562
563 cairo_surface_destroy(window->pending_surface);
564 window->pending_surface = NULL;
565 if (window->cairo_surface)
566 window_attach_surface(window);
567}
568
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500569static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500570window_attach_surface(struct window *window)
571{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400572 struct display *display = window->display;
573 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500574 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500575
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500576 if (window->pending_surface != NULL)
577 return;
578
Kristian Høgsberg09531622010-06-14 23:22:15 -0400579 window->pending_surface = window->cairo_surface;
580 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500581
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400582 buffer = display_get_buffer_for_surface(display,
583 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500584 if (window->resize_edges & WINDOW_RESIZING_LEFT)
585 x = window->server_allocation.width -
586 window->allocation.width;
587 else
588 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500589
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500590 if (window->resize_edges & WINDOW_RESIZING_TOP)
591 y = window->server_allocation.height -
592 window->allocation.height;
593 else
594 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400595
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500596 window->server_allocation = window->allocation;
597 window->resize_edges = 0;
598 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400599 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500600
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500601 if (window->fullscreen)
602 wl_surface_map_fullscreen(window->surface);
603 else 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
610 wl_surface_damage(window->surface, 0, 0,
611 window->allocation.width,
612 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500613}
614
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500615void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400616window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500617{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400618 if (window->cairo_surface)
619 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500620}
621
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400622void
623window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500625 cairo_surface_reference(surface);
626
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400627 if (window->cairo_surface != NULL)
628 cairo_surface_destroy(window->cairo_surface);
629
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500630 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400631}
632
633void
634window_create_surface(struct window *window)
635{
636 cairo_surface_t *surface;
637
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500639#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400641 surface = display_create_surface(window->display,
642 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400643 break;
644#endif
645 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400646 surface = display_create_shm_surface(window->display,
647 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800649 default:
650 surface = NULL;
651 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400653
654 window_set_surface(window, surface);
655 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656}
657
658static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500659window_draw_menu(struct window *window)
660{
661 cairo_t *cr;
662 int width, height, r = 5;
663
664 window_create_surface(window);
665
666 cr = cairo_create(window->cairo_surface);
667 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
668 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
669 cairo_paint(cr);
670
671 width = window->allocation.width;
672 height = window->allocation.height;
673 rounded_rect(cr, r, r, width - r, height - r, r);
674 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
675 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
676 cairo_fill(cr);
677 cairo_destroy(cr);
678}
679
680static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500681window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500682{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500683 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500684 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400685 cairo_surface_t *frame;
686 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500687
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400688 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400690 width = window->allocation.width;
691 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500692
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500693 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500694
Kristian Høgsberg09531622010-06-14 23:22:15 -0400695 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400696 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400697 cairo_paint(cr);
698
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400699 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 tile_mask(cr, window->display->shadow,
701 shadow_dx, shadow_dy, width, height,
702 window->margin + 10 - shadow_dx,
703 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500704
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400705 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400706 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400707 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 frame = window->display->inactive_frame;
709
710 tile_source(cr, frame, 0, 0, width, height,
711 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500712
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500713 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
714 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500715 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400716 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400717 if (window->keyboard_device)
718 cairo_set_source_rgb(cr, 0, 0, 0);
719 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400720 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400721 cairo_show_text(cr, window->title);
722
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500723 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400724
725 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500726}
727
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400728void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500729window_destroy(struct window *window)
730{
731 wl_surface_destroy(window->surface);
732 wl_list_remove(&window->link);
733 free(window);
734}
735
736void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400737display_flush_cairo_device(struct display *display)
738{
739 cairo_device_flush (display->device);
740}
741
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500742void
743window_draw(struct window *window)
744{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500745 if (window->parent)
746 window_draw_menu(window);
747 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400748 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500749 else
750 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500751}
752
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400753cairo_surface_t *
754window_get_surface(struct window *window)
755{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400756 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400757}
758
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400759static int
760get_pointer_location(struct window *window, int32_t x, int32_t y)
761{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400762 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400763 const int grip_size = 8;
764
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500765 if (!window->decoration)
766 return WINDOW_CLIENT_AREA;
767
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400768 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400769 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400770 else if (window->margin <= x && x < window->margin + grip_size)
771 hlocation = WINDOW_RESIZING_LEFT;
772 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400773 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400774 else if (x < window->allocation.width - window->margin)
775 hlocation = WINDOW_RESIZING_RIGHT;
776 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400777 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400778
779 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400780 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400781 else if (window->margin <= y && y < window->margin + grip_size)
782 vlocation = WINDOW_RESIZING_TOP;
783 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400784 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400785 else if (y < window->allocation.height - window->margin)
786 vlocation = WINDOW_RESIZING_BOTTOM;
787 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400788 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400789
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400790 location = vlocation | hlocation;
791 if (location & WINDOW_EXTERIOR)
792 location = WINDOW_EXTERIOR;
793 if (location == WINDOW_INTERIOR && y < window->margin + 50)
794 location = WINDOW_TITLEBAR;
795 else if (location == WINDOW_INTERIOR)
796 location = WINDOW_CLIENT_AREA;
797
798 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400799}
800
801static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400802set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400803{
804 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400805 struct wl_buffer *buffer;
806 cairo_surface_t *surface;
807 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400808
809 location = get_pointer_location(input->pointer_focus,
810 input->sx, input->sy);
811 switch (location) {
812 case WINDOW_RESIZING_TOP:
813 pointer = POINTER_TOP;
814 break;
815 case WINDOW_RESIZING_BOTTOM:
816 pointer = POINTER_BOTTOM;
817 break;
818 case WINDOW_RESIZING_LEFT:
819 pointer = POINTER_LEFT;
820 break;
821 case WINDOW_RESIZING_RIGHT:
822 pointer = POINTER_RIGHT;
823 break;
824 case WINDOW_RESIZING_TOP_LEFT:
825 pointer = POINTER_TOP_LEFT;
826 break;
827 case WINDOW_RESIZING_TOP_RIGHT:
828 pointer = POINTER_TOP_RIGHT;
829 break;
830 case WINDOW_RESIZING_BOTTOM_LEFT:
831 pointer = POINTER_BOTTOM_LEFT;
832 break;
833 case WINDOW_RESIZING_BOTTOM_RIGHT:
834 pointer = POINTER_BOTTOM_RIGHT;
835 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400836 case WINDOW_EXTERIOR:
837 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400838 if (input->current_pointer_image == POINTER_DEFAULT)
839 return;
840
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400841 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400842 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400843 return;
844 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400845 break;
846 }
847
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400848 if (pointer == input->current_pointer_image)
849 return;
850
851 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400852 surface = display->pointer_surfaces[pointer];
853 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400854 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400855 pointer_images[pointer].hotspot_x,
856 pointer_images[pointer].hotspot_y);
857}
858
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500859static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500860window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400861 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500862 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500863{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400864 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400865 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500866 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400867
868 input->x = x;
869 input->y = y;
870 input->sx = sx;
871 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400872
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400873 if (window->motion_handler)
874 pointer = (*window->motion_handler)(window, input, time,
875 x, y, sx, sy,
876 window->user_data);
877
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400878 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500879}
880
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400881static void
882window_handle_button(void *data,
883 struct wl_input_device *input_device,
884 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500885{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400886 struct input *input = data;
887 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400888 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400889
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400890 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500891
892 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400893 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400894 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400895 wl_shell_move(window->display->shell,
896 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500897 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400898 case WINDOW_RESIZING_TOP:
899 case WINDOW_RESIZING_BOTTOM:
900 case WINDOW_RESIZING_LEFT:
901 case WINDOW_RESIZING_RIGHT:
902 case WINDOW_RESIZING_TOP_LEFT:
903 case WINDOW_RESIZING_TOP_RIGHT:
904 case WINDOW_RESIZING_BOTTOM_LEFT:
905 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400906 wl_shell_resize(window->display->shell,
907 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400908 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500909 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400910 case WINDOW_CLIENT_AREA:
911 if (window->button_handler)
912 (*window->button_handler)(window,
913 input, time,
914 button, state,
915 window->user_data);
916 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500917 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400918 } else {
919 if (window->button_handler)
920 (*window->button_handler)(window,
921 input, time,
922 button, state,
923 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500924 }
925}
926
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500927static void
928window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400929 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500930{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400931 struct input *input = data;
932 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400933 struct display *d = window->display;
934 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500935
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400936 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400937 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500938 return;
939
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400940 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500941 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400942 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
943 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500944
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400945 sym = XkbKeySymEntry(d->xkb, code, level, 0);
946
947 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400948 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400949 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400950 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500951
952 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500953 (*window->key_handler)(window, input, time, key, sym, state,
954 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500955}
956
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500957static void
958window_handle_pointer_focus(void *data,
959 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400960 uint32_t time, struct wl_surface *surface,
961 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500962{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400963 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400964 struct window *window;
965 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400966
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400967 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400968 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400969 window = input->pointer_focus;
970
Kristian Høgsberg59826582011-01-20 11:56:57 -0500971 input->x = x;
972 input->y = y;
973 input->sx = sx;
974 input->sy = sy;
975
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400976 pointer = POINTER_LEFT_PTR;
977 if (window->motion_handler)
978 pointer = (*window->motion_handler)(window,
979 input, time,
980 x, y, sx, sy,
981 window->user_data);
982
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400983 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400984 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400985 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400986 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400987 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500988}
989
990static void
991window_handle_keyboard_focus(void *data,
992 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400993 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500994 struct wl_surface *surface,
995 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500996{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400997 struct input *input = data;
998 struct window *window = input->keyboard_focus;
999 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001000 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001001
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001002 window = input->keyboard_focus;
1003 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001004 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001005 if (window->keyboard_focus_handler)
1006 (*window->keyboard_focus_handler)(window, NULL,
1007 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001008 }
1009
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001010 if (surface)
1011 input->keyboard_focus = wl_surface_get_user_data(surface);
1012 else
1013 input->keyboard_focus = NULL;
1014
1015 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001016 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001017 for (k = keys->data; k < end; k++)
1018 input->modifiers |= d->xkb->map->modmap[*k];
1019
1020 window = input->keyboard_focus;
1021 if (window) {
1022 window->keyboard_device = input;
1023 if (window->keyboard_focus_handler)
1024 (*window->keyboard_focus_handler)(window,
1025 window->keyboard_device,
1026 window->user_data);
1027 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001028}
1029
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001030static const struct wl_input_device_listener input_device_listener = {
1031 window_handle_motion,
1032 window_handle_button,
1033 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001034 window_handle_pointer_focus,
1035 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001036};
1037
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001038void
1039input_get_position(struct input *input, int32_t *x, int32_t *y)
1040{
1041 *x = input->sx;
1042 *y = input->sy;
1043}
1044
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001045struct wl_input_device *
1046input_get_input_device(struct input *input)
1047{
1048 return input->input_device;
1049}
1050
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001051uint32_t
1052input_get_modifiers(struct input *input)
1053{
1054 return input->modifiers;
1055}
1056
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001057struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001058window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001059{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001060 cairo_device_flush (window->display->device);
1061
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001062 return wl_shell_create_drag(window->display->shell);
1063}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001064
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001065void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001066window_move(struct window *window, struct input *input, uint32_t time)
1067{
1068 wl_shell_move(window->display->shell,
1069 window->surface, input->input_device, time);
1070}
1071
1072void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001073window_activate_drag(struct wl_drag *drag, struct window *window,
1074 struct input *input, uint32_t time)
1075{
1076 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001077}
1078
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001079static void
1080handle_configure(void *data, struct wl_shell *shell,
1081 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001082 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001083{
1084 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001085 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001086
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001087 /* FIXME: this is probably the wrong place to check for width
1088 * or height <= 0, but it prevents the compositor from crashing
1089 */
1090 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001091 return;
1092
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001093 window->resize_edges = edges;
1094
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001095 if (window->resize_handler) {
1096 child_width = width - 20 - window->margin * 2;
1097 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001098
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001099 (*window->resize_handler)(window,
1100 child_width, child_height,
1101 window->user_data);
1102 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001103 window->allocation.width = width;
1104 window->allocation.height = height;
1105
1106 if (window->redraw_handler)
1107 window_schedule_redraw(window);
1108 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001109}
1110
1111static const struct wl_shell_listener shell_listener = {
1112 handle_configure,
1113};
1114
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001115void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001116window_get_child_allocation(struct window *window,
1117 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001118{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001119 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001120 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001121 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001122 allocation->x = window->margin + 10;
1123 allocation->y = window->margin + 50;
1124 allocation->width =
1125 window->allocation.width - 20 - window->margin * 2;
1126 allocation->height =
1127 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001128 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001129}
1130
1131void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001132window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001133{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001134 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001135 window->allocation.x = 20 + window->margin;
1136 window->allocation.y = 60 + window->margin;
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øgsberg0ce24572011-01-28 15:18:33 -05001139 } else {
1140 window->allocation.x = 0;
1141 window->allocation.y = 0;
1142 window->allocation.width = width;
1143 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001144 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001145}
1146
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001147static gboolean
1148idle_redraw(void *data)
1149{
1150 struct window *window = data;
1151
1152 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001153
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001154 window->redraw_scheduled = 0;
1155
1156 return FALSE;
1157}
1158
1159void
1160window_schedule_redraw(struct window *window)
1161{
1162 if (!window->redraw_scheduled) {
1163 g_idle_add(idle_redraw, window);
1164 window->redraw_scheduled = 1;
1165 }
1166}
1167
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001168void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001169window_set_fullscreen(struct window *window, int fullscreen)
1170{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001171 int32_t width, height;
1172
1173 if (window->fullscreen == fullscreen)
1174 return;
1175
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001176 window->fullscreen = fullscreen;
1177 if (window->fullscreen) {
1178 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001179 width = window->display->screen_allocation.width;
1180 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001181 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001182 width = window->saved_allocation.width - 20 - window->margin * 2;
1183 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001184 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001185
1186 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001187}
1188
1189void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001190window_set_decoration(struct window *window, int decoration)
1191{
1192 window->decoration = decoration;
1193}
1194
1195void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001196window_set_user_data(struct window *window, void *data)
1197{
1198 window->user_data = data;
1199}
1200
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001201void *
1202window_get_user_data(struct window *window)
1203{
1204 return window->user_data;
1205}
1206
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001207void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001208window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001209 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001210{
1211 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001212}
1213
1214void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001215window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001216 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001217{
1218 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001219}
1220
1221void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001222window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001223 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001224{
1225 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001226}
1227
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001228void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001229window_set_button_handler(struct window *window,
1230 window_button_handler_t handler)
1231{
1232 window->button_handler = handler;
1233}
1234
1235void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236window_set_motion_handler(struct window *window,
1237 window_motion_handler_t handler)
1238{
1239 window->motion_handler = handler;
1240}
1241
1242void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001243window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001244 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001245{
1246 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001247}
1248
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001249void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001250window_set_title(struct window *window, const char *title)
1251{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001252 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001253 window->title = strdup(title);
1254}
1255
1256const char *
1257window_get_title(struct window *window)
1258{
1259 return window->title;
1260}
1261
1262void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001263window_damage(struct window *window, int32_t x, int32_t y,
1264 int32_t width, int32_t height)
1265{
1266 wl_surface_damage(window->surface, x, y, width, height);
1267}
1268
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001269static struct window *
1270window_create_internal(struct display *display, struct window *parent,
1271 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001272{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001273 struct window *window;
1274
1275 window = malloc(sizeof *window);
1276 if (window == NULL)
1277 return NULL;
1278
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001279 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001280 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001281 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001282 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001283 window->allocation.x = 0;
1284 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001285 window->allocation.width = width;
1286 window->allocation.height = height;
1287 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001288 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001289 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001290
Yuval Fledel45568f62010-12-06 09:18:12 -05001291 if (display->drm)
1292 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1293 else
1294 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001295
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001296 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001297 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001298
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001299 return window;
1300}
1301
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001302struct window *
1303window_create(struct display *display, int32_t width, int32_t height)
1304{
1305 struct window *window;
1306
1307 window = window_create_internal(display, NULL, width, height);
1308 if (!window)
1309 return NULL;
1310
1311 return window;
1312}
1313
1314struct window *
1315window_create_transient(struct display *display, struct window *parent,
1316 int32_t x, int32_t y, int32_t width, int32_t height)
1317{
1318 struct window *window;
1319
1320 window = window_create_internal(parent->display,
1321 parent, width, height);
1322 if (!window)
1323 return NULL;
1324
1325 window->x = x;
1326 window->y = y;
1327
1328 return window;
1329}
1330
1331void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001332window_set_buffer_type(struct window *window, enum window_buffer_type type)
1333{
1334 window->buffer_type = type;
1335}
1336
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001337static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001338drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001339{
1340 struct display *d = data;
1341
1342 d->device_name = strdup(device);
1343}
1344
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001345static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1346{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001347 struct display *d = data;
1348
1349 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001350}
1351
1352static const struct wl_drm_listener drm_listener = {
1353 drm_handle_device,
1354 drm_handle_authenticated
1355};
1356
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001357static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001358display_handle_geometry(void *data,
1359 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001360 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001361{
1362 struct display *display = data;
1363
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001364 display->screen_allocation.x = x;
1365 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001366 display->screen_allocation.width = width;
1367 display->screen_allocation.height = height;
1368}
1369
1370static const struct wl_output_listener output_listener = {
1371 display_handle_geometry,
1372};
1373
1374static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001375display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001376{
1377 struct input *input;
1378
1379 input = malloc(sizeof *input);
1380 if (input == NULL)
1381 return;
1382
1383 memset(input, 0, sizeof *input);
1384 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001385 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001386 input->pointer_focus = NULL;
1387 input->keyboard_focus = NULL;
1388 wl_list_insert(d->input_list.prev, &input->link);
1389
1390 wl_input_device_add_listener(input->input_device,
1391 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001392 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001393}
1394
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001395struct selection_offer {
1396 struct display *display;
1397 struct wl_selection_offer *offer;
1398 struct wl_array types;
1399 struct input *input;
1400};
1401
1402int
1403input_offers_mime_type(struct input *input, const char *type)
1404{
1405 struct selection_offer *offer = input->offer;
1406 char **p, **end;
1407
1408 if (offer == NULL)
1409 return 0;
1410
1411 end = offer->types.data + offer->types.size;
1412 for (p = offer->types.data; p < end; p++)
1413 if (strcmp(*p, type) == 0)
1414 return 1;
1415
1416 return 0;
1417}
1418
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001419void
1420input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001421{
1422 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001423
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001424 /* FIXME: A number of things can go wrong here: the object may
1425 * not be the current selection offer any more (which could
1426 * still work, but the source may have gone away or just
1427 * destroyed its wl_selection) or the offer may not have the
1428 * requested type after all (programmer/client error,
1429 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001430 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001431}
1432
1433static void
1434selection_offer_offer(void *data,
1435 struct wl_selection_offer *selection_offer,
1436 const char *type)
1437{
1438 struct selection_offer *offer = data;
1439
1440 char **p;
1441
1442 p = wl_array_add(&offer->types, sizeof *p);
1443 if (p)
1444 *p = strdup(type);
1445};
1446
1447static void
1448selection_offer_keyboard_focus(void *data,
1449 struct wl_selection_offer *selection_offer,
1450 struct wl_input_device *input_device)
1451{
1452 struct selection_offer *offer = data;
1453 struct input *input;
1454 char **p, **end;
1455
1456 if (input_device == NULL) {
1457 printf("selection offer retracted %p\n", selection_offer);
1458 input = offer->input;
1459 input->offer = NULL;
1460 wl_selection_offer_destroy(selection_offer);
1461 wl_array_release(&offer->types);
1462 free(offer);
1463 return;
1464 }
1465
1466 input = wl_input_device_get_user_data(input_device);
1467 printf("new selection offer %p:", selection_offer);
1468
1469 offer->input = input;
1470 input->offer = offer;
1471 end = offer->types.data + offer->types.size;
1472 for (p = offer->types.data; p < end; p++)
1473 printf(" %s", *p);
1474
1475 printf("\n");
1476}
1477
1478struct wl_selection_offer_listener selection_offer_listener = {
1479 selection_offer_offer,
1480 selection_offer_keyboard_focus
1481};
1482
1483static void
1484add_selection_offer(struct display *d, uint32_t id)
1485{
1486 struct selection_offer *offer;
1487
1488 offer = malloc(sizeof *offer);
1489 if (offer == NULL)
1490 return;
1491
1492 offer->offer = wl_selection_offer_create(d->display, id);
1493 offer->display = d;
1494 wl_array_init(&offer->types);
1495 offer->input = NULL;
1496
1497 wl_selection_offer_add_listener(offer->offer,
1498 &selection_offer_listener, offer);
1499}
1500
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001501static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001502display_handle_global(struct wl_display *display, uint32_t id,
1503 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001504{
1505 struct display *d = data;
1506
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001507 if (strcmp(interface, "compositor") == 0) {
1508 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001509 } else if (strcmp(interface, "output") == 0) {
1510 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001511 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001512 } else if (strcmp(interface, "input_device") == 0) {
1513 display_add_input(d, id);
1514 } else if (strcmp(interface, "shell") == 0) {
1515 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001516 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001517 } else if (strcmp(interface, "drm") == 0) {
1518 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001519 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001520 } else if (strcmp(interface, "shm") == 0) {
1521 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001522 } else if (strcmp(interface, "selection_offer") == 0) {
1523 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001524 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001525 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001526 }
1527}
1528
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001529static void
1530display_render_frame(struct display *d)
1531{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001532 int radius = 8;
1533 cairo_t *cr;
1534
1535 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1536 cr = cairo_create(d->shadow);
1537 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1538 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1539 rounded_rect(cr, 16, 16, 112, 112, radius);
1540 cairo_fill(cr);
1541 cairo_destroy(cr);
1542 blur_surface(d->shadow, 64);
1543
1544 d->active_frame =
1545 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1546 cr = cairo_create(d->active_frame);
1547 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1548 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1549 rounded_rect(cr, 16, 16, 112, 112, radius);
1550 cairo_fill(cr);
1551 cairo_destroy(cr);
1552
1553 d->inactive_frame =
1554 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1555 cr = cairo_create(d->inactive_frame);
1556 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1557 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1558 rounded_rect(cr, 16, 16, 112, 112, radius);
1559 cairo_fill(cr);
1560 cairo_destroy(cr);
1561}
1562
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001563static void
1564init_xkb(struct display *d)
1565{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001566 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001567
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001568 names.rules = "evdev";
1569 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001570 names.layout = option_xkb_layout;
1571 names.variant = option_xkb_variant;
1572 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001573
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001574 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001575 if (!d->xkb) {
1576 fprintf(stderr, "Failed to compile keymap\n");
1577 exit(1);
1578 }
1579}
1580
Yuval Fledel45568f62010-12-06 09:18:12 -05001581static int
1582init_drm(struct display *d)
1583{
1584 EGLint major, minor;
1585 int fd;
1586 drm_magic_t magic;
1587
1588 fd = open(d->device_name, O_RDWR);
1589 if (fd < 0) {
1590 fprintf(stderr, "drm open failed: %m\n");
1591 return -1;
1592 }
1593
1594 if (drmGetMagic(fd, &magic)) {
nobled14d222f2011-02-01 18:48:46 +00001595 fprintf(stderr, "DRI2: failed to get drm magic\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001596 return -1;
1597 }
1598
1599 /* Wait for authenticated event */
1600 wl_drm_authenticate(d->drm, magic);
1601 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1602 while (!d->authenticated)
1603 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1604
1605 d->dpy = eglGetDRMDisplayMESA(fd);
1606 if (!eglInitialize(d->dpy, &major, &minor)) {
1607 fprintf(stderr, "failed to initialize display\n");
1608 return -1;
1609 }
1610
1611 if (!eglBindAPI(EGL_OPENGL_API)) {
1612 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1613 return -1;
1614 }
1615
1616 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1617 if (d->ctx == NULL) {
1618 fprintf(stderr, "failed to create context\n");
1619 return -1;
1620 }
1621
1622 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1623 fprintf(stderr, "faile to make context current\n");
1624 return -1;
1625 }
1626
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001627#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001628 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1629 if (d->device == NULL) {
1630 fprintf(stderr, "failed to get cairo drm device\n");
1631 return -1;
1632 }
1633#endif
1634
1635 return 0;
1636}
1637
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001638struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001639display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001640{
1641 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001642 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001643 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001644 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001645
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001646 g_type_init();
1647
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001648 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001649 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001650 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001651
1652 xkb_option_group = g_option_group_new("xkb",
1653 "Keyboard options",
1654 "Show all XKB options",
1655 NULL, NULL);
1656 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1657 g_option_context_add_group (context, xkb_option_group);
1658
1659 if (!g_option_context_parse(context, argc, argv, &error)) {
1660 fprintf(stderr, "option parsing failed: %s\n", error->message);
1661 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001662 }
1663
Tim Wiederhake748f6722011-01-23 23:25:25 +01001664 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001665
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001666 d = malloc(sizeof *d);
1667 if (d == NULL)
1668 return NULL;
1669
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001670 memset(d, 0, sizeof *d);
1671
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001672 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001673 if (d->display == NULL) {
1674 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001675 return NULL;
1676 }
1677
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001678 wl_list_init(&d->input_list);
1679
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001680 /* Set up listener so we'll catch all events. */
1681 wl_display_add_global_listener(d->display,
1682 display_handle_global, d);
1683
1684 /* Process connection events. */
1685 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1686
Yuval Fledel45568f62010-12-06 09:18:12 -05001687 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001688 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001689
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001690 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001691
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001692 display_render_frame(d);
1693
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001694 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001695 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001696 g_source_attach(d->source, NULL);
1697
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001698 wl_list_init(&d->window_list);
1699
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001700 init_xkb(d);
1701
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001702 return d;
1703}
1704
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001705struct wl_display *
1706display_get_display(struct display *display)
1707{
1708 return display->display;
1709}
1710
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001711struct wl_compositor *
1712display_get_compositor(struct display *display)
1713{
1714 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001715}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001716
1717EGLDisplay
1718display_get_egl_display(struct display *d)
1719{
1720 return d->dpy;
1721}
1722
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001723struct wl_shell *
1724display_get_shell(struct display *display)
1725{
1726 return display->shell;
1727}
1728
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001729void
1730display_run(struct display *d)
1731{
1732 g_main_loop_run(d->loop);
1733}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001734
1735void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001736display_set_global_handler(struct display *display,
1737 display_global_handler_t handler)
1738{
1739 display->global_handler = handler;
1740}