blob: da9b339986e9cc4f6ec40a4365e28b20792018c6 [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
nobled7b87cb02011-02-01 18:51:47 +0000248 if (data->image == EGL_NO_IMAGE_KHR) {
249 free(data);
250 return NULL;
251 }
252
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800253 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400254 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400255 glBindTexture(GL_TEXTURE_2D, data->texture);
256 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800257 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400258
259 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
260
261 visual = wl_display_get_premultiplied_argb_visual(display->display);
262 data->data.buffer =
263 wl_drm_create_buffer(display->drm, name, rectangle->width,
264 rectangle->height, stride, visual);
265
266 surface = cairo_gl_surface_create_for_texture(display->device,
267 CAIRO_CONTENT_COLOR_ALPHA,
268 data->texture,
269 rectangle->width,
270 rectangle->height);
271
272 cairo_surface_set_user_data (surface, &surface_data_key,
273 data, drm_surface_data_destroy);
274
275 return surface;
276}
277
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500278static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400279display_create_drm_surface_from_file(struct display *display,
280 const char *filename,
281 struct rectangle *rect)
282{
283 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400284 GdkPixbuf *pixbuf;
285 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400286 int stride, i;
287 unsigned char *pixels, *p, *end;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800288 struct drm_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400289
290 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400291 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400292 FALSE, &error);
293 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400294 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400295
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400296 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
297 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500298 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400299 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400300 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400301
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400302
303 stride = gdk_pixbuf_get_rowstride(pixbuf);
304 pixels = gdk_pixbuf_get_pixels(pixbuf);
305
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400306 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400307 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400308 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400309 while (p < end) {
310 unsigned int t;
311
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400312 MULT(p[0], p[0], p[3], t);
313 MULT(p[1], p[1], p[3], t);
314 MULT(p[2], p[2], p[3], t);
315 p += 4;
316
317 }
318 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400319
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320 surface = display_create_drm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000321 if (surface == NULL) {
322 g_object_unref(pixbuf);
323 return NULL;
324 }
325
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326 data = cairo_surface_get_user_data(surface, &surface_data_key);
327
328 cairo_device_acquire(display->device);
329 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800330 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
331 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800332 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400333
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500334 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400335
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336 return surface;
337}
338
339#endif
340
341struct wl_buffer *
342display_get_buffer_for_surface(struct display *display,
343 cairo_surface_t *surface)
344{
345 struct surface_data *data;
346
347 data = cairo_surface_get_user_data (surface, &surface_data_key);
348
349 return data->buffer;
350}
351
352struct shm_surface_data {
353 struct surface_data data;
354 void *map;
355 size_t length;
356};
357
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500358static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359shm_surface_data_destroy(void *p)
360{
361 struct shm_surface_data *data = p;
362
363 wl_buffer_destroy(data->data.buffer);
364 munmap(data->map, data->length);
365}
366
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500367static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400368display_create_shm_surface(struct display *display,
369 struct rectangle *rectangle)
370{
371 struct shm_surface_data *data;
372 cairo_surface_t *surface;
373 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800374 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400375 char filename[] = "/tmp/wayland-shm-XXXXXX";
376
377 data = malloc(sizeof *data);
378 if (data == NULL)
379 return NULL;
380
381 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
382 rectangle->width);
383 data->length = stride * rectangle->height;
384 fd = mkstemp(filename);
385 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000386 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400387 return NULL;
388 }
389 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000390 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 close(fd);
392 return NULL;
393 }
394
395 data->map = mmap(NULL, data->length,
396 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
397 unlink(filename);
398
399 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000400 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401 close(fd);
402 return NULL;
403 }
404
405 surface = cairo_image_surface_create_for_data (data->map,
406 CAIRO_FORMAT_ARGB32,
407 rectangle->width,
408 rectangle->height,
409 stride);
410
411 cairo_surface_set_user_data (surface, &surface_data_key,
412 data, shm_surface_data_destroy);
413
414 visual = wl_display_get_premultiplied_argb_visual(display->display);
415 data->data.buffer = wl_shm_create_buffer(display->shm,
416 fd,
417 rectangle->width,
418 rectangle->height,
419 stride, visual);
420
421 close(fd);
422
423 return surface;
424}
425
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500426static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427display_create_shm_surface_from_file(struct display *display,
428 const char *filename,
429 struct rectangle *rect)
430{
431 cairo_surface_t *surface;
432 GdkPixbuf *pixbuf;
433 GError *error = NULL;
434 int stride, i;
435 unsigned char *pixels, *p, *end, *dest_data;
436 int dest_stride;
437 uint32_t *d;
438
439 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
440 rect->width, rect->height,
441 FALSE, &error);
442 if (error != NULL)
443 return NULL;
444
445 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
446 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500447 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400448 return NULL;
449 }
450
451 stride = gdk_pixbuf_get_rowstride(pixbuf);
452 pixels = gdk_pixbuf_get_pixels(pixbuf);
453
454 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000455 if (surface == NULL) {
456 g_object_unref(pixbuf);
457 return NULL;
458 }
459
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400460 dest_data = cairo_image_surface_get_data (surface);
461 dest_stride = cairo_image_surface_get_stride (surface);
462
463 for (i = 0; i < rect->height; i++) {
464 d = (uint32_t *) (dest_data + i * dest_stride);
465 p = pixels + i * stride;
466 end = p + rect->width * 4;
467 while (p < end) {
468 unsigned int t;
469 unsigned char a, r, g, b;
470
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400471 a = p[3];
472 MULT(r, p[0], a, t);
473 MULT(g, p[1], a, t);
474 MULT(b, p[2], a, t);
475 p += 4;
476 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
477 }
478 }
479
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500480 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400481
482 return surface;
483}
484
nobled7b87cb02011-02-01 18:51:47 +0000485static int
486check_size(struct rectangle *rect)
487{
488 if (rect->width && rect->height)
489 return 0;
490
491 fprintf(stderr, "tried to create surface of "
492 "width: %d, height: %d\n", rect->width, rect->height);
493 return -1;
494}
495
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496cairo_surface_t *
497display_create_surface(struct display *display,
498 struct rectangle *rectangle)
499{
nobled7b87cb02011-02-01 18:51:47 +0000500 if (check_size(rectangle) < 0)
501 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500502#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500503 if (display->drm) {
504 return display_create_drm_surface(display, rectangle);
505 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500507 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508}
509
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500510static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511display_create_surface_from_file(struct display *display,
512 const char *filename,
513 struct rectangle *rectangle)
514{
nobled7b87cb02011-02-01 18:51:47 +0000515 if (check_size(rectangle) < 0)
516 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500517#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -0500518 if (display->drm) {
519 return display_create_drm_surface_from_file(display, filename, rectangle);
520 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500522 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400523}
Yuval Fledel45568f62010-12-06 09:18:12 -0500524 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400525 const char *filename;
526 int hotspot_x, hotspot_y;
527} pointer_images[] = {
528 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
529 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
530 { DATADIR "/wayland/bottom_side.png", 16, 20 },
531 { DATADIR "/wayland/grabbing.png", 20, 17 },
532 { DATADIR "/wayland/left_ptr.png", 10, 5 },
533 { DATADIR "/wayland/left_side.png", 10, 20 },
534 { DATADIR "/wayland/right_side.png", 30, 19 },
535 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
536 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
537 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400538 { DATADIR "/wayland/xterm.png", 15, 15 },
539 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400540};
541
542static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400543create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400544{
545 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400546 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400547 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400548
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400549 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400550 display->pointer_surfaces =
551 malloc(count * sizeof *display->pointer_surfaces);
552 rect.width = width;
553 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400554 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400555 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556 display_create_surface_from_file(display,
557 pointer_images[i].filename,
558 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400559 }
560
561}
562
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400563cairo_surface_t *
564display_get_pointer_surface(struct display *display, int pointer,
565 int *width, int *height,
566 int *hotspot_x, int *hotspot_y)
567{
568 cairo_surface_t *surface;
569
570 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500571#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400572 *width = cairo_gl_surface_get_width(surface);
573 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000574#else
575 *width = cairo_image_surface_get_width(surface);
576 *height = cairo_image_surface_get_height(surface);
577#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400578 *hotspot_x = pointer_images[pointer].hotspot_x;
579 *hotspot_y = pointer_images[pointer].hotspot_y;
580
581 return cairo_surface_reference(surface);
582}
583
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400584
585static void
586window_attach_surface(struct window *window);
587
588static void
589free_surface(void *data)
590{
591 struct window *window = data;
592
593 cairo_surface_destroy(window->pending_surface);
594 window->pending_surface = NULL;
595 if (window->cairo_surface)
596 window_attach_surface(window);
597}
598
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500599static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500600window_attach_surface(struct window *window)
601{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400602 struct display *display = window->display;
603 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500604 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500605
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500606 if (window->pending_surface != NULL)
607 return;
608
Kristian Høgsberg09531622010-06-14 23:22:15 -0400609 window->pending_surface = window->cairo_surface;
610 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500611
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400612 buffer = display_get_buffer_for_surface(display,
613 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500614 if (window->resize_edges & WINDOW_RESIZING_LEFT)
615 x = window->server_allocation.width -
616 window->allocation.width;
617 else
618 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500619
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500620 if (window->resize_edges & WINDOW_RESIZING_TOP)
621 y = window->server_allocation.height -
622 window->allocation.height;
623 else
624 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400625
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500626 window->server_allocation = window->allocation;
627 window->resize_edges = 0;
628 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400629 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500630
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500631 if (window->fullscreen)
632 wl_surface_map_fullscreen(window->surface);
633 else if (!window->parent)
634 wl_surface_map_toplevel(window->surface);
635 else
636 wl_surface_map_transient(window->surface,
637 window->parent->surface,
638 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500639
640 wl_surface_damage(window->surface, 0, 0,
641 window->allocation.width,
642 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500643}
644
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500645void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400646window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500647{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400648 if (window->cairo_surface)
649 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500650}
651
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400652void
653window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500655 cairo_surface_reference(surface);
656
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400657 if (window->cairo_surface != NULL)
658 cairo_surface_destroy(window->cairo_surface);
659
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500660 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400661}
662
663void
664window_create_surface(struct window *window)
665{
666 cairo_surface_t *surface;
667
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500669#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670 case WINDOW_BUFFER_TYPE_DRM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400671 surface = display_create_surface(window->display,
672 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400673 break;
674#endif
675 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400676 surface = display_create_shm_surface(window->display,
677 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400678 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800679 default:
680 surface = NULL;
681 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400683
684 window_set_surface(window, surface);
685 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400686}
687
688static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500689window_draw_menu(struct window *window)
690{
691 cairo_t *cr;
692 int width, height, r = 5;
693
694 window_create_surface(window);
695
696 cr = cairo_create(window->cairo_surface);
697 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
698 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
699 cairo_paint(cr);
700
701 width = window->allocation.width;
702 height = window->allocation.height;
703 rounded_rect(cr, r, r, width - r, height - r, r);
704 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
705 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
706 cairo_fill(cr);
707 cairo_destroy(cr);
708}
709
710static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500711window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500712{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500713 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500714 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400715 cairo_surface_t *frame;
716 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500717
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400718 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400719
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400720 width = window->allocation.width;
721 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500722
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500723 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500724
Kristian Høgsberg09531622010-06-14 23:22:15 -0400725 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400726 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400727 cairo_paint(cr);
728
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400729 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400730 tile_mask(cr, window->display->shadow,
731 shadow_dx, shadow_dy, width, height,
732 window->margin + 10 - shadow_dx,
733 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500734
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400735 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400736 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400737 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400738 frame = window->display->inactive_frame;
739
740 tile_source(cr, frame, 0, 0, width, height,
741 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500742
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500743 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
744 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500745 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400746 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400747 if (window->keyboard_device)
748 cairo_set_source_rgb(cr, 0, 0, 0);
749 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400750 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400751 cairo_show_text(cr, window->title);
752
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500753 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400754
755 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500756}
757
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400758void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500759window_destroy(struct window *window)
760{
761 wl_surface_destroy(window->surface);
762 wl_list_remove(&window->link);
763 free(window);
764}
765
766void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400767display_flush_cairo_device(struct display *display)
768{
769 cairo_device_flush (display->device);
770}
771
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500772void
773window_draw(struct window *window)
774{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500775 if (window->parent)
776 window_draw_menu(window);
777 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400778 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500779 else
780 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500781}
782
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400783cairo_surface_t *
784window_get_surface(struct window *window)
785{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400786 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400787}
788
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400789static int
790get_pointer_location(struct window *window, int32_t x, int32_t y)
791{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400792 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400793 const int grip_size = 8;
794
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500795 if (!window->decoration)
796 return WINDOW_CLIENT_AREA;
797
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400798 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400799 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400800 else if (window->margin <= x && x < window->margin + grip_size)
801 hlocation = WINDOW_RESIZING_LEFT;
802 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400803 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400804 else if (x < window->allocation.width - window->margin)
805 hlocation = WINDOW_RESIZING_RIGHT;
806 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400807 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400808
809 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400810 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400811 else if (window->margin <= y && y < window->margin + grip_size)
812 vlocation = WINDOW_RESIZING_TOP;
813 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400814 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400815 else if (y < window->allocation.height - window->margin)
816 vlocation = WINDOW_RESIZING_BOTTOM;
817 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400818 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400819
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400820 location = vlocation | hlocation;
821 if (location & WINDOW_EXTERIOR)
822 location = WINDOW_EXTERIOR;
823 if (location == WINDOW_INTERIOR && y < window->margin + 50)
824 location = WINDOW_TITLEBAR;
825 else if (location == WINDOW_INTERIOR)
826 location = WINDOW_CLIENT_AREA;
827
828 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400829}
830
831static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400832set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400833{
834 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400835 struct wl_buffer *buffer;
836 cairo_surface_t *surface;
837 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400838
839 location = get_pointer_location(input->pointer_focus,
840 input->sx, input->sy);
841 switch (location) {
842 case WINDOW_RESIZING_TOP:
843 pointer = POINTER_TOP;
844 break;
845 case WINDOW_RESIZING_BOTTOM:
846 pointer = POINTER_BOTTOM;
847 break;
848 case WINDOW_RESIZING_LEFT:
849 pointer = POINTER_LEFT;
850 break;
851 case WINDOW_RESIZING_RIGHT:
852 pointer = POINTER_RIGHT;
853 break;
854 case WINDOW_RESIZING_TOP_LEFT:
855 pointer = POINTER_TOP_LEFT;
856 break;
857 case WINDOW_RESIZING_TOP_RIGHT:
858 pointer = POINTER_TOP_RIGHT;
859 break;
860 case WINDOW_RESIZING_BOTTOM_LEFT:
861 pointer = POINTER_BOTTOM_LEFT;
862 break;
863 case WINDOW_RESIZING_BOTTOM_RIGHT:
864 pointer = POINTER_BOTTOM_RIGHT;
865 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400866 case WINDOW_EXTERIOR:
867 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400868 if (input->current_pointer_image == POINTER_DEFAULT)
869 return;
870
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400871 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400872 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400873 return;
874 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400875 break;
876 }
877
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400878 if (pointer == input->current_pointer_image)
879 return;
880
881 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400882 surface = display->pointer_surfaces[pointer];
883 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400884 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400885 pointer_images[pointer].hotspot_x,
886 pointer_images[pointer].hotspot_y);
887}
888
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500889static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500890window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400891 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500892 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500893{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400894 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400895 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500896 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400897
898 input->x = x;
899 input->y = y;
900 input->sx = sx;
901 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400902
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400903 if (window->motion_handler)
904 pointer = (*window->motion_handler)(window, input, time,
905 x, y, sx, sy,
906 window->user_data);
907
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400908 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500909}
910
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400911static void
912window_handle_button(void *data,
913 struct wl_input_device *input_device,
914 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500915{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400916 struct input *input = data;
917 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400918 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400919
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400920 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500921
922 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400923 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400924 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400925 wl_shell_move(window->display->shell,
926 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500927 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400928 case WINDOW_RESIZING_TOP:
929 case WINDOW_RESIZING_BOTTOM:
930 case WINDOW_RESIZING_LEFT:
931 case WINDOW_RESIZING_RIGHT:
932 case WINDOW_RESIZING_TOP_LEFT:
933 case WINDOW_RESIZING_TOP_RIGHT:
934 case WINDOW_RESIZING_BOTTOM_LEFT:
935 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400936 wl_shell_resize(window->display->shell,
937 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400938 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500939 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400940 case WINDOW_CLIENT_AREA:
941 if (window->button_handler)
942 (*window->button_handler)(window,
943 input, time,
944 button, state,
945 window->user_data);
946 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500947 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400948 } else {
949 if (window->button_handler)
950 (*window->button_handler)(window,
951 input, time,
952 button, state,
953 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500954 }
955}
956
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500957static void
958window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400959 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500960{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400961 struct input *input = data;
962 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400963 struct display *d = window->display;
964 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500965
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400966 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400967 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500968 return;
969
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400970 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500971 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400972 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
973 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500974
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400975 sym = XkbKeySymEntry(d->xkb, code, level, 0);
976
977 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400978 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400979 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400980 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500981
982 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500983 (*window->key_handler)(window, input, time, key, sym, state,
984 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500985}
986
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500987static void
988window_handle_pointer_focus(void *data,
989 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400990 uint32_t time, struct wl_surface *surface,
991 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500992{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400993 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400994 struct window *window;
995 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400996
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400997 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400998 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400999 window = input->pointer_focus;
1000
Kristian Høgsberg59826582011-01-20 11:56:57 -05001001 input->x = x;
1002 input->y = y;
1003 input->sx = sx;
1004 input->sy = sy;
1005
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001006 pointer = POINTER_LEFT_PTR;
1007 if (window->motion_handler)
1008 pointer = (*window->motion_handler)(window,
1009 input, time,
1010 x, y, sx, sy,
1011 window->user_data);
1012
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001013 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001014 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001015 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001016 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001017 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001018}
1019
1020static void
1021window_handle_keyboard_focus(void *data,
1022 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001023 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001024 struct wl_surface *surface,
1025 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001026{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001027 struct input *input = data;
1028 struct window *window = input->keyboard_focus;
1029 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001030 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001031
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001032 window = input->keyboard_focus;
1033 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001034 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001035 if (window->keyboard_focus_handler)
1036 (*window->keyboard_focus_handler)(window, NULL,
1037 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001038 }
1039
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001040 if (surface)
1041 input->keyboard_focus = wl_surface_get_user_data(surface);
1042 else
1043 input->keyboard_focus = NULL;
1044
1045 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001046 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001047 for (k = keys->data; k < end; k++)
1048 input->modifiers |= d->xkb->map->modmap[*k];
1049
1050 window = input->keyboard_focus;
1051 if (window) {
1052 window->keyboard_device = input;
1053 if (window->keyboard_focus_handler)
1054 (*window->keyboard_focus_handler)(window,
1055 window->keyboard_device,
1056 window->user_data);
1057 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001058}
1059
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001060static const struct wl_input_device_listener input_device_listener = {
1061 window_handle_motion,
1062 window_handle_button,
1063 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001064 window_handle_pointer_focus,
1065 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001066};
1067
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001068void
1069input_get_position(struct input *input, int32_t *x, int32_t *y)
1070{
1071 *x = input->sx;
1072 *y = input->sy;
1073}
1074
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001075struct wl_input_device *
1076input_get_input_device(struct input *input)
1077{
1078 return input->input_device;
1079}
1080
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001081uint32_t
1082input_get_modifiers(struct input *input)
1083{
1084 return input->modifiers;
1085}
1086
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001087struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001088window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001089{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001090 cairo_device_flush (window->display->device);
1091
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001092 return wl_shell_create_drag(window->display->shell);
1093}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001094
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001095void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001096window_move(struct window *window, struct input *input, uint32_t time)
1097{
1098 wl_shell_move(window->display->shell,
1099 window->surface, input->input_device, time);
1100}
1101
1102void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001103window_activate_drag(struct wl_drag *drag, struct window *window,
1104 struct input *input, uint32_t time)
1105{
1106 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107}
1108
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001109static void
1110handle_configure(void *data, struct wl_shell *shell,
1111 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001112 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001113{
1114 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001115 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001116
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001117 /* FIXME: this is probably the wrong place to check for width
1118 * or height <= 0, but it prevents the compositor from crashing
1119 */
1120 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001121 return;
1122
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001123 window->resize_edges = edges;
1124
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001125 if (window->resize_handler) {
1126 child_width = width - 20 - window->margin * 2;
1127 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001128
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001129 (*window->resize_handler)(window,
1130 child_width, child_height,
1131 window->user_data);
1132 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001133 window->allocation.width = width;
1134 window->allocation.height = height;
1135
1136 if (window->redraw_handler)
1137 window_schedule_redraw(window);
1138 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001139}
1140
1141static const struct wl_shell_listener shell_listener = {
1142 handle_configure,
1143};
1144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001145void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001146window_get_child_allocation(struct window *window,
1147 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001148{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001149 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001150 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001151 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001152 allocation->x = window->margin + 10;
1153 allocation->y = window->margin + 50;
1154 allocation->width =
1155 window->allocation.width - 20 - window->margin * 2;
1156 allocation->height =
1157 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001158 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001159}
1160
1161void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001162window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001163{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001164 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001165 window->allocation.x = 20 + window->margin;
1166 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001167 window->allocation.width = width + 20 + window->margin * 2;
1168 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001169 } else {
1170 window->allocation.x = 0;
1171 window->allocation.y = 0;
1172 window->allocation.width = width;
1173 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001174 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001175}
1176
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001177static gboolean
1178idle_redraw(void *data)
1179{
1180 struct window *window = data;
1181
1182 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001183
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001184 window->redraw_scheduled = 0;
1185
1186 return FALSE;
1187}
1188
1189void
1190window_schedule_redraw(struct window *window)
1191{
1192 if (!window->redraw_scheduled) {
1193 g_idle_add(idle_redraw, window);
1194 window->redraw_scheduled = 1;
1195 }
1196}
1197
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001198void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001199window_set_fullscreen(struct window *window, int fullscreen)
1200{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001201 int32_t width, height;
1202
1203 if (window->fullscreen == fullscreen)
1204 return;
1205
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001206 window->fullscreen = fullscreen;
1207 if (window->fullscreen) {
1208 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001209 width = window->display->screen_allocation.width;
1210 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001211 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001212 width = window->saved_allocation.width - 20 - window->margin * 2;
1213 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001214 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001215
1216 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001217}
1218
1219void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001220window_set_decoration(struct window *window, int decoration)
1221{
1222 window->decoration = decoration;
1223}
1224
1225void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001226window_set_user_data(struct window *window, void *data)
1227{
1228 window->user_data = data;
1229}
1230
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001231void *
1232window_get_user_data(struct window *window)
1233{
1234 return window->user_data;
1235}
1236
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001237void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001238window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001239 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001240{
1241 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001242}
1243
1244void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001245window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001246 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001247{
1248 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001249}
1250
1251void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001252window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001253 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001254{
1255 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001256}
1257
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001258void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259window_set_button_handler(struct window *window,
1260 window_button_handler_t handler)
1261{
1262 window->button_handler = handler;
1263}
1264
1265void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001266window_set_motion_handler(struct window *window,
1267 window_motion_handler_t handler)
1268{
1269 window->motion_handler = handler;
1270}
1271
1272void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001273window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001274 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001275{
1276 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001277}
1278
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001279void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001280window_set_title(struct window *window, const char *title)
1281{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001282 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001283 window->title = strdup(title);
1284}
1285
1286const char *
1287window_get_title(struct window *window)
1288{
1289 return window->title;
1290}
1291
1292void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001293window_damage(struct window *window, int32_t x, int32_t y,
1294 int32_t width, int32_t height)
1295{
1296 wl_surface_damage(window->surface, x, y, width, height);
1297}
1298
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001299static struct window *
1300window_create_internal(struct display *display, struct window *parent,
1301 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001302{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001303 struct window *window;
1304
1305 window = malloc(sizeof *window);
1306 if (window == NULL)
1307 return NULL;
1308
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001309 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001310 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001311 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001312 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001313 window->allocation.x = 0;
1314 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001315 window->allocation.width = width;
1316 window->allocation.height = height;
1317 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001318 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001319 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001320
Yuval Fledel45568f62010-12-06 09:18:12 -05001321 if (display->drm)
1322 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1323 else
1324 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001325
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001326 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001327 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001328
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001329 return window;
1330}
1331
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001332struct window *
1333window_create(struct display *display, int32_t width, int32_t height)
1334{
1335 struct window *window;
1336
1337 window = window_create_internal(display, NULL, width, height);
1338 if (!window)
1339 return NULL;
1340
1341 return window;
1342}
1343
1344struct window *
1345window_create_transient(struct display *display, struct window *parent,
1346 int32_t x, int32_t y, int32_t width, int32_t height)
1347{
1348 struct window *window;
1349
1350 window = window_create_internal(parent->display,
1351 parent, width, height);
1352 if (!window)
1353 return NULL;
1354
1355 window->x = x;
1356 window->y = y;
1357
1358 return window;
1359}
1360
1361void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001362window_set_buffer_type(struct window *window, enum window_buffer_type type)
1363{
1364 window->buffer_type = type;
1365}
1366
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001367static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001368drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001369{
1370 struct display *d = data;
1371
1372 d->device_name = strdup(device);
1373}
1374
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001375static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1376{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001377 struct display *d = data;
1378
1379 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001380}
1381
1382static const struct wl_drm_listener drm_listener = {
1383 drm_handle_device,
1384 drm_handle_authenticated
1385};
1386
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001387static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001388display_handle_geometry(void *data,
1389 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001390 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001391{
1392 struct display *display = data;
1393
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001394 display->screen_allocation.x = x;
1395 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001396 display->screen_allocation.width = width;
1397 display->screen_allocation.height = height;
1398}
1399
1400static const struct wl_output_listener output_listener = {
1401 display_handle_geometry,
1402};
1403
1404static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001405display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001406{
1407 struct input *input;
1408
1409 input = malloc(sizeof *input);
1410 if (input == NULL)
1411 return;
1412
1413 memset(input, 0, sizeof *input);
1414 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001415 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001416 input->pointer_focus = NULL;
1417 input->keyboard_focus = NULL;
1418 wl_list_insert(d->input_list.prev, &input->link);
1419
1420 wl_input_device_add_listener(input->input_device,
1421 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001422 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001423}
1424
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001425struct selection_offer {
1426 struct display *display;
1427 struct wl_selection_offer *offer;
1428 struct wl_array types;
1429 struct input *input;
1430};
1431
1432int
1433input_offers_mime_type(struct input *input, const char *type)
1434{
1435 struct selection_offer *offer = input->offer;
1436 char **p, **end;
1437
1438 if (offer == NULL)
1439 return 0;
1440
1441 end = offer->types.data + offer->types.size;
1442 for (p = offer->types.data; p < end; p++)
1443 if (strcmp(*p, type) == 0)
1444 return 1;
1445
1446 return 0;
1447}
1448
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001449void
1450input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001451{
1452 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001453
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001454 /* FIXME: A number of things can go wrong here: the object may
1455 * not be the current selection offer any more (which could
1456 * still work, but the source may have gone away or just
1457 * destroyed its wl_selection) or the offer may not have the
1458 * requested type after all (programmer/client error,
1459 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001460 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001461}
1462
1463static void
1464selection_offer_offer(void *data,
1465 struct wl_selection_offer *selection_offer,
1466 const char *type)
1467{
1468 struct selection_offer *offer = data;
1469
1470 char **p;
1471
1472 p = wl_array_add(&offer->types, sizeof *p);
1473 if (p)
1474 *p = strdup(type);
1475};
1476
1477static void
1478selection_offer_keyboard_focus(void *data,
1479 struct wl_selection_offer *selection_offer,
1480 struct wl_input_device *input_device)
1481{
1482 struct selection_offer *offer = data;
1483 struct input *input;
1484 char **p, **end;
1485
1486 if (input_device == NULL) {
1487 printf("selection offer retracted %p\n", selection_offer);
1488 input = offer->input;
1489 input->offer = NULL;
1490 wl_selection_offer_destroy(selection_offer);
1491 wl_array_release(&offer->types);
1492 free(offer);
1493 return;
1494 }
1495
1496 input = wl_input_device_get_user_data(input_device);
1497 printf("new selection offer %p:", selection_offer);
1498
1499 offer->input = input;
1500 input->offer = offer;
1501 end = offer->types.data + offer->types.size;
1502 for (p = offer->types.data; p < end; p++)
1503 printf(" %s", *p);
1504
1505 printf("\n");
1506}
1507
1508struct wl_selection_offer_listener selection_offer_listener = {
1509 selection_offer_offer,
1510 selection_offer_keyboard_focus
1511};
1512
1513static void
1514add_selection_offer(struct display *d, uint32_t id)
1515{
1516 struct selection_offer *offer;
1517
1518 offer = malloc(sizeof *offer);
1519 if (offer == NULL)
1520 return;
1521
1522 offer->offer = wl_selection_offer_create(d->display, id);
1523 offer->display = d;
1524 wl_array_init(&offer->types);
1525 offer->input = NULL;
1526
1527 wl_selection_offer_add_listener(offer->offer,
1528 &selection_offer_listener, offer);
1529}
1530
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001531static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001532display_handle_global(struct wl_display *display, uint32_t id,
1533 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001534{
1535 struct display *d = data;
1536
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001537 if (strcmp(interface, "compositor") == 0) {
1538 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001539 } else if (strcmp(interface, "output") == 0) {
1540 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001541 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001542 } else if (strcmp(interface, "input_device") == 0) {
1543 display_add_input(d, id);
1544 } else if (strcmp(interface, "shell") == 0) {
1545 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001546 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001547 } else if (strcmp(interface, "drm") == 0) {
1548 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001549 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001550 } else if (strcmp(interface, "shm") == 0) {
1551 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001552 } else if (strcmp(interface, "selection_offer") == 0) {
1553 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001554 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001555 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001556 }
1557}
1558
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001559static void
1560display_render_frame(struct display *d)
1561{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001562 int radius = 8;
1563 cairo_t *cr;
1564
1565 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1566 cr = cairo_create(d->shadow);
1567 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1568 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1569 rounded_rect(cr, 16, 16, 112, 112, radius);
1570 cairo_fill(cr);
1571 cairo_destroy(cr);
1572 blur_surface(d->shadow, 64);
1573
1574 d->active_frame =
1575 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1576 cr = cairo_create(d->active_frame);
1577 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1578 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1579 rounded_rect(cr, 16, 16, 112, 112, radius);
1580 cairo_fill(cr);
1581 cairo_destroy(cr);
1582
1583 d->inactive_frame =
1584 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1585 cr = cairo_create(d->inactive_frame);
1586 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1587 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1588 rounded_rect(cr, 16, 16, 112, 112, radius);
1589 cairo_fill(cr);
1590 cairo_destroy(cr);
1591}
1592
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001593static void
1594init_xkb(struct display *d)
1595{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001596 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001597
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001598 names.rules = "evdev";
1599 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001600 names.layout = option_xkb_layout;
1601 names.variant = option_xkb_variant;
1602 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001603
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001604 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001605 if (!d->xkb) {
1606 fprintf(stderr, "Failed to compile keymap\n");
1607 exit(1);
1608 }
1609}
1610
Yuval Fledel45568f62010-12-06 09:18:12 -05001611static int
1612init_drm(struct display *d)
1613{
1614 EGLint major, minor;
1615 int fd;
1616 drm_magic_t magic;
1617
1618 fd = open(d->device_name, O_RDWR);
1619 if (fd < 0) {
1620 fprintf(stderr, "drm open failed: %m\n");
1621 return -1;
1622 }
1623
1624 if (drmGetMagic(fd, &magic)) {
nobled14d222f2011-02-01 18:48:46 +00001625 fprintf(stderr, "DRI2: failed to get drm magic\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001626 return -1;
1627 }
1628
1629 /* Wait for authenticated event */
1630 wl_drm_authenticate(d->drm, magic);
1631 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1632 while (!d->authenticated)
1633 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1634
1635 d->dpy = eglGetDRMDisplayMESA(fd);
1636 if (!eglInitialize(d->dpy, &major, &minor)) {
1637 fprintf(stderr, "failed to initialize display\n");
1638 return -1;
1639 }
1640
1641 if (!eglBindAPI(EGL_OPENGL_API)) {
1642 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1643 return -1;
1644 }
1645
1646 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1647 if (d->ctx == NULL) {
1648 fprintf(stderr, "failed to create context\n");
1649 return -1;
1650 }
1651
1652 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1653 fprintf(stderr, "faile to make context current\n");
1654 return -1;
1655 }
1656
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001657#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001658 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1659 if (d->device == NULL) {
1660 fprintf(stderr, "failed to get cairo drm device\n");
1661 return -1;
1662 }
1663#endif
1664
1665 return 0;
1666}
1667
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001668struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001669display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001670{
1671 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001672 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001673 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001674 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001675
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001676 g_type_init();
1677
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001678 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001679 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001680 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001681
1682 xkb_option_group = g_option_group_new("xkb",
1683 "Keyboard options",
1684 "Show all XKB options",
1685 NULL, NULL);
1686 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1687 g_option_context_add_group (context, xkb_option_group);
1688
1689 if (!g_option_context_parse(context, argc, argv, &error)) {
1690 fprintf(stderr, "option parsing failed: %s\n", error->message);
1691 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001692 }
1693
Tim Wiederhake748f6722011-01-23 23:25:25 +01001694 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001695
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001696 d = malloc(sizeof *d);
1697 if (d == NULL)
1698 return NULL;
1699
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001700 memset(d, 0, sizeof *d);
1701
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001702 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001703 if (d->display == NULL) {
1704 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001705 return NULL;
1706 }
1707
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001708 wl_list_init(&d->input_list);
1709
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001710 /* Set up listener so we'll catch all events. */
1711 wl_display_add_global_listener(d->display,
1712 display_handle_global, d);
1713
1714 /* Process connection events. */
1715 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1716
Yuval Fledel45568f62010-12-06 09:18:12 -05001717 if (d->device_name && init_drm(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001718 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001719
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001720 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001721
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001722 display_render_frame(d);
1723
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001724 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001725 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001726 g_source_attach(d->source, NULL);
1727
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001728 wl_list_init(&d->window_list);
1729
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001730 init_xkb(d);
1731
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001732 return d;
1733}
1734
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001735struct wl_display *
1736display_get_display(struct display *display)
1737{
1738 return display->display;
1739}
1740
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001741struct wl_compositor *
1742display_get_compositor(struct display *display)
1743{
1744 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001745}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001746
1747EGLDisplay
1748display_get_egl_display(struct display *d)
1749{
1750 return d->dpy;
1751}
1752
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001753struct wl_shell *
1754display_get_shell(struct display *display)
1755{
1756 return display->shell;
1757}
1758
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001759void
1760display_run(struct display *d)
1761{
1762 g_main_loop_run(d->loop);
1763}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001764
1765void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001766display_set_global_handler(struct display *display,
1767 display_global_handler_t handler)
1768{
1769 display->global_handler = handler;
1770}