blob: 517887324cc3464e95389a9bbdefe1ffafb98add [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) {
143 fprintf(stderr, "DRI2: failed to query version");
144 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) {
155 fprintf(stderr, "DRI2: failed to authenticate");
156 return -1;
157 }
158
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800159#ifdef XCB_DRI2_CONNECT_DEVICE_NAME_BROKEN
160 {
161 char *driver_name, *device_name;
162
163 driver_name = xcb_dri2_connect_driver_name (connect);
164 device_name = driver_name +
165 ((connect->driver_name_length + 3) & ~3);
166 snprintf(path, sizeof path, "%.*s",
167 xcb_dri2_connect_device_name_length (connect),
168 device_name);
169 }
170#else
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400171 snprintf(path, sizeof path, "%.*s",
172 xcb_dri2_connect_device_name_length (connect),
173 xcb_dri2_connect_device_name (connect));
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800174#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400175 free(connect);
176
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400177 fd = open(path, O_RDWR);
178 if (fd < 0) {
179 fprintf(stderr,
180 "DRI2: could not open %s (%s)", path, strerror(errno));
181 return -1;
182 }
183
184 return wlsc_drm_init(&c->base, fd, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400185}
186
187static int
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400188dri2_authenticate(struct x11_compositor *c, uint32_t magic)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400189{
190 xcb_dri2_authenticate_reply_t *authenticate;
191 xcb_dri2_authenticate_cookie_t authenticate_cookie;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400192
193 authenticate_cookie =
194 xcb_dri2_authenticate_unchecked(c->conn,
195 c->screen->root, magic);
196 authenticate =
197 xcb_dri2_authenticate_reply(c->conn,
198 authenticate_cookie, NULL);
199 if (authenticate == NULL || !authenticate->authenticated) {
200 fprintf(stderr, "DRI2: failed to authenticate");
201 free(authenticate);
202 return -1;
203 }
204
205 free(authenticate);
206
207 return 0;
208}
209
210static int
211x11_compositor_init_egl(struct x11_compositor *c)
212{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400213 EGLint major, minor;
214 const char *extensions;
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400215 drm_magic_t magic;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400216 static const EGLint context_attribs[] = {
217 EGL_CONTEXT_CLIENT_VERSION, 2,
218 EGL_NONE
219 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400220
221 if (dri2_connect(c) < 0)
222 return -1;
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400223
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400224 if (drmGetMagic(c->base.drm.fd, &magic)) {
225 fprintf(stderr, "DRI2: failed to get drm magic");
226 return -1;
227 }
228
229 if (dri2_authenticate(c, magic) < 0)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400230 return -1;
231
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400232 c->base.display = eglGetDRMDisplayMESA(c->base.drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400233 if (c->base.display == NULL) {
234 fprintf(stderr, "failed to create display\n");
235 return -1;
236 }
237
238 if (!eglInitialize(c->base.display, &major, &minor)) {
239 fprintf(stderr, "failed to initialize display\n");
240 return -1;
241 }
242
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400243 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
244 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
245 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400246 return -1;
247 }
248
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400249 eglBindAPI(EGL_OPENGL_ES_API);
250 c->base.context = eglCreateContext(c->base.display, NULL,
251 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400252 if (c->base.context == NULL) {
253 fprintf(stderr, "failed to create context\n");
254 return -1;
255 }
256
257 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
258 EGL_NO_SURFACE, c->base.context)) {
259 fprintf(stderr, "failed to make context current\n");
260 return -1;
261 }
262
263 return 0;
264}
265
266static void
267x11_compositor_present(struct wlsc_compositor *base)
268{
269 struct x11_compositor *c = (struct x11_compositor *) base;
270 struct x11_output *output;
271 xcb_dri2_copy_region_cookie_t cookie;
272 struct timeval tv;
273 uint32_t msec;
274
275 glFlush();
276
277 wl_list_for_each(output, &c->base.output_list, base.link) {
278 cookie = xcb_dri2_copy_region_unchecked(c->conn,
279 output->window,
280 output->region,
281 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
282 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
283 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
284 }
285
286 gettimeofday(&tv, NULL);
287 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
288 wlsc_compositor_finish_frame(&c->base, msec);
289}
290
291static void
292x11_output_set_wm_protocols(struct x11_output *output)
293{
294 xcb_atom_t list[1];
295 struct x11_compositor *c =
296 (struct x11_compositor *) output->base.compositor;
297
298 list[0] = c->atom.wm_delete_window;
299 xcb_change_property (c->conn,
300 XCB_PROP_MODE_REPLACE,
301 output->window,
302 c->atom.wm_protocols,
303 XCB_ATOM_ATOM,
304 32,
305 ARRAY_SIZE(list),
306 list);
307}
308
309struct wm_normal_hints {
310 uint32_t flags;
311 uint32_t pad[4];
312 int32_t min_width, min_height;
313 int32_t max_width, max_height;
314 int32_t width_inc, height_inc;
315 int32_t min_aspect_x, min_aspect_y;
316 int32_t max_aspect_x, max_aspect_y;
317 int32_t base_width, base_height;
318 int32_t win_gravity;
319};
320
321#define WM_NORMAL_HINTS_MIN_SIZE 16
322#define WM_NORMAL_HINTS_MAX_SIZE 32
323
324static int
325x11_compositor_create_output(struct x11_compositor *c, int width, int height)
326{
327 static const char name[] = "Wayland Compositor";
328 struct x11_output *output;
329 xcb_dri2_dri2_buffer_t *buffers;
330 xcb_dri2_get_buffers_reply_t *reply;
331 xcb_dri2_get_buffers_cookie_t cookie;
332 xcb_screen_iterator_t iter;
333 xcb_rectangle_t rectangle;
334 struct wm_normal_hints normal_hints;
335 unsigned int attachments[] =
336 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
337 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
338 uint32_t values[2] = {
339 XCB_EVENT_MASK_KEY_PRESS |
340 XCB_EVENT_MASK_KEY_RELEASE |
341 XCB_EVENT_MASK_BUTTON_PRESS |
342 XCB_EVENT_MASK_BUTTON_RELEASE |
343 XCB_EVENT_MASK_POINTER_MOTION |
344 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400345 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
346 XCB_EVENT_MASK_ENTER_WINDOW |
347 XCB_EVENT_MASK_LEAVE_WINDOW,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400348 0
349 };
350
351 EGLint attribs[] = {
352 EGL_WIDTH, 0,
353 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400354 EGL_DRM_BUFFER_STRIDE_MESA, 0,
355 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400356 EGL_NONE
357 };
358
359 output = malloc(sizeof *output);
360 if (output == NULL)
361 return -1;
362
363 memset(output, 0, sizeof *output);
364 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
365
366 values[1] = c->null_cursor;
367 output->window = xcb_generate_id(c->conn);
368 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
369 xcb_create_window(c->conn,
370 XCB_COPY_FROM_PARENT,
371 output->window,
372 iter.data->root,
373 0, 0,
374 width, height,
375 0,
376 XCB_WINDOW_CLASS_INPUT_OUTPUT,
377 iter.data->root_visual,
378 mask, values);
379
380 /* Don't resize me. */
381 memset(&normal_hints, 0, sizeof normal_hints);
382 normal_hints.flags =
383 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
384 normal_hints.min_width = width;
385 normal_hints.min_height = height;
386 normal_hints.max_width = width;
387 normal_hints.max_height = height;
388 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
389 c->atom.wm_normal_hints,
390 c->atom.wm_size_hints, 32,
391 sizeof normal_hints / 4,
392 (uint8_t *) &normal_hints);
393
394 xcb_map_window(c->conn, output->window);
395
396 /* Set window name. Don't bother with non-EWMH WMs. */
397 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
398 c->atom.net_wm_name, c->atom.utf8_string, 8,
399 strlen(name), name);
400
401 rectangle.x = 0;
402 rectangle.y = 0;
403 rectangle.width = width;
404 rectangle.height = height;
405 output->region = xcb_generate_id(c->conn);
406 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
407
408 xcb_dri2_create_drawable (c->conn, output->window);
409
410 x11_output_set_wm_protocols(output);
411
412 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
413 output->window,
414 1, 1, attachments);
415 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
416 if (reply == NULL)
417 return -1;
418 buffers = xcb_dri2_get_buffers_buffers (reply);
419 if (buffers == NULL)
420 return -1;
421
422 if (reply->count != 1) {
423 fprintf(stderr,
424 "got wrong number of buffers (%d)\n", reply->count);
425 return -1;
426 }
427
428 attribs[1] = reply->width;
429 attribs[3] = reply->height;
430 attribs[5] = buffers[0].pitch / 4;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400431 output->image =
432 eglCreateImageKHR(c->base.display, c->base.context,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400433 EGL_DRM_BUFFER_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400434 (EGLClientBuffer) buffers[0].name,
435 attribs);
Kristian Høgsberg8f2e6772010-07-29 14:48:13 -0400436 free(reply);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400437
438 glGenRenderbuffers(1, &output->rbo);
439 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
440
441 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
442 output->image);
443
444 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
445 GL_COLOR_ATTACHMENT0,
446 GL_RENDERBUFFER,
447 output->rbo);
448
449 wl_list_insert(c->base.output_list.prev, &output->base.link);
450
451 return 0;
452}
453
454static void
455idle_repaint(void *data)
456{
457 struct x11_output *output = data;
458 struct x11_compositor *c =
459 (struct x11_compositor *) output->base.compositor;
460 xcb_xfixes_region_t region;
461 xcb_dri2_copy_region_cookie_t cookie;
462
463 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
464 region = xcb_generate_id(c->conn);
465 xcb_xfixes_create_region(c->conn, region,
466 output->damage_count, output->damage);
467 } else {
468 region = output->region;
469 }
470
471 cookie = xcb_dri2_copy_region_unchecked(c->conn,
472 output->window,
473 region,
474 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
475 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
476
477 if (region != output->region)
478 xcb_xfixes_destroy_region(c->conn, region);
479
480 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
481 output->damage_count = 0;
482}
483
484static struct x11_output *
485x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
486{
487 struct x11_output *output;
488
489 wl_list_for_each(output, &c->base.output_list, base.link) {
490 if (output->window == window)
491 return output;
492 }
493
494 return NULL;
495}
496
497static void
498x11_compositor_handle_event(int fd, uint32_t mask, void *data)
499{
500 struct x11_compositor *c = data;
501 struct x11_output *output;
502 xcb_generic_event_t *event;
503 struct wl_event_loop *loop;
504 xcb_client_message_event_t *client_message;
505 xcb_motion_notify_event_t *motion_notify;
506 xcb_key_press_event_t *key_press;
507 xcb_button_press_event_t *button_press;
508 xcb_expose_event_t *expose;
509 xcb_rectangle_t *r;
510 xcb_atom_t atom;
511
512 loop = wl_display_get_event_loop(c->base.wl_display);
513 while (event = xcb_poll_for_event (c->conn), event != NULL) {
514 switch (event->response_type & ~0x80) {
515
516 case XCB_KEY_PRESS:
517 key_press = (xcb_key_press_event_t *) event;
518 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400519 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400520 break;
521 case XCB_KEY_RELEASE:
522 key_press = (xcb_key_press_event_t *) event;
523 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400524 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400525 break;
526 case XCB_BUTTON_PRESS:
527 button_press = (xcb_button_press_event_t *) event;
528 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400529 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400530 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400531 break;
532 case XCB_BUTTON_RELEASE:
533 button_press = (xcb_button_press_event_t *) event;
534 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400535 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400536 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400537 break;
538
539 case XCB_MOTION_NOTIFY:
540 motion_notify = (xcb_motion_notify_event_t *) event;
541 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400542 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400543 motion_notify->event_x,
544 motion_notify->event_y);
545 break;
546
547 case XCB_EXPOSE:
548 expose = (xcb_expose_event_t *) event;
549 output = x11_compositor_find_output(c, expose->window);
550 if (output->damage_count == 0)
551 wl_event_loop_add_idle(loop,
552 idle_repaint, output);
553
554 r = &output->damage[output->damage_count++];
555 if (output->damage_count > 16)
556 break;
557 r->x = expose->x;
558 r->y = expose->y;
559 r->width = expose->width;
560 r->height = expose->height;
561 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400562
563 case XCB_ENTER_NOTIFY:
564 c->base.focus = 1;
565 wlsc_compositor_schedule_repaint(&c->base);
566 break;
567
568 case XCB_LEAVE_NOTIFY:
569 c->base.focus = 0;
570 wlsc_compositor_schedule_repaint(&c->base);
571 break;
572
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400573 case XCB_CLIENT_MESSAGE:
574 client_message = (xcb_client_message_event_t *) event;
575 atom = client_message->data.data32[0];
576 if (atom == c->atom.wm_delete_window)
577 exit(1);
578 break;
579 default:
580
581 break;
582 }
583
584 free (event);
585 }
586}
587
588#define F(field) offsetof(struct x11_compositor, field)
589
590static void
591x11_compositor_get_resources(struct x11_compositor *c)
592{
593 static const struct { const char *name; int offset; } atoms[] = {
594 { "WM_PROTOCOLS", F(atom.wm_protocols) },
595 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
596 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
597 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
598 { "_NET_WM_NAME", F(atom.net_wm_name) },
599 { "UTF8_STRING", F(atom.utf8_string) },
600 };
601
602 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
603 xcb_intern_atom_reply_t *reply;
604 xcb_pixmap_t pixmap;
605 xcb_gc_t gc;
606 int i;
607 uint8_t data[] = { 0, 0, 0, 0 };
608
609 for (i = 0; i < ARRAY_SIZE(atoms); i++)
610 cookies[i] = xcb_intern_atom (c->conn, 0,
611 strlen(atoms[i].name),
612 atoms[i].name);
613
614 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
615 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
616 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
617 free(reply);
618 }
619
620 pixmap = xcb_generate_id(c->conn);
621 gc = xcb_generate_id(c->conn);
622 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
623 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
624 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
625 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
626 c->null_cursor = xcb_generate_id(c->conn);
627 xcb_create_cursor (c->conn, c->null_cursor,
628 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
629 xcb_free_gc(c->conn, gc);
630 xcb_free_pixmap(c->conn, pixmap);
631}
632
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400633static int
634x11_authenticate(struct wlsc_compositor *c, uint32_t id)
635{
636 return dri2_authenticate((struct x11_compositor *) c, id);
637}
638
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400639struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400640x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400641{
642 struct x11_compositor *c;
643 struct wl_event_loop *loop;
644 xcb_screen_iterator_t s;
645
646 c = malloc(sizeof *c);
647 if (c == NULL)
648 return NULL;
649
650 memset(c, 0, sizeof *c);
651 c->conn = xcb_connect(0, 0);
652
653 if (xcb_connection_has_error(c->conn))
654 return NULL;
655
656 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
657 c->screen = s.data;
658
659 x11_compositor_get_resources(c);
660
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400661 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200662 if (x11_compositor_init_egl(c) < 0)
663 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400664
665 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400666 if (wlsc_compositor_init(&c->base, display) < 0)
667 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400668
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400669 x11_compositor_create_output(c, width, height);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400670
671 x11_input_create(c);
672
673 loop = wl_display_get_event_loop(c->base.wl_display);
674
675 c->xcb_source =
676 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
677 WL_EVENT_READABLE,
678 x11_compositor_handle_event, c);
679
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400680 c->base.authenticate = x11_authenticate;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400681 c->base.present = x11_compositor_present;
682
683 return &c->base;
684}