2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 #include <linux/input.h>
40 #include <X11/Xlib-xcb.h>
42 #include <GLES2/gl2.h>
45 #include "compositor.h"
47 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
49 struct x11_compositor {
50 struct wlsc_compositor base;
53 xcb_connection_t *conn;
55 xcb_cursor_t null_cursor;
57 struct wl_event_source *xcb_source;
59 xcb_atom_t wm_protocols;
60 xcb_atom_t wm_normal_hints;
61 xcb_atom_t wm_size_hints;
62 xcb_atom_t wm_delete_window;
64 xcb_atom_t net_wm_name;
65 xcb_atom_t net_wm_icon;
66 xcb_atom_t net_wm_state;
67 xcb_atom_t net_wm_state_fullscreen;
69 xcb_atom_t utf8_string;
75 struct wlsc_output base;
78 EGLSurface egl_surface;
79 struct wlsc_mode mode;
80 struct wl_event_source *finish_frame_timer;
84 struct wlsc_input_device base;
89 x11_input_create(struct x11_compositor *c)
91 struct x11_input *input;
93 input = malloc(sizeof *input);
97 memset(input, 0, sizeof *input);
98 wlsc_input_device_init(&input->base, &c->base);
100 c->base.input_device = &input->base.input_device;
106 x11_compositor_init_egl(struct x11_compositor *c)
110 const char *extensions;
111 EGLint config_attribs[] = {
112 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
117 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
120 static const EGLint context_attribs[] = {
121 EGL_CONTEXT_CLIENT_VERSION, 2,
125 c->base.display = eglGetDisplay(c->dpy);
126 if (c->base.display == NULL) {
127 fprintf(stderr, "failed to create display\n");
131 if (!eglInitialize(c->base.display, &major, &minor)) {
132 fprintf(stderr, "failed to initialize display\n");
136 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
137 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
138 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
142 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
143 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
146 if (!eglChooseConfig(c->base.display, config_attribs,
147 &c->base.config, 1, &n) || n == 0) {
148 fprintf(stderr, "failed to choose config: %d\n", n);
152 c->base.context = eglCreateContext(c->base.display, c->base.config,
153 EGL_NO_CONTEXT, context_attribs);
154 if (c->base.context == NULL) {
155 fprintf(stderr, "failed to create context\n");
159 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
160 EGL_NO_SURFACE, c->base.context)) {
161 fprintf(stderr, "failed to make context current\n");
169 x11_output_prepare_render(struct wlsc_output *output_base)
171 struct x11_output *output = (struct x11_output *) output_base;
172 struct wlsc_compositor *ec = output->base.compositor;
174 if (!eglMakeCurrent(ec->display, output->egl_surface,
175 output->egl_surface, ec->context)) {
176 fprintf(stderr, "failed to make current\n");
184 finish_frame_handler(void *data)
186 struct x11_output *output = data;
190 gettimeofday(&tv, NULL);
191 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
192 wlsc_output_finish_frame(&output->base, msec);
198 x11_output_present(struct wlsc_output *output_base)
200 struct x11_output *output = (struct x11_output *) output_base;
201 struct wlsc_compositor *ec = output->base.compositor;
203 if (x11_output_prepare_render(&output->base))
206 eglSwapBuffers(ec->display, output->egl_surface);
208 wl_event_source_timer_update(output->finish_frame_timer, 10);
214 x11_output_prepare_scanout_surface(struct wlsc_output *output_base,
215 struct wlsc_surface *es)
221 x11_output_set_cursor(struct wlsc_output *output_base,
222 struct wlsc_input_device *input)
228 x11_output_destroy(struct wlsc_output *output_base)
234 x11_output_set_wm_protocols(struct x11_output *output)
237 struct x11_compositor *c =
238 (struct x11_compositor *) output->base.compositor;
240 list[0] = c->atom.wm_delete_window;
241 xcb_change_property (c->conn,
242 XCB_PROP_MODE_REPLACE,
244 c->atom.wm_protocols,
252 x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
254 xcb_client_message_event_t event;
255 struct x11_compositor *c =
256 (struct x11_compositor *) output->base.compositor;
257 xcb_screen_iterator_t iter;
259 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
260 #define _NET_WM_STATE_ADD 1 /* add/set property */
261 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
263 memset(&event, 0, sizeof event);
264 event.response_type = XCB_CLIENT_MESSAGE;
266 event.window = output->window;
267 event.type = c->atom.net_wm_state;
269 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
270 event.data.data32[1] = state;
271 event.data.data32[2] = 0;
272 event.data.data32[3] = 0;
273 event.data.data32[4] = 0;
275 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
276 xcb_send_event(c->conn, 0, iter.data->root,
277 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
278 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
283 struct wm_normal_hints {
286 int32_t min_width, min_height;
287 int32_t max_width, max_height;
288 int32_t width_inc, height_inc;
289 int32_t min_aspect_x, min_aspect_y;
290 int32_t max_aspect_x, max_aspect_y;
291 int32_t base_width, base_height;
295 #define WM_NORMAL_HINTS_MIN_SIZE 16
296 #define WM_NORMAL_HINTS_MAX_SIZE 32
299 x11_output_set_icon(struct x11_compositor *c,
300 struct x11_output *output, const char *filename)
302 uint32_t *icon, *pixels, stride;
303 int32_t width, height;
305 pixels = wlsc_load_image(filename, &width, &height, &stride);
308 icon = malloc(width * height * 4 + 8);
316 memcpy(icon + 2, pixels, width * height * 4);
317 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
318 c->atom.net_wm_icon, c->atom.cardinal, 32,
319 width * height + 2, icon);
325 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
326 int width, int height, int fullscreen)
328 static const char name[] = "Wayland Compositor";
329 static const char class[] = "wayland-1\0Wayland Compositor";
330 struct x11_output *output;
331 xcb_screen_iterator_t iter;
332 struct wm_normal_hints normal_hints;
333 struct wl_event_loop *loop;
334 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
335 uint32_t values[2] = {
336 XCB_EVENT_MASK_KEY_PRESS |
337 XCB_EVENT_MASK_KEY_RELEASE |
338 XCB_EVENT_MASK_BUTTON_PRESS |
339 XCB_EVENT_MASK_BUTTON_RELEASE |
340 XCB_EVENT_MASK_POINTER_MOTION |
341 XCB_EVENT_MASK_EXPOSURE |
342 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
343 XCB_EVENT_MASK_ENTER_WINDOW |
344 XCB_EVENT_MASK_LEAVE_WINDOW |
345 XCB_EVENT_MASK_KEYMAP_STATE |
346 XCB_EVENT_MASK_FOCUS_CHANGE,
350 output = malloc(sizeof *output);
354 memset(output, 0, sizeof *output);
357 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
358 output->mode.width = width;
359 output->mode.height = height;
360 output->mode.refresh = 60;
361 wl_list_init(&output->base.mode_list);
362 wl_list_insert(&output->base.mode_list, &output->mode.link);
364 output->base.current = &output->mode;
365 wlsc_output_init(&output->base, &c->base, x, y, width, height,
368 values[1] = c->null_cursor;
369 output->window = xcb_generate_id(c->conn);
370 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
371 xcb_create_window(c->conn,
372 XCB_COPY_FROM_PARENT,
378 XCB_WINDOW_CLASS_INPUT_OUTPUT,
379 iter.data->root_visual,
382 /* Don't resize me. */
383 memset(&normal_hints, 0, sizeof normal_hints);
385 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
386 normal_hints.min_width = width;
387 normal_hints.min_height = height;
388 normal_hints.max_width = width;
389 normal_hints.max_height = height;
390 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
391 c->atom.wm_normal_hints,
392 c->atom.wm_size_hints, 32,
393 sizeof normal_hints / 4,
394 (uint8_t *) &normal_hints);
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,
400 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
401 c->atom.wm_class, c->atom.string, 8,
402 sizeof class, class);
404 x11_output_set_icon(c, output, DATADIR "/wayland/wayland.png");
406 xcb_map_window(c->conn, output->window);
408 x11_output_set_wm_protocols(output);
411 x11_output_change_state(output, 1,
412 c->atom.net_wm_state_fullscreen);
414 output->egl_surface =
415 eglCreateWindowSurface(c->base.display, c->base.config,
416 output->window, NULL);
417 if (!output->egl_surface) {
418 fprintf(stderr, "failed to create window surface\n");
421 if (!eglMakeCurrent(c->base.display, output->egl_surface,
422 output->egl_surface, c->base.context)) {
423 fprintf(stderr, "failed to make surface current\n");
427 loop = wl_display_get_event_loop(c->base.wl_display);
428 output->finish_frame_timer =
429 wl_event_loop_add_timer(loop, finish_frame_handler, output);
431 output->base.prepare_render = x11_output_prepare_render;
432 output->base.present = x11_output_present;
433 output->base.prepare_scanout_surface =
434 x11_output_prepare_scanout_surface;
435 output->base.set_hardware_cursor = x11_output_set_cursor;
436 output->base.destroy = x11_output_destroy;
438 wl_list_insert(c->base.output_list.prev, &output->base.link);
443 static struct x11_output *
444 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
446 struct x11_output *output;
448 wl_list_for_each(output, &c->base.output_list, base.link) {
449 if (output->window == window)
457 x11_compositor_next_event(struct x11_compositor *c,
458 xcb_generic_event_t **event, uint32_t mask)
460 if (mask & WL_EVENT_READABLE) {
461 *event = xcb_poll_for_event(c->conn);
463 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
464 *event = xcb_poll_for_queued_event(c->conn);
466 *event = xcb_poll_for_event(c->conn);
470 return *event != NULL;
474 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
476 struct x11_compositor *c = data;
477 struct x11_output *output;
478 xcb_generic_event_t *event, *prev;
479 xcb_client_message_event_t *client_message;
480 xcb_motion_notify_event_t *motion_notify;
481 xcb_enter_notify_event_t *enter_notify;
482 xcb_key_press_event_t *key_press, *key_release;
483 xcb_button_press_event_t *button_press;
484 xcb_keymap_notify_event_t *keymap_notify;
485 xcb_focus_in_event_t *focus_in;
491 while (x11_compositor_next_event(c, &event, mask)) {
492 switch (prev ? prev->response_type & ~0x80 : 0x80) {
493 case XCB_KEY_RELEASE:
494 key_release = (xcb_key_press_event_t *) prev;
495 key_press = (xcb_key_press_event_t *) event;
496 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
497 key_release->time == key_press->time &&
498 key_release->detail == key_press->detail) {
499 /* Don't deliver the held key release
500 * event or the new key press event. */
506 /* Deliver the held key release now
507 * and fall through and handle the new
509 notify_key(c->base.input_device,
510 wlsc_compositor_get_time(),
511 key_release->detail - 8, 0);
518 /* assert event is keymap_notify */
519 focus_in = (xcb_focus_in_event_t *) prev;
520 keymap_notify = (xcb_keymap_notify_event_t *) event;
522 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
523 set = keymap_notify->keys[i >> 3] &
526 k = wl_array_add(&c->keys, sizeof *k);
531 output = x11_compositor_find_output(c, focus_in->event);
532 notify_keyboard_focus(c->base.input_device,
533 wlsc_compositor_get_time(),
534 &output->base, &c->keys);
541 /* No previous event held */
545 switch (event->response_type & ~0x80) {
547 key_press = (xcb_key_press_event_t *) event;
548 notify_key(c->base.input_device,
549 wlsc_compositor_get_time(),
550 key_press->detail - 8, 1);
552 case XCB_KEY_RELEASE:
555 case XCB_BUTTON_PRESS:
556 button_press = (xcb_button_press_event_t *) event;
557 notify_button(c->base.input_device,
558 wlsc_compositor_get_time(),
559 button_press->detail + BTN_LEFT - 1, 1);
561 case XCB_BUTTON_RELEASE:
562 button_press = (xcb_button_press_event_t *) event;
563 notify_button(c->base.input_device,
564 wlsc_compositor_get_time(),
565 button_press->detail + BTN_LEFT - 1, 0);
568 case XCB_MOTION_NOTIFY:
569 motion_notify = (xcb_motion_notify_event_t *) event;
570 output = x11_compositor_find_output(c, motion_notify->event);
571 notify_motion(c->base.input_device,
572 wlsc_compositor_get_time(),
573 output->base.x + motion_notify->event_x,
574 output->base.y + motion_notify->event_y);
578 /* FIXME: schedule output repaint */
579 /* output = x11_compositor_find_output(c, expose->window); */
581 wlsc_compositor_schedule_repaint(&c->base);
584 case XCB_ENTER_NOTIFY:
585 enter_notify = (xcb_enter_notify_event_t *) event;
586 if (enter_notify->state >= Button1Mask)
588 output = x11_compositor_find_output(c, enter_notify->event);
589 notify_pointer_focus(c->base.input_device,
590 wlsc_compositor_get_time(),
592 output->base.x + enter_notify->event_x,
593 output->base.y + enter_notify->event_y);
596 case XCB_LEAVE_NOTIFY:
597 enter_notify = (xcb_enter_notify_event_t *) event;
598 if (enter_notify->state >= Button1Mask)
600 output = x11_compositor_find_output(c, enter_notify->event);
601 notify_pointer_focus(c->base.input_device,
602 wlsc_compositor_get_time(),
604 output->base.x + enter_notify->event_x,
605 output->base.y + enter_notify->event_y);
608 case XCB_CLIENT_MESSAGE:
609 client_message = (xcb_client_message_event_t *) event;
610 atom = client_message->data.data32[0];
611 if (atom == c->atom.wm_delete_window)
612 wl_display_terminate(c->base.wl_display);
616 focus_in = (xcb_focus_in_event_t *) event;
617 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
624 focus_in = (xcb_focus_in_event_t *) event;
625 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
627 notify_keyboard_focus(c->base.input_device,
628 wlsc_compositor_get_time(),
640 switch (prev ? prev->response_type & ~0x80 : 0x80) {
641 case XCB_KEY_RELEASE:
642 key_release = (xcb_key_press_event_t *) prev;
643 notify_key(c->base.input_device,
644 wlsc_compositor_get_time(),
645 key_release->detail - 8, 0);
653 return event != NULL;
656 #define F(field) offsetof(struct x11_compositor, field)
659 x11_compositor_get_resources(struct x11_compositor *c)
661 static const struct { const char *name; int offset; } atoms[] = {
662 { "WM_PROTOCOLS", F(atom.wm_protocols) },
663 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
664 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
665 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
666 { "WM_CLASS", F(atom.wm_class) },
667 { "_NET_WM_NAME", F(atom.net_wm_name) },
668 { "_NET_WM_ICON", F(atom.net_wm_icon) },
669 { "_NET_WM_STATE", F(atom.net_wm_state) },
670 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
671 { "STRING", F(atom.string) },
672 { "UTF8_STRING", F(atom.utf8_string) },
673 { "CARDINAL", F(atom.cardinal) },
676 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
677 xcb_intern_atom_reply_t *reply;
681 uint8_t data[] = { 0, 0, 0, 0 };
683 for (i = 0; i < ARRAY_SIZE(atoms); i++)
684 cookies[i] = xcb_intern_atom (c->conn, 0,
685 strlen(atoms[i].name),
688 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
689 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
690 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
694 pixmap = xcb_generate_id(c->conn);
695 gc = xcb_generate_id(c->conn);
696 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
697 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
698 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
699 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
700 c->null_cursor = xcb_generate_id(c->conn);
701 xcb_create_cursor (c->conn, c->null_cursor,
702 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
703 xcb_free_gc(c->conn, gc);
704 xcb_free_pixmap(c->conn, pixmap);
708 x11_destroy(struct wlsc_compositor *ec)
710 wlsc_compositor_shutdown(ec);
715 static struct wlsc_compositor *
716 x11_compositor_create(struct wl_display *display,
717 int width, int height, int count, int fullscreen)
719 struct x11_compositor *c;
720 struct wl_event_loop *loop;
721 xcb_screen_iterator_t s;
724 c = malloc(sizeof *c);
728 memset(c, 0, sizeof *c);
730 c->dpy = XOpenDisplay(NULL);
734 c->conn = XGetXCBConnection(c->dpy);
735 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
737 if (xcb_connection_has_error(c->conn))
740 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
742 wl_array_init(&c->keys);
744 x11_compositor_get_resources(c);
746 c->base.wl_display = display;
747 if (x11_compositor_init_egl(c) < 0)
750 c->base.destroy = x11_destroy;
752 /* Can't init base class until we have a current egl context */
753 if (wlsc_compositor_init(&c->base, display) < 0)
756 for (i = 0, x = 0; i < count; i++) {
757 if (x11_compositor_create_output(c, x, 0, width, height,
763 if (x11_input_create(c) < 0)
766 loop = wl_display_get_event_loop(c->base.wl_display);
769 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
771 x11_compositor_handle_event, c);
772 wl_event_source_check(c->xcb_source);
777 struct wlsc_compositor *
778 backend_init(struct wl_display *display, char *options);
780 WL_EXPORT struct wlsc_compositor *
781 backend_init(struct wl_display *display, char *options)
783 int width = 1024, height = 640, fullscreen = 0, count = 1, i;
786 static char * const tokens[] = {
787 "width", "height", "fullscreen", "output-count", NULL
791 while (i = getsubopt(&p, tokens, &value), i != -1) {
794 width = strtol(value, NULL, 0);
797 height = strtol(value, NULL, 0);
803 count = strtol(value, NULL, 0);
808 return x11_compositor_create(display,
809 width, height, count, fullscreen);