blob: 8c3f8d2cbf211b569d1831aad6dcf071fa049810 [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{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100737 if (window->cairo_surface) {
738 switch (window->buffer_type) {
739 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
740 case WINDOW_BUFFER_TYPE_SHM:
741 display_surface_damage(window->display,
742 window->cairo_surface,
743 0, 0,
744 window->allocation.width,
745 window->allocation.height);
746 break;
747 default:
748 break;
749 }
750 window_attach_surface(window);
751 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500752}
753
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400754void
755window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400756{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500757 cairo_surface_reference(surface);
758
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400759 if (window->cairo_surface != NULL)
760 cairo_surface_destroy(window->cairo_surface);
761
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500762 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400763}
764
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100765static void
766window_resize_cairo_window_surface(struct window *window)
767{
768 struct egl_window_surface_data *data;
769
770 data = cairo_surface_get_user_data(window->cairo_surface,
771 &surface_data_key);
772
773 wl_egl_window_resize(data->window,
774 window->allocation.width,
775 window->allocation.height, 0, 0);
776 cairo_gl_surface_set_size(window->cairo_surface,
777 window->allocation.width,
778 window->allocation.height);
779}
780
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400781void
782window_create_surface(struct window *window)
783{
784 cairo_surface_t *surface;
785
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400786 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500787#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
789 if (window->cairo_surface) {
790 window_resize_cairo_window_surface(window);
791 return;
792 }
793 surface = display_create_surface(window->display,
794 window->surface,
795 &window->allocation);
796 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500797 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400798 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799 NULL,
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400800 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400801 break;
802#endif
803 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400804 surface = display_create_shm_surface(window->display,
805 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400806 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800807 default:
808 surface = NULL;
809 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400811
812 window_set_surface(window, surface);
813 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814}
815
816static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500817window_draw_menu(struct window *window)
818{
819 cairo_t *cr;
820 int width, height, r = 5;
821
822 window_create_surface(window);
823
824 cr = cairo_create(window->cairo_surface);
825 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
826 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
827 cairo_paint(cr);
828
829 width = window->allocation.width;
830 height = window->allocation.height;
831 rounded_rect(cr, r, r, width - r, height - r, r);
832 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
833 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
834 cairo_fill(cr);
835 cairo_destroy(cr);
836}
837
838static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500839window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500840{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500841 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500842 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400843 cairo_surface_t *frame;
844 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500845
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400846 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400848 width = window->allocation.width;
849 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500850
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500851 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500852
Kristian Høgsberg09531622010-06-14 23:22:15 -0400853 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400854 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400855 cairo_paint(cr);
856
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400857 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400858 tile_mask(cr, window->display->shadow,
859 shadow_dx, shadow_dy, width, height,
860 window->margin + 10 - shadow_dx,
861 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500862
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400863 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400864 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400865 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400866 frame = window->display->inactive_frame;
867
868 tile_source(cr, frame, 0, 0, width, height,
869 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500870
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500871 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
872 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500873 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400874 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400875 if (window->keyboard_device)
876 cairo_set_source_rgb(cr, 0, 0, 0);
877 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400878 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400879 cairo_show_text(cr, window->title);
880
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500881 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400882
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100883 /* FIXME: this breakes gears, fix cairo? */
884#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400885 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100886#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500887}
888
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400889void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500890window_destroy(struct window *window)
891{
892 wl_surface_destroy(window->surface);
893 wl_list_remove(&window->link);
894 free(window);
895}
896
897void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400898display_flush_cairo_device(struct display *display)
899{
900 cairo_device_flush (display->device);
901}
902
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500903void
904window_draw(struct window *window)
905{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500906 if (window->parent)
907 window_draw_menu(window);
908 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400909 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500910 else
911 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500912}
913
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400914cairo_surface_t *
915window_get_surface(struct window *window)
916{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400917 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400918}
919
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100920struct wl_surface *
921window_get_wl_surface(struct window *window)
922{
923 return window->surface;
924}
925
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400926static int
927get_pointer_location(struct window *window, int32_t x, int32_t y)
928{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400929 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400930 const int grip_size = 8;
931
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500932 if (!window->decoration)
933 return WINDOW_CLIENT_AREA;
934
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400935 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400936 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400937 else if (window->margin <= x && x < window->margin + grip_size)
938 hlocation = WINDOW_RESIZING_LEFT;
939 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400940 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400941 else if (x < window->allocation.width - window->margin)
942 hlocation = WINDOW_RESIZING_RIGHT;
943 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400944 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400945
946 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400947 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400948 else if (window->margin <= y && y < window->margin + grip_size)
949 vlocation = WINDOW_RESIZING_TOP;
950 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400951 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400952 else if (y < window->allocation.height - window->margin)
953 vlocation = WINDOW_RESIZING_BOTTOM;
954 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400955 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400956
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400957 location = vlocation | hlocation;
958 if (location & WINDOW_EXTERIOR)
959 location = WINDOW_EXTERIOR;
960 if (location == WINDOW_INTERIOR && y < window->margin + 50)
961 location = WINDOW_TITLEBAR;
962 else if (location == WINDOW_INTERIOR)
963 location = WINDOW_CLIENT_AREA;
964
965 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400966}
967
968static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400969set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400970{
971 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400972 struct wl_buffer *buffer;
973 cairo_surface_t *surface;
974 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400975
976 location = get_pointer_location(input->pointer_focus,
977 input->sx, input->sy);
978 switch (location) {
979 case WINDOW_RESIZING_TOP:
980 pointer = POINTER_TOP;
981 break;
982 case WINDOW_RESIZING_BOTTOM:
983 pointer = POINTER_BOTTOM;
984 break;
985 case WINDOW_RESIZING_LEFT:
986 pointer = POINTER_LEFT;
987 break;
988 case WINDOW_RESIZING_RIGHT:
989 pointer = POINTER_RIGHT;
990 break;
991 case WINDOW_RESIZING_TOP_LEFT:
992 pointer = POINTER_TOP_LEFT;
993 break;
994 case WINDOW_RESIZING_TOP_RIGHT:
995 pointer = POINTER_TOP_RIGHT;
996 break;
997 case WINDOW_RESIZING_BOTTOM_LEFT:
998 pointer = POINTER_BOTTOM_LEFT;
999 break;
1000 case WINDOW_RESIZING_BOTTOM_RIGHT:
1001 pointer = POINTER_BOTTOM_RIGHT;
1002 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001003 case WINDOW_EXTERIOR:
1004 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001005 if (input->current_pointer_image == POINTER_DEFAULT)
1006 return;
1007
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001008 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001009 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001010 return;
1011 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001012 break;
1013 }
1014
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001015 if (pointer == input->current_pointer_image)
1016 return;
1017
1018 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001019 surface = display->pointer_surfaces[pointer];
1020 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001021 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001022 pointer_images[pointer].hotspot_x,
1023 pointer_images[pointer].hotspot_y);
1024}
1025
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001026static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001027window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001028 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001029 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001030{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001031 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001032 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001033 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001034
1035 input->x = x;
1036 input->y = y;
1037 input->sx = sx;
1038 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001039
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001040 if (window->motion_handler)
1041 pointer = (*window->motion_handler)(window, input, time,
1042 x, y, sx, sy,
1043 window->user_data);
1044
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001045 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001046}
1047
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001048static void
1049window_handle_button(void *data,
1050 struct wl_input_device *input_device,
1051 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001052{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001053 struct input *input = data;
1054 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001055 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001056
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001057 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001058
1059 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001060 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001061 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001062 wl_shell_move(window->display->shell,
1063 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001064 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001065 case WINDOW_RESIZING_TOP:
1066 case WINDOW_RESIZING_BOTTOM:
1067 case WINDOW_RESIZING_LEFT:
1068 case WINDOW_RESIZING_RIGHT:
1069 case WINDOW_RESIZING_TOP_LEFT:
1070 case WINDOW_RESIZING_TOP_RIGHT:
1071 case WINDOW_RESIZING_BOTTOM_LEFT:
1072 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001073 wl_shell_resize(window->display->shell,
1074 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001075 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001076 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001077 case WINDOW_CLIENT_AREA:
1078 if (window->button_handler)
1079 (*window->button_handler)(window,
1080 input, time,
1081 button, state,
1082 window->user_data);
1083 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001084 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001085 } else {
1086 if (window->button_handler)
1087 (*window->button_handler)(window,
1088 input, time,
1089 button, state,
1090 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001091 }
1092}
1093
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001094static void
1095window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001096 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001097{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001098 struct input *input = data;
1099 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001100 struct display *d = window->display;
1101 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001102
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001103 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001104 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001105 return;
1106
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001107 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001108 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001109 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1110 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001111
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001112 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1113
1114 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001115 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001116 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001117 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001118
1119 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001120 (*window->key_handler)(window, input, time, key, sym, state,
1121 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001122}
1123
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001124static void
1125window_handle_pointer_focus(void *data,
1126 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001127 uint32_t time, struct wl_surface *surface,
1128 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001129{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001130 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131 struct window *window;
1132 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001133
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001134 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001135 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001136 window = input->pointer_focus;
1137
Kristian Høgsberg59826582011-01-20 11:56:57 -05001138 input->x = x;
1139 input->y = y;
1140 input->sx = sx;
1141 input->sy = sy;
1142
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143 pointer = POINTER_LEFT_PTR;
1144 if (window->motion_handler)
1145 pointer = (*window->motion_handler)(window,
1146 input, time,
1147 x, y, sx, sy,
1148 window->user_data);
1149
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001150 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001151 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001152 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001153 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001154 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001155}
1156
1157static void
1158window_handle_keyboard_focus(void *data,
1159 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001160 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001161 struct wl_surface *surface,
1162 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001163{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001164 struct input *input = data;
1165 struct window *window = input->keyboard_focus;
1166 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001167 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001168
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001169 window = input->keyboard_focus;
1170 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001171 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001172 if (window->keyboard_focus_handler)
1173 (*window->keyboard_focus_handler)(window, NULL,
1174 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001175 }
1176
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001177 if (surface)
1178 input->keyboard_focus = wl_surface_get_user_data(surface);
1179 else
1180 input->keyboard_focus = NULL;
1181
1182 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001183 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001184 for (k = keys->data; k < end; k++)
1185 input->modifiers |= d->xkb->map->modmap[*k];
1186
1187 window = input->keyboard_focus;
1188 if (window) {
1189 window->keyboard_device = input;
1190 if (window->keyboard_focus_handler)
1191 (*window->keyboard_focus_handler)(window,
1192 window->keyboard_device,
1193 window->user_data);
1194 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001195}
1196
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001197static const struct wl_input_device_listener input_device_listener = {
1198 window_handle_motion,
1199 window_handle_button,
1200 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001201 window_handle_pointer_focus,
1202 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001203};
1204
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001205void
1206input_get_position(struct input *input, int32_t *x, int32_t *y)
1207{
1208 *x = input->sx;
1209 *y = input->sy;
1210}
1211
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001212struct wl_input_device *
1213input_get_input_device(struct input *input)
1214{
1215 return input->input_device;
1216}
1217
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001218uint32_t
1219input_get_modifiers(struct input *input)
1220{
1221 return input->modifiers;
1222}
1223
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001224struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001225window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001226{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001227 cairo_device_flush (window->display->device);
1228
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001229 return wl_shell_create_drag(window->display->shell);
1230}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001231
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001232void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001233window_move(struct window *window, struct input *input, uint32_t time)
1234{
1235 wl_shell_move(window->display->shell,
1236 window->surface, input->input_device, time);
1237}
1238
1239void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001240window_activate_drag(struct wl_drag *drag, struct window *window,
1241 struct input *input, uint32_t time)
1242{
1243 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001244}
1245
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001246static void
1247handle_configure(void *data, struct wl_shell *shell,
1248 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001249 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001250{
1251 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001252 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001253
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001254 /* FIXME: this is probably the wrong place to check for width
1255 * or height <= 0, but it prevents the compositor from crashing
1256 */
1257 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001258 return;
1259
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001260 window->resize_edges = edges;
1261
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001262 if (window->resize_handler) {
1263 child_width = width - 20 - window->margin * 2;
1264 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001265
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001266 (*window->resize_handler)(window,
1267 child_width, child_height,
1268 window->user_data);
1269 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001270 window->allocation.width = width;
1271 window->allocation.height = height;
1272
1273 if (window->redraw_handler)
1274 window_schedule_redraw(window);
1275 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001276}
1277
1278static const struct wl_shell_listener shell_listener = {
1279 handle_configure,
1280};
1281
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001282void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001283window_get_allocation(struct window *window,
1284 struct rectangle *allocation)
1285{
1286 *allocation = window->allocation;
1287}
1288
1289void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001290window_get_child_allocation(struct window *window,
1291 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001292{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001293 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001294 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001295 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001296 allocation->x = window->margin + 10;
1297 allocation->y = window->margin + 50;
1298 allocation->width =
1299 window->allocation.width - 20 - window->margin * 2;
1300 allocation->height =
1301 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001302 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001303}
1304
1305void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001306window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001307{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001308 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001309 window->allocation.x = 20 + window->margin;
1310 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001311 window->allocation.width = width + 20 + window->margin * 2;
1312 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001313 } else {
1314 window->allocation.x = 0;
1315 window->allocation.y = 0;
1316 window->allocation.width = width;
1317 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001318 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001319}
1320
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001321static gboolean
1322idle_redraw(void *data)
1323{
1324 struct window *window = data;
1325
1326 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001327
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001328 window->redraw_scheduled = 0;
1329
1330 return FALSE;
1331}
1332
1333void
1334window_schedule_redraw(struct window *window)
1335{
1336 if (!window->redraw_scheduled) {
1337 g_idle_add(idle_redraw, window);
1338 window->redraw_scheduled = 1;
1339 }
1340}
1341
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001342void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001343window_set_fullscreen(struct window *window, int fullscreen)
1344{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001345 int32_t width, height;
1346
1347 if (window->fullscreen == fullscreen)
1348 return;
1349
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001350 window->fullscreen = fullscreen;
1351 if (window->fullscreen) {
1352 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001353 width = window->display->screen_allocation.width;
1354 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001355 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001356 width = window->saved_allocation.width - 20 - window->margin * 2;
1357 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001358 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001359
1360 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001361}
1362
1363void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001364window_set_decoration(struct window *window, int decoration)
1365{
1366 window->decoration = decoration;
1367}
1368
1369void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001370window_set_user_data(struct window *window, void *data)
1371{
1372 window->user_data = data;
1373}
1374
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001375void *
1376window_get_user_data(struct window *window)
1377{
1378 return window->user_data;
1379}
1380
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001381void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001382window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001383 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001384{
1385 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001386}
1387
1388void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001389window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001390 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001391{
1392 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001393}
1394
1395void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001396window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001397 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001398{
1399 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001400}
1401
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001402void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001403window_set_button_handler(struct window *window,
1404 window_button_handler_t handler)
1405{
1406 window->button_handler = handler;
1407}
1408
1409void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001410window_set_motion_handler(struct window *window,
1411 window_motion_handler_t handler)
1412{
1413 window->motion_handler = handler;
1414}
1415
1416void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001417window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001418 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001419{
1420 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001421}
1422
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001423void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001424window_set_title(struct window *window, const char *title)
1425{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001426 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001427 window->title = strdup(title);
1428}
1429
1430const char *
1431window_get_title(struct window *window)
1432{
1433 return window->title;
1434}
1435
1436void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001437display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1438 int32_t x, int32_t y, int32_t width, int32_t height)
1439{
1440 struct wl_buffer *buffer;
1441
1442 buffer = display_get_buffer_for_surface(display, cairo_surface);
1443
1444 wl_buffer_damage(buffer, x, y, width, height);
1445}
1446
1447void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001448window_damage(struct window *window, int32_t x, int32_t y,
1449 int32_t width, int32_t height)
1450{
1451 wl_surface_damage(window->surface, x, y, width, height);
1452}
1453
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001454static struct window *
1455window_create_internal(struct display *display, struct window *parent,
1456 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001457{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001458 struct window *window;
1459
1460 window = malloc(sizeof *window);
1461 if (window == NULL)
1462 return NULL;
1463
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001464 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001465 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001466 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001467 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001468 window->allocation.x = 0;
1469 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001470 window->allocation.width = width;
1471 window->allocation.height = height;
1472 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001473 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001474 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001475
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001476 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001477 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1478 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001479 else
1480 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001481
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001482 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001483 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001484
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001485 return window;
1486}
1487
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001488struct window *
1489window_create(struct display *display, int32_t width, int32_t height)
1490{
1491 struct window *window;
1492
1493 window = window_create_internal(display, NULL, width, height);
1494 if (!window)
1495 return NULL;
1496
1497 return window;
1498}
1499
1500struct window *
1501window_create_transient(struct display *display, struct window *parent,
1502 int32_t x, int32_t y, int32_t width, int32_t height)
1503{
1504 struct window *window;
1505
1506 window = window_create_internal(parent->display,
1507 parent, width, height);
1508 if (!window)
1509 return NULL;
1510
1511 window->x = x;
1512 window->y = y;
1513
1514 return window;
1515}
1516
1517void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001518window_set_buffer_type(struct window *window, enum window_buffer_type type)
1519{
1520 window->buffer_type = type;
1521}
1522
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001523static void
1524display_handle_geometry(void *data,
1525 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001526 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001527{
1528 struct display *display = data;
1529
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001530 display->screen_allocation.x = x;
1531 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001532 display->screen_allocation.width = width;
1533 display->screen_allocation.height = height;
1534}
1535
1536static const struct wl_output_listener output_listener = {
1537 display_handle_geometry,
1538};
1539
1540static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001541display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001542{
1543 struct input *input;
1544
1545 input = malloc(sizeof *input);
1546 if (input == NULL)
1547 return;
1548
1549 memset(input, 0, sizeof *input);
1550 input->display = d;
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001551 input->input_device = wl_input_device_create(d->display, id, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001552 input->pointer_focus = NULL;
1553 input->keyboard_focus = NULL;
1554 wl_list_insert(d->input_list.prev, &input->link);
1555
1556 wl_input_device_add_listener(input->input_device,
1557 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001558 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001559}
1560
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001561struct selection_offer {
1562 struct display *display;
1563 struct wl_selection_offer *offer;
1564 struct wl_array types;
1565 struct input *input;
1566};
1567
1568int
1569input_offers_mime_type(struct input *input, const char *type)
1570{
1571 struct selection_offer *offer = input->offer;
1572 char **p, **end;
1573
1574 if (offer == NULL)
1575 return 0;
1576
1577 end = offer->types.data + offer->types.size;
1578 for (p = offer->types.data; p < end; p++)
1579 if (strcmp(*p, type) == 0)
1580 return 1;
1581
1582 return 0;
1583}
1584
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001585void
1586input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001587{
1588 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001589
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001590 /* FIXME: A number of things can go wrong here: the object may
1591 * not be the current selection offer any more (which could
1592 * still work, but the source may have gone away or just
1593 * destroyed its wl_selection) or the offer may not have the
1594 * requested type after all (programmer/client error,
1595 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001596 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001597}
1598
1599static void
1600selection_offer_offer(void *data,
1601 struct wl_selection_offer *selection_offer,
1602 const char *type)
1603{
1604 struct selection_offer *offer = data;
1605
1606 char **p;
1607
1608 p = wl_array_add(&offer->types, sizeof *p);
1609 if (p)
1610 *p = strdup(type);
1611};
1612
1613static void
1614selection_offer_keyboard_focus(void *data,
1615 struct wl_selection_offer *selection_offer,
1616 struct wl_input_device *input_device)
1617{
1618 struct selection_offer *offer = data;
1619 struct input *input;
1620 char **p, **end;
1621
1622 if (input_device == NULL) {
1623 printf("selection offer retracted %p\n", selection_offer);
1624 input = offer->input;
1625 input->offer = NULL;
1626 wl_selection_offer_destroy(selection_offer);
1627 wl_array_release(&offer->types);
1628 free(offer);
1629 return;
1630 }
1631
1632 input = wl_input_device_get_user_data(input_device);
1633 printf("new selection offer %p:", selection_offer);
1634
1635 offer->input = input;
1636 input->offer = offer;
1637 end = offer->types.data + offer->types.size;
1638 for (p = offer->types.data; p < end; p++)
1639 printf(" %s", *p);
1640
1641 printf("\n");
1642}
1643
1644struct wl_selection_offer_listener selection_offer_listener = {
1645 selection_offer_offer,
1646 selection_offer_keyboard_focus
1647};
1648
1649static void
1650add_selection_offer(struct display *d, uint32_t id)
1651{
1652 struct selection_offer *offer;
1653
1654 offer = malloc(sizeof *offer);
1655 if (offer == NULL)
1656 return;
1657
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001658 offer->offer = wl_selection_offer_create(d->display, id, 1);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001659 offer->display = d;
1660 wl_array_init(&offer->types);
1661 offer->input = NULL;
1662
1663 wl_selection_offer_add_listener(offer->offer,
1664 &selection_offer_listener, offer);
1665}
1666
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001667static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001668display_handle_global(struct wl_display *display, uint32_t id,
1669 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001670{
1671 struct display *d = data;
1672
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001673 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001674 d->compositor = wl_compositor_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001675 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001676 d->output = wl_output_create(display, id, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001677 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001678 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001679 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001680 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001681 d->shell = wl_shell_create(display, id, 1);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001682 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001683 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001684 d->shm = wl_shm_create(display, id, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001685 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001686 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001687 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001688 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001689 }
1690}
1691
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001692static void
1693display_render_frame(struct display *d)
1694{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001695 int radius = 8;
1696 cairo_t *cr;
1697
1698 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1699 cr = cairo_create(d->shadow);
1700 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1701 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1702 rounded_rect(cr, 16, 16, 112, 112, radius);
1703 cairo_fill(cr);
1704 cairo_destroy(cr);
1705 blur_surface(d->shadow, 64);
1706
1707 d->active_frame =
1708 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1709 cr = cairo_create(d->active_frame);
1710 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1711 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1712 rounded_rect(cr, 16, 16, 112, 112, radius);
1713 cairo_fill(cr);
1714 cairo_destroy(cr);
1715
1716 d->inactive_frame =
1717 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1718 cr = cairo_create(d->inactive_frame);
1719 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1720 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1721 rounded_rect(cr, 16, 16, 112, 112, radius);
1722 cairo_fill(cr);
1723 cairo_destroy(cr);
1724}
1725
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001726static void
1727init_xkb(struct display *d)
1728{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001729 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001730
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001731 names.rules = "evdev";
1732 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001733 names.layout = option_xkb_layout;
1734 names.variant = option_xkb_variant;
1735 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001736
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001737 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001738 if (!d->xkb) {
1739 fprintf(stderr, "Failed to compile keymap\n");
1740 exit(1);
1741 }
1742}
1743
Yuval Fledel45568f62010-12-06 09:18:12 -05001744static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001745init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001746{
1747 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001748 EGLint n;
1749 static const EGLint cfg_attribs[] = {
1750 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1751 EGL_RED_SIZE, 1,
1752 EGL_GREEN_SIZE, 1,
1753 EGL_BLUE_SIZE, 1,
1754 EGL_ALPHA_SIZE, 1,
1755 EGL_DEPTH_SIZE, 1,
1756 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1757 EGL_NONE
1758 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001759
Egbert Eiche7b8d902011-05-10 20:00:19 +00001760 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001761 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001762 if (!eglInitialize(d->dpy, &major, &minor)) {
1763 fprintf(stderr, "failed to initialize display\n");
1764 return -1;
1765 }
1766
1767 if (!eglBindAPI(EGL_OPENGL_API)) {
1768 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1769 return -1;
1770 }
1771
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001772 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1773 fprintf(stderr, "failed to choose config\n");
1774 return -1;
1775 }
1776
1777 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001778 if (d->ctx == NULL) {
1779 fprintf(stderr, "failed to create context\n");
1780 return -1;
1781 }
1782
1783 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001784 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001785 return -1;
1786 }
1787
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001788#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001789 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001790 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001791 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001792 return -1;
1793 }
1794#endif
1795
1796 return 0;
1797}
1798
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001799struct display *
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001800display_create(int *argc, char **argv[], const GOptionEntry *option_entries,
1801 display_global_handler_t handler)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001802{
1803 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001804 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001805 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001806 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001807
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001808 g_type_init();
1809
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001810 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001811 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001812 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001813
1814 xkb_option_group = g_option_group_new("xkb",
1815 "Keyboard options",
1816 "Show all XKB options",
1817 NULL, NULL);
1818 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1819 g_option_context_add_group (context, xkb_option_group);
1820
1821 if (!g_option_context_parse(context, argc, argv, &error)) {
1822 fprintf(stderr, "option parsing failed: %s\n", error->message);
1823 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001824 }
1825
Tim Wiederhake748f6722011-01-23 23:25:25 +01001826 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001827
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001828 d = malloc(sizeof *d);
1829 if (d == NULL)
1830 return NULL;
1831
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001832 memset(d, 0, sizeof *d);
1833
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001834 d->global_handler = handler;
1835
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001836 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001837 if (d->display == NULL) {
1838 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001839 return NULL;
1840 }
1841
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001842 wl_list_init(&d->input_list);
1843
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001844 /* Set up listener so we'll catch all events. */
1845 wl_display_add_global_listener(d->display,
1846 display_handle_global, d);
1847
1848 /* Process connection events. */
1849 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1850
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001851 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001852 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001853
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001854 d->image_target_texture_2d =
1855 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1856 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1857 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1858
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001859 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001860
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001861 display_render_frame(d);
1862
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001863 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001864 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001865 g_source_attach(d->source, NULL);
1866
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001867 wl_list_init(&d->window_list);
1868
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001869 init_xkb(d);
1870
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001871 return d;
1872}
1873
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001874struct wl_display *
1875display_get_display(struct display *display)
1876{
1877 return display->display;
1878}
1879
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001880struct wl_compositor *
1881display_get_compositor(struct display *display)
1882{
1883 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001884}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001885
1886EGLDisplay
1887display_get_egl_display(struct display *d)
1888{
1889 return d->dpy;
1890}
1891
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001892EGLConfig
1893display_get_egl_config(struct display *d)
1894{
1895 return d->conf;
1896}
1897
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001898struct wl_shell *
1899display_get_shell(struct display *display)
1900{
1901 return display->shell;
1902}
1903
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001904void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001905display_acquire_window_surface(struct display *display,
1906 struct window *window,
1907 EGLContext ctx)
1908{
1909 struct egl_window_surface_data *data;
1910
1911 if (!window->cairo_surface)
1912 return;
1913
1914 if (!ctx)
1915 ctx = display->ctx;
1916
1917 data = cairo_surface_get_user_data(window->cairo_surface,
1918 &surface_data_key);
1919
1920 cairo_device_acquire(display->device);
1921 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
1922 fprintf(stderr, "failed to make surface current\n");
1923}
1924
1925void
1926display_release(struct display *display)
1927{
1928 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
1929 fprintf(stderr, "failed to make context current\n");
1930 cairo_device_release(display->device);
1931}
1932
1933void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001934display_run(struct display *d)
1935{
1936 g_main_loop_run(d->loop);
1937}