blob: be6e7ea015efaaea3b27182d09bcf0d1ff4028ae [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øgsberga85fe3c2010-06-08 14:08:30 -040034
35#define EGL_EGLEXT_PROTOTYPES 1
36#define GL_GLEXT_PROTOTYPES 1
37#include <GL/gl.h>
38#include <EGL/egl.h>
39#include <EGL/eglext.h>
40#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050041
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050042#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050043#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050044#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050045#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040046#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050047
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050048#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050049
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050050struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050051 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050052 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050053 struct wl_output *output;
54 struct wl_input_device *input_device;
55 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056 EGLDisplay dpy;
57 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010058 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040060 GMainLoop *loop;
61 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040062 struct wl_list window_list;
63 char *device_name;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064};
65
66struct window {
67 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050068 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069 const char *title;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050070 struct rectangle allocation, saved_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050071 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050073 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050074 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050075 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040076 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050077 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050078 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050080 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050081
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040082 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050083 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberga341fa02010-01-24 18:10:15 -050084 int new_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050085
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050086 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050087 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050088 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040089 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040090 window_frame_handler_t frame_handler;
91
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050092 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050094};
95
Kristian Høgsberge4feb562008-11-08 18:53:37 -050096static void
97rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
98{
99 cairo_move_to(cr, x0, y0 + radius);
100 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
101 cairo_line_to(cr, x1 - radius, y0);
102 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
103 cairo_line_to(cr, x1, y1 - radius);
104 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
105 cairo_line_to(cr, x0 + radius, y1);
106 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
107 cairo_close_path(cr);
108}
109
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110static const cairo_user_data_key_t surface_data_key;
111struct surface_data {
112 EGLImageKHR image;
113 GLuint texture;
114 EGLDisplay dpy;
115};
116
117static void
118surface_data_destroy(void *p)
119{
120 struct surface_data *data = p;
121
122 glDeleteTextures(1, &data->texture);
123 eglDestroyImageKHR(data->dpy, data->image);
124}
125
126cairo_surface_t *
127window_create_surface(struct window *window,
128 struct rectangle *rectangle)
129{
130 struct surface_data *data;
131 EGLDisplay dpy = window->display->dpy;
132 cairo_surface_t *surface;
133
134 EGLint image_attribs[] = {
135 EGL_WIDTH, 0,
136 EGL_HEIGHT, 0,
137 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
138 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
139 EGL_NONE
140 };
141
142 data = malloc(sizeof *data);
143 image_attribs[1] = rectangle->width;
144 image_attribs[3] = rectangle->height;
145 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
146 glGenTextures(1, &data->texture);
147 data->dpy = dpy;
148 glBindTexture(GL_TEXTURE_2D, data->texture);
149 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
150
151 surface = cairo_gl_surface_create_for_texture(window->display->device,
152 CAIRO_CONTENT_COLOR_ALPHA,
153 data->texture,
154 rectangle->width,
155 rectangle->height);
156
157 cairo_surface_set_user_data (surface, &surface_data_key,
158 data, surface_data_destroy);
159
160 return surface;
161}
162
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500163static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500164window_attach_surface(struct window *window)
165{
166 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400167 struct surface_data *data;
168 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500169
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500170 if (window->pending_surface != NULL)
171 return;
172
173 window->pending_surface =
174 cairo_surface_reference(window->cairo_surface);
175
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400176 data = cairo_surface_get_user_data (window->cairo_surface, &surface_data_key);
177 eglExportDRMImageMESA(window->display->dpy, data->image, &name, NULL, &stride);
178
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500179 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
180 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400181 name,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500182 window->allocation.width,
183 window->allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400184 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500185 visual);
186
187 wl_surface_map(window->surface,
188 window->allocation.x - window->margin,
189 window->allocation.y - window->margin,
190 window->allocation.width,
191 window->allocation.height);
192}
193
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500194void
195window_commit(struct window *window, uint32_t key)
196{
197 if (window->new_surface) {
198 window_attach_surface(window);
199 window->new_surface = 0;
200 }
201
202 wl_compositor_commit(window->display->compositor, key);
203}
204
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500205static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500206window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500207{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500208 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500209 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500210 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500211 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500212 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400213 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500214
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500215 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400216 window_create_surface(window, &window->allocation);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500217
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500218 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500219 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500220 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
221
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500222 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500223
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500224 width = window->allocation.width - window->margin * 2;
225 height = window->allocation.height - window->margin * 2;
226
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400227 cairo_translate(cr, window->margin + shadow_dx,
228 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500229 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500230 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400231 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500232 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500233
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400234#define SLOW_BUT_PWETTY_not_right_now
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500235#ifdef SLOW_BUT_PWETTY
236 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
237 * Ideally we should have convolution filters in cairo, but we
238 * can also fallback to compositing the shadow image a bunch
239 * of times according to the blur kernel. */
240 {
241 cairo_surface_t *map;
242
243 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500244 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500245 cairo_drm_surface_unmap(window->cairo_surface, map);
246 }
247#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500248
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400249 cairo_translate(cr, -shadow_dx, -shadow_dy);
250 if (window->keyboard_device) {
251 rounded_rect(cr, 0, 0, width, height, radius);
252 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
253 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
254 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
255 cairo_set_source(cr, gradient);
256 cairo_fill(cr);
257 cairo_pattern_destroy(gradient);
258 } else {
259 rounded_rect(cr, 0, 0, width, height, radius);
260 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
261 cairo_fill(cr);
262 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500263
264 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
265 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500266 cairo_line_to(cr, width - 10, 50);
267 cairo_line_to(cr, width - 10, height - 10);
268 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500269 cairo_close_path(cr);
270 cairo_set_source(cr, dim);
271 cairo_stroke(cr);
272
273 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500274 cairo_line_to(cr, width - 10, 51);
275 cairo_line_to(cr, width - 10, height - 10);
276 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500277 cairo_close_path(cr);
278 cairo_set_source(cr, bright);
279 cairo_stroke(cr);
280
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500281 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
282 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500283 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500284 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500285 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
286 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
287 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500288 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400289 if (window->keyboard_device) {
290 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
291 cairo_stroke_preserve(cr);
292 cairo_set_source_rgb(cr, 1, 1, 1);
293 cairo_fill(cr);
294 } else {
295 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
296 cairo_fill(cr);
297 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500299}
300
301static void
302window_draw_fullscreen(struct window *window)
303{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500304 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400305 window_create_surface(window, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500306}
307
308void
309window_draw(struct window *window)
310{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500311 if (window->cairo_surface != NULL)
312 cairo_surface_destroy(window->cairo_surface);
313
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400314 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500315 window_draw_fullscreen(window);
316 else
317 window_draw_decorations(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500318
319 window->new_surface = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500320}
321
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400322cairo_surface_t *
323window_get_surface(struct window *window)
324{
325 return window->cairo_surface;
326}
327
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500328enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400329 WINDOW_MOVING = 0,
330 WINDOW_RESIZING_TOP = 1,
331 WINDOW_RESIZING_BOTTOM = 2,
332 WINDOW_RESIZING_LEFT = 4,
333 WINDOW_RESIZING_TOP_LEFT = 5,
334 WINDOW_RESIZING_BOTTOM_LEFT = 6,
335 WINDOW_RESIZING_RIGHT = 8,
336 WINDOW_RESIZING_TOP_RIGHT = 9,
337 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
338 WINDOW_RESIZING_MASK = 15,
339 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500340};
341
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500342static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500343window_handle_motion(void *data, struct wl_input_device *input_device,
344 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500345{
346 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500347
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500348 switch (window->state) {
349 case WINDOW_MOVING:
350 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500351 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500352 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500353 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500354 window->allocation.x = window->drag_x + x;
355 window->allocation.y = window->drag_y + y;
356 wl_surface_map(window->surface,
357 window->allocation.x - window->margin,
358 window->allocation.y - window->margin,
359 window->allocation.width,
360 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500361 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500362 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400363
364 case WINDOW_RESIZING_TOP:
365 case WINDOW_RESIZING_BOTTOM:
366 case WINDOW_RESIZING_LEFT:
367 case WINDOW_RESIZING_RIGHT:
368 case WINDOW_RESIZING_TOP_LEFT:
369 case WINDOW_RESIZING_TOP_RIGHT:
370 case WINDOW_RESIZING_BOTTOM_LEFT:
371 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500372 if (window->fullscreen)
373 break;
374 if (window->grab_device != input_device)
375 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400376 if (window->state & WINDOW_RESIZING_LEFT) {
377 window->allocation.x = x - window->drag_x + window->saved_allocation.x;
378 window->allocation.width = window->drag_x - x + window->saved_allocation.width;
379 }
380 if (window->state & WINDOW_RESIZING_RIGHT)
381 window->allocation.width = x - window->drag_x + window->saved_allocation.width;
382 if (window->state & WINDOW_RESIZING_TOP) {
383 window->allocation.y = y - window->drag_y + window->saved_allocation.y;
384 window->allocation.height = window->drag_y - y + window->saved_allocation.height;
385 }
386 if (window->state & WINDOW_RESIZING_BOTTOM)
387 window->allocation.height = y - window->drag_y + window->saved_allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500388
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500389 if (window->resize_handler)
390 (*window->resize_handler)(window,
391 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500392
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500393 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500394 }
395}
396
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500397static void window_handle_button(void *data, struct wl_input_device *input_device,
398 uint32_t button, uint32_t state,
399 int32_t x, int32_t y, int32_t sx, int32_t sy)
400{
401 struct window *window = data;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400402 int grip_size = 8, vlocation, hlocation;
403
404 if (window->margin <= sx && sx < window->margin + grip_size)
405 hlocation = WINDOW_RESIZING_LEFT;
406 else if (sx < window->allocation.width - window->margin - grip_size)
407 hlocation = WINDOW_MOVING;
408 else if (sx < window->allocation.width - window->margin)
409 hlocation = WINDOW_RESIZING_RIGHT;
410 else
411 hlocation = WINDOW_STABLE;
412
413 if (window->margin <= sy && sy < window->margin + grip_size)
414 vlocation = WINDOW_RESIZING_TOP;
415 else if (sy < window->allocation.height - window->margin - grip_size)
416 vlocation = WINDOW_MOVING;
417 else if (sy < window->allocation.height - window->margin)
418 vlocation = WINDOW_RESIZING_BOTTOM;
419 else
420 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500421
422 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400423 switch (hlocation | vlocation) {
424 case WINDOW_MOVING:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500425 window->drag_x = window->allocation.x - x;
426 window->drag_y = window->allocation.y - y;
427 window->state = WINDOW_MOVING;
428 window->grab_device = input_device;
429 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400430 case WINDOW_RESIZING_TOP:
431 case WINDOW_RESIZING_BOTTOM:
432 case WINDOW_RESIZING_LEFT:
433 case WINDOW_RESIZING_RIGHT:
434 case WINDOW_RESIZING_TOP_LEFT:
435 case WINDOW_RESIZING_TOP_RIGHT:
436 case WINDOW_RESIZING_BOTTOM_LEFT:
437 case WINDOW_RESIZING_BOTTOM_RIGHT:
438 window->drag_x = x;
439 window->drag_y = y;
440 window->saved_allocation = window->allocation;
441 window->state = hlocation | vlocation;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500442 window->grab_device = input_device;
443 break;
444 default:
445 window->state = WINDOW_STABLE;
446 break;
447 }
448 } else if (button == BTN_LEFT &&
449 state == 0 && window->grab_device == input_device) {
450 window->state = WINDOW_STABLE;
451 }
452}
453
Kristian Høgsberg55444912009-02-21 14:31:09 -0500454
455struct key {
456 uint32_t code[4];
457} evdev_keymap[] = {
458 { { 0, 0 } }, /* 0 */
459 { { 0x1b, 0x1b } },
460 { { '1', '!' } },
461 { { '2', '@' } },
462 { { '3', '#' } },
463 { { '4', '$' } },
464 { { '5', '%' } },
465 { { '6', '^' } },
466 { { '7', '&' } },
467 { { '8', '*' } },
468 { { '9', '(' } },
469 { { '0', ')' } },
470 { { '-', '_' } },
471 { { '=', '+' } },
472 { { '\b', '\b' } },
473 { { '\t', '\t' } },
474
475 { { 'q', 'Q', 0x11 } }, /* 16 */
476 { { 'w', 'W', 0x17 } },
477 { { 'e', 'E', 0x05 } },
478 { { 'r', 'R', 0x12 } },
479 { { 't', 'T', 0x14 } },
480 { { 'y', 'Y', 0x19 } },
481 { { 'u', 'U', 0x15 } },
482 { { 'i', 'I', 0x09 } },
483 { { 'o', 'O', 0x0f } },
484 { { 'p', 'P', 0x10 } },
485 { { '[', '{', 0x1b } },
486 { { ']', '}', 0x1d } },
487 { { '\n', '\n' } },
488 { { 0, 0 } },
489 { { 'a', 'A', 0x01} },
490 { { 's', 'S', 0x13 } },
491
492 { { 'd', 'D', 0x04 } }, /* 32 */
493 { { 'f', 'F', 0x06 } },
494 { { 'g', 'G', 0x07 } },
495 { { 'h', 'H', 0x08 } },
496 { { 'j', 'J', 0x0a } },
497 { { 'k', 'K', 0x0b } },
498 { { 'l', 'L', 0x0c } },
499 { { ';', ':' } },
500 { { '\'', '"' } },
501 { { '`', '~' } },
502 { { 0, 0 } },
503 { { '\\', '|', 0x1c } },
504 { { 'z', 'Z', 0x1a } },
505 { { 'x', 'X', 0x18 } },
506 { { 'c', 'C', 0x03 } },
507 { { 'v', 'V', 0x16 } },
508
509 { { 'b', 'B', 0x02 } }, /* 48 */
510 { { 'n', 'N', 0x0e } },
511 { { 'm', 'M', 0x0d } },
512 { { ',', '<' } },
513 { { '.', '>' } },
514 { { '/', '?' } },
515 { { 0, 0 } },
516 { { '*', '*' } },
517 { { 0, 0 } },
518 { { ' ', ' ' } },
519 { { 0, 0 } }
520
521 /* 59 */
522};
523
524#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
525
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500526static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500527window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500528{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500529 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500530
Kristian Høgsberg55444912009-02-21 14:31:09 -0500531 switch (key) {
532 case KEY_LEFTSHIFT:
533 case KEY_RIGHTSHIFT:
534 mod = WINDOW_MODIFIER_SHIFT;
535 break;
536 case KEY_LEFTCTRL:
537 case KEY_RIGHTCTRL:
538 mod = WINDOW_MODIFIER_CONTROL;
539 break;
540 case KEY_LEFTALT:
541 case KEY_RIGHTALT:
542 mod = WINDOW_MODIFIER_ALT;
543 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500544 }
545
546 if (state)
547 window->modifiers |= mod;
548 else
549 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500550}
551
552static void
553window_handle_key(void *data, struct wl_input_device *input_device,
554 uint32_t key, uint32_t state)
555{
556 struct window *window = data;
557 uint32_t unicode = 0;
558
559 if (window->keyboard_device != input_device)
560 return;
561
562 window_update_modifiers(window, key, state);
563
564 if (key < ARRAY_LENGTH(evdev_keymap)) {
565 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
566 unicode = evdev_keymap[key].code[2];
567 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
568 unicode = evdev_keymap[key].code[1];
569 else
570 unicode = evdev_keymap[key].code[0];
571 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500572
573 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500574 (*window->key_handler)(window, key, unicode,
575 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500576}
577
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500578static void
579window_handle_pointer_focus(void *data,
580 struct wl_input_device *input_device,
581 struct wl_surface *surface)
582{
583}
584
585static void
586window_handle_keyboard_focus(void *data,
587 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500588 struct wl_surface *surface,
589 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500590{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500591 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500592 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500593
594 if (window->keyboard_device == input_device && surface != window->surface)
595 window->keyboard_device = NULL;
596 else if (window->keyboard_device == NULL && surface == window->surface)
597 window->keyboard_device = input_device;
598 else
599 return;
600
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500601 if (window->keyboard_device) {
602 end = keys->data + keys->size;
603 for (k = keys->data; k < end; k++)
604 window_update_modifiers(window, *k, 1);
605 } else {
606 window->modifiers = 0;
607 }
608
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500609 if (window->keyboard_focus_handler)
610 (*window->keyboard_focus_handler)(window,
611 window->keyboard_device,
612 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500613}
614
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500615static const struct wl_input_device_listener input_device_listener = {
616 window_handle_motion,
617 window_handle_button,
618 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500619 window_handle_pointer_focus,
620 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500621};
622
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500623void
624window_get_child_rectangle(struct window *window,
625 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500626{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400627 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500628 *rectangle = window->allocation;
629 } else {
630 rectangle->x = window->margin + 10;
631 rectangle->y = window->margin + 50;
632 rectangle->width = window->allocation.width - 20 - window->margin * 2;
633 rectangle->height = window->allocation.height - 60 - window->margin * 2;
634 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500635}
636
637void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500638window_set_child_size(struct window *window,
639 struct rectangle *rectangle)
640{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400641 int32_t width, height;
642
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500643 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400644 width = rectangle->width + 20 + window->margin * 2;
645 height = rectangle->height + 60 + window->margin * 2;
646 if (window->state & WINDOW_RESIZING_LEFT)
647 window->allocation.x -=
648 width - window->allocation.width;
649 if (window->state & WINDOW_RESIZING_TOP)
650 window->allocation.y -=
651 height - window->allocation.height;
652 window->allocation.width = width;
653 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500654 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500655}
656
657void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400658window_copy_image(struct window *window,
659 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500660{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400661 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500662}
663
664void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500665window_copy_surface(struct window *window,
666 struct rectangle *rectangle,
667 cairo_surface_t *surface)
668{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500669 cairo_t *cr;
670
671 cr = cairo_create (window->cairo_surface);
672
673 cairo_set_source_surface (cr,
674 surface,
675 rectangle->x, rectangle->y);
676
677 cairo_paint (cr);
678 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500679}
680
681void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500682window_set_fullscreen(struct window *window, int fullscreen)
683{
684 window->fullscreen = fullscreen;
685 if (window->fullscreen) {
686 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500687 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500688 } else {
689 window->allocation = window->saved_allocation;
690 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500691}
692
693void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400694window_set_decoration(struct window *window, int decoration)
695{
696 window->decoration = decoration;
697}
698
699void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500700window_set_resize_handler(struct window *window,
701 window_resize_handler_t handler, void *data)
702{
703 window->resize_handler = handler;
704 window->user_data = data;
705}
706
707void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500708window_set_key_handler(struct window *window,
709 window_key_handler_t handler, void *data)
710{
711 window->key_handler = handler;
712 window->user_data = data;
713}
714
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500715void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400716window_set_acknowledge_handler(struct window *window,
717 window_acknowledge_handler_t handler, void *data)
718{
719 window->acknowledge_handler = handler;
720 window->user_data = data;
721}
722
723void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400724window_set_frame_handler(struct window *window,
725 window_frame_handler_t handler, void *data)
726{
727 window->frame_handler = handler;
728 window->user_data = data;
729}
730
731void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500732window_set_keyboard_focus_handler(struct window *window,
733 window_keyboard_focus_handler_t handler, void *data)
734{
735 window->keyboard_focus_handler = handler;
736 window->user_data = data;
737}
738
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400739void
740window_move(struct window *window, int32_t x, int32_t y)
741{
742 window->allocation.x = x;
743 window->allocation.y = y;
744
745 wl_surface_map(window->surface,
746 window->allocation.x - window->margin,
747 window->allocation.y - window->margin,
748 window->allocation.width,
749 window->allocation.height);
750}
751
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500752struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500753window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500754 int32_t x, int32_t y, int32_t width, int32_t height)
755{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500756 struct window *window;
757
758 window = malloc(sizeof *window);
759 if (window == NULL)
760 return NULL;
761
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500762 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500763 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500764 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500765 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500766 window->allocation.x = x;
767 window->allocation.y = y;
768 window->allocation.width = width;
769 window->allocation.height = height;
770 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500771 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500772 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400773 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500774
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400775 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500776
777 wl_input_device_add_listener(display->input_device,
778 &input_device_listener, window);
779
780 return window;
781}
782
783static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400784display_handle_device(void *data,
785 struct wl_compositor *compositor,
786 const char *device)
787{
788 struct display *d = data;
789
790 d->device_name = strdup(device);
791}
792
793static void
794display_handle_acknowledge(void *data,
795 struct wl_compositor *compositor,
796 uint32_t key, uint32_t frame)
797{
798 struct display *d = data;
799 struct window *window;
800 cairo_surface_t *pending;
801
802 /* The acknowledge event means that the server processed our
803 * last commit request and we can now safely free the old
804 * window buffer if we resized and render the next frame into
805 * our back buffer.. */
806 wl_list_for_each(window, &d->window_list, link) {
807 pending = window->pending_surface;
808 window->pending_surface = NULL;
809 if (pending != window->cairo_surface)
810 window_attach_surface(window);
811 cairo_surface_destroy(pending);
812 if (window->acknowledge_handler)
813 (*window->acknowledge_handler)(window, key, frame, window->user_data);
814 }
815}
816
817static void
818display_handle_frame(void *data,
819 struct wl_compositor *compositor,
820 uint32_t frame, uint32_t timestamp)
821{
822 struct display *d = data;
823 struct window *window;
824
825 wl_list_for_each(window, &d->window_list, link) {
826 if (window->frame_handler)
827 (*window->frame_handler)(window, frame,
828 timestamp, window->user_data);
829 }
830}
831
832static const struct wl_compositor_listener compositor_listener = {
833 display_handle_device,
834 display_handle_acknowledge,
835 display_handle_frame,
836};
837
838static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500839display_handle_geometry(void *data,
840 struct wl_output *output,
841 int32_t width, int32_t height)
842{
843 struct display *display = data;
844
845 display->screen_allocation.x = 0;
846 display->screen_allocation.y = 0;
847 display->screen_allocation.width = width;
848 display->screen_allocation.height = height;
849}
850
851static const struct wl_output_listener output_listener = {
852 display_handle_geometry,
853};
854
855static void
856display_handle_global(struct wl_display *display,
857 struct wl_object *object, void *data)
858{
859 struct display *d = data;
860
861 if (wl_object_implements(object, "compositor", 1)) {
862 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400863 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500864 } else if (wl_object_implements(object, "output", 1)) {
865 d->output = (struct wl_output *) object;
866 wl_output_add_listener(d->output, &output_listener, d);
867 } else if (wl_object_implements(object, "input_device", 1)) {
868 d->input_device =(struct wl_input_device *) object;
869 }
870}
871
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400872static const char socket_name[] = "\0wayland";
873
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500874struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400875display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500876{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400877 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500878 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400879 EGLint major, minor, count;
880 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400881 int fd;
882 GOptionContext *context;
883 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400884
885 static const EGLint config_attribs[] = {
886 EGL_SURFACE_TYPE, 0,
887 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
888 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
889 EGL_NONE
890 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500891
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400892 g_type_init();
893
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400894 context = g_option_context_new(NULL);
895 if (option_entries) {
896 g_option_context_add_main_entries(context, option_entries, "Wayland View");
897 if (!g_option_context_parse(context, argc, argv, &error)) {
898 fprintf(stderr, "option parsing failed: %s\n", error->message);
899 exit(EXIT_FAILURE);
900 }
901 }
902
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500903 d = malloc(sizeof *d);
904 if (d == NULL)
905 return NULL;
906
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400907 d->display = wl_display_create(socket_name, sizeof socket_name);
908 if (d->display == NULL) {
909 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400910 return NULL;
911 }
912
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400913 /* Set up listener so we'll catch all events. */
914 wl_display_add_global_listener(d->display,
915 display_handle_global, d);
916
917 /* Process connection events. */
918 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
919
920 fd = open(d->device_name, O_RDWR);
921 if (fd < 0) {
922 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400923 return NULL;
924 }
925
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400926 get_typed_display_mesa =
927 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
928 if (get_typed_display_mesa == NULL) {
929 fprintf(stderr, "eglGetDisplayMESA() not found\n");
930 return NULL;
931 }
932
933 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
934 (void *) fd);
935 if (!eglInitialize(d->dpy, &major, &minor)) {
936 fprintf(stderr, "failed to initialize display\n");
937 return NULL;
938 }
939
940 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
941 count == 0) {
942 fprintf(stderr, "eglChooseConfig() failed\n");
943 return NULL;
944 }
945
946 eglBindAPI(EGL_OPENGL_API);
947
948 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
949 if (d->ctx == NULL) {
950 fprintf(stderr, "failed to create context\n");
951 return NULL;
952 }
953
954 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
955 fprintf(stderr, "faile to make context current\n");
956 return NULL;
957 }
958
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400959 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400960 if (d->device == NULL) {
961 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500962 return NULL;
963 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500964
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400965 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400966 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400967 g_source_attach(d->source, NULL);
968
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400969 wl_list_init(&d->window_list);
970
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500971 return d;
972}
973
974struct wl_compositor *
975display_get_compositor(struct display *display)
976{
977 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500978}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400979
980EGLDisplay
981display_get_egl_display(struct display *d)
982{
983 return d->dpy;
984}
985
986void
987display_run(struct display *d)
988{
989 g_main_loop_run(d->loop);
990}