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 <GLES2/gl2.h>
40 #include <GLES2/gl2ext.h>
42 #include <EGL/eglext.h>
44 #include "compositor.h"
46 struct wayland_compositor {
47 struct weston_compositor base;
50 struct wl_display *wl_display;
51 struct wl_registry *registry;
52 struct wl_compositor *compositor;
53 struct wl_shell *shell;
54 struct wl_output *output;
57 int32_t x, y, width, height;
60 struct wl_event_source *wl_source;
65 int32_t top, bottom, left, right;
67 int32_t width, height;
70 struct wl_list input_list;
73 struct wayland_output {
74 struct weston_output base;
75 struct wl_listener frame_listener;
77 struct wl_surface *surface;
78 struct wl_shell_surface *shell_surface;
79 struct wl_egl_window *egl_window;
81 struct weston_mode mode;
84 struct wayland_input {
85 struct weston_seat base;
86 struct wayland_compositor *compositor;
88 struct wl_pointer *pointer;
89 struct wl_keyboard *keyboard;
90 struct wl_touch *touch;
93 uint32_t enter_serial;
95 struct wayland_output *output;
100 texture_border(struct wayland_output *output)
102 struct wayland_compositor *c =
103 (struct wayland_compositor *) output->base.compositor;
107 GLfloat x[4], y[4], u[4], v[4];
109 x[0] = -c->border.left;
111 x[2] = output->base.current->width;
112 x[3] = output->base.current->width + c->border.right;
114 y[0] = -c->border.top;
116 y[2] = output->base.current->height;
117 y[3] = output->base.current->height + c->border.bottom;
120 u[1] = (GLfloat) c->border.left / c->border.width;
121 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
125 v[1] = (GLfloat) c->border.top / c->border.height;
126 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
130 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
131 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
134 for (i = 0; i < 3; i++)
135 for (j = 0; j < 3; j++) {
137 if (i == 1 && j == 1)
176 draw_border(struct wayland_output *output)
178 struct wayland_compositor *c =
179 (struct wayland_compositor *) output->base.compositor;
180 struct weston_shader *shader = &c->base.texture_shader_rgba;
185 glUseProgram(shader->program);
186 c->base.current_shader = shader;
188 glUniformMatrix4fv(shader->proj_uniform,
189 1, GL_FALSE, output->base.matrix.d);
191 glUniform1i(shader->tex_uniforms[0], 0);
192 glUniform1f(shader->alpha_uniform, 1);
194 n = texture_border(output);
196 glBindTexture(GL_TEXTURE_2D, c->border.texture);
198 v = c->base.vertices.data;
199 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
200 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
201 glEnableVertexAttribArray(0);
202 glEnableVertexAttribArray(1);
204 glDrawElements(GL_TRIANGLES, n * 6,
205 GL_UNSIGNED_INT, c->base.indices.data);
207 glDisableVertexAttribArray(1);
208 glDisableVertexAttribArray(0);
210 c->base.vertices.size = 0;
211 c->base.indices.size = 0;
215 create_border(struct wayland_compositor *c)
217 pixman_image_t *image;
219 image = load_image(DATADIR "/weston/border.png");
221 weston_log("could'nt load border image\n");
225 c->border.width = pixman_image_get_width(image);
226 c->border.height = pixman_image_get_height(image);
228 glGenTextures(1, &c->border.texture);
229 glBindTexture(GL_TEXTURE_2D, c->border.texture);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
235 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
238 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
239 pixman_image_get_data(image));
241 pixman_image_unref(image);
245 wayland_compositor_init_egl(struct wayland_compositor *c)
249 EGLint config_attribs[] = {
250 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
255 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
259 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
260 if (c->base.egl_display == NULL) {
261 weston_log("failed to create display\n");
265 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
266 weston_log("failed to initialize display\n");
270 if (!eglChooseConfig(c->base.egl_display, config_attribs,
271 &c->base.egl_config, 1, &n) || n == 0) {
272 weston_log("failed to choose config: %d\n", n);
280 frame_done(void *data, struct wl_callback *callback, uint32_t time)
282 struct weston_output *output = data;
284 wl_callback_destroy(callback);
285 weston_output_finish_frame(output, time);
288 static const struct wl_callback_listener frame_listener = {
293 wayland_output_frame_notify(struct wl_listener *listener, void *data)
295 struct wayland_output *output =
296 container_of(listener,
297 struct wayland_output, frame_listener);
303 wayland_output_repaint(struct weston_output *output_base,
304 pixman_region32_t *damage)
306 struct wayland_output *output = (struct wayland_output *) output_base;
307 struct weston_compositor *ec = output->base.compositor;
308 struct wl_callback *callback;
310 callback = wl_surface_frame(output->parent.surface);
311 wl_callback_add_listener(callback, &frame_listener, output);
313 ec->renderer->repaint_output(&output->base, damage);
317 wayland_output_destroy(struct weston_output *output_base)
319 struct wayland_output *output = (struct wayland_output *) output_base;
320 struct weston_compositor *ec = output->base.compositor;
322 eglDestroySurface(ec->egl_display, output->base.egl_surface);
323 wl_egl_window_destroy(output->parent.egl_window);
329 static const struct wl_shell_surface_listener shell_surface_listener;
332 wayland_compositor_create_output(struct wayland_compositor *c,
333 int width, int height)
335 struct wayland_output *output;
337 output = malloc(sizeof *output);
340 memset(output, 0, sizeof *output);
343 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
344 output->mode.width = width;
345 output->mode.height = height;
346 output->mode.refresh = 60;
347 wl_list_init(&output->base.mode_list);
348 wl_list_insert(&output->base.mode_list, &output->mode.link);
350 output->base.current = &output->mode;
351 weston_output_init(&output->base, &c->base, 0, 0, width, height,
352 WL_OUTPUT_TRANSFORM_NORMAL);
354 output->base.border.top = c->border.top;
355 output->base.border.bottom = c->border.bottom;
356 output->base.border.left = c->border.left;
357 output->base.border.right = c->border.right;
358 output->base.make = "waywayland";
359 output->base.model = "none";
361 weston_output_move(&output->base, 0, 0);
363 output->parent.surface =
364 wl_compositor_create_surface(c->parent.compositor);
365 wl_surface_set_user_data(output->parent.surface, output);
367 output->parent.egl_window =
368 wl_egl_window_create(output->parent.surface,
369 width + c->border.left + c->border.right,
370 height + c->border.top + c->border.bottom);
371 if (!output->parent.egl_window) {
372 weston_log("failure to create wl_egl_window\n");
376 output->base.egl_surface =
377 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
378 output->parent.egl_window, NULL);
379 if (!output->base.egl_surface) {
380 weston_log("failed to create window surface\n");
384 output->parent.shell_surface =
385 wl_shell_get_shell_surface(c->parent.shell,
386 output->parent.surface);
387 wl_shell_surface_add_listener(output->parent.shell_surface,
388 &shell_surface_listener, output);
389 wl_shell_surface_set_toplevel(output->parent.shell_surface);
391 output->base.origin = output->base.current;
392 output->base.repaint = wayland_output_repaint;
393 output->base.destroy = wayland_output_destroy;
394 output->base.assign_planes = NULL;
395 output->base.set_backlight = NULL;
396 output->base.set_dpms = NULL;
397 output->base.switch_mode = NULL;
399 wl_list_insert(c->base.output_list.prev, &output->base.link);
401 output->frame_listener.notify = wayland_output_frame_notify;
402 wl_signal_add(&output->base.frame_signal, &output->frame_listener);
407 wl_egl_window_destroy(output->parent.egl_window);
409 /* FIXME: cleanup weston_output */
416 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
419 wl_shell_surface_pong(shell_surface, serial);
423 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
424 uint32_t edges, int32_t width, int32_t height)
426 /* FIXME: implement resizing */
430 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
434 static const struct wl_shell_surface_listener shell_surface_listener = {
436 shell_surface_configure,
437 shell_surface_popup_done
440 /* Events received from the wayland-server this compositor is client of: */
442 /* parent output interface */
444 display_handle_geometry(void *data,
445 struct wl_output *wl_output,
455 struct wayland_compositor *c = data;
457 c->parent.screen_allocation.x = x;
458 c->parent.screen_allocation.y = y;
462 display_handle_mode(void *data,
463 struct wl_output *wl_output,
469 struct wayland_compositor *c = data;
471 c->parent.screen_allocation.width = width;
472 c->parent.screen_allocation.height = height;
475 static const struct wl_output_listener output_listener = {
476 display_handle_geometry,
481 check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
483 struct wayland_compositor *c = input->compositor;
484 int width, height, inside;
486 width = input->output->mode.width;
487 height = input->output->mode.height;
489 inside = c->border.left <= wl_fixed_to_int(x) &&
490 wl_fixed_to_int(x) < width + c->border.left &&
491 c->border.top <= wl_fixed_to_int(y) &&
492 wl_fixed_to_int(y) < height + c->border.top;
494 if (!input->focus && inside) {
495 notify_pointer_focus(&input->base, &input->output->base,
496 x - wl_fixed_from_int(c->border.left),
497 y = wl_fixed_from_int(c->border.top));
498 wl_pointer_set_cursor(input->pointer,
499 input->enter_serial, NULL, 0, 0);
500 } else if (input->focus && !inside) {
501 notify_pointer_focus(&input->base, NULL, 0, 0);
502 /* FIXME: Should set default cursor here. */
505 input->focus = inside;
508 /* parent input interface */
510 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
511 uint32_t serial, struct wl_surface *surface,
512 wl_fixed_t x, wl_fixed_t y)
514 struct wayland_input *input = data;
516 /* XXX: If we get a modifier event immediately before the focus,
517 * we should try to keep the same serial. */
518 input->enter_serial = serial;
519 input->output = wl_surface_get_user_data(surface);
520 check_focus(input, x, y);
524 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
525 uint32_t serial, struct wl_surface *surface)
527 struct wayland_input *input = data;
529 notify_pointer_focus(&input->base, NULL, 0, 0);
530 input->output = NULL;
535 input_handle_motion(void *data, struct wl_pointer *pointer,
536 uint32_t time, wl_fixed_t x, wl_fixed_t y)
538 struct wayland_input *input = data;
539 struct wayland_compositor *c = input->compositor;
541 check_focus(input, x, y);
543 notify_motion(&input->base, time,
544 x - wl_fixed_from_int(c->border.left),
545 y - wl_fixed_from_int(c->border.top));
549 input_handle_button(void *data, struct wl_pointer *pointer,
550 uint32_t serial, uint32_t time, uint32_t button,
553 struct wayland_input *input = data;
554 enum wl_pointer_button_state state = state_w;
556 notify_button(&input->base, time, button, state);
560 input_handle_axis(void *data, struct wl_pointer *pointer,
561 uint32_t time, uint32_t axis, wl_fixed_t value)
563 struct wayland_input *input = data;
565 notify_axis(&input->base, time, axis, value);
568 static const struct wl_pointer_listener pointer_listener = {
569 input_handle_pointer_enter,
570 input_handle_pointer_leave,
577 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
578 int fd, uint32_t size)
580 struct wayland_input *input = data;
581 struct xkb_keymap *keymap;
589 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
594 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
595 if (map_str == MAP_FAILED) {
600 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
602 XKB_KEYMAP_FORMAT_TEXT_V1,
604 munmap(map_str, size);
608 weston_log("failed to compile keymap\n");
612 weston_seat_init_keyboard(&input->base, keymap);
613 xkb_map_unref(keymap);
617 input_handle_keyboard_enter(void *data,
618 struct wl_keyboard *keyboard,
620 struct wl_surface *surface,
621 struct wl_array *keys)
623 struct wayland_input *input = data;
625 /* XXX: If we get a modifier event immediately before the focus,
626 * we should try to keep the same serial. */
627 notify_keyboard_focus_in(&input->base, keys,
628 STATE_UPDATE_AUTOMATIC);
632 input_handle_keyboard_leave(void *data,
633 struct wl_keyboard *keyboard,
635 struct wl_surface *surface)
637 struct wayland_input *input = data;
639 notify_keyboard_focus_out(&input->base);
643 input_handle_key(void *data, struct wl_keyboard *keyboard,
644 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
646 struct wayland_input *input = data;
648 input->key_serial = serial;
649 notify_key(&input->base, time, key,
650 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
651 WL_KEYBOARD_KEY_STATE_RELEASED,
656 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
657 uint32_t serial_in, uint32_t mods_depressed,
658 uint32_t mods_latched, uint32_t mods_locked,
661 struct wayland_input *input = data;
662 struct wayland_compositor *c = input->compositor;
665 /* If we get a key event followed by a modifier event with the
666 * same serial number, then we try to preserve those semantics by
667 * reusing the same serial number on the way out too. */
668 if (serial_in == input->key_serial)
669 serial_out = wl_display_get_serial(c->base.wl_display);
671 serial_out = wl_display_next_serial(c->base.wl_display);
673 xkb_state_update_mask(input->base.xkb_state.state,
674 mods_depressed, mods_latched,
675 mods_locked, 0, 0, group);
676 notify_modifiers(&input->base, serial_out);
679 static const struct wl_keyboard_listener keyboard_listener = {
681 input_handle_keyboard_enter,
682 input_handle_keyboard_leave,
684 input_handle_modifiers,
688 input_handle_capabilities(void *data, struct wl_seat *seat,
689 enum wl_seat_capability caps)
691 struct wayland_input *input = data;
693 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
694 input->pointer = wl_seat_get_pointer(seat);
695 wl_pointer_set_user_data(input->pointer, input);
696 wl_pointer_add_listener(input->pointer, &pointer_listener,
698 weston_seat_init_pointer(&input->base);
699 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
700 wl_pointer_destroy(input->pointer);
701 input->pointer = NULL;
704 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
705 input->keyboard = wl_seat_get_keyboard(seat);
706 wl_keyboard_set_user_data(input->keyboard, input);
707 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
709 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
710 wl_keyboard_destroy(input->keyboard);
711 input->keyboard = NULL;
715 static const struct wl_seat_listener seat_listener = {
716 input_handle_capabilities,
720 display_add_seat(struct wayland_compositor *c, uint32_t id)
722 struct wayland_input *input;
724 input = malloc(sizeof *input);
728 memset(input, 0, sizeof *input);
730 weston_seat_init(&input->base, &c->base);
731 input->compositor = c;
732 input->seat = wl_registry_bind(c->parent.registry, id,
733 &wl_seat_interface, 1);
734 wl_list_insert(c->input_list.prev, &input->link);
736 wl_seat_add_listener(input->seat, &seat_listener, input);
737 wl_seat_set_user_data(input->seat, input);
741 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
742 const char *interface, uint32_t version)
744 struct wayland_compositor *c = data;
746 if (strcmp(interface, "wl_compositor") == 0) {
747 c->parent.compositor =
748 wl_registry_bind(registry, name,
749 &wl_compositor_interface, 1);
750 } else if (strcmp(interface, "wl_output") == 0) {
752 wl_registry_bind(registry, name,
753 &wl_output_interface, 1);
754 wl_output_add_listener(c->parent.output, &output_listener, c);
755 } else if (strcmp(interface, "wl_shell") == 0) {
757 wl_registry_bind(registry, name,
758 &wl_shell_interface, 1);
759 } else if (strcmp(interface, "wl_seat") == 0) {
760 display_add_seat(c, name);
764 static const struct wl_registry_listener registry_listener = {
765 registry_handle_global
769 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
771 struct wayland_compositor *c = data;
774 if (mask & WL_EVENT_READABLE)
775 count = wl_display_dispatch(c->parent.wl_display);
776 if (mask & WL_EVENT_WRITABLE)
777 wl_display_flush(c->parent.wl_display);
780 count = wl_display_dispatch_pending(c->parent.wl_display);
781 wl_display_flush(c->parent.wl_display);
788 wayland_restore(struct weston_compositor *ec)
793 wayland_destroy(struct weston_compositor *ec)
795 gles2_renderer_destroy(ec);
797 weston_compositor_shutdown(ec);
802 static struct weston_compositor *
803 wayland_compositor_create(struct wl_display *display,
804 int width, int height, const char *display_name,
805 int argc, char *argv[], const char *config_file)
807 struct wayland_compositor *c;
808 struct wl_event_loop *loop;
811 c = malloc(sizeof *c);
815 memset(c, 0, sizeof *c);
817 if (weston_compositor_init(&c->base, display, argc, argv,
821 c->parent.wl_display = wl_display_connect(display_name);
823 if (c->parent.wl_display == NULL) {
824 weston_log("failed to create display: %m\n");
828 wl_list_init(&c->input_list);
829 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
830 wl_registry_add_listener(c->parent.registry, ®istry_listener, c);
831 wl_display_dispatch(c->parent.wl_display);
833 c->base.wl_display = display;
834 if (wayland_compositor_init_egl(c) < 0)
837 c->base.destroy = wayland_destroy;
838 c->base.restore = wayland_restore;
841 c->border.bottom = 24;
843 c->border.right = 26;
845 /* requires border fields */
846 if (wayland_compositor_create_output(c, width, height) < 0)
849 /* requires wayland_compositor_create_output */
850 if (gles2_renderer_init(&c->base) < 0)
853 /* requires gles2_renderer_init */
856 loop = wl_display_get_event_loop(c->base.wl_display);
858 fd = wl_display_get_fd(c->parent.wl_display);
859 c->parent.wl_source =
860 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
861 wayland_compositor_handle_event, c);
862 if (c->parent.wl_source == NULL)
865 wl_event_source_check(c->parent.wl_source);
870 wl_display_disconnect(c->parent.wl_display);
872 weston_compositor_shutdown(&c->base);
878 WL_EXPORT struct weston_compositor *
879 backend_init(struct wl_display *display, int argc, char *argv[],
880 const char *config_file)
882 int width = 1024, height = 640;
883 char *display_name = NULL;
885 const struct weston_option wayland_options[] = {
886 { WESTON_OPTION_INTEGER, "width", 0, &width },
887 { WESTON_OPTION_INTEGER, "height", 0, &height },
888 { WESTON_OPTION_STRING, "display", 0, &display_name },
891 parse_options(wayland_options,
892 ARRAY_LENGTH(wayland_options), argc, argv);
894 return wayland_compositor_create(display, width, height, display_name,
895 argc, argv, config_file);