blob: 2a68a2870ae0e781a74e011533ecaeca6aa2380d [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øgsberg94adf6c2010-06-25 16:50:05 -040042#include <X11/extensions/XKBcommon.h>
43
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050045#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050046#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050047#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040048#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050049
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050050#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050051
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050052struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050053 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050054 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050055 struct wl_output *output;
56 struct wl_input_device *input_device;
57 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040058 EGLDisplay dpy;
59 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010060 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040062 GMainLoop *loop;
63 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040064 struct wl_list window_list;
65 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040066 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040067 struct xkb_desc *xkb;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050068};
69
70struct window {
71 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050072 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073 const char *title;
Kristian Høgsberg09531622010-06-14 23:22:15 -040074 struct rectangle allocation, saved_allocation, surface_allocation;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040075 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050077 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050078 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050080 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040081 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050082 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050083 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050084 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050085 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050086
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040087 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050088 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050089
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050090 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040091 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050092 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050093 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040094 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 window_frame_handler_t frame_handler;
96
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050097 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040098 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050099};
100
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500101static void
102rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
103{
104 cairo_move_to(cr, x0, y0 + radius);
105 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
106 cairo_line_to(cr, x1 - radius, y0);
107 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
108 cairo_line_to(cr, x1, y1 - radius);
109 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
110 cairo_line_to(cr, x0 + radius, y1);
111 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
112 cairo_close_path(cr);
113}
114
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400115static const cairo_user_data_key_t surface_data_key;
116struct surface_data {
117 EGLImageKHR image;
118 GLuint texture;
119 EGLDisplay dpy;
120};
121
122static void
123surface_data_destroy(void *p)
124{
125 struct surface_data *data = p;
126
127 glDeleteTextures(1, &data->texture);
128 eglDestroyImageKHR(data->dpy, data->image);
129}
130
131cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400132display_create_surface(struct display *display,
133 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400134{
135 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400136 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400137 cairo_surface_t *surface;
138
139 EGLint image_attribs[] = {
140 EGL_WIDTH, 0,
141 EGL_HEIGHT, 0,
142 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
143 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
144 EGL_NONE
145 };
146
147 data = malloc(sizeof *data);
148 image_attribs[1] = rectangle->width;
149 image_attribs[3] = rectangle->height;
150 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
151 glGenTextures(1, &data->texture);
152 data->dpy = dpy;
153 glBindTexture(GL_TEXTURE_2D, data->texture);
154 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
155
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400156 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400157 CAIRO_CONTENT_COLOR_ALPHA,
158 data->texture,
159 rectangle->width,
160 rectangle->height);
161
162 cairo_surface_set_user_data (surface, &surface_data_key,
163 data, surface_data_destroy);
164
165 return surface;
166}
167
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500168static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500169window_attach_surface(struct window *window)
170{
171 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400172 struct surface_data *data;
173 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500174
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500175 if (window->pending_surface != NULL)
176 return;
177
Kristian Høgsberg09531622010-06-14 23:22:15 -0400178 window->pending_surface = window->cairo_surface;
179 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500180
Kristian Høgsberg09531622010-06-14 23:22:15 -0400181 data = cairo_surface_get_user_data (window->pending_surface,
182 &surface_data_key);
183 eglExportDRMImageMESA(window->display->dpy,
184 data->image, &name, NULL, &stride);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400185
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500186 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
187 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400188 name,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400189 window->surface_allocation.width,
190 window->surface_allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400191 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500192 visual);
193
194 wl_surface_map(window->surface,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400195 window->surface_allocation.x - window->margin,
196 window->surface_allocation.y - window->margin,
197 window->surface_allocation.width,
198 window->surface_allocation.height);
199
200 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500201}
202
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500203void
204window_commit(struct window *window, uint32_t key)
205{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400206 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500207 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400208 } else {
209 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500210 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500211}
212
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500213static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500214window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500215{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500216 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500217 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400218 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500219 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500220
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500221 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400222 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400223 window->surface_allocation = window->allocation;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400224 width = window->allocation.width;
225 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500226
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500227 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500228 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500229 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
230
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500231 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500232
Kristian Høgsberg09531622010-06-14 23:22:15 -0400233 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400234 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400235 cairo_paint(cr);
236
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400237 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
238 tile_mask(cr, window->display->shadow, 3, 3, width, height, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500239
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400240 if (window->keyboard_device)
241 tile_source(cr, window->display->active_frame,
242 0, 0, width, height, 96);
243 else
244 tile_source(cr, window->display->inactive_frame,
245 0, 0, width, height, 96);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500246
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500247 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
248 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500249 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400250 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500251 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
252 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
253 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400254 if (window->keyboard_device)
255 cairo_set_source_rgb(cr, 0, 0, 0);
256 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400257 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400258 cairo_show_text(cr, window->title);
259
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500260 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500261}
262
263static void
264window_draw_fullscreen(struct window *window)
265{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500266 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400267 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400268 window->surface_allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500269}
270
271void
272window_draw(struct window *window)
273{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500274 if (window->cairo_surface != NULL)
275 cairo_surface_destroy(window->cairo_surface);
276
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400277 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500278 window_draw_fullscreen(window);
279 else
280 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500281}
282
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400283cairo_surface_t *
284window_get_surface(struct window *window)
285{
286 return window->cairo_surface;
287}
288
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400290 WINDOW_MOVING = 0,
291 WINDOW_RESIZING_TOP = 1,
292 WINDOW_RESIZING_BOTTOM = 2,
293 WINDOW_RESIZING_LEFT = 4,
294 WINDOW_RESIZING_TOP_LEFT = 5,
295 WINDOW_RESIZING_BOTTOM_LEFT = 6,
296 WINDOW_RESIZING_RIGHT = 8,
297 WINDOW_RESIZING_TOP_RIGHT = 9,
298 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
299 WINDOW_RESIZING_MASK = 15,
300 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500301};
302
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500303static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500304window_handle_motion(void *data, struct wl_input_device *input_device,
305 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500306{
307 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500308
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500309 switch (window->state) {
310 case WINDOW_MOVING:
311 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500312 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500313 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500314 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500315 window->allocation.x = window->drag_x + x;
316 window->allocation.y = window->drag_y + y;
317 wl_surface_map(window->surface,
318 window->allocation.x - window->margin,
319 window->allocation.y - window->margin,
320 window->allocation.width,
321 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500322 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500323 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400324
325 case WINDOW_RESIZING_TOP:
326 case WINDOW_RESIZING_BOTTOM:
327 case WINDOW_RESIZING_LEFT:
328 case WINDOW_RESIZING_RIGHT:
329 case WINDOW_RESIZING_TOP_LEFT:
330 case WINDOW_RESIZING_TOP_RIGHT:
331 case WINDOW_RESIZING_BOTTOM_LEFT:
332 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500333 if (window->fullscreen)
334 break;
335 if (window->grab_device != input_device)
336 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400337 if (window->state & WINDOW_RESIZING_LEFT) {
338 window->allocation.x = x - window->drag_x + window->saved_allocation.x;
339 window->allocation.width = window->drag_x - x + window->saved_allocation.width;
340 }
341 if (window->state & WINDOW_RESIZING_RIGHT)
342 window->allocation.width = x - window->drag_x + window->saved_allocation.width;
343 if (window->state & WINDOW_RESIZING_TOP) {
344 window->allocation.y = y - window->drag_y + window->saved_allocation.y;
345 window->allocation.height = window->drag_y - y + window->saved_allocation.height;
346 }
347 if (window->state & WINDOW_RESIZING_BOTTOM)
348 window->allocation.height = y - window->drag_y + window->saved_allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500349
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500350 if (window->resize_handler)
351 (*window->resize_handler)(window,
352 window->user_data);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400353 else if (window->redraw_handler)
354 window_schedule_redraw(window);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500355 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500356 }
357}
358
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500359static void window_handle_button(void *data, struct wl_input_device *input_device,
360 uint32_t button, uint32_t state,
361 int32_t x, int32_t y, int32_t sx, int32_t sy)
362{
363 struct window *window = data;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400364 int grip_size = 8, vlocation, hlocation;
365
366 if (window->margin <= sx && sx < window->margin + grip_size)
367 hlocation = WINDOW_RESIZING_LEFT;
368 else if (sx < window->allocation.width - window->margin - grip_size)
369 hlocation = WINDOW_MOVING;
370 else if (sx < window->allocation.width - window->margin)
371 hlocation = WINDOW_RESIZING_RIGHT;
372 else
373 hlocation = WINDOW_STABLE;
374
375 if (window->margin <= sy && sy < window->margin + grip_size)
376 vlocation = WINDOW_RESIZING_TOP;
377 else if (sy < window->allocation.height - window->margin - grip_size)
378 vlocation = WINDOW_MOVING;
379 else if (sy < window->allocation.height - window->margin)
380 vlocation = WINDOW_RESIZING_BOTTOM;
381 else
382 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500383
384 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400385 switch (hlocation | vlocation) {
386 case WINDOW_MOVING:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500387 window->drag_x = window->allocation.x - x;
388 window->drag_y = window->allocation.y - y;
389 window->state = WINDOW_MOVING;
390 window->grab_device = input_device;
391 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400392 case WINDOW_RESIZING_TOP:
393 case WINDOW_RESIZING_BOTTOM:
394 case WINDOW_RESIZING_LEFT:
395 case WINDOW_RESIZING_RIGHT:
396 case WINDOW_RESIZING_TOP_LEFT:
397 case WINDOW_RESIZING_TOP_RIGHT:
398 case WINDOW_RESIZING_BOTTOM_LEFT:
399 case WINDOW_RESIZING_BOTTOM_RIGHT:
400 window->drag_x = x;
401 window->drag_y = y;
402 window->saved_allocation = window->allocation;
403 window->state = hlocation | vlocation;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500404 window->grab_device = input_device;
405 break;
406 default:
407 window->state = WINDOW_STABLE;
408 break;
409 }
410 } else if (button == BTN_LEFT &&
411 state == 0 && window->grab_device == input_device) {
412 window->state = WINDOW_STABLE;
413 }
414}
415
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500416static void
417window_handle_key(void *data, struct wl_input_device *input_device,
418 uint32_t key, uint32_t state)
419{
420 struct window *window = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400421 struct display *d = window->display;
422 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500423
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400424 code = key + d->xkb->min_key_code;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500425 if (window->keyboard_device != input_device)
426 return;
427
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400428 level = 0;
429 if (window->modifiers & WINDOW_MODIFIER_SHIFT &&
430 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
431 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500432
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400433 sym = XkbKeySymEntry(d->xkb, code, level, 0);
434
435 if (state)
436 window->modifiers |= d->xkb->map->modmap[code];
437 else
438 window->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500439
440 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400441 (*window->key_handler)(window, key, sym, state,
442 window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500443}
444
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500445static void
446window_handle_pointer_focus(void *data,
447 struct wl_input_device *input_device,
448 struct wl_surface *surface)
449{
450}
451
452static void
453window_handle_keyboard_focus(void *data,
454 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500455 struct wl_surface *surface,
456 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500457{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500458 struct window *window = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400459 struct display *d = window->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500460 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500461
462 if (window->keyboard_device == input_device && surface != window->surface)
463 window->keyboard_device = NULL;
464 else if (window->keyboard_device == NULL && surface == window->surface)
465 window->keyboard_device = input_device;
466 else
467 return;
468
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500469 if (window->keyboard_device) {
470 end = keys->data + keys->size;
471 for (k = keys->data; k < end; k++)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400472 window->modifiers |= d->xkb->map->modmap[*k];
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500473 } else {
474 window->modifiers = 0;
475 }
476
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500477 if (window->keyboard_focus_handler)
478 (*window->keyboard_focus_handler)(window,
479 window->keyboard_device,
480 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500481}
482
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500483static const struct wl_input_device_listener input_device_listener = {
484 window_handle_motion,
485 window_handle_button,
486 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500487 window_handle_pointer_focus,
488 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500489};
490
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500491void
492window_get_child_rectangle(struct window *window,
493 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500494{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400495 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500496 *rectangle = window->allocation;
497 } else {
498 rectangle->x = window->margin + 10;
499 rectangle->y = window->margin + 50;
500 rectangle->width = window->allocation.width - 20 - window->margin * 2;
501 rectangle->height = window->allocation.height - 60 - window->margin * 2;
502 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500503}
504
505void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500506window_set_child_size(struct window *window,
507 struct rectangle *rectangle)
508{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400509 int32_t width, height;
510
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500511 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400512 width = rectangle->width + 20 + window->margin * 2;
513 height = rectangle->height + 60 + window->margin * 2;
514 if (window->state & WINDOW_RESIZING_LEFT)
515 window->allocation.x -=
516 width - window->allocation.width;
517 if (window->state & WINDOW_RESIZING_TOP)
518 window->allocation.y -=
519 height - window->allocation.height;
520 window->allocation.width = width;
521 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500522 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500523}
524
525void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400526window_copy_image(struct window *window,
527 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500528{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400529 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500530}
531
532void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500533window_copy_surface(struct window *window,
534 struct rectangle *rectangle,
535 cairo_surface_t *surface)
536{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500537 cairo_t *cr;
538
539 cr = cairo_create (window->cairo_surface);
540
541 cairo_set_source_surface (cr,
542 surface,
543 rectangle->x, rectangle->y);
544
545 cairo_paint (cr);
546 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500547}
548
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400549static gboolean
550idle_redraw(void *data)
551{
552 struct window *window = data;
553
554 window->redraw_handler(window, window->user_data);
555 window->redraw_scheduled = 0;
556
557 return FALSE;
558}
559
560void
561window_schedule_redraw(struct window *window)
562{
563 if (!window->redraw_scheduled) {
564 g_idle_add(idle_redraw, window);
565 window->redraw_scheduled = 1;
566 }
567}
568
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500569void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500570window_set_fullscreen(struct window *window, int fullscreen)
571{
572 window->fullscreen = fullscreen;
573 if (window->fullscreen) {
574 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500575 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500576 } else {
577 window->allocation = window->saved_allocation;
578 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500579}
580
581void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400582window_set_decoration(struct window *window, int decoration)
583{
584 window->decoration = decoration;
585}
586
587void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400588window_set_user_data(struct window *window, void *data)
589{
590 window->user_data = data;
591}
592
593void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500594window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400595 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500596{
597 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500598}
599
600void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400601window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400602 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400603{
604 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400605}
606
607void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500608window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400609 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500610{
611 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500612}
613
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500614void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400615window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400616 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400617{
618 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400619}
620
621void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400622window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400623 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400624{
625 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400626}
627
628void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500629window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400630 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500631{
632 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500633}
634
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400635void
636window_move(struct window *window, int32_t x, int32_t y)
637{
638 window->allocation.x = x;
639 window->allocation.y = y;
640
641 wl_surface_map(window->surface,
642 window->allocation.x - window->margin,
643 window->allocation.y - window->margin,
644 window->allocation.width,
645 window->allocation.height);
646}
647
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500648struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500649window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500650 int32_t x, int32_t y, int32_t width, int32_t height)
651{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500652 struct window *window;
653
654 window = malloc(sizeof *window);
655 if (window == NULL)
656 return NULL;
657
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500658 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500659 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500660 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500661 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500662 window->allocation.x = x;
663 window->allocation.y = y;
664 window->allocation.width = width;
665 window->allocation.height = height;
666 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500667 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500668 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400669 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500670
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400671 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500672
673 wl_input_device_add_listener(display->input_device,
674 &input_device_listener, window);
675
676 return window;
677}
678
679static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400680display_handle_device(void *data,
681 struct wl_compositor *compositor,
682 const char *device)
683{
684 struct display *d = data;
685
686 d->device_name = strdup(device);
687}
688
689static void
690display_handle_acknowledge(void *data,
691 struct wl_compositor *compositor,
692 uint32_t key, uint32_t frame)
693{
694 struct display *d = data;
695 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400696
697 /* The acknowledge event means that the server processed our
698 * last commit request and we can now safely free the old
699 * window buffer if we resized and render the next frame into
700 * our back buffer.. */
701 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400702 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400703 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400704 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400705 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400706 if (window->acknowledge_handler)
707 (*window->acknowledge_handler)(window, key, frame, window->user_data);
708 }
709}
710
711static void
712display_handle_frame(void *data,
713 struct wl_compositor *compositor,
714 uint32_t frame, uint32_t timestamp)
715{
716 struct display *d = data;
717 struct window *window;
718
719 wl_list_for_each(window, &d->window_list, link) {
720 if (window->frame_handler)
721 (*window->frame_handler)(window, frame,
722 timestamp, window->user_data);
723 }
724}
725
726static const struct wl_compositor_listener compositor_listener = {
727 display_handle_device,
728 display_handle_acknowledge,
729 display_handle_frame,
730};
731
732static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500733display_handle_geometry(void *data,
734 struct wl_output *output,
735 int32_t width, int32_t height)
736{
737 struct display *display = data;
738
739 display->screen_allocation.x = 0;
740 display->screen_allocation.y = 0;
741 display->screen_allocation.width = width;
742 display->screen_allocation.height = height;
743}
744
745static const struct wl_output_listener output_listener = {
746 display_handle_geometry,
747};
748
749static void
750display_handle_global(struct wl_display *display,
751 struct wl_object *object, void *data)
752{
753 struct display *d = data;
754
755 if (wl_object_implements(object, "compositor", 1)) {
756 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400757 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500758 } else if (wl_object_implements(object, "output", 1)) {
759 d->output = (struct wl_output *) object;
760 wl_output_add_listener(d->output, &output_listener, d);
761 } else if (wl_object_implements(object, "input_device", 1)) {
762 d->input_device =(struct wl_input_device *) object;
763 }
764}
765
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400766static const char socket_name[] = "\0wayland";
767
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400768static void
769display_render_frame(struct display *d)
770{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400771 int radius = 8;
772 cairo_t *cr;
773
774 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
775 cr = cairo_create(d->shadow);
776 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
777 cairo_set_source_rgba(cr, 0, 0, 0, 1);
778 rounded_rect(cr, 16, 16, 112, 112, radius);
779 cairo_fill(cr);
780 cairo_destroy(cr);
781 blur_surface(d->shadow, 64);
782
783 d->active_frame =
784 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
785 cr = cairo_create(d->active_frame);
786 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
787 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
788 rounded_rect(cr, 16, 16, 112, 112, radius);
789 cairo_fill(cr);
790 cairo_destroy(cr);
791
792 d->inactive_frame =
793 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
794 cr = cairo_create(d->inactive_frame);
795 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
796 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
797 rounded_rect(cr, 16, 16, 112, 112, radius);
798 cairo_fill(cr);
799 cairo_destroy(cr);
800}
801
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400802static void
803init_xkb(struct display *d)
804{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400805 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400806
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400807 names.rules = "evdev";
808 names.model = "pc105";
809 names.layout = "us";
810 names.variant = "";
811 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400812
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400813 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400814 if (!d->xkb) {
815 fprintf(stderr, "Failed to compile keymap\n");
816 exit(1);
817 }
818}
819
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500820struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400821display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500822{
823 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400824 EGLint major, minor, count;
825 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400826 int fd;
827 GOptionContext *context;
828 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400829
830 static const EGLint config_attribs[] = {
831 EGL_SURFACE_TYPE, 0,
832 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
833 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
834 EGL_NONE
835 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500836
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400837 g_type_init();
838
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400839 context = g_option_context_new(NULL);
840 if (option_entries) {
841 g_option_context_add_main_entries(context, option_entries, "Wayland View");
842 if (!g_option_context_parse(context, argc, argv, &error)) {
843 fprintf(stderr, "option parsing failed: %s\n", error->message);
844 exit(EXIT_FAILURE);
845 }
846 }
847
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500848 d = malloc(sizeof *d);
849 if (d == NULL)
850 return NULL;
851
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400852 d->display = wl_display_create(socket_name, sizeof socket_name);
853 if (d->display == NULL) {
854 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400855 return NULL;
856 }
857
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400858 /* Set up listener so we'll catch all events. */
859 wl_display_add_global_listener(d->display,
860 display_handle_global, d);
861
862 /* Process connection events. */
863 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
864
865 fd = open(d->device_name, O_RDWR);
866 if (fd < 0) {
867 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400868 return NULL;
869 }
870
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400871 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400872 if (!eglInitialize(d->dpy, &major, &minor)) {
873 fprintf(stderr, "failed to initialize display\n");
874 return NULL;
875 }
876
877 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
878 count == 0) {
879 fprintf(stderr, "eglChooseConfig() failed\n");
880 return NULL;
881 }
882
883 eglBindAPI(EGL_OPENGL_API);
884
885 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
886 if (d->ctx == NULL) {
887 fprintf(stderr, "failed to create context\n");
888 return NULL;
889 }
890
891 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
892 fprintf(stderr, "faile to make context current\n");
893 return NULL;
894 }
895
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400896 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400897 if (d->device == NULL) {
898 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500899 return NULL;
900 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500901
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400902 display_render_frame(d);
903
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400904 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400905 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400906 g_source_attach(d->source, NULL);
907
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400908 wl_list_init(&d->window_list);
909
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400910 init_xkb(d);
911
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500912 return d;
913}
914
915struct wl_compositor *
916display_get_compositor(struct display *display)
917{
918 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500919}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400920
921EGLDisplay
922display_get_egl_display(struct display *d)
923{
924 return d->dpy;
925}
926
927void
928display_run(struct display *d)
929{
930 g_main_loop_run(d->loop);
931}