blob: bc6579084e2b3044ecb5fa93cab2eecd081e5341 [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;
69 EGLConfig premul_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;
236 attribps = NULL;
237 } 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
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400396 visual = display->premultiplied_argb_visual;
397 surface = display_create_egl_image_surface(display, visual, rect);
nobled7b87cb02011-02-01 18:51:47 +0000398 if (surface == NULL) {
399 g_object_unref(pixbuf);
400 return NULL;
401 }
402
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800403 data = cairo_surface_get_user_data(surface, &surface_data_key);
404
405 cairo_device_acquire(display->device);
406 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800407 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
408 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800409 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400410
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500411 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400412
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413 return surface;
414}
415
416#endif
417
418struct wl_buffer *
419display_get_buffer_for_surface(struct display *display,
420 cairo_surface_t *surface)
421{
422 struct surface_data *data;
423
424 data = cairo_surface_get_user_data (surface, &surface_data_key);
425
426 return data->buffer;
427}
428
429struct shm_surface_data {
430 struct surface_data data;
431 void *map;
432 size_t length;
433};
434
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500435static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436shm_surface_data_destroy(void *p)
437{
438 struct shm_surface_data *data = p;
439
440 wl_buffer_destroy(data->data.buffer);
441 munmap(data->map, data->length);
442}
443
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500444static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400446 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400447{
448 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400449 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400450 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800451 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400452 char filename[] = "/tmp/wayland-shm-XXXXXX";
453
454 data = malloc(sizeof *data);
455 if (data == NULL)
456 return NULL;
457
458 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
459 rectangle->width);
460 data->length = stride * rectangle->height;
461 fd = mkstemp(filename);
462 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000463 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464 return NULL;
465 }
466 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000467 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468 close(fd);
469 return NULL;
470 }
471
472 data->map = mmap(NULL, data->length,
473 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
474 unlink(filename);
475
476 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000477 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400478 close(fd);
479 return NULL;
480 }
481
482 surface = cairo_image_surface_create_for_data (data->map,
483 CAIRO_FORMAT_ARGB32,
484 rectangle->width,
485 rectangle->height,
486 stride);
487
488 cairo_surface_set_user_data (surface, &surface_data_key,
489 data, shm_surface_data_destroy);
490
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400491 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400492 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400493 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400494 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400495
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496 data->data.buffer = wl_shm_create_buffer(display->shm,
497 fd,
498 rectangle->width,
499 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400500 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501
502 close(fd);
503
504 return surface;
505}
506
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500507static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508display_create_shm_surface_from_file(struct display *display,
509 const char *filename,
510 struct rectangle *rect)
511{
512 cairo_surface_t *surface;
513 GdkPixbuf *pixbuf;
514 GError *error = NULL;
515 int stride, i;
516 unsigned char *pixels, *p, *end, *dest_data;
517 int dest_stride;
518 uint32_t *d;
519
520 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
521 rect->width, rect->height,
522 FALSE, &error);
523 if (error != NULL)
524 return NULL;
525
526 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
527 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500528 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529 return NULL;
530 }
531
532 stride = gdk_pixbuf_get_rowstride(pixbuf);
533 pixels = gdk_pixbuf_get_pixels(pixbuf);
534
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400535 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000536 if (surface == NULL) {
537 g_object_unref(pixbuf);
538 return NULL;
539 }
540
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541 dest_data = cairo_image_surface_get_data (surface);
542 dest_stride = cairo_image_surface_get_stride (surface);
543
544 for (i = 0; i < rect->height; i++) {
545 d = (uint32_t *) (dest_data + i * dest_stride);
546 p = pixels + i * stride;
547 end = p + rect->width * 4;
548 while (p < end) {
549 unsigned int t;
550 unsigned char a, r, g, b;
551
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400552 a = p[3];
553 MULT(r, p[0], a, t);
554 MULT(g, p[1], a, t);
555 MULT(b, p[2], a, t);
556 p += 4;
557 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
558 }
559 }
560
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500561 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562
563 return surface;
564}
565
nobled7b87cb02011-02-01 18:51:47 +0000566static int
567check_size(struct rectangle *rect)
568{
569 if (rect->width && rect->height)
570 return 0;
571
572 fprintf(stderr, "tried to create surface of "
573 "width: %d, height: %d\n", rect->width, rect->height);
574 return -1;
575}
576
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400577cairo_surface_t *
578display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100579 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400580 struct rectangle *rectangle,
581 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582{
nobled7b87cb02011-02-01 18:51:47 +0000583 if (check_size(rectangle) < 0)
584 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500585#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500586 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100587 if (surface)
588 return display_create_egl_window_surface(display,
589 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400590 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100591 rectangle);
592 else
593 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400594 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100595 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500596 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400598 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400599}
600
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500601static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602display_create_surface_from_file(struct display *display,
603 const char *filename,
604 struct rectangle *rectangle)
605{
nobled7b87cb02011-02-01 18:51:47 +0000606 if (check_size(rectangle) < 0)
607 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500608#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500609 if (display->dpy) {
610 return display_create_egl_image_surface_from_file(display,
611 filename,
612 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500613 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500615 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400616}
Yuval Fledel45568f62010-12-06 09:18:12 -0500617 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618 const char *filename;
619 int hotspot_x, hotspot_y;
620} pointer_images[] = {
621 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
622 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
623 { DATADIR "/wayland/bottom_side.png", 16, 20 },
624 { DATADIR "/wayland/grabbing.png", 20, 17 },
625 { DATADIR "/wayland/left_ptr.png", 10, 5 },
626 { DATADIR "/wayland/left_side.png", 10, 20 },
627 { DATADIR "/wayland/right_side.png", 30, 19 },
628 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
629 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
630 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400631 { DATADIR "/wayland/xterm.png", 15, 15 },
632 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400633};
634
635static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400636create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400637{
638 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400639 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400640 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400641
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400642 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400643 display->pointer_surfaces =
644 malloc(count * sizeof *display->pointer_surfaces);
645 rect.width = width;
646 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400647 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400648 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649 display_create_surface_from_file(display,
650 pointer_images[i].filename,
651 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400652 }
653
654}
655
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400656cairo_surface_t *
657display_get_pointer_surface(struct display *display, int pointer,
658 int *width, int *height,
659 int *hotspot_x, int *hotspot_y)
660{
661 cairo_surface_t *surface;
662
663 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500664#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400665 *width = cairo_gl_surface_get_width(surface);
666 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000667#else
668 *width = cairo_image_surface_get_width(surface);
669 *height = cairo_image_surface_get_height(surface);
670#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400671 *hotspot_x = pointer_images[pointer].hotspot_x;
672 *hotspot_y = pointer_images[pointer].hotspot_y;
673
674 return cairo_surface_reference(surface);
675}
676
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400677
678static void
679window_attach_surface(struct window *window);
680
681static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400682free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400683{
684 struct window *window = data;
685
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400686 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400687 cairo_surface_destroy(window->pending_surface);
688 window->pending_surface = NULL;
689 if (window->cairo_surface)
690 window_attach_surface(window);
691}
692
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400693static const struct wl_callback_listener free_surface_listener = {
694 free_surface
695};
696
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500697static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200698window_get_resize_dx_dy(struct window *window, int *x, int *y)
699{
700 if (window->resize_edges & WINDOW_RESIZING_LEFT)
701 *x = window->server_allocation.width - window->allocation.width;
702 else
703 *x = 0;
704
705 if (window->resize_edges & WINDOW_RESIZING_TOP)
706 *y = window->server_allocation.height -
707 window->allocation.height;
708 else
709 *y = 0;
710
711 window->resize_edges = 0;
712}
713
714static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500715window_attach_surface(struct window *window)
716{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400717 struct display *display = window->display;
718 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400719 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000720#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000722#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500723 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724
725 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000726#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
728 data = cairo_surface_get_user_data(window->cairo_surface,
729 &surface_data_key);
730
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 cairo_gl_surface_swapbuffers(window->cairo_surface);
732 wl_egl_window_get_attached_size(data->window,
733 &window->server_allocation.width,
734 &window->server_allocation.height);
735 break;
736 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000737#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200739 window_get_resize_dx_dy(window, &x, &y);
740
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100741 if (window->pending_surface != NULL)
742 return;
743
744 window->pending_surface = window->cairo_surface;
745 window->cairo_surface = NULL;
746
747 buffer =
748 display_get_buffer_for_surface(display,
749 window->pending_surface);
750
751 wl_surface_attach(window->surface, buffer, x, y);
752 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400753 cb = wl_display_sync(display->display);
754 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000756 default:
757 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100758 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500759
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500760 if (window->fullscreen)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400761 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500762 else if (!window->parent)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400763 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500764 else
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400765 wl_shell_set_transient(display->shell, window->surface,
766 window->parent->surface,
767 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500768
769 wl_surface_damage(window->surface, 0, 0,
770 window->allocation.width,
771 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500772}
773
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500774void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400775window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500776{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100777 if (window->cairo_surface) {
778 switch (window->buffer_type) {
779 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
780 case WINDOW_BUFFER_TYPE_SHM:
781 display_surface_damage(window->display,
782 window->cairo_surface,
783 0, 0,
784 window->allocation.width,
785 window->allocation.height);
786 break;
787 default:
788 break;
789 }
790 window_attach_surface(window);
791 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500792}
793
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400794void
795window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500797 cairo_surface_reference(surface);
798
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400799 if (window->cairo_surface != NULL)
800 cairo_surface_destroy(window->cairo_surface);
801
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500802 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400803}
804
Benjamin Franzke22d54812011-07-16 19:50:32 +0000805#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100806static void
807window_resize_cairo_window_surface(struct window *window)
808{
809 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200810 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811
812 data = cairo_surface_get_user_data(window->cairo_surface,
813 &surface_data_key);
814
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200815 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 wl_egl_window_resize(data->window,
817 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200818 window->allocation.height,
819 x,y);
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 cairo_gl_surface_set_size(window->cairo_surface,
822 window->allocation.width,
823 window->allocation.height);
824}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400827void
828window_create_surface(struct window *window)
829{
830 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400831 uint32_t flags = 0;
832
833 if (!window->transparent)
834 flags = SURFACE_OPAQUE;
835
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400836 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500837#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
839 if (window->cairo_surface) {
840 window_resize_cairo_window_surface(window);
841 return;
842 }
843 surface = display_create_surface(window->display,
844 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500847 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400848 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400850 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400851 break;
852#endif
853 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400854 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400855 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800857 default:
858 surface = NULL;
859 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400860 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400861
862 window_set_surface(window, surface);
863 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400864}
865
866static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500867window_draw_menu(struct window *window)
868{
869 cairo_t *cr;
870 int width, height, r = 5;
871
872 window_create_surface(window);
873
874 cr = cairo_create(window->cairo_surface);
875 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
876 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
877 cairo_paint(cr);
878
879 width = window->allocation.width;
880 height = window->allocation.height;
881 rounded_rect(cr, r, r, width - r, height - r, r);
882 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
883 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
884 cairo_fill(cr);
885 cairo_destroy(cr);
886}
887
888static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500889window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500890{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500891 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500892 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400893 cairo_surface_t *frame;
894 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500895
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400897
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400898 width = window->allocation.width;
899 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500900
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500901 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500902
Kristian Høgsberg09531622010-06-14 23:22:15 -0400903 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400904 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400905 cairo_paint(cr);
906
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400907 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400908 tile_mask(cr, window->display->shadow,
909 shadow_dx, shadow_dy, width, height,
910 window->margin + 10 - shadow_dx,
911 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500912
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400913 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400914 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400915 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400916 frame = window->display->inactive_frame;
917
918 tile_source(cr, frame, 0, 0, width, height,
919 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500920
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500921 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
922 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500923 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400924 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400925 if (window->keyboard_device)
926 cairo_set_source_rgb(cr, 0, 0, 0);
927 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400928 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400929 cairo_show_text(cr, window->title);
930
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500931 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400932
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100933 /* FIXME: this breakes gears, fix cairo? */
934#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400935 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100936#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500937}
938
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400939void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500940window_destroy(struct window *window)
941{
942 wl_surface_destroy(window->surface);
943 wl_list_remove(&window->link);
944 free(window);
945}
946
947void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400948display_flush_cairo_device(struct display *display)
949{
950 cairo_device_flush (display->device);
951}
952
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500953void
954window_draw(struct window *window)
955{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500956 if (window->parent)
957 window_draw_menu(window);
958 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400959 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500960 else
961 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500962}
963
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400964cairo_surface_t *
965window_get_surface(struct window *window)
966{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400967 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400968}
969
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100970struct wl_surface *
971window_get_wl_surface(struct window *window)
972{
973 return window->surface;
974}
975
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400976static int
977get_pointer_location(struct window *window, int32_t x, int32_t y)
978{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400979 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400980 const int grip_size = 8;
981
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500982 if (!window->decoration)
983 return WINDOW_CLIENT_AREA;
984
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400985 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400986 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400987 else if (window->margin <= x && x < window->margin + grip_size)
988 hlocation = WINDOW_RESIZING_LEFT;
989 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400990 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400991 else if (x < window->allocation.width - window->margin)
992 hlocation = WINDOW_RESIZING_RIGHT;
993 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400994 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400995
996 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400997 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400998 else if (window->margin <= y && y < window->margin + grip_size)
999 vlocation = WINDOW_RESIZING_TOP;
1000 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001001 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001002 else if (y < window->allocation.height - window->margin)
1003 vlocation = WINDOW_RESIZING_BOTTOM;
1004 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001005 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001006
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001007 location = vlocation | hlocation;
1008 if (location & WINDOW_EXTERIOR)
1009 location = WINDOW_EXTERIOR;
1010 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1011 location = WINDOW_TITLEBAR;
1012 else if (location == WINDOW_INTERIOR)
1013 location = WINDOW_CLIENT_AREA;
1014
1015 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001016}
1017
1018static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001019set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001020{
1021 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001022 struct wl_buffer *buffer;
1023 cairo_surface_t *surface;
1024 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001025
1026 location = get_pointer_location(input->pointer_focus,
1027 input->sx, input->sy);
1028 switch (location) {
1029 case WINDOW_RESIZING_TOP:
1030 pointer = POINTER_TOP;
1031 break;
1032 case WINDOW_RESIZING_BOTTOM:
1033 pointer = POINTER_BOTTOM;
1034 break;
1035 case WINDOW_RESIZING_LEFT:
1036 pointer = POINTER_LEFT;
1037 break;
1038 case WINDOW_RESIZING_RIGHT:
1039 pointer = POINTER_RIGHT;
1040 break;
1041 case WINDOW_RESIZING_TOP_LEFT:
1042 pointer = POINTER_TOP_LEFT;
1043 break;
1044 case WINDOW_RESIZING_TOP_RIGHT:
1045 pointer = POINTER_TOP_RIGHT;
1046 break;
1047 case WINDOW_RESIZING_BOTTOM_LEFT:
1048 pointer = POINTER_BOTTOM_LEFT;
1049 break;
1050 case WINDOW_RESIZING_BOTTOM_RIGHT:
1051 pointer = POINTER_BOTTOM_RIGHT;
1052 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001053 case WINDOW_EXTERIOR:
1054 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001055 if (input->current_pointer_image == POINTER_DEFAULT)
1056 return;
1057
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001058 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001059 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001060 return;
1061 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001062 break;
1063 }
1064
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001065 if (pointer == input->current_pointer_image)
1066 return;
1067
1068 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001069 surface = display->pointer_surfaces[pointer];
1070 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001071 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001072 pointer_images[pointer].hotspot_x,
1073 pointer_images[pointer].hotspot_y);
1074}
1075
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001076static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001077window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001078 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001079 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001080{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001081 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001082 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001083 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001084
1085 input->x = x;
1086 input->y = y;
1087 input->sx = sx;
1088 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001089
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001090 if (window->motion_handler)
1091 pointer = (*window->motion_handler)(window, input, time,
1092 x, y, sx, sy,
1093 window->user_data);
1094
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001095 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001096}
1097
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001098static void
1099window_handle_button(void *data,
1100 struct wl_input_device *input_device,
1101 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001102{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001103 struct input *input = data;
1104 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001105 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001106
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001107 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001108
1109 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001110 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001112 wl_shell_move(window->display->shell,
1113 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001114 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001115 case WINDOW_RESIZING_TOP:
1116 case WINDOW_RESIZING_BOTTOM:
1117 case WINDOW_RESIZING_LEFT:
1118 case WINDOW_RESIZING_RIGHT:
1119 case WINDOW_RESIZING_TOP_LEFT:
1120 case WINDOW_RESIZING_TOP_RIGHT:
1121 case WINDOW_RESIZING_BOTTOM_LEFT:
1122 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001123 wl_shell_resize(window->display->shell,
1124 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001125 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001126 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 case WINDOW_CLIENT_AREA:
1128 if (window->button_handler)
1129 (*window->button_handler)(window,
1130 input, time,
1131 button, state,
1132 window->user_data);
1133 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001134 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135 } else {
1136 if (window->button_handler)
1137 (*window->button_handler)(window,
1138 input, time,
1139 button, state,
1140 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001141 }
1142}
1143
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001144static void
1145window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001146 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001147{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001148 struct input *input = data;
1149 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001150 struct display *d = window->display;
1151 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001152
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001153 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001154 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001155 return;
1156
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001157 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001158 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001159 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1160 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001161
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001162 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1163
1164 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001165 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001166 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001167 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001168
1169 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001170 (*window->key_handler)(window, input, time, key, sym, state,
1171 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001172}
1173
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001174static void
1175window_handle_pointer_focus(void *data,
1176 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001177 uint32_t time, struct wl_surface *surface,
1178 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001179{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001180 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001181 struct window *window;
1182 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001183
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001184 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001185 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001186 window = input->pointer_focus;
1187
Kristian Høgsberg59826582011-01-20 11:56:57 -05001188 input->x = x;
1189 input->y = y;
1190 input->sx = sx;
1191 input->sy = sy;
1192
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001193 pointer = POINTER_LEFT_PTR;
1194 if (window->motion_handler)
1195 pointer = (*window->motion_handler)(window,
1196 input, time,
1197 x, y, sx, sy,
1198 window->user_data);
1199
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001200 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001201 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001202 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001203 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001204 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001205}
1206
1207static void
1208window_handle_keyboard_focus(void *data,
1209 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001210 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001211 struct wl_surface *surface,
1212 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001213{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001214 struct input *input = data;
1215 struct window *window = input->keyboard_focus;
1216 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001217 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001218
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001219 window = input->keyboard_focus;
1220 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001221 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001222 if (window->keyboard_focus_handler)
1223 (*window->keyboard_focus_handler)(window, NULL,
1224 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001225 }
1226
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001227 if (surface)
1228 input->keyboard_focus = wl_surface_get_user_data(surface);
1229 else
1230 input->keyboard_focus = NULL;
1231
1232 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001233 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001234 for (k = keys->data; k < end; k++)
1235 input->modifiers |= d->xkb->map->modmap[*k];
1236
1237 window = input->keyboard_focus;
1238 if (window) {
1239 window->keyboard_device = input;
1240 if (window->keyboard_focus_handler)
1241 (*window->keyboard_focus_handler)(window,
1242 window->keyboard_device,
1243 window->user_data);
1244 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001245}
1246
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001247static const struct wl_input_device_listener input_device_listener = {
1248 window_handle_motion,
1249 window_handle_button,
1250 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001251 window_handle_pointer_focus,
1252 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001253};
1254
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001255void
1256input_get_position(struct input *input, int32_t *x, int32_t *y)
1257{
1258 *x = input->sx;
1259 *y = input->sy;
1260}
1261
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001262struct wl_input_device *
1263input_get_input_device(struct input *input)
1264{
1265 return input->input_device;
1266}
1267
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001268uint32_t
1269input_get_modifiers(struct input *input)
1270{
1271 return input->modifiers;
1272}
1273
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001274struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001275window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001276{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 cairo_device_flush (window->display->device);
1278
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001279 return wl_shell_create_drag(window->display->shell);
1280}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001281
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001282void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001283window_move(struct window *window, struct input *input, uint32_t time)
1284{
1285 wl_shell_move(window->display->shell,
1286 window->surface, input->input_device, time);
1287}
1288
1289void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001290window_activate_drag(struct wl_drag *drag, struct window *window,
1291 struct input *input, uint32_t time)
1292{
1293 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001294}
1295
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001296static void
1297handle_configure(void *data, struct wl_shell *shell,
1298 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001299 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001300{
1301 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001302 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001303
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001304 /* FIXME: this is probably the wrong place to check for width
1305 * or height <= 0, but it prevents the compositor from crashing
1306 */
1307 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001308 return;
1309
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001310 window->resize_edges = edges;
1311
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001312 if (window->resize_handler) {
1313 child_width = width - 20 - window->margin * 2;
1314 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001315
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001316 (*window->resize_handler)(window,
1317 child_width, child_height,
1318 window->user_data);
1319 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001320 window->allocation.width = width;
1321 window->allocation.height = height;
1322
1323 if (window->redraw_handler)
1324 window_schedule_redraw(window);
1325 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001326}
1327
1328static const struct wl_shell_listener shell_listener = {
1329 handle_configure,
1330};
1331
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001332void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001333window_get_allocation(struct window *window,
1334 struct rectangle *allocation)
1335{
1336 *allocation = window->allocation;
1337}
1338
1339void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001340window_get_child_allocation(struct window *window,
1341 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001342{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001343 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001344 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001345 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001346 allocation->x = window->margin + 10;
1347 allocation->y = window->margin + 50;
1348 allocation->width =
1349 window->allocation.width - 20 - window->margin * 2;
1350 allocation->height =
1351 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001352 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001353}
1354
1355void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001356window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001357{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001358 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001359 window->allocation.x = 20 + window->margin;
1360 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001361 window->allocation.width = width + 20 + window->margin * 2;
1362 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001363 } else {
1364 window->allocation.x = 0;
1365 window->allocation.y = 0;
1366 window->allocation.width = width;
1367 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001368 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001369}
1370
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001371static gboolean
1372idle_redraw(void *data)
1373{
1374 struct window *window = data;
1375
1376 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001377
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001378 window->redraw_scheduled = 0;
1379
1380 return FALSE;
1381}
1382
1383void
1384window_schedule_redraw(struct window *window)
1385{
1386 if (!window->redraw_scheduled) {
1387 g_idle_add(idle_redraw, window);
1388 window->redraw_scheduled = 1;
1389 }
1390}
1391
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001392void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001393window_set_fullscreen(struct window *window, int fullscreen)
1394{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001395 int32_t width, height;
1396
1397 if (window->fullscreen == fullscreen)
1398 return;
1399
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001400 window->fullscreen = fullscreen;
1401 if (window->fullscreen) {
1402 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001403 width = window->display->screen_allocation.width;
1404 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001405 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001406 width = window->saved_allocation.width - 20 - window->margin * 2;
1407 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001408 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001409
1410 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001411}
1412
1413void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001414window_set_decoration(struct window *window, int decoration)
1415{
1416 window->decoration = decoration;
1417}
1418
1419void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001420window_set_user_data(struct window *window, void *data)
1421{
1422 window->user_data = data;
1423}
1424
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001425void *
1426window_get_user_data(struct window *window)
1427{
1428 return window->user_data;
1429}
1430
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001431void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001432window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001433 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001434{
1435 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001436}
1437
1438void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001439window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001440 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001441{
1442 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001443}
1444
1445void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001446window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001447 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001448{
1449 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001450}
1451
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001452void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001453window_set_button_handler(struct window *window,
1454 window_button_handler_t handler)
1455{
1456 window->button_handler = handler;
1457}
1458
1459void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001460window_set_motion_handler(struct window *window,
1461 window_motion_handler_t handler)
1462{
1463 window->motion_handler = handler;
1464}
1465
1466void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001467window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001468 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001469{
1470 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001471}
1472
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001473void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001474window_set_transparent(struct window *window, int transparent)
1475{
1476 window->transparent = transparent;
1477}
1478
1479void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001480window_set_title(struct window *window, const char *title)
1481{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001482 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001483 window->title = strdup(title);
1484}
1485
1486const char *
1487window_get_title(struct window *window)
1488{
1489 return window->title;
1490}
1491
1492void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001493display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1494 int32_t x, int32_t y, int32_t width, int32_t height)
1495{
1496 struct wl_buffer *buffer;
1497
1498 buffer = display_get_buffer_for_surface(display, cairo_surface);
1499
1500 wl_buffer_damage(buffer, x, y, width, height);
1501}
1502
1503void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001504window_damage(struct window *window, int32_t x, int32_t y,
1505 int32_t width, int32_t height)
1506{
1507 wl_surface_damage(window->surface, x, y, width, height);
1508}
1509
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001510static struct window *
1511window_create_internal(struct display *display, struct window *parent,
1512 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001513{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001514 struct window *window;
1515
1516 window = malloc(sizeof *window);
1517 if (window == NULL)
1518 return NULL;
1519
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001520 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001521 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001522 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001523 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001524 window->allocation.x = 0;
1525 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001526 window->allocation.width = width;
1527 window->allocation.height = height;
1528 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001529 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001530 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001531 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001532
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001533 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001534#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001535 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1536 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001537#else
1538 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1539#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001540 else
1541 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001542
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001543 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001544 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001545
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001546 return window;
1547}
1548
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001549struct window *
1550window_create(struct display *display, int32_t width, int32_t height)
1551{
1552 struct window *window;
1553
1554 window = window_create_internal(display, NULL, width, height);
1555 if (!window)
1556 return NULL;
1557
1558 return window;
1559}
1560
1561struct window *
1562window_create_transient(struct display *display, struct window *parent,
1563 int32_t x, int32_t y, int32_t width, int32_t height)
1564{
1565 struct window *window;
1566
1567 window = window_create_internal(parent->display,
1568 parent, width, height);
1569 if (!window)
1570 return NULL;
1571
1572 window->x = x;
1573 window->y = y;
1574
1575 return window;
1576}
1577
1578void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001579window_set_buffer_type(struct window *window, enum window_buffer_type type)
1580{
1581 window->buffer_type = type;
1582}
1583
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001584
1585static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001586display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001587 struct wl_output *wl_output,
1588 int x, int y,
1589 int physical_width,
1590 int physical_height,
1591 int subpixel,
1592 const char *make,
1593 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001594{
1595 struct display *display = data;
1596
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001597 display->screen_allocation.x = x;
1598 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001599}
1600
1601static void
1602display_handle_mode(void *data,
1603 struct wl_output *wl_output,
1604 uint32_t flags,
1605 int width,
1606 int height,
1607 int refresh)
1608{
1609 struct display *display = data;
1610
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001611 display->screen_allocation.width = width;
1612 display->screen_allocation.height = height;
1613}
1614
1615static const struct wl_output_listener output_listener = {
1616 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001617 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001618};
1619
1620static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001621display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001622{
1623 struct input *input;
1624
1625 input = malloc(sizeof *input);
1626 if (input == NULL)
1627 return;
1628
1629 memset(input, 0, sizeof *input);
1630 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001631 input->input_device =
1632 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001633 input->pointer_focus = NULL;
1634 input->keyboard_focus = NULL;
1635 wl_list_insert(d->input_list.prev, &input->link);
1636
1637 wl_input_device_add_listener(input->input_device,
1638 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001639 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001640}
1641
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001642struct selection_offer {
1643 struct display *display;
1644 struct wl_selection_offer *offer;
1645 struct wl_array types;
1646 struct input *input;
1647};
1648
1649int
1650input_offers_mime_type(struct input *input, const char *type)
1651{
1652 struct selection_offer *offer = input->offer;
1653 char **p, **end;
1654
1655 if (offer == NULL)
1656 return 0;
1657
1658 end = offer->types.data + offer->types.size;
1659 for (p = offer->types.data; p < end; p++)
1660 if (strcmp(*p, type) == 0)
1661 return 1;
1662
1663 return 0;
1664}
1665
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001666void
1667input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001668{
1669 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001670
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001671 /* FIXME: A number of things can go wrong here: the object may
1672 * not be the current selection offer any more (which could
1673 * still work, but the source may have gone away or just
1674 * destroyed its wl_selection) or the offer may not have the
1675 * requested type after all (programmer/client error,
1676 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001677 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001678}
1679
1680static void
1681selection_offer_offer(void *data,
1682 struct wl_selection_offer *selection_offer,
1683 const char *type)
1684{
1685 struct selection_offer *offer = data;
1686
1687 char **p;
1688
1689 p = wl_array_add(&offer->types, sizeof *p);
1690 if (p)
1691 *p = strdup(type);
1692};
1693
1694static void
1695selection_offer_keyboard_focus(void *data,
1696 struct wl_selection_offer *selection_offer,
1697 struct wl_input_device *input_device)
1698{
1699 struct selection_offer *offer = data;
1700 struct input *input;
1701 char **p, **end;
1702
1703 if (input_device == NULL) {
1704 printf("selection offer retracted %p\n", selection_offer);
1705 input = offer->input;
1706 input->offer = NULL;
1707 wl_selection_offer_destroy(selection_offer);
1708 wl_array_release(&offer->types);
1709 free(offer);
1710 return;
1711 }
1712
1713 input = wl_input_device_get_user_data(input_device);
1714 printf("new selection offer %p:", selection_offer);
1715
1716 offer->input = input;
1717 input->offer = offer;
1718 end = offer->types.data + offer->types.size;
1719 for (p = offer->types.data; p < end; p++)
1720 printf(" %s", *p);
1721
1722 printf("\n");
1723}
1724
1725struct wl_selection_offer_listener selection_offer_listener = {
1726 selection_offer_offer,
1727 selection_offer_keyboard_focus
1728};
1729
1730static void
1731add_selection_offer(struct display *d, uint32_t id)
1732{
1733 struct selection_offer *offer;
1734
1735 offer = malloc(sizeof *offer);
1736 if (offer == NULL)
1737 return;
1738
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001739 offer->offer =
1740 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001741 offer->display = d;
1742 wl_array_init(&offer->types);
1743 offer->input = NULL;
1744
1745 wl_selection_offer_add_listener(offer->offer,
1746 &selection_offer_listener, offer);
1747}
1748
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001749static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001750display_handle_global(struct wl_display *display, uint32_t id,
1751 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001752{
1753 struct display *d = data;
1754
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001755 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001756 d->compositor =
1757 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001758 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001759 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001760 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001761 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001762 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001763 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001764 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001765 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001766 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001767 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001768 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001769 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001770 }
1771}
1772
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001773static void
1774display_render_frame(struct display *d)
1775{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001776 int radius = 8;
1777 cairo_t *cr;
1778
1779 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1780 cr = cairo_create(d->shadow);
1781 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1782 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1783 rounded_rect(cr, 16, 16, 112, 112, radius);
1784 cairo_fill(cr);
1785 cairo_destroy(cr);
1786 blur_surface(d->shadow, 64);
1787
1788 d->active_frame =
1789 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1790 cr = cairo_create(d->active_frame);
1791 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1792 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1793 rounded_rect(cr, 16, 16, 112, 112, radius);
1794 cairo_fill(cr);
1795 cairo_destroy(cr);
1796
1797 d->inactive_frame =
1798 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1799 cr = cairo_create(d->inactive_frame);
1800 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1801 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1802 rounded_rect(cr, 16, 16, 112, 112, radius);
1803 cairo_fill(cr);
1804 cairo_destroy(cr);
1805}
1806
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001807static void
1808init_xkb(struct display *d)
1809{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001810 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001811
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001812 names.rules = "evdev";
1813 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001814 names.layout = option_xkb_layout;
1815 names.variant = option_xkb_variant;
1816 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001817
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001818 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001819 if (!d->xkb) {
1820 fprintf(stderr, "Failed to compile keymap\n");
1821 exit(1);
1822 }
1823}
1824
Yuval Fledel45568f62010-12-06 09:18:12 -05001825static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001826init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001827{
1828 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001829 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001830
1831 static const EGLint premul_argb_cfg_attribs[] = {
1832 EGL_SURFACE_TYPE,
1833 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1834 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001835 EGL_RED_SIZE, 1,
1836 EGL_GREEN_SIZE, 1,
1837 EGL_BLUE_SIZE, 1,
1838 EGL_ALPHA_SIZE, 1,
1839 EGL_DEPTH_SIZE, 1,
1840 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1841 EGL_NONE
1842 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001843
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001844 static const EGLint rgb_cfg_attribs[] = {
1845 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1846 EGL_RED_SIZE, 1,
1847 EGL_GREEN_SIZE, 1,
1848 EGL_BLUE_SIZE, 1,
1849 EGL_ALPHA_SIZE, 0,
1850 EGL_DEPTH_SIZE, 1,
1851 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1852 EGL_NONE
1853 };
1854
Egbert Eiche7b8d902011-05-10 20:00:19 +00001855 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001856 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001857 if (!eglInitialize(d->dpy, &major, &minor)) {
1858 fprintf(stderr, "failed to initialize display\n");
1859 return -1;
1860 }
1861
1862 if (!eglBindAPI(EGL_OPENGL_API)) {
1863 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1864 return -1;
1865 }
1866
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001867 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
1868 &d->premul_argb_config, 1, &n) || n != 1) {
1869 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001870 return -1;
1871 }
1872
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001873 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
1874 &d->rgb_config, 1, &n) || n != 1) {
1875 fprintf(stderr, "failed to choose rgb config\n");
1876 return -1;
1877 }
1878
1879 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001880 if (d->ctx == NULL) {
1881 fprintf(stderr, "failed to create context\n");
1882 return -1;
1883 }
1884
1885 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001886 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001887 return -1;
1888 }
1889
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001890#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001891 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001892 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001893 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001894 return -1;
1895 }
1896#endif
1897
1898 return 0;
1899}
1900
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001901struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04001902display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001903{
1904 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001905 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001906 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001907 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001908
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001909 g_type_init();
1910
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001911 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001912 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001913 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001914
1915 xkb_option_group = g_option_group_new("xkb",
1916 "Keyboard options",
1917 "Show all XKB options",
1918 NULL, NULL);
1919 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1920 g_option_context_add_group (context, xkb_option_group);
1921
1922 if (!g_option_context_parse(context, argc, argv, &error)) {
1923 fprintf(stderr, "option parsing failed: %s\n", error->message);
1924 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001925 }
1926
Tim Wiederhake748f6722011-01-23 23:25:25 +01001927 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001928
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001929 d = malloc(sizeof *d);
1930 if (d == NULL)
1931 return NULL;
1932
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001933 memset(d, 0, sizeof *d);
1934
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001935 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001936 if (d->display == NULL) {
1937 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001938 return NULL;
1939 }
1940
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001941 wl_list_init(&d->input_list);
1942
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001943 /* Set up listener so we'll catch all events. */
1944 wl_display_add_global_listener(d->display,
1945 display_handle_global, d);
1946
1947 /* Process connection events. */
1948 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001949 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001950 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001951
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001952 d->image_target_texture_2d =
1953 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1954 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1955 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1956
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001957 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001958
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001959 display_render_frame(d);
1960
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001961 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001962 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001963 g_source_attach(d->source, NULL);
1964
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001965 wl_list_init(&d->window_list);
1966
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001967 init_xkb(d);
1968
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001969 return d;
1970}
1971
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001972struct wl_display *
1973display_get_display(struct display *display)
1974{
1975 return display->display;
1976}
1977
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001978struct wl_compositor *
1979display_get_compositor(struct display *display)
1980{
1981 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001982}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001983
1984EGLDisplay
1985display_get_egl_display(struct display *d)
1986{
1987 return d->dpy;
1988}
1989
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001990EGLConfig
1991display_get_egl_config(struct display *d)
1992{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001993 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001994}
1995
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001996struct wl_shell *
1997display_get_shell(struct display *display)
1998{
1999 return display->shell;
2000}
2001
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002002void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002003display_acquire_window_surface(struct display *display,
2004 struct window *window,
2005 EGLContext ctx)
2006{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002007#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002008 struct egl_window_surface_data *data;
2009
2010 if (!window->cairo_surface)
2011 return;
2012
2013 if (!ctx)
2014 ctx = display->ctx;
2015
2016 data = cairo_surface_get_user_data(window->cairo_surface,
2017 &surface_data_key);
2018
2019 cairo_device_acquire(display->device);
2020 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2021 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002022#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002023}
2024
2025void
2026display_release(struct display *display)
2027{
2028 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2029 fprintf(stderr, "failed to make context current\n");
2030 cairo_device_release(display->device);
2031}
2032
2033void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002034display_run(struct display *d)
2035{
2036 g_main_loop_run(d->loop);
2037}