blob: a328198812e32b5e53a6e5e5a6ff1a9d432f237d [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øgsberg43c28ee2009-01-26 23:42:46 -050065 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040066 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +010068 EGLConfig conf;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040069 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010070 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040072 GMainLoop *loop;
73 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040074 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040075 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040076 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040077 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040078 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040079 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040080
Kristian Høgsbergb46df052011-01-18 09:17:57 -050081 display_global_handler_t global_handler;
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050082
83 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
84 PFNEGLCREATEIMAGEKHRPROC create_image;
85 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050086};
87
88struct window {
89 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050090 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050092 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050093 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050094 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040095 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040096 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050097 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050098 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050099 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400101 struct input *grab_device;
102 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500103 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400104 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500105
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400106 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500107 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500108
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500109 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400110 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500111 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400112 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500113 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400114 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400115
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500116 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400117 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500118};
119
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400120struct input {
121 struct display *display;
122 struct wl_input_device *input_device;
123 struct window *pointer_focus;
124 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500125 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400126 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400127 uint32_t modifiers;
128 int32_t x, y, sx, sy;
129 struct wl_list link;
130};
131
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400132enum {
133 POINTER_DEFAULT = 100,
134 POINTER_UNSET
135};
136
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137enum window_location {
138 WINDOW_INTERIOR = 0,
139 WINDOW_RESIZING_TOP = 1,
140 WINDOW_RESIZING_BOTTOM = 2,
141 WINDOW_RESIZING_LEFT = 4,
142 WINDOW_RESIZING_TOP_LEFT = 5,
143 WINDOW_RESIZING_BOTTOM_LEFT = 6,
144 WINDOW_RESIZING_RIGHT = 8,
145 WINDOW_RESIZING_TOP_RIGHT = 9,
146 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
147 WINDOW_RESIZING_MASK = 15,
148 WINDOW_EXTERIOR = 16,
149 WINDOW_TITLEBAR = 17,
150 WINDOW_CLIENT_AREA = 18,
151};
152
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400153const char *option_xkb_layout = "us";
154const char *option_xkb_variant = "";
155const char *option_xkb_options = "";
156
157static const GOptionEntry xkb_option_entries[] = {
158 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_layout, "XKB Layout" },
160 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_variant, "XKB Variant" },
162 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
163 &option_xkb_options, "XKB Options" },
164 { NULL }
165};
166
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500167static void
168rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
169{
170 cairo_move_to(cr, x0, y0 + radius);
171 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
172 cairo_line_to(cr, x1 - radius, y0);
173 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
174 cairo_line_to(cr, x1, y1 - radius);
175 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
176 cairo_line_to(cr, x0 + radius, y1);
177 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
178 cairo_close_path(cr);
179}
180
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400181static const cairo_user_data_key_t surface_data_key;
182struct surface_data {
183 struct wl_buffer *buffer;
184};
185
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500186#define MULT(_d,c,a,t) \
187 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
188
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500189#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400190
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100191struct egl_window_surface_data {
192 struct display *display;
193 struct wl_surface *surface;
194 struct wl_egl_window *window;
195 EGLSurface surf;
196};
197
198static void
199egl_window_surface_data_destroy(void *p)
200{
201 struct egl_window_surface_data *data = p;
202 struct display *d = data->display;
203
204 eglDestroySurface(d->dpy, data->surf);
205 wl_egl_window_destroy(data->window);
206 data->surface = NULL;
207
208 free(p);
209}
210
211static cairo_surface_t *
212display_create_egl_window_surface(struct display *display,
213 struct wl_surface *surface,
214 struct rectangle *rectangle)
215{
216 cairo_surface_t *cairo_surface;
217 struct egl_window_surface_data *data;
218 struct wl_visual *visual;
219
220 data = malloc(sizeof *data);
221 if (data == NULL)
222 return NULL;
223
224 data->display = display;
225 data->surface = surface;
226
227 visual = wl_display_get_premultiplied_argb_visual(display->display);
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,
285 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400286{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500287 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400288 EGLDisplay dpy = display->dpy;
289 cairo_surface_t *surface;
290 struct wl_visual *visual;
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øgsberg297c6312011-02-04 14:11:33 -0500298 visual = wl_display_get_premultiplied_argb_visual(display->display);
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400299 data->pixmap = wl_egl_pixmap_create(rectangle->width,
300 rectangle->height,
301 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500302 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000303 free(data);
304 return NULL;
305 }
306
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500307 data->image = display->create_image(dpy, NULL,
308 EGL_NATIVE_PIXMAP_KHR,
309 (EGLClientBuffer) data->pixmap,
310 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500311 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500312 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313 free(data);
314 return NULL;
315 }
316
317 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400318 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800320 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500323 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800324 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326 surface = cairo_gl_surface_create_for_texture(display->device,
327 CAIRO_CONTENT_COLOR_ALPHA,
328 data->texture,
329 rectangle->width,
330 rectangle->height);
331
332 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500333 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334
335 return surface;
336}
337
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500338static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339display_create_egl_image_surface_from_file(struct display *display,
340 const char *filename,
341 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342{
343 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400344 GdkPixbuf *pixbuf;
345 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400346 int stride, i;
347 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 struct egl_image_surface_data *data;
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øgsberg297c6312011-02-04 14:11:33 -0500380 surface = display_create_egl_image_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000381 if (surface == NULL) {
382 g_object_unref(pixbuf);
383 return NULL;
384 }
385
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800386 data = cairo_surface_get_user_data(surface, &surface_data_key);
387
388 cairo_device_acquire(display->device);
389 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800390 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
391 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800392 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400393
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500394 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400395
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396 return surface;
397}
398
399#endif
400
401struct wl_buffer *
402display_get_buffer_for_surface(struct display *display,
403 cairo_surface_t *surface)
404{
405 struct surface_data *data;
406
407 data = cairo_surface_get_user_data (surface, &surface_data_key);
408
409 return data->buffer;
410}
411
412struct shm_surface_data {
413 struct surface_data data;
414 void *map;
415 size_t length;
416};
417
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500418static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419shm_surface_data_destroy(void *p)
420{
421 struct shm_surface_data *data = p;
422
423 wl_buffer_destroy(data->data.buffer);
424 munmap(data->map, data->length);
425}
426
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500427static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428display_create_shm_surface(struct display *display,
429 struct rectangle *rectangle)
430{
431 struct shm_surface_data *data;
432 cairo_surface_t *surface;
433 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800434 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435 char filename[] = "/tmp/wayland-shm-XXXXXX";
436
437 data = malloc(sizeof *data);
438 if (data == NULL)
439 return NULL;
440
441 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
442 rectangle->width);
443 data->length = stride * rectangle->height;
444 fd = mkstemp(filename);
445 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000446 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400447 return NULL;
448 }
449 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000450 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400451 close(fd);
452 return NULL;
453 }
454
455 data->map = mmap(NULL, data->length,
456 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
457 unlink(filename);
458
459 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000460 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461 close(fd);
462 return NULL;
463 }
464
465 surface = cairo_image_surface_create_for_data (data->map,
466 CAIRO_FORMAT_ARGB32,
467 rectangle->width,
468 rectangle->height,
469 stride);
470
471 cairo_surface_set_user_data (surface, &surface_data_key,
472 data, shm_surface_data_destroy);
473
474 visual = wl_display_get_premultiplied_argb_visual(display->display);
475 data->data.buffer = wl_shm_create_buffer(display->shm,
476 fd,
477 rectangle->width,
478 rectangle->height,
479 stride, visual);
480
481 close(fd);
482
483 return surface;
484}
485
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500486static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400487display_create_shm_surface_from_file(struct display *display,
488 const char *filename,
489 struct rectangle *rect)
490{
491 cairo_surface_t *surface;
492 GdkPixbuf *pixbuf;
493 GError *error = NULL;
494 int stride, i;
495 unsigned char *pixels, *p, *end, *dest_data;
496 int dest_stride;
497 uint32_t *d;
498
499 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
500 rect->width, rect->height,
501 FALSE, &error);
502 if (error != NULL)
503 return NULL;
504
505 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
506 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500507 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508 return NULL;
509 }
510
511 stride = gdk_pixbuf_get_rowstride(pixbuf);
512 pixels = gdk_pixbuf_get_pixels(pixbuf);
513
514 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000515 if (surface == NULL) {
516 g_object_unref(pixbuf);
517 return NULL;
518 }
519
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520 dest_data = cairo_image_surface_get_data (surface);
521 dest_stride = cairo_image_surface_get_stride (surface);
522
523 for (i = 0; i < rect->height; i++) {
524 d = (uint32_t *) (dest_data + i * dest_stride);
525 p = pixels + i * stride;
526 end = p + rect->width * 4;
527 while (p < end) {
528 unsigned int t;
529 unsigned char a, r, g, b;
530
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531 a = p[3];
532 MULT(r, p[0], a, t);
533 MULT(g, p[1], a, t);
534 MULT(b, p[2], a, t);
535 p += 4;
536 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
537 }
538 }
539
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500540 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
542 return surface;
543}
544
nobled7b87cb02011-02-01 18:51:47 +0000545static int
546check_size(struct rectangle *rect)
547{
548 if (rect->width && rect->height)
549 return 0;
550
551 fprintf(stderr, "tried to create surface of "
552 "width: %d, height: %d\n", rect->width, rect->height);
553 return -1;
554}
555
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556cairo_surface_t *
557display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558 struct wl_surface *surface,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559 struct rectangle *rectangle)
560{
nobled7b87cb02011-02-01 18:51:47 +0000561 if (check_size(rectangle) < 0)
562 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500563#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500564 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100565 if (surface)
566 return display_create_egl_window_surface(display,
567 surface,
568 rectangle);
569 else
570 return display_create_egl_image_surface(display,
571 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500572 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500574 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575}
576
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500577static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578display_create_surface_from_file(struct display *display,
579 const char *filename,
580 struct rectangle *rectangle)
581{
nobled7b87cb02011-02-01 18:51:47 +0000582 if (check_size(rectangle) < 0)
583 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500584#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500585 if (display->dpy) {
586 return display_create_egl_image_surface_from_file(display,
587 filename,
588 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500589 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500591 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400592}
Yuval Fledel45568f62010-12-06 09:18:12 -0500593 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400594 const char *filename;
595 int hotspot_x, hotspot_y;
596} pointer_images[] = {
597 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
598 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
599 { DATADIR "/wayland/bottom_side.png", 16, 20 },
600 { DATADIR "/wayland/grabbing.png", 20, 17 },
601 { DATADIR "/wayland/left_ptr.png", 10, 5 },
602 { DATADIR "/wayland/left_side.png", 10, 20 },
603 { DATADIR "/wayland/right_side.png", 30, 19 },
604 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
605 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
606 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400607 { DATADIR "/wayland/xterm.png", 15, 15 },
608 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400609};
610
611static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400612create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400613{
614 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400615 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400616 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400617
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400619 display->pointer_surfaces =
620 malloc(count * sizeof *display->pointer_surfaces);
621 rect.width = width;
622 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400623 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400624 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625 display_create_surface_from_file(display,
626 pointer_images[i].filename,
627 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400628 }
629
630}
631
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400632cairo_surface_t *
633display_get_pointer_surface(struct display *display, int pointer,
634 int *width, int *height,
635 int *hotspot_x, int *hotspot_y)
636{
637 cairo_surface_t *surface;
638
639 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500640#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400641 *width = cairo_gl_surface_get_width(surface);
642 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000643#else
644 *width = cairo_image_surface_get_width(surface);
645 *height = cairo_image_surface_get_height(surface);
646#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400647 *hotspot_x = pointer_images[pointer].hotspot_x;
648 *hotspot_y = pointer_images[pointer].hotspot_y;
649
650 return cairo_surface_reference(surface);
651}
652
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400653
654static void
655window_attach_surface(struct window *window);
656
657static void
658free_surface(void *data)
659{
660 struct window *window = data;
661
662 cairo_surface_destroy(window->pending_surface);
663 window->pending_surface = NULL;
664 if (window->cairo_surface)
665 window_attach_surface(window);
666}
667
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500668static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500669window_attach_surface(struct window *window)
670{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400671 struct display *display = window->display;
672 struct wl_buffer *buffer;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100673 struct egl_window_surface_data *data;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500674 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100675 int width = window->allocation.width;
676 int height = window->allocation.height;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500677
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500678 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100679 x = window->server_allocation.width - width;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500680 else
681 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500682
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500683 if (window->resize_edges & WINDOW_RESIZING_TOP)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100684 y = window->server_allocation.height - height;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500685 else
686 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400687
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500688 window->resize_edges = 0;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689
690 switch (window->buffer_type) {
691 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
692 data = cairo_surface_get_user_data(window->cairo_surface,
693 &surface_data_key);
694
695 wl_egl_window_resize(data->window, width, height, x, y);
696 cairo_gl_surface_swapbuffers(window->cairo_surface);
697 wl_egl_window_get_attached_size(data->window,
698 &window->server_allocation.width,
699 &window->server_allocation.height);
700 break;
701 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
702 case WINDOW_BUFFER_TYPE_SHM:
703 if (window->pending_surface != NULL)
704 return;
705
706 window->pending_surface = window->cairo_surface;
707 window->cairo_surface = NULL;
708
709 buffer =
710 display_get_buffer_for_surface(display,
711 window->pending_surface);
712
713 wl_surface_attach(window->surface, buffer, x, y);
714 window->server_allocation = window->allocation;
715 wl_display_sync_callback(display->display, free_surface,
716 window);
717 break;
718 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500719
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500720 if (window->fullscreen)
721 wl_surface_map_fullscreen(window->surface);
722 else if (!window->parent)
723 wl_surface_map_toplevel(window->surface);
724 else
725 wl_surface_map_transient(window->surface,
726 window->parent->surface,
727 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500728
729 wl_surface_damage(window->surface, 0, 0,
730 window->allocation.width,
731 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500732}
733
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500734void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400735window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500736{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400737 if (window->cairo_surface)
738 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500739}
740
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400741void
742window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400743{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500744 cairo_surface_reference(surface);
745
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400746 if (window->cairo_surface != NULL)
747 cairo_surface_destroy(window->cairo_surface);
748
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500749 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400750}
751
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100752static void
753window_resize_cairo_window_surface(struct window *window)
754{
755 struct egl_window_surface_data *data;
756
757 data = cairo_surface_get_user_data(window->cairo_surface,
758 &surface_data_key);
759
760 wl_egl_window_resize(data->window,
761 window->allocation.width,
762 window->allocation.height, 0, 0);
763 cairo_gl_surface_set_size(window->cairo_surface,
764 window->allocation.width,
765 window->allocation.height);
766}
767
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400768void
769window_create_surface(struct window *window)
770{
771 cairo_surface_t *surface;
772
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400773 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500774#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
776 if (window->cairo_surface) {
777 window_resize_cairo_window_surface(window);
778 return;
779 }
780 surface = display_create_surface(window->display,
781 window->surface,
782 &window->allocation);
783 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500784 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400785 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786 NULL,
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400787 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400788 break;
789#endif
790 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400791 surface = display_create_shm_surface(window->display,
792 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400793 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800794 default:
795 surface = NULL;
796 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400798
799 window_set_surface(window, surface);
800 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400801}
802
803static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500804window_draw_menu(struct window *window)
805{
806 cairo_t *cr;
807 int width, height, r = 5;
808
809 window_create_surface(window);
810
811 cr = cairo_create(window->cairo_surface);
812 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
813 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
814 cairo_paint(cr);
815
816 width = window->allocation.width;
817 height = window->allocation.height;
818 rounded_rect(cr, r, r, width - r, height - r, r);
819 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
820 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
821 cairo_fill(cr);
822 cairo_destroy(cr);
823}
824
825static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500826window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500827{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500828 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500829 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400830 cairo_surface_t *frame;
831 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500832
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400833 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400835 width = window->allocation.width;
836 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500837
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500838 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500839
Kristian Høgsberg09531622010-06-14 23:22:15 -0400840 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400841 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400842 cairo_paint(cr);
843
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400844 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400845 tile_mask(cr, window->display->shadow,
846 shadow_dx, shadow_dy, width, height,
847 window->margin + 10 - shadow_dx,
848 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500849
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400850 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400851 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400852 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400853 frame = window->display->inactive_frame;
854
855 tile_source(cr, frame, 0, 0, width, height,
856 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500857
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500858 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
859 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500860 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400861 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400862 if (window->keyboard_device)
863 cairo_set_source_rgb(cr, 0, 0, 0);
864 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400865 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400866 cairo_show_text(cr, window->title);
867
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500868 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400869
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100870 /* FIXME: this breakes gears, fix cairo? */
871#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400872 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100873#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500874}
875
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400876void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500877window_destroy(struct window *window)
878{
879 wl_surface_destroy(window->surface);
880 wl_list_remove(&window->link);
881 free(window);
882}
883
884void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400885display_flush_cairo_device(struct display *display)
886{
887 cairo_device_flush (display->device);
888}
889
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500890void
891window_draw(struct window *window)
892{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500893 if (window->parent)
894 window_draw_menu(window);
895 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500897 else
898 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500899}
900
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400901cairo_surface_t *
902window_get_surface(struct window *window)
903{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400904 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400905}
906
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100907struct wl_surface *
908window_get_wl_surface(struct window *window)
909{
910 return window->surface;
911}
912
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400913static int
914get_pointer_location(struct window *window, int32_t x, int32_t y)
915{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400916 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400917 const int grip_size = 8;
918
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500919 if (!window->decoration)
920 return WINDOW_CLIENT_AREA;
921
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400922 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400923 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400924 else if (window->margin <= x && x < window->margin + grip_size)
925 hlocation = WINDOW_RESIZING_LEFT;
926 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400927 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400928 else if (x < window->allocation.width - window->margin)
929 hlocation = WINDOW_RESIZING_RIGHT;
930 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400931 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400932
933 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400934 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400935 else if (window->margin <= y && y < window->margin + grip_size)
936 vlocation = WINDOW_RESIZING_TOP;
937 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400938 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400939 else if (y < window->allocation.height - window->margin)
940 vlocation = WINDOW_RESIZING_BOTTOM;
941 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400942 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400943
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400944 location = vlocation | hlocation;
945 if (location & WINDOW_EXTERIOR)
946 location = WINDOW_EXTERIOR;
947 if (location == WINDOW_INTERIOR && y < window->margin + 50)
948 location = WINDOW_TITLEBAR;
949 else if (location == WINDOW_INTERIOR)
950 location = WINDOW_CLIENT_AREA;
951
952 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400953}
954
955static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400956set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400957{
958 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400959 struct wl_buffer *buffer;
960 cairo_surface_t *surface;
961 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400962
963 location = get_pointer_location(input->pointer_focus,
964 input->sx, input->sy);
965 switch (location) {
966 case WINDOW_RESIZING_TOP:
967 pointer = POINTER_TOP;
968 break;
969 case WINDOW_RESIZING_BOTTOM:
970 pointer = POINTER_BOTTOM;
971 break;
972 case WINDOW_RESIZING_LEFT:
973 pointer = POINTER_LEFT;
974 break;
975 case WINDOW_RESIZING_RIGHT:
976 pointer = POINTER_RIGHT;
977 break;
978 case WINDOW_RESIZING_TOP_LEFT:
979 pointer = POINTER_TOP_LEFT;
980 break;
981 case WINDOW_RESIZING_TOP_RIGHT:
982 pointer = POINTER_TOP_RIGHT;
983 break;
984 case WINDOW_RESIZING_BOTTOM_LEFT:
985 pointer = POINTER_BOTTOM_LEFT;
986 break;
987 case WINDOW_RESIZING_BOTTOM_RIGHT:
988 pointer = POINTER_BOTTOM_RIGHT;
989 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400990 case WINDOW_EXTERIOR:
991 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400992 if (input->current_pointer_image == POINTER_DEFAULT)
993 return;
994
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400995 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400996 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400997 return;
998 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400999 break;
1000 }
1001
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001002 if (pointer == input->current_pointer_image)
1003 return;
1004
1005 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001006 surface = display->pointer_surfaces[pointer];
1007 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001008 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001009 pointer_images[pointer].hotspot_x,
1010 pointer_images[pointer].hotspot_y);
1011}
1012
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001013static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001014window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001015 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001016 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001017{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001018 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001019 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001020 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001021
1022 input->x = x;
1023 input->y = y;
1024 input->sx = sx;
1025 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001026
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001027 if (window->motion_handler)
1028 pointer = (*window->motion_handler)(window, input, time,
1029 x, y, sx, sy,
1030 window->user_data);
1031
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001032 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001033}
1034
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001035static void
1036window_handle_button(void *data,
1037 struct wl_input_device *input_device,
1038 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001039{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001040 struct input *input = data;
1041 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001042 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001043
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001044 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001045
1046 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001047 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001048 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001049 wl_shell_move(window->display->shell,
1050 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001051 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001052 case WINDOW_RESIZING_TOP:
1053 case WINDOW_RESIZING_BOTTOM:
1054 case WINDOW_RESIZING_LEFT:
1055 case WINDOW_RESIZING_RIGHT:
1056 case WINDOW_RESIZING_TOP_LEFT:
1057 case WINDOW_RESIZING_TOP_RIGHT:
1058 case WINDOW_RESIZING_BOTTOM_LEFT:
1059 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001060 wl_shell_resize(window->display->shell,
1061 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001062 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001063 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001064 case WINDOW_CLIENT_AREA:
1065 if (window->button_handler)
1066 (*window->button_handler)(window,
1067 input, time,
1068 button, state,
1069 window->user_data);
1070 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001071 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001072 } else {
1073 if (window->button_handler)
1074 (*window->button_handler)(window,
1075 input, time,
1076 button, state,
1077 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001078 }
1079}
1080
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001081static void
1082window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001083 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001084{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001085 struct input *input = data;
1086 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001087 struct display *d = window->display;
1088 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001089
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001090 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001091 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001092 return;
1093
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001094 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001095 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001096 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1097 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001098
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001099 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1100
1101 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001102 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001103 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001104 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001105
1106 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001107 (*window->key_handler)(window, input, time, key, sym, state,
1108 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001109}
1110
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001111static void
1112window_handle_pointer_focus(void *data,
1113 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001114 uint32_t time, struct wl_surface *surface,
1115 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001116{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001117 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001118 struct window *window;
1119 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001120
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001121 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001122 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001123 window = input->pointer_focus;
1124
Kristian Høgsberg59826582011-01-20 11:56:57 -05001125 input->x = x;
1126 input->y = y;
1127 input->sx = sx;
1128 input->sy = sy;
1129
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001130 pointer = POINTER_LEFT_PTR;
1131 if (window->motion_handler)
1132 pointer = (*window->motion_handler)(window,
1133 input, time,
1134 x, y, sx, sy,
1135 window->user_data);
1136
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001137 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001138 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001139 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001140 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001142}
1143
1144static void
1145window_handle_keyboard_focus(void *data,
1146 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001147 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001148 struct wl_surface *surface,
1149 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001150{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001151 struct input *input = data;
1152 struct window *window = input->keyboard_focus;
1153 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001154 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001155
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001156 window = input->keyboard_focus;
1157 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001158 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001159 if (window->keyboard_focus_handler)
1160 (*window->keyboard_focus_handler)(window, NULL,
1161 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001162 }
1163
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001164 if (surface)
1165 input->keyboard_focus = wl_surface_get_user_data(surface);
1166 else
1167 input->keyboard_focus = NULL;
1168
1169 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001170 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001171 for (k = keys->data; k < end; k++)
1172 input->modifiers |= d->xkb->map->modmap[*k];
1173
1174 window = input->keyboard_focus;
1175 if (window) {
1176 window->keyboard_device = input;
1177 if (window->keyboard_focus_handler)
1178 (*window->keyboard_focus_handler)(window,
1179 window->keyboard_device,
1180 window->user_data);
1181 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001182}
1183
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001184static const struct wl_input_device_listener input_device_listener = {
1185 window_handle_motion,
1186 window_handle_button,
1187 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001188 window_handle_pointer_focus,
1189 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001190};
1191
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001192void
1193input_get_position(struct input *input, int32_t *x, int32_t *y)
1194{
1195 *x = input->sx;
1196 *y = input->sy;
1197}
1198
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001199struct wl_input_device *
1200input_get_input_device(struct input *input)
1201{
1202 return input->input_device;
1203}
1204
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001205uint32_t
1206input_get_modifiers(struct input *input)
1207{
1208 return input->modifiers;
1209}
1210
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001211struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001212window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001213{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001214 cairo_device_flush (window->display->device);
1215
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001216 return wl_shell_create_drag(window->display->shell);
1217}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001218
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001219void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001220window_move(struct window *window, struct input *input, uint32_t time)
1221{
1222 wl_shell_move(window->display->shell,
1223 window->surface, input->input_device, time);
1224}
1225
1226void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001227window_activate_drag(struct wl_drag *drag, struct window *window,
1228 struct input *input, uint32_t time)
1229{
1230 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001231}
1232
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001233static void
1234handle_configure(void *data, struct wl_shell *shell,
1235 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001236 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001237{
1238 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001239 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001240
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001241 /* FIXME: this is probably the wrong place to check for width
1242 * or height <= 0, but it prevents the compositor from crashing
1243 */
1244 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001245 return;
1246
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001247 window->resize_edges = edges;
1248
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001249 if (window->resize_handler) {
1250 child_width = width - 20 - window->margin * 2;
1251 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001252
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001253 (*window->resize_handler)(window,
1254 child_width, child_height,
1255 window->user_data);
1256 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001257 window->allocation.width = width;
1258 window->allocation.height = height;
1259
1260 if (window->redraw_handler)
1261 window_schedule_redraw(window);
1262 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001263}
1264
1265static const struct wl_shell_listener shell_listener = {
1266 handle_configure,
1267};
1268
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001269void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001270window_get_allocation(struct window *window,
1271 struct rectangle *allocation)
1272{
1273 *allocation = window->allocation;
1274}
1275
1276void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001277window_get_child_allocation(struct window *window,
1278 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001279{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001280 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001281 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001282 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001283 allocation->x = window->margin + 10;
1284 allocation->y = window->margin + 50;
1285 allocation->width =
1286 window->allocation.width - 20 - window->margin * 2;
1287 allocation->height =
1288 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001289 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001290}
1291
1292void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001293window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001294{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001295 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001296 window->allocation.x = 20 + window->margin;
1297 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001298 window->allocation.width = width + 20 + window->margin * 2;
1299 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001300 } else {
1301 window->allocation.x = 0;
1302 window->allocation.y = 0;
1303 window->allocation.width = width;
1304 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001305 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001306}
1307
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001308static gboolean
1309idle_redraw(void *data)
1310{
1311 struct window *window = data;
1312
1313 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001314
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001315 window->redraw_scheduled = 0;
1316
1317 return FALSE;
1318}
1319
1320void
1321window_schedule_redraw(struct window *window)
1322{
1323 if (!window->redraw_scheduled) {
1324 g_idle_add(idle_redraw, window);
1325 window->redraw_scheduled = 1;
1326 }
1327}
1328
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001329void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001330window_set_fullscreen(struct window *window, int fullscreen)
1331{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001332 int32_t width, height;
1333
1334 if (window->fullscreen == fullscreen)
1335 return;
1336
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001337 window->fullscreen = fullscreen;
1338 if (window->fullscreen) {
1339 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001340 width = window->display->screen_allocation.width;
1341 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001342 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001343 width = window->saved_allocation.width - 20 - window->margin * 2;
1344 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001345 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001346
1347 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001348}
1349
1350void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001351window_set_decoration(struct window *window, int decoration)
1352{
1353 window->decoration = decoration;
1354}
1355
1356void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001357window_set_user_data(struct window *window, void *data)
1358{
1359 window->user_data = data;
1360}
1361
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001362void *
1363window_get_user_data(struct window *window)
1364{
1365 return window->user_data;
1366}
1367
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001368void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001369window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001370 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001371{
1372 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001373}
1374
1375void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001376window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001377 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001378{
1379 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001380}
1381
1382void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001383window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001384 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001385{
1386 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001387}
1388
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001389void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001390window_set_button_handler(struct window *window,
1391 window_button_handler_t handler)
1392{
1393 window->button_handler = handler;
1394}
1395
1396void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001397window_set_motion_handler(struct window *window,
1398 window_motion_handler_t handler)
1399{
1400 window->motion_handler = handler;
1401}
1402
1403void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001404window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001405 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001406{
1407 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001408}
1409
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001410void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001411window_set_title(struct window *window, const char *title)
1412{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001413 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001414 window->title = strdup(title);
1415}
1416
1417const char *
1418window_get_title(struct window *window)
1419{
1420 return window->title;
1421}
1422
1423void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001424window_damage(struct window *window, int32_t x, int32_t y,
1425 int32_t width, int32_t height)
1426{
1427 wl_surface_damage(window->surface, x, y, width, height);
1428}
1429
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001430static struct window *
1431window_create_internal(struct display *display, struct window *parent,
1432 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001433{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001434 struct window *window;
1435
1436 window = malloc(sizeof *window);
1437 if (window == NULL)
1438 return NULL;
1439
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001440 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001441 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001442 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001443 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001444 window->allocation.x = 0;
1445 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001446 window->allocation.width = width;
1447 window->allocation.height = height;
1448 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001449 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001450 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001451
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001452 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001453 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1454 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001455 else
1456 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001457
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001458 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001459 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001460
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001461 return window;
1462}
1463
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001464struct window *
1465window_create(struct display *display, int32_t width, int32_t height)
1466{
1467 struct window *window;
1468
1469 window = window_create_internal(display, NULL, width, height);
1470 if (!window)
1471 return NULL;
1472
1473 return window;
1474}
1475
1476struct window *
1477window_create_transient(struct display *display, struct window *parent,
1478 int32_t x, int32_t y, int32_t width, int32_t height)
1479{
1480 struct window *window;
1481
1482 window = window_create_internal(parent->display,
1483 parent, width, height);
1484 if (!window)
1485 return NULL;
1486
1487 window->x = x;
1488 window->y = y;
1489
1490 return window;
1491}
1492
1493void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001494window_set_buffer_type(struct window *window, enum window_buffer_type type)
1495{
1496 window->buffer_type = type;
1497}
1498
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001499static void
1500display_handle_geometry(void *data,
1501 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001502 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001503{
1504 struct display *display = data;
1505
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001506 display->screen_allocation.x = x;
1507 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001508 display->screen_allocation.width = width;
1509 display->screen_allocation.height = height;
1510}
1511
1512static const struct wl_output_listener output_listener = {
1513 display_handle_geometry,
1514};
1515
1516static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001517display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001518{
1519 struct input *input;
1520
1521 input = malloc(sizeof *input);
1522 if (input == NULL)
1523 return;
1524
1525 memset(input, 0, sizeof *input);
1526 input->display = d;
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001527 input->input_device = wl_input_device_create(d->display, id, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001528 input->pointer_focus = NULL;
1529 input->keyboard_focus = NULL;
1530 wl_list_insert(d->input_list.prev, &input->link);
1531
1532 wl_input_device_add_listener(input->input_device,
1533 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001534 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001535}
1536
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001537struct selection_offer {
1538 struct display *display;
1539 struct wl_selection_offer *offer;
1540 struct wl_array types;
1541 struct input *input;
1542};
1543
1544int
1545input_offers_mime_type(struct input *input, const char *type)
1546{
1547 struct selection_offer *offer = input->offer;
1548 char **p, **end;
1549
1550 if (offer == NULL)
1551 return 0;
1552
1553 end = offer->types.data + offer->types.size;
1554 for (p = offer->types.data; p < end; p++)
1555 if (strcmp(*p, type) == 0)
1556 return 1;
1557
1558 return 0;
1559}
1560
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001561void
1562input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001563{
1564 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001565
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001566 /* FIXME: A number of things can go wrong here: the object may
1567 * not be the current selection offer any more (which could
1568 * still work, but the source may have gone away or just
1569 * destroyed its wl_selection) or the offer may not have the
1570 * requested type after all (programmer/client error,
1571 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001572 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001573}
1574
1575static void
1576selection_offer_offer(void *data,
1577 struct wl_selection_offer *selection_offer,
1578 const char *type)
1579{
1580 struct selection_offer *offer = data;
1581
1582 char **p;
1583
1584 p = wl_array_add(&offer->types, sizeof *p);
1585 if (p)
1586 *p = strdup(type);
1587};
1588
1589static void
1590selection_offer_keyboard_focus(void *data,
1591 struct wl_selection_offer *selection_offer,
1592 struct wl_input_device *input_device)
1593{
1594 struct selection_offer *offer = data;
1595 struct input *input;
1596 char **p, **end;
1597
1598 if (input_device == NULL) {
1599 printf("selection offer retracted %p\n", selection_offer);
1600 input = offer->input;
1601 input->offer = NULL;
1602 wl_selection_offer_destroy(selection_offer);
1603 wl_array_release(&offer->types);
1604 free(offer);
1605 return;
1606 }
1607
1608 input = wl_input_device_get_user_data(input_device);
1609 printf("new selection offer %p:", selection_offer);
1610
1611 offer->input = input;
1612 input->offer = offer;
1613 end = offer->types.data + offer->types.size;
1614 for (p = offer->types.data; p < end; p++)
1615 printf(" %s", *p);
1616
1617 printf("\n");
1618}
1619
1620struct wl_selection_offer_listener selection_offer_listener = {
1621 selection_offer_offer,
1622 selection_offer_keyboard_focus
1623};
1624
1625static void
1626add_selection_offer(struct display *d, uint32_t id)
1627{
1628 struct selection_offer *offer;
1629
1630 offer = malloc(sizeof *offer);
1631 if (offer == NULL)
1632 return;
1633
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001634 offer->offer = wl_selection_offer_create(d->display, id, 1);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001635 offer->display = d;
1636 wl_array_init(&offer->types);
1637 offer->input = NULL;
1638
1639 wl_selection_offer_add_listener(offer->offer,
1640 &selection_offer_listener, offer);
1641}
1642
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001643static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001644display_handle_global(struct wl_display *display, uint32_t id,
1645 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001646{
1647 struct display *d = data;
1648
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001649 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001650 d->compositor = wl_compositor_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001651 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001652 d->output = wl_output_create(display, id, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001653 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001654 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001655 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001656 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001657 d->shell = wl_shell_create(display, id, 1);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001658 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001659 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001660 d->shm = wl_shm_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001661 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001662 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001663 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001664 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001665 }
1666}
1667
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001668static void
1669display_render_frame(struct display *d)
1670{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001671 int radius = 8;
1672 cairo_t *cr;
1673
1674 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1675 cr = cairo_create(d->shadow);
1676 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1677 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1678 rounded_rect(cr, 16, 16, 112, 112, radius);
1679 cairo_fill(cr);
1680 cairo_destroy(cr);
1681 blur_surface(d->shadow, 64);
1682
1683 d->active_frame =
1684 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1685 cr = cairo_create(d->active_frame);
1686 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1687 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1688 rounded_rect(cr, 16, 16, 112, 112, radius);
1689 cairo_fill(cr);
1690 cairo_destroy(cr);
1691
1692 d->inactive_frame =
1693 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1694 cr = cairo_create(d->inactive_frame);
1695 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1696 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1697 rounded_rect(cr, 16, 16, 112, 112, radius);
1698 cairo_fill(cr);
1699 cairo_destroy(cr);
1700}
1701
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001702static void
1703init_xkb(struct display *d)
1704{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001705 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001706
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001707 names.rules = "evdev";
1708 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001709 names.layout = option_xkb_layout;
1710 names.variant = option_xkb_variant;
1711 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001712
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001713 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001714 if (!d->xkb) {
1715 fprintf(stderr, "Failed to compile keymap\n");
1716 exit(1);
1717 }
1718}
1719
Yuval Fledel45568f62010-12-06 09:18:12 -05001720static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001721init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001722{
1723 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001724 EGLint n;
1725 static const EGLint cfg_attribs[] = {
1726 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1727 EGL_RED_SIZE, 1,
1728 EGL_GREEN_SIZE, 1,
1729 EGL_BLUE_SIZE, 1,
1730 EGL_ALPHA_SIZE, 1,
1731 EGL_DEPTH_SIZE, 1,
1732 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1733 EGL_NONE
1734 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001735
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001736 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001737 if (!eglInitialize(d->dpy, &major, &minor)) {
1738 fprintf(stderr, "failed to initialize display\n");
1739 return -1;
1740 }
1741
1742 if (!eglBindAPI(EGL_OPENGL_API)) {
1743 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1744 return -1;
1745 }
1746
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001747 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1748 fprintf(stderr, "failed to choose config\n");
1749 return -1;
1750 }
1751
1752 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001753 if (d->ctx == NULL) {
1754 fprintf(stderr, "failed to create context\n");
1755 return -1;
1756 }
1757
1758 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001759 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001760 return -1;
1761 }
1762
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001763#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001764 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001765 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001766 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001767 return -1;
1768 }
1769#endif
1770
1771 return 0;
1772}
1773
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001774struct display *
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001775display_create(int *argc, char **argv[], const GOptionEntry *option_entries,
1776 display_global_handler_t handler)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001777{
1778 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001779 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001780 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001781 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001782
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001783 g_type_init();
1784
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001785 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001786 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001787 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001788
1789 xkb_option_group = g_option_group_new("xkb",
1790 "Keyboard options",
1791 "Show all XKB options",
1792 NULL, NULL);
1793 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1794 g_option_context_add_group (context, xkb_option_group);
1795
1796 if (!g_option_context_parse(context, argc, argv, &error)) {
1797 fprintf(stderr, "option parsing failed: %s\n", error->message);
1798 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001799 }
1800
Tim Wiederhake748f6722011-01-23 23:25:25 +01001801 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001802
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001803 d = malloc(sizeof *d);
1804 if (d == NULL)
1805 return NULL;
1806
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001807 memset(d, 0, sizeof *d);
1808
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001809 d->global_handler = handler;
1810
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001811 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001812 if (d->display == NULL) {
1813 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001814 return NULL;
1815 }
1816
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001817 wl_list_init(&d->input_list);
1818
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001819 /* Set up listener so we'll catch all events. */
1820 wl_display_add_global_listener(d->display,
1821 display_handle_global, d);
1822
1823 /* Process connection events. */
1824 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1825
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001826 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001827 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001828
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001829 d->image_target_texture_2d =
1830 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1831 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1832 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1833
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001834 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001835
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001836 display_render_frame(d);
1837
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001838 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001839 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001840 g_source_attach(d->source, NULL);
1841
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001842 wl_list_init(&d->window_list);
1843
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001844 init_xkb(d);
1845
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001846 return d;
1847}
1848
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001849struct wl_display *
1850display_get_display(struct display *display)
1851{
1852 return display->display;
1853}
1854
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001855struct wl_compositor *
1856display_get_compositor(struct display *display)
1857{
1858 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001859}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001860
1861EGLDisplay
1862display_get_egl_display(struct display *d)
1863{
1864 return d->dpy;
1865}
1866
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001867EGLConfig
1868display_get_egl_config(struct display *d)
1869{
1870 return d->conf;
1871}
1872
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001873struct wl_shell *
1874display_get_shell(struct display *display)
1875{
1876 return display->shell;
1877}
1878
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001879void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001880display_acquire_window_surface(struct display *display,
1881 struct window *window,
1882 EGLContext ctx)
1883{
1884 struct egl_window_surface_data *data;
1885
1886 if (!window->cairo_surface)
1887 return;
1888
1889 if (!ctx)
1890 ctx = display->ctx;
1891
1892 data = cairo_surface_get_user_data(window->cairo_surface,
1893 &surface_data_key);
1894
1895 cairo_device_acquire(display->device);
1896 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
1897 fprintf(stderr, "failed to make surface current\n");
1898}
1899
1900void
1901display_release(struct display *display)
1902{
1903 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
1904 fprintf(stderr, "failed to make context current\n");
1905 cairo_device_release(display->device);
1906}
1907
1908void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001909display_run(struct display *d)
1910{
1911 g_main_loop_run(d->loop);
1912}