blob: 68c1eadf1f4d9988ebabf9fb4d2bed1927bb45b3 [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;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020069 EGLConfig premultiplied_argb_config;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040070 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010071 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040073 GMainLoop *loop;
74 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040075 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040076 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040077 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040078 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040079 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040080 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040081
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050082 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
83 PFNEGLCREATEIMAGEKHRPROC create_image;
84 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085};
86
87struct window {
88 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050089 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050090 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050091 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050092 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050093 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040094 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040095 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050096 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050097 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050098 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040099 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400100 int transparent;
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,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400214 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100215 struct rectangle *rectangle)
216{
217 cairo_surface_t *cairo_surface;
218 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400219 EGLConfig config;
220 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100221
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400222 static const EGLint premul_attribs[] = {
223 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
224 EGL_NONE
225 };
226
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100227 data = malloc(sizeof *data);
228 if (data == NULL)
229 return NULL;
230
231 data->display = display;
232 data->surface = surface;
233
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400234 if (flags & SURFACE_OPAQUE) {
235 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200236 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400237 } else {
238 config = display->premultiplied_argb_config;
239 attribs = premul_attribs;
240 }
241
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400242 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100243 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400244 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100245
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400246 data->surf = eglCreateWindowSurface(display->dpy, config,
247 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100248
249 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
250 data->surf,
251 rectangle->width,
252 rectangle->height);
253
254 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
255 data, egl_window_surface_data_destroy);
256
257 return cairo_surface;
258}
259
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500260struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400261 struct surface_data data;
262 EGLImageKHR image;
263 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800264 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500265 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400266};
267
268static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500269egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400270{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500271 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800272 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400273
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800274 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800276 cairo_device_release(d->device);
277
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500278 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400279 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500280 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500281 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400282}
283
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500284EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500285display_get_image_for_egl_image_surface(struct display *display,
286 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500287{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500288 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500289
290 data = cairo_surface_get_user_data (surface, &surface_data_key);
291
292 return data->image;
293}
294
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500295static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500296display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500298 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400299{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301 EGLDisplay dpy = display->dpy;
302 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400303 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400304
305 data = malloc(sizeof *data);
306 if (data == NULL)
307 return NULL;
308
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800309 data->display = display;
310
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400311 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400312 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000314 free(data);
315 return NULL;
316 }
317
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400318 if (flags & SURFACE_OPAQUE)
319 config = display->rgb_config;
320 else
321 config = display->premultiplied_argb_config;
322
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500323 data->image = display->create_image(dpy, NULL,
324 EGL_NATIVE_PIXMAP_KHR,
325 (EGLClientBuffer) data->pixmap,
326 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500327 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500328 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329 free(data);
330 return NULL;
331 }
332
333 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400334 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800336 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400338 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500339 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800340 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400341
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342 surface = cairo_gl_surface_create_for_texture(display->device,
343 CAIRO_CONTENT_COLOR_ALPHA,
344 data->texture,
345 rectangle->width,
346 rectangle->height);
347
348 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350
351 return surface;
352}
353
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500354static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500355display_create_egl_image_surface_from_file(struct display *display,
356 const char *filename,
357 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358{
359 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400360 GdkPixbuf *pixbuf;
361 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400362 int stride, i;
363 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500364 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400365
366 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400367 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400368 FALSE, &error);
369 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400370 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400371
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
373 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500374 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400375 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400376 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400377
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400378
379 stride = gdk_pixbuf_get_rowstride(pixbuf);
380 pixels = gdk_pixbuf_get_pixels(pixbuf);
381
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400382 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400383 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400384 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400385 while (p < end) {
386 unsigned int t;
387
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400388 MULT(p[0], p[0], p[3], t);
389 MULT(p[1], p[1], p[3], t);
390 MULT(p[2], p[2], p[3], t);
391 p += 4;
392
393 }
394 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400395
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200396 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000397 if (surface == NULL) {
398 g_object_unref(pixbuf);
399 return NULL;
400 }
401
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800402 data = cairo_surface_get_user_data(surface, &surface_data_key);
403
404 cairo_device_acquire(display->device);
405 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800406 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
407 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800408 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400409
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500410 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400411
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412 return surface;
413}
414
415#endif
416
417struct wl_buffer *
418display_get_buffer_for_surface(struct display *display,
419 cairo_surface_t *surface)
420{
421 struct surface_data *data;
422
423 data = cairo_surface_get_user_data (surface, &surface_data_key);
424
425 return data->buffer;
426}
427
428struct shm_surface_data {
429 struct surface_data data;
430 void *map;
431 size_t length;
432};
433
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500434static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435shm_surface_data_destroy(void *p)
436{
437 struct shm_surface_data *data = p;
438
439 wl_buffer_destroy(data->data.buffer);
440 munmap(data->map, data->length);
441}
442
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500443static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400444display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400445 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400446{
447 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400448 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400449 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800450 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400451 char filename[] = "/tmp/wayland-shm-XXXXXX";
452
453 data = malloc(sizeof *data);
454 if (data == NULL)
455 return NULL;
456
457 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
458 rectangle->width);
459 data->length = stride * rectangle->height;
460 fd = mkstemp(filename);
461 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000462 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400463 return NULL;
464 }
465 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000466 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467 close(fd);
468 return NULL;
469 }
470
471 data->map = mmap(NULL, data->length,
472 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
473 unlink(filename);
474
475 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000476 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400477 close(fd);
478 return NULL;
479 }
480
481 surface = cairo_image_surface_create_for_data (data->map,
482 CAIRO_FORMAT_ARGB32,
483 rectangle->width,
484 rectangle->height,
485 stride);
486
487 cairo_surface_set_user_data (surface, &surface_data_key,
488 data, shm_surface_data_destroy);
489
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400490 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400491 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400492 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400493 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400494
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400495 data->data.buffer = wl_shm_create_buffer(display->shm,
496 fd,
497 rectangle->width,
498 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400499 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500
501 close(fd);
502
503 return surface;
504}
505
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500506static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507display_create_shm_surface_from_file(struct display *display,
508 const char *filename,
509 struct rectangle *rect)
510{
511 cairo_surface_t *surface;
512 GdkPixbuf *pixbuf;
513 GError *error = NULL;
514 int stride, i;
515 unsigned char *pixels, *p, *end, *dest_data;
516 int dest_stride;
517 uint32_t *d;
518
519 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
520 rect->width, rect->height,
521 FALSE, &error);
522 if (error != NULL)
523 return NULL;
524
525 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
526 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500527 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528 return NULL;
529 }
530
531 stride = gdk_pixbuf_get_rowstride(pixbuf);
532 pixels = gdk_pixbuf_get_pixels(pixbuf);
533
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400534 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000535 if (surface == NULL) {
536 g_object_unref(pixbuf);
537 return NULL;
538 }
539
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 dest_data = cairo_image_surface_get_data (surface);
541 dest_stride = cairo_image_surface_get_stride (surface);
542
543 for (i = 0; i < rect->height; i++) {
544 d = (uint32_t *) (dest_data + i * dest_stride);
545 p = pixels + i * stride;
546 end = p + rect->width * 4;
547 while (p < end) {
548 unsigned int t;
549 unsigned char a, r, g, b;
550
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 a = p[3];
552 MULT(r, p[0], a, t);
553 MULT(g, p[1], a, t);
554 MULT(b, p[2], a, t);
555 p += 4;
556 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
557 }
558 }
559
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500560 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561
562 return surface;
563}
564
nobled7b87cb02011-02-01 18:51:47 +0000565static int
566check_size(struct rectangle *rect)
567{
568 if (rect->width && rect->height)
569 return 0;
570
571 fprintf(stderr, "tried to create surface of "
572 "width: %d, height: %d\n", rect->width, rect->height);
573 return -1;
574}
575
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576cairo_surface_t *
577display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100578 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400579 struct rectangle *rectangle,
580 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581{
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) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100586 if (surface)
587 return display_create_egl_window_surface(display,
588 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400589 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100590 rectangle);
591 else
592 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400593 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100594 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500595 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400597 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598}
599
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500600static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400601display_create_surface_from_file(struct display *display,
602 const char *filename,
603 struct rectangle *rectangle)
604{
nobled7b87cb02011-02-01 18:51:47 +0000605 if (check_size(rectangle) < 0)
606 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500607#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500608 if (display->dpy) {
609 return display_create_egl_image_surface_from_file(display,
610 filename,
611 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500612 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500614 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400615}
Yuval Fledel45568f62010-12-06 09:18:12 -0500616 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400617 const char *filename;
618 int hotspot_x, hotspot_y;
619} pointer_images[] = {
620 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
621 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
622 { DATADIR "/wayland/bottom_side.png", 16, 20 },
623 { DATADIR "/wayland/grabbing.png", 20, 17 },
624 { DATADIR "/wayland/left_ptr.png", 10, 5 },
625 { DATADIR "/wayland/left_side.png", 10, 20 },
626 { DATADIR "/wayland/right_side.png", 30, 19 },
627 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
628 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
629 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400630 { DATADIR "/wayland/xterm.png", 15, 15 },
631 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400632};
633
634static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400635create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400636{
637 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400638 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400639 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400640
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400641 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400642 display->pointer_surfaces =
643 malloc(count * sizeof *display->pointer_surfaces);
644 rect.width = width;
645 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400646 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400647 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648 display_create_surface_from_file(display,
649 pointer_images[i].filename,
650 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400651 }
652
653}
654
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400655cairo_surface_t *
656display_get_pointer_surface(struct display *display, int pointer,
657 int *width, int *height,
658 int *hotspot_x, int *hotspot_y)
659{
660 cairo_surface_t *surface;
661
662 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500663#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400664 *width = cairo_gl_surface_get_width(surface);
665 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000666#else
667 *width = cairo_image_surface_get_width(surface);
668 *height = cairo_image_surface_get_height(surface);
669#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400670 *hotspot_x = pointer_images[pointer].hotspot_x;
671 *hotspot_y = pointer_images[pointer].hotspot_y;
672
673 return cairo_surface_reference(surface);
674}
675
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400676
677static void
678window_attach_surface(struct window *window);
679
680static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400681free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400682{
683 struct window *window = data;
684
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400685 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400686 cairo_surface_destroy(window->pending_surface);
687 window->pending_surface = NULL;
688 if (window->cairo_surface)
689 window_attach_surface(window);
690}
691
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400692static const struct wl_callback_listener free_surface_listener = {
693 free_surface
694};
695
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500696static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200697window_get_resize_dx_dy(struct window *window, int *x, int *y)
698{
699 if (window->resize_edges & WINDOW_RESIZING_LEFT)
700 *x = window->server_allocation.width - window->allocation.width;
701 else
702 *x = 0;
703
704 if (window->resize_edges & WINDOW_RESIZING_TOP)
705 *y = window->server_allocation.height -
706 window->allocation.height;
707 else
708 *y = 0;
709
710 window->resize_edges = 0;
711}
712
713static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500714window_attach_surface(struct window *window)
715{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400716 struct display *display = window->display;
717 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400718 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000719#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500722 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723
724 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000725#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
727 data = cairo_surface_get_user_data(window->cairo_surface,
728 &surface_data_key);
729
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730 cairo_gl_surface_swapbuffers(window->cairo_surface);
731 wl_egl_window_get_attached_size(data->window,
732 &window->server_allocation.width,
733 &window->server_allocation.height);
734 break;
735 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000736#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200738 window_get_resize_dx_dy(window, &x, &y);
739
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 if (window->pending_surface != NULL)
741 return;
742
743 window->pending_surface = window->cairo_surface;
744 window->cairo_surface = NULL;
745
746 buffer =
747 display_get_buffer_for_surface(display,
748 window->pending_surface);
749
750 wl_surface_attach(window->surface, buffer, x, y);
751 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400752 cb = wl_display_sync(display->display);
753 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100754 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000755 default:
756 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100757 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500758
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500759 if (window->fullscreen)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400760 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500761 else if (!window->parent)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400762 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500763 else
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400764 wl_shell_set_transient(display->shell, window->surface,
765 window->parent->surface,
766 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500767
768 wl_surface_damage(window->surface, 0, 0,
769 window->allocation.width,
770 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500771}
772
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500773void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400774window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500775{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100776 if (window->cairo_surface) {
777 switch (window->buffer_type) {
778 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
779 case WINDOW_BUFFER_TYPE_SHM:
780 display_surface_damage(window->display,
781 window->cairo_surface,
782 0, 0,
783 window->allocation.width,
784 window->allocation.height);
785 break;
786 default:
787 break;
788 }
789 window_attach_surface(window);
790 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500791}
792
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400793void
794window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400795{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500796 cairo_surface_reference(surface);
797
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400798 if (window->cairo_surface != NULL)
799 cairo_surface_destroy(window->cairo_surface);
800
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500801 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400802}
803
Benjamin Franzke22d54812011-07-16 19:50:32 +0000804#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805static void
806window_resize_cairo_window_surface(struct window *window)
807{
808 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200809 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100810
811 data = cairo_surface_get_user_data(window->cairo_surface,
812 &surface_data_key);
813
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200814 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 wl_egl_window_resize(data->window,
816 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200817 window->allocation.height,
818 x,y);
819
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 cairo_gl_surface_set_size(window->cairo_surface,
821 window->allocation.width,
822 window->allocation.height);
823}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400826void
827window_create_surface(struct window *window)
828{
829 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400830 uint32_t flags = 0;
831
832 if (!window->transparent)
833 flags = SURFACE_OPAQUE;
834
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500836#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
838 if (window->cairo_surface) {
839 window_resize_cairo_window_surface(window);
840 return;
841 }
842 surface = display_create_surface(window->display,
843 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400844 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500846 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400847 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400849 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 break;
851#endif
852 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400853 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400854 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400855 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800856 default:
857 surface = NULL;
858 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400860
861 window_set_surface(window, surface);
862 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400863}
864
865static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500866window_draw_menu(struct window *window)
867{
868 cairo_t *cr;
869 int width, height, r = 5;
870
871 window_create_surface(window);
872
873 cr = cairo_create(window->cairo_surface);
874 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
875 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
876 cairo_paint(cr);
877
878 width = window->allocation.width;
879 height = window->allocation.height;
880 rounded_rect(cr, r, r, width - r, height - r, r);
881 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
882 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
883 cairo_fill(cr);
884 cairo_destroy(cr);
885}
886
887static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500888window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500889{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500890 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500891 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400892 cairo_surface_t *frame;
893 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500894
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400895 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400897 width = window->allocation.width;
898 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500899
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500900 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500901
Kristian Høgsberg09531622010-06-14 23:22:15 -0400902 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400903 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400904 cairo_paint(cr);
905
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400906 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400907 tile_mask(cr, window->display->shadow,
908 shadow_dx, shadow_dy, width, height,
909 window->margin + 10 - shadow_dx,
910 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500911
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400912 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400913 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400914 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400915 frame = window->display->inactive_frame;
916
917 tile_source(cr, frame, 0, 0, width, height,
918 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500919
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500920 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
921 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500922 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400923 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400924 if (window->keyboard_device)
925 cairo_set_source_rgb(cr, 0, 0, 0);
926 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400927 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400928 cairo_show_text(cr, window->title);
929
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500930 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400931
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100932 /* FIXME: this breakes gears, fix cairo? */
933#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400934 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100935#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500936}
937
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400938void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500939window_destroy(struct window *window)
940{
941 wl_surface_destroy(window->surface);
942 wl_list_remove(&window->link);
943 free(window);
944}
945
946void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400947display_flush_cairo_device(struct display *display)
948{
949 cairo_device_flush (display->device);
950}
951
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500952void
953window_draw(struct window *window)
954{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500955 if (window->parent)
956 window_draw_menu(window);
957 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400958 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500959 else
960 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500961}
962
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400963cairo_surface_t *
964window_get_surface(struct window *window)
965{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400966 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400967}
968
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100969struct wl_surface *
970window_get_wl_surface(struct window *window)
971{
972 return window->surface;
973}
974
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400975static int
976get_pointer_location(struct window *window, int32_t x, int32_t y)
977{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400978 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400979 const int grip_size = 8;
980
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500981 if (!window->decoration)
982 return WINDOW_CLIENT_AREA;
983
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400984 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400985 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400986 else if (window->margin <= x && x < window->margin + grip_size)
987 hlocation = WINDOW_RESIZING_LEFT;
988 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400989 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400990 else if (x < window->allocation.width - window->margin)
991 hlocation = WINDOW_RESIZING_RIGHT;
992 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400993 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400994
995 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400996 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400997 else if (window->margin <= y && y < window->margin + grip_size)
998 vlocation = WINDOW_RESIZING_TOP;
999 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001000 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001001 else if (y < window->allocation.height - window->margin)
1002 vlocation = WINDOW_RESIZING_BOTTOM;
1003 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001004 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001005
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001006 location = vlocation | hlocation;
1007 if (location & WINDOW_EXTERIOR)
1008 location = WINDOW_EXTERIOR;
1009 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1010 location = WINDOW_TITLEBAR;
1011 else if (location == WINDOW_INTERIOR)
1012 location = WINDOW_CLIENT_AREA;
1013
1014 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001015}
1016
1017static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001018set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001019{
1020 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001021 struct wl_buffer *buffer;
1022 cairo_surface_t *surface;
1023 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001024
1025 location = get_pointer_location(input->pointer_focus,
1026 input->sx, input->sy);
1027 switch (location) {
1028 case WINDOW_RESIZING_TOP:
1029 pointer = POINTER_TOP;
1030 break;
1031 case WINDOW_RESIZING_BOTTOM:
1032 pointer = POINTER_BOTTOM;
1033 break;
1034 case WINDOW_RESIZING_LEFT:
1035 pointer = POINTER_LEFT;
1036 break;
1037 case WINDOW_RESIZING_RIGHT:
1038 pointer = POINTER_RIGHT;
1039 break;
1040 case WINDOW_RESIZING_TOP_LEFT:
1041 pointer = POINTER_TOP_LEFT;
1042 break;
1043 case WINDOW_RESIZING_TOP_RIGHT:
1044 pointer = POINTER_TOP_RIGHT;
1045 break;
1046 case WINDOW_RESIZING_BOTTOM_LEFT:
1047 pointer = POINTER_BOTTOM_LEFT;
1048 break;
1049 case WINDOW_RESIZING_BOTTOM_RIGHT:
1050 pointer = POINTER_BOTTOM_RIGHT;
1051 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001052 case WINDOW_EXTERIOR:
1053 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001054 if (input->current_pointer_image == POINTER_DEFAULT)
1055 return;
1056
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001057 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001058 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001059 return;
1060 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001061 break;
1062 }
1063
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001064 if (pointer == input->current_pointer_image)
1065 return;
1066
1067 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001068 surface = display->pointer_surfaces[pointer];
1069 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001070 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001071 pointer_images[pointer].hotspot_x,
1072 pointer_images[pointer].hotspot_y);
1073}
1074
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001075static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001076window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001077 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001078 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001079{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001080 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001081 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001082 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001083
1084 input->x = x;
1085 input->y = y;
1086 input->sx = sx;
1087 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001088
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001089 if (window->motion_handler)
1090 pointer = (*window->motion_handler)(window, input, time,
1091 x, y, sx, sy,
1092 window->user_data);
1093
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001094 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001095}
1096
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001097static void
1098window_handle_button(void *data,
1099 struct wl_input_device *input_device,
1100 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001101{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001102 struct input *input = data;
1103 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001104 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001105
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001106 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001107
1108 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001109 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001110 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001111 wl_shell_move(window->display->shell,
1112 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001113 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001114 case WINDOW_RESIZING_TOP:
1115 case WINDOW_RESIZING_BOTTOM:
1116 case WINDOW_RESIZING_LEFT:
1117 case WINDOW_RESIZING_RIGHT:
1118 case WINDOW_RESIZING_TOP_LEFT:
1119 case WINDOW_RESIZING_TOP_RIGHT:
1120 case WINDOW_RESIZING_BOTTOM_LEFT:
1121 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001122 wl_shell_resize(window->display->shell,
1123 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001125 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001126 case WINDOW_CLIENT_AREA:
1127 if (window->button_handler)
1128 (*window->button_handler)(window,
1129 input, time,
1130 button, state,
1131 window->user_data);
1132 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001133 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001134 } else {
1135 if (window->button_handler)
1136 (*window->button_handler)(window,
1137 input, time,
1138 button, state,
1139 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001140 }
1141}
1142
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001143static void
1144window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001145 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001146{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001147 struct input *input = data;
1148 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001149 struct display *d = window->display;
1150 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001151
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001152 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001153 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001154 return;
1155
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001156 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001157 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001158 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1159 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001160
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001161 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1162
1163 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001164 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001165 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001166 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001167
1168 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001169 (*window->key_handler)(window, input, time, key, sym, state,
1170 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001171}
1172
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001173static void
1174window_handle_pointer_focus(void *data,
1175 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001176 uint32_t time, struct wl_surface *surface,
1177 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001178{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001179 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001180 struct window *window;
1181 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001182
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001183 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001184 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001185 window = input->pointer_focus;
1186
Kristian Høgsberg59826582011-01-20 11:56:57 -05001187 input->x = x;
1188 input->y = y;
1189 input->sx = sx;
1190 input->sy = sy;
1191
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001192 pointer = POINTER_LEFT_PTR;
1193 if (window->motion_handler)
1194 pointer = (*window->motion_handler)(window,
1195 input, time,
1196 x, y, sx, sy,
1197 window->user_data);
1198
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001199 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001200 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001201 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001202 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001204}
1205
1206static void
1207window_handle_keyboard_focus(void *data,
1208 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001209 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001210 struct wl_surface *surface,
1211 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001212{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001213 struct input *input = data;
1214 struct window *window = input->keyboard_focus;
1215 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001216 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001217
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001218 window = input->keyboard_focus;
1219 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001220 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001221 if (window->keyboard_focus_handler)
1222 (*window->keyboard_focus_handler)(window, NULL,
1223 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001224 }
1225
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001226 if (surface)
1227 input->keyboard_focus = wl_surface_get_user_data(surface);
1228 else
1229 input->keyboard_focus = NULL;
1230
1231 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001232 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001233 for (k = keys->data; k < end; k++)
1234 input->modifiers |= d->xkb->map->modmap[*k];
1235
1236 window = input->keyboard_focus;
1237 if (window) {
1238 window->keyboard_device = input;
1239 if (window->keyboard_focus_handler)
1240 (*window->keyboard_focus_handler)(window,
1241 window->keyboard_device,
1242 window->user_data);
1243 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001244}
1245
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001246static const struct wl_input_device_listener input_device_listener = {
1247 window_handle_motion,
1248 window_handle_button,
1249 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001250 window_handle_pointer_focus,
1251 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001252};
1253
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254void
1255input_get_position(struct input *input, int32_t *x, int32_t *y)
1256{
1257 *x = input->sx;
1258 *y = input->sy;
1259}
1260
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001261struct wl_input_device *
1262input_get_input_device(struct input *input)
1263{
1264 return input->input_device;
1265}
1266
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001267uint32_t
1268input_get_modifiers(struct input *input)
1269{
1270 return input->modifiers;
1271}
1272
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001273struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001274window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001275{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001276 cairo_device_flush (window->display->device);
1277
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001278 return wl_shell_create_drag(window->display->shell);
1279}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001280
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001281void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001282window_move(struct window *window, struct input *input, uint32_t time)
1283{
1284 wl_shell_move(window->display->shell,
1285 window->surface, input->input_device, time);
1286}
1287
1288void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001289window_activate_drag(struct wl_drag *drag, struct window *window,
1290 struct input *input, uint32_t time)
1291{
1292 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001293}
1294
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001295static void
1296handle_configure(void *data, struct wl_shell *shell,
1297 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001298 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001299{
1300 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001301 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001302
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001303 /* FIXME: this is probably the wrong place to check for width
1304 * or height <= 0, but it prevents the compositor from crashing
1305 */
1306 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001307 return;
1308
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001309 window->resize_edges = edges;
1310
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001311 if (window->resize_handler) {
1312 child_width = width - 20 - window->margin * 2;
1313 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001314
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001315 (*window->resize_handler)(window,
1316 child_width, child_height,
1317 window->user_data);
1318 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001319 window->allocation.width = width;
1320 window->allocation.height = height;
1321
1322 if (window->redraw_handler)
1323 window_schedule_redraw(window);
1324 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001325}
1326
1327static const struct wl_shell_listener shell_listener = {
1328 handle_configure,
1329};
1330
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001331void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001332window_get_allocation(struct window *window,
1333 struct rectangle *allocation)
1334{
1335 *allocation = window->allocation;
1336}
1337
1338void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001339window_get_child_allocation(struct window *window,
1340 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001341{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001342 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001343 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001344 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001345 allocation->x = window->margin + 10;
1346 allocation->y = window->margin + 50;
1347 allocation->width =
1348 window->allocation.width - 20 - window->margin * 2;
1349 allocation->height =
1350 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001351 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001352}
1353
1354void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001355window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001356{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001357 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001358 window->allocation.x = 20 + window->margin;
1359 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001360 window->allocation.width = width + 20 + window->margin * 2;
1361 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001362 } else {
1363 window->allocation.x = 0;
1364 window->allocation.y = 0;
1365 window->allocation.width = width;
1366 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001367 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001368}
1369
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001370static gboolean
1371idle_redraw(void *data)
1372{
1373 struct window *window = data;
1374
1375 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001376
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001377 window->redraw_scheduled = 0;
1378
1379 return FALSE;
1380}
1381
1382void
1383window_schedule_redraw(struct window *window)
1384{
1385 if (!window->redraw_scheduled) {
1386 g_idle_add(idle_redraw, window);
1387 window->redraw_scheduled = 1;
1388 }
1389}
1390
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001391void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001392window_set_fullscreen(struct window *window, int fullscreen)
1393{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001394 int32_t width, height;
1395
1396 if (window->fullscreen == fullscreen)
1397 return;
1398
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001399 window->fullscreen = fullscreen;
1400 if (window->fullscreen) {
1401 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001402 width = window->display->screen_allocation.width;
1403 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001404 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001405 width = window->saved_allocation.width - 20 - window->margin * 2;
1406 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001407 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001408
1409 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001410}
1411
1412void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001413window_set_decoration(struct window *window, int decoration)
1414{
1415 window->decoration = decoration;
1416}
1417
1418void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001419window_set_user_data(struct window *window, void *data)
1420{
1421 window->user_data = data;
1422}
1423
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001424void *
1425window_get_user_data(struct window *window)
1426{
1427 return window->user_data;
1428}
1429
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001430void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001431window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001432 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001433{
1434 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001435}
1436
1437void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001438window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001439 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001440{
1441 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001442}
1443
1444void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001445window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001446 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001447{
1448 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001449}
1450
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001451void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001452window_set_button_handler(struct window *window,
1453 window_button_handler_t handler)
1454{
1455 window->button_handler = handler;
1456}
1457
1458void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001459window_set_motion_handler(struct window *window,
1460 window_motion_handler_t handler)
1461{
1462 window->motion_handler = handler;
1463}
1464
1465void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001466window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001467 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001468{
1469 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001470}
1471
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001472void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001473window_set_transparent(struct window *window, int transparent)
1474{
1475 window->transparent = transparent;
1476}
1477
1478void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001479window_set_title(struct window *window, const char *title)
1480{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001481 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001482 window->title = strdup(title);
1483}
1484
1485const char *
1486window_get_title(struct window *window)
1487{
1488 return window->title;
1489}
1490
1491void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001492display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1493 int32_t x, int32_t y, int32_t width, int32_t height)
1494{
1495 struct wl_buffer *buffer;
1496
1497 buffer = display_get_buffer_for_surface(display, cairo_surface);
1498
1499 wl_buffer_damage(buffer, x, y, width, height);
1500}
1501
1502void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001503window_damage(struct window *window, int32_t x, int32_t y,
1504 int32_t width, int32_t height)
1505{
1506 wl_surface_damage(window->surface, x, y, width, height);
1507}
1508
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001509static struct window *
1510window_create_internal(struct display *display, struct window *parent,
1511 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001512{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001513 struct window *window;
1514
1515 window = malloc(sizeof *window);
1516 if (window == NULL)
1517 return NULL;
1518
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001519 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001520 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001521 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001522 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001523 window->allocation.x = 0;
1524 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001525 window->allocation.width = width;
1526 window->allocation.height = height;
1527 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001528 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001529 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001530 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001531
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001532 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001533#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001534 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1535 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001536#else
1537 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1538#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001539 else
1540 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001541
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001542 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001543 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001544
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001545 return window;
1546}
1547
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001548struct window *
1549window_create(struct display *display, int32_t width, int32_t height)
1550{
1551 struct window *window;
1552
1553 window = window_create_internal(display, NULL, width, height);
1554 if (!window)
1555 return NULL;
1556
1557 return window;
1558}
1559
1560struct window *
1561window_create_transient(struct display *display, struct window *parent,
1562 int32_t x, int32_t y, int32_t width, int32_t height)
1563{
1564 struct window *window;
1565
1566 window = window_create_internal(parent->display,
1567 parent, width, height);
1568 if (!window)
1569 return NULL;
1570
1571 window->x = x;
1572 window->y = y;
1573
1574 return window;
1575}
1576
1577void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001578window_set_buffer_type(struct window *window, enum window_buffer_type type)
1579{
1580 window->buffer_type = type;
1581}
1582
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001583
1584static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001585display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001586 struct wl_output *wl_output,
1587 int x, int y,
1588 int physical_width,
1589 int physical_height,
1590 int subpixel,
1591 const char *make,
1592 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001593{
1594 struct display *display = data;
1595
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001596 display->screen_allocation.x = x;
1597 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001598}
1599
1600static void
1601display_handle_mode(void *data,
1602 struct wl_output *wl_output,
1603 uint32_t flags,
1604 int width,
1605 int height,
1606 int refresh)
1607{
1608 struct display *display = data;
1609
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001610 display->screen_allocation.width = width;
1611 display->screen_allocation.height = height;
1612}
1613
1614static const struct wl_output_listener output_listener = {
1615 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001616 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001617};
1618
1619static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001620display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001621{
1622 struct input *input;
1623
1624 input = malloc(sizeof *input);
1625 if (input == NULL)
1626 return;
1627
1628 memset(input, 0, sizeof *input);
1629 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001630 input->input_device =
1631 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001632 input->pointer_focus = NULL;
1633 input->keyboard_focus = NULL;
1634 wl_list_insert(d->input_list.prev, &input->link);
1635
1636 wl_input_device_add_listener(input->input_device,
1637 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001638 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001639}
1640
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001641struct selection_offer {
1642 struct display *display;
1643 struct wl_selection_offer *offer;
1644 struct wl_array types;
1645 struct input *input;
1646};
1647
1648int
1649input_offers_mime_type(struct input *input, const char *type)
1650{
1651 struct selection_offer *offer = input->offer;
1652 char **p, **end;
1653
1654 if (offer == NULL)
1655 return 0;
1656
1657 end = offer->types.data + offer->types.size;
1658 for (p = offer->types.data; p < end; p++)
1659 if (strcmp(*p, type) == 0)
1660 return 1;
1661
1662 return 0;
1663}
1664
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001665void
1666input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001667{
1668 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001669
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001670 /* FIXME: A number of things can go wrong here: the object may
1671 * not be the current selection offer any more (which could
1672 * still work, but the source may have gone away or just
1673 * destroyed its wl_selection) or the offer may not have the
1674 * requested type after all (programmer/client error,
1675 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001676 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001677}
1678
1679static void
1680selection_offer_offer(void *data,
1681 struct wl_selection_offer *selection_offer,
1682 const char *type)
1683{
1684 struct selection_offer *offer = data;
1685
1686 char **p;
1687
1688 p = wl_array_add(&offer->types, sizeof *p);
1689 if (p)
1690 *p = strdup(type);
1691};
1692
1693static void
1694selection_offer_keyboard_focus(void *data,
1695 struct wl_selection_offer *selection_offer,
1696 struct wl_input_device *input_device)
1697{
1698 struct selection_offer *offer = data;
1699 struct input *input;
1700 char **p, **end;
1701
1702 if (input_device == NULL) {
1703 printf("selection offer retracted %p\n", selection_offer);
1704 input = offer->input;
1705 input->offer = NULL;
1706 wl_selection_offer_destroy(selection_offer);
1707 wl_array_release(&offer->types);
1708 free(offer);
1709 return;
1710 }
1711
1712 input = wl_input_device_get_user_data(input_device);
1713 printf("new selection offer %p:", selection_offer);
1714
1715 offer->input = input;
1716 input->offer = offer;
1717 end = offer->types.data + offer->types.size;
1718 for (p = offer->types.data; p < end; p++)
1719 printf(" %s", *p);
1720
1721 printf("\n");
1722}
1723
1724struct wl_selection_offer_listener selection_offer_listener = {
1725 selection_offer_offer,
1726 selection_offer_keyboard_focus
1727};
1728
1729static void
1730add_selection_offer(struct display *d, uint32_t id)
1731{
1732 struct selection_offer *offer;
1733
1734 offer = malloc(sizeof *offer);
1735 if (offer == NULL)
1736 return;
1737
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001738 offer->offer =
1739 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001740 offer->display = d;
1741 wl_array_init(&offer->types);
1742 offer->input = NULL;
1743
1744 wl_selection_offer_add_listener(offer->offer,
1745 &selection_offer_listener, offer);
1746}
1747
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001748static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001749display_handle_global(struct wl_display *display, uint32_t id,
1750 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001751{
1752 struct display *d = data;
1753
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001754 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001755 d->compositor =
1756 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001757 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001758 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001759 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001760 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001761 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001762 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001763 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001764 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001765 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001766 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001767 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001768 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001769 }
1770}
1771
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001772static void
1773display_render_frame(struct display *d)
1774{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001775 int radius = 8;
1776 cairo_t *cr;
1777
1778 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1779 cr = cairo_create(d->shadow);
1780 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1781 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1782 rounded_rect(cr, 16, 16, 112, 112, radius);
1783 cairo_fill(cr);
1784 cairo_destroy(cr);
1785 blur_surface(d->shadow, 64);
1786
1787 d->active_frame =
1788 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1789 cr = cairo_create(d->active_frame);
1790 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1791 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1792 rounded_rect(cr, 16, 16, 112, 112, radius);
1793 cairo_fill(cr);
1794 cairo_destroy(cr);
1795
1796 d->inactive_frame =
1797 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1798 cr = cairo_create(d->inactive_frame);
1799 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1800 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1801 rounded_rect(cr, 16, 16, 112, 112, radius);
1802 cairo_fill(cr);
1803 cairo_destroy(cr);
1804}
1805
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001806static void
1807init_xkb(struct display *d)
1808{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001809 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001810
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001811 names.rules = "evdev";
1812 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001813 names.layout = option_xkb_layout;
1814 names.variant = option_xkb_variant;
1815 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001816
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001817 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001818 if (!d->xkb) {
1819 fprintf(stderr, "Failed to compile keymap\n");
1820 exit(1);
1821 }
1822}
1823
Yuval Fledel45568f62010-12-06 09:18:12 -05001824static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001825init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001826{
1827 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001828 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001829
1830 static const EGLint premul_argb_cfg_attribs[] = {
1831 EGL_SURFACE_TYPE,
1832 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1833 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001834 EGL_RED_SIZE, 1,
1835 EGL_GREEN_SIZE, 1,
1836 EGL_BLUE_SIZE, 1,
1837 EGL_ALPHA_SIZE, 1,
1838 EGL_DEPTH_SIZE, 1,
1839 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1840 EGL_NONE
1841 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001842
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001843 static const EGLint rgb_cfg_attribs[] = {
1844 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1845 EGL_RED_SIZE, 1,
1846 EGL_GREEN_SIZE, 1,
1847 EGL_BLUE_SIZE, 1,
1848 EGL_ALPHA_SIZE, 0,
1849 EGL_DEPTH_SIZE, 1,
1850 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1851 EGL_NONE
1852 };
1853
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001854 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001855 if (!eglInitialize(d->dpy, &major, &minor)) {
1856 fprintf(stderr, "failed to initialize display\n");
1857 return -1;
1858 }
1859
1860 if (!eglBindAPI(EGL_OPENGL_API)) {
1861 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1862 return -1;
1863 }
1864
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001865 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02001866 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001867 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001868 return -1;
1869 }
1870
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001871 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
1872 &d->rgb_config, 1, &n) || n != 1) {
1873 fprintf(stderr, "failed to choose rgb config\n");
1874 return -1;
1875 }
1876
1877 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001878 if (d->ctx == NULL) {
1879 fprintf(stderr, "failed to create context\n");
1880 return -1;
1881 }
1882
1883 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001884 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001885 return -1;
1886 }
1887
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001888#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001889 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001890 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001891 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001892 return -1;
1893 }
1894#endif
1895
1896 return 0;
1897}
1898
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001899struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04001900display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001901{
1902 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001903 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001904 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001905 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001906
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001907 g_type_init();
1908
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001909 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001910 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001911 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001912
1913 xkb_option_group = g_option_group_new("xkb",
1914 "Keyboard options",
1915 "Show all XKB options",
1916 NULL, NULL);
1917 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1918 g_option_context_add_group (context, xkb_option_group);
1919
1920 if (!g_option_context_parse(context, argc, argv, &error)) {
1921 fprintf(stderr, "option parsing failed: %s\n", error->message);
1922 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001923 }
1924
Tim Wiederhake748f6722011-01-23 23:25:25 +01001925 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001926
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001927 d = malloc(sizeof *d);
1928 if (d == NULL)
1929 return NULL;
1930
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001931 memset(d, 0, sizeof *d);
1932
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001933 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001934 if (d->display == NULL) {
1935 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001936 return NULL;
1937 }
1938
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001939 wl_list_init(&d->input_list);
1940
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001941 /* Set up listener so we'll catch all events. */
1942 wl_display_add_global_listener(d->display,
1943 display_handle_global, d);
1944
1945 /* Process connection events. */
1946 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001947 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001948 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001949
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001950 d->image_target_texture_2d =
1951 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1952 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1953 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1954
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001955 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001956
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001957 display_render_frame(d);
1958
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001959 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001960 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001961 g_source_attach(d->source, NULL);
1962
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001963 wl_list_init(&d->window_list);
1964
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001965 init_xkb(d);
1966
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001967 return d;
1968}
1969
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001970struct wl_display *
1971display_get_display(struct display *display)
1972{
1973 return display->display;
1974}
1975
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001976struct wl_compositor *
1977display_get_compositor(struct display *display)
1978{
1979 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001980}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001981
1982EGLDisplay
1983display_get_egl_display(struct display *d)
1984{
1985 return d->dpy;
1986}
1987
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001988EGLConfig
1989display_get_egl_config(struct display *d)
1990{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001991 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001992}
1993
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001994struct wl_shell *
1995display_get_shell(struct display *display)
1996{
1997 return display->shell;
1998}
1999
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002000void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002001display_acquire_window_surface(struct display *display,
2002 struct window *window,
2003 EGLContext ctx)
2004{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002005#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002006 struct egl_window_surface_data *data;
2007
2008 if (!window->cairo_surface)
2009 return;
2010
2011 if (!ctx)
2012 ctx = display->ctx;
2013
2014 data = cairo_surface_get_user_data(window->cairo_surface,
2015 &surface_data_key);
2016
2017 cairo_device_acquire(display->device);
2018 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2019 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002020#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002021}
2022
2023void
2024display_release(struct display *display)
2025{
2026 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2027 fprintf(stderr, "failed to make context current\n");
2028 cairo_device_release(display->device);
2029}
2030
2031void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002032display_run(struct display *d)
2033{
2034 g_main_loop_run(d->loop);
2035}