blob: 7c2579d8bcb166725eebae0614f8b78461105cef [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øgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg478d9262010-06-08 20:34:11 -040033#include <glib-object.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040034#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg640609a2010-08-09 22:11:47 -040035#include <xf86drm.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040036
37#define EGL_EGLEXT_PROTOTYPES 1
38#define GL_GLEXT_PROTOTYPES 1
39#include <GL/gl.h>
40#include <EGL/egl.h>
41#include <EGL/eglext.h>
42#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050043
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040044#include <X11/extensions/XKBcommon.h>
45
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050047#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050049#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040050#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050053
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050054struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050055 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050056 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040057 struct wl_shell *shell;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -040058 struct wl_drm *drm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040061 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040062 EGLDisplay dpy;
63 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010064 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040066 GMainLoop *loop;
67 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040068 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040069 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040070 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040071 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040072 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040073 cairo_surface_t **pointer_surfaces;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050074};
75
76struct window {
77 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050078 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050079 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040080 struct rectangle allocation, saved_allocation, pending_allocation;
81 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040082 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050083 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050085 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040086 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040087 struct input *grab_device;
88 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050089 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050090
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040091 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050092 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050093
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050094 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040095 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050096 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040097 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050098 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040099 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 window_frame_handler_t frame_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400101 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500103 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400104 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105};
106
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400107struct input {
108 struct display *display;
109 struct wl_input_device *input_device;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 struct wl_drag *drag;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct window *pointer_focus;
112 struct window *keyboard_focus;
113 uint32_t modifiers;
114 int32_t x, y, sx, sy;
115 struct wl_list link;
116};
117
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500118static void
119rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
120{
121 cairo_move_to(cr, x0, y0 + radius);
122 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
123 cairo_line_to(cr, x1 - radius, y0);
124 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
125 cairo_line_to(cr, x1, y1 - radius);
126 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
127 cairo_line_to(cr, x0 + radius, y1);
128 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
129 cairo_close_path(cr);
130}
131
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400132static int
133texture_from_png(const char *filename, int width, int height)
134{
135 GdkPixbuf *pixbuf;
136 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400137 int stride, i;
138 unsigned char *pixels, *p, *end;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400139
140 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
141 width, height,
142 FALSE, &error);
143 if (error != NULL)
144 return -1;
145
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400146 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
147 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
148 gdk_pixbuf_unref(pixbuf);
149 return -1;
150 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400151
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400152
153 stride = gdk_pixbuf_get_rowstride(pixbuf);
154 pixels = gdk_pixbuf_get_pixels(pixbuf);
155
156 for (i = 0; i < height; i++) {
157 p = pixels + i * stride;
158 end = p + width * 4;
159 while (p < end) {
160 unsigned int t;
161
162#define MULT(d,c,a,t) \
163 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
164
165 MULT(p[0], p[0], p[3], t);
166 MULT(p[1], p[1], p[3], t);
167 MULT(p[2], p[2], p[3], t);
168 p += 4;
169
170 }
171 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400172
173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400174 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400175
176 gdk_pixbuf_unref(pixbuf);
177
178 return 0;
179}
180
181static const struct {
182 const char *filename;
183 int hotspot_x, hotspot_y;
184} pointer_images[] = {
185 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
186 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
187 { DATADIR "/wayland/bottom_side.png", 16, 20 },
188 { DATADIR "/wayland/grabbing.png", 20, 17 },
189 { DATADIR "/wayland/left_ptr.png", 10, 5 },
190 { DATADIR "/wayland/left_side.png", 10, 20 },
191 { DATADIR "/wayland/right_side.png", 30, 19 },
192 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
193 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
194 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400195 { DATADIR "/wayland/xterm.png", 15, 15 },
196 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400197};
198
199static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400200create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400201{
202 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400203 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400204 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400205
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400206 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400207 display->pointer_surfaces =
208 malloc(count * sizeof *display->pointer_surfaces);
209 rect.width = width;
210 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400211 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400212 display->pointer_surfaces[i] =
213 display_create_surface(display, &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400214 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400215 }
216
217}
218
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400219static const cairo_user_data_key_t surface_data_key;
220struct surface_data {
221 EGLImageKHR image;
222 GLuint texture;
223 EGLDisplay dpy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400224 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400225};
226
227static void
228surface_data_destroy(void *p)
229{
230 struct surface_data *data = p;
231
232 glDeleteTextures(1, &data->texture);
233 eglDestroyImageKHR(data->dpy, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400234 if (data->buffer)
235 wl_buffer_destroy(data->buffer);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400236}
237
238cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400239display_create_surface(struct display *display,
240 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400241{
242 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400243 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400244 cairo_surface_t *surface;
245
246 EGLint image_attribs[] = {
247 EGL_WIDTH, 0,
248 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400249 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
250 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400251 EGL_NONE
252 };
253
254 data = malloc(sizeof *data);
255 image_attribs[1] = rectangle->width;
256 image_attribs[3] = rectangle->height;
257 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
258 glGenTextures(1, &data->texture);
259 data->dpy = dpy;
260 glBindTexture(GL_TEXTURE_2D, data->texture);
261 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400262 data->buffer = NULL;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400263
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400264 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400265 CAIRO_CONTENT_COLOR_ALPHA,
266 data->texture,
267 rectangle->width,
268 rectangle->height);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400269
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400270 cairo_surface_set_user_data (surface, &surface_data_key,
271 data, surface_data_destroy);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400272
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400273 return surface;
274}
275
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400276struct wl_buffer *
277display_get_buffer_for_surface(struct display *display,
278 cairo_surface_t *surface)
279{
280 struct surface_data *data;
281 struct wl_visual *visual;
282 struct wl_buffer *buffer;
283 EGLint name, stride;
284 int width, height;
285
286 data = cairo_surface_get_user_data (surface, &surface_data_key);
287 if (data->buffer)
288 return data->buffer;
289
290 width = cairo_gl_surface_get_width (surface);
291 height = cairo_gl_surface_get_height (surface);
292
293 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
294
295 visual = wl_display_get_premultiplied_argb_visual(display->display);
296 buffer = wl_drm_create_buffer(display->drm,
297 name, width, height, stride, visual);
298 data->buffer = buffer;
299
300 return buffer;
301}
302
303cairo_surface_t *
304display_get_pointer_surface(struct display *display, int pointer,
305 int *width, int *height,
306 int *hotspot_x, int *hotspot_y)
307{
308 cairo_surface_t *surface;
309
310 surface = display->pointer_surfaces[pointer];
311 *width = cairo_gl_surface_get_width(surface);
312 *height = cairo_gl_surface_get_height(surface);
313 *hotspot_x = pointer_images[pointer].hotspot_x;
314 *hotspot_y = pointer_images[pointer].hotspot_y;
315
316 return cairo_surface_reference(surface);
317}
318
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500319static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500320window_attach_surface(struct window *window)
321{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400322 struct display *display = window->display;
323 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500324
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500325 if (window->pending_surface != NULL)
326 return;
327
Kristian Høgsberg09531622010-06-14 23:22:15 -0400328 window->pending_surface = window->cairo_surface;
329 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500330
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400331 buffer = display_get_buffer_for_surface(display,
332 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400333 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500334
335 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400336 window->allocation.x,
337 window->allocation.y,
338 window->allocation.width,
339 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400340
341 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500342}
343
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500344void
345window_commit(struct window *window, uint32_t key)
346{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400347 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500348 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400349 } else {
350 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500351 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500352}
353
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500354static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500355window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500356{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500357 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500358 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400359 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400360 cairo_surface_t *frame;
361 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500362
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500363 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400364 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400365 width = window->allocation.width;
366 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500367
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500368 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500369 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500370 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
371
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500372 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500373
Kristian Høgsberg09531622010-06-14 23:22:15 -0400374 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400375 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400376 cairo_paint(cr);
377
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400378 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400379 tile_mask(cr, window->display->shadow,
380 shadow_dx, shadow_dy, width, height,
381 window->margin + 10 - shadow_dx,
382 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500383
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400384 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400385 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400386 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400387 frame = window->display->inactive_frame;
388
389 tile_source(cr, frame, 0, 0, width, height,
390 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500391
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500392 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
393 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500394 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400395 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500396 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
397 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
398 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400399 if (window->keyboard_device)
400 cairo_set_source_rgb(cr, 0, 0, 0);
401 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400402 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400403 cairo_show_text(cr, window->title);
404
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500405 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400406
407 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500408}
409
410static void
411window_draw_fullscreen(struct window *window)
412{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500413 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400414 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500415}
416
417void
418window_draw(struct window *window)
419{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500420 if (window->cairo_surface != NULL)
421 cairo_surface_destroy(window->cairo_surface);
422
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400423 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500424 window_draw_fullscreen(window);
425 else
426 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500427}
428
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400429cairo_surface_t *
430window_get_surface(struct window *window)
431{
432 return window->cairo_surface;
433}
434
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400435enum window_location {
436 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400437 WINDOW_RESIZING_TOP = 1,
438 WINDOW_RESIZING_BOTTOM = 2,
439 WINDOW_RESIZING_LEFT = 4,
440 WINDOW_RESIZING_TOP_LEFT = 5,
441 WINDOW_RESIZING_BOTTOM_LEFT = 6,
442 WINDOW_RESIZING_RIGHT = 8,
443 WINDOW_RESIZING_TOP_RIGHT = 9,
444 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
445 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400446 WINDOW_EXTERIOR = 16,
447 WINDOW_TITLEBAR = 17,
448 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400449};
450
451static int
452get_pointer_location(struct window *window, int32_t x, int32_t y)
453{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400454 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400455 const int grip_size = 8;
456
457 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400458 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400459 else if (window->margin <= x && x < window->margin + grip_size)
460 hlocation = WINDOW_RESIZING_LEFT;
461 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400462 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400463 else if (x < window->allocation.width - window->margin)
464 hlocation = WINDOW_RESIZING_RIGHT;
465 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400466 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400467
468 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400469 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400470 else if (window->margin <= y && y < window->margin + grip_size)
471 vlocation = WINDOW_RESIZING_TOP;
472 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400473 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400474 else if (y < window->allocation.height - window->margin)
475 vlocation = WINDOW_RESIZING_BOTTOM;
476 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400477 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400478
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400479 location = vlocation | hlocation;
480 if (location & WINDOW_EXTERIOR)
481 location = WINDOW_EXTERIOR;
482 if (location == WINDOW_INTERIOR && y < window->margin + 50)
483 location = WINDOW_TITLEBAR;
484 else if (location == WINDOW_INTERIOR)
485 location = WINDOW_CLIENT_AREA;
486
487 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400488}
489
490static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400491set_pointer_image(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400492{
493 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400494 struct wl_buffer *buffer;
495 cairo_surface_t *surface;
496 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400497
498 location = get_pointer_location(input->pointer_focus,
499 input->sx, input->sy);
500 switch (location) {
501 case WINDOW_RESIZING_TOP:
502 pointer = POINTER_TOP;
503 break;
504 case WINDOW_RESIZING_BOTTOM:
505 pointer = POINTER_BOTTOM;
506 break;
507 case WINDOW_RESIZING_LEFT:
508 pointer = POINTER_LEFT;
509 break;
510 case WINDOW_RESIZING_RIGHT:
511 pointer = POINTER_RIGHT;
512 break;
513 case WINDOW_RESIZING_TOP_LEFT:
514 pointer = POINTER_TOP_LEFT;
515 break;
516 case WINDOW_RESIZING_TOP_RIGHT:
517 pointer = POINTER_TOP_RIGHT;
518 break;
519 case WINDOW_RESIZING_BOTTOM_LEFT:
520 pointer = POINTER_BOTTOM_LEFT;
521 break;
522 case WINDOW_RESIZING_BOTTOM_RIGHT:
523 pointer = POINTER_BOTTOM_RIGHT;
524 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400525 case WINDOW_EXTERIOR:
526 case WINDOW_TITLEBAR:
527 wl_input_device_attach(input->input_device, NULL, 0, 0);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400528 return;
529 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400530 break;
531 }
532
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400533 surface = display->pointer_surfaces[pointer];
534 buffer = display_get_buffer_for_surface(display, surface);
535 wl_input_device_attach(input->input_device, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400536 pointer_images[pointer].hotspot_x,
537 pointer_images[pointer].hotspot_y);
538}
539
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500540static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500541window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400542 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500543 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500544{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400545 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400546 struct window *window = input->pointer_focus;
547 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400548
549 input->x = x;
550 input->y = y;
551 input->sx = sx;
552 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400553
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400554 location = get_pointer_location(window, input->sx, input->sy);
555
556 if (window->motion_handler)
557 pointer = (*window->motion_handler)(window, input, time,
558 x, y, sx, sy,
559 window->user_data);
560
561 set_pointer_image(input, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500562}
563
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400564static void
565window_handle_button(void *data,
566 struct wl_input_device *input_device,
567 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500568{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400569 struct input *input = data;
570 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400571 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400572
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400573 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500574
575 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400576 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400577 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400578 wl_shell_move(window->display->shell,
579 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500580 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400581 case WINDOW_RESIZING_TOP:
582 case WINDOW_RESIZING_BOTTOM:
583 case WINDOW_RESIZING_LEFT:
584 case WINDOW_RESIZING_RIGHT:
585 case WINDOW_RESIZING_TOP_LEFT:
586 case WINDOW_RESIZING_TOP_RIGHT:
587 case WINDOW_RESIZING_BOTTOM_LEFT:
588 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400589 wl_shell_resize(window->display->shell,
590 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400591 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500592 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400593 case WINDOW_CLIENT_AREA:
594 if (window->button_handler)
595 (*window->button_handler)(window,
596 input, time,
597 button, state,
598 window->user_data);
599 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500600 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400601 } else {
602 if (window->button_handler)
603 (*window->button_handler)(window,
604 input, time,
605 button, state,
606 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500607 }
608}
609
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500610static void
611window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400612 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500613{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400614 struct input *input = data;
615 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400616 struct display *d = window->display;
617 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500618
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400619 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400620 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500621 return;
622
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400623 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400624 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400625 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
626 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500627
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400628 sym = XkbKeySymEntry(d->xkb, code, level, 0);
629
630 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400631 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400632 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400633 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500634
635 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400636 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400637 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500638}
639
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500640static void
641window_handle_pointer_focus(void *data,
642 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400643 uint32_t time, struct wl_surface *surface,
644 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500645{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400646 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400647 struct window *window;
648 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400649
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400650 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400651 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400652 window = input->pointer_focus;
653
654 pointer = POINTER_LEFT_PTR;
655 if (window->motion_handler)
656 pointer = (*window->motion_handler)(window,
657 input, time,
658 x, y, sx, sy,
659 window->user_data);
660
661 set_pointer_image(input, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400662 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400663 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500665}
666
667static void
668window_handle_keyboard_focus(void *data,
669 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400670 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500671 struct wl_surface *surface,
672 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500673{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400674 struct input *input = data;
675 struct window *window = input->keyboard_focus;
676 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500677 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500678
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400679 window = input->keyboard_focus;
680 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500681 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400682 if (window->keyboard_focus_handler)
683 (*window->keyboard_focus_handler)(window, NULL,
684 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500685 }
686
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400687 if (surface)
688 input->keyboard_focus = wl_surface_get_user_data(surface);
689 else
690 input->keyboard_focus = NULL;
691
692 end = keys->data + keys->size;
693 for (k = keys->data; k < end; k++)
694 input->modifiers |= d->xkb->map->modmap[*k];
695
696 window = input->keyboard_focus;
697 if (window) {
698 window->keyboard_device = input;
699 if (window->keyboard_focus_handler)
700 (*window->keyboard_focus_handler)(window,
701 window->keyboard_device,
702 window->user_data);
703 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500704}
705
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500706static const struct wl_input_device_listener input_device_listener = {
707 window_handle_motion,
708 window_handle_button,
709 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500710 window_handle_pointer_focus,
711 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500712};
713
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400714void
715input_get_position(struct input *input, int32_t *x, int32_t *y)
716{
717 *x = input->sx;
718 *y = input->sy;
719}
720
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400721struct wl_input_device *
722input_get_input_device(struct input *input)
723{
724 return input->input_device;
725}
726
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400727void
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400728display_add_drag_listener(struct display *display,
729 const struct wl_drag_listener *drag_listener,
730 void *data)
731{
732 struct input *input;
733
734 wl_list_for_each(input, &display->input_list, link)
735 wl_drag_add_listener(input->drag, drag_listener, data);
736}
737
738void
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400739window_start_drag(struct window *window, struct input *input, uint32_t time)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400740{
741 cairo_device_flush (window->display->device);
742
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400743 wl_drag_prepare(input->drag, window->surface, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400744 wl_drag_offer(input->drag, "text/plain");
745 wl_drag_activate(input->drag);
746}
747
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400748static void
749handle_configure(void *data, struct wl_shell *shell,
750 uint32_t time, uint32_t edges,
751 struct wl_surface *surface,
752 int32_t x, int32_t y, int32_t width, int32_t height)
753{
754 struct window *window = wl_surface_get_user_data(surface);
755
756 window->resize_edges = edges;
757 window->pending_allocation.x = x;
758 window->pending_allocation.y = y;
759 window->pending_allocation.width = width;
760 window->pending_allocation.height = height;
761
762 if (!(edges & 15))
763 return;
764
765 if (window->resize_handler)
766 (*window->resize_handler)(window,
767 window->user_data);
768 else if (window->redraw_handler)
769 window_schedule_redraw(window);
770}
771
772static const struct wl_shell_listener shell_listener = {
773 handle_configure,
774};
775
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500776void
777window_get_child_rectangle(struct window *window,
778 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500779{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400780 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500781 *rectangle = window->allocation;
782 } else {
783 rectangle->x = window->margin + 10;
784 rectangle->y = window->margin + 50;
785 rectangle->width = window->allocation.width - 20 - window->margin * 2;
786 rectangle->height = window->allocation.height - 60 - window->margin * 2;
787 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500788}
789
790void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500791window_set_child_size(struct window *window,
792 struct rectangle *rectangle)
793{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400794 int32_t width, height;
795
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500796 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400797 width = rectangle->width + 20 + window->margin * 2;
798 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400799
800 if (window->resize_edges & WINDOW_RESIZING_LEFT)
801 window->allocation.x +=
802 window->allocation.width - width;
803 if (window->resize_edges & WINDOW_RESIZING_TOP)
804 window->allocation.y +=
805 window->allocation.height - height;
806
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400807 window->allocation.width = width;
808 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500809 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500810}
811
812void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400813window_copy_image(struct window *window,
814 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500815{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400816 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500817}
818
819void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500820window_copy_surface(struct window *window,
821 struct rectangle *rectangle,
822 cairo_surface_t *surface)
823{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500824 cairo_t *cr;
825
826 cr = cairo_create (window->cairo_surface);
827
828 cairo_set_source_surface (cr,
829 surface,
830 rectangle->x, rectangle->y);
831
832 cairo_paint (cr);
833 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500834}
835
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400836static gboolean
837idle_redraw(void *data)
838{
839 struct window *window = data;
840
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400841 if (window->resize_edges)
842 window->allocation = window->pending_allocation;
843
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400844 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400845
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400846 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400847 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400848
849 return FALSE;
850}
851
852void
853window_schedule_redraw(struct window *window)
854{
855 if (!window->redraw_scheduled) {
856 g_idle_add(idle_redraw, window);
857 window->redraw_scheduled = 1;
858 }
859}
860
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500861void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500862window_set_fullscreen(struct window *window, int fullscreen)
863{
864 window->fullscreen = fullscreen;
865 if (window->fullscreen) {
866 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500867 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500868 } else {
869 window->allocation = window->saved_allocation;
870 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500871}
872
873void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400874window_set_decoration(struct window *window, int decoration)
875{
876 window->decoration = decoration;
877}
878
879void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400880window_set_user_data(struct window *window, void *data)
881{
882 window->user_data = data;
883}
884
885void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500886window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400887 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500888{
889 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500890}
891
892void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400893window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400894 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400895{
896 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400897}
898
899void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500900window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400901 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500902{
903 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500904}
905
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500906void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400907window_set_button_handler(struct window *window,
908 window_button_handler_t handler)
909{
910 window->button_handler = handler;
911}
912
913void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400914window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400915 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400916{
917 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400918}
919
920void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400921window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400922 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400923{
924 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400925}
926
927void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400928window_set_motion_handler(struct window *window,
929 window_motion_handler_t handler)
930{
931 window->motion_handler = handler;
932}
933
934void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500935window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400936 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500937{
938 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500939}
940
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400941void
942window_move(struct window *window, int32_t x, int32_t y)
943{
944 window->allocation.x = x;
945 window->allocation.y = y;
946
947 wl_surface_map(window->surface,
948 window->allocation.x - window->margin,
949 window->allocation.y - window->margin,
950 window->allocation.width,
951 window->allocation.height);
952}
953
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500954struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500955window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500956 int32_t x, int32_t y, int32_t width, int32_t height)
957{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500958 struct window *window;
959
960 window = malloc(sizeof *window);
961 if (window == NULL)
962 return NULL;
963
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500964 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500965 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500966 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500967 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500968 window->allocation.x = x;
969 window->allocation.y = y;
970 window->allocation.width = width;
971 window->allocation.height = height;
972 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500973 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400974 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500975
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400976 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400977 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500978
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500979 return window;
980}
981
982static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400983drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400984{
985 struct display *d = data;
986
987 d->device_name = strdup(device);
988}
989
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400990static void drm_handle_authenticated(void *data, struct wl_drm *drm)
991{
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400992 struct display *d = data;
993
994 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400995}
996
997static const struct wl_drm_listener drm_listener = {
998 drm_handle_device,
999 drm_handle_authenticated
1000};
1001
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001002static void
1003display_handle_acknowledge(void *data,
1004 struct wl_compositor *compositor,
1005 uint32_t key, uint32_t frame)
1006{
1007 struct display *d = data;
1008 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001009
1010 /* The acknowledge event means that the server processed our
1011 * last commit request and we can now safely free the old
1012 * window buffer if we resized and render the next frame into
1013 * our back buffer.. */
1014 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -04001015 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001016 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -04001017 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001018 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001019 if (window->acknowledge_handler)
1020 (*window->acknowledge_handler)(window, key, frame, window->user_data);
1021 }
1022}
1023
1024static void
1025display_handle_frame(void *data,
1026 struct wl_compositor *compositor,
1027 uint32_t frame, uint32_t timestamp)
1028{
1029 struct display *d = data;
1030 struct window *window;
1031
1032 wl_list_for_each(window, &d->window_list, link) {
1033 if (window->frame_handler)
1034 (*window->frame_handler)(window, frame,
1035 timestamp, window->user_data);
1036 }
1037}
1038
1039static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001040 display_handle_acknowledge,
1041 display_handle_frame,
1042};
1043
1044static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001045display_handle_geometry(void *data,
1046 struct wl_output *output,
1047 int32_t width, int32_t height)
1048{
1049 struct display *display = data;
1050
1051 display->screen_allocation.x = 0;
1052 display->screen_allocation.y = 0;
1053 display->screen_allocation.width = width;
1054 display->screen_allocation.height = height;
1055}
1056
1057static const struct wl_output_listener output_listener = {
1058 display_handle_geometry,
1059};
1060
1061static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001062display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001063{
1064 struct input *input;
1065
1066 input = malloc(sizeof *input);
1067 if (input == NULL)
1068 return;
1069
1070 memset(input, 0, sizeof *input);
1071 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001072 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001073 input->pointer_focus = NULL;
1074 input->keyboard_focus = NULL;
1075 wl_list_insert(d->input_list.prev, &input->link);
1076
1077 wl_input_device_add_listener(input->input_device,
1078 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001079 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001080}
1081
1082static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001083drag_handle_device(void *data,
1084 struct wl_drag *drag, struct wl_input_device *device)
1085{
1086 struct input *input;
1087 fprintf(stderr, "device for drag %p: %p\n", drag, device);
1088
1089 input = wl_input_device_get_user_data(device);
1090 input->drag = drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001091 wl_drag_set_user_data(drag, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001092}
1093
1094static void
1095drag_pointer_focus(void *data,
1096 struct wl_drag *drag,
1097 uint32_t time, struct wl_surface *surface,
1098 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1099{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001100}
1101
1102static void
1103drag_offer(void *data,
1104 struct wl_drag *drag, const char *type)
1105{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001106}
1107
1108static void
1109drag_motion(void *data,
1110 struct wl_drag *drag,
1111 uint32_t time,
1112 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1113{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001114}
1115
1116static void
1117drag_target(void *data,
1118 struct wl_drag *drag, const char *mime_type)
1119{
1120}
1121
1122static void
1123drag_finish(void *data, struct wl_drag *drag)
1124{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125}
1126
1127static void
1128drag_data(void *data,
1129 struct wl_drag *drag, struct wl_array *contents)
1130{
1131}
1132
1133static const struct wl_drag_listener drag_listener = {
1134 drag_handle_device,
1135 drag_pointer_focus,
1136 drag_offer,
1137 drag_motion,
1138 drag_target,
1139 drag_finish,
1140 drag_data
1141};
1142
1143static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001144display_handle_global(struct wl_display *display, uint32_t id,
1145 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001146{
1147 struct display *d = data;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001148 struct wl_drag *drag;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001149
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001150 if (strcmp(interface, "compositor") == 0) {
1151 d->compositor = wl_compositor_create(display, id);
1152 wl_compositor_add_listener(d->compositor,
1153 &compositor_listener, d);
1154 } else if (strcmp(interface, "output") == 0) {
1155 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001156 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001157 } else if (strcmp(interface, "input_device") == 0) {
1158 display_add_input(d, id);
1159 } else if (strcmp(interface, "shell") == 0) {
1160 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001161 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001162 } else if (strcmp(interface, "drm") == 0) {
1163 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001164 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001165 } else if (strcmp(interface, "drag") == 0) {
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001166 drag = wl_drag_create(display, id);
1167 wl_drag_add_listener(drag, &drag_listener, NULL);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001168 }
1169}
1170
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001171static const char socket_name[] = "\0wayland";
1172
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001173static void
1174display_render_frame(struct display *d)
1175{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001176 int radius = 8;
1177 cairo_t *cr;
1178
1179 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1180 cr = cairo_create(d->shadow);
1181 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1182 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1183 rounded_rect(cr, 16, 16, 112, 112, radius);
1184 cairo_fill(cr);
1185 cairo_destroy(cr);
1186 blur_surface(d->shadow, 64);
1187
1188 d->active_frame =
1189 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1190 cr = cairo_create(d->active_frame);
1191 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1192 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1193 rounded_rect(cr, 16, 16, 112, 112, radius);
1194 cairo_fill(cr);
1195 cairo_destroy(cr);
1196
1197 d->inactive_frame =
1198 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1199 cr = cairo_create(d->inactive_frame);
1200 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1201 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1202 rounded_rect(cr, 16, 16, 112, 112, radius);
1203 cairo_fill(cr);
1204 cairo_destroy(cr);
1205}
1206
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001207static void
1208init_xkb(struct display *d)
1209{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001210 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001211
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001212 names.rules = "evdev";
1213 names.model = "pc105";
1214 names.layout = "us";
1215 names.variant = "";
1216 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001217
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001218 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001219 if (!d->xkb) {
1220 fprintf(stderr, "Failed to compile keymap\n");
1221 exit(1);
1222 }
1223}
1224
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001225struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001226display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001227{
1228 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001229 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001230 int fd;
1231 GOptionContext *context;
1232 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001233 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001234
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001235 g_type_init();
1236
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001237 context = g_option_context_new(NULL);
1238 if (option_entries) {
1239 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1240 if (!g_option_context_parse(context, argc, argv, &error)) {
1241 fprintf(stderr, "option parsing failed: %s\n", error->message);
1242 exit(EXIT_FAILURE);
1243 }
1244 }
1245
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001246 d = malloc(sizeof *d);
1247 if (d == NULL)
1248 return NULL;
1249
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001250 d->display = wl_display_create(socket_name, sizeof socket_name);
1251 if (d->display == NULL) {
1252 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001253 return NULL;
1254 }
1255
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001256 wl_list_init(&d->input_list);
1257
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001258 /* Set up listener so we'll catch all events. */
1259 wl_display_add_global_listener(d->display,
1260 display_handle_global, d);
1261
1262 /* Process connection events. */
1263 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1264
1265 fd = open(d->device_name, O_RDWR);
1266 if (fd < 0) {
1267 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001268 return NULL;
1269 }
1270
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001271 if (drmGetMagic(fd, &magic)) {
1272 fprintf(stderr, "DRI2: failed to get drm magic");
1273 return NULL;
1274 }
1275
1276 /* Wait for authenticated event */
1277 wl_drm_authenticate(d->drm, magic);
1278 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1279 while (!d->authenticated)
1280 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1281
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001282 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001283 if (!eglInitialize(d->dpy, &major, &minor)) {
1284 fprintf(stderr, "failed to initialize display\n");
1285 return NULL;
1286 }
1287
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001288 eglBindAPI(EGL_OPENGL_API);
1289
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001290 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001291 if (d->ctx == NULL) {
1292 fprintf(stderr, "failed to create context\n");
1293 return NULL;
1294 }
1295
1296 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1297 fprintf(stderr, "faile to make context current\n");
1298 return NULL;
1299 }
1300
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001301 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001302 if (d->device == NULL) {
1303 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001304 return NULL;
1305 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001306
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001308
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001309 display_render_frame(d);
1310
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001311 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001312 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001313 g_source_attach(d->source, NULL);
1314
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001315 wl_list_init(&d->window_list);
1316
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001317 init_xkb(d);
1318
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001319 return d;
1320}
1321
1322struct wl_compositor *
1323display_get_compositor(struct display *display)
1324{
1325 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001326}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001327
1328EGLDisplay
1329display_get_egl_display(struct display *d)
1330{
1331 return d->dpy;
1332}
1333
1334void
1335display_run(struct display *d)
1336{
1337 g_main_loop_run(d->loop);
1338}