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;
79 struct wl_surface *surface;
80 struct wl_shell_surface *shell_surface;
81 struct wl_egl_window *egl_window;
83 EGLSurface egl_surface;
84 struct weston_mode mode;
87 struct wayland_input {
88 struct weston_seat base;
89 struct wayland_compositor *compositor;
91 struct wl_pointer *pointer;
92 struct wl_keyboard *keyboard;
93 struct wl_touch *touch;
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);
193 glUniform1f(shader->texwidth_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));
243 c->border.bottom = 50;
245 c->border.right = 25;
247 pixman_image_unref(image);
251 wayland_compositor_init_egl(struct wayland_compositor *c)
255 EGLint config_attribs[] = {
256 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
261 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
264 static const EGLint context_attribs[] = {
265 EGL_CONTEXT_CLIENT_VERSION, 2,
269 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
270 if (c->base.egl_display == NULL) {
271 weston_log("failed to create display\n");
275 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
276 weston_log("failed to initialize display\n");
280 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
281 weston_log("failed to bind EGL_OPENGL_ES_API\n");
284 if (!eglChooseConfig(c->base.egl_display, config_attribs,
285 &c->base.egl_config, 1, &n) || n == 0) {
286 weston_log("failed to choose config: %d\n", n);
290 c->base.egl_context =
291 eglCreateContext(c->base.egl_display, c->base.egl_config,
292 EGL_NO_CONTEXT, context_attribs);
293 if (c->base.egl_context == NULL) {
294 weston_log("failed to create context\n");
298 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
299 if (!c->dummy_pixmap) {
300 weston_log("failure to create dummy_pixmap\n");
304 c->dummy_egl_surface =
305 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
306 c->dummy_pixmap, NULL);
307 if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
308 c->dummy_egl_surface, c->base.egl_context)) {
309 weston_log("failed to make context current\n");
317 frame_done(void *data, struct wl_callback *callback, uint32_t time)
319 struct weston_output *output = data;
321 wl_callback_destroy(callback);
322 weston_output_finish_frame(output, time);
325 static const struct wl_callback_listener frame_listener = {
330 wayland_output_repaint(struct weston_output *output_base,
331 pixman_region32_t *damage)
333 struct wayland_output *output = (struct wayland_output *) output_base;
334 struct wayland_compositor *compositor =
335 (struct wayland_compositor *) output->base.compositor;
336 struct wl_callback *callback;
337 struct weston_surface *surface;
339 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
341 compositor->base.egl_context)) {
342 weston_log("failed to make current\n");
346 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
347 weston_surface_draw(surface, &output->base, damage);
351 wl_signal_emit(&output->base.frame_signal, output);
353 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
354 callback = wl_surface_frame(output->parent.surface);
355 wl_callback_add_listener(callback, &frame_listener, output);
361 wayland_output_destroy(struct weston_output *output_base)
363 struct wayland_output *output = (struct wayland_output *) output_base;
364 struct weston_compositor *ec = output->base.compositor;
366 eglDestroySurface(ec->egl_display, output->egl_surface);
367 wl_egl_window_destroy(output->parent.egl_window);
373 static const struct wl_shell_surface_listener shell_surface_listener;
376 wayland_compositor_create_output(struct wayland_compositor *c,
377 int width, int height)
379 struct wayland_output *output;
381 output = malloc(sizeof *output);
384 memset(output, 0, sizeof *output);
387 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
388 output->mode.width = width;
389 output->mode.height = height;
390 output->mode.refresh = 60;
391 wl_list_init(&output->base.mode_list);
392 wl_list_insert(&output->base.mode_list, &output->mode.link);
394 output->base.current = &output->mode;
395 weston_output_init(&output->base, &c->base, 0, 0, width, height);
397 output->base.border.top = c->border.top;
398 output->base.border.bottom = c->border.bottom;
399 output->base.border.left = c->border.left;
400 output->base.border.right = c->border.right;
402 weston_output_move(&output->base, 0, 0);
404 output->parent.surface =
405 wl_compositor_create_surface(c->parent.compositor);
406 wl_surface_set_user_data(output->parent.surface, output);
408 output->parent.egl_window =
409 wl_egl_window_create(output->parent.surface,
410 width + c->border.left + c->border.right,
411 height + c->border.top + c->border.bottom);
412 if (!output->parent.egl_window) {
413 weston_log("failure to create wl_egl_window\n");
417 output->egl_surface =
418 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
419 output->parent.egl_window, NULL);
420 if (!output->egl_surface) {
421 weston_log("failed to create window surface\n");
425 if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
426 output->egl_surface, c->base.egl_context)) {
427 weston_log("failed to make surface current\n");
428 goto cleanup_surface;
432 output->parent.shell_surface =
433 wl_shell_get_shell_surface(c->parent.shell,
434 output->parent.surface);
435 wl_shell_surface_add_listener(output->parent.shell_surface,
436 &shell_surface_listener, output);
437 wl_shell_surface_set_toplevel(output->parent.shell_surface);
439 output->base.origin = output->base.current;
440 output->base.repaint = wayland_output_repaint;
441 output->base.destroy = wayland_output_destroy;
442 output->base.assign_planes = NULL;
443 output->base.set_backlight = NULL;
444 output->base.set_dpms = NULL;
445 output->base.switch_mode = NULL;
447 wl_list_insert(c->base.output_list.prev, &output->base.link);
452 eglDestroySurface(c->base.egl_display, output->egl_surface);
454 wl_egl_window_destroy(output->parent.egl_window);
456 /* FIXME: cleanup weston_output */
463 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
466 wl_shell_surface_pong(shell_surface, serial);
470 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
471 uint32_t edges, int32_t width, int32_t height)
473 /* FIXME: implement resizing */
477 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
481 static const struct wl_shell_surface_listener shell_surface_listener = {
483 shell_surface_configure,
484 shell_surface_popup_done
487 /* Events received from the wayland-server this compositor is client of: */
489 /* parent output interface */
491 display_handle_geometry(void *data,
492 struct wl_output *wl_output,
502 struct wayland_compositor *c = data;
504 c->parent.screen_allocation.x = x;
505 c->parent.screen_allocation.y = y;
509 display_handle_mode(void *data,
510 struct wl_output *wl_output,
516 struct wayland_compositor *c = data;
518 c->parent.screen_allocation.width = width;
519 c->parent.screen_allocation.height = height;
522 static const struct wl_output_listener output_listener = {
523 display_handle_geometry,
527 /* parent input interface */
529 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
530 uint32_t serial, struct wl_surface *surface,
531 wl_fixed_t x, wl_fixed_t y)
533 struct wayland_input *input = data;
534 struct wayland_output *output;
536 /* XXX: If we get a modifier event immediately before the focus,
537 * we should try to keep the same serial. */
538 output = wl_surface_get_user_data(surface);
539 notify_pointer_focus(&input->base, &output->base, x, y);
540 wl_pointer_set_cursor(input->pointer, serial, NULL, 0, 0);
544 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
545 uint32_t serial, struct wl_surface *surface)
547 struct wayland_input *input = data;
549 notify_pointer_focus(&input->base, NULL, 0, 0);
553 input_handle_motion(void *data, struct wl_pointer *pointer,
554 uint32_t time, wl_fixed_t x, wl_fixed_t y)
556 struct wayland_input *input = data;
557 struct wayland_compositor *c = input->compositor;
559 notify_motion(&input->base, time,
560 x - wl_fixed_from_int(c->border.left),
561 y - wl_fixed_from_int(c->border.top));
565 input_handle_button(void *data, struct wl_pointer *pointer,
566 uint32_t serial, uint32_t time, uint32_t button,
569 struct wayland_input *input = data;
570 enum wl_pointer_button_state state = state_w;
572 notify_button(&input->base, time, button, state);
576 input_handle_axis(void *data, struct wl_pointer *pointer,
577 uint32_t time, uint32_t axis, wl_fixed_t value)
579 struct wayland_input *input = data;
581 notify_axis(&input->base, time, axis, value);
584 static const struct wl_pointer_listener pointer_listener = {
585 input_handle_pointer_enter,
586 input_handle_pointer_leave,
593 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
594 int fd, uint32_t size)
596 struct wayland_input *input = data;
597 struct xkb_keymap *keymap;
605 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
610 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
611 if (map_str == MAP_FAILED) {
616 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
618 XKB_KEYMAP_FORMAT_TEXT_V1,
620 munmap(map_str, size);
624 weston_log("failed to compile keymap\n");
628 weston_seat_init_keyboard(&input->base, keymap);
629 xkb_map_unref(keymap);
633 input_handle_keyboard_enter(void *data,
634 struct wl_keyboard *keyboard,
636 struct wl_surface *surface,
637 struct wl_array *keys)
639 struct wayland_input *input = data;
641 /* XXX: If we get a modifier event immediately before the focus,
642 * we should try to keep the same serial. */
643 notify_keyboard_focus_in(&input->base, keys,
644 STATE_UPDATE_AUTOMATIC);
648 input_handle_keyboard_leave(void *data,
649 struct wl_keyboard *keyboard,
651 struct wl_surface *surface)
653 struct wayland_input *input = data;
655 notify_keyboard_focus_out(&input->base);
659 input_handle_key(void *data, struct wl_keyboard *keyboard,
660 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
662 struct wayland_input *input = data;
664 input->key_serial = serial;
665 notify_key(&input->base, time, key,
666 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
667 WL_KEYBOARD_KEY_STATE_RELEASED,
672 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
673 uint32_t serial_in, uint32_t mods_depressed,
674 uint32_t mods_latched, uint32_t mods_locked,
677 struct wayland_input *input = data;
678 struct wayland_compositor *c = input->compositor;
681 /* If we get a key event followed by a modifier event with the
682 * same serial number, then we try to preserve those semantics by
683 * reusing the same serial number on the way out too. */
684 if (serial_in == input->key_serial)
685 serial_out = wl_display_get_serial(c->base.wl_display);
687 serial_out = wl_display_next_serial(c->base.wl_display);
689 xkb_state_update_mask(input->base.xkb_state.state,
690 mods_depressed, mods_latched,
691 mods_locked, 0, 0, group);
692 notify_modifiers(&input->base, serial_out);
695 static const struct wl_keyboard_listener keyboard_listener = {
697 input_handle_keyboard_enter,
698 input_handle_keyboard_leave,
700 input_handle_modifiers,
704 input_handle_capabilities(void *data, struct wl_seat *seat,
705 enum wl_seat_capability caps)
707 struct wayland_input *input = data;
709 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
710 input->pointer = wl_seat_get_pointer(seat);
711 wl_pointer_set_user_data(input->pointer, input);
712 wl_pointer_add_listener(input->pointer, &pointer_listener,
714 weston_seat_init_pointer(&input->base);
715 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
716 wl_pointer_destroy(input->pointer);
717 input->pointer = NULL;
720 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
721 input->keyboard = wl_seat_get_keyboard(seat);
722 wl_keyboard_set_user_data(input->keyboard, input);
723 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
725 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
726 wl_keyboard_destroy(input->keyboard);
727 input->keyboard = NULL;
731 static const struct wl_seat_listener seat_listener = {
732 input_handle_capabilities,
736 display_add_seat(struct wayland_compositor *c, uint32_t id)
738 struct wayland_input *input;
740 input = malloc(sizeof *input);
744 memset(input, 0, sizeof *input);
746 weston_seat_init(&input->base, &c->base);
747 input->compositor = c;
748 input->seat = wl_display_bind(c->parent.wl_display, id,
750 wl_list_insert(c->input_list.prev, &input->link);
752 wl_seat_add_listener(input->seat, &seat_listener, input);
753 wl_seat_set_user_data(input->seat, input);
757 display_handle_global(struct wl_display *display, uint32_t id,
758 const char *interface, uint32_t version, void *data)
760 struct wayland_compositor *c = data;
762 if (strcmp(interface, "wl_compositor") == 0) {
763 c->parent.compositor =
764 wl_display_bind(display, id, &wl_compositor_interface);
765 } else if (strcmp(interface, "wl_output") == 0) {
767 wl_display_bind(display, id, &wl_output_interface);
768 wl_output_add_listener(c->parent.output, &output_listener, c);
769 } else if (strcmp(interface, "wl_shell") == 0) {
771 wl_display_bind(display, id, &wl_shell_interface);
772 } else if (strcmp(interface, "wl_seat") == 0) {
773 display_add_seat(c, id);
778 update_event_mask(uint32_t mask, void *data)
780 struct wayland_compositor *c = data;
782 c->parent.event_mask = mask;
783 if (c->parent.wl_source)
784 wl_event_source_fd_update(c->parent.wl_source, mask);
790 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
792 struct wayland_compositor *c = data;
794 if (mask & WL_EVENT_READABLE)
795 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
796 if (mask & WL_EVENT_WRITABLE)
797 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
803 wayland_restore(struct weston_compositor *ec)
808 wayland_destroy(struct weston_compositor *ec)
810 weston_compositor_shutdown(ec);
815 static struct weston_compositor *
816 wayland_compositor_create(struct wl_display *display,
817 int width, int height, const char *display_name,
818 int argc, char *argv[], const char *config_file)
820 struct wayland_compositor *c;
821 struct wl_event_loop *loop;
824 c = malloc(sizeof *c);
828 memset(c, 0, sizeof *c);
830 if (weston_compositor_init(&c->base, display, argc, argv,
834 c->parent.wl_display = wl_display_connect(display_name);
836 if (c->parent.wl_display == NULL) {
837 weston_log("failed to create display: %m\n");
841 wl_list_init(&c->input_list);
842 wl_display_add_global_listener(c->parent.wl_display,
843 display_handle_global, c);
845 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
847 c->base.wl_display = display;
848 if (wayland_compositor_init_egl(c) < 0)
851 c->base.destroy = wayland_destroy;
852 c->base.restore = wayland_restore;
854 if (weston_compositor_init_gl(&c->base) < 0)
858 if (wayland_compositor_create_output(c, width, height) < 0)
861 loop = wl_display_get_event_loop(c->base.wl_display);
863 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
864 c->parent.wl_source =
865 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
866 wayland_compositor_handle_event, c);
867 if (c->parent.wl_source == NULL)
873 wl_display_disconnect(c->parent.wl_display);
875 weston_compositor_shutdown(&c->base);
881 WL_EXPORT struct weston_compositor *
882 backend_init(struct wl_display *display, int argc, char *argv[],
883 const char *config_file)
885 int width = 1024, height = 640;
886 char *display_name = NULL;
888 const struct weston_option wayland_options[] = {
889 { WESTON_OPTION_INTEGER, "width", 0, &width },
890 { WESTON_OPTION_INTEGER, "height", 0, &height },
891 { WESTON_OPTION_STRING, "display", 0, &display_name },
894 parse_options(wayland_options,
895 ARRAY_LENGTH(wayland_options), argc, argv);
897 return wayland_compositor_create(display, width, height, display_name,
898 argc, argv, config_file);