2 * Copyright © 2010-2011 Benjamin Franzke
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 #include <wayland-client.h>
37 #include <wayland-egl.h>
39 #include "compositor.h"
40 #include "gl-renderer.h"
41 #include "../shared/image-loader.h"
42 #include "../shared/os-compatibility.h"
44 struct wayland_compositor {
45 struct weston_compositor base;
48 struct wl_display *wl_display;
49 struct wl_registry *registry;
50 struct wl_compositor *compositor;
51 struct wl_shell *shell;
52 struct wl_output *output;
56 int32_t x, y, width, height;
59 struct wl_event_source *wl_source;
64 int32_t top, bottom, left, right;
67 struct wl_list input_list;
70 struct wayland_output {
71 struct weston_output base;
73 int draw_initial_frame;
74 struct wl_surface *surface;
75 struct wl_shell_surface *shell_surface;
76 struct wl_egl_window *egl_window;
78 struct weston_mode mode;
81 struct wayland_input {
82 struct weston_seat base;
83 struct wayland_compositor *compositor;
85 struct wl_pointer *pointer;
86 struct wl_keyboard *keyboard;
87 struct wl_touch *touch;
90 uint32_t enter_serial;
92 struct wayland_output *output;
96 create_border(struct wayland_compositor *c)
98 pixman_image_t *image;
101 image = load_image(DATADIR "/weston/border.png");
103 weston_log("could'nt load border image\n");
107 edges[0] = c->border.left;
108 edges[1] = c->border.right;
109 edges[2] = c->border.top;
110 edges[3] = c->border.bottom;
112 gl_renderer_set_border(&c->base, pixman_image_get_width(image),
113 pixman_image_get_height(image),
114 pixman_image_get_data(image), edges);
116 pixman_image_unref(image);
120 frame_done(void *data, struct wl_callback *callback, uint32_t time)
122 struct weston_output *output = data;
124 wl_callback_destroy(callback);
125 weston_output_finish_frame(output, time);
128 static const struct wl_callback_listener frame_listener = {
133 buffer_release(void *data, struct wl_buffer *buffer)
135 wl_buffer_destroy(buffer);
138 static const struct wl_buffer_listener buffer_listener = {
143 draw_initial_frame(struct wayland_output *output)
145 struct wayland_compositor *c =
146 (struct wayland_compositor *) output->base.compositor;
147 struct wl_shm *shm = c->parent.shm;
148 struct wl_surface *surface = output->parent.surface;
149 struct wl_shm_pool *pool;
150 struct wl_buffer *buffer;
152 int width, height, stride;
157 width = output->mode.width + c->border.left + c->border.right;
158 height = output->mode.height + c->border.top + c->border.bottom;
160 size = height * stride;
162 fd = os_create_anonymous_file(size);
164 perror("os_create_anonymous_file");
168 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
169 if (data == MAP_FAILED) {
175 pool = wl_shm_create_pool(shm, fd, size);
177 buffer = wl_shm_pool_create_buffer(pool, 0,
180 WL_SHM_FORMAT_ARGB8888);
181 wl_buffer_add_listener(buffer, &buffer_listener, buffer);
182 wl_shm_pool_destroy(pool);
185 memset(data, 0, size);
187 wl_surface_attach(surface, buffer, 0, 0);
189 /* We only need to damage some part, as its only transparant
191 wl_surface_damage(surface, 0, 0, 1, 1);
195 wayland_output_start_repaint_loop(struct weston_output *output_base)
197 struct wayland_output *output = (struct wayland_output *) output_base;
198 struct wl_callback *callback;
200 /* If this is the initial frame, we need to attach a buffer so that
201 * the compositor can map the surface and include it in its render
202 * loop. If the surface doesn't end up in the render loop, the frame
203 * callback won't be invoked. The buffer is transparent and of the
204 * same size as the future real output buffer. */
205 if (output->parent.draw_initial_frame) {
206 output->parent.draw_initial_frame = 0;
208 draw_initial_frame(output);
211 callback = wl_surface_frame(output->parent.surface);
212 wl_callback_add_listener(callback, &frame_listener, output);
213 wl_surface_commit(output->parent.surface);
217 wayland_output_repaint(struct weston_output *output_base,
218 pixman_region32_t *damage)
220 struct wayland_output *output = (struct wayland_output *) output_base;
221 struct weston_compositor *ec = output->base.compositor;
222 struct wl_callback *callback;
224 callback = wl_surface_frame(output->parent.surface);
225 wl_callback_add_listener(callback, &frame_listener, output);
227 ec->renderer->repaint_output(&output->base, damage);
229 pixman_region32_subtract(&ec->primary_plane.damage,
230 &ec->primary_plane.damage, damage);
235 wayland_output_destroy(struct weston_output *output_base)
237 struct wayland_output *output = (struct wayland_output *) output_base;
239 gl_renderer_output_destroy(output_base);
241 wl_egl_window_destroy(output->parent.egl_window);
247 static const struct wl_shell_surface_listener shell_surface_listener;
250 wayland_compositor_create_output(struct wayland_compositor *c,
251 int width, int height)
253 struct wayland_output *output;
255 output = malloc(sizeof *output);
258 memset(output, 0, sizeof *output);
261 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
262 output->mode.width = width;
263 output->mode.height = height;
264 output->mode.refresh = 60;
265 wl_list_init(&output->base.mode_list);
266 wl_list_insert(&output->base.mode_list, &output->mode.link);
268 output->base.current = &output->mode;
269 weston_output_init(&output->base, &c->base, 0, 0, width, height,
270 WL_OUTPUT_TRANSFORM_NORMAL);
272 output->base.make = "waywayland";
273 output->base.model = "none";
275 weston_output_move(&output->base, 0, 0);
277 output->parent.surface =
278 wl_compositor_create_surface(c->parent.compositor);
279 wl_surface_set_user_data(output->parent.surface, output);
281 output->parent.egl_window =
282 wl_egl_window_create(output->parent.surface,
283 width + c->border.left + c->border.right,
284 height + c->border.top + c->border.bottom);
285 if (!output->parent.egl_window) {
286 weston_log("failure to create wl_egl_window\n");
290 if (gl_renderer_output_create(&output->base,
291 output->parent.egl_window) < 0)
294 output->parent.draw_initial_frame = 1;
295 output->parent.shell_surface =
296 wl_shell_get_shell_surface(c->parent.shell,
297 output->parent.surface);
298 wl_shell_surface_add_listener(output->parent.shell_surface,
299 &shell_surface_listener, output);
300 wl_shell_surface_set_toplevel(output->parent.shell_surface);
302 output->base.origin = output->base.current;
303 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
304 output->base.repaint = wayland_output_repaint;
305 output->base.destroy = wayland_output_destroy;
306 output->base.assign_planes = NULL;
307 output->base.set_backlight = NULL;
308 output->base.set_dpms = NULL;
309 output->base.switch_mode = NULL;
311 wl_list_insert(c->base.output_list.prev, &output->base.link);
316 wl_egl_window_destroy(output->parent.egl_window);
318 /* FIXME: cleanup weston_output */
325 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
328 wl_shell_surface_pong(shell_surface, serial);
332 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
333 uint32_t edges, int32_t width, int32_t height)
335 /* FIXME: implement resizing */
339 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
343 static const struct wl_shell_surface_listener shell_surface_listener = {
345 shell_surface_configure,
346 shell_surface_popup_done
349 /* Events received from the wayland-server this compositor is client of: */
351 /* parent output interface */
353 display_handle_geometry(void *data,
354 struct wl_output *wl_output,
364 struct wayland_compositor *c = data;
366 c->parent.screen_allocation.x = x;
367 c->parent.screen_allocation.y = y;
371 display_handle_mode(void *data,
372 struct wl_output *wl_output,
378 struct wayland_compositor *c = data;
380 c->parent.screen_allocation.width = width;
381 c->parent.screen_allocation.height = height;
384 static const struct wl_output_listener output_listener = {
385 display_handle_geometry,
390 check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
392 struct wayland_compositor *c = input->compositor;
393 int width, height, inside;
395 width = input->output->mode.width;
396 height = input->output->mode.height;
398 inside = c->border.left <= wl_fixed_to_int(x) &&
399 wl_fixed_to_int(x) < width + c->border.left &&
400 c->border.top <= wl_fixed_to_int(y) &&
401 wl_fixed_to_int(y) < height + c->border.top;
403 if (!input->focus && inside) {
404 notify_pointer_focus(&input->base, &input->output->base,
405 x - wl_fixed_from_int(c->border.left),
406 y = wl_fixed_from_int(c->border.top));
407 wl_pointer_set_cursor(input->pointer,
408 input->enter_serial, NULL, 0, 0);
409 } else if (input->focus && !inside) {
410 notify_pointer_focus(&input->base, NULL, 0, 0);
411 /* FIXME: Should set default cursor here. */
414 input->focus = inside;
417 /* parent input interface */
419 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
420 uint32_t serial, struct wl_surface *surface,
421 wl_fixed_t x, wl_fixed_t y)
423 struct wayland_input *input = data;
425 /* XXX: If we get a modifier event immediately before the focus,
426 * we should try to keep the same serial. */
427 input->enter_serial = serial;
428 input->output = wl_surface_get_user_data(surface);
429 check_focus(input, x, y);
433 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
434 uint32_t serial, struct wl_surface *surface)
436 struct wayland_input *input = data;
438 notify_pointer_focus(&input->base, NULL, 0, 0);
439 input->output = NULL;
444 input_handle_motion(void *data, struct wl_pointer *pointer,
445 uint32_t time, wl_fixed_t x, wl_fixed_t y)
447 struct wayland_input *input = data;
448 struct wayland_compositor *c = input->compositor;
450 check_focus(input, x, y);
452 notify_motion(&input->base, time,
453 x - wl_fixed_from_int(c->border.left) -
454 input->base.pointer->x,
455 y - wl_fixed_from_int(c->border.top) -
456 input->base.pointer->y);
460 input_handle_button(void *data, struct wl_pointer *pointer,
461 uint32_t serial, uint32_t time, uint32_t button,
464 struct wayland_input *input = data;
465 enum wl_pointer_button_state state = state_w;
467 notify_button(&input->base, time, button, state);
471 input_handle_axis(void *data, struct wl_pointer *pointer,
472 uint32_t time, uint32_t axis, wl_fixed_t value)
474 struct wayland_input *input = data;
476 notify_axis(&input->base, time, axis, value);
479 static const struct wl_pointer_listener pointer_listener = {
480 input_handle_pointer_enter,
481 input_handle_pointer_leave,
488 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
489 int fd, uint32_t size)
491 struct wayland_input *input = data;
492 struct xkb_keymap *keymap;
500 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
505 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
506 if (map_str == MAP_FAILED) {
511 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
513 XKB_KEYMAP_FORMAT_TEXT_V1,
515 munmap(map_str, size);
519 weston_log("failed to compile keymap\n");
523 weston_seat_init_keyboard(&input->base, keymap);
524 xkb_map_unref(keymap);
528 input_handle_keyboard_enter(void *data,
529 struct wl_keyboard *keyboard,
531 struct wl_surface *surface,
532 struct wl_array *keys)
534 struct wayland_input *input = data;
536 /* XXX: If we get a modifier event immediately before the focus,
537 * we should try to keep the same serial. */
538 notify_keyboard_focus_in(&input->base, keys,
539 STATE_UPDATE_AUTOMATIC);
543 input_handle_keyboard_leave(void *data,
544 struct wl_keyboard *keyboard,
546 struct wl_surface *surface)
548 struct wayland_input *input = data;
550 notify_keyboard_focus_out(&input->base);
554 input_handle_key(void *data, struct wl_keyboard *keyboard,
555 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
557 struct wayland_input *input = data;
559 input->key_serial = serial;
560 notify_key(&input->base, time, key,
561 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
562 WL_KEYBOARD_KEY_STATE_RELEASED,
567 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
568 uint32_t serial_in, uint32_t mods_depressed,
569 uint32_t mods_latched, uint32_t mods_locked,
572 struct wayland_input *input = data;
573 struct wayland_compositor *c = input->compositor;
576 /* If we get a key event followed by a modifier event with the
577 * same serial number, then we try to preserve those semantics by
578 * reusing the same serial number on the way out too. */
579 if (serial_in == input->key_serial)
580 serial_out = wl_display_get_serial(c->base.wl_display);
582 serial_out = wl_display_next_serial(c->base.wl_display);
584 xkb_state_update_mask(input->base.xkb_state.state,
585 mods_depressed, mods_latched,
586 mods_locked, 0, 0, group);
587 notify_modifiers(&input->base, serial_out);
590 static const struct wl_keyboard_listener keyboard_listener = {
592 input_handle_keyboard_enter,
593 input_handle_keyboard_leave,
595 input_handle_modifiers,
599 input_handle_capabilities(void *data, struct wl_seat *seat,
600 enum wl_seat_capability caps)
602 struct wayland_input *input = data;
604 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
605 input->pointer = wl_seat_get_pointer(seat);
606 wl_pointer_set_user_data(input->pointer, input);
607 wl_pointer_add_listener(input->pointer, &pointer_listener,
609 weston_seat_init_pointer(&input->base);
610 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
611 wl_pointer_destroy(input->pointer);
612 input->pointer = NULL;
615 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
616 input->keyboard = wl_seat_get_keyboard(seat);
617 wl_keyboard_set_user_data(input->keyboard, input);
618 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
620 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
621 wl_keyboard_destroy(input->keyboard);
622 input->keyboard = NULL;
626 static const struct wl_seat_listener seat_listener = {
627 input_handle_capabilities,
631 display_add_seat(struct wayland_compositor *c, uint32_t id)
633 struct wayland_input *input;
635 input = malloc(sizeof *input);
639 memset(input, 0, sizeof *input);
641 weston_seat_init(&input->base, &c->base);
642 input->compositor = c;
643 input->seat = wl_registry_bind(c->parent.registry, id,
644 &wl_seat_interface, 1);
645 wl_list_insert(c->input_list.prev, &input->link);
647 wl_seat_add_listener(input->seat, &seat_listener, input);
648 wl_seat_set_user_data(input->seat, input);
652 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
653 const char *interface, uint32_t version)
655 struct wayland_compositor *c = data;
657 if (strcmp(interface, "wl_compositor") == 0) {
658 c->parent.compositor =
659 wl_registry_bind(registry, name,
660 &wl_compositor_interface, 1);
661 } else if (strcmp(interface, "wl_output") == 0) {
663 wl_registry_bind(registry, name,
664 &wl_output_interface, 1);
665 wl_output_add_listener(c->parent.output, &output_listener, c);
666 } else if (strcmp(interface, "wl_shell") == 0) {
668 wl_registry_bind(registry, name,
669 &wl_shell_interface, 1);
670 } else if (strcmp(interface, "wl_seat") == 0) {
671 display_add_seat(c, name);
672 } else if (strcmp(interface, "wl_shm") == 0) {
674 wl_registry_bind(registry, name, &wl_shm_interface, 1);
678 static const struct wl_registry_listener registry_listener = {
679 registry_handle_global
683 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
685 struct wayland_compositor *c = data;
688 if (mask & WL_EVENT_READABLE)
689 count = wl_display_dispatch(c->parent.wl_display);
690 if (mask & WL_EVENT_WRITABLE)
691 wl_display_flush(c->parent.wl_display);
694 count = wl_display_dispatch_pending(c->parent.wl_display);
695 wl_display_flush(c->parent.wl_display);
702 wayland_restore(struct weston_compositor *ec)
707 wayland_destroy(struct weston_compositor *ec)
709 struct wayland_compositor *c = (struct wayland_compositor *) ec;
711 ec->renderer->destroy(ec);
713 weston_compositor_shutdown(ec);
716 wl_shm_destroy(c->parent.shm);
721 static struct weston_compositor *
722 wayland_compositor_create(struct wl_display *display,
723 int width, int height, const char *display_name,
724 int *argc, char *argv[], int config_fd)
726 struct wayland_compositor *c;
727 struct wl_event_loop *loop;
730 c = malloc(sizeof *c);
734 memset(c, 0, sizeof *c);
736 if (weston_compositor_init(&c->base, display, argc, argv,
740 c->parent.wl_display = wl_display_connect(display_name);
742 if (c->parent.wl_display == NULL) {
743 weston_log("failed to create display: %m\n");
747 wl_list_init(&c->input_list);
748 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
749 wl_registry_add_listener(c->parent.registry, ®istry_listener, c);
750 wl_display_dispatch(c->parent.wl_display);
752 c->base.wl_display = display;
753 if (gl_renderer_create(&c->base, c->parent.wl_display,
754 gl_renderer_alpha_attribs,
758 c->base.destroy = wayland_destroy;
759 c->base.restore = wayland_restore;
762 c->border.bottom = 24;
764 c->border.right = 26;
766 /* requires border fields */
767 if (wayland_compositor_create_output(c, width, height) < 0)
770 /* requires gl_renderer_output_state_create called
771 * by wayland_compositor_create_output */
774 loop = wl_display_get_event_loop(c->base.wl_display);
776 fd = wl_display_get_fd(c->parent.wl_display);
777 c->parent.wl_source =
778 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
779 wayland_compositor_handle_event, c);
780 if (c->parent.wl_source == NULL)
783 wl_event_source_check(c->parent.wl_source);
788 c->base.renderer->destroy(&c->base);
790 wl_display_disconnect(c->parent.wl_display);
792 weston_compositor_shutdown(&c->base);
798 WL_EXPORT struct weston_compositor *
799 backend_init(struct wl_display *display, int *argc, char *argv[],
802 int width = 1024, height = 640;
803 char *display_name = NULL;
805 const struct weston_option wayland_options[] = {
806 { WESTON_OPTION_INTEGER, "width", 0, &width },
807 { WESTON_OPTION_INTEGER, "height", 0, &height },
808 { WESTON_OPTION_STRING, "display", 0, &display_name },
811 parse_options(wayland_options,
812 ARRAY_LENGTH(wayland_options), argc, argv);
814 return wayland_compositor_create(display, width, height, display_name,
815 argc, argv, config_fd);