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"
45 #include "../shared/image-loader.h"
47 struct wayland_compositor {
48 struct weston_compositor base;
51 struct wl_display *wl_display;
52 struct wl_registry *registry;
53 struct wl_compositor *compositor;
54 struct wl_shell *shell;
55 struct wl_output *output;
58 int32_t x, y, width, height;
61 struct wl_event_source *wl_source;
66 int32_t top, bottom, left, right;
68 int32_t width, height;
71 struct wl_list input_list;
74 struct wayland_output {
75 struct weston_output base;
76 struct wl_listener frame_listener;
78 struct wl_surface *surface;
79 struct wl_shell_surface *shell_surface;
80 struct wl_egl_window *egl_window;
82 struct weston_mode mode;
85 struct wayland_input {
86 struct weston_seat base;
87 struct wayland_compositor *compositor;
89 struct wl_pointer *pointer;
90 struct wl_keyboard *keyboard;
91 struct wl_touch *touch;
94 uint32_t enter_serial;
96 struct wayland_output *output;
101 texture_border(struct wayland_output *output)
103 struct wayland_compositor *c =
104 (struct wayland_compositor *) output->base.compositor;
108 GLfloat x[4], y[4], u[4], v[4];
110 x[0] = -c->border.left;
112 x[2] = output->base.current->width;
113 x[3] = output->base.current->width + c->border.right;
115 y[0] = -c->border.top;
117 y[2] = output->base.current->height;
118 y[3] = output->base.current->height + c->border.bottom;
121 u[1] = (GLfloat) c->border.left / c->border.width;
122 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
126 v[1] = (GLfloat) c->border.top / c->border.height;
127 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
131 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
132 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
135 for (i = 0; i < 3; i++)
136 for (j = 0; j < 3; j++) {
138 if (i == 1 && j == 1)
177 draw_border(struct wayland_output *output)
179 struct wayland_compositor *c =
180 (struct wayland_compositor *) output->base.compositor;
181 struct weston_shader *shader = &c->base.texture_shader_rgba;
186 glUseProgram(shader->program);
187 c->base.current_shader = shader;
189 glUniformMatrix4fv(shader->proj_uniform,
190 1, GL_FALSE, output->base.matrix.d);
192 glUniform1i(shader->tex_uniforms[0], 0);
193 glUniform1f(shader->alpha_uniform, 1);
195 n = texture_border(output);
197 glBindTexture(GL_TEXTURE_2D, c->border.texture);
199 v = c->base.vertices.data;
200 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
201 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
202 glEnableVertexAttribArray(0);
203 glEnableVertexAttribArray(1);
205 glDrawElements(GL_TRIANGLES, n * 6,
206 GL_UNSIGNED_INT, c->base.indices.data);
208 glDisableVertexAttribArray(1);
209 glDisableVertexAttribArray(0);
211 c->base.vertices.size = 0;
212 c->base.indices.size = 0;
216 create_border(struct wayland_compositor *c)
218 pixman_image_t *image;
220 image = load_image(DATADIR "/weston/border.png");
222 weston_log("could'nt load border image\n");
226 c->border.width = pixman_image_get_width(image);
227 c->border.height = pixman_image_get_height(image);
229 glGenTextures(1, &c->border.texture);
230 glBindTexture(GL_TEXTURE_2D, c->border.texture);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
236 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
239 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
240 pixman_image_get_data(image));
242 pixman_image_unref(image);
246 wayland_compositor_init_egl(struct wayland_compositor *c)
250 EGLint config_attribs[] = {
251 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
256 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
260 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
261 if (c->base.egl_display == NULL) {
262 weston_log("failed to create display\n");
266 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
267 weston_log("failed to initialize display\n");
271 if (!eglChooseConfig(c->base.egl_display, config_attribs,
272 &c->base.egl_config, 1, &n) || n == 0) {
273 weston_log("failed to choose config: %d\n", n);
281 frame_done(void *data, struct wl_callback *callback, uint32_t time)
283 struct weston_output *output = data;
285 wl_callback_destroy(callback);
286 weston_output_finish_frame(output, time);
289 static const struct wl_callback_listener frame_listener = {
294 wayland_output_frame_notify(struct wl_listener *listener, void *data)
296 struct wayland_output *output =
297 container_of(listener,
298 struct wayland_output, frame_listener);
304 wayland_output_repaint(struct weston_output *output_base,
305 pixman_region32_t *damage)
307 struct wayland_output *output = (struct wayland_output *) output_base;
308 struct weston_compositor *ec = output->base.compositor;
309 struct wl_callback *callback;
311 callback = wl_surface_frame(output->parent.surface);
312 wl_callback_add_listener(callback, &frame_listener, output);
314 ec->renderer->repaint_output(&output->base, damage);
316 pixman_region32_subtract(&ec->primary_plane.damage,
317 &ec->primary_plane.damage, damage);
322 wayland_output_destroy(struct weston_output *output_base)
324 struct wayland_output *output = (struct wayland_output *) output_base;
325 struct weston_compositor *ec = output->base.compositor;
327 eglDestroySurface(ec->egl_display, output->base.egl_surface);
328 wl_egl_window_destroy(output->parent.egl_window);
334 static const struct wl_shell_surface_listener shell_surface_listener;
337 wayland_compositor_create_output(struct wayland_compositor *c,
338 int width, int height)
340 struct wayland_output *output;
342 output = malloc(sizeof *output);
345 memset(output, 0, sizeof *output);
348 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
349 output->mode.width = width;
350 output->mode.height = height;
351 output->mode.refresh = 60;
352 wl_list_init(&output->base.mode_list);
353 wl_list_insert(&output->base.mode_list, &output->mode.link);
355 output->base.current = &output->mode;
356 weston_output_init(&output->base, &c->base, 0, 0, width, height,
357 WL_OUTPUT_TRANSFORM_NORMAL);
359 output->base.border.top = c->border.top;
360 output->base.border.bottom = c->border.bottom;
361 output->base.border.left = c->border.left;
362 output->base.border.right = c->border.right;
363 output->base.make = "waywayland";
364 output->base.model = "none";
366 weston_output_move(&output->base, 0, 0);
368 output->parent.surface =
369 wl_compositor_create_surface(c->parent.compositor);
370 wl_surface_set_user_data(output->parent.surface, output);
372 output->parent.egl_window =
373 wl_egl_window_create(output->parent.surface,
374 width + c->border.left + c->border.right,
375 height + c->border.top + c->border.bottom);
376 if (!output->parent.egl_window) {
377 weston_log("failure to create wl_egl_window\n");
381 output->base.egl_surface =
382 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
383 output->parent.egl_window, NULL);
384 if (!output->base.egl_surface) {
385 weston_log("failed to create window surface\n");
389 output->parent.shell_surface =
390 wl_shell_get_shell_surface(c->parent.shell,
391 output->parent.surface);
392 wl_shell_surface_add_listener(output->parent.shell_surface,
393 &shell_surface_listener, output);
394 wl_shell_surface_set_toplevel(output->parent.shell_surface);
396 output->base.origin = output->base.current;
397 output->base.repaint = wayland_output_repaint;
398 output->base.destroy = wayland_output_destroy;
399 output->base.assign_planes = NULL;
400 output->base.set_backlight = NULL;
401 output->base.set_dpms = NULL;
402 output->base.switch_mode = NULL;
404 wl_list_insert(c->base.output_list.prev, &output->base.link);
406 output->frame_listener.notify = wayland_output_frame_notify;
407 wl_signal_add(&output->base.frame_signal, &output->frame_listener);
412 wl_egl_window_destroy(output->parent.egl_window);
414 /* FIXME: cleanup weston_output */
421 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
424 wl_shell_surface_pong(shell_surface, serial);
428 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
429 uint32_t edges, int32_t width, int32_t height)
431 /* FIXME: implement resizing */
435 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
439 static const struct wl_shell_surface_listener shell_surface_listener = {
441 shell_surface_configure,
442 shell_surface_popup_done
445 /* Events received from the wayland-server this compositor is client of: */
447 /* parent output interface */
449 display_handle_geometry(void *data,
450 struct wl_output *wl_output,
460 struct wayland_compositor *c = data;
462 c->parent.screen_allocation.x = x;
463 c->parent.screen_allocation.y = y;
467 display_handle_mode(void *data,
468 struct wl_output *wl_output,
474 struct wayland_compositor *c = data;
476 c->parent.screen_allocation.width = width;
477 c->parent.screen_allocation.height = height;
480 static const struct wl_output_listener output_listener = {
481 display_handle_geometry,
486 check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
488 struct wayland_compositor *c = input->compositor;
489 int width, height, inside;
491 width = input->output->mode.width;
492 height = input->output->mode.height;
494 inside = c->border.left <= wl_fixed_to_int(x) &&
495 wl_fixed_to_int(x) < width + c->border.left &&
496 c->border.top <= wl_fixed_to_int(y) &&
497 wl_fixed_to_int(y) < height + c->border.top;
499 if (!input->focus && inside) {
500 notify_pointer_focus(&input->base, &input->output->base,
501 x - wl_fixed_from_int(c->border.left),
502 y = wl_fixed_from_int(c->border.top));
503 wl_pointer_set_cursor(input->pointer,
504 input->enter_serial, NULL, 0, 0);
505 } else if (input->focus && !inside) {
506 notify_pointer_focus(&input->base, NULL, 0, 0);
507 /* FIXME: Should set default cursor here. */
510 input->focus = inside;
513 /* parent input interface */
515 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
516 uint32_t serial, struct wl_surface *surface,
517 wl_fixed_t x, wl_fixed_t y)
519 struct wayland_input *input = data;
521 /* XXX: If we get a modifier event immediately before the focus,
522 * we should try to keep the same serial. */
523 input->enter_serial = serial;
524 input->output = wl_surface_get_user_data(surface);
525 check_focus(input, x, y);
529 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
530 uint32_t serial, struct wl_surface *surface)
532 struct wayland_input *input = data;
534 notify_pointer_focus(&input->base, NULL, 0, 0);
535 input->output = NULL;
540 input_handle_motion(void *data, struct wl_pointer *pointer,
541 uint32_t time, wl_fixed_t x, wl_fixed_t y)
543 struct wayland_input *input = data;
544 struct wayland_compositor *c = input->compositor;
546 check_focus(input, x, y);
548 notify_motion(&input->base, time,
549 x - wl_fixed_from_int(c->border.left),
550 y - wl_fixed_from_int(c->border.top));
554 input_handle_button(void *data, struct wl_pointer *pointer,
555 uint32_t serial, uint32_t time, uint32_t button,
558 struct wayland_input *input = data;
559 enum wl_pointer_button_state state = state_w;
561 notify_button(&input->base, time, button, state);
565 input_handle_axis(void *data, struct wl_pointer *pointer,
566 uint32_t time, uint32_t axis, wl_fixed_t value)
568 struct wayland_input *input = data;
570 notify_axis(&input->base, time, axis, value);
573 static const struct wl_pointer_listener pointer_listener = {
574 input_handle_pointer_enter,
575 input_handle_pointer_leave,
582 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
583 int fd, uint32_t size)
585 struct wayland_input *input = data;
586 struct xkb_keymap *keymap;
594 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
599 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
600 if (map_str == MAP_FAILED) {
605 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
607 XKB_KEYMAP_FORMAT_TEXT_V1,
609 munmap(map_str, size);
613 weston_log("failed to compile keymap\n");
617 weston_seat_init_keyboard(&input->base, keymap);
618 xkb_map_unref(keymap);
622 input_handle_keyboard_enter(void *data,
623 struct wl_keyboard *keyboard,
625 struct wl_surface *surface,
626 struct wl_array *keys)
628 struct wayland_input *input = data;
630 /* XXX: If we get a modifier event immediately before the focus,
631 * we should try to keep the same serial. */
632 notify_keyboard_focus_in(&input->base, keys,
633 STATE_UPDATE_AUTOMATIC);
637 input_handle_keyboard_leave(void *data,
638 struct wl_keyboard *keyboard,
640 struct wl_surface *surface)
642 struct wayland_input *input = data;
644 notify_keyboard_focus_out(&input->base);
648 input_handle_key(void *data, struct wl_keyboard *keyboard,
649 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
651 struct wayland_input *input = data;
653 input->key_serial = serial;
654 notify_key(&input->base, time, key,
655 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
656 WL_KEYBOARD_KEY_STATE_RELEASED,
661 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
662 uint32_t serial_in, uint32_t mods_depressed,
663 uint32_t mods_latched, uint32_t mods_locked,
666 struct wayland_input *input = data;
667 struct wayland_compositor *c = input->compositor;
670 /* If we get a key event followed by a modifier event with the
671 * same serial number, then we try to preserve those semantics by
672 * reusing the same serial number on the way out too. */
673 if (serial_in == input->key_serial)
674 serial_out = wl_display_get_serial(c->base.wl_display);
676 serial_out = wl_display_next_serial(c->base.wl_display);
678 xkb_state_update_mask(input->base.xkb_state.state,
679 mods_depressed, mods_latched,
680 mods_locked, 0, 0, group);
681 notify_modifiers(&input->base, serial_out);
684 static const struct wl_keyboard_listener keyboard_listener = {
686 input_handle_keyboard_enter,
687 input_handle_keyboard_leave,
689 input_handle_modifiers,
693 input_handle_capabilities(void *data, struct wl_seat *seat,
694 enum wl_seat_capability caps)
696 struct wayland_input *input = data;
698 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
699 input->pointer = wl_seat_get_pointer(seat);
700 wl_pointer_set_user_data(input->pointer, input);
701 wl_pointer_add_listener(input->pointer, &pointer_listener,
703 weston_seat_init_pointer(&input->base);
704 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
705 wl_pointer_destroy(input->pointer);
706 input->pointer = NULL;
709 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
710 input->keyboard = wl_seat_get_keyboard(seat);
711 wl_keyboard_set_user_data(input->keyboard, input);
712 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
714 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
715 wl_keyboard_destroy(input->keyboard);
716 input->keyboard = NULL;
720 static const struct wl_seat_listener seat_listener = {
721 input_handle_capabilities,
725 display_add_seat(struct wayland_compositor *c, uint32_t id)
727 struct wayland_input *input;
729 input = malloc(sizeof *input);
733 memset(input, 0, sizeof *input);
735 weston_seat_init(&input->base, &c->base);
736 input->compositor = c;
737 input->seat = wl_registry_bind(c->parent.registry, id,
738 &wl_seat_interface, 1);
739 wl_list_insert(c->input_list.prev, &input->link);
741 wl_seat_add_listener(input->seat, &seat_listener, input);
742 wl_seat_set_user_data(input->seat, input);
746 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
747 const char *interface, uint32_t version)
749 struct wayland_compositor *c = data;
751 if (strcmp(interface, "wl_compositor") == 0) {
752 c->parent.compositor =
753 wl_registry_bind(registry, name,
754 &wl_compositor_interface, 1);
755 } else if (strcmp(interface, "wl_output") == 0) {
757 wl_registry_bind(registry, name,
758 &wl_output_interface, 1);
759 wl_output_add_listener(c->parent.output, &output_listener, c);
760 } else if (strcmp(interface, "wl_shell") == 0) {
762 wl_registry_bind(registry, name,
763 &wl_shell_interface, 1);
764 } else if (strcmp(interface, "wl_seat") == 0) {
765 display_add_seat(c, name);
769 static const struct wl_registry_listener registry_listener = {
770 registry_handle_global
774 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
776 struct wayland_compositor *c = data;
779 if (mask & WL_EVENT_READABLE)
780 count = wl_display_dispatch(c->parent.wl_display);
781 if (mask & WL_EVENT_WRITABLE)
782 wl_display_flush(c->parent.wl_display);
785 count = wl_display_dispatch_pending(c->parent.wl_display);
786 wl_display_flush(c->parent.wl_display);
793 wayland_restore(struct weston_compositor *ec)
798 wayland_destroy(struct weston_compositor *ec)
800 gles2_renderer_destroy(ec);
802 weston_compositor_shutdown(ec);
807 static struct weston_compositor *
808 wayland_compositor_create(struct wl_display *display,
809 int width, int height, const char *display_name,
810 int argc, char *argv[], const char *config_file)
812 struct wayland_compositor *c;
813 struct wl_event_loop *loop;
816 c = malloc(sizeof *c);
820 memset(c, 0, sizeof *c);
822 if (weston_compositor_init(&c->base, display, argc, argv,
826 c->parent.wl_display = wl_display_connect(display_name);
828 if (c->parent.wl_display == NULL) {
829 weston_log("failed to create display: %m\n");
833 wl_list_init(&c->input_list);
834 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
835 wl_registry_add_listener(c->parent.registry, ®istry_listener, c);
836 wl_display_dispatch(c->parent.wl_display);
838 c->base.wl_display = display;
839 if (wayland_compositor_init_egl(c) < 0)
842 c->base.destroy = wayland_destroy;
843 c->base.restore = wayland_restore;
846 c->border.bottom = 24;
848 c->border.right = 26;
850 /* requires border fields */
851 if (wayland_compositor_create_output(c, width, height) < 0)
854 /* requires wayland_compositor_create_output */
855 if (gles2_renderer_init(&c->base) < 0)
858 /* requires gles2_renderer_init */
861 loop = wl_display_get_event_loop(c->base.wl_display);
863 fd = wl_display_get_fd(c->parent.wl_display);
864 c->parent.wl_source =
865 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
866 wayland_compositor_handle_event, c);
867 if (c->parent.wl_source == NULL)
870 wl_event_source_check(c->parent.wl_source);
875 wl_display_disconnect(c->parent.wl_display);
877 weston_compositor_shutdown(&c->base);
883 WL_EXPORT struct weston_compositor *
884 backend_init(struct wl_display *display, int argc, char *argv[],
885 const char *config_file)
887 int width = 1024, height = 640;
888 char *display_name = NULL;
890 const struct weston_option wayland_options[] = {
891 { WESTON_OPTION_INTEGER, "width", 0, &width },
892 { WESTON_OPTION_INTEGER, "height", 0, &height },
893 { WESTON_OPTION_STRING, "display", 0, &display_name },
896 parse_options(wayland_options,
897 ARRAY_LENGTH(wayland_options), argc, argv);
899 return wayland_compositor_create(display, width, height, display_name,
900 argc, argv, config_file);