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;
88 struct wl_input_device *input_device;
94 texture_border(struct wayland_output *output)
96 struct wayland_compositor *c =
97 (struct wayland_compositor *) output->base.compositor;
101 GLfloat x[4], y[4], u[4], v[4];
103 x[0] = -c->border.left;
105 x[2] = output->base.current->width;
106 x[3] = output->base.current->width + c->border.right;
108 y[0] = -c->border.top;
110 y[2] = output->base.current->height;
111 y[3] = output->base.current->height + c->border.bottom;
114 u[1] = (GLfloat) c->border.left / c->border.width;
115 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
119 v[1] = (GLfloat) c->border.top / c->border.height;
120 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
124 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
125 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
128 for (i = 0; i < 3; i++)
129 for (j = 0; j < 3; j++) {
131 if (i == 1 && j == 1)
170 draw_border(struct wayland_output *output)
172 struct wayland_compositor *c =
173 (struct wayland_compositor *) output->base.compositor;
174 struct weston_shader *shader = &c->base.texture_shader;
179 glUseProgram(shader->program);
180 c->base.current_shader = shader;
182 glUniformMatrix4fv(shader->proj_uniform,
183 1, GL_FALSE, output->base.matrix.d);
185 glUniform1i(shader->tex_uniform, 0);
186 glUniform1f(shader->alpha_uniform, 1);
187 glUniform1f(shader->texwidth_uniform, 1);
189 n = texture_border(output);
191 glBindTexture(GL_TEXTURE_2D, c->border.texture);
193 v = c->base.vertices.data;
194 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
195 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
196 glEnableVertexAttribArray(0);
197 glEnableVertexAttribArray(1);
199 glDrawElements(GL_TRIANGLES, n * 6,
200 GL_UNSIGNED_INT, c->base.indices.data);
202 glDisableVertexAttribArray(1);
203 glDisableVertexAttribArray(0);
205 c->base.vertices.size = 0;
206 c->base.indices.size = 0;
210 create_border(struct wayland_compositor *c)
212 pixman_image_t *image;
214 image = load_image(DATADIR "/weston/border.png");
216 fprintf(stderr, "could'nt load border image\n");
220 c->border.width = pixman_image_get_width(image);
221 c->border.height = pixman_image_get_height(image);
223 glGenTextures(1, &c->border.texture);
224 glBindTexture(GL_TEXTURE_2D, c->border.texture);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
230 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
233 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
234 pixman_image_get_data(image));
237 c->border.bottom = 50;
239 c->border.right = 25;
241 pixman_image_unref(image);
245 wayland_input_create(struct wayland_compositor *c)
247 struct weston_input_device *input;
249 input = malloc(sizeof *input);
253 memset(input, 0, sizeof *input);
254 weston_input_device_init(input, &c->base);
256 c->base.input_device = &input->input_device;
262 wayland_compositor_init_egl(struct wayland_compositor *c)
266 EGLint config_attribs[] = {
267 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
272 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
275 static const EGLint context_attribs[] = {
276 EGL_CONTEXT_CLIENT_VERSION, 2,
280 c->base.display = eglGetDisplay(c->parent.display);
281 if (c->base.display == NULL) {
282 fprintf(stderr, "failed to create display\n");
286 if (!eglInitialize(c->base.display, &major, &minor)) {
287 fprintf(stderr, "failed to initialize display\n");
291 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
292 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
295 if (!eglChooseConfig(c->base.display, config_attribs,
296 &c->base.config, 1, &n) || n == 0) {
297 fprintf(stderr, "failed to choose config: %d\n", n);
301 c->base.context = eglCreateContext(c->base.display, c->base.config,
302 EGL_NO_CONTEXT, context_attribs);
303 if (c->base.context == NULL) {
304 fprintf(stderr, "failed to create context\n");
308 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
309 if (!c->dummy_pixmap) {
310 fprintf(stderr, "failure to create dummy_pixmap\n");
314 c->dummy_egl_surface =
315 eglCreatePixmapSurface(c->base.display, c->base.config,
316 c->dummy_pixmap, NULL);
317 if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface,
318 c->dummy_egl_surface, c->base.context)) {
319 fprintf(stderr, "failed to make context current\n");
327 frame_done(void *data, struct wl_callback *callback, uint32_t time)
329 struct weston_output *output = data;
331 wl_callback_destroy(callback);
332 weston_output_finish_frame(output, time);
335 static const struct wl_callback_listener frame_listener = {
340 wayland_output_repaint(struct weston_output *output_base,
341 pixman_region32_t *damage)
343 struct wayland_output *output = (struct wayland_output *) output_base;
344 struct wayland_compositor *compositor =
345 (struct wayland_compositor *) output->base.compositor;
346 struct wl_callback *callback;
347 struct weston_surface *surface;
349 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
350 output->egl_surface, compositor->base.context)) {
351 fprintf(stderr, "failed to make current\n");
355 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
356 weston_surface_draw(surface, &output->base, damage);
360 eglSwapBuffers(compositor->base.display, output->egl_surface);
361 callback = wl_surface_frame(output->parent.surface);
362 wl_callback_add_listener(callback, &frame_listener, output);
368 wayland_output_destroy(struct weston_output *output_base)
370 struct wayland_output *output = (struct wayland_output *) output_base;
371 struct weston_compositor *ec = output->base.compositor;
373 eglDestroySurface(ec->display, output->egl_surface);
374 wl_egl_window_destroy(output->parent.egl_window);
381 wayland_output_read_pixels(struct weston_output *output_base, void *data)
383 struct wayland_output *output = (struct wayland_output *) output_base;
384 struct wayland_compositor *compositor =
385 (struct wayland_compositor *) output->base.compositor;
387 eglMakeCurrent(compositor->base.display, output->egl_surface,
388 output->egl_surface, compositor->base.context);
390 glReadPixels(0, 0, output_base->current->width, output_base->current->height,
391 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
395 wayland_compositor_create_output(struct wayland_compositor *c,
396 int width, int height)
398 struct wayland_output *output;
400 output = malloc(sizeof *output);
403 memset(output, 0, sizeof *output);
406 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
407 output->mode.width = width;
408 output->mode.height = height;
409 output->mode.refresh = 60;
410 wl_list_init(&output->base.mode_list);
411 wl_list_insert(&output->base.mode_list, &output->mode.link);
413 output->base.current = &output->mode;
414 weston_output_init(&output->base, &c->base, 0, 0, width, height,
417 output->base.border.top = c->border.top;
418 output->base.border.bottom = c->border.bottom;
419 output->base.border.left = c->border.left;
420 output->base.border.right = c->border.right;
422 weston_output_move(&output->base, 0, 0);
424 output->parent.surface =
425 wl_compositor_create_surface(c->parent.compositor);
426 wl_surface_set_user_data(output->parent.surface, output);
428 output->parent.egl_window =
429 wl_egl_window_create(output->parent.surface,
430 width + c->border.left + c->border.right,
431 height + c->border.top + c->border.bottom);
432 if (!output->parent.egl_window) {
433 fprintf(stderr, "failure to create wl_egl_window\n");
437 output->egl_surface =
438 eglCreateWindowSurface(c->base.display, c->base.config,
439 output->parent.egl_window, NULL);
440 if (!output->egl_surface) {
441 fprintf(stderr, "failed to create window surface\n");
445 if (!eglMakeCurrent(c->base.display, output->egl_surface,
446 output->egl_surface, c->base.context)) {
447 fprintf(stderr, "failed to make surface current\n");
448 goto cleanup_surface;
452 output->parent.shell_surface =
453 wl_shell_get_shell_surface(c->parent.shell,
454 output->parent.surface);
455 /* FIXME: add shell_surface listener for resizing */
456 wl_shell_surface_set_toplevel(output->parent.shell_surface);
458 output->base.repaint = wayland_output_repaint;
459 output->base.destroy = wayland_output_destroy;
460 output->base.assign_planes = NULL;
461 output->base.read_pixels = wayland_output_read_pixels;
462 output->base.set_backlight = NULL;
463 output->base.set_dpms = NULL;
465 wl_list_insert(c->base.output_list.prev, &output->base.link);
470 eglDestroySurface(c->base.display, output->egl_surface);
472 wl_egl_window_destroy(output->parent.egl_window);
474 /* FIXME: cleanup weston_output */
480 /* Events received from the wayland-server this compositor is client of: */
482 /* parent output interface */
484 display_handle_geometry(void *data,
485 struct wl_output *wl_output,
494 struct wayland_compositor *c = data;
496 c->parent.screen_allocation.x = x;
497 c->parent.screen_allocation.y = y;
501 display_handle_mode(void *data,
502 struct wl_output *wl_output,
508 struct wayland_compositor *c = data;
510 c->parent.screen_allocation.width = width;
511 c->parent.screen_allocation.height = height;
514 static const struct wl_output_listener output_listener = {
515 display_handle_geometry,
519 /* parent input interface */
521 input_handle_motion(void *data, struct wl_input_device *input_device,
522 uint32_t time, int32_t sx, int32_t sy)
524 struct wayland_input *input = data;
525 struct wayland_compositor *c = input->compositor;
527 notify_motion(c->base.input_device, time,
528 sx - c->border.left, sy - c->border.top);
532 input_handle_button(void *data,
533 struct wl_input_device *input_device,
534 uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
536 struct wayland_input *input = data;
537 struct wayland_compositor *c = input->compositor;
539 notify_button(c->base.input_device, time, button, state);
543 input_handle_axis(void *data, struct wl_input_device *input_device,
544 uint32_t time, uint32_t axis, int32_t value)
546 struct wayland_input *input = data;
547 struct wayland_compositor *c = input->compositor;
549 notify_axis(c->base.input_device, time, axis, value);
553 input_handle_key(void *data, struct wl_input_device *input_device,
554 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
556 struct wayland_input *input = data;
557 struct wayland_compositor *c = input->compositor;
559 notify_key(c->base.input_device, time, key, state);
563 input_handle_pointer_enter(void *data,
564 struct wl_input_device *input_device,
565 uint32_t time, struct wl_surface *surface,
566 int32_t sx, int32_t sy)
568 struct wayland_input *input = data;
569 struct wayland_output *output;
570 struct wayland_compositor *c = input->compositor;
572 output = wl_surface_get_user_data(surface);
573 notify_pointer_focus(c->base.input_device, &output->base, sx, sy);
574 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
578 input_handle_pointer_leave(void *data,
579 struct wl_input_device *input_device,
580 uint32_t time, struct wl_surface *surface)
582 struct wayland_input *input = data;
583 struct wayland_compositor *c = input->compositor;
585 notify_pointer_focus(c->base.input_device, NULL, 0, 0);
589 input_handle_keyboard_enter(void *data,
590 struct wl_input_device *input_device,
592 struct wl_surface *surface,
593 struct wl_array *keys)
595 struct wayland_input *input = data;
596 struct wayland_compositor *c = input->compositor;
598 notify_keyboard_focus(c->base.input_device, keys);
602 input_handle_keyboard_leave(void *data,
603 struct wl_input_device *input_device,
605 struct wl_surface *surface)
607 struct wayland_input *input = data;
608 struct wayland_compositor *c = input->compositor;
610 notify_keyboard_focus(c->base.input_device, NULL);
613 static const struct wl_input_device_listener input_device_listener = {
618 input_handle_pointer_enter,
619 input_handle_pointer_leave,
620 input_handle_keyboard_enter,
621 input_handle_keyboard_leave,
625 display_add_input(struct wayland_compositor *c, uint32_t id)
627 struct wayland_input *input;
629 input = malloc(sizeof *input);
633 memset(input, 0, sizeof *input);
635 input->compositor = c;
636 input->input_device = wl_display_bind(c->parent.display,
637 id, &wl_input_device_interface);
638 wl_list_insert(c->input_list.prev, &input->link);
640 wl_input_device_add_listener(input->input_device,
641 &input_device_listener, input);
642 wl_input_device_set_user_data(input->input_device, input);
646 display_handle_global(struct wl_display *display, uint32_t id,
647 const char *interface, uint32_t version, void *data)
649 struct wayland_compositor *c = data;
651 if (strcmp(interface, "wl_compositor") == 0) {
652 c->parent.compositor =
653 wl_display_bind(display, id, &wl_compositor_interface);
654 } else if (strcmp(interface, "wl_output") == 0) {
656 wl_display_bind(display, id, &wl_output_interface);
657 wl_output_add_listener(c->parent.output, &output_listener, c);
658 } else if (strcmp(interface, "wl_input_device") == 0) {
659 display_add_input(c, id);
660 } else if (strcmp(interface, "wl_shell") == 0) {
662 wl_display_bind(display, id, &wl_shell_interface);
667 update_event_mask(uint32_t mask, void *data)
669 struct wayland_compositor *c = data;
671 c->parent.event_mask = mask;
672 if (c->parent.wl_source)
673 wl_event_source_fd_update(c->parent.wl_source, mask);
679 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
681 struct wayland_compositor *c = data;
683 if (mask & WL_EVENT_READABLE)
684 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
685 if (mask & WL_EVENT_WRITABLE)
686 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
692 wayland_destroy(struct weston_compositor *ec)
694 weston_compositor_shutdown(ec);
699 static struct weston_compositor *
700 wayland_compositor_create(struct wl_display *display,
701 int width, int height, const char *display_name)
703 struct wayland_compositor *c;
704 struct wl_event_loop *loop;
707 c = malloc(sizeof *c);
711 memset(c, 0, sizeof *c);
713 c->parent.display = wl_display_connect(display_name);
715 if (c->parent.display == NULL) {
716 fprintf(stderr, "failed to create display: %m\n");
720 wl_list_init(&c->input_list);
721 wl_display_add_global_listener(c->parent.display,
722 display_handle_global, c);
724 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
726 c->base.wl_display = display;
727 if (wayland_compositor_init_egl(c) < 0)
730 c->base.destroy = wayland_destroy;
732 /* Can't init base class until we have a current egl context */
733 if (weston_compositor_init(&c->base, display) < 0)
737 if (wayland_compositor_create_output(c, width, height) < 0)
740 if (wayland_input_create(c) < 0)
743 loop = wl_display_get_event_loop(c->base.wl_display);
745 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
746 c->parent.wl_source =
747 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
748 wayland_compositor_handle_event, c);
749 if (c->parent.wl_source == NULL)
755 WL_EXPORT struct weston_compositor *
756 backend_init(struct wl_display *display, int argc, char *argv[])
758 int width = 1024, height = 640;
759 char *display_name = NULL;
761 const struct weston_option wayland_options[] = {
762 { WESTON_OPTION_INTEGER, "width", 0, &width },
763 { WESTON_OPTION_INTEGER, "height", 0, &height },
764 { WESTON_OPTION_STRING, "display", 0, &display_name },
767 parse_options(wayland_options,
768 ARRAY_LENGTH(wayland_options), argc, argv);
770 return wayland_compositor_create(display, width, height, display_name);