2 * Copyright © 2008-2010 Kristian Høgsberg
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.
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.
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.
27 #include <linux/input.h>
31 #include <xcb/xfixes.h>
33 #define GL_GLEXT_PROTOTYPES
34 #define EGL_EGLEXT_PROTOTYPES
35 #include <GLES2/gl2.h>
36 #include <GLES2/gl2ext.h>
38 #include <EGL/eglext.h>
41 #include "wayland-protocol.h"
42 #include "compositor.h"
44 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
46 struct x11_compositor {
47 struct wlsc_compositor base;
49 xcb_connection_t *conn;
51 xcb_cursor_t null_cursor;
55 struct wl_event_source *xcb_source;
57 xcb_atom_t wm_protocols;
58 xcb_atom_t wm_normal_hints;
59 xcb_atom_t wm_size_hints;
60 xcb_atom_t wm_delete_window;
61 xcb_atom_t net_wm_name;
62 xcb_atom_t utf8_string;
67 struct wlsc_output base;
69 xcb_xfixes_region_t region;
73 xcb_rectangle_t damage[16];
78 struct wlsc_input_device base;
83 x11_input_create(struct x11_compositor *c)
85 struct x11_input *input;
87 input = malloc(sizeof *input);
91 memset(input, 0, sizeof *input);
92 wlsc_input_device_init(&input->base, &c->base);
94 c->base.input_device = &input->base;
99 dri2_connect(struct x11_compositor *c)
101 xcb_xfixes_query_version_reply_t *xfixes_query;
102 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
103 xcb_dri2_query_version_reply_t *dri2_query;
104 xcb_dri2_query_version_cookie_t dri2_query_cookie;
105 xcb_dri2_connect_reply_t *connect;
106 xcb_dri2_connect_cookie_t connect_cookie;
107 xcb_generic_error_t *error;
109 xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
110 xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
112 xfixes_query_cookie =
113 xcb_xfixes_query_version(c->conn,
114 XCB_XFIXES_MAJOR_VERSION,
115 XCB_XFIXES_MINOR_VERSION);
118 xcb_dri2_query_version (c->conn,
119 XCB_DRI2_MAJOR_VERSION,
120 XCB_DRI2_MINOR_VERSION);
122 connect_cookie = xcb_dri2_connect_unchecked (c->conn,
124 XCB_DRI2_DRIVER_TYPE_DRI);
127 xcb_xfixes_query_version_reply (c->conn,
128 xfixes_query_cookie, &error);
129 if (xfixes_query == NULL ||
130 error != NULL || xfixes_query->major_version < 2) {
137 xcb_dri2_query_version_reply (c->conn,
138 dri2_query_cookie, &error);
139 if (dri2_query == NULL || error != NULL) {
140 fprintf(stderr, "DRI2: failed to query version");
144 c->dri2_major = dri2_query->major_version;
145 c->dri2_minor = dri2_query->minor_version;
148 connect = xcb_dri2_connect_reply (c->conn,
149 connect_cookie, NULL);
150 if (connect == NULL ||
151 connect->driver_name_length + connect->device_name_length == 0) {
152 fprintf(stderr, "DRI2: failed to authenticate");
156 c->base.base.device =
157 strndup(xcb_dri2_connect_device_name (connect),
158 xcb_dri2_connect_device_name_length (connect));
160 if (c->base.base.device == NULL) {
170 dri2_authenticate(struct x11_compositor *c)
172 xcb_dri2_authenticate_reply_t *authenticate;
173 xcb_dri2_authenticate_cookie_t authenticate_cookie;
176 if (drmGetMagic(c->drm_fd, &magic)) {
177 fprintf(stderr, "DRI2: failed to get drm magic");
181 authenticate_cookie =
182 xcb_dri2_authenticate_unchecked(c->conn,
183 c->screen->root, magic);
185 xcb_dri2_authenticate_reply(c->conn,
186 authenticate_cookie, NULL);
187 if (authenticate == NULL || !authenticate->authenticated) {
188 fprintf(stderr, "DRI2: failed to authenticate");
199 x11_compositor_init_egl(struct x11_compositor *c)
201 EGLint major, minor, count;
204 static const EGLint config_attribs[] = {
206 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
207 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
211 if (dri2_connect(c) < 0)
214 c->drm_fd = open(c->base.base.device, O_RDWR);
215 if (c->drm_fd == -1) {
217 "DRI2: could not open %s (%s)", c->base.base.device,
222 if (dri2_authenticate(c) < 0)
225 c->base.display = eglGetDRMDisplayMESA(c->drm_fd);
226 if (c->base.display == NULL) {
227 fprintf(stderr, "failed to create display\n");
231 if (!eglInitialize(c->base.display, &major, &minor)) {
232 fprintf(stderr, "failed to initialize display\n");
236 if (!eglChooseConfig(c->base.display,
237 config_attribs, &config, 1, &count) ||
239 fprintf(stderr, "eglChooseConfig() failed\n");
243 eglBindAPI(EGL_OPENGL_API);
244 c->base.context = eglCreateContext(c->base.display,
245 config, EGL_NO_CONTEXT, NULL);
246 if (c->base.context == NULL) {
247 fprintf(stderr, "failed to create context\n");
251 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
252 EGL_NO_SURFACE, c->base.context)) {
253 fprintf(stderr, "failed to make context current\n");
261 x11_compositor_present(struct wlsc_compositor *base)
263 struct x11_compositor *c = (struct x11_compositor *) base;
264 struct x11_output *output;
265 xcb_dri2_copy_region_cookie_t cookie;
271 wl_list_for_each(output, &c->base.output_list, base.link) {
272 cookie = xcb_dri2_copy_region_unchecked(c->conn,
275 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
276 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
277 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
280 gettimeofday(&tv, NULL);
281 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
282 wlsc_compositor_finish_frame(&c->base, msec);
286 x11_output_set_wm_protocols(struct x11_output *output)
289 struct x11_compositor *c =
290 (struct x11_compositor *) output->base.compositor;
292 list[0] = c->atom.wm_delete_window;
293 xcb_change_property (c->conn,
294 XCB_PROP_MODE_REPLACE,
296 c->atom.wm_protocols,
303 struct wm_normal_hints {
306 int32_t min_width, min_height;
307 int32_t max_width, max_height;
308 int32_t width_inc, height_inc;
309 int32_t min_aspect_x, min_aspect_y;
310 int32_t max_aspect_x, max_aspect_y;
311 int32_t base_width, base_height;
315 #define WM_NORMAL_HINTS_MIN_SIZE 16
316 #define WM_NORMAL_HINTS_MAX_SIZE 32
319 x11_compositor_create_output(struct x11_compositor *c, int width, int height)
321 static const char name[] = "Wayland Compositor";
322 struct x11_output *output;
323 xcb_dri2_dri2_buffer_t *buffers;
324 xcb_dri2_get_buffers_reply_t *reply;
325 xcb_dri2_get_buffers_cookie_t cookie;
326 xcb_screen_iterator_t iter;
327 xcb_rectangle_t rectangle;
328 struct wm_normal_hints normal_hints;
329 unsigned int attachments[] =
330 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
331 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
332 uint32_t values[2] = {
333 XCB_EVENT_MASK_KEY_PRESS |
334 XCB_EVENT_MASK_KEY_RELEASE |
335 XCB_EVENT_MASK_BUTTON_PRESS |
336 XCB_EVENT_MASK_BUTTON_RELEASE |
337 XCB_EVENT_MASK_POINTER_MOTION |
338 XCB_EVENT_MASK_EXPOSURE |
339 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
340 XCB_EVENT_MASK_ENTER_WINDOW |
341 XCB_EVENT_MASK_LEAVE_WINDOW,
348 EGL_IMAGE_STRIDE_MESA, 0,
349 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
353 output = malloc(sizeof *output);
357 memset(output, 0, sizeof *output);
358 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
360 values[1] = c->null_cursor;
361 output->window = xcb_generate_id(c->conn);
362 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
363 xcb_create_window(c->conn,
364 XCB_COPY_FROM_PARENT,
370 XCB_WINDOW_CLASS_INPUT_OUTPUT,
371 iter.data->root_visual,
374 /* Don't resize me. */
375 memset(&normal_hints, 0, sizeof normal_hints);
377 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
378 normal_hints.min_width = width;
379 normal_hints.min_height = height;
380 normal_hints.max_width = width;
381 normal_hints.max_height = height;
382 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
383 c->atom.wm_normal_hints,
384 c->atom.wm_size_hints, 32,
385 sizeof normal_hints / 4,
386 (uint8_t *) &normal_hints);
388 xcb_map_window(c->conn, output->window);
390 /* Set window name. Don't bother with non-EWMH WMs. */
391 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
392 c->atom.net_wm_name, c->atom.utf8_string, 8,
397 rectangle.width = width;
398 rectangle.height = height;
399 output->region = xcb_generate_id(c->conn);
400 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
402 xcb_dri2_create_drawable (c->conn, output->window);
404 x11_output_set_wm_protocols(output);
406 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
409 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
412 buffers = xcb_dri2_get_buffers_buffers (reply);
416 if (reply->count != 1) {
418 "got wrong number of buffers (%d)\n", reply->count);
422 attribs[1] = reply->width;
423 attribs[3] = reply->height;
424 attribs[5] = buffers[0].pitch / 4;
428 eglCreateImageKHR(c->base.display, c->base.context,
430 (EGLClientBuffer) buffers[0].name,
433 glGenRenderbuffers(1, &output->rbo);
434 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
436 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
439 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
440 GL_COLOR_ATTACHMENT0,
444 wl_list_insert(c->base.output_list.prev, &output->base.link);
450 idle_repaint(void *data)
452 struct x11_output *output = data;
453 struct x11_compositor *c =
454 (struct x11_compositor *) output->base.compositor;
455 xcb_xfixes_region_t region;
456 xcb_dri2_copy_region_cookie_t cookie;
458 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
459 region = xcb_generate_id(c->conn);
460 xcb_xfixes_create_region(c->conn, region,
461 output->damage_count, output->damage);
463 region = output->region;
466 cookie = xcb_dri2_copy_region_unchecked(c->conn,
469 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
470 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
472 if (region != output->region)
473 xcb_xfixes_destroy_region(c->conn, region);
475 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
476 output->damage_count = 0;
479 static struct x11_output *
480 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
482 struct x11_output *output;
484 wl_list_for_each(output, &c->base.output_list, base.link) {
485 if (output->window == window)
493 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
495 struct x11_compositor *c = data;
496 struct x11_output *output;
497 xcb_generic_event_t *event;
498 struct wl_event_loop *loop;
499 xcb_client_message_event_t *client_message;
500 xcb_motion_notify_event_t *motion_notify;
501 xcb_key_press_event_t *key_press;
502 xcb_button_press_event_t *button_press;
503 xcb_expose_event_t *expose;
507 loop = wl_display_get_event_loop(c->base.wl_display);
508 while (event = xcb_poll_for_event (c->conn), event != NULL) {
509 switch (event->response_type & ~0x80) {
512 key_press = (xcb_key_press_event_t *) event;
513 notify_key(c->base.input_device,
514 key_press->detail - 8, 1);
516 case XCB_KEY_RELEASE:
517 key_press = (xcb_key_press_event_t *) event;
518 notify_key(c->base.input_device,
519 key_press->detail - 8, 0);
521 case XCB_BUTTON_PRESS:
522 button_press = (xcb_button_press_event_t *) event;
523 notify_button(c->base.input_device,
524 button_press->detail + BTN_LEFT - 1, 1);
526 case XCB_BUTTON_RELEASE:
527 button_press = (xcb_button_press_event_t *) event;
528 notify_button(c->base.input_device,
529 button_press->detail + BTN_LEFT - 1, 0);
532 case XCB_MOTION_NOTIFY:
533 motion_notify = (xcb_motion_notify_event_t *) event;
534 notify_motion(c->base.input_device,
535 motion_notify->event_x,
536 motion_notify->event_y);
540 expose = (xcb_expose_event_t *) event;
541 output = x11_compositor_find_output(c, expose->window);
542 if (output->damage_count == 0)
543 wl_event_loop_add_idle(loop,
544 idle_repaint, output);
546 r = &output->damage[output->damage_count++];
547 if (output->damage_count > 16)
551 r->width = expose->width;
552 r->height = expose->height;
555 case XCB_ENTER_NOTIFY:
557 wlsc_compositor_schedule_repaint(&c->base);
560 case XCB_LEAVE_NOTIFY:
562 wlsc_compositor_schedule_repaint(&c->base);
565 case XCB_CLIENT_MESSAGE:
566 client_message = (xcb_client_message_event_t *) event;
567 atom = client_message->data.data32[0];
568 if (atom == c->atom.wm_delete_window)
580 #define F(field) offsetof(struct x11_compositor, field)
583 x11_compositor_get_resources(struct x11_compositor *c)
585 static const struct { const char *name; int offset; } atoms[] = {
586 { "WM_PROTOCOLS", F(atom.wm_protocols) },
587 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
588 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
589 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
590 { "_NET_WM_NAME", F(atom.net_wm_name) },
591 { "UTF8_STRING", F(atom.utf8_string) },
594 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
595 xcb_intern_atom_reply_t *reply;
599 uint8_t data[] = { 0, 0, 0, 0 };
601 for (i = 0; i < ARRAY_SIZE(atoms); i++)
602 cookies[i] = xcb_intern_atom (c->conn, 0,
603 strlen(atoms[i].name),
606 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
607 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
608 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
612 pixmap = xcb_generate_id(c->conn);
613 gc = xcb_generate_id(c->conn);
614 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
615 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
616 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
617 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
618 c->null_cursor = xcb_generate_id(c->conn);
619 xcb_create_cursor (c->conn, c->null_cursor,
620 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
621 xcb_free_gc(c->conn, gc);
622 xcb_free_pixmap(c->conn, pixmap);
625 struct wlsc_compositor *
626 x11_compositor_create(struct wl_display *display)
628 struct x11_compositor *c;
629 struct wl_event_loop *loop;
630 xcb_screen_iterator_t s;
632 c = malloc(sizeof *c);
636 memset(c, 0, sizeof *c);
637 c->conn = xcb_connect(0, 0);
639 if (xcb_connection_has_error(c->conn))
642 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
645 x11_compositor_get_resources(c);
647 x11_compositor_init_egl(c);
649 /* Can't init base class until we have a current egl context */
650 if (wlsc_compositor_init(&c->base, display) < 0)
653 x11_compositor_create_output(c, 1024, 640);
657 loop = wl_display_get_event_loop(c->base.wl_display);
660 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
662 x11_compositor_handle_event, c);
664 c->base.present = x11_compositor_present;