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 uint32_t *pixels, stride;
211 pixels = weston_load_image(DATADIR "/weston/border.png",
213 &c->border.height, &stride);
215 fprintf(stderr, "could'nt load border image\n");
219 glGenTextures(1, &c->border.texture);
220 glBindTexture(GL_TEXTURE_2D, c->border.texture);
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
226 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
229 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
232 c->border.bottom = 50;
234 c->border.right = 25;
238 wayland_input_create(struct wayland_compositor *c)
240 struct weston_input_device *input;
242 input = malloc(sizeof *input);
246 memset(input, 0, sizeof *input);
247 weston_input_device_init(input, &c->base);
249 c->base.input_device = &input->input_device;
255 wayland_compositor_init_egl(struct wayland_compositor *c)
259 const char *extensions;
260 EGLint config_attribs[] = {
261 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
267 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
270 static const EGLint context_attribs[] = {
271 EGL_CONTEXT_CLIENT_VERSION, 2,
275 c->base.display = eglGetDisplay(c->parent.display);
276 if (c->base.display == NULL) {
277 fprintf(stderr, "failed to create display\n");
281 if (!eglInitialize(c->base.display, &major, &minor)) {
282 fprintf(stderr, "failed to initialize display\n");
286 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
287 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
288 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
292 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
293 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
296 if (!eglChooseConfig(c->base.display, config_attribs,
297 &c->base.config, 1, &n) || n == 0) {
298 fprintf(stderr, "failed to choose config: %d\n", n);
302 c->base.context = eglCreateContext(c->base.display, c->base.config,
303 EGL_NO_CONTEXT, context_attribs);
304 if (c->base.context == NULL) {
305 fprintf(stderr, "failed to create context\n");
309 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
310 EGL_NO_SURFACE, c->base.context)) {
311 fprintf(stderr, "failed to make context current\n");
319 frame_done(void *data, struct wl_callback *callback, uint32_t time)
321 struct weston_output *output = data;
323 wl_callback_destroy(callback);
324 weston_output_finish_frame(output, time);
327 static const struct wl_callback_listener frame_listener = {
332 wayland_output_repaint(struct weston_output *output_base)
334 struct wayland_output *output = (struct wayland_output *) output_base;
335 struct wayland_compositor *compositor =
336 (struct wayland_compositor *) output->base.compositor;
337 struct wl_callback *callback;
338 struct weston_surface *surface;
340 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
341 output->egl_surface, compositor->base.context)) {
342 fprintf(stderr, "failed to make current\n");
346 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
347 weston_surface_draw(surface, &output->base);
351 eglSwapBuffers(compositor->base.display, output->egl_surface);
352 callback = wl_surface_frame(output->parent.surface);
353 wl_callback_add_listener(callback, &frame_listener, output);
359 wayland_output_set_cursor(struct weston_output *output_base,
360 struct weston_input_device *input)
366 wayland_output_destroy(struct weston_output *output_base)
368 struct wayland_output *output = (struct wayland_output *) output_base;
369 struct weston_compositor *ec = output->base.compositor;
371 eglDestroySurface(ec->display, output->egl_surface);
372 wl_egl_window_destroy(output->parent.egl_window);
379 wayland_compositor_create_output(struct wayland_compositor *c,
380 int width, int height)
382 struct wayland_output *output;
384 output = malloc(sizeof *output);
387 memset(output, 0, sizeof *output);
390 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
391 output->mode.width = width;
392 output->mode.height = height;
393 output->mode.refresh = 60;
394 wl_list_init(&output->base.mode_list);
395 wl_list_insert(&output->base.mode_list, &output->mode.link);
397 output->base.current = &output->mode;
398 weston_output_init(&output->base, &c->base, 0, 0, width, height,
401 output->base.border.top = c->border.top;
402 output->base.border.bottom = c->border.bottom;
403 output->base.border.left = c->border.left;
404 output->base.border.right = c->border.right;
406 weston_output_move(&output->base, 0, 0);
408 output->parent.surface =
409 wl_compositor_create_surface(c->parent.compositor);
410 wl_surface_set_user_data(output->parent.surface, output);
412 output->parent.egl_window =
413 wl_egl_window_create(output->parent.surface,
414 width + c->border.left + c->border.right,
415 height + c->border.top + c->border.bottom);
416 if (!output->parent.egl_window) {
417 fprintf(stderr, "failure to create wl_egl_window\n");
421 output->egl_surface =
422 eglCreateWindowSurface(c->base.display, c->base.config,
423 output->parent.egl_window, NULL);
424 if (!output->egl_surface) {
425 fprintf(stderr, "failed to create window surface\n");
429 if (!eglMakeCurrent(c->base.display, output->egl_surface,
430 output->egl_surface, c->base.context)) {
431 fprintf(stderr, "failed to make surface current\n");
432 goto cleanup_surface;
436 output->parent.shell_surface =
437 wl_shell_get_shell_surface(c->parent.shell,
438 output->parent.surface);
439 /* FIXME: add shell_surface listener for resizing */
440 wl_shell_surface_set_toplevel(output->parent.shell_surface);
442 output->base.repaint = wayland_output_repaint;
443 output->base.set_hardware_cursor = wayland_output_set_cursor;
444 output->base.destroy = wayland_output_destroy;
446 wl_list_insert(c->base.output_list.prev, &output->base.link);
451 eglDestroySurface(c->base.display, output->egl_surface);
453 wl_egl_window_destroy(output->parent.egl_window);
455 /* FIXME: cleanup weston_output */
461 /* Events received from the wayland-server this compositor is client of: */
463 /* parent output interface */
465 display_handle_geometry(void *data,
466 struct wl_output *wl_output,
475 struct wayland_compositor *c = data;
477 c->parent.screen_allocation.x = x;
478 c->parent.screen_allocation.y = y;
482 display_handle_mode(void *data,
483 struct wl_output *wl_output,
489 struct wayland_compositor *c = data;
491 c->parent.screen_allocation.width = width;
492 c->parent.screen_allocation.height = height;
495 static const struct wl_output_listener output_listener = {
496 display_handle_geometry,
500 /* parent input interface */
502 input_handle_motion(void *data, struct wl_input_device *input_device,
503 uint32_t time, int32_t sx, int32_t sy)
505 struct wayland_input *input = data;
506 struct wayland_compositor *c = input->compositor;
508 notify_motion(c->base.input_device, time,
509 sx - c->border.left, sy - c->border.top);
513 input_handle_button(void *data,
514 struct wl_input_device *input_device,
515 uint32_t time, uint32_t button, uint32_t state)
517 struct wayland_input *input = data;
518 struct wayland_compositor *c = input->compositor;
520 notify_button(c->base.input_device, time, button, state);
524 input_handle_key(void *data, struct wl_input_device *input_device,
525 uint32_t time, uint32_t key, uint32_t state)
527 struct wayland_input *input = data;
528 struct wayland_compositor *c = input->compositor;
530 notify_key(c->base.input_device, time, key, state);
534 input_handle_pointer_focus(void *data,
535 struct wl_input_device *input_device,
536 uint32_t time, struct wl_surface *surface,
537 int32_t sx, int32_t sy)
539 struct wayland_input *input = data;
540 struct wayland_output *output;
541 struct wayland_compositor *c = input->compositor;
544 output = wl_surface_get_user_data(surface);
545 notify_pointer_focus(c->base.input_device,
546 time, &output->base, sx, sy);
548 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
550 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
555 input_handle_keyboard_focus(void *data,
556 struct wl_input_device *input_device,
558 struct wl_surface *surface,
559 struct wl_array *keys)
561 struct wayland_input *input = data;
562 struct wayland_compositor *c = input->compositor;
563 struct wayland_output *output;
566 output = wl_surface_get_user_data(surface);
567 notify_keyboard_focus(c->base.input_device,
568 time, &output->base, keys);
570 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
574 static const struct wl_input_device_listener input_device_listener = {
578 input_handle_pointer_focus,
579 input_handle_keyboard_focus,
583 display_add_input(struct wayland_compositor *c, uint32_t id)
585 struct wayland_input *input;
587 input = malloc(sizeof *input);
591 memset(input, 0, sizeof *input);
593 input->compositor = c;
594 input->input_device = wl_display_bind(c->parent.display,
595 id, &wl_input_device_interface);
596 wl_list_insert(c->input_list.prev, &input->link);
598 wl_input_device_add_listener(input->input_device,
599 &input_device_listener, input);
600 wl_input_device_set_user_data(input->input_device, input);
604 display_handle_global(struct wl_display *display, uint32_t id,
605 const char *interface, uint32_t version, void *data)
607 struct wayland_compositor *c = data;
609 if (strcmp(interface, "wl_compositor") == 0) {
610 c->parent.compositor =
611 wl_display_bind(display, id, &wl_compositor_interface);
612 } else if (strcmp(interface, "wl_output") == 0) {
614 wl_display_bind(display, id, &wl_output_interface);
615 wl_output_add_listener(c->parent.output, &output_listener, c);
616 } else if (strcmp(interface, "wl_input_device") == 0) {
617 display_add_input(c, id);
618 } else if (strcmp(interface, "wl_shell") == 0) {
620 wl_display_bind(display, id, &wl_shell_interface);
625 update_event_mask(uint32_t mask, void *data)
627 struct wayland_compositor *c = data;
629 c->parent.event_mask = mask;
630 if (c->parent.wl_source)
631 wl_event_source_fd_update(c->parent.wl_source, mask);
637 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
639 struct wayland_compositor *c = data;
641 if (mask & WL_EVENT_READABLE)
642 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
643 if (mask & WL_EVENT_WRITABLE)
644 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
650 wayland_destroy(struct weston_compositor *ec)
652 weston_compositor_shutdown(ec);
657 static struct weston_compositor *
658 wayland_compositor_create(struct wl_display *display,
659 int width, int height, const char *display_name)
661 struct wayland_compositor *c;
662 struct wl_event_loop *loop;
665 c = malloc(sizeof *c);
669 memset(c, 0, sizeof *c);
671 c->parent.display = wl_display_connect(display_name);
673 if (c->parent.display == NULL) {
674 fprintf(stderr, "failed to create display: %m\n");
678 wl_list_init(&c->input_list);
679 wl_display_add_global_listener(c->parent.display,
680 display_handle_global, c);
682 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
684 c->base.wl_display = display;
685 if (wayland_compositor_init_egl(c) < 0)
688 c->base.destroy = wayland_destroy;
690 /* Can't init base class until we have a current egl context */
691 if (weston_compositor_init(&c->base, display) < 0)
695 if (wayland_compositor_create_output(c, width, height) < 0)
698 if (wayland_input_create(c) < 0)
701 loop = wl_display_get_event_loop(c->base.wl_display);
703 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
704 c->parent.wl_source =
705 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
706 wayland_compositor_handle_event, c);
707 if (c->parent.wl_source == NULL)
713 struct weston_compositor *
714 backend_init(struct wl_display *display, char *options);
716 WL_EXPORT struct weston_compositor *
717 backend_init(struct wl_display *display, char *options)
719 int width = 1024, height = 640, i;
720 char *p, *value, *display_name = NULL;
722 static char * const tokens[] = { "width", "height", "display", NULL };
725 while (i = getsubopt(&p, tokens, &value), i != -1) {
728 width = strtol(value, NULL, 0);
731 height = strtol(value, NULL, 0);
734 display_name = strdup(value);
739 return wayland_compositor_create(display, width, height, display_name);