blob: cac1eb8d4c74621aa4c3675a2e90e40e6946091d [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øgsbergdcb71b62010-06-15 17:16:35 -040064 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065};
66
67struct window {
68 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050069 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050070 const char *title;
Kristian Høgsberg09531622010-06-14 23:22:15 -040071 struct rectangle allocation, saved_allocation, surface_allocation;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040072 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050074 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050075 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050076 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050077 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 int decoration;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050079 struct wl_input_device *grab_device;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050080 struct wl_input_device *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050081 uint32_t name;
Kristian Høgsberg55444912009-02-21 14:31:09 -050082 uint32_t modifiers;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050083
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040084 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050085 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050086
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050087 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040088 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050089 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050090 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040091 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040092 window_frame_handler_t frame_handler;
93
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050094 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040095 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050096};
97
Kristian Høgsberge4feb562008-11-08 18:53:37 -050098static void
99rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
100{
101 cairo_move_to(cr, x0, y0 + radius);
102 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
103 cairo_line_to(cr, x1 - radius, y0);
104 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
105 cairo_line_to(cr, x1, y1 - radius);
106 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
107 cairo_line_to(cr, x0 + radius, y1);
108 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
109 cairo_close_path(cr);
110}
111
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112static const cairo_user_data_key_t surface_data_key;
113struct surface_data {
114 EGLImageKHR image;
115 GLuint texture;
116 EGLDisplay dpy;
117};
118
119static void
120surface_data_destroy(void *p)
121{
122 struct surface_data *data = p;
123
124 glDeleteTextures(1, &data->texture);
125 eglDestroyImageKHR(data->dpy, data->image);
126}
127
128cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400129display_create_surface(struct display *display,
130 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400131{
132 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400133 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400134 cairo_surface_t *surface;
135
136 EGLint image_attribs[] = {
137 EGL_WIDTH, 0,
138 EGL_HEIGHT, 0,
139 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
140 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
141 EGL_NONE
142 };
143
144 data = malloc(sizeof *data);
145 image_attribs[1] = rectangle->width;
146 image_attribs[3] = rectangle->height;
147 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
148 glGenTextures(1, &data->texture);
149 data->dpy = dpy;
150 glBindTexture(GL_TEXTURE_2D, data->texture);
151 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
152
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400153 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400154 CAIRO_CONTENT_COLOR_ALPHA,
155 data->texture,
156 rectangle->width,
157 rectangle->height);
158
159 cairo_surface_set_user_data (surface, &surface_data_key,
160 data, surface_data_destroy);
161
162 return surface;
163}
164
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500165static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500166window_attach_surface(struct window *window)
167{
168 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400169 struct surface_data *data;
170 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500171
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500172 if (window->pending_surface != NULL)
173 return;
174
Kristian Høgsberg09531622010-06-14 23:22:15 -0400175 window->pending_surface = window->cairo_surface;
176 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500177
Kristian Høgsberg09531622010-06-14 23:22:15 -0400178 data = cairo_surface_get_user_data (window->pending_surface,
179 &surface_data_key);
180 eglExportDRMImageMESA(window->display->dpy,
181 data->image, &name, NULL, &stride);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400182
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500183 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
184 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400185 name,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400186 window->surface_allocation.width,
187 window->surface_allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400188 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500189 visual);
190
191 wl_surface_map(window->surface,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400192 window->surface_allocation.x - window->margin,
193 window->surface_allocation.y - window->margin,
194 window->surface_allocation.width,
195 window->surface_allocation.height);
196
197 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500198}
199
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500200void
201window_commit(struct window *window, uint32_t key)
202{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400203 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500204 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400205 } else {
206 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500207 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500208}
209
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500210static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500211window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500212{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500213 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500214 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500215 cairo_text_extents_t extents;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400216 cairo_pattern_t *pattern, *gradient, *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500217 int width, height;
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400218 int shadow_dx = 4, shadow_dy = 4;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400219 cairo_matrix_t matrix;
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øgsberg55444912009-02-21 14:31:09 -0500416
417struct key {
418 uint32_t code[4];
419} evdev_keymap[] = {
420 { { 0, 0 } }, /* 0 */
421 { { 0x1b, 0x1b } },
422 { { '1', '!' } },
423 { { '2', '@' } },
424 { { '3', '#' } },
425 { { '4', '$' } },
426 { { '5', '%' } },
427 { { '6', '^' } },
428 { { '7', '&' } },
429 { { '8', '*' } },
430 { { '9', '(' } },
431 { { '0', ')' } },
432 { { '-', '_' } },
433 { { '=', '+' } },
434 { { '\b', '\b' } },
435 { { '\t', '\t' } },
436
437 { { 'q', 'Q', 0x11 } }, /* 16 */
438 { { 'w', 'W', 0x17 } },
439 { { 'e', 'E', 0x05 } },
440 { { 'r', 'R', 0x12 } },
441 { { 't', 'T', 0x14 } },
442 { { 'y', 'Y', 0x19 } },
443 { { 'u', 'U', 0x15 } },
444 { { 'i', 'I', 0x09 } },
445 { { 'o', 'O', 0x0f } },
446 { { 'p', 'P', 0x10 } },
447 { { '[', '{', 0x1b } },
448 { { ']', '}', 0x1d } },
449 { { '\n', '\n' } },
450 { { 0, 0 } },
451 { { 'a', 'A', 0x01} },
452 { { 's', 'S', 0x13 } },
453
454 { { 'd', 'D', 0x04 } }, /* 32 */
455 { { 'f', 'F', 0x06 } },
456 { { 'g', 'G', 0x07 } },
457 { { 'h', 'H', 0x08 } },
458 { { 'j', 'J', 0x0a } },
459 { { 'k', 'K', 0x0b } },
460 { { 'l', 'L', 0x0c } },
461 { { ';', ':' } },
462 { { '\'', '"' } },
463 { { '`', '~' } },
464 { { 0, 0 } },
465 { { '\\', '|', 0x1c } },
466 { { 'z', 'Z', 0x1a } },
467 { { 'x', 'X', 0x18 } },
468 { { 'c', 'C', 0x03 } },
469 { { 'v', 'V', 0x16 } },
470
471 { { 'b', 'B', 0x02 } }, /* 48 */
472 { { 'n', 'N', 0x0e } },
473 { { 'm', 'M', 0x0d } },
474 { { ',', '<' } },
475 { { '.', '>' } },
476 { { '/', '?' } },
477 { { 0, 0 } },
478 { { '*', '*' } },
479 { { 0, 0 } },
480 { { ' ', ' ' } },
481 { { 0, 0 } }
482
483 /* 59 */
484};
485
486#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
487
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500488static void
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500489window_update_modifiers(struct window *window, uint32_t key, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500490{
Kristian Høgsberg55444912009-02-21 14:31:09 -0500491 uint32_t mod = 0;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500492
Kristian Høgsberg55444912009-02-21 14:31:09 -0500493 switch (key) {
494 case KEY_LEFTSHIFT:
495 case KEY_RIGHTSHIFT:
496 mod = WINDOW_MODIFIER_SHIFT;
497 break;
498 case KEY_LEFTCTRL:
499 case KEY_RIGHTCTRL:
500 mod = WINDOW_MODIFIER_CONTROL;
501 break;
502 case KEY_LEFTALT:
503 case KEY_RIGHTALT:
504 mod = WINDOW_MODIFIER_ALT;
505 break;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500506 }
507
508 if (state)
509 window->modifiers |= mod;
510 else
511 window->modifiers &= ~mod;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500512}
513
514static void
515window_handle_key(void *data, struct wl_input_device *input_device,
516 uint32_t key, uint32_t state)
517{
518 struct window *window = data;
519 uint32_t unicode = 0;
520
521 if (window->keyboard_device != input_device)
522 return;
523
524 window_update_modifiers(window, key, state);
525
526 if (key < ARRAY_LENGTH(evdev_keymap)) {
527 if (window->modifiers & WINDOW_MODIFIER_CONTROL)
528 unicode = evdev_keymap[key].code[2];
529 else if (window->modifiers & WINDOW_MODIFIER_SHIFT)
530 unicode = evdev_keymap[key].code[1];
531 else
532 unicode = evdev_keymap[key].code[0];
533 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500534
535 if (window->key_handler)
Kristian Høgsberg55444912009-02-21 14:31:09 -0500536 (*window->key_handler)(window, key, unicode,
537 state, window->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500538}
539
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500540static void
541window_handle_pointer_focus(void *data,
542 struct wl_input_device *input_device,
543 struct wl_surface *surface)
544{
545}
546
547static void
548window_handle_keyboard_focus(void *data,
549 struct wl_input_device *input_device,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500550 struct wl_surface *surface,
551 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500552{
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500553 struct window *window = data;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500554 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500555
556 if (window->keyboard_device == input_device && surface != window->surface)
557 window->keyboard_device = NULL;
558 else if (window->keyboard_device == NULL && surface == window->surface)
559 window->keyboard_device = input_device;
560 else
561 return;
562
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500563 if (window->keyboard_device) {
564 end = keys->data + keys->size;
565 for (k = keys->data; k < end; k++)
566 window_update_modifiers(window, *k, 1);
567 } else {
568 window->modifiers = 0;
569 }
570
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500571 if (window->keyboard_focus_handler)
572 (*window->keyboard_focus_handler)(window,
573 window->keyboard_device,
574 window->user_data);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500575}
576
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500577static const struct wl_input_device_listener input_device_listener = {
578 window_handle_motion,
579 window_handle_button,
580 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500581 window_handle_pointer_focus,
582 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500583};
584
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500585void
586window_get_child_rectangle(struct window *window,
587 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500588{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400589 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500590 *rectangle = window->allocation;
591 } else {
592 rectangle->x = window->margin + 10;
593 rectangle->y = window->margin + 50;
594 rectangle->width = window->allocation.width - 20 - window->margin * 2;
595 rectangle->height = window->allocation.height - 60 - window->margin * 2;
596 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500597}
598
599void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500600window_set_child_size(struct window *window,
601 struct rectangle *rectangle)
602{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400603 int32_t width, height;
604
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500605 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400606 width = rectangle->width + 20 + window->margin * 2;
607 height = rectangle->height + 60 + window->margin * 2;
608 if (window->state & WINDOW_RESIZING_LEFT)
609 window->allocation.x -=
610 width - window->allocation.width;
611 if (window->state & WINDOW_RESIZING_TOP)
612 window->allocation.y -=
613 height - window->allocation.height;
614 window->allocation.width = width;
615 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500616 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500617}
618
619void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400620window_copy_image(struct window *window,
621 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500622{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400623 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500624}
625
626void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500627window_copy_surface(struct window *window,
628 struct rectangle *rectangle,
629 cairo_surface_t *surface)
630{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500631 cairo_t *cr;
632
633 cr = cairo_create (window->cairo_surface);
634
635 cairo_set_source_surface (cr,
636 surface,
637 rectangle->x, rectangle->y);
638
639 cairo_paint (cr);
640 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500641}
642
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400643static gboolean
644idle_redraw(void *data)
645{
646 struct window *window = data;
647
648 window->redraw_handler(window, window->user_data);
649 window->redraw_scheduled = 0;
650
651 return FALSE;
652}
653
654void
655window_schedule_redraw(struct window *window)
656{
657 if (!window->redraw_scheduled) {
658 g_idle_add(idle_redraw, window);
659 window->redraw_scheduled = 1;
660 }
661}
662
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500663void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500664window_set_fullscreen(struct window *window, int fullscreen)
665{
666 window->fullscreen = fullscreen;
667 if (window->fullscreen) {
668 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500669 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500670 } else {
671 window->allocation = window->saved_allocation;
672 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500673}
674
675void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400676window_set_decoration(struct window *window, int decoration)
677{
678 window->decoration = decoration;
679}
680
681void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400682window_set_user_data(struct window *window, void *data)
683{
684 window->user_data = data;
685}
686
687void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500688window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400689 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500690{
691 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500692}
693
694void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400695window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400696 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400697{
698 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400699}
700
701void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500702window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400703 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500704{
705 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500706}
707
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500708void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400709window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400710 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400711{
712 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400713}
714
715void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400716window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400717 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400718{
719 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400720}
721
722void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500723window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400724 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500725{
726 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500727}
728
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400729void
730window_move(struct window *window, int32_t x, int32_t y)
731{
732 window->allocation.x = x;
733 window->allocation.y = y;
734
735 wl_surface_map(window->surface,
736 window->allocation.x - window->margin,
737 window->allocation.y - window->margin,
738 window->allocation.width,
739 window->allocation.height);
740}
741
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500742struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500743window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500744 int32_t x, int32_t y, int32_t width, int32_t height)
745{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500746 struct window *window;
747
748 window = malloc(sizeof *window);
749 if (window == NULL)
750 return NULL;
751
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500752 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500753 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500754 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500755 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500756 window->allocation.x = x;
757 window->allocation.y = y;
758 window->allocation.width = width;
759 window->allocation.height = height;
760 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500761 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500762 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400763 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500764
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400765 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500766
767 wl_input_device_add_listener(display->input_device,
768 &input_device_listener, window);
769
770 return window;
771}
772
773static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400774display_handle_device(void *data,
775 struct wl_compositor *compositor,
776 const char *device)
777{
778 struct display *d = data;
779
780 d->device_name = strdup(device);
781}
782
783static void
784display_handle_acknowledge(void *data,
785 struct wl_compositor *compositor,
786 uint32_t key, uint32_t frame)
787{
788 struct display *d = data;
789 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400790
791 /* The acknowledge event means that the server processed our
792 * last commit request and we can now safely free the old
793 * window buffer if we resized and render the next frame into
794 * our back buffer.. */
795 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400796 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400797 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400798 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400799 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400800 if (window->acknowledge_handler)
801 (*window->acknowledge_handler)(window, key, frame, window->user_data);
802 }
803}
804
805static void
806display_handle_frame(void *data,
807 struct wl_compositor *compositor,
808 uint32_t frame, uint32_t timestamp)
809{
810 struct display *d = data;
811 struct window *window;
812
813 wl_list_for_each(window, &d->window_list, link) {
814 if (window->frame_handler)
815 (*window->frame_handler)(window, frame,
816 timestamp, window->user_data);
817 }
818}
819
820static const struct wl_compositor_listener compositor_listener = {
821 display_handle_device,
822 display_handle_acknowledge,
823 display_handle_frame,
824};
825
826static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500827display_handle_geometry(void *data,
828 struct wl_output *output,
829 int32_t width, int32_t height)
830{
831 struct display *display = data;
832
833 display->screen_allocation.x = 0;
834 display->screen_allocation.y = 0;
835 display->screen_allocation.width = width;
836 display->screen_allocation.height = height;
837}
838
839static const struct wl_output_listener output_listener = {
840 display_handle_geometry,
841};
842
843static void
844display_handle_global(struct wl_display *display,
845 struct wl_object *object, void *data)
846{
847 struct display *d = data;
848
849 if (wl_object_implements(object, "compositor", 1)) {
850 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400851 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500852 } else if (wl_object_implements(object, "output", 1)) {
853 d->output = (struct wl_output *) object;
854 wl_output_add_listener(d->output, &output_listener, d);
855 } else if (wl_object_implements(object, "input_device", 1)) {
856 d->input_device =(struct wl_input_device *) object;
857 }
858}
859
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400860static const char socket_name[] = "\0wayland";
861
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400862static void
863display_render_frame(struct display *d)
864{
865 struct rectangle r = { 0, 0, 128, 128 };
866 int radius = 8;
867 cairo_t *cr;
868
869 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
870 cr = cairo_create(d->shadow);
871 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
872 cairo_set_source_rgba(cr, 0, 0, 0, 1);
873 rounded_rect(cr, 16, 16, 112, 112, radius);
874 cairo_fill(cr);
875 cairo_destroy(cr);
876 blur_surface(d->shadow, 64);
877
878 d->active_frame =
879 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
880 cr = cairo_create(d->active_frame);
881 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
882 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
883 rounded_rect(cr, 16, 16, 112, 112, radius);
884 cairo_fill(cr);
885 cairo_destroy(cr);
886
887 d->inactive_frame =
888 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
889 cr = cairo_create(d->inactive_frame);
890 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
891 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
892 rounded_rect(cr, 16, 16, 112, 112, radius);
893 cairo_fill(cr);
894 cairo_destroy(cr);
895}
896
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500897struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400898display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500899{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400900 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500901 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400902 EGLint major, minor, count;
903 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400904 int fd;
905 GOptionContext *context;
906 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400907
908 static const EGLint config_attribs[] = {
909 EGL_SURFACE_TYPE, 0,
910 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
911 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
912 EGL_NONE
913 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500914
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400915 g_type_init();
916
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400917 context = g_option_context_new(NULL);
918 if (option_entries) {
919 g_option_context_add_main_entries(context, option_entries, "Wayland View");
920 if (!g_option_context_parse(context, argc, argv, &error)) {
921 fprintf(stderr, "option parsing failed: %s\n", error->message);
922 exit(EXIT_FAILURE);
923 }
924 }
925
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500926 d = malloc(sizeof *d);
927 if (d == NULL)
928 return NULL;
929
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400930 d->display = wl_display_create(socket_name, sizeof socket_name);
931 if (d->display == NULL) {
932 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400933 return NULL;
934 }
935
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400936 /* Set up listener so we'll catch all events. */
937 wl_display_add_global_listener(d->display,
938 display_handle_global, d);
939
940 /* Process connection events. */
941 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
942
943 fd = open(d->device_name, O_RDWR);
944 if (fd < 0) {
945 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400946 return NULL;
947 }
948
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400949 get_typed_display_mesa =
950 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
951 if (get_typed_display_mesa == NULL) {
952 fprintf(stderr, "eglGetDisplayMESA() not found\n");
953 return NULL;
954 }
955
956 d->dpy = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
957 (void *) fd);
958 if (!eglInitialize(d->dpy, &major, &minor)) {
959 fprintf(stderr, "failed to initialize display\n");
960 return NULL;
961 }
962
963 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
964 count == 0) {
965 fprintf(stderr, "eglChooseConfig() failed\n");
966 return NULL;
967 }
968
969 eglBindAPI(EGL_OPENGL_API);
970
971 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
972 if (d->ctx == NULL) {
973 fprintf(stderr, "failed to create context\n");
974 return NULL;
975 }
976
977 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
978 fprintf(stderr, "faile to make context current\n");
979 return NULL;
980 }
981
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400982 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400983 if (d->device == NULL) {
984 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500985 return NULL;
986 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500987
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400988 display_render_frame(d);
989
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400990 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400991 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400992 g_source_attach(d->source, NULL);
993
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400994 wl_list_init(&d->window_list);
995
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500996 return d;
997}
998
999struct wl_compositor *
1000display_get_compositor(struct display *display)
1001{
1002 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001003}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001004
1005EGLDisplay
1006display_get_egl_display(struct display *d)
1007{
1008 return d->dpy;
1009}
1010
1011void
1012display_run(struct display *d)
1013{
1014 g_main_loop_run(d->loop);
1015}