blob: f495476611178c5326c3703ab909c216f1a8f780 [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øgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050039#include <wayland-egl.h>
40
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041#define EGL_EGLEXT_PROTOTYPES 1
42#define GL_GLEXT_PROTOTYPES 1
43#include <GL/gl.h>
44#include <EGL/egl.h>
45#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040046
Kristian Høgsberg8def2642011-01-14 17:41:33 -050047#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040049#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050050
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040051#include <X11/extensions/XKBcommon.h>
52
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050053#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050054#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050055#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050056#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040057#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050060
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050062 struct wl_display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -050063 struct wl_egl_display *native_dpy;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050064 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040065 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040066 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050068 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040069 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040070 EGLDisplay dpy;
71 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010072 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050073 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040074 GMainLoop *loop;
75 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040076 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040077 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040078 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040079 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040080 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040081 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040082
Kristian Høgsbergb46df052011-01-18 09:17:57 -050083 display_global_handler_t global_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084};
85
86struct window {
87 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050088 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050089 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050090 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050091 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050092 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040093 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040094 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050095 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050096 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050097 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040098 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040099 struct input *grab_device;
100 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500101 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400102 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500103
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400104 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500105 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500106
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500107 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400108 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500109 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500111 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400112 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400113
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500114 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400115 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116};
117
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400118struct input {
119 struct display *display;
120 struct wl_input_device *input_device;
121 struct window *pointer_focus;
122 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500123 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400124 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 uint32_t modifiers;
126 int32_t x, y, sx, sy;
127 struct wl_list link;
128};
129
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400130enum {
131 POINTER_DEFAULT = 100,
132 POINTER_UNSET
133};
134
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500135enum window_location {
136 WINDOW_INTERIOR = 0,
137 WINDOW_RESIZING_TOP = 1,
138 WINDOW_RESIZING_BOTTOM = 2,
139 WINDOW_RESIZING_LEFT = 4,
140 WINDOW_RESIZING_TOP_LEFT = 5,
141 WINDOW_RESIZING_BOTTOM_LEFT = 6,
142 WINDOW_RESIZING_RIGHT = 8,
143 WINDOW_RESIZING_TOP_RIGHT = 9,
144 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
145 WINDOW_RESIZING_MASK = 15,
146 WINDOW_EXTERIOR = 16,
147 WINDOW_TITLEBAR = 17,
148 WINDOW_CLIENT_AREA = 18,
149};
150
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400151const char *option_xkb_layout = "us";
152const char *option_xkb_variant = "";
153const char *option_xkb_options = "";
154
155static const GOptionEntry xkb_option_entries[] = {
156 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
157 &option_xkb_layout, "XKB Layout" },
158 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_variant, "XKB Variant" },
160 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_options, "XKB Options" },
162 { NULL }
163};
164
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500165static void
166rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
167{
168 cairo_move_to(cr, x0, y0 + radius);
169 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
170 cairo_line_to(cr, x1 - radius, y0);
171 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
172 cairo_line_to(cr, x1, y1 - radius);
173 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
174 cairo_line_to(cr, x0 + radius, y1);
175 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
176 cairo_close_path(cr);
177}
178
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400179static const cairo_user_data_key_t surface_data_key;
180struct surface_data {
181 struct wl_buffer *buffer;
182};
183
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500184#define MULT(_d,c,a,t) \
185 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
186
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500187#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400188
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500189struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400190 struct surface_data data;
191 EGLImageKHR image;
192 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800193 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500194 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400195};
196
197static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500198egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400199{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500200 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800201 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400202
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800203 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400204 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800205 cairo_device_release(d->device);
206
207 eglDestroyImageKHR(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400208 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500209 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500210 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400211}
212
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500213EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500214display_get_image_for_egl_image_surface(struct display *display,
215 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500216{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500217 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500218
219 data = cairo_surface_get_user_data (surface, &surface_data_key);
220
221 return data->image;
222}
223
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500224static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500225display_create_egl_image_surface(struct display *display,
226 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400227{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500228 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400229 EGLDisplay dpy = display->dpy;
230 cairo_surface_t *surface;
231 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400232
233 data = malloc(sizeof *data);
234 if (data == NULL)
235 return NULL;
236
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800237 data->display = display;
238
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500239 visual = wl_display_get_premultiplied_argb_visual(display->display);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500240 data->pixmap =wl_egl_pixmap_create(display->native_dpy,
241 rectangle->width,
242 rectangle->height,
243 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500244 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000245 free(data);
246 return NULL;
247 }
248
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500249 data->image = eglCreateImageKHR(dpy, NULL,
250 EGL_NATIVE_PIXMAP_KHR,
251 (EGLClientBuffer) data->pixmap, NULL);
252 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500253 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500254 free(data);
255 return NULL;
256 }
257
258 data->data.buffer =
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500259 wl_egl_pixmap_create_buffer(display->native_dpy, data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500260
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800261 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400262 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400263 glBindTexture(GL_TEXTURE_2D, data->texture);
264 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800265 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400266
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400267 surface = cairo_gl_surface_create_for_texture(display->device,
268 CAIRO_CONTENT_COLOR_ALPHA,
269 data->texture,
270 rectangle->width,
271 rectangle->height);
272
273 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500274 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275
276 return surface;
277}
278
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500279static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500280display_create_egl_image_surface_from_file(struct display *display,
281 const char *filename,
282 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400283{
284 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400285 GdkPixbuf *pixbuf;
286 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400287 int stride, i;
288 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500289 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400290
291 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400293 FALSE, &error);
294 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400295 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400296
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400297 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
298 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500299 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400300 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400301 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400302
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400303
304 stride = gdk_pixbuf_get_rowstride(pixbuf);
305 pixels = gdk_pixbuf_get_pixels(pixbuf);
306
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400307 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400308 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400309 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400310 while (p < end) {
311 unsigned int t;
312
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400313 MULT(p[0], p[0], p[3], t);
314 MULT(p[1], p[1], p[3], t);
315 MULT(p[2], p[2], p[3], t);
316 p += 4;
317
318 }
319 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400320
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 surface = display_create_egl_image_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000322 if (surface == NULL) {
323 g_object_unref(pixbuf);
324 return NULL;
325 }
326
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327 data = cairo_surface_get_user_data(surface, &surface_data_key);
328
329 cairo_device_acquire(display->device);
330 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800331 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
332 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800333 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400334
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500335 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400336
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337 return surface;
338}
339
340#endif
341
342struct wl_buffer *
343display_get_buffer_for_surface(struct display *display,
344 cairo_surface_t *surface)
345{
346 struct surface_data *data;
347
348 data = cairo_surface_get_user_data (surface, &surface_data_key);
349
350 return data->buffer;
351}
352
353struct shm_surface_data {
354 struct surface_data data;
355 void *map;
356 size_t length;
357};
358
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500359static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360shm_surface_data_destroy(void *p)
361{
362 struct shm_surface_data *data = p;
363
364 wl_buffer_destroy(data->data.buffer);
365 munmap(data->map, data->length);
366}
367
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500368static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369display_create_shm_surface(struct display *display,
370 struct rectangle *rectangle)
371{
372 struct shm_surface_data *data;
373 cairo_surface_t *surface;
374 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800375 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400376 char filename[] = "/tmp/wayland-shm-XXXXXX";
377
378 data = malloc(sizeof *data);
379 if (data == NULL)
380 return NULL;
381
382 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
383 rectangle->width);
384 data->length = stride * rectangle->height;
385 fd = mkstemp(filename);
386 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000387 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388 return NULL;
389 }
390 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000391 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 close(fd);
393 return NULL;
394 }
395
396 data->map = mmap(NULL, data->length,
397 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
398 unlink(filename);
399
400 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000401 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402 close(fd);
403 return NULL;
404 }
405
406 surface = cairo_image_surface_create_for_data (data->map,
407 CAIRO_FORMAT_ARGB32,
408 rectangle->width,
409 rectangle->height,
410 stride);
411
412 cairo_surface_set_user_data (surface, &surface_data_key,
413 data, shm_surface_data_destroy);
414
415 visual = wl_display_get_premultiplied_argb_visual(display->display);
416 data->data.buffer = wl_shm_create_buffer(display->shm,
417 fd,
418 rectangle->width,
419 rectangle->height,
420 stride, visual);
421
422 close(fd);
423
424 return surface;
425}
426
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500427static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428display_create_shm_surface_from_file(struct display *display,
429 const char *filename,
430 struct rectangle *rect)
431{
432 cairo_surface_t *surface;
433 GdkPixbuf *pixbuf;
434 GError *error = NULL;
435 int stride, i;
436 unsigned char *pixels, *p, *end, *dest_data;
437 int dest_stride;
438 uint32_t *d;
439
440 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
441 rect->width, rect->height,
442 FALSE, &error);
443 if (error != NULL)
444 return NULL;
445
446 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
447 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500448 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400449 return NULL;
450 }
451
452 stride = gdk_pixbuf_get_rowstride(pixbuf);
453 pixels = gdk_pixbuf_get_pixels(pixbuf);
454
455 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000456 if (surface == NULL) {
457 g_object_unref(pixbuf);
458 return NULL;
459 }
460
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461 dest_data = cairo_image_surface_get_data (surface);
462 dest_stride = cairo_image_surface_get_stride (surface);
463
464 for (i = 0; i < rect->height; i++) {
465 d = (uint32_t *) (dest_data + i * dest_stride);
466 p = pixels + i * stride;
467 end = p + rect->width * 4;
468 while (p < end) {
469 unsigned int t;
470 unsigned char a, r, g, b;
471
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472 a = p[3];
473 MULT(r, p[0], a, t);
474 MULT(g, p[1], a, t);
475 MULT(b, p[2], a, t);
476 p += 4;
477 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
478 }
479 }
480
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500481 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400482
483 return surface;
484}
485
nobled7b87cb02011-02-01 18:51:47 +0000486static int
487check_size(struct rectangle *rect)
488{
489 if (rect->width && rect->height)
490 return 0;
491
492 fprintf(stderr, "tried to create surface of "
493 "width: %d, height: %d\n", rect->width, rect->height);
494 return -1;
495}
496
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497cairo_surface_t *
498display_create_surface(struct display *display,
499 struct rectangle *rectangle)
500{
nobled7b87cb02011-02-01 18:51:47 +0000501 if (check_size(rectangle) < 0)
502 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500503#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500504 if (display->dpy) {
505 return display_create_egl_image_surface(display, rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500506 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500508 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509}
510
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500511static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400512display_create_surface_from_file(struct display *display,
513 const char *filename,
514 struct rectangle *rectangle)
515{
nobled7b87cb02011-02-01 18:51:47 +0000516 if (check_size(rectangle) < 0)
517 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500518#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500519 if (display->dpy) {
520 return display_create_egl_image_surface_from_file(display,
521 filename,
522 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500523 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500525 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400526}
Yuval Fledel45568f62010-12-06 09:18:12 -0500527 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400528 const char *filename;
529 int hotspot_x, hotspot_y;
530} pointer_images[] = {
531 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
532 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
533 { DATADIR "/wayland/bottom_side.png", 16, 20 },
534 { DATADIR "/wayland/grabbing.png", 20, 17 },
535 { DATADIR "/wayland/left_ptr.png", 10, 5 },
536 { DATADIR "/wayland/left_side.png", 10, 20 },
537 { DATADIR "/wayland/right_side.png", 30, 19 },
538 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
539 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
540 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400541 { DATADIR "/wayland/xterm.png", 15, 15 },
542 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400543};
544
545static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400546create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400547{
548 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400549 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400550 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400551
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400552 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400553 display->pointer_surfaces =
554 malloc(count * sizeof *display->pointer_surfaces);
555 rect.width = width;
556 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400557 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400558 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559 display_create_surface_from_file(display,
560 pointer_images[i].filename,
561 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400562 }
563
564}
565
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400566cairo_surface_t *
567display_get_pointer_surface(struct display *display, int pointer,
568 int *width, int *height,
569 int *hotspot_x, int *hotspot_y)
570{
571 cairo_surface_t *surface;
572
573 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500574#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400575 *width = cairo_gl_surface_get_width(surface);
576 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000577#else
578 *width = cairo_image_surface_get_width(surface);
579 *height = cairo_image_surface_get_height(surface);
580#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400581 *hotspot_x = pointer_images[pointer].hotspot_x;
582 *hotspot_y = pointer_images[pointer].hotspot_y;
583
584 return cairo_surface_reference(surface);
585}
586
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400587
588static void
589window_attach_surface(struct window *window);
590
591static void
592free_surface(void *data)
593{
594 struct window *window = data;
595
596 cairo_surface_destroy(window->pending_surface);
597 window->pending_surface = NULL;
598 if (window->cairo_surface)
599 window_attach_surface(window);
600}
601
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500602static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500603window_attach_surface(struct window *window)
604{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400605 struct display *display = window->display;
606 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500607 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500608
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500609 if (window->pending_surface != NULL)
610 return;
611
Kristian Høgsberg09531622010-06-14 23:22:15 -0400612 window->pending_surface = window->cairo_surface;
613 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500614
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400615 buffer = display_get_buffer_for_surface(display,
616 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500617 if (window->resize_edges & WINDOW_RESIZING_LEFT)
618 x = window->server_allocation.width -
619 window->allocation.width;
620 else
621 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500622
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500623 if (window->resize_edges & WINDOW_RESIZING_TOP)
624 y = window->server_allocation.height -
625 window->allocation.height;
626 else
627 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400628
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500629 window->server_allocation = window->allocation;
630 window->resize_edges = 0;
631 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400632 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500633
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500634 if (window->fullscreen)
635 wl_surface_map_fullscreen(window->surface);
636 else if (!window->parent)
637 wl_surface_map_toplevel(window->surface);
638 else
639 wl_surface_map_transient(window->surface,
640 window->parent->surface,
641 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500642
643 wl_surface_damage(window->surface, 0, 0,
644 window->allocation.width,
645 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500646}
647
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500648void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400649window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500650{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400651 if (window->cairo_surface)
652 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500653}
654
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400655void
656window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500658 cairo_surface_reference(surface);
659
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400660 if (window->cairo_surface != NULL)
661 cairo_surface_destroy(window->cairo_surface);
662
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500663 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400664}
665
666void
667window_create_surface(struct window *window)
668{
669 cairo_surface_t *surface;
670
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500672#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500673 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400674 surface = display_create_surface(window->display,
675 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676 break;
677#endif
678 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400679 surface = display_create_shm_surface(window->display,
680 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800682 default:
683 surface = NULL;
684 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400685 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400686
687 window_set_surface(window, surface);
688 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689}
690
691static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500692window_draw_menu(struct window *window)
693{
694 cairo_t *cr;
695 int width, height, r = 5;
696
697 window_create_surface(window);
698
699 cr = cairo_create(window->cairo_surface);
700 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
701 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
702 cairo_paint(cr);
703
704 width = window->allocation.width;
705 height = window->allocation.height;
706 rounded_rect(cr, r, r, width - r, height - r, r);
707 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
708 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
709 cairo_fill(cr);
710 cairo_destroy(cr);
711}
712
713static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500714window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500715{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500716 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500717 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400718 cairo_surface_t *frame;
719 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500720
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400721 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400722
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400723 width = window->allocation.width;
724 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500725
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500726 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500727
Kristian Høgsberg09531622010-06-14 23:22:15 -0400728 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400729 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400730 cairo_paint(cr);
731
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400732 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733 tile_mask(cr, window->display->shadow,
734 shadow_dx, shadow_dy, width, height,
735 window->margin + 10 - shadow_dx,
736 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500737
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400738 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400739 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400740 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400741 frame = window->display->inactive_frame;
742
743 tile_source(cr, frame, 0, 0, width, height,
744 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500745
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500746 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
747 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500748 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400749 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400750 if (window->keyboard_device)
751 cairo_set_source_rgb(cr, 0, 0, 0);
752 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400753 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400754 cairo_show_text(cr, window->title);
755
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500756 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400757
758 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500759}
760
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400761void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500762window_destroy(struct window *window)
763{
764 wl_surface_destroy(window->surface);
765 wl_list_remove(&window->link);
766 free(window);
767}
768
769void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400770display_flush_cairo_device(struct display *display)
771{
772 cairo_device_flush (display->device);
773}
774
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500775void
776window_draw(struct window *window)
777{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500778 if (window->parent)
779 window_draw_menu(window);
780 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400781 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500782 else
783 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500784}
785
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400786cairo_surface_t *
787window_get_surface(struct window *window)
788{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400789 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400790}
791
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400792static int
793get_pointer_location(struct window *window, int32_t x, int32_t y)
794{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400795 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400796 const int grip_size = 8;
797
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500798 if (!window->decoration)
799 return WINDOW_CLIENT_AREA;
800
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400801 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400802 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400803 else if (window->margin <= x && x < window->margin + grip_size)
804 hlocation = WINDOW_RESIZING_LEFT;
805 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400806 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400807 else if (x < window->allocation.width - window->margin)
808 hlocation = WINDOW_RESIZING_RIGHT;
809 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400810 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400811
812 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400813 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400814 else if (window->margin <= y && y < window->margin + grip_size)
815 vlocation = WINDOW_RESIZING_TOP;
816 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400817 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400818 else if (y < window->allocation.height - window->margin)
819 vlocation = WINDOW_RESIZING_BOTTOM;
820 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400821 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400822
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400823 location = vlocation | hlocation;
824 if (location & WINDOW_EXTERIOR)
825 location = WINDOW_EXTERIOR;
826 if (location == WINDOW_INTERIOR && y < window->margin + 50)
827 location = WINDOW_TITLEBAR;
828 else if (location == WINDOW_INTERIOR)
829 location = WINDOW_CLIENT_AREA;
830
831 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400832}
833
834static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400835set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400836{
837 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400838 struct wl_buffer *buffer;
839 cairo_surface_t *surface;
840 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400841
842 location = get_pointer_location(input->pointer_focus,
843 input->sx, input->sy);
844 switch (location) {
845 case WINDOW_RESIZING_TOP:
846 pointer = POINTER_TOP;
847 break;
848 case WINDOW_RESIZING_BOTTOM:
849 pointer = POINTER_BOTTOM;
850 break;
851 case WINDOW_RESIZING_LEFT:
852 pointer = POINTER_LEFT;
853 break;
854 case WINDOW_RESIZING_RIGHT:
855 pointer = POINTER_RIGHT;
856 break;
857 case WINDOW_RESIZING_TOP_LEFT:
858 pointer = POINTER_TOP_LEFT;
859 break;
860 case WINDOW_RESIZING_TOP_RIGHT:
861 pointer = POINTER_TOP_RIGHT;
862 break;
863 case WINDOW_RESIZING_BOTTOM_LEFT:
864 pointer = POINTER_BOTTOM_LEFT;
865 break;
866 case WINDOW_RESIZING_BOTTOM_RIGHT:
867 pointer = POINTER_BOTTOM_RIGHT;
868 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400869 case WINDOW_EXTERIOR:
870 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400871 if (input->current_pointer_image == POINTER_DEFAULT)
872 return;
873
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400874 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400875 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400876 return;
877 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400878 break;
879 }
880
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400881 if (pointer == input->current_pointer_image)
882 return;
883
884 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400885 surface = display->pointer_surfaces[pointer];
886 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400887 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400888 pointer_images[pointer].hotspot_x,
889 pointer_images[pointer].hotspot_y);
890}
891
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500892static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500893window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400894 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500895 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500896{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400897 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400898 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500899 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400900
901 input->x = x;
902 input->y = y;
903 input->sx = sx;
904 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400905
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400906 if (window->motion_handler)
907 pointer = (*window->motion_handler)(window, input, time,
908 x, y, sx, sy,
909 window->user_data);
910
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400911 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500912}
913
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400914static void
915window_handle_button(void *data,
916 struct wl_input_device *input_device,
917 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500918{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400919 struct input *input = data;
920 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400921 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400922
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400923 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500924
925 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400926 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400927 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400928 wl_shell_move(window->display->shell,
929 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500930 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400931 case WINDOW_RESIZING_TOP:
932 case WINDOW_RESIZING_BOTTOM:
933 case WINDOW_RESIZING_LEFT:
934 case WINDOW_RESIZING_RIGHT:
935 case WINDOW_RESIZING_TOP_LEFT:
936 case WINDOW_RESIZING_TOP_RIGHT:
937 case WINDOW_RESIZING_BOTTOM_LEFT:
938 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400939 wl_shell_resize(window->display->shell,
940 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400941 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500942 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400943 case WINDOW_CLIENT_AREA:
944 if (window->button_handler)
945 (*window->button_handler)(window,
946 input, time,
947 button, state,
948 window->user_data);
949 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500950 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400951 } else {
952 if (window->button_handler)
953 (*window->button_handler)(window,
954 input, time,
955 button, state,
956 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500957 }
958}
959
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500960static void
961window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400962 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500963{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400964 struct input *input = data;
965 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400966 struct display *d = window->display;
967 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500968
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400969 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400970 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500971 return;
972
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400973 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500974 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400975 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
976 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500977
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400978 sym = XkbKeySymEntry(d->xkb, code, level, 0);
979
980 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400981 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400982 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400983 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500984
985 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500986 (*window->key_handler)(window, input, time, key, sym, state,
987 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500988}
989
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500990static void
991window_handle_pointer_focus(void *data,
992 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400993 uint32_t time, struct wl_surface *surface,
994 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500995{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400996 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400997 struct window *window;
998 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001000 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001001 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001002 window = input->pointer_focus;
1003
Kristian Høgsberg59826582011-01-20 11:56:57 -05001004 input->x = x;
1005 input->y = y;
1006 input->sx = sx;
1007 input->sy = sy;
1008
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001009 pointer = POINTER_LEFT_PTR;
1010 if (window->motion_handler)
1011 pointer = (*window->motion_handler)(window,
1012 input, time,
1013 x, y, sx, sy,
1014 window->user_data);
1015
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001016 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001017 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001018 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001019 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001020 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001021}
1022
1023static void
1024window_handle_keyboard_focus(void *data,
1025 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001026 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001027 struct wl_surface *surface,
1028 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001029{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001030 struct input *input = data;
1031 struct window *window = input->keyboard_focus;
1032 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001033 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001034
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001035 window = input->keyboard_focus;
1036 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001037 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001038 if (window->keyboard_focus_handler)
1039 (*window->keyboard_focus_handler)(window, NULL,
1040 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001041 }
1042
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001043 if (surface)
1044 input->keyboard_focus = wl_surface_get_user_data(surface);
1045 else
1046 input->keyboard_focus = NULL;
1047
1048 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001049 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001050 for (k = keys->data; k < end; k++)
1051 input->modifiers |= d->xkb->map->modmap[*k];
1052
1053 window = input->keyboard_focus;
1054 if (window) {
1055 window->keyboard_device = input;
1056 if (window->keyboard_focus_handler)
1057 (*window->keyboard_focus_handler)(window,
1058 window->keyboard_device,
1059 window->user_data);
1060 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001061}
1062
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001063static const struct wl_input_device_listener input_device_listener = {
1064 window_handle_motion,
1065 window_handle_button,
1066 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001067 window_handle_pointer_focus,
1068 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001069};
1070
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001071void
1072input_get_position(struct input *input, int32_t *x, int32_t *y)
1073{
1074 *x = input->sx;
1075 *y = input->sy;
1076}
1077
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001078struct wl_input_device *
1079input_get_input_device(struct input *input)
1080{
1081 return input->input_device;
1082}
1083
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001084uint32_t
1085input_get_modifiers(struct input *input)
1086{
1087 return input->modifiers;
1088}
1089
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001090struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001091window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001092{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001093 cairo_device_flush (window->display->device);
1094
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001095 return wl_shell_create_drag(window->display->shell);
1096}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001097
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001098void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001099window_move(struct window *window, struct input *input, uint32_t time)
1100{
1101 wl_shell_move(window->display->shell,
1102 window->surface, input->input_device, time);
1103}
1104
1105void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001106window_activate_drag(struct wl_drag *drag, struct window *window,
1107 struct input *input, uint32_t time)
1108{
1109 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110}
1111
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001112static void
1113handle_configure(void *data, struct wl_shell *shell,
1114 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001115 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001116{
1117 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001118 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001119
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001120 /* FIXME: this is probably the wrong place to check for width
1121 * or height <= 0, but it prevents the compositor from crashing
1122 */
1123 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001124 return;
1125
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001126 window->resize_edges = edges;
1127
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001128 if (window->resize_handler) {
1129 child_width = width - 20 - window->margin * 2;
1130 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001131
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001132 (*window->resize_handler)(window,
1133 child_width, child_height,
1134 window->user_data);
1135 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001136 window->allocation.width = width;
1137 window->allocation.height = height;
1138
1139 if (window->redraw_handler)
1140 window_schedule_redraw(window);
1141 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001142}
1143
1144static const struct wl_shell_listener shell_listener = {
1145 handle_configure,
1146};
1147
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001148void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001149window_get_child_allocation(struct window *window,
1150 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001151{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001152 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001153 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001154 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001155 allocation->x = window->margin + 10;
1156 allocation->y = window->margin + 50;
1157 allocation->width =
1158 window->allocation.width - 20 - window->margin * 2;
1159 allocation->height =
1160 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001161 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001162}
1163
1164void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001165window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001166{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001167 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001168 window->allocation.x = 20 + window->margin;
1169 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001170 window->allocation.width = width + 20 + window->margin * 2;
1171 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001172 } else {
1173 window->allocation.x = 0;
1174 window->allocation.y = 0;
1175 window->allocation.width = width;
1176 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001177 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001178}
1179
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001180static gboolean
1181idle_redraw(void *data)
1182{
1183 struct window *window = data;
1184
1185 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001186
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001187 window->redraw_scheduled = 0;
1188
1189 return FALSE;
1190}
1191
1192void
1193window_schedule_redraw(struct window *window)
1194{
1195 if (!window->redraw_scheduled) {
1196 g_idle_add(idle_redraw, window);
1197 window->redraw_scheduled = 1;
1198 }
1199}
1200
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001201void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001202window_set_fullscreen(struct window *window, int fullscreen)
1203{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001204 int32_t width, height;
1205
1206 if (window->fullscreen == fullscreen)
1207 return;
1208
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001209 window->fullscreen = fullscreen;
1210 if (window->fullscreen) {
1211 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001212 width = window->display->screen_allocation.width;
1213 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001214 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001215 width = window->saved_allocation.width - 20 - window->margin * 2;
1216 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001217 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001218
1219 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001220}
1221
1222void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001223window_set_decoration(struct window *window, int decoration)
1224{
1225 window->decoration = decoration;
1226}
1227
1228void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001229window_set_user_data(struct window *window, void *data)
1230{
1231 window->user_data = data;
1232}
1233
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001234void *
1235window_get_user_data(struct window *window)
1236{
1237 return window->user_data;
1238}
1239
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001240void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001241window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001242 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001243{
1244 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001245}
1246
1247void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001248window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001249 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001250{
1251 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001252}
1253
1254void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001255window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001256 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001257{
1258 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001259}
1260
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001261void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262window_set_button_handler(struct window *window,
1263 window_button_handler_t handler)
1264{
1265 window->button_handler = handler;
1266}
1267
1268void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269window_set_motion_handler(struct window *window,
1270 window_motion_handler_t handler)
1271{
1272 window->motion_handler = handler;
1273}
1274
1275void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001276window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001277 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001278{
1279 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001280}
1281
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001282void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001283window_set_title(struct window *window, const char *title)
1284{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001285 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001286 window->title = strdup(title);
1287}
1288
1289const char *
1290window_get_title(struct window *window)
1291{
1292 return window->title;
1293}
1294
1295void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001296window_damage(struct window *window, int32_t x, int32_t y,
1297 int32_t width, int32_t height)
1298{
1299 wl_surface_damage(window->surface, x, y, width, height);
1300}
1301
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001302static struct window *
1303window_create_internal(struct display *display, struct window *parent,
1304 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001305{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001306 struct window *window;
1307
1308 window = malloc(sizeof *window);
1309 if (window == NULL)
1310 return NULL;
1311
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001312 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001313 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001314 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001315 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001316 window->allocation.x = 0;
1317 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001318 window->allocation.width = width;
1319 window->allocation.height = height;
1320 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001321 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001322 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001323
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001324 if (display->dpy)
1325 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_IMAGE;
Yuval Fledel45568f62010-12-06 09:18:12 -05001326 else
1327 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001328
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001329 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001330 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001331
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001332 return window;
1333}
1334
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001335struct window *
1336window_create(struct display *display, int32_t width, int32_t height)
1337{
1338 struct window *window;
1339
1340 window = window_create_internal(display, NULL, width, height);
1341 if (!window)
1342 return NULL;
1343
1344 return window;
1345}
1346
1347struct window *
1348window_create_transient(struct display *display, struct window *parent,
1349 int32_t x, int32_t y, int32_t width, int32_t height)
1350{
1351 struct window *window;
1352
1353 window = window_create_internal(parent->display,
1354 parent, width, height);
1355 if (!window)
1356 return NULL;
1357
1358 window->x = x;
1359 window->y = y;
1360
1361 return window;
1362}
1363
1364void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001365window_set_buffer_type(struct window *window, enum window_buffer_type type)
1366{
1367 window->buffer_type = type;
1368}
1369
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001370static void
1371display_handle_geometry(void *data,
1372 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001373 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001374{
1375 struct display *display = data;
1376
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001377 display->screen_allocation.x = x;
1378 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001379 display->screen_allocation.width = width;
1380 display->screen_allocation.height = height;
1381}
1382
1383static const struct wl_output_listener output_listener = {
1384 display_handle_geometry,
1385};
1386
1387static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001388display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001389{
1390 struct input *input;
1391
1392 input = malloc(sizeof *input);
1393 if (input == NULL)
1394 return;
1395
1396 memset(input, 0, sizeof *input);
1397 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001398 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001399 input->pointer_focus = NULL;
1400 input->keyboard_focus = NULL;
1401 wl_list_insert(d->input_list.prev, &input->link);
1402
1403 wl_input_device_add_listener(input->input_device,
1404 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001405 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001406}
1407
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001408struct selection_offer {
1409 struct display *display;
1410 struct wl_selection_offer *offer;
1411 struct wl_array types;
1412 struct input *input;
1413};
1414
1415int
1416input_offers_mime_type(struct input *input, const char *type)
1417{
1418 struct selection_offer *offer = input->offer;
1419 char **p, **end;
1420
1421 if (offer == NULL)
1422 return 0;
1423
1424 end = offer->types.data + offer->types.size;
1425 for (p = offer->types.data; p < end; p++)
1426 if (strcmp(*p, type) == 0)
1427 return 1;
1428
1429 return 0;
1430}
1431
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001432void
1433input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001434{
1435 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001436
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001437 /* FIXME: A number of things can go wrong here: the object may
1438 * not be the current selection offer any more (which could
1439 * still work, but the source may have gone away or just
1440 * destroyed its wl_selection) or the offer may not have the
1441 * requested type after all (programmer/client error,
1442 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001443 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001444}
1445
1446static void
1447selection_offer_offer(void *data,
1448 struct wl_selection_offer *selection_offer,
1449 const char *type)
1450{
1451 struct selection_offer *offer = data;
1452
1453 char **p;
1454
1455 p = wl_array_add(&offer->types, sizeof *p);
1456 if (p)
1457 *p = strdup(type);
1458};
1459
1460static void
1461selection_offer_keyboard_focus(void *data,
1462 struct wl_selection_offer *selection_offer,
1463 struct wl_input_device *input_device)
1464{
1465 struct selection_offer *offer = data;
1466 struct input *input;
1467 char **p, **end;
1468
1469 if (input_device == NULL) {
1470 printf("selection offer retracted %p\n", selection_offer);
1471 input = offer->input;
1472 input->offer = NULL;
1473 wl_selection_offer_destroy(selection_offer);
1474 wl_array_release(&offer->types);
1475 free(offer);
1476 return;
1477 }
1478
1479 input = wl_input_device_get_user_data(input_device);
1480 printf("new selection offer %p:", selection_offer);
1481
1482 offer->input = input;
1483 input->offer = offer;
1484 end = offer->types.data + offer->types.size;
1485 for (p = offer->types.data; p < end; p++)
1486 printf(" %s", *p);
1487
1488 printf("\n");
1489}
1490
1491struct wl_selection_offer_listener selection_offer_listener = {
1492 selection_offer_offer,
1493 selection_offer_keyboard_focus
1494};
1495
1496static void
1497add_selection_offer(struct display *d, uint32_t id)
1498{
1499 struct selection_offer *offer;
1500
1501 offer = malloc(sizeof *offer);
1502 if (offer == NULL)
1503 return;
1504
1505 offer->offer = wl_selection_offer_create(d->display, id);
1506 offer->display = d;
1507 wl_array_init(&offer->types);
1508 offer->input = NULL;
1509
1510 wl_selection_offer_add_listener(offer->offer,
1511 &selection_offer_listener, offer);
1512}
1513
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001514static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001515display_handle_global(struct wl_display *display, uint32_t id,
1516 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001517{
1518 struct display *d = data;
1519
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001520 if (strcmp(interface, "compositor") == 0) {
1521 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001522 } else if (strcmp(interface, "output") == 0) {
1523 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001524 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001525 } else if (strcmp(interface, "input_device") == 0) {
1526 display_add_input(d, id);
1527 } else if (strcmp(interface, "shell") == 0) {
1528 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001529 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001530 } else if (strcmp(interface, "shm") == 0) {
1531 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001532 } else if (strcmp(interface, "selection_offer") == 0) {
1533 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001534 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001535 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001536 }
1537}
1538
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001539static void
1540display_render_frame(struct display *d)
1541{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001542 int radius = 8;
1543 cairo_t *cr;
1544
1545 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1546 cr = cairo_create(d->shadow);
1547 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1548 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1549 rounded_rect(cr, 16, 16, 112, 112, radius);
1550 cairo_fill(cr);
1551 cairo_destroy(cr);
1552 blur_surface(d->shadow, 64);
1553
1554 d->active_frame =
1555 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1556 cr = cairo_create(d->active_frame);
1557 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1558 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1559 rounded_rect(cr, 16, 16, 112, 112, radius);
1560 cairo_fill(cr);
1561 cairo_destroy(cr);
1562
1563 d->inactive_frame =
1564 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1565 cr = cairo_create(d->inactive_frame);
1566 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1567 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1568 rounded_rect(cr, 16, 16, 112, 112, radius);
1569 cairo_fill(cr);
1570 cairo_destroy(cr);
1571}
1572
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001573static void
1574init_xkb(struct display *d)
1575{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001576 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001577
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001578 names.rules = "evdev";
1579 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001580 names.layout = option_xkb_layout;
1581 names.variant = option_xkb_variant;
1582 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001583
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001584 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001585 if (!d->xkb) {
1586 fprintf(stderr, "Failed to compile keymap\n");
1587 exit(1);
1588 }
1589}
1590
Yuval Fledel45568f62010-12-06 09:18:12 -05001591static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001592init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001593{
1594 EGLint major, minor;
Yuval Fledel45568f62010-12-06 09:18:12 -05001595
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -05001596 d->dpy = eglGetDisplay(d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001597 if (!eglInitialize(d->dpy, &major, &minor)) {
1598 fprintf(stderr, "failed to initialize display\n");
1599 return -1;
1600 }
1601
1602 if (!eglBindAPI(EGL_OPENGL_API)) {
1603 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1604 return -1;
1605 }
1606
1607 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1608 if (d->ctx == NULL) {
1609 fprintf(stderr, "failed to create context\n");
1610 return -1;
1611 }
1612
1613 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1614 fprintf(stderr, "faile to make context current\n");
1615 return -1;
1616 }
1617
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001618#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001619 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1620 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001621 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001622 return -1;
1623 }
1624#endif
1625
1626 return 0;
1627}
1628
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001629struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001630display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001631{
1632 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001633 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001634 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001635 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001636
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001637 g_type_init();
1638
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001639 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001640 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001641 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001642
1643 xkb_option_group = g_option_group_new("xkb",
1644 "Keyboard options",
1645 "Show all XKB options",
1646 NULL, NULL);
1647 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1648 g_option_context_add_group (context, xkb_option_group);
1649
1650 if (!g_option_context_parse(context, argc, argv, &error)) {
1651 fprintf(stderr, "option parsing failed: %s\n", error->message);
1652 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001653 }
1654
Tim Wiederhake748f6722011-01-23 23:25:25 +01001655 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001656
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001657 d = malloc(sizeof *d);
1658 if (d == NULL)
1659 return NULL;
1660
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001661 memset(d, 0, sizeof *d);
1662
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001663 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001664 if (d->display == NULL) {
1665 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001666 return NULL;
1667 }
1668
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001669 wl_list_init(&d->input_list);
1670
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001671 /* Set up listener so we'll catch all events. */
1672 wl_display_add_global_listener(d->display,
1673 display_handle_global, d);
1674
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -05001675 d->native_dpy = wl_egl_display_create(d->display);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001676
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001677 /* Process connection events. */
1678 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1679
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001680 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001681 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001682
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001683 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001684
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001685 display_render_frame(d);
1686
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001687 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001688 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001689 g_source_attach(d->source, NULL);
1690
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001691 wl_list_init(&d->window_list);
1692
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001693 init_xkb(d);
1694
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001695 return d;
1696}
1697
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001698struct wl_display *
1699display_get_display(struct display *display)
1700{
1701 return display->display;
1702}
1703
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001704struct wl_compositor *
1705display_get_compositor(struct display *display)
1706{
1707 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001708}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001709
1710EGLDisplay
1711display_get_egl_display(struct display *d)
1712{
1713 return d->dpy;
1714}
1715
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001716struct wl_shell *
1717display_get_shell(struct display *display)
1718{
1719 return display->shell;
1720}
1721
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001722void
1723display_run(struct display *d)
1724{
1725 g_main_loop_run(d->loop);
1726}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001727
1728void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001729display_set_global_handler(struct display *display,
1730 display_global_handler_t handler)
1731{
1732 display->global_handler = handler;
1733}