blob: d1c33a2c992469648853d6100e2e3b30403b388b [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øgsberg808fd412010-07-20 17:06:19 -0400102 struct input *grab_device;
103 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400105 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500106
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400107 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500108 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500109
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500110 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400111 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500112 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400113 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500114 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400115 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400116
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500117 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119};
120
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400121struct input {
122 struct display *display;
123 struct wl_input_device *input_device;
124 struct window *pointer_focus;
125 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500126 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400127 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 uint32_t modifiers;
129 int32_t x, y, sx, sy;
130 struct wl_list link;
131};
132
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400133enum {
134 POINTER_DEFAULT = 100,
135 POINTER_UNSET
136};
137
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138enum window_location {
139 WINDOW_INTERIOR = 0,
140 WINDOW_RESIZING_TOP = 1,
141 WINDOW_RESIZING_BOTTOM = 2,
142 WINDOW_RESIZING_LEFT = 4,
143 WINDOW_RESIZING_TOP_LEFT = 5,
144 WINDOW_RESIZING_BOTTOM_LEFT = 6,
145 WINDOW_RESIZING_RIGHT = 8,
146 WINDOW_RESIZING_TOP_RIGHT = 9,
147 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
148 WINDOW_RESIZING_MASK = 15,
149 WINDOW_EXTERIOR = 16,
150 WINDOW_TITLEBAR = 17,
151 WINDOW_CLIENT_AREA = 18,
152};
153
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400154const char *option_xkb_layout = "us";
155const char *option_xkb_variant = "";
156const char *option_xkb_options = "";
157
158static const GOptionEntry xkb_option_entries[] = {
159 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
160 &option_xkb_layout, "XKB Layout" },
161 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
162 &option_xkb_variant, "XKB Variant" },
163 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
164 &option_xkb_options, "XKB Options" },
165 { NULL }
166};
167
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500168static void
169rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
170{
171 cairo_move_to(cr, x0, y0 + radius);
172 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
173 cairo_line_to(cr, x1 - radius, y0);
174 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
175 cairo_line_to(cr, x1, y1 - radius);
176 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
177 cairo_line_to(cr, x0 + radius, y1);
178 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
179 cairo_close_path(cr);
180}
181
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400182static const cairo_user_data_key_t surface_data_key;
183struct surface_data {
184 struct wl_buffer *buffer;
185};
186
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500187#define MULT(_d,c,a,t) \
188 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
189
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500190#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100192struct egl_window_surface_data {
193 struct display *display;
194 struct wl_surface *surface;
195 struct wl_egl_window *window;
196 EGLSurface surf;
197};
198
199static void
200egl_window_surface_data_destroy(void *p)
201{
202 struct egl_window_surface_data *data = p;
203 struct display *d = data->display;
204
205 eglDestroySurface(d->dpy, data->surf);
206 wl_egl_window_destroy(data->window);
207 data->surface = NULL;
208
209 free(p);
210}
211
212static cairo_surface_t *
213display_create_egl_window_surface(struct display *display,
214 struct wl_surface *surface,
215 struct rectangle *rectangle)
216{
217 cairo_surface_t *cairo_surface;
218 struct egl_window_surface_data *data;
219 struct wl_visual *visual;
220
221 data = malloc(sizeof *data);
222 if (data == NULL)
223 return NULL;
224
225 data->display = display;
226 data->surface = surface;
227
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400228 visual = display->premultiplied_argb_visual;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100229
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400230 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100231 rectangle->width,
232 rectangle->height,
233 visual);
234
235 data->surf = eglCreateWindowSurface(display->dpy, display->conf,
236 data->window, NULL);
237
238 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
239 data->surf,
240 rectangle->width,
241 rectangle->height);
242
243 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
244 data, egl_window_surface_data_destroy);
245
246 return cairo_surface;
247}
248
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500249struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400250 struct surface_data data;
251 EGLImageKHR image;
252 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800253 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500254 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400255};
256
257static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500258egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400259{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500260 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800261 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400262
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800263 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400264 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800265 cairo_device_release(d->device);
266
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500267 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400268 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500269 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500270 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400271}
272
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500273EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500274display_get_image_for_egl_image_surface(struct display *display,
275 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500276{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500277 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500278
279 data = cairo_surface_get_user_data (surface, &surface_data_key);
280
281 return data->image;
282}
283
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500284static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500285display_create_egl_image_surface(struct display *display,
286 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;
291 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400292
293 data = malloc(sizeof *data);
294 if (data == NULL)
295 return NULL;
296
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800297 data->display = display;
298
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400299 visual = display->premultiplied_argb_visual;
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400300 data->pixmap = wl_egl_pixmap_create(rectangle->width,
301 rectangle->height,
302 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500303 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000304 free(data);
305 return NULL;
306 }
307
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500308 data->image = display->create_image(dpy, NULL,
309 EGL_NATIVE_PIXMAP_KHR,
310 (EGLClientBuffer) data->pixmap,
311 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500313 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500314 free(data);
315 return NULL;
316 }
317
318 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400319 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800321 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500324 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800325 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327 surface = cairo_gl_surface_create_for_texture(display->device,
328 CAIRO_CONTENT_COLOR_ALPHA,
329 data->texture,
330 rectangle->width,
331 rectangle->height);
332
333 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
336 return surface;
337}
338
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500339static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500340display_create_egl_image_surface_from_file(struct display *display,
341 const char *filename,
342 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400343{
344 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400345 GdkPixbuf *pixbuf;
346 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400347 int stride, i;
348 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400350
351 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400353 FALSE, &error);
354 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400356
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400357 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
358 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500359 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400361 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400362
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400363
364 stride = gdk_pixbuf_get_rowstride(pixbuf);
365 pixels = gdk_pixbuf_get_pixels(pixbuf);
366
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400367 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400368 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400370 while (p < end) {
371 unsigned int t;
372
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400373 MULT(p[0], p[0], p[3], t);
374 MULT(p[1], p[1], p[3], t);
375 MULT(p[2], p[2], p[3], t);
376 p += 4;
377
378 }
379 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400380
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381 surface = display_create_egl_image_surface(display, 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,
430 struct rectangle *rectangle)
431{
432 struct shm_surface_data *data;
433 cairo_surface_t *surface;
434 struct wl_visual *visual;
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øgsberg8357cd62011-05-13 13:24:56 -0400475 visual = display->premultiplied_argb_visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400476 data->data.buffer = wl_shm_create_buffer(display->shm,
477 fd,
478 rectangle->width,
479 rectangle->height,
480 stride, visual);
481
482 close(fd);
483
484 return surface;
485}
486
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500487static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488display_create_shm_surface_from_file(struct display *display,
489 const char *filename,
490 struct rectangle *rect)
491{
492 cairo_surface_t *surface;
493 GdkPixbuf *pixbuf;
494 GError *error = NULL;
495 int stride, i;
496 unsigned char *pixels, *p, *end, *dest_data;
497 int dest_stride;
498 uint32_t *d;
499
500 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
501 rect->width, rect->height,
502 FALSE, &error);
503 if (error != NULL)
504 return NULL;
505
506 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
507 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500508 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509 return NULL;
510 }
511
512 stride = gdk_pixbuf_get_rowstride(pixbuf);
513 pixels = gdk_pixbuf_get_pixels(pixbuf);
514
515 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000516 if (surface == NULL) {
517 g_object_unref(pixbuf);
518 return NULL;
519 }
520
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521 dest_data = cairo_image_surface_get_data (surface);
522 dest_stride = cairo_image_surface_get_stride (surface);
523
524 for (i = 0; i < rect->height; i++) {
525 d = (uint32_t *) (dest_data + i * dest_stride);
526 p = pixels + i * stride;
527 end = p + rect->width * 4;
528 while (p < end) {
529 unsigned int t;
530 unsigned char a, r, g, b;
531
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532 a = p[3];
533 MULT(r, p[0], a, t);
534 MULT(g, p[1], a, t);
535 MULT(b, p[2], a, t);
536 p += 4;
537 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
538 }
539 }
540
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500541 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542
543 return surface;
544}
545
nobled7b87cb02011-02-01 18:51:47 +0000546static int
547check_size(struct rectangle *rect)
548{
549 if (rect->width && rect->height)
550 return 0;
551
552 fprintf(stderr, "tried to create surface of "
553 "width: %d, height: %d\n", rect->width, rect->height);
554 return -1;
555}
556
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557cairo_surface_t *
558display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100559 struct wl_surface *surface,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560 struct rectangle *rectangle)
561{
nobled7b87cb02011-02-01 18:51:47 +0000562 if (check_size(rectangle) < 0)
563 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500564#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500565 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100566 if (surface)
567 return display_create_egl_window_surface(display,
568 surface,
569 rectangle);
570 else
571 return display_create_egl_image_surface(display,
572 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500573 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500575 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576}
577
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500578static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579display_create_surface_from_file(struct display *display,
580 const char *filename,
581 struct rectangle *rectangle)
582{
nobled7b87cb02011-02-01 18:51:47 +0000583 if (check_size(rectangle) < 0)
584 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500585#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500586 if (display->dpy) {
587 return display_create_egl_image_surface_from_file(display,
588 filename,
589 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500590 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500592 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400593}
Yuval Fledel45568f62010-12-06 09:18:12 -0500594 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400595 const char *filename;
596 int hotspot_x, hotspot_y;
597} pointer_images[] = {
598 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
599 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
600 { DATADIR "/wayland/bottom_side.png", 16, 20 },
601 { DATADIR "/wayland/grabbing.png", 20, 17 },
602 { DATADIR "/wayland/left_ptr.png", 10, 5 },
603 { DATADIR "/wayland/left_side.png", 10, 20 },
604 { DATADIR "/wayland/right_side.png", 30, 19 },
605 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
606 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
607 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400608 { DATADIR "/wayland/xterm.png", 15, 15 },
609 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400610};
611
612static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400613create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400614{
615 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400616 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400617 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400619 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400620 display->pointer_surfaces =
621 malloc(count * sizeof *display->pointer_surfaces);
622 rect.width = width;
623 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400624 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400625 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626 display_create_surface_from_file(display,
627 pointer_images[i].filename,
628 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400629 }
630
631}
632
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400633cairo_surface_t *
634display_get_pointer_surface(struct display *display, int pointer,
635 int *width, int *height,
636 int *hotspot_x, int *hotspot_y)
637{
638 cairo_surface_t *surface;
639
640 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400642 *width = cairo_gl_surface_get_width(surface);
643 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000644#else
645 *width = cairo_image_surface_get_width(surface);
646 *height = cairo_image_surface_get_height(surface);
647#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400648 *hotspot_x = pointer_images[pointer].hotspot_x;
649 *hotspot_y = pointer_images[pointer].hotspot_y;
650
651 return cairo_surface_reference(surface);
652}
653
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400654
655static void
656window_attach_surface(struct window *window);
657
658static void
659free_surface(void *data)
660{
661 struct window *window = data;
662
663 cairo_surface_destroy(window->pending_surface);
664 window->pending_surface = NULL;
665 if (window->cairo_surface)
666 window_attach_surface(window);
667}
668
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500669static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500670window_attach_surface(struct window *window)
671{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400672 struct display *display = window->display;
673 struct wl_buffer *buffer;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100674 struct egl_window_surface_data *data;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500675 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676 int width = window->allocation.width;
677 int height = window->allocation.height;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500678
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500679 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100680 x = window->server_allocation.width - width;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500681 else
682 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500683
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500684 if (window->resize_edges & WINDOW_RESIZING_TOP)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100685 y = window->server_allocation.height - height;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500686 else
687 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400688
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500689 window->resize_edges = 0;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100690
691 switch (window->buffer_type) {
692 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
693 data = cairo_surface_get_user_data(window->cairo_surface,
694 &surface_data_key);
695
696 wl_egl_window_resize(data->window, width, height, x, y);
697 cairo_gl_surface_swapbuffers(window->cairo_surface);
698 wl_egl_window_get_attached_size(data->window,
699 &window->server_allocation.width,
700 &window->server_allocation.height);
701 break;
702 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
703 case WINDOW_BUFFER_TYPE_SHM:
704 if (window->pending_surface != NULL)
705 return;
706
707 window->pending_surface = window->cairo_surface;
708 window->cairo_surface = NULL;
709
710 buffer =
711 display_get_buffer_for_surface(display,
712 window->pending_surface);
713
714 wl_surface_attach(window->surface, buffer, x, y);
715 window->server_allocation = window->allocation;
716 wl_display_sync_callback(display->display, free_surface,
717 window);
718 break;
719 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500720
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500721 if (window->fullscreen)
722 wl_surface_map_fullscreen(window->surface);
723 else if (!window->parent)
724 wl_surface_map_toplevel(window->surface);
725 else
726 wl_surface_map_transient(window->surface,
727 window->parent->surface,
728 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500729
730 wl_surface_damage(window->surface, 0, 0,
731 window->allocation.width,
732 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500733}
734
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500735void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400736window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500737{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100738 if (window->cairo_surface) {
739 switch (window->buffer_type) {
740 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
741 case WINDOW_BUFFER_TYPE_SHM:
742 display_surface_damage(window->display,
743 window->cairo_surface,
744 0, 0,
745 window->allocation.width,
746 window->allocation.height);
747 break;
748 default:
749 break;
750 }
751 window_attach_surface(window);
752 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500753}
754
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400755void
756window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400757{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500758 cairo_surface_reference(surface);
759
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400760 if (window->cairo_surface != NULL)
761 cairo_surface_destroy(window->cairo_surface);
762
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500763 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400764}
765
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100766static void
767window_resize_cairo_window_surface(struct window *window)
768{
769 struct egl_window_surface_data *data;
770
771 data = cairo_surface_get_user_data(window->cairo_surface,
772 &surface_data_key);
773
774 wl_egl_window_resize(data->window,
775 window->allocation.width,
776 window->allocation.height, 0, 0);
777 cairo_gl_surface_set_size(window->cairo_surface,
778 window->allocation.width,
779 window->allocation.height);
780}
781
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400782void
783window_create_surface(struct window *window)
784{
785 cairo_surface_t *surface;
786
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400787 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500788#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
790 if (window->cairo_surface) {
791 window_resize_cairo_window_surface(window);
792 return;
793 }
794 surface = display_create_surface(window->display,
795 window->surface,
796 &window->allocation);
797 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500798 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400799 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 NULL,
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400801 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400802 break;
803#endif
804 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400805 surface = display_create_shm_surface(window->display,
806 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400807 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800808 default:
809 surface = NULL;
810 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400812
813 window_set_surface(window, surface);
814 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815}
816
817static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500818window_draw_menu(struct window *window)
819{
820 cairo_t *cr;
821 int width, height, r = 5;
822
823 window_create_surface(window);
824
825 cr = cairo_create(window->cairo_surface);
826 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
827 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
828 cairo_paint(cr);
829
830 width = window->allocation.width;
831 height = window->allocation.height;
832 rounded_rect(cr, r, r, width - r, height - r, r);
833 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
834 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
835 cairo_fill(cr);
836 cairo_destroy(cr);
837}
838
839static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500840window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500841{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500842 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500843 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400844 cairo_surface_t *frame;
845 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500846
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400847 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400848
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400849 width = window->allocation.width;
850 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500851
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500852 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500853
Kristian Høgsberg09531622010-06-14 23:22:15 -0400854 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400855 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400856 cairo_paint(cr);
857
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400858 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400859 tile_mask(cr, window->display->shadow,
860 shadow_dx, shadow_dy, width, height,
861 window->margin + 10 - shadow_dx,
862 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500863
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400864 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400865 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400866 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400867 frame = window->display->inactive_frame;
868
869 tile_source(cr, frame, 0, 0, width, height,
870 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500871
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500872 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
873 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500874 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400875 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400876 if (window->keyboard_device)
877 cairo_set_source_rgb(cr, 0, 0, 0);
878 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400879 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400880 cairo_show_text(cr, window->title);
881
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500882 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400883
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100884 /* FIXME: this breakes gears, fix cairo? */
885#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400886 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100887#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500888}
889
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400890void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500891window_destroy(struct window *window)
892{
893 wl_surface_destroy(window->surface);
894 wl_list_remove(&window->link);
895 free(window);
896}
897
898void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400899display_flush_cairo_device(struct display *display)
900{
901 cairo_device_flush (display->device);
902}
903
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500904void
905window_draw(struct window *window)
906{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500907 if (window->parent)
908 window_draw_menu(window);
909 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400910 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500911 else
912 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500913}
914
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400915cairo_surface_t *
916window_get_surface(struct window *window)
917{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400918 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400919}
920
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100921struct wl_surface *
922window_get_wl_surface(struct window *window)
923{
924 return window->surface;
925}
926
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400927static int
928get_pointer_location(struct window *window, int32_t x, int32_t y)
929{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400931 const int grip_size = 8;
932
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500933 if (!window->decoration)
934 return WINDOW_CLIENT_AREA;
935
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400936 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400937 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400938 else if (window->margin <= x && x < window->margin + grip_size)
939 hlocation = WINDOW_RESIZING_LEFT;
940 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400942 else if (x < window->allocation.width - window->margin)
943 hlocation = WINDOW_RESIZING_RIGHT;
944 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400945 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400946
947 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400948 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400949 else if (window->margin <= y && y < window->margin + grip_size)
950 vlocation = WINDOW_RESIZING_TOP;
951 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400952 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400953 else if (y < window->allocation.height - window->margin)
954 vlocation = WINDOW_RESIZING_BOTTOM;
955 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400956 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400957
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400958 location = vlocation | hlocation;
959 if (location & WINDOW_EXTERIOR)
960 location = WINDOW_EXTERIOR;
961 if (location == WINDOW_INTERIOR && y < window->margin + 50)
962 location = WINDOW_TITLEBAR;
963 else if (location == WINDOW_INTERIOR)
964 location = WINDOW_CLIENT_AREA;
965
966 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400967}
968
969static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400970set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400971{
972 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400973 struct wl_buffer *buffer;
974 cairo_surface_t *surface;
975 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400976
977 location = get_pointer_location(input->pointer_focus,
978 input->sx, input->sy);
979 switch (location) {
980 case WINDOW_RESIZING_TOP:
981 pointer = POINTER_TOP;
982 break;
983 case WINDOW_RESIZING_BOTTOM:
984 pointer = POINTER_BOTTOM;
985 break;
986 case WINDOW_RESIZING_LEFT:
987 pointer = POINTER_LEFT;
988 break;
989 case WINDOW_RESIZING_RIGHT:
990 pointer = POINTER_RIGHT;
991 break;
992 case WINDOW_RESIZING_TOP_LEFT:
993 pointer = POINTER_TOP_LEFT;
994 break;
995 case WINDOW_RESIZING_TOP_RIGHT:
996 pointer = POINTER_TOP_RIGHT;
997 break;
998 case WINDOW_RESIZING_BOTTOM_LEFT:
999 pointer = POINTER_BOTTOM_LEFT;
1000 break;
1001 case WINDOW_RESIZING_BOTTOM_RIGHT:
1002 pointer = POINTER_BOTTOM_RIGHT;
1003 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001004 case WINDOW_EXTERIOR:
1005 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001006 if (input->current_pointer_image == POINTER_DEFAULT)
1007 return;
1008
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001009 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001010 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001011 return;
1012 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001013 break;
1014 }
1015
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001016 if (pointer == input->current_pointer_image)
1017 return;
1018
1019 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001020 surface = display->pointer_surfaces[pointer];
1021 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001022 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001023 pointer_images[pointer].hotspot_x,
1024 pointer_images[pointer].hotspot_y);
1025}
1026
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001027static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001028window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001029 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001030 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001031{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001032 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001033 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001034 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001035
1036 input->x = x;
1037 input->y = y;
1038 input->sx = sx;
1039 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001040
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001041 if (window->motion_handler)
1042 pointer = (*window->motion_handler)(window, input, time,
1043 x, y, sx, sy,
1044 window->user_data);
1045
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001046 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001047}
1048
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001049static void
1050window_handle_button(void *data,
1051 struct wl_input_device *input_device,
1052 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001053{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001054 struct input *input = data;
1055 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001056 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001057
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001058 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001059
1060 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001061 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001062 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001063 wl_shell_move(window->display->shell,
1064 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001065 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001066 case WINDOW_RESIZING_TOP:
1067 case WINDOW_RESIZING_BOTTOM:
1068 case WINDOW_RESIZING_LEFT:
1069 case WINDOW_RESIZING_RIGHT:
1070 case WINDOW_RESIZING_TOP_LEFT:
1071 case WINDOW_RESIZING_TOP_RIGHT:
1072 case WINDOW_RESIZING_BOTTOM_LEFT:
1073 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001074 wl_shell_resize(window->display->shell,
1075 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001076 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001077 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001078 case WINDOW_CLIENT_AREA:
1079 if (window->button_handler)
1080 (*window->button_handler)(window,
1081 input, time,
1082 button, state,
1083 window->user_data);
1084 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001085 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001086 } else {
1087 if (window->button_handler)
1088 (*window->button_handler)(window,
1089 input, time,
1090 button, state,
1091 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001092 }
1093}
1094
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001095static void
1096window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001097 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001098{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001099 struct input *input = data;
1100 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001101 struct display *d = window->display;
1102 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001103
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001104 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001105 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001106 return;
1107
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001108 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001109 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001110 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1111 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001112
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001113 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1114
1115 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001116 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001117 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001118 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001119
1120 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001121 (*window->key_handler)(window, input, time, key, sym, state,
1122 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001123}
1124
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001125static void
1126window_handle_pointer_focus(void *data,
1127 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001128 uint32_t time, struct wl_surface *surface,
1129 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001130{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001131 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001132 struct window *window;
1133 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001134
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001135 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001136 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001137 window = input->pointer_focus;
1138
Kristian Høgsberg59826582011-01-20 11:56:57 -05001139 input->x = x;
1140 input->y = y;
1141 input->sx = sx;
1142 input->sy = sy;
1143
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001144 pointer = POINTER_LEFT_PTR;
1145 if (window->motion_handler)
1146 pointer = (*window->motion_handler)(window,
1147 input, time,
1148 x, y, sx, sy,
1149 window->user_data);
1150
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001151 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001152 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001153 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001154 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001155 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001156}
1157
1158static void
1159window_handle_keyboard_focus(void *data,
1160 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001161 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001162 struct wl_surface *surface,
1163 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001164{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001165 struct input *input = data;
1166 struct window *window = input->keyboard_focus;
1167 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001168 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001169
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001170 window = input->keyboard_focus;
1171 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001172 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001173 if (window->keyboard_focus_handler)
1174 (*window->keyboard_focus_handler)(window, NULL,
1175 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001176 }
1177
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001178 if (surface)
1179 input->keyboard_focus = wl_surface_get_user_data(surface);
1180 else
1181 input->keyboard_focus = NULL;
1182
1183 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001184 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001185 for (k = keys->data; k < end; k++)
1186 input->modifiers |= d->xkb->map->modmap[*k];
1187
1188 window = input->keyboard_focus;
1189 if (window) {
1190 window->keyboard_device = input;
1191 if (window->keyboard_focus_handler)
1192 (*window->keyboard_focus_handler)(window,
1193 window->keyboard_device,
1194 window->user_data);
1195 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001196}
1197
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001198static const struct wl_input_device_listener input_device_listener = {
1199 window_handle_motion,
1200 window_handle_button,
1201 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001202 window_handle_pointer_focus,
1203 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001204};
1205
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001206void
1207input_get_position(struct input *input, int32_t *x, int32_t *y)
1208{
1209 *x = input->sx;
1210 *y = input->sy;
1211}
1212
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001213struct wl_input_device *
1214input_get_input_device(struct input *input)
1215{
1216 return input->input_device;
1217}
1218
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001219uint32_t
1220input_get_modifiers(struct input *input)
1221{
1222 return input->modifiers;
1223}
1224
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001225struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001226window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001227{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228 cairo_device_flush (window->display->device);
1229
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001230 return wl_shell_create_drag(window->display->shell);
1231}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001232
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001233void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001234window_move(struct window *window, struct input *input, uint32_t time)
1235{
1236 wl_shell_move(window->display->shell,
1237 window->surface, input->input_device, time);
1238}
1239
1240void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001241window_activate_drag(struct wl_drag *drag, struct window *window,
1242 struct input *input, uint32_t time)
1243{
1244 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245}
1246
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001247static void
1248handle_configure(void *data, struct wl_shell *shell,
1249 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001250 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001251{
1252 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001253 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001254
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001255 /* FIXME: this is probably the wrong place to check for width
1256 * or height <= 0, but it prevents the compositor from crashing
1257 */
1258 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001259 return;
1260
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001261 window->resize_edges = edges;
1262
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001263 if (window->resize_handler) {
1264 child_width = width - 20 - window->margin * 2;
1265 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001266
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001267 (*window->resize_handler)(window,
1268 child_width, child_height,
1269 window->user_data);
1270 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001271 window->allocation.width = width;
1272 window->allocation.height = height;
1273
1274 if (window->redraw_handler)
1275 window_schedule_redraw(window);
1276 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001277}
1278
1279static const struct wl_shell_listener shell_listener = {
1280 handle_configure,
1281};
1282
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001283void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001284window_get_allocation(struct window *window,
1285 struct rectangle *allocation)
1286{
1287 *allocation = window->allocation;
1288}
1289
1290void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001291window_get_child_allocation(struct window *window,
1292 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001293{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001294 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001295 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001296 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001297 allocation->x = window->margin + 10;
1298 allocation->y = window->margin + 50;
1299 allocation->width =
1300 window->allocation.width - 20 - window->margin * 2;
1301 allocation->height =
1302 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001303 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001304}
1305
1306void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001307window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001308{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001309 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001310 window->allocation.x = 20 + window->margin;
1311 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001312 window->allocation.width = width + 20 + window->margin * 2;
1313 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001314 } else {
1315 window->allocation.x = 0;
1316 window->allocation.y = 0;
1317 window->allocation.width = width;
1318 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001319 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001320}
1321
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001322static gboolean
1323idle_redraw(void *data)
1324{
1325 struct window *window = data;
1326
1327 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001328
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001329 window->redraw_scheduled = 0;
1330
1331 return FALSE;
1332}
1333
1334void
1335window_schedule_redraw(struct window *window)
1336{
1337 if (!window->redraw_scheduled) {
1338 g_idle_add(idle_redraw, window);
1339 window->redraw_scheduled = 1;
1340 }
1341}
1342
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001343void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001344window_set_fullscreen(struct window *window, int fullscreen)
1345{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001346 int32_t width, height;
1347
1348 if (window->fullscreen == fullscreen)
1349 return;
1350
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001351 window->fullscreen = fullscreen;
1352 if (window->fullscreen) {
1353 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001354 width = window->display->screen_allocation.width;
1355 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001356 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001357 width = window->saved_allocation.width - 20 - window->margin * 2;
1358 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001359 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001360
1361 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001362}
1363
1364void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001365window_set_decoration(struct window *window, int decoration)
1366{
1367 window->decoration = decoration;
1368}
1369
1370void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001371window_set_user_data(struct window *window, void *data)
1372{
1373 window->user_data = data;
1374}
1375
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001376void *
1377window_get_user_data(struct window *window)
1378{
1379 return window->user_data;
1380}
1381
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001382void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001383window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001384 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001385{
1386 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001387}
1388
1389void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001390window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001391 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001392{
1393 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001394}
1395
1396void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001397window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001398 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001399{
1400 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001401}
1402
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001403void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001404window_set_button_handler(struct window *window,
1405 window_button_handler_t handler)
1406{
1407 window->button_handler = handler;
1408}
1409
1410void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001411window_set_motion_handler(struct window *window,
1412 window_motion_handler_t handler)
1413{
1414 window->motion_handler = handler;
1415}
1416
1417void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001418window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001419 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001420{
1421 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001422}
1423
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001424void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001425window_set_title(struct window *window, const char *title)
1426{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001427 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001428 window->title = strdup(title);
1429}
1430
1431const char *
1432window_get_title(struct window *window)
1433{
1434 return window->title;
1435}
1436
1437void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001438display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1439 int32_t x, int32_t y, int32_t width, int32_t height)
1440{
1441 struct wl_buffer *buffer;
1442
1443 buffer = display_get_buffer_for_surface(display, cairo_surface);
1444
1445 wl_buffer_damage(buffer, x, y, width, height);
1446}
1447
1448void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001449window_damage(struct window *window, int32_t x, int32_t y,
1450 int32_t width, int32_t height)
1451{
1452 wl_surface_damage(window->surface, x, y, width, height);
1453}
1454
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001455static struct window *
1456window_create_internal(struct display *display, struct window *parent,
1457 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001458{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001459 struct window *window;
1460
1461 window = malloc(sizeof *window);
1462 if (window == NULL)
1463 return NULL;
1464
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001465 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001466 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001467 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001468 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001469 window->allocation.x = 0;
1470 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001471 window->allocation.width = width;
1472 window->allocation.height = height;
1473 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001474 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001475 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001476
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001477 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001478 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1479 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001480 else
1481 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001482
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001483 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001484 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001485
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001486 return window;
1487}
1488
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001489struct window *
1490window_create(struct display *display, int32_t width, int32_t height)
1491{
1492 struct window *window;
1493
1494 window = window_create_internal(display, NULL, width, height);
1495 if (!window)
1496 return NULL;
1497
1498 return window;
1499}
1500
1501struct window *
1502window_create_transient(struct display *display, struct window *parent,
1503 int32_t x, int32_t y, int32_t width, int32_t height)
1504{
1505 struct window *window;
1506
1507 window = window_create_internal(parent->display,
1508 parent, width, height);
1509 if (!window)
1510 return NULL;
1511
1512 window->x = x;
1513 window->y = y;
1514
1515 return window;
1516}
1517
1518void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001519window_set_buffer_type(struct window *window, enum window_buffer_type type)
1520{
1521 window->buffer_type = type;
1522}
1523
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001524static void
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001525compositor_handle_visual(void *data,
1526 struct wl_compositor *compositor,
1527 uint32_t id, uint32_t token)
1528{
1529 struct display *d = data;
1530
1531 switch (token) {
1532 case WL_COMPOSITOR_VISUAL_ARGB32:
1533 d->argb_visual = wl_visual_create(d->display, id, 1);
1534 break;
1535 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
1536 d->premultiplied_argb_visual =
1537 wl_visual_create(d->display, id, 1);
1538 break;
1539 case WL_COMPOSITOR_VISUAL_XRGB32:
1540 d->rgb_visual = wl_visual_create(d->display, id, 1);
1541 break;
1542 }
1543}
1544
1545static const struct wl_compositor_listener compositor_listener = {
1546 compositor_handle_visual,
1547};
1548
1549static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001550display_handle_geometry(void *data,
1551 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001552 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001553{
1554 struct display *display = data;
1555
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001556 display->screen_allocation.x = x;
1557 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001558 display->screen_allocation.width = width;
1559 display->screen_allocation.height = height;
1560}
1561
1562static const struct wl_output_listener output_listener = {
1563 display_handle_geometry,
1564};
1565
1566static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001567display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001568{
1569 struct input *input;
1570
1571 input = malloc(sizeof *input);
1572 if (input == NULL)
1573 return;
1574
1575 memset(input, 0, sizeof *input);
1576 input->display = d;
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001577 input->input_device = wl_input_device_create(d->display, id, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001578 input->pointer_focus = NULL;
1579 input->keyboard_focus = NULL;
1580 wl_list_insert(d->input_list.prev, &input->link);
1581
1582 wl_input_device_add_listener(input->input_device,
1583 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001584 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001585}
1586
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001587struct selection_offer {
1588 struct display *display;
1589 struct wl_selection_offer *offer;
1590 struct wl_array types;
1591 struct input *input;
1592};
1593
1594int
1595input_offers_mime_type(struct input *input, const char *type)
1596{
1597 struct selection_offer *offer = input->offer;
1598 char **p, **end;
1599
1600 if (offer == NULL)
1601 return 0;
1602
1603 end = offer->types.data + offer->types.size;
1604 for (p = offer->types.data; p < end; p++)
1605 if (strcmp(*p, type) == 0)
1606 return 1;
1607
1608 return 0;
1609}
1610
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001611void
1612input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001613{
1614 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001615
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001616 /* FIXME: A number of things can go wrong here: the object may
1617 * not be the current selection offer any more (which could
1618 * still work, but the source may have gone away or just
1619 * destroyed its wl_selection) or the offer may not have the
1620 * requested type after all (programmer/client error,
1621 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001622 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001623}
1624
1625static void
1626selection_offer_offer(void *data,
1627 struct wl_selection_offer *selection_offer,
1628 const char *type)
1629{
1630 struct selection_offer *offer = data;
1631
1632 char **p;
1633
1634 p = wl_array_add(&offer->types, sizeof *p);
1635 if (p)
1636 *p = strdup(type);
1637};
1638
1639static void
1640selection_offer_keyboard_focus(void *data,
1641 struct wl_selection_offer *selection_offer,
1642 struct wl_input_device *input_device)
1643{
1644 struct selection_offer *offer = data;
1645 struct input *input;
1646 char **p, **end;
1647
1648 if (input_device == NULL) {
1649 printf("selection offer retracted %p\n", selection_offer);
1650 input = offer->input;
1651 input->offer = NULL;
1652 wl_selection_offer_destroy(selection_offer);
1653 wl_array_release(&offer->types);
1654 free(offer);
1655 return;
1656 }
1657
1658 input = wl_input_device_get_user_data(input_device);
1659 printf("new selection offer %p:", selection_offer);
1660
1661 offer->input = input;
1662 input->offer = offer;
1663 end = offer->types.data + offer->types.size;
1664 for (p = offer->types.data; p < end; p++)
1665 printf(" %s", *p);
1666
1667 printf("\n");
1668}
1669
1670struct wl_selection_offer_listener selection_offer_listener = {
1671 selection_offer_offer,
1672 selection_offer_keyboard_focus
1673};
1674
1675static void
1676add_selection_offer(struct display *d, uint32_t id)
1677{
1678 struct selection_offer *offer;
1679
1680 offer = malloc(sizeof *offer);
1681 if (offer == NULL)
1682 return;
1683
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001684 offer->offer = wl_selection_offer_create(d->display, id, 1);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001685 offer->display = d;
1686 wl_array_init(&offer->types);
1687 offer->input = NULL;
1688
1689 wl_selection_offer_add_listener(offer->offer,
1690 &selection_offer_listener, offer);
1691}
1692
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001693static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001694display_handle_global(struct wl_display *display, uint32_t id,
1695 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001696{
1697 struct display *d = data;
1698
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001699 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001700 d->compositor = wl_compositor_create(display, id, 1);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001701 wl_compositor_add_listener(d->compositor,
1702 &compositor_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001703 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001704 d->output = wl_output_create(display, id, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001705 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001706 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001707 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001708 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001709 d->shell = wl_shell_create(display, id, 1);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001710 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001711 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001712 d->shm = wl_shm_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001713 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001714 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001715 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001716 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001717 }
1718}
1719
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001720static void
1721display_render_frame(struct display *d)
1722{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001723 int radius = 8;
1724 cairo_t *cr;
1725
1726 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1727 cr = cairo_create(d->shadow);
1728 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1729 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1730 rounded_rect(cr, 16, 16, 112, 112, radius);
1731 cairo_fill(cr);
1732 cairo_destroy(cr);
1733 blur_surface(d->shadow, 64);
1734
1735 d->active_frame =
1736 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1737 cr = cairo_create(d->active_frame);
1738 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1739 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1740 rounded_rect(cr, 16, 16, 112, 112, radius);
1741 cairo_fill(cr);
1742 cairo_destroy(cr);
1743
1744 d->inactive_frame =
1745 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1746 cr = cairo_create(d->inactive_frame);
1747 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1748 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1749 rounded_rect(cr, 16, 16, 112, 112, radius);
1750 cairo_fill(cr);
1751 cairo_destroy(cr);
1752}
1753
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001754static void
1755init_xkb(struct display *d)
1756{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001757 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001758
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001759 names.rules = "evdev";
1760 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001761 names.layout = option_xkb_layout;
1762 names.variant = option_xkb_variant;
1763 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001764
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001765 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001766 if (!d->xkb) {
1767 fprintf(stderr, "Failed to compile keymap\n");
1768 exit(1);
1769 }
1770}
1771
Yuval Fledel45568f62010-12-06 09:18:12 -05001772static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001773init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001774{
1775 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001776 EGLint n;
1777 static const EGLint cfg_attribs[] = {
1778 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1779 EGL_RED_SIZE, 1,
1780 EGL_GREEN_SIZE, 1,
1781 EGL_BLUE_SIZE, 1,
1782 EGL_ALPHA_SIZE, 1,
1783 EGL_DEPTH_SIZE, 1,
1784 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1785 EGL_NONE
1786 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001787
Egbert Eiche7b8d902011-05-10 20:00:19 +00001788 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001789 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001790 if (!eglInitialize(d->dpy, &major, &minor)) {
1791 fprintf(stderr, "failed to initialize display\n");
1792 return -1;
1793 }
1794
1795 if (!eglBindAPI(EGL_OPENGL_API)) {
1796 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1797 return -1;
1798 }
1799
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001800 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1801 fprintf(stderr, "failed to choose config\n");
1802 return -1;
1803 }
1804
1805 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001806 if (d->ctx == NULL) {
1807 fprintf(stderr, "failed to create context\n");
1808 return -1;
1809 }
1810
1811 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001812 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001813 return -1;
1814 }
1815
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001816#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001817 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001818 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001819 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001820 return -1;
1821 }
1822#endif
1823
1824 return 0;
1825}
1826
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001827struct display *
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001828display_create(int *argc, char **argv[], const GOptionEntry *option_entries,
1829 display_global_handler_t handler)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001830{
1831 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001832 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001833 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001834 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001835
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001836 g_type_init();
1837
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001838 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001839 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001840 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001841
1842 xkb_option_group = g_option_group_new("xkb",
1843 "Keyboard options",
1844 "Show all XKB options",
1845 NULL, NULL);
1846 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1847 g_option_context_add_group (context, xkb_option_group);
1848
1849 if (!g_option_context_parse(context, argc, argv, &error)) {
1850 fprintf(stderr, "option parsing failed: %s\n", error->message);
1851 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001852 }
1853
Tim Wiederhake748f6722011-01-23 23:25:25 +01001854 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001855
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001856 d = malloc(sizeof *d);
1857 if (d == NULL)
1858 return NULL;
1859
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001860 memset(d, 0, sizeof *d);
1861
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001862 d->global_handler = handler;
1863
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001864 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001865 if (d->display == NULL) {
1866 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001867 return NULL;
1868 }
1869
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001870 wl_list_init(&d->input_list);
1871
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001872 /* Set up listener so we'll catch all events. */
1873 wl_display_add_global_listener(d->display,
1874 display_handle_global, d);
1875
1876 /* Process connection events. */
1877 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1878
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001879 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001880 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001881
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001882 d->image_target_texture_2d =
1883 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1884 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1885 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1886
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001887 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001888
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001889 display_render_frame(d);
1890
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001891 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001892 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001893 g_source_attach(d->source, NULL);
1894
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001895 wl_list_init(&d->window_list);
1896
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001897 init_xkb(d);
1898
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001899 return d;
1900}
1901
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001902struct wl_display *
1903display_get_display(struct display *display)
1904{
1905 return display->display;
1906}
1907
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001908struct wl_compositor *
1909display_get_compositor(struct display *display)
1910{
1911 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001912}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001913
1914EGLDisplay
1915display_get_egl_display(struct display *d)
1916{
1917 return d->dpy;
1918}
1919
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001920EGLConfig
1921display_get_egl_config(struct display *d)
1922{
1923 return d->conf;
1924}
1925
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001926struct wl_shell *
1927display_get_shell(struct display *display)
1928{
1929 return display->shell;
1930}
1931
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001932void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001933display_acquire_window_surface(struct display *display,
1934 struct window *window,
1935 EGLContext ctx)
1936{
1937 struct egl_window_surface_data *data;
1938
1939 if (!window->cairo_surface)
1940 return;
1941
1942 if (!ctx)
1943 ctx = display->ctx;
1944
1945 data = cairo_surface_get_user_data(window->cairo_surface,
1946 &surface_data_key);
1947
1948 cairo_device_acquire(display->device);
1949 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
1950 fprintf(stderr, "failed to make surface current\n");
1951}
1952
1953void
1954display_release(struct display *display)
1955{
1956 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
1957 fprintf(stderr, "failed to make context current\n");
1958 cairo_device_release(display->device);
1959}
1960
1961void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001962display_run(struct display *d)
1963{
1964 g_main_loop_run(d->loop);
1965}