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.
30 #include <sys/ioctl.h>
35 #include <linux/input.h>
40 #include "wayland-server.h"
41 #include "compositor.h"
43 /* The plan here is to generate a random anonymous socket name and
44 * advertise that through a service on the session dbus.
46 static const char *option_socket_name = NULL;
47 static const char *option_background = "background.png";
48 static int option_idle_time = 300;
50 static struct wl_list child_process_list;
53 sigchld_handler(int signal_number, void *data)
55 struct wlsc_process *p;
60 wl_list_for_each(p, &child_process_list, link) {
65 if (&p->link == &child_process_list) {
66 fprintf(stderr, "unknown child process exited\n");
70 wl_list_remove(&p->link);
71 p->cleanup(p, status);
77 wlsc_watch_process(struct wlsc_process *process)
79 wl_list_insert(&child_process_list, &process->link);
83 wlsc_matrix_init(struct wlsc_matrix *matrix)
85 static const struct wlsc_matrix identity = {
86 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
89 memcpy(matrix, &identity, sizeof identity);
93 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
95 struct wlsc_matrix tmp;
96 const GLfloat *row, *column;
100 for (i = 0; i < 16; i++) {
103 row = m->d + d.quot * 4;
104 column = n->d + d.rem;
105 for (j = 0; j < 4; j++)
106 tmp.d[i] += row[j] * column[j * 4];
108 memcpy(m, &tmp, sizeof tmp);
112 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
114 struct wlsc_matrix translate = {
115 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
118 wlsc_matrix_multiply(matrix, &translate);
122 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
124 struct wlsc_matrix scale = {
125 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
128 wlsc_matrix_multiply(matrix, &scale);
132 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
135 struct wlsc_vector t;
137 for (i = 0; i < 4; i++) {
139 for (j = 0; j < 4; j++)
140 t.f[i] += v->f[j] * matrix->d[i + j * 4];
147 wlsc_spring_init(struct wlsc_spring *spring,
148 double k, double current, double target)
151 spring->friction = 100.0;
152 spring->current = current;
153 spring->previous = current;
154 spring->target = target;
158 wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec)
160 double force, v, current, step;
162 step = (msec - spring->timestamp) / 300.0;
163 spring->timestamp = msec;
165 current = spring->current;
166 v = current - spring->previous;
167 force = spring->k * (spring->target - current) / 10.0 +
168 (spring->previous - current) - v * spring->friction;
171 current + (current - spring->previous) + force * step * step;
172 spring->previous = current;
175 if (spring->current >= 1.0) {
176 #ifdef TWEENER_BOUNCE
177 spring->current = 2.0 - spring->current;
178 spring->previous = 2.0 - spring->previous;
180 spring->current = 1.0;
181 spring->previous = 1.0;
185 if (spring->current <= 0.0) {
186 spring->current = 0.0;
187 spring->previous = 0.0;
193 wlsc_spring_done(struct wlsc_spring *spring)
195 return fabs(spring->previous - spring->target) < 0.0002 &&
196 fabs(spring->current - spring->target) < 0.0002;
200 surface_handle_buffer_destroy(struct wl_listener *listener,
201 struct wl_resource *resource, uint32_t time)
203 struct wlsc_surface *es = container_of(listener, struct wlsc_surface,
204 buffer_destroy_listener);
205 struct wl_buffer *buffer = (struct wl_buffer *) resource;
207 if (es->buffer == buffer)
212 output_handle_scanout_buffer_destroy(struct wl_listener *listener,
213 struct wl_resource *resource,
216 struct wlsc_output *output =
217 container_of(listener, struct wlsc_output,
218 scanout_buffer_destroy_listener);
219 struct wl_buffer *buffer = (struct wl_buffer *) resource;
221 if (output->scanout_buffer == buffer)
222 output->scanout_buffer = NULL;
225 WL_EXPORT struct wlsc_surface *
226 wlsc_surface_create(struct wlsc_compositor *compositor,
227 int32_t x, int32_t y, int32_t width, int32_t height)
229 struct wlsc_surface *surface;
231 surface = malloc(sizeof *surface);
235 wl_list_init(&surface->link);
236 wl_list_init(&surface->buffer_link);
237 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
239 glGenTextures(1, &surface->texture);
240 glBindTexture(GL_TEXTURE_2D, surface->texture);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
244 surface->surface.resource.client = NULL;
246 surface->compositor = compositor;
247 surface->visual = WLSC_NONE_VISUAL;
248 surface->image = EGL_NO_IMAGE_KHR;
249 surface->saved_texture = 0;
252 surface->width = width;
253 surface->height = height;
255 surface->buffer = NULL;
256 surface->output = NULL;
258 pixman_region32_init(&surface->damage);
259 pixman_region32_init(&surface->opaque);
261 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
262 wl_list_init(&surface->buffer_destroy_listener.link);
264 surface->transform = NULL;
270 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
271 int32_t x, int32_t y,
272 int32_t width, int32_t height)
274 struct wlsc_compositor *compositor = surface->compositor;
276 pixman_region32_union_rect(&surface->damage,
278 surface->x + x, surface->y + y,
280 wlsc_compositor_schedule_repaint(compositor);
284 wlsc_surface_damage(struct wlsc_surface *surface)
286 wlsc_surface_damage_rectangle(surface, 0, 0,
287 surface->width, surface->height);
291 wlsc_surface_damage_below(struct wlsc_surface *surface)
293 struct wlsc_surface *below;
295 if (surface->link.next == &surface->compositor->surface_list)
298 below = container_of(surface->link.next, struct wlsc_surface, link);
300 pixman_region32_union_rect(&below->damage,
302 surface->x, surface->y,
303 surface->width, surface->height);
304 wlsc_compositor_schedule_repaint(surface->compositor);
308 wlsc_surface_configure(struct wlsc_surface *surface,
309 int x, int y, int width, int height)
311 wlsc_surface_damage_below(surface);
315 surface->width = width;
316 surface->height = height;
318 wlsc_surface_assign_output(surface);
319 wlsc_surface_damage(surface);
321 pixman_region32_fini(&surface->opaque);
322 if (surface->visual == WLSC_RGB_VISUAL)
323 pixman_region32_init_rect(&surface->opaque,
324 surface->x, surface->y,
325 surface->width, surface->height);
327 pixman_region32_init(&surface->opaque);
331 wlsc_compositor_get_time(void)
335 gettimeofday(&tv, NULL);
337 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
341 destroy_surface(struct wl_resource *resource)
343 struct wlsc_surface *surface =
344 container_of(resource, struct wlsc_surface, surface.resource);
345 struct wlsc_compositor *compositor = surface->compositor;
347 wlsc_surface_damage_below(surface);
349 wl_list_remove(&surface->link);
350 if (surface->saved_texture == 0)
351 glDeleteTextures(1, &surface->texture);
353 glDeleteTextures(1, &surface->saved_texture);
356 wl_list_remove(&surface->buffer_destroy_listener.link);
358 if (surface->image != EGL_NO_IMAGE_KHR)
359 compositor->destroy_image(compositor->display,
362 wl_list_remove(&surface->buffer_link);
368 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
370 struct wlsc_surface *es = (struct wlsc_surface *) surface;
371 struct wlsc_compositor *ec = es->compositor;
372 struct wl_list *surfaces_attached_to;
374 if (es->saved_texture != 0)
375 es->texture = es->saved_texture;
377 glBindTexture(GL_TEXTURE_2D, es->texture);
379 if (wl_buffer_is_shm(buffer)) {
380 /* Unbind any EGLImage texture that may be bound, so we don't
382 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
383 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
384 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
385 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
386 es->pitch, buffer->height, 0,
387 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
388 wl_shm_buffer_get_data(buffer));
390 switch (wl_shm_buffer_get_format(buffer)) {
391 case WL_SHM_FORMAT_ARGB32:
392 es->visual = WLSC_ARGB_VISUAL;
394 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
395 es->visual = WLSC_PREMUL_ARGB_VISUAL;
397 case WL_SHM_FORMAT_XRGB32:
398 es->visual = WLSC_RGB_VISUAL;
402 surfaces_attached_to = buffer->user_data;
404 wl_list_remove(&es->buffer_link);
405 wl_list_insert(surfaces_attached_to, &es->buffer_link);
407 if (es->image != EGL_NO_IMAGE_KHR)
408 ec->destroy_image(ec->display, es->image);
409 es->image = ec->create_image(ec->display, NULL,
410 EGL_WAYLAND_BUFFER_WL,
413 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
415 /* FIXME: we need to get the visual from the wl_buffer */
416 es->visual = WLSC_PREMUL_ARGB_VISUAL;
417 es->pitch = es->width;
422 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
424 struct wlsc_surface *es = (struct wlsc_surface *) surface;
425 struct wlsc_compositor *ec = es->compositor;
427 es->pitch = es->width;
428 es->image = sprite->image;
429 if (sprite->image != EGL_NO_IMAGE_KHR) {
430 glBindTexture(GL_TEXTURE_2D, es->texture);
431 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
433 if (es->saved_texture == 0)
434 es->saved_texture = es->texture;
435 es->texture = sprite->texture;
438 es->visual = sprite->visual;
445 SPRITE_USE_CURSOR = (1 << 0),
448 static struct wlsc_sprite *
449 create_sprite_from_png(struct wlsc_compositor *ec,
450 const char *filename, uint32_t usage)
453 struct wlsc_sprite *sprite;
454 int32_t width, height;
457 pixels = wlsc_load_image(filename, &width, &height, &stride);
461 sprite = malloc(sizeof *sprite);
462 if (sprite == NULL) {
467 sprite->visual = WLSC_PREMUL_ARGB_VISUAL;
468 sprite->width = width;
469 sprite->height = height;
470 sprite->image = EGL_NO_IMAGE_KHR;
472 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
473 sprite->image = ec->create_cursor_image(ec, width, height);
475 glGenTextures(1, &sprite->texture);
476 glBindTexture(GL_TEXTURE_2D, sprite->texture);
477 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
478 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
482 if (sprite->image != EGL_NO_IMAGE_KHR) {
483 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
484 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
485 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
487 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
488 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
496 static const struct {
497 const char *filename;
498 int hotspot_x, hotspot_y;
499 } pointer_images[] = {
500 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
501 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
502 { DATADIR "/wayland/bottom_side.png", 16, 20 },
503 { DATADIR "/wayland/grabbing.png", 20, 17 },
504 { DATADIR "/wayland/left_ptr.png", 10, 5 },
505 { DATADIR "/wayland/left_side.png", 10, 20 },
506 { DATADIR "/wayland/right_side.png", 30, 19 },
507 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
508 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
509 { DATADIR "/wayland/top_side.png", 18, 8 },
510 { DATADIR "/wayland/xterm.png", 15, 15 }
514 create_pointer_images(struct wlsc_compositor *ec)
518 count = ARRAY_LENGTH(pointer_images);
519 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
520 for (i = 0; i < count; i++) {
521 ec->pointer_sprites[i] =
522 create_sprite_from_png(ec,
523 pointer_images[i].filename,
528 static struct wlsc_surface *
529 background_create(struct wlsc_output *output, const char *filename)
531 struct wlsc_surface *background;
532 struct wlsc_sprite *sprite;
534 background = wlsc_surface_create(output->compositor,
535 output->x, output->y,
536 output->current->width,
537 output->current->height);
538 if (background == NULL)
541 sprite = create_sprite_from_png(output->compositor, filename, 0);
542 if (sprite == NULL) {
547 wlsc_sprite_attach(sprite, &background->surface);
553 texture_region(struct wlsc_surface *es, pixman_region32_t *region)
555 struct wlsc_compositor *ec = es->compositor;
556 GLfloat *v, inv_width, inv_height;
557 pixman_box32_t *rectangles;
561 rectangles = pixman_region32_rectangles(region, &n);
562 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
563 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
564 inv_width = 1.0 / es->pitch;
565 inv_height = 1.0 / es->height;
567 for (i = 0; i < n; i++, v += 16, p += 6) {
568 v[ 0] = rectangles[i].x1;
569 v[ 1] = rectangles[i].y1;
570 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
571 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
573 v[ 4] = rectangles[i].x1;
574 v[ 5] = rectangles[i].y2;
576 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
578 v[ 8] = rectangles[i].x2;
579 v[ 9] = rectangles[i].y1;
580 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
583 v[12] = rectangles[i].x2;
584 v[13] = rectangles[i].y2;
600 transform_vertex(struct wlsc_surface *surface,
601 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
603 struct wlsc_vector t;
610 wlsc_matrix_transform(&surface->transform->matrix, &t);
619 texture_transformed_surface(struct wlsc_surface *es)
621 struct wlsc_compositor *ec = es->compositor;
625 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
626 p = wl_array_add(&ec->indices, 6 * sizeof *p);
628 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
629 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
630 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
631 transform_vertex(es, es->x + es->width, es->y + es->height,
645 wlsc_surface_draw(struct wlsc_surface *es,
646 struct wlsc_output *output, pixman_region32_t *clip)
648 struct wlsc_compositor *ec = es->compositor;
650 pixman_region32_t repaint;
654 pixman_region32_init_rect(&repaint,
655 es->x, es->y, es->width, es->height);
656 pixman_region32_intersect(&repaint, &repaint, clip);
657 if (!pixman_region32_not_empty(&repaint))
660 switch (es->visual) {
661 case WLSC_ARGB_VISUAL:
662 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
665 case WLSC_PREMUL_ARGB_VISUAL:
666 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
669 case WLSC_RGB_VISUAL:
673 fprintf(stderr, "bogus visual\n");
677 if (es->transform == NULL) {
679 n = texture_region(es, &repaint);
682 n = texture_transformed_surface(es);
685 glBindTexture(GL_TEXTURE_2D, es->texture);
686 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
687 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
689 v = ec->vertices.data;
690 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
691 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
692 glEnableVertexAttribArray(0);
693 glEnableVertexAttribArray(1);
694 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
696 ec->vertices.size = 0;
697 ec->indices.size = 0;
698 pixman_region32_fini(&repaint);
702 wlsc_surface_raise(struct wlsc_surface *surface)
704 struct wlsc_compositor *compositor = surface->compositor;
706 wl_list_remove(&surface->link);
707 wl_list_insert(&compositor->surface_list, &surface->link);
708 wlsc_surface_damage(surface);
712 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
714 struct wlsc_output *output;
716 wl_list_for_each(output, &compositor->output_list, link)
717 wlsc_output_damage(output);
721 wlsc_buffer_post_release(struct wl_buffer *buffer)
723 if (buffer == NULL || --buffer->busy_count > 0)
726 assert(buffer->resource.client != NULL);
727 wl_resource_post_event(&buffer->resource, WL_BUFFER_RELEASE);
731 wlsc_output_damage(struct wlsc_output *output)
733 struct wlsc_compositor *compositor = output->compositor;
735 pixman_region32_union(&compositor->damage,
736 &compositor->damage, &output->region);
737 wlsc_compositor_schedule_repaint(compositor);
741 fade_frame(struct wlsc_animation *animation,
742 struct wlsc_output *output, uint32_t msecs)
744 struct wlsc_compositor *compositor =
745 container_of(animation,
746 struct wlsc_compositor, fade.animation);
748 wlsc_spring_update(&compositor->fade.spring, msecs);
749 if (wlsc_spring_done(&compositor->fade.spring)) {
750 if (compositor->fade.spring.current > 0.999) {
751 compositor->state = WLSC_COMPOSITOR_SLEEPING;
752 compositor->shell->lock(compositor->shell);
754 compositor->fade.spring.current =
755 compositor->fade.spring.target;
756 wl_list_remove(&animation->link);
757 wl_list_init(&animation->link);
760 wlsc_output_damage(output);
764 fade_output(struct wlsc_output *output,
765 GLfloat tint, pixman_region32_t *region)
767 struct wlsc_compositor *compositor = output->compositor;
768 struct wlsc_surface surface;
769 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
771 surface.compositor = compositor;
772 surface.x = output->x;
773 surface.y = output->y;
774 surface.pitch = output->current->width;
775 surface.width = output->current->width;
776 surface.height = output->current->height;
777 surface.texture = GL_NONE;
778 surface.transform = NULL;
781 surface.visual = WLSC_PREMUL_ARGB_VISUAL;
783 surface.visual = WLSC_RGB_VISUAL;
785 glUseProgram(compositor->solid_shader.program);
786 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
787 1, GL_FALSE, output->matrix.d);
788 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
789 wlsc_surface_draw(&surface, output, region);
793 wlsc_output_set_cursor(struct wlsc_output *output,
794 struct wl_input_device *dev, int force_sw)
796 struct wlsc_compositor *ec = output->compositor;
797 struct wlsc_input_device *device = (struct wlsc_input_device *) dev;
798 pixman_region32_t cursor_region;
799 int use_hardware_cursor = 1, prior_was_hardware;
801 pixman_region32_init_rect(&cursor_region,
802 device->sprite->x, device->sprite->y,
803 device->sprite->width,
804 device->sprite->height);
806 pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
808 if (!pixman_region32_not_empty(&cursor_region)) {
809 output->set_hardware_cursor(output, NULL);
813 prior_was_hardware = wl_list_empty(&device->sprite->link);
814 if (force_sw || output->set_hardware_cursor(output, device) < 0) {
815 if (prior_was_hardware) {
816 wlsc_surface_damage(device->sprite);
817 output->set_hardware_cursor(output, NULL);
819 use_hardware_cursor = 0;
820 } else if (!prior_was_hardware) {
821 wlsc_surface_damage_below(device->sprite);
824 /* Remove always to be on top. */
825 wl_list_remove(&device->sprite->link);
826 if (!use_hardware_cursor && ec->focus)
827 wl_list_insert(&ec->surface_list, &device->sprite->link);
829 wl_list_init(&device->sprite->link);
832 pixman_region32_fini(&cursor_region);
836 wlsc_output_repaint(struct wlsc_output *output)
838 struct wlsc_compositor *ec = output->compositor;
839 struct wlsc_surface *es;
840 pixman_region32_t opaque, new_damage, total_damage, repaint;
842 output->prepare_render(output);
844 glViewport(0, 0, output->current->width, output->current->height);
846 glUseProgram(ec->texture_shader.program);
847 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
848 1, GL_FALSE, output->matrix.d);
849 glUniform1i(ec->texture_shader.tex_uniform, 0);
851 wlsc_output_set_cursor(output, ec->input_device,
852 ec->fade.spring.current >= 0.001);
854 pixman_region32_init(&new_damage);
855 pixman_region32_copy(&new_damage, &ec->damage);
856 pixman_region32_init(&opaque);
858 wl_list_for_each(es, &ec->surface_list, link) {
859 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
860 pixman_region32_union(&new_damage, &new_damage, &es->damage);
861 pixman_region32_union(&opaque, &opaque, &es->opaque);
864 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
866 pixman_region32_init(&total_damage);
867 pixman_region32_union(&total_damage, &new_damage,
868 &output->previous_damage);
869 pixman_region32_intersect(&output->previous_damage,
870 &new_damage, &output->region);
872 pixman_region32_fini(&opaque);
873 pixman_region32_fini(&new_damage);
875 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
877 if (es->visual == WLSC_RGB_VISUAL &&
878 output->prepare_scanout_surface(output, es) == 0) {
879 /* We're drawing nothing now,
880 * draw the damaged regions later. */
881 pixman_region32_union(&ec->damage, &ec->damage, &total_damage);
883 output->scanout_buffer = es->buffer;
884 output->scanout_buffer->busy_count++;
886 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
887 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
888 &output->scanout_buffer_destroy_listener.link);
893 if (es->fullscreen_output == output) {
894 if (es->width < output->current->width ||
895 es->height < output->current->height)
896 glClear(GL_COLOR_BUFFER_BIT);
897 wlsc_surface_draw(es, output, &total_damage);
899 wl_list_for_each(es, &ec->surface_list, link) {
900 pixman_region32_copy(&es->damage, &total_damage);
901 pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
904 wl_list_for_each_reverse(es, &ec->surface_list, link) {
905 pixman_region32_init(&repaint);
906 pixman_region32_intersect(&repaint, &output->region,
908 wlsc_surface_draw(es, output, &repaint);
909 pixman_region32_subtract(&es->damage,
910 &es->damage, &output->region);
914 if (ec->fade.spring.current > 0.001)
915 fade_output(output, ec->fade.spring.current, &total_damage);
918 struct wlsc_frame_callback {
919 struct wl_resource resource;
924 repaint(void *data, int msecs)
926 struct wlsc_output *output = data;
927 struct wlsc_compositor *compositor = output->compositor;
928 struct wlsc_animation *animation, *next;
929 struct wlsc_frame_callback *cb, *cnext;
931 wlsc_output_repaint(output);
932 output->repaint_needed = 0;
933 output->repaint_scheduled = 1;
934 output->present(output);
936 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
937 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
938 wl_resource_destroy(&cb->resource, 0);
941 wl_list_for_each_safe(animation, next,
942 &compositor->animation_list, link)
943 animation->frame(animation, output, msecs);
947 idle_repaint(void *data)
949 repaint(data, wlsc_compositor_get_time());
953 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
955 wlsc_buffer_post_release(output->scanout_buffer);
956 output->scanout_buffer = NULL;
957 output->repaint_scheduled = 0;
959 if (output->repaint_needed)
960 repaint(output, msecs);
964 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
966 struct wlsc_output *output;
967 struct wl_event_loop *loop;
969 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
972 loop = wl_display_get_event_loop(compositor->wl_display);
973 wl_list_for_each(output, &compositor->output_list, link) {
974 output->repaint_needed = 1;
975 if (output->repaint_scheduled)
978 wl_event_loop_add_idle(loop, idle_repaint, output);
979 output->repaint_scheduled = 1;
984 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
988 done = wlsc_spring_done(&compositor->fade.spring);
989 compositor->fade.spring.target = tint;
990 if (wlsc_spring_done(&compositor->fade.spring))
994 compositor->fade.spring.timestamp =
995 wlsc_compositor_get_time();
997 wlsc_compositor_damage_all(compositor);
998 if (wl_list_empty(&compositor->fade.animation.link))
999 wl_list_insert(compositor->animation_list.prev,
1000 &compositor->fade.animation.link);
1004 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1006 wl_resource_destroy(resource, wlsc_compositor_get_time());
1010 wlsc_surface_assign_output(struct wlsc_surface *es)
1012 struct wlsc_compositor *ec = es->compositor;
1013 struct wlsc_output *output;
1014 pixman_region32_t region;
1019 wl_list_for_each(output, &ec->output_list, link) {
1020 pixman_region32_init_rect(®ion,
1021 es->x, es->y, es->width, es->height);
1022 pixman_region32_intersect(®ion, ®ion, &output->region);
1024 e = pixman_region32_extents(®ion);
1025 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1028 es->output = output;
1035 surface_attach(struct wl_client *client,
1036 struct wl_resource *resource,
1037 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1039 struct wlsc_surface *es = resource->data;
1040 struct wl_buffer *buffer = buffer_resource->data;
1042 buffer->busy_count++;
1043 wlsc_buffer_post_release(es->buffer);
1045 es->buffer = buffer;
1046 wl_list_remove(&es->buffer_destroy_listener.link);
1047 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1048 &es->buffer_destroy_listener.link);
1050 if (es->visual == WLSC_NONE_VISUAL)
1051 wl_list_insert(&es->compositor->surface_list, &es->link);
1053 if (x != 0 || y != 0 ||
1054 es->width != buffer->width || es->height != buffer->height)
1055 wlsc_surface_configure(es, es->x + x, es->y + y,
1056 buffer->width, buffer->height);
1058 wlsc_buffer_attach(buffer, &es->surface);
1060 es->compositor->shell->attach(es->compositor->shell, es);
1064 surface_damage(struct wl_client *client,
1065 struct wl_resource *resource,
1066 int32_t x, int32_t y, int32_t width, int32_t height)
1068 struct wlsc_surface *es = resource->data;
1070 wlsc_surface_damage_rectangle(es, x, y, width, height);
1074 destroy_frame_callback(struct wl_resource *resource)
1076 struct wlsc_frame_callback *cb = resource->data;
1078 wl_list_remove(&cb->link);
1083 surface_frame(struct wl_client *client,
1084 struct wl_resource *resource, uint32_t callback)
1086 struct wlsc_frame_callback *cb;
1087 struct wlsc_surface *es = resource->data;
1089 cb = malloc(sizeof *cb);
1091 wl_resource_post_no_memory(resource);
1095 cb->resource.object.interface = &wl_callback_interface;
1096 cb->resource.object.id = callback;
1097 cb->resource.destroy = destroy_frame_callback;
1098 cb->resource.client = client;
1099 cb->resource.data = cb;
1101 wl_client_add_resource(client, &cb->resource);
1104 wl_list_insert(es->output->frame_callback_list.prev,
1107 wl_list_init(&cb->link);
1108 wl_resource_destroy(&cb->resource, 0);
1112 const static struct wl_surface_interface surface_interface = {
1120 wlsc_input_device_attach(struct wlsc_input_device *device,
1121 int x, int y, int width, int height)
1123 wlsc_surface_damage_below(device->sprite);
1125 device->hotspot_x = x;
1126 device->hotspot_y = y;
1128 device->sprite->x = device->input_device.x - device->hotspot_x;
1129 device->sprite->y = device->input_device.y - device->hotspot_y;
1130 device->sprite->width = width;
1131 device->sprite->height = height;
1133 wlsc_surface_damage(device->sprite);
1137 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1138 struct wl_buffer *buffer, int x, int y)
1140 wlsc_buffer_attach(buffer, &device->sprite->surface);
1141 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1145 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1146 struct wlsc_sprite *sprite, int x, int y)
1148 wlsc_sprite_attach(sprite, &device->sprite->surface);
1149 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1153 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1154 enum wlsc_pointer_type type)
1156 struct wlsc_compositor *compositor =
1157 (struct wlsc_compositor *) device->input_device.compositor;
1159 wlsc_input_device_attach_sprite(device,
1160 compositor->pointer_sprites[type],
1161 pointer_images[type].hotspot_x,
1162 pointer_images[type].hotspot_y);
1166 compositor_create_surface(struct wl_client *client,
1167 struct wl_resource *resource, uint32_t id)
1169 struct wlsc_compositor *ec = resource->data;
1170 struct wlsc_surface *surface;
1172 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1173 if (surface == NULL) {
1174 wl_resource_post_no_memory(resource);
1178 surface->surface.resource.destroy = destroy_surface;
1180 surface->surface.resource.object.id = id;
1181 surface->surface.resource.object.interface = &wl_surface_interface;
1182 surface->surface.resource.object.implementation =
1183 (void (**)(void)) &surface_interface;
1184 surface->surface.resource.data = surface;
1186 wl_client_add_resource(client, &surface->surface.resource);
1189 const static struct wl_compositor_interface compositor_interface = {
1190 compositor_create_surface,
1194 wlsc_surface_transform(struct wlsc_surface *surface,
1195 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1197 *sx = x - surface->x;
1198 *sy = y - surface->y;
1201 WL_EXPORT struct wlsc_surface *
1202 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1204 struct wlsc_compositor *ec =
1205 (struct wlsc_compositor *) device->compositor;
1206 struct wlsc_surface *es;
1208 wl_list_for_each(es, &ec->surface_list, link) {
1209 if (es->surface.resource.client == NULL)
1211 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1212 if (0 <= *sx && *sx < es->width &&
1213 0 <= *sy && *sy < es->height)
1222 motion_grab_motion(struct wl_grab *grab,
1223 uint32_t time, int32_t x, int32_t y)
1225 struct wlsc_input_device *device =
1226 (struct wlsc_input_device *) grab->input_device;
1227 struct wlsc_surface *es =
1228 (struct wlsc_surface *) device->input_device.pointer_focus;
1230 struct wl_resource *resource;
1232 resource = grab->input_device->pointer_focus_resource;
1234 wlsc_surface_transform(es, x, y, &sx, &sy);
1235 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1236 time, x, y, sx, sy);
1241 motion_grab_button(struct wl_grab *grab,
1242 uint32_t time, int32_t button, int32_t state)
1244 struct wl_resource *resource;
1246 resource = grab->input_device->pointer_focus_resource;
1248 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1249 time, button, state);
1253 motion_grab_end(struct wl_grab *grab, uint32_t time)
1257 static const struct wl_grab_interface motion_grab_interface = {
1264 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1266 if (compositor->idle_inhibit)
1269 wlsc_compositor_fade(compositor, 0.0);
1270 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1272 wl_event_source_timer_update(compositor->idle_source,
1273 option_idle_time * 1000);
1277 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1279 wlsc_compositor_wake(compositor);
1280 compositor->idle_inhibit++;
1284 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1286 compositor->idle_inhibit--;
1287 wlsc_compositor_wake(compositor);
1291 idle_handler(void *data)
1293 struct wlsc_compositor *compositor = data;
1295 if (compositor->idle_inhibit)
1298 wlsc_compositor_fade(compositor, 1.0);
1304 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1306 struct wlsc_surface *es;
1307 struct wlsc_compositor *ec =
1308 (struct wlsc_compositor *) device->compositor;
1309 struct wlsc_output *output;
1310 const struct wl_grab_interface *interface;
1311 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1313 int x_valid = 0, y_valid = 0;
1314 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1316 wlsc_compositor_wake(ec);
1318 wl_list_for_each(output, &ec->output_list, link) {
1319 if (output->x <= x && x <= output->x + output->current->width)
1322 if (output->y <= y && y <= output->y + output->current->height)
1325 /* FIXME: calculate this only on output addition/deletion */
1326 if (output->x < min_x)
1328 if (output->y < min_y)
1331 if (output->x + output->current->width > max_x)
1332 max_x = output->x + output->current->width;
1333 if (output->y + output->current->height > max_y)
1334 max_y = output->y + output->current->height;
1340 else if (x >= max_x)
1346 else if (y >= max_y)
1354 interface = device->grab->interface;
1355 interface->motion(device->grab, time, x, y);
1357 es = pick_surface(device, &sx, &sy);
1358 wl_input_device_set_pointer_focus(device,
1360 time, x, y, sx, sy);
1361 if (device->pointer_focus_resource)
1362 wl_resource_post_event(device->pointer_focus_resource,
1363 WL_INPUT_DEVICE_MOTION,
1364 time, x, y, sx, sy);
1367 wlsc_surface_damage_below(wd->sprite);
1369 wd->sprite->x = device->x - wd->hotspot_x;
1370 wd->sprite->y = device->y - wd->hotspot_y;
1372 wlsc_surface_damage(wd->sprite);
1376 wlsc_surface_activate(struct wlsc_surface *surface,
1377 struct wlsc_input_device *device, uint32_t time)
1379 struct wlsc_shell *shell = surface->compositor->shell;
1381 wlsc_surface_raise(surface);
1382 if (device->selection)
1383 shell->set_selection_focus(shell,
1385 &surface->surface, time);
1387 wl_input_device_set_keyboard_focus(&device->input_device,
1392 struct wlsc_binding {
1396 wlsc_binding_handler_t handler;
1398 struct wl_list link;
1402 notify_button(struct wl_input_device *device,
1403 uint32_t time, int32_t button, int32_t state)
1405 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1406 struct wlsc_compositor *compositor =
1407 (struct wlsc_compositor *) device->compositor;
1408 struct wlsc_binding *b;
1409 struct wlsc_surface *surface =
1410 (struct wlsc_surface *) device->pointer_focus;
1414 wlsc_compositor_idle_inhibit(compositor);
1416 wlsc_compositor_idle_release(compositor);
1418 if (state && surface && device->grab == NULL) {
1419 wlsc_surface_activate(surface, wd, time);
1420 wl_input_device_start_grab(device,
1421 &device->motion_grab,
1425 wl_list_for_each(b, &compositor->binding_list, link) {
1426 if (b->button == button &&
1427 b->modifier == wd->modifier_state && state) {
1428 b->handler(&wd->input_device,
1429 time, 0, button, state, b->data);
1435 device->grab->interface->button(device->grab, time,
1438 if (!state && device->grab && device->grab_button == button) {
1439 wl_input_device_end_grab(device, time);
1440 surface = pick_surface(device, &sx, &sy);
1441 wl_input_device_set_pointer_focus(device, &surface->surface,
1442 time, device->x, device->y,
1448 terminate_binding(struct wl_input_device *device, uint32_t time,
1449 uint32_t key, uint32_t button, uint32_t state, void *data)
1451 struct wlsc_compositor *compositor = data;
1454 wl_display_terminate(compositor->wl_display);
1457 WL_EXPORT struct wlsc_binding *
1458 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1459 uint32_t key, uint32_t button, uint32_t modifier,
1460 wlsc_binding_handler_t handler, void *data)
1462 struct wlsc_binding *binding;
1464 binding = malloc(sizeof *binding);
1465 if (binding == NULL)
1469 binding->button = button;
1470 binding->modifier = modifier;
1471 binding->handler = handler;
1472 binding->data = data;
1473 wl_list_insert(compositor->binding_list.prev, &binding->link);
1479 wlsc_binding_destroy(struct wlsc_binding *binding)
1481 wl_list_remove(&binding->link);
1486 update_modifier_state(struct wlsc_input_device *device,
1487 uint32_t key, uint32_t state)
1494 modifier = MODIFIER_CTRL;
1499 modifier = MODIFIER_ALT;
1504 modifier = MODIFIER_SUPER;
1513 device->modifier_state |= modifier;
1515 device->modifier_state &= ~modifier;
1519 notify_key(struct wl_input_device *device,
1520 uint32_t time, uint32_t key, uint32_t state)
1522 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1523 struct wlsc_compositor *compositor =
1524 (struct wlsc_compositor *) device->compositor;
1526 struct wlsc_binding *b;
1529 wlsc_compositor_idle_inhibit(compositor);
1531 wlsc_compositor_idle_release(compositor);
1533 wl_list_for_each(b, &compositor->binding_list, link) {
1534 if (b->key == key &&
1535 b->modifier == wd->modifier_state) {
1536 b->handler(&wd->input_device,
1537 time, key, 0, state, b->data);
1542 update_modifier_state(wd, key, state);
1543 end = device->keys.data + device->keys.size;
1544 for (k = device->keys.data; k < end; k++) {
1548 device->keys.size = (void *) end - device->keys.data;
1550 k = wl_array_add(&device->keys, sizeof *k);
1554 if (device->keyboard_focus_resource)
1555 wl_resource_post_event(device->keyboard_focus_resource,
1556 WL_INPUT_DEVICE_KEY, time, key, state);
1560 notify_pointer_focus(struct wl_input_device *device,
1561 uint32_t time, struct wlsc_output *output,
1562 int32_t x, int32_t y)
1564 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1565 struct wlsc_compositor *compositor =
1566 (struct wlsc_compositor *) device->compositor;
1567 struct wlsc_surface *es;
1573 es = pick_surface(device, &sx, &sy);
1574 wl_input_device_set_pointer_focus(device,
1576 time, x, y, sx, sy);
1578 compositor->focus = 1;
1580 wd->sprite->x = device->x - wd->hotspot_x;
1581 wd->sprite->y = device->y - wd->hotspot_y;
1583 wl_input_device_set_pointer_focus(device, NULL,
1585 compositor->focus = 0;
1588 wlsc_surface_damage(wd->sprite);
1592 notify_keyboard_focus(struct wl_input_device *device,
1593 uint32_t time, struct wlsc_output *output,
1594 struct wl_array *keys)
1596 struct wlsc_input_device *wd =
1597 (struct wlsc_input_device *) device;
1598 struct wlsc_compositor *compositor =
1599 (struct wlsc_compositor *) device->compositor;
1600 struct wlsc_surface *es;
1603 if (!wl_list_empty(&compositor->surface_list))
1604 es = container_of(compositor->surface_list.next,
1605 struct wlsc_surface, link);
1610 wl_array_copy(&wd->input_device.keys, keys);
1611 wd->modifier_state = 0;
1612 end = device->keys.data + device->keys.size;
1613 for (k = device->keys.data; k < end; k++) {
1614 wlsc_compositor_idle_inhibit(compositor);
1615 update_modifier_state(wd, *k, 1);
1618 if (es->surface.resource.client)
1619 wl_input_device_set_keyboard_focus(&wd->input_device,
1620 &es->surface, time);
1622 end = device->keys.data + device->keys.size;
1623 for (k = device->keys.data; k < end; k++)
1624 wlsc_compositor_idle_release(compositor);
1626 wd->modifier_state = 0;
1627 wl_input_device_set_keyboard_focus(&wd->input_device,
1634 input_device_attach(struct wl_client *client,
1635 struct wl_resource *resource,
1637 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1639 struct wlsc_input_device *device = resource->data;
1640 struct wl_buffer *buffer;
1642 if (time < device->input_device.pointer_focus_time)
1644 if (device->input_device.pointer_focus == NULL)
1646 if (device->input_device.pointer_focus->resource.client != client)
1649 if (buffer_resource) {
1650 buffer = buffer_resource->data;
1651 wlsc_input_device_attach_buffer(device, buffer, x, y);
1653 wlsc_input_device_set_pointer_image(device,
1654 WLSC_POINTER_LEFT_PTR);
1659 const static struct wl_input_device_interface input_device_interface = {
1660 input_device_attach,
1664 bind_input_device(struct wl_client *client,
1665 void *data, uint32_t version, uint32_t id)
1667 struct wl_input_device *device = data;
1668 struct wl_resource *resource;
1670 resource = wl_client_add_object(client, &wl_input_device_interface,
1671 &input_device_interface, id, data);
1672 wl_list_insert(&device->resource_list, &resource->link);
1676 wlsc_input_device_init(struct wlsc_input_device *device,
1677 struct wlsc_compositor *ec)
1679 wl_input_device_init(&device->input_device, &ec->compositor);
1681 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1682 device, bind_input_device);
1684 device->sprite = wlsc_surface_create(ec,
1685 device->input_device.x,
1686 device->input_device.y, 32, 32);
1687 wl_list_insert(&ec->surface_list, &device->sprite->link);
1689 device->hotspot_x = 16;
1690 device->hotspot_y = 16;
1691 device->modifier_state = 0;
1693 device->input_device.motion_grab.interface = &motion_grab_interface;
1695 wl_list_insert(ec->input_device_list.prev, &device->link);
1697 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1701 bind_output(struct wl_client *client,
1702 void *data, uint32_t version, uint32_t id)
1704 struct wlsc_output *output = data;
1705 struct wlsc_mode *mode;
1706 struct wl_resource *resource;
1708 resource = wl_client_add_object(client,
1709 &wl_output_interface, NULL, id, data);
1711 wl_resource_post_event(resource,
1718 output->make, output->model);
1720 wl_list_for_each (mode, &output->mode_list, link) {
1721 wl_resource_post_event(resource,
1730 static const char vertex_shader[] =
1731 "uniform mat4 proj;\n"
1732 "attribute vec2 position;\n"
1733 "attribute vec2 texcoord;\n"
1734 "varying vec2 v_texcoord;\n"
1737 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1738 " v_texcoord = texcoord;\n"
1741 static const char texture_fragment_shader[] =
1742 "precision mediump float;\n"
1743 "varying vec2 v_texcoord;\n"
1744 "uniform sampler2D tex;\n"
1747 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1750 static const char solid_fragment_shader[] =
1751 "precision mediump float;\n"
1752 "uniform vec4 color;\n"
1755 " gl_FragColor = color\n;"
1759 compile_shader(GLenum type, const char *source)
1765 s = glCreateShader(type);
1766 glShaderSource(s, 1, &source, NULL);
1768 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1770 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1771 fprintf(stderr, "shader info: %s\n", msg);
1779 wlsc_shader_init(struct wlsc_shader *shader,
1780 const char *vertex_source, const char *fragment_source)
1785 shader->vertex_shader =
1786 compile_shader(GL_VERTEX_SHADER, vertex_source);
1787 shader->fragment_shader =
1788 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1790 shader->program = glCreateProgram();
1791 glAttachShader(shader->program, shader->vertex_shader);
1792 glAttachShader(shader->program, shader->fragment_shader);
1793 glBindAttribLocation(shader->program, 0, "position");
1794 glBindAttribLocation(shader->program, 1, "texcoord");
1796 glLinkProgram(shader->program);
1797 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1799 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1800 fprintf(stderr, "link info: %s\n", msg);
1804 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1805 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1811 init_solid_shader(struct wlsc_shader *shader,
1812 GLuint vertex_shader, const char *fragment_source)
1817 shader->vertex_shader = vertex_shader;
1818 shader->fragment_shader =
1819 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1821 shader->program = glCreateProgram();
1822 glAttachShader(shader->program, shader->vertex_shader);
1823 glAttachShader(shader->program, shader->fragment_shader);
1824 glBindAttribLocation(shader->program, 0, "position");
1825 glBindAttribLocation(shader->program, 1, "texcoord");
1827 glLinkProgram(shader->program);
1828 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1830 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1831 fprintf(stderr, "link info: %s\n", msg);
1835 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1836 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1842 wlsc_output_destroy(struct wlsc_output *output)
1844 pixman_region32_fini(&output->region);
1845 pixman_region32_fini(&output->previous_damage);
1846 destroy_surface(&output->background->surface.resource);
1850 wlsc_output_move(struct wlsc_output *output, int x, int y)
1852 struct wlsc_compositor *c = output->compositor;
1858 if (output->background) {
1859 output->background->x = x;
1860 output->background->y = y;
1863 pixman_region32_init(&output->previous_damage);
1864 pixman_region32_init_rect(&output->region, x, y,
1865 output->current->width,
1866 output->current->height);
1868 wlsc_matrix_init(&output->matrix);
1869 wlsc_matrix_translate(&output->matrix,
1870 -output->x - output->current->width / 2.0,
1871 -output->y - output->current->height / 2.0, 0);
1873 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1874 wlsc_matrix_scale(&output->matrix,
1875 2.0 / output->current->width,
1876 flip * 2.0 / output->current->height, 1);
1878 pixman_region32_union(&c->damage, &c->damage, &output->region);
1882 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1883 int x, int y, int width, int height, uint32_t flags)
1885 output->compositor = c;
1888 output->mm_width = width;
1889 output->mm_height = height;
1891 output->background =
1892 background_create(output, option_background);
1894 if (output->background != NULL)
1895 wl_list_insert(c->surface_list.prev,
1896 &output->background->link);
1898 output->flags = flags;
1899 wlsc_output_move(output, x, y);
1901 output->scanout_buffer_destroy_listener.func =
1902 output_handle_scanout_buffer_destroy;
1903 wl_list_init(&output->scanout_buffer_destroy_listener.link);
1904 wl_list_init(&output->frame_callback_list);
1906 output->resource.object.interface = &wl_output_interface;
1907 wl_display_add_global(c->wl_display,
1908 &wl_output_interface, output, bind_output);
1912 shm_buffer_created(struct wl_buffer *buffer)
1914 struct wl_list *surfaces_attached_to;
1916 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1917 if (!surfaces_attached_to) {
1918 buffer->user_data = NULL;
1922 wl_list_init(surfaces_attached_to);
1924 buffer->user_data = surfaces_attached_to;
1928 shm_buffer_damaged(struct wl_buffer *buffer,
1929 int32_t x, int32_t y, int32_t width, int32_t height)
1931 struct wl_list *surfaces_attached_to = buffer->user_data;
1932 struct wlsc_surface *es;
1933 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1935 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1936 glBindTexture(GL_TEXTURE_2D, es->texture);
1937 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1938 tex_width, buffer->height, 0,
1939 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1940 wl_shm_buffer_get_data(buffer));
1941 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1942 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1947 shm_buffer_destroyed(struct wl_buffer *buffer)
1949 struct wl_list *surfaces_attached_to = buffer->user_data;
1950 struct wlsc_surface *es, *next;
1952 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1953 wl_list_remove(&es->buffer_link);
1954 wl_list_init(&es->buffer_link);
1957 free(surfaces_attached_to);
1960 const static struct wl_shm_callbacks shm_callbacks = {
1963 shm_buffer_destroyed
1967 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1969 struct wl_event_loop *loop;
1970 const char *extensions;
1972 ec->wl_display = display;
1974 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1976 ec->shm = wl_shm_init(display, &shm_callbacks);
1978 ec->image_target_texture_2d =
1979 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1980 ec->image_target_renderbuffer_storage = (void *)
1981 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1982 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1983 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1985 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1986 ec->unbind_display =
1987 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1989 extensions = (const char *) glGetString(GL_EXTENSIONS);
1990 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1992 "GL_EXT_texture_format_BGRA8888 not available\n");
1997 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1998 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1999 ec->has_bind_display = 1;
2000 if (ec->has_bind_display)
2001 ec->bind_display(ec->display, ec->wl_display);
2003 wl_list_init(&ec->surface_list);
2004 wl_list_init(&ec->input_device_list);
2005 wl_list_init(&ec->output_list);
2006 wl_list_init(&ec->binding_list);
2007 wl_list_init(&ec->animation_list);
2008 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
2009 ec->fade.animation.frame = fade_frame;
2010 wl_list_init(&ec->fade.animation.link);
2012 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
2013 MODIFIER_CTRL | MODIFIER_ALT,
2014 terminate_binding, ec);
2016 create_pointer_images(ec);
2018 screenshooter_create(ec);
2020 glActiveTexture(GL_TEXTURE0);
2022 if (wlsc_shader_init(&ec->texture_shader,
2023 vertex_shader, texture_fragment_shader) < 0)
2025 if (init_solid_shader(&ec->solid_shader,
2026 ec->texture_shader.vertex_shader,
2027 solid_fragment_shader) < 0)
2030 loop = wl_display_get_event_loop(ec->wl_display);
2031 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2032 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
2034 pixman_region32_init(&ec->damage);
2035 wlsc_compositor_schedule_repaint(ec);
2041 wlsc_compositor_shutdown(struct wlsc_compositor *ec)
2043 struct wlsc_output *output;
2045 /* Destroy all outputs associated with this compositor */
2046 wl_list_for_each(output, &ec->output_list, link)
2047 output->destroy(output);
2050 static int on_term_signal(int signal_number, void *data)
2052 struct wl_display *display = data;
2054 fprintf(stderr, "caught signal %d\n", signal_number);
2055 wl_display_terminate(display);
2061 load_module(const char *name, const char *entrypoint, void **handle)
2063 char path[PATH_MAX];
2064 void *module, *init;
2067 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2069 snprintf(path, sizeof path, "%s", name);
2071 module = dlopen(path, RTLD_LAZY);
2074 "failed to load module: %s\n", dlerror());
2078 init = dlsym(module, entrypoint);
2081 "failed to lookup init function: %s\n", dlerror());
2088 int main(int argc, char *argv[])
2090 struct wl_display *display;
2091 struct wlsc_compositor *ec;
2092 struct wl_event_loop *loop;
2094 void *shell_module, *backend_module;
2095 int (*shell_init)(struct wlsc_compositor *ec);
2096 struct wlsc_compositor
2097 *(*backend_init)(struct wl_display *display, char *options);
2098 char *backend = NULL;
2099 char *backend_options = "";
2103 static const char opts[] = "B:b:o:S:i:s:x";
2104 static const struct option longopts[ ] = {
2105 { "backend", 1, NULL, 'B' },
2106 { "backend-options", 1, NULL, 'o' },
2107 { "background", 1, NULL, 'b' },
2108 { "socket", 1, NULL, 'S' },
2109 { "idle-time", 1, NULL, 'i' },
2110 { "shell", 1, NULL, 's' },
2111 { "xserver", 0, NULL, 'x' },
2115 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2118 option_background = optarg;
2124 backend_options = optarg;
2127 option_socket_name = optarg;
2130 option_idle_time = strtol(optarg, &p, 0);
2133 "invalid idle time option: %s\n",
2147 display = wl_display_create();
2149 loop = wl_display_get_event_loop(display);
2150 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2151 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2152 wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
2154 wl_list_init(&child_process_list);
2155 wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
2158 if (getenv("WAYLAND_DISPLAY"))
2159 backend = "wayland-backend.so";
2160 else if (getenv("DISPLAY"))
2161 backend = "x11-backend.so";
2162 else if (getenv("OPENWFD"))
2163 backend = "openwfd-backend.so";
2165 backend = "drm-backend.so";
2169 shell = "desktop-shell.so";
2171 backend_init = load_module(backend, "backend_init", &backend_module);
2175 shell_init = load_module(shell, "shell_init", &shell_module);
2179 ec = backend_init(display, backend_options);
2181 fprintf(stderr, "failed to create compositor\n");
2185 if (shell_init(ec) < 0)
2189 wlsc_xserver_init(ec);
2191 if (wl_display_add_socket(display, option_socket_name)) {
2192 fprintf(stderr, "failed to add socket: %m\n");
2196 wl_display_run(display);
2199 wlsc_xserver_destroy(ec);
2201 if (ec->has_bind_display)
2202 ec->unbind_display(ec->display, display);
2203 wl_display_destroy(display);