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", 0, 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", 's', 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;
253 struct wlsc_compositor *compositor = surface->compositor;
256 wlsc_surface_damage(surface);
258 wl_list_remove(&surface->link);
259 if (surface->saved_texture == 0)
260 glDeleteTextures(1, &surface->texture);
262 glDeleteTextures(1, &surface->saved_texture);
265 if (surface->image != EGL_NO_IMAGE_KHR)
266 compositor->destroy_image(compositor->display,
269 wl_list_remove(&surface->buffer_link);
271 time = wlsc_compositor_get_time();
272 wl_list_for_each_safe(l, next,
273 &surface->surface.destroy_listener_list, link)
274 l->func(l, &surface->surface, time);
280 wlsc_load_image(const char *filename, int width, int height)
283 GError *error = NULL;
284 int stride, i, n_channels;
285 unsigned char *pixels, *end, *argb_pixels, *s, *d;
287 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
291 fprintf(stderr, "failed to load image: %s\n", error->message);
296 stride = gdk_pixbuf_get_rowstride(pixbuf);
297 pixels = gdk_pixbuf_get_pixels(pixbuf);
298 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
300 argb_pixels = malloc (height * width * 4);
301 if (argb_pixels == NULL) {
302 g_object_unref(pixbuf);
306 if (n_channels == 4) {
307 for (i = 0; i < height; i++) {
308 s = pixels + i * stride;
310 d = argb_pixels + i * width * 4;
314 #define MULT(_d,c,a,t) \
315 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
317 MULT(d[0], s[2], s[3], t);
318 MULT(d[1], s[1], s[3], t);
319 MULT(d[2], s[0], s[3], t);
325 } else if (n_channels == 3) {
326 for (i = 0; i < height; i++) {
327 s = pixels + i * stride;
329 d = argb_pixels + i * width * 4;
341 g_object_unref(pixbuf);
343 return (uint32_t *) argb_pixels;
347 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
349 struct wlsc_surface *es = (struct wlsc_surface *) surface;
350 struct wlsc_compositor *ec = es->compositor;
351 struct wl_list *surfaces_attached_to;
353 if (es->saved_texture != 0)
354 es->texture = es->saved_texture;
356 glBindTexture(GL_TEXTURE_2D, es->texture);
358 if (wl_buffer_is_shm(buffer)) {
359 /* Unbind any EGLImage texture that may be bound, so we don't
361 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
362 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
363 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
364 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
365 es->pitch, buffer->height, 0,
366 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
367 wl_shm_buffer_get_data(buffer));
368 es->visual = buffer->visual;
370 surfaces_attached_to = buffer->user_data;
373 wl_list_remove(&es->buffer_link);
374 wl_list_insert(surfaces_attached_to, &es->buffer_link);
376 es->image = ec->create_image(ec->display, NULL,
377 EGL_WAYLAND_BUFFER_WL,
380 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
381 es->visual = buffer->visual;
382 es->pitch = es->width;
387 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
389 struct wlsc_surface *es = (struct wlsc_surface *) surface;
390 struct wlsc_compositor *ec = es->compositor;
392 es->pitch = es->width;
393 es->image = sprite->image;
394 if (sprite->image != EGL_NO_IMAGE_KHR) {
395 glBindTexture(GL_TEXTURE_2D, es->texture);
396 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
398 if (es->saved_texture == 0)
399 es->saved_texture = es->texture;
400 es->texture = sprite->texture;
403 es->visual = sprite->visual;
407 SPRITE_USE_CURSOR = (1 << 0),
410 static struct wlsc_sprite *
411 create_sprite_from_png(struct wlsc_compositor *ec,
412 const char *filename, int width, int height,
416 struct wlsc_sprite *sprite;
418 pixels = wlsc_load_image(filename, width, height);
422 sprite = malloc(sizeof *sprite);
423 if (sprite == NULL) {
428 sprite->visual = &ec->compositor.premultiplied_argb_visual;
429 sprite->width = width;
430 sprite->height = height;
431 sprite->image = EGL_NO_IMAGE_KHR;
433 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
434 sprite->image = ec->create_cursor_image(ec, width, height);
436 glGenTextures(1, &sprite->texture);
437 glBindTexture(GL_TEXTURE_2D, sprite->texture);
438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
440 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
441 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
443 if (sprite->image != EGL_NO_IMAGE_KHR) {
444 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
445 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
446 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
448 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
449 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
457 static const struct {
458 const char *filename;
459 int hotspot_x, hotspot_y;
460 } pointer_images[] = {
461 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
462 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
463 { DATADIR "/wayland/bottom_side.png", 16, 20 },
464 { DATADIR "/wayland/grabbing.png", 20, 17 },
465 { DATADIR "/wayland/left_ptr.png", 10, 5 },
466 { DATADIR "/wayland/left_side.png", 10, 20 },
467 { DATADIR "/wayland/right_side.png", 30, 19 },
468 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
469 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
470 { DATADIR "/wayland/top_side.png", 18, 8 },
471 { DATADIR "/wayland/xterm.png", 15, 15 }
475 create_pointer_images(struct wlsc_compositor *ec)
478 const int width = 32, height = 32;
480 count = ARRAY_LENGTH(pointer_images);
481 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
482 for (i = 0; i < count; i++) {
483 ec->pointer_sprites[i] =
484 create_sprite_from_png(ec,
485 pointer_images[i].filename,
491 static struct wlsc_surface *
492 background_create(struct wlsc_output *output, const char *filename)
494 struct wlsc_surface *background;
495 struct wlsc_sprite *sprite;
497 background = wlsc_surface_create(output->compositor,
498 output->x, output->y,
499 output->width, output->height);
500 if (background == NULL)
503 sprite = create_sprite_from_png(output->compositor, filename,
504 output->width, output->height, 0);
505 if (sprite == NULL) {
510 wlsc_sprite_attach(sprite, &background->surface);
516 wlsc_surface_draw(struct wlsc_surface *es,
517 struct wlsc_output *output, pixman_region32_t *clip)
519 struct wlsc_compositor *ec = es->compositor;
520 GLfloat *v, inv_width, inv_height;
522 pixman_region32_t repaint;
523 pixman_box32_t *rectangles;
526 pixman_region32_init_rect(&repaint,
527 es->x, es->y, es->width, es->height);
528 pixman_region32_intersect(&repaint, &repaint, clip);
529 if (!pixman_region32_not_empty(&repaint))
532 if (es->visual == &ec->compositor.argb_visual) {
533 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
535 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
536 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
542 rectangles = pixman_region32_rectangles(&repaint, &n);
543 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
544 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
545 inv_width = 1.0 / es->pitch;
546 inv_height = 1.0 / es->height;
547 for (i = 0; i < n; i++, v += 16, p += 6) {
548 v[ 0] = rectangles[i].x1;
549 v[ 1] = rectangles[i].y1;
550 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
551 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
553 v[ 4] = rectangles[i].x1;
554 v[ 5] = rectangles[i].y2;
556 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
558 v[ 8] = rectangles[i].x2;
559 v[ 9] = rectangles[i].y1;
560 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
563 v[12] = rectangles[i].x2;
564 v[13] = rectangles[i].y2;
576 glBindTexture(GL_TEXTURE_2D, es->texture);
577 v = ec->vertices.data;
578 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
579 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
580 glEnableVertexAttribArray(0);
581 glEnableVertexAttribArray(1);
582 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
584 ec->vertices.size = 0;
585 ec->indices.size = 0;
586 pixman_region32_fini(&repaint);
590 wlsc_surface_raise(struct wlsc_surface *surface)
592 struct wlsc_compositor *compositor = surface->compositor;
594 wl_list_remove(&surface->link);
595 wl_list_insert(&compositor->surface_list, &surface->link);
599 wlsc_surface_update_matrix(struct wlsc_surface *es)
601 wlsc_matrix_init(&es->matrix);
602 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
603 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
605 wlsc_matrix_init(&es->matrix_inv);
606 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
607 wlsc_matrix_scale(&es->matrix_inv,
608 1.0 / es->width, 1.0 / es->height, 1);
612 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
614 struct wlsc_output *output;
616 wl_list_for_each(output, &compositor->output_list, link)
617 wlsc_output_damage(output);
621 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
623 struct wlsc_compositor *compositor = output->compositor;
624 struct wlsc_surface *es;
625 struct wlsc_animation *animation, *next;
627 wl_list_for_each(es, &compositor->surface_list, link) {
628 if (es->output == output) {
629 wl_display_post_frame(compositor->wl_display,
630 &es->surface, msecs);
634 output->finished = 1;
636 wl_event_source_timer_update(compositor->timer_source, 5);
637 compositor->repaint_on_timeout = 1;
639 wl_list_for_each_safe(animation, next,
640 &compositor->animation_list, link)
641 animation->frame(animation, output, msecs);
645 wlsc_output_damage(struct wlsc_output *output)
647 struct wlsc_compositor *compositor = output->compositor;
649 pixman_region32_union_rect(&compositor->damage_region,
650 &compositor->damage_region,
651 output->x, output->y,
652 output->width, output->height);
653 wlsc_compositor_schedule_repaint(compositor);
657 fade_frame(struct wlsc_animation *animation,
658 struct wlsc_output *output, uint32_t msecs)
660 struct wlsc_compositor *compositor =
661 container_of(animation,
662 struct wlsc_compositor, fade.animation);
664 wlsc_tweener_update(&compositor->fade.tweener, msecs);
665 if (wlsc_tweener_done(&compositor->fade.tweener)) {
666 if (compositor->fade.tweener.current > 0.999) {
667 compositor->state = WLSC_COMPOSITOR_SLEEPING;
668 compositor->shell->lock(compositor->shell);
670 compositor->fade.tweener.current =
671 compositor->fade.tweener.target;
672 wl_list_remove(&animation->link);
673 wl_list_init(&animation->link);
676 wlsc_output_damage(output);
680 fade_output(struct wlsc_output *output,
681 GLfloat tint, pixman_region32_t *region)
683 struct wlsc_compositor *compositor = output->compositor;
684 struct wlsc_surface surface;
685 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
687 surface.compositor = compositor;
688 surface.x = output->x;
689 surface.y = output->y;
690 surface.width = output->width;
691 surface.height = output->height;
692 surface.texture = GL_NONE;
696 &compositor->compositor.premultiplied_argb_visual;
698 surface.visual = &compositor->compositor.rgb_visual;
700 glUseProgram(compositor->solid_shader.program);
701 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
702 1, GL_FALSE, output->matrix.d);
703 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
704 wlsc_surface_draw(&surface, output, region);
708 wlsc_output_repaint(struct wlsc_output *output)
710 struct wlsc_compositor *ec = output->compositor;
711 struct wlsc_surface *es;
712 struct wlsc_input_device *eid;
713 pixman_region32_t new_damage, total_damage, repaint;
714 int using_hardware_cursor = 1;
716 output->prepare_render(output);
718 glViewport(0, 0, output->width, output->height);
720 glUseProgram(ec->texture_shader.program);
721 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
722 1, GL_FALSE, output->matrix.d);
723 glUniform1i(ec->texture_shader.tex_uniform, 0);
725 pixman_region32_init(&new_damage);
726 pixman_region32_init(&total_damage);
727 pixman_region32_intersect_rect(&new_damage,
729 output->x, output->y,
730 output->width, output->height);
731 pixman_region32_subtract(&ec->damage_region,
732 &ec->damage_region, &new_damage);
733 pixman_region32_union(&total_damage, &new_damage,
734 &output->previous_damage_region);
735 pixman_region32_copy(&output->previous_damage_region, &new_damage);
738 if (output->set_hardware_cursor(output, ec->input_device) < 0)
739 using_hardware_cursor = 0;
740 if (ec->fade.tweener.current > 0.001)
741 using_hardware_cursor = 0;
743 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
744 if (es->fullscreen_output == output) {
745 if (es->visual == &ec->compositor.rgb_visual &&
746 using_hardware_cursor) {
747 if (output->prepare_scanout_surface(output, es) == 0) {
748 /* We're drawing nothing now,
749 * draw the damaged regions later. */
750 pixman_region32_union(&ec->damage_region,
757 if (es->width < output->width ||
758 es->height < output->height)
759 glClear(GL_COLOR_BUFFER_BIT);
760 wlsc_surface_draw(es, output, &total_damage);
762 wl_list_for_each(es, &ec->surface_list, link) {
763 if (es->visual != &ec->compositor.rgb_visual)
766 pixman_region32_init_rect(&repaint,
768 es->width, es->height);
769 wlsc_surface_draw(es, output, &total_damage);
770 pixman_region32_subtract(&total_damage,
771 &total_damage, &repaint);
774 if (output->background)
775 wlsc_surface_draw(output->background,
776 output, &total_damage);
778 fade_output(output, 1.0, &total_damage);
780 glUseProgram(ec->texture_shader.program);
781 wl_list_for_each_reverse(es, &ec->surface_list, link) {
782 if (ec->overlay == es)
785 if (es->visual == &ec->compositor.rgb_visual) {
786 pixman_region32_union_rect(&total_damage,
789 es->width, es->height);
793 wlsc_surface_draw(es, output, &total_damage);
798 wlsc_surface_draw(ec->overlay, output, &total_damage);
801 wl_list_for_each(eid, &ec->input_device_list, link) {
802 if (&eid->input_device != ec->input_device ||
803 !using_hardware_cursor)
804 wlsc_surface_draw(eid->sprite, output,
808 if (ec->fade.tweener.current > 0.001)
809 fade_output(output, ec->fade.tweener.current, &total_damage);
815 struct wlsc_compositor *ec = data;
816 struct wlsc_output *output;
817 int repainted_all_outputs = 1;
819 wl_list_for_each(output, &ec->output_list, link) {
820 if (!output->repaint_needed)
823 if (!output->finished) {
824 repainted_all_outputs = 0;
828 wlsc_output_repaint(output);
829 output->finished = 0;
830 output->repaint_needed = 0;
831 output->present(output);
834 if (repainted_all_outputs)
835 ec->repaint_on_timeout = 0;
837 wl_event_source_timer_update(ec->timer_source, 1);
843 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
845 struct wlsc_output *output;
847 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
850 wl_list_for_each(output, &compositor->output_list, link)
851 output->repaint_needed = 1;
853 if (compositor->repaint_on_timeout)
856 wl_event_source_timer_update(compositor->timer_source, 1);
857 compositor->repaint_on_timeout = 1;
861 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
865 done = wlsc_tweener_done(&compositor->fade.tweener);
866 compositor->fade.tweener.target = tint;
867 if (wlsc_tweener_done(&compositor->fade.tweener))
871 compositor->fade.tweener.timestamp =
872 wlsc_compositor_get_time();
874 wlsc_compositor_damage_all(compositor);
875 if (wl_list_empty(&compositor->fade.animation.link))
876 wl_list_insert(compositor->animation_list.prev,
877 &compositor->fade.animation.link);
881 surface_destroy(struct wl_client *client,
882 struct wl_surface *surface)
884 wl_resource_destroy(&surface->resource, client);
888 wlsc_surface_assign_output(struct wlsc_surface *es)
890 struct wlsc_compositor *ec = es->compositor;
891 struct wlsc_output *output;
893 struct wlsc_output *tmp = es->output;
896 wl_list_for_each(output, &ec->output_list, link) {
897 if (output->x < es->x && es->x < output->x + output->width &&
898 output->y < es->y && es->y < output->y + output->height) {
900 printf("assiging surface %p to output %p\n",
906 if (es->output == NULL) {
907 printf("no output found\n");
908 es->output = container_of(ec->output_list.next,
909 struct wlsc_output, link);
914 surface_attach(struct wl_client *client,
915 struct wl_surface *surface, struct wl_buffer *buffer,
916 int32_t x, int32_t y)
918 struct wlsc_surface *es = (struct wlsc_surface *) surface;
920 /* FIXME: This damages the entire old surface, but we should
921 * really just damage the part that's no longer covered by the
922 * surface. Anything covered by the new surface will be
923 * damaged by the client. */
924 wlsc_surface_damage(es);
926 wlsc_buffer_attach(buffer, surface);
929 switch (es->map_type) {
930 case WLSC_SURFACE_MAP_FULLSCREEN:
931 es->x = (es->fullscreen_output->width - es->width) / 2;
932 es->y = (es->fullscreen_output->height - es->height) / 2;
939 es->width = buffer->width;
940 es->height = buffer->height;
941 if (x != 0 || y != 0)
942 wlsc_surface_assign_output(es);
943 wlsc_surface_update_matrix(es);
945 es->compositor->shell->attach(es->compositor->shell, es);
949 surface_map_toplevel(struct wl_client *client,
950 struct wl_surface *surface)
952 struct wlsc_surface *es = (struct wlsc_surface *) surface;
953 struct wlsc_compositor *ec = es->compositor;
955 switch (es->map_type) {
956 case WLSC_SURFACE_MAP_UNMAPPED:
957 es->x = 10 + random() % 400;
958 es->y = 10 + random() % 400;
959 wlsc_surface_update_matrix(es);
960 /* assign to first output */
961 es->output = container_of(ec->output_list.next,
962 struct wlsc_output, link);
963 wl_list_insert(&es->compositor->surface_list, &es->link);
965 case WLSC_SURFACE_MAP_TOPLEVEL:
967 case WLSC_SURFACE_MAP_FULLSCREEN:
968 es->fullscreen_output = NULL;
971 wlsc_surface_update_matrix(es);
977 wlsc_surface_damage(es);
978 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
982 surface_map_transient(struct wl_client *client,
983 struct wl_surface *surface, struct wl_surface *parent,
984 int x, int y, uint32_t flags)
986 struct wlsc_surface *es = (struct wlsc_surface *) surface;
987 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
989 switch (es->map_type) {
990 case WLSC_SURFACE_MAP_UNMAPPED:
991 wl_list_insert(&es->compositor->surface_list, &es->link);
992 /* assign to parents output */
993 es->output = pes->output;
995 case WLSC_SURFACE_MAP_FULLSCREEN:
996 es->fullscreen_output = NULL;
1005 wlsc_surface_update_matrix(es);
1006 wlsc_surface_damage(es);
1007 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
1011 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
1013 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1014 struct wlsc_output *output;
1016 /* FIXME: Fullscreen on first output */
1017 /* FIXME: Handle output going away */
1018 output = container_of(es->compositor->output_list.next,
1019 struct wlsc_output, link);
1021 switch (es->map_type) {
1022 case WLSC_SURFACE_MAP_UNMAPPED:
1023 es->x = 10 + random() % 400;
1024 es->y = 10 + random() % 400;
1025 /* assign to first output */
1026 es->output = output;
1027 wl_list_insert(&es->compositor->surface_list, &es->link);
1029 case WLSC_SURFACE_MAP_FULLSCREEN:
1035 es->saved_x = es->x;
1036 es->saved_y = es->y;
1037 es->x = (output->width - es->width) / 2;
1038 es->y = (output->height - es->height) / 2;
1039 es->fullscreen_output = output;
1040 wlsc_surface_update_matrix(es);
1041 wlsc_surface_damage(es);
1042 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
1046 surface_damage(struct wl_client *client,
1047 struct wl_surface *surface,
1048 int32_t x, int32_t y, int32_t width, int32_t height)
1050 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1052 wlsc_surface_damage_rectangle(es, x, y, width, height);
1055 const static struct wl_surface_interface surface_interface = {
1058 surface_map_toplevel,
1059 surface_map_transient,
1060 surface_map_fullscreen,
1065 wlsc_input_device_attach(struct wlsc_input_device *device,
1066 int x, int y, int width, int height)
1068 wlsc_surface_damage(device->sprite);
1070 device->hotspot_x = x;
1071 device->hotspot_y = y;
1073 device->sprite->x = device->input_device.x - device->hotspot_x;
1074 device->sprite->y = device->input_device.y - device->hotspot_y;
1075 device->sprite->width = width;
1076 device->sprite->height = height;
1077 wlsc_surface_update_matrix(device->sprite);
1079 wlsc_surface_damage(device->sprite);
1083 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1084 struct wl_buffer *buffer, int x, int y)
1086 wlsc_buffer_attach(buffer, &device->sprite->surface);
1087 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1091 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1092 struct wlsc_sprite *sprite, int x, int y)
1094 wlsc_sprite_attach(sprite, &device->sprite->surface);
1095 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1099 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1100 enum wlsc_pointer_type type)
1102 struct wlsc_compositor *compositor =
1103 (struct wlsc_compositor *) device->input_device.compositor;
1105 wlsc_input_device_attach_sprite(device,
1106 compositor->pointer_sprites[type],
1107 pointer_images[type].hotspot_x,
1108 pointer_images[type].hotspot_y);
1112 compositor_create_surface(struct wl_client *client,
1113 struct wl_compositor *compositor, uint32_t id)
1115 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1116 struct wlsc_surface *surface;
1118 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1119 if (surface == NULL) {
1120 wl_client_post_no_memory(client);
1124 surface->surface.resource.destroy = destroy_surface;
1126 surface->surface.resource.object.id = id;
1127 surface->surface.resource.object.interface = &wl_surface_interface;
1128 surface->surface.resource.object.implementation =
1129 (void (**)(void)) &surface_interface;
1130 surface->surface.client = client;
1132 wl_client_add_resource(client, &surface->surface.resource);
1135 const static struct wl_compositor_interface compositor_interface = {
1136 compositor_create_surface,
1140 wlsc_surface_transform(struct wlsc_surface *surface,
1141 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1143 struct wlsc_vector v = { { x, y, 0, 1 } };
1145 wlsc_matrix_transform(&surface->matrix_inv, &v);
1146 *sx = v.f[0] * surface->width;
1147 *sy = v.f[1] * surface->height;
1150 struct wlsc_surface *
1151 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1153 struct wlsc_compositor *ec =
1154 (struct wlsc_compositor *) device->compositor;
1155 struct wlsc_surface *es;
1157 wl_list_for_each(es, &ec->surface_list, link) {
1158 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1159 if (0 <= *sx && *sx < es->width &&
1160 0 <= *sy && *sy < es->height)
1169 motion_grab_motion(struct wl_grab *grab,
1170 uint32_t time, int32_t x, int32_t y)
1172 struct wlsc_input_device *device =
1173 (struct wlsc_input_device *) grab->input_device;
1174 struct wlsc_surface *es =
1175 (struct wlsc_surface *) device->input_device.pointer_focus;
1178 wlsc_surface_transform(es, x, y, &sx, &sy);
1179 wl_client_post_event(es->surface.client,
1180 &device->input_device.object,
1181 WL_INPUT_DEVICE_MOTION,
1182 time, x, y, sx, sy);
1186 motion_grab_button(struct wl_grab *grab,
1187 uint32_t time, int32_t button, int32_t state)
1189 wl_client_post_event(grab->input_device->pointer_focus->client,
1190 &grab->input_device->object,
1191 WL_INPUT_DEVICE_BUTTON,
1192 time, button, state);
1196 motion_grab_end(struct wl_grab *grab, uint32_t time)
1200 static const struct wl_grab_interface motion_grab_interface = {
1207 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1209 if (compositor->idle_inhibit)
1212 wlsc_compositor_fade(compositor, 0.0);
1213 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1215 wl_event_source_timer_update(compositor->idle_source,
1216 option_idle_time * 1000);
1220 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1222 wlsc_compositor_wake(compositor);
1223 compositor->idle_inhibit++;
1227 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1229 compositor->idle_inhibit--;
1230 wlsc_compositor_wake(compositor);
1234 idle_handler(void *data)
1236 struct wlsc_compositor *compositor = data;
1238 if (compositor->idle_inhibit)
1241 wlsc_compositor_fade(compositor, 1.0);
1247 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1249 struct wlsc_surface *es;
1250 struct wlsc_compositor *ec =
1251 (struct wlsc_compositor *) device->compositor;
1252 struct wlsc_output *output;
1253 const struct wl_grab_interface *interface;
1254 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1256 int x_valid = 0, y_valid = 0;
1257 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1259 wlsc_compositor_wake(ec);
1261 wl_list_for_each(output, &ec->output_list, link) {
1262 if (output->x <= x && x <= output->x + output->width)
1265 if (output->y <= y && y <= output->y + output->height)
1268 /* FIXME: calculate this only on output addition/deletion */
1269 if (output->x < min_x)
1271 if (output->y < min_y)
1274 if (output->x + output->width > max_x)
1275 max_x = output->x + output->width;
1276 if (output->y + output->height > max_y)
1277 max_y = output->y + output->height;
1283 else if (x >= max_x)
1289 else if (y >= max_y)
1297 interface = device->grab->interface;
1298 interface->motion(device->grab, time, x, y);
1300 es = pick_surface(device, &sx, &sy);
1301 wl_input_device_set_pointer_focus(device,
1303 time, x, y, sx, sy);
1305 wl_client_post_event(es->surface.client,
1307 WL_INPUT_DEVICE_MOTION,
1308 time, x, y, sx, sy);
1311 wlsc_surface_damage(wd->sprite);
1313 wd->sprite->x = device->x - wd->hotspot_x;
1314 wd->sprite->y = device->y - wd->hotspot_y;
1315 wlsc_surface_update_matrix(wd->sprite);
1317 wlsc_surface_damage(wd->sprite);
1321 wlsc_surface_activate(struct wlsc_surface *surface,
1322 struct wlsc_input_device *device, uint32_t time)
1324 wlsc_surface_raise(surface);
1325 if (device->selection)
1326 wlsc_selection_set_focus(device->selection,
1327 &surface->surface, time);
1329 wl_input_device_set_keyboard_focus(&device->input_device,
1334 struct wlsc_binding {
1338 wlsc_binding_handler_t handler;
1340 struct wl_list link;
1344 notify_button(struct wl_input_device *device,
1345 uint32_t time, int32_t button, int32_t state)
1347 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1348 struct wlsc_compositor *compositor =
1349 (struct wlsc_compositor *) device->compositor;
1350 struct wlsc_binding *b;
1351 struct wlsc_surface *surface =
1352 (struct wlsc_surface *) device->pointer_focus;
1355 wlsc_compositor_idle_inhibit(compositor);
1357 wlsc_compositor_idle_release(compositor);
1359 if (state && surface && device->grab == NULL) {
1360 wlsc_surface_activate(surface, wd, time);
1361 wl_input_device_start_grab(device,
1362 &device->motion_grab,
1366 wl_list_for_each(b, &compositor->binding_list, link) {
1367 if (b->button == button &&
1368 b->modifier == wd->modifier_state && state) {
1369 b->handler(&wd->input_device,
1370 time, 0, button, state, b->data);
1376 device->grab->interface->button(device->grab, time,
1379 if (!state && device->grab && device->grab_button == button)
1380 wl_input_device_end_grab(device, time);
1384 terminate_binding(struct wl_input_device *device, uint32_t time,
1385 uint32_t key, uint32_t button, uint32_t state, void *data)
1387 struct wlsc_compositor *compositor = data;
1390 wl_display_terminate(compositor->wl_display);
1393 WL_EXPORT struct wlsc_binding *
1394 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1395 uint32_t key, uint32_t button, uint32_t modifier,
1396 wlsc_binding_handler_t handler, void *data)
1398 struct wlsc_binding *binding;
1400 binding = malloc(sizeof *binding);
1401 if (binding == NULL)
1405 binding->button = button;
1406 binding->modifier = modifier;
1407 binding->handler = handler;
1408 binding->data = data;
1409 wl_list_insert(compositor->binding_list.prev, &binding->link);
1415 wlsc_binding_destroy(struct wlsc_binding *binding)
1417 wl_list_remove(&binding->link);
1422 update_modifier_state(struct wlsc_input_device *device,
1423 uint32_t key, uint32_t state)
1430 modifier = MODIFIER_CTRL;
1435 modifier = MODIFIER_ALT;
1440 modifier = MODIFIER_SUPER;
1449 device->modifier_state |= modifier;
1451 device->modifier_state &= ~modifier;
1455 notify_key(struct wl_input_device *device,
1456 uint32_t time, uint32_t key, uint32_t state)
1458 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1459 struct wlsc_compositor *compositor =
1460 (struct wlsc_compositor *) device->compositor;
1462 struct wlsc_binding *b;
1465 wlsc_compositor_idle_inhibit(compositor);
1467 wlsc_compositor_idle_release(compositor);
1469 wl_list_for_each(b, &compositor->binding_list, link) {
1470 if (b->key == key &&
1471 b->modifier == wd->modifier_state) {
1472 b->handler(&wd->input_device,
1473 time, key, 0, state, b->data);
1478 update_modifier_state(wd, key, state);
1479 end = device->keys.data + device->keys.size;
1480 for (k = device->keys.data; k < end; k++) {
1484 device->keys.size = (void *) end - device->keys.data;
1486 k = wl_array_add(&device->keys, sizeof *k);
1490 if (device->keyboard_focus != NULL)
1491 wl_client_post_event(device->keyboard_focus->client,
1493 WL_INPUT_DEVICE_KEY, time, key, state);
1497 notify_pointer_focus(struct wl_input_device *device,
1498 uint32_t time, struct wlsc_output *output,
1499 int32_t x, int32_t y)
1501 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1502 struct wlsc_compositor *compositor =
1503 (struct wlsc_compositor *) device->compositor;
1504 struct wlsc_surface *es;
1510 es = pick_surface(device, &sx, &sy);
1511 wl_input_device_set_pointer_focus(device,
1513 time, x, y, sx, sy);
1515 compositor->focus = 1;
1517 wd->sprite->x = device->x - wd->hotspot_x;
1518 wd->sprite->y = device->y - wd->hotspot_y;
1519 wlsc_surface_update_matrix(wd->sprite);
1521 wl_input_device_set_pointer_focus(device, NULL,
1523 compositor->focus = 0;
1526 wlsc_surface_damage(wd->sprite);
1530 notify_keyboard_focus(struct wl_input_device *device,
1531 uint32_t time, struct wlsc_output *output,
1532 struct wl_array *keys)
1534 struct wlsc_input_device *wd =
1535 (struct wlsc_input_device *) device;
1536 struct wlsc_compositor *compositor =
1537 (struct wlsc_compositor *) device->compositor;
1538 struct wlsc_surface *es;
1541 if (!wl_list_empty(&compositor->surface_list))
1542 es = container_of(compositor->surface_list.next,
1543 struct wlsc_surface, link);
1548 wl_array_copy(&wd->input_device.keys, keys);
1549 wd->modifier_state = 0;
1550 end = device->keys.data + device->keys.size;
1551 for (k = device->keys.data; k < end; k++) {
1552 wlsc_compositor_idle_inhibit(compositor);
1553 update_modifier_state(wd, *k, 1);
1556 wl_input_device_set_keyboard_focus(&wd->input_device,
1557 &es->surface, time);
1559 end = device->keys.data + device->keys.size;
1560 for (k = device->keys.data; k < end; k++)
1561 wlsc_compositor_idle_release(compositor);
1563 wd->modifier_state = 0;
1564 wl_input_device_set_keyboard_focus(&wd->input_device,
1571 input_device_attach(struct wl_client *client,
1572 struct wl_input_device *device_base,
1574 struct wl_buffer *buffer, int32_t x, int32_t y)
1576 struct wlsc_input_device *device =
1577 (struct wlsc_input_device *) device_base;
1579 if (time < device->input_device.pointer_focus_time)
1581 if (device->input_device.pointer_focus == NULL)
1583 if (device->input_device.pointer_focus->client != client)
1586 if (buffer == NULL) {
1587 wlsc_input_device_set_pointer_image(device,
1588 WLSC_POINTER_LEFT_PTR);
1592 wlsc_input_device_attach_buffer(device, buffer, x, y);
1595 const static struct wl_input_device_interface input_device_interface = {
1596 input_device_attach,
1600 wlsc_input_device_init(struct wlsc_input_device *device,
1601 struct wlsc_compositor *ec)
1603 wl_input_device_init(&device->input_device, &ec->compositor);
1605 device->input_device.object.interface = &wl_input_device_interface;
1606 device->input_device.object.implementation =
1607 (void (**)(void)) &input_device_interface;
1608 wl_display_add_object(ec->wl_display, &device->input_device.object);
1609 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1611 device->sprite = wlsc_surface_create(ec,
1612 device->input_device.x,
1613 device->input_device.y, 32, 32);
1614 device->hotspot_x = 16;
1615 device->hotspot_y = 16;
1616 device->modifier_state = 0;
1618 device->input_device.motion_grab.interface = &motion_grab_interface;
1620 wl_list_insert(ec->input_device_list.prev, &device->link);
1622 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1626 wlsc_output_post_geometry(struct wl_client *client,
1627 struct wl_object *global, uint32_t version)
1629 struct wlsc_output *output =
1630 container_of(global, struct wlsc_output, object);
1632 wl_client_post_event(client, global,
1634 output->x, output->y,
1635 output->width, output->height);
1638 static const char vertex_shader[] =
1639 "uniform mat4 proj;\n"
1640 "attribute vec2 position;\n"
1641 "attribute vec2 texcoord;\n"
1642 "varying vec2 v_texcoord;\n"
1645 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1646 " v_texcoord = texcoord;\n"
1649 static const char texture_fragment_shader[] =
1650 "precision mediump float;\n"
1651 "varying vec2 v_texcoord;\n"
1652 "uniform sampler2D tex;\n"
1655 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1658 static const char solid_fragment_shader[] =
1659 "precision mediump float;\n"
1660 "varying vec2 v_texcoord;\n"
1661 "uniform vec4 color;\n"
1664 " gl_FragColor = color\n;"
1668 compile_shader(GLenum type, const char *source)
1674 s = glCreateShader(type);
1675 glShaderSource(s, 1, &source, NULL);
1677 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1679 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1680 fprintf(stderr, "shader info: %s\n", msg);
1688 wlsc_shader_init(struct wlsc_shader *shader,
1689 const char *vertex_source, const char *fragment_source)
1694 shader->vertex_shader =
1695 compile_shader(GL_VERTEX_SHADER, vertex_source);
1696 shader->fragment_shader =
1697 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1699 shader->program = glCreateProgram();
1700 glAttachShader(shader->program, shader->vertex_shader);
1701 glAttachShader(shader->program, shader->fragment_shader);
1702 glBindAttribLocation(shader->program, 0, "position");
1703 glBindAttribLocation(shader->program, 1, "texcoord");
1705 glLinkProgram(shader->program);
1706 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1708 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1709 fprintf(stderr, "link info: %s\n", msg);
1713 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1714 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1720 init_solid_shader(struct wlsc_shader *shader,
1721 GLuint vertex_shader, const char *fragment_source)
1726 shader->vertex_shader = vertex_shader;
1727 shader->fragment_shader =
1728 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1730 shader->program = glCreateProgram();
1731 glAttachShader(shader->program, shader->vertex_shader);
1732 glAttachShader(shader->program, shader->fragment_shader);
1733 glBindAttribLocation(shader->program, 0, "position");
1734 glBindAttribLocation(shader->program, 1, "texcoord");
1736 glLinkProgram(shader->program);
1737 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1739 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1740 fprintf(stderr, "link info: %s\n", msg);
1744 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1745 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1751 wlsc_output_destroy(struct wlsc_output *output)
1753 destroy_surface(&output->background->surface.resource, NULL);
1757 wlsc_output_move(struct wlsc_output *output, int x, int y)
1759 struct wlsc_compositor *c = output->compositor;
1765 if (output->background) {
1766 output->background->x = x;
1767 output->background->y = y;
1768 wlsc_surface_update_matrix(output->background);
1771 pixman_region32_init(&output->previous_damage_region);
1773 wlsc_matrix_init(&output->matrix);
1774 wlsc_matrix_translate(&output->matrix,
1775 -output->x - output->width / 2.0,
1776 -output->y - output->height / 2.0, 0);
1778 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1779 wlsc_matrix_scale(&output->matrix,
1780 2.0 / output->width,
1781 flip * 2.0 / output->height, 1);
1783 pixman_region32_union_rect(&c->damage_region,
1785 x, y, output->width, output->height);
1789 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1790 int x, int y, int width, int height, uint32_t flags)
1792 output->compositor = c;
1795 output->width = width;
1796 output->height = height;
1798 output->background =
1799 background_create(output, option_background);
1801 output->flags = flags;
1802 output->finished = 1;
1803 wlsc_output_move(output, x, y);
1805 output->object.interface = &wl_output_interface;
1806 wl_display_add_object(c->wl_display, &output->object);
1807 wl_display_add_global(c->wl_display, &output->object,
1808 wlsc_output_post_geometry);
1812 shm_buffer_created(struct wl_buffer *buffer)
1814 struct wl_list *surfaces_attached_to;
1816 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1817 if (!surfaces_attached_to) {
1818 buffer->user_data = NULL;
1822 wl_list_init(surfaces_attached_to);
1824 buffer->user_data = surfaces_attached_to;
1828 shm_buffer_damaged(struct wl_buffer *buffer,
1829 int32_t x, int32_t y, int32_t width, int32_t height)
1831 struct wl_list *surfaces_attached_to = buffer->user_data;
1832 struct wlsc_surface *es;
1833 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1835 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1836 glBindTexture(GL_TEXTURE_2D, es->texture);
1837 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1838 tex_width, buffer->height, 0,
1839 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1840 wl_shm_buffer_get_data(buffer));
1841 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1842 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1847 shm_buffer_destroyed(struct wl_buffer *buffer)
1849 struct wl_list *surfaces_attached_to = buffer->user_data;
1850 struct wlsc_surface *es, *next;
1852 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1854 wl_list_remove(&es->buffer_link);
1857 free(surfaces_attached_to);
1860 const static struct wl_shm_callbacks shm_callbacks = {
1863 shm_buffer_destroyed
1867 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1869 struct wl_event_loop *loop;
1870 const char *extensions;
1872 ec->wl_display = display;
1874 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1876 ec->shm = wl_shm_init(display, &shm_callbacks);
1878 ec->image_target_texture_2d =
1879 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1880 ec->image_target_renderbuffer_storage = (void *)
1881 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1882 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1883 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1885 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1886 ec->unbind_display =
1887 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1889 extensions = (const char *) glGetString(GL_EXTENSIONS);
1890 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1892 "GL_EXT_texture_format_BGRA8888 not available\n");
1897 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1898 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1899 ec->has_bind_display = 1;
1900 if (ec->has_bind_display)
1901 ec->bind_display(ec->display, ec->wl_display);
1903 wl_list_init(&ec->surface_list);
1904 wl_list_init(&ec->input_device_list);
1905 wl_list_init(&ec->output_list);
1906 wl_list_init(&ec->binding_list);
1907 wl_list_init(&ec->animation_list);
1908 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1909 ec->fade.animation.frame = fade_frame;
1910 wl_list_init(&ec->fade.animation.link);
1912 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1913 MODIFIER_CTRL | MODIFIER_ALT,
1914 terminate_binding, ec);
1916 create_pointer_images(ec);
1918 screenshooter_create(ec);
1920 glActiveTexture(GL_TEXTURE0);
1922 if (wlsc_shader_init(&ec->texture_shader,
1923 vertex_shader, texture_fragment_shader) < 0)
1925 if (init_solid_shader(&ec->solid_shader,
1926 ec->texture_shader.vertex_shader,
1927 solid_fragment_shader) < 0)
1930 loop = wl_display_get_event_loop(ec->wl_display);
1931 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1932 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1934 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1935 pixman_region32_init(&ec->damage_region);
1936 wlsc_compositor_schedule_repaint(ec);
1941 static int on_term_signal(int signal_number, void *data)
1943 struct wlsc_compositor *ec = data;
1945 wl_display_terminate(ec->wl_display);
1950 int main(int argc, char *argv[])
1952 struct wl_display *display;
1953 struct wlsc_compositor *ec;
1954 struct wl_event_loop *loop;
1955 GError *error = NULL;
1956 GOptionContext *context;
1959 int (*shell_init)(struct wlsc_compositor *ec);
1961 g_type_init(); /* GdkPixbuf needs this, it seems. */
1963 context = g_option_context_new(NULL);
1964 g_option_context_add_main_entries(context, option_entries, "Wayland");
1965 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1966 fprintf(stderr, "option parsing failed: %s\n", error->message);
1969 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1970 fprintf(stderr, "invalid geometry option: %s \n",
1975 display = wl_display_create();
1979 shell_init = desktop_shell_init;
1981 shell_module = dlopen(option_shell, RTLD_LAZY);
1982 if (!shell_module) {
1983 fprintf(stderr, "failed to load shell module: %m\n");
1986 shell_init = dlsym(shell_module, "shell_init");
1989 "failed to lookup shell init function: %m\n");
1994 #if BUILD_WAYLAND_COMPOSITOR
1995 if (getenv("WAYLAND_DISPLAY"))
1996 ec = wayland_compositor_create(display, width, height);
1999 #if BUILD_X11_COMPOSITOR
2000 if (ec == NULL && getenv("DISPLAY"))
2001 ec = x11_compositor_create(display, width, height);
2004 #if BUILD_OPENWFD_COMPOSITOR
2005 if (ec == NULL && getenv("OPENWFD"))
2006 ec = wfd_compositor_create(display, option_connector);
2009 #if BUILD_DRM_COMPOSITOR
2011 ec = drm_compositor_create(display, option_connector);
2015 fprintf(stderr, "failed to create compositor\n");
2019 if (shell_init(ec) < 0)
2022 if (wl_display_add_socket(display, option_socket_name)) {
2023 fprintf(stderr, "failed to add socket: %m\n");
2027 loop = wl_display_get_event_loop(ec->wl_display);
2028 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
2029 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
2031 wl_display_run(display);
2033 if (ec->has_bind_display)
2034 ec->unbind_display(ec->display, display);
2035 wl_display_destroy(display);