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>
33 #include <linux/input.h>
38 #include "wayland-server.h"
39 #include "compositor.h"
41 /* The plan here is to generate a random anonymous socket name and
42 * advertise that through a service on the session dbus.
44 static const char *option_socket_name = NULL;
45 static const char *option_background = "background.jpg";
46 static int option_idle_time = 300;
49 wlsc_matrix_init(struct wlsc_matrix *matrix)
51 static const struct wlsc_matrix identity = {
52 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
55 memcpy(matrix, &identity, sizeof identity);
59 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
61 struct wlsc_matrix tmp;
62 const GLfloat *row, *column;
66 for (i = 0; i < 16; i++) {
69 row = m->d + d.quot * 4;
70 column = n->d + d.rem;
71 for (j = 0; j < 4; j++)
72 tmp.d[i] += row[j] * column[j * 4];
74 memcpy(m, &tmp, sizeof tmp);
78 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
80 struct wlsc_matrix translate = {
81 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
84 wlsc_matrix_multiply(matrix, &translate);
88 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
90 struct wlsc_matrix scale = {
91 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
94 wlsc_matrix_multiply(matrix, &scale);
98 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
101 struct wlsc_vector t;
103 for (i = 0; i < 4; i++) {
105 for (j = 0; j < 4; j++)
106 t.f[i] += v->f[j] * matrix->d[i + j * 4];
113 wlsc_spring_init(struct wlsc_spring *spring,
114 double k, double current, double target)
117 spring->friction = 100.0;
118 spring->current = current;
119 spring->previous = current;
120 spring->target = target;
124 wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec)
126 double force, v, current, step;
128 step = (msec - spring->timestamp) / 500.0;
129 spring->timestamp = msec;
131 current = spring->current;
132 v = current - spring->previous;
133 force = spring->k * (spring->target - current) / 10.0 +
134 (spring->previous - current) - v * spring->friction;
137 current + (current - spring->previous) + force * step * step;
138 spring->previous = current;
140 if (spring->current >= 1.0) {
141 #ifdef TWEENER_BOUNCE
142 spring->current = 2.0 - spring->current;
143 spring->previous = 2.0 - spring->previous;
145 spring->current = 1.0;
146 spring->previous = 1.0;
150 if (spring->current <= 0.0) {
151 spring->current = 0.0;
152 spring->previous = 0.0;
157 wlsc_spring_done(struct wlsc_spring *spring)
159 return fabs(spring->previous - spring->target) < 0.0002 &&
160 fabs(spring->current - spring->target) < 0.0002;
163 WL_EXPORT struct wlsc_surface *
164 wlsc_surface_create(struct wlsc_compositor *compositor,
165 int32_t x, int32_t y, int32_t width, int32_t height)
167 struct wlsc_surface *surface;
169 surface = malloc(sizeof *surface);
173 wl_list_init(&surface->surface.destroy_listener_list);
174 wl_list_init(&surface->link);
175 wl_list_init(&surface->buffer_link);
176 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
178 glGenTextures(1, &surface->texture);
179 glBindTexture(GL_TEXTURE_2D, surface->texture);
180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
185 surface->compositor = compositor;
186 surface->visual = NULL;
187 surface->image = EGL_NO_IMAGE_KHR;
188 surface->saved_texture = 0;
191 surface->width = width;
192 surface->height = height;
194 surface->transform = NULL;
200 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
201 int32_t x, int32_t y,
202 int32_t width, int32_t height)
204 struct wlsc_compositor *compositor = surface->compositor;
206 pixman_region32_union_rect(&compositor->damage_region,
207 &compositor->damage_region,
208 surface->x + x, surface->y + y,
210 wlsc_compositor_schedule_repaint(compositor);
214 wlsc_surface_damage(struct wlsc_surface *surface)
216 wlsc_surface_damage_rectangle(surface, 0, 0,
217 surface->width, surface->height);
221 wlsc_compositor_get_time(void)
225 gettimeofday(&tv, NULL);
227 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
231 destroy_surface(struct wl_resource *resource, struct wl_client *client)
233 struct wlsc_surface *surface =
234 container_of(resource, struct wlsc_surface, surface.resource);
235 struct wl_listener *l, *next;
236 struct wlsc_compositor *compositor = surface->compositor;
239 wlsc_surface_damage(surface);
241 wl_list_remove(&surface->link);
242 if (surface->saved_texture == 0)
243 glDeleteTextures(1, &surface->texture);
245 glDeleteTextures(1, &surface->saved_texture);
248 if (surface->image != EGL_NO_IMAGE_KHR)
249 compositor->destroy_image(compositor->display,
252 wl_list_remove(&surface->buffer_link);
254 time = wlsc_compositor_get_time();
255 wl_list_for_each_safe(l, next,
256 &surface->surface.destroy_listener_list, link)
257 l->func(l, &surface->surface, time);
263 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
265 struct wlsc_surface *es = (struct wlsc_surface *) surface;
266 struct wlsc_compositor *ec = es->compositor;
267 struct wl_list *surfaces_attached_to;
269 if (es->saved_texture != 0)
270 es->texture = es->saved_texture;
272 glBindTexture(GL_TEXTURE_2D, es->texture);
274 if (wl_buffer_is_shm(buffer)) {
275 /* Unbind any EGLImage texture that may be bound, so we don't
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
278 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
279 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
281 es->pitch, buffer->height, 0,
282 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
283 wl_shm_buffer_get_data(buffer));
284 es->visual = buffer->visual;
286 surfaces_attached_to = buffer->user_data;
288 wl_list_remove(&es->buffer_link);
289 wl_list_insert(surfaces_attached_to, &es->buffer_link);
291 es->image = ec->create_image(ec->display, NULL,
292 EGL_WAYLAND_BUFFER_WL,
295 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
296 es->visual = buffer->visual;
297 es->pitch = es->width;
302 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
304 struct wlsc_surface *es = (struct wlsc_surface *) surface;
305 struct wlsc_compositor *ec = es->compositor;
307 es->pitch = es->width;
308 es->image = sprite->image;
309 if (sprite->image != EGL_NO_IMAGE_KHR) {
310 glBindTexture(GL_TEXTURE_2D, es->texture);
311 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
313 if (es->saved_texture == 0)
314 es->saved_texture = es->texture;
315 es->texture = sprite->texture;
318 es->visual = sprite->visual;
322 SPRITE_USE_CURSOR = (1 << 0),
325 static struct wlsc_sprite *
326 create_sprite_from_png(struct wlsc_compositor *ec,
327 const char *filename, uint32_t usage)
330 struct wlsc_sprite *sprite;
331 int32_t width, height;
334 pixels = wlsc_load_image(filename, &width, &height, &stride);
338 sprite = malloc(sizeof *sprite);
339 if (sprite == NULL) {
344 sprite->visual = &ec->compositor.premultiplied_argb_visual;
345 sprite->width = width;
346 sprite->height = height;
347 sprite->image = EGL_NO_IMAGE_KHR;
349 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
350 sprite->image = ec->create_cursor_image(ec, width, height);
352 glGenTextures(1, &sprite->texture);
353 glBindTexture(GL_TEXTURE_2D, sprite->texture);
354 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
355 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
359 if (sprite->image != EGL_NO_IMAGE_KHR) {
360 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
361 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
362 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
364 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
365 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
373 static const struct {
374 const char *filename;
375 int hotspot_x, hotspot_y;
376 } pointer_images[] = {
377 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
378 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
379 { DATADIR "/wayland/bottom_side.png", 16, 20 },
380 { DATADIR "/wayland/grabbing.png", 20, 17 },
381 { DATADIR "/wayland/left_ptr.png", 10, 5 },
382 { DATADIR "/wayland/left_side.png", 10, 20 },
383 { DATADIR "/wayland/right_side.png", 30, 19 },
384 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
385 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
386 { DATADIR "/wayland/top_side.png", 18, 8 },
387 { DATADIR "/wayland/xterm.png", 15, 15 }
391 create_pointer_images(struct wlsc_compositor *ec)
395 count = ARRAY_LENGTH(pointer_images);
396 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
397 for (i = 0; i < count; i++) {
398 ec->pointer_sprites[i] =
399 create_sprite_from_png(ec,
400 pointer_images[i].filename,
405 static struct wlsc_surface *
406 background_create(struct wlsc_output *output, const char *filename)
408 struct wlsc_surface *background;
409 struct wlsc_sprite *sprite;
411 background = wlsc_surface_create(output->compositor,
412 output->x, output->y,
413 output->width, output->height);
414 if (background == NULL)
417 sprite = create_sprite_from_png(output->compositor, filename, 0);
418 if (sprite == NULL) {
423 wlsc_sprite_attach(sprite, &background->surface);
429 texture_region(struct wlsc_surface *es, pixman_region32_t *region)
431 struct wlsc_compositor *ec = es->compositor;
432 GLfloat *v, inv_width, inv_height;
433 pixman_box32_t *rectangles;
437 rectangles = pixman_region32_rectangles(region, &n);
438 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
439 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
440 inv_width = 1.0 / es->pitch;
441 inv_height = 1.0 / es->height;
443 for (i = 0; i < n; i++, v += 16, p += 6) {
444 v[ 0] = rectangles[i].x1;
445 v[ 1] = rectangles[i].y1;
446 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
447 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
449 v[ 4] = rectangles[i].x1;
450 v[ 5] = rectangles[i].y2;
452 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
454 v[ 8] = rectangles[i].x2;
455 v[ 9] = rectangles[i].y1;
456 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
459 v[12] = rectangles[i].x2;
460 v[13] = rectangles[i].y2;
476 transform_vertex(struct wlsc_surface *surface,
477 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
479 struct wlsc_vector t;
486 wlsc_matrix_transform(&surface->transform->matrix, &t);
495 texture_transformed_surface(struct wlsc_surface *es)
497 struct wlsc_compositor *ec = es->compositor;
501 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
502 p = wl_array_add(&ec->indices, 6 * sizeof *p);
504 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
505 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
506 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
507 transform_vertex(es, es->x + es->width, es->y + es->height,
521 wlsc_surface_draw(struct wlsc_surface *es,
522 struct wlsc_output *output, pixman_region32_t *clip)
524 struct wlsc_compositor *ec = es->compositor;
526 pixman_region32_t repaint;
529 pixman_region32_init_rect(&repaint,
530 es->x, es->y, es->width, es->height);
531 pixman_region32_intersect(&repaint, &repaint, clip);
532 if (!pixman_region32_not_empty(&repaint))
535 if (es->visual == &ec->compositor.argb_visual) {
536 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
538 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
539 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
545 if (es->transform == NULL)
546 n = texture_region(es, &repaint);
548 n = texture_transformed_surface(es);
550 glBindTexture(GL_TEXTURE_2D, es->texture);
551 v = ec->vertices.data;
552 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
553 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
554 glEnableVertexAttribArray(0);
555 glEnableVertexAttribArray(1);
556 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
558 ec->vertices.size = 0;
559 ec->indices.size = 0;
560 pixman_region32_fini(&repaint);
564 wlsc_surface_raise(struct wlsc_surface *surface)
566 struct wlsc_compositor *compositor = surface->compositor;
568 wl_list_remove(&surface->link);
569 wl_list_insert(&compositor->surface_list, &surface->link);
573 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
575 struct wlsc_output *output;
577 wl_list_for_each(output, &compositor->output_list, link)
578 wlsc_output_damage(output);
582 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
584 struct wlsc_compositor *compositor = output->compositor;
585 struct wlsc_surface *es;
586 struct wlsc_animation *animation, *next;
588 wl_list_for_each(es, &compositor->surface_list, link) {
589 if (es->output == output) {
590 wl_display_post_frame(compositor->wl_display,
591 &es->surface, msecs);
595 output->finished = 1;
597 wl_event_source_timer_update(compositor->timer_source, 5);
598 compositor->repaint_on_timeout = 1;
600 wl_list_for_each_safe(animation, next,
601 &compositor->animation_list, link)
602 animation->frame(animation, output, msecs);
606 wlsc_output_damage(struct wlsc_output *output)
608 struct wlsc_compositor *compositor = output->compositor;
610 pixman_region32_union_rect(&compositor->damage_region,
611 &compositor->damage_region,
612 output->x, output->y,
613 output->width, output->height);
614 wlsc_compositor_schedule_repaint(compositor);
618 fade_frame(struct wlsc_animation *animation,
619 struct wlsc_output *output, uint32_t msecs)
621 struct wlsc_compositor *compositor =
622 container_of(animation,
623 struct wlsc_compositor, fade.animation);
625 wlsc_spring_update(&compositor->fade.spring, msecs);
626 if (wlsc_spring_done(&compositor->fade.spring)) {
627 if (compositor->fade.spring.current > 0.999) {
628 compositor->state = WLSC_COMPOSITOR_SLEEPING;
629 compositor->shell->lock(compositor->shell);
631 compositor->fade.spring.current =
632 compositor->fade.spring.target;
633 wl_list_remove(&animation->link);
634 wl_list_init(&animation->link);
637 wlsc_output_damage(output);
641 fade_output(struct wlsc_output *output,
642 GLfloat tint, pixman_region32_t *region)
644 struct wlsc_compositor *compositor = output->compositor;
645 struct wlsc_surface surface;
646 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
648 surface.compositor = compositor;
649 surface.x = output->x;
650 surface.y = output->y;
651 surface.width = output->width;
652 surface.height = output->height;
653 surface.texture = GL_NONE;
654 surface.transform = NULL;
658 &compositor->compositor.premultiplied_argb_visual;
660 surface.visual = &compositor->compositor.rgb_visual;
662 glUseProgram(compositor->solid_shader.program);
663 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
664 1, GL_FALSE, output->matrix.d);
665 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
666 wlsc_surface_draw(&surface, output, region);
670 wlsc_output_repaint(struct wlsc_output *output)
672 struct wlsc_compositor *ec = output->compositor;
673 struct wlsc_surface *es;
674 struct wlsc_input_device *eid;
675 pixman_region32_t new_damage, total_damage;
676 int using_hardware_cursor = 1;
678 output->prepare_render(output);
680 glViewport(0, 0, output->width, output->height);
682 glUseProgram(ec->texture_shader.program);
683 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
684 1, GL_FALSE, output->matrix.d);
685 glUniform1i(ec->texture_shader.tex_uniform, 0);
687 pixman_region32_init(&new_damage);
688 pixman_region32_init(&total_damage);
689 pixman_region32_intersect_rect(&new_damage,
691 output->x, output->y,
692 output->width, output->height);
693 pixman_region32_subtract(&ec->damage_region,
694 &ec->damage_region, &new_damage);
695 pixman_region32_union(&total_damage, &new_damage,
696 &output->previous_damage_region);
697 pixman_region32_copy(&output->previous_damage_region, &new_damage);
700 if (output->set_hardware_cursor(output, ec->input_device) < 0)
701 using_hardware_cursor = 0;
702 if (ec->fade.spring.current > 0.001)
703 using_hardware_cursor = 0;
705 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
707 if (es->visual == &ec->compositor.rgb_visual && using_hardware_cursor) {
708 if (output->prepare_scanout_surface(output, es) == 0) {
709 /* We're drawing nothing now,
710 * draw the damaged regions later. */
711 pixman_region32_union(&ec->damage_region,
718 if (es->fullscreen_output == output) {
719 if (es->width < output->width ||
720 es->height < output->height)
721 glClear(GL_COLOR_BUFFER_BIT);
722 wlsc_surface_draw(es, output, &total_damage);
724 if (output->background)
725 wlsc_surface_draw(output->background,
726 output, &total_damage);
728 fade_output(output, 1.0, &total_damage);
730 glUseProgram(ec->texture_shader.program);
731 wl_list_for_each_reverse(es, &ec->surface_list, link) {
732 if (ec->overlay == es)
735 wlsc_surface_draw(es, output, &total_damage);
740 wlsc_surface_draw(ec->overlay, output, &total_damage);
743 wl_list_for_each(eid, &ec->input_device_list, link) {
744 if (&eid->input_device != ec->input_device ||
745 !using_hardware_cursor)
746 wlsc_surface_draw(eid->sprite, output,
750 if (ec->fade.spring.current > 0.001)
751 fade_output(output, ec->fade.spring.current, &total_damage);
757 struct wlsc_compositor *ec = data;
758 struct wlsc_output *output;
759 int repainted_all_outputs = 1;
761 wl_list_for_each(output, &ec->output_list, link) {
762 if (!output->repaint_needed)
765 if (!output->finished) {
766 repainted_all_outputs = 0;
770 wlsc_output_repaint(output);
771 output->finished = 0;
772 output->repaint_needed = 0;
773 output->present(output);
776 if (repainted_all_outputs)
777 ec->repaint_on_timeout = 0;
779 wl_event_source_timer_update(ec->timer_source, 1);
785 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
787 struct wlsc_output *output;
789 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
792 wl_list_for_each(output, &compositor->output_list, link)
793 output->repaint_needed = 1;
795 if (compositor->repaint_on_timeout)
798 wl_event_source_timer_update(compositor->timer_source, 1);
799 compositor->repaint_on_timeout = 1;
803 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
807 done = wlsc_spring_done(&compositor->fade.spring);
808 compositor->fade.spring.target = tint;
809 if (wlsc_spring_done(&compositor->fade.spring))
813 compositor->fade.spring.timestamp =
814 wlsc_compositor_get_time();
816 wlsc_compositor_damage_all(compositor);
817 if (wl_list_empty(&compositor->fade.animation.link))
818 wl_list_insert(compositor->animation_list.prev,
819 &compositor->fade.animation.link);
823 surface_destroy(struct wl_client *client,
824 struct wl_surface *surface)
826 wl_resource_destroy(&surface->resource, client);
830 wlsc_surface_assign_output(struct wlsc_surface *es)
832 struct wlsc_compositor *ec = es->compositor;
833 struct wlsc_output *output;
835 struct wlsc_output *tmp = es->output;
838 wl_list_for_each(output, &ec->output_list, link) {
839 if (output->x < es->x && es->x < output->x + output->width &&
840 output->y < es->y && es->y < output->y + output->height) {
842 printf("assiging surface %p to output %p\n",
848 if (es->output == NULL) {
849 printf("no output found\n");
850 es->output = container_of(ec->output_list.next,
851 struct wlsc_output, link);
856 surface_attach(struct wl_client *client,
857 struct wl_surface *surface, struct wl_buffer *buffer,
858 int32_t x, int32_t y)
860 struct wlsc_surface *es = (struct wlsc_surface *) surface;
862 /* FIXME: This damages the entire old surface, but we should
863 * really just damage the part that's no longer covered by the
864 * surface. Anything covered by the new surface will be
865 * damaged by the client. */
866 wlsc_surface_damage(es);
868 wlsc_buffer_attach(buffer, surface);
870 switch (es->map_type) {
871 case WLSC_SURFACE_MAP_FULLSCREEN:
872 es->x = (es->fullscreen_output->width - es->width) / 2;
873 es->y = (es->fullscreen_output->height - es->height) / 2;
880 es->width = buffer->width;
881 es->height = buffer->height;
882 if (x != 0 || y != 0)
883 wlsc_surface_assign_output(es);
885 es->compositor->shell->attach(es->compositor->shell, es);
889 surface_map_toplevel(struct wl_client *client,
890 struct wl_surface *surface)
892 struct wlsc_surface *es = (struct wlsc_surface *) surface;
893 struct wlsc_compositor *ec = es->compositor;
895 switch (es->map_type) {
896 case WLSC_SURFACE_MAP_UNMAPPED:
897 es->x = 10 + random() % 400;
898 es->y = 10 + random() % 400;
899 /* assign to first output */
900 es->output = container_of(ec->output_list.next,
901 struct wlsc_output, link);
902 wl_list_insert(&es->compositor->surface_list, &es->link);
904 case WLSC_SURFACE_MAP_TOPLEVEL:
906 case WLSC_SURFACE_MAP_FULLSCREEN:
907 es->fullscreen_output = NULL;
915 wlsc_surface_damage(es);
916 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
920 surface_map_transient(struct wl_client *client,
921 struct wl_surface *surface, struct wl_surface *parent,
922 int x, int y, uint32_t flags)
924 struct wlsc_surface *es = (struct wlsc_surface *) surface;
925 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
927 switch (es->map_type) {
928 case WLSC_SURFACE_MAP_UNMAPPED:
929 wl_list_insert(&es->compositor->surface_list, &es->link);
930 /* assign to parents output */
931 es->output = pes->output;
933 case WLSC_SURFACE_MAP_FULLSCREEN:
934 es->fullscreen_output = NULL;
943 wlsc_surface_damage(es);
944 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
948 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
950 struct wlsc_surface *es = (struct wlsc_surface *) surface;
951 struct wlsc_output *output;
953 /* FIXME: Fullscreen on first output */
954 /* FIXME: Handle output going away */
955 output = container_of(es->compositor->output_list.next,
956 struct wlsc_output, link);
958 switch (es->map_type) {
959 case WLSC_SURFACE_MAP_UNMAPPED:
960 es->x = 10 + random() % 400;
961 es->y = 10 + random() % 400;
962 /* assign to first output */
964 wl_list_insert(&es->compositor->surface_list, &es->link);
966 case WLSC_SURFACE_MAP_FULLSCREEN:
974 es->x = (output->width - es->width) / 2;
975 es->y = (output->height - es->height) / 2;
976 es->fullscreen_output = output;
977 wlsc_surface_damage(es);
978 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
982 surface_damage(struct wl_client *client,
983 struct wl_surface *surface,
984 int32_t x, int32_t y, int32_t width, int32_t height)
986 struct wlsc_surface *es = (struct wlsc_surface *) surface;
988 wlsc_surface_damage_rectangle(es, x, y, width, height);
991 const static struct wl_surface_interface surface_interface = {
994 surface_map_toplevel,
995 surface_map_transient,
996 surface_map_fullscreen,
1001 wlsc_input_device_attach(struct wlsc_input_device *device,
1002 int x, int y, int width, int height)
1004 wlsc_surface_damage(device->sprite);
1006 device->hotspot_x = x;
1007 device->hotspot_y = y;
1009 device->sprite->x = device->input_device.x - device->hotspot_x;
1010 device->sprite->y = device->input_device.y - device->hotspot_y;
1011 device->sprite->width = width;
1012 device->sprite->height = height;
1014 wlsc_surface_damage(device->sprite);
1018 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1019 struct wl_buffer *buffer, int x, int y)
1021 wlsc_buffer_attach(buffer, &device->sprite->surface);
1022 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1026 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1027 struct wlsc_sprite *sprite, int x, int y)
1029 wlsc_sprite_attach(sprite, &device->sprite->surface);
1030 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1034 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1035 enum wlsc_pointer_type type)
1037 struct wlsc_compositor *compositor =
1038 (struct wlsc_compositor *) device->input_device.compositor;
1040 wlsc_input_device_attach_sprite(device,
1041 compositor->pointer_sprites[type],
1042 pointer_images[type].hotspot_x,
1043 pointer_images[type].hotspot_y);
1047 compositor_create_surface(struct wl_client *client,
1048 struct wl_compositor *compositor, uint32_t id)
1050 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1051 struct wlsc_surface *surface;
1053 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1054 if (surface == NULL) {
1055 wl_client_post_no_memory(client);
1059 surface->surface.resource.destroy = destroy_surface;
1061 surface->surface.resource.object.id = id;
1062 surface->surface.resource.object.interface = &wl_surface_interface;
1063 surface->surface.resource.object.implementation =
1064 (void (**)(void)) &surface_interface;
1065 surface->surface.client = client;
1067 wl_client_add_resource(client, &surface->surface.resource);
1070 const static struct wl_compositor_interface compositor_interface = {
1071 compositor_create_surface,
1075 wlsc_surface_transform(struct wlsc_surface *surface,
1076 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1078 *sx = x - surface->x;
1079 *sy = y - surface->y;
1082 WL_EXPORT struct wlsc_surface *
1083 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1085 struct wlsc_compositor *ec =
1086 (struct wlsc_compositor *) device->compositor;
1087 struct wlsc_surface *es;
1089 wl_list_for_each(es, &ec->surface_list, link) {
1090 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1091 if (0 <= *sx && *sx < es->width &&
1092 0 <= *sy && *sy < es->height)
1101 motion_grab_motion(struct wl_grab *grab,
1102 uint32_t time, int32_t x, int32_t y)
1104 struct wlsc_input_device *device =
1105 (struct wlsc_input_device *) grab->input_device;
1106 struct wlsc_surface *es =
1107 (struct wlsc_surface *) device->input_device.pointer_focus;
1110 wlsc_surface_transform(es, x, y, &sx, &sy);
1111 wl_client_post_event(es->surface.client,
1112 &device->input_device.object,
1113 WL_INPUT_DEVICE_MOTION,
1114 time, x, y, sx, sy);
1118 motion_grab_button(struct wl_grab *grab,
1119 uint32_t time, int32_t button, int32_t state)
1121 wl_client_post_event(grab->input_device->pointer_focus->client,
1122 &grab->input_device->object,
1123 WL_INPUT_DEVICE_BUTTON,
1124 time, button, state);
1128 motion_grab_end(struct wl_grab *grab, uint32_t time)
1132 static const struct wl_grab_interface motion_grab_interface = {
1139 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1141 if (compositor->idle_inhibit)
1144 wlsc_compositor_fade(compositor, 0.0);
1145 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1147 wl_event_source_timer_update(compositor->idle_source,
1148 option_idle_time * 1000);
1152 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1154 wlsc_compositor_wake(compositor);
1155 compositor->idle_inhibit++;
1159 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1161 compositor->idle_inhibit--;
1162 wlsc_compositor_wake(compositor);
1166 idle_handler(void *data)
1168 struct wlsc_compositor *compositor = data;
1170 if (compositor->idle_inhibit)
1173 wlsc_compositor_fade(compositor, 1.0);
1179 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1181 struct wlsc_surface *es;
1182 struct wlsc_compositor *ec =
1183 (struct wlsc_compositor *) device->compositor;
1184 struct wlsc_output *output;
1185 const struct wl_grab_interface *interface;
1186 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1188 int x_valid = 0, y_valid = 0;
1189 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1191 wlsc_compositor_wake(ec);
1193 wl_list_for_each(output, &ec->output_list, link) {
1194 if (output->x <= x && x <= output->x + output->width)
1197 if (output->y <= y && y <= output->y + output->height)
1200 /* FIXME: calculate this only on output addition/deletion */
1201 if (output->x < min_x)
1203 if (output->y < min_y)
1206 if (output->x + output->width > max_x)
1207 max_x = output->x + output->width;
1208 if (output->y + output->height > max_y)
1209 max_y = output->y + output->height;
1215 else if (x >= max_x)
1221 else if (y >= max_y)
1229 interface = device->grab->interface;
1230 interface->motion(device->grab, time, x, y);
1232 es = pick_surface(device, &sx, &sy);
1233 wl_input_device_set_pointer_focus(device,
1235 time, x, y, sx, sy);
1237 wl_client_post_event(es->surface.client,
1239 WL_INPUT_DEVICE_MOTION,
1240 time, x, y, sx, sy);
1243 wlsc_surface_damage(wd->sprite);
1245 wd->sprite->x = device->x - wd->hotspot_x;
1246 wd->sprite->y = device->y - wd->hotspot_y;
1248 wlsc_surface_damage(wd->sprite);
1252 wlsc_surface_activate(struct wlsc_surface *surface,
1253 struct wlsc_input_device *device, uint32_t time)
1255 struct wlsc_shell *shell = surface->compositor->shell;
1257 wlsc_surface_raise(surface);
1258 if (device->selection)
1259 shell->set_selection_focus(shell,
1261 &surface->surface, time);
1263 wl_input_device_set_keyboard_focus(&device->input_device,
1268 struct wlsc_binding {
1272 wlsc_binding_handler_t handler;
1274 struct wl_list link;
1278 notify_button(struct wl_input_device *device,
1279 uint32_t time, int32_t button, int32_t state)
1281 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1282 struct wlsc_compositor *compositor =
1283 (struct wlsc_compositor *) device->compositor;
1284 struct wlsc_binding *b;
1285 struct wlsc_surface *surface =
1286 (struct wlsc_surface *) device->pointer_focus;
1289 wlsc_compositor_idle_inhibit(compositor);
1291 wlsc_compositor_idle_release(compositor);
1293 if (state && surface && device->grab == NULL) {
1294 wlsc_surface_activate(surface, wd, time);
1295 wl_input_device_start_grab(device,
1296 &device->motion_grab,
1300 wl_list_for_each(b, &compositor->binding_list, link) {
1301 if (b->button == button &&
1302 b->modifier == wd->modifier_state && state) {
1303 b->handler(&wd->input_device,
1304 time, 0, button, state, b->data);
1310 device->grab->interface->button(device->grab, time,
1313 if (!state && device->grab && device->grab_button == button)
1314 wl_input_device_end_grab(device, time);
1318 terminate_binding(struct wl_input_device *device, uint32_t time,
1319 uint32_t key, uint32_t button, uint32_t state, void *data)
1321 struct wlsc_compositor *compositor = data;
1324 wl_display_terminate(compositor->wl_display);
1327 WL_EXPORT struct wlsc_binding *
1328 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1329 uint32_t key, uint32_t button, uint32_t modifier,
1330 wlsc_binding_handler_t handler, void *data)
1332 struct wlsc_binding *binding;
1334 binding = malloc(sizeof *binding);
1335 if (binding == NULL)
1339 binding->button = button;
1340 binding->modifier = modifier;
1341 binding->handler = handler;
1342 binding->data = data;
1343 wl_list_insert(compositor->binding_list.prev, &binding->link);
1349 wlsc_binding_destroy(struct wlsc_binding *binding)
1351 wl_list_remove(&binding->link);
1356 update_modifier_state(struct wlsc_input_device *device,
1357 uint32_t key, uint32_t state)
1364 modifier = MODIFIER_CTRL;
1369 modifier = MODIFIER_ALT;
1374 modifier = MODIFIER_SUPER;
1383 device->modifier_state |= modifier;
1385 device->modifier_state &= ~modifier;
1389 notify_key(struct wl_input_device *device,
1390 uint32_t time, uint32_t key, uint32_t state)
1392 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1393 struct wlsc_compositor *compositor =
1394 (struct wlsc_compositor *) device->compositor;
1396 struct wlsc_binding *b;
1399 wlsc_compositor_idle_inhibit(compositor);
1401 wlsc_compositor_idle_release(compositor);
1403 wl_list_for_each(b, &compositor->binding_list, link) {
1404 if (b->key == key &&
1405 b->modifier == wd->modifier_state) {
1406 b->handler(&wd->input_device,
1407 time, key, 0, state, b->data);
1412 update_modifier_state(wd, key, state);
1413 end = device->keys.data + device->keys.size;
1414 for (k = device->keys.data; k < end; k++) {
1418 device->keys.size = (void *) end - device->keys.data;
1420 k = wl_array_add(&device->keys, sizeof *k);
1424 if (device->keyboard_focus != NULL)
1425 wl_client_post_event(device->keyboard_focus->client,
1427 WL_INPUT_DEVICE_KEY, time, key, state);
1431 notify_pointer_focus(struct wl_input_device *device,
1432 uint32_t time, struct wlsc_output *output,
1433 int32_t x, int32_t y)
1435 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1436 struct wlsc_compositor *compositor =
1437 (struct wlsc_compositor *) device->compositor;
1438 struct wlsc_surface *es;
1444 es = pick_surface(device, &sx, &sy);
1445 wl_input_device_set_pointer_focus(device,
1447 time, x, y, sx, sy);
1449 compositor->focus = 1;
1451 wd->sprite->x = device->x - wd->hotspot_x;
1452 wd->sprite->y = device->y - wd->hotspot_y;
1454 wl_input_device_set_pointer_focus(device, NULL,
1456 compositor->focus = 0;
1459 wlsc_surface_damage(wd->sprite);
1463 notify_keyboard_focus(struct wl_input_device *device,
1464 uint32_t time, struct wlsc_output *output,
1465 struct wl_array *keys)
1467 struct wlsc_input_device *wd =
1468 (struct wlsc_input_device *) device;
1469 struct wlsc_compositor *compositor =
1470 (struct wlsc_compositor *) device->compositor;
1471 struct wlsc_surface *es;
1474 if (!wl_list_empty(&compositor->surface_list))
1475 es = container_of(compositor->surface_list.next,
1476 struct wlsc_surface, link);
1481 wl_array_copy(&wd->input_device.keys, keys);
1482 wd->modifier_state = 0;
1483 end = device->keys.data + device->keys.size;
1484 for (k = device->keys.data; k < end; k++) {
1485 wlsc_compositor_idle_inhibit(compositor);
1486 update_modifier_state(wd, *k, 1);
1489 wl_input_device_set_keyboard_focus(&wd->input_device,
1490 &es->surface, time);
1492 end = device->keys.data + device->keys.size;
1493 for (k = device->keys.data; k < end; k++)
1494 wlsc_compositor_idle_release(compositor);
1496 wd->modifier_state = 0;
1497 wl_input_device_set_keyboard_focus(&wd->input_device,
1504 input_device_attach(struct wl_client *client,
1505 struct wl_input_device *device_base,
1507 struct wl_buffer *buffer, int32_t x, int32_t y)
1509 struct wlsc_input_device *device =
1510 (struct wlsc_input_device *) device_base;
1512 if (time < device->input_device.pointer_focus_time)
1514 if (device->input_device.pointer_focus == NULL)
1516 if (device->input_device.pointer_focus->client != client)
1519 if (buffer == NULL) {
1520 wlsc_input_device_set_pointer_image(device,
1521 WLSC_POINTER_LEFT_PTR);
1525 wlsc_input_device_attach_buffer(device, buffer, x, y);
1528 const static struct wl_input_device_interface input_device_interface = {
1529 input_device_attach,
1533 wlsc_input_device_init(struct wlsc_input_device *device,
1534 struct wlsc_compositor *ec)
1536 wl_input_device_init(&device->input_device, &ec->compositor);
1538 device->input_device.object.interface = &wl_input_device_interface;
1539 device->input_device.object.implementation =
1540 (void (**)(void)) &input_device_interface;
1541 wl_display_add_object(ec->wl_display, &device->input_device.object);
1542 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1544 device->sprite = wlsc_surface_create(ec,
1545 device->input_device.x,
1546 device->input_device.y, 32, 32);
1547 device->hotspot_x = 16;
1548 device->hotspot_y = 16;
1549 device->modifier_state = 0;
1551 device->input_device.motion_grab.interface = &motion_grab_interface;
1553 wl_list_insert(ec->input_device_list.prev, &device->link);
1555 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1559 wlsc_output_post_geometry(struct wl_client *client,
1560 struct wl_object *global, uint32_t version)
1562 struct wlsc_output *output =
1563 container_of(global, struct wlsc_output, object);
1565 wl_client_post_event(client, global,
1567 output->x, output->y,
1568 output->width, output->height);
1571 static const char vertex_shader[] =
1572 "uniform mat4 proj;\n"
1573 "attribute vec2 position;\n"
1574 "attribute vec2 texcoord;\n"
1575 "varying vec2 v_texcoord;\n"
1578 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1579 " v_texcoord = texcoord;\n"
1582 static const char texture_fragment_shader[] =
1583 "precision mediump float;\n"
1584 "varying vec2 v_texcoord;\n"
1585 "uniform sampler2D tex;\n"
1588 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1591 static const char solid_fragment_shader[] =
1592 "precision mediump float;\n"
1593 "varying vec2 v_texcoord;\n"
1594 "uniform vec4 color;\n"
1597 " gl_FragColor = color\n;"
1601 compile_shader(GLenum type, const char *source)
1607 s = glCreateShader(type);
1608 glShaderSource(s, 1, &source, NULL);
1610 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1612 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1613 fprintf(stderr, "shader info: %s\n", msg);
1621 wlsc_shader_init(struct wlsc_shader *shader,
1622 const char *vertex_source, const char *fragment_source)
1627 shader->vertex_shader =
1628 compile_shader(GL_VERTEX_SHADER, vertex_source);
1629 shader->fragment_shader =
1630 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1632 shader->program = glCreateProgram();
1633 glAttachShader(shader->program, shader->vertex_shader);
1634 glAttachShader(shader->program, shader->fragment_shader);
1635 glBindAttribLocation(shader->program, 0, "position");
1636 glBindAttribLocation(shader->program, 1, "texcoord");
1638 glLinkProgram(shader->program);
1639 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1641 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1642 fprintf(stderr, "link info: %s\n", msg);
1646 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1647 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1653 init_solid_shader(struct wlsc_shader *shader,
1654 GLuint vertex_shader, const char *fragment_source)
1659 shader->vertex_shader = vertex_shader;
1660 shader->fragment_shader =
1661 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1663 shader->program = glCreateProgram();
1664 glAttachShader(shader->program, shader->vertex_shader);
1665 glAttachShader(shader->program, shader->fragment_shader);
1666 glBindAttribLocation(shader->program, 0, "position");
1667 glBindAttribLocation(shader->program, 1, "texcoord");
1669 glLinkProgram(shader->program);
1670 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1672 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1673 fprintf(stderr, "link info: %s\n", msg);
1677 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1678 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1684 wlsc_output_destroy(struct wlsc_output *output)
1686 destroy_surface(&output->background->surface.resource, NULL);
1690 wlsc_output_move(struct wlsc_output *output, int x, int y)
1692 struct wlsc_compositor *c = output->compositor;
1698 if (output->background) {
1699 output->background->x = x;
1700 output->background->y = y;
1703 pixman_region32_init(&output->previous_damage_region);
1705 wlsc_matrix_init(&output->matrix);
1706 wlsc_matrix_translate(&output->matrix,
1707 -output->x - output->width / 2.0,
1708 -output->y - output->height / 2.0, 0);
1710 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1711 wlsc_matrix_scale(&output->matrix,
1712 2.0 / output->width,
1713 flip * 2.0 / output->height, 1);
1715 pixman_region32_union_rect(&c->damage_region,
1717 x, y, output->width, output->height);
1721 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1722 int x, int y, int width, int height, uint32_t flags)
1724 output->compositor = c;
1727 output->width = width;
1728 output->height = height;
1730 output->background =
1731 background_create(output, option_background);
1733 output->flags = flags;
1734 output->finished = 1;
1735 wlsc_output_move(output, x, y);
1737 output->object.interface = &wl_output_interface;
1738 wl_display_add_object(c->wl_display, &output->object);
1739 wl_display_add_global(c->wl_display, &output->object,
1740 wlsc_output_post_geometry);
1744 shm_buffer_created(struct wl_buffer *buffer)
1746 struct wl_list *surfaces_attached_to;
1748 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1749 if (!surfaces_attached_to) {
1750 buffer->user_data = NULL;
1754 wl_list_init(surfaces_attached_to);
1756 buffer->user_data = surfaces_attached_to;
1760 shm_buffer_damaged(struct wl_buffer *buffer,
1761 int32_t x, int32_t y, int32_t width, int32_t height)
1763 struct wl_list *surfaces_attached_to = buffer->user_data;
1764 struct wlsc_surface *es;
1765 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1767 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1768 glBindTexture(GL_TEXTURE_2D, es->texture);
1769 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1770 tex_width, buffer->height, 0,
1771 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1772 wl_shm_buffer_get_data(buffer));
1773 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1774 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1779 shm_buffer_destroyed(struct wl_buffer *buffer)
1781 struct wl_list *surfaces_attached_to = buffer->user_data;
1782 struct wlsc_surface *es, *next;
1784 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1785 wl_list_remove(&es->buffer_link);
1786 wl_list_init(&es->buffer_link);
1789 free(surfaces_attached_to);
1792 const static struct wl_shm_callbacks shm_callbacks = {
1795 shm_buffer_destroyed
1799 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1801 struct wl_event_loop *loop;
1802 const char *extensions;
1804 ec->wl_display = display;
1806 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1808 ec->shm = wl_shm_init(display, &shm_callbacks);
1810 ec->image_target_texture_2d =
1811 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1812 ec->image_target_renderbuffer_storage = (void *)
1813 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1814 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1815 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1817 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1818 ec->unbind_display =
1819 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1821 extensions = (const char *) glGetString(GL_EXTENSIONS);
1822 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1824 "GL_EXT_texture_format_BGRA8888 not available\n");
1829 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1830 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1831 ec->has_bind_display = 1;
1832 if (ec->has_bind_display)
1833 ec->bind_display(ec->display, ec->wl_display);
1835 wl_list_init(&ec->surface_list);
1836 wl_list_init(&ec->input_device_list);
1837 wl_list_init(&ec->output_list);
1838 wl_list_init(&ec->binding_list);
1839 wl_list_init(&ec->animation_list);
1840 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
1841 ec->fade.animation.frame = fade_frame;
1842 wl_list_init(&ec->fade.animation.link);
1844 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1845 MODIFIER_CTRL | MODIFIER_ALT,
1846 terminate_binding, ec);
1848 create_pointer_images(ec);
1850 screenshooter_create(ec);
1852 glActiveTexture(GL_TEXTURE0);
1854 if (wlsc_shader_init(&ec->texture_shader,
1855 vertex_shader, texture_fragment_shader) < 0)
1857 if (init_solid_shader(&ec->solid_shader,
1858 ec->texture_shader.vertex_shader,
1859 solid_fragment_shader) < 0)
1862 loop = wl_display_get_event_loop(ec->wl_display);
1863 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1864 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1866 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1867 pixman_region32_init(&ec->damage_region);
1868 wlsc_compositor_schedule_repaint(ec);
1873 static int on_term_signal(int signal_number, void *data)
1875 struct wlsc_compositor *ec = data;
1877 wl_display_terminate(ec->wl_display);
1883 load_module(const char *name, const char *entrypoint, void **handle)
1885 char path[PATH_MAX];
1886 void *module, *init;
1889 snprintf(path, sizeof path, MODULEDIR "/%s", name);
1891 snprintf(path, sizeof path, name);
1893 module = dlopen(path, RTLD_LAZY);
1896 "failed to load module: %s\n", dlerror());
1900 init = dlsym(module, entrypoint);
1903 "failed to lookup init function: %s\n", dlerror());
1910 int main(int argc, char *argv[])
1912 struct wl_display *display;
1913 struct wlsc_compositor *ec;
1914 struct wl_event_loop *loop;
1916 void *shell_module, *backend_module;
1917 int (*shell_init)(struct wlsc_compositor *ec);
1918 struct wlsc_compositor
1919 *(*backend_init)(struct wl_display *display, char *options);
1920 char *backend = NULL;
1921 char *backend_options = "";
1925 static const char opts[] = "B:b:o:S:i:s:";
1926 static const struct option longopts[ ] = {
1927 { "backend", 1, NULL, 'B' },
1928 { "backend-options", 1, NULL, 'o' },
1929 { "background", 1, NULL, 'b' },
1930 { "socket", 1, NULL, 'S' },
1931 { "idle-time", 1, NULL, 'i' },
1932 { "shell", 1, NULL, 's' },
1936 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1939 option_background = optarg;
1945 backend_options = optarg;
1948 option_socket_name = optarg;
1951 option_idle_time = strtol(optarg, &p, 0);
1954 "invalid idle time option: %s\n",
1965 display = wl_display_create();
1970 if (getenv("WAYLAND_DISPLAY"))
1971 backend = "wayland-backend.so";
1972 else if (getenv("DISPLAY"))
1973 backend = "x11-backend.so";
1974 else if (getenv("OPENWFD"))
1975 backend = "openwfd-backend.so";
1977 backend = "drm-backend.so";
1981 shell = "desktop-shell.so";
1983 backend_init = load_module(backend, "backend_init", &backend_module);
1987 shell_init = load_module(shell, "shell_init", &shell_module);
1991 ec = backend_init(display, backend_options);
1993 fprintf(stderr, "failed to create compositor\n");
1997 if (shell_init(ec) < 0)
2000 if (wl_display_add_socket(display, option_socket_name)) {
2001 fprintf(stderr, "failed to add socket: %m\n");
2005 loop = wl_display_get_event_loop(ec->wl_display);
2006 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
2007 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
2009 wl_display_run(display);
2011 if (ec->has_bind_display)
2012 ec->unbind_display(ec->display, display);
2013 wl_display_destroy(display);