blob: 0c93b9cfacbbd7783533d5a55e4eacfcc9e5acb6 [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
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400795static int
796get_pointer_location(struct window *window, int32_t x, int32_t y)
797{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400798 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400799 const int grip_size = 8;
800
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500801 if (!window->decoration)
802 return WINDOW_CLIENT_AREA;
803
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400804 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400805 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400806 else if (window->margin <= x && x < window->margin + grip_size)
807 hlocation = WINDOW_RESIZING_LEFT;
808 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400809 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400810 else if (x < window->allocation.width - window->margin)
811 hlocation = WINDOW_RESIZING_RIGHT;
812 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400813 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400814
815 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400816 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400817 else if (window->margin <= y && y < window->margin + grip_size)
818 vlocation = WINDOW_RESIZING_TOP;
819 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400820 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400821 else if (y < window->allocation.height - window->margin)
822 vlocation = WINDOW_RESIZING_BOTTOM;
823 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400824 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400825
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400826 location = vlocation | hlocation;
827 if (location & WINDOW_EXTERIOR)
828 location = WINDOW_EXTERIOR;
829 if (location == WINDOW_INTERIOR && y < window->margin + 50)
830 location = WINDOW_TITLEBAR;
831 else if (location == WINDOW_INTERIOR)
832 location = WINDOW_CLIENT_AREA;
833
834 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400835}
836
837static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400838set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400839{
840 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400841 struct wl_buffer *buffer;
842 cairo_surface_t *surface;
843 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400844
845 location = get_pointer_location(input->pointer_focus,
846 input->sx, input->sy);
847 switch (location) {
848 case WINDOW_RESIZING_TOP:
849 pointer = POINTER_TOP;
850 break;
851 case WINDOW_RESIZING_BOTTOM:
852 pointer = POINTER_BOTTOM;
853 break;
854 case WINDOW_RESIZING_LEFT:
855 pointer = POINTER_LEFT;
856 break;
857 case WINDOW_RESIZING_RIGHT:
858 pointer = POINTER_RIGHT;
859 break;
860 case WINDOW_RESIZING_TOP_LEFT:
861 pointer = POINTER_TOP_LEFT;
862 break;
863 case WINDOW_RESIZING_TOP_RIGHT:
864 pointer = POINTER_TOP_RIGHT;
865 break;
866 case WINDOW_RESIZING_BOTTOM_LEFT:
867 pointer = POINTER_BOTTOM_LEFT;
868 break;
869 case WINDOW_RESIZING_BOTTOM_RIGHT:
870 pointer = POINTER_BOTTOM_RIGHT;
871 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400872 case WINDOW_EXTERIOR:
873 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400874 if (input->current_pointer_image == POINTER_DEFAULT)
875 return;
876
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400877 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400878 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400879 return;
880 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400881 break;
882 }
883
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400884 if (pointer == input->current_pointer_image)
885 return;
886
887 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400888 surface = display->pointer_surfaces[pointer];
889 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400890 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400891 pointer_images[pointer].hotspot_x,
892 pointer_images[pointer].hotspot_y);
893}
894
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500895static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500896window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400897 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500898 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500899{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400900 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400901 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500902 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400903
904 input->x = x;
905 input->y = y;
906 input->sx = sx;
907 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400908
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400909 if (window->motion_handler)
910 pointer = (*window->motion_handler)(window, input, time,
911 x, y, sx, sy,
912 window->user_data);
913
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400914 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500915}
916
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400917static void
918window_handle_button(void *data,
919 struct wl_input_device *input_device,
920 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500921{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400922 struct input *input = data;
923 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400924 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400925
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400926 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500927
928 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400929 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400931 wl_shell_move(window->display->shell,
932 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500933 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400934 case WINDOW_RESIZING_TOP:
935 case WINDOW_RESIZING_BOTTOM:
936 case WINDOW_RESIZING_LEFT:
937 case WINDOW_RESIZING_RIGHT:
938 case WINDOW_RESIZING_TOP_LEFT:
939 case WINDOW_RESIZING_TOP_RIGHT:
940 case WINDOW_RESIZING_BOTTOM_LEFT:
941 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400942 wl_shell_resize(window->display->shell,
943 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400944 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500945 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400946 case WINDOW_CLIENT_AREA:
947 if (window->button_handler)
948 (*window->button_handler)(window,
949 input, time,
950 button, state,
951 window->user_data);
952 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500953 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400954 } else {
955 if (window->button_handler)
956 (*window->button_handler)(window,
957 input, time,
958 button, state,
959 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500960 }
961}
962
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500963static void
964window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400965 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500966{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400967 struct input *input = data;
968 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400969 struct display *d = window->display;
970 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500971
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400972 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400973 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500974 return;
975
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400976 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500977 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400978 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
979 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500980
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400981 sym = XkbKeySymEntry(d->xkb, code, level, 0);
982
983 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400984 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400985 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400986 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500987
988 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500989 (*window->key_handler)(window, input, time, key, sym, state,
990 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500991}
992
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500993static void
994window_handle_pointer_focus(void *data,
995 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400996 uint32_t time, struct wl_surface *surface,
997 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500998{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001000 struct window *window;
1001 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001002
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001003 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001004 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001005 window = input->pointer_focus;
1006
Kristian Høgsberg59826582011-01-20 11:56:57 -05001007 input->x = x;
1008 input->y = y;
1009 input->sx = sx;
1010 input->sy = sy;
1011
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001012 pointer = POINTER_LEFT_PTR;
1013 if (window->motion_handler)
1014 pointer = (*window->motion_handler)(window,
1015 input, time,
1016 x, y, sx, sy,
1017 window->user_data);
1018
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001019 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001020 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001021 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001022 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001023 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001024}
1025
1026static void
1027window_handle_keyboard_focus(void *data,
1028 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001029 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001030 struct wl_surface *surface,
1031 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001032{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001033 struct input *input = data;
1034 struct window *window = input->keyboard_focus;
1035 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001036 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001037
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001038 window = input->keyboard_focus;
1039 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001040 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001041 if (window->keyboard_focus_handler)
1042 (*window->keyboard_focus_handler)(window, NULL,
1043 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001044 }
1045
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001046 if (surface)
1047 input->keyboard_focus = wl_surface_get_user_data(surface);
1048 else
1049 input->keyboard_focus = NULL;
1050
1051 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001052 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001053 for (k = keys->data; k < end; k++)
1054 input->modifiers |= d->xkb->map->modmap[*k];
1055
1056 window = input->keyboard_focus;
1057 if (window) {
1058 window->keyboard_device = input;
1059 if (window->keyboard_focus_handler)
1060 (*window->keyboard_focus_handler)(window,
1061 window->keyboard_device,
1062 window->user_data);
1063 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001064}
1065
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001066static const struct wl_input_device_listener input_device_listener = {
1067 window_handle_motion,
1068 window_handle_button,
1069 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001070 window_handle_pointer_focus,
1071 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001072};
1073
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001074void
1075input_get_position(struct input *input, int32_t *x, int32_t *y)
1076{
1077 *x = input->sx;
1078 *y = input->sy;
1079}
1080
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001081struct wl_input_device *
1082input_get_input_device(struct input *input)
1083{
1084 return input->input_device;
1085}
1086
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001087uint32_t
1088input_get_modifiers(struct input *input)
1089{
1090 return input->modifiers;
1091}
1092
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001093struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001094window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001095{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001096 cairo_device_flush (window->display->device);
1097
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001098 return wl_shell_create_drag(window->display->shell);
1099}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001100
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001101void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001102window_move(struct window *window, struct input *input, uint32_t time)
1103{
1104 wl_shell_move(window->display->shell,
1105 window->surface, input->input_device, time);
1106}
1107
1108void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001109window_activate_drag(struct wl_drag *drag, struct window *window,
1110 struct input *input, uint32_t time)
1111{
1112 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001113}
1114
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001115static void
1116handle_configure(void *data, struct wl_shell *shell,
1117 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001118 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001119{
1120 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001121 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001122
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001123 /* FIXME: this is probably the wrong place to check for width
1124 * or height <= 0, but it prevents the compositor from crashing
1125 */
1126 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001127 return;
1128
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001129 window->resize_edges = edges;
1130
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001131 if (window->resize_handler) {
1132 child_width = width - 20 - window->margin * 2;
1133 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001134
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001135 (*window->resize_handler)(window,
1136 child_width, child_height,
1137 window->user_data);
1138 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001139 window->allocation.width = width;
1140 window->allocation.height = height;
1141
1142 if (window->redraw_handler)
1143 window_schedule_redraw(window);
1144 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001145}
1146
1147static const struct wl_shell_listener shell_listener = {
1148 handle_configure,
1149};
1150
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001151void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001152window_get_child_allocation(struct window *window,
1153 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001154{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001155 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001156 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001157 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001158 allocation->x = window->margin + 10;
1159 allocation->y = window->margin + 50;
1160 allocation->width =
1161 window->allocation.width - 20 - window->margin * 2;
1162 allocation->height =
1163 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001164 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001165}
1166
1167void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001168window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001169{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001170 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001171 window->allocation.x = 20 + window->margin;
1172 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001173 window->allocation.width = width + 20 + window->margin * 2;
1174 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001175 } else {
1176 window->allocation.x = 0;
1177 window->allocation.y = 0;
1178 window->allocation.width = width;
1179 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001180 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001181}
1182
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001183static gboolean
1184idle_redraw(void *data)
1185{
1186 struct window *window = data;
1187
1188 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001189
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001190 window->redraw_scheduled = 0;
1191
1192 return FALSE;
1193}
1194
1195void
1196window_schedule_redraw(struct window *window)
1197{
1198 if (!window->redraw_scheduled) {
1199 g_idle_add(idle_redraw, window);
1200 window->redraw_scheduled = 1;
1201 }
1202}
1203
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001204void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001205window_set_fullscreen(struct window *window, int fullscreen)
1206{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001207 int32_t width, height;
1208
1209 if (window->fullscreen == fullscreen)
1210 return;
1211
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001212 window->fullscreen = fullscreen;
1213 if (window->fullscreen) {
1214 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001215 width = window->display->screen_allocation.width;
1216 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001217 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001218 width = window->saved_allocation.width - 20 - window->margin * 2;
1219 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001220 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001221
1222 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001223}
1224
1225void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001226window_set_decoration(struct window *window, int decoration)
1227{
1228 window->decoration = decoration;
1229}
1230
1231void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001232window_set_user_data(struct window *window, void *data)
1233{
1234 window->user_data = data;
1235}
1236
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001237void *
1238window_get_user_data(struct window *window)
1239{
1240 return window->user_data;
1241}
1242
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001243void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001244window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001245 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001246{
1247 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001248}
1249
1250void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001251window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001252 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001253{
1254 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001255}
1256
1257void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001258window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001259 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001260{
1261 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001262}
1263
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001264void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001265window_set_button_handler(struct window *window,
1266 window_button_handler_t handler)
1267{
1268 window->button_handler = handler;
1269}
1270
1271void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001272window_set_motion_handler(struct window *window,
1273 window_motion_handler_t handler)
1274{
1275 window->motion_handler = handler;
1276}
1277
1278void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001279window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001280 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001281{
1282 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001283}
1284
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001285void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001286window_set_title(struct window *window, const char *title)
1287{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001288 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001289 window->title = strdup(title);
1290}
1291
1292const char *
1293window_get_title(struct window *window)
1294{
1295 return window->title;
1296}
1297
1298void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001299window_damage(struct window *window, int32_t x, int32_t y,
1300 int32_t width, int32_t height)
1301{
1302 wl_surface_damage(window->surface, x, y, width, height);
1303}
1304
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001305static struct window *
1306window_create_internal(struct display *display, struct window *parent,
1307 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001308{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001309 struct window *window;
1310
1311 window = malloc(sizeof *window);
1312 if (window == NULL)
1313 return NULL;
1314
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001315 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001316 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001317 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001318 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001319 window->allocation.x = 0;
1320 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001321 window->allocation.width = width;
1322 window->allocation.height = height;
1323 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001324 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001325 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001326
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001327 if (display->dpy)
1328 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_IMAGE;
Yuval Fledel45568f62010-12-06 09:18:12 -05001329 else
1330 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001331
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001332 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001333 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001334
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001335 return window;
1336}
1337
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001338struct window *
1339window_create(struct display *display, int32_t width, int32_t height)
1340{
1341 struct window *window;
1342
1343 window = window_create_internal(display, NULL, width, height);
1344 if (!window)
1345 return NULL;
1346
1347 return window;
1348}
1349
1350struct window *
1351window_create_transient(struct display *display, struct window *parent,
1352 int32_t x, int32_t y, int32_t width, int32_t height)
1353{
1354 struct window *window;
1355
1356 window = window_create_internal(parent->display,
1357 parent, width, height);
1358 if (!window)
1359 return NULL;
1360
1361 window->x = x;
1362 window->y = y;
1363
1364 return window;
1365}
1366
1367void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001368window_set_buffer_type(struct window *window, enum window_buffer_type type)
1369{
1370 window->buffer_type = type;
1371}
1372
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001373static void
1374display_handle_geometry(void *data,
1375 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001376 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001377{
1378 struct display *display = data;
1379
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001380 display->screen_allocation.x = x;
1381 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001382 display->screen_allocation.width = width;
1383 display->screen_allocation.height = height;
1384}
1385
1386static const struct wl_output_listener output_listener = {
1387 display_handle_geometry,
1388};
1389
1390static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001391display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001392{
1393 struct input *input;
1394
1395 input = malloc(sizeof *input);
1396 if (input == NULL)
1397 return;
1398
1399 memset(input, 0, sizeof *input);
1400 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001401 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001402 input->pointer_focus = NULL;
1403 input->keyboard_focus = NULL;
1404 wl_list_insert(d->input_list.prev, &input->link);
1405
1406 wl_input_device_add_listener(input->input_device,
1407 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001408 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001409}
1410
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001411struct selection_offer {
1412 struct display *display;
1413 struct wl_selection_offer *offer;
1414 struct wl_array types;
1415 struct input *input;
1416};
1417
1418int
1419input_offers_mime_type(struct input *input, const char *type)
1420{
1421 struct selection_offer *offer = input->offer;
1422 char **p, **end;
1423
1424 if (offer == NULL)
1425 return 0;
1426
1427 end = offer->types.data + offer->types.size;
1428 for (p = offer->types.data; p < end; p++)
1429 if (strcmp(*p, type) == 0)
1430 return 1;
1431
1432 return 0;
1433}
1434
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001435void
1436input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001437{
1438 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001439
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001440 /* FIXME: A number of things can go wrong here: the object may
1441 * not be the current selection offer any more (which could
1442 * still work, but the source may have gone away or just
1443 * destroyed its wl_selection) or the offer may not have the
1444 * requested type after all (programmer/client error,
1445 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001446 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001447}
1448
1449static void
1450selection_offer_offer(void *data,
1451 struct wl_selection_offer *selection_offer,
1452 const char *type)
1453{
1454 struct selection_offer *offer = data;
1455
1456 char **p;
1457
1458 p = wl_array_add(&offer->types, sizeof *p);
1459 if (p)
1460 *p = strdup(type);
1461};
1462
1463static void
1464selection_offer_keyboard_focus(void *data,
1465 struct wl_selection_offer *selection_offer,
1466 struct wl_input_device *input_device)
1467{
1468 struct selection_offer *offer = data;
1469 struct input *input;
1470 char **p, **end;
1471
1472 if (input_device == NULL) {
1473 printf("selection offer retracted %p\n", selection_offer);
1474 input = offer->input;
1475 input->offer = NULL;
1476 wl_selection_offer_destroy(selection_offer);
1477 wl_array_release(&offer->types);
1478 free(offer);
1479 return;
1480 }
1481
1482 input = wl_input_device_get_user_data(input_device);
1483 printf("new selection offer %p:", selection_offer);
1484
1485 offer->input = input;
1486 input->offer = offer;
1487 end = offer->types.data + offer->types.size;
1488 for (p = offer->types.data; p < end; p++)
1489 printf(" %s", *p);
1490
1491 printf("\n");
1492}
1493
1494struct wl_selection_offer_listener selection_offer_listener = {
1495 selection_offer_offer,
1496 selection_offer_keyboard_focus
1497};
1498
1499static void
1500add_selection_offer(struct display *d, uint32_t id)
1501{
1502 struct selection_offer *offer;
1503
1504 offer = malloc(sizeof *offer);
1505 if (offer == NULL)
1506 return;
1507
1508 offer->offer = wl_selection_offer_create(d->display, id);
1509 offer->display = d;
1510 wl_array_init(&offer->types);
1511 offer->input = NULL;
1512
1513 wl_selection_offer_add_listener(offer->offer,
1514 &selection_offer_listener, offer);
1515}
1516
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001517static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001518display_handle_global(struct wl_display *display, uint32_t id,
1519 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001520{
1521 struct display *d = data;
1522
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001523 if (strcmp(interface, "compositor") == 0) {
1524 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001525 } else if (strcmp(interface, "output") == 0) {
1526 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001527 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001528 } else if (strcmp(interface, "input_device") == 0) {
1529 display_add_input(d, id);
1530 } else if (strcmp(interface, "shell") == 0) {
1531 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001532 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001533 } else if (strcmp(interface, "shm") == 0) {
1534 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001535 } else if (strcmp(interface, "selection_offer") == 0) {
1536 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001537 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001538 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001539 }
1540}
1541
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001542static void
1543display_render_frame(struct display *d)
1544{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001545 int radius = 8;
1546 cairo_t *cr;
1547
1548 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1549 cr = cairo_create(d->shadow);
1550 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1551 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1552 rounded_rect(cr, 16, 16, 112, 112, radius);
1553 cairo_fill(cr);
1554 cairo_destroy(cr);
1555 blur_surface(d->shadow, 64);
1556
1557 d->active_frame =
1558 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1559 cr = cairo_create(d->active_frame);
1560 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1561 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1562 rounded_rect(cr, 16, 16, 112, 112, radius);
1563 cairo_fill(cr);
1564 cairo_destroy(cr);
1565
1566 d->inactive_frame =
1567 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1568 cr = cairo_create(d->inactive_frame);
1569 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1570 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1571 rounded_rect(cr, 16, 16, 112, 112, radius);
1572 cairo_fill(cr);
1573 cairo_destroy(cr);
1574}
1575
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001576static void
1577init_xkb(struct display *d)
1578{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001579 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001580
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001581 names.rules = "evdev";
1582 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001583 names.layout = option_xkb_layout;
1584 names.variant = option_xkb_variant;
1585 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001586
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001587 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001588 if (!d->xkb) {
1589 fprintf(stderr, "Failed to compile keymap\n");
1590 exit(1);
1591 }
1592}
1593
Yuval Fledel45568f62010-12-06 09:18:12 -05001594static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001595init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001596{
1597 EGLint major, minor;
Yuval Fledel45568f62010-12-06 09:18:12 -05001598
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -05001599 d->dpy = eglGetDisplay(d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001600 if (!eglInitialize(d->dpy, &major, &minor)) {
1601 fprintf(stderr, "failed to initialize display\n");
1602 return -1;
1603 }
1604
1605 if (!eglBindAPI(EGL_OPENGL_API)) {
1606 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1607 return -1;
1608 }
1609
1610 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1611 if (d->ctx == NULL) {
1612 fprintf(stderr, "failed to create context\n");
1613 return -1;
1614 }
1615
1616 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001617 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001618 return -1;
1619 }
1620
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001621#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001622 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1623 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001624 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001625 return -1;
1626 }
1627#endif
1628
1629 return 0;
1630}
1631
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001632struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001633display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001634{
1635 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001636 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001637 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001638 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001639
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001640 g_type_init();
1641
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001642 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001643 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001644 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001645
1646 xkb_option_group = g_option_group_new("xkb",
1647 "Keyboard options",
1648 "Show all XKB options",
1649 NULL, NULL);
1650 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1651 g_option_context_add_group (context, xkb_option_group);
1652
1653 if (!g_option_context_parse(context, argc, argv, &error)) {
1654 fprintf(stderr, "option parsing failed: %s\n", error->message);
1655 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001656 }
1657
Tim Wiederhake748f6722011-01-23 23:25:25 +01001658 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001659
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001660 d = malloc(sizeof *d);
1661 if (d == NULL)
1662 return NULL;
1663
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001664 memset(d, 0, sizeof *d);
1665
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001666 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001667 if (d->display == NULL) {
1668 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001669 return NULL;
1670 }
1671
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001672 wl_list_init(&d->input_list);
1673
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001674 /* Set up listener so we'll catch all events. */
1675 wl_display_add_global_listener(d->display,
1676 display_handle_global, d);
1677
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -05001678 d->native_dpy = wl_egl_display_create(d->display);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001679
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001680 /* Process connection events. */
1681 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1682
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001683 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001684 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001685
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001686 d->image_target_texture_2d =
1687 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1688 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1689 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1690
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001691 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001692
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001693 display_render_frame(d);
1694
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001695 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001696 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001697 g_source_attach(d->source, NULL);
1698
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001699 wl_list_init(&d->window_list);
1700
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001701 init_xkb(d);
1702
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001703 return d;
1704}
1705
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001706struct wl_display *
1707display_get_display(struct display *display)
1708{
1709 return display->display;
1710}
1711
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001712struct wl_compositor *
1713display_get_compositor(struct display *display)
1714{
1715 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001716}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001717
1718EGLDisplay
1719display_get_egl_display(struct display *d)
1720{
1721 return d->dpy;
1722}
1723
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001724struct wl_shell *
1725display_get_shell(struct display *display)
1726{
1727 return display->shell;
1728}
1729
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001730void
1731display_run(struct display *d)
1732{
1733 g_main_loop_run(d->loop);
1734}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001735
1736void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001737display_set_global_handler(struct display *display,
1738 display_global_handler_t handler)
1739{
1740 display->global_handler = handler;
1741}