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;
49 struct wl_display *display;
50 struct wl_compositor *compositor;
51 struct wl_shell *shell;
52 struct wl_output *output;
55 int32_t x, y, width, height;
58 struct wl_event_source *wl_source;
63 int32_t top, bottom, left, right;
65 int32_t width, height;
68 struct wl_list input_list;
71 struct wayland_output {
72 struct weston_output base;
75 struct wl_surface *surface;
76 struct wl_shell_surface *shell_surface;
77 struct wl_egl_window *egl_window;
79 EGLSurface egl_surface;
80 struct weston_mode mode;
83 struct wayland_input {
84 struct wayland_compositor *compositor;
85 struct wl_input_device *input_device;
91 texture_border(struct wayland_output *output)
93 struct wayland_compositor *c =
94 (struct wayland_compositor *) output->base.compositor;
98 GLfloat x[4], y[4], u[4], v[4];
100 x[0] = -c->border.left;
102 x[2] = output->base.current->width;
103 x[3] = output->base.current->width + c->border.right;
105 y[0] = -c->border.top;
107 y[2] = output->base.current->height;
108 y[3] = output->base.current->height + c->border.bottom;
111 u[1] = (GLfloat) c->border.left / c->border.width;
112 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
116 v[1] = (GLfloat) c->border.top / c->border.height;
117 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
121 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
122 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
125 for (i = 0; i < 3; i++)
126 for (j = 0; j < 3; j++) {
128 if (i == 1 && j == 1)
167 draw_border(struct wayland_output *output)
169 struct wayland_compositor *c =
170 (struct wayland_compositor *) output->base.compositor;
171 struct weston_shader *shader = &c->base.texture_shader;
176 glUseProgram(shader->program);
177 c->base.current_shader = shader;
179 glUniformMatrix4fv(shader->proj_uniform,
180 1, GL_FALSE, output->base.matrix.d);
182 glUniform1i(shader->tex_uniform, 0);
183 glUniform1f(shader->alpha_uniform, 1);
184 glUniform1f(shader->texwidth_uniform, 1);
186 n = texture_border(output);
188 glBindTexture(GL_TEXTURE_2D, c->border.texture);
190 v = c->base.vertices.data;
191 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
192 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
193 glEnableVertexAttribArray(0);
194 glEnableVertexAttribArray(1);
196 glDrawElements(GL_TRIANGLES, n * 6,
197 GL_UNSIGNED_INT, c->base.indices.data);
199 glDisableVertexAttribArray(1);
200 glDisableVertexAttribArray(0);
202 c->base.vertices.size = 0;
203 c->base.indices.size = 0;
207 create_border(struct wayland_compositor *c)
209 pixman_image_t *image;
211 image = load_image(DATADIR "/weston/border.png");
213 fprintf(stderr, "could'nt load border image\n");
217 c->border.width = pixman_image_get_width(image);
218 c->border.height = pixman_image_get_height(image);
220 glGenTextures(1, &c->border.texture);
221 glBindTexture(GL_TEXTURE_2D, c->border.texture);
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
227 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
230 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
231 pixman_image_get_data(image));
234 c->border.bottom = 50;
236 c->border.right = 25;
238 pixman_image_unref(image);
242 wayland_input_create(struct wayland_compositor *c)
244 struct weston_input_device *input;
246 input = malloc(sizeof *input);
250 memset(input, 0, sizeof *input);
251 weston_input_device_init(input, &c->base);
253 c->base.input_device = &input->input_device;
259 wayland_compositor_init_egl(struct wayland_compositor *c)
263 const char *extensions;
264 EGLint config_attribs[] = {
265 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
270 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
273 static const EGLint context_attribs[] = {
274 EGL_CONTEXT_CLIENT_VERSION, 2,
278 c->base.display = eglGetDisplay(c->parent.display);
279 if (c->base.display == NULL) {
280 fprintf(stderr, "failed to create display\n");
284 if (!eglInitialize(c->base.display, &major, &minor)) {
285 fprintf(stderr, "failed to initialize display\n");
289 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
290 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
291 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
295 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
296 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
299 if (!eglChooseConfig(c->base.display, config_attribs,
300 &c->base.config, 1, &n) || n == 0) {
301 fprintf(stderr, "failed to choose config: %d\n", n);
305 c->base.context = eglCreateContext(c->base.display, c->base.config,
306 EGL_NO_CONTEXT, context_attribs);
307 if (c->base.context == NULL) {
308 fprintf(stderr, "failed to create context\n");
312 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
313 EGL_NO_SURFACE, c->base.context)) {
314 fprintf(stderr, "failed to make context current\n");
322 frame_done(void *data, struct wl_callback *callback, uint32_t time)
324 struct weston_output *output = data;
326 wl_callback_destroy(callback);
327 weston_output_finish_frame(output, time);
330 static const struct wl_callback_listener frame_listener = {
335 wayland_output_repaint(struct weston_output *output_base,
336 pixman_region32_t *damage)
338 struct wayland_output *output = (struct wayland_output *) output_base;
339 struct wayland_compositor *compositor =
340 (struct wayland_compositor *) output->base.compositor;
341 struct wl_callback *callback;
342 struct weston_surface *surface;
344 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
345 output->egl_surface, compositor->base.context)) {
346 fprintf(stderr, "failed to make current\n");
350 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
351 weston_surface_draw(surface, &output->base, damage);
355 eglSwapBuffers(compositor->base.display, output->egl_surface);
356 callback = wl_surface_frame(output->parent.surface);
357 wl_callback_add_listener(callback, &frame_listener, output);
363 wayland_output_destroy(struct weston_output *output_base)
365 struct wayland_output *output = (struct wayland_output *) output_base;
366 struct weston_compositor *ec = output->base.compositor;
368 eglDestroySurface(ec->display, output->egl_surface);
369 wl_egl_window_destroy(output->parent.egl_window);
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,
398 output->base.border.top = c->border.top;
399 output->base.border.bottom = c->border.bottom;
400 output->base.border.left = c->border.left;
401 output->base.border.right = c->border.right;
403 weston_output_move(&output->base, 0, 0);
405 output->parent.surface =
406 wl_compositor_create_surface(c->parent.compositor);
407 wl_surface_set_user_data(output->parent.surface, output);
409 output->parent.egl_window =
410 wl_egl_window_create(output->parent.surface,
411 width + c->border.left + c->border.right,
412 height + c->border.top + c->border.bottom);
413 if (!output->parent.egl_window) {
414 fprintf(stderr, "failure to create wl_egl_window\n");
418 output->egl_surface =
419 eglCreateWindowSurface(c->base.display, c->base.config,
420 output->parent.egl_window, NULL);
421 if (!output->egl_surface) {
422 fprintf(stderr, "failed to create window surface\n");
426 if (!eglMakeCurrent(c->base.display, output->egl_surface,
427 output->egl_surface, c->base.context)) {
428 fprintf(stderr, "failed to make surface current\n");
429 goto cleanup_surface;
433 output->parent.shell_surface =
434 wl_shell_get_shell_surface(c->parent.shell,
435 output->parent.surface);
436 /* FIXME: add shell_surface listener for resizing */
437 wl_shell_surface_set_toplevel(output->parent.shell_surface);
439 output->base.repaint = wayland_output_repaint;
440 output->base.destroy = wayland_output_destroy;
441 output->base.assign_planes = NULL;
442 output->base.set_backlight = NULL;
443 output->base.set_dpms = NULL;
445 wl_list_insert(c->base.output_list.prev, &output->base.link);
450 eglDestroySurface(c->base.display, output->egl_surface);
452 wl_egl_window_destroy(output->parent.egl_window);
454 /* FIXME: cleanup weston_output */
460 /* Events received from the wayland-server this compositor is client of: */
462 /* parent output interface */
464 display_handle_geometry(void *data,
465 struct wl_output *wl_output,
474 struct wayland_compositor *c = data;
476 c->parent.screen_allocation.x = x;
477 c->parent.screen_allocation.y = y;
481 display_handle_mode(void *data,
482 struct wl_output *wl_output,
488 struct wayland_compositor *c = data;
490 c->parent.screen_allocation.width = width;
491 c->parent.screen_allocation.height = height;
494 static const struct wl_output_listener output_listener = {
495 display_handle_geometry,
499 /* parent input interface */
501 input_handle_motion(void *data, struct wl_input_device *input_device,
502 uint32_t time, int32_t sx, int32_t sy)
504 struct wayland_input *input = data;
505 struct wayland_compositor *c = input->compositor;
507 notify_motion(c->base.input_device, time,
508 sx - c->border.left, sy - c->border.top);
512 input_handle_button(void *data,
513 struct wl_input_device *input_device,
514 uint32_t time, uint32_t button, uint32_t state)
516 struct wayland_input *input = data;
517 struct wayland_compositor *c = input->compositor;
519 notify_button(c->base.input_device, time, button, state);
523 input_handle_axis(void *data, struct wl_input_device *input_device,
524 uint32_t time, uint32_t axis, int32_t value)
526 struct wayland_input *input = data;
527 struct wayland_compositor *c = input->compositor;
529 notify_axis(c->base.input_device, time, axis, value);
533 input_handle_key(void *data, struct wl_input_device *input_device,
534 uint32_t time, uint32_t key, uint32_t state)
536 struct wayland_input *input = data;
537 struct wayland_compositor *c = input->compositor;
539 notify_key(c->base.input_device, time, key, state);
543 input_handle_pointer_enter(void *data,
544 struct wl_input_device *input_device,
545 uint32_t time, struct wl_surface *surface,
546 int32_t sx, int32_t sy)
548 struct wayland_input *input = data;
549 struct wayland_output *output;
550 struct wayland_compositor *c = input->compositor;
552 output = wl_surface_get_user_data(surface);
553 notify_pointer_focus(c->base.input_device,
554 time, &output->base, sx, sy);
555 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
559 input_handle_pointer_leave(void *data,
560 struct wl_input_device *input_device,
561 uint32_t time, struct wl_surface *surface)
563 struct wayland_input *input = data;
564 struct wayland_compositor *c = input->compositor;
566 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
570 input_handle_keyboard_enter(void *data,
571 struct wl_input_device *input_device,
573 struct wl_surface *surface,
574 struct wl_array *keys)
576 struct wayland_input *input = data;
577 struct wayland_compositor *c = input->compositor;
578 struct wayland_output *output;
580 output = wl_surface_get_user_data(surface);
581 notify_keyboard_focus(c->base.input_device, time, &output->base, keys);
585 input_handle_keyboard_leave(void *data,
586 struct wl_input_device *input_device,
588 struct wl_surface *surface)
590 struct wayland_input *input = data;
591 struct wayland_compositor *c = input->compositor;
593 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
596 static const struct wl_input_device_listener input_device_listener = {
601 input_handle_pointer_enter,
602 input_handle_pointer_leave,
603 input_handle_keyboard_enter,
604 input_handle_keyboard_leave,
608 display_add_input(struct wayland_compositor *c, uint32_t id)
610 struct wayland_input *input;
612 input = malloc(sizeof *input);
616 memset(input, 0, sizeof *input);
618 input->compositor = c;
619 input->input_device = wl_display_bind(c->parent.display,
620 id, &wl_input_device_interface);
621 wl_list_insert(c->input_list.prev, &input->link);
623 wl_input_device_add_listener(input->input_device,
624 &input_device_listener, input);
625 wl_input_device_set_user_data(input->input_device, input);
629 display_handle_global(struct wl_display *display, uint32_t id,
630 const char *interface, uint32_t version, void *data)
632 struct wayland_compositor *c = data;
634 if (strcmp(interface, "wl_compositor") == 0) {
635 c->parent.compositor =
636 wl_display_bind(display, id, &wl_compositor_interface);
637 } else if (strcmp(interface, "wl_output") == 0) {
639 wl_display_bind(display, id, &wl_output_interface);
640 wl_output_add_listener(c->parent.output, &output_listener, c);
641 } else if (strcmp(interface, "wl_input_device") == 0) {
642 display_add_input(c, id);
643 } else if (strcmp(interface, "wl_shell") == 0) {
645 wl_display_bind(display, id, &wl_shell_interface);
650 update_event_mask(uint32_t mask, void *data)
652 struct wayland_compositor *c = data;
654 c->parent.event_mask = mask;
655 if (c->parent.wl_source)
656 wl_event_source_fd_update(c->parent.wl_source, mask);
662 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
664 struct wayland_compositor *c = data;
666 if (mask & WL_EVENT_READABLE)
667 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
668 if (mask & WL_EVENT_WRITABLE)
669 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
675 wayland_destroy(struct weston_compositor *ec)
677 weston_compositor_shutdown(ec);
682 static struct weston_compositor *
683 wayland_compositor_create(struct wl_display *display,
684 int width, int height, const char *display_name)
686 struct wayland_compositor *c;
687 struct wl_event_loop *loop;
690 c = malloc(sizeof *c);
694 memset(c, 0, sizeof *c);
696 c->parent.display = wl_display_connect(display_name);
698 if (c->parent.display == NULL) {
699 fprintf(stderr, "failed to create display: %m\n");
703 wl_list_init(&c->input_list);
704 wl_display_add_global_listener(c->parent.display,
705 display_handle_global, c);
707 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
709 c->base.wl_display = display;
710 if (wayland_compositor_init_egl(c) < 0)
713 c->base.destroy = wayland_destroy;
715 /* Can't init base class until we have a current egl context */
716 if (weston_compositor_init(&c->base, display) < 0)
720 if (wayland_compositor_create_output(c, width, height) < 0)
723 if (wayland_input_create(c) < 0)
726 loop = wl_display_get_event_loop(c->base.wl_display);
728 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
729 c->parent.wl_source =
730 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
731 wayland_compositor_handle_event, c);
732 if (c->parent.wl_source == NULL)
738 WL_EXPORT struct weston_compositor *
739 backend_init(struct wl_display *display, int argc, char *argv[])
741 int width = 1024, height = 640;
742 char *display_name = NULL;
744 const struct weston_option wayland_options[] = {
745 { WESTON_OPTION_INTEGER, "width", 0, &width },
746 { WESTON_OPTION_INTEGER, "height", 0, &height },
747 { WESTON_OPTION_STRING, "display", 0, &display_name },
750 parse_options(wayland_options,
751 ARRAY_LENGTH(wayland_options), argc, argv);
753 return wayland_compositor_create(display, width, height, display_name);