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.
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
38 #include <GLES2/gl2.h>
39 #include <GLES2/gl2ext.h>
41 #include <EGL/eglext.h>
43 #include "compositor.h"
45 struct wayland_compositor {
46 struct weston_compositor base;
48 struct wl_egl_pixmap *dummy_pixmap;
49 EGLSurface dummy_egl_surface;
52 struct wl_display *display;
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;
78 struct wl_surface *surface;
79 struct wl_shell_surface *shell_surface;
80 struct wl_egl_window *egl_window;
82 EGLSurface egl_surface;
83 struct weston_mode mode;
86 struct wayland_input {
87 struct wayland_compositor *compositor;
89 struct wl_pointer *pointer;
90 struct wl_keyboard *keyboard;
91 struct wl_touch *touch;
97 texture_border(struct wayland_output *output)
99 struct wayland_compositor *c =
100 (struct wayland_compositor *) output->base.compositor;
104 GLfloat x[4], y[4], u[4], v[4];
106 x[0] = -c->border.left;
108 x[2] = output->base.current->width;
109 x[3] = output->base.current->width + c->border.right;
111 y[0] = -c->border.top;
113 y[2] = output->base.current->height;
114 y[3] = output->base.current->height + c->border.bottom;
117 u[1] = (GLfloat) c->border.left / c->border.width;
118 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
122 v[1] = (GLfloat) c->border.top / c->border.height;
123 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
127 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
128 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
131 for (i = 0; i < 3; i++)
132 for (j = 0; j < 3; j++) {
134 if (i == 1 && j == 1)
173 draw_border(struct wayland_output *output)
175 struct wayland_compositor *c =
176 (struct wayland_compositor *) output->base.compositor;
177 struct weston_shader *shader = &c->base.texture_shader;
182 glUseProgram(shader->program);
183 c->base.current_shader = shader;
185 glUniformMatrix4fv(shader->proj_uniform,
186 1, GL_FALSE, output->base.matrix.d);
188 glUniform1i(shader->tex_uniform, 0);
189 glUniform1f(shader->alpha_uniform, 1);
190 glUniform1f(shader->texwidth_uniform, 1);
192 n = texture_border(output);
194 glBindTexture(GL_TEXTURE_2D, c->border.texture);
196 v = c->base.vertices.data;
197 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
198 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
199 glEnableVertexAttribArray(0);
200 glEnableVertexAttribArray(1);
202 glDrawElements(GL_TRIANGLES, n * 6,
203 GL_UNSIGNED_INT, c->base.indices.data);
205 glDisableVertexAttribArray(1);
206 glDisableVertexAttribArray(0);
208 c->base.vertices.size = 0;
209 c->base.indices.size = 0;
213 create_border(struct wayland_compositor *c)
215 pixman_image_t *image;
217 image = load_image(DATADIR "/weston/border.png");
219 fprintf(stderr, "could'nt load border image\n");
223 c->border.width = pixman_image_get_width(image);
224 c->border.height = pixman_image_get_height(image);
226 glGenTextures(1, &c->border.texture);
227 glBindTexture(GL_TEXTURE_2D, c->border.texture);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
236 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
237 pixman_image_get_data(image));
240 c->border.bottom = 50;
242 c->border.right = 25;
244 pixman_image_unref(image);
248 wayland_input_create(struct wayland_compositor *c)
250 struct weston_seat *seat;
252 seat = malloc(sizeof *seat);
256 memset(seat, 0, sizeof *seat);
257 weston_seat_init(seat, &c->base);
265 wayland_compositor_init_egl(struct wayland_compositor *c)
269 EGLint config_attribs[] = {
270 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
275 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
278 static const EGLint context_attribs[] = {
279 EGL_CONTEXT_CLIENT_VERSION, 2,
283 c->base.display = eglGetDisplay(c->parent.display);
284 if (c->base.display == NULL) {
285 fprintf(stderr, "failed to create display\n");
289 if (!eglInitialize(c->base.display, &major, &minor)) {
290 fprintf(stderr, "failed to initialize display\n");
294 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
295 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
298 if (!eglChooseConfig(c->base.display, config_attribs,
299 &c->base.config, 1, &n) || n == 0) {
300 fprintf(stderr, "failed to choose config: %d\n", n);
304 c->base.context = eglCreateContext(c->base.display, c->base.config,
305 EGL_NO_CONTEXT, context_attribs);
306 if (c->base.context == NULL) {
307 fprintf(stderr, "failed to create context\n");
311 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
312 if (!c->dummy_pixmap) {
313 fprintf(stderr, "failure to create dummy_pixmap\n");
317 c->dummy_egl_surface =
318 eglCreatePixmapSurface(c->base.display, c->base.config,
319 c->dummy_pixmap, NULL);
320 if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface,
321 c->dummy_egl_surface, c->base.context)) {
322 fprintf(stderr, "failed to make context current\n");
330 frame_done(void *data, struct wl_callback *callback, uint32_t time)
332 struct weston_output *output = data;
334 wl_callback_destroy(callback);
335 weston_output_finish_frame(output, time);
338 static const struct wl_callback_listener frame_listener = {
343 wayland_output_repaint(struct weston_output *output_base,
344 pixman_region32_t *damage)
346 struct wayland_output *output = (struct wayland_output *) output_base;
347 struct wayland_compositor *compositor =
348 (struct wayland_compositor *) output->base.compositor;
349 struct wl_callback *callback;
350 struct weston_surface *surface;
352 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
353 output->egl_surface, compositor->base.context)) {
354 fprintf(stderr, "failed to make current\n");
358 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
359 weston_surface_draw(surface, &output->base, damage);
363 weston_output_do_read_pixels(&output->base);
365 eglSwapBuffers(compositor->base.display, output->egl_surface);
366 callback = wl_surface_frame(output->parent.surface);
367 wl_callback_add_listener(callback, &frame_listener, output);
373 wayland_output_destroy(struct weston_output *output_base)
375 struct wayland_output *output = (struct wayland_output *) output_base;
376 struct weston_compositor *ec = output->base.compositor;
378 eglDestroySurface(ec->display, output->egl_surface);
379 wl_egl_window_destroy(output->parent.egl_window);
386 wayland_compositor_create_output(struct wayland_compositor *c,
387 int width, int height)
389 struct wayland_output *output;
391 output = malloc(sizeof *output);
394 memset(output, 0, sizeof *output);
397 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
398 output->mode.width = width;
399 output->mode.height = height;
400 output->mode.refresh = 60;
401 wl_list_init(&output->base.mode_list);
402 wl_list_insert(&output->base.mode_list, &output->mode.link);
404 output->base.current = &output->mode;
405 weston_output_init(&output->base, &c->base, 0, 0, width, height,
408 output->base.border.top = c->border.top;
409 output->base.border.bottom = c->border.bottom;
410 output->base.border.left = c->border.left;
411 output->base.border.right = c->border.right;
413 weston_output_move(&output->base, 0, 0);
415 output->parent.surface =
416 wl_compositor_create_surface(c->parent.compositor);
417 wl_surface_set_user_data(output->parent.surface, output);
419 output->parent.egl_window =
420 wl_egl_window_create(output->parent.surface,
421 width + c->border.left + c->border.right,
422 height + c->border.top + c->border.bottom);
423 if (!output->parent.egl_window) {
424 fprintf(stderr, "failure to create wl_egl_window\n");
428 output->egl_surface =
429 eglCreateWindowSurface(c->base.display, c->base.config,
430 output->parent.egl_window, NULL);
431 if (!output->egl_surface) {
432 fprintf(stderr, "failed to create window surface\n");
436 if (!eglMakeCurrent(c->base.display, output->egl_surface,
437 output->egl_surface, c->base.context)) {
438 fprintf(stderr, "failed to make surface current\n");
439 goto cleanup_surface;
443 output->parent.shell_surface =
444 wl_shell_get_shell_surface(c->parent.shell,
445 output->parent.surface);
446 /* FIXME: add shell_surface listener for resizing */
447 wl_shell_surface_set_toplevel(output->parent.shell_surface);
449 output->base.origin = output->base.current;
450 output->base.repaint = wayland_output_repaint;
451 output->base.destroy = wayland_output_destroy;
452 output->base.assign_planes = NULL;
453 output->base.set_backlight = NULL;
454 output->base.set_dpms = NULL;
455 output->base.switch_mode = NULL;
457 wl_list_insert(c->base.output_list.prev, &output->base.link);
462 eglDestroySurface(c->base.display, output->egl_surface);
464 wl_egl_window_destroy(output->parent.egl_window);
466 /* FIXME: cleanup weston_output */
472 /* Events received from the wayland-server this compositor is client of: */
474 /* parent output interface */
476 display_handle_geometry(void *data,
477 struct wl_output *wl_output,
486 struct wayland_compositor *c = data;
488 c->parent.screen_allocation.x = x;
489 c->parent.screen_allocation.y = y;
493 display_handle_mode(void *data,
494 struct wl_output *wl_output,
500 struct wayland_compositor *c = data;
502 c->parent.screen_allocation.width = width;
503 c->parent.screen_allocation.height = height;
506 static const struct wl_output_listener output_listener = {
507 display_handle_geometry,
511 /* parent input interface */
513 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
514 uint32_t serial, struct wl_surface *surface,
515 wl_fixed_t x, wl_fixed_t y)
517 struct wayland_input *input = data;
518 struct wayland_output *output;
519 struct wayland_compositor *c = input->compositor;
521 output = wl_surface_get_user_data(surface);
522 notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
523 wl_pointer_attach(input->pointer, serial, NULL, 0, 0);
527 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
528 uint32_t serial, struct wl_surface *surface)
530 struct wayland_input *input = data;
531 struct wayland_compositor *c = input->compositor;
533 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
537 input_handle_motion(void *data, struct wl_pointer *pointer,
538 uint32_t time, wl_fixed_t x, wl_fixed_t y)
540 struct wayland_input *input = data;
541 struct wayland_compositor *c = input->compositor;
543 notify_motion(&c->base.seat->seat, time,
544 x - wl_fixed_from_int(c->border.left),
545 y - wl_fixed_from_int(c->border.top));
549 input_handle_button(void *data, struct wl_pointer *pointer,
550 uint32_t serial, uint32_t time, uint32_t button,
553 struct wayland_input *input = data;
554 struct wayland_compositor *c = input->compositor;
555 enum wl_pointer_button_state state = state_w;
557 notify_button(&c->base.seat->seat, time, button, state);
561 input_handle_axis(void *data, struct wl_pointer *pointer,
562 uint32_t time, uint32_t axis, int32_t value)
564 struct wayland_input *input = data;
565 struct wayland_compositor *c = input->compositor;
567 notify_axis(&c->base.seat->seat, time, axis, value);
570 static const struct wl_pointer_listener pointer_listener = {
571 input_handle_pointer_enter,
572 input_handle_pointer_leave,
579 input_handle_keyboard_enter(void *data,
580 struct wl_keyboard *keyboard,
582 struct wl_surface *surface,
583 struct wl_array *keys)
585 struct wayland_input *input = data;
586 struct wayland_compositor *c = input->compositor;
588 notify_keyboard_focus(&c->base.seat->seat, keys);
592 input_handle_keyboard_leave(void *data,
593 struct wl_keyboard *keyboard,
595 struct wl_surface *surface)
597 struct wayland_input *input = data;
598 struct wayland_compositor *c = input->compositor;
600 notify_keyboard_focus(&c->base.seat->seat, NULL);
604 input_handle_key(void *data, struct wl_keyboard *keyboard,
605 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
607 struct wayland_input *input = data;
608 struct wayland_compositor *c = input->compositor;
610 notify_key(&c->base.seat->seat, time, key, state);
614 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
615 uint32_t serial, uint32_t mods_depressed,
616 uint32_t mods_latched, uint32_t mods_locked,
619 /* XXX notify_modifiers(); */
622 static const struct wl_keyboard_listener keyboard_listener = {
623 input_handle_keyboard_enter,
624 input_handle_keyboard_leave,
626 input_handle_modifiers,
630 input_handle_capabilities(void *data, struct wl_seat *seat,
631 enum wl_seat_capability caps)
633 struct wayland_input *input = data;
635 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
636 input->pointer = wl_seat_get_pointer(seat);
637 wl_pointer_set_user_data(input->pointer, input);
638 wl_pointer_add_listener(input->pointer, &pointer_listener,
640 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
641 wl_pointer_destroy(input->pointer);
642 input->pointer = NULL;
645 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
646 input->keyboard = wl_seat_get_keyboard(seat);
647 wl_keyboard_set_user_data(input->keyboard, input);
648 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
650 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
651 wl_keyboard_destroy(input->keyboard);
652 input->keyboard = NULL;
656 static const struct wl_seat_listener seat_listener = {
657 input_handle_capabilities,
661 display_add_seat(struct wayland_compositor *c, uint32_t id)
663 struct wayland_input *input;
665 input = malloc(sizeof *input);
669 memset(input, 0, sizeof *input);
671 input->compositor = c;
672 input->seat = wl_display_bind(c->parent.display, id,
674 wl_list_insert(c->input_list.prev, &input->link);
676 wl_seat_add_listener(input->seat, &seat_listener, input);
677 wl_seat_set_user_data(input->seat, input);
681 display_handle_global(struct wl_display *display, uint32_t id,
682 const char *interface, uint32_t version, void *data)
684 struct wayland_compositor *c = data;
686 if (strcmp(interface, "wl_compositor") == 0) {
687 c->parent.compositor =
688 wl_display_bind(display, id, &wl_compositor_interface);
689 } else if (strcmp(interface, "wl_output") == 0) {
691 wl_display_bind(display, id, &wl_output_interface);
692 wl_output_add_listener(c->parent.output, &output_listener, c);
693 } else if (strcmp(interface, "wl_seat") == 0) {
694 display_add_seat(c, id);
695 } else if (strcmp(interface, "wl_shell") == 0) {
697 wl_display_bind(display, id, &wl_shell_interface);
702 update_event_mask(uint32_t mask, void *data)
704 struct wayland_compositor *c = data;
706 c->parent.event_mask = mask;
707 if (c->parent.wl_source)
708 wl_event_source_fd_update(c->parent.wl_source, mask);
714 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
716 struct wayland_compositor *c = data;
718 if (mask & WL_EVENT_READABLE)
719 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
720 if (mask & WL_EVENT_WRITABLE)
721 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
727 wayland_destroy(struct weston_compositor *ec)
729 weston_compositor_shutdown(ec);
734 static struct weston_compositor *
735 wayland_compositor_create(struct wl_display *display,
736 int width, int height, const char *display_name)
738 struct wayland_compositor *c;
739 struct wl_event_loop *loop;
742 c = malloc(sizeof *c);
746 memset(c, 0, sizeof *c);
748 c->parent.display = wl_display_connect(display_name);
750 if (c->parent.display == NULL) {
751 fprintf(stderr, "failed to create display: %m\n");
755 wl_list_init(&c->input_list);
756 wl_display_add_global_listener(c->parent.display,
757 display_handle_global, c);
759 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
761 c->base.wl_display = display;
762 if (wayland_compositor_init_egl(c) < 0)
765 c->base.destroy = wayland_destroy;
767 /* Can't init base class until we have a current egl context */
768 if (weston_compositor_init(&c->base, display) < 0)
772 if (wayland_compositor_create_output(c, width, height) < 0)
775 if (wayland_input_create(c) < 0)
778 loop = wl_display_get_event_loop(c->base.wl_display);
780 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
781 c->parent.wl_source =
782 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
783 wayland_compositor_handle_event, c);
784 if (c->parent.wl_source == NULL)
790 WL_EXPORT struct weston_compositor *
791 backend_init(struct wl_display *display, int argc, char *argv[])
793 int width = 1024, height = 640;
794 char *display_name = NULL;
796 const struct weston_option wayland_options[] = {
797 { WESTON_OPTION_INTEGER, "width", 0, &width },
798 { WESTON_OPTION_INTEGER, "height", 0, &height },
799 { WESTON_OPTION_STRING, "display", 0, &display_name },
802 parse_options(wayland_options,
803 ARRAY_LENGTH(wayland_options), argc, argv);
805 return wayland_compositor_create(display, width, height, display_name);