blob: 37afcc5fae29f644ce15b856baea8190142c6eaf [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
39#define EGL_EGLEXT_PROTOTYPES 1
40#define GL_GLEXT_PROTOTYPES 1
41#include <GL/gl.h>
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040044
Kristian Høgsberg8def2642011-01-14 17:41:33 -050045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040046#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040049#include <X11/extensions/XKBcommon.h>
50
Kristian Høgsberg297c6312011-02-04 14:11:33 -050051#include <wayland-egl.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øgsberg297c6312011-02-04 14:11:33 -0500209 wl_egl_native_pixmap_destroy(data->pixmap);
210 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);
240 data->pixmap =wl_egl_native_pixmap_create(display->native_dpy,
241 rectangle->width,
242 rectangle->height,
243 visual, 0);
244 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) {
253 wl_egl_native_pixmap_destroy(data->pixmap);
254 free(data);
255 return NULL;
256 }
257
258 data->data.buffer =
259 wl_egl_native_pixmap_create_buffer(display->native_dpy,
260 data->pixmap);
261
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800262 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400263 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400264 glBindTexture(GL_TEXTURE_2D, data->texture);
265 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800266 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400267
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400268 surface = cairo_gl_surface_create_for_texture(display->device,
269 CAIRO_CONTENT_COLOR_ALPHA,
270 data->texture,
271 rectangle->width,
272 rectangle->height);
273
274 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400276
277 return surface;
278}
279
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500280static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500281display_create_egl_image_surface_from_file(struct display *display,
282 const char *filename,
283 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400284{
285 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400286 GdkPixbuf *pixbuf;
287 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400288 int stride, i;
289 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500290 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400291
292 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400293 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400294 FALSE, &error);
295 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400296 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400297
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400298 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
299 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500300 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400302 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400303
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400304
305 stride = gdk_pixbuf_get_rowstride(pixbuf);
306 pixels = gdk_pixbuf_get_pixels(pixbuf);
307
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400308 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400309 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400311 while (p < end) {
312 unsigned int t;
313
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400314 MULT(p[0], p[0], p[3], t);
315 MULT(p[1], p[1], p[3], t);
316 MULT(p[2], p[2], p[3], t);
317 p += 4;
318
319 }
320 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 surface = display_create_egl_image_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000323 if (surface == NULL) {
324 g_object_unref(pixbuf);
325 return NULL;
326 }
327
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800328 data = cairo_surface_get_user_data(surface, &surface_data_key);
329
330 cairo_device_acquire(display->device);
331 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800332 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
333 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800334 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400335
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500336 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400337
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400338 return surface;
339}
340
341#endif
342
343struct wl_buffer *
344display_get_buffer_for_surface(struct display *display,
345 cairo_surface_t *surface)
346{
347 struct surface_data *data;
348
349 data = cairo_surface_get_user_data (surface, &surface_data_key);
350
351 return data->buffer;
352}
353
354struct shm_surface_data {
355 struct surface_data data;
356 void *map;
357 size_t length;
358};
359
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500360static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361shm_surface_data_destroy(void *p)
362{
363 struct shm_surface_data *data = p;
364
365 wl_buffer_destroy(data->data.buffer);
366 munmap(data->map, data->length);
367}
368
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500369static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400370display_create_shm_surface(struct display *display,
371 struct rectangle *rectangle)
372{
373 struct shm_surface_data *data;
374 cairo_surface_t *surface;
375 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800376 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400377 char filename[] = "/tmp/wayland-shm-XXXXXX";
378
379 data = malloc(sizeof *data);
380 if (data == NULL)
381 return NULL;
382
383 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
384 rectangle->width);
385 data->length = stride * rectangle->height;
386 fd = mkstemp(filename);
387 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000388 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389 return NULL;
390 }
391 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000392 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 close(fd);
394 return NULL;
395 }
396
397 data->map = mmap(NULL, data->length,
398 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
399 unlink(filename);
400
401 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000402 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403 close(fd);
404 return NULL;
405 }
406
407 surface = cairo_image_surface_create_for_data (data->map,
408 CAIRO_FORMAT_ARGB32,
409 rectangle->width,
410 rectangle->height,
411 stride);
412
413 cairo_surface_set_user_data (surface, &surface_data_key,
414 data, shm_surface_data_destroy);
415
416 visual = wl_display_get_premultiplied_argb_visual(display->display);
417 data->data.buffer = wl_shm_create_buffer(display->shm,
418 fd,
419 rectangle->width,
420 rectangle->height,
421 stride, visual);
422
423 close(fd);
424
425 return surface;
426}
427
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500428static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429display_create_shm_surface_from_file(struct display *display,
430 const char *filename,
431 struct rectangle *rect)
432{
433 cairo_surface_t *surface;
434 GdkPixbuf *pixbuf;
435 GError *error = NULL;
436 int stride, i;
437 unsigned char *pixels, *p, *end, *dest_data;
438 int dest_stride;
439 uint32_t *d;
440
441 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
442 rect->width, rect->height,
443 FALSE, &error);
444 if (error != NULL)
445 return NULL;
446
447 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
448 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500449 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400450 return NULL;
451 }
452
453 stride = gdk_pixbuf_get_rowstride(pixbuf);
454 pixels = gdk_pixbuf_get_pixels(pixbuf);
455
456 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000457 if (surface == NULL) {
458 g_object_unref(pixbuf);
459 return NULL;
460 }
461
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400462 dest_data = cairo_image_surface_get_data (surface);
463 dest_stride = cairo_image_surface_get_stride (surface);
464
465 for (i = 0; i < rect->height; i++) {
466 d = (uint32_t *) (dest_data + i * dest_stride);
467 p = pixels + i * stride;
468 end = p + rect->width * 4;
469 while (p < end) {
470 unsigned int t;
471 unsigned char a, r, g, b;
472
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 a = p[3];
474 MULT(r, p[0], a, t);
475 MULT(g, p[1], a, t);
476 MULT(b, p[2], a, t);
477 p += 4;
478 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
479 }
480 }
481
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500482 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400483
484 return surface;
485}
486
nobled7b87cb02011-02-01 18:51:47 +0000487static int
488check_size(struct rectangle *rect)
489{
490 if (rect->width && rect->height)
491 return 0;
492
493 fprintf(stderr, "tried to create surface of "
494 "width: %d, height: %d\n", rect->width, rect->height);
495 return -1;
496}
497
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498cairo_surface_t *
499display_create_surface(struct display *display,
500 struct rectangle *rectangle)
501{
nobled7b87cb02011-02-01 18:51:47 +0000502 if (check_size(rectangle) < 0)
503 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500504#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500505 if (display->dpy) {
506 return display_create_egl_image_surface(display, rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500507 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500509 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510}
511
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500512static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513display_create_surface_from_file(struct display *display,
514 const char *filename,
515 struct rectangle *rectangle)
516{
nobled7b87cb02011-02-01 18:51:47 +0000517 if (check_size(rectangle) < 0)
518 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500519#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500520 if (display->dpy) {
521 return display_create_egl_image_surface_from_file(display,
522 filename,
523 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500524 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500526 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400527}
Yuval Fledel45568f62010-12-06 09:18:12 -0500528 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400529 const char *filename;
530 int hotspot_x, hotspot_y;
531} pointer_images[] = {
532 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
533 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
534 { DATADIR "/wayland/bottom_side.png", 16, 20 },
535 { DATADIR "/wayland/grabbing.png", 20, 17 },
536 { DATADIR "/wayland/left_ptr.png", 10, 5 },
537 { DATADIR "/wayland/left_side.png", 10, 20 },
538 { DATADIR "/wayland/right_side.png", 30, 19 },
539 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
540 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
541 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400542 { DATADIR "/wayland/xterm.png", 15, 15 },
543 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400544};
545
546static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400547create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400548{
549 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400550 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400551 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400552
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400553 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400554 display->pointer_surfaces =
555 malloc(count * sizeof *display->pointer_surfaces);
556 rect.width = width;
557 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400558 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400559 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560 display_create_surface_from_file(display,
561 pointer_images[i].filename,
562 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400563 }
564
565}
566
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400567cairo_surface_t *
568display_get_pointer_surface(struct display *display, int pointer,
569 int *width, int *height,
570 int *hotspot_x, int *hotspot_y)
571{
572 cairo_surface_t *surface;
573
574 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500575#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400576 *width = cairo_gl_surface_get_width(surface);
577 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000578#else
579 *width = cairo_image_surface_get_width(surface);
580 *height = cairo_image_surface_get_height(surface);
581#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400582 *hotspot_x = pointer_images[pointer].hotspot_x;
583 *hotspot_y = pointer_images[pointer].hotspot_y;
584
585 return cairo_surface_reference(surface);
586}
587
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400588
589static void
590window_attach_surface(struct window *window);
591
592static void
593free_surface(void *data)
594{
595 struct window *window = data;
596
597 cairo_surface_destroy(window->pending_surface);
598 window->pending_surface = NULL;
599 if (window->cairo_surface)
600 window_attach_surface(window);
601}
602
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500603static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500604window_attach_surface(struct window *window)
605{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400606 struct display *display = window->display;
607 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500608 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500609
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500610 if (window->pending_surface != NULL)
611 return;
612
Kristian Høgsberg09531622010-06-14 23:22:15 -0400613 window->pending_surface = window->cairo_surface;
614 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500615
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400616 buffer = display_get_buffer_for_surface(display,
617 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500618 if (window->resize_edges & WINDOW_RESIZING_LEFT)
619 x = window->server_allocation.width -
620 window->allocation.width;
621 else
622 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500623
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500624 if (window->resize_edges & WINDOW_RESIZING_TOP)
625 y = window->server_allocation.height -
626 window->allocation.height;
627 else
628 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400629
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500630 window->server_allocation = window->allocation;
631 window->resize_edges = 0;
632 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400633 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500634
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500635 if (window->fullscreen)
636 wl_surface_map_fullscreen(window->surface);
637 else if (!window->parent)
638 wl_surface_map_toplevel(window->surface);
639 else
640 wl_surface_map_transient(window->surface,
641 window->parent->surface,
642 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500643
644 wl_surface_damage(window->surface, 0, 0,
645 window->allocation.width,
646 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500647}
648
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500649void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400650window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500651{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400652 if (window->cairo_surface)
653 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500654}
655
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400656void
657window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500659 cairo_surface_reference(surface);
660
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400661 if (window->cairo_surface != NULL)
662 cairo_surface_destroy(window->cairo_surface);
663
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500664 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400665}
666
667void
668window_create_surface(struct window *window)
669{
670 cairo_surface_t *surface;
671
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400672 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500673#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500674 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400675 surface = display_create_surface(window->display,
676 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400677 break;
678#endif
679 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400680 surface = display_create_shm_surface(window->display,
681 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800683 default:
684 surface = NULL;
685 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400686 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400687
688 window_set_surface(window, surface);
689 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690}
691
692static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500693window_draw_menu(struct window *window)
694{
695 cairo_t *cr;
696 int width, height, r = 5;
697
698 window_create_surface(window);
699
700 cr = cairo_create(window->cairo_surface);
701 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
702 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
703 cairo_paint(cr);
704
705 width = window->allocation.width;
706 height = window->allocation.height;
707 rounded_rect(cr, r, r, width - r, height - r, r);
708 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
709 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
710 cairo_fill(cr);
711 cairo_destroy(cr);
712}
713
714static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500715window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500716{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500717 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500718 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400719 cairo_surface_t *frame;
720 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500721
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400722 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400723
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400724 width = window->allocation.width;
725 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500726
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500727 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500728
Kristian Høgsberg09531622010-06-14 23:22:15 -0400729 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400730 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400731 cairo_paint(cr);
732
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400733 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 tile_mask(cr, window->display->shadow,
735 shadow_dx, shadow_dy, width, height,
736 window->margin + 10 - shadow_dx,
737 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500738
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400739 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400740 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400741 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 frame = window->display->inactive_frame;
743
744 tile_source(cr, frame, 0, 0, width, height,
745 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500746
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500747 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
748 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500749 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400750 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400751 if (window->keyboard_device)
752 cairo_set_source_rgb(cr, 0, 0, 0);
753 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400754 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400755 cairo_show_text(cr, window->title);
756
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500757 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400758
759 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500760}
761
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400762void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500763window_destroy(struct window *window)
764{
765 wl_surface_destroy(window->surface);
766 wl_list_remove(&window->link);
767 free(window);
768}
769
770void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400771display_flush_cairo_device(struct display *display)
772{
773 cairo_device_flush (display->device);
774}
775
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500776void
777window_draw(struct window *window)
778{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500779 if (window->parent)
780 window_draw_menu(window);
781 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400782 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500783 else
784 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500785}
786
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400787cairo_surface_t *
788window_get_surface(struct window *window)
789{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400790 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400791}
792
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400793static int
794get_pointer_location(struct window *window, int32_t x, int32_t y)
795{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400796 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400797 const int grip_size = 8;
798
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500799 if (!window->decoration)
800 return WINDOW_CLIENT_AREA;
801
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400802 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400803 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400804 else if (window->margin <= x && x < window->margin + grip_size)
805 hlocation = WINDOW_RESIZING_LEFT;
806 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400807 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400808 else if (x < window->allocation.width - window->margin)
809 hlocation = WINDOW_RESIZING_RIGHT;
810 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400811 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400812
813 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400814 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400815 else if (window->margin <= y && y < window->margin + grip_size)
816 vlocation = WINDOW_RESIZING_TOP;
817 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400818 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400819 else if (y < window->allocation.height - window->margin)
820 vlocation = WINDOW_RESIZING_BOTTOM;
821 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400822 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400823
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400824 location = vlocation | hlocation;
825 if (location & WINDOW_EXTERIOR)
826 location = WINDOW_EXTERIOR;
827 if (location == WINDOW_INTERIOR && y < window->margin + 50)
828 location = WINDOW_TITLEBAR;
829 else if (location == WINDOW_INTERIOR)
830 location = WINDOW_CLIENT_AREA;
831
832 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400833}
834
835static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400836set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400837{
838 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400839 struct wl_buffer *buffer;
840 cairo_surface_t *surface;
841 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400842
843 location = get_pointer_location(input->pointer_focus,
844 input->sx, input->sy);
845 switch (location) {
846 case WINDOW_RESIZING_TOP:
847 pointer = POINTER_TOP;
848 break;
849 case WINDOW_RESIZING_BOTTOM:
850 pointer = POINTER_BOTTOM;
851 break;
852 case WINDOW_RESIZING_LEFT:
853 pointer = POINTER_LEFT;
854 break;
855 case WINDOW_RESIZING_RIGHT:
856 pointer = POINTER_RIGHT;
857 break;
858 case WINDOW_RESIZING_TOP_LEFT:
859 pointer = POINTER_TOP_LEFT;
860 break;
861 case WINDOW_RESIZING_TOP_RIGHT:
862 pointer = POINTER_TOP_RIGHT;
863 break;
864 case WINDOW_RESIZING_BOTTOM_LEFT:
865 pointer = POINTER_BOTTOM_LEFT;
866 break;
867 case WINDOW_RESIZING_BOTTOM_RIGHT:
868 pointer = POINTER_BOTTOM_RIGHT;
869 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400870 case WINDOW_EXTERIOR:
871 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400872 if (input->current_pointer_image == POINTER_DEFAULT)
873 return;
874
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400875 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400876 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400877 return;
878 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400879 break;
880 }
881
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400882 if (pointer == input->current_pointer_image)
883 return;
884
885 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400886 surface = display->pointer_surfaces[pointer];
887 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400888 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400889 pointer_images[pointer].hotspot_x,
890 pointer_images[pointer].hotspot_y);
891}
892
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500893static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500894window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400895 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500896 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500897{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400898 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400899 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500900 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400901
902 input->x = x;
903 input->y = y;
904 input->sx = sx;
905 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400906
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400907 if (window->motion_handler)
908 pointer = (*window->motion_handler)(window, input, time,
909 x, y, sx, sy,
910 window->user_data);
911
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400912 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500913}
914
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400915static void
916window_handle_button(void *data,
917 struct wl_input_device *input_device,
918 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500919{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400920 struct input *input = data;
921 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400922 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400923
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400924 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500925
926 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400927 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400928 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400929 wl_shell_move(window->display->shell,
930 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500931 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400932 case WINDOW_RESIZING_TOP:
933 case WINDOW_RESIZING_BOTTOM:
934 case WINDOW_RESIZING_LEFT:
935 case WINDOW_RESIZING_RIGHT:
936 case WINDOW_RESIZING_TOP_LEFT:
937 case WINDOW_RESIZING_TOP_RIGHT:
938 case WINDOW_RESIZING_BOTTOM_LEFT:
939 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400940 wl_shell_resize(window->display->shell,
941 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400942 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500943 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400944 case WINDOW_CLIENT_AREA:
945 if (window->button_handler)
946 (*window->button_handler)(window,
947 input, time,
948 button, state,
949 window->user_data);
950 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500951 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400952 } else {
953 if (window->button_handler)
954 (*window->button_handler)(window,
955 input, time,
956 button, state,
957 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500958 }
959}
960
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500961static void
962window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400963 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500964{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400965 struct input *input = data;
966 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400967 struct display *d = window->display;
968 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500969
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400970 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400971 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500972 return;
973
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400974 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500975 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400976 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
977 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500978
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400979 sym = XkbKeySymEntry(d->xkb, code, level, 0);
980
981 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400982 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400983 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400984 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500985
986 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500987 (*window->key_handler)(window, input, time, key, sym, state,
988 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500989}
990
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500991static void
992window_handle_pointer_focus(void *data,
993 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400994 uint32_t time, struct wl_surface *surface,
995 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500996{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400997 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400998 struct window *window;
999 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001000
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001001 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001002 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001003 window = input->pointer_focus;
1004
Kristian Høgsberg59826582011-01-20 11:56:57 -05001005 input->x = x;
1006 input->y = y;
1007 input->sx = sx;
1008 input->sy = sy;
1009
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001010 pointer = POINTER_LEFT_PTR;
1011 if (window->motion_handler)
1012 pointer = (*window->motion_handler)(window,
1013 input, time,
1014 x, y, sx, sy,
1015 window->user_data);
1016
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001017 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001018 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001019 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001020 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001021 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001022}
1023
1024static void
1025window_handle_keyboard_focus(void *data,
1026 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001027 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001028 struct wl_surface *surface,
1029 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001030{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001031 struct input *input = data;
1032 struct window *window = input->keyboard_focus;
1033 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001034 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001035
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001036 window = input->keyboard_focus;
1037 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001038 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001039 if (window->keyboard_focus_handler)
1040 (*window->keyboard_focus_handler)(window, NULL,
1041 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001042 }
1043
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001044 if (surface)
1045 input->keyboard_focus = wl_surface_get_user_data(surface);
1046 else
1047 input->keyboard_focus = NULL;
1048
1049 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001050 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001051 for (k = keys->data; k < end; k++)
1052 input->modifiers |= d->xkb->map->modmap[*k];
1053
1054 window = input->keyboard_focus;
1055 if (window) {
1056 window->keyboard_device = input;
1057 if (window->keyboard_focus_handler)
1058 (*window->keyboard_focus_handler)(window,
1059 window->keyboard_device,
1060 window->user_data);
1061 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001062}
1063
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001064static const struct wl_input_device_listener input_device_listener = {
1065 window_handle_motion,
1066 window_handle_button,
1067 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001068 window_handle_pointer_focus,
1069 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001070};
1071
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001072void
1073input_get_position(struct input *input, int32_t *x, int32_t *y)
1074{
1075 *x = input->sx;
1076 *y = input->sy;
1077}
1078
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001079struct wl_input_device *
1080input_get_input_device(struct input *input)
1081{
1082 return input->input_device;
1083}
1084
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001085uint32_t
1086input_get_modifiers(struct input *input)
1087{
1088 return input->modifiers;
1089}
1090
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001091struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001092window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001093{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001094 cairo_device_flush (window->display->device);
1095
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001096 return wl_shell_create_drag(window->display->shell);
1097}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001098
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001099void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001100window_move(struct window *window, struct input *input, uint32_t time)
1101{
1102 wl_shell_move(window->display->shell,
1103 window->surface, input->input_device, time);
1104}
1105
1106void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001107window_activate_drag(struct wl_drag *drag, struct window *window,
1108 struct input *input, uint32_t time)
1109{
1110 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111}
1112
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001113static void
1114handle_configure(void *data, struct wl_shell *shell,
1115 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001116 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001117{
1118 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001119 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001120
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001121 /* FIXME: this is probably the wrong place to check for width
1122 * or height <= 0, but it prevents the compositor from crashing
1123 */
1124 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001125 return;
1126
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001127 window->resize_edges = edges;
1128
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001129 if (window->resize_handler) {
1130 child_width = width - 20 - window->margin * 2;
1131 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001132
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001133 (*window->resize_handler)(window,
1134 child_width, child_height,
1135 window->user_data);
1136 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001137 window->allocation.width = width;
1138 window->allocation.height = height;
1139
1140 if (window->redraw_handler)
1141 window_schedule_redraw(window);
1142 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001143}
1144
1145static const struct wl_shell_listener shell_listener = {
1146 handle_configure,
1147};
1148
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001149void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001150window_get_child_allocation(struct window *window,
1151 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001152{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001153 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001154 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001155 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001156 allocation->x = window->margin + 10;
1157 allocation->y = window->margin + 50;
1158 allocation->width =
1159 window->allocation.width - 20 - window->margin * 2;
1160 allocation->height =
1161 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001162 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001163}
1164
1165void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001166window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001167{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001168 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001169 window->allocation.x = 20 + window->margin;
1170 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001171 window->allocation.width = width + 20 + window->margin * 2;
1172 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001173 } else {
1174 window->allocation.x = 0;
1175 window->allocation.y = 0;
1176 window->allocation.width = width;
1177 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001178 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001179}
1180
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001181static gboolean
1182idle_redraw(void *data)
1183{
1184 struct window *window = data;
1185
1186 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001187
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001188 window->redraw_scheduled = 0;
1189
1190 return FALSE;
1191}
1192
1193void
1194window_schedule_redraw(struct window *window)
1195{
1196 if (!window->redraw_scheduled) {
1197 g_idle_add(idle_redraw, window);
1198 window->redraw_scheduled = 1;
1199 }
1200}
1201
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001202void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001203window_set_fullscreen(struct window *window, int fullscreen)
1204{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001205 int32_t width, height;
1206
1207 if (window->fullscreen == fullscreen)
1208 return;
1209
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001210 window->fullscreen = fullscreen;
1211 if (window->fullscreen) {
1212 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001213 width = window->display->screen_allocation.width;
1214 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001215 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001216 width = window->saved_allocation.width - 20 - window->margin * 2;
1217 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001218 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001219
1220 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001221}
1222
1223void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001224window_set_decoration(struct window *window, int decoration)
1225{
1226 window->decoration = decoration;
1227}
1228
1229void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001230window_set_user_data(struct window *window, void *data)
1231{
1232 window->user_data = data;
1233}
1234
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001235void *
1236window_get_user_data(struct window *window)
1237{
1238 return window->user_data;
1239}
1240
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001241void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001242window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001243 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001244{
1245 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001246}
1247
1248void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001249window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001250 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001251{
1252 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001253}
1254
1255void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001256window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001257 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001258{
1259 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001260}
1261
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001262void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263window_set_button_handler(struct window *window,
1264 window_button_handler_t handler)
1265{
1266 window->button_handler = handler;
1267}
1268
1269void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001270window_set_motion_handler(struct window *window,
1271 window_motion_handler_t handler)
1272{
1273 window->motion_handler = handler;
1274}
1275
1276void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001277window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001278 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001279{
1280 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001281}
1282
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001283void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001284window_set_title(struct window *window, const char *title)
1285{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001286 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001287 window->title = strdup(title);
1288}
1289
1290const char *
1291window_get_title(struct window *window)
1292{
1293 return window->title;
1294}
1295
1296void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001297window_damage(struct window *window, int32_t x, int32_t y,
1298 int32_t width, int32_t height)
1299{
1300 wl_surface_damage(window->surface, x, y, width, height);
1301}
1302
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001303static struct window *
1304window_create_internal(struct display *display, struct window *parent,
1305 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001306{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001307 struct window *window;
1308
1309 window = malloc(sizeof *window);
1310 if (window == NULL)
1311 return NULL;
1312
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001313 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001314 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001315 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001316 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001317 window->allocation.x = 0;
1318 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001319 window->allocation.width = width;
1320 window->allocation.height = height;
1321 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001322 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001323 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001324
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001325 if (display->dpy)
1326 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_IMAGE;
Yuval Fledel45568f62010-12-06 09:18:12 -05001327 else
1328 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001329
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001330 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001331 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001332
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001333 return window;
1334}
1335
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001336struct window *
1337window_create(struct display *display, int32_t width, int32_t height)
1338{
1339 struct window *window;
1340
1341 window = window_create_internal(display, NULL, width, height);
1342 if (!window)
1343 return NULL;
1344
1345 return window;
1346}
1347
1348struct window *
1349window_create_transient(struct display *display, struct window *parent,
1350 int32_t x, int32_t y, int32_t width, int32_t height)
1351{
1352 struct window *window;
1353
1354 window = window_create_internal(parent->display,
1355 parent, width, height);
1356 if (!window)
1357 return NULL;
1358
1359 window->x = x;
1360 window->y = y;
1361
1362 return window;
1363}
1364
1365void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001366window_set_buffer_type(struct window *window, enum window_buffer_type type)
1367{
1368 window->buffer_type = type;
1369}
1370
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001371static void
1372display_handle_geometry(void *data,
1373 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001374 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001375{
1376 struct display *display = data;
1377
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001378 display->screen_allocation.x = x;
1379 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001380 display->screen_allocation.width = width;
1381 display->screen_allocation.height = height;
1382}
1383
1384static const struct wl_output_listener output_listener = {
1385 display_handle_geometry,
1386};
1387
1388static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001389display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001390{
1391 struct input *input;
1392
1393 input = malloc(sizeof *input);
1394 if (input == NULL)
1395 return;
1396
1397 memset(input, 0, sizeof *input);
1398 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001399 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001400 input->pointer_focus = NULL;
1401 input->keyboard_focus = NULL;
1402 wl_list_insert(d->input_list.prev, &input->link);
1403
1404 wl_input_device_add_listener(input->input_device,
1405 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001406 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001407}
1408
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001409struct selection_offer {
1410 struct display *display;
1411 struct wl_selection_offer *offer;
1412 struct wl_array types;
1413 struct input *input;
1414};
1415
1416int
1417input_offers_mime_type(struct input *input, const char *type)
1418{
1419 struct selection_offer *offer = input->offer;
1420 char **p, **end;
1421
1422 if (offer == NULL)
1423 return 0;
1424
1425 end = offer->types.data + offer->types.size;
1426 for (p = offer->types.data; p < end; p++)
1427 if (strcmp(*p, type) == 0)
1428 return 1;
1429
1430 return 0;
1431}
1432
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001433void
1434input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001435{
1436 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001437
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001438 /* FIXME: A number of things can go wrong here: the object may
1439 * not be the current selection offer any more (which could
1440 * still work, but the source may have gone away or just
1441 * destroyed its wl_selection) or the offer may not have the
1442 * requested type after all (programmer/client error,
1443 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001444 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001445}
1446
1447static void
1448selection_offer_offer(void *data,
1449 struct wl_selection_offer *selection_offer,
1450 const char *type)
1451{
1452 struct selection_offer *offer = data;
1453
1454 char **p;
1455
1456 p = wl_array_add(&offer->types, sizeof *p);
1457 if (p)
1458 *p = strdup(type);
1459};
1460
1461static void
1462selection_offer_keyboard_focus(void *data,
1463 struct wl_selection_offer *selection_offer,
1464 struct wl_input_device *input_device)
1465{
1466 struct selection_offer *offer = data;
1467 struct input *input;
1468 char **p, **end;
1469
1470 if (input_device == NULL) {
1471 printf("selection offer retracted %p\n", selection_offer);
1472 input = offer->input;
1473 input->offer = NULL;
1474 wl_selection_offer_destroy(selection_offer);
1475 wl_array_release(&offer->types);
1476 free(offer);
1477 return;
1478 }
1479
1480 input = wl_input_device_get_user_data(input_device);
1481 printf("new selection offer %p:", selection_offer);
1482
1483 offer->input = input;
1484 input->offer = offer;
1485 end = offer->types.data + offer->types.size;
1486 for (p = offer->types.data; p < end; p++)
1487 printf(" %s", *p);
1488
1489 printf("\n");
1490}
1491
1492struct wl_selection_offer_listener selection_offer_listener = {
1493 selection_offer_offer,
1494 selection_offer_keyboard_focus
1495};
1496
1497static void
1498add_selection_offer(struct display *d, uint32_t id)
1499{
1500 struct selection_offer *offer;
1501
1502 offer = malloc(sizeof *offer);
1503 if (offer == NULL)
1504 return;
1505
1506 offer->offer = wl_selection_offer_create(d->display, id);
1507 offer->display = d;
1508 wl_array_init(&offer->types);
1509 offer->input = NULL;
1510
1511 wl_selection_offer_add_listener(offer->offer,
1512 &selection_offer_listener, offer);
1513}
1514
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001515static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001516display_handle_global(struct wl_display *display, uint32_t id,
1517 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001518{
1519 struct display *d = data;
1520
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001521 if (strcmp(interface, "compositor") == 0) {
1522 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001523 } else if (strcmp(interface, "output") == 0) {
1524 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001525 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001526 } else if (strcmp(interface, "input_device") == 0) {
1527 display_add_input(d, id);
1528 } else if (strcmp(interface, "shell") == 0) {
1529 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001530 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001531 } else if (strcmp(interface, "shm") == 0) {
1532 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001533 } else if (strcmp(interface, "selection_offer") == 0) {
1534 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001535 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001536 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001537 }
1538}
1539
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001540static void
1541display_render_frame(struct display *d)
1542{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001543 int radius = 8;
1544 cairo_t *cr;
1545
1546 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1547 cr = cairo_create(d->shadow);
1548 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1549 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1550 rounded_rect(cr, 16, 16, 112, 112, radius);
1551 cairo_fill(cr);
1552 cairo_destroy(cr);
1553 blur_surface(d->shadow, 64);
1554
1555 d->active_frame =
1556 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1557 cr = cairo_create(d->active_frame);
1558 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1559 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1560 rounded_rect(cr, 16, 16, 112, 112, radius);
1561 cairo_fill(cr);
1562 cairo_destroy(cr);
1563
1564 d->inactive_frame =
1565 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1566 cr = cairo_create(d->inactive_frame);
1567 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1568 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1569 rounded_rect(cr, 16, 16, 112, 112, radius);
1570 cairo_fill(cr);
1571 cairo_destroy(cr);
1572}
1573
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001574static void
1575init_xkb(struct display *d)
1576{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001577 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001578
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001579 names.rules = "evdev";
1580 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001581 names.layout = option_xkb_layout;
1582 names.variant = option_xkb_variant;
1583 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001584
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001585 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001586 if (!d->xkb) {
1587 fprintf(stderr, "Failed to compile keymap\n");
1588 exit(1);
1589 }
1590}
1591
Yuval Fledel45568f62010-12-06 09:18:12 -05001592static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001593init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001594{
1595 EGLint major, minor;
Yuval Fledel45568f62010-12-06 09:18:12 -05001596
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001597 d->dpy = eglGetDisplay((EGLNativeDisplayType) d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001598 if (!eglInitialize(d->dpy, &major, &minor)) {
1599 fprintf(stderr, "failed to initialize display\n");
1600 return -1;
1601 }
1602
1603 if (!eglBindAPI(EGL_OPENGL_API)) {
1604 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1605 return -1;
1606 }
1607
1608 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1609 if (d->ctx == NULL) {
1610 fprintf(stderr, "failed to create context\n");
1611 return -1;
1612 }
1613
1614 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1615 fprintf(stderr, "faile to make context current\n");
1616 return -1;
1617 }
1618
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001619#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001620 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1621 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001622 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001623 return -1;
1624 }
1625#endif
1626
1627 return 0;
1628}
1629
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001630struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001631display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001632{
1633 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001634 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001635 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001636 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001637
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001638 g_type_init();
1639
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001640 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001641 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001642 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001643
1644 xkb_option_group = g_option_group_new("xkb",
1645 "Keyboard options",
1646 "Show all XKB options",
1647 NULL, NULL);
1648 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1649 g_option_context_add_group (context, xkb_option_group);
1650
1651 if (!g_option_context_parse(context, argc, argv, &error)) {
1652 fprintf(stderr, "option parsing failed: %s\n", error->message);
1653 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001654 }
1655
Tim Wiederhake748f6722011-01-23 23:25:25 +01001656 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001657
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001658 d = malloc(sizeof *d);
1659 if (d == NULL)
1660 return NULL;
1661
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001662 memset(d, 0, sizeof *d);
1663
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001664 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001665 if (d->display == NULL) {
1666 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001667 return NULL;
1668 }
1669
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001670 wl_list_init(&d->input_list);
1671
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001672 /* Set up listener so we'll catch all events. */
1673 wl_display_add_global_listener(d->display,
1674 display_handle_global, d);
1675
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001676 d->native_dpy = wl_egl_native_display_create(d->display);
1677
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001678 /* Process connection events. */
1679 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1680
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001681 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001682 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001683
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001684 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001685
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001686 display_render_frame(d);
1687
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001688 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001689 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001690 g_source_attach(d->source, NULL);
1691
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001692 wl_list_init(&d->window_list);
1693
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001694 init_xkb(d);
1695
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001696 return d;
1697}
1698
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001699struct wl_display *
1700display_get_display(struct display *display)
1701{
1702 return display->display;
1703}
1704
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001705struct wl_compositor *
1706display_get_compositor(struct display *display)
1707{
1708 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001709}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001710
1711EGLDisplay
1712display_get_egl_display(struct display *d)
1713{
1714 return d->dpy;
1715}
1716
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001717struct wl_shell *
1718display_get_shell(struct display *display)
1719{
1720 return display->shell;
1721}
1722
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001723void
1724display_run(struct display *d)
1725{
1726 g_main_loop_run(d->loop);
1727}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001728
1729void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001730display_set_global_handler(struct display *display,
1731 display_global_handler_t handler)
1732{
1733 display->global_handler = handler;
1734}