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>
37 #include "wayland-server.h"
38 #include "compositor.h"
40 /* The plan here is to generate a random anonymous socket name and
41 * advertise that through a service on the session dbus.
43 static const char *option_socket_name = NULL;
44 static const char *option_background = "background.jpg";
45 static const char *option_geometry = "1024x640";
46 static const char *option_shell = NULL;
47 static int option_idle_time = 5;
48 static int option_connector = 0;
50 static const GOptionEntry option_entries[] = {
51 { "background", 'b', 0, G_OPTION_ARG_STRING,
52 &option_background, "Background image" },
53 { "connector", 'c', 0, G_OPTION_ARG_INT,
54 &option_connector, "KMS connector" },
55 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
56 &option_geometry, "Geometry" },
57 { "socket", 's', 0, G_OPTION_ARG_STRING,
58 &option_socket_name, "Socket Name" },
59 { "idle-time", 'i', 0, G_OPTION_ARG_INT,
60 &option_idle_time, "Screensaver idle time" },
61 { "shell", 'i', 0, G_OPTION_ARG_STRING,
62 &option_shell, "Shell module" },
67 wlsc_matrix_init(struct wlsc_matrix *matrix)
69 static const struct wlsc_matrix identity = {
70 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
73 memcpy(matrix, &identity, sizeof identity);
77 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
79 struct wlsc_matrix tmp;
80 const GLfloat *row, *column;
84 for (i = 0; i < 16; i++) {
87 row = m->d + d.quot * 4;
88 column = n->d + d.rem;
89 for (j = 0; j < 4; j++)
90 tmp.d[i] += row[j] * column[j * 4];
92 memcpy(m, &tmp, sizeof tmp);
96 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
98 struct wlsc_matrix translate = {
99 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
102 wlsc_matrix_multiply(matrix, &translate);
106 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
108 struct wlsc_matrix scale = {
109 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
112 wlsc_matrix_multiply(matrix, &scale);
116 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
119 struct wlsc_vector t;
121 for (i = 0; i < 4; i++) {
123 for (j = 0; j < 4; j++)
124 t.f[i] += v->f[j] * matrix->d[i + j * 4];
131 wlsc_tweener_init(struct wlsc_tweener *tweener,
132 double k, double current, double target)
135 tweener->current = current;
136 tweener->previous = current;
137 tweener->target = target;
141 wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
143 double force, current, step;
145 step = (msec - tweener->timestamp) / 100.0;
146 tweener->timestamp = msec;
148 current = tweener->current;
149 force = tweener->k * (tweener->target - current) / 10.0 +
150 (tweener->previous - current);
153 current + (current - tweener->previous) + force * step * step;
154 tweener->previous = current;
156 if (tweener->current >= 1.0) {
157 tweener->current = 1.0;
158 tweener->previous = 1.0;
161 if (tweener->current <= 0.0) {
162 tweener->current = 0.0;
163 tweener->previous = 0.0;
168 wlsc_tweener_done(struct wlsc_tweener *tweener)
170 return fabs(tweener->previous - tweener->target) < 0.0002 &&
171 fabs(tweener->current - tweener->target) < 0.0002;
174 WL_EXPORT struct wlsc_surface *
175 wlsc_surface_create(struct wlsc_compositor *compositor,
176 int32_t x, int32_t y, int32_t width, int32_t height)
178 struct wlsc_surface *surface;
180 surface = malloc(sizeof *surface);
184 wl_list_init(&surface->surface.destroy_listener_list);
185 wl_list_init(&surface->link);
186 wl_list_init(&surface->buffer_link);
187 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
189 glGenTextures(1, &surface->texture);
190 glBindTexture(GL_TEXTURE_2D, surface->texture);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
196 surface->compositor = compositor;
197 surface->visual = NULL;
198 surface->image = EGL_NO_IMAGE_KHR;
199 surface->saved_texture = 0;
200 surface->buffer = NULL;
203 surface->width = width;
204 surface->height = height;
205 wlsc_matrix_init(&surface->matrix);
206 wlsc_matrix_scale(&surface->matrix, width, height, 1);
207 wlsc_matrix_translate(&surface->matrix, x, y, 0);
209 wlsc_matrix_init(&surface->matrix_inv);
210 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
211 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
217 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
218 int32_t x, int32_t y,
219 int32_t width, int32_t height)
221 struct wlsc_compositor *compositor = surface->compositor;
223 pixman_region32_union_rect(&compositor->damage_region,
224 &compositor->damage_region,
225 surface->x + x, surface->y + y,
227 wlsc_compositor_schedule_repaint(compositor);
231 wlsc_surface_damage(struct wlsc_surface *surface)
233 wlsc_surface_damage_rectangle(surface, 0, 0,
234 surface->width, surface->height);
238 wlsc_compositor_get_time(void)
242 gettimeofday(&tv, NULL);
244 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
248 destroy_surface(struct wl_resource *resource, struct wl_client *client)
250 struct wlsc_surface *surface =
251 container_of(resource, struct wlsc_surface, surface.resource);
252 struct wl_listener *l, *next;
255 wlsc_surface_damage(surface);
257 wl_list_remove(&surface->link);
258 if (surface->saved_texture == 0)
259 glDeleteTextures(1, &surface->texture);
261 glDeleteTextures(1, &surface->saved_texture);
264 if (surface->image != EGL_NO_IMAGE_KHR)
265 eglDestroyImageKHR(surface->compositor->display,
268 wl_list_remove(&surface->buffer_link);
270 time = wlsc_compositor_get_time();
271 wl_list_for_each_safe(l, next,
272 &surface->surface.destroy_listener_list, link)
273 l->func(l, &surface->surface, time);
279 wlsc_load_image(const char *filename, int width, int height)
282 GError *error = NULL;
283 int stride, i, n_channels;
284 unsigned char *pixels, *end, *argb_pixels, *s, *d;
286 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
290 fprintf(stderr, "failed to load image: %s\n", error->message);
295 stride = gdk_pixbuf_get_rowstride(pixbuf);
296 pixels = gdk_pixbuf_get_pixels(pixbuf);
297 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
299 argb_pixels = malloc (height * width * 4);
300 if (argb_pixels == NULL) {
301 g_object_unref(pixbuf);
305 if (n_channels == 4) {
306 for (i = 0; i < height; i++) {
307 s = pixels + i * stride;
309 d = argb_pixels + i * width * 4;
313 #define MULT(_d,c,a,t) \
314 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
316 MULT(d[0], s[2], s[3], t);
317 MULT(d[1], s[1], s[3], t);
318 MULT(d[2], s[0], s[3], t);
324 } else if (n_channels == 3) {
325 for (i = 0; i < height; i++) {
326 s = pixels + i * stride;
328 d = argb_pixels + i * width * 4;
340 g_object_unref(pixbuf);
342 return (uint32_t *) argb_pixels;
346 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
348 struct wlsc_surface *es = (struct wlsc_surface *) surface;
349 struct wlsc_compositor *ec = es->compositor;
350 struct wl_list *surfaces_attached_to;
352 if (es->saved_texture != 0)
353 es->texture = es->saved_texture;
355 glBindTexture(GL_TEXTURE_2D, es->texture);
357 if (wl_buffer_is_shm(buffer)) {
358 /* Unbind any EGLImage texture that may be bound, so we don't
360 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
361 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
362 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
363 buffer->width, buffer->height, 0,
364 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
365 wl_shm_buffer_get_data(buffer));
366 es->visual = buffer->visual;
368 surfaces_attached_to = buffer->user_data;
371 wl_list_remove(&es->buffer_link);
372 wl_list_insert(surfaces_attached_to, &es->buffer_link);
374 es->image = eglCreateImageKHR(ec->display, NULL,
375 EGL_WAYLAND_BUFFER_WL,
378 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
379 es->visual = buffer->visual;
384 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
386 struct wlsc_surface *es = (struct wlsc_surface *) surface;
388 es->image = sprite->image;
389 if (sprite->image != EGL_NO_IMAGE_KHR) {
390 glBindTexture(GL_TEXTURE_2D, es->texture);
391 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
393 if (es->saved_texture == 0)
394 es->saved_texture = es->texture;
395 es->texture = sprite->texture;
398 es->visual = sprite->visual;
402 SPRITE_USE_CURSOR = (1 << 0),
405 static struct wlsc_sprite *
406 create_sprite_from_png(struct wlsc_compositor *ec,
407 const char *filename, int width, int height,
411 struct wlsc_sprite *sprite;
413 pixels = wlsc_load_image(filename, width, height);
417 sprite = malloc(sizeof *sprite);
418 if (sprite == NULL) {
423 sprite->visual = &ec->compositor.premultiplied_argb_visual;
424 sprite->width = width;
425 sprite->height = height;
426 sprite->image = EGL_NO_IMAGE_KHR;
428 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
429 sprite->image = ec->create_cursor_image(ec, width, height);
431 glGenTextures(1, &sprite->texture);
432 glBindTexture(GL_TEXTURE_2D, sprite->texture);
433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
434 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
435 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
438 if (sprite->image != EGL_NO_IMAGE_KHR) {
439 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
440 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
441 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
443 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
444 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
452 static const struct {
453 const char *filename;
454 int hotspot_x, hotspot_y;
455 } pointer_images[] = {
456 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
457 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
458 { DATADIR "/wayland/bottom_side.png", 16, 20 },
459 { DATADIR "/wayland/grabbing.png", 20, 17 },
460 { DATADIR "/wayland/left_ptr.png", 10, 5 },
461 { DATADIR "/wayland/left_side.png", 10, 20 },
462 { DATADIR "/wayland/right_side.png", 30, 19 },
463 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
464 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
465 { DATADIR "/wayland/top_side.png", 18, 8 },
466 { DATADIR "/wayland/xterm.png", 15, 15 }
470 create_pointer_images(struct wlsc_compositor *ec)
473 const int width = 32, height = 32;
475 count = ARRAY_LENGTH(pointer_images);
476 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
477 for (i = 0; i < count; i++) {
478 ec->pointer_sprites[i] =
479 create_sprite_from_png(ec,
480 pointer_images[i].filename,
486 static struct wlsc_surface *
487 background_create(struct wlsc_output *output, const char *filename)
489 struct wlsc_surface *background;
490 struct wlsc_sprite *sprite;
492 background = wlsc_surface_create(output->compositor,
493 output->x, output->y,
494 output->width, output->height);
495 if (background == NULL)
498 sprite = create_sprite_from_png(output->compositor, filename,
499 output->width, output->height, 0);
500 if (sprite == NULL) {
505 wlsc_sprite_attach(sprite, &background->surface);
511 wlsc_surface_draw(struct wlsc_surface *es,
512 struct wlsc_output *output, pixman_region32_t *clip)
514 struct wlsc_compositor *ec = es->compositor;
515 GLfloat *v, inv_width, inv_height;
517 pixman_region32_t repaint;
518 pixman_box32_t *rectangles;
521 pixman_region32_init_rect(&repaint,
522 es->x, es->y, es->width, es->height);
523 pixman_region32_intersect(&repaint, &repaint, clip);
524 if (!pixman_region32_not_empty(&repaint))
527 if (es->visual == &ec->compositor.argb_visual) {
528 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
530 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
531 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
537 rectangles = pixman_region32_rectangles(&repaint, &n);
538 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
539 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
540 inv_width = 1.0 / es->width;
541 inv_height = 1.0 / es->height;
542 for (i = 0; i < n; i++, v += 16, p += 6) {
543 v[ 0] = rectangles[i].x1;
544 v[ 1] = rectangles[i].y1;
545 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
546 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
548 v[ 4] = rectangles[i].x1;
549 v[ 5] = rectangles[i].y2;
551 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
553 v[ 8] = rectangles[i].x2;
554 v[ 9] = rectangles[i].y1;
555 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
558 v[12] = rectangles[i].x2;
559 v[13] = rectangles[i].y2;
571 glBindTexture(GL_TEXTURE_2D, es->texture);
572 v = ec->vertices.data;
573 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
574 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
575 glEnableVertexAttribArray(0);
576 glEnableVertexAttribArray(1);
577 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
579 ec->vertices.size = 0;
580 ec->indices.size = 0;
581 pixman_region32_fini(&repaint);
585 wlsc_surface_raise(struct wlsc_surface *surface)
587 struct wlsc_compositor *compositor = surface->compositor;
589 wl_list_remove(&surface->link);
590 wl_list_insert(&compositor->surface_list, &surface->link);
594 wlsc_surface_update_matrix(struct wlsc_surface *es)
596 wlsc_matrix_init(&es->matrix);
597 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
598 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
600 wlsc_matrix_init(&es->matrix_inv);
601 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
602 wlsc_matrix_scale(&es->matrix_inv,
603 1.0 / es->width, 1.0 / es->height, 1);
607 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
609 struct wlsc_output *output;
611 wl_list_for_each(output, &compositor->output_list, link)
612 wlsc_output_damage(output);
616 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
618 struct wlsc_compositor *compositor = output->compositor;
619 struct wlsc_surface *es;
620 struct wlsc_animation *animation, *next;
622 wl_list_for_each(es, &compositor->surface_list, link) {
623 if (es->output == output) {
624 wl_display_post_frame(compositor->wl_display,
625 &es->surface, msecs);
629 output->finished = 1;
631 wl_event_source_timer_update(compositor->timer_source, 5);
632 compositor->repaint_on_timeout = 1;
634 wl_list_for_each_safe(animation, next,
635 &compositor->animation_list, link)
636 animation->frame(animation, output, msecs);
640 wlsc_output_damage(struct wlsc_output *output)
642 struct wlsc_compositor *compositor = output->compositor;
644 pixman_region32_union_rect(&compositor->damage_region,
645 &compositor->damage_region,
646 output->x, output->y,
647 output->width, output->height);
648 wlsc_compositor_schedule_repaint(compositor);
652 fade_frame(struct wlsc_animation *animation,
653 struct wlsc_output *output, uint32_t msecs)
655 struct wlsc_compositor *compositor =
656 container_of(animation,
657 struct wlsc_compositor, fade.animation);
659 wlsc_tweener_update(&compositor->fade.tweener, msecs);
660 if (wlsc_tweener_done(&compositor->fade.tweener)) {
661 if (compositor->fade.tweener.current > 0.999) {
662 compositor->state = WLSC_COMPOSITOR_SLEEPING;
663 compositor->shell->lock(compositor->shell);
665 compositor->fade.tweener.current =
666 compositor->fade.tweener.target;
667 wl_list_remove(&animation->link);
668 wl_list_init(&animation->link);
671 wlsc_output_damage(output);
675 fade_output(struct wlsc_output *output,
676 GLfloat tint, pixman_region32_t *region)
678 struct wlsc_compositor *compositor = output->compositor;
679 struct wlsc_surface surface;
680 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
682 surface.compositor = compositor;
683 surface.x = output->x;
684 surface.y = output->y;
685 surface.width = output->width;
686 surface.height = output->height;
687 surface.visual = &compositor->compositor.premultiplied_argb_visual;
688 surface.texture = GL_NONE;
689 glUseProgram(compositor->solid_shader.program);
690 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
691 1, GL_FALSE, output->matrix.d);
692 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
693 wlsc_surface_draw(&surface, output, region);
697 wlsc_output_repaint(struct wlsc_output *output)
699 struct wlsc_compositor *ec = output->compositor;
700 struct wlsc_surface *es;
701 struct wlsc_input_device *eid;
702 pixman_region32_t new_damage, total_damage, repaint;
703 int using_hardware_cursor = 1;
705 output->prepare_render(output);
707 glViewport(0, 0, output->width, output->height);
709 glUseProgram(ec->texture_shader.program);
710 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
711 1, GL_FALSE, output->matrix.d);
712 glUniform1i(ec->texture_shader.tex_uniform, 0);
714 pixman_region32_init(&new_damage);
715 pixman_region32_init(&total_damage);
716 pixman_region32_intersect_rect(&new_damage,
718 output->x, output->y,
719 output->width, output->height);
720 pixman_region32_subtract(&ec->damage_region,
721 &ec->damage_region, &new_damage);
722 pixman_region32_union(&total_damage, &new_damage,
723 &output->previous_damage_region);
724 pixman_region32_copy(&output->previous_damage_region, &new_damage);
727 if (output->set_hardware_cursor(output, ec->input_device) < 0)
728 using_hardware_cursor = 0;
729 if (ec->fade.tweener.current > 0.001)
730 using_hardware_cursor = 0;
732 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
733 if (es->fullscreen_output == output) {
734 if (es->visual == &ec->compositor.rgb_visual &&
735 using_hardware_cursor) {
736 if (output->prepare_scanout_surface(output, es) == 0) {
737 /* We're drawing nothing now,
738 * draw the damaged regions later. */
739 pixman_region32_union(&ec->damage_region,
746 if (es->width < output->width ||
747 es->height < output->height)
748 glClear(GL_COLOR_BUFFER_BIT);
749 wlsc_surface_draw(es, output, &total_damage);
751 wl_list_for_each(es, &ec->surface_list, link) {
752 if (es->visual != &ec->compositor.rgb_visual)
755 pixman_region32_init_rect(&repaint,
757 es->width, es->height);
758 wlsc_surface_draw(es, output, &total_damage);
759 pixman_region32_subtract(&total_damage,
760 &total_damage, &repaint);
763 if (output->background)
764 wlsc_surface_draw(output->background,
765 output, &total_damage);
767 glClear(GL_COLOR_BUFFER_BIT);
769 wl_list_for_each_reverse(es, &ec->surface_list, link) {
770 if (ec->overlay == es)
773 if (es->visual == &ec->compositor.rgb_visual) {
774 pixman_region32_union_rect(&total_damage,
777 es->width, es->height);
781 wlsc_surface_draw(es, output, &total_damage);
786 wlsc_surface_draw(ec->overlay, output, &total_damage);
789 wl_list_for_each(eid, &ec->input_device_list, link) {
790 if (&eid->input_device != ec->input_device ||
791 !using_hardware_cursor)
792 wlsc_surface_draw(eid->sprite, output,
796 if (ec->fade.tweener.current > 0.001)
797 fade_output(output, ec->fade.tweener.current, &total_damage);
803 struct wlsc_compositor *ec = data;
804 struct wlsc_output *output;
805 int repainted_all_outputs = 1;
807 wl_list_for_each(output, &ec->output_list, link) {
808 if (!output->repaint_needed)
811 if (!output->finished) {
812 repainted_all_outputs = 0;
816 wlsc_output_repaint(output);
817 output->finished = 0;
818 output->repaint_needed = 0;
819 output->present(output);
822 if (repainted_all_outputs)
823 ec->repaint_on_timeout = 0;
825 wl_event_source_timer_update(ec->timer_source, 1);
831 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
833 struct wlsc_output *output;
835 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
838 wl_list_for_each(output, &compositor->output_list, link)
839 output->repaint_needed = 1;
841 if (compositor->repaint_on_timeout)
844 wl_event_source_timer_update(compositor->timer_source, 1);
845 compositor->repaint_on_timeout = 1;
849 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
853 done = wlsc_tweener_done(&compositor->fade.tweener);
854 compositor->fade.tweener.target = tint;
855 if (wlsc_tweener_done(&compositor->fade.tweener))
859 compositor->fade.tweener.timestamp =
860 wlsc_compositor_get_time();
862 wlsc_compositor_damage_all(compositor);
863 if (wl_list_empty(&compositor->fade.animation.link))
864 wl_list_insert(compositor->animation_list.prev,
865 &compositor->fade.animation.link);
869 surface_destroy(struct wl_client *client,
870 struct wl_surface *surface)
872 wl_resource_destroy(&surface->resource, client);
876 wlsc_surface_assign_output(struct wlsc_surface *es)
878 struct wlsc_compositor *ec = es->compositor;
879 struct wlsc_output *output;
881 struct wlsc_output *tmp = es->output;
884 wl_list_for_each(output, &ec->output_list, link) {
885 if (output->x < es->x && es->x < output->x + output->width &&
886 output->y < es->y && es->y < output->y + output->height) {
888 printf("assiging surface %p to output %p\n",
894 if (es->output == NULL) {
895 printf("no output found\n");
896 es->output = container_of(ec->output_list.next,
897 struct wlsc_output, link);
902 surface_attach(struct wl_client *client,
903 struct wl_surface *surface, struct wl_buffer *buffer,
904 int32_t x, int32_t y)
906 struct wlsc_surface *es = (struct wlsc_surface *) surface;
908 /* FIXME: This damages the entire old surface, but we should
909 * really just damage the part that's no longer covered by the
910 * surface. Anything covered by the new surface will be
911 * damaged by the client. */
912 wlsc_surface_damage(es);
914 wlsc_buffer_attach(buffer, surface);
919 es->width = buffer->width;
920 es->height = buffer->height;
921 if (x != 0 || y != 0)
922 wlsc_surface_assign_output(es);
923 wlsc_surface_update_matrix(es);
925 es->compositor->shell->attach(es->compositor->shell, es);
929 surface_map_toplevel(struct wl_client *client,
930 struct wl_surface *surface)
932 struct wlsc_surface *es = (struct wlsc_surface *) surface;
933 struct wlsc_compositor *ec = es->compositor;
935 switch (es->map_type) {
936 case WLSC_SURFACE_MAP_UNMAPPED:
937 es->x = 10 + random() % 400;
938 es->y = 10 + random() % 400;
939 wlsc_surface_update_matrix(es);
940 /* assign to first output */
941 es->output = container_of(ec->output_list.next,
942 struct wlsc_output, link);
943 wl_list_insert(&es->compositor->surface_list, &es->link);
945 case WLSC_SURFACE_MAP_TOPLEVEL:
947 case WLSC_SURFACE_MAP_FULLSCREEN:
948 es->fullscreen_output = NULL;
951 wlsc_surface_update_matrix(es);
957 wlsc_surface_damage(es);
958 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
962 surface_map_transient(struct wl_client *client,
963 struct wl_surface *surface, struct wl_surface *parent,
964 int x, int y, uint32_t flags)
966 struct wlsc_surface *es = (struct wlsc_surface *) surface;
967 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
969 switch (es->map_type) {
970 case WLSC_SURFACE_MAP_UNMAPPED:
971 wl_list_insert(&es->compositor->surface_list, &es->link);
972 /* assign to parents output */
973 es->output = pes->output;
975 case WLSC_SURFACE_MAP_FULLSCREEN:
976 es->fullscreen_output = NULL;
985 wlsc_surface_update_matrix(es);
986 wlsc_surface_damage(es);
987 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
991 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
993 struct wlsc_surface *es = (struct wlsc_surface *) surface;
994 struct wlsc_output *output;
996 /* FIXME: Fullscreen on first output */
997 /* FIXME: Handle output going away */
998 output = container_of(es->compositor->output_list.next,
999 struct wlsc_output, link);
1001 switch (es->map_type) {
1002 case WLSC_SURFACE_MAP_UNMAPPED:
1003 es->x = 10 + random() % 400;
1004 es->y = 10 + random() % 400;
1005 /* assign to first output */
1006 es->output = output;
1007 wl_list_insert(&es->compositor->surface_list, &es->link);
1009 case WLSC_SURFACE_MAP_FULLSCREEN:
1015 es->saved_x = es->x;
1016 es->saved_y = es->y;
1017 es->x = (output->width - es->width) / 2;
1018 es->y = (output->height - es->height) / 2;
1019 es->fullscreen_output = output;
1020 wlsc_surface_update_matrix(es);
1021 wlsc_surface_damage(es);
1022 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
1026 surface_damage(struct wl_client *client,
1027 struct wl_surface *surface,
1028 int32_t x, int32_t y, int32_t width, int32_t height)
1030 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1032 wlsc_surface_damage_rectangle(es, x, y, width, height);
1035 const static struct wl_surface_interface surface_interface = {
1038 surface_map_toplevel,
1039 surface_map_transient,
1040 surface_map_fullscreen,
1045 wlsc_input_device_attach(struct wlsc_input_device *device,
1046 int x, int y, int width, int height)
1048 wlsc_surface_damage(device->sprite);
1050 device->hotspot_x = x;
1051 device->hotspot_y = y;
1053 device->sprite->x = device->input_device.x - device->hotspot_x;
1054 device->sprite->y = device->input_device.y - device->hotspot_y;
1055 device->sprite->width = width;
1056 device->sprite->height = height;
1057 wlsc_surface_update_matrix(device->sprite);
1059 wlsc_surface_damage(device->sprite);
1063 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1064 struct wl_buffer *buffer, int x, int y)
1066 wlsc_buffer_attach(buffer, &device->sprite->surface);
1067 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1071 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1072 struct wlsc_sprite *sprite, int x, int y)
1074 wlsc_sprite_attach(sprite, &device->sprite->surface);
1075 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1079 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1080 enum wlsc_pointer_type type)
1082 struct wlsc_compositor *compositor =
1083 (struct wlsc_compositor *) device->input_device.compositor;
1085 wlsc_input_device_attach_sprite(device,
1086 compositor->pointer_sprites[type],
1087 pointer_images[type].hotspot_x,
1088 pointer_images[type].hotspot_y);
1092 compositor_create_surface(struct wl_client *client,
1093 struct wl_compositor *compositor, uint32_t id)
1095 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1096 struct wlsc_surface *surface;
1098 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1099 if (surface == NULL) {
1100 wl_client_post_no_memory(client);
1104 surface->surface.resource.destroy = destroy_surface;
1106 surface->surface.resource.object.id = id;
1107 surface->surface.resource.object.interface = &wl_surface_interface;
1108 surface->surface.resource.object.implementation =
1109 (void (**)(void)) &surface_interface;
1110 surface->surface.client = client;
1112 wl_client_add_resource(client, &surface->surface.resource);
1115 const static struct wl_compositor_interface compositor_interface = {
1116 compositor_create_surface,
1120 wlsc_surface_transform(struct wlsc_surface *surface,
1121 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1123 struct wlsc_vector v = { { x, y, 0, 1 } };
1125 wlsc_matrix_transform(&surface->matrix_inv, &v);
1126 *sx = v.f[0] * surface->width;
1127 *sy = v.f[1] * surface->height;
1130 struct wlsc_surface *
1131 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1133 struct wlsc_compositor *ec =
1134 (struct wlsc_compositor *) device->compositor;
1135 struct wlsc_surface *es;
1137 wl_list_for_each(es, &ec->surface_list, link) {
1138 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1139 if (0 <= *sx && *sx < es->width &&
1140 0 <= *sy && *sy < es->height)
1149 motion_grab_motion(struct wl_grab *grab,
1150 uint32_t time, int32_t x, int32_t y)
1152 struct wlsc_input_device *device =
1153 (struct wlsc_input_device *) grab->input_device;
1154 struct wlsc_surface *es =
1155 (struct wlsc_surface *) device->input_device.pointer_focus;
1158 wlsc_surface_transform(es, x, y, &sx, &sy);
1159 wl_client_post_event(es->surface.client,
1160 &device->input_device.object,
1161 WL_INPUT_DEVICE_MOTION,
1162 time, x, y, sx, sy);
1166 motion_grab_button(struct wl_grab *grab,
1167 uint32_t time, int32_t button, int32_t state)
1169 wl_client_post_event(grab->input_device->pointer_focus->client,
1170 &grab->input_device->object,
1171 WL_INPUT_DEVICE_BUTTON,
1172 time, button, state);
1176 motion_grab_end(struct wl_grab *grab, uint32_t time)
1180 static const struct wl_grab_interface motion_grab_interface = {
1187 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1189 if (compositor->idle_inhibit)
1192 wlsc_compositor_fade(compositor, 0.0);
1193 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1195 wl_event_source_timer_update(compositor->idle_source,
1196 option_idle_time * 1000);
1200 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1202 wlsc_compositor_wake(compositor);
1203 compositor->idle_inhibit++;
1207 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1209 compositor->idle_inhibit--;
1210 wlsc_compositor_wake(compositor);
1214 idle_handler(void *data)
1216 struct wlsc_compositor *compositor = data;
1218 if (compositor->idle_inhibit)
1221 wlsc_compositor_fade(compositor, 1.0);
1227 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1229 struct wlsc_surface *es;
1230 struct wlsc_compositor *ec =
1231 (struct wlsc_compositor *) device->compositor;
1232 struct wlsc_output *output;
1233 const struct wl_grab_interface *interface;
1234 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1236 int x_valid = 0, y_valid = 0;
1237 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1239 wlsc_compositor_wake(ec);
1241 wl_list_for_each(output, &ec->output_list, link) {
1242 if (output->x <= x && x <= output->x + output->width)
1245 if (output->y <= y && y <= output->y + output->height)
1248 /* FIXME: calculate this only on output addition/deletion */
1249 if (output->x < min_x)
1251 if (output->y < min_y)
1254 if (output->x + output->width > max_x)
1255 max_x = output->x + output->width;
1256 if (output->y + output->height > max_y)
1257 max_y = output->y + output->height;
1263 else if (x >= max_x)
1269 else if (y >= max_y)
1277 interface = device->grab->interface;
1278 interface->motion(device->grab, time, x, y);
1280 es = pick_surface(device, &sx, &sy);
1281 wl_input_device_set_pointer_focus(device,
1283 time, x, y, sx, sy);
1285 wl_client_post_event(es->surface.client,
1287 WL_INPUT_DEVICE_MOTION,
1288 time, x, y, sx, sy);
1291 wlsc_surface_damage(wd->sprite);
1293 wd->sprite->x = device->x - wd->hotspot_x;
1294 wd->sprite->y = device->y - wd->hotspot_y;
1295 wlsc_surface_update_matrix(wd->sprite);
1297 wlsc_surface_damage(wd->sprite);
1301 wlsc_surface_activate(struct wlsc_surface *surface,
1302 struct wlsc_input_device *device, uint32_t time)
1304 wlsc_surface_raise(surface);
1305 if (device->selection)
1306 wlsc_selection_set_focus(device->selection,
1307 &surface->surface, time);
1309 wl_input_device_set_keyboard_focus(&device->input_device,
1314 struct wlsc_binding {
1318 wlsc_binding_handler_t handler;
1320 struct wl_list link;
1324 notify_button(struct wl_input_device *device,
1325 uint32_t time, int32_t button, int32_t state)
1327 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1328 struct wlsc_compositor *compositor =
1329 (struct wlsc_compositor *) device->compositor;
1330 struct wlsc_binding *b;
1331 struct wlsc_surface *surface =
1332 (struct wlsc_surface *) device->pointer_focus;
1335 wlsc_compositor_idle_inhibit(compositor);
1337 wlsc_compositor_idle_release(compositor);
1339 if (state && surface && device->grab == NULL) {
1340 wlsc_surface_activate(surface, wd, time);
1341 wl_input_device_start_grab(device,
1342 &device->motion_grab,
1346 wl_list_for_each(b, &compositor->binding_list, link) {
1347 if (b->button == button &&
1348 b->modifier == wd->modifier_state && state) {
1349 b->handler(&wd->input_device,
1350 time, 0, button, state, b->data);
1356 device->grab->interface->button(device->grab, time,
1359 if (!state && device->grab && device->grab_button == button)
1360 wl_input_device_end_grab(device, time);
1364 terminate_binding(struct wl_input_device *device, uint32_t time,
1365 uint32_t key, uint32_t button, uint32_t state, void *data)
1367 struct wlsc_compositor *compositor = data;
1370 wl_display_terminate(compositor->wl_display);
1373 WL_EXPORT struct wlsc_binding *
1374 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1375 uint32_t key, uint32_t button, uint32_t modifier,
1376 wlsc_binding_handler_t handler, void *data)
1378 struct wlsc_binding *binding;
1380 binding = malloc(sizeof *binding);
1381 if (binding == NULL)
1385 binding->button = button;
1386 binding->modifier = modifier;
1387 binding->handler = handler;
1388 binding->data = data;
1389 wl_list_insert(compositor->binding_list.prev, &binding->link);
1395 wlsc_binding_destroy(struct wlsc_binding *binding)
1397 wl_list_remove(&binding->link);
1402 update_modifier_state(struct wlsc_input_device *device,
1403 uint32_t key, uint32_t state)
1410 modifier = MODIFIER_CTRL;
1415 modifier = MODIFIER_ALT;
1420 modifier = MODIFIER_SUPER;
1429 device->modifier_state |= modifier;
1431 device->modifier_state &= ~modifier;
1435 notify_key(struct wl_input_device *device,
1436 uint32_t time, uint32_t key, uint32_t state)
1438 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1439 struct wlsc_compositor *compositor =
1440 (struct wlsc_compositor *) device->compositor;
1442 struct wlsc_binding *b;
1445 wlsc_compositor_idle_inhibit(compositor);
1447 wlsc_compositor_idle_release(compositor);
1449 wl_list_for_each(b, &compositor->binding_list, link) {
1450 if (b->key == key &&
1451 b->modifier == wd->modifier_state) {
1452 b->handler(&wd->input_device,
1453 time, key, 0, state, b->data);
1458 update_modifier_state(wd, key, state);
1459 end = device->keys.data + device->keys.size;
1460 for (k = device->keys.data; k < end; k++) {
1464 device->keys.size = (void *) end - device->keys.data;
1466 k = wl_array_add(&device->keys, sizeof *k);
1470 if (device->keyboard_focus != NULL)
1471 wl_client_post_event(device->keyboard_focus->client,
1473 WL_INPUT_DEVICE_KEY, time, key, state);
1477 notify_pointer_focus(struct wl_input_device *device,
1478 uint32_t time, struct wlsc_output *output,
1479 int32_t x, int32_t y)
1481 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1482 struct wlsc_compositor *compositor =
1483 (struct wlsc_compositor *) device->compositor;
1484 struct wlsc_surface *es;
1490 es = pick_surface(device, &sx, &sy);
1491 wl_input_device_set_pointer_focus(device,
1493 time, x, y, sx, sy);
1495 compositor->focus = 1;
1497 wd->sprite->x = device->x - wd->hotspot_x;
1498 wd->sprite->y = device->y - wd->hotspot_y;
1499 wlsc_surface_update_matrix(wd->sprite);
1501 wl_input_device_set_pointer_focus(device, NULL,
1503 compositor->focus = 0;
1506 wlsc_surface_damage(wd->sprite);
1510 notify_keyboard_focus(struct wl_input_device *device,
1511 uint32_t time, struct wlsc_output *output,
1512 struct wl_array *keys)
1514 struct wlsc_input_device *wd =
1515 (struct wlsc_input_device *) device;
1516 struct wlsc_compositor *compositor =
1517 (struct wlsc_compositor *) device->compositor;
1518 struct wlsc_surface *es;
1521 if (!wl_list_empty(&compositor->surface_list))
1522 es = container_of(compositor->surface_list.next,
1523 struct wlsc_surface, link);
1528 wl_array_copy(&wd->input_device.keys, keys);
1529 wd->modifier_state = 0;
1530 end = device->keys.data + device->keys.size;
1531 for (k = device->keys.data; k < end; k++) {
1532 wlsc_compositor_idle_inhibit(compositor);
1533 update_modifier_state(wd, *k, 1);
1536 wl_input_device_set_keyboard_focus(&wd->input_device,
1537 &es->surface, time);
1539 end = device->keys.data + device->keys.size;
1540 for (k = device->keys.data; k < end; k++)
1541 wlsc_compositor_idle_release(compositor);
1543 wd->modifier_state = 0;
1544 wl_input_device_set_keyboard_focus(&wd->input_device,
1551 input_device_attach(struct wl_client *client,
1552 struct wl_input_device *device_base,
1554 struct wl_buffer *buffer, int32_t x, int32_t y)
1556 struct wlsc_input_device *device =
1557 (struct wlsc_input_device *) device_base;
1559 if (time < device->input_device.pointer_focus_time)
1561 if (device->input_device.pointer_focus == NULL)
1563 if (device->input_device.pointer_focus->client != client)
1566 if (buffer == NULL) {
1567 wlsc_input_device_set_pointer_image(device,
1568 WLSC_POINTER_LEFT_PTR);
1572 wlsc_input_device_attach_buffer(device, buffer, x, y);
1575 const static struct wl_input_device_interface input_device_interface = {
1576 input_device_attach,
1580 wlsc_input_device_init(struct wlsc_input_device *device,
1581 struct wlsc_compositor *ec)
1583 wl_input_device_init(&device->input_device, &ec->compositor);
1585 device->input_device.object.interface = &wl_input_device_interface;
1586 device->input_device.object.implementation =
1587 (void (**)(void)) &input_device_interface;
1588 wl_display_add_object(ec->wl_display, &device->input_device.object);
1589 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1591 device->sprite = wlsc_surface_create(ec,
1592 device->input_device.x,
1593 device->input_device.y, 32, 32);
1594 device->hotspot_x = 16;
1595 device->hotspot_y = 16;
1596 device->modifier_state = 0;
1598 device->input_device.motion_grab.interface = &motion_grab_interface;
1600 wl_list_insert(ec->input_device_list.prev, &device->link);
1602 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1606 wlsc_output_post_geometry(struct wl_client *client,
1607 struct wl_object *global, uint32_t version)
1609 struct wlsc_output *output =
1610 container_of(global, struct wlsc_output, object);
1612 wl_client_post_event(client, global,
1614 output->x, output->y,
1615 output->width, output->height);
1618 static const char vertex_shader[] =
1619 "uniform mat4 proj;\n"
1620 "attribute vec2 position;\n"
1621 "attribute vec2 texcoord;\n"
1622 "varying vec2 v_texcoord;\n"
1625 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1626 " v_texcoord = texcoord;\n"
1629 static const char texture_fragment_shader[] =
1630 "precision mediump float;\n"
1631 "varying vec2 v_texcoord;\n"
1632 "uniform sampler2D tex;\n"
1635 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1638 static const char solid_fragment_shader[] =
1639 "precision mediump float;\n"
1640 "varying vec2 v_texcoord;\n"
1641 "uniform vec4 color;\n"
1644 " gl_FragColor = color\n;"
1648 compile_shader(GLenum type, const char *source)
1654 s = glCreateShader(type);
1655 glShaderSource(s, 1, &source, NULL);
1657 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1659 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1660 fprintf(stderr, "shader info: %s\n", msg);
1668 wlsc_shader_init(struct wlsc_shader *shader,
1669 const char *vertex_source, const char *fragment_source)
1674 shader->vertex_shader =
1675 compile_shader(GL_VERTEX_SHADER, vertex_source);
1676 shader->fragment_shader =
1677 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1679 shader->program = glCreateProgram();
1680 glAttachShader(shader->program, shader->vertex_shader);
1681 glAttachShader(shader->program, shader->fragment_shader);
1682 glBindAttribLocation(shader->program, 0, "position");
1683 glBindAttribLocation(shader->program, 1, "texcoord");
1685 glLinkProgram(shader->program);
1686 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1688 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1689 fprintf(stderr, "link info: %s\n", msg);
1693 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1694 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1700 init_solid_shader(struct wlsc_shader *shader,
1701 GLuint vertex_shader, const char *fragment_source)
1706 shader->vertex_shader = vertex_shader;
1707 shader->fragment_shader =
1708 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1710 shader->program = glCreateProgram();
1711 glAttachShader(shader->program, shader->vertex_shader);
1712 glAttachShader(shader->program, shader->fragment_shader);
1713 glBindAttribLocation(shader->program, 0, "position");
1714 glBindAttribLocation(shader->program, 1, "texcoord");
1716 glLinkProgram(shader->program);
1717 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1719 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1720 fprintf(stderr, "link info: %s\n", msg);
1724 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1725 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1731 wlsc_output_destroy(struct wlsc_output *output)
1733 destroy_surface(&output->background->surface.resource, NULL);
1737 wlsc_output_move(struct wlsc_output *output, int x, int y)
1739 struct wlsc_compositor *c = output->compositor;
1745 if (output->background) {
1746 output->background->x = x;
1747 output->background->y = y;
1748 wlsc_surface_update_matrix(output->background);
1751 pixman_region32_init(&output->previous_damage_region);
1753 wlsc_matrix_init(&output->matrix);
1754 wlsc_matrix_translate(&output->matrix,
1755 -output->x - output->width / 2.0,
1756 -output->y - output->height / 2.0, 0);
1758 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1759 wlsc_matrix_scale(&output->matrix,
1760 2.0 / output->width,
1761 flip * 2.0 / output->height, 1);
1763 pixman_region32_union_rect(&c->damage_region,
1765 x, y, output->width, output->height);
1769 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1770 int x, int y, int width, int height, uint32_t flags)
1772 output->compositor = c;
1775 output->width = width;
1776 output->height = height;
1778 output->background =
1779 background_create(output, option_background);
1781 output->flags = flags;
1782 output->finished = 1;
1783 wlsc_output_move(output, x, y);
1785 output->object.interface = &wl_output_interface;
1786 wl_display_add_object(c->wl_display, &output->object);
1787 wl_display_add_global(c->wl_display, &output->object,
1788 wlsc_output_post_geometry);
1792 shm_buffer_created(struct wl_buffer *buffer)
1794 struct wl_list *surfaces_attached_to;
1796 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1797 if (!surfaces_attached_to) {
1798 buffer->user_data = NULL;
1802 wl_list_init(surfaces_attached_to);
1804 buffer->user_data = surfaces_attached_to;
1808 shm_buffer_damaged(struct wl_buffer *buffer,
1809 int32_t x, int32_t y, int32_t width, int32_t height)
1811 struct wl_list *surfaces_attached_to = buffer->user_data;
1812 struct wlsc_surface *es;
1814 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1815 glBindTexture(GL_TEXTURE_2D, es->texture);
1816 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1817 buffer->width, buffer->height, 0,
1818 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1819 wl_shm_buffer_get_data(buffer));
1820 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1821 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1826 shm_buffer_destroyed(struct wl_buffer *buffer)
1828 struct wl_list *surfaces_attached_to = buffer->user_data;
1829 struct wlsc_surface *es, *next;
1831 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1833 wl_list_remove(&es->buffer_link);
1836 free(surfaces_attached_to);
1839 const static struct wl_shm_callbacks shm_callbacks = {
1842 shm_buffer_destroyed
1846 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1848 struct wl_event_loop *loop;
1849 const char *extensions;
1851 ec->wl_display = display;
1853 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1855 ec->shm = wl_shm_init(display, &shm_callbacks);
1856 if (strstr(eglQueryString(ec->display, EGL_EXTENSIONS),
1857 "EGL_WL_bind_wayland_display"))
1858 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
1860 wl_list_init(&ec->surface_list);
1861 wl_list_init(&ec->input_device_list);
1862 wl_list_init(&ec->output_list);
1863 wl_list_init(&ec->binding_list);
1864 wl_list_init(&ec->animation_list);
1865 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1866 ec->fade.animation.frame = fade_frame;
1867 wl_list_init(&ec->fade.animation.link);
1869 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1870 MODIFIER_CTRL | MODIFIER_ALT,
1871 terminate_binding, ec);
1873 create_pointer_images(ec);
1875 screenshooter_create(ec);
1877 extensions = (const char *) glGetString(GL_EXTENSIONS);
1878 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1880 "GL_EXT_texture_format_BGRA8888 not available\n");
1884 glActiveTexture(GL_TEXTURE0);
1886 if (wlsc_shader_init(&ec->texture_shader,
1887 vertex_shader, texture_fragment_shader) < 0)
1889 if (init_solid_shader(&ec->solid_shader,
1890 ec->texture_shader.vertex_shader,
1891 solid_fragment_shader) < 0)
1894 loop = wl_display_get_event_loop(ec->wl_display);
1895 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1896 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1898 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1899 pixman_region32_init(&ec->damage_region);
1900 wlsc_compositor_schedule_repaint(ec);
1905 static int on_term_signal(int signal_number, void *data)
1907 struct wlsc_compositor *ec = data;
1909 wl_display_terminate(ec->wl_display);
1914 int main(int argc, char *argv[])
1916 struct wl_display *display;
1917 struct wlsc_compositor *ec;
1918 struct wl_event_loop *loop;
1919 GError *error = NULL;
1920 GOptionContext *context;
1923 int (*shell_init)(struct wlsc_compositor *ec);
1925 g_type_init(); /* GdkPixbuf needs this, it seems. */
1927 context = g_option_context_new(NULL);
1928 g_option_context_add_main_entries(context, option_entries, "Wayland");
1929 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1930 fprintf(stderr, "option parsing failed: %s\n", error->message);
1933 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1934 fprintf(stderr, "invalid geometry option: %s \n",
1939 display = wl_display_create();
1943 shell_init = desktop_shell_init;
1945 shell_module = dlopen(option_shell, RTLD_LAZY);
1946 if (!shell_module) {
1947 fprintf(stderr, "failed to load shell module: %m\n");
1950 shell_init = dlsym(shell_module, "shell_init");
1953 "failed to lookup shell init function: %m\n");
1958 #if BUILD_WAYLAND_COMPOSITOR
1959 if (getenv("WAYLAND_DISPLAY"))
1960 ec = wayland_compositor_create(display, width, height);
1963 #if BUILD_X11_COMPOSITOR
1964 if (ec == NULL && getenv("DISPLAY"))
1965 ec = x11_compositor_create(display, width, height);
1968 #if BUILD_OPENWFD_COMPOSITOR
1969 if (ec == NULL && getenv("OPENWFD"))
1970 ec = wfd_compositor_create(display, option_connector);
1973 #if BUILD_DRM_COMPOSITOR
1975 ec = drm_compositor_create(display, option_connector);
1979 fprintf(stderr, "failed to create compositor\n");
1983 if (shell_init(ec) < 0)
1986 if (wl_display_add_socket(display, option_socket_name)) {
1987 fprintf(stderr, "failed to add socket: %m\n");
1991 loop = wl_display_get_event_loop(ec->wl_display);
1992 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1993 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1995 wl_display_run(display);
1997 if (strstr(eglQueryString(ec->display, EGL_EXTENSIONS),
1998 "EGL_WL_bind_wayland_display"))
1999 eglUnbindWaylandDisplayWL(ec->display, display);
2000 wl_display_destroy(display);