blob: 612e5b3a5a35313cc5ed5a55651cd2c84809e394 [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#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øgsberg5ee1a602008-12-11 23:18:45 -050051#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050052#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050054#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040055#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050057#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050058
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050060 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050061 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040062 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040063 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 struct wl_output *output;
Kristian Høgsberg8357cd62011-05-13 13:24:56 -040065 struct wl_visual *argb_visual, *premultiplied_argb_visual, *rgb_visual;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040067 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLDisplay dpy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +010069 EGLConfig conf;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040070 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010071 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040073 GMainLoop *loop;
74 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040075 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040076 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040077 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040078 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040079 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040080 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040081
Kristian Høgsbergb46df052011-01-18 09:17:57 -050082 display_global_handler_t global_handler;
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050083
84 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
85 PFNEGLCREATEIMAGEKHRPROC create_image;
86 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050087};
88
89struct window {
90 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050091 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050093 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050094 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050095 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040096 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040097 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050098 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050099 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500100 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400102 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400103 struct input *grab_device;
104 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400106 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500107
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400108 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500109 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500110
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500111 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400112 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500113 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400114 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500115 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400116 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400119 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500120};
121
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122struct input {
123 struct display *display;
124 struct wl_input_device *input_device;
125 struct window *pointer_focus;
126 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500127 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400128 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400129 uint32_t modifiers;
130 int32_t x, y, sx, sy;
131 struct wl_list link;
132};
133
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400134enum {
135 POINTER_DEFAULT = 100,
136 POINTER_UNSET
137};
138
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500139enum window_location {
140 WINDOW_INTERIOR = 0,
141 WINDOW_RESIZING_TOP = 1,
142 WINDOW_RESIZING_BOTTOM = 2,
143 WINDOW_RESIZING_LEFT = 4,
144 WINDOW_RESIZING_TOP_LEFT = 5,
145 WINDOW_RESIZING_BOTTOM_LEFT = 6,
146 WINDOW_RESIZING_RIGHT = 8,
147 WINDOW_RESIZING_TOP_RIGHT = 9,
148 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
149 WINDOW_RESIZING_MASK = 15,
150 WINDOW_EXTERIOR = 16,
151 WINDOW_TITLEBAR = 17,
152 WINDOW_CLIENT_AREA = 18,
153};
154
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400155const char *option_xkb_layout = "us";
156const char *option_xkb_variant = "";
157const char *option_xkb_options = "";
158
159static const GOptionEntry xkb_option_entries[] = {
160 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_layout, "XKB Layout" },
162 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
163 &option_xkb_variant, "XKB Variant" },
164 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
165 &option_xkb_options, "XKB Options" },
166 { NULL }
167};
168
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500169static void
170rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
171{
172 cairo_move_to(cr, x0, y0 + radius);
173 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
174 cairo_line_to(cr, x1 - radius, y0);
175 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
176 cairo_line_to(cr, x1, y1 - radius);
177 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
178 cairo_line_to(cr, x0 + radius, y1);
179 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
180 cairo_close_path(cr);
181}
182
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400183static const cairo_user_data_key_t surface_data_key;
184struct surface_data {
185 struct wl_buffer *buffer;
186};
187
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500188#define MULT(_d,c,a,t) \
189 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
190
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500191#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400192
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100193struct egl_window_surface_data {
194 struct display *display;
195 struct wl_surface *surface;
196 struct wl_egl_window *window;
197 EGLSurface surf;
198};
199
200static void
201egl_window_surface_data_destroy(void *p)
202{
203 struct egl_window_surface_data *data = p;
204 struct display *d = data->display;
205
206 eglDestroySurface(d->dpy, data->surf);
207 wl_egl_window_destroy(data->window);
208 data->surface = NULL;
209
210 free(p);
211}
212
213static cairo_surface_t *
214display_create_egl_window_surface(struct display *display,
215 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400216 struct wl_visual *visual,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100217 struct rectangle *rectangle)
218{
219 cairo_surface_t *cairo_surface;
220 struct egl_window_surface_data *data;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100221
222 data = malloc(sizeof *data);
223 if (data == NULL)
224 return NULL;
225
226 data->display = display;
227 data->surface = surface;
228
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400229 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100230 rectangle->width,
231 rectangle->height,
232 visual);
233
234 data->surf = eglCreateWindowSurface(display->dpy, display->conf,
235 data->window, NULL);
236
237 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
238 data->surf,
239 rectangle->width,
240 rectangle->height);
241
242 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
243 data, egl_window_surface_data_destroy);
244
245 return cairo_surface;
246}
247
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500248struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400249 struct surface_data data;
250 EGLImageKHR image;
251 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800252 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500253 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400254};
255
256static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500257egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400258{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500259 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800260 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400261
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800262 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400263 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800264 cairo_device_release(d->device);
265
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500266 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400267 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500268 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500269 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400270}
271
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500272EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500273display_get_image_for_egl_image_surface(struct display *display,
274 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500275{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500276 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500277
278 data = cairo_surface_get_user_data (surface, &surface_data_key);
279
280 return data->image;
281}
282
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500283static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500284display_create_egl_image_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400285 struct wl_visual *visual,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500286 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400287{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500288 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400289 EGLDisplay dpy = display->dpy;
290 cairo_surface_t *surface;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400291
292 data = malloc(sizeof *data);
293 if (data == NULL)
294 return NULL;
295
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800296 data->display = display;
297
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400298 data->pixmap = wl_egl_pixmap_create(rectangle->width,
299 rectangle->height,
300 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500301 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000302 free(data);
303 return NULL;
304 }
305
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500306 data->image = display->create_image(dpy, NULL,
307 EGL_NATIVE_PIXMAP_KHR,
308 (EGLClientBuffer) data->pixmap,
309 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500311 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312 free(data);
313 return NULL;
314 }
315
316 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400317 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800319 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500322 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 surface = cairo_gl_surface_create_for_texture(display->device,
326 CAIRO_CONTENT_COLOR_ALPHA,
327 data->texture,
328 rectangle->width,
329 rectangle->height);
330
331 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500332 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333
334 return surface;
335}
336
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500337static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338display_create_egl_image_surface_from_file(struct display *display,
339 const char *filename,
340 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400341{
342 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400343 GdkPixbuf *pixbuf;
344 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400345 int stride, i;
346 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347 struct egl_image_surface_data *data;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400348 struct wl_visual *visual;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400349
350 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400352 FALSE, &error);
353 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400355
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400356 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
357 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500358 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400360 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400361
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400362
363 stride = gdk_pixbuf_get_rowstride(pixbuf);
364 pixels = gdk_pixbuf_get_pixels(pixbuf);
365
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400366 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400367 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400368 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400369 while (p < end) {
370 unsigned int t;
371
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372 MULT(p[0], p[0], p[3], t);
373 MULT(p[1], p[1], p[3], t);
374 MULT(p[2], p[2], p[3], t);
375 p += 4;
376
377 }
378 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400379
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400380 visual = display->premultiplied_argb_visual;
381 surface = display_create_egl_image_surface(display, visual, rect);
nobled7b87cb02011-02-01 18:51:47 +0000382 if (surface == NULL) {
383 g_object_unref(pixbuf);
384 return NULL;
385 }
386
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800387 data = cairo_surface_get_user_data(surface, &surface_data_key);
388
389 cairo_device_acquire(display->device);
390 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800391 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
392 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800393 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400394
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500395 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400396
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397 return surface;
398}
399
400#endif
401
402struct wl_buffer *
403display_get_buffer_for_surface(struct display *display,
404 cairo_surface_t *surface)
405{
406 struct surface_data *data;
407
408 data = cairo_surface_get_user_data (surface, &surface_data_key);
409
410 return data->buffer;
411}
412
413struct shm_surface_data {
414 struct surface_data data;
415 void *map;
416 size_t length;
417};
418
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500419static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420shm_surface_data_destroy(void *p)
421{
422 struct shm_surface_data *data = p;
423
424 wl_buffer_destroy(data->data.buffer);
425 munmap(data->map, data->length);
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(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400430 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431{
432 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433 struct wl_visual *visual;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400434 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800435 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 char filename[] = "/tmp/wayland-shm-XXXXXX";
437
438 data = malloc(sizeof *data);
439 if (data == NULL)
440 return NULL;
441
442 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
443 rectangle->width);
444 data->length = stride * rectangle->height;
445 fd = mkstemp(filename);
446 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000447 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400448 return NULL;
449 }
450 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000451 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400452 close(fd);
453 return NULL;
454 }
455
456 data->map = mmap(NULL, data->length,
457 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
458 unlink(filename);
459
460 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000461 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400462 close(fd);
463 return NULL;
464 }
465
466 surface = cairo_image_surface_create_for_data (data->map,
467 CAIRO_FORMAT_ARGB32,
468 rectangle->width,
469 rectangle->height,
470 stride);
471
472 cairo_surface_set_user_data (surface, &surface_data_key,
473 data, shm_surface_data_destroy);
474
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400475 if (flags & SURFACE_OPAQUE)
476 visual = display->rgb_visual;
477 else
478 visual = display->premultiplied_argb_visual;
479
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400480 data->data.buffer = wl_shm_create_buffer(display->shm,
481 fd,
482 rectangle->width,
483 rectangle->height,
484 stride, visual);
485
486 close(fd);
487
488 return surface;
489}
490
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500491static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492display_create_shm_surface_from_file(struct display *display,
493 const char *filename,
494 struct rectangle *rect)
495{
496 cairo_surface_t *surface;
497 GdkPixbuf *pixbuf;
498 GError *error = NULL;
499 int stride, i;
500 unsigned char *pixels, *p, *end, *dest_data;
501 int dest_stride;
502 uint32_t *d;
503
504 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
505 rect->width, rect->height,
506 FALSE, &error);
507 if (error != NULL)
508 return NULL;
509
510 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
511 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500512 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513 return NULL;
514 }
515
516 stride = gdk_pixbuf_get_rowstride(pixbuf);
517 pixels = gdk_pixbuf_get_pixels(pixbuf);
518
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400519 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000520 if (surface == NULL) {
521 g_object_unref(pixbuf);
522 return NULL;
523 }
524
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525 dest_data = cairo_image_surface_get_data (surface);
526 dest_stride = cairo_image_surface_get_stride (surface);
527
528 for (i = 0; i < rect->height; i++) {
529 d = (uint32_t *) (dest_data + i * dest_stride);
530 p = pixels + i * stride;
531 end = p + rect->width * 4;
532 while (p < end) {
533 unsigned int t;
534 unsigned char a, r, g, b;
535
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536 a = p[3];
537 MULT(r, p[0], a, t);
538 MULT(g, p[1], a, t);
539 MULT(b, p[2], a, t);
540 p += 4;
541 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
542 }
543 }
544
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500545 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
547 return surface;
548}
549
nobled7b87cb02011-02-01 18:51:47 +0000550static int
551check_size(struct rectangle *rect)
552{
553 if (rect->width && rect->height)
554 return 0;
555
556 fprintf(stderr, "tried to create surface of "
557 "width: %d, height: %d\n", rect->width, rect->height);
558 return -1;
559}
560
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561cairo_surface_t *
562display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100563 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400564 struct rectangle *rectangle,
565 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566{
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400567 struct wl_visual *visual;
568
569 if (flags & SURFACE_OPAQUE)
570 visual = display->rgb_visual;
571 else
572 visual = display->premultiplied_argb_visual;
573
nobled7b87cb02011-02-01 18:51:47 +0000574 if (check_size(rectangle) < 0)
575 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500576#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500577 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100578 if (surface)
579 return display_create_egl_window_surface(display,
580 surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400581 visual,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100582 rectangle);
583 else
584 return display_create_egl_image_surface(display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400585 visual,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100586 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500587 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400588#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400589 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590}
591
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500592static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593display_create_surface_from_file(struct display *display,
594 const char *filename,
595 struct rectangle *rectangle)
596{
nobled7b87cb02011-02-01 18:51:47 +0000597 if (check_size(rectangle) < 0)
598 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500599#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500600 if (display->dpy) {
601 return display_create_egl_image_surface_from_file(display,
602 filename,
603 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500604 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500606 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400607}
Yuval Fledel45568f62010-12-06 09:18:12 -0500608 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400609 const char *filename;
610 int hotspot_x, hotspot_y;
611} pointer_images[] = {
612 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
613 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
614 { DATADIR "/wayland/bottom_side.png", 16, 20 },
615 { DATADIR "/wayland/grabbing.png", 20, 17 },
616 { DATADIR "/wayland/left_ptr.png", 10, 5 },
617 { DATADIR "/wayland/left_side.png", 10, 20 },
618 { DATADIR "/wayland/right_side.png", 30, 19 },
619 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
620 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
621 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400622 { DATADIR "/wayland/xterm.png", 15, 15 },
623 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400624};
625
626static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400627create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400628{
629 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400630 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400631 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400632
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400633 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400634 display->pointer_surfaces =
635 malloc(count * sizeof *display->pointer_surfaces);
636 rect.width = width;
637 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400638 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400639 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640 display_create_surface_from_file(display,
641 pointer_images[i].filename,
642 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400643 }
644
645}
646
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400647cairo_surface_t *
648display_get_pointer_surface(struct display *display, int pointer,
649 int *width, int *height,
650 int *hotspot_x, int *hotspot_y)
651{
652 cairo_surface_t *surface;
653
654 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500655#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400656 *width = cairo_gl_surface_get_width(surface);
657 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000658#else
659 *width = cairo_image_surface_get_width(surface);
660 *height = cairo_image_surface_get_height(surface);
661#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400662 *hotspot_x = pointer_images[pointer].hotspot_x;
663 *hotspot_y = pointer_images[pointer].hotspot_y;
664
665 return cairo_surface_reference(surface);
666}
667
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400668
669static void
670window_attach_surface(struct window *window);
671
672static void
673free_surface(void *data)
674{
675 struct window *window = data;
676
677 cairo_surface_destroy(window->pending_surface);
678 window->pending_surface = NULL;
679 if (window->cairo_surface)
680 window_attach_surface(window);
681}
682
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500683static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500684window_attach_surface(struct window *window)
685{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400686 struct display *display = window->display;
687 struct wl_buffer *buffer;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100688 struct egl_window_surface_data *data;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500689 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100690 int width = window->allocation.width;
691 int height = window->allocation.height;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500692
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500693 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100694 x = window->server_allocation.width - width;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500695 else
696 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500697
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500698 if (window->resize_edges & WINDOW_RESIZING_TOP)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100699 y = window->server_allocation.height - height;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500700 else
701 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400702
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500703 window->resize_edges = 0;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100704
705 switch (window->buffer_type) {
706 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
707 data = cairo_surface_get_user_data(window->cairo_surface,
708 &surface_data_key);
709
710 wl_egl_window_resize(data->window, width, height, x, y);
711 cairo_gl_surface_swapbuffers(window->cairo_surface);
712 wl_egl_window_get_attached_size(data->window,
713 &window->server_allocation.width,
714 &window->server_allocation.height);
715 break;
716 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
717 case WINDOW_BUFFER_TYPE_SHM:
718 if (window->pending_surface != NULL)
719 return;
720
721 window->pending_surface = window->cairo_surface;
722 window->cairo_surface = NULL;
723
724 buffer =
725 display_get_buffer_for_surface(display,
726 window->pending_surface);
727
728 wl_surface_attach(window->surface, buffer, x, y);
729 window->server_allocation = window->allocation;
730 wl_display_sync_callback(display->display, free_surface,
731 window);
732 break;
733 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500734
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500735 if (window->fullscreen)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400736 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500737 else if (!window->parent)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400738 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500739 else
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400740 wl_shell_set_transient(display->shell, window->surface,
741 window->parent->surface,
742 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500743
744 wl_surface_damage(window->surface, 0, 0,
745 window->allocation.width,
746 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500747}
748
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500749void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400750window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500751{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100752 if (window->cairo_surface) {
753 switch (window->buffer_type) {
754 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
755 case WINDOW_BUFFER_TYPE_SHM:
756 display_surface_damage(window->display,
757 window->cairo_surface,
758 0, 0,
759 window->allocation.width,
760 window->allocation.height);
761 break;
762 default:
763 break;
764 }
765 window_attach_surface(window);
766 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500767}
768
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400769void
770window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500772 cairo_surface_reference(surface);
773
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400774 if (window->cairo_surface != NULL)
775 cairo_surface_destroy(window->cairo_surface);
776
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500777 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400778}
779
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780static void
781window_resize_cairo_window_surface(struct window *window)
782{
783 struct egl_window_surface_data *data;
784
785 data = cairo_surface_get_user_data(window->cairo_surface,
786 &surface_data_key);
787
788 wl_egl_window_resize(data->window,
789 window->allocation.width,
790 window->allocation.height, 0, 0);
791 cairo_gl_surface_set_size(window->cairo_surface,
792 window->allocation.width,
793 window->allocation.height);
794}
795
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400796void
797window_create_surface(struct window *window)
798{
799 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400800 uint32_t flags = 0;
801
802 if (!window->transparent)
803 flags = SURFACE_OPAQUE;
804
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400805 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500806#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100807 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
808 if (window->cairo_surface) {
809 window_resize_cairo_window_surface(window);
810 return;
811 }
812 surface = display_create_surface(window->display,
813 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400814 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500816 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400817 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400819 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820 break;
821#endif
822 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400823 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800826 default:
827 surface = NULL;
828 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400830
831 window_set_surface(window, surface);
832 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833}
834
835static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500836window_draw_menu(struct window *window)
837{
838 cairo_t *cr;
839 int width, height, r = 5;
840
841 window_create_surface(window);
842
843 cr = cairo_create(window->cairo_surface);
844 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
845 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
846 cairo_paint(cr);
847
848 width = window->allocation.width;
849 height = window->allocation.height;
850 rounded_rect(cr, r, r, width - r, height - r, r);
851 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
852 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
853 cairo_fill(cr);
854 cairo_destroy(cr);
855}
856
857static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500858window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500859{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500860 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500861 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400862 cairo_surface_t *frame;
863 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500864
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400865 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400866
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400867 width = window->allocation.width;
868 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500869
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500870 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500871
Kristian Høgsberg09531622010-06-14 23:22:15 -0400872 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400873 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400874 cairo_paint(cr);
875
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400876 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400877 tile_mask(cr, window->display->shadow,
878 shadow_dx, shadow_dy, width, height,
879 window->margin + 10 - shadow_dx,
880 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500881
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400882 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400883 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400884 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400885 frame = window->display->inactive_frame;
886
887 tile_source(cr, frame, 0, 0, width, height,
888 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500889
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500890 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
891 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500892 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400893 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400894 if (window->keyboard_device)
895 cairo_set_source_rgb(cr, 0, 0, 0);
896 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400897 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400898 cairo_show_text(cr, window->title);
899
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500900 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400901
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100902 /* FIXME: this breakes gears, fix cairo? */
903#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400904 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100905#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500906}
907
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400908void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500909window_destroy(struct window *window)
910{
911 wl_surface_destroy(window->surface);
912 wl_list_remove(&window->link);
913 free(window);
914}
915
916void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400917display_flush_cairo_device(struct display *display)
918{
919 cairo_device_flush (display->device);
920}
921
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500922void
923window_draw(struct window *window)
924{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500925 if (window->parent)
926 window_draw_menu(window);
927 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500929 else
930 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500931}
932
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400933cairo_surface_t *
934window_get_surface(struct window *window)
935{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400936 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400937}
938
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100939struct wl_surface *
940window_get_wl_surface(struct window *window)
941{
942 return window->surface;
943}
944
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400945static int
946get_pointer_location(struct window *window, int32_t x, int32_t y)
947{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400948 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400949 const int grip_size = 8;
950
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500951 if (!window->decoration)
952 return WINDOW_CLIENT_AREA;
953
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400954 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400955 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400956 else if (window->margin <= x && x < window->margin + grip_size)
957 hlocation = WINDOW_RESIZING_LEFT;
958 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400959 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400960 else if (x < window->allocation.width - window->margin)
961 hlocation = WINDOW_RESIZING_RIGHT;
962 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400963 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400964
965 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400966 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400967 else if (window->margin <= y && y < window->margin + grip_size)
968 vlocation = WINDOW_RESIZING_TOP;
969 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400970 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400971 else if (y < window->allocation.height - window->margin)
972 vlocation = WINDOW_RESIZING_BOTTOM;
973 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400974 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400975
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400976 location = vlocation | hlocation;
977 if (location & WINDOW_EXTERIOR)
978 location = WINDOW_EXTERIOR;
979 if (location == WINDOW_INTERIOR && y < window->margin + 50)
980 location = WINDOW_TITLEBAR;
981 else if (location == WINDOW_INTERIOR)
982 location = WINDOW_CLIENT_AREA;
983
984 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400985}
986
987static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400988set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400989{
990 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400991 struct wl_buffer *buffer;
992 cairo_surface_t *surface;
993 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400994
995 location = get_pointer_location(input->pointer_focus,
996 input->sx, input->sy);
997 switch (location) {
998 case WINDOW_RESIZING_TOP:
999 pointer = POINTER_TOP;
1000 break;
1001 case WINDOW_RESIZING_BOTTOM:
1002 pointer = POINTER_BOTTOM;
1003 break;
1004 case WINDOW_RESIZING_LEFT:
1005 pointer = POINTER_LEFT;
1006 break;
1007 case WINDOW_RESIZING_RIGHT:
1008 pointer = POINTER_RIGHT;
1009 break;
1010 case WINDOW_RESIZING_TOP_LEFT:
1011 pointer = POINTER_TOP_LEFT;
1012 break;
1013 case WINDOW_RESIZING_TOP_RIGHT:
1014 pointer = POINTER_TOP_RIGHT;
1015 break;
1016 case WINDOW_RESIZING_BOTTOM_LEFT:
1017 pointer = POINTER_BOTTOM_LEFT;
1018 break;
1019 case WINDOW_RESIZING_BOTTOM_RIGHT:
1020 pointer = POINTER_BOTTOM_RIGHT;
1021 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001022 case WINDOW_EXTERIOR:
1023 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001024 if (input->current_pointer_image == POINTER_DEFAULT)
1025 return;
1026
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001027 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001028 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001029 return;
1030 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001031 break;
1032 }
1033
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001034 if (pointer == input->current_pointer_image)
1035 return;
1036
1037 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001038 surface = display->pointer_surfaces[pointer];
1039 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001040 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001041 pointer_images[pointer].hotspot_x,
1042 pointer_images[pointer].hotspot_y);
1043}
1044
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001045static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001046window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001047 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001048 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001049{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001050 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001051 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001052 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001053
1054 input->x = x;
1055 input->y = y;
1056 input->sx = sx;
1057 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001058
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001059 if (window->motion_handler)
1060 pointer = (*window->motion_handler)(window, input, time,
1061 x, y, sx, sy,
1062 window->user_data);
1063
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001064 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001065}
1066
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001067static void
1068window_handle_button(void *data,
1069 struct wl_input_device *input_device,
1070 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001071{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001072 struct input *input = data;
1073 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001074 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001075
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001076 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001077
1078 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001079 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001080 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001081 wl_shell_move(window->display->shell,
1082 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001083 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001084 case WINDOW_RESIZING_TOP:
1085 case WINDOW_RESIZING_BOTTOM:
1086 case WINDOW_RESIZING_LEFT:
1087 case WINDOW_RESIZING_RIGHT:
1088 case WINDOW_RESIZING_TOP_LEFT:
1089 case WINDOW_RESIZING_TOP_RIGHT:
1090 case WINDOW_RESIZING_BOTTOM_LEFT:
1091 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001092 wl_shell_resize(window->display->shell,
1093 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001094 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001095 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001096 case WINDOW_CLIENT_AREA:
1097 if (window->button_handler)
1098 (*window->button_handler)(window,
1099 input, time,
1100 button, state,
1101 window->user_data);
1102 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001103 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001104 } else {
1105 if (window->button_handler)
1106 (*window->button_handler)(window,
1107 input, time,
1108 button, state,
1109 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001110 }
1111}
1112
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001113static void
1114window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001115 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001116{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001117 struct input *input = data;
1118 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001119 struct display *d = window->display;
1120 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001121
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001122 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001123 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001124 return;
1125
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001126 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001127 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001128 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1129 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001130
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001131 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1132
1133 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001134 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001135 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001136 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001137
1138 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001139 (*window->key_handler)(window, input, time, key, sym, state,
1140 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001141}
1142
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001143static void
1144window_handle_pointer_focus(void *data,
1145 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001146 uint32_t time, struct wl_surface *surface,
1147 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001148{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001149 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001150 struct window *window;
1151 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001152
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001153 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001154 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001155 window = input->pointer_focus;
1156
Kristian Høgsberg59826582011-01-20 11:56:57 -05001157 input->x = x;
1158 input->y = y;
1159 input->sx = sx;
1160 input->sy = sy;
1161
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001162 pointer = POINTER_LEFT_PTR;
1163 if (window->motion_handler)
1164 pointer = (*window->motion_handler)(window,
1165 input, time,
1166 x, y, sx, sy,
1167 window->user_data);
1168
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001169 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001170 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001171 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001172 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001174}
1175
1176static void
1177window_handle_keyboard_focus(void *data,
1178 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001179 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001180 struct wl_surface *surface,
1181 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001182{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001183 struct input *input = data;
1184 struct window *window = input->keyboard_focus;
1185 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001186 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001187
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001188 window = input->keyboard_focus;
1189 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001190 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001191 if (window->keyboard_focus_handler)
1192 (*window->keyboard_focus_handler)(window, NULL,
1193 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001194 }
1195
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001196 if (surface)
1197 input->keyboard_focus = wl_surface_get_user_data(surface);
1198 else
1199 input->keyboard_focus = NULL;
1200
1201 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001202 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001203 for (k = keys->data; k < end; k++)
1204 input->modifiers |= d->xkb->map->modmap[*k];
1205
1206 window = input->keyboard_focus;
1207 if (window) {
1208 window->keyboard_device = input;
1209 if (window->keyboard_focus_handler)
1210 (*window->keyboard_focus_handler)(window,
1211 window->keyboard_device,
1212 window->user_data);
1213 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001214}
1215
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001216static const struct wl_input_device_listener input_device_listener = {
1217 window_handle_motion,
1218 window_handle_button,
1219 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001220 window_handle_pointer_focus,
1221 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001222};
1223
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001224void
1225input_get_position(struct input *input, int32_t *x, int32_t *y)
1226{
1227 *x = input->sx;
1228 *y = input->sy;
1229}
1230
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001231struct wl_input_device *
1232input_get_input_device(struct input *input)
1233{
1234 return input->input_device;
1235}
1236
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001237uint32_t
1238input_get_modifiers(struct input *input)
1239{
1240 return input->modifiers;
1241}
1242
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001243struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001244window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001245{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 cairo_device_flush (window->display->device);
1247
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001248 return wl_shell_create_drag(window->display->shell);
1249}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001250
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001251void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001252window_move(struct window *window, struct input *input, uint32_t time)
1253{
1254 wl_shell_move(window->display->shell,
1255 window->surface, input->input_device, time);
1256}
1257
1258void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001259window_activate_drag(struct wl_drag *drag, struct window *window,
1260 struct input *input, uint32_t time)
1261{
1262 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263}
1264
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001265static void
1266handle_configure(void *data, struct wl_shell *shell,
1267 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001268 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001269{
1270 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001271 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001272
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001273 /* FIXME: this is probably the wrong place to check for width
1274 * or height <= 0, but it prevents the compositor from crashing
1275 */
1276 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001277 return;
1278
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001279 window->resize_edges = edges;
1280
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001281 if (window->resize_handler) {
1282 child_width = width - 20 - window->margin * 2;
1283 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001284
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001285 (*window->resize_handler)(window,
1286 child_width, child_height,
1287 window->user_data);
1288 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001289 window->allocation.width = width;
1290 window->allocation.height = height;
1291
1292 if (window->redraw_handler)
1293 window_schedule_redraw(window);
1294 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001295}
1296
1297static const struct wl_shell_listener shell_listener = {
1298 handle_configure,
1299};
1300
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001301void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001302window_get_allocation(struct window *window,
1303 struct rectangle *allocation)
1304{
1305 *allocation = window->allocation;
1306}
1307
1308void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001309window_get_child_allocation(struct window *window,
1310 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001311{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001312 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001313 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001314 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001315 allocation->x = window->margin + 10;
1316 allocation->y = window->margin + 50;
1317 allocation->width =
1318 window->allocation.width - 20 - window->margin * 2;
1319 allocation->height =
1320 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001321 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001322}
1323
1324void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001325window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001326{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001327 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001328 window->allocation.x = 20 + window->margin;
1329 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001330 window->allocation.width = width + 20 + window->margin * 2;
1331 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001332 } else {
1333 window->allocation.x = 0;
1334 window->allocation.y = 0;
1335 window->allocation.width = width;
1336 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001337 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001338}
1339
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001340static gboolean
1341idle_redraw(void *data)
1342{
1343 struct window *window = data;
1344
1345 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001346
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001347 window->redraw_scheduled = 0;
1348
1349 return FALSE;
1350}
1351
1352void
1353window_schedule_redraw(struct window *window)
1354{
1355 if (!window->redraw_scheduled) {
1356 g_idle_add(idle_redraw, window);
1357 window->redraw_scheduled = 1;
1358 }
1359}
1360
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001361void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001362window_set_fullscreen(struct window *window, int fullscreen)
1363{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001364 int32_t width, height;
1365
1366 if (window->fullscreen == fullscreen)
1367 return;
1368
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001369 window->fullscreen = fullscreen;
1370 if (window->fullscreen) {
1371 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001372 width = window->display->screen_allocation.width;
1373 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001374 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001375 width = window->saved_allocation.width - 20 - window->margin * 2;
1376 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001377 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001378
1379 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001380}
1381
1382void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001383window_set_decoration(struct window *window, int decoration)
1384{
1385 window->decoration = decoration;
1386}
1387
1388void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001389window_set_user_data(struct window *window, void *data)
1390{
1391 window->user_data = data;
1392}
1393
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001394void *
1395window_get_user_data(struct window *window)
1396{
1397 return window->user_data;
1398}
1399
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001400void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001401window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001402 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001403{
1404 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001405}
1406
1407void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001408window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001409 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001410{
1411 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001412}
1413
1414void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001415window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001416 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001417{
1418 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001419}
1420
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001421void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001422window_set_button_handler(struct window *window,
1423 window_button_handler_t handler)
1424{
1425 window->button_handler = handler;
1426}
1427
1428void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001429window_set_motion_handler(struct window *window,
1430 window_motion_handler_t handler)
1431{
1432 window->motion_handler = handler;
1433}
1434
1435void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001436window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001437 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001438{
1439 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001440}
1441
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001442void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001443window_set_transparent(struct window *window, int transparent)
1444{
1445 window->transparent = transparent;
1446}
1447
1448void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001449window_set_title(struct window *window, const char *title)
1450{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001451 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001452 window->title = strdup(title);
1453}
1454
1455const char *
1456window_get_title(struct window *window)
1457{
1458 return window->title;
1459}
1460
1461void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001462display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1463 int32_t x, int32_t y, int32_t width, int32_t height)
1464{
1465 struct wl_buffer *buffer;
1466
1467 buffer = display_get_buffer_for_surface(display, cairo_surface);
1468
1469 wl_buffer_damage(buffer, x, y, width, height);
1470}
1471
1472void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001473window_damage(struct window *window, int32_t x, int32_t y,
1474 int32_t width, int32_t height)
1475{
1476 wl_surface_damage(window->surface, x, y, width, height);
1477}
1478
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001479static struct window *
1480window_create_internal(struct display *display, struct window *parent,
1481 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001482{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001483 struct window *window;
1484
1485 window = malloc(sizeof *window);
1486 if (window == NULL)
1487 return NULL;
1488
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001489 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001490 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001491 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001492 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001493 window->allocation.x = 0;
1494 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001495 window->allocation.width = width;
1496 window->allocation.height = height;
1497 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001498 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001499 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001500 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001501
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001502 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001503 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1504 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001505 else
1506 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001507
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001508 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001509 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001510
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001511 return window;
1512}
1513
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001514struct window *
1515window_create(struct display *display, int32_t width, int32_t height)
1516{
1517 struct window *window;
1518
1519 window = window_create_internal(display, NULL, width, height);
1520 if (!window)
1521 return NULL;
1522
1523 return window;
1524}
1525
1526struct window *
1527window_create_transient(struct display *display, struct window *parent,
1528 int32_t x, int32_t y, int32_t width, int32_t height)
1529{
1530 struct window *window;
1531
1532 window = window_create_internal(parent->display,
1533 parent, width, height);
1534 if (!window)
1535 return NULL;
1536
1537 window->x = x;
1538 window->y = y;
1539
1540 return window;
1541}
1542
1543void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001544window_set_buffer_type(struct window *window, enum window_buffer_type type)
1545{
1546 window->buffer_type = type;
1547}
1548
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001549static void
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001550compositor_handle_visual(void *data,
1551 struct wl_compositor *compositor,
1552 uint32_t id, uint32_t token)
1553{
1554 struct display *d = data;
1555
1556 switch (token) {
1557 case WL_COMPOSITOR_VISUAL_ARGB32:
1558 d->argb_visual = wl_visual_create(d->display, id, 1);
1559 break;
1560 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
1561 d->premultiplied_argb_visual =
1562 wl_visual_create(d->display, id, 1);
1563 break;
1564 case WL_COMPOSITOR_VISUAL_XRGB32:
1565 d->rgb_visual = wl_visual_create(d->display, id, 1);
1566 break;
1567 }
1568}
1569
1570static const struct wl_compositor_listener compositor_listener = {
1571 compositor_handle_visual,
1572};
1573
1574static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001575display_handle_geometry(void *data,
1576 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001577 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001578{
1579 struct display *display = data;
1580
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001581 display->screen_allocation.x = x;
1582 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001583 display->screen_allocation.width = width;
1584 display->screen_allocation.height = height;
1585}
1586
1587static const struct wl_output_listener output_listener = {
1588 display_handle_geometry,
1589};
1590
1591static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001592display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001593{
1594 struct input *input;
1595
1596 input = malloc(sizeof *input);
1597 if (input == NULL)
1598 return;
1599
1600 memset(input, 0, sizeof *input);
1601 input->display = d;
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001602 input->input_device = wl_input_device_create(d->display, id, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001603 input->pointer_focus = NULL;
1604 input->keyboard_focus = NULL;
1605 wl_list_insert(d->input_list.prev, &input->link);
1606
1607 wl_input_device_add_listener(input->input_device,
1608 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001609 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001610}
1611
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001612struct selection_offer {
1613 struct display *display;
1614 struct wl_selection_offer *offer;
1615 struct wl_array types;
1616 struct input *input;
1617};
1618
1619int
1620input_offers_mime_type(struct input *input, const char *type)
1621{
1622 struct selection_offer *offer = input->offer;
1623 char **p, **end;
1624
1625 if (offer == NULL)
1626 return 0;
1627
1628 end = offer->types.data + offer->types.size;
1629 for (p = offer->types.data; p < end; p++)
1630 if (strcmp(*p, type) == 0)
1631 return 1;
1632
1633 return 0;
1634}
1635
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001636void
1637input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001638{
1639 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001640
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001641 /* FIXME: A number of things can go wrong here: the object may
1642 * not be the current selection offer any more (which could
1643 * still work, but the source may have gone away or just
1644 * destroyed its wl_selection) or the offer may not have the
1645 * requested type after all (programmer/client error,
1646 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001647 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001648}
1649
1650static void
1651selection_offer_offer(void *data,
1652 struct wl_selection_offer *selection_offer,
1653 const char *type)
1654{
1655 struct selection_offer *offer = data;
1656
1657 char **p;
1658
1659 p = wl_array_add(&offer->types, sizeof *p);
1660 if (p)
1661 *p = strdup(type);
1662};
1663
1664static void
1665selection_offer_keyboard_focus(void *data,
1666 struct wl_selection_offer *selection_offer,
1667 struct wl_input_device *input_device)
1668{
1669 struct selection_offer *offer = data;
1670 struct input *input;
1671 char **p, **end;
1672
1673 if (input_device == NULL) {
1674 printf("selection offer retracted %p\n", selection_offer);
1675 input = offer->input;
1676 input->offer = NULL;
1677 wl_selection_offer_destroy(selection_offer);
1678 wl_array_release(&offer->types);
1679 free(offer);
1680 return;
1681 }
1682
1683 input = wl_input_device_get_user_data(input_device);
1684 printf("new selection offer %p:", selection_offer);
1685
1686 offer->input = input;
1687 input->offer = offer;
1688 end = offer->types.data + offer->types.size;
1689 for (p = offer->types.data; p < end; p++)
1690 printf(" %s", *p);
1691
1692 printf("\n");
1693}
1694
1695struct wl_selection_offer_listener selection_offer_listener = {
1696 selection_offer_offer,
1697 selection_offer_keyboard_focus
1698};
1699
1700static void
1701add_selection_offer(struct display *d, uint32_t id)
1702{
1703 struct selection_offer *offer;
1704
1705 offer = malloc(sizeof *offer);
1706 if (offer == NULL)
1707 return;
1708
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001709 offer->offer = wl_selection_offer_create(d->display, id, 1);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001710 offer->display = d;
1711 wl_array_init(&offer->types);
1712 offer->input = NULL;
1713
1714 wl_selection_offer_add_listener(offer->offer,
1715 &selection_offer_listener, offer);
1716}
1717
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001718static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001719display_handle_global(struct wl_display *display, uint32_t id,
1720 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001721{
1722 struct display *d = data;
1723
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001724 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001725 d->compositor = wl_compositor_create(display, id, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001726 wl_compositor_add_listener(d->compositor,
1727 &compositor_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001728 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001729 d->output = wl_output_create(display, id, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001730 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001731 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001732 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001733 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001734 d->shell = wl_shell_create(display, id, 1);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001735 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001736 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001737 d->shm = wl_shm_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001738 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001739 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001740 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001741 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001742 }
1743}
1744
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001745static void
1746display_render_frame(struct display *d)
1747{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001748 int radius = 8;
1749 cairo_t *cr;
1750
1751 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1752 cr = cairo_create(d->shadow);
1753 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1754 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1755 rounded_rect(cr, 16, 16, 112, 112, radius);
1756 cairo_fill(cr);
1757 cairo_destroy(cr);
1758 blur_surface(d->shadow, 64);
1759
1760 d->active_frame =
1761 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1762 cr = cairo_create(d->active_frame);
1763 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1764 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1765 rounded_rect(cr, 16, 16, 112, 112, radius);
1766 cairo_fill(cr);
1767 cairo_destroy(cr);
1768
1769 d->inactive_frame =
1770 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1771 cr = cairo_create(d->inactive_frame);
1772 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1773 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1774 rounded_rect(cr, 16, 16, 112, 112, radius);
1775 cairo_fill(cr);
1776 cairo_destroy(cr);
1777}
1778
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001779static void
1780init_xkb(struct display *d)
1781{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001782 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001783
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001784 names.rules = "evdev";
1785 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001786 names.layout = option_xkb_layout;
1787 names.variant = option_xkb_variant;
1788 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001789
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001790 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001791 if (!d->xkb) {
1792 fprintf(stderr, "Failed to compile keymap\n");
1793 exit(1);
1794 }
1795}
1796
Yuval Fledel45568f62010-12-06 09:18:12 -05001797static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001798init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001799{
1800 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001801 EGLint n;
1802 static const EGLint cfg_attribs[] = {
1803 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1804 EGL_RED_SIZE, 1,
1805 EGL_GREEN_SIZE, 1,
1806 EGL_BLUE_SIZE, 1,
1807 EGL_ALPHA_SIZE, 1,
1808 EGL_DEPTH_SIZE, 1,
1809 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1810 EGL_NONE
1811 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001812
Egbert Eiche7b8d902011-05-10 20:00:19 +00001813 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001814 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001815 if (!eglInitialize(d->dpy, &major, &minor)) {
1816 fprintf(stderr, "failed to initialize display\n");
1817 return -1;
1818 }
1819
1820 if (!eglBindAPI(EGL_OPENGL_API)) {
1821 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1822 return -1;
1823 }
1824
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001825 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1826 fprintf(stderr, "failed to choose config\n");
1827 return -1;
1828 }
1829
1830 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001831 if (d->ctx == NULL) {
1832 fprintf(stderr, "failed to create context\n");
1833 return -1;
1834 }
1835
1836 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001837 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001838 return -1;
1839 }
1840
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001841#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001842 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001843 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001844 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001845 return -1;
1846 }
1847#endif
1848
1849 return 0;
1850}
1851
Benjamin Franzke65e50512011-05-31 11:36:31 +02001852static void
1853sync_callback(void *data)
1854{
1855 int *done = data;
1856
1857 *done = 1;
1858}
1859
1860static void
1861force_roundtrip(struct display *d)
1862{
1863 int done = 0;
1864
1865 wl_display_sync_callback(d->display, sync_callback, &done);
1866 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1867 while (!done)
1868 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1869}
1870
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001871struct display *
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001872display_create(int *argc, char **argv[], const GOptionEntry *option_entries,
1873 display_global_handler_t handler)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001874{
1875 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001876 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001877 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001878 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001879
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001880 g_type_init();
1881
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001882 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001883 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001884 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001885
1886 xkb_option_group = g_option_group_new("xkb",
1887 "Keyboard options",
1888 "Show all XKB options",
1889 NULL, NULL);
1890 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1891 g_option_context_add_group (context, xkb_option_group);
1892
1893 if (!g_option_context_parse(context, argc, argv, &error)) {
1894 fprintf(stderr, "option parsing failed: %s\n", error->message);
1895 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001896 }
1897
Tim Wiederhake748f6722011-01-23 23:25:25 +01001898 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001899
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001900 d = malloc(sizeof *d);
1901 if (d == NULL)
1902 return NULL;
1903
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001904 memset(d, 0, sizeof *d);
1905
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001906 d->global_handler = handler;
1907
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001908 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001909 if (d->display == NULL) {
1910 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001911 return NULL;
1912 }
1913
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001914 wl_list_init(&d->input_list);
1915
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001916 /* Set up listener so we'll catch all events. */
1917 wl_display_add_global_listener(d->display,
1918 display_handle_global, d);
1919
1920 /* Process connection events. */
1921 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001922 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001923 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001924
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001925 d->image_target_texture_2d =
1926 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1927 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1928 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1929
Benjamin Franzke65e50512011-05-31 11:36:31 +02001930 if (!d->premultiplied_argb_visual || !d->rgb_visual) {
1931 force_roundtrip(d);
1932 if (!d->premultiplied_argb_visual || !d->rgb_visual) {
1933 fprintf(stderr, "failed to retreive visuals\n");
1934 return NULL;
1935 }
1936 }
1937
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001938 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001939
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001940 display_render_frame(d);
1941
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001942 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001943 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001944 g_source_attach(d->source, NULL);
1945
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001946 wl_list_init(&d->window_list);
1947
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001948 init_xkb(d);
1949
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001950 return d;
1951}
1952
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001953struct wl_display *
1954display_get_display(struct display *display)
1955{
1956 return display->display;
1957}
1958
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001959struct wl_compositor *
1960display_get_compositor(struct display *display)
1961{
1962 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001963}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001964
1965EGLDisplay
1966display_get_egl_display(struct display *d)
1967{
1968 return d->dpy;
1969}
1970
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001971EGLConfig
1972display_get_egl_config(struct display *d)
1973{
1974 return d->conf;
1975}
1976
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001977struct wl_shell *
1978display_get_shell(struct display *display)
1979{
1980 return display->shell;
1981}
1982
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001983void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001984display_acquire_window_surface(struct display *display,
1985 struct window *window,
1986 EGLContext ctx)
1987{
1988 struct egl_window_surface_data *data;
1989
1990 if (!window->cairo_surface)
1991 return;
1992
1993 if (!ctx)
1994 ctx = display->ctx;
1995
1996 data = cairo_surface_get_user_data(window->cairo_surface,
1997 &surface_data_key);
1998
1999 cairo_device_acquire(display->device);
2000 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2001 fprintf(stderr, "failed to make surface current\n");
2002}
2003
2004void
2005display_release(struct display *display)
2006{
2007 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2008 fprintf(stderr, "failed to make context current\n");
2009 cairo_device_release(display->device);
2010}
2011
2012void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002013display_run(struct display *d)
2014{
2015 g_main_loop_run(d->loop);
2016}