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"
47 struct wayland_compositor {
48 struct weston_compositor base;
50 struct wl_egl_pixmap *dummy_pixmap;
51 EGLSurface dummy_egl_surface;
54 struct wl_display *wl_display;
55 struct wl_compositor *compositor;
56 struct wl_shell *shell;
57 struct wl_output *output;
60 int32_t x, y, width, height;
63 struct wl_event_source *wl_source;
68 int32_t top, bottom, left, right;
70 int32_t width, height;
73 struct wl_list input_list;
76 struct wayland_output {
77 struct weston_output base;
80 struct wl_surface *surface;
81 struct wl_shell_surface *shell_surface;
82 struct wl_egl_window *egl_window;
84 EGLSurface egl_surface;
85 struct weston_mode mode;
88 struct wayland_input {
89 struct wayland_compositor *compositor;
91 struct wl_pointer *pointer;
92 struct wl_keyboard *keyboard;
93 struct wl_touch *touch;
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;
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_uniform, 0);
191 glUniform1f(shader->alpha_uniform, 1);
192 glUniform1f(shader->texwidth_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));
242 c->border.bottom = 50;
244 c->border.right = 25;
246 pixman_image_unref(image);
250 wayland_compositor_init_egl(struct wayland_compositor *c)
254 EGLint config_attribs[] = {
255 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
260 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
263 static const EGLint context_attribs[] = {
264 EGL_CONTEXT_CLIENT_VERSION, 2,
268 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
269 if (c->base.egl_display == NULL) {
270 weston_log("failed to create display\n");
274 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
275 weston_log("failed to initialize display\n");
279 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
280 weston_log("failed to bind EGL_OPENGL_ES_API\n");
283 if (!eglChooseConfig(c->base.egl_display, config_attribs,
284 &c->base.egl_config, 1, &n) || n == 0) {
285 weston_log("failed to choose config: %d\n", n);
289 c->base.egl_context =
290 eglCreateContext(c->base.egl_display, c->base.egl_config,
291 EGL_NO_CONTEXT, context_attribs);
292 if (c->base.egl_context == NULL) {
293 weston_log("failed to create context\n");
297 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
298 if (!c->dummy_pixmap) {
299 weston_log("failure to create dummy_pixmap\n");
303 c->dummy_egl_surface =
304 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
305 c->dummy_pixmap, NULL);
306 if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
307 c->dummy_egl_surface, c->base.egl_context)) {
308 weston_log("failed to make context current\n");
316 frame_done(void *data, struct wl_callback *callback, uint32_t time)
318 struct weston_output *output = data;
320 wl_callback_destroy(callback);
321 weston_output_finish_frame(output, time);
324 static const struct wl_callback_listener frame_listener = {
329 wayland_output_repaint(struct weston_output *output_base,
330 pixman_region32_t *damage)
332 struct wayland_output *output = (struct wayland_output *) output_base;
333 struct wayland_compositor *compositor =
334 (struct wayland_compositor *) output->base.compositor;
335 struct wl_callback *callback;
336 struct weston_surface *surface;
338 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
340 compositor->base.egl_context)) {
341 weston_log("failed to make current\n");
345 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
346 weston_surface_draw(surface, &output->base, damage);
350 wl_signal_emit(&output->base.frame_signal, output);
352 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
353 callback = wl_surface_frame(output->parent.surface);
354 wl_callback_add_listener(callback, &frame_listener, output);
360 wayland_output_destroy(struct weston_output *output_base)
362 struct wayland_output *output = (struct wayland_output *) output_base;
363 struct weston_compositor *ec = output->base.compositor;
365 eglDestroySurface(ec->egl_display, output->egl_surface);
366 wl_egl_window_destroy(output->parent.egl_window);
372 static const struct wl_shell_surface_listener shell_surface_listener;
375 wayland_compositor_create_output(struct wayland_compositor *c,
376 int width, int height)
378 struct wayland_output *output;
380 output = malloc(sizeof *output);
383 memset(output, 0, sizeof *output);
386 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
387 output->mode.width = width;
388 output->mode.height = height;
389 output->mode.refresh = 60;
390 wl_list_init(&output->base.mode_list);
391 wl_list_insert(&output->base.mode_list, &output->mode.link);
393 output->base.current = &output->mode;
394 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,
501 struct wayland_compositor *c = data;
503 c->parent.screen_allocation.x = x;
504 c->parent.screen_allocation.y = y;
508 display_handle_mode(void *data,
509 struct wl_output *wl_output,
515 struct wayland_compositor *c = data;
517 c->parent.screen_allocation.width = width;
518 c->parent.screen_allocation.height = height;
521 static const struct wl_output_listener output_listener = {
522 display_handle_geometry,
526 /* parent input interface */
528 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
529 uint32_t serial, struct wl_surface *surface,
530 wl_fixed_t x, wl_fixed_t y)
532 struct wayland_input *input = data;
533 struct wayland_output *output;
534 struct wayland_compositor *c = input->compositor;
536 output = wl_surface_get_user_data(surface);
537 notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
538 wl_pointer_set_cursor(input->pointer, serial, NULL, 0, 0);
542 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
543 uint32_t serial, struct wl_surface *surface)
545 struct wayland_input *input = data;
546 struct wayland_compositor *c = input->compositor;
548 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
552 input_handle_motion(void *data, struct wl_pointer *pointer,
553 uint32_t time, wl_fixed_t x, wl_fixed_t y)
555 struct wayland_input *input = data;
556 struct wayland_compositor *c = input->compositor;
558 notify_motion(&c->base.seat->seat, time,
559 x - wl_fixed_from_int(c->border.left),
560 y - wl_fixed_from_int(c->border.top));
564 input_handle_button(void *data, struct wl_pointer *pointer,
565 uint32_t serial, uint32_t time, uint32_t button,
568 struct wayland_input *input = data;
569 struct wayland_compositor *c = input->compositor;
570 enum wl_pointer_button_state state = state_w;
572 notify_button(&c->base.seat->seat, 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;
580 struct wayland_compositor *c = input->compositor;
582 notify_axis(&c->base.seat->seat, time, axis, value);
585 static const struct wl_pointer_listener pointer_listener = {
586 input_handle_pointer_enter,
587 input_handle_pointer_leave,
594 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
595 int fd, uint32_t size)
597 struct wayland_input *input = data;
598 struct xkb_keymap *keymap;
606 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
611 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
612 if (map_str == MAP_FAILED) {
617 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
619 XKB_KEYMAP_FORMAT_TEXT_V1,
621 munmap(map_str, size);
625 weston_log("failed to compile keymap\n");
629 weston_seat_init_keyboard(input->compositor->base.seat, keymap);
630 xkb_map_unref(keymap);
634 input_handle_keyboard_enter(void *data,
635 struct wl_keyboard *keyboard,
637 struct wl_surface *surface,
638 struct wl_array *keys)
640 struct wayland_input *input = data;
641 struct wayland_compositor *c = input->compositor;
643 /* XXX: Need to wait for modifier event and send with state then. */
644 notify_keyboard_focus_in(&c->base.seat->seat, keys,
645 STATE_UPDATE_AUTOMATIC);
649 input_handle_keyboard_leave(void *data,
650 struct wl_keyboard *keyboard,
652 struct wl_surface *surface)
654 struct wayland_input *input = data;
655 struct wayland_compositor *c = input->compositor;
657 notify_keyboard_focus_out(&c->base.seat->seat);
661 input_handle_key(void *data, struct wl_keyboard *keyboard,
662 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
664 struct wayland_input *input = data;
665 struct wayland_compositor *c = input->compositor;
667 notify_key(&c->base.seat->seat, time, key,
668 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
669 WL_KEYBOARD_KEY_STATE_RELEASED,
670 STATE_UPDATE_AUTOMATIC);
674 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
675 uint32_t serial, uint32_t mods_depressed,
676 uint32_t mods_latched, uint32_t mods_locked,
679 /* XXX notify_modifiers(); */
682 static const struct wl_keyboard_listener keyboard_listener = {
684 input_handle_keyboard_enter,
685 input_handle_keyboard_leave,
687 input_handle_modifiers,
691 input_handle_capabilities(void *data, struct wl_seat *seat,
692 enum wl_seat_capability caps)
694 struct wayland_input *input = data;
696 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
697 input->pointer = wl_seat_get_pointer(seat);
698 wl_pointer_set_user_data(input->pointer, input);
699 wl_pointer_add_listener(input->pointer, &pointer_listener,
701 weston_seat_init_pointer(input->compositor->base.seat);
702 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
703 wl_pointer_destroy(input->pointer);
704 input->pointer = NULL;
707 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
708 input->keyboard = wl_seat_get_keyboard(seat);
709 wl_keyboard_set_user_data(input->keyboard, input);
710 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
712 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
713 wl_keyboard_destroy(input->keyboard);
714 input->keyboard = NULL;
718 static const struct wl_seat_listener seat_listener = {
719 input_handle_capabilities,
723 display_add_seat(struct wayland_compositor *c, uint32_t id)
725 struct wayland_input *input;
727 input = malloc(sizeof *input);
731 memset(input, 0, sizeof *input);
733 input->compositor = c;
734 input->seat = wl_display_bind(c->parent.wl_display, id,
736 wl_list_insert(c->input_list.prev, &input->link);
738 wl_seat_add_listener(input->seat, &seat_listener, input);
739 wl_seat_set_user_data(input->seat, input);
742 /* We can't start adding input devices until weston_compositor_init, but
743 * also can't call weston_compositor_init until we've handled some of the
744 * base display code first. So, we have a separate global handler for
747 display_handle_global_input(struct wl_display *display, uint32_t id,
748 const char *interface, uint32_t version,
751 struct wayland_compositor *c = data;
753 if (strcmp(interface, "wl_seat") == 0)
754 display_add_seat(c, id);
759 display_handle_global(struct wl_display *display, uint32_t id,
760 const char *interface, uint32_t version, void *data)
762 struct wayland_compositor *c = data;
764 if (strcmp(interface, "wl_compositor") == 0) {
765 c->parent.compositor =
766 wl_display_bind(display, id, &wl_compositor_interface);
767 } else if (strcmp(interface, "wl_output") == 0) {
769 wl_display_bind(display, id, &wl_output_interface);
770 wl_output_add_listener(c->parent.output, &output_listener, c);
771 } else if (strcmp(interface, "wl_shell") == 0) {
773 wl_display_bind(display, id, &wl_shell_interface);
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_input_create(struct wayland_compositor *c)
805 struct weston_seat *seat;
807 seat = malloc(sizeof *seat);
811 memset(seat, 0, sizeof *seat);
812 weston_seat_init(seat, &c->base);
816 wl_display_add_global_listener(c->parent.wl_display,
817 display_handle_global_input,
824 wayland_destroy(struct weston_compositor *ec)
826 weston_compositor_shutdown(ec);
831 static struct weston_compositor *
832 wayland_compositor_create(struct wl_display *display,
833 int width, int height, const char *display_name,
834 int argc, char *argv[], const char *config_file)
836 struct wayland_compositor *c;
837 struct wl_event_loop *loop;
840 c = malloc(sizeof *c);
844 memset(c, 0, sizeof *c);
846 c->parent.wl_display = wl_display_connect(display_name);
848 if (c->parent.wl_display == NULL) {
849 weston_log("failed to create display: %m\n");
853 wl_list_init(&c->input_list);
854 wl_display_add_global_listener(c->parent.wl_display,
855 display_handle_global, c);
857 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
859 c->base.wl_display = display;
860 if (wayland_compositor_init_egl(c) < 0)
863 c->base.destroy = wayland_destroy;
865 /* Can't init base class until we have a current egl context */
866 if (weston_compositor_init(&c->base, display, argc, argv,
871 if (wayland_compositor_create_output(c, width, height) < 0)
874 if (wayland_input_create(c) < 0)
877 loop = wl_display_get_event_loop(c->base.wl_display);
879 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
880 c->parent.wl_source =
881 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
882 wayland_compositor_handle_event, c);
883 if (c->parent.wl_source == NULL)
889 WL_EXPORT struct weston_compositor *
890 backend_init(struct wl_display *display, int argc, char *argv[],
891 const char *config_file)
893 int width = 1024, height = 640;
894 char *display_name = NULL;
896 const struct weston_option wayland_options[] = {
897 { WESTON_OPTION_INTEGER, "width", 0, &width },
898 { WESTON_OPTION_INTEGER, "height", 0, &height },
899 { WESTON_OPTION_STRING, "display", 0, &display_name },
902 parse_options(wayland_options,
903 ARRAY_LENGTH(wayland_options), argc, argv);
905 return wayland_compositor_create(display, width, height, display_name,
906 argc, argv, config_file);