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;
49 struct wl_egl_pixmap *dummy_pixmap;
50 EGLSurface dummy_egl_surface;
53 struct wl_display *wl_display;
54 struct wl_compositor *compositor;
55 struct wl_shell *shell;
56 struct wl_output *output;
59 int32_t x, y, width, height;
62 struct wl_event_source *wl_source;
67 int32_t top, bottom, left, right;
69 int32_t width, height;
72 struct wl_list input_list;
75 struct wayland_output {
76 struct weston_output base;
77 struct wl_listener frame_listener;
79 struct wl_surface *surface;
80 struct wl_shell_surface *shell_surface;
81 struct wl_egl_window *egl_window;
83 struct weston_mode mode;
86 struct wayland_input {
87 struct weston_seat base;
88 struct wayland_compositor *compositor;
90 struct wl_pointer *pointer;
91 struct wl_keyboard *keyboard;
92 struct wl_touch *touch;
95 uint32_t enter_serial;
97 struct wayland_output *output;
102 texture_border(struct wayland_output *output)
104 struct wayland_compositor *c =
105 (struct wayland_compositor *) output->base.compositor;
109 GLfloat x[4], y[4], u[4], v[4];
111 x[0] = -c->border.left;
113 x[2] = output->base.current->width;
114 x[3] = output->base.current->width + c->border.right;
116 y[0] = -c->border.top;
118 y[2] = output->base.current->height;
119 y[3] = output->base.current->height + c->border.bottom;
122 u[1] = (GLfloat) c->border.left / c->border.width;
123 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
127 v[1] = (GLfloat) c->border.top / c->border.height;
128 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
132 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
133 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
136 for (i = 0; i < 3; i++)
137 for (j = 0; j < 3; j++) {
139 if (i == 1 && j == 1)
178 draw_border(struct wayland_output *output)
180 struct wayland_compositor *c =
181 (struct wayland_compositor *) output->base.compositor;
182 struct weston_shader *shader = &c->base.texture_shader_rgba;
187 glUseProgram(shader->program);
188 c->base.current_shader = shader;
190 glUniformMatrix4fv(shader->proj_uniform,
191 1, GL_FALSE, output->base.matrix.d);
193 glUniform1i(shader->tex_uniforms[0], 0);
194 glUniform1f(shader->alpha_uniform, 1);
196 n = texture_border(output);
198 glBindTexture(GL_TEXTURE_2D, c->border.texture);
200 v = c->base.vertices.data;
201 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
202 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
203 glEnableVertexAttribArray(0);
204 glEnableVertexAttribArray(1);
206 glDrawElements(GL_TRIANGLES, n * 6,
207 GL_UNSIGNED_INT, c->base.indices.data);
209 glDisableVertexAttribArray(1);
210 glDisableVertexAttribArray(0);
212 c->base.vertices.size = 0;
213 c->base.indices.size = 0;
217 create_border(struct wayland_compositor *c)
219 pixman_image_t *image;
221 image = load_image(DATADIR "/weston/border.png");
223 weston_log("could'nt load border image\n");
227 c->border.width = pixman_image_get_width(image);
228 c->border.height = pixman_image_get_height(image);
230 glGenTextures(1, &c->border.texture);
231 glBindTexture(GL_TEXTURE_2D, c->border.texture);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
237 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
240 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
241 pixman_image_get_data(image));
244 c->border.bottom = 50;
246 c->border.right = 25;
248 pixman_image_unref(image);
252 wayland_compositor_init_egl(struct wayland_compositor *c)
256 EGLint config_attribs[] = {
257 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
262 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
265 static const EGLint context_attribs[] = {
266 EGL_CONTEXT_CLIENT_VERSION, 2,
270 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
271 if (c->base.egl_display == NULL) {
272 weston_log("failed to create display\n");
276 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
277 weston_log("failed to initialize display\n");
281 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
282 weston_log("failed to bind EGL_OPENGL_ES_API\n");
285 if (!eglChooseConfig(c->base.egl_display, config_attribs,
286 &c->base.egl_config, 1, &n) || n == 0) {
287 weston_log("failed to choose config: %d\n", n);
291 c->base.egl_context =
292 eglCreateContext(c->base.egl_display, c->base.egl_config,
293 EGL_NO_CONTEXT, context_attribs);
294 if (c->base.egl_context == NULL) {
295 weston_log("failed to create context\n");
299 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
300 if (!c->dummy_pixmap) {
301 weston_log("failure to create dummy_pixmap\n");
305 c->dummy_egl_surface =
306 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
307 c->dummy_pixmap, NULL);
308 if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
309 c->dummy_egl_surface, c->base.egl_context)) {
310 weston_log("failed to make context current\n");
318 frame_done(void *data, struct wl_callback *callback, uint32_t time)
320 struct weston_output *output = data;
322 wl_callback_destroy(callback);
323 weston_output_finish_frame(output, time);
326 static const struct wl_callback_listener frame_listener = {
331 wayland_output_frame_notify(struct wl_listener *listener, void *data)
333 struct wayland_output *output =
334 container_of(listener,
335 struct wayland_output, frame_listener);
341 wayland_output_repaint(struct weston_output *output_base,
342 pixman_region32_t *damage)
344 struct wayland_output *output = (struct wayland_output *) output_base;
345 struct wl_callback *callback;
347 gles2_renderer_repaint_output(output_base, damage);
349 callback = wl_surface_frame(output->parent.surface);
350 wl_callback_add_listener(callback, &frame_listener, output);
356 wayland_output_destroy(struct weston_output *output_base)
358 struct wayland_output *output = (struct wayland_output *) output_base;
359 struct weston_compositor *ec = output->base.compositor;
361 eglDestroySurface(ec->egl_display, output->base.egl_surface);
362 wl_egl_window_destroy(output->parent.egl_window);
368 static const struct wl_shell_surface_listener shell_surface_listener;
371 wayland_compositor_create_output(struct wayland_compositor *c,
372 int width, int height)
374 struct wayland_output *output;
376 output = malloc(sizeof *output);
379 memset(output, 0, sizeof *output);
382 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
383 output->mode.width = width;
384 output->mode.height = height;
385 output->mode.refresh = 60;
386 wl_list_init(&output->base.mode_list);
387 wl_list_insert(&output->base.mode_list, &output->mode.link);
389 output->base.current = &output->mode;
390 weston_output_init(&output->base, &c->base, 0, 0, width, height,
391 WL_OUTPUT_TRANSFORM_NORMAL);
393 output->base.border.top = c->border.top;
394 output->base.border.bottom = c->border.bottom;
395 output->base.border.left = c->border.left;
396 output->base.border.right = c->border.right;
397 output->base.make = "waywayland";
398 output->base.model = "none";
400 weston_output_move(&output->base, 0, 0);
402 output->parent.surface =
403 wl_compositor_create_surface(c->parent.compositor);
404 wl_surface_set_user_data(output->parent.surface, output);
406 output->parent.egl_window =
407 wl_egl_window_create(output->parent.surface,
408 width + c->border.left + c->border.right,
409 height + c->border.top + c->border.bottom);
410 if (!output->parent.egl_window) {
411 weston_log("failure to create wl_egl_window\n");
415 output->base.egl_surface =
416 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
417 output->parent.egl_window, NULL);
418 if (!output->base.egl_surface) {
419 weston_log("failed to create window surface\n");
423 if (!eglMakeCurrent(c->base.egl_display, output->base.egl_surface,
424 output->base.egl_surface, c->base.egl_context)) {
425 weston_log("failed to make surface current\n");
426 goto cleanup_surface;
430 output->parent.shell_surface =
431 wl_shell_get_shell_surface(c->parent.shell,
432 output->parent.surface);
433 wl_shell_surface_add_listener(output->parent.shell_surface,
434 &shell_surface_listener, output);
435 wl_shell_surface_set_toplevel(output->parent.shell_surface);
437 output->base.origin = output->base.current;
438 output->base.repaint = wayland_output_repaint;
439 output->base.destroy = wayland_output_destroy;
440 output->base.assign_planes = NULL;
441 output->base.set_backlight = NULL;
442 output->base.set_dpms = NULL;
443 output->base.switch_mode = NULL;
445 wl_list_insert(c->base.output_list.prev, &output->base.link);
447 output->frame_listener.notify = wayland_output_frame_notify;
448 wl_signal_add(&output->base.frame_signal, &output->frame_listener);
453 eglDestroySurface(c->base.egl_display, output->base.egl_surface);
455 wl_egl_window_destroy(output->parent.egl_window);
457 /* FIXME: cleanup weston_output */
464 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
467 wl_shell_surface_pong(shell_surface, serial);
471 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
472 uint32_t edges, int32_t width, int32_t height)
474 /* FIXME: implement resizing */
478 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
482 static const struct wl_shell_surface_listener shell_surface_listener = {
484 shell_surface_configure,
485 shell_surface_popup_done
488 /* Events received from the wayland-server this compositor is client of: */
490 /* parent output interface */
492 display_handle_geometry(void *data,
493 struct wl_output *wl_output,
503 struct wayland_compositor *c = data;
505 c->parent.screen_allocation.x = x;
506 c->parent.screen_allocation.y = y;
510 display_handle_mode(void *data,
511 struct wl_output *wl_output,
517 struct wayland_compositor *c = data;
519 c->parent.screen_allocation.width = width;
520 c->parent.screen_allocation.height = height;
523 static const struct wl_output_listener output_listener = {
524 display_handle_geometry,
529 check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
531 struct wayland_compositor *c = input->compositor;
532 int width, height, inside;
534 width = input->output->mode.width;
535 height = input->output->mode.height;
537 inside = c->border.left <= wl_fixed_to_int(x) &&
538 wl_fixed_to_int(x) < width + c->border.left &&
539 c->border.top <= wl_fixed_to_int(y) &&
540 wl_fixed_to_int(y) < height + c->border.top;
542 if (!input->focus && inside) {
543 notify_pointer_focus(&input->base, &input->output->base,
544 x - wl_fixed_from_int(c->border.left),
545 y = wl_fixed_from_int(c->border.top));
546 wl_pointer_set_cursor(input->pointer,
547 input->enter_serial, NULL, 0, 0);
548 } else if (input->focus && !inside) {
549 notify_pointer_focus(&input->base, NULL, 0, 0);
550 /* FIXME: Should set default cursor here. */
553 input->focus = inside;
556 /* parent input interface */
558 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
559 uint32_t serial, struct wl_surface *surface,
560 wl_fixed_t x, wl_fixed_t y)
562 struct wayland_input *input = data;
564 /* XXX: If we get a modifier event immediately before the focus,
565 * we should try to keep the same serial. */
566 input->enter_serial = serial;
567 input->output = wl_surface_get_user_data(surface);
568 check_focus(input, x, y);
572 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
573 uint32_t serial, struct wl_surface *surface)
575 struct wayland_input *input = data;
577 notify_pointer_focus(&input->base, NULL, 0, 0);
578 input->output = NULL;
583 input_handle_motion(void *data, struct wl_pointer *pointer,
584 uint32_t time, wl_fixed_t x, wl_fixed_t y)
586 struct wayland_input *input = data;
587 struct wayland_compositor *c = input->compositor;
589 check_focus(input, x, y);
591 notify_motion(&input->base, time,
592 x - wl_fixed_from_int(c->border.left),
593 y - wl_fixed_from_int(c->border.top));
597 input_handle_button(void *data, struct wl_pointer *pointer,
598 uint32_t serial, uint32_t time, uint32_t button,
601 struct wayland_input *input = data;
602 enum wl_pointer_button_state state = state_w;
604 notify_button(&input->base, time, button, state);
608 input_handle_axis(void *data, struct wl_pointer *pointer,
609 uint32_t time, uint32_t axis, wl_fixed_t value)
611 struct wayland_input *input = data;
613 notify_axis(&input->base, time, axis, value);
616 static const struct wl_pointer_listener pointer_listener = {
617 input_handle_pointer_enter,
618 input_handle_pointer_leave,
625 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
626 int fd, uint32_t size)
628 struct wayland_input *input = data;
629 struct xkb_keymap *keymap;
637 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
642 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
643 if (map_str == MAP_FAILED) {
648 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
650 XKB_KEYMAP_FORMAT_TEXT_V1,
652 munmap(map_str, size);
656 weston_log("failed to compile keymap\n");
660 weston_seat_init_keyboard(&input->base, keymap);
661 xkb_map_unref(keymap);
665 input_handle_keyboard_enter(void *data,
666 struct wl_keyboard *keyboard,
668 struct wl_surface *surface,
669 struct wl_array *keys)
671 struct wayland_input *input = data;
673 /* XXX: If we get a modifier event immediately before the focus,
674 * we should try to keep the same serial. */
675 notify_keyboard_focus_in(&input->base, keys,
676 STATE_UPDATE_AUTOMATIC);
680 input_handle_keyboard_leave(void *data,
681 struct wl_keyboard *keyboard,
683 struct wl_surface *surface)
685 struct wayland_input *input = data;
687 notify_keyboard_focus_out(&input->base);
691 input_handle_key(void *data, struct wl_keyboard *keyboard,
692 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
694 struct wayland_input *input = data;
696 input->key_serial = serial;
697 notify_key(&input->base, time, key,
698 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
699 WL_KEYBOARD_KEY_STATE_RELEASED,
704 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
705 uint32_t serial_in, uint32_t mods_depressed,
706 uint32_t mods_latched, uint32_t mods_locked,
709 struct wayland_input *input = data;
710 struct wayland_compositor *c = input->compositor;
713 /* If we get a key event followed by a modifier event with the
714 * same serial number, then we try to preserve those semantics by
715 * reusing the same serial number on the way out too. */
716 if (serial_in == input->key_serial)
717 serial_out = wl_display_get_serial(c->base.wl_display);
719 serial_out = wl_display_next_serial(c->base.wl_display);
721 xkb_state_update_mask(input->base.xkb_state.state,
722 mods_depressed, mods_latched,
723 mods_locked, 0, 0, group);
724 notify_modifiers(&input->base, serial_out);
727 static const struct wl_keyboard_listener keyboard_listener = {
729 input_handle_keyboard_enter,
730 input_handle_keyboard_leave,
732 input_handle_modifiers,
736 input_handle_capabilities(void *data, struct wl_seat *seat,
737 enum wl_seat_capability caps)
739 struct wayland_input *input = data;
741 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
742 input->pointer = wl_seat_get_pointer(seat);
743 wl_pointer_set_user_data(input->pointer, input);
744 wl_pointer_add_listener(input->pointer, &pointer_listener,
746 weston_seat_init_pointer(&input->base);
747 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
748 wl_pointer_destroy(input->pointer);
749 input->pointer = NULL;
752 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
753 input->keyboard = wl_seat_get_keyboard(seat);
754 wl_keyboard_set_user_data(input->keyboard, input);
755 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
757 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
758 wl_keyboard_destroy(input->keyboard);
759 input->keyboard = NULL;
763 static const struct wl_seat_listener seat_listener = {
764 input_handle_capabilities,
768 display_add_seat(struct wayland_compositor *c, uint32_t id)
770 struct wayland_input *input;
772 input = malloc(sizeof *input);
776 memset(input, 0, sizeof *input);
778 weston_seat_init(&input->base, &c->base);
779 input->compositor = c;
780 input->seat = wl_display_bind(c->parent.wl_display, id,
782 wl_list_insert(c->input_list.prev, &input->link);
784 wl_seat_add_listener(input->seat, &seat_listener, input);
785 wl_seat_set_user_data(input->seat, input);
789 display_handle_global(struct wl_display *display, uint32_t id,
790 const char *interface, uint32_t version, void *data)
792 struct wayland_compositor *c = data;
794 if (strcmp(interface, "wl_compositor") == 0) {
795 c->parent.compositor =
796 wl_display_bind(display, id, &wl_compositor_interface);
797 } else if (strcmp(interface, "wl_output") == 0) {
799 wl_display_bind(display, id, &wl_output_interface);
800 wl_output_add_listener(c->parent.output, &output_listener, c);
801 } else if (strcmp(interface, "wl_shell") == 0) {
803 wl_display_bind(display, id, &wl_shell_interface);
804 } else if (strcmp(interface, "wl_seat") == 0) {
805 display_add_seat(c, id);
810 update_event_mask(uint32_t mask, void *data)
812 struct wayland_compositor *c = data;
814 c->parent.event_mask = mask;
815 if (c->parent.wl_source)
816 wl_event_source_fd_update(c->parent.wl_source, mask);
822 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
824 struct wayland_compositor *c = data;
826 if (mask & WL_EVENT_READABLE)
827 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
828 if (mask & WL_EVENT_WRITABLE)
829 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
835 wayland_restore(struct weston_compositor *ec)
840 wayland_destroy(struct weston_compositor *ec)
842 weston_compositor_shutdown(ec);
847 static struct weston_compositor *
848 wayland_compositor_create(struct wl_display *display,
849 int width, int height, const char *display_name,
850 int argc, char *argv[], const char *config_file)
852 struct wayland_compositor *c;
853 struct wl_event_loop *loop;
856 c = malloc(sizeof *c);
860 memset(c, 0, sizeof *c);
862 if (weston_compositor_init(&c->base, display, argc, argv,
866 c->parent.wl_display = wl_display_connect(display_name);
868 if (c->parent.wl_display == NULL) {
869 weston_log("failed to create display: %m\n");
873 wl_list_init(&c->input_list);
874 wl_display_add_global_listener(c->parent.wl_display,
875 display_handle_global, c);
877 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
879 c->base.wl_display = display;
880 if (wayland_compositor_init_egl(c) < 0)
883 c->base.destroy = wayland_destroy;
884 c->base.restore = wayland_restore;
886 if (weston_compositor_init_gl(&c->base) < 0)
890 if (wayland_compositor_create_output(c, width, height) < 0)
893 loop = wl_display_get_event_loop(c->base.wl_display);
895 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
896 c->parent.wl_source =
897 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
898 wayland_compositor_handle_event, c);
899 if (c->parent.wl_source == NULL)
905 wl_display_disconnect(c->parent.wl_display);
907 weston_compositor_shutdown(&c->base);
913 WL_EXPORT struct weston_compositor *
914 backend_init(struct wl_display *display, int argc, char *argv[],
915 const char *config_file)
917 int width = 1024, height = 640;
918 char *display_name = NULL;
920 const struct weston_option wayland_options[] = {
921 { WESTON_OPTION_INTEGER, "width", 0, &width },
922 { WESTON_OPTION_INTEGER, "height", 0, &height },
923 { WESTON_OPTION_STRING, "display", 0, &display_name },
926 parse_options(wayland_options,
927 ARRAY_LENGTH(wayland_options), argc, argv);
929 return wayland_compositor_create(display, width, height, display_name,
930 argc, argv, config_file);