blob: ec1714e9892f4a78f37755f17a99c907a6d0073d [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;
69 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010070 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040072 GMainLoop *loop;
73 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040074 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040075 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040076 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040077 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040078 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040079 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040080
Kristian Høgsbergb46df052011-01-18 09:17:57 -050081 display_global_handler_t global_handler;
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050082
83 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
84 PFNEGLCREATEIMAGEKHRPROC create_image;
85 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050086};
87
88struct window {
89 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050090 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050092 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050093 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050094 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040095 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040096 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050097 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050098 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050099 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400101 struct input *grab_device;
102 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500103 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400104 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500105
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400106 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500107 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500108
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500109 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400110 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500111 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400112 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500113 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400114 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400115
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500116 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400117 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500118};
119
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400120struct input {
121 struct display *display;
122 struct wl_input_device *input_device;
123 struct window *pointer_focus;
124 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500125 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400126 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400127 uint32_t modifiers;
128 int32_t x, y, sx, sy;
129 struct wl_list link;
130};
131
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400132enum {
133 POINTER_DEFAULT = 100,
134 POINTER_UNSET
135};
136
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137enum window_location {
138 WINDOW_INTERIOR = 0,
139 WINDOW_RESIZING_TOP = 1,
140 WINDOW_RESIZING_BOTTOM = 2,
141 WINDOW_RESIZING_LEFT = 4,
142 WINDOW_RESIZING_TOP_LEFT = 5,
143 WINDOW_RESIZING_BOTTOM_LEFT = 6,
144 WINDOW_RESIZING_RIGHT = 8,
145 WINDOW_RESIZING_TOP_RIGHT = 9,
146 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
147 WINDOW_RESIZING_MASK = 15,
148 WINDOW_EXTERIOR = 16,
149 WINDOW_TITLEBAR = 17,
150 WINDOW_CLIENT_AREA = 18,
151};
152
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400153const char *option_xkb_layout = "us";
154const char *option_xkb_variant = "";
155const char *option_xkb_options = "";
156
157static const GOptionEntry xkb_option_entries[] = {
158 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_layout, "XKB Layout" },
160 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_variant, "XKB Variant" },
162 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
163 &option_xkb_options, "XKB Options" },
164 { NULL }
165};
166
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500167static void
168rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
169{
170 cairo_move_to(cr, x0, y0 + radius);
171 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
172 cairo_line_to(cr, x1 - radius, y0);
173 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
174 cairo_line_to(cr, x1, y1 - radius);
175 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
176 cairo_line_to(cr, x0 + radius, y1);
177 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
178 cairo_close_path(cr);
179}
180
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400181static const cairo_user_data_key_t surface_data_key;
182struct surface_data {
183 struct wl_buffer *buffer;
184};
185
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500186#define MULT(_d,c,a,t) \
187 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
188
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500189#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400190
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500191struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400192 struct surface_data data;
193 EGLImageKHR image;
194 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800195 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500196 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400197};
198
199static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500200egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400201{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500202 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800203 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400204
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800205 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400206 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800207 cairo_device_release(d->device);
208
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500209 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400210 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500211 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500212 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400213}
214
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500215EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500216display_get_image_for_egl_image_surface(struct display *display,
217 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500218{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500219 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500220
221 data = cairo_surface_get_user_data (surface, &surface_data_key);
222
223 return data->image;
224}
225
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500226static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500227display_create_egl_image_surface(struct display *display,
228 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400229{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500230 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400231 EGLDisplay dpy = display->dpy;
232 cairo_surface_t *surface;
233 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400234
235 data = malloc(sizeof *data);
236 if (data == NULL)
237 return NULL;
238
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800239 data->display = display;
240
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500241 visual = wl_display_get_premultiplied_argb_visual(display->display);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500242 data->pixmap =wl_egl_pixmap_create(display->native_dpy,
243 rectangle->width,
244 rectangle->height,
245 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500246 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000247 free(data);
248 return NULL;
249 }
250
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500251 data->image = display->create_image(dpy, NULL,
252 EGL_NATIVE_PIXMAP_KHR,
253 (EGLClientBuffer) data->pixmap,
254 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500255 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500256 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500257 free(data);
258 return NULL;
259 }
260
261 data->data.buffer =
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500262 wl_egl_pixmap_create_buffer(display->native_dpy, data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500263
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800264 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400265 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400266 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500267 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800268 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400269
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400270 surface = cairo_gl_surface_create_for_texture(display->device,
271 CAIRO_CONTENT_COLOR_ALPHA,
272 data->texture,
273 rectangle->width,
274 rectangle->height);
275
276 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500277 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400278
279 return surface;
280}
281
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500282static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500283display_create_egl_image_surface_from_file(struct display *display,
284 const char *filename,
285 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400286{
287 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400288 GdkPixbuf *pixbuf;
289 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400290 int stride, i;
291 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500292 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400293
294 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400295 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400296 FALSE, &error);
297 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400298 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400299
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400300 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
301 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500302 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400303 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400304 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400305
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400306
307 stride = gdk_pixbuf_get_rowstride(pixbuf);
308 pixels = gdk_pixbuf_get_pixels(pixbuf);
309
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400311 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400313 while (p < end) {
314 unsigned int t;
315
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400316 MULT(p[0], p[0], p[3], t);
317 MULT(p[1], p[1], p[3], t);
318 MULT(p[2], p[2], p[3], t);
319 p += 4;
320
321 }
322 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324 surface = display_create_egl_image_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000325 if (surface == NULL) {
326 g_object_unref(pixbuf);
327 return NULL;
328 }
329
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330 data = cairo_surface_get_user_data(surface, &surface_data_key);
331
332 cairo_device_acquire(display->device);
333 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800334 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
335 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800336 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400337
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500338 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400339
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340 return surface;
341}
342
343#endif
344
345struct wl_buffer *
346display_get_buffer_for_surface(struct display *display,
347 cairo_surface_t *surface)
348{
349 struct surface_data *data;
350
351 data = cairo_surface_get_user_data (surface, &surface_data_key);
352
353 return data->buffer;
354}
355
356struct shm_surface_data {
357 struct surface_data data;
358 void *map;
359 size_t length;
360};
361
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500362static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400363shm_surface_data_destroy(void *p)
364{
365 struct shm_surface_data *data = p;
366
367 wl_buffer_destroy(data->data.buffer);
368 munmap(data->map, data->length);
369}
370
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500371static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400372display_create_shm_surface(struct display *display,
373 struct rectangle *rectangle)
374{
375 struct shm_surface_data *data;
376 cairo_surface_t *surface;
377 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800378 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400379 char filename[] = "/tmp/wayland-shm-XXXXXX";
380
381 data = malloc(sizeof *data);
382 if (data == NULL)
383 return NULL;
384
385 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
386 rectangle->width);
387 data->length = stride * rectangle->height;
388 fd = mkstemp(filename);
389 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000390 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 return NULL;
392 }
393 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000394 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395 close(fd);
396 return NULL;
397 }
398
399 data->map = mmap(NULL, data->length,
400 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
401 unlink(filename);
402
403 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000404 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405 close(fd);
406 return NULL;
407 }
408
409 surface = cairo_image_surface_create_for_data (data->map,
410 CAIRO_FORMAT_ARGB32,
411 rectangle->width,
412 rectangle->height,
413 stride);
414
415 cairo_surface_set_user_data (surface, &surface_data_key,
416 data, shm_surface_data_destroy);
417
418 visual = wl_display_get_premultiplied_argb_visual(display->display);
419 data->data.buffer = wl_shm_create_buffer(display->shm,
420 fd,
421 rectangle->width,
422 rectangle->height,
423 stride, visual);
424
425 close(fd);
426
427 return surface;
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_from_file(struct display *display,
432 const char *filename,
433 struct rectangle *rect)
434{
435 cairo_surface_t *surface;
436 GdkPixbuf *pixbuf;
437 GError *error = NULL;
438 int stride, i;
439 unsigned char *pixels, *p, *end, *dest_data;
440 int dest_stride;
441 uint32_t *d;
442
443 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
444 rect->width, rect->height,
445 FALSE, &error);
446 if (error != NULL)
447 return NULL;
448
449 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
450 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500451 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400452 return NULL;
453 }
454
455 stride = gdk_pixbuf_get_rowstride(pixbuf);
456 pixels = gdk_pixbuf_get_pixels(pixbuf);
457
458 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000459 if (surface == NULL) {
460 g_object_unref(pixbuf);
461 return NULL;
462 }
463
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464 dest_data = cairo_image_surface_get_data (surface);
465 dest_stride = cairo_image_surface_get_stride (surface);
466
467 for (i = 0; i < rect->height; i++) {
468 d = (uint32_t *) (dest_data + i * dest_stride);
469 p = pixels + i * stride;
470 end = p + rect->width * 4;
471 while (p < end) {
472 unsigned int t;
473 unsigned char a, r, g, b;
474
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400475 a = p[3];
476 MULT(r, p[0], a, t);
477 MULT(g, p[1], a, t);
478 MULT(b, p[2], a, t);
479 p += 4;
480 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
481 }
482 }
483
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500484 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485
486 return surface;
487}
488
nobled7b87cb02011-02-01 18:51:47 +0000489static int
490check_size(struct rectangle *rect)
491{
492 if (rect->width && rect->height)
493 return 0;
494
495 fprintf(stderr, "tried to create surface of "
496 "width: %d, height: %d\n", rect->width, rect->height);
497 return -1;
498}
499
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500cairo_surface_t *
501display_create_surface(struct display *display,
502 struct rectangle *rectangle)
503{
nobled7b87cb02011-02-01 18:51:47 +0000504 if (check_size(rectangle) < 0)
505 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500506#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500507 if (display->dpy) {
508 return display_create_egl_image_surface(display, rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500509 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500511 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400512}
513
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500514static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400515display_create_surface_from_file(struct display *display,
516 const char *filename,
517 struct rectangle *rectangle)
518{
nobled7b87cb02011-02-01 18:51:47 +0000519 if (check_size(rectangle) < 0)
520 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500521#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500522 if (display->dpy) {
523 return display_create_egl_image_surface_from_file(display,
524 filename,
525 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500526 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500528 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400529}
Yuval Fledel45568f62010-12-06 09:18:12 -0500530 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400531 const char *filename;
532 int hotspot_x, hotspot_y;
533} pointer_images[] = {
534 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
535 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
536 { DATADIR "/wayland/bottom_side.png", 16, 20 },
537 { DATADIR "/wayland/grabbing.png", 20, 17 },
538 { DATADIR "/wayland/left_ptr.png", 10, 5 },
539 { DATADIR "/wayland/left_side.png", 10, 20 },
540 { DATADIR "/wayland/right_side.png", 30, 19 },
541 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
542 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
543 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400544 { DATADIR "/wayland/xterm.png", 15, 15 },
545 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400546};
547
548static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400549create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400550{
551 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400552 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400553 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400554
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400555 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400556 display->pointer_surfaces =
557 malloc(count * sizeof *display->pointer_surfaces);
558 rect.width = width;
559 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400560 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400561 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562 display_create_surface_from_file(display,
563 pointer_images[i].filename,
564 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400565 }
566
567}
568
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400569cairo_surface_t *
570display_get_pointer_surface(struct display *display, int pointer,
571 int *width, int *height,
572 int *hotspot_x, int *hotspot_y)
573{
574 cairo_surface_t *surface;
575
576 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500577#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400578 *width = cairo_gl_surface_get_width(surface);
579 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000580#else
581 *width = cairo_image_surface_get_width(surface);
582 *height = cairo_image_surface_get_height(surface);
583#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400584 *hotspot_x = pointer_images[pointer].hotspot_x;
585 *hotspot_y = pointer_images[pointer].hotspot_y;
586
587 return cairo_surface_reference(surface);
588}
589
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400590
591static void
592window_attach_surface(struct window *window);
593
594static void
595free_surface(void *data)
596{
597 struct window *window = data;
598
599 cairo_surface_destroy(window->pending_surface);
600 window->pending_surface = NULL;
601 if (window->cairo_surface)
602 window_attach_surface(window);
603}
604
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500605static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500606window_attach_surface(struct window *window)
607{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400608 struct display *display = window->display;
609 struct wl_buffer *buffer;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500610 int32_t x, y;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500611
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500612 if (window->pending_surface != NULL)
613 return;
614
Kristian Høgsberg09531622010-06-14 23:22:15 -0400615 window->pending_surface = window->cairo_surface;
616 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500617
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400618 buffer = display_get_buffer_for_surface(display,
619 window->pending_surface);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500620 if (window->resize_edges & WINDOW_RESIZING_LEFT)
621 x = window->server_allocation.width -
622 window->allocation.width;
623 else
624 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500625
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500626 if (window->resize_edges & WINDOW_RESIZING_TOP)
627 y = window->server_allocation.height -
628 window->allocation.height;
629 else
630 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400631
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500632 window->server_allocation = window->allocation;
633 window->resize_edges = 0;
634 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400635 wl_display_sync_callback(display->display, free_surface, window);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500636
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500637 if (window->fullscreen)
638 wl_surface_map_fullscreen(window->surface);
639 else if (!window->parent)
640 wl_surface_map_toplevel(window->surface);
641 else
642 wl_surface_map_transient(window->surface,
643 window->parent->surface,
644 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500645
646 wl_surface_damage(window->surface, 0, 0,
647 window->allocation.width,
648 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500649}
650
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500651void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400652window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500653{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400654 if (window->cairo_surface)
655 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500656}
657
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400658void
659window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400660{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500661 cairo_surface_reference(surface);
662
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400663 if (window->cairo_surface != NULL)
664 cairo_surface_destroy(window->cairo_surface);
665
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500666 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400667}
668
669void
670window_create_surface(struct window *window)
671{
672 cairo_surface_t *surface;
673
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500675#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500676 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400677 surface = display_create_surface(window->display,
678 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400679 break;
680#endif
681 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400682 surface = display_create_shm_surface(window->display,
683 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800685 default:
686 surface = NULL;
687 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400688 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400689
690 window_set_surface(window, surface);
691 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692}
693
694static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500695window_draw_menu(struct window *window)
696{
697 cairo_t *cr;
698 int width, height, r = 5;
699
700 window_create_surface(window);
701
702 cr = cairo_create(window->cairo_surface);
703 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
704 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
705 cairo_paint(cr);
706
707 width = window->allocation.width;
708 height = window->allocation.height;
709 rounded_rect(cr, r, r, width - r, height - r, r);
710 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
711 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
712 cairo_fill(cr);
713 cairo_destroy(cr);
714}
715
716static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500717window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500718{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500719 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500720 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400721 cairo_surface_t *frame;
722 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500723
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400724 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400725
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400726 width = window->allocation.width;
727 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500728
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500729 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500730
Kristian Høgsberg09531622010-06-14 23:22:15 -0400731 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400732 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400733 cairo_paint(cr);
734
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400735 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400736 tile_mask(cr, window->display->shadow,
737 shadow_dx, shadow_dy, width, height,
738 window->margin + 10 - shadow_dx,
739 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500740
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400741 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400743 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400744 frame = window->display->inactive_frame;
745
746 tile_source(cr, frame, 0, 0, width, height,
747 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500748
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500749 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
750 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500751 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400752 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400753 if (window->keyboard_device)
754 cairo_set_source_rgb(cr, 0, 0, 0);
755 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400756 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400757 cairo_show_text(cr, window->title);
758
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500759 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400760
761 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500762}
763
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400764void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500765window_destroy(struct window *window)
766{
767 wl_surface_destroy(window->surface);
768 wl_list_remove(&window->link);
769 free(window);
770}
771
772void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400773display_flush_cairo_device(struct display *display)
774{
775 cairo_device_flush (display->device);
776}
777
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500778void
779window_draw(struct window *window)
780{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500781 if (window->parent)
782 window_draw_menu(window);
783 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400784 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500785 else
786 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500787}
788
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400789cairo_surface_t *
790window_get_surface(struct window *window)
791{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400792 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400793}
794
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100795struct wl_surface *
796window_get_wl_surface(struct window *window)
797{
798 return window->surface;
799}
800
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400801static int
802get_pointer_location(struct window *window, int32_t x, int32_t y)
803{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400804 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400805 const int grip_size = 8;
806
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500807 if (!window->decoration)
808 return WINDOW_CLIENT_AREA;
809
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400810 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400811 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400812 else if (window->margin <= x && x < window->margin + grip_size)
813 hlocation = WINDOW_RESIZING_LEFT;
814 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400815 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400816 else if (x < window->allocation.width - window->margin)
817 hlocation = WINDOW_RESIZING_RIGHT;
818 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400819 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400820
821 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400822 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400823 else if (window->margin <= y && y < window->margin + grip_size)
824 vlocation = WINDOW_RESIZING_TOP;
825 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400826 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400827 else if (y < window->allocation.height - window->margin)
828 vlocation = WINDOW_RESIZING_BOTTOM;
829 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400830 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400831
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400832 location = vlocation | hlocation;
833 if (location & WINDOW_EXTERIOR)
834 location = WINDOW_EXTERIOR;
835 if (location == WINDOW_INTERIOR && y < window->margin + 50)
836 location = WINDOW_TITLEBAR;
837 else if (location == WINDOW_INTERIOR)
838 location = WINDOW_CLIENT_AREA;
839
840 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400841}
842
843static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400844set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400845{
846 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400847 struct wl_buffer *buffer;
848 cairo_surface_t *surface;
849 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400850
851 location = get_pointer_location(input->pointer_focus,
852 input->sx, input->sy);
853 switch (location) {
854 case WINDOW_RESIZING_TOP:
855 pointer = POINTER_TOP;
856 break;
857 case WINDOW_RESIZING_BOTTOM:
858 pointer = POINTER_BOTTOM;
859 break;
860 case WINDOW_RESIZING_LEFT:
861 pointer = POINTER_LEFT;
862 break;
863 case WINDOW_RESIZING_RIGHT:
864 pointer = POINTER_RIGHT;
865 break;
866 case WINDOW_RESIZING_TOP_LEFT:
867 pointer = POINTER_TOP_LEFT;
868 break;
869 case WINDOW_RESIZING_TOP_RIGHT:
870 pointer = POINTER_TOP_RIGHT;
871 break;
872 case WINDOW_RESIZING_BOTTOM_LEFT:
873 pointer = POINTER_BOTTOM_LEFT;
874 break;
875 case WINDOW_RESIZING_BOTTOM_RIGHT:
876 pointer = POINTER_BOTTOM_RIGHT;
877 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400878 case WINDOW_EXTERIOR:
879 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400880 if (input->current_pointer_image == POINTER_DEFAULT)
881 return;
882
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400883 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400884 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400885 return;
886 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400887 break;
888 }
889
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400890 if (pointer == input->current_pointer_image)
891 return;
892
893 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400894 surface = display->pointer_surfaces[pointer];
895 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400896 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400897 pointer_images[pointer].hotspot_x,
898 pointer_images[pointer].hotspot_y);
899}
900
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500901static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500902window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400903 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500904 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500905{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400906 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400907 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500908 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400909
910 input->x = x;
911 input->y = y;
912 input->sx = sx;
913 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400914
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400915 if (window->motion_handler)
916 pointer = (*window->motion_handler)(window, input, time,
917 x, y, sx, sy,
918 window->user_data);
919
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400920 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500921}
922
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400923static void
924window_handle_button(void *data,
925 struct wl_input_device *input_device,
926 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500927{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400928 struct input *input = data;
929 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400930 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400931
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400932 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500933
934 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400935 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400936 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400937 wl_shell_move(window->display->shell,
938 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500939 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400940 case WINDOW_RESIZING_TOP:
941 case WINDOW_RESIZING_BOTTOM:
942 case WINDOW_RESIZING_LEFT:
943 case WINDOW_RESIZING_RIGHT:
944 case WINDOW_RESIZING_TOP_LEFT:
945 case WINDOW_RESIZING_TOP_RIGHT:
946 case WINDOW_RESIZING_BOTTOM_LEFT:
947 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400948 wl_shell_resize(window->display->shell,
949 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400950 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500951 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400952 case WINDOW_CLIENT_AREA:
953 if (window->button_handler)
954 (*window->button_handler)(window,
955 input, time,
956 button, state,
957 window->user_data);
958 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500959 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400960 } else {
961 if (window->button_handler)
962 (*window->button_handler)(window,
963 input, time,
964 button, state,
965 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500966 }
967}
968
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500969static void
970window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400971 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500972{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400973 struct input *input = data;
974 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400975 struct display *d = window->display;
976 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500977
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400978 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400979 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500980 return;
981
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400982 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500983 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400984 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
985 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500986
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400987 sym = XkbKeySymEntry(d->xkb, code, level, 0);
988
989 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400990 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400991 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400992 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500993
994 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500995 (*window->key_handler)(window, input, time, key, sym, state,
996 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500997}
998
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500999static void
1000window_handle_pointer_focus(void *data,
1001 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001002 uint32_t time, struct wl_surface *surface,
1003 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001004{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001005 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001006 struct window *window;
1007 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001008
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001009 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001010 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001011 window = input->pointer_focus;
1012
Kristian Høgsberg59826582011-01-20 11:56:57 -05001013 input->x = x;
1014 input->y = y;
1015 input->sx = sx;
1016 input->sy = sy;
1017
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001018 pointer = POINTER_LEFT_PTR;
1019 if (window->motion_handler)
1020 pointer = (*window->motion_handler)(window,
1021 input, time,
1022 x, y, sx, sy,
1023 window->user_data);
1024
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001025 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001026 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001027 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001028 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001029 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001030}
1031
1032static void
1033window_handle_keyboard_focus(void *data,
1034 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001035 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001036 struct wl_surface *surface,
1037 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001038{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001039 struct input *input = data;
1040 struct window *window = input->keyboard_focus;
1041 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001042 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001043
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001044 window = input->keyboard_focus;
1045 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001046 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001047 if (window->keyboard_focus_handler)
1048 (*window->keyboard_focus_handler)(window, NULL,
1049 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001050 }
1051
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001052 if (surface)
1053 input->keyboard_focus = wl_surface_get_user_data(surface);
1054 else
1055 input->keyboard_focus = NULL;
1056
1057 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001058 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001059 for (k = keys->data; k < end; k++)
1060 input->modifiers |= d->xkb->map->modmap[*k];
1061
1062 window = input->keyboard_focus;
1063 if (window) {
1064 window->keyboard_device = input;
1065 if (window->keyboard_focus_handler)
1066 (*window->keyboard_focus_handler)(window,
1067 window->keyboard_device,
1068 window->user_data);
1069 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001070}
1071
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001072static const struct wl_input_device_listener input_device_listener = {
1073 window_handle_motion,
1074 window_handle_button,
1075 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001076 window_handle_pointer_focus,
1077 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001078};
1079
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001080void
1081input_get_position(struct input *input, int32_t *x, int32_t *y)
1082{
1083 *x = input->sx;
1084 *y = input->sy;
1085}
1086
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001087struct wl_input_device *
1088input_get_input_device(struct input *input)
1089{
1090 return input->input_device;
1091}
1092
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001093uint32_t
1094input_get_modifiers(struct input *input)
1095{
1096 return input->modifiers;
1097}
1098
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001099struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001100window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001101{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001102 cairo_device_flush (window->display->device);
1103
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001104 return wl_shell_create_drag(window->display->shell);
1105}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001106
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001107void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001108window_move(struct window *window, struct input *input, uint32_t time)
1109{
1110 wl_shell_move(window->display->shell,
1111 window->surface, input->input_device, time);
1112}
1113
1114void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001115window_activate_drag(struct wl_drag *drag, struct window *window,
1116 struct input *input, uint32_t time)
1117{
1118 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001119}
1120
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001121static void
1122handle_configure(void *data, struct wl_shell *shell,
1123 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001124 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001125{
1126 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001127 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001128
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001129 /* FIXME: this is probably the wrong place to check for width
1130 * or height <= 0, but it prevents the compositor from crashing
1131 */
1132 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001133 return;
1134
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001135 window->resize_edges = edges;
1136
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001137 if (window->resize_handler) {
1138 child_width = width - 20 - window->margin * 2;
1139 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001140
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001141 (*window->resize_handler)(window,
1142 child_width, child_height,
1143 window->user_data);
1144 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001145 window->allocation.width = width;
1146 window->allocation.height = height;
1147
1148 if (window->redraw_handler)
1149 window_schedule_redraw(window);
1150 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001151}
1152
1153static const struct wl_shell_listener shell_listener = {
1154 handle_configure,
1155};
1156
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001157void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001158window_get_child_allocation(struct window *window,
1159 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001160{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001161 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001162 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001163 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001164 allocation->x = window->margin + 10;
1165 allocation->y = window->margin + 50;
1166 allocation->width =
1167 window->allocation.width - 20 - window->margin * 2;
1168 allocation->height =
1169 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001170 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001171}
1172
1173void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001174window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001175{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001176 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001177 window->allocation.x = 20 + window->margin;
1178 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001179 window->allocation.width = width + 20 + window->margin * 2;
1180 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001181 } else {
1182 window->allocation.x = 0;
1183 window->allocation.y = 0;
1184 window->allocation.width = width;
1185 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001186 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001187}
1188
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001189static gboolean
1190idle_redraw(void *data)
1191{
1192 struct window *window = data;
1193
1194 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001195
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001196 window->redraw_scheduled = 0;
1197
1198 return FALSE;
1199}
1200
1201void
1202window_schedule_redraw(struct window *window)
1203{
1204 if (!window->redraw_scheduled) {
1205 g_idle_add(idle_redraw, window);
1206 window->redraw_scheduled = 1;
1207 }
1208}
1209
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001210void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001211window_set_fullscreen(struct window *window, int fullscreen)
1212{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001213 int32_t width, height;
1214
1215 if (window->fullscreen == fullscreen)
1216 return;
1217
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001218 window->fullscreen = fullscreen;
1219 if (window->fullscreen) {
1220 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001221 width = window->display->screen_allocation.width;
1222 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001223 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001224 width = window->saved_allocation.width - 20 - window->margin * 2;
1225 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001226 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001227
1228 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001229}
1230
1231void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001232window_set_decoration(struct window *window, int decoration)
1233{
1234 window->decoration = decoration;
1235}
1236
1237void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001238window_set_user_data(struct window *window, void *data)
1239{
1240 window->user_data = data;
1241}
1242
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001243void *
1244window_get_user_data(struct window *window)
1245{
1246 return window->user_data;
1247}
1248
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001249void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001250window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001251 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001252{
1253 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001254}
1255
1256void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001257window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001258 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001259{
1260 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001261}
1262
1263void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001264window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001265 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001266{
1267 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001268}
1269
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001270void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001271window_set_button_handler(struct window *window,
1272 window_button_handler_t handler)
1273{
1274 window->button_handler = handler;
1275}
1276
1277void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001278window_set_motion_handler(struct window *window,
1279 window_motion_handler_t handler)
1280{
1281 window->motion_handler = handler;
1282}
1283
1284void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001285window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001286 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001287{
1288 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001289}
1290
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001291void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001292window_set_title(struct window *window, const char *title)
1293{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001294 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001295 window->title = strdup(title);
1296}
1297
1298const char *
1299window_get_title(struct window *window)
1300{
1301 return window->title;
1302}
1303
1304void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001305window_damage(struct window *window, int32_t x, int32_t y,
1306 int32_t width, int32_t height)
1307{
1308 wl_surface_damage(window->surface, x, y, width, height);
1309}
1310
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001311static struct window *
1312window_create_internal(struct display *display, struct window *parent,
1313 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001314{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001315 struct window *window;
1316
1317 window = malloc(sizeof *window);
1318 if (window == NULL)
1319 return NULL;
1320
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001321 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001322 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001323 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001324 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001325 window->allocation.x = 0;
1326 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001327 window->allocation.width = width;
1328 window->allocation.height = height;
1329 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001330 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001331 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001332
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001333 if (display->dpy)
1334 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_IMAGE;
Yuval Fledel45568f62010-12-06 09:18:12 -05001335 else
1336 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001337
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001338 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001339 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001340
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001341 return window;
1342}
1343
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001344struct window *
1345window_create(struct display *display, int32_t width, int32_t height)
1346{
1347 struct window *window;
1348
1349 window = window_create_internal(display, NULL, width, height);
1350 if (!window)
1351 return NULL;
1352
1353 return window;
1354}
1355
1356struct window *
1357window_create_transient(struct display *display, struct window *parent,
1358 int32_t x, int32_t y, int32_t width, int32_t height)
1359{
1360 struct window *window;
1361
1362 window = window_create_internal(parent->display,
1363 parent, width, height);
1364 if (!window)
1365 return NULL;
1366
1367 window->x = x;
1368 window->y = y;
1369
1370 return window;
1371}
1372
1373void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001374window_set_buffer_type(struct window *window, enum window_buffer_type type)
1375{
1376 window->buffer_type = type;
1377}
1378
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001379static void
1380display_handle_geometry(void *data,
1381 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001382 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001383{
1384 struct display *display = data;
1385
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001386 display->screen_allocation.x = x;
1387 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001388 display->screen_allocation.width = width;
1389 display->screen_allocation.height = height;
1390}
1391
1392static const struct wl_output_listener output_listener = {
1393 display_handle_geometry,
1394};
1395
1396static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001397display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001398{
1399 struct input *input;
1400
1401 input = malloc(sizeof *input);
1402 if (input == NULL)
1403 return;
1404
1405 memset(input, 0, sizeof *input);
1406 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001407 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001408 input->pointer_focus = NULL;
1409 input->keyboard_focus = NULL;
1410 wl_list_insert(d->input_list.prev, &input->link);
1411
1412 wl_input_device_add_listener(input->input_device,
1413 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001414 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001415}
1416
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001417struct selection_offer {
1418 struct display *display;
1419 struct wl_selection_offer *offer;
1420 struct wl_array types;
1421 struct input *input;
1422};
1423
1424int
1425input_offers_mime_type(struct input *input, const char *type)
1426{
1427 struct selection_offer *offer = input->offer;
1428 char **p, **end;
1429
1430 if (offer == NULL)
1431 return 0;
1432
1433 end = offer->types.data + offer->types.size;
1434 for (p = offer->types.data; p < end; p++)
1435 if (strcmp(*p, type) == 0)
1436 return 1;
1437
1438 return 0;
1439}
1440
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001441void
1442input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001443{
1444 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001445
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001446 /* FIXME: A number of things can go wrong here: the object may
1447 * not be the current selection offer any more (which could
1448 * still work, but the source may have gone away or just
1449 * destroyed its wl_selection) or the offer may not have the
1450 * requested type after all (programmer/client error,
1451 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001452 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001453}
1454
1455static void
1456selection_offer_offer(void *data,
1457 struct wl_selection_offer *selection_offer,
1458 const char *type)
1459{
1460 struct selection_offer *offer = data;
1461
1462 char **p;
1463
1464 p = wl_array_add(&offer->types, sizeof *p);
1465 if (p)
1466 *p = strdup(type);
1467};
1468
1469static void
1470selection_offer_keyboard_focus(void *data,
1471 struct wl_selection_offer *selection_offer,
1472 struct wl_input_device *input_device)
1473{
1474 struct selection_offer *offer = data;
1475 struct input *input;
1476 char **p, **end;
1477
1478 if (input_device == NULL) {
1479 printf("selection offer retracted %p\n", selection_offer);
1480 input = offer->input;
1481 input->offer = NULL;
1482 wl_selection_offer_destroy(selection_offer);
1483 wl_array_release(&offer->types);
1484 free(offer);
1485 return;
1486 }
1487
1488 input = wl_input_device_get_user_data(input_device);
1489 printf("new selection offer %p:", selection_offer);
1490
1491 offer->input = input;
1492 input->offer = offer;
1493 end = offer->types.data + offer->types.size;
1494 for (p = offer->types.data; p < end; p++)
1495 printf(" %s", *p);
1496
1497 printf("\n");
1498}
1499
1500struct wl_selection_offer_listener selection_offer_listener = {
1501 selection_offer_offer,
1502 selection_offer_keyboard_focus
1503};
1504
1505static void
1506add_selection_offer(struct display *d, uint32_t id)
1507{
1508 struct selection_offer *offer;
1509
1510 offer = malloc(sizeof *offer);
1511 if (offer == NULL)
1512 return;
1513
1514 offer->offer = wl_selection_offer_create(d->display, id);
1515 offer->display = d;
1516 wl_array_init(&offer->types);
1517 offer->input = NULL;
1518
1519 wl_selection_offer_add_listener(offer->offer,
1520 &selection_offer_listener, offer);
1521}
1522
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001523static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001524display_handle_global(struct wl_display *display, uint32_t id,
1525 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001526{
1527 struct display *d = data;
1528
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001529 if (strcmp(interface, "compositor") == 0) {
1530 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001531 } else if (strcmp(interface, "output") == 0) {
1532 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001533 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001534 } else if (strcmp(interface, "input_device") == 0) {
1535 display_add_input(d, id);
1536 } else if (strcmp(interface, "shell") == 0) {
1537 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001538 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001539 } else if (strcmp(interface, "shm") == 0) {
1540 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001541 } else if (strcmp(interface, "selection_offer") == 0) {
1542 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001543 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001544 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001545 }
1546}
1547
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001548static void
1549display_render_frame(struct display *d)
1550{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001551 int radius = 8;
1552 cairo_t *cr;
1553
1554 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1555 cr = cairo_create(d->shadow);
1556 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1557 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1558 rounded_rect(cr, 16, 16, 112, 112, radius);
1559 cairo_fill(cr);
1560 cairo_destroy(cr);
1561 blur_surface(d->shadow, 64);
1562
1563 d->active_frame =
1564 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1565 cr = cairo_create(d->active_frame);
1566 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1567 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1568 rounded_rect(cr, 16, 16, 112, 112, radius);
1569 cairo_fill(cr);
1570 cairo_destroy(cr);
1571
1572 d->inactive_frame =
1573 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1574 cr = cairo_create(d->inactive_frame);
1575 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1576 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1577 rounded_rect(cr, 16, 16, 112, 112, radius);
1578 cairo_fill(cr);
1579 cairo_destroy(cr);
1580}
1581
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001582static void
1583init_xkb(struct display *d)
1584{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001585 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001586
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001587 names.rules = "evdev";
1588 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001589 names.layout = option_xkb_layout;
1590 names.variant = option_xkb_variant;
1591 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001592
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001593 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001594 if (!d->xkb) {
1595 fprintf(stderr, "Failed to compile keymap\n");
1596 exit(1);
1597 }
1598}
1599
Yuval Fledel45568f62010-12-06 09:18:12 -05001600static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001601init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001602{
1603 EGLint major, minor;
Yuval Fledel45568f62010-12-06 09:18:12 -05001604
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -05001605 d->dpy = eglGetDisplay(d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001606 if (!eglInitialize(d->dpy, &major, &minor)) {
1607 fprintf(stderr, "failed to initialize display\n");
1608 return -1;
1609 }
1610
1611 if (!eglBindAPI(EGL_OPENGL_API)) {
1612 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1613 return -1;
1614 }
1615
1616 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1617 if (d->ctx == NULL) {
1618 fprintf(stderr, "failed to create context\n");
1619 return -1;
1620 }
1621
1622 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001623 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001624 return -1;
1625 }
1626
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001627#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001628 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1629 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001630 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001631 return -1;
1632 }
1633#endif
1634
1635 return 0;
1636}
1637
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001638struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001639display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001640{
1641 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001642 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001643 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001644 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001645
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001646 g_type_init();
1647
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001648 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001649 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001650 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001651
1652 xkb_option_group = g_option_group_new("xkb",
1653 "Keyboard options",
1654 "Show all XKB options",
1655 NULL, NULL);
1656 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1657 g_option_context_add_group (context, xkb_option_group);
1658
1659 if (!g_option_context_parse(context, argc, argv, &error)) {
1660 fprintf(stderr, "option parsing failed: %s\n", error->message);
1661 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001662 }
1663
Tim Wiederhake748f6722011-01-23 23:25:25 +01001664 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001665
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001666 d = malloc(sizeof *d);
1667 if (d == NULL)
1668 return NULL;
1669
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001670 memset(d, 0, sizeof *d);
1671
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001672 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001673 if (d->display == NULL) {
1674 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001675 return NULL;
1676 }
1677
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001678 wl_list_init(&d->input_list);
1679
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001680 /* Set up listener so we'll catch all events. */
1681 wl_display_add_global_listener(d->display,
1682 display_handle_global, d);
1683
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -05001684 d->native_dpy = wl_egl_display_create(d->display);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001685
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001686 /* Process connection events. */
1687 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1688
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001689 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001690 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001691
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001692 d->image_target_texture_2d =
1693 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1694 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1695 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1696
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001697 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001698
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001699 display_render_frame(d);
1700
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001701 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001702 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001703 g_source_attach(d->source, NULL);
1704
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001705 wl_list_init(&d->window_list);
1706
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001707 init_xkb(d);
1708
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001709 return d;
1710}
1711
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001712struct wl_display *
1713display_get_display(struct display *display)
1714{
1715 return display->display;
1716}
1717
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001718struct wl_compositor *
1719display_get_compositor(struct display *display)
1720{
1721 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001722}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001723
1724EGLDisplay
1725display_get_egl_display(struct display *d)
1726{
1727 return d->dpy;
1728}
1729
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001730struct wl_shell *
1731display_get_shell(struct display *display)
1732{
1733 return display->shell;
1734}
1735
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001736void
1737display_run(struct display *d)
1738{
1739 g_main_loop_run(d->loop);
1740}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001741
1742void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001743display_set_global_handler(struct display *display,
1744 display_global_handler_t handler)
1745{
1746 display->global_handler = handler;
1747}