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 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
363 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
364 es->pitch, buffer->height, 0,
365 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
366 wl_shm_buffer_get_data(buffer));
367 es->visual = buffer->visual;
369 surfaces_attached_to = buffer->user_data;
372 wl_list_remove(&es->buffer_link);
373 wl_list_insert(surfaces_attached_to, &es->buffer_link);
375 es->image = eglCreateImageKHR(ec->display, NULL,
376 EGL_WAYLAND_BUFFER_WL,
379 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
380 es->visual = buffer->visual;
381 es->pitch = es->width;
386 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
388 struct wlsc_surface *es = (struct wlsc_surface *) surface;
390 es->pitch = es->width;
391 es->image = sprite->image;
392 if (sprite->image != EGL_NO_IMAGE_KHR) {
393 glBindTexture(GL_TEXTURE_2D, es->texture);
394 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
396 if (es->saved_texture == 0)
397 es->saved_texture = es->texture;
398 es->texture = sprite->texture;
401 es->visual = sprite->visual;
405 SPRITE_USE_CURSOR = (1 << 0),
408 static struct wlsc_sprite *
409 create_sprite_from_png(struct wlsc_compositor *ec,
410 const char *filename, int width, int height,
414 struct wlsc_sprite *sprite;
416 pixels = wlsc_load_image(filename, width, height);
420 sprite = malloc(sizeof *sprite);
421 if (sprite == NULL) {
426 sprite->visual = &ec->compositor.premultiplied_argb_visual;
427 sprite->width = width;
428 sprite->height = height;
429 sprite->image = EGL_NO_IMAGE_KHR;
431 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
432 sprite->image = ec->create_cursor_image(ec, width, height);
434 glGenTextures(1, &sprite->texture);
435 glBindTexture(GL_TEXTURE_2D, sprite->texture);
436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
437 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
441 if (sprite->image != EGL_NO_IMAGE_KHR) {
442 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
443 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
444 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
446 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
447 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
455 static const struct {
456 const char *filename;
457 int hotspot_x, hotspot_y;
458 } pointer_images[] = {
459 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
460 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
461 { DATADIR "/wayland/bottom_side.png", 16, 20 },
462 { DATADIR "/wayland/grabbing.png", 20, 17 },
463 { DATADIR "/wayland/left_ptr.png", 10, 5 },
464 { DATADIR "/wayland/left_side.png", 10, 20 },
465 { DATADIR "/wayland/right_side.png", 30, 19 },
466 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
467 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
468 { DATADIR "/wayland/top_side.png", 18, 8 },
469 { DATADIR "/wayland/xterm.png", 15, 15 }
473 create_pointer_images(struct wlsc_compositor *ec)
476 const int width = 32, height = 32;
478 count = ARRAY_LENGTH(pointer_images);
479 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
480 for (i = 0; i < count; i++) {
481 ec->pointer_sprites[i] =
482 create_sprite_from_png(ec,
483 pointer_images[i].filename,
489 static struct wlsc_surface *
490 background_create(struct wlsc_output *output, const char *filename)
492 struct wlsc_surface *background;
493 struct wlsc_sprite *sprite;
495 background = wlsc_surface_create(output->compositor,
496 output->x, output->y,
497 output->width, output->height);
498 if (background == NULL)
501 sprite = create_sprite_from_png(output->compositor, filename,
502 output->width, output->height, 0);
503 if (sprite == NULL) {
508 wlsc_sprite_attach(sprite, &background->surface);
514 wlsc_surface_draw(struct wlsc_surface *es,
515 struct wlsc_output *output, pixman_region32_t *clip)
517 struct wlsc_compositor *ec = es->compositor;
518 GLfloat *v, inv_width, inv_height;
520 pixman_region32_t repaint;
521 pixman_box32_t *rectangles;
524 pixman_region32_init_rect(&repaint,
525 es->x, es->y, es->width, es->height);
526 pixman_region32_intersect(&repaint, &repaint, clip);
527 if (!pixman_region32_not_empty(&repaint))
530 if (es->visual == &ec->compositor.argb_visual) {
531 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
533 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
534 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
540 rectangles = pixman_region32_rectangles(&repaint, &n);
541 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
542 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
543 inv_width = 1.0 / es->pitch;
544 inv_height = 1.0 / es->height;
545 for (i = 0; i < n; i++, v += 16, p += 6) {
546 v[ 0] = rectangles[i].x1;
547 v[ 1] = rectangles[i].y1;
548 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
549 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
551 v[ 4] = rectangles[i].x1;
552 v[ 5] = rectangles[i].y2;
554 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
556 v[ 8] = rectangles[i].x2;
557 v[ 9] = rectangles[i].y1;
558 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
561 v[12] = rectangles[i].x2;
562 v[13] = rectangles[i].y2;
574 glBindTexture(GL_TEXTURE_2D, es->texture);
575 v = ec->vertices.data;
576 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
577 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
578 glEnableVertexAttribArray(0);
579 glEnableVertexAttribArray(1);
580 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
582 ec->vertices.size = 0;
583 ec->indices.size = 0;
584 pixman_region32_fini(&repaint);
588 wlsc_surface_raise(struct wlsc_surface *surface)
590 struct wlsc_compositor *compositor = surface->compositor;
592 wl_list_remove(&surface->link);
593 wl_list_insert(&compositor->surface_list, &surface->link);
597 wlsc_surface_update_matrix(struct wlsc_surface *es)
599 wlsc_matrix_init(&es->matrix);
600 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
601 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
603 wlsc_matrix_init(&es->matrix_inv);
604 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
605 wlsc_matrix_scale(&es->matrix_inv,
606 1.0 / es->width, 1.0 / es->height, 1);
610 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
612 struct wlsc_output *output;
614 wl_list_for_each(output, &compositor->output_list, link)
615 wlsc_output_damage(output);
619 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
621 struct wlsc_compositor *compositor = output->compositor;
622 struct wlsc_surface *es;
623 struct wlsc_animation *animation, *next;
625 wl_list_for_each(es, &compositor->surface_list, link) {
626 if (es->output == output) {
627 wl_display_post_frame(compositor->wl_display,
628 &es->surface, msecs);
632 output->finished = 1;
634 wl_event_source_timer_update(compositor->timer_source, 5);
635 compositor->repaint_on_timeout = 1;
637 wl_list_for_each_safe(animation, next,
638 &compositor->animation_list, link)
639 animation->frame(animation, output, msecs);
643 wlsc_output_damage(struct wlsc_output *output)
645 struct wlsc_compositor *compositor = output->compositor;
647 pixman_region32_union_rect(&compositor->damage_region,
648 &compositor->damage_region,
649 output->x, output->y,
650 output->width, output->height);
651 wlsc_compositor_schedule_repaint(compositor);
655 fade_frame(struct wlsc_animation *animation,
656 struct wlsc_output *output, uint32_t msecs)
658 struct wlsc_compositor *compositor =
659 container_of(animation,
660 struct wlsc_compositor, fade.animation);
662 wlsc_tweener_update(&compositor->fade.tweener, msecs);
663 if (wlsc_tweener_done(&compositor->fade.tweener)) {
664 if (compositor->fade.tweener.current > 0.999) {
665 compositor->state = WLSC_COMPOSITOR_SLEEPING;
666 compositor->shell->lock(compositor->shell);
668 compositor->fade.tweener.current =
669 compositor->fade.tweener.target;
670 wl_list_remove(&animation->link);
671 wl_list_init(&animation->link);
674 wlsc_output_damage(output);
678 fade_output(struct wlsc_output *output,
679 GLfloat tint, pixman_region32_t *region)
681 struct wlsc_compositor *compositor = output->compositor;
682 struct wlsc_surface surface;
683 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
685 surface.compositor = compositor;
686 surface.x = output->x;
687 surface.y = output->y;
688 surface.width = output->width;
689 surface.height = output->height;
690 surface.texture = GL_NONE;
694 &compositor->compositor.premultiplied_argb_visual;
696 surface.visual = &compositor->compositor.rgb_visual;
698 glUseProgram(compositor->solid_shader.program);
699 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
700 1, GL_FALSE, output->matrix.d);
701 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
702 wlsc_surface_draw(&surface, output, region);
706 wlsc_output_repaint(struct wlsc_output *output)
708 struct wlsc_compositor *ec = output->compositor;
709 struct wlsc_surface *es;
710 struct wlsc_input_device *eid;
711 pixman_region32_t new_damage, total_damage, repaint;
712 int using_hardware_cursor = 1;
714 output->prepare_render(output);
716 glViewport(0, 0, output->width, output->height);
718 glUseProgram(ec->texture_shader.program);
719 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
720 1, GL_FALSE, output->matrix.d);
721 glUniform1i(ec->texture_shader.tex_uniform, 0);
723 pixman_region32_init(&new_damage);
724 pixman_region32_init(&total_damage);
725 pixman_region32_intersect_rect(&new_damage,
727 output->x, output->y,
728 output->width, output->height);
729 pixman_region32_subtract(&ec->damage_region,
730 &ec->damage_region, &new_damage);
731 pixman_region32_union(&total_damage, &new_damage,
732 &output->previous_damage_region);
733 pixman_region32_copy(&output->previous_damage_region, &new_damage);
736 if (output->set_hardware_cursor(output, ec->input_device) < 0)
737 using_hardware_cursor = 0;
738 if (ec->fade.tweener.current > 0.001)
739 using_hardware_cursor = 0;
741 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
742 if (es->fullscreen_output == output) {
743 if (es->visual == &ec->compositor.rgb_visual &&
744 using_hardware_cursor) {
745 if (output->prepare_scanout_surface(output, es) == 0) {
746 /* We're drawing nothing now,
747 * draw the damaged regions later. */
748 pixman_region32_union(&ec->damage_region,
755 if (es->width < output->width ||
756 es->height < output->height)
757 glClear(GL_COLOR_BUFFER_BIT);
758 wlsc_surface_draw(es, output, &total_damage);
760 wl_list_for_each(es, &ec->surface_list, link) {
761 if (es->visual != &ec->compositor.rgb_visual)
764 pixman_region32_init_rect(&repaint,
766 es->width, es->height);
767 wlsc_surface_draw(es, output, &total_damage);
768 pixman_region32_subtract(&total_damage,
769 &total_damage, &repaint);
772 if (output->background)
773 wlsc_surface_draw(output->background,
774 output, &total_damage);
776 fade_output(output, 1.0, &total_damage);
778 glUseProgram(ec->texture_shader.program);
779 wl_list_for_each_reverse(es, &ec->surface_list, link) {
780 if (ec->overlay == es)
783 if (es->visual == &ec->compositor.rgb_visual) {
784 pixman_region32_union_rect(&total_damage,
787 es->width, es->height);
791 wlsc_surface_draw(es, output, &total_damage);
796 wlsc_surface_draw(ec->overlay, output, &total_damage);
799 wl_list_for_each(eid, &ec->input_device_list, link) {
800 if (&eid->input_device != ec->input_device ||
801 !using_hardware_cursor)
802 wlsc_surface_draw(eid->sprite, output,
806 if (ec->fade.tweener.current > 0.001)
807 fade_output(output, ec->fade.tweener.current, &total_damage);
813 struct wlsc_compositor *ec = data;
814 struct wlsc_output *output;
815 int repainted_all_outputs = 1;
817 wl_list_for_each(output, &ec->output_list, link) {
818 if (!output->repaint_needed)
821 if (!output->finished) {
822 repainted_all_outputs = 0;
826 wlsc_output_repaint(output);
827 output->finished = 0;
828 output->repaint_needed = 0;
829 output->present(output);
832 if (repainted_all_outputs)
833 ec->repaint_on_timeout = 0;
835 wl_event_source_timer_update(ec->timer_source, 1);
841 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
843 struct wlsc_output *output;
845 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
848 wl_list_for_each(output, &compositor->output_list, link)
849 output->repaint_needed = 1;
851 if (compositor->repaint_on_timeout)
854 wl_event_source_timer_update(compositor->timer_source, 1);
855 compositor->repaint_on_timeout = 1;
859 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
863 done = wlsc_tweener_done(&compositor->fade.tweener);
864 compositor->fade.tweener.target = tint;
865 if (wlsc_tweener_done(&compositor->fade.tweener))
869 compositor->fade.tweener.timestamp =
870 wlsc_compositor_get_time();
872 wlsc_compositor_damage_all(compositor);
873 if (wl_list_empty(&compositor->fade.animation.link))
874 wl_list_insert(compositor->animation_list.prev,
875 &compositor->fade.animation.link);
879 surface_destroy(struct wl_client *client,
880 struct wl_surface *surface)
882 wl_resource_destroy(&surface->resource, client);
886 wlsc_surface_assign_output(struct wlsc_surface *es)
888 struct wlsc_compositor *ec = es->compositor;
889 struct wlsc_output *output;
891 struct wlsc_output *tmp = es->output;
894 wl_list_for_each(output, &ec->output_list, link) {
895 if (output->x < es->x && es->x < output->x + output->width &&
896 output->y < es->y && es->y < output->y + output->height) {
898 printf("assiging surface %p to output %p\n",
904 if (es->output == NULL) {
905 printf("no output found\n");
906 es->output = container_of(ec->output_list.next,
907 struct wlsc_output, link);
912 surface_attach(struct wl_client *client,
913 struct wl_surface *surface, struct wl_buffer *buffer,
914 int32_t x, int32_t y)
916 struct wlsc_surface *es = (struct wlsc_surface *) surface;
918 /* FIXME: This damages the entire old surface, but we should
919 * really just damage the part that's no longer covered by the
920 * surface. Anything covered by the new surface will be
921 * damaged by the client. */
922 wlsc_surface_damage(es);
924 wlsc_buffer_attach(buffer, surface);
929 es->width = buffer->width;
930 es->height = buffer->height;
931 if (x != 0 || y != 0)
932 wlsc_surface_assign_output(es);
933 wlsc_surface_update_matrix(es);
935 es->compositor->shell->attach(es->compositor->shell, es);
939 surface_map_toplevel(struct wl_client *client,
940 struct wl_surface *surface)
942 struct wlsc_surface *es = (struct wlsc_surface *) surface;
943 struct wlsc_compositor *ec = es->compositor;
945 switch (es->map_type) {
946 case WLSC_SURFACE_MAP_UNMAPPED:
947 es->x = 10 + random() % 400;
948 es->y = 10 + random() % 400;
949 wlsc_surface_update_matrix(es);
950 /* assign to first output */
951 es->output = container_of(ec->output_list.next,
952 struct wlsc_output, link);
953 wl_list_insert(&es->compositor->surface_list, &es->link);
955 case WLSC_SURFACE_MAP_TOPLEVEL:
957 case WLSC_SURFACE_MAP_FULLSCREEN:
958 es->fullscreen_output = NULL;
961 wlsc_surface_update_matrix(es);
967 wlsc_surface_damage(es);
968 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
972 surface_map_transient(struct wl_client *client,
973 struct wl_surface *surface, struct wl_surface *parent,
974 int x, int y, uint32_t flags)
976 struct wlsc_surface *es = (struct wlsc_surface *) surface;
977 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
979 switch (es->map_type) {
980 case WLSC_SURFACE_MAP_UNMAPPED:
981 wl_list_insert(&es->compositor->surface_list, &es->link);
982 /* assign to parents output */
983 es->output = pes->output;
985 case WLSC_SURFACE_MAP_FULLSCREEN:
986 es->fullscreen_output = NULL;
995 wlsc_surface_update_matrix(es);
996 wlsc_surface_damage(es);
997 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
1001 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
1003 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1004 struct wlsc_output *output;
1006 /* FIXME: Fullscreen on first output */
1007 /* FIXME: Handle output going away */
1008 output = container_of(es->compositor->output_list.next,
1009 struct wlsc_output, link);
1011 switch (es->map_type) {
1012 case WLSC_SURFACE_MAP_UNMAPPED:
1013 es->x = 10 + random() % 400;
1014 es->y = 10 + random() % 400;
1015 /* assign to first output */
1016 es->output = output;
1017 wl_list_insert(&es->compositor->surface_list, &es->link);
1019 case WLSC_SURFACE_MAP_FULLSCREEN:
1025 es->saved_x = es->x;
1026 es->saved_y = es->y;
1027 es->x = (output->width - es->width) / 2;
1028 es->y = (output->height - es->height) / 2;
1029 es->fullscreen_output = output;
1030 wlsc_surface_update_matrix(es);
1031 wlsc_surface_damage(es);
1032 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
1036 surface_damage(struct wl_client *client,
1037 struct wl_surface *surface,
1038 int32_t x, int32_t y, int32_t width, int32_t height)
1040 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1042 wlsc_surface_damage_rectangle(es, x, y, width, height);
1045 const static struct wl_surface_interface surface_interface = {
1048 surface_map_toplevel,
1049 surface_map_transient,
1050 surface_map_fullscreen,
1055 wlsc_input_device_attach(struct wlsc_input_device *device,
1056 int x, int y, int width, int height)
1058 wlsc_surface_damage(device->sprite);
1060 device->hotspot_x = x;
1061 device->hotspot_y = y;
1063 device->sprite->x = device->input_device.x - device->hotspot_x;
1064 device->sprite->y = device->input_device.y - device->hotspot_y;
1065 device->sprite->width = width;
1066 device->sprite->height = height;
1067 wlsc_surface_update_matrix(device->sprite);
1069 wlsc_surface_damage(device->sprite);
1073 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1074 struct wl_buffer *buffer, int x, int y)
1076 wlsc_buffer_attach(buffer, &device->sprite->surface);
1077 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1081 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1082 struct wlsc_sprite *sprite, int x, int y)
1084 wlsc_sprite_attach(sprite, &device->sprite->surface);
1085 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1089 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1090 enum wlsc_pointer_type type)
1092 struct wlsc_compositor *compositor =
1093 (struct wlsc_compositor *) device->input_device.compositor;
1095 wlsc_input_device_attach_sprite(device,
1096 compositor->pointer_sprites[type],
1097 pointer_images[type].hotspot_x,
1098 pointer_images[type].hotspot_y);
1102 compositor_create_surface(struct wl_client *client,
1103 struct wl_compositor *compositor, uint32_t id)
1105 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1106 struct wlsc_surface *surface;
1108 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1109 if (surface == NULL) {
1110 wl_client_post_no_memory(client);
1114 surface->surface.resource.destroy = destroy_surface;
1116 surface->surface.resource.object.id = id;
1117 surface->surface.resource.object.interface = &wl_surface_interface;
1118 surface->surface.resource.object.implementation =
1119 (void (**)(void)) &surface_interface;
1120 surface->surface.client = client;
1122 wl_client_add_resource(client, &surface->surface.resource);
1125 const static struct wl_compositor_interface compositor_interface = {
1126 compositor_create_surface,
1130 wlsc_surface_transform(struct wlsc_surface *surface,
1131 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1133 struct wlsc_vector v = { { x, y, 0, 1 } };
1135 wlsc_matrix_transform(&surface->matrix_inv, &v);
1136 *sx = v.f[0] * surface->width;
1137 *sy = v.f[1] * surface->height;
1140 struct wlsc_surface *
1141 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1143 struct wlsc_compositor *ec =
1144 (struct wlsc_compositor *) device->compositor;
1145 struct wlsc_surface *es;
1147 wl_list_for_each(es, &ec->surface_list, link) {
1148 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1149 if (0 <= *sx && *sx < es->width &&
1150 0 <= *sy && *sy < es->height)
1159 motion_grab_motion(struct wl_grab *grab,
1160 uint32_t time, int32_t x, int32_t y)
1162 struct wlsc_input_device *device =
1163 (struct wlsc_input_device *) grab->input_device;
1164 struct wlsc_surface *es =
1165 (struct wlsc_surface *) device->input_device.pointer_focus;
1168 wlsc_surface_transform(es, x, y, &sx, &sy);
1169 wl_client_post_event(es->surface.client,
1170 &device->input_device.object,
1171 WL_INPUT_DEVICE_MOTION,
1172 time, x, y, sx, sy);
1176 motion_grab_button(struct wl_grab *grab,
1177 uint32_t time, int32_t button, int32_t state)
1179 wl_client_post_event(grab->input_device->pointer_focus->client,
1180 &grab->input_device->object,
1181 WL_INPUT_DEVICE_BUTTON,
1182 time, button, state);
1186 motion_grab_end(struct wl_grab *grab, uint32_t time)
1190 static const struct wl_grab_interface motion_grab_interface = {
1197 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1199 if (compositor->idle_inhibit)
1202 wlsc_compositor_fade(compositor, 0.0);
1203 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1205 wl_event_source_timer_update(compositor->idle_source,
1206 option_idle_time * 1000);
1210 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1212 wlsc_compositor_wake(compositor);
1213 compositor->idle_inhibit++;
1217 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1219 compositor->idle_inhibit--;
1220 wlsc_compositor_wake(compositor);
1224 idle_handler(void *data)
1226 struct wlsc_compositor *compositor = data;
1228 if (compositor->idle_inhibit)
1231 wlsc_compositor_fade(compositor, 1.0);
1237 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1239 struct wlsc_surface *es;
1240 struct wlsc_compositor *ec =
1241 (struct wlsc_compositor *) device->compositor;
1242 struct wlsc_output *output;
1243 const struct wl_grab_interface *interface;
1244 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1246 int x_valid = 0, y_valid = 0;
1247 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1249 wlsc_compositor_wake(ec);
1251 wl_list_for_each(output, &ec->output_list, link) {
1252 if (output->x <= x && x <= output->x + output->width)
1255 if (output->y <= y && y <= output->y + output->height)
1258 /* FIXME: calculate this only on output addition/deletion */
1259 if (output->x < min_x)
1261 if (output->y < min_y)
1264 if (output->x + output->width > max_x)
1265 max_x = output->x + output->width;
1266 if (output->y + output->height > max_y)
1267 max_y = output->y + output->height;
1273 else if (x >= max_x)
1279 else if (y >= max_y)
1287 interface = device->grab->interface;
1288 interface->motion(device->grab, time, x, y);
1290 es = pick_surface(device, &sx, &sy);
1291 wl_input_device_set_pointer_focus(device,
1293 time, x, y, sx, sy);
1295 wl_client_post_event(es->surface.client,
1297 WL_INPUT_DEVICE_MOTION,
1298 time, x, y, sx, sy);
1301 wlsc_surface_damage(wd->sprite);
1303 wd->sprite->x = device->x - wd->hotspot_x;
1304 wd->sprite->y = device->y - wd->hotspot_y;
1305 wlsc_surface_update_matrix(wd->sprite);
1307 wlsc_surface_damage(wd->sprite);
1311 wlsc_surface_activate(struct wlsc_surface *surface,
1312 struct wlsc_input_device *device, uint32_t time)
1314 wlsc_surface_raise(surface);
1315 if (device->selection)
1316 wlsc_selection_set_focus(device->selection,
1317 &surface->surface, time);
1319 wl_input_device_set_keyboard_focus(&device->input_device,
1324 struct wlsc_binding {
1328 wlsc_binding_handler_t handler;
1330 struct wl_list link;
1334 notify_button(struct wl_input_device *device,
1335 uint32_t time, int32_t button, int32_t state)
1337 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1338 struct wlsc_compositor *compositor =
1339 (struct wlsc_compositor *) device->compositor;
1340 struct wlsc_binding *b;
1341 struct wlsc_surface *surface =
1342 (struct wlsc_surface *) device->pointer_focus;
1345 wlsc_compositor_idle_inhibit(compositor);
1347 wlsc_compositor_idle_release(compositor);
1349 if (state && surface && device->grab == NULL) {
1350 wlsc_surface_activate(surface, wd, time);
1351 wl_input_device_start_grab(device,
1352 &device->motion_grab,
1356 wl_list_for_each(b, &compositor->binding_list, link) {
1357 if (b->button == button &&
1358 b->modifier == wd->modifier_state && state) {
1359 b->handler(&wd->input_device,
1360 time, 0, button, state, b->data);
1366 device->grab->interface->button(device->grab, time,
1369 if (!state && device->grab && device->grab_button == button)
1370 wl_input_device_end_grab(device, time);
1374 terminate_binding(struct wl_input_device *device, uint32_t time,
1375 uint32_t key, uint32_t button, uint32_t state, void *data)
1377 struct wlsc_compositor *compositor = data;
1380 wl_display_terminate(compositor->wl_display);
1383 WL_EXPORT struct wlsc_binding *
1384 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1385 uint32_t key, uint32_t button, uint32_t modifier,
1386 wlsc_binding_handler_t handler, void *data)
1388 struct wlsc_binding *binding;
1390 binding = malloc(sizeof *binding);
1391 if (binding == NULL)
1395 binding->button = button;
1396 binding->modifier = modifier;
1397 binding->handler = handler;
1398 binding->data = data;
1399 wl_list_insert(compositor->binding_list.prev, &binding->link);
1405 wlsc_binding_destroy(struct wlsc_binding *binding)
1407 wl_list_remove(&binding->link);
1412 update_modifier_state(struct wlsc_input_device *device,
1413 uint32_t key, uint32_t state)
1420 modifier = MODIFIER_CTRL;
1425 modifier = MODIFIER_ALT;
1430 modifier = MODIFIER_SUPER;
1439 device->modifier_state |= modifier;
1441 device->modifier_state &= ~modifier;
1445 notify_key(struct wl_input_device *device,
1446 uint32_t time, uint32_t key, uint32_t state)
1448 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1449 struct wlsc_compositor *compositor =
1450 (struct wlsc_compositor *) device->compositor;
1452 struct wlsc_binding *b;
1455 wlsc_compositor_idle_inhibit(compositor);
1457 wlsc_compositor_idle_release(compositor);
1459 wl_list_for_each(b, &compositor->binding_list, link) {
1460 if (b->key == key &&
1461 b->modifier == wd->modifier_state) {
1462 b->handler(&wd->input_device,
1463 time, key, 0, state, b->data);
1468 update_modifier_state(wd, key, state);
1469 end = device->keys.data + device->keys.size;
1470 for (k = device->keys.data; k < end; k++) {
1474 device->keys.size = (void *) end - device->keys.data;
1476 k = wl_array_add(&device->keys, sizeof *k);
1480 if (device->keyboard_focus != NULL)
1481 wl_client_post_event(device->keyboard_focus->client,
1483 WL_INPUT_DEVICE_KEY, time, key, state);
1487 notify_pointer_focus(struct wl_input_device *device,
1488 uint32_t time, struct wlsc_output *output,
1489 int32_t x, int32_t y)
1491 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1492 struct wlsc_compositor *compositor =
1493 (struct wlsc_compositor *) device->compositor;
1494 struct wlsc_surface *es;
1500 es = pick_surface(device, &sx, &sy);
1501 wl_input_device_set_pointer_focus(device,
1503 time, x, y, sx, sy);
1505 compositor->focus = 1;
1507 wd->sprite->x = device->x - wd->hotspot_x;
1508 wd->sprite->y = device->y - wd->hotspot_y;
1509 wlsc_surface_update_matrix(wd->sprite);
1511 wl_input_device_set_pointer_focus(device, NULL,
1513 compositor->focus = 0;
1516 wlsc_surface_damage(wd->sprite);
1520 notify_keyboard_focus(struct wl_input_device *device,
1521 uint32_t time, struct wlsc_output *output,
1522 struct wl_array *keys)
1524 struct wlsc_input_device *wd =
1525 (struct wlsc_input_device *) device;
1526 struct wlsc_compositor *compositor =
1527 (struct wlsc_compositor *) device->compositor;
1528 struct wlsc_surface *es;
1531 if (!wl_list_empty(&compositor->surface_list))
1532 es = container_of(compositor->surface_list.next,
1533 struct wlsc_surface, link);
1538 wl_array_copy(&wd->input_device.keys, keys);
1539 wd->modifier_state = 0;
1540 end = device->keys.data + device->keys.size;
1541 for (k = device->keys.data; k < end; k++) {
1542 wlsc_compositor_idle_inhibit(compositor);
1543 update_modifier_state(wd, *k, 1);
1546 wl_input_device_set_keyboard_focus(&wd->input_device,
1547 &es->surface, time);
1549 end = device->keys.data + device->keys.size;
1550 for (k = device->keys.data; k < end; k++)
1551 wlsc_compositor_idle_release(compositor);
1553 wd->modifier_state = 0;
1554 wl_input_device_set_keyboard_focus(&wd->input_device,
1561 input_device_attach(struct wl_client *client,
1562 struct wl_input_device *device_base,
1564 struct wl_buffer *buffer, int32_t x, int32_t y)
1566 struct wlsc_input_device *device =
1567 (struct wlsc_input_device *) device_base;
1569 if (time < device->input_device.pointer_focus_time)
1571 if (device->input_device.pointer_focus == NULL)
1573 if (device->input_device.pointer_focus->client != client)
1576 if (buffer == NULL) {
1577 wlsc_input_device_set_pointer_image(device,
1578 WLSC_POINTER_LEFT_PTR);
1582 wlsc_input_device_attach_buffer(device, buffer, x, y);
1585 const static struct wl_input_device_interface input_device_interface = {
1586 input_device_attach,
1590 wlsc_input_device_init(struct wlsc_input_device *device,
1591 struct wlsc_compositor *ec)
1593 wl_input_device_init(&device->input_device, &ec->compositor);
1595 device->input_device.object.interface = &wl_input_device_interface;
1596 device->input_device.object.implementation =
1597 (void (**)(void)) &input_device_interface;
1598 wl_display_add_object(ec->wl_display, &device->input_device.object);
1599 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1601 device->sprite = wlsc_surface_create(ec,
1602 device->input_device.x,
1603 device->input_device.y, 32, 32);
1604 device->hotspot_x = 16;
1605 device->hotspot_y = 16;
1606 device->modifier_state = 0;
1608 device->input_device.motion_grab.interface = &motion_grab_interface;
1610 wl_list_insert(ec->input_device_list.prev, &device->link);
1612 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1616 wlsc_output_post_geometry(struct wl_client *client,
1617 struct wl_object *global, uint32_t version)
1619 struct wlsc_output *output =
1620 container_of(global, struct wlsc_output, object);
1622 wl_client_post_event(client, global,
1624 output->x, output->y,
1625 output->width, output->height);
1628 static const char vertex_shader[] =
1629 "uniform mat4 proj;\n"
1630 "attribute vec2 position;\n"
1631 "attribute vec2 texcoord;\n"
1632 "varying vec2 v_texcoord;\n"
1635 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1636 " v_texcoord = texcoord;\n"
1639 static const char texture_fragment_shader[] =
1640 "precision mediump float;\n"
1641 "varying vec2 v_texcoord;\n"
1642 "uniform sampler2D tex;\n"
1645 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1648 static const char solid_fragment_shader[] =
1649 "precision mediump float;\n"
1650 "varying vec2 v_texcoord;\n"
1651 "uniform vec4 color;\n"
1654 " gl_FragColor = color\n;"
1658 compile_shader(GLenum type, const char *source)
1664 s = glCreateShader(type);
1665 glShaderSource(s, 1, &source, NULL);
1667 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1669 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1670 fprintf(stderr, "shader info: %s\n", msg);
1678 wlsc_shader_init(struct wlsc_shader *shader,
1679 const char *vertex_source, const char *fragment_source)
1684 shader->vertex_shader =
1685 compile_shader(GL_VERTEX_SHADER, vertex_source);
1686 shader->fragment_shader =
1687 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1689 shader->program = glCreateProgram();
1690 glAttachShader(shader->program, shader->vertex_shader);
1691 glAttachShader(shader->program, shader->fragment_shader);
1692 glBindAttribLocation(shader->program, 0, "position");
1693 glBindAttribLocation(shader->program, 1, "texcoord");
1695 glLinkProgram(shader->program);
1696 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1698 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1699 fprintf(stderr, "link info: %s\n", msg);
1703 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1704 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1710 init_solid_shader(struct wlsc_shader *shader,
1711 GLuint vertex_shader, const char *fragment_source)
1716 shader->vertex_shader = vertex_shader;
1717 shader->fragment_shader =
1718 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1720 shader->program = glCreateProgram();
1721 glAttachShader(shader->program, shader->vertex_shader);
1722 glAttachShader(shader->program, shader->fragment_shader);
1723 glBindAttribLocation(shader->program, 0, "position");
1724 glBindAttribLocation(shader->program, 1, "texcoord");
1726 glLinkProgram(shader->program);
1727 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1729 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1730 fprintf(stderr, "link info: %s\n", msg);
1734 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1735 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1741 wlsc_output_destroy(struct wlsc_output *output)
1743 destroy_surface(&output->background->surface.resource, NULL);
1747 wlsc_output_move(struct wlsc_output *output, int x, int y)
1749 struct wlsc_compositor *c = output->compositor;
1755 if (output->background) {
1756 output->background->x = x;
1757 output->background->y = y;
1758 wlsc_surface_update_matrix(output->background);
1761 pixman_region32_init(&output->previous_damage_region);
1763 wlsc_matrix_init(&output->matrix);
1764 wlsc_matrix_translate(&output->matrix,
1765 -output->x - output->width / 2.0,
1766 -output->y - output->height / 2.0, 0);
1768 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1769 wlsc_matrix_scale(&output->matrix,
1770 2.0 / output->width,
1771 flip * 2.0 / output->height, 1);
1773 pixman_region32_union_rect(&c->damage_region,
1775 x, y, output->width, output->height);
1779 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1780 int x, int y, int width, int height, uint32_t flags)
1782 output->compositor = c;
1785 output->width = width;
1786 output->height = height;
1788 output->background =
1789 background_create(output, option_background);
1791 output->flags = flags;
1792 output->finished = 1;
1793 wlsc_output_move(output, x, y);
1795 output->object.interface = &wl_output_interface;
1796 wl_display_add_object(c->wl_display, &output->object);
1797 wl_display_add_global(c->wl_display, &output->object,
1798 wlsc_output_post_geometry);
1802 shm_buffer_created(struct wl_buffer *buffer)
1804 struct wl_list *surfaces_attached_to;
1806 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1807 if (!surfaces_attached_to) {
1808 buffer->user_data = NULL;
1812 wl_list_init(surfaces_attached_to);
1814 buffer->user_data = surfaces_attached_to;
1818 shm_buffer_damaged(struct wl_buffer *buffer,
1819 int32_t x, int32_t y, int32_t width, int32_t height)
1821 struct wl_list *surfaces_attached_to = buffer->user_data;
1822 struct wlsc_surface *es;
1823 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1825 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1826 glBindTexture(GL_TEXTURE_2D, es->texture);
1827 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1828 tex_width, buffer->height, 0,
1829 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1830 wl_shm_buffer_get_data(buffer));
1831 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1832 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1837 shm_buffer_destroyed(struct wl_buffer *buffer)
1839 struct wl_list *surfaces_attached_to = buffer->user_data;
1840 struct wlsc_surface *es, *next;
1842 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1844 wl_list_remove(&es->buffer_link);
1847 free(surfaces_attached_to);
1850 const static struct wl_shm_callbacks shm_callbacks = {
1853 shm_buffer_destroyed
1857 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1859 struct wl_event_loop *loop;
1860 const char *extensions;
1862 ec->wl_display = display;
1864 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1866 ec->shm = wl_shm_init(display, &shm_callbacks);
1867 if (strstr(eglQueryString(ec->display, EGL_EXTENSIONS),
1868 "EGL_WL_bind_wayland_display"))
1869 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
1871 wl_list_init(&ec->surface_list);
1872 wl_list_init(&ec->input_device_list);
1873 wl_list_init(&ec->output_list);
1874 wl_list_init(&ec->binding_list);
1875 wl_list_init(&ec->animation_list);
1876 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1877 ec->fade.animation.frame = fade_frame;
1878 wl_list_init(&ec->fade.animation.link);
1880 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1881 MODIFIER_CTRL | MODIFIER_ALT,
1882 terminate_binding, ec);
1884 create_pointer_images(ec);
1886 screenshooter_create(ec);
1888 extensions = (const char *) glGetString(GL_EXTENSIONS);
1889 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1891 "GL_EXT_texture_format_BGRA8888 not available\n");
1895 glActiveTexture(GL_TEXTURE0);
1897 if (wlsc_shader_init(&ec->texture_shader,
1898 vertex_shader, texture_fragment_shader) < 0)
1900 if (init_solid_shader(&ec->solid_shader,
1901 ec->texture_shader.vertex_shader,
1902 solid_fragment_shader) < 0)
1905 loop = wl_display_get_event_loop(ec->wl_display);
1906 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1907 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1909 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1910 pixman_region32_init(&ec->damage_region);
1911 wlsc_compositor_schedule_repaint(ec);
1916 static int on_term_signal(int signal_number, void *data)
1918 struct wlsc_compositor *ec = data;
1920 wl_display_terminate(ec->wl_display);
1925 int main(int argc, char *argv[])
1927 struct wl_display *display;
1928 struct wlsc_compositor *ec;
1929 struct wl_event_loop *loop;
1930 GError *error = NULL;
1931 GOptionContext *context;
1934 int (*shell_init)(struct wlsc_compositor *ec);
1936 g_type_init(); /* GdkPixbuf needs this, it seems. */
1938 context = g_option_context_new(NULL);
1939 g_option_context_add_main_entries(context, option_entries, "Wayland");
1940 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1941 fprintf(stderr, "option parsing failed: %s\n", error->message);
1944 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1945 fprintf(stderr, "invalid geometry option: %s \n",
1950 display = wl_display_create();
1954 shell_init = desktop_shell_init;
1956 shell_module = dlopen(option_shell, RTLD_LAZY);
1957 if (!shell_module) {
1958 fprintf(stderr, "failed to load shell module: %m\n");
1961 shell_init = dlsym(shell_module, "shell_init");
1964 "failed to lookup shell init function: %m\n");
1969 #if BUILD_WAYLAND_COMPOSITOR
1970 if (getenv("WAYLAND_DISPLAY"))
1971 ec = wayland_compositor_create(display, width, height);
1974 #if BUILD_X11_COMPOSITOR
1975 if (ec == NULL && getenv("DISPLAY"))
1976 ec = x11_compositor_create(display, width, height);
1979 #if BUILD_OPENWFD_COMPOSITOR
1980 if (ec == NULL && getenv("OPENWFD"))
1981 ec = wfd_compositor_create(display, option_connector);
1984 #if BUILD_DRM_COMPOSITOR
1986 ec = drm_compositor_create(display, option_connector);
1990 fprintf(stderr, "failed to create compositor\n");
1994 if (shell_init(ec) < 0)
1997 if (wl_display_add_socket(display, option_socket_name)) {
1998 fprintf(stderr, "failed to add socket: %m\n");
2002 loop = wl_display_get_event_loop(ec->wl_display);
2003 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
2004 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
2006 wl_display_run(display);
2008 if (strstr(eglQueryString(ec->display, EGL_EXTENSIONS),
2009 "EGL_WL_bind_wayland_display"))
2010 eglUnbindWaylandDisplayWL(ec->display, display);
2011 wl_display_destroy(display);