2 * Copyright © 2008 Kristian Høgsberg
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <sys/ioctl.h>
32 #include <gdk-pixbuf/gdk-pixbuf.h>
34 #include <linux/input.h>
36 #include "wayland-server.h"
37 #include "compositor.h"
39 /* The plan here is to generate a random anonymous socket name and
40 * advertise that through a service on the session dbus.
42 static const char *option_socket_name = NULL;
43 static const char *option_background = "background.jpg";
44 static const char *option_geometry = "1024x640";
45 static int option_idle_time = 5;
46 static int option_connector = 0;
48 static const GOptionEntry option_entries[] = {
49 { "background", 'b', 0, G_OPTION_ARG_STRING,
50 &option_background, "Background image" },
51 { "connector", 'c', 0, G_OPTION_ARG_INT,
52 &option_connector, "KMS connector" },
53 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
54 &option_geometry, "Geometry" },
55 { "socket", 's', 0, G_OPTION_ARG_STRING,
56 &option_socket_name, "Socket Name" },
57 { "idle-time", 'i', 0, G_OPTION_ARG_INT,
58 &option_idle_time, "Screensaver idle time" },
63 wlsc_matrix_init(struct wlsc_matrix *matrix)
65 static const struct wlsc_matrix identity = {
66 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
69 memcpy(matrix, &identity, sizeof identity);
73 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
75 struct wlsc_matrix tmp;
76 const GLfloat *row, *column;
80 for (i = 0; i < 16; i++) {
83 row = m->d + d.quot * 4;
84 column = n->d + d.rem;
85 for (j = 0; j < 4; j++)
86 tmp.d[i] += row[j] * column[j * 4];
88 memcpy(m, &tmp, sizeof tmp);
92 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
94 struct wlsc_matrix translate = {
95 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
98 wlsc_matrix_multiply(matrix, &translate);
102 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
104 struct wlsc_matrix scale = {
105 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
108 wlsc_matrix_multiply(matrix, &scale);
112 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
115 struct wlsc_vector t;
117 for (i = 0; i < 4; i++) {
119 for (j = 0; j < 4; j++)
120 t.f[i] += v->f[j] * matrix->d[i + j * 4];
126 struct wlsc_surface *
127 wlsc_surface_create(struct wlsc_compositor *compositor,
128 int32_t x, int32_t y, int32_t width, int32_t height)
130 struct wlsc_surface *surface;
132 surface = malloc(sizeof *surface);
136 wl_list_init(&surface->surface.destroy_listener_list);
137 wl_list_init(&surface->link);
138 wl_list_init(&surface->buffer_link);
139 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
141 glGenTextures(1, &surface->texture);
142 glBindTexture(GL_TEXTURE_2D, surface->texture);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
148 surface->compositor = compositor;
149 surface->visual = NULL;
150 surface->image = EGL_NO_IMAGE_KHR;
151 surface->saved_texture = 0;
152 surface->buffer = NULL;
155 surface->width = width;
156 surface->height = height;
157 wlsc_matrix_init(&surface->matrix);
158 wlsc_matrix_scale(&surface->matrix, width, height, 1);
159 wlsc_matrix_translate(&surface->matrix, x, y, 0);
161 wlsc_matrix_init(&surface->matrix_inv);
162 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
163 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
169 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
170 int32_t x, int32_t y,
171 int32_t width, int32_t height)
173 struct wlsc_compositor *compositor = surface->compositor;
175 pixman_region32_union_rect(&compositor->damage_region,
176 &compositor->damage_region,
177 surface->x + x, surface->y + y,
179 wlsc_compositor_schedule_repaint(compositor);
183 wlsc_surface_damage(struct wlsc_surface *surface)
185 wlsc_surface_damage_rectangle(surface, 0, 0,
186 surface->width, surface->height);
194 gettimeofday(&tv, NULL);
196 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
200 destroy_surface(struct wl_resource *resource, struct wl_client *client)
202 struct wlsc_surface *surface =
203 container_of(resource, struct wlsc_surface, surface.resource);
204 struct wl_listener *l, *next;
207 wlsc_surface_damage(surface);
209 wl_list_remove(&surface->link);
210 if (surface->saved_texture == 0)
211 glDeleteTextures(1, &surface->texture);
213 glDeleteTextures(1, &surface->saved_texture);
216 if (surface->image != EGL_NO_IMAGE_KHR)
217 eglDestroyImageKHR(surface->compositor->display,
220 wl_list_remove(&surface->buffer_link);
223 wl_list_for_each_safe(l, next,
224 &surface->surface.destroy_listener_list, link)
225 l->func(l, &surface->surface, time);
231 wlsc_load_image(const char *filename, int width, int height)
234 GError *error = NULL;
235 int stride, i, n_channels;
236 unsigned char *pixels, *end, *argb_pixels, *s, *d;
238 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
242 fprintf(stderr, "failed to load image: %s\n", error->message);
247 stride = gdk_pixbuf_get_rowstride(pixbuf);
248 pixels = gdk_pixbuf_get_pixels(pixbuf);
249 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
251 argb_pixels = malloc (height * width * 4);
252 if (argb_pixels == NULL) {
253 g_object_unref(pixbuf);
257 if (n_channels == 4) {
258 for (i = 0; i < height; i++) {
259 s = pixels + i * stride;
261 d = argb_pixels + i * width * 4;
265 #define MULT(_d,c,a,t) \
266 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
268 MULT(d[0], s[2], s[3], t);
269 MULT(d[1], s[1], s[3], t);
270 MULT(d[2], s[0], s[3], t);
276 } else if (n_channels == 3) {
277 for (i = 0; i < height; i++) {
278 s = pixels + i * stride;
280 d = argb_pixels + i * width * 4;
292 g_object_unref(pixbuf);
294 return (uint32_t *) argb_pixels;
298 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
300 struct wlsc_surface *es = (struct wlsc_surface *) surface;
301 struct wlsc_compositor *ec = es->compositor;
302 struct wl_list *surfaces_attached_to;
304 if (es->saved_texture != 0)
305 es->texture = es->saved_texture;
307 glBindTexture(GL_TEXTURE_2D, es->texture);
309 if (wl_buffer_is_shm(buffer)) {
310 /* Unbind any EGLImage texture that may be bound, so we don't
312 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
313 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
314 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
315 buffer->width, buffer->height, 0,
316 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
317 wl_shm_buffer_get_data(buffer));
318 es->visual = buffer->visual;
320 surfaces_attached_to = buffer->user_data;
323 wl_list_remove(&es->buffer_link);
324 wl_list_insert(surfaces_attached_to, &es->buffer_link);
326 es->image = eglCreateImageKHR(ec->display, NULL,
327 EGL_WAYLAND_BUFFER_WL,
330 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
331 es->visual = buffer->visual;
336 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
338 struct wlsc_surface *es = (struct wlsc_surface *) surface;
340 es->image = sprite->image;
341 if (sprite->image != EGL_NO_IMAGE_KHR) {
342 glBindTexture(GL_TEXTURE_2D, es->texture);
343 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
345 if (es->saved_texture == 0)
346 es->saved_texture = es->texture;
347 es->texture = sprite->texture;
350 es->visual = sprite->visual;
354 SPRITE_USE_CURSOR = (1 << 0),
357 static struct wlsc_sprite *
358 create_sprite_from_png(struct wlsc_compositor *ec,
359 const char *filename, int width, int height,
363 struct wlsc_sprite *sprite;
365 pixels = wlsc_load_image(filename, width, height);
369 sprite = malloc(sizeof *sprite);
370 if (sprite == NULL) {
375 sprite->visual = &ec->compositor.premultiplied_argb_visual;
376 sprite->width = width;
377 sprite->height = height;
378 sprite->image = EGL_NO_IMAGE_KHR;
380 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
381 sprite->image = ec->create_cursor_image(ec, width, height);
383 glGenTextures(1, &sprite->texture);
384 glBindTexture(GL_TEXTURE_2D, sprite->texture);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
388 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
390 if (sprite->image != EGL_NO_IMAGE_KHR) {
391 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
392 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
393 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
395 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
396 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
404 static const struct {
405 const char *filename;
406 int hotspot_x, hotspot_y;
407 } pointer_images[] = {
408 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
409 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
410 { DATADIR "/wayland/bottom_side.png", 16, 20 },
411 { DATADIR "/wayland/grabbing.png", 20, 17 },
412 { DATADIR "/wayland/left_ptr.png", 10, 5 },
413 { DATADIR "/wayland/left_side.png", 10, 20 },
414 { DATADIR "/wayland/right_side.png", 30, 19 },
415 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
416 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
417 { DATADIR "/wayland/top_side.png", 18, 8 },
418 { DATADIR "/wayland/xterm.png", 15, 15 }
422 create_pointer_images(struct wlsc_compositor *ec)
425 const int width = 32, height = 32;
427 count = ARRAY_LENGTH(pointer_images);
428 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
429 for (i = 0; i < count; i++) {
430 ec->pointer_sprites[i] =
431 create_sprite_from_png(ec,
432 pointer_images[i].filename,
438 static struct wlsc_surface *
439 background_create(struct wlsc_output *output, const char *filename)
441 struct wlsc_surface *background;
442 struct wlsc_sprite *sprite;
444 background = wlsc_surface_create(output->compositor,
445 output->x, output->y,
446 output->width, output->height);
447 if (background == NULL)
450 sprite = create_sprite_from_png(output->compositor, filename,
451 output->width, output->height, 0);
452 if (sprite == NULL) {
457 wlsc_sprite_attach(sprite, &background->surface);
463 wlsc_surface_draw(struct wlsc_surface *es,
464 struct wlsc_output *output, pixman_region32_t *clip)
466 struct wlsc_compositor *ec = es->compositor;
467 GLfloat *v, inv_width, inv_height;
469 pixman_region32_t repaint;
470 pixman_box32_t *rectangles;
473 pixman_region32_init_rect(&repaint,
474 es->x, es->y, es->width, es->height);
475 pixman_region32_intersect(&repaint, &repaint, clip);
476 if (!pixman_region32_not_empty(&repaint))
479 if (es->visual == &ec->compositor.argb_visual) {
480 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
482 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
483 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
489 rectangles = pixman_region32_rectangles(&repaint, &n);
490 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
491 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
492 inv_width = 1.0 / es->width;
493 inv_height = 1.0 / es->height;
494 for (i = 0; i < n; i++, v += 16, p += 6) {
495 v[ 0] = rectangles[i].x1;
496 v[ 1] = rectangles[i].y1;
497 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
498 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
500 v[ 4] = rectangles[i].x1;
501 v[ 5] = rectangles[i].y2;
503 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
505 v[ 8] = rectangles[i].x2;
506 v[ 9] = rectangles[i].y1;
507 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
510 v[12] = rectangles[i].x2;
511 v[13] = rectangles[i].y2;
523 glBindTexture(GL_TEXTURE_2D, es->texture);
524 v = ec->vertices.data;
525 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
526 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
527 glEnableVertexAttribArray(0);
528 glEnableVertexAttribArray(1);
529 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
531 ec->vertices.size = 0;
532 ec->indices.size = 0;
533 pixman_region32_fini(&repaint);
537 wlsc_surface_raise(struct wlsc_surface *surface)
539 struct wlsc_compositor *compositor = surface->compositor;
541 wl_list_remove(&surface->link);
542 wl_list_insert(&compositor->surface_list, &surface->link);
546 wlsc_surface_update_matrix(struct wlsc_surface *es)
548 wlsc_matrix_init(&es->matrix);
549 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
550 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
552 wlsc_matrix_init(&es->matrix_inv);
553 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
554 wlsc_matrix_scale(&es->matrix_inv,
555 1.0 / es->width, 1.0 / es->height, 1);
559 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
561 struct wlsc_output *output;
563 wl_list_for_each(output, &compositor->output_list, link)
564 wlsc_output_damage(output);
568 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
570 struct wlsc_compositor *compositor = output->compositor;
571 struct wlsc_surface *es;
573 wl_list_for_each(es, &compositor->surface_list, link) {
574 if (es->output == output) {
575 wl_display_post_frame(compositor->wl_display,
576 &es->surface, msecs);
580 output->finished = 1;
582 wl_event_source_timer_update(compositor->timer_source, 5);
583 compositor->repaint_on_timeout = 1;
587 wlsc_output_damage(struct wlsc_output *output)
589 struct wlsc_compositor *compositor = output->compositor;
591 pixman_region32_union_rect(&compositor->damage_region,
592 &compositor->damage_region,
593 output->x, output->y,
594 output->width, output->height);
595 wlsc_compositor_schedule_repaint(compositor);
599 wlsc_output_repaint(struct wlsc_output *output)
601 struct wlsc_compositor *ec = output->compositor;
602 struct wlsc_surface *es;
603 struct wlsc_input_device *eid;
604 pixman_region32_t new_damage, total_damage, repaint;
605 int using_hardware_cursor = 1;
607 output->prepare_render(output);
609 glViewport(0, 0, output->width, output->height);
611 glUseProgram(ec->texture_shader.program);
612 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
613 1, GL_FALSE, output->matrix.d);
614 glUniform1i(ec->texture_shader.tex_uniform, 0);
616 pixman_region32_init(&new_damage);
617 pixman_region32_init(&total_damage);
618 pixman_region32_intersect_rect(&new_damage,
620 output->x, output->y,
621 output->width, output->height);
622 pixman_region32_subtract(&ec->damage_region,
623 &ec->damage_region, &new_damage);
624 pixman_region32_union(&total_damage, &new_damage,
625 &output->previous_damage_region);
626 pixman_region32_copy(&output->previous_damage_region, &new_damage);
628 if (ec->state == WLSC_COMPOSITOR_SLEEPING) {
629 glClear(GL_COLOR_BUFFER_BIT);
634 if (output->set_hardware_cursor(output, ec->input_device) < 0)
635 using_hardware_cursor = 0;
637 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
638 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN &&
639 es->fullscreen_output == output) {
640 if (es->visual == &ec->compositor.rgb_visual &&
641 using_hardware_cursor) {
642 if (output->prepare_scanout_surface(output, es) == 0) {
643 /* We're drawing nothing now,
644 * draw the damaged regions later. */
645 pixman_region32_union(&ec->damage_region,
652 if (es->width < output->width ||
653 es->height < output->height)
654 glClear(GL_COLOR_BUFFER_BIT);
655 wlsc_surface_draw(es, output, &total_damage);
657 wl_list_for_each(es, &ec->surface_list, link) {
658 if (es->visual != &ec->compositor.rgb_visual)
661 pixman_region32_init_rect(&repaint,
663 es->width, es->height);
664 wlsc_surface_draw(es, output, &total_damage);
665 pixman_region32_subtract(&total_damage,
666 &total_damage, &repaint);
669 if (output->background)
670 wlsc_surface_draw(output->background,
671 output, &total_damage);
673 glClear(GL_COLOR_BUFFER_BIT);
675 wl_list_for_each_reverse(es, &ec->surface_list, link) {
676 if (ec->overlay == es)
679 if (es->visual == &ec->compositor.rgb_visual) {
680 pixman_region32_union_rect(&total_damage,
683 es->width, es->height);
687 wlsc_surface_draw(es, output, &total_damage);
692 wlsc_surface_draw(ec->overlay, output, &total_damage);
695 wl_list_for_each(eid, &ec->input_device_list, link) {
696 if (&eid->input_device != ec->input_device ||
697 !using_hardware_cursor)
698 wlsc_surface_draw(eid->sprite, output,
706 struct wlsc_compositor *ec = data;
707 struct wlsc_output *output;
708 int repainted_all_outputs = 1;
710 wl_list_for_each(output, &ec->output_list, link) {
711 if (!output->repaint_needed)
714 if (!output->finished) {
715 repainted_all_outputs = 0;
719 wlsc_output_repaint(output);
720 output->finished = 0;
721 output->repaint_needed = 0;
722 output->present(output);
725 if (repainted_all_outputs)
726 ec->repaint_on_timeout = 0;
728 wl_event_source_timer_update(ec->timer_source, 1);
734 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
736 struct wlsc_output *output;
738 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
741 wl_list_for_each(output, &compositor->output_list, link)
742 output->repaint_needed = 1;
744 if (compositor->repaint_on_timeout)
747 wl_event_source_timer_update(compositor->timer_source, 1);
748 compositor->repaint_on_timeout = 1;
752 surface_destroy(struct wl_client *client,
753 struct wl_surface *surface)
755 wl_resource_destroy(&surface->resource, client);
759 wlsc_surface_assign_output(struct wlsc_surface *es)
761 struct wlsc_compositor *ec = es->compositor;
762 struct wlsc_output *output;
764 struct wlsc_output *tmp = es->output;
767 wl_list_for_each(output, &ec->output_list, link) {
768 if (output->x < es->x && es->x < output->x + output->width &&
769 output->y < es->y && es->y < output->y + output->height) {
771 printf("assiging surface %p to output %p\n",
777 if (es->output == NULL) {
778 printf("no output found\n");
779 es->output = container_of(ec->output_list.next,
780 struct wlsc_output, link);
785 surface_attach(struct wl_client *client,
786 struct wl_surface *surface, struct wl_buffer *buffer,
787 int32_t x, int32_t y)
789 struct wlsc_surface *es = (struct wlsc_surface *) surface;
791 /* FIXME: This damages the entire old surface, but we should
792 * really just damage the part that's no longer covered by the
793 * surface. Anything covered by the new surface will be
794 * damaged by the client. */
795 wlsc_surface_damage(es);
797 wlsc_buffer_attach(buffer, surface);
802 es->width = buffer->width;
803 es->height = buffer->height;
804 if (x != 0 || y != 0)
805 wlsc_surface_assign_output(es);
806 wlsc_surface_update_matrix(es);
810 surface_map_toplevel(struct wl_client *client,
811 struct wl_surface *surface)
813 struct wlsc_surface *es = (struct wlsc_surface *) surface;
814 struct wlsc_compositor *ec = es->compositor;
816 switch (es->map_type) {
817 case WLSC_SURFACE_MAP_UNMAPPED:
818 es->x = 10 + random() % 400;
819 es->y = 10 + random() % 400;
820 wlsc_surface_update_matrix(es);
821 /* assign to first output */
822 es->output = container_of(ec->output_list.next,
823 struct wlsc_output, link);
824 wl_list_insert(&es->compositor->surface_list, &es->link);
826 case WLSC_SURFACE_MAP_TOPLEVEL:
828 case WLSC_SURFACE_MAP_FULLSCREEN:
829 es->fullscreen_output = NULL;
832 wlsc_surface_update_matrix(es);
838 wlsc_surface_damage(es);
839 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
843 surface_map_transient(struct wl_client *client,
844 struct wl_surface *surface, struct wl_surface *parent,
845 int x, int y, uint32_t flags)
847 struct wlsc_surface *es = (struct wlsc_surface *) surface;
848 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
850 switch (es->map_type) {
851 case WLSC_SURFACE_MAP_UNMAPPED:
852 wl_list_insert(&es->compositor->surface_list, &es->link);
853 /* assign to parents output */
854 es->output = pes->output;
856 case WLSC_SURFACE_MAP_FULLSCREEN:
857 es->fullscreen_output = NULL;
866 wlsc_surface_update_matrix(es);
867 wlsc_surface_damage(es);
868 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
872 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
874 struct wlsc_surface *es = (struct wlsc_surface *) surface;
875 struct wlsc_output *output;
877 /* FIXME: Fullscreen on first output */
878 /* FIXME: Handle output going away */
879 output = container_of(es->compositor->output_list.next,
880 struct wlsc_output, link);
882 switch (es->map_type) {
883 case WLSC_SURFACE_MAP_UNMAPPED:
884 es->x = 10 + random() % 400;
885 es->y = 10 + random() % 400;
886 /* assign to first output */
888 wl_list_insert(&es->compositor->surface_list, &es->link);
890 case WLSC_SURFACE_MAP_FULLSCREEN:
898 es->x = (output->width - es->width) / 2;
899 es->y = (output->height - es->height) / 2;
900 es->fullscreen_output = output;
901 wlsc_surface_update_matrix(es);
902 wlsc_surface_damage(es);
903 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
907 surface_damage(struct wl_client *client,
908 struct wl_surface *surface,
909 int32_t x, int32_t y, int32_t width, int32_t height)
911 struct wlsc_surface *es = (struct wlsc_surface *) surface;
913 wlsc_surface_damage_rectangle(es, x, y, width, height);
916 const static struct wl_surface_interface surface_interface = {
919 surface_map_toplevel,
920 surface_map_transient,
921 surface_map_fullscreen,
926 wlsc_input_device_attach(struct wlsc_input_device *device,
927 int x, int y, int width, int height)
929 wlsc_surface_damage(device->sprite);
931 device->hotspot_x = x;
932 device->hotspot_y = y;
934 device->sprite->x = device->input_device.x - device->hotspot_x;
935 device->sprite->y = device->input_device.y - device->hotspot_y;
936 device->sprite->width = width;
937 device->sprite->height = height;
938 wlsc_surface_update_matrix(device->sprite);
940 wlsc_surface_damage(device->sprite);
944 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
945 struct wl_buffer *buffer, int x, int y)
947 wlsc_buffer_attach(buffer, &device->sprite->surface);
948 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
952 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
953 struct wlsc_sprite *sprite, int x, int y)
955 wlsc_sprite_attach(sprite, &device->sprite->surface);
956 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
960 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
961 enum wlsc_pointer_type type)
963 struct wlsc_compositor *compositor =
964 (struct wlsc_compositor *) device->input_device.compositor;
966 wlsc_input_device_attach_sprite(device,
967 compositor->pointer_sprites[type],
968 pointer_images[type].hotspot_x,
969 pointer_images[type].hotspot_y);
973 compositor_create_surface(struct wl_client *client,
974 struct wl_compositor *compositor, uint32_t id)
976 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
977 struct wlsc_surface *surface;
979 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
980 if (surface == NULL) {
981 wl_client_post_no_memory(client);
985 surface->surface.resource.destroy = destroy_surface;
987 surface->surface.resource.object.id = id;
988 surface->surface.resource.object.interface = &wl_surface_interface;
989 surface->surface.resource.object.implementation =
990 (void (**)(void)) &surface_interface;
991 surface->surface.client = client;
993 wl_client_add_resource(client, &surface->surface.resource);
996 const static struct wl_compositor_interface compositor_interface = {
997 compositor_create_surface,
1001 wlsc_surface_transform(struct wlsc_surface *surface,
1002 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1004 struct wlsc_vector v = { { x, y, 0, 1 } };
1006 wlsc_matrix_transform(&surface->matrix_inv, &v);
1007 *sx = v.f[0] * surface->width;
1008 *sy = v.f[1] * surface->height;
1011 struct wlsc_surface *
1012 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1014 struct wlsc_compositor *ec =
1015 (struct wlsc_compositor *) device->compositor;
1016 struct wlsc_surface *es;
1018 wl_list_for_each(es, &ec->surface_list, link) {
1019 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1020 if (0 <= *sx && *sx < es->width &&
1021 0 <= *sy && *sy < es->height)
1030 motion_grab_motion(struct wl_grab *grab,
1031 uint32_t time, int32_t x, int32_t y)
1033 struct wlsc_input_device *device =
1034 (struct wlsc_input_device *) grab->input_device;
1035 struct wlsc_surface *es =
1036 (struct wlsc_surface *) device->input_device.pointer_focus;
1039 wlsc_surface_transform(es, x, y, &sx, &sy);
1040 wl_client_post_event(es->surface.client,
1041 &device->input_device.object,
1042 WL_INPUT_DEVICE_MOTION,
1043 time, x, y, sx, sy);
1047 motion_grab_button(struct wl_grab *grab,
1048 uint32_t time, int32_t button, int32_t state)
1050 wl_client_post_event(grab->input_device->pointer_focus->client,
1051 &grab->input_device->object,
1052 WL_INPUT_DEVICE_BUTTON,
1053 time, button, state);
1057 motion_grab_end(struct wl_grab *grab, uint32_t time)
1061 static const struct wl_grab_interface motion_grab_interface = {
1068 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1070 if (compositor->idle_inhibit)
1073 if (compositor->state == WLSC_COMPOSITOR_SLEEPING) {
1074 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1075 wlsc_compositor_damage_all(compositor);
1078 wl_event_source_timer_update(compositor->idle_source,
1079 option_idle_time * 1000);
1083 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1085 wlsc_compositor_wake(compositor);
1086 compositor->idle_inhibit++;
1090 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1092 compositor->idle_inhibit--;
1093 wlsc_compositor_wake(compositor);
1097 idle_handler(void *data)
1099 struct wlsc_compositor *compositor = data;
1101 if (compositor->idle_inhibit)
1104 wlsc_compositor_damage_all(compositor);
1105 compositor->state = WLSC_COMPOSITOR_SLEEPING;
1111 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1113 struct wlsc_surface *es;
1114 struct wlsc_compositor *ec =
1115 (struct wlsc_compositor *) device->compositor;
1116 struct wlsc_output *output;
1117 const struct wl_grab_interface *interface;
1118 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1120 int x_valid = 0, y_valid = 0;
1121 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1123 wlsc_compositor_wake(ec);
1125 wl_list_for_each(output, &ec->output_list, link) {
1126 if (output->x <= x && x <= output->x + output->width)
1129 if (output->y <= y && y <= output->y + output->height)
1132 /* FIXME: calculate this only on output addition/deletion */
1133 if (output->x < min_x)
1135 if (output->y < min_y)
1138 if (output->x + output->width > max_x)
1139 max_x = output->x + output->width;
1140 if (output->y + output->height > max_y)
1141 max_y = output->y + output->height;
1147 else if (x >= max_x)
1153 else if (y >= max_y)
1161 interface = device->grab->interface;
1162 interface->motion(device->grab, time, x, y);
1164 es = pick_surface(device, &sx, &sy);
1165 wl_input_device_set_pointer_focus(device,
1167 time, x, y, sx, sy);
1169 wl_client_post_event(es->surface.client,
1171 WL_INPUT_DEVICE_MOTION,
1172 time, x, y, sx, sy);
1175 wlsc_surface_damage(wd->sprite);
1177 wd->sprite->x = device->x - wd->hotspot_x;
1178 wd->sprite->y = device->y - wd->hotspot_y;
1179 wlsc_surface_update_matrix(wd->sprite);
1181 wlsc_surface_damage(wd->sprite);
1185 wlsc_surface_activate(struct wlsc_surface *surface,
1186 struct wlsc_input_device *device, uint32_t time)
1188 wlsc_surface_raise(surface);
1189 if (device->selection)
1190 wlsc_selection_set_focus(device->selection,
1191 &surface->surface, time);
1193 wl_input_device_set_keyboard_focus(&device->input_device,
1198 struct wlsc_binding {
1202 wlsc_binding_handler_t handler;
1204 struct wl_list link;
1208 notify_button(struct wl_input_device *device,
1209 uint32_t time, int32_t button, int32_t state)
1211 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1212 struct wlsc_compositor *compositor =
1213 (struct wlsc_compositor *) device->compositor;
1214 struct wlsc_binding *b;
1215 struct wlsc_surface *surface =
1216 (struct wlsc_surface *) device->pointer_focus;
1219 wlsc_compositor_idle_inhibit(compositor);
1221 wlsc_compositor_idle_release(compositor);
1223 if (state && surface && device->grab == NULL) {
1224 wlsc_surface_activate(surface, wd, time);
1225 wl_input_device_start_grab(device,
1226 &device->motion_grab,
1230 wl_list_for_each(b, &compositor->binding_list, link) {
1231 if (b->button == button &&
1232 b->modifier == wd->modifier_state && state) {
1233 b->handler(&wd->input_device,
1234 time, 0, button, state, b->data);
1240 device->grab->interface->button(device->grab, time,
1243 if (!state && device->grab && device->grab_button == button)
1244 wl_input_device_end_grab(device, time);
1248 terminate_binding(struct wl_input_device *device, uint32_t time,
1249 uint32_t key, uint32_t button, uint32_t state, void *data)
1251 struct wlsc_compositor *compositor = data;
1254 wl_display_terminate(compositor->wl_display);
1257 struct wlsc_binding *
1258 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1259 uint32_t key, uint32_t button, uint32_t modifier,
1260 wlsc_binding_handler_t handler, void *data)
1262 struct wlsc_binding *binding;
1264 binding = malloc(sizeof *binding);
1265 if (binding == NULL)
1269 binding->button = button;
1270 binding->modifier = modifier;
1271 binding->handler = handler;
1272 binding->data = data;
1273 wl_list_insert(compositor->binding_list.prev, &binding->link);
1279 wlsc_binding_destroy(struct wlsc_binding *binding)
1281 wl_list_remove(&binding->link);
1286 update_modifier_state(struct wlsc_input_device *device,
1287 uint32_t key, uint32_t state)
1294 modifier = MODIFIER_CTRL;
1299 modifier = MODIFIER_ALT;
1304 modifier = MODIFIER_SUPER;
1313 device->modifier_state |= modifier;
1315 device->modifier_state &= ~modifier;
1319 notify_key(struct wl_input_device *device,
1320 uint32_t time, uint32_t key, uint32_t state)
1322 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1323 struct wlsc_compositor *compositor =
1324 (struct wlsc_compositor *) device->compositor;
1326 struct wlsc_binding *b;
1329 wlsc_compositor_idle_inhibit(compositor);
1331 wlsc_compositor_idle_release(compositor);
1333 wl_list_for_each(b, &compositor->binding_list, link) {
1334 if (b->key == key &&
1335 b->modifier == wd->modifier_state) {
1336 b->handler(&wd->input_device,
1337 time, key, 0, state, b->data);
1342 update_modifier_state(wd, key, state);
1343 end = device->keys.data + device->keys.size;
1344 for (k = device->keys.data; k < end; k++) {
1348 device->keys.size = (void *) end - device->keys.data;
1350 k = wl_array_add(&device->keys, sizeof *k);
1354 if (device->keyboard_focus != NULL)
1355 wl_client_post_event(device->keyboard_focus->client,
1357 WL_INPUT_DEVICE_KEY, time, key, state);
1361 notify_pointer_focus(struct wl_input_device *device,
1362 uint32_t time, struct wlsc_output *output,
1363 int32_t x, int32_t y)
1365 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1366 struct wlsc_compositor *compositor =
1367 (struct wlsc_compositor *) device->compositor;
1368 struct wlsc_surface *es;
1374 es = pick_surface(device, &sx, &sy);
1375 wl_input_device_set_pointer_focus(device,
1377 time, x, y, sx, sy);
1379 compositor->focus = 1;
1381 wd->sprite->x = device->x - wd->hotspot_x;
1382 wd->sprite->y = device->y - wd->hotspot_y;
1383 wlsc_surface_update_matrix(wd->sprite);
1385 wl_input_device_set_pointer_focus(device, NULL,
1387 compositor->focus = 0;
1390 wlsc_surface_damage(wd->sprite);
1394 notify_keyboard_focus(struct wl_input_device *device,
1395 uint32_t time, struct wlsc_output *output,
1396 struct wl_array *keys)
1398 struct wlsc_input_device *wd =
1399 (struct wlsc_input_device *) device;
1400 struct wlsc_compositor *compositor =
1401 (struct wlsc_compositor *) device->compositor;
1402 struct wlsc_surface *es;
1405 if (!wl_list_empty(&compositor->surface_list))
1406 es = container_of(compositor->surface_list.next,
1407 struct wlsc_surface, link);
1412 wl_array_copy(&wd->input_device.keys, keys);
1413 wd->modifier_state = 0;
1414 end = device->keys.data + device->keys.size;
1415 for (k = device->keys.data; k < end; k++) {
1416 wlsc_compositor_idle_inhibit(compositor);
1417 update_modifier_state(wd, *k, 1);
1420 wl_input_device_set_keyboard_focus(&wd->input_device,
1421 &es->surface, time);
1423 end = device->keys.data + device->keys.size;
1424 for (k = device->keys.data; k < end; k++)
1425 wlsc_compositor_idle_release(compositor);
1427 wd->modifier_state = 0;
1428 wl_input_device_set_keyboard_focus(&wd->input_device,
1435 input_device_attach(struct wl_client *client,
1436 struct wl_input_device *device_base,
1438 struct wl_buffer *buffer, int32_t x, int32_t y)
1440 struct wlsc_input_device *device =
1441 (struct wlsc_input_device *) device_base;
1443 if (time < device->input_device.pointer_focus_time)
1445 if (device->input_device.pointer_focus == NULL)
1447 if (device->input_device.pointer_focus->client != client)
1450 if (buffer == NULL) {
1451 wlsc_input_device_set_pointer_image(device,
1452 WLSC_POINTER_LEFT_PTR);
1456 wlsc_input_device_attach_buffer(device, buffer, x, y);
1459 const static struct wl_input_device_interface input_device_interface = {
1460 input_device_attach,
1464 wlsc_input_device_init(struct wlsc_input_device *device,
1465 struct wlsc_compositor *ec)
1467 wl_input_device_init(&device->input_device, &ec->compositor);
1469 device->input_device.object.interface = &wl_input_device_interface;
1470 device->input_device.object.implementation =
1471 (void (**)(void)) &input_device_interface;
1472 wl_display_add_object(ec->wl_display, &device->input_device.object);
1473 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1475 device->sprite = wlsc_surface_create(ec,
1476 device->input_device.x,
1477 device->input_device.y, 32, 32);
1478 device->hotspot_x = 16;
1479 device->hotspot_y = 16;
1480 device->modifier_state = 0;
1482 device->input_device.motion_grab.interface = &motion_grab_interface;
1484 wl_list_insert(ec->input_device_list.prev, &device->link);
1486 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1490 wlsc_output_post_geometry(struct wl_client *client,
1491 struct wl_object *global, uint32_t version)
1493 struct wlsc_output *output =
1494 container_of(global, struct wlsc_output, object);
1496 wl_client_post_event(client, global,
1498 output->x, output->y,
1499 output->width, output->height);
1502 static const char vertex_shader[] =
1503 "uniform mat4 proj;\n"
1504 "attribute vec2 position;\n"
1505 "attribute vec2 texcoord;\n"
1506 "varying vec2 v_texcoord;\n"
1509 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1510 " v_texcoord = texcoord;\n"
1513 static const char texture_fragment_shader[] =
1514 "precision mediump float;\n"
1515 "varying vec2 v_texcoord;\n"
1516 "uniform sampler2D tex;\n"
1519 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1523 compile_shader(GLenum type, const char *source)
1529 s = glCreateShader(type);
1530 glShaderSource(s, 1, &source, NULL);
1532 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1534 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1535 fprintf(stderr, "shader info: %s\n", msg);
1543 wlsc_shader_init(struct wlsc_shader *shader,
1544 const char *vertex_source, const char *fragment_source)
1549 shader->vertex_shader =
1550 compile_shader(GL_VERTEX_SHADER, vertex_source);
1551 shader->fragment_shader =
1552 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1554 shader->program = glCreateProgram();
1555 glAttachShader(shader->program, shader->vertex_shader);
1556 glAttachShader(shader->program, shader->fragment_shader);
1557 glBindAttribLocation(shader->program, 0, "position");
1558 glBindAttribLocation(shader->program, 1, "texcoord");
1560 glLinkProgram(shader->program);
1561 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1563 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1564 fprintf(stderr, "link info: %s\n", msg);
1568 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1569 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1575 wlsc_output_destroy(struct wlsc_output *output)
1577 destroy_surface(&output->background->surface.resource, NULL);
1581 wlsc_output_move(struct wlsc_output *output, int x, int y)
1583 struct wlsc_compositor *c = output->compositor;
1589 if (output->background) {
1590 output->background->x = x;
1591 output->background->y = y;
1592 wlsc_surface_update_matrix(output->background);
1595 pixman_region32_init(&output->previous_damage_region);
1597 wlsc_matrix_init(&output->matrix);
1598 wlsc_matrix_translate(&output->matrix,
1599 -output->x - output->width / 2.0,
1600 -output->y - output->height / 2.0, 0);
1602 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1603 wlsc_matrix_scale(&output->matrix,
1604 2.0 / output->width,
1605 flip * 2.0 / output->height, 1);
1607 pixman_region32_union_rect(&c->damage_region,
1609 x, y, output->width, output->height);
1613 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1614 int x, int y, int width, int height, uint32_t flags)
1616 output->compositor = c;
1619 output->width = width;
1620 output->height = height;
1622 output->background =
1623 background_create(output, option_background);
1625 output->flags = flags;
1626 output->finished = 1;
1627 wlsc_output_move(output, x, y);
1629 output->object.interface = &wl_output_interface;
1630 wl_display_add_object(c->wl_display, &output->object);
1631 wl_display_add_global(c->wl_display, &output->object,
1632 wlsc_output_post_geometry);
1636 shm_buffer_created(struct wl_buffer *buffer)
1638 struct wl_list *surfaces_attached_to;
1640 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1641 if (!surfaces_attached_to) {
1642 buffer->user_data = NULL;
1646 wl_list_init(surfaces_attached_to);
1648 buffer->user_data = surfaces_attached_to;
1652 shm_buffer_damaged(struct wl_buffer *buffer,
1653 int32_t x, int32_t y, int32_t width, int32_t height)
1655 struct wl_list *surfaces_attached_to = buffer->user_data;
1656 struct wlsc_surface *es;
1658 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1659 glBindTexture(GL_TEXTURE_2D, es->texture);
1660 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1661 buffer->width, buffer->height, 0,
1662 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1663 wl_shm_buffer_get_data(buffer));
1664 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1665 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1670 shm_buffer_destroyed(struct wl_buffer *buffer)
1672 struct wl_list *surfaces_attached_to = buffer->user_data;
1673 struct wlsc_surface *es, *next;
1675 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1677 wl_list_remove(&es->buffer_link);
1680 free(surfaces_attached_to);
1683 const static struct wl_shm_callbacks shm_callbacks = {
1686 shm_buffer_destroyed
1690 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1692 struct wl_event_loop *loop;
1693 const char *extensions;
1695 ec->wl_display = display;
1697 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1699 ec->shm = wl_shm_init(display, &shm_callbacks);
1700 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
1702 wl_list_init(&ec->surface_list);
1703 wl_list_init(&ec->input_device_list);
1704 wl_list_init(&ec->output_list);
1705 wl_list_init(&ec->binding_list);
1707 wlsc_shell_init(ec);
1708 wlsc_switcher_init(ec);
1710 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1711 MODIFIER_CTRL | MODIFIER_ALT,
1712 terminate_binding, ec);
1714 create_pointer_images(ec);
1716 screenshooter_create(ec);
1718 extensions = (const char *) glGetString(GL_EXTENSIONS);
1719 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1721 "GL_EXT_texture_format_BGRA8888 not available\n");
1725 glActiveTexture(GL_TEXTURE0);
1727 if (wlsc_shader_init(&ec->texture_shader,
1728 vertex_shader, texture_fragment_shader) < 0)
1731 loop = wl_display_get_event_loop(ec->wl_display);
1732 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1733 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1735 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1736 pixman_region32_init(&ec->damage_region);
1737 wlsc_compositor_schedule_repaint(ec);
1742 static int on_term_signal(int signal_number, void *data)
1744 struct wlsc_compositor *ec = data;
1746 wl_display_terminate(ec->wl_display);
1751 int main(int argc, char *argv[])
1753 struct wl_display *display;
1754 struct wlsc_compositor *ec;
1755 struct wl_event_loop *loop;
1756 GError *error = NULL;
1757 GOptionContext *context;
1760 g_type_init(); /* GdkPixbuf needs this, it seems. */
1762 context = g_option_context_new(NULL);
1763 g_option_context_add_main_entries(context, option_entries, "Wayland");
1764 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1765 fprintf(stderr, "option parsing failed: %s\n", error->message);
1768 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1769 fprintf(stderr, "invalid geometry option: %s \n",
1774 display = wl_display_create();
1778 #if BUILD_WAYLAND_COMPOSITOR
1779 if (getenv("WAYLAND_DISPLAY"))
1780 ec = wayland_compositor_create(display, width, height);
1783 #if BUILD_X11_COMPOSITOR
1784 if (ec == NULL && getenv("DISPLAY"))
1785 ec = x11_compositor_create(display, width, height);
1788 #if BUILD_OPENWFD_COMPOSITOR
1789 if (ec == NULL && getenv("OPENWFD"))
1790 ec = wfd_compositor_create(display, option_connector);
1793 #if BUILD_DRM_COMPOSITOR
1795 ec = drm_compositor_create(display, option_connector);
1799 fprintf(stderr, "failed to create compositor\n");
1803 if (wl_display_add_socket(display, option_socket_name)) {
1804 fprintf(stderr, "failed to add socket: %m\n");
1808 loop = wl_display_get_event_loop(ec->wl_display);
1809 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1810 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1812 wl_display_run(display);
1814 eglUnbindWaylandDisplayWL(ec->display, display);
1815 wl_display_destroy(display);