blob: c1cc2dd8f4045411efb7c2482e163a81c928e1c3 [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øgsberg09531622010-06-14 23:22:15 -040070 struct rectangle allocation, saved_allocation, surface_allocation;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040071 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050073 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050074 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050075 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050076 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040077 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050078 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050079 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050080 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050081 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050082
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040083 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050084 cairo_surface_t *cairo_surface, *pending_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øgsberg80d746f2010-06-14 23:52:50 -040087 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050088 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050089 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040090 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040091 window_frame_handler_t frame_handler;
92
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050093 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040094 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050095};
96
Kristian Høgsberge4feb562008-11-08 18:53:37 -050097static void
98rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
99{
100 cairo_move_to(cr, x0, y0 + radius);
101 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
102 cairo_line_to(cr, x1 - radius, y0);
103 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
104 cairo_line_to(cr, x1, y1 - radius);
105 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
106 cairo_line_to(cr, x0 + radius, y1);
107 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
108 cairo_close_path(cr);
109}
110
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400111static const cairo_user_data_key_t surface_data_key;
112struct surface_data {
113 EGLImageKHR image;
114 GLuint texture;
115 EGLDisplay dpy;
116};
117
118static void
119surface_data_destroy(void *p)
120{
121 struct surface_data *data = p;
122
123 glDeleteTextures(1, &data->texture);
124 eglDestroyImageKHR(data->dpy, data->image);
125}
126
127cairo_surface_t *
128window_create_surface(struct window *window,
129 struct rectangle *rectangle)
130{
131 struct surface_data *data;
132 EGLDisplay dpy = window->display->dpy;
133 cairo_surface_t *surface;
134
135 EGLint image_attribs[] = {
136 EGL_WIDTH, 0,
137 EGL_HEIGHT, 0,
138 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
139 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
140 EGL_NONE
141 };
142
143 data = malloc(sizeof *data);
144 image_attribs[1] = rectangle->width;
145 image_attribs[3] = rectangle->height;
146 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
147 glGenTextures(1, &data->texture);
148 data->dpy = dpy;
149 glBindTexture(GL_TEXTURE_2D, data->texture);
150 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
151
152 surface = cairo_gl_surface_create_for_texture(window->display->device,
153 CAIRO_CONTENT_COLOR_ALPHA,
154 data->texture,
155 rectangle->width,
156 rectangle->height);
157
158 cairo_surface_set_user_data (surface, &surface_data_key,
159 data, surface_data_destroy);
160
161 return surface;
162}
163
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500164static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500165window_attach_surface(struct window *window)
166{
167 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400168 struct surface_data *data;
169 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500170
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500171 if (window->pending_surface != NULL)
172 return;
173
Kristian Høgsberg09531622010-06-14 23:22:15 -0400174 window->pending_surface = window->cairo_surface;
175 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500176
Kristian Høgsberg09531622010-06-14 23:22:15 -0400177 data = cairo_surface_get_user_data (window->pending_surface,
178 &surface_data_key);
179 eglExportDRMImageMESA(window->display->dpy,
180 data->image, &name, NULL, &stride);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400181
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500182 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
183 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400184 name,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400185 window->surface_allocation.width,
186 window->surface_allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400187 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500188 visual);
189
190 wl_surface_map(window->surface,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400191 window->surface_allocation.x - window->margin,
192 window->surface_allocation.y - window->margin,
193 window->surface_allocation.width,
194 window->surface_allocation.height);
195
196 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500197}
198
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500199void
200window_commit(struct window *window, uint32_t key)
201{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400202 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500203 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400204 } else {
205 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500206 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500207}
208
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500209static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500210window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500211{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500212 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500213 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500214 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500215 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500216 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400217 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500218
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500219 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400220 window_create_surface(window, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400221 window->surface_allocation = window->allocation;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500222
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500223 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500224 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500225 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
226
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500227 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500228
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500229 width = window->allocation.width - window->margin * 2;
230 height = window->allocation.height - window->margin * 2;
231
Kristian Høgsberg09531622010-06-14 23:22:15 -0400232 cairo_set_source_rgba(cr, 0, 0, 0, 0);
233 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
234 cairo_paint(cr);
235
236 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400237 cairo_translate(cr, window->margin + shadow_dx,
238 window->margin + shadow_dy);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500239 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500240 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400241 rounded_rect(cr, -1, -1, width + 1, height + 1, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500242 cairo_fill(cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500243
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400244#define SLOW_BUT_PWETTY_not_right_now
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500245#ifdef SLOW_BUT_PWETTY
246 /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
247 * Ideally we should have convolution filters in cairo, but we
248 * can also fallback to compositing the shadow image a bunch
249 * of times according to the blur kernel. */
250 {
251 cairo_surface_t *map;
252
253 map = cairo_drm_surface_map(window->cairo_surface);
Kristian Høgsberg0acc6c42009-03-05 07:49:42 -0500254 blur_surface(map, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500255 cairo_drm_surface_unmap(window->cairo_surface, map);
256 }
257#endif
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500258
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400259 cairo_translate(cr, -shadow_dx, -shadow_dy);
260 if (window->keyboard_device) {
261 rounded_rect(cr, 0, 0, width, height, radius);
262 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
263 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.6);
264 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.8);
265 cairo_set_source(cr, gradient);
266 cairo_fill(cr);
267 cairo_pattern_destroy(gradient);
268 } else {
269 rounded_rect(cr, 0, 0, width, height, radius);
270 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
271 cairo_fill(cr);
272 }
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500273
274 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
275 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500276 cairo_line_to(cr, width - 10, 50);
277 cairo_line_to(cr, width - 10, height - 10);
278 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500279 cairo_close_path(cr);
280 cairo_set_source(cr, dim);
281 cairo_stroke(cr);
282
283 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500284 cairo_line_to(cr, width - 10, 51);
285 cairo_line_to(cr, width - 10, height - 10);
286 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500287 cairo_close_path(cr);
288 cairo_set_source(cr, bright);
289 cairo_stroke(cr);
290
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500291 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
292 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500293 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500294 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500295 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
296 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
297 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500298 cairo_text_path(cr, window->title);
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400299 if (window->keyboard_device) {
300 cairo_set_source_rgb(cr, 0.56, 0.56, 0.56);
301 cairo_stroke_preserve(cr);
302 cairo_set_source_rgb(cr, 1, 1, 1);
303 cairo_fill(cr);
304 } else {
305 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
306 cairo_fill(cr);
307 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500308 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500309}
310
311static void
312window_draw_fullscreen(struct window *window)
313{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500314 window->cairo_surface =
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400315 window_create_surface(window, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400316 window->surface_allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500317}
318
319void
320window_draw(struct window *window)
321{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500322 if (window->cairo_surface != NULL)
323 cairo_surface_destroy(window->cairo_surface);
324
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400325 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500326 window_draw_fullscreen(window);
327 else
328 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500329}
330
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400331cairo_surface_t *
332window_get_surface(struct window *window)
333{
334 return window->cairo_surface;
335}
336
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500337enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400338 WINDOW_MOVING = 0,
339 WINDOW_RESIZING_TOP = 1,
340 WINDOW_RESIZING_BOTTOM = 2,
341 WINDOW_RESIZING_LEFT = 4,
342 WINDOW_RESIZING_TOP_LEFT = 5,
343 WINDOW_RESIZING_BOTTOM_LEFT = 6,
344 WINDOW_RESIZING_RIGHT = 8,
345 WINDOW_RESIZING_TOP_RIGHT = 9,
346 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
347 WINDOW_RESIZING_MASK = 15,
348 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500349};
350
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500351static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500352window_handle_motion(void *data, struct wl_input_device *input_device,
353 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500354{
355 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500356
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500357 switch (window->state) {
358 case WINDOW_MOVING:
359 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500360 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500361 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500362 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500363 window->allocation.x = window->drag_x + x;
364 window->allocation.y = window->drag_y + y;
365 wl_surface_map(window->surface,
366 window->allocation.x - window->margin,
367 window->allocation.y - window->margin,
368 window->allocation.width,
369 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500370 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500371 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400372
373 case WINDOW_RESIZING_TOP:
374 case WINDOW_RESIZING_BOTTOM:
375 case WINDOW_RESIZING_LEFT:
376 case WINDOW_RESIZING_RIGHT:
377 case WINDOW_RESIZING_TOP_LEFT:
378 case WINDOW_RESIZING_TOP_RIGHT:
379 case WINDOW_RESIZING_BOTTOM_LEFT:
380 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500381 if (window->fullscreen)
382 break;
383 if (window->grab_device != input_device)
384 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400385 if (window->state & WINDOW_RESIZING_LEFT) {
386 window->allocation.x = x - window->drag_x + window->saved_allocation.x;
387 window->allocation.width = window->drag_x - x + window->saved_allocation.width;
388 }
389 if (window->state & WINDOW_RESIZING_RIGHT)
390 window->allocation.width = x - window->drag_x + window->saved_allocation.width;
391 if (window->state & WINDOW_RESIZING_TOP) {
392 window->allocation.y = y - window->drag_y + window->saved_allocation.y;
393 window->allocation.height = window->drag_y - y + window->saved_allocation.height;
394 }
395 if (window->state & WINDOW_RESIZING_BOTTOM)
396 window->allocation.height = y - window->drag_y + window->saved_allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500397
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500398 if (window->resize_handler)
399 (*window->resize_handler)(window,
400 window->user_data);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400401 else if (window->redraw_handler)
402 window_schedule_redraw(window);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500403 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500404 }
405}
406
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500407static void window_handle_button(void *data, struct wl_input_device *input_device,
408 uint32_t button, uint32_t state,
409 int32_t x, int32_t y, int32_t sx, int32_t sy)
410{
411 struct window *window = data;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400412 int grip_size = 8, vlocation, hlocation;
413
414 if (window->margin <= sx && sx < window->margin + grip_size)
415 hlocation = WINDOW_RESIZING_LEFT;
416 else if (sx < window->allocation.width - window->margin - grip_size)
417 hlocation = WINDOW_MOVING;
418 else if (sx < window->allocation.width - window->margin)
419 hlocation = WINDOW_RESIZING_RIGHT;
420 else
421 hlocation = WINDOW_STABLE;
422
423 if (window->margin <= sy && sy < window->margin + grip_size)
424 vlocation = WINDOW_RESIZING_TOP;
425 else if (sy < window->allocation.height - window->margin - grip_size)
426 vlocation = WINDOW_MOVING;
427 else if (sy < window->allocation.height - window->margin)
428 vlocation = WINDOW_RESIZING_BOTTOM;
429 else
430 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500431
432 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400433 switch (hlocation | vlocation) {
434 case WINDOW_MOVING:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500435 window->drag_x = window->allocation.x - x;
436 window->drag_y = window->allocation.y - y;
437 window->state = WINDOW_MOVING;
438 window->grab_device = input_device;
439 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400440 case WINDOW_RESIZING_TOP:
441 case WINDOW_RESIZING_BOTTOM:
442 case WINDOW_RESIZING_LEFT:
443 case WINDOW_RESIZING_RIGHT:
444 case WINDOW_RESIZING_TOP_LEFT:
445 case WINDOW_RESIZING_TOP_RIGHT:
446 case WINDOW_RESIZING_BOTTOM_LEFT:
447 case WINDOW_RESIZING_BOTTOM_RIGHT:
448 window->drag_x = x;
449 window->drag_y = y;
450 window->saved_allocation = window->allocation;
451 window->state = hlocation | vlocation;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500452 window->grab_device = input_device;
453 break;
454 default:
455 window->state = WINDOW_STABLE;
456 break;
457 }
458 } else if (button == BTN_LEFT &&
459 state == 0 && window->grab_device == input_device) {
460 window->state = WINDOW_STABLE;
461 }
462}
463
Kristian Høgsberg55444912009-02-21 14:31:09 -0500464
465struct key {
466 uint32_t code[4];
467} evdev_keymap[] = {
468 { { 0, 0 } }, /* 0 */
469 { { 0x1b, 0x1b } },
470 { { '1', '!' } },
471 { { '2', '@' } },
472 { { '3', '#' } },
473 { { '4', '$' } },
474 { { '5', '%' } },
475 { { '6', '^' } },
476 { { '7', '&' } },
477 { { '8', '*' } },
478 { { '9', '(' } },
479 { { '0', ')' } },
480 { { '-', '_' } },
481 { { '=', '+' } },
482 { { '\b', '\b' } },
483 { { '\t', '\t' } },
484
485 { { 'q', 'Q', 0x11 } }, /* 16 */
486 { { 'w', 'W', 0x17 } },
487 { { 'e', 'E', 0x05 } },
488 { { 'r', 'R', 0x12 } },
489 { { 't', 'T', 0x14 } },
490 { { 'y', 'Y', 0x19 } },
491 { { 'u', 'U', 0x15 } },
492 { { 'i', 'I', 0x09 } },
493 { { 'o', 'O', 0x0f } },
494 { { 'p', 'P', 0x10 } },
495 { { '[', '{', 0x1b } },
496 { { ']', '}', 0x1d } },
497 { { '\n', '\n' } },
498 { { 0, 0 } },
499 { { 'a', 'A', 0x01} },
500 { { 's', 'S', 0x13 } },
501
502 { { 'd', 'D', 0x04 } }, /* 32 */
503 { { 'f', 'F', 0x06 } },
504 { { 'g', 'G', 0x07 } },
505 { { 'h', 'H', 0x08 } },
506 { { 'j', 'J', 0x0a } },
507 { { 'k', 'K', 0x0b } },
508 { { 'l', 'L', 0x0c } },
509 { { ';', ':' } },
510 { { '\'', '"' } },
511 { { '`', '~' } },
512 { { 0, 0 } },
513 { { '\\', '|', 0x1c } },
514 { { 'z', 'Z', 0x1a } },
515 { { 'x', 'X', 0x18 } },
516 { { 'c', 'C', 0x03 } },
517 { { 'v', 'V', 0x16 } },
518
519 { { 'b', 'B', 0x02 } }, /* 48 */
520 { { 'n', 'N', 0x0e } },
521 { { 'm', 'M', 0x0d } },
522 { { ',', '<' } },
523 { { '.', '>' } },
524 { { '/', '?' } },
525 { { 0, 0 } },
526 { { '*', '*' } },
527 { { 0, 0 } },
528 { { ' ', ' ' } },
529 { { 0, 0 } }
530
531 /* 59 */
532};
533
534#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
535
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500536static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500537window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500538{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500539 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500540
Kristian Høgsberg55444912009-02-21 14:31:09 -0500541 switch (key) {
542 case KEY_LEFTSHIFT:
543 case KEY_RIGHTSHIFT:
544 mod = WINDOW_MODIFIER_SHIFT;
545 break;
546 case KEY_LEFTCTRL:
547 case KEY_RIGHTCTRL:
548 mod = WINDOW_MODIFIER_CONTROL;
549 break;
550 case KEY_LEFTALT:
551 case KEY_RIGHTALT:
552 mod = WINDOW_MODIFIER_ALT;
553 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500554 }
555
556 if (state)
557 window->modifiers |= mod;
558 else
559 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500560}
561
562static void
563window_handle_key(void *data, struct wl_input_device *input_device,
564 uint32_t key, uint32_t state)
565{
566 struct window *window = data;
567 uint32_t unicode = 0;
568
569 if (window->keyboard_device != input_device)
570 return;
571
572 window_update_modifiers(window, key, state);
573
574 if (key < ARRAY_LENGTH(evdev_keymap)) {
575 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
576 unicode = evdev_keymap[key].code[2];
577 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
578 unicode = evdev_keymap[key].code[1];
579 else
580 unicode = evdev_keymap[key].code[0];
581 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500582
583 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500584 (*window->key_handler)(window, key, unicode,
585 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500586}
587
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500588static void
589window_handle_pointer_focus(void *data,
590 struct wl_input_device *input_device,
591 struct wl_surface *surface)
592{
593}
594
595static void
596window_handle_keyboard_focus(void *data,
597 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500598 struct wl_surface *surface,
599 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500600{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500601 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500602 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500603
604 if (window->keyboard_device == input_device && surface != window->surface)
605 window->keyboard_device = NULL;
606 else if (window->keyboard_device == NULL && surface == window->surface)
607 window->keyboard_device = input_device;
608 else
609 return;
610
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500611 if (window->keyboard_device) {
612 end = keys->data + keys->size;
613 for (k = keys->data; k < end; k++)
614 window_update_modifiers(window, *k, 1);
615 } else {
616 window->modifiers = 0;
617 }
618
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500619 if (window->keyboard_focus_handler)
620 (*window->keyboard_focus_handler)(window,
621 window->keyboard_device,
622 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500623}
624
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500625static const struct wl_input_device_listener input_device_listener = {
626 window_handle_motion,
627 window_handle_button,
628 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500629 window_handle_pointer_focus,
630 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500631};
632
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500633void
634window_get_child_rectangle(struct window *window,
635 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500636{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400637 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500638 *rectangle = window->allocation;
639 } else {
640 rectangle->x = window->margin + 10;
641 rectangle->y = window->margin + 50;
642 rectangle->width = window->allocation.width - 20 - window->margin * 2;
643 rectangle->height = window->allocation.height - 60 - window->margin * 2;
644 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500645}
646
647void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500648window_set_child_size(struct window *window,
649 struct rectangle *rectangle)
650{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400651 int32_t width, height;
652
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500653 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400654 width = rectangle->width + 20 + window->margin * 2;
655 height = rectangle->height + 60 + window->margin * 2;
656 if (window->state & WINDOW_RESIZING_LEFT)
657 window->allocation.x -=
658 width - window->allocation.width;
659 if (window->state & WINDOW_RESIZING_TOP)
660 window->allocation.y -=
661 height - window->allocation.height;
662 window->allocation.width = width;
663 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500664 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500665}
666
667void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400668window_copy_image(struct window *window,
669 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500670{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400671 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500672}
673
674void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500675window_copy_surface(struct window *window,
676 struct rectangle *rectangle,
677 cairo_surface_t *surface)
678{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500679 cairo_t *cr;
680
681 cr = cairo_create (window->cairo_surface);
682
683 cairo_set_source_surface (cr,
684 surface,
685 rectangle->x, rectangle->y);
686
687 cairo_paint (cr);
688 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500689}
690
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400691static gboolean
692idle_redraw(void *data)
693{
694 struct window *window = data;
695
696 window->redraw_handler(window, window->user_data);
697 window->redraw_scheduled = 0;
698
699 return FALSE;
700}
701
702void
703window_schedule_redraw(struct window *window)
704{
705 if (!window->redraw_scheduled) {
706 g_idle_add(idle_redraw, window);
707 window->redraw_scheduled = 1;
708 }
709}
710
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500711void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500712window_set_fullscreen(struct window *window, int fullscreen)
713{
714 window->fullscreen = fullscreen;
715 if (window->fullscreen) {
716 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500717 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500718 } else {
719 window->allocation = window->saved_allocation;
720 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500721}
722
723void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400724window_set_decoration(struct window *window, int decoration)
725{
726 window->decoration = decoration;
727}
728
729void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500730window_set_resize_handler(struct window *window,
731 window_resize_handler_t handler, void *data)
732{
733 window->resize_handler = handler;
734 window->user_data = data;
735}
736
737void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400738window_set_redraw_handler(struct window *window,
739 window_redraw_handler_t handler, void *data)
740{
741 window->redraw_handler = handler;
742 window->user_data = data;
743}
744
745void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500746window_set_key_handler(struct window *window,
747 window_key_handler_t handler, void *data)
748{
749 window->key_handler = handler;
750 window->user_data = data;
751}
752
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500753void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400754window_set_acknowledge_handler(struct window *window,
755 window_acknowledge_handler_t handler, void *data)
756{
757 window->acknowledge_handler = handler;
758 window->user_data = data;
759}
760
761void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400762window_set_frame_handler(struct window *window,
763 window_frame_handler_t handler, void *data)
764{
765 window->frame_handler = handler;
766 window->user_data = data;
767}
768
769void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500770window_set_keyboard_focus_handler(struct window *window,
771 window_keyboard_focus_handler_t handler, void *data)
772{
773 window->keyboard_focus_handler = handler;
774 window->user_data = data;
775}
776
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400777void
778window_move(struct window *window, int32_t x, int32_t y)
779{
780 window->allocation.x = x;
781 window->allocation.y = y;
782
783 wl_surface_map(window->surface,
784 window->allocation.x - window->margin,
785 window->allocation.y - window->margin,
786 window->allocation.width,
787 window->allocation.height);
788}
789
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500790struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500791window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500792 int32_t x, int32_t y, int32_t width, int32_t height)
793{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500794 struct window *window;
795
796 window = malloc(sizeof *window);
797 if (window == NULL)
798 return NULL;
799
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500800 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500801 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500802 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500803 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500804 window->allocation.x = x;
805 window->allocation.y = y;
806 window->allocation.width = width;
807 window->allocation.height = height;
808 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500809 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500810 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400811 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500812
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400813 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500814
815 wl_input_device_add_listener(display->input_device,
816 &input_device_listener, window);
817
818 return window;
819}
820
821static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400822display_handle_device(void *data,
823 struct wl_compositor *compositor,
824 const char *device)
825{
826 struct display *d = data;
827
828 d->device_name = strdup(device);
829}
830
831static void
832display_handle_acknowledge(void *data,
833 struct wl_compositor *compositor,
834 uint32_t key, uint32_t frame)
835{
836 struct display *d = data;
837 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400838
839 /* The acknowledge event means that the server processed our
840 * last commit request and we can now safely free the old
841 * window buffer if we resized and render the next frame into
842 * our back buffer.. */
843 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400844 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400845 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400846 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400847 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400848 if (window->acknowledge_handler)
849 (*window->acknowledge_handler)(window, key, frame, window->user_data);
850 }
851}
852
853static void
854display_handle_frame(void *data,
855 struct wl_compositor *compositor,
856 uint32_t frame, uint32_t timestamp)
857{
858 struct display *d = data;
859 struct window *window;
860
861 wl_list_for_each(window, &d->window_list, link) {
862 if (window->frame_handler)
863 (*window->frame_handler)(window, frame,
864 timestamp, window->user_data);
865 }
866}
867
868static const struct wl_compositor_listener compositor_listener = {
869 display_handle_device,
870 display_handle_acknowledge,
871 display_handle_frame,
872};
873
874static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500875display_handle_geometry(void *data,
876 struct wl_output *output,
877 int32_t width, int32_t height)
878{
879 struct display *display = data;
880
881 display->screen_allocation.x = 0;
882 display->screen_allocation.y = 0;
883 display->screen_allocation.width = width;
884 display->screen_allocation.height = height;
885}
886
887static const struct wl_output_listener output_listener = {
888 display_handle_geometry,
889};
890
891static void
892display_handle_global(struct wl_display *display,
893 struct wl_object *object, void *data)
894{
895 struct display *d = data;
896
897 if (wl_object_implements(object, "compositor", 1)) {
898 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400899 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500900 } else if (wl_object_implements(object, "output", 1)) {
901 d->output = (struct wl_output *) object;
902 wl_output_add_listener(d->output, &output_listener, d);
903 } else if (wl_object_implements(object, "input_device", 1)) {
904 d->input_device =(struct wl_input_device *) object;
905 }
906}
907
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400908static const char socket_name[] = "\0wayland";
909
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500910struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400911display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500912{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400913 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500914 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400915 EGLint major, minor, count;
916 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400917 int fd;
918 GOptionContext *context;
919 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400920
921 static const EGLint config_attribs[] = {
922 EGL_SURFACE_TYPE, 0,
923 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
924 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
925 EGL_NONE
926 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500927
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400928 g_type_init();
929
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400930 context = g_option_context_new(NULL);
931 if (option_entries) {
932 g_option_context_add_main_entries(context, option_entries, "Wayland View");
933 if (!g_option_context_parse(context, argc, argv, &error)) {
934 fprintf(stderr, "option parsing failed: %s\n", error->message);
935 exit(EXIT_FAILURE);
936 }
937 }
938
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500939 d = malloc(sizeof *d);
940 if (d == NULL)
941 return NULL;
942
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400943 d->display = wl_display_create(socket_name, sizeof socket_name);
944 if (d->display == NULL) {
945 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400946 return NULL;
947 }
948
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400949 /* Set up listener so we'll catch all events. */
950 wl_display_add_global_listener(d->display,
951 display_handle_global, d);
952
953 /* Process connection events. */
954 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
955
956 fd = open(d->device_name, O_RDWR);
957 if (fd < 0) {
958 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400959 return NULL;
960 }
961
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400962 get_typed_display_mesa =
963 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
964 if (get_typed_display_mesa == NULL) {
965 fprintf(stderr, "eglGetDisplayMESA() not found\n");
966 return NULL;
967 }
968
969 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
970 (void *) fd);
971 if (!eglInitialize(d->dpy, &major, &minor)) {
972 fprintf(stderr, "failed to initialize display\n");
973 return NULL;
974 }
975
976 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
977 count == 0) {
978 fprintf(stderr, "eglChooseConfig() failed\n");
979 return NULL;
980 }
981
982 eglBindAPI(EGL_OPENGL_API);
983
984 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
985 if (d->ctx == NULL) {
986 fprintf(stderr, "failed to create context\n");
987 return NULL;
988 }
989
990 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
991 fprintf(stderr, "faile to make context current\n");
992 return NULL;
993 }
994
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400995 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400996 if (d->device == NULL) {
997 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500998 return NULL;
999 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001000
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001001 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001002 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001003 g_source_attach(d->source, NULL);
1004
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001005 wl_list_init(&d->window_list);
1006
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001007 return d;
1008}
1009
1010struct wl_compositor *
1011display_get_compositor(struct display *display)
1012{
1013 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001014}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001015
1016EGLDisplay
1017display_get_egl_display(struct display *d)
1018{
1019 return d->dpy;
1020}
1021
1022void
1023display_run(struct display *d)
1024{
1025 g_main_loop_run(d->loop);
1026}