blob: f6e705b6774b16887bcdbd52b54326e5170a62a3 [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
2 * Copyright © 2008-2010 Kristian Høgsberg
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040023#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <errno.h>
30#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040031#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040032
33#include <xcb/xcb.h>
34#include <xcb/dri2.h>
35#include <xcb/xfixes.h>
36
37#define GL_GLEXT_PROTOTYPES
38#define EGL_EGLEXT_PROTOTYPES
39#include <GLES2/gl2.h>
40#include <GLES2/gl2ext.h>
41#include <EGL/egl.h>
42#include <EGL/eglext.h>
43
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040044#include "compositor.h"
45
46#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
47
48struct x11_compositor {
49 struct wlsc_compositor base;
50
51 xcb_connection_t *conn;
52 xcb_screen_t *screen;
53 xcb_cursor_t null_cursor;
54 int dri2_major;
55 int dri2_minor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040056 struct wl_event_source *xcb_source;
57 struct {
58 xcb_atom_t wm_protocols;
59 xcb_atom_t wm_normal_hints;
60 xcb_atom_t wm_size_hints;
61 xcb_atom_t wm_delete_window;
62 xcb_atom_t net_wm_name;
63 xcb_atom_t utf8_string;
64 } atom;
65};
66
67struct x11_output {
68 struct wlsc_output base;
69
70 xcb_xfixes_region_t region;
71 xcb_window_t window;
72 GLuint rbo;
73 EGLImageKHR image;
74 xcb_rectangle_t damage[16];
75 int damage_count;
76};
77
78struct x11_input {
79 struct wlsc_input_device base;
80};
81
82
83static void
84x11_input_create(struct x11_compositor *c)
85{
86 struct x11_input *input;
87
88 input = malloc(sizeof *input);
89 if (input == NULL)
90 return;
91
92 memset(input, 0, sizeof *input);
93 wlsc_input_device_init(&input->base, &c->base);
94
95 c->base.input_device = &input->base;
96}
97
98
99static int
100dri2_connect(struct x11_compositor *c)
101{
102 xcb_xfixes_query_version_reply_t *xfixes_query;
103 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
104 xcb_dri2_query_version_reply_t *dri2_query;
105 xcb_dri2_query_version_cookie_t dri2_query_cookie;
106 xcb_dri2_connect_reply_t *connect;
107 xcb_dri2_connect_cookie_t connect_cookie;
108 xcb_generic_error_t *error;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400109 char path[256];
110 int fd;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400111
112 xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
113 xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
114
115 xfixes_query_cookie =
116 xcb_xfixes_query_version(c->conn,
117 XCB_XFIXES_MAJOR_VERSION,
118 XCB_XFIXES_MINOR_VERSION);
119
120 dri2_query_cookie =
121 xcb_dri2_query_version (c->conn,
122 XCB_DRI2_MAJOR_VERSION,
123 XCB_DRI2_MINOR_VERSION);
124
125 connect_cookie = xcb_dri2_connect_unchecked (c->conn,
126 c->screen->root,
127 XCB_DRI2_DRIVER_TYPE_DRI);
128
129 xfixes_query =
130 xcb_xfixes_query_version_reply (c->conn,
131 xfixes_query_cookie, &error);
132 if (xfixes_query == NULL ||
133 error != NULL || xfixes_query->major_version < 2) {
134 free(error);
135 return -1;
136 }
137 free(xfixes_query);
138
139 dri2_query =
140 xcb_dri2_query_version_reply (c->conn,
141 dri2_query_cookie, &error);
142 if (dri2_query == NULL || error != NULL) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200143 fprintf(stderr, "DRI2: failed to query version\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400144 free(error);
145 return EGL_FALSE;
146 }
147 c->dri2_major = dri2_query->major_version;
148 c->dri2_minor = dri2_query->minor_version;
149 free(dri2_query);
150
151 connect = xcb_dri2_connect_reply (c->conn,
152 connect_cookie, NULL);
153 if (connect == NULL ||
154 connect->driver_name_length + connect->device_name_length == 0) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200155 fprintf(stderr, "DRI2: failed to connect, DRI2 version: %u.%u\n",
156 c->dri2_major, c->dri2_minor);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400157 return -1;
158 }
159
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800160#ifdef XCB_DRI2_CONNECT_DEVICE_NAME_BROKEN
161 {
162 char *driver_name, *device_name;
163
164 driver_name = xcb_dri2_connect_driver_name (connect);
165 device_name = driver_name +
166 ((connect->driver_name_length + 3) & ~3);
167 snprintf(path, sizeof path, "%.*s",
168 xcb_dri2_connect_device_name_length (connect),
169 device_name);
170 }
171#else
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400172 snprintf(path, sizeof path, "%.*s",
173 xcb_dri2_connect_device_name_length (connect),
174 xcb_dri2_connect_device_name (connect));
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800175#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400176 free(connect);
177
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400178 fd = open(path, O_RDWR);
179 if (fd < 0) {
180 fprintf(stderr,
Yuval Fledel91b59992010-11-22 21:42:58 +0200181 "DRI2: could not open %s (%s)\n", path, strerror(errno));
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400182 return -1;
183 }
184
185 return wlsc_drm_init(&c->base, fd, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400186}
187
188static int
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400189dri2_authenticate(struct x11_compositor *c, uint32_t magic)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400190{
191 xcb_dri2_authenticate_reply_t *authenticate;
192 xcb_dri2_authenticate_cookie_t authenticate_cookie;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400193
194 authenticate_cookie =
195 xcb_dri2_authenticate_unchecked(c->conn,
196 c->screen->root, magic);
197 authenticate =
198 xcb_dri2_authenticate_reply(c->conn,
199 authenticate_cookie, NULL);
200 if (authenticate == NULL || !authenticate->authenticated) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200201 fprintf(stderr, "DRI2: failed to authenticate\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400202 free(authenticate);
203 return -1;
204 }
205
206 free(authenticate);
207
208 return 0;
209}
210
211static int
212x11_compositor_init_egl(struct x11_compositor *c)
213{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400214 EGLint major, minor;
215 const char *extensions;
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400216 drm_magic_t magic;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400217 static const EGLint context_attribs[] = {
218 EGL_CONTEXT_CLIENT_VERSION, 2,
219 EGL_NONE
220 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400221
222 if (dri2_connect(c) < 0)
223 return -1;
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400224
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400225 if (drmGetMagic(c->base.drm.fd, &magic)) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200226 fprintf(stderr, "DRI2: failed to get drm magic\n");
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400227 return -1;
228 }
229
230 if (dri2_authenticate(c, magic) < 0)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400231 return -1;
232
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400233 c->base.display = eglGetDRMDisplayMESA(c->base.drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400234 if (c->base.display == NULL) {
235 fprintf(stderr, "failed to create display\n");
236 return -1;
237 }
238
239 if (!eglInitialize(c->base.display, &major, &minor)) {
240 fprintf(stderr, "failed to initialize display\n");
241 return -1;
242 }
243
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400244 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
245 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
246 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400247 return -1;
248 }
249
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400250 eglBindAPI(EGL_OPENGL_ES_API);
251 c->base.context = eglCreateContext(c->base.display, NULL,
252 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400253 if (c->base.context == NULL) {
254 fprintf(stderr, "failed to create context\n");
255 return -1;
256 }
257
258 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
259 EGL_NO_SURFACE, c->base.context)) {
260 fprintf(stderr, "failed to make context current\n");
261 return -1;
262 }
263
264 return 0;
265}
266
267static void
268x11_compositor_present(struct wlsc_compositor *base)
269{
270 struct x11_compositor *c = (struct x11_compositor *) base;
271 struct x11_output *output;
272 xcb_dri2_copy_region_cookie_t cookie;
273 struct timeval tv;
274 uint32_t msec;
275
276 glFlush();
277
278 wl_list_for_each(output, &c->base.output_list, base.link) {
279 cookie = xcb_dri2_copy_region_unchecked(c->conn,
280 output->window,
281 output->region,
282 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
283 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
284 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
285 }
286
287 gettimeofday(&tv, NULL);
288 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
289 wlsc_compositor_finish_frame(&c->base, msec);
290}
291
292static void
293x11_output_set_wm_protocols(struct x11_output *output)
294{
295 xcb_atom_t list[1];
296 struct x11_compositor *c =
297 (struct x11_compositor *) output->base.compositor;
298
299 list[0] = c->atom.wm_delete_window;
300 xcb_change_property (c->conn,
301 XCB_PROP_MODE_REPLACE,
302 output->window,
303 c->atom.wm_protocols,
304 XCB_ATOM_ATOM,
305 32,
306 ARRAY_SIZE(list),
307 list);
308}
309
310struct wm_normal_hints {
311 uint32_t flags;
312 uint32_t pad[4];
313 int32_t min_width, min_height;
314 int32_t max_width, max_height;
315 int32_t width_inc, height_inc;
316 int32_t min_aspect_x, min_aspect_y;
317 int32_t max_aspect_x, max_aspect_y;
318 int32_t base_width, base_height;
319 int32_t win_gravity;
320};
321
322#define WM_NORMAL_HINTS_MIN_SIZE 16
323#define WM_NORMAL_HINTS_MAX_SIZE 32
324
325static int
326x11_compositor_create_output(struct x11_compositor *c, int width, int height)
327{
328 static const char name[] = "Wayland Compositor";
329 struct x11_output *output;
330 xcb_dri2_dri2_buffer_t *buffers;
331 xcb_dri2_get_buffers_reply_t *reply;
332 xcb_dri2_get_buffers_cookie_t cookie;
333 xcb_screen_iterator_t iter;
334 xcb_rectangle_t rectangle;
335 struct wm_normal_hints normal_hints;
336 unsigned int attachments[] =
337 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
338 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
339 uint32_t values[2] = {
340 XCB_EVENT_MASK_KEY_PRESS |
341 XCB_EVENT_MASK_KEY_RELEASE |
342 XCB_EVENT_MASK_BUTTON_PRESS |
343 XCB_EVENT_MASK_BUTTON_RELEASE |
344 XCB_EVENT_MASK_POINTER_MOTION |
345 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400346 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
347 XCB_EVENT_MASK_ENTER_WINDOW |
348 XCB_EVENT_MASK_LEAVE_WINDOW,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400349 0
350 };
351
352 EGLint attribs[] = {
353 EGL_WIDTH, 0,
354 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400355 EGL_DRM_BUFFER_STRIDE_MESA, 0,
356 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400357 EGL_NONE
358 };
359
360 output = malloc(sizeof *output);
361 if (output == NULL)
362 return -1;
363
364 memset(output, 0, sizeof *output);
365 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
366
367 values[1] = c->null_cursor;
368 output->window = xcb_generate_id(c->conn);
369 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
370 xcb_create_window(c->conn,
371 XCB_COPY_FROM_PARENT,
372 output->window,
373 iter.data->root,
374 0, 0,
375 width, height,
376 0,
377 XCB_WINDOW_CLASS_INPUT_OUTPUT,
378 iter.data->root_visual,
379 mask, values);
380
381 /* Don't resize me. */
382 memset(&normal_hints, 0, sizeof normal_hints);
383 normal_hints.flags =
384 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
385 normal_hints.min_width = width;
386 normal_hints.min_height = height;
387 normal_hints.max_width = width;
388 normal_hints.max_height = height;
389 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
390 c->atom.wm_normal_hints,
391 c->atom.wm_size_hints, 32,
392 sizeof normal_hints / 4,
393 (uint8_t *) &normal_hints);
394
395 xcb_map_window(c->conn, output->window);
396
397 /* Set window name. Don't bother with non-EWMH WMs. */
398 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
399 c->atom.net_wm_name, c->atom.utf8_string, 8,
400 strlen(name), name);
401
402 rectangle.x = 0;
403 rectangle.y = 0;
404 rectangle.width = width;
405 rectangle.height = height;
406 output->region = xcb_generate_id(c->conn);
407 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
408
409 xcb_dri2_create_drawable (c->conn, output->window);
410
411 x11_output_set_wm_protocols(output);
412
413 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
414 output->window,
415 1, 1, attachments);
416 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
417 if (reply == NULL)
418 return -1;
419 buffers = xcb_dri2_get_buffers_buffers (reply);
420 if (buffers == NULL)
421 return -1;
422
423 if (reply->count != 1) {
424 fprintf(stderr,
425 "got wrong number of buffers (%d)\n", reply->count);
426 return -1;
427 }
428
429 attribs[1] = reply->width;
430 attribs[3] = reply->height;
431 attribs[5] = buffers[0].pitch / 4;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400432 output->image =
433 eglCreateImageKHR(c->base.display, c->base.context,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400434 EGL_DRM_BUFFER_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400435 (EGLClientBuffer) buffers[0].name,
436 attribs);
Kristian Høgsberg8f2e6772010-07-29 14:48:13 -0400437 free(reply);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400438
439 glGenRenderbuffers(1, &output->rbo);
440 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
441
442 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
443 output->image);
444
445 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
446 GL_COLOR_ATTACHMENT0,
447 GL_RENDERBUFFER,
448 output->rbo);
449
450 wl_list_insert(c->base.output_list.prev, &output->base.link);
451
452 return 0;
453}
454
455static void
456idle_repaint(void *data)
457{
458 struct x11_output *output = data;
459 struct x11_compositor *c =
460 (struct x11_compositor *) output->base.compositor;
461 xcb_xfixes_region_t region;
462 xcb_dri2_copy_region_cookie_t cookie;
463
464 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
465 region = xcb_generate_id(c->conn);
466 xcb_xfixes_create_region(c->conn, region,
467 output->damage_count, output->damage);
468 } else {
469 region = output->region;
470 }
471
472 cookie = xcb_dri2_copy_region_unchecked(c->conn,
473 output->window,
474 region,
475 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
476 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
477
478 if (region != output->region)
479 xcb_xfixes_destroy_region(c->conn, region);
480
481 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
482 output->damage_count = 0;
483}
484
485static struct x11_output *
486x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
487{
488 struct x11_output *output;
489
490 wl_list_for_each(output, &c->base.output_list, base.link) {
491 if (output->window == window)
492 return output;
493 }
494
495 return NULL;
496}
497
498static void
499x11_compositor_handle_event(int fd, uint32_t mask, void *data)
500{
501 struct x11_compositor *c = data;
502 struct x11_output *output;
503 xcb_generic_event_t *event;
504 struct wl_event_loop *loop;
505 xcb_client_message_event_t *client_message;
506 xcb_motion_notify_event_t *motion_notify;
507 xcb_key_press_event_t *key_press;
508 xcb_button_press_event_t *button_press;
509 xcb_expose_event_t *expose;
510 xcb_rectangle_t *r;
511 xcb_atom_t atom;
512
513 loop = wl_display_get_event_loop(c->base.wl_display);
514 while (event = xcb_poll_for_event (c->conn), event != NULL) {
515 switch (event->response_type & ~0x80) {
516
517 case XCB_KEY_PRESS:
518 key_press = (xcb_key_press_event_t *) event;
519 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400520 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400521 break;
522 case XCB_KEY_RELEASE:
523 key_press = (xcb_key_press_event_t *) event;
524 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400525 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400526 break;
527 case XCB_BUTTON_PRESS:
528 button_press = (xcb_button_press_event_t *) event;
529 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400530 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400531 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400532 break;
533 case XCB_BUTTON_RELEASE:
534 button_press = (xcb_button_press_event_t *) event;
535 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400536 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400537 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538 break;
539
540 case XCB_MOTION_NOTIFY:
541 motion_notify = (xcb_motion_notify_event_t *) event;
542 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400543 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400544 motion_notify->event_x,
545 motion_notify->event_y);
546 break;
547
548 case XCB_EXPOSE:
549 expose = (xcb_expose_event_t *) event;
550 output = x11_compositor_find_output(c, expose->window);
551 if (output->damage_count == 0)
552 wl_event_loop_add_idle(loop,
553 idle_repaint, output);
554
555 r = &output->damage[output->damage_count++];
556 if (output->damage_count > 16)
557 break;
558 r->x = expose->x;
559 r->y = expose->y;
560 r->width = expose->width;
561 r->height = expose->height;
562 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400563
564 case XCB_ENTER_NOTIFY:
565 c->base.focus = 1;
566 wlsc_compositor_schedule_repaint(&c->base);
567 break;
568
569 case XCB_LEAVE_NOTIFY:
570 c->base.focus = 0;
571 wlsc_compositor_schedule_repaint(&c->base);
572 break;
573
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400574 case XCB_CLIENT_MESSAGE:
575 client_message = (xcb_client_message_event_t *) event;
576 atom = client_message->data.data32[0];
577 if (atom == c->atom.wm_delete_window)
578 exit(1);
579 break;
580 default:
581
582 break;
583 }
584
585 free (event);
586 }
587}
588
589#define F(field) offsetof(struct x11_compositor, field)
590
591static void
592x11_compositor_get_resources(struct x11_compositor *c)
593{
594 static const struct { const char *name; int offset; } atoms[] = {
595 { "WM_PROTOCOLS", F(atom.wm_protocols) },
596 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
597 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
598 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
599 { "_NET_WM_NAME", F(atom.net_wm_name) },
600 { "UTF8_STRING", F(atom.utf8_string) },
601 };
602
603 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
604 xcb_intern_atom_reply_t *reply;
605 xcb_pixmap_t pixmap;
606 xcb_gc_t gc;
607 int i;
608 uint8_t data[] = { 0, 0, 0, 0 };
609
610 for (i = 0; i < ARRAY_SIZE(atoms); i++)
611 cookies[i] = xcb_intern_atom (c->conn, 0,
612 strlen(atoms[i].name),
613 atoms[i].name);
614
615 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
616 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
617 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
618 free(reply);
619 }
620
621 pixmap = xcb_generate_id(c->conn);
622 gc = xcb_generate_id(c->conn);
623 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
624 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
625 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
626 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
627 c->null_cursor = xcb_generate_id(c->conn);
628 xcb_create_cursor (c->conn, c->null_cursor,
629 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
630 xcb_free_gc(c->conn, gc);
631 xcb_free_pixmap(c->conn, pixmap);
632}
633
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400634static int
635x11_authenticate(struct wlsc_compositor *c, uint32_t id)
636{
637 return dri2_authenticate((struct x11_compositor *) c, id);
638}
639
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400640struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400641x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400642{
643 struct x11_compositor *c;
644 struct wl_event_loop *loop;
645 xcb_screen_iterator_t s;
646
647 c = malloc(sizeof *c);
648 if (c == NULL)
649 return NULL;
650
651 memset(c, 0, sizeof *c);
652 c->conn = xcb_connect(0, 0);
653
654 if (xcb_connection_has_error(c->conn))
655 return NULL;
656
657 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
658 c->screen = s.data;
659
660 x11_compositor_get_resources(c);
661
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400662 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200663 if (x11_compositor_init_egl(c) < 0)
664 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400665
666 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400667 if (wlsc_compositor_init(&c->base, display) < 0)
668 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400669
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400670 x11_compositor_create_output(c, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400671
672 x11_input_create(c);
673
674 loop = wl_display_get_event_loop(c->base.wl_display);
675
676 c->xcb_source =
677 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
678 WL_EVENT_READABLE,
679 x11_compositor_handle_event, c);
680
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400681 c->base.authenticate = x11_authenticate;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400682 c->base.present = x11_compositor_present;
683
684 return &c->base;
685}