blob: 2d88512fefe103df482d741b8d67111982adbe8d [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øgsberg94adf6c2010-06-25 16:50:05 -040067 XkbcDescPtr 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øgsberg40979232008-11-25 22:40:39 -0500217 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500218 cairo_text_extents_t extents;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400219 cairo_pattern_t *pattern, *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500220 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400221 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400222 cairo_matrix_t matrix;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500223
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500224 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400225 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400226 window->surface_allocation = window->allocation;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400227 width = window->allocation.width;
228 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500229
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500230 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500231 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500232 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
233
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500234 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500235
Kristian Høgsberg09531622010-06-14 23:22:15 -0400236 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400237 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400238 cairo_paint(cr);
239
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400240 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
241 tile_mask(cr, window->display->shadow, 3, 3, width, height, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500242
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400243 if (window->keyboard_device)
244 tile_source(cr, window->display->active_frame,
245 0, 0, width, height, 96);
246 else
247 tile_source(cr, window->display->inactive_frame,
248 0, 0, width, height, 96);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500249
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500250 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
251 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500252 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400253 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500254 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
255 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
256 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400257 if (window->keyboard_device)
258 cairo_set_source_rgb(cr, 0, 0, 0);
259 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400260 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400261 cairo_show_text(cr, window->title);
262
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500263 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500264}
265
266static void
267window_draw_fullscreen(struct window *window)
268{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500269 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400270 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400271 window->surface_allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500272}
273
274void
275window_draw(struct window *window)
276{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500277 if (window->cairo_surface != NULL)
278 cairo_surface_destroy(window->cairo_surface);
279
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400280 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500281 window_draw_fullscreen(window);
282 else
283 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500284}
285
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400286cairo_surface_t *
287window_get_surface(struct window *window)
288{
289 return window->cairo_surface;
290}
291
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500292enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400293 WINDOW_MOVING = 0,
294 WINDOW_RESIZING_TOP = 1,
295 WINDOW_RESIZING_BOTTOM = 2,
296 WINDOW_RESIZING_LEFT = 4,
297 WINDOW_RESIZING_TOP_LEFT = 5,
298 WINDOW_RESIZING_BOTTOM_LEFT = 6,
299 WINDOW_RESIZING_RIGHT = 8,
300 WINDOW_RESIZING_TOP_RIGHT = 9,
301 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
302 WINDOW_RESIZING_MASK = 15,
303 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500304};
305
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500306static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500307window_handle_motion(void *data, struct wl_input_device *input_device,
308 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500309{
310 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500311
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500312 switch (window->state) {
313 case WINDOW_MOVING:
314 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500315 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500316 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500317 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500318 window->allocation.x = window->drag_x + x;
319 window->allocation.y = window->drag_y + y;
320 wl_surface_map(window->surface,
321 window->allocation.x - window->margin,
322 window->allocation.y - window->margin,
323 window->allocation.width,
324 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500325 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500326 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400327
328 case WINDOW_RESIZING_TOP:
329 case WINDOW_RESIZING_BOTTOM:
330 case WINDOW_RESIZING_LEFT:
331 case WINDOW_RESIZING_RIGHT:
332 case WINDOW_RESIZING_TOP_LEFT:
333 case WINDOW_RESIZING_TOP_RIGHT:
334 case WINDOW_RESIZING_BOTTOM_LEFT:
335 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500336 if (window->fullscreen)
337 break;
338 if (window->grab_device != input_device)
339 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400340 if (window->state & WINDOW_RESIZING_LEFT) {
341 window->allocation.x = x - window->drag_x + window->saved_allocation.x;
342 window->allocation.width = window->drag_x - x + window->saved_allocation.width;
343 }
344 if (window->state & WINDOW_RESIZING_RIGHT)
345 window->allocation.width = x - window->drag_x + window->saved_allocation.width;
346 if (window->state & WINDOW_RESIZING_TOP) {
347 window->allocation.y = y - window->drag_y + window->saved_allocation.y;
348 window->allocation.height = window->drag_y - y + window->saved_allocation.height;
349 }
350 if (window->state & WINDOW_RESIZING_BOTTOM)
351 window->allocation.height = y - window->drag_y + window->saved_allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500352
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500353 if (window->resize_handler)
354 (*window->resize_handler)(window,
355 window->user_data);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400356 else if (window->redraw_handler)
357 window_schedule_redraw(window);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500358 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500359 }
360}
361
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500362static void window_handle_button(void *data, struct wl_input_device *input_device,
363 uint32_t button, uint32_t state,
364 int32_t x, int32_t y, int32_t sx, int32_t sy)
365{
366 struct window *window = data;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400367 int grip_size = 8, vlocation, hlocation;
368
369 if (window->margin <= sx && sx < window->margin + grip_size)
370 hlocation = WINDOW_RESIZING_LEFT;
371 else if (sx < window->allocation.width - window->margin - grip_size)
372 hlocation = WINDOW_MOVING;
373 else if (sx < window->allocation.width - window->margin)
374 hlocation = WINDOW_RESIZING_RIGHT;
375 else
376 hlocation = WINDOW_STABLE;
377
378 if (window->margin <= sy && sy < window->margin + grip_size)
379 vlocation = WINDOW_RESIZING_TOP;
380 else if (sy < window->allocation.height - window->margin - grip_size)
381 vlocation = WINDOW_MOVING;
382 else if (sy < window->allocation.height - window->margin)
383 vlocation = WINDOW_RESIZING_BOTTOM;
384 else
385 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500386
387 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400388 switch (hlocation | vlocation) {
389 case WINDOW_MOVING:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500390 window->drag_x = window->allocation.x - x;
391 window->drag_y = window->allocation.y - y;
392 window->state = WINDOW_MOVING;
393 window->grab_device = input_device;
394 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400395 case WINDOW_RESIZING_TOP:
396 case WINDOW_RESIZING_BOTTOM:
397 case WINDOW_RESIZING_LEFT:
398 case WINDOW_RESIZING_RIGHT:
399 case WINDOW_RESIZING_TOP_LEFT:
400 case WINDOW_RESIZING_TOP_RIGHT:
401 case WINDOW_RESIZING_BOTTOM_LEFT:
402 case WINDOW_RESIZING_BOTTOM_RIGHT:
403 window->drag_x = x;
404 window->drag_y = y;
405 window->saved_allocation = window->allocation;
406 window->state = hlocation | vlocation;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500407 window->grab_device = input_device;
408 break;
409 default:
410 window->state = WINDOW_STABLE;
411 break;
412 }
413 } else if (button == BTN_LEFT &&
414 state == 0 && window->grab_device == input_device) {
415 window->state = WINDOW_STABLE;
416 }
417}
418
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500419static void
420window_handle_key(void *data, struct wl_input_device *input_device,
421 uint32_t key, uint32_t state)
422{
423 struct window *window = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400424 struct display *d = window->display;
425 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500426
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400427 code = key + d->xkb->min_key_code;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500428 if (window->keyboard_device != input_device)
429 return;
430
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400431 level = 0;
432 if (window->modifiers & WINDOW_MODIFIER_SHIFT &&
433 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
434 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500435
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400436 sym = XkbKeySymEntry(d->xkb, code, level, 0);
437
438 if (state)
439 window->modifiers |= d->xkb->map->modmap[code];
440 else
441 window->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500442
443 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400444 (*window->key_handler)(window, key, sym, state,
445 window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500446}
447
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500448static void
449window_handle_pointer_focus(void *data,
450 struct wl_input_device *input_device,
451 struct wl_surface *surface)
452{
453}
454
455static void
456window_handle_keyboard_focus(void *data,
457 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500458 struct wl_surface *surface,
459 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500460{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500461 struct window *window = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400462 struct display *d = window->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500463 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500464
465 if (window->keyboard_device == input_device && surface != window->surface)
466 window->keyboard_device = NULL;
467 else if (window->keyboard_device == NULL && surface == window->surface)
468 window->keyboard_device = input_device;
469 else
470 return;
471
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500472 if (window->keyboard_device) {
473 end = keys->data + keys->size;
474 for (k = keys->data; k < end; k++)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400475 window->modifiers |= d->xkb->map->modmap[*k];
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500476 } else {
477 window->modifiers = 0;
478 }
479
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500480 if (window->keyboard_focus_handler)
481 (*window->keyboard_focus_handler)(window,
482 window->keyboard_device,
483 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500484}
485
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500486static const struct wl_input_device_listener input_device_listener = {
487 window_handle_motion,
488 window_handle_button,
489 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500490 window_handle_pointer_focus,
491 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500492};
493
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500494void
495window_get_child_rectangle(struct window *window,
496 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500497{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400498 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500499 *rectangle = window->allocation;
500 } else {
501 rectangle->x = window->margin + 10;
502 rectangle->y = window->margin + 50;
503 rectangle->width = window->allocation.width - 20 - window->margin * 2;
504 rectangle->height = window->allocation.height - 60 - window->margin * 2;
505 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500506}
507
508void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500509window_set_child_size(struct window *window,
510 struct rectangle *rectangle)
511{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400512 int32_t width, height;
513
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500514 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400515 width = rectangle->width + 20 + window->margin * 2;
516 height = rectangle->height + 60 + window->margin * 2;
517 if (window->state & WINDOW_RESIZING_LEFT)
518 window->allocation.x -=
519 width - window->allocation.width;
520 if (window->state & WINDOW_RESIZING_TOP)
521 window->allocation.y -=
522 height - window->allocation.height;
523 window->allocation.width = width;
524 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500525 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500526}
527
528void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400529window_copy_image(struct window *window,
530 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500531{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400532 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500533}
534
535void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500536window_copy_surface(struct window *window,
537 struct rectangle *rectangle,
538 cairo_surface_t *surface)
539{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500540 cairo_t *cr;
541
542 cr = cairo_create (window->cairo_surface);
543
544 cairo_set_source_surface (cr,
545 surface,
546 rectangle->x, rectangle->y);
547
548 cairo_paint (cr);
549 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500550}
551
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400552static gboolean
553idle_redraw(void *data)
554{
555 struct window *window = data;
556
557 window->redraw_handler(window, window->user_data);
558 window->redraw_scheduled = 0;
559
560 return FALSE;
561}
562
563void
564window_schedule_redraw(struct window *window)
565{
566 if (!window->redraw_scheduled) {
567 g_idle_add(idle_redraw, window);
568 window->redraw_scheduled = 1;
569 }
570}
571
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500572void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500573window_set_fullscreen(struct window *window, int fullscreen)
574{
575 window->fullscreen = fullscreen;
576 if (window->fullscreen) {
577 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500578 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500579 } else {
580 window->allocation = window->saved_allocation;
581 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500582}
583
584void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400585window_set_decoration(struct window *window, int decoration)
586{
587 window->decoration = decoration;
588}
589
590void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400591window_set_user_data(struct window *window, void *data)
592{
593 window->user_data = data;
594}
595
596void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500597window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400598 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500599{
600 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500601}
602
603void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400604window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400605 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400606{
607 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400608}
609
610void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500611window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400612 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500613{
614 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500615}
616
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500617void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400618window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400619 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400620{
621 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400622}
623
624void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400625window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400626 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400627{
628 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400629}
630
631void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500632window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400633 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500634{
635 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500636}
637
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400638void
639window_move(struct window *window, int32_t x, int32_t y)
640{
641 window->allocation.x = x;
642 window->allocation.y = y;
643
644 wl_surface_map(window->surface,
645 window->allocation.x - window->margin,
646 window->allocation.y - window->margin,
647 window->allocation.width,
648 window->allocation.height);
649}
650
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500651struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500652window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500653 int32_t x, int32_t y, int32_t width, int32_t height)
654{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500655 struct window *window;
656
657 window = malloc(sizeof *window);
658 if (window == NULL)
659 return NULL;
660
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500661 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500662 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500663 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500664 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500665 window->allocation.x = x;
666 window->allocation.y = y;
667 window->allocation.width = width;
668 window->allocation.height = height;
669 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500670 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500671 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400672 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500673
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400674 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500675
676 wl_input_device_add_listener(display->input_device,
677 &input_device_listener, window);
678
679 return window;
680}
681
682static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400683display_handle_device(void *data,
684 struct wl_compositor *compositor,
685 const char *device)
686{
687 struct display *d = data;
688
689 d->device_name = strdup(device);
690}
691
692static void
693display_handle_acknowledge(void *data,
694 struct wl_compositor *compositor,
695 uint32_t key, uint32_t frame)
696{
697 struct display *d = data;
698 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400699
700 /* The acknowledge event means that the server processed our
701 * last commit request and we can now safely free the old
702 * window buffer if we resized and render the next frame into
703 * our back buffer.. */
704 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400705 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400706 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400707 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400708 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400709 if (window->acknowledge_handler)
710 (*window->acknowledge_handler)(window, key, frame, window->user_data);
711 }
712}
713
714static void
715display_handle_frame(void *data,
716 struct wl_compositor *compositor,
717 uint32_t frame, uint32_t timestamp)
718{
719 struct display *d = data;
720 struct window *window;
721
722 wl_list_for_each(window, &d->window_list, link) {
723 if (window->frame_handler)
724 (*window->frame_handler)(window, frame,
725 timestamp, window->user_data);
726 }
727}
728
729static const struct wl_compositor_listener compositor_listener = {
730 display_handle_device,
731 display_handle_acknowledge,
732 display_handle_frame,
733};
734
735static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500736display_handle_geometry(void *data,
737 struct wl_output *output,
738 int32_t width, int32_t height)
739{
740 struct display *display = data;
741
742 display->screen_allocation.x = 0;
743 display->screen_allocation.y = 0;
744 display->screen_allocation.width = width;
745 display->screen_allocation.height = height;
746}
747
748static const struct wl_output_listener output_listener = {
749 display_handle_geometry,
750};
751
752static void
753display_handle_global(struct wl_display *display,
754 struct wl_object *object, void *data)
755{
756 struct display *d = data;
757
758 if (wl_object_implements(object, "compositor", 1)) {
759 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400760 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500761 } else if (wl_object_implements(object, "output", 1)) {
762 d->output = (struct wl_output *) object;
763 wl_output_add_listener(d->output, &output_listener, d);
764 } else if (wl_object_implements(object, "input_device", 1)) {
765 d->input_device =(struct wl_input_device *) object;
766 }
767}
768
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400769static const char socket_name[] = "\0wayland";
770
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400771static void
772display_render_frame(struct display *d)
773{
774 struct rectangle r = { 0, 0, 128, 128 };
775 int radius = 8;
776 cairo_t *cr;
777
778 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
779 cr = cairo_create(d->shadow);
780 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
781 cairo_set_source_rgba(cr, 0, 0, 0, 1);
782 rounded_rect(cr, 16, 16, 112, 112, radius);
783 cairo_fill(cr);
784 cairo_destroy(cr);
785 blur_surface(d->shadow, 64);
786
787 d->active_frame =
788 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
789 cr = cairo_create(d->active_frame);
790 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
791 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
792 rounded_rect(cr, 16, 16, 112, 112, radius);
793 cairo_fill(cr);
794 cairo_destroy(cr);
795
796 d->inactive_frame =
797 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
798 cr = cairo_create(d->inactive_frame);
799 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
800 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
801 rounded_rect(cr, 16, 16, 112, 112, radius);
802 cairo_fill(cr);
803 cairo_destroy(cr);
804}
805
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400806static void
807init_xkb(struct display *d)
808{
809 XkbRMLVOSet rmlvo;
810 char rules[] = "evdev", model[] = "pc105", layout[] = "us";
811
812 rmlvo.rules = rules;
813 rmlvo.model = model;
814 rmlvo.layout = layout;
815 rmlvo.variant = "";
816 rmlvo.options = "";
817
818 XkbcInitAtoms(NULL, NULL);
819 d->xkb = XkbcCompileKeymapFromRules(&rmlvo);
820 if (!d->xkb) {
821 fprintf(stderr, "Failed to compile keymap\n");
822 exit(1);
823 }
824}
825
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500826struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400827display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500828{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400829 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500830 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400831 EGLint major, minor, count;
832 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400833 int fd;
834 GOptionContext *context;
835 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400836
837 static const EGLint config_attribs[] = {
838 EGL_SURFACE_TYPE, 0,
839 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
840 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
841 EGL_NONE
842 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500843
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400844 g_type_init();
845
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400846 context = g_option_context_new(NULL);
847 if (option_entries) {
848 g_option_context_add_main_entries(context, option_entries, "Wayland View");
849 if (!g_option_context_parse(context, argc, argv, &error)) {
850 fprintf(stderr, "option parsing failed: %s\n", error->message);
851 exit(EXIT_FAILURE);
852 }
853 }
854
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500855 d = malloc(sizeof *d);
856 if (d == NULL)
857 return NULL;
858
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400859 d->display = wl_display_create(socket_name, sizeof socket_name);
860 if (d->display == NULL) {
861 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400862 return NULL;
863 }
864
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400865 /* Set up listener so we'll catch all events. */
866 wl_display_add_global_listener(d->display,
867 display_handle_global, d);
868
869 /* Process connection events. */
870 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
871
872 fd = open(d->device_name, O_RDWR);
873 if (fd < 0) {
874 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400875 return NULL;
876 }
877
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400878 get_typed_display_mesa =
879 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
880 if (get_typed_display_mesa == NULL) {
881 fprintf(stderr, "eglGetDisplayMESA() not found\n");
882 return NULL;
883 }
884
885 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
886 (void *) fd);
887 if (!eglInitialize(d->dpy, &major, &minor)) {
888 fprintf(stderr, "failed to initialize display\n");
889 return NULL;
890 }
891
892 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
893 count == 0) {
894 fprintf(stderr, "eglChooseConfig() failed\n");
895 return NULL;
896 }
897
898 eglBindAPI(EGL_OPENGL_API);
899
900 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
901 if (d->ctx == NULL) {
902 fprintf(stderr, "failed to create context\n");
903 return NULL;
904 }
905
906 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
907 fprintf(stderr, "faile to make context current\n");
908 return NULL;
909 }
910
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400911 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400912 if (d->device == NULL) {
913 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500914 return NULL;
915 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500916
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400917 display_render_frame(d);
918
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400919 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400920 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400921 g_source_attach(d->source, NULL);
922
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400923 wl_list_init(&d->window_list);
924
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400925 init_xkb(d);
926
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500927 return d;
928}
929
930struct wl_compositor *
931display_get_compositor(struct display *display)
932{
933 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500934}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400935
936EGLDisplay
937display_get_egl_display(struct display *d)
938{
939 return d->dpy;
940}
941
942void
943display_run(struct display *d)
944{
945 g_main_loop_run(d->loop);
946}