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_compositor *compositor;
52 struct wl_shell *shell;
53 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;
66 int32_t width, height;
69 struct wl_list input_list;
72 struct wayland_output {
73 struct weston_output base;
74 struct wl_listener frame_listener;
76 struct wl_surface *surface;
77 struct wl_shell_surface *shell_surface;
78 struct wl_egl_window *egl_window;
80 struct weston_mode mode;
83 struct wayland_input {
84 struct weston_seat base;
85 struct wayland_compositor *compositor;
87 struct wl_pointer *pointer;
88 struct wl_keyboard *keyboard;
89 struct wl_touch *touch;
92 uint32_t enter_serial;
94 struct wayland_output *output;
99 texture_border(struct wayland_output *output)
101 struct wayland_compositor *c =
102 (struct wayland_compositor *) output->base.compositor;
106 GLfloat x[4], y[4], u[4], v[4];
108 x[0] = -c->border.left;
110 x[2] = output->base.current->width;
111 x[3] = output->base.current->width + c->border.right;
113 y[0] = -c->border.top;
115 y[2] = output->base.current->height;
116 y[3] = output->base.current->height + c->border.bottom;
119 u[1] = (GLfloat) c->border.left / c->border.width;
120 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
124 v[1] = (GLfloat) c->border.top / c->border.height;
125 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
129 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
130 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
133 for (i = 0; i < 3; i++)
134 for (j = 0; j < 3; j++) {
136 if (i == 1 && j == 1)
175 draw_border(struct wayland_output *output)
177 struct wayland_compositor *c =
178 (struct wayland_compositor *) output->base.compositor;
179 struct weston_shader *shader = &c->base.texture_shader_rgba;
184 glUseProgram(shader->program);
185 c->base.current_shader = shader;
187 glUniformMatrix4fv(shader->proj_uniform,
188 1, GL_FALSE, output->base.matrix.d);
190 glUniform1i(shader->tex_uniforms[0], 0);
191 glUniform1f(shader->alpha_uniform, 1);
193 n = texture_border(output);
195 glBindTexture(GL_TEXTURE_2D, c->border.texture);
197 v = c->base.vertices.data;
198 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
199 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
200 glEnableVertexAttribArray(0);
201 glEnableVertexAttribArray(1);
203 glDrawElements(GL_TRIANGLES, n * 6,
204 GL_UNSIGNED_INT, c->base.indices.data);
206 glDisableVertexAttribArray(1);
207 glDisableVertexAttribArray(0);
209 c->base.vertices.size = 0;
210 c->base.indices.size = 0;
214 create_border(struct wayland_compositor *c)
216 pixman_image_t *image;
218 image = load_image(DATADIR "/weston/border.png");
220 weston_log("could'nt load border image\n");
224 c->border.width = pixman_image_get_width(image);
225 c->border.height = pixman_image_get_height(image);
227 glGenTextures(1, &c->border.texture);
228 glBindTexture(GL_TEXTURE_2D, c->border.texture);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
237 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
238 pixman_image_get_data(image));
241 c->border.bottom = 50;
243 c->border.right = 25;
245 pixman_image_unref(image);
249 wayland_compositor_init_egl(struct wayland_compositor *c)
253 EGLint config_attribs[] = {
254 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
259 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
263 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
264 if (c->base.egl_display == NULL) {
265 weston_log("failed to create display\n");
269 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
270 weston_log("failed to initialize display\n");
274 if (!eglChooseConfig(c->base.egl_display, config_attribs,
275 &c->base.egl_config, 1, &n) || n == 0) {
276 weston_log("failed to choose config: %d\n", n);
284 frame_done(void *data, struct wl_callback *callback, uint32_t time)
286 struct weston_output *output = data;
288 wl_callback_destroy(callback);
289 weston_output_finish_frame(output, time);
292 static const struct wl_callback_listener frame_listener = {
297 wayland_output_frame_notify(struct wl_listener *listener, void *data)
299 struct wayland_output *output =
300 container_of(listener,
301 struct wayland_output, frame_listener);
307 wayland_output_repaint(struct weston_output *output_base,
308 pixman_region32_t *damage)
310 struct wayland_output *output = (struct wayland_output *) output_base;
311 struct weston_compositor *ec = output->base.compositor;
312 struct wl_callback *callback;
314 ec->renderer->repaint_output(&output->base, damage);
316 callback = wl_surface_frame(output->parent.surface);
317 wl_callback_add_listener(callback, &frame_listener, output);
323 wayland_output_destroy(struct weston_output *output_base)
325 struct wayland_output *output = (struct wayland_output *) output_base;
326 struct weston_compositor *ec = output->base.compositor;
328 eglDestroySurface(ec->egl_display, output->base.egl_surface);
329 wl_egl_window_destroy(output->parent.egl_window);
335 static const struct wl_shell_surface_listener shell_surface_listener;
338 wayland_compositor_create_output(struct wayland_compositor *c,
339 int width, int height)
341 struct wayland_output *output;
343 output = malloc(sizeof *output);
346 memset(output, 0, sizeof *output);
349 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
350 output->mode.width = width;
351 output->mode.height = height;
352 output->mode.refresh = 60;
353 wl_list_init(&output->base.mode_list);
354 wl_list_insert(&output->base.mode_list, &output->mode.link);
356 output->base.current = &output->mode;
357 weston_output_init(&output->base, &c->base, 0, 0, width, height,
358 WL_OUTPUT_TRANSFORM_NORMAL);
360 output->base.border.top = c->border.top;
361 output->base.border.bottom = c->border.bottom;
362 output->base.border.left = c->border.left;
363 output->base.border.right = c->border.right;
364 output->base.make = "waywayland";
365 output->base.model = "none";
367 weston_output_move(&output->base, 0, 0);
369 output->parent.surface =
370 wl_compositor_create_surface(c->parent.compositor);
371 wl_surface_set_user_data(output->parent.surface, output);
373 output->parent.egl_window =
374 wl_egl_window_create(output->parent.surface,
375 width + c->border.left + c->border.right,
376 height + c->border.top + c->border.bottom);
377 if (!output->parent.egl_window) {
378 weston_log("failure to create wl_egl_window\n");
382 output->base.egl_surface =
383 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
384 output->parent.egl_window, NULL);
385 if (!output->base.egl_surface) {
386 weston_log("failed to create window surface\n");
390 output->parent.shell_surface =
391 wl_shell_get_shell_surface(c->parent.shell,
392 output->parent.surface);
393 wl_shell_surface_add_listener(output->parent.shell_surface,
394 &shell_surface_listener, output);
395 wl_shell_surface_set_toplevel(output->parent.shell_surface);
397 output->base.origin = output->base.current;
398 output->base.repaint = wayland_output_repaint;
399 output->base.destroy = wayland_output_destroy;
400 output->base.assign_planes = NULL;
401 output->base.set_backlight = NULL;
402 output->base.set_dpms = NULL;
403 output->base.switch_mode = NULL;
405 wl_list_insert(c->base.output_list.prev, &output->base.link);
407 output->frame_listener.notify = wayland_output_frame_notify;
408 wl_signal_add(&output->base.frame_signal, &output->frame_listener);
413 wl_egl_window_destroy(output->parent.egl_window);
415 /* FIXME: cleanup weston_output */
422 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
425 wl_shell_surface_pong(shell_surface, serial);
429 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
430 uint32_t edges, int32_t width, int32_t height)
432 /* FIXME: implement resizing */
436 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
440 static const struct wl_shell_surface_listener shell_surface_listener = {
442 shell_surface_configure,
443 shell_surface_popup_done
446 /* Events received from the wayland-server this compositor is client of: */
448 /* parent output interface */
450 display_handle_geometry(void *data,
451 struct wl_output *wl_output,
461 struct wayland_compositor *c = data;
463 c->parent.screen_allocation.x = x;
464 c->parent.screen_allocation.y = y;
468 display_handle_mode(void *data,
469 struct wl_output *wl_output,
475 struct wayland_compositor *c = data;
477 c->parent.screen_allocation.width = width;
478 c->parent.screen_allocation.height = height;
481 static const struct wl_output_listener output_listener = {
482 display_handle_geometry,
487 check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
489 struct wayland_compositor *c = input->compositor;
490 int width, height, inside;
492 width = input->output->mode.width;
493 height = input->output->mode.height;
495 inside = c->border.left <= wl_fixed_to_int(x) &&
496 wl_fixed_to_int(x) < width + c->border.left &&
497 c->border.top <= wl_fixed_to_int(y) &&
498 wl_fixed_to_int(y) < height + c->border.top;
500 if (!input->focus && inside) {
501 notify_pointer_focus(&input->base, &input->output->base,
502 x - wl_fixed_from_int(c->border.left),
503 y = wl_fixed_from_int(c->border.top));
504 wl_pointer_set_cursor(input->pointer,
505 input->enter_serial, NULL, 0, 0);
506 } else if (input->focus && !inside) {
507 notify_pointer_focus(&input->base, NULL, 0, 0);
508 /* FIXME: Should set default cursor here. */
511 input->focus = inside;
514 /* parent input interface */
516 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
517 uint32_t serial, struct wl_surface *surface,
518 wl_fixed_t x, wl_fixed_t y)
520 struct wayland_input *input = data;
522 /* XXX: If we get a modifier event immediately before the focus,
523 * we should try to keep the same serial. */
524 input->enter_serial = serial;
525 input->output = wl_surface_get_user_data(surface);
526 check_focus(input, x, y);
530 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
531 uint32_t serial, struct wl_surface *surface)
533 struct wayland_input *input = data;
535 notify_pointer_focus(&input->base, NULL, 0, 0);
536 input->output = NULL;
541 input_handle_motion(void *data, struct wl_pointer *pointer,
542 uint32_t time, wl_fixed_t x, wl_fixed_t y)
544 struct wayland_input *input = data;
545 struct wayland_compositor *c = input->compositor;
547 check_focus(input, x, y);
549 notify_motion(&input->base, time,
550 x - wl_fixed_from_int(c->border.left),
551 y - wl_fixed_from_int(c->border.top));
555 input_handle_button(void *data, struct wl_pointer *pointer,
556 uint32_t serial, uint32_t time, uint32_t button,
559 struct wayland_input *input = data;
560 enum wl_pointer_button_state state = state_w;
562 notify_button(&input->base, time, button, state);
566 input_handle_axis(void *data, struct wl_pointer *pointer,
567 uint32_t time, uint32_t axis, wl_fixed_t value)
569 struct wayland_input *input = data;
571 notify_axis(&input->base, time, axis, value);
574 static const struct wl_pointer_listener pointer_listener = {
575 input_handle_pointer_enter,
576 input_handle_pointer_leave,
583 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
584 int fd, uint32_t size)
586 struct wayland_input *input = data;
587 struct xkb_keymap *keymap;
595 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
600 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
601 if (map_str == MAP_FAILED) {
606 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
608 XKB_KEYMAP_FORMAT_TEXT_V1,
610 munmap(map_str, size);
614 weston_log("failed to compile keymap\n");
618 weston_seat_init_keyboard(&input->base, keymap);
619 xkb_map_unref(keymap);
623 input_handle_keyboard_enter(void *data,
624 struct wl_keyboard *keyboard,
626 struct wl_surface *surface,
627 struct wl_array *keys)
629 struct wayland_input *input = data;
631 /* XXX: If we get a modifier event immediately before the focus,
632 * we should try to keep the same serial. */
633 notify_keyboard_focus_in(&input->base, keys,
634 STATE_UPDATE_AUTOMATIC);
638 input_handle_keyboard_leave(void *data,
639 struct wl_keyboard *keyboard,
641 struct wl_surface *surface)
643 struct wayland_input *input = data;
645 notify_keyboard_focus_out(&input->base);
649 input_handle_key(void *data, struct wl_keyboard *keyboard,
650 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
652 struct wayland_input *input = data;
654 input->key_serial = serial;
655 notify_key(&input->base, time, key,
656 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
657 WL_KEYBOARD_KEY_STATE_RELEASED,
662 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
663 uint32_t serial_in, uint32_t mods_depressed,
664 uint32_t mods_latched, uint32_t mods_locked,
667 struct wayland_input *input = data;
668 struct wayland_compositor *c = input->compositor;
671 /* If we get a key event followed by a modifier event with the
672 * same serial number, then we try to preserve those semantics by
673 * reusing the same serial number on the way out too. */
674 if (serial_in == input->key_serial)
675 serial_out = wl_display_get_serial(c->base.wl_display);
677 serial_out = wl_display_next_serial(c->base.wl_display);
679 xkb_state_update_mask(input->base.xkb_state.state,
680 mods_depressed, mods_latched,
681 mods_locked, 0, 0, group);
682 notify_modifiers(&input->base, serial_out);
685 static const struct wl_keyboard_listener keyboard_listener = {
687 input_handle_keyboard_enter,
688 input_handle_keyboard_leave,
690 input_handle_modifiers,
694 input_handle_capabilities(void *data, struct wl_seat *seat,
695 enum wl_seat_capability caps)
697 struct wayland_input *input = data;
699 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
700 input->pointer = wl_seat_get_pointer(seat);
701 wl_pointer_set_user_data(input->pointer, input);
702 wl_pointer_add_listener(input->pointer, &pointer_listener,
704 weston_seat_init_pointer(&input->base);
705 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
706 wl_pointer_destroy(input->pointer);
707 input->pointer = NULL;
710 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
711 input->keyboard = wl_seat_get_keyboard(seat);
712 wl_keyboard_set_user_data(input->keyboard, input);
713 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
715 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
716 wl_keyboard_destroy(input->keyboard);
717 input->keyboard = NULL;
721 static const struct wl_seat_listener seat_listener = {
722 input_handle_capabilities,
726 display_add_seat(struct wayland_compositor *c, uint32_t id)
728 struct wayland_input *input;
730 input = malloc(sizeof *input);
734 memset(input, 0, sizeof *input);
736 weston_seat_init(&input->base, &c->base);
737 input->compositor = c;
738 input->seat = wl_display_bind(c->parent.wl_display, id,
740 wl_list_insert(c->input_list.prev, &input->link);
742 wl_seat_add_listener(input->seat, &seat_listener, input);
743 wl_seat_set_user_data(input->seat, input);
747 display_handle_global(struct wl_display *display, uint32_t id,
748 const char *interface, uint32_t version, void *data)
750 struct wayland_compositor *c = data;
752 if (strcmp(interface, "wl_compositor") == 0) {
753 c->parent.compositor =
754 wl_display_bind(display, id, &wl_compositor_interface);
755 } else if (strcmp(interface, "wl_output") == 0) {
757 wl_display_bind(display, id, &wl_output_interface);
758 wl_output_add_listener(c->parent.output, &output_listener, c);
759 } else if (strcmp(interface, "wl_shell") == 0) {
761 wl_display_bind(display, id, &wl_shell_interface);
762 } else if (strcmp(interface, "wl_seat") == 0) {
763 display_add_seat(c, id);
768 update_event_mask(uint32_t mask, void *data)
770 struct wayland_compositor *c = data;
772 c->parent.event_mask = mask;
773 if (c->parent.wl_source)
774 wl_event_source_fd_update(c->parent.wl_source, mask);
780 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
782 struct wayland_compositor *c = data;
784 if (mask & WL_EVENT_READABLE)
785 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
786 if (mask & WL_EVENT_WRITABLE)
787 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
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 wl_display_add_global_listener(c->parent.wl_display,
835 display_handle_global, c);
837 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
839 c->base.wl_display = display;
840 if (wayland_compositor_init_egl(c) < 0)
843 c->base.destroy = wayland_destroy;
844 c->base.restore = wayland_restore;
847 if (wayland_compositor_create_output(c, width, height) < 0)
850 if (gles2_renderer_init(&c->base) < 0)
853 loop = wl_display_get_event_loop(c->base.wl_display);
855 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
856 c->parent.wl_source =
857 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
858 wayland_compositor_handle_event, c);
859 if (c->parent.wl_source == NULL)
865 wl_display_disconnect(c->parent.wl_display);
867 weston_compositor_shutdown(&c->base);
873 WL_EXPORT struct weston_compositor *
874 backend_init(struct wl_display *display, int argc, char *argv[],
875 const char *config_file)
877 int width = 1024, height = 640;
878 char *display_name = NULL;
880 const struct weston_option wayland_options[] = {
881 { WESTON_OPTION_INTEGER, "width", 0, &width },
882 { WESTON_OPTION_INTEGER, "height", 0, &height },
883 { WESTON_OPTION_STRING, "display", 0, &display_name },
886 parse_options(wayland_options,
887 ARRAY_LENGTH(wayland_options), argc, argv);
889 return wayland_compositor_create(display, width, height, display_name,
890 argc, argv, config_file);