blob: 95e46d512f8635dbf58e4e56e7eb1334cdd692a3 [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
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200684window_get_resize_dx_dy(struct window *window, int *x, int *y)
685{
686 if (window->resize_edges & WINDOW_RESIZING_LEFT)
687 *x = window->server_allocation.width - window->allocation.width;
688 else
689 *x = 0;
690
691 if (window->resize_edges & WINDOW_RESIZING_TOP)
692 *y = window->server_allocation.height -
693 window->allocation.height;
694 else
695 *y = 0;
696
697 window->resize_edges = 0;
698}
699
700static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500701window_attach_surface(struct window *window)
702{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400703 struct display *display = window->display;
704 struct wl_buffer *buffer;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100705 struct egl_window_surface_data *data;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500706 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100707
708 switch (window->buffer_type) {
709 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
710 data = cairo_surface_get_user_data(window->cairo_surface,
711 &surface_data_key);
712
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713 cairo_gl_surface_swapbuffers(window->cairo_surface);
714 wl_egl_window_get_attached_size(data->window,
715 &window->server_allocation.width,
716 &window->server_allocation.height);
717 break;
718 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
719 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200720 window_get_resize_dx_dy(window, &x, &y);
721
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 if (window->pending_surface != NULL)
723 return;
724
725 window->pending_surface = window->cairo_surface;
726 window->cairo_surface = NULL;
727
728 buffer =
729 display_get_buffer_for_surface(display,
730 window->pending_surface);
731
732 wl_surface_attach(window->surface, buffer, x, y);
733 window->server_allocation = window->allocation;
734 wl_display_sync_callback(display->display, free_surface,
735 window);
736 break;
737 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500738
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500739 if (window->fullscreen)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400740 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500741 else if (!window->parent)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400742 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500743 else
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400744 wl_shell_set_transient(display->shell, window->surface,
745 window->parent->surface,
746 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500747
748 wl_surface_damage(window->surface, 0, 0,
749 window->allocation.width,
750 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500751}
752
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500753void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500755{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100756 if (window->cairo_surface) {
757 switch (window->buffer_type) {
758 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
759 case WINDOW_BUFFER_TYPE_SHM:
760 display_surface_damage(window->display,
761 window->cairo_surface,
762 0, 0,
763 window->allocation.width,
764 window->allocation.height);
765 break;
766 default:
767 break;
768 }
769 window_attach_surface(window);
770 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500771}
772
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400773void
774window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400775{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500776 cairo_surface_reference(surface);
777
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400778 if (window->cairo_surface != NULL)
779 cairo_surface_destroy(window->cairo_surface);
780
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500781 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400782}
783
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784static void
785window_resize_cairo_window_surface(struct window *window)
786{
787 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200788 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789
790 data = cairo_surface_get_user_data(window->cairo_surface,
791 &surface_data_key);
792
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200793 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 wl_egl_window_resize(data->window,
795 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200796 window->allocation.height,
797 x,y);
798
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799 cairo_gl_surface_set_size(window->cairo_surface,
800 window->allocation.width,
801 window->allocation.height);
802}
803
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400804void
805window_create_surface(struct window *window)
806{
807 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400808 uint32_t flags = 0;
809
810 if (!window->transparent)
811 flags = SURFACE_OPAQUE;
812
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400813 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
816 if (window->cairo_surface) {
817 window_resize_cairo_window_surface(window);
818 return;
819 }
820 surface = display_create_surface(window->display,
821 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400822 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500824 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400825 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400827 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828 break;
829#endif
830 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400832 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800834 default:
835 surface = NULL;
836 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400838
839 window_set_surface(window, surface);
840 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841}
842
843static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500844window_draw_menu(struct window *window)
845{
846 cairo_t *cr;
847 int width, height, r = 5;
848
849 window_create_surface(window);
850
851 cr = cairo_create(window->cairo_surface);
852 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
853 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
854 cairo_paint(cr);
855
856 width = window->allocation.width;
857 height = window->allocation.height;
858 rounded_rect(cr, r, r, width - r, height - r, r);
859 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
860 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
861 cairo_fill(cr);
862 cairo_destroy(cr);
863}
864
865static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500866window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500867{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500868 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500869 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400870 cairo_surface_t *frame;
871 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500872
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400873 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400875 width = window->allocation.width;
876 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500877
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500878 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500879
Kristian Høgsberg09531622010-06-14 23:22:15 -0400880 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400881 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400882 cairo_paint(cr);
883
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400884 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400885 tile_mask(cr, window->display->shadow,
886 shadow_dx, shadow_dy, width, height,
887 window->margin + 10 - shadow_dx,
888 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500889
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400890 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400891 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400892 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400893 frame = window->display->inactive_frame;
894
895 tile_source(cr, frame, 0, 0, width, height,
896 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500897
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500898 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
899 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500900 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400901 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400902 if (window->keyboard_device)
903 cairo_set_source_rgb(cr, 0, 0, 0);
904 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400905 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400906 cairo_show_text(cr, window->title);
907
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500908 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400909
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100910 /* FIXME: this breakes gears, fix cairo? */
911#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400912 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100913#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500914}
915
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400916void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500917window_destroy(struct window *window)
918{
919 wl_surface_destroy(window->surface);
920 wl_list_remove(&window->link);
921 free(window);
922}
923
924void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400925display_flush_cairo_device(struct display *display)
926{
927 cairo_device_flush (display->device);
928}
929
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500930void
931window_draw(struct window *window)
932{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500933 if (window->parent)
934 window_draw_menu(window);
935 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400936 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500937 else
938 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500939}
940
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400941cairo_surface_t *
942window_get_surface(struct window *window)
943{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400944 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400945}
946
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100947struct wl_surface *
948window_get_wl_surface(struct window *window)
949{
950 return window->surface;
951}
952
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400953static int
954get_pointer_location(struct window *window, int32_t x, int32_t y)
955{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400956 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400957 const int grip_size = 8;
958
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500959 if (!window->decoration)
960 return WINDOW_CLIENT_AREA;
961
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400962 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400963 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400964 else if (window->margin <= x && x < window->margin + grip_size)
965 hlocation = WINDOW_RESIZING_LEFT;
966 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400967 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400968 else if (x < window->allocation.width - window->margin)
969 hlocation = WINDOW_RESIZING_RIGHT;
970 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400971 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400972
973 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400974 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400975 else if (window->margin <= y && y < window->margin + grip_size)
976 vlocation = WINDOW_RESIZING_TOP;
977 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400978 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400979 else if (y < window->allocation.height - window->margin)
980 vlocation = WINDOW_RESIZING_BOTTOM;
981 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400982 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400983
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400984 location = vlocation | hlocation;
985 if (location & WINDOW_EXTERIOR)
986 location = WINDOW_EXTERIOR;
987 if (location == WINDOW_INTERIOR && y < window->margin + 50)
988 location = WINDOW_TITLEBAR;
989 else if (location == WINDOW_INTERIOR)
990 location = WINDOW_CLIENT_AREA;
991
992 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400993}
994
995static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400996set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400997{
998 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400999 struct wl_buffer *buffer;
1000 cairo_surface_t *surface;
1001 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001002
1003 location = get_pointer_location(input->pointer_focus,
1004 input->sx, input->sy);
1005 switch (location) {
1006 case WINDOW_RESIZING_TOP:
1007 pointer = POINTER_TOP;
1008 break;
1009 case WINDOW_RESIZING_BOTTOM:
1010 pointer = POINTER_BOTTOM;
1011 break;
1012 case WINDOW_RESIZING_LEFT:
1013 pointer = POINTER_LEFT;
1014 break;
1015 case WINDOW_RESIZING_RIGHT:
1016 pointer = POINTER_RIGHT;
1017 break;
1018 case WINDOW_RESIZING_TOP_LEFT:
1019 pointer = POINTER_TOP_LEFT;
1020 break;
1021 case WINDOW_RESIZING_TOP_RIGHT:
1022 pointer = POINTER_TOP_RIGHT;
1023 break;
1024 case WINDOW_RESIZING_BOTTOM_LEFT:
1025 pointer = POINTER_BOTTOM_LEFT;
1026 break;
1027 case WINDOW_RESIZING_BOTTOM_RIGHT:
1028 pointer = POINTER_BOTTOM_RIGHT;
1029 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001030 case WINDOW_EXTERIOR:
1031 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001032 if (input->current_pointer_image == POINTER_DEFAULT)
1033 return;
1034
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001035 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001036 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001037 return;
1038 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001039 break;
1040 }
1041
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001042 if (pointer == input->current_pointer_image)
1043 return;
1044
1045 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001046 surface = display->pointer_surfaces[pointer];
1047 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001048 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001049 pointer_images[pointer].hotspot_x,
1050 pointer_images[pointer].hotspot_y);
1051}
1052
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001053static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001054window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001055 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001056 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001057{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001058 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001059 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001060 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001061
1062 input->x = x;
1063 input->y = y;
1064 input->sx = sx;
1065 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001066
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001067 if (window->motion_handler)
1068 pointer = (*window->motion_handler)(window, input, time,
1069 x, y, sx, sy,
1070 window->user_data);
1071
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001072 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001073}
1074
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001075static void
1076window_handle_button(void *data,
1077 struct wl_input_device *input_device,
1078 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001079{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001080 struct input *input = data;
1081 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001082 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001083
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001084 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001085
1086 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001087 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001088 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001089 wl_shell_move(window->display->shell,
1090 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001091 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001092 case WINDOW_RESIZING_TOP:
1093 case WINDOW_RESIZING_BOTTOM:
1094 case WINDOW_RESIZING_LEFT:
1095 case WINDOW_RESIZING_RIGHT:
1096 case WINDOW_RESIZING_TOP_LEFT:
1097 case WINDOW_RESIZING_TOP_RIGHT:
1098 case WINDOW_RESIZING_BOTTOM_LEFT:
1099 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001100 wl_shell_resize(window->display->shell,
1101 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001102 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001103 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001104 case WINDOW_CLIENT_AREA:
1105 if (window->button_handler)
1106 (*window->button_handler)(window,
1107 input, time,
1108 button, state,
1109 window->user_data);
1110 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001111 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001112 } else {
1113 if (window->button_handler)
1114 (*window->button_handler)(window,
1115 input, time,
1116 button, state,
1117 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001118 }
1119}
1120
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001121static void
1122window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001123 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001124{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001125 struct input *input = data;
1126 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001127 struct display *d = window->display;
1128 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001129
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001130 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001131 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001132 return;
1133
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001134 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001135 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001136 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1137 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001138
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001139 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1140
1141 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001142 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001143 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001144 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001145
1146 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001147 (*window->key_handler)(window, input, time, key, sym, state,
1148 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001149}
1150
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001151static void
1152window_handle_pointer_focus(void *data,
1153 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001154 uint32_t time, struct wl_surface *surface,
1155 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001156{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001157 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001158 struct window *window;
1159 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001160
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001162 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001163 window = input->pointer_focus;
1164
Kristian Høgsberg59826582011-01-20 11:56:57 -05001165 input->x = x;
1166 input->y = y;
1167 input->sx = sx;
1168 input->sy = sy;
1169
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001170 pointer = POINTER_LEFT_PTR;
1171 if (window->motion_handler)
1172 pointer = (*window->motion_handler)(window,
1173 input, time,
1174 x, y, sx, sy,
1175 window->user_data);
1176
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001177 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001178 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001179 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001180 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001181 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001182}
1183
1184static void
1185window_handle_keyboard_focus(void *data,
1186 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001187 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001188 struct wl_surface *surface,
1189 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001190{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001191 struct input *input = data;
1192 struct window *window = input->keyboard_focus;
1193 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001194 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001195
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001196 window = input->keyboard_focus;
1197 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001198 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001199 if (window->keyboard_focus_handler)
1200 (*window->keyboard_focus_handler)(window, NULL,
1201 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001202 }
1203
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001204 if (surface)
1205 input->keyboard_focus = wl_surface_get_user_data(surface);
1206 else
1207 input->keyboard_focus = NULL;
1208
1209 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001210 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001211 for (k = keys->data; k < end; k++)
1212 input->modifiers |= d->xkb->map->modmap[*k];
1213
1214 window = input->keyboard_focus;
1215 if (window) {
1216 window->keyboard_device = input;
1217 if (window->keyboard_focus_handler)
1218 (*window->keyboard_focus_handler)(window,
1219 window->keyboard_device,
1220 window->user_data);
1221 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001222}
1223
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001224static const struct wl_input_device_listener input_device_listener = {
1225 window_handle_motion,
1226 window_handle_button,
1227 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001228 window_handle_pointer_focus,
1229 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001230};
1231
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001232void
1233input_get_position(struct input *input, int32_t *x, int32_t *y)
1234{
1235 *x = input->sx;
1236 *y = input->sy;
1237}
1238
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001239struct wl_input_device *
1240input_get_input_device(struct input *input)
1241{
1242 return input->input_device;
1243}
1244
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001245uint32_t
1246input_get_modifiers(struct input *input)
1247{
1248 return input->modifiers;
1249}
1250
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001251struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001252window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001253{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 cairo_device_flush (window->display->device);
1255
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001256 return wl_shell_create_drag(window->display->shell);
1257}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001258
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001259void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001260window_move(struct window *window, struct input *input, uint32_t time)
1261{
1262 wl_shell_move(window->display->shell,
1263 window->surface, input->input_device, time);
1264}
1265
1266void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001267window_activate_drag(struct wl_drag *drag, struct window *window,
1268 struct input *input, uint32_t time)
1269{
1270 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001271}
1272
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001273static void
1274handle_configure(void *data, struct wl_shell *shell,
1275 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001276 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001277{
1278 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001279 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001280
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001281 /* FIXME: this is probably the wrong place to check for width
1282 * or height <= 0, but it prevents the compositor from crashing
1283 */
1284 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001285 return;
1286
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001287 window->resize_edges = edges;
1288
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001289 if (window->resize_handler) {
1290 child_width = width - 20 - window->margin * 2;
1291 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001292
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001293 (*window->resize_handler)(window,
1294 child_width, child_height,
1295 window->user_data);
1296 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001297 window->allocation.width = width;
1298 window->allocation.height = height;
1299
1300 if (window->redraw_handler)
1301 window_schedule_redraw(window);
1302 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001303}
1304
1305static const struct wl_shell_listener shell_listener = {
1306 handle_configure,
1307};
1308
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001309void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001310window_get_allocation(struct window *window,
1311 struct rectangle *allocation)
1312{
1313 *allocation = window->allocation;
1314}
1315
1316void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001317window_get_child_allocation(struct window *window,
1318 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001319{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001320 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001321 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001322 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001323 allocation->x = window->margin + 10;
1324 allocation->y = window->margin + 50;
1325 allocation->width =
1326 window->allocation.width - 20 - window->margin * 2;
1327 allocation->height =
1328 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001329 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001330}
1331
1332void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001333window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001334{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001335 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001336 window->allocation.x = 20 + window->margin;
1337 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001338 window->allocation.width = width + 20 + window->margin * 2;
1339 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001340 } else {
1341 window->allocation.x = 0;
1342 window->allocation.y = 0;
1343 window->allocation.width = width;
1344 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001345 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001346}
1347
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001348static gboolean
1349idle_redraw(void *data)
1350{
1351 struct window *window = data;
1352
1353 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001354
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001355 window->redraw_scheduled = 0;
1356
1357 return FALSE;
1358}
1359
1360void
1361window_schedule_redraw(struct window *window)
1362{
1363 if (!window->redraw_scheduled) {
1364 g_idle_add(idle_redraw, window);
1365 window->redraw_scheduled = 1;
1366 }
1367}
1368
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001369void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001370window_set_fullscreen(struct window *window, int fullscreen)
1371{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001372 int32_t width, height;
1373
1374 if (window->fullscreen == fullscreen)
1375 return;
1376
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001377 window->fullscreen = fullscreen;
1378 if (window->fullscreen) {
1379 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001380 width = window->display->screen_allocation.width;
1381 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001382 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001383 width = window->saved_allocation.width - 20 - window->margin * 2;
1384 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001385 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001386
1387 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001388}
1389
1390void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001391window_set_decoration(struct window *window, int decoration)
1392{
1393 window->decoration = decoration;
1394}
1395
1396void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001397window_set_user_data(struct window *window, void *data)
1398{
1399 window->user_data = data;
1400}
1401
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001402void *
1403window_get_user_data(struct window *window)
1404{
1405 return window->user_data;
1406}
1407
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001408void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001409window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001410 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001411{
1412 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001413}
1414
1415void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001416window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001417 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001418{
1419 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001420}
1421
1422void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001423window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001424 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001425{
1426 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001427}
1428
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001429void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001430window_set_button_handler(struct window *window,
1431 window_button_handler_t handler)
1432{
1433 window->button_handler = handler;
1434}
1435
1436void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001437window_set_motion_handler(struct window *window,
1438 window_motion_handler_t handler)
1439{
1440 window->motion_handler = handler;
1441}
1442
1443void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001444window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001445 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001446{
1447 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001448}
1449
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001450void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001451window_set_transparent(struct window *window, int transparent)
1452{
1453 window->transparent = transparent;
1454}
1455
1456void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001457window_set_title(struct window *window, const char *title)
1458{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001459 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001460 window->title = strdup(title);
1461}
1462
1463const char *
1464window_get_title(struct window *window)
1465{
1466 return window->title;
1467}
1468
1469void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001470display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1471 int32_t x, int32_t y, int32_t width, int32_t height)
1472{
1473 struct wl_buffer *buffer;
1474
1475 buffer = display_get_buffer_for_surface(display, cairo_surface);
1476
1477 wl_buffer_damage(buffer, x, y, width, height);
1478}
1479
1480void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001481window_damage(struct window *window, int32_t x, int32_t y,
1482 int32_t width, int32_t height)
1483{
1484 wl_surface_damage(window->surface, x, y, width, height);
1485}
1486
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001487static struct window *
1488window_create_internal(struct display *display, struct window *parent,
1489 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001490{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001491 struct window *window;
1492
1493 window = malloc(sizeof *window);
1494 if (window == NULL)
1495 return NULL;
1496
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001497 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001498 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001499 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001500 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001501 window->allocation.x = 0;
1502 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001503 window->allocation.width = width;
1504 window->allocation.height = height;
1505 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001506 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001507 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001508 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001509
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001510 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001511 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1512 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001513 else
1514 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001515
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001516 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001517 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001518
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001519 return window;
1520}
1521
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001522struct window *
1523window_create(struct display *display, int32_t width, int32_t height)
1524{
1525 struct window *window;
1526
1527 window = window_create_internal(display, NULL, width, height);
1528 if (!window)
1529 return NULL;
1530
1531 return window;
1532}
1533
1534struct window *
1535window_create_transient(struct display *display, struct window *parent,
1536 int32_t x, int32_t y, int32_t width, int32_t height)
1537{
1538 struct window *window;
1539
1540 window = window_create_internal(parent->display,
1541 parent, width, height);
1542 if (!window)
1543 return NULL;
1544
1545 window->x = x;
1546 window->y = y;
1547
1548 return window;
1549}
1550
1551void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001552window_set_buffer_type(struct window *window, enum window_buffer_type type)
1553{
1554 window->buffer_type = type;
1555}
1556
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001557static void
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001558compositor_handle_visual(void *data,
1559 struct wl_compositor *compositor,
1560 uint32_t id, uint32_t token)
1561{
1562 struct display *d = data;
1563
1564 switch (token) {
1565 case WL_COMPOSITOR_VISUAL_ARGB32:
1566 d->argb_visual = wl_visual_create(d->display, id, 1);
1567 break;
1568 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
1569 d->premultiplied_argb_visual =
1570 wl_visual_create(d->display, id, 1);
1571 break;
1572 case WL_COMPOSITOR_VISUAL_XRGB32:
1573 d->rgb_visual = wl_visual_create(d->display, id, 1);
1574 break;
1575 }
1576}
1577
1578static const struct wl_compositor_listener compositor_listener = {
1579 compositor_handle_visual,
1580};
1581
1582static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001583display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001584 struct wl_output *wl_output,
1585 int x, int y,
1586 int physical_width,
1587 int physical_height,
1588 int subpixel,
1589 const char *make,
1590 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001591{
1592 struct display *display = data;
1593
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001594 display->screen_allocation.x = x;
1595 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001596}
1597
1598static void
1599display_handle_mode(void *data,
1600 struct wl_output *wl_output,
1601 uint32_t flags,
1602 int width,
1603 int height,
1604 int refresh)
1605{
1606 struct display *display = data;
1607
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001608 display->screen_allocation.width = width;
1609 display->screen_allocation.height = height;
1610}
1611
1612static const struct wl_output_listener output_listener = {
1613 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001614 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001615};
1616
1617static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001618display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001619{
1620 struct input *input;
1621
1622 input = malloc(sizeof *input);
1623 if (input == NULL)
1624 return;
1625
1626 memset(input, 0, sizeof *input);
1627 input->display = d;
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001628 input->input_device = wl_input_device_create(d->display, id, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001629 input->pointer_focus = NULL;
1630 input->keyboard_focus = NULL;
1631 wl_list_insert(d->input_list.prev, &input->link);
1632
1633 wl_input_device_add_listener(input->input_device,
1634 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001635 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001636}
1637
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001638struct selection_offer {
1639 struct display *display;
1640 struct wl_selection_offer *offer;
1641 struct wl_array types;
1642 struct input *input;
1643};
1644
1645int
1646input_offers_mime_type(struct input *input, const char *type)
1647{
1648 struct selection_offer *offer = input->offer;
1649 char **p, **end;
1650
1651 if (offer == NULL)
1652 return 0;
1653
1654 end = offer->types.data + offer->types.size;
1655 for (p = offer->types.data; p < end; p++)
1656 if (strcmp(*p, type) == 0)
1657 return 1;
1658
1659 return 0;
1660}
1661
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001662void
1663input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001664{
1665 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001666
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001667 /* FIXME: A number of things can go wrong here: the object may
1668 * not be the current selection offer any more (which could
1669 * still work, but the source may have gone away or just
1670 * destroyed its wl_selection) or the offer may not have the
1671 * requested type after all (programmer/client error,
1672 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001673 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001674}
1675
1676static void
1677selection_offer_offer(void *data,
1678 struct wl_selection_offer *selection_offer,
1679 const char *type)
1680{
1681 struct selection_offer *offer = data;
1682
1683 char **p;
1684
1685 p = wl_array_add(&offer->types, sizeof *p);
1686 if (p)
1687 *p = strdup(type);
1688};
1689
1690static void
1691selection_offer_keyboard_focus(void *data,
1692 struct wl_selection_offer *selection_offer,
1693 struct wl_input_device *input_device)
1694{
1695 struct selection_offer *offer = data;
1696 struct input *input;
1697 char **p, **end;
1698
1699 if (input_device == NULL) {
1700 printf("selection offer retracted %p\n", selection_offer);
1701 input = offer->input;
1702 input->offer = NULL;
1703 wl_selection_offer_destroy(selection_offer);
1704 wl_array_release(&offer->types);
1705 free(offer);
1706 return;
1707 }
1708
1709 input = wl_input_device_get_user_data(input_device);
1710 printf("new selection offer %p:", selection_offer);
1711
1712 offer->input = input;
1713 input->offer = offer;
1714 end = offer->types.data + offer->types.size;
1715 for (p = offer->types.data; p < end; p++)
1716 printf(" %s", *p);
1717
1718 printf("\n");
1719}
1720
1721struct wl_selection_offer_listener selection_offer_listener = {
1722 selection_offer_offer,
1723 selection_offer_keyboard_focus
1724};
1725
1726static void
1727add_selection_offer(struct display *d, uint32_t id)
1728{
1729 struct selection_offer *offer;
1730
1731 offer = malloc(sizeof *offer);
1732 if (offer == NULL)
1733 return;
1734
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001735 offer->offer = wl_selection_offer_create(d->display, id, 1);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001736 offer->display = d;
1737 wl_array_init(&offer->types);
1738 offer->input = NULL;
1739
1740 wl_selection_offer_add_listener(offer->offer,
1741 &selection_offer_listener, offer);
1742}
1743
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001744static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001745display_handle_global(struct wl_display *display, uint32_t id,
1746 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001747{
1748 struct display *d = data;
1749
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001750 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001751 d->compositor = wl_compositor_create(display, id, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001752 wl_compositor_add_listener(d->compositor,
1753 &compositor_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001754 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001755 d->output = wl_output_create(display, id, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001756 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001757 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001758 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001759 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001760 d->shell = wl_shell_create(display, id, 1);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001761 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001762 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001763 d->shm = wl_shm_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001764 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001765 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001766 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001767 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001768 }
1769}
1770
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001771static void
1772display_render_frame(struct display *d)
1773{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001774 int radius = 8;
1775 cairo_t *cr;
1776
1777 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1778 cr = cairo_create(d->shadow);
1779 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1780 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1781 rounded_rect(cr, 16, 16, 112, 112, radius);
1782 cairo_fill(cr);
1783 cairo_destroy(cr);
1784 blur_surface(d->shadow, 64);
1785
1786 d->active_frame =
1787 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1788 cr = cairo_create(d->active_frame);
1789 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1790 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1791 rounded_rect(cr, 16, 16, 112, 112, radius);
1792 cairo_fill(cr);
1793 cairo_destroy(cr);
1794
1795 d->inactive_frame =
1796 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1797 cr = cairo_create(d->inactive_frame);
1798 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1799 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1800 rounded_rect(cr, 16, 16, 112, 112, radius);
1801 cairo_fill(cr);
1802 cairo_destroy(cr);
1803}
1804
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001805static void
1806init_xkb(struct display *d)
1807{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001808 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001809
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001810 names.rules = "evdev";
1811 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001812 names.layout = option_xkb_layout;
1813 names.variant = option_xkb_variant;
1814 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001815
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001816 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001817 if (!d->xkb) {
1818 fprintf(stderr, "Failed to compile keymap\n");
1819 exit(1);
1820 }
1821}
1822
Yuval Fledel45568f62010-12-06 09:18:12 -05001823static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001824init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001825{
1826 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001827 EGLint n;
1828 static const EGLint cfg_attribs[] = {
1829 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1830 EGL_RED_SIZE, 1,
1831 EGL_GREEN_SIZE, 1,
1832 EGL_BLUE_SIZE, 1,
1833 EGL_ALPHA_SIZE, 1,
1834 EGL_DEPTH_SIZE, 1,
1835 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1836 EGL_NONE
1837 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001838
Egbert Eiche7b8d902011-05-10 20:00:19 +00001839 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001840 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001841 if (!eglInitialize(d->dpy, &major, &minor)) {
1842 fprintf(stderr, "failed to initialize display\n");
1843 return -1;
1844 }
1845
1846 if (!eglBindAPI(EGL_OPENGL_API)) {
1847 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1848 return -1;
1849 }
1850
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001851 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1852 fprintf(stderr, "failed to choose config\n");
1853 return -1;
1854 }
1855
1856 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001857 if (d->ctx == NULL) {
1858 fprintf(stderr, "failed to create context\n");
1859 return -1;
1860 }
1861
1862 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001863 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001864 return -1;
1865 }
1866
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001867#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001868 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001869 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001870 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001871 return -1;
1872 }
1873#endif
1874
1875 return 0;
1876}
1877
Benjamin Franzke65e50512011-05-31 11:36:31 +02001878static void
1879sync_callback(void *data)
1880{
1881 int *done = data;
1882
1883 *done = 1;
1884}
1885
1886static void
1887force_roundtrip(struct display *d)
1888{
1889 int done = 0;
1890
1891 wl_display_sync_callback(d->display, sync_callback, &done);
1892 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1893 while (!done)
1894 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1895}
1896
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001897struct display *
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001898display_create(int *argc, char **argv[], const GOptionEntry *option_entries,
1899 display_global_handler_t handler)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001900{
1901 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001902 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001903 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001904 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001905
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001906 g_type_init();
1907
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001908 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001909 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001910 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001911
1912 xkb_option_group = g_option_group_new("xkb",
1913 "Keyboard options",
1914 "Show all XKB options",
1915 NULL, NULL);
1916 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1917 g_option_context_add_group (context, xkb_option_group);
1918
1919 if (!g_option_context_parse(context, argc, argv, &error)) {
1920 fprintf(stderr, "option parsing failed: %s\n", error->message);
1921 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001922 }
1923
Tim Wiederhake748f6722011-01-23 23:25:25 +01001924 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001925
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001926 d = malloc(sizeof *d);
1927 if (d == NULL)
1928 return NULL;
1929
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001930 memset(d, 0, sizeof *d);
1931
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001932 d->global_handler = handler;
1933
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001934 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001935 if (d->display == NULL) {
1936 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001937 return NULL;
1938 }
1939
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001940 wl_list_init(&d->input_list);
1941
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001942 /* Set up listener so we'll catch all events. */
1943 wl_display_add_global_listener(d->display,
1944 display_handle_global, d);
1945
1946 /* Process connection events. */
1947 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001948 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001949 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001950
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001951 d->image_target_texture_2d =
1952 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1953 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1954 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1955
Benjamin Franzke65e50512011-05-31 11:36:31 +02001956 if (!d->premultiplied_argb_visual || !d->rgb_visual) {
1957 force_roundtrip(d);
1958 if (!d->premultiplied_argb_visual || !d->rgb_visual) {
1959 fprintf(stderr, "failed to retreive visuals\n");
1960 return NULL;
1961 }
1962 }
1963
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001964 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001965
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001966 display_render_frame(d);
1967
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001968 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001969 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001970 g_source_attach(d->source, NULL);
1971
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001972 wl_list_init(&d->window_list);
1973
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001974 init_xkb(d);
1975
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001976 return d;
1977}
1978
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001979struct wl_display *
1980display_get_display(struct display *display)
1981{
1982 return display->display;
1983}
1984
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001985struct wl_compositor *
1986display_get_compositor(struct display *display)
1987{
1988 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001989}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001990
1991EGLDisplay
1992display_get_egl_display(struct display *d)
1993{
1994 return d->dpy;
1995}
1996
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001997EGLConfig
1998display_get_egl_config(struct display *d)
1999{
2000 return d->conf;
2001}
2002
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002003struct wl_shell *
2004display_get_shell(struct display *display)
2005{
2006 return display->shell;
2007}
2008
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002009void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002010display_acquire_window_surface(struct display *display,
2011 struct window *window,
2012 EGLContext ctx)
2013{
2014 struct egl_window_surface_data *data;
2015
2016 if (!window->cairo_surface)
2017 return;
2018
2019 if (!ctx)
2020 ctx = display->ctx;
2021
2022 data = cairo_surface_get_user_data(window->cairo_surface,
2023 &surface_data_key);
2024
2025 cairo_device_acquire(display->device);
2026 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2027 fprintf(stderr, "failed to make surface current\n");
2028}
2029
2030void
2031display_release(struct display *display)
2032{
2033 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2034 fprintf(stderr, "failed to make context current\n");
2035 cairo_device_release(display->device);
2036}
2037
2038void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002039display_run(struct display *d)
2040{
2041 g_main_loop_run(d->loop);
2042}