blob: 4e958b5092c345c952679d9f60f12e3bd02e4e1c [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øgsberg900b2262011-09-06 14:33:52 -0400115 window_enter_handler_t enter_handler;
116 window_leave_handler_t leave_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400119 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500120};
121
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122struct input {
123 struct display *display;
124 struct wl_input_device *input_device;
125 struct window *pointer_focus;
126 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500127 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400128 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400129 uint32_t modifiers;
130 int32_t x, y, sx, sy;
131 struct wl_list link;
132};
133
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400134enum {
135 POINTER_DEFAULT = 100,
136 POINTER_UNSET
137};
138
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500139enum window_location {
140 WINDOW_INTERIOR = 0,
141 WINDOW_RESIZING_TOP = 1,
142 WINDOW_RESIZING_BOTTOM = 2,
143 WINDOW_RESIZING_LEFT = 4,
144 WINDOW_RESIZING_TOP_LEFT = 5,
145 WINDOW_RESIZING_BOTTOM_LEFT = 6,
146 WINDOW_RESIZING_RIGHT = 8,
147 WINDOW_RESIZING_TOP_RIGHT = 9,
148 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
149 WINDOW_RESIZING_MASK = 15,
150 WINDOW_EXTERIOR = 16,
151 WINDOW_TITLEBAR = 17,
152 WINDOW_CLIENT_AREA = 18,
153};
154
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400155const char *option_xkb_layout = "us";
156const char *option_xkb_variant = "";
157const char *option_xkb_options = "";
158
159static const GOptionEntry xkb_option_entries[] = {
160 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_layout, "XKB Layout" },
162 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
163 &option_xkb_variant, "XKB Variant" },
164 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
165 &option_xkb_options, "XKB Options" },
166 { NULL }
167};
168
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500169static void
170rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
171{
172 cairo_move_to(cr, x0, y0 + radius);
173 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
174 cairo_line_to(cr, x1 - radius, y0);
175 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
176 cairo_line_to(cr, x1, y1 - radius);
177 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
178 cairo_line_to(cr, x0 + radius, y1);
179 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
180 cairo_close_path(cr);
181}
182
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400183static const cairo_user_data_key_t surface_data_key;
184struct surface_data {
185 struct wl_buffer *buffer;
186};
187
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500188#define MULT(_d,c,a,t) \
189 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
190
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500191#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400192
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100193struct egl_window_surface_data {
194 struct display *display;
195 struct wl_surface *surface;
196 struct wl_egl_window *window;
197 EGLSurface surf;
198};
199
200static void
201egl_window_surface_data_destroy(void *p)
202{
203 struct egl_window_surface_data *data = p;
204 struct display *d = data->display;
205
206 eglDestroySurface(d->dpy, data->surf);
207 wl_egl_window_destroy(data->window);
208 data->surface = NULL;
209
210 free(p);
211}
212
213static cairo_surface_t *
214display_create_egl_window_surface(struct display *display,
215 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400216 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100217 struct rectangle *rectangle)
218{
219 cairo_surface_t *cairo_surface;
220 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400221 EGLConfig config;
222 const EGLint *attribs;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100223
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400224 static const EGLint premul_attribs[] = {
225 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
226 EGL_NONE
227 };
228
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100229 data = malloc(sizeof *data);
230 if (data == NULL)
231 return NULL;
232
233 data->display = display;
234 data->surface = surface;
235
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400236 if (flags & SURFACE_OPAQUE) {
237 config = display->rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200238 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400239 } else {
240 config = display->premultiplied_argb_config;
241 attribs = premul_attribs;
242 }
243
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400244 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100245 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400246 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100247
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400248 data->surf = eglCreateWindowSurface(display->dpy, config,
249 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100250
251 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
252 data->surf,
253 rectangle->width,
254 rectangle->height);
255
256 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
257 data, egl_window_surface_data_destroy);
258
259 return cairo_surface;
260}
261
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500262struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400263 struct surface_data data;
264 EGLImageKHR image;
265 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800266 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500267 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400268};
269
270static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500271egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400272{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500273 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800274 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400275
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800276 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400277 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800278 cairo_device_release(d->device);
279
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500280 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400281 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500282 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500283 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400284}
285
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500286EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500287display_get_image_for_egl_image_surface(struct display *display,
288 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500289{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500290 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500291
292 data = cairo_surface_get_user_data (surface, &surface_data_key);
293
294 return data->image;
295}
296
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500297static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500298display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400299 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400301{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500302 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400303 EGLDisplay dpy = display->dpy;
304 cairo_surface_t *surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400305 EGLConfig config;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400306
307 data = malloc(sizeof *data);
308 if (data == NULL)
309 return NULL;
310
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800311 data->display = display;
312
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400313 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400314 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000316 free(data);
317 return NULL;
318 }
319
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400320 if (flags & SURFACE_OPAQUE)
321 config = display->rgb_config;
322 else
323 config = display->premultiplied_argb_config;
324
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500325 data->image = display->create_image(dpy, NULL,
326 EGL_NATIVE_PIXMAP_KHR,
327 (EGLClientBuffer) data->pixmap,
328 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500330 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 free(data);
332 return NULL;
333 }
334
335 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400336 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800338 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500341 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800342 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400343
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400344 surface = cairo_gl_surface_create_for_texture(display->device,
345 CAIRO_CONTENT_COLOR_ALPHA,
346 data->texture,
347 rectangle->width,
348 rectangle->height);
349
350 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352
353 return surface;
354}
355
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500356static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500357display_create_egl_image_surface_from_file(struct display *display,
358 const char *filename,
359 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400360{
361 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400362 GdkPixbuf *pixbuf;
363 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400364 int stride, i;
365 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500366 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400367
368 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400370 FALSE, &error);
371 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400372 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400373
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400374 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
375 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500376 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400377 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400378 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400379
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400380
381 stride = gdk_pixbuf_get_rowstride(pixbuf);
382 pixels = gdk_pixbuf_get_pixels(pixbuf);
383
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400384 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400385 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400386 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400387 while (p < end) {
388 unsigned int t;
389
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400390 MULT(p[0], p[0], p[3], t);
391 MULT(p[1], p[1], p[3], t);
392 MULT(p[2], p[2], p[3], t);
393 p += 4;
394
395 }
396 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400397
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200398 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000399 if (surface == NULL) {
400 g_object_unref(pixbuf);
401 return NULL;
402 }
403
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800404 data = cairo_surface_get_user_data(surface, &surface_data_key);
405
406 cairo_device_acquire(display->device);
407 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800408 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
409 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800410 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400411
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500412 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414 return surface;
415}
416
417#endif
418
419struct wl_buffer *
420display_get_buffer_for_surface(struct display *display,
421 cairo_surface_t *surface)
422{
423 struct surface_data *data;
424
425 data = cairo_surface_get_user_data (surface, &surface_data_key);
426
427 return data->buffer;
428}
429
430struct shm_surface_data {
431 struct surface_data data;
432 void *map;
433 size_t length;
434};
435
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500436static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437shm_surface_data_destroy(void *p)
438{
439 struct shm_surface_data *data = p;
440
441 wl_buffer_destroy(data->data.buffer);
442 munmap(data->map, data->length);
443}
444
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500445static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400446display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400447 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400448{
449 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400450 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400451 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800452 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400453 char filename[] = "/tmp/wayland-shm-XXXXXX";
454
455 data = malloc(sizeof *data);
456 if (data == NULL)
457 return NULL;
458
459 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
460 rectangle->width);
461 data->length = stride * rectangle->height;
462 fd = mkstemp(filename);
463 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000464 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465 return NULL;
466 }
467 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000468 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469 close(fd);
470 return NULL;
471 }
472
473 data->map = mmap(NULL, data->length,
474 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
475 unlink(filename);
476
477 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000478 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400479 close(fd);
480 return NULL;
481 }
482
483 surface = cairo_image_surface_create_for_data (data->map,
484 CAIRO_FORMAT_ARGB32,
485 rectangle->width,
486 rectangle->height,
487 stride);
488
489 cairo_surface_set_user_data (surface, &surface_data_key,
490 data, shm_surface_data_destroy);
491
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400492 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400493 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400494 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400495 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400496
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497 data->data.buffer = wl_shm_create_buffer(display->shm,
498 fd,
499 rectangle->width,
500 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400502
503 close(fd);
504
505 return surface;
506}
507
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500508static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509display_create_shm_surface_from_file(struct display *display,
510 const char *filename,
511 struct rectangle *rect)
512{
513 cairo_surface_t *surface;
514 GdkPixbuf *pixbuf;
515 GError *error = NULL;
516 int stride, i;
517 unsigned char *pixels, *p, *end, *dest_data;
518 int dest_stride;
519 uint32_t *d;
520
521 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
522 rect->width, rect->height,
523 FALSE, &error);
524 if (error != NULL)
525 return NULL;
526
527 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
528 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500529 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530 return NULL;
531 }
532
533 stride = gdk_pixbuf_get_rowstride(pixbuf);
534 pixels = gdk_pixbuf_get_pixels(pixbuf);
535
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400536 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000537 if (surface == NULL) {
538 g_object_unref(pixbuf);
539 return NULL;
540 }
541
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542 dest_data = cairo_image_surface_get_data (surface);
543 dest_stride = cairo_image_surface_get_stride (surface);
544
545 for (i = 0; i < rect->height; i++) {
546 d = (uint32_t *) (dest_data + i * dest_stride);
547 p = pixels + i * stride;
548 end = p + rect->width * 4;
549 while (p < end) {
550 unsigned int t;
551 unsigned char a, r, g, b;
552
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553 a = p[3];
554 MULT(r, p[0], a, t);
555 MULT(g, p[1], a, t);
556 MULT(b, p[2], a, t);
557 p += 4;
558 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
559 }
560 }
561
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500562 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563
564 return surface;
565}
566
nobled7b87cb02011-02-01 18:51:47 +0000567static int
568check_size(struct rectangle *rect)
569{
570 if (rect->width && rect->height)
571 return 0;
572
573 fprintf(stderr, "tried to create surface of "
574 "width: %d, height: %d\n", rect->width, rect->height);
575 return -1;
576}
577
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578cairo_surface_t *
579display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100580 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400581 struct rectangle *rectangle,
582 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583{
nobled7b87cb02011-02-01 18:51:47 +0000584 if (check_size(rectangle) < 0)
585 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500586#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500587 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100588 if (surface)
589 return display_create_egl_window_surface(display,
590 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400591 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100592 rectangle);
593 else
594 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400595 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500597 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400599 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600}
601
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500602static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603display_create_surface_from_file(struct display *display,
604 const char *filename,
605 struct rectangle *rectangle)
606{
nobled7b87cb02011-02-01 18:51:47 +0000607 if (check_size(rectangle) < 0)
608 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500609#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500610 if (display->dpy) {
611 return display_create_egl_image_surface_from_file(display,
612 filename,
613 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500614 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500616 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400617}
Yuval Fledel45568f62010-12-06 09:18:12 -0500618 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400619 const char *filename;
620 int hotspot_x, hotspot_y;
621} pointer_images[] = {
622 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
623 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
624 { DATADIR "/wayland/bottom_side.png", 16, 20 },
625 { DATADIR "/wayland/grabbing.png", 20, 17 },
626 { DATADIR "/wayland/left_ptr.png", 10, 5 },
627 { DATADIR "/wayland/left_side.png", 10, 20 },
628 { DATADIR "/wayland/right_side.png", 30, 19 },
629 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
630 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
631 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400632 { DATADIR "/wayland/xterm.png", 15, 15 },
633 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400634};
635
636static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400637create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400638{
639 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400640 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400641 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400642
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400643 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400644 display->pointer_surfaces =
645 malloc(count * sizeof *display->pointer_surfaces);
646 rect.width = width;
647 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400648 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400649 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650 display_create_surface_from_file(display,
651 pointer_images[i].filename,
652 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400653 }
654
655}
656
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400657cairo_surface_t *
658display_get_pointer_surface(struct display *display, int pointer,
659 int *width, int *height,
660 int *hotspot_x, int *hotspot_y)
661{
662 cairo_surface_t *surface;
663
664 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500665#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400666 *width = cairo_gl_surface_get_width(surface);
667 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000668#else
669 *width = cairo_image_surface_get_width(surface);
670 *height = cairo_image_surface_get_height(surface);
671#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672 *hotspot_x = pointer_images[pointer].hotspot_x;
673 *hotspot_y = pointer_images[pointer].hotspot_y;
674
675 return cairo_surface_reference(surface);
676}
677
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400678
679static void
680window_attach_surface(struct window *window);
681
682static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400683free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400684{
685 struct window *window = data;
686
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400687 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400688 cairo_surface_destroy(window->pending_surface);
689 window->pending_surface = NULL;
690 if (window->cairo_surface)
691 window_attach_surface(window);
692}
693
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400694static const struct wl_callback_listener free_surface_listener = {
695 free_surface
696};
697
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500698static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200699window_get_resize_dx_dy(struct window *window, int *x, int *y)
700{
701 if (window->resize_edges & WINDOW_RESIZING_LEFT)
702 *x = window->server_allocation.width - window->allocation.width;
703 else
704 *x = 0;
705
706 if (window->resize_edges & WINDOW_RESIZING_TOP)
707 *y = window->server_allocation.height -
708 window->allocation.height;
709 else
710 *y = 0;
711
712 window->resize_edges = 0;
713}
714
715static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500716window_attach_surface(struct window *window)
717{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400718 struct display *display = window->display;
719 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400720 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000723#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500724 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725
726 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000727#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
729 data = cairo_surface_get_user_data(window->cairo_surface,
730 &surface_data_key);
731
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 cairo_gl_surface_swapbuffers(window->cairo_surface);
733 wl_egl_window_get_attached_size(data->window,
734 &window->server_allocation.width,
735 &window->server_allocation.height);
736 break;
737 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000738#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200740 window_get_resize_dx_dy(window, &x, &y);
741
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 if (window->pending_surface != NULL)
743 return;
744
745 window->pending_surface = window->cairo_surface;
746 window->cairo_surface = NULL;
747
748 buffer =
749 display_get_buffer_for_surface(display,
750 window->pending_surface);
751
752 wl_surface_attach(window->surface, buffer, x, y);
753 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754 cb = wl_display_sync(display->display);
755 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100756 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000757 default:
758 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100759 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500760
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500761 if (window->fullscreen)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400762 wl_shell_set_fullscreen(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500763 else if (!window->parent)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400764 wl_shell_set_toplevel(display->shell, window->surface);
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500765 else
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400766 wl_shell_set_transient(display->shell, window->surface,
767 window->parent->surface,
768 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500769
770 wl_surface_damage(window->surface, 0, 0,
771 window->allocation.width,
772 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500773}
774
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500775void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400776window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500777{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100778 if (window->cairo_surface) {
779 switch (window->buffer_type) {
780 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
781 case WINDOW_BUFFER_TYPE_SHM:
782 display_surface_damage(window->display,
783 window->cairo_surface,
784 0, 0,
785 window->allocation.width,
786 window->allocation.height);
787 break;
788 default:
789 break;
790 }
791 window_attach_surface(window);
792 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500793}
794
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400795void
796window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500798 cairo_surface_reference(surface);
799
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400800 if (window->cairo_surface != NULL)
801 cairo_surface_destroy(window->cairo_surface);
802
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500803 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400804}
805
Benjamin Franzke22d54812011-07-16 19:50:32 +0000806#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100807static void
808window_resize_cairo_window_surface(struct window *window)
809{
810 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200811 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812
813 data = cairo_surface_get_user_data(window->cairo_surface,
814 &surface_data_key);
815
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200816 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 wl_egl_window_resize(data->window,
818 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200819 window->allocation.height,
820 x,y);
821
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 cairo_gl_surface_set_size(window->cairo_surface,
823 window->allocation.width,
824 window->allocation.height);
825}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000826#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828void
829window_create_surface(struct window *window)
830{
831 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400832 uint32_t flags = 0;
833
834 if (!window->transparent)
835 flags = SURFACE_OPAQUE;
836
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500838#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
840 if (window->cairo_surface) {
841 window_resize_cairo_window_surface(window);
842 return;
843 }
844 surface = display_create_surface(window->display,
845 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400846 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500848 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400849 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400851 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400852 break;
853#endif
854 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400855 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400856 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400857 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800858 default:
859 surface = NULL;
860 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862
863 window_set_surface(window, surface);
864 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865}
866
867static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500868window_draw_menu(struct window *window)
869{
870 cairo_t *cr;
871 int width, height, r = 5;
872
873 window_create_surface(window);
874
875 cr = cairo_create(window->cairo_surface);
876 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
877 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
878 cairo_paint(cr);
879
880 width = window->allocation.width;
881 height = window->allocation.height;
882 rounded_rect(cr, r, r, width - r, height - r, r);
883 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
884 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
885 cairo_fill(cr);
886 cairo_destroy(cr);
887}
888
889static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500890window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500891{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500892 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500893 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400894 cairo_surface_t *frame;
895 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500896
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400898
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400899 width = window->allocation.width;
900 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500901
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500902 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500903
Kristian Høgsberg09531622010-06-14 23:22:15 -0400904 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400905 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400906 cairo_paint(cr);
907
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400908 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400909 tile_mask(cr, window->display->shadow,
910 shadow_dx, shadow_dy, width, height,
911 window->margin + 10 - shadow_dx,
912 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500913
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400914 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400915 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400916 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400917 frame = window->display->inactive_frame;
918
919 tile_source(cr, frame, 0, 0, width, height,
920 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500921
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500922 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
923 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500924 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400925 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400926 if (window->keyboard_device)
927 cairo_set_source_rgb(cr, 0, 0, 0);
928 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400929 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400930 cairo_show_text(cr, window->title);
931
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500932 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400933
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100934 /* FIXME: this breakes gears, fix cairo? */
935#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400936 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100937#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500938}
939
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400940void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500941window_destroy(struct window *window)
942{
943 wl_surface_destroy(window->surface);
944 wl_list_remove(&window->link);
945 free(window);
946}
947
948void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400949display_flush_cairo_device(struct display *display)
950{
951 cairo_device_flush (display->device);
952}
953
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500954void
955window_draw(struct window *window)
956{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500957 if (window->parent)
958 window_draw_menu(window);
959 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400960 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500961 else
962 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500963}
964
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400965cairo_surface_t *
966window_get_surface(struct window *window)
967{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400968 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400969}
970
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100971struct wl_surface *
972window_get_wl_surface(struct window *window)
973{
974 return window->surface;
975}
976
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400977static int
978get_pointer_location(struct window *window, int32_t x, int32_t y)
979{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400980 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400981 const int grip_size = 8;
982
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500983 if (!window->decoration)
984 return WINDOW_CLIENT_AREA;
985
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400986 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400988 else if (window->margin <= x && x < window->margin + grip_size)
989 hlocation = WINDOW_RESIZING_LEFT;
990 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400991 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400992 else if (x < window->allocation.width - window->margin)
993 hlocation = WINDOW_RESIZING_RIGHT;
994 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400995 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400996
997 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400998 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400999 else if (window->margin <= y && y < window->margin + grip_size)
1000 vlocation = WINDOW_RESIZING_TOP;
1001 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001002 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001003 else if (y < window->allocation.height - window->margin)
1004 vlocation = WINDOW_RESIZING_BOTTOM;
1005 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001006 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001007
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001008 location = vlocation | hlocation;
1009 if (location & WINDOW_EXTERIOR)
1010 location = WINDOW_EXTERIOR;
1011 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1012 location = WINDOW_TITLEBAR;
1013 else if (location == WINDOW_INTERIOR)
1014 location = WINDOW_CLIENT_AREA;
1015
1016 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001017}
1018
1019static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001020set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001021{
1022 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001023 struct wl_buffer *buffer;
1024 cairo_surface_t *surface;
1025 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001026
1027 location = get_pointer_location(input->pointer_focus,
1028 input->sx, input->sy);
1029 switch (location) {
1030 case WINDOW_RESIZING_TOP:
1031 pointer = POINTER_TOP;
1032 break;
1033 case WINDOW_RESIZING_BOTTOM:
1034 pointer = POINTER_BOTTOM;
1035 break;
1036 case WINDOW_RESIZING_LEFT:
1037 pointer = POINTER_LEFT;
1038 break;
1039 case WINDOW_RESIZING_RIGHT:
1040 pointer = POINTER_RIGHT;
1041 break;
1042 case WINDOW_RESIZING_TOP_LEFT:
1043 pointer = POINTER_TOP_LEFT;
1044 break;
1045 case WINDOW_RESIZING_TOP_RIGHT:
1046 pointer = POINTER_TOP_RIGHT;
1047 break;
1048 case WINDOW_RESIZING_BOTTOM_LEFT:
1049 pointer = POINTER_BOTTOM_LEFT;
1050 break;
1051 case WINDOW_RESIZING_BOTTOM_RIGHT:
1052 pointer = POINTER_BOTTOM_RIGHT;
1053 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001054 case WINDOW_EXTERIOR:
1055 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001056 if (input->current_pointer_image == POINTER_DEFAULT)
1057 return;
1058
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001059 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001060 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001061 return;
1062 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001063 break;
1064 }
1065
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001066 if (pointer == input->current_pointer_image)
1067 return;
1068
1069 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001070 surface = display->pointer_surfaces[pointer];
1071 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001072 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001073 pointer_images[pointer].hotspot_x,
1074 pointer_images[pointer].hotspot_y);
1075}
1076
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001077static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001078window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001079 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001080 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001081{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001082 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001083 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001084 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001085
1086 input->x = x;
1087 input->y = y;
1088 input->sx = sx;
1089 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001090
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001091 if (window->motion_handler)
1092 pointer = (*window->motion_handler)(window, input, time,
1093 x, y, sx, sy,
1094 window->user_data);
1095
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001096 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001097}
1098
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001099static void
1100window_handle_button(void *data,
1101 struct wl_input_device *input_device,
1102 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001103{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001104 struct input *input = data;
1105 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001106 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001107
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001109
1110 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001112 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001113 wl_shell_move(window->display->shell,
1114 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001115 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001116 case WINDOW_RESIZING_TOP:
1117 case WINDOW_RESIZING_BOTTOM:
1118 case WINDOW_RESIZING_LEFT:
1119 case WINDOW_RESIZING_RIGHT:
1120 case WINDOW_RESIZING_TOP_LEFT:
1121 case WINDOW_RESIZING_TOP_RIGHT:
1122 case WINDOW_RESIZING_BOTTOM_LEFT:
1123 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001124 wl_shell_resize(window->display->shell,
1125 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001126 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001127 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001128 case WINDOW_CLIENT_AREA:
1129 if (window->button_handler)
1130 (*window->button_handler)(window,
1131 input, time,
1132 button, state,
1133 window->user_data);
1134 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001135 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001136 } else {
1137 if (window->button_handler)
1138 (*window->button_handler)(window,
1139 input, time,
1140 button, state,
1141 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001142 }
1143}
1144
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001145static void
1146window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001147 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001148{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001149 struct input *input = data;
1150 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001151 struct display *d = window->display;
1152 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001153
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001154 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001155 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001156 return;
1157
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001158 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001159 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001160 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1161 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001162
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001163 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1164
1165 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001166 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001167 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001168 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001169
1170 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001171 (*window->key_handler)(window, input, time, key, sym, state,
1172 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001173}
1174
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001175static void
1176window_handle_pointer_focus(void *data,
1177 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001178 uint32_t time, struct wl_surface *surface,
1179 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001180{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001181 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001182 struct window *window;
1183 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001184
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001185 window = input->pointer_focus;
1186 if (window && window->surface != surface) {
1187 if (window->leave_handler)
1188 window->leave_handler(window, input,
1189 time, window->user_data);
1190 input->pointer_focus = NULL;
1191 input->current_pointer_image = POINTER_UNSET;
1192 }
1193
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001194 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001195 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001196 window = input->pointer_focus;
1197
Kristian Høgsberg59826582011-01-20 11:56:57 -05001198 input->x = x;
1199 input->y = y;
1200 input->sx = sx;
1201 input->sy = sy;
1202
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001203 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001204 if (window->enter_handler)
1205 pointer = window->enter_handler(window, input,
1206 time, sx, sy,
1207 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001209 set_pointer_image(input, time, pointer);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001210
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001211 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001212}
1213
1214static void
1215window_handle_keyboard_focus(void *data,
1216 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001217 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001218 struct wl_surface *surface,
1219 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001220{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001221 struct input *input = data;
1222 struct window *window = input->keyboard_focus;
1223 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001224 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001225
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001226 window = input->keyboard_focus;
1227 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001228 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001229 if (window->keyboard_focus_handler)
1230 (*window->keyboard_focus_handler)(window, NULL,
1231 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001232 }
1233
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001234 if (surface)
1235 input->keyboard_focus = wl_surface_get_user_data(surface);
1236 else
1237 input->keyboard_focus = NULL;
1238
1239 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001240 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001241 for (k = keys->data; k < end; k++)
1242 input->modifiers |= d->xkb->map->modmap[*k];
1243
1244 window = input->keyboard_focus;
1245 if (window) {
1246 window->keyboard_device = input;
1247 if (window->keyboard_focus_handler)
1248 (*window->keyboard_focus_handler)(window,
1249 window->keyboard_device,
1250 window->user_data);
1251 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001252}
1253
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001254static const struct wl_input_device_listener input_device_listener = {
1255 window_handle_motion,
1256 window_handle_button,
1257 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001258 window_handle_pointer_focus,
1259 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001260};
1261
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262void
1263input_get_position(struct input *input, int32_t *x, int32_t *y)
1264{
1265 *x = input->sx;
1266 *y = input->sy;
1267}
1268
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001269struct wl_input_device *
1270input_get_input_device(struct input *input)
1271{
1272 return input->input_device;
1273}
1274
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001275uint32_t
1276input_get_modifiers(struct input *input)
1277{
1278 return input->modifiers;
1279}
1280
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001281struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001282window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001283{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001284 cairo_device_flush (window->display->device);
1285
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001286 return wl_shell_create_drag(window->display->shell);
1287}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001288
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001289void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001290window_move(struct window *window, struct input *input, uint32_t time)
1291{
1292 wl_shell_move(window->display->shell,
1293 window->surface, input->input_device, time);
1294}
1295
1296void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001297window_activate_drag(struct wl_drag *drag, struct window *window,
1298 struct input *input, uint32_t time)
1299{
1300 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301}
1302
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001303static void
1304handle_configure(void *data, struct wl_shell *shell,
1305 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001306 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001307{
1308 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001309 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001310
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001311 /* FIXME: this is probably the wrong place to check for width
1312 * or height <= 0, but it prevents the compositor from crashing
1313 */
1314 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001315 return;
1316
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001317 window->resize_edges = edges;
1318
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001319 if (window->resize_handler) {
1320 child_width = width - 20 - window->margin * 2;
1321 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001322
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001323 (*window->resize_handler)(window,
1324 child_width, child_height,
1325 window->user_data);
1326 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001327 window->allocation.width = width;
1328 window->allocation.height = height;
1329
1330 if (window->redraw_handler)
1331 window_schedule_redraw(window);
1332 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001333}
1334
1335static const struct wl_shell_listener shell_listener = {
1336 handle_configure,
1337};
1338
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001339void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001340window_get_allocation(struct window *window,
1341 struct rectangle *allocation)
1342{
1343 *allocation = window->allocation;
1344}
1345
1346void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001347window_get_child_allocation(struct window *window,
1348 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001349{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001350 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001351 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001352 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001353 allocation->x = window->margin + 10;
1354 allocation->y = window->margin + 50;
1355 allocation->width =
1356 window->allocation.width - 20 - window->margin * 2;
1357 allocation->height =
1358 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001359 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001360}
1361
1362void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001363window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001364{
Kristian Høgsberg12b0bb32011-04-11 13:18:31 -04001365 if (!window->fullscreen && window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001366 window->allocation.x = 20 + window->margin;
1367 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001368 window->allocation.width = width + 20 + window->margin * 2;
1369 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001370 } else {
1371 window->allocation.x = 0;
1372 window->allocation.y = 0;
1373 window->allocation.width = width;
1374 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001375 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001376}
1377
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001378static gboolean
1379idle_redraw(void *data)
1380{
1381 struct window *window = data;
1382
1383 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001384
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001385 window->redraw_scheduled = 0;
1386
1387 return FALSE;
1388}
1389
1390void
1391window_schedule_redraw(struct window *window)
1392{
1393 if (!window->redraw_scheduled) {
1394 g_idle_add(idle_redraw, window);
1395 window->redraw_scheduled = 1;
1396 }
1397}
1398
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001399void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001400window_set_fullscreen(struct window *window, int fullscreen)
1401{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001402 int32_t width, height;
1403
1404 if (window->fullscreen == fullscreen)
1405 return;
1406
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001407 window->fullscreen = fullscreen;
1408 if (window->fullscreen) {
1409 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001410 width = window->display->screen_allocation.width;
1411 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001412 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001413 width = window->saved_allocation.width - 20 - window->margin * 2;
1414 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001415 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001416
1417 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001418}
1419
1420void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001421window_set_decoration(struct window *window, int decoration)
1422{
1423 window->decoration = decoration;
1424}
1425
1426void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001427window_set_user_data(struct window *window, void *data)
1428{
1429 window->user_data = data;
1430}
1431
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001432void *
1433window_get_user_data(struct window *window)
1434{
1435 return window->user_data;
1436}
1437
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001438void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001439window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001440 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001441{
1442 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001443}
1444
1445void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001446window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001447 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001448{
1449 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001450}
1451
1452void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001453window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001454 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001455{
1456 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001457}
1458
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001459void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001460window_set_button_handler(struct window *window,
1461 window_button_handler_t handler)
1462{
1463 window->button_handler = handler;
1464}
1465
1466void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001467window_set_motion_handler(struct window *window,
1468 window_motion_handler_t handler)
1469{
1470 window->motion_handler = handler;
1471}
1472
1473void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001474window_set_enter_handler(struct window *window,
1475 window_enter_handler_t handler)
1476{
1477 window->enter_handler = handler;
1478}
1479
1480void
1481window_set_leave_handler(struct window *window,
1482 window_leave_handler_t handler)
1483{
1484 window->leave_handler = handler;
1485}
1486
1487void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001488window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001489 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001490{
1491 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001492}
1493
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001494void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001495window_set_transparent(struct window *window, int transparent)
1496{
1497 window->transparent = transparent;
1498}
1499
1500void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001501window_set_title(struct window *window, const char *title)
1502{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001503 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001504 window->title = strdup(title);
1505}
1506
1507const char *
1508window_get_title(struct window *window)
1509{
1510 return window->title;
1511}
1512
1513void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01001514display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1515 int32_t x, int32_t y, int32_t width, int32_t height)
1516{
1517 struct wl_buffer *buffer;
1518
1519 buffer = display_get_buffer_for_surface(display, cairo_surface);
1520
1521 wl_buffer_damage(buffer, x, y, width, height);
1522}
1523
1524void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001525window_damage(struct window *window, int32_t x, int32_t y,
1526 int32_t width, int32_t height)
1527{
1528 wl_surface_damage(window->surface, x, y, width, height);
1529}
1530
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001531static struct window *
1532window_create_internal(struct display *display, struct window *parent,
1533 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001534{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001535 struct window *window;
1536
1537 window = malloc(sizeof *window);
1538 if (window == NULL)
1539 return NULL;
1540
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001541 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001542 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001543 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001544 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001545 window->allocation.x = 0;
1546 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001547 window->allocation.width = width;
1548 window->allocation.height = height;
1549 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001550 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001551 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001552 window->transparent = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001553
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001554 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00001555#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001556 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1557 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00001558#else
1559 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1560#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05001561 else
1562 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001563
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001564 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001565 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001566
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001567 return window;
1568}
1569
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001570struct window *
1571window_create(struct display *display, int32_t width, int32_t height)
1572{
1573 struct window *window;
1574
1575 window = window_create_internal(display, NULL, width, height);
1576 if (!window)
1577 return NULL;
1578
1579 return window;
1580}
1581
1582struct window *
1583window_create_transient(struct display *display, struct window *parent,
1584 int32_t x, int32_t y, int32_t width, int32_t height)
1585{
1586 struct window *window;
1587
1588 window = window_create_internal(parent->display,
1589 parent, width, height);
1590 if (!window)
1591 return NULL;
1592
1593 window->x = x;
1594 window->y = y;
1595
1596 return window;
1597}
1598
1599void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001600window_set_buffer_type(struct window *window, enum window_buffer_type type)
1601{
1602 window->buffer_type = type;
1603}
1604
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04001605
1606static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001607display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001608 struct wl_output *wl_output,
1609 int x, int y,
1610 int physical_width,
1611 int physical_height,
1612 int subpixel,
1613 const char *make,
1614 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001615{
1616 struct display *display = data;
1617
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001618 display->screen_allocation.x = x;
1619 display->screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001620}
1621
1622static void
1623display_handle_mode(void *data,
1624 struct wl_output *wl_output,
1625 uint32_t flags,
1626 int width,
1627 int height,
1628 int refresh)
1629{
1630 struct display *display = data;
1631
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001632 display->screen_allocation.width = width;
1633 display->screen_allocation.height = height;
1634}
1635
1636static const struct wl_output_listener output_listener = {
1637 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001638 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001639};
1640
1641static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001642display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001643{
1644 struct input *input;
1645
1646 input = malloc(sizeof *input);
1647 if (input == NULL)
1648 return;
1649
1650 memset(input, 0, sizeof *input);
1651 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001652 input->input_device =
1653 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001654 input->pointer_focus = NULL;
1655 input->keyboard_focus = NULL;
1656 wl_list_insert(d->input_list.prev, &input->link);
1657
1658 wl_input_device_add_listener(input->input_device,
1659 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001660 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001661}
1662
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001663struct selection_offer {
1664 struct display *display;
1665 struct wl_selection_offer *offer;
1666 struct wl_array types;
1667 struct input *input;
1668};
1669
1670int
1671input_offers_mime_type(struct input *input, const char *type)
1672{
1673 struct selection_offer *offer = input->offer;
1674 char **p, **end;
1675
1676 if (offer == NULL)
1677 return 0;
1678
1679 end = offer->types.data + offer->types.size;
1680 for (p = offer->types.data; p < end; p++)
1681 if (strcmp(*p, type) == 0)
1682 return 1;
1683
1684 return 0;
1685}
1686
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001687void
1688input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001689{
1690 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001691
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001692 /* FIXME: A number of things can go wrong here: the object may
1693 * not be the current selection offer any more (which could
1694 * still work, but the source may have gone away or just
1695 * destroyed its wl_selection) or the offer may not have the
1696 * requested type after all (programmer/client error,
1697 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001698 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001699}
1700
1701static void
1702selection_offer_offer(void *data,
1703 struct wl_selection_offer *selection_offer,
1704 const char *type)
1705{
1706 struct selection_offer *offer = data;
1707
1708 char **p;
1709
1710 p = wl_array_add(&offer->types, sizeof *p);
1711 if (p)
1712 *p = strdup(type);
1713};
1714
1715static void
1716selection_offer_keyboard_focus(void *data,
1717 struct wl_selection_offer *selection_offer,
1718 struct wl_input_device *input_device)
1719{
1720 struct selection_offer *offer = data;
1721 struct input *input;
1722 char **p, **end;
1723
1724 if (input_device == NULL) {
1725 printf("selection offer retracted %p\n", selection_offer);
1726 input = offer->input;
1727 input->offer = NULL;
1728 wl_selection_offer_destroy(selection_offer);
1729 wl_array_release(&offer->types);
1730 free(offer);
1731 return;
1732 }
1733
1734 input = wl_input_device_get_user_data(input_device);
1735 printf("new selection offer %p:", selection_offer);
1736
1737 offer->input = input;
1738 input->offer = offer;
1739 end = offer->types.data + offer->types.size;
1740 for (p = offer->types.data; p < end; p++)
1741 printf(" %s", *p);
1742
1743 printf("\n");
1744}
1745
1746struct wl_selection_offer_listener selection_offer_listener = {
1747 selection_offer_offer,
1748 selection_offer_keyboard_focus
1749};
1750
1751static void
1752add_selection_offer(struct display *d, uint32_t id)
1753{
1754 struct selection_offer *offer;
1755
1756 offer = malloc(sizeof *offer);
1757 if (offer == NULL)
1758 return;
1759
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001760 offer->offer =
1761 wl_display_bind(d->display, id, &wl_selection_offer_interface);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001762 offer->display = d;
1763 wl_array_init(&offer->types);
1764 offer->input = NULL;
1765
1766 wl_selection_offer_add_listener(offer->offer,
1767 &selection_offer_listener, offer);
1768}
1769
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001770static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001771display_handle_global(struct wl_display *display, uint32_t id,
1772 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001773{
1774 struct display *d = data;
1775
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001776 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001777 d->compositor =
1778 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001779 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001780 d->output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001781 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001782 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001783 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001784 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001785 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001786 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001787 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04001788 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04001789 } else if (strcmp(interface, "wl_selection_offer") == 0) {
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001790 add_selection_offer(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001791 }
1792}
1793
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001794static void
1795display_render_frame(struct display *d)
1796{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001797 int radius = 8;
1798 cairo_t *cr;
1799
1800 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1801 cr = cairo_create(d->shadow);
1802 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1803 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1804 rounded_rect(cr, 16, 16, 112, 112, radius);
1805 cairo_fill(cr);
1806 cairo_destroy(cr);
1807 blur_surface(d->shadow, 64);
1808
1809 d->active_frame =
1810 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1811 cr = cairo_create(d->active_frame);
1812 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1813 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1814 rounded_rect(cr, 16, 16, 112, 112, radius);
1815 cairo_fill(cr);
1816 cairo_destroy(cr);
1817
1818 d->inactive_frame =
1819 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1820 cr = cairo_create(d->inactive_frame);
1821 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1822 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1823 rounded_rect(cr, 16, 16, 112, 112, radius);
1824 cairo_fill(cr);
1825 cairo_destroy(cr);
1826}
1827
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001828static void
1829init_xkb(struct display *d)
1830{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001831 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001832
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001833 names.rules = "evdev";
1834 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001835 names.layout = option_xkb_layout;
1836 names.variant = option_xkb_variant;
1837 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001838
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001839 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001840 if (!d->xkb) {
1841 fprintf(stderr, "Failed to compile keymap\n");
1842 exit(1);
1843 }
1844}
1845
Yuval Fledel45568f62010-12-06 09:18:12 -05001846static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001847init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001848{
1849 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001850 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001851
1852 static const EGLint premul_argb_cfg_attribs[] = {
1853 EGL_SURFACE_TYPE,
1854 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
1855 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001856 EGL_RED_SIZE, 1,
1857 EGL_GREEN_SIZE, 1,
1858 EGL_BLUE_SIZE, 1,
1859 EGL_ALPHA_SIZE, 1,
1860 EGL_DEPTH_SIZE, 1,
1861 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1862 EGL_NONE
1863 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001864
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001865 static const EGLint rgb_cfg_attribs[] = {
1866 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1867 EGL_RED_SIZE, 1,
1868 EGL_GREEN_SIZE, 1,
1869 EGL_BLUE_SIZE, 1,
1870 EGL_ALPHA_SIZE, 0,
1871 EGL_DEPTH_SIZE, 1,
1872 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1873 EGL_NONE
1874 };
1875
Kristian Høgsberg91342c62011-04-14 14:44:58 -04001876 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05001877 if (!eglInitialize(d->dpy, &major, &minor)) {
1878 fprintf(stderr, "failed to initialize display\n");
1879 return -1;
1880 }
1881
1882 if (!eglBindAPI(EGL_OPENGL_API)) {
1883 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1884 return -1;
1885 }
1886
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001887 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02001888 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001889 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001890 return -1;
1891 }
1892
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04001893 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
1894 &d->rgb_config, 1, &n) || n != 1) {
1895 fprintf(stderr, "failed to choose rgb config\n");
1896 return -1;
1897 }
1898
1899 d->ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001900 if (d->ctx == NULL) {
1901 fprintf(stderr, "failed to create context\n");
1902 return -1;
1903 }
1904
1905 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001906 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001907 return -1;
1908 }
1909
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001910#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001911 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsbergd11eadb2011-04-14 11:54:59 -04001912 if (cairo_device_status(d->device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001913 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001914 return -1;
1915 }
1916#endif
1917
1918 return 0;
1919}
1920
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001921struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04001922display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001923{
1924 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001925 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001926 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001927 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001928
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001929 g_type_init();
1930
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001931 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001932 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001933 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001934
1935 xkb_option_group = g_option_group_new("xkb",
1936 "Keyboard options",
1937 "Show all XKB options",
1938 NULL, NULL);
1939 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1940 g_option_context_add_group (context, xkb_option_group);
1941
1942 if (!g_option_context_parse(context, argc, argv, &error)) {
1943 fprintf(stderr, "option parsing failed: %s\n", error->message);
1944 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001945 }
1946
Tim Wiederhake748f6722011-01-23 23:25:25 +01001947 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001948
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001949 d = malloc(sizeof *d);
1950 if (d == NULL)
1951 return NULL;
1952
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001953 memset(d, 0, sizeof *d);
1954
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001955 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001956 if (d->display == NULL) {
1957 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001958 return NULL;
1959 }
1960
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001961 wl_list_init(&d->input_list);
1962
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001963 /* Set up listener so we'll catch all events. */
1964 wl_display_add_global_listener(d->display,
1965 display_handle_global, d);
1966
1967 /* Process connection events. */
1968 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001969 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001970 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001971
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001972 d->image_target_texture_2d =
1973 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1974 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1975 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1976
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001977 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001978
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001979 display_render_frame(d);
1980
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001981 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001982 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001983 g_source_attach(d->source, NULL);
1984
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001985 wl_list_init(&d->window_list);
1986
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001987 init_xkb(d);
1988
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001989 return d;
1990}
1991
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001992struct wl_display *
1993display_get_display(struct display *display)
1994{
1995 return display->display;
1996}
1997
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001998struct wl_compositor *
1999display_get_compositor(struct display *display)
2000{
2001 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002002}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002003
2004EGLDisplay
2005display_get_egl_display(struct display *d)
2006{
2007 return d->dpy;
2008}
2009
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002010EGLConfig
2011display_get_egl_config(struct display *d)
2012{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002013 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002014}
2015
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002016struct wl_shell *
2017display_get_shell(struct display *display)
2018{
2019 return display->shell;
2020}
2021
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002022void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002023display_acquire_window_surface(struct display *display,
2024 struct window *window,
2025 EGLContext ctx)
2026{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002027#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002028 struct egl_window_surface_data *data;
2029
2030 if (!window->cairo_surface)
2031 return;
2032
2033 if (!ctx)
2034 ctx = display->ctx;
2035
2036 data = cairo_surface_get_user_data(window->cairo_surface,
2037 &surface_data_key);
2038
2039 cairo_device_acquire(display->device);
2040 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2041 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke22d54812011-07-16 19:50:32 +00002042#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002043}
2044
2045void
2046display_release(struct display *display)
2047{
2048 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
2049 fprintf(stderr, "failed to make context current\n");
2050 cairo_device_release(display->device);
2051}
2052
2053void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002054display_run(struct display *d)
2055{
2056 g_main_loop_run(d->loop);
2057}