blob: 6e7ef9b2e4c7018bd34507a4e9a733b96c2ca02e [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øgsberg297c6312011-02-04 14:11:33 -050061 struct wl_egl_display *native_dpy;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040067 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLDisplay dpy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +010069 EGLConfig conf;
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øgsbergb46df052011-01-18 09:17:57 -050082 display_global_handler_t global_handler;
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050083
84 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
85 PFNEGLCREATEIMAGEKHRPROC create_image;
86 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050087};
88
89struct window {
90 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050091 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050093 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050094 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050095 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040096 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040097 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050098 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050099 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500100 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400102 struct input *grab_device;
103 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400105 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500106
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400107 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500108 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500109
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500110 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400111 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500112 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400113 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500114 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400115 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400116
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500117 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119};
120
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400121struct input {
122 struct display *display;
123 struct wl_input_device *input_device;
124 struct window *pointer_focus;
125 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500126 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400127 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 uint32_t modifiers;
129 int32_t x, y, sx, sy;
130 struct wl_list link;
131};
132
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400133enum {
134 POINTER_DEFAULT = 100,
135 POINTER_UNSET
136};
137
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138enum window_location {
139 WINDOW_INTERIOR = 0,
140 WINDOW_RESIZING_TOP = 1,
141 WINDOW_RESIZING_BOTTOM = 2,
142 WINDOW_RESIZING_LEFT = 4,
143 WINDOW_RESIZING_TOP_LEFT = 5,
144 WINDOW_RESIZING_BOTTOM_LEFT = 6,
145 WINDOW_RESIZING_RIGHT = 8,
146 WINDOW_RESIZING_TOP_RIGHT = 9,
147 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
148 WINDOW_RESIZING_MASK = 15,
149 WINDOW_EXTERIOR = 16,
150 WINDOW_TITLEBAR = 17,
151 WINDOW_CLIENT_AREA = 18,
152};
153
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400154const char *option_xkb_layout = "us";
155const char *option_xkb_variant = "";
156const char *option_xkb_options = "";
157
158static const GOptionEntry xkb_option_entries[] = {
159 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
160 &option_xkb_layout, "XKB Layout" },
161 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
162 &option_xkb_variant, "XKB Variant" },
163 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
164 &option_xkb_options, "XKB Options" },
165 { NULL }
166};
167
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500168static void
169rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
170{
171 cairo_move_to(cr, x0, y0 + radius);
172 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
173 cairo_line_to(cr, x1 - radius, y0);
174 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
175 cairo_line_to(cr, x1, y1 - radius);
176 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
177 cairo_line_to(cr, x0 + radius, y1);
178 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
179 cairo_close_path(cr);
180}
181
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400182static const cairo_user_data_key_t surface_data_key;
183struct surface_data {
184 struct wl_buffer *buffer;
185};
186
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500187#define MULT(_d,c,a,t) \
188 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
189
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500190#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100192struct egl_window_surface_data {
193 struct display *display;
194 struct wl_surface *surface;
195 struct wl_egl_window *window;
196 EGLSurface surf;
197};
198
199static void
200egl_window_surface_data_destroy(void *p)
201{
202 struct egl_window_surface_data *data = p;
203 struct display *d = data->display;
204
205 eglDestroySurface(d->dpy, data->surf);
206 wl_egl_window_destroy(data->window);
207 data->surface = NULL;
208
209 free(p);
210}
211
212static cairo_surface_t *
213display_create_egl_window_surface(struct display *display,
214 struct wl_surface *surface,
215 struct rectangle *rectangle)
216{
217 cairo_surface_t *cairo_surface;
218 struct egl_window_surface_data *data;
219 struct wl_visual *visual;
220
221 data = malloc(sizeof *data);
222 if (data == NULL)
223 return NULL;
224
225 data->display = display;
226 data->surface = surface;
227
228 visual = wl_display_get_premultiplied_argb_visual(display->display);
229
230 data->window = wl_egl_window_create(display->native_dpy,
231 surface,
232 rectangle->width,
233 rectangle->height,
234 visual);
235
236 data->surf = eglCreateWindowSurface(display->dpy, display->conf,
237 data->window, NULL);
238
239 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
240 data->surf,
241 rectangle->width,
242 rectangle->height);
243
244 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
245 data, egl_window_surface_data_destroy);
246
247 return cairo_surface;
248}
249
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500250struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251 struct surface_data data;
252 EGLImageKHR image;
253 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800254 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500255 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400256};
257
258static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500259egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400260{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500261 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800262 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400263
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800264 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400265 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800266 cairo_device_release(d->device);
267
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500268 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400269 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500270 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500271 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272}
273
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500274EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275display_get_image_for_egl_image_surface(struct display *display,
276 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500277{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500278 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500279
280 data = cairo_surface_get_user_data (surface, &surface_data_key);
281
282 return data->image;
283}
284
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500285static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500286display_create_egl_image_surface(struct display *display,
287 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400288{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500289 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290 EGLDisplay dpy = display->dpy;
291 cairo_surface_t *surface;
292 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400293
294 data = malloc(sizeof *data);
295 if (data == NULL)
296 return NULL;
297
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800298 data->display = display;
299
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300 visual = wl_display_get_premultiplied_argb_visual(display->display);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500301 data->pixmap =wl_egl_pixmap_create(display->native_dpy,
302 rectangle->width,
303 rectangle->height,
304 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500305 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000306 free(data);
307 return NULL;
308 }
309
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500310 data->image = display->create_image(dpy, NULL,
311 EGL_NATIVE_PIXMAP_KHR,
312 (EGLClientBuffer) data->pixmap,
313 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500314 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500315 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 free(data);
317 return NULL;
318 }
319
320 data->data.buffer =
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500321 wl_egl_pixmap_create_buffer(display->native_dpy, data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500326 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 surface = cairo_gl_surface_create_for_texture(display->device,
330 CAIRO_CONTENT_COLOR_ALPHA,
331 data->texture,
332 rectangle->width,
333 rectangle->height);
334
335 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337
338 return surface;
339}
340
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500341static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342display_create_egl_image_surface_from_file(struct display *display,
343 const char *filename,
344 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345{
346 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400347 GdkPixbuf *pixbuf;
348 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400349 int stride, i;
350 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400352
353 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400355 FALSE, &error);
356 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400358
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400359 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
360 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500361 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400362 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400363 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400364
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400365
366 stride = gdk_pixbuf_get_rowstride(pixbuf);
367 pixels = gdk_pixbuf_get_pixels(pixbuf);
368
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400370 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400371 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372 while (p < end) {
373 unsigned int t;
374
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400375 MULT(p[0], p[0], p[3], t);
376 MULT(p[1], p[1], p[3], t);
377 MULT(p[2], p[2], p[3], t);
378 p += 4;
379
380 }
381 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400382
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 surface = display_create_egl_image_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000384 if (surface == NULL) {
385 g_object_unref(pixbuf);
386 return NULL;
387 }
388
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800389 data = cairo_surface_get_user_data(surface, &surface_data_key);
390
391 cairo_device_acquire(display->device);
392 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800393 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
394 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800395 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400396
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500397 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400398
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 return surface;
400}
401
402#endif
403
404struct wl_buffer *
405display_get_buffer_for_surface(struct display *display,
406 cairo_surface_t *surface)
407{
408 struct surface_data *data;
409
410 data = cairo_surface_get_user_data (surface, &surface_data_key);
411
412 return data->buffer;
413}
414
415struct shm_surface_data {
416 struct surface_data data;
417 void *map;
418 size_t length;
419};
420
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500421static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422shm_surface_data_destroy(void *p)
423{
424 struct shm_surface_data *data = p;
425
426 wl_buffer_destroy(data->data.buffer);
427 munmap(data->map, data->length);
428}
429
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500430static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431display_create_shm_surface(struct display *display,
432 struct rectangle *rectangle)
433{
434 struct shm_surface_data *data;
435 cairo_surface_t *surface;
436 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800437 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 char filename[] = "/tmp/wayland-shm-XXXXXX";
439
440 data = malloc(sizeof *data);
441 if (data == NULL)
442 return NULL;
443
444 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
445 rectangle->width);
446 data->length = stride * rectangle->height;
447 fd = mkstemp(filename);
448 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000449 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400450 return NULL;
451 }
452 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000453 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454 close(fd);
455 return NULL;
456 }
457
458 data->map = mmap(NULL, data->length,
459 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
460 unlink(filename);
461
462 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000463 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464 close(fd);
465 return NULL;
466 }
467
468 surface = cairo_image_surface_create_for_data (data->map,
469 CAIRO_FORMAT_ARGB32,
470 rectangle->width,
471 rectangle->height,
472 stride);
473
474 cairo_surface_set_user_data (surface, &surface_data_key,
475 data, shm_surface_data_destroy);
476
477 visual = wl_display_get_premultiplied_argb_visual(display->display);
478 data->data.buffer = wl_shm_create_buffer(display->shm,
479 fd,
480 rectangle->width,
481 rectangle->height,
482 stride, visual);
483
484 close(fd);
485
486 return surface;
487}
488
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500489static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490display_create_shm_surface_from_file(struct display *display,
491 const char *filename,
492 struct rectangle *rect)
493{
494 cairo_surface_t *surface;
495 GdkPixbuf *pixbuf;
496 GError *error = NULL;
497 int stride, i;
498 unsigned char *pixels, *p, *end, *dest_data;
499 int dest_stride;
500 uint32_t *d;
501
502 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
503 rect->width, rect->height,
504 FALSE, &error);
505 if (error != NULL)
506 return NULL;
507
508 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
509 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500510 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511 return NULL;
512 }
513
514 stride = gdk_pixbuf_get_rowstride(pixbuf);
515 pixels = gdk_pixbuf_get_pixels(pixbuf);
516
517 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000518 if (surface == NULL) {
519 g_object_unref(pixbuf);
520 return NULL;
521 }
522
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523 dest_data = cairo_image_surface_get_data (surface);
524 dest_stride = cairo_image_surface_get_stride (surface);
525
526 for (i = 0; i < rect->height; i++) {
527 d = (uint32_t *) (dest_data + i * dest_stride);
528 p = pixels + i * stride;
529 end = p + rect->width * 4;
530 while (p < end) {
531 unsigned int t;
532 unsigned char a, r, g, b;
533
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534 a = p[3];
535 MULT(r, p[0], a, t);
536 MULT(g, p[1], a, t);
537 MULT(b, p[2], a, t);
538 p += 4;
539 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
540 }
541 }
542
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500543 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
545 return surface;
546}
547
nobled7b87cb02011-02-01 18:51:47 +0000548static int
549check_size(struct rectangle *rect)
550{
551 if (rect->width && rect->height)
552 return 0;
553
554 fprintf(stderr, "tried to create surface of "
555 "width: %d, height: %d\n", rect->width, rect->height);
556 return -1;
557}
558
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559cairo_surface_t *
560display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100561 struct wl_surface *surface,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562 struct rectangle *rectangle)
563{
nobled7b87cb02011-02-01 18:51:47 +0000564 if (check_size(rectangle) < 0)
565 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500566#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500567 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100568 if (surface)
569 return display_create_egl_window_surface(display,
570 surface,
571 rectangle);
572 else
573 return display_create_egl_image_surface(display,
574 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500575 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500577 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578}
579
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500580static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581display_create_surface_from_file(struct display *display,
582 const char *filename,
583 struct rectangle *rectangle)
584{
nobled7b87cb02011-02-01 18:51:47 +0000585 if (check_size(rectangle) < 0)
586 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500587#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500588 if (display->dpy) {
589 return display_create_egl_image_surface_from_file(display,
590 filename,
591 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500592 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500594 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400595}
Yuval Fledel45568f62010-12-06 09:18:12 -0500596 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400597 const char *filename;
598 int hotspot_x, hotspot_y;
599} pointer_images[] = {
600 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
601 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
602 { DATADIR "/wayland/bottom_side.png", 16, 20 },
603 { DATADIR "/wayland/grabbing.png", 20, 17 },
604 { DATADIR "/wayland/left_ptr.png", 10, 5 },
605 { DATADIR "/wayland/left_side.png", 10, 20 },
606 { DATADIR "/wayland/right_side.png", 30, 19 },
607 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
608 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
609 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400610 { DATADIR "/wayland/xterm.png", 15, 15 },
611 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400612};
613
614static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400615create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400616{
617 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400619 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400620
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400621 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400622 display->pointer_surfaces =
623 malloc(count * sizeof *display->pointer_surfaces);
624 rect.width = width;
625 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400626 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400627 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628 display_create_surface_from_file(display,
629 pointer_images[i].filename,
630 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400631 }
632
633}
634
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400635cairo_surface_t *
636display_get_pointer_surface(struct display *display, int pointer,
637 int *width, int *height,
638 int *hotspot_x, int *hotspot_y)
639{
640 cairo_surface_t *surface;
641
642 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500643#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400644 *width = cairo_gl_surface_get_width(surface);
645 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000646#else
647 *width = cairo_image_surface_get_width(surface);
648 *height = cairo_image_surface_get_height(surface);
649#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400650 *hotspot_x = pointer_images[pointer].hotspot_x;
651 *hotspot_y = pointer_images[pointer].hotspot_y;
652
653 return cairo_surface_reference(surface);
654}
655
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400656
657static void
658window_attach_surface(struct window *window);
659
660static void
661free_surface(void *data)
662{
663 struct window *window = data;
664
665 cairo_surface_destroy(window->pending_surface);
666 window->pending_surface = NULL;
667 if (window->cairo_surface)
668 window_attach_surface(window);
669}
670
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500671static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500672window_attach_surface(struct window *window)
673{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400674 struct display *display = window->display;
675 struct wl_buffer *buffer;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676 struct egl_window_surface_data *data;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500677 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678 int width = window->allocation.width;
679 int height = window->allocation.height;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500680
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500681 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100682 x = window->server_allocation.width - width;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500683 else
684 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500685
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500686 if (window->resize_edges & WINDOW_RESIZING_TOP)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687 y = window->server_allocation.height - height;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500688 else
689 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400690
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500691 window->resize_edges = 0;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100692
693 switch (window->buffer_type) {
694 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
695 data = cairo_surface_get_user_data(window->cairo_surface,
696 &surface_data_key);
697
698 wl_egl_window_resize(data->window, width, height, x, y);
699 cairo_gl_surface_swapbuffers(window->cairo_surface);
700 wl_egl_window_get_attached_size(data->window,
701 &window->server_allocation.width,
702 &window->server_allocation.height);
703 break;
704 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
705 case WINDOW_BUFFER_TYPE_SHM:
706 if (window->pending_surface != NULL)
707 return;
708
709 window->pending_surface = window->cairo_surface;
710 window->cairo_surface = NULL;
711
712 buffer =
713 display_get_buffer_for_surface(display,
714 window->pending_surface);
715
716 wl_surface_attach(window->surface, buffer, x, y);
717 window->server_allocation = window->allocation;
718 wl_display_sync_callback(display->display, free_surface,
719 window);
720 break;
721 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500722
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500723 if (window->fullscreen)
724 wl_surface_map_fullscreen(window->surface);
725 else if (!window->parent)
726 wl_surface_map_toplevel(window->surface);
727 else
728 wl_surface_map_transient(window->surface,
729 window->parent->surface,
730 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500731
732 wl_surface_damage(window->surface, 0, 0,
733 window->allocation.width,
734 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500735}
736
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500737void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400738window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500739{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400740 if (window->cairo_surface)
741 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500742}
743
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400744void
745window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400746{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500747 cairo_surface_reference(surface);
748
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400749 if (window->cairo_surface != NULL)
750 cairo_surface_destroy(window->cairo_surface);
751
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500752 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400753}
754
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755static void
756window_resize_cairo_window_surface(struct window *window)
757{
758 struct egl_window_surface_data *data;
759
760 data = cairo_surface_get_user_data(window->cairo_surface,
761 &surface_data_key);
762
763 wl_egl_window_resize(data->window,
764 window->allocation.width,
765 window->allocation.height, 0, 0);
766 cairo_gl_surface_set_size(window->cairo_surface,
767 window->allocation.width,
768 window->allocation.height);
769}
770
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771void
772window_create_surface(struct window *window)
773{
774 cairo_surface_t *surface;
775
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400776 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500777#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
779 if (window->cairo_surface) {
780 window_resize_cairo_window_surface(window);
781 return;
782 }
783 surface = display_create_surface(window->display,
784 window->surface,
785 &window->allocation);
786 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500787 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400788 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 NULL,
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400790 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791 break;
792#endif
793 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400794 surface = display_create_shm_surface(window->display,
795 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800797 default:
798 surface = NULL;
799 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400800 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400801
802 window_set_surface(window, surface);
803 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804}
805
806static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500807window_draw_menu(struct window *window)
808{
809 cairo_t *cr;
810 int width, height, r = 5;
811
812 window_create_surface(window);
813
814 cr = cairo_create(window->cairo_surface);
815 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
816 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
817 cairo_paint(cr);
818
819 width = window->allocation.width;
820 height = window->allocation.height;
821 rounded_rect(cr, r, r, width - r, height - r, r);
822 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
823 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
824 cairo_fill(cr);
825 cairo_destroy(cr);
826}
827
828static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500829window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500830{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500831 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500832 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400833 cairo_surface_t *frame;
834 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500835
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400838 width = window->allocation.width;
839 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500840
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500841 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500842
Kristian Høgsberg09531622010-06-14 23:22:15 -0400843 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400844 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400845 cairo_paint(cr);
846
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400847 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400848 tile_mask(cr, window->display->shadow,
849 shadow_dx, shadow_dy, width, height,
850 window->margin + 10 - shadow_dx,
851 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500852
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400853 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400854 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400855 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400856 frame = window->display->inactive_frame;
857
858 tile_source(cr, frame, 0, 0, width, height,
859 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500860
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500861 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
862 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500863 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400864 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400865 if (window->keyboard_device)
866 cairo_set_source_rgb(cr, 0, 0, 0);
867 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400868 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400869 cairo_show_text(cr, window->title);
870
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500871 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400872
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100873 /* FIXME: this breakes gears, fix cairo? */
874#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400875 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100876#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500877}
878
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400879void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500880window_destroy(struct window *window)
881{
882 wl_surface_destroy(window->surface);
883 wl_list_remove(&window->link);
884 free(window);
885}
886
887void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400888display_flush_cairo_device(struct display *display)
889{
890 cairo_device_flush (display->device);
891}
892
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500893void
894window_draw(struct window *window)
895{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500896 if (window->parent)
897 window_draw_menu(window);
898 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400899 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500900 else
901 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500902}
903
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400904cairo_surface_t *
905window_get_surface(struct window *window)
906{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400907 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400908}
909
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400910static int
911get_pointer_location(struct window *window, int32_t x, int32_t y)
912{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400913 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400914 const int grip_size = 8;
915
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500916 if (!window->decoration)
917 return WINDOW_CLIENT_AREA;
918
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400919 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400920 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400921 else if (window->margin <= x && x < window->margin + grip_size)
922 hlocation = WINDOW_RESIZING_LEFT;
923 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400924 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400925 else if (x < window->allocation.width - window->margin)
926 hlocation = WINDOW_RESIZING_RIGHT;
927 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400928 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400929
930 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400931 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400932 else if (window->margin <= y && y < window->margin + grip_size)
933 vlocation = WINDOW_RESIZING_TOP;
934 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400935 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400936 else if (y < window->allocation.height - window->margin)
937 vlocation = WINDOW_RESIZING_BOTTOM;
938 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400939 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400940
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 location = vlocation | hlocation;
942 if (location & WINDOW_EXTERIOR)
943 location = WINDOW_EXTERIOR;
944 if (location == WINDOW_INTERIOR && y < window->margin + 50)
945 location = WINDOW_TITLEBAR;
946 else if (location == WINDOW_INTERIOR)
947 location = WINDOW_CLIENT_AREA;
948
949 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400950}
951
952static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400953set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400954{
955 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400956 struct wl_buffer *buffer;
957 cairo_surface_t *surface;
958 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400959
960 location = get_pointer_location(input->pointer_focus,
961 input->sx, input->sy);
962 switch (location) {
963 case WINDOW_RESIZING_TOP:
964 pointer = POINTER_TOP;
965 break;
966 case WINDOW_RESIZING_BOTTOM:
967 pointer = POINTER_BOTTOM;
968 break;
969 case WINDOW_RESIZING_LEFT:
970 pointer = POINTER_LEFT;
971 break;
972 case WINDOW_RESIZING_RIGHT:
973 pointer = POINTER_RIGHT;
974 break;
975 case WINDOW_RESIZING_TOP_LEFT:
976 pointer = POINTER_TOP_LEFT;
977 break;
978 case WINDOW_RESIZING_TOP_RIGHT:
979 pointer = POINTER_TOP_RIGHT;
980 break;
981 case WINDOW_RESIZING_BOTTOM_LEFT:
982 pointer = POINTER_BOTTOM_LEFT;
983 break;
984 case WINDOW_RESIZING_BOTTOM_RIGHT:
985 pointer = POINTER_BOTTOM_RIGHT;
986 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 case WINDOW_EXTERIOR:
988 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400989 if (input->current_pointer_image == POINTER_DEFAULT)
990 return;
991
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400992 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400993 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400994 return;
995 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400996 break;
997 }
998
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400999 if (pointer == input->current_pointer_image)
1000 return;
1001
1002 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001003 surface = display->pointer_surfaces[pointer];
1004 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001005 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001006 pointer_images[pointer].hotspot_x,
1007 pointer_images[pointer].hotspot_y);
1008}
1009
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001010static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001011window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001012 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001013 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001014{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001015 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001016 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001017 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001018
1019 input->x = x;
1020 input->y = y;
1021 input->sx = sx;
1022 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001023
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001024 if (window->motion_handler)
1025 pointer = (*window->motion_handler)(window, input, time,
1026 x, y, sx, sy,
1027 window->user_data);
1028
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001029 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001030}
1031
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001032static void
1033window_handle_button(void *data,
1034 struct wl_input_device *input_device,
1035 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001036{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001037 struct input *input = data;
1038 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001039 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001040
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001041 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001042
1043 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001044 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001045 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001046 wl_shell_move(window->display->shell,
1047 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001048 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001049 case WINDOW_RESIZING_TOP:
1050 case WINDOW_RESIZING_BOTTOM:
1051 case WINDOW_RESIZING_LEFT:
1052 case WINDOW_RESIZING_RIGHT:
1053 case WINDOW_RESIZING_TOP_LEFT:
1054 case WINDOW_RESIZING_TOP_RIGHT:
1055 case WINDOW_RESIZING_BOTTOM_LEFT:
1056 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001057 wl_shell_resize(window->display->shell,
1058 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001059 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001060 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001061 case WINDOW_CLIENT_AREA:
1062 if (window->button_handler)
1063 (*window->button_handler)(window,
1064 input, time,
1065 button, state,
1066 window->user_data);
1067 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001068 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001069 } else {
1070 if (window->button_handler)
1071 (*window->button_handler)(window,
1072 input, time,
1073 button, state,
1074 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001075 }
1076}
1077
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001078static void
1079window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001080 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001081{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001082 struct input *input = data;
1083 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001084 struct display *d = window->display;
1085 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001086
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001087 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001088 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001089 return;
1090
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001091 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001092 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001093 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1094 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001095
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001096 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1097
1098 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001099 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001100 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001101 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001102
1103 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001104 (*window->key_handler)(window, input, time, key, sym, state,
1105 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001106}
1107
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001108static void
1109window_handle_pointer_focus(void *data,
1110 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001111 uint32_t time, struct wl_surface *surface,
1112 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001113{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001114 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001115 struct window *window;
1116 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001117
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001118 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001119 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001120 window = input->pointer_focus;
1121
Kristian Høgsberg59826582011-01-20 11:56:57 -05001122 input->x = x;
1123 input->y = y;
1124 input->sx = sx;
1125 input->sy = sy;
1126
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001127 pointer = POINTER_LEFT_PTR;
1128 if (window->motion_handler)
1129 pointer = (*window->motion_handler)(window,
1130 input, time,
1131 x, y, sx, sy,
1132 window->user_data);
1133
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001134 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001135 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001136 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001137 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001138 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001139}
1140
1141static void
1142window_handle_keyboard_focus(void *data,
1143 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001144 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001145 struct wl_surface *surface,
1146 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001147{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001148 struct input *input = data;
1149 struct window *window = input->keyboard_focus;
1150 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001151 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001152
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001153 window = input->keyboard_focus;
1154 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001155 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001156 if (window->keyboard_focus_handler)
1157 (*window->keyboard_focus_handler)(window, NULL,
1158 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001159 }
1160
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001161 if (surface)
1162 input->keyboard_focus = wl_surface_get_user_data(surface);
1163 else
1164 input->keyboard_focus = NULL;
1165
1166 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001167 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001168 for (k = keys->data; k < end; k++)
1169 input->modifiers |= d->xkb->map->modmap[*k];
1170
1171 window = input->keyboard_focus;
1172 if (window) {
1173 window->keyboard_device = input;
1174 if (window->keyboard_focus_handler)
1175 (*window->keyboard_focus_handler)(window,
1176 window->keyboard_device,
1177 window->user_data);
1178 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001179}
1180
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001181static const struct wl_input_device_listener input_device_listener = {
1182 window_handle_motion,
1183 window_handle_button,
1184 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001185 window_handle_pointer_focus,
1186 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001187};
1188
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001189void
1190input_get_position(struct input *input, int32_t *x, int32_t *y)
1191{
1192 *x = input->sx;
1193 *y = input->sy;
1194}
1195
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001196struct wl_input_device *
1197input_get_input_device(struct input *input)
1198{
1199 return input->input_device;
1200}
1201
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001202uint32_t
1203input_get_modifiers(struct input *input)
1204{
1205 return input->modifiers;
1206}
1207
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001208struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001209window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001210{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001211 cairo_device_flush (window->display->device);
1212
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001213 return wl_shell_create_drag(window->display->shell);
1214}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001215
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001216void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001217window_move(struct window *window, struct input *input, uint32_t time)
1218{
1219 wl_shell_move(window->display->shell,
1220 window->surface, input->input_device, time);
1221}
1222
1223void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001224window_activate_drag(struct wl_drag *drag, struct window *window,
1225 struct input *input, uint32_t time)
1226{
1227 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228}
1229
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001230static void
1231handle_configure(void *data, struct wl_shell *shell,
1232 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001233 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001234{
1235 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001236 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001237
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001238 /* FIXME: this is probably the wrong place to check for width
1239 * or height <= 0, but it prevents the compositor from crashing
1240 */
1241 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001242 return;
1243
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001244 window->resize_edges = edges;
1245
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001246 if (window->resize_handler) {
1247 child_width = width - 20 - window->margin * 2;
1248 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001249
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001250 (*window->resize_handler)(window,
1251 child_width, child_height,
1252 window->user_data);
1253 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001254 window->allocation.width = width;
1255 window->allocation.height = height;
1256
1257 if (window->redraw_handler)
1258 window_schedule_redraw(window);
1259 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001260}
1261
1262static const struct wl_shell_listener shell_listener = {
1263 handle_configure,
1264};
1265
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001266void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001267window_get_allocation(struct window *window,
1268 struct rectangle *allocation)
1269{
1270 *allocation = window->allocation;
1271}
1272
1273void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001274window_get_child_allocation(struct window *window,
1275 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001276{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001277 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001278 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001279 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001280 allocation->x = window->margin + 10;
1281 allocation->y = window->margin + 50;
1282 allocation->width =
1283 window->allocation.width - 20 - window->margin * 2;
1284 allocation->height =
1285 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001286 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001287}
1288
1289void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001290window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001291{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001292 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001293 window->allocation.x = 20 + window->margin;
1294 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001295 window->allocation.width = width + 20 + window->margin * 2;
1296 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001297 } else {
1298 window->allocation.x = 0;
1299 window->allocation.y = 0;
1300 window->allocation.width = width;
1301 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001302 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001303}
1304
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001305static gboolean
1306idle_redraw(void *data)
1307{
1308 struct window *window = data;
1309
1310 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001311
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001312 window->redraw_scheduled = 0;
1313
1314 return FALSE;
1315}
1316
1317void
1318window_schedule_redraw(struct window *window)
1319{
1320 if (!window->redraw_scheduled) {
1321 g_idle_add(idle_redraw, window);
1322 window->redraw_scheduled = 1;
1323 }
1324}
1325
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001326void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001327window_set_fullscreen(struct window *window, int fullscreen)
1328{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001329 int32_t width, height;
1330
1331 if (window->fullscreen == fullscreen)
1332 return;
1333
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001334 window->fullscreen = fullscreen;
1335 if (window->fullscreen) {
1336 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001337 width = window->display->screen_allocation.width;
1338 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001339 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001340 width = window->saved_allocation.width - 20 - window->margin * 2;
1341 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001342 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001343
1344 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001345}
1346
1347void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001348window_set_decoration(struct window *window, int decoration)
1349{
1350 window->decoration = decoration;
1351}
1352
1353void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001354window_set_user_data(struct window *window, void *data)
1355{
1356 window->user_data = data;
1357}
1358
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001359void *
1360window_get_user_data(struct window *window)
1361{
1362 return window->user_data;
1363}
1364
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001365void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001366window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001367 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001368{
1369 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001370}
1371
1372void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001373window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001374 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001375{
1376 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001377}
1378
1379void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001380window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001381 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001382{
1383 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001384}
1385
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001386void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001387window_set_button_handler(struct window *window,
1388 window_button_handler_t handler)
1389{
1390 window->button_handler = handler;
1391}
1392
1393void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001394window_set_motion_handler(struct window *window,
1395 window_motion_handler_t handler)
1396{
1397 window->motion_handler = handler;
1398}
1399
1400void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001401window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001402 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001403{
1404 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001405}
1406
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001407void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001408window_set_title(struct window *window, const char *title)
1409{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001410 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001411 window->title = strdup(title);
1412}
1413
1414const char *
1415window_get_title(struct window *window)
1416{
1417 return window->title;
1418}
1419
1420void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001421window_damage(struct window *window, int32_t x, int32_t y,
1422 int32_t width, int32_t height)
1423{
1424 wl_surface_damage(window->surface, x, y, width, height);
1425}
1426
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001427static struct window *
1428window_create_internal(struct display *display, struct window *parent,
1429 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001430{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001431 struct window *window;
1432
1433 window = malloc(sizeof *window);
1434 if (window == NULL)
1435 return NULL;
1436
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001437 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001438 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001439 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001440 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001441 window->allocation.x = 0;
1442 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001443 window->allocation.width = width;
1444 window->allocation.height = height;
1445 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001446 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001447 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001448
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001449 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001450 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1451 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001452 else
1453 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001454
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001455 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001456 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001457
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001458 return window;
1459}
1460
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001461struct window *
1462window_create(struct display *display, int32_t width, int32_t height)
1463{
1464 struct window *window;
1465
1466 window = window_create_internal(display, NULL, width, height);
1467 if (!window)
1468 return NULL;
1469
1470 return window;
1471}
1472
1473struct window *
1474window_create_transient(struct display *display, struct window *parent,
1475 int32_t x, int32_t y, int32_t width, int32_t height)
1476{
1477 struct window *window;
1478
1479 window = window_create_internal(parent->display,
1480 parent, width, height);
1481 if (!window)
1482 return NULL;
1483
1484 window->x = x;
1485 window->y = y;
1486
1487 return window;
1488}
1489
1490void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001491window_set_buffer_type(struct window *window, enum window_buffer_type type)
1492{
1493 window->buffer_type = type;
1494}
1495
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001496static void
1497display_handle_geometry(void *data,
1498 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001499 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001500{
1501 struct display *display = data;
1502
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001503 display->screen_allocation.x = x;
1504 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001505 display->screen_allocation.width = width;
1506 display->screen_allocation.height = height;
1507}
1508
1509static const struct wl_output_listener output_listener = {
1510 display_handle_geometry,
1511};
1512
1513static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001514display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001515{
1516 struct input *input;
1517
1518 input = malloc(sizeof *input);
1519 if (input == NULL)
1520 return;
1521
1522 memset(input, 0, sizeof *input);
1523 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001524 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001525 input->pointer_focus = NULL;
1526 input->keyboard_focus = NULL;
1527 wl_list_insert(d->input_list.prev, &input->link);
1528
1529 wl_input_device_add_listener(input->input_device,
1530 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001531 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001532}
1533
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001534struct selection_offer {
1535 struct display *display;
1536 struct wl_selection_offer *offer;
1537 struct wl_array types;
1538 struct input *input;
1539};
1540
1541int
1542input_offers_mime_type(struct input *input, const char *type)
1543{
1544 struct selection_offer *offer = input->offer;
1545 char **p, **end;
1546
1547 if (offer == NULL)
1548 return 0;
1549
1550 end = offer->types.data + offer->types.size;
1551 for (p = offer->types.data; p < end; p++)
1552 if (strcmp(*p, type) == 0)
1553 return 1;
1554
1555 return 0;
1556}
1557
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001558void
1559input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001560{
1561 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001562
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001563 /* FIXME: A number of things can go wrong here: the object may
1564 * not be the current selection offer any more (which could
1565 * still work, but the source may have gone away or just
1566 * destroyed its wl_selection) or the offer may not have the
1567 * requested type after all (programmer/client error,
1568 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001569 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001570}
1571
1572static void
1573selection_offer_offer(void *data,
1574 struct wl_selection_offer *selection_offer,
1575 const char *type)
1576{
1577 struct selection_offer *offer = data;
1578
1579 char **p;
1580
1581 p = wl_array_add(&offer->types, sizeof *p);
1582 if (p)
1583 *p = strdup(type);
1584};
1585
1586static void
1587selection_offer_keyboard_focus(void *data,
1588 struct wl_selection_offer *selection_offer,
1589 struct wl_input_device *input_device)
1590{
1591 struct selection_offer *offer = data;
1592 struct input *input;
1593 char **p, **end;
1594
1595 if (input_device == NULL) {
1596 printf("selection offer retracted %p\n", selection_offer);
1597 input = offer->input;
1598 input->offer = NULL;
1599 wl_selection_offer_destroy(selection_offer);
1600 wl_array_release(&offer->types);
1601 free(offer);
1602 return;
1603 }
1604
1605 input = wl_input_device_get_user_data(input_device);
1606 printf("new selection offer %p:", selection_offer);
1607
1608 offer->input = input;
1609 input->offer = offer;
1610 end = offer->types.data + offer->types.size;
1611 for (p = offer->types.data; p < end; p++)
1612 printf(" %s", *p);
1613
1614 printf("\n");
1615}
1616
1617struct wl_selection_offer_listener selection_offer_listener = {
1618 selection_offer_offer,
1619 selection_offer_keyboard_focus
1620};
1621
1622static void
1623add_selection_offer(struct display *d, uint32_t id)
1624{
1625 struct selection_offer *offer;
1626
1627 offer = malloc(sizeof *offer);
1628 if (offer == NULL)
1629 return;
1630
1631 offer->offer = wl_selection_offer_create(d->display, id);
1632 offer->display = d;
1633 wl_array_init(&offer->types);
1634 offer->input = NULL;
1635
1636 wl_selection_offer_add_listener(offer->offer,
1637 &selection_offer_listener, offer);
1638}
1639
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001640static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001641display_handle_global(struct wl_display *display, uint32_t id,
1642 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001643{
1644 struct display *d = data;
1645
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001646 if (strcmp(interface, "compositor") == 0) {
1647 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001648 } else if (strcmp(interface, "output") == 0) {
1649 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001650 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001651 } else if (strcmp(interface, "input_device") == 0) {
1652 display_add_input(d, id);
1653 } else if (strcmp(interface, "shell") == 0) {
1654 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001655 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001656 } else if (strcmp(interface, "shm") == 0) {
1657 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001658 } else if (strcmp(interface, "selection_offer") == 0) {
1659 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001660 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001661 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001662 }
1663}
1664
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001665static void
1666display_render_frame(struct display *d)
1667{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001668 int radius = 8;
1669 cairo_t *cr;
1670
1671 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1672 cr = cairo_create(d->shadow);
1673 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1674 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1675 rounded_rect(cr, 16, 16, 112, 112, radius);
1676 cairo_fill(cr);
1677 cairo_destroy(cr);
1678 blur_surface(d->shadow, 64);
1679
1680 d->active_frame =
1681 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1682 cr = cairo_create(d->active_frame);
1683 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1684 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1685 rounded_rect(cr, 16, 16, 112, 112, radius);
1686 cairo_fill(cr);
1687 cairo_destroy(cr);
1688
1689 d->inactive_frame =
1690 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1691 cr = cairo_create(d->inactive_frame);
1692 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1693 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1694 rounded_rect(cr, 16, 16, 112, 112, radius);
1695 cairo_fill(cr);
1696 cairo_destroy(cr);
1697}
1698
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001699static void
1700init_xkb(struct display *d)
1701{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001702 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001703
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001704 names.rules = "evdev";
1705 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001706 names.layout = option_xkb_layout;
1707 names.variant = option_xkb_variant;
1708 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001709
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001710 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001711 if (!d->xkb) {
1712 fprintf(stderr, "Failed to compile keymap\n");
1713 exit(1);
1714 }
1715}
1716
Yuval Fledel45568f62010-12-06 09:18:12 -05001717static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001718init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001719{
1720 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001721 EGLint n;
1722 static const EGLint cfg_attribs[] = {
1723 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1724 EGL_RED_SIZE, 1,
1725 EGL_GREEN_SIZE, 1,
1726 EGL_BLUE_SIZE, 1,
1727 EGL_ALPHA_SIZE, 1,
1728 EGL_DEPTH_SIZE, 1,
1729 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1730 EGL_NONE
1731 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001732
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -05001733 d->dpy = eglGetDisplay(d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001734 if (!eglInitialize(d->dpy, &major, &minor)) {
1735 fprintf(stderr, "failed to initialize display\n");
1736 return -1;
1737 }
1738
1739 if (!eglBindAPI(EGL_OPENGL_API)) {
1740 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1741 return -1;
1742 }
1743
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001744 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1745 fprintf(stderr, "failed to choose config\n");
1746 return -1;
1747 }
1748
1749 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001750 if (d->ctx == NULL) {
1751 fprintf(stderr, "failed to create context\n");
1752 return -1;
1753 }
1754
1755 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001756 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001757 return -1;
1758 }
1759
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001760#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001761 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1762 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001763 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001764 return -1;
1765 }
1766#endif
1767
1768 return 0;
1769}
1770
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001771struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001772display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001773{
1774 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001775 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001776 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001777 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001778
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001779 g_type_init();
1780
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001781 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001782 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001783 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001784
1785 xkb_option_group = g_option_group_new("xkb",
1786 "Keyboard options",
1787 "Show all XKB options",
1788 NULL, NULL);
1789 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1790 g_option_context_add_group (context, xkb_option_group);
1791
1792 if (!g_option_context_parse(context, argc, argv, &error)) {
1793 fprintf(stderr, "option parsing failed: %s\n", error->message);
1794 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001795 }
1796
Tim Wiederhake748f6722011-01-23 23:25:25 +01001797 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001798
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001799 d = malloc(sizeof *d);
1800 if (d == NULL)
1801 return NULL;
1802
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001803 memset(d, 0, sizeof *d);
1804
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001805 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001806 if (d->display == NULL) {
1807 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001808 return NULL;
1809 }
1810
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001811 wl_list_init(&d->input_list);
1812
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001813 /* Set up listener so we'll catch all events. */
1814 wl_display_add_global_listener(d->display,
1815 display_handle_global, d);
1816
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -05001817 d->native_dpy = wl_egl_display_create(d->display);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001818
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001819 /* Process connection events. */
1820 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1821
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001822 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001823 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001824
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001825 d->image_target_texture_2d =
1826 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1827 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1828 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1829
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001830 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001831
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001832 display_render_frame(d);
1833
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001834 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001835 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001836 g_source_attach(d->source, NULL);
1837
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001838 wl_list_init(&d->window_list);
1839
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001840 init_xkb(d);
1841
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001842 return d;
1843}
1844
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001845struct wl_display *
1846display_get_display(struct display *display)
1847{
1848 return display->display;
1849}
1850
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001851struct wl_compositor *
1852display_get_compositor(struct display *display)
1853{
1854 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001855}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001856
1857EGLDisplay
1858display_get_egl_display(struct display *d)
1859{
1860 return d->dpy;
1861}
1862
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001863EGLConfig
1864display_get_egl_config(struct display *d)
1865{
1866 return d->conf;
1867}
1868
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001869struct wl_shell *
1870display_get_shell(struct display *display)
1871{
1872 return display->shell;
1873}
1874
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001875void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001876display_acquire_window_surface(struct display *display,
1877 struct window *window,
1878 EGLContext ctx)
1879{
1880 struct egl_window_surface_data *data;
1881
1882 if (!window->cairo_surface)
1883 return;
1884
1885 if (!ctx)
1886 ctx = display->ctx;
1887
1888 data = cairo_surface_get_user_data(window->cairo_surface,
1889 &surface_data_key);
1890
1891 cairo_device_acquire(display->device);
1892 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
1893 fprintf(stderr, "failed to make surface current\n");
1894}
1895
1896void
1897display_release(struct display *display)
1898{
1899 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
1900 fprintf(stderr, "failed to make context current\n");
1901 cairo_device_release(display->device);
1902}
1903
1904void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001905display_run(struct display *d)
1906{
1907 g_main_loop_run(d->loop);
1908}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001909
1910void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001911display_set_global_handler(struct display *display,
1912 display_global_handler_t handler)
1913{
1914 display->global_handler = handler;
1915}