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.
31 #include <linux/input.h>
35 #include <X11/Xlib-xcb.h>
37 #include <GLES2/gl2.h>
40 #include "compositor.h"
42 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
44 struct x11_compositor {
45 struct wlsc_compositor base;
48 xcb_connection_t *conn;
50 xcb_cursor_t null_cursor;
52 struct wl_event_source *xcb_source;
54 xcb_atom_t wm_protocols;
55 xcb_atom_t wm_normal_hints;
56 xcb_atom_t wm_size_hints;
57 xcb_atom_t wm_delete_window;
59 xcb_atom_t net_wm_name;
60 xcb_atom_t net_wm_icon;
62 xcb_atom_t utf8_string;
68 struct wlsc_output base;
71 EGLSurface egl_surface;
75 struct wlsc_input_device base;
80 x11_input_create(struct x11_compositor *c)
82 struct x11_input *input;
84 input = malloc(sizeof *input);
88 memset(input, 0, sizeof *input);
89 wlsc_input_device_init(&input->base, &c->base);
91 c->base.input_device = &input->base.input_device;
97 x11_compositor_init_egl(struct x11_compositor *c)
101 const char *extensions;
102 EGLint config_attribs[] = {
103 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
111 static const EGLint context_attribs[] = {
112 EGL_CONTEXT_CLIENT_VERSION, 2,
116 c->base.display = eglGetDisplay(c->dpy);
117 if (c->base.display == NULL) {
118 fprintf(stderr, "failed to create display\n");
122 if (!eglInitialize(c->base.display, &major, &minor)) {
123 fprintf(stderr, "failed to initialize display\n");
127 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
128 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
129 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
133 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
134 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
137 if (!eglChooseConfig(c->base.display, config_attribs,
138 &c->base.config, 1, &n) || n == 0) {
139 fprintf(stderr, "failed to choose config: %d\n", n);
143 c->base.context = eglCreateContext(c->base.display, c->base.config,
144 EGL_NO_CONTEXT, context_attribs);
145 if (c->base.context == NULL) {
146 fprintf(stderr, "failed to create context\n");
150 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
151 EGL_NO_SURFACE, c->base.context)) {
152 fprintf(stderr, "failed to make context current\n");
160 x11_output_prepare_render(struct wlsc_output *output_base)
162 struct x11_output *output = (struct x11_output *) output_base;
163 struct wlsc_compositor *ec = output->base.compositor;
165 if (!eglMakeCurrent(ec->display, output->egl_surface,
166 output->egl_surface, ec->context)) {
167 fprintf(stderr, "failed to make current\n");
175 x11_output_present(struct wlsc_output *output_base)
177 struct x11_output *output = (struct x11_output *) output_base;
178 struct wlsc_compositor *ec = output->base.compositor;
182 if (x11_output_prepare_render(&output->base))
185 eglSwapBuffers(ec->display, output->egl_surface);
187 gettimeofday(&tv, NULL);
188 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
189 wlsc_output_finish_frame(&output->base, msec);
195 x11_output_image_is_scanoutable(struct wlsc_output *output_base,
202 x11_output_set_cursor(struct wlsc_output *output_base,
203 struct wl_input_device *input)
210 x11_output_set_wm_protocols(struct x11_output *output)
213 struct x11_compositor *c =
214 (struct x11_compositor *) output->base.compositor;
216 list[0] = c->atom.wm_delete_window;
217 xcb_change_property (c->conn,
218 XCB_PROP_MODE_REPLACE,
220 c->atom.wm_protocols,
227 struct wm_normal_hints {
230 int32_t min_width, min_height;
231 int32_t max_width, max_height;
232 int32_t width_inc, height_inc;
233 int32_t min_aspect_x, min_aspect_y;
234 int32_t max_aspect_x, max_aspect_y;
235 int32_t base_width, base_height;
239 #define WM_NORMAL_HINTS_MIN_SIZE 16
240 #define WM_NORMAL_HINTS_MAX_SIZE 32
243 x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
244 const char *filename, int width, int height)
246 uint32_t *icon, *pixels;
248 pixels = wlsc_load_image(filename, width, height);
251 icon = malloc(width * height * 4 + 8);
259 memcpy(icon + 2, pixels, width * height * 4);
260 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
261 c->atom.net_wm_icon, c->atom.cardinal, 32,
262 width * height + 2, icon);
268 x11_compositor_create_output(struct x11_compositor *c, int width, int height)
270 static const char name[] = "Wayland Compositor";
271 static const char class[] = "wayland-1\0Wayland Compositor";
272 struct x11_output *output;
273 xcb_screen_iterator_t iter;
274 struct wm_normal_hints normal_hints;
275 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
276 uint32_t values[2] = {
277 XCB_EVENT_MASK_KEY_PRESS |
278 XCB_EVENT_MASK_KEY_RELEASE |
279 XCB_EVENT_MASK_BUTTON_PRESS |
280 XCB_EVENT_MASK_BUTTON_RELEASE |
281 XCB_EVENT_MASK_POINTER_MOTION |
282 XCB_EVENT_MASK_EXPOSURE |
283 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
284 XCB_EVENT_MASK_ENTER_WINDOW |
285 XCB_EVENT_MASK_LEAVE_WINDOW |
286 XCB_EVENT_MASK_KEYMAP_STATE |
287 XCB_EVENT_MASK_FOCUS_CHANGE,
291 output = malloc(sizeof *output);
295 memset(output, 0, sizeof *output);
296 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
299 values[1] = c->null_cursor;
300 output->window = xcb_generate_id(c->conn);
301 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
302 xcb_create_window(c->conn,
303 XCB_COPY_FROM_PARENT,
309 XCB_WINDOW_CLASS_INPUT_OUTPUT,
310 iter.data->root_visual,
313 /* Don't resize me. */
314 memset(&normal_hints, 0, sizeof normal_hints);
316 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
317 normal_hints.min_width = width;
318 normal_hints.min_height = height;
319 normal_hints.max_width = width;
320 normal_hints.max_height = height;
321 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
322 c->atom.wm_normal_hints,
323 c->atom.wm_size_hints, 32,
324 sizeof normal_hints / 4,
325 (uint8_t *) &normal_hints);
327 /* Set window name. Don't bother with non-EWMH WMs. */
328 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
329 c->atom.net_wm_name, c->atom.utf8_string, 8,
331 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
332 c->atom.wm_class, c->atom.string, 8,
333 sizeof class, class);
335 x11_output_set_icon(c, output,
336 DATADIR "/wayland/wayland.png", 128, 128);
338 xcb_map_window(c->conn, output->window);
340 x11_output_set_wm_protocols(output);
342 output->egl_surface =
343 eglCreateWindowSurface(c->base.display, c->base.config,
344 output->window, NULL);
345 if (!output->egl_surface) {
346 fprintf(stderr, "failed to create window surface\n");
349 if (!eglMakeCurrent(c->base.display, output->egl_surface,
350 output->egl_surface, c->base.context)) {
351 fprintf(stderr, "failed to make surface current\n");
355 output->base.prepare_render = x11_output_prepare_render;
356 output->base.present = x11_output_present;
357 output->base.image_is_scanoutable = x11_output_image_is_scanoutable;
358 output->base.set_hardware_cursor = x11_output_set_cursor;
360 wl_list_insert(c->base.output_list.prev, &output->base.link);
365 static struct x11_output *
366 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
368 struct x11_output *output;
370 wl_list_for_each(output, &c->base.output_list, base.link) {
371 if (output->window == window)
379 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
381 struct x11_compositor *c = data;
382 struct x11_output *output;
383 xcb_generic_event_t *event;
384 xcb_client_message_event_t *client_message;
385 xcb_motion_notify_event_t *motion_notify;
386 xcb_enter_notify_event_t *enter_notify;
387 xcb_key_press_event_t *key_press, *prev_release;
388 xcb_button_press_event_t *button_press;
389 xcb_keymap_notify_event_t *keymap_notify;
390 xcb_focus_in_event_t *focus_in;
396 while (event = xcb_poll_for_event (c->conn), event != NULL) {
397 key_press = (xcb_key_press_event_t *) event;
399 (event->response_type & ~0x80) == XCB_KEY_PRESS &&
400 prev_release->time == key_press->time) {
405 } else if (prev_release) {
406 notify_key(c->base.input_device,
408 prev_release->detail - 8, 0);
414 switch (event->response_type & ~0x80) {
417 key_press = (xcb_key_press_event_t *) event;
418 notify_key(c->base.input_device,
419 key_press->time, key_press->detail - 8, 1);
421 case XCB_KEY_RELEASE:
422 prev_release = (xcb_key_press_event_t *) event;
424 case XCB_BUTTON_PRESS:
425 button_press = (xcb_button_press_event_t *) event;
426 notify_button(c->base.input_device,
428 button_press->detail + BTN_LEFT - 1, 1);
430 case XCB_BUTTON_RELEASE:
431 button_press = (xcb_button_press_event_t *) event;
432 notify_button(c->base.input_device,
434 button_press->detail + BTN_LEFT - 1, 0);
437 case XCB_MOTION_NOTIFY:
438 motion_notify = (xcb_motion_notify_event_t *) event;
439 notify_motion(c->base.input_device,
441 motion_notify->event_x,
442 motion_notify->event_y);
446 /* FIXME: schedule output repaint */
447 /* output = x11_compositor_find_output(c, expose->window); */
449 wlsc_compositor_schedule_repaint(&c->base);
452 case XCB_ENTER_NOTIFY:
453 enter_notify = (xcb_enter_notify_event_t *) event;
454 if (enter_notify->state >= Button1Mask)
456 output = x11_compositor_find_output(c, enter_notify->event);
457 notify_pointer_focus(c->base.input_device,
460 enter_notify->event_x,
461 enter_notify->event_y);
464 case XCB_LEAVE_NOTIFY:
465 enter_notify = (xcb_enter_notify_event_t *) event;
466 if (enter_notify->state >= Button1Mask)
468 notify_pointer_focus(c->base.input_device,
471 enter_notify->event_x,
472 enter_notify->event_y);
475 case XCB_CLIENT_MESSAGE:
476 client_message = (xcb_client_message_event_t *) event;
477 atom = client_message->data.data32[0];
478 if (atom == c->atom.wm_delete_window)
479 wl_display_terminate(c->base.wl_display);
483 focus_in = (xcb_focus_in_event_t *) event;
484 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
487 output = x11_compositor_find_output(c, focus_in->event);
488 notify_keyboard_focus(c->base.input_device,
490 &output->base, &c->keys);
494 focus_in = (xcb_focus_in_event_t *) event;
495 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
497 notify_keyboard_focus(c->base.input_device,
498 get_time(), NULL, NULL);
501 case XCB_KEYMAP_NOTIFY:
502 keymap_notify = (xcb_keymap_notify_event_t *) event;
504 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
505 set = keymap_notify->keys[i >> 3] &
508 k = wl_array_add(&c->keys, sizeof *k);
517 if ((xcb_generic_event_t *) prev_release != event)
522 key_press = (xcb_key_press_event_t *) prev_release;
523 notify_key(c->base.input_device,
524 prev_release->time, prev_release->detail - 8, 0);
530 #define F(field) offsetof(struct x11_compositor, field)
533 x11_compositor_get_resources(struct x11_compositor *c)
535 static const struct { const char *name; int offset; } atoms[] = {
536 { "WM_PROTOCOLS", F(atom.wm_protocols) },
537 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
538 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
539 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
540 { "WM_CLASS", F(atom.wm_class) },
541 { "_NET_WM_NAME", F(atom.net_wm_name) },
542 { "_NET_WM_ICON", F(atom.net_wm_icon) },
543 { "STRING", F(atom.string) },
544 { "UTF8_STRING", F(atom.utf8_string) },
545 { "CARDINAL", F(atom.cardinal) },
548 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
549 xcb_intern_atom_reply_t *reply;
553 uint8_t data[] = { 0, 0, 0, 0 };
555 for (i = 0; i < ARRAY_SIZE(atoms); i++)
556 cookies[i] = xcb_intern_atom (c->conn, 0,
557 strlen(atoms[i].name),
560 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
561 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
562 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
566 pixmap = xcb_generate_id(c->conn);
567 gc = xcb_generate_id(c->conn);
568 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
569 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
570 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
571 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
572 c->null_cursor = xcb_generate_id(c->conn);
573 xcb_create_cursor (c->conn, c->null_cursor,
574 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
575 xcb_free_gc(c->conn, gc);
576 xcb_free_pixmap(c->conn, pixmap);
580 x11_destroy(struct wlsc_compositor *ec)
585 struct wlsc_compositor *
586 x11_compositor_create(struct wl_display *display, int width, int height)
588 struct x11_compositor *c;
589 struct wl_event_loop *loop;
590 xcb_screen_iterator_t s;
592 c = malloc(sizeof *c);
596 memset(c, 0, sizeof *c);
598 c->dpy = XOpenDisplay(NULL);
603 c->conn = XGetXCBConnection(c->dpy);
605 if (xcb_connection_has_error(c->conn))
608 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
610 wl_array_init(&c->keys);
612 x11_compositor_get_resources(c);
614 c->base.wl_display = display;
615 if (x11_compositor_init_egl(c) < 0)
618 c->base.destroy = x11_destroy;
620 /* Can't init base class until we have a current egl context */
621 if (wlsc_compositor_init(&c->base, display) < 0)
624 if (x11_compositor_create_output(c, width, height) < 0)
627 if (x11_input_create(c) < 0)
630 loop = wl_display_get_event_loop(c->base.wl_display);
633 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
635 x11_compositor_handle_event, c);