2 * Copyright © 2008 Kristian Høgsberg
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <sys/ioctl.h>
32 #include <gdk-pixbuf/gdk-pixbuf.h>
34 #include <linux/input.h>
36 #include "wayland-server.h"
37 #include "compositor.h"
39 /* The plan here is to generate a random anonymous socket name and
40 * advertise that through a service on the session dbus.
42 static const char *option_socket_name = NULL;
43 static const char *option_background = "background.jpg";
44 static const char *option_geometry = "1024x640";
45 static int option_idle_time = 5;
46 static int option_connector = 0;
48 static const GOptionEntry option_entries[] = {
49 { "background", 'b', 0, G_OPTION_ARG_STRING,
50 &option_background, "Background image" },
51 { "connector", 'c', 0, G_OPTION_ARG_INT,
52 &option_connector, "KMS connector" },
53 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
54 &option_geometry, "Geometry" },
55 { "socket", 's', 0, G_OPTION_ARG_STRING,
56 &option_socket_name, "Socket Name" },
57 { "idle-time", 'i', 0, G_OPTION_ARG_INT,
58 &option_idle_time, "Screensaver idle time" },
63 wlsc_matrix_init(struct wlsc_matrix *matrix)
65 static const struct wlsc_matrix identity = {
66 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
69 memcpy(matrix, &identity, sizeof identity);
73 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
75 struct wlsc_matrix tmp;
76 const GLfloat *row, *column;
80 for (i = 0; i < 16; i++) {
83 row = m->d + d.quot * 4;
84 column = n->d + d.rem;
85 for (j = 0; j < 4; j++)
86 tmp.d[i] += row[j] * column[j * 4];
88 memcpy(m, &tmp, sizeof tmp);
92 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
94 struct wlsc_matrix translate = {
95 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
98 wlsc_matrix_multiply(matrix, &translate);
102 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
104 struct wlsc_matrix scale = {
105 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
108 wlsc_matrix_multiply(matrix, &scale);
112 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
115 struct wlsc_vector t;
117 for (i = 0; i < 4; i++) {
119 for (j = 0; j < 4; j++)
120 t.f[i] += v->f[j] * matrix->d[i + j * 4];
127 wlsc_tweener_init(struct wlsc_tweener *tweener,
128 double k, double current, double target)
131 tweener->current = current;
132 tweener->previous = current;
133 tweener->target = target;
137 wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
139 double force, current, step;
141 step = (msec - tweener->timestamp) / 100.0;
142 tweener->timestamp = msec;
144 current = tweener->current;
145 force = tweener->k * (tweener->target - current) / 10.0 +
146 (tweener->previous - current);
149 current + (current - tweener->previous) + force * step * step;
150 tweener->previous = current;
152 if (tweener->current >= 1.0) {
153 tweener->current = 1.0;
154 tweener->previous = 1.0;
157 if (tweener->current <= 0.0) {
158 tweener->current = 0.0;
159 tweener->previous = 0.0;
164 wlsc_tweener_done(struct wlsc_tweener *tweener)
166 return fabs(tweener->previous - tweener->target) < 0.0002 &&
167 fabs(tweener->current - tweener->target) < 0.0002;
170 struct wlsc_surface *
171 wlsc_surface_create(struct wlsc_compositor *compositor,
172 int32_t x, int32_t y, int32_t width, int32_t height)
174 struct wlsc_surface *surface;
176 surface = malloc(sizeof *surface);
180 wl_list_init(&surface->surface.destroy_listener_list);
181 wl_list_init(&surface->link);
182 wl_list_init(&surface->buffer_link);
183 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
185 glGenTextures(1, &surface->texture);
186 glBindTexture(GL_TEXTURE_2D, surface->texture);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
192 surface->compositor = compositor;
193 surface->visual = NULL;
194 surface->image = EGL_NO_IMAGE_KHR;
195 surface->saved_texture = 0;
196 surface->buffer = NULL;
199 surface->width = width;
200 surface->height = height;
201 wlsc_matrix_init(&surface->matrix);
202 wlsc_matrix_scale(&surface->matrix, width, height, 1);
203 wlsc_matrix_translate(&surface->matrix, x, y, 0);
205 wlsc_matrix_init(&surface->matrix_inv);
206 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
207 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
213 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
214 int32_t x, int32_t y,
215 int32_t width, int32_t height)
217 struct wlsc_compositor *compositor = surface->compositor;
219 pixman_region32_union_rect(&compositor->damage_region,
220 &compositor->damage_region,
221 surface->x + x, surface->y + y,
223 wlsc_compositor_schedule_repaint(compositor);
227 wlsc_surface_damage(struct wlsc_surface *surface)
229 wlsc_surface_damage_rectangle(surface, 0, 0,
230 surface->width, surface->height);
238 gettimeofday(&tv, NULL);
240 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
244 destroy_surface(struct wl_resource *resource, struct wl_client *client)
246 struct wlsc_surface *surface =
247 container_of(resource, struct wlsc_surface, surface.resource);
248 struct wl_listener *l, *next;
251 wlsc_surface_damage(surface);
253 wl_list_remove(&surface->link);
254 if (surface->saved_texture == 0)
255 glDeleteTextures(1, &surface->texture);
257 glDeleteTextures(1, &surface->saved_texture);
260 if (surface->image != EGL_NO_IMAGE_KHR)
261 eglDestroyImageKHR(surface->compositor->display,
264 wl_list_remove(&surface->buffer_link);
267 wl_list_for_each_safe(l, next,
268 &surface->surface.destroy_listener_list, link)
269 l->func(l, &surface->surface, time);
275 wlsc_load_image(const char *filename, int width, int height)
278 GError *error = NULL;
279 int stride, i, n_channels;
280 unsigned char *pixels, *end, *argb_pixels, *s, *d;
282 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
286 fprintf(stderr, "failed to load image: %s\n", error->message);
291 stride = gdk_pixbuf_get_rowstride(pixbuf);
292 pixels = gdk_pixbuf_get_pixels(pixbuf);
293 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
295 argb_pixels = malloc (height * width * 4);
296 if (argb_pixels == NULL) {
297 g_object_unref(pixbuf);
301 if (n_channels == 4) {
302 for (i = 0; i < height; i++) {
303 s = pixels + i * stride;
305 d = argb_pixels + i * width * 4;
309 #define MULT(_d,c,a,t) \
310 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
312 MULT(d[0], s[2], s[3], t);
313 MULT(d[1], s[1], s[3], t);
314 MULT(d[2], s[0], s[3], t);
320 } else if (n_channels == 3) {
321 for (i = 0; i < height; i++) {
322 s = pixels + i * stride;
324 d = argb_pixels + i * width * 4;
336 g_object_unref(pixbuf);
338 return (uint32_t *) argb_pixels;
342 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
344 struct wlsc_surface *es = (struct wlsc_surface *) surface;
345 struct wlsc_compositor *ec = es->compositor;
346 struct wl_list *surfaces_attached_to;
348 if (es->saved_texture != 0)
349 es->texture = es->saved_texture;
351 glBindTexture(GL_TEXTURE_2D, es->texture);
353 if (wl_buffer_is_shm(buffer)) {
354 /* Unbind any EGLImage texture that may be bound, so we don't
356 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
357 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
358 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
359 buffer->width, buffer->height, 0,
360 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
361 wl_shm_buffer_get_data(buffer));
362 es->visual = buffer->visual;
364 surfaces_attached_to = buffer->user_data;
367 wl_list_remove(&es->buffer_link);
368 wl_list_insert(surfaces_attached_to, &es->buffer_link);
370 es->image = eglCreateImageKHR(ec->display, NULL,
371 EGL_WAYLAND_BUFFER_WL,
374 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
375 es->visual = buffer->visual;
380 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
382 struct wlsc_surface *es = (struct wlsc_surface *) surface;
384 es->image = sprite->image;
385 if (sprite->image != EGL_NO_IMAGE_KHR) {
386 glBindTexture(GL_TEXTURE_2D, es->texture);
387 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
389 if (es->saved_texture == 0)
390 es->saved_texture = es->texture;
391 es->texture = sprite->texture;
394 es->visual = sprite->visual;
398 SPRITE_USE_CURSOR = (1 << 0),
401 static struct wlsc_sprite *
402 create_sprite_from_png(struct wlsc_compositor *ec,
403 const char *filename, int width, int height,
407 struct wlsc_sprite *sprite;
409 pixels = wlsc_load_image(filename, width, height);
413 sprite = malloc(sizeof *sprite);
414 if (sprite == NULL) {
419 sprite->visual = &ec->compositor.premultiplied_argb_visual;
420 sprite->width = width;
421 sprite->height = height;
422 sprite->image = EGL_NO_IMAGE_KHR;
424 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
425 sprite->image = ec->create_cursor_image(ec, width, height);
427 glGenTextures(1, &sprite->texture);
428 glBindTexture(GL_TEXTURE_2D, sprite->texture);
429 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
430 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
431 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
434 if (sprite->image != EGL_NO_IMAGE_KHR) {
435 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, sprite->image);
436 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
437 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
439 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
440 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
448 static const struct {
449 const char *filename;
450 int hotspot_x, hotspot_y;
451 } pointer_images[] = {
452 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
453 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
454 { DATADIR "/wayland/bottom_side.png", 16, 20 },
455 { DATADIR "/wayland/grabbing.png", 20, 17 },
456 { DATADIR "/wayland/left_ptr.png", 10, 5 },
457 { DATADIR "/wayland/left_side.png", 10, 20 },
458 { DATADIR "/wayland/right_side.png", 30, 19 },
459 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
460 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
461 { DATADIR "/wayland/top_side.png", 18, 8 },
462 { DATADIR "/wayland/xterm.png", 15, 15 }
466 create_pointer_images(struct wlsc_compositor *ec)
469 const int width = 32, height = 32;
471 count = ARRAY_LENGTH(pointer_images);
472 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
473 for (i = 0; i < count; i++) {
474 ec->pointer_sprites[i] =
475 create_sprite_from_png(ec,
476 pointer_images[i].filename,
482 static struct wlsc_surface *
483 background_create(struct wlsc_output *output, const char *filename)
485 struct wlsc_surface *background;
486 struct wlsc_sprite *sprite;
488 background = wlsc_surface_create(output->compositor,
489 output->x, output->y,
490 output->width, output->height);
491 if (background == NULL)
494 sprite = create_sprite_from_png(output->compositor, filename,
495 output->width, output->height, 0);
496 if (sprite == NULL) {
501 wlsc_sprite_attach(sprite, &background->surface);
507 wlsc_surface_draw(struct wlsc_surface *es,
508 struct wlsc_output *output, pixman_region32_t *clip)
510 struct wlsc_compositor *ec = es->compositor;
511 GLfloat *v, inv_width, inv_height;
513 pixman_region32_t repaint;
514 pixman_box32_t *rectangles;
517 pixman_region32_init_rect(&repaint,
518 es->x, es->y, es->width, es->height);
519 pixman_region32_intersect(&repaint, &repaint, clip);
520 if (!pixman_region32_not_empty(&repaint))
523 if (es->visual == &ec->compositor.argb_visual) {
524 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
526 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
527 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
533 rectangles = pixman_region32_rectangles(&repaint, &n);
534 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
535 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
536 inv_width = 1.0 / es->width;
537 inv_height = 1.0 / es->height;
538 for (i = 0; i < n; i++, v += 16, p += 6) {
539 v[ 0] = rectangles[i].x1;
540 v[ 1] = rectangles[i].y1;
541 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
542 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
544 v[ 4] = rectangles[i].x1;
545 v[ 5] = rectangles[i].y2;
547 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
549 v[ 8] = rectangles[i].x2;
550 v[ 9] = rectangles[i].y1;
551 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
554 v[12] = rectangles[i].x2;
555 v[13] = rectangles[i].y2;
567 glBindTexture(GL_TEXTURE_2D, es->texture);
568 v = ec->vertices.data;
569 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
570 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
571 glEnableVertexAttribArray(0);
572 glEnableVertexAttribArray(1);
573 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
575 ec->vertices.size = 0;
576 ec->indices.size = 0;
577 pixman_region32_fini(&repaint);
581 wlsc_surface_raise(struct wlsc_surface *surface)
583 struct wlsc_compositor *compositor = surface->compositor;
585 wl_list_remove(&surface->link);
586 wl_list_insert(&compositor->surface_list, &surface->link);
590 wlsc_surface_update_matrix(struct wlsc_surface *es)
592 wlsc_matrix_init(&es->matrix);
593 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
594 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
596 wlsc_matrix_init(&es->matrix_inv);
597 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
598 wlsc_matrix_scale(&es->matrix_inv,
599 1.0 / es->width, 1.0 / es->height, 1);
603 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
605 struct wlsc_output *output;
607 wl_list_for_each(output, &compositor->output_list, link)
608 wlsc_output_damage(output);
612 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
614 struct wlsc_compositor *compositor = output->compositor;
615 struct wlsc_surface *es;
616 struct wlsc_animation *animation, *next;
618 wl_list_for_each(es, &compositor->surface_list, link) {
619 if (es->output == output) {
620 wl_display_post_frame(compositor->wl_display,
621 &es->surface, msecs);
625 output->finished = 1;
627 wl_event_source_timer_update(compositor->timer_source, 5);
628 compositor->repaint_on_timeout = 1;
630 wl_list_for_each_safe(animation, next,
631 &compositor->animation_list, link)
632 animation->frame(animation, output, msecs);
636 wlsc_output_damage(struct wlsc_output *output)
638 struct wlsc_compositor *compositor = output->compositor;
640 pixman_region32_union_rect(&compositor->damage_region,
641 &compositor->damage_region,
642 output->x, output->y,
643 output->width, output->height);
644 wlsc_compositor_schedule_repaint(compositor);
648 fade_frame(struct wlsc_animation *animation,
649 struct wlsc_output *output, uint32_t msecs)
651 struct wlsc_compositor *compositor =
652 container_of(animation,
653 struct wlsc_compositor, fade.animation);
655 wlsc_tweener_update(&compositor->fade.tweener, msecs);
656 if (wlsc_tweener_done(&compositor->fade.tweener)) {
657 if (compositor->fade.tweener.current > 0.999)
658 compositor->state = WLSC_COMPOSITOR_SLEEPING;
659 compositor->fade.tweener.current =
660 compositor->fade.tweener.target;
661 wl_list_remove(&animation->link);
662 wl_list_init(&animation->link);
665 wlsc_output_damage(output);
669 fade_output(struct wlsc_output *output,
670 GLfloat tint, pixman_region32_t *region)
672 struct wlsc_compositor *compositor = output->compositor;
673 struct wlsc_surface surface;
674 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
676 surface.compositor = compositor;
677 surface.x = output->x;
678 surface.y = output->y;
679 surface.width = output->width;
680 surface.height = output->height;
681 surface.visual = &compositor->compositor.premultiplied_argb_visual;
682 surface.texture = GL_NONE;
683 glUseProgram(compositor->solid_shader.program);
684 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
685 1, GL_FALSE, output->matrix.d);
686 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
687 wlsc_surface_draw(&surface, output, region);
691 wlsc_output_repaint(struct wlsc_output *output)
693 struct wlsc_compositor *ec = output->compositor;
694 struct wlsc_surface *es;
695 struct wlsc_input_device *eid;
696 pixman_region32_t new_damage, total_damage, repaint;
697 int using_hardware_cursor = 1;
699 output->prepare_render(output);
701 glViewport(0, 0, output->width, output->height);
703 glUseProgram(ec->texture_shader.program);
704 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
705 1, GL_FALSE, output->matrix.d);
706 glUniform1i(ec->texture_shader.tex_uniform, 0);
708 pixman_region32_init(&new_damage);
709 pixman_region32_init(&total_damage);
710 pixman_region32_intersect_rect(&new_damage,
712 output->x, output->y,
713 output->width, output->height);
714 pixman_region32_subtract(&ec->damage_region,
715 &ec->damage_region, &new_damage);
716 pixman_region32_union(&total_damage, &new_damage,
717 &output->previous_damage_region);
718 pixman_region32_copy(&output->previous_damage_region, &new_damage);
720 if (ec->state == WLSC_COMPOSITOR_SLEEPING) {
721 glClear(GL_COLOR_BUFFER_BIT);
726 if (output->set_hardware_cursor(output, ec->input_device) < 0)
727 using_hardware_cursor = 0;
728 if (ec->fade.tweener.current > 0.001)
729 using_hardware_cursor = 0;
731 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
732 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN &&
733 es->fullscreen_output == output) {
734 if (es->visual == &ec->compositor.rgb_visual &&
735 using_hardware_cursor) {
736 if (output->prepare_scanout_surface(output, es) == 0) {
737 /* We're drawing nothing now,
738 * draw the damaged regions later. */
739 pixman_region32_union(&ec->damage_region,
746 if (es->width < output->width ||
747 es->height < output->height)
748 glClear(GL_COLOR_BUFFER_BIT);
749 wlsc_surface_draw(es, output, &total_damage);
751 wl_list_for_each(es, &ec->surface_list, link) {
752 if (es->visual != &ec->compositor.rgb_visual)
755 pixman_region32_init_rect(&repaint,
757 es->width, es->height);
758 wlsc_surface_draw(es, output, &total_damage);
759 pixman_region32_subtract(&total_damage,
760 &total_damage, &repaint);
763 if (output->background)
764 wlsc_surface_draw(output->background,
765 output, &total_damage);
767 glClear(GL_COLOR_BUFFER_BIT);
769 wl_list_for_each_reverse(es, &ec->surface_list, link) {
770 if (ec->overlay == es)
773 if (es->visual == &ec->compositor.rgb_visual) {
774 pixman_region32_union_rect(&total_damage,
777 es->width, es->height);
781 wlsc_surface_draw(es, output, &total_damage);
786 wlsc_surface_draw(ec->overlay, output, &total_damage);
789 wl_list_for_each(eid, &ec->input_device_list, link) {
790 if (&eid->input_device != ec->input_device ||
791 !using_hardware_cursor)
792 wlsc_surface_draw(eid->sprite, output,
796 if (ec->fade.tweener.current > 0.001)
797 fade_output(output, ec->fade.tweener.current, &total_damage);
803 struct wlsc_compositor *ec = data;
804 struct wlsc_output *output;
805 int repainted_all_outputs = 1;
807 wl_list_for_each(output, &ec->output_list, link) {
808 if (!output->repaint_needed)
811 if (!output->finished) {
812 repainted_all_outputs = 0;
816 wlsc_output_repaint(output);
817 output->finished = 0;
818 output->repaint_needed = 0;
819 output->present(output);
822 if (repainted_all_outputs)
823 ec->repaint_on_timeout = 0;
825 wl_event_source_timer_update(ec->timer_source, 1);
831 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
833 struct wlsc_output *output;
835 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
838 wl_list_for_each(output, &compositor->output_list, link)
839 output->repaint_needed = 1;
841 if (compositor->repaint_on_timeout)
844 wl_event_source_timer_update(compositor->timer_source, 1);
845 compositor->repaint_on_timeout = 1;
849 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
853 done = wlsc_tweener_done(&compositor->fade.tweener);
854 compositor->fade.tweener.target = tint;
855 if (wlsc_tweener_done(&compositor->fade.tweener))
859 compositor->fade.tweener.timestamp = get_time();
861 wlsc_compositor_damage_all(compositor);
862 if (wl_list_empty(&compositor->fade.animation.link))
863 wl_list_insert(compositor->animation_list.prev,
864 &compositor->fade.animation.link);
868 surface_destroy(struct wl_client *client,
869 struct wl_surface *surface)
871 wl_resource_destroy(&surface->resource, client);
875 wlsc_surface_assign_output(struct wlsc_surface *es)
877 struct wlsc_compositor *ec = es->compositor;
878 struct wlsc_output *output;
880 struct wlsc_output *tmp = es->output;
883 wl_list_for_each(output, &ec->output_list, link) {
884 if (output->x < es->x && es->x < output->x + output->width &&
885 output->y < es->y && es->y < output->y + output->height) {
887 printf("assiging surface %p to output %p\n",
893 if (es->output == NULL) {
894 printf("no output found\n");
895 es->output = container_of(ec->output_list.next,
896 struct wlsc_output, link);
901 surface_attach(struct wl_client *client,
902 struct wl_surface *surface, struct wl_buffer *buffer,
903 int32_t x, int32_t y)
905 struct wlsc_surface *es = (struct wlsc_surface *) surface;
907 /* FIXME: This damages the entire old surface, but we should
908 * really just damage the part that's no longer covered by the
909 * surface. Anything covered by the new surface will be
910 * damaged by the client. */
911 wlsc_surface_damage(es);
913 wlsc_buffer_attach(buffer, surface);
918 es->width = buffer->width;
919 es->height = buffer->height;
920 if (x != 0 || y != 0)
921 wlsc_surface_assign_output(es);
922 wlsc_surface_update_matrix(es);
926 surface_map_toplevel(struct wl_client *client,
927 struct wl_surface *surface)
929 struct wlsc_surface *es = (struct wlsc_surface *) surface;
930 struct wlsc_compositor *ec = es->compositor;
932 switch (es->map_type) {
933 case WLSC_SURFACE_MAP_UNMAPPED:
934 es->x = 10 + random() % 400;
935 es->y = 10 + random() % 400;
936 wlsc_surface_update_matrix(es);
937 /* assign to first output */
938 es->output = container_of(ec->output_list.next,
939 struct wlsc_output, link);
940 wl_list_insert(&es->compositor->surface_list, &es->link);
942 case WLSC_SURFACE_MAP_TOPLEVEL:
944 case WLSC_SURFACE_MAP_FULLSCREEN:
945 es->fullscreen_output = NULL;
948 wlsc_surface_update_matrix(es);
954 wlsc_surface_damage(es);
955 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
959 surface_map_transient(struct wl_client *client,
960 struct wl_surface *surface, struct wl_surface *parent,
961 int x, int y, uint32_t flags)
963 struct wlsc_surface *es = (struct wlsc_surface *) surface;
964 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
966 switch (es->map_type) {
967 case WLSC_SURFACE_MAP_UNMAPPED:
968 wl_list_insert(&es->compositor->surface_list, &es->link);
969 /* assign to parents output */
970 es->output = pes->output;
972 case WLSC_SURFACE_MAP_FULLSCREEN:
973 es->fullscreen_output = NULL;
982 wlsc_surface_update_matrix(es);
983 wlsc_surface_damage(es);
984 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
988 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
990 struct wlsc_surface *es = (struct wlsc_surface *) surface;
991 struct wlsc_output *output;
993 /* FIXME: Fullscreen on first output */
994 /* FIXME: Handle output going away */
995 output = container_of(es->compositor->output_list.next,
996 struct wlsc_output, link);
998 switch (es->map_type) {
999 case WLSC_SURFACE_MAP_UNMAPPED:
1000 es->x = 10 + random() % 400;
1001 es->y = 10 + random() % 400;
1002 /* assign to first output */
1003 es->output = output;
1004 wl_list_insert(&es->compositor->surface_list, &es->link);
1006 case WLSC_SURFACE_MAP_FULLSCREEN:
1012 es->saved_x = es->x;
1013 es->saved_y = es->y;
1014 es->x = (output->width - es->width) / 2;
1015 es->y = (output->height - es->height) / 2;
1016 es->fullscreen_output = output;
1017 wlsc_surface_update_matrix(es);
1018 wlsc_surface_damage(es);
1019 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
1023 surface_damage(struct wl_client *client,
1024 struct wl_surface *surface,
1025 int32_t x, int32_t y, int32_t width, int32_t height)
1027 struct wlsc_surface *es = (struct wlsc_surface *) surface;
1029 wlsc_surface_damage_rectangle(es, x, y, width, height);
1032 const static struct wl_surface_interface surface_interface = {
1035 surface_map_toplevel,
1036 surface_map_transient,
1037 surface_map_fullscreen,
1042 wlsc_input_device_attach(struct wlsc_input_device *device,
1043 int x, int y, int width, int height)
1045 wlsc_surface_damage(device->sprite);
1047 device->hotspot_x = x;
1048 device->hotspot_y = y;
1050 device->sprite->x = device->input_device.x - device->hotspot_x;
1051 device->sprite->y = device->input_device.y - device->hotspot_y;
1052 device->sprite->width = width;
1053 device->sprite->height = height;
1054 wlsc_surface_update_matrix(device->sprite);
1056 wlsc_surface_damage(device->sprite);
1060 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1061 struct wl_buffer *buffer, int x, int y)
1063 wlsc_buffer_attach(buffer, &device->sprite->surface);
1064 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1068 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1069 struct wlsc_sprite *sprite, int x, int y)
1071 wlsc_sprite_attach(sprite, &device->sprite->surface);
1072 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1076 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1077 enum wlsc_pointer_type type)
1079 struct wlsc_compositor *compositor =
1080 (struct wlsc_compositor *) device->input_device.compositor;
1082 wlsc_input_device_attach_sprite(device,
1083 compositor->pointer_sprites[type],
1084 pointer_images[type].hotspot_x,
1085 pointer_images[type].hotspot_y);
1089 compositor_create_surface(struct wl_client *client,
1090 struct wl_compositor *compositor, uint32_t id)
1092 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1093 struct wlsc_surface *surface;
1095 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1096 if (surface == NULL) {
1097 wl_client_post_no_memory(client);
1101 surface->surface.resource.destroy = destroy_surface;
1103 surface->surface.resource.object.id = id;
1104 surface->surface.resource.object.interface = &wl_surface_interface;
1105 surface->surface.resource.object.implementation =
1106 (void (**)(void)) &surface_interface;
1107 surface->surface.client = client;
1109 wl_client_add_resource(client, &surface->surface.resource);
1112 const static struct wl_compositor_interface compositor_interface = {
1113 compositor_create_surface,
1117 wlsc_surface_transform(struct wlsc_surface *surface,
1118 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1120 struct wlsc_vector v = { { x, y, 0, 1 } };
1122 wlsc_matrix_transform(&surface->matrix_inv, &v);
1123 *sx = v.f[0] * surface->width;
1124 *sy = v.f[1] * surface->height;
1127 struct wlsc_surface *
1128 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1130 struct wlsc_compositor *ec =
1131 (struct wlsc_compositor *) device->compositor;
1132 struct wlsc_surface *es;
1134 wl_list_for_each(es, &ec->surface_list, link) {
1135 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1136 if (0 <= *sx && *sx < es->width &&
1137 0 <= *sy && *sy < es->height)
1146 motion_grab_motion(struct wl_grab *grab,
1147 uint32_t time, int32_t x, int32_t y)
1149 struct wlsc_input_device *device =
1150 (struct wlsc_input_device *) grab->input_device;
1151 struct wlsc_surface *es =
1152 (struct wlsc_surface *) device->input_device.pointer_focus;
1155 wlsc_surface_transform(es, x, y, &sx, &sy);
1156 wl_client_post_event(es->surface.client,
1157 &device->input_device.object,
1158 WL_INPUT_DEVICE_MOTION,
1159 time, x, y, sx, sy);
1163 motion_grab_button(struct wl_grab *grab,
1164 uint32_t time, int32_t button, int32_t state)
1166 wl_client_post_event(grab->input_device->pointer_focus->client,
1167 &grab->input_device->object,
1168 WL_INPUT_DEVICE_BUTTON,
1169 time, button, state);
1173 motion_grab_end(struct wl_grab *grab, uint32_t time)
1177 static const struct wl_grab_interface motion_grab_interface = {
1184 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1186 if (compositor->idle_inhibit)
1189 wlsc_compositor_fade(compositor, 0.0);
1190 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1192 wl_event_source_timer_update(compositor->idle_source,
1193 option_idle_time * 1000);
1197 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1199 wlsc_compositor_wake(compositor);
1200 compositor->idle_inhibit++;
1204 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1206 compositor->idle_inhibit--;
1207 wlsc_compositor_wake(compositor);
1211 idle_handler(void *data)
1213 struct wlsc_compositor *compositor = data;
1215 if (compositor->idle_inhibit)
1218 wlsc_compositor_fade(compositor, 1.0);
1224 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1226 struct wlsc_surface *es;
1227 struct wlsc_compositor *ec =
1228 (struct wlsc_compositor *) device->compositor;
1229 struct wlsc_output *output;
1230 const struct wl_grab_interface *interface;
1231 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1233 int x_valid = 0, y_valid = 0;
1234 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1236 wlsc_compositor_wake(ec);
1238 wl_list_for_each(output, &ec->output_list, link) {
1239 if (output->x <= x && x <= output->x + output->width)
1242 if (output->y <= y && y <= output->y + output->height)
1245 /* FIXME: calculate this only on output addition/deletion */
1246 if (output->x < min_x)
1248 if (output->y < min_y)
1251 if (output->x + output->width > max_x)
1252 max_x = output->x + output->width;
1253 if (output->y + output->height > max_y)
1254 max_y = output->y + output->height;
1260 else if (x >= max_x)
1266 else if (y >= max_y)
1274 interface = device->grab->interface;
1275 interface->motion(device->grab, time, x, y);
1277 es = pick_surface(device, &sx, &sy);
1278 wl_input_device_set_pointer_focus(device,
1280 time, x, y, sx, sy);
1282 wl_client_post_event(es->surface.client,
1284 WL_INPUT_DEVICE_MOTION,
1285 time, x, y, sx, sy);
1288 wlsc_surface_damage(wd->sprite);
1290 wd->sprite->x = device->x - wd->hotspot_x;
1291 wd->sprite->y = device->y - wd->hotspot_y;
1292 wlsc_surface_update_matrix(wd->sprite);
1294 wlsc_surface_damage(wd->sprite);
1298 wlsc_surface_activate(struct wlsc_surface *surface,
1299 struct wlsc_input_device *device, uint32_t time)
1301 wlsc_surface_raise(surface);
1302 if (device->selection)
1303 wlsc_selection_set_focus(device->selection,
1304 &surface->surface, time);
1306 wl_input_device_set_keyboard_focus(&device->input_device,
1311 struct wlsc_binding {
1315 wlsc_binding_handler_t handler;
1317 struct wl_list link;
1321 notify_button(struct wl_input_device *device,
1322 uint32_t time, int32_t button, int32_t state)
1324 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1325 struct wlsc_compositor *compositor =
1326 (struct wlsc_compositor *) device->compositor;
1327 struct wlsc_binding *b;
1328 struct wlsc_surface *surface =
1329 (struct wlsc_surface *) device->pointer_focus;
1332 wlsc_compositor_idle_inhibit(compositor);
1334 wlsc_compositor_idle_release(compositor);
1336 if (state && surface && device->grab == NULL) {
1337 wlsc_surface_activate(surface, wd, time);
1338 wl_input_device_start_grab(device,
1339 &device->motion_grab,
1343 wl_list_for_each(b, &compositor->binding_list, link) {
1344 if (b->button == button &&
1345 b->modifier == wd->modifier_state && state) {
1346 b->handler(&wd->input_device,
1347 time, 0, button, state, b->data);
1353 device->grab->interface->button(device->grab, time,
1356 if (!state && device->grab && device->grab_button == button)
1357 wl_input_device_end_grab(device, time);
1361 terminate_binding(struct wl_input_device *device, uint32_t time,
1362 uint32_t key, uint32_t button, uint32_t state, void *data)
1364 struct wlsc_compositor *compositor = data;
1367 wl_display_terminate(compositor->wl_display);
1370 struct wlsc_binding *
1371 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1372 uint32_t key, uint32_t button, uint32_t modifier,
1373 wlsc_binding_handler_t handler, void *data)
1375 struct wlsc_binding *binding;
1377 binding = malloc(sizeof *binding);
1378 if (binding == NULL)
1382 binding->button = button;
1383 binding->modifier = modifier;
1384 binding->handler = handler;
1385 binding->data = data;
1386 wl_list_insert(compositor->binding_list.prev, &binding->link);
1392 wlsc_binding_destroy(struct wlsc_binding *binding)
1394 wl_list_remove(&binding->link);
1399 update_modifier_state(struct wlsc_input_device *device,
1400 uint32_t key, uint32_t state)
1407 modifier = MODIFIER_CTRL;
1412 modifier = MODIFIER_ALT;
1417 modifier = MODIFIER_SUPER;
1426 device->modifier_state |= modifier;
1428 device->modifier_state &= ~modifier;
1432 notify_key(struct wl_input_device *device,
1433 uint32_t time, uint32_t key, uint32_t state)
1435 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1436 struct wlsc_compositor *compositor =
1437 (struct wlsc_compositor *) device->compositor;
1439 struct wlsc_binding *b;
1442 wlsc_compositor_idle_inhibit(compositor);
1444 wlsc_compositor_idle_release(compositor);
1446 wl_list_for_each(b, &compositor->binding_list, link) {
1447 if (b->key == key &&
1448 b->modifier == wd->modifier_state) {
1449 b->handler(&wd->input_device,
1450 time, key, 0, state, b->data);
1455 update_modifier_state(wd, key, state);
1456 end = device->keys.data + device->keys.size;
1457 for (k = device->keys.data; k < end; k++) {
1461 device->keys.size = (void *) end - device->keys.data;
1463 k = wl_array_add(&device->keys, sizeof *k);
1467 if (device->keyboard_focus != NULL)
1468 wl_client_post_event(device->keyboard_focus->client,
1470 WL_INPUT_DEVICE_KEY, time, key, state);
1474 notify_pointer_focus(struct wl_input_device *device,
1475 uint32_t time, struct wlsc_output *output,
1476 int32_t x, int32_t y)
1478 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1479 struct wlsc_compositor *compositor =
1480 (struct wlsc_compositor *) device->compositor;
1481 struct wlsc_surface *es;
1487 es = pick_surface(device, &sx, &sy);
1488 wl_input_device_set_pointer_focus(device,
1490 time, x, y, sx, sy);
1492 compositor->focus = 1;
1494 wd->sprite->x = device->x - wd->hotspot_x;
1495 wd->sprite->y = device->y - wd->hotspot_y;
1496 wlsc_surface_update_matrix(wd->sprite);
1498 wl_input_device_set_pointer_focus(device, NULL,
1500 compositor->focus = 0;
1503 wlsc_surface_damage(wd->sprite);
1507 notify_keyboard_focus(struct wl_input_device *device,
1508 uint32_t time, struct wlsc_output *output,
1509 struct wl_array *keys)
1511 struct wlsc_input_device *wd =
1512 (struct wlsc_input_device *) device;
1513 struct wlsc_compositor *compositor =
1514 (struct wlsc_compositor *) device->compositor;
1515 struct wlsc_surface *es;
1518 if (!wl_list_empty(&compositor->surface_list))
1519 es = container_of(compositor->surface_list.next,
1520 struct wlsc_surface, link);
1525 wl_array_copy(&wd->input_device.keys, keys);
1526 wd->modifier_state = 0;
1527 end = device->keys.data + device->keys.size;
1528 for (k = device->keys.data; k < end; k++) {
1529 wlsc_compositor_idle_inhibit(compositor);
1530 update_modifier_state(wd, *k, 1);
1533 wl_input_device_set_keyboard_focus(&wd->input_device,
1534 &es->surface, time);
1536 end = device->keys.data + device->keys.size;
1537 for (k = device->keys.data; k < end; k++)
1538 wlsc_compositor_idle_release(compositor);
1540 wd->modifier_state = 0;
1541 wl_input_device_set_keyboard_focus(&wd->input_device,
1548 input_device_attach(struct wl_client *client,
1549 struct wl_input_device *device_base,
1551 struct wl_buffer *buffer, int32_t x, int32_t y)
1553 struct wlsc_input_device *device =
1554 (struct wlsc_input_device *) device_base;
1556 if (time < device->input_device.pointer_focus_time)
1558 if (device->input_device.pointer_focus == NULL)
1560 if (device->input_device.pointer_focus->client != client)
1563 if (buffer == NULL) {
1564 wlsc_input_device_set_pointer_image(device,
1565 WLSC_POINTER_LEFT_PTR);
1569 wlsc_input_device_attach_buffer(device, buffer, x, y);
1572 const static struct wl_input_device_interface input_device_interface = {
1573 input_device_attach,
1577 wlsc_input_device_init(struct wlsc_input_device *device,
1578 struct wlsc_compositor *ec)
1580 wl_input_device_init(&device->input_device, &ec->compositor);
1582 device->input_device.object.interface = &wl_input_device_interface;
1583 device->input_device.object.implementation =
1584 (void (**)(void)) &input_device_interface;
1585 wl_display_add_object(ec->wl_display, &device->input_device.object);
1586 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1588 device->sprite = wlsc_surface_create(ec,
1589 device->input_device.x,
1590 device->input_device.y, 32, 32);
1591 device->hotspot_x = 16;
1592 device->hotspot_y = 16;
1593 device->modifier_state = 0;
1595 device->input_device.motion_grab.interface = &motion_grab_interface;
1597 wl_list_insert(ec->input_device_list.prev, &device->link);
1599 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1603 wlsc_output_post_geometry(struct wl_client *client,
1604 struct wl_object *global, uint32_t version)
1606 struct wlsc_output *output =
1607 container_of(global, struct wlsc_output, object);
1609 wl_client_post_event(client, global,
1611 output->x, output->y,
1612 output->width, output->height);
1615 static const char vertex_shader[] =
1616 "uniform mat4 proj;\n"
1617 "attribute vec2 position;\n"
1618 "attribute vec2 texcoord;\n"
1619 "varying vec2 v_texcoord;\n"
1622 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1623 " v_texcoord = texcoord;\n"
1626 static const char texture_fragment_shader[] =
1627 "precision mediump float;\n"
1628 "varying vec2 v_texcoord;\n"
1629 "uniform sampler2D tex;\n"
1632 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1635 static const char solid_fragment_shader[] =
1636 "precision mediump float;\n"
1637 "varying vec2 v_texcoord;\n"
1638 "uniform vec4 color;\n"
1641 " gl_FragColor = color\n;"
1645 compile_shader(GLenum type, const char *source)
1651 s = glCreateShader(type);
1652 glShaderSource(s, 1, &source, NULL);
1654 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1656 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1657 fprintf(stderr, "shader info: %s\n", msg);
1665 wlsc_shader_init(struct wlsc_shader *shader,
1666 const char *vertex_source, const char *fragment_source)
1671 shader->vertex_shader =
1672 compile_shader(GL_VERTEX_SHADER, vertex_source);
1673 shader->fragment_shader =
1674 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1676 shader->program = glCreateProgram();
1677 glAttachShader(shader->program, shader->vertex_shader);
1678 glAttachShader(shader->program, shader->fragment_shader);
1679 glBindAttribLocation(shader->program, 0, "position");
1680 glBindAttribLocation(shader->program, 1, "texcoord");
1682 glLinkProgram(shader->program);
1683 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1685 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1686 fprintf(stderr, "link info: %s\n", msg);
1690 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1691 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1697 init_solid_shader(struct wlsc_shader *shader,
1698 GLuint vertex_shader, const char *fragment_source)
1703 shader->vertex_shader = vertex_shader;
1704 shader->fragment_shader =
1705 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1707 shader->program = glCreateProgram();
1708 glAttachShader(shader->program, shader->vertex_shader);
1709 glAttachShader(shader->program, shader->fragment_shader);
1710 glBindAttribLocation(shader->program, 0, "position");
1711 glBindAttribLocation(shader->program, 1, "texcoord");
1713 glLinkProgram(shader->program);
1714 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1716 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1717 fprintf(stderr, "link info: %s\n", msg);
1721 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1722 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1728 wlsc_output_destroy(struct wlsc_output *output)
1730 destroy_surface(&output->background->surface.resource, NULL);
1734 wlsc_output_move(struct wlsc_output *output, int x, int y)
1736 struct wlsc_compositor *c = output->compositor;
1742 if (output->background) {
1743 output->background->x = x;
1744 output->background->y = y;
1745 wlsc_surface_update_matrix(output->background);
1748 pixman_region32_init(&output->previous_damage_region);
1750 wlsc_matrix_init(&output->matrix);
1751 wlsc_matrix_translate(&output->matrix,
1752 -output->x - output->width / 2.0,
1753 -output->y - output->height / 2.0, 0);
1755 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1756 wlsc_matrix_scale(&output->matrix,
1757 2.0 / output->width,
1758 flip * 2.0 / output->height, 1);
1760 pixman_region32_union_rect(&c->damage_region,
1762 x, y, output->width, output->height);
1766 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1767 int x, int y, int width, int height, uint32_t flags)
1769 output->compositor = c;
1772 output->width = width;
1773 output->height = height;
1775 output->background =
1776 background_create(output, option_background);
1778 output->flags = flags;
1779 output->finished = 1;
1780 wlsc_output_move(output, x, y);
1782 output->object.interface = &wl_output_interface;
1783 wl_display_add_object(c->wl_display, &output->object);
1784 wl_display_add_global(c->wl_display, &output->object,
1785 wlsc_output_post_geometry);
1789 shm_buffer_created(struct wl_buffer *buffer)
1791 struct wl_list *surfaces_attached_to;
1793 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1794 if (!surfaces_attached_to) {
1795 buffer->user_data = NULL;
1799 wl_list_init(surfaces_attached_to);
1801 buffer->user_data = surfaces_attached_to;
1805 shm_buffer_damaged(struct wl_buffer *buffer,
1806 int32_t x, int32_t y, int32_t width, int32_t height)
1808 struct wl_list *surfaces_attached_to = buffer->user_data;
1809 struct wlsc_surface *es;
1811 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1812 glBindTexture(GL_TEXTURE_2D, es->texture);
1813 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1814 buffer->width, buffer->height, 0,
1815 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1816 wl_shm_buffer_get_data(buffer));
1817 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1818 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1823 shm_buffer_destroyed(struct wl_buffer *buffer)
1825 struct wl_list *surfaces_attached_to = buffer->user_data;
1826 struct wlsc_surface *es, *next;
1828 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1830 wl_list_remove(&es->buffer_link);
1833 free(surfaces_attached_to);
1836 const static struct wl_shm_callbacks shm_callbacks = {
1839 shm_buffer_destroyed
1843 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1845 struct wl_event_loop *loop;
1846 const char *extensions;
1848 ec->wl_display = display;
1850 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1852 ec->shm = wl_shm_init(display, &shm_callbacks);
1853 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
1855 wl_list_init(&ec->surface_list);
1856 wl_list_init(&ec->input_device_list);
1857 wl_list_init(&ec->output_list);
1858 wl_list_init(&ec->binding_list);
1859 wl_list_init(&ec->animation_list);
1860 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1861 ec->fade.animation.frame = fade_frame;
1862 wl_list_init(&ec->fade.animation.link);
1864 wlsc_shell_init(ec);
1865 wlsc_switcher_init(ec);
1867 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1868 MODIFIER_CTRL | MODIFIER_ALT,
1869 terminate_binding, ec);
1871 create_pointer_images(ec);
1873 screenshooter_create(ec);
1875 extensions = (const char *) glGetString(GL_EXTENSIONS);
1876 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1878 "GL_EXT_texture_format_BGRA8888 not available\n");
1882 glActiveTexture(GL_TEXTURE0);
1884 if (wlsc_shader_init(&ec->texture_shader,
1885 vertex_shader, texture_fragment_shader) < 0)
1887 if (init_solid_shader(&ec->solid_shader,
1888 ec->texture_shader.vertex_shader,
1889 solid_fragment_shader) < 0)
1892 loop = wl_display_get_event_loop(ec->wl_display);
1893 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1894 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1896 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1897 pixman_region32_init(&ec->damage_region);
1898 wlsc_compositor_schedule_repaint(ec);
1903 static int on_term_signal(int signal_number, void *data)
1905 struct wlsc_compositor *ec = data;
1907 wl_display_terminate(ec->wl_display);
1912 int main(int argc, char *argv[])
1914 struct wl_display *display;
1915 struct wlsc_compositor *ec;
1916 struct wl_event_loop *loop;
1917 GError *error = NULL;
1918 GOptionContext *context;
1921 g_type_init(); /* GdkPixbuf needs this, it seems. */
1923 context = g_option_context_new(NULL);
1924 g_option_context_add_main_entries(context, option_entries, "Wayland");
1925 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1926 fprintf(stderr, "option parsing failed: %s\n", error->message);
1929 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1930 fprintf(stderr, "invalid geometry option: %s \n",
1935 display = wl_display_create();
1939 #if BUILD_WAYLAND_COMPOSITOR
1940 if (getenv("WAYLAND_DISPLAY"))
1941 ec = wayland_compositor_create(display, width, height);
1944 #if BUILD_X11_COMPOSITOR
1945 if (ec == NULL && getenv("DISPLAY"))
1946 ec = x11_compositor_create(display, width, height);
1949 #if BUILD_OPENWFD_COMPOSITOR
1950 if (ec == NULL && getenv("OPENWFD"))
1951 ec = wfd_compositor_create(display, option_connector);
1954 #if BUILD_DRM_COMPOSITOR
1956 ec = drm_compositor_create(display, option_connector);
1960 fprintf(stderr, "failed to create compositor\n");
1964 if (wl_display_add_socket(display, option_socket_name)) {
1965 fprintf(stderr, "failed to add socket: %m\n");
1969 loop = wl_display_get_event_loop(ec->wl_display);
1970 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1971 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1973 wl_display_run(display);
1975 eglUnbindWaylandDisplayWL(ec->display, display);
1976 wl_display_destroy(display);