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) / 300.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;
141 if (spring->current >= 1.0) {
142 #ifdef TWEENER_BOUNCE
143 spring->current = 2.0 - spring->current;
144 spring->previous = 2.0 - spring->previous;
146 spring->current = 1.0;
147 spring->previous = 1.0;
151 if (spring->current <= 0.0) {
152 spring->current = 0.0;
153 spring->previous = 0.0;
159 wlsc_spring_done(struct wlsc_spring *spring)
161 return fabs(spring->previous - spring->target) < 0.0002 &&
162 fabs(spring->current - spring->target) < 0.0002;
165 WL_EXPORT struct wlsc_surface *
166 wlsc_surface_create(struct wlsc_compositor *compositor,
167 int32_t x, int32_t y, int32_t width, int32_t height)
169 struct wlsc_surface *surface;
171 surface = malloc(sizeof *surface);
175 wl_list_init(&surface->link);
176 wl_list_init(&surface->buffer_link);
177 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
179 glGenTextures(1, &surface->texture);
180 glBindTexture(GL_TEXTURE_2D, surface->texture);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
186 surface->compositor = compositor;
187 surface->visual = NULL;
188 surface->image = EGL_NO_IMAGE_KHR;
189 surface->saved_texture = 0;
192 surface->width = width;
193 surface->height = height;
195 surface->transform = NULL;
201 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
202 int32_t x, int32_t y,
203 int32_t width, int32_t height)
205 struct wlsc_compositor *compositor = surface->compositor;
207 pixman_region32_union_rect(&compositor->damage_region,
208 &compositor->damage_region,
209 surface->x + x, surface->y + y,
211 wlsc_compositor_schedule_repaint(compositor);
215 wlsc_surface_damage(struct wlsc_surface *surface)
217 wlsc_surface_damage_rectangle(surface, 0, 0,
218 surface->width, surface->height);
222 wlsc_compositor_get_time(void)
226 gettimeofday(&tv, NULL);
228 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
232 destroy_surface(struct wl_resource *resource, struct wl_client *client)
234 struct wlsc_surface *surface =
235 container_of(resource, struct wlsc_surface, surface.resource);
236 struct wlsc_compositor *compositor = surface->compositor;
238 wlsc_surface_damage(surface);
240 wl_list_remove(&surface->link);
241 if (surface->saved_texture == 0)
242 glDeleteTextures(1, &surface->texture);
244 glDeleteTextures(1, &surface->saved_texture);
247 if (surface->image != EGL_NO_IMAGE_KHR)
248 compositor->destroy_image(compositor->display,
251 wl_list_remove(&surface->buffer_link);
257 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
259 struct wlsc_surface *es = (struct wlsc_surface *) surface;
260 struct wlsc_compositor *ec = es->compositor;
261 struct wl_list *surfaces_attached_to;
263 if (es->saved_texture != 0)
264 es->texture = es->saved_texture;
266 glBindTexture(GL_TEXTURE_2D, es->texture);
268 if (wl_buffer_is_shm(buffer)) {
269 /* Unbind any EGLImage texture that may be bound, so we don't
271 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
272 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
273 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
274 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
275 es->pitch, buffer->height, 0,
276 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
277 wl_shm_buffer_get_data(buffer));
278 es->visual = buffer->visual;
280 surfaces_attached_to = buffer->user_data;
282 wl_list_remove(&es->buffer_link);
283 wl_list_insert(surfaces_attached_to, &es->buffer_link);
285 es->image = ec->create_image(ec->display, NULL,
286 EGL_WAYLAND_BUFFER_WL,
289 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
290 es->visual = buffer->visual;
291 es->pitch = es->width;
296 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
298 struct wlsc_surface *es = (struct wlsc_surface *) surface;
299 struct wlsc_compositor *ec = es->compositor;
301 es->pitch = es->width;
302 es->image = sprite->image;
303 if (sprite->image != EGL_NO_IMAGE_KHR) {
304 glBindTexture(GL_TEXTURE_2D, es->texture);
305 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
307 if (es->saved_texture == 0)
308 es->saved_texture = es->texture;
309 es->texture = sprite->texture;
312 es->visual = sprite->visual;
316 SPRITE_USE_CURSOR = (1 << 0),
319 static struct wlsc_sprite *
320 create_sprite_from_png(struct wlsc_compositor *ec,
321 const char *filename, uint32_t usage)
324 struct wlsc_sprite *sprite;
325 int32_t width, height;
328 pixels = wlsc_load_image(filename, &width, &height, &stride);
332 sprite = malloc(sizeof *sprite);
333 if (sprite == NULL) {
338 sprite->visual = &ec->compositor.premultiplied_argb_visual;
339 sprite->width = width;
340 sprite->height = height;
341 sprite->image = EGL_NO_IMAGE_KHR;
343 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
344 sprite->image = ec->create_cursor_image(ec, width, height);
346 glGenTextures(1, &sprite->texture);
347 glBindTexture(GL_TEXTURE_2D, sprite->texture);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
351 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
353 if (sprite->image != EGL_NO_IMAGE_KHR) {
354 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
355 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
356 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
358 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
359 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
367 static const struct {
368 const char *filename;
369 int hotspot_x, hotspot_y;
370 } pointer_images[] = {
371 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
372 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
373 { DATADIR "/wayland/bottom_side.png", 16, 20 },
374 { DATADIR "/wayland/grabbing.png", 20, 17 },
375 { DATADIR "/wayland/left_ptr.png", 10, 5 },
376 { DATADIR "/wayland/left_side.png", 10, 20 },
377 { DATADIR "/wayland/right_side.png", 30, 19 },
378 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
379 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
380 { DATADIR "/wayland/top_side.png", 18, 8 },
381 { DATADIR "/wayland/xterm.png", 15, 15 }
385 create_pointer_images(struct wlsc_compositor *ec)
389 count = ARRAY_LENGTH(pointer_images);
390 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
391 for (i = 0; i < count; i++) {
392 ec->pointer_sprites[i] =
393 create_sprite_from_png(ec,
394 pointer_images[i].filename,
399 static struct wlsc_surface *
400 background_create(struct wlsc_output *output, const char *filename)
402 struct wlsc_surface *background;
403 struct wlsc_sprite *sprite;
405 background = wlsc_surface_create(output->compositor,
406 output->x, output->y,
407 output->width, output->height);
408 if (background == NULL)
411 sprite = create_sprite_from_png(output->compositor, filename, 0);
412 if (sprite == NULL) {
417 wlsc_sprite_attach(sprite, &background->surface);
423 texture_region(struct wlsc_surface *es, pixman_region32_t *region)
425 struct wlsc_compositor *ec = es->compositor;
426 GLfloat *v, inv_width, inv_height;
427 pixman_box32_t *rectangles;
431 rectangles = pixman_region32_rectangles(region, &n);
432 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
433 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
434 inv_width = 1.0 / es->pitch;
435 inv_height = 1.0 / es->height;
437 for (i = 0; i < n; i++, v += 16, p += 6) {
438 v[ 0] = rectangles[i].x1;
439 v[ 1] = rectangles[i].y1;
440 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
441 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
443 v[ 4] = rectangles[i].x1;
444 v[ 5] = rectangles[i].y2;
446 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
448 v[ 8] = rectangles[i].x2;
449 v[ 9] = rectangles[i].y1;
450 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
453 v[12] = rectangles[i].x2;
454 v[13] = rectangles[i].y2;
470 transform_vertex(struct wlsc_surface *surface,
471 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
473 struct wlsc_vector t;
480 wlsc_matrix_transform(&surface->transform->matrix, &t);
489 texture_transformed_surface(struct wlsc_surface *es)
491 struct wlsc_compositor *ec = es->compositor;
495 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
496 p = wl_array_add(&ec->indices, 6 * sizeof *p);
498 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
499 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
500 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
501 transform_vertex(es, es->x + es->width, es->y + es->height,
515 wlsc_surface_draw(struct wlsc_surface *es,
516 struct wlsc_output *output, pixman_region32_t *clip)
518 struct wlsc_compositor *ec = es->compositor;
520 pixman_region32_t repaint;
523 pixman_region32_init_rect(&repaint,
524 es->x, es->y, es->width, es->height);
525 pixman_region32_intersect(&repaint, &repaint, clip);
526 if (!pixman_region32_not_empty(&repaint))
529 if (es->visual == &ec->compositor.argb_visual) {
530 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
532 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
533 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
539 if (es->transform == NULL)
540 n = texture_region(es, &repaint);
542 n = texture_transformed_surface(es);
544 glBindTexture(GL_TEXTURE_2D, es->texture);
545 v = ec->vertices.data;
546 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
547 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
548 glEnableVertexAttribArray(0);
549 glEnableVertexAttribArray(1);
550 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
552 ec->vertices.size = 0;
553 ec->indices.size = 0;
554 pixman_region32_fini(&repaint);
558 wlsc_surface_raise(struct wlsc_surface *surface)
560 struct wlsc_compositor *compositor = surface->compositor;
562 wl_list_remove(&surface->link);
563 wl_list_insert(&compositor->surface_list, &surface->link);
567 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
569 struct wlsc_output *output;
571 wl_list_for_each(output, &compositor->output_list, link)
572 wlsc_output_damage(output);
576 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
578 struct wlsc_compositor *compositor = output->compositor;
579 struct wlsc_surface *es;
580 struct wlsc_animation *animation, *next;
582 wl_list_for_each(es, &compositor->surface_list, link) {
583 if (es->output == output) {
584 wl_display_post_frame(compositor->wl_display,
585 &es->surface, msecs);
589 output->finished = 1;
591 wl_event_source_timer_update(compositor->timer_source, 5);
592 compositor->repaint_on_timeout = 1;
594 wl_list_for_each_safe(animation, next,
595 &compositor->animation_list, link)
596 animation->frame(animation, output, msecs);
600 wlsc_output_damage(struct wlsc_output *output)
602 struct wlsc_compositor *compositor = output->compositor;
604 pixman_region32_union_rect(&compositor->damage_region,
605 &compositor->damage_region,
606 output->x, output->y,
607 output->width, output->height);
608 wlsc_compositor_schedule_repaint(compositor);
612 fade_frame(struct wlsc_animation *animation,
613 struct wlsc_output *output, uint32_t msecs)
615 struct wlsc_compositor *compositor =
616 container_of(animation,
617 struct wlsc_compositor, fade.animation);
619 wlsc_spring_update(&compositor->fade.spring, msecs);
620 if (wlsc_spring_done(&compositor->fade.spring)) {
621 if (compositor->fade.spring.current > 0.999) {
622 compositor->state = WLSC_COMPOSITOR_SLEEPING;
623 compositor->shell->lock(compositor->shell);
625 compositor->fade.spring.current =
626 compositor->fade.spring.target;
627 wl_list_remove(&animation->link);
628 wl_list_init(&animation->link);
631 wlsc_output_damage(output);
635 fade_output(struct wlsc_output *output,
636 GLfloat tint, pixman_region32_t *region)
638 struct wlsc_compositor *compositor = output->compositor;
639 struct wlsc_surface surface;
640 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
642 surface.compositor = compositor;
643 surface.x = output->x;
644 surface.y = output->y;
645 surface.width = output->width;
646 surface.height = output->height;
647 surface.texture = GL_NONE;
648 surface.transform = NULL;
652 &compositor->compositor.premultiplied_argb_visual;
654 surface.visual = &compositor->compositor.rgb_visual;
656 glUseProgram(compositor->solid_shader.program);
657 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
658 1, GL_FALSE, output->matrix.d);
659 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
660 wlsc_surface_draw(&surface, output, region);
664 wlsc_output_repaint(struct wlsc_output *output)
666 struct wlsc_compositor *ec = output->compositor;
667 struct wlsc_surface *es;
668 struct wlsc_input_device *eid, *hw_cursor;
669 pixman_region32_t new_damage, total_damage;
671 output->prepare_render(output);
673 glViewport(0, 0, output->width, output->height);
675 glUseProgram(ec->texture_shader.program);
676 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
677 1, GL_FALSE, output->matrix.d);
678 glUniform1i(ec->texture_shader.tex_uniform, 0);
680 pixman_region32_init(&new_damage);
681 pixman_region32_init(&total_damage);
682 pixman_region32_intersect_rect(&new_damage,
684 output->x, output->y,
685 output->width, output->height);
686 pixman_region32_subtract(&ec->damage_region,
687 &ec->damage_region, &new_damage);
688 pixman_region32_union(&total_damage, &new_damage,
689 &output->previous_damage_region);
690 pixman_region32_copy(&output->previous_damage_region, &new_damage);
693 if (ec->focus && ec->fade.spring.current < 0.001) {
694 hw_cursor = (struct wlsc_input_device *) ec->input_device;
695 if (output->set_hardware_cursor(output, hw_cursor) < 0)
698 output->set_hardware_cursor(output, NULL);
701 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
703 if (es->visual == &ec->compositor.rgb_visual && hw_cursor) {
704 if (output->prepare_scanout_surface(output, es) == 0) {
705 /* We're drawing nothing now,
706 * draw the damaged regions later. */
707 pixman_region32_union(&ec->damage_region,
714 if (es->fullscreen_output == output) {
715 if (es->width < output->width ||
716 es->height < output->height)
717 glClear(GL_COLOR_BUFFER_BIT);
718 wlsc_surface_draw(es, output, &total_damage);
720 if (output->background)
721 wlsc_surface_draw(output->background,
722 output, &total_damage);
724 fade_output(output, 1.0, &total_damage);
726 glUseProgram(ec->texture_shader.program);
727 wl_list_for_each_reverse(es, &ec->surface_list, link) {
728 if (ec->overlay == es)
731 wlsc_surface_draw(es, output, &total_damage);
736 wlsc_surface_draw(ec->overlay, output, &total_damage);
739 wl_list_for_each(eid, &ec->input_device_list, link) {
740 if (&eid->input_device != ec->input_device ||
742 wlsc_surface_draw(eid->sprite, output,
746 if (ec->fade.spring.current > 0.001)
747 fade_output(output, ec->fade.spring.current, &total_damage);
753 struct wlsc_compositor *ec = data;
754 struct wlsc_output *output;
755 int repainted_all_outputs = 1;
757 wl_list_for_each(output, &ec->output_list, link) {
758 if (!output->repaint_needed)
761 if (!output->finished) {
762 repainted_all_outputs = 0;
766 wlsc_output_repaint(output);
767 output->finished = 0;
768 output->repaint_needed = 0;
769 output->present(output);
772 if (repainted_all_outputs)
773 ec->repaint_on_timeout = 0;
775 wl_event_source_timer_update(ec->timer_source, 1);
781 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
783 struct wlsc_output *output;
785 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
788 wl_list_for_each(output, &compositor->output_list, link)
789 output->repaint_needed = 1;
791 if (compositor->repaint_on_timeout)
794 wl_event_source_timer_update(compositor->timer_source, 1);
795 compositor->repaint_on_timeout = 1;
799 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
803 done = wlsc_spring_done(&compositor->fade.spring);
804 compositor->fade.spring.target = tint;
805 if (wlsc_spring_done(&compositor->fade.spring))
809 compositor->fade.spring.timestamp =
810 wlsc_compositor_get_time();
812 wlsc_compositor_damage_all(compositor);
813 if (wl_list_empty(&compositor->fade.animation.link))
814 wl_list_insert(compositor->animation_list.prev,
815 &compositor->fade.animation.link);
819 surface_destroy(struct wl_client *client,
820 struct wl_surface *surface)
822 wl_resource_destroy(&surface->resource, client,
823 wlsc_compositor_get_time());
827 wlsc_surface_assign_output(struct wlsc_surface *es)
829 struct wlsc_compositor *ec = es->compositor;
830 struct wlsc_output *output;
832 struct wlsc_output *tmp = es->output;
835 wl_list_for_each(output, &ec->output_list, link) {
836 if (output->x < es->x && es->x < output->x + output->width &&
837 output->y < es->y && es->y < output->y + output->height) {
839 printf("assiging surface %p to output %p\n",
845 if (es->output == NULL) {
846 printf("no output found\n");
847 es->output = container_of(ec->output_list.next,
848 struct wlsc_output, link);
853 surface_attach(struct wl_client *client,
854 struct wl_surface *surface, struct wl_buffer *buffer,
855 int32_t x, int32_t y)
857 struct wlsc_surface *es = (struct wlsc_surface *) surface;
861 es->width = buffer->width;
862 es->height = buffer->height;
863 if (x != 0 || y != 0)
864 wlsc_surface_assign_output(es);
865 if (es->visual == NULL)
866 wl_list_insert(&es->compositor->surface_list, &es->link);
868 wlsc_buffer_attach(buffer, surface);
870 /* FIXME: This damages the entire old surface, but we should
871 * really just damage the part that's no longer covered by the
872 * surface. Anything covered by the new surface will be
873 * damaged by the client. */
874 wlsc_surface_damage(es);
876 es->compositor->shell->attach(es->compositor->shell, es);
880 surface_damage(struct wl_client *client,
881 struct wl_surface *surface,
882 int32_t x, int32_t y, int32_t width, int32_t height)
884 struct wlsc_surface *es = (struct wlsc_surface *) surface;
886 wlsc_surface_damage_rectangle(es, x, y, width, height);
889 const static struct wl_surface_interface surface_interface = {
896 wlsc_input_device_attach(struct wlsc_input_device *device,
897 int x, int y, int width, int height)
899 wlsc_surface_damage(device->sprite);
901 device->hotspot_x = x;
902 device->hotspot_y = y;
904 device->sprite->x = device->input_device.x - device->hotspot_x;
905 device->sprite->y = device->input_device.y - device->hotspot_y;
906 device->sprite->width = width;
907 device->sprite->height = height;
909 wlsc_surface_damage(device->sprite);
913 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
914 struct wl_buffer *buffer, int x, int y)
916 wlsc_buffer_attach(buffer, &device->sprite->surface);
917 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
921 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
922 struct wlsc_sprite *sprite, int x, int y)
924 wlsc_sprite_attach(sprite, &device->sprite->surface);
925 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
929 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
930 enum wlsc_pointer_type type)
932 struct wlsc_compositor *compositor =
933 (struct wlsc_compositor *) device->input_device.compositor;
935 wlsc_input_device_attach_sprite(device,
936 compositor->pointer_sprites[type],
937 pointer_images[type].hotspot_x,
938 pointer_images[type].hotspot_y);
942 compositor_create_surface(struct wl_client *client,
943 struct wl_compositor *compositor, uint32_t id)
945 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
946 struct wlsc_surface *surface;
948 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
949 if (surface == NULL) {
950 wl_client_post_no_memory(client);
954 surface->surface.resource.destroy = destroy_surface;
956 surface->surface.resource.object.id = id;
957 surface->surface.resource.object.interface = &wl_surface_interface;
958 surface->surface.resource.object.implementation =
959 (void (**)(void)) &surface_interface;
960 surface->surface.client = client;
962 wl_client_add_resource(client, &surface->surface.resource);
965 const static struct wl_compositor_interface compositor_interface = {
966 compositor_create_surface,
970 wlsc_surface_transform(struct wlsc_surface *surface,
971 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
973 *sx = x - surface->x;
974 *sy = y - surface->y;
977 WL_EXPORT struct wlsc_surface *
978 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
980 struct wlsc_compositor *ec =
981 (struct wlsc_compositor *) device->compositor;
982 struct wlsc_surface *es;
984 wl_list_for_each(es, &ec->surface_list, link) {
985 wlsc_surface_transform(es, device->x, device->y, sx, sy);
986 if (0 <= *sx && *sx < es->width &&
987 0 <= *sy && *sy < es->height)
996 motion_grab_motion(struct wl_grab *grab,
997 uint32_t time, int32_t x, int32_t y)
999 struct wlsc_input_device *device =
1000 (struct wlsc_input_device *) grab->input_device;
1001 struct wlsc_surface *es =
1002 (struct wlsc_surface *) device->input_device.pointer_focus;
1005 wlsc_surface_transform(es, x, y, &sx, &sy);
1006 wl_client_post_event(es->surface.client,
1007 &device->input_device.object,
1008 WL_INPUT_DEVICE_MOTION,
1009 time, x, y, sx, sy);
1013 motion_grab_button(struct wl_grab *grab,
1014 uint32_t time, int32_t button, int32_t state)
1016 wl_client_post_event(grab->input_device->pointer_focus->client,
1017 &grab->input_device->object,
1018 WL_INPUT_DEVICE_BUTTON,
1019 time, button, state);
1023 motion_grab_end(struct wl_grab *grab, uint32_t time)
1027 static const struct wl_grab_interface motion_grab_interface = {
1034 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1036 if (compositor->idle_inhibit)
1039 wlsc_compositor_fade(compositor, 0.0);
1040 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1042 wl_event_source_timer_update(compositor->idle_source,
1043 option_idle_time * 1000);
1047 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1049 wlsc_compositor_wake(compositor);
1050 compositor->idle_inhibit++;
1054 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1056 compositor->idle_inhibit--;
1057 wlsc_compositor_wake(compositor);
1061 idle_handler(void *data)
1063 struct wlsc_compositor *compositor = data;
1065 if (compositor->idle_inhibit)
1068 wlsc_compositor_fade(compositor, 1.0);
1074 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1076 struct wlsc_surface *es;
1077 struct wlsc_compositor *ec =
1078 (struct wlsc_compositor *) device->compositor;
1079 struct wlsc_output *output;
1080 const struct wl_grab_interface *interface;
1081 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1083 int x_valid = 0, y_valid = 0;
1084 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1086 wlsc_compositor_wake(ec);
1088 wl_list_for_each(output, &ec->output_list, link) {
1089 if (output->x <= x && x <= output->x + output->width)
1092 if (output->y <= y && y <= output->y + output->height)
1095 /* FIXME: calculate this only on output addition/deletion */
1096 if (output->x < min_x)
1098 if (output->y < min_y)
1101 if (output->x + output->width > max_x)
1102 max_x = output->x + output->width;
1103 if (output->y + output->height > max_y)
1104 max_y = output->y + output->height;
1110 else if (x >= max_x)
1116 else if (y >= max_y)
1124 interface = device->grab->interface;
1125 interface->motion(device->grab, time, x, y);
1127 es = pick_surface(device, &sx, &sy);
1128 wl_input_device_set_pointer_focus(device,
1130 time, x, y, sx, sy);
1132 wl_client_post_event(es->surface.client,
1134 WL_INPUT_DEVICE_MOTION,
1135 time, x, y, sx, sy);
1138 wlsc_surface_damage(wd->sprite);
1140 wd->sprite->x = device->x - wd->hotspot_x;
1141 wd->sprite->y = device->y - wd->hotspot_y;
1143 wlsc_surface_damage(wd->sprite);
1147 wlsc_surface_activate(struct wlsc_surface *surface,
1148 struct wlsc_input_device *device, uint32_t time)
1150 struct wlsc_shell *shell = surface->compositor->shell;
1152 wlsc_surface_raise(surface);
1153 if (device->selection)
1154 shell->set_selection_focus(shell,
1156 &surface->surface, time);
1158 wl_input_device_set_keyboard_focus(&device->input_device,
1163 struct wlsc_binding {
1167 wlsc_binding_handler_t handler;
1169 struct wl_list link;
1173 notify_button(struct wl_input_device *device,
1174 uint32_t time, int32_t button, int32_t state)
1176 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1177 struct wlsc_compositor *compositor =
1178 (struct wlsc_compositor *) device->compositor;
1179 struct wlsc_binding *b;
1180 struct wlsc_surface *surface =
1181 (struct wlsc_surface *) device->pointer_focus;
1184 wlsc_compositor_idle_inhibit(compositor);
1186 wlsc_compositor_idle_release(compositor);
1188 if (state && surface && device->grab == NULL) {
1189 wlsc_surface_activate(surface, wd, time);
1190 wl_input_device_start_grab(device,
1191 &device->motion_grab,
1195 wl_list_for_each(b, &compositor->binding_list, link) {
1196 if (b->button == button &&
1197 b->modifier == wd->modifier_state && state) {
1198 b->handler(&wd->input_device,
1199 time, 0, button, state, b->data);
1205 device->grab->interface->button(device->grab, time,
1208 if (!state && device->grab && device->grab_button == button)
1209 wl_input_device_end_grab(device, time);
1213 terminate_binding(struct wl_input_device *device, uint32_t time,
1214 uint32_t key, uint32_t button, uint32_t state, void *data)
1216 struct wlsc_compositor *compositor = data;
1219 wl_display_terminate(compositor->wl_display);
1222 WL_EXPORT struct wlsc_binding *
1223 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1224 uint32_t key, uint32_t button, uint32_t modifier,
1225 wlsc_binding_handler_t handler, void *data)
1227 struct wlsc_binding *binding;
1229 binding = malloc(sizeof *binding);
1230 if (binding == NULL)
1234 binding->button = button;
1235 binding->modifier = modifier;
1236 binding->handler = handler;
1237 binding->data = data;
1238 wl_list_insert(compositor->binding_list.prev, &binding->link);
1244 wlsc_binding_destroy(struct wlsc_binding *binding)
1246 wl_list_remove(&binding->link);
1251 update_modifier_state(struct wlsc_input_device *device,
1252 uint32_t key, uint32_t state)
1259 modifier = MODIFIER_CTRL;
1264 modifier = MODIFIER_ALT;
1269 modifier = MODIFIER_SUPER;
1278 device->modifier_state |= modifier;
1280 device->modifier_state &= ~modifier;
1284 notify_key(struct wl_input_device *device,
1285 uint32_t time, uint32_t key, uint32_t state)
1287 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1288 struct wlsc_compositor *compositor =
1289 (struct wlsc_compositor *) device->compositor;
1291 struct wlsc_binding *b;
1294 wlsc_compositor_idle_inhibit(compositor);
1296 wlsc_compositor_idle_release(compositor);
1298 wl_list_for_each(b, &compositor->binding_list, link) {
1299 if (b->key == key &&
1300 b->modifier == wd->modifier_state) {
1301 b->handler(&wd->input_device,
1302 time, key, 0, state, b->data);
1307 update_modifier_state(wd, key, state);
1308 end = device->keys.data + device->keys.size;
1309 for (k = device->keys.data; k < end; k++) {
1313 device->keys.size = (void *) end - device->keys.data;
1315 k = wl_array_add(&device->keys, sizeof *k);
1319 if (device->keyboard_focus != NULL)
1320 wl_client_post_event(device->keyboard_focus->client,
1322 WL_INPUT_DEVICE_KEY, time, key, state);
1326 notify_pointer_focus(struct wl_input_device *device,
1327 uint32_t time, struct wlsc_output *output,
1328 int32_t x, int32_t y)
1330 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1331 struct wlsc_compositor *compositor =
1332 (struct wlsc_compositor *) device->compositor;
1333 struct wlsc_surface *es;
1339 es = pick_surface(device, &sx, &sy);
1340 wl_input_device_set_pointer_focus(device,
1342 time, x, y, sx, sy);
1344 compositor->focus = 1;
1346 wd->sprite->x = device->x - wd->hotspot_x;
1347 wd->sprite->y = device->y - wd->hotspot_y;
1349 wl_input_device_set_pointer_focus(device, NULL,
1351 compositor->focus = 0;
1354 wlsc_surface_damage(wd->sprite);
1358 notify_keyboard_focus(struct wl_input_device *device,
1359 uint32_t time, struct wlsc_output *output,
1360 struct wl_array *keys)
1362 struct wlsc_input_device *wd =
1363 (struct wlsc_input_device *) device;
1364 struct wlsc_compositor *compositor =
1365 (struct wlsc_compositor *) device->compositor;
1366 struct wlsc_surface *es;
1369 if (!wl_list_empty(&compositor->surface_list))
1370 es = container_of(compositor->surface_list.next,
1371 struct wlsc_surface, link);
1376 wl_array_copy(&wd->input_device.keys, keys);
1377 wd->modifier_state = 0;
1378 end = device->keys.data + device->keys.size;
1379 for (k = device->keys.data; k < end; k++) {
1380 wlsc_compositor_idle_inhibit(compositor);
1381 update_modifier_state(wd, *k, 1);
1384 wl_input_device_set_keyboard_focus(&wd->input_device,
1385 &es->surface, time);
1387 end = device->keys.data + device->keys.size;
1388 for (k = device->keys.data; k < end; k++)
1389 wlsc_compositor_idle_release(compositor);
1391 wd->modifier_state = 0;
1392 wl_input_device_set_keyboard_focus(&wd->input_device,
1399 input_device_attach(struct wl_client *client,
1400 struct wl_input_device *device_base,
1402 struct wl_buffer *buffer, int32_t x, int32_t y)
1404 struct wlsc_input_device *device =
1405 (struct wlsc_input_device *) device_base;
1407 if (time < device->input_device.pointer_focus_time)
1409 if (device->input_device.pointer_focus == NULL)
1411 if (device->input_device.pointer_focus->client != client)
1414 if (buffer == NULL) {
1415 wlsc_input_device_set_pointer_image(device,
1416 WLSC_POINTER_LEFT_PTR);
1420 wlsc_input_device_attach_buffer(device, buffer, x, y);
1423 const static struct wl_input_device_interface input_device_interface = {
1424 input_device_attach,
1428 wlsc_input_device_init(struct wlsc_input_device *device,
1429 struct wlsc_compositor *ec)
1431 wl_input_device_init(&device->input_device, &ec->compositor);
1433 device->input_device.object.interface = &wl_input_device_interface;
1434 device->input_device.object.implementation =
1435 (void (**)(void)) &input_device_interface;
1436 wl_display_add_object(ec->wl_display, &device->input_device.object);
1437 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1439 device->sprite = wlsc_surface_create(ec,
1440 device->input_device.x,
1441 device->input_device.y, 32, 32);
1442 device->hotspot_x = 16;
1443 device->hotspot_y = 16;
1444 device->modifier_state = 0;
1446 device->input_device.motion_grab.interface = &motion_grab_interface;
1448 wl_list_insert(ec->input_device_list.prev, &device->link);
1450 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1454 wlsc_output_post_geometry(struct wl_client *client,
1455 struct wl_object *global, uint32_t version)
1457 struct wlsc_output *output =
1458 container_of(global, struct wlsc_output, object);
1460 wl_client_post_event(client, global,
1462 output->x, output->y,
1463 output->width, output->height);
1466 static const char vertex_shader[] =
1467 "uniform mat4 proj;\n"
1468 "attribute vec2 position;\n"
1469 "attribute vec2 texcoord;\n"
1470 "varying vec2 v_texcoord;\n"
1473 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1474 " v_texcoord = texcoord;\n"
1477 static const char texture_fragment_shader[] =
1478 "precision mediump float;\n"
1479 "varying vec2 v_texcoord;\n"
1480 "uniform sampler2D tex;\n"
1483 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1486 static const char solid_fragment_shader[] =
1487 "precision mediump float;\n"
1488 "uniform vec4 color;\n"
1491 " gl_FragColor = color\n;"
1495 compile_shader(GLenum type, const char *source)
1501 s = glCreateShader(type);
1502 glShaderSource(s, 1, &source, NULL);
1504 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1506 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1507 fprintf(stderr, "shader info: %s\n", msg);
1515 wlsc_shader_init(struct wlsc_shader *shader,
1516 const char *vertex_source, const char *fragment_source)
1521 shader->vertex_shader =
1522 compile_shader(GL_VERTEX_SHADER, vertex_source);
1523 shader->fragment_shader =
1524 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1526 shader->program = glCreateProgram();
1527 glAttachShader(shader->program, shader->vertex_shader);
1528 glAttachShader(shader->program, shader->fragment_shader);
1529 glBindAttribLocation(shader->program, 0, "position");
1530 glBindAttribLocation(shader->program, 1, "texcoord");
1532 glLinkProgram(shader->program);
1533 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1535 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1536 fprintf(stderr, "link info: %s\n", msg);
1540 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1541 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1547 init_solid_shader(struct wlsc_shader *shader,
1548 GLuint vertex_shader, const char *fragment_source)
1553 shader->vertex_shader = vertex_shader;
1554 shader->fragment_shader =
1555 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1557 shader->program = glCreateProgram();
1558 glAttachShader(shader->program, shader->vertex_shader);
1559 glAttachShader(shader->program, shader->fragment_shader);
1560 glBindAttribLocation(shader->program, 0, "position");
1561 glBindAttribLocation(shader->program, 1, "texcoord");
1563 glLinkProgram(shader->program);
1564 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1566 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1567 fprintf(stderr, "link info: %s\n", msg);
1571 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1572 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1578 wlsc_output_destroy(struct wlsc_output *output)
1580 destroy_surface(&output->background->surface.resource, NULL);
1584 wlsc_output_move(struct wlsc_output *output, int x, int y)
1586 struct wlsc_compositor *c = output->compositor;
1592 if (output->background) {
1593 output->background->x = x;
1594 output->background->y = y;
1597 pixman_region32_init(&output->previous_damage_region);
1599 wlsc_matrix_init(&output->matrix);
1600 wlsc_matrix_translate(&output->matrix,
1601 -output->x - output->width / 2.0,
1602 -output->y - output->height / 2.0, 0);
1604 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1605 wlsc_matrix_scale(&output->matrix,
1606 2.0 / output->width,
1607 flip * 2.0 / output->height, 1);
1609 pixman_region32_union_rect(&c->damage_region,
1611 x, y, output->width, output->height);
1615 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1616 int x, int y, int width, int height, uint32_t flags)
1618 output->compositor = c;
1621 output->width = width;
1622 output->height = height;
1624 output->background =
1625 background_create(output, option_background);
1627 output->flags = flags;
1628 output->finished = 1;
1629 wlsc_output_move(output, x, y);
1631 output->object.interface = &wl_output_interface;
1632 wl_display_add_object(c->wl_display, &output->object);
1633 wl_display_add_global(c->wl_display, &output->object,
1634 wlsc_output_post_geometry);
1638 shm_buffer_created(struct wl_buffer *buffer)
1640 struct wl_list *surfaces_attached_to;
1642 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1643 if (!surfaces_attached_to) {
1644 buffer->user_data = NULL;
1648 wl_list_init(surfaces_attached_to);
1650 buffer->user_data = surfaces_attached_to;
1654 shm_buffer_damaged(struct wl_buffer *buffer,
1655 int32_t x, int32_t y, int32_t width, int32_t height)
1657 struct wl_list *surfaces_attached_to = buffer->user_data;
1658 struct wlsc_surface *es;
1659 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1661 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1662 glBindTexture(GL_TEXTURE_2D, es->texture);
1663 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1664 tex_width, buffer->height, 0,
1665 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1666 wl_shm_buffer_get_data(buffer));
1667 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1668 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1673 shm_buffer_destroyed(struct wl_buffer *buffer)
1675 struct wl_list *surfaces_attached_to = buffer->user_data;
1676 struct wlsc_surface *es, *next;
1678 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1679 wl_list_remove(&es->buffer_link);
1680 wl_list_init(&es->buffer_link);
1683 free(surfaces_attached_to);
1686 const static struct wl_shm_callbacks shm_callbacks = {
1689 shm_buffer_destroyed
1693 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1695 struct wl_event_loop *loop;
1696 const char *extensions;
1698 ec->wl_display = display;
1700 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1702 ec->shm = wl_shm_init(display, &shm_callbacks);
1704 ec->image_target_texture_2d =
1705 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1706 ec->image_target_renderbuffer_storage = (void *)
1707 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1708 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1709 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1711 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1712 ec->unbind_display =
1713 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1715 extensions = (const char *) glGetString(GL_EXTENSIONS);
1716 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1718 "GL_EXT_texture_format_BGRA8888 not available\n");
1723 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1724 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1725 ec->has_bind_display = 1;
1726 if (ec->has_bind_display)
1727 ec->bind_display(ec->display, ec->wl_display);
1729 wl_list_init(&ec->surface_list);
1730 wl_list_init(&ec->input_device_list);
1731 wl_list_init(&ec->output_list);
1732 wl_list_init(&ec->binding_list);
1733 wl_list_init(&ec->animation_list);
1734 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
1735 ec->fade.animation.frame = fade_frame;
1736 wl_list_init(&ec->fade.animation.link);
1738 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1739 MODIFIER_CTRL | MODIFIER_ALT,
1740 terminate_binding, ec);
1742 create_pointer_images(ec);
1744 screenshooter_create(ec);
1746 glActiveTexture(GL_TEXTURE0);
1748 if (wlsc_shader_init(&ec->texture_shader,
1749 vertex_shader, texture_fragment_shader) < 0)
1751 if (init_solid_shader(&ec->solid_shader,
1752 ec->texture_shader.vertex_shader,
1753 solid_fragment_shader) < 0)
1756 loop = wl_display_get_event_loop(ec->wl_display);
1757 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1758 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1760 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1761 pixman_region32_init(&ec->damage_region);
1762 wlsc_compositor_schedule_repaint(ec);
1767 static int on_term_signal(int signal_number, void *data)
1769 struct wlsc_compositor *ec = data;
1771 wl_display_terminate(ec->wl_display);
1777 load_module(const char *name, const char *entrypoint, void **handle)
1779 char path[PATH_MAX];
1780 void *module, *init;
1783 snprintf(path, sizeof path, MODULEDIR "/%s", name);
1785 snprintf(path, sizeof path, "%s", name);
1787 module = dlopen(path, RTLD_LAZY);
1790 "failed to load module: %s\n", dlerror());
1794 init = dlsym(module, entrypoint);
1797 "failed to lookup init function: %s\n", dlerror());
1804 int main(int argc, char *argv[])
1806 struct wl_display *display;
1807 struct wlsc_compositor *ec;
1808 struct wl_event_loop *loop;
1810 void *shell_module, *backend_module;
1811 int (*shell_init)(struct wlsc_compositor *ec);
1812 struct wlsc_compositor
1813 *(*backend_init)(struct wl_display *display, char *options);
1814 char *backend = NULL;
1815 char *backend_options = "";
1819 static const char opts[] = "B:b:o:S:i:s:";
1820 static const struct option longopts[ ] = {
1821 { "backend", 1, NULL, 'B' },
1822 { "backend-options", 1, NULL, 'o' },
1823 { "background", 1, NULL, 'b' },
1824 { "socket", 1, NULL, 'S' },
1825 { "idle-time", 1, NULL, 'i' },
1826 { "shell", 1, NULL, 's' },
1830 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1833 option_background = optarg;
1839 backend_options = optarg;
1842 option_socket_name = optarg;
1845 option_idle_time = strtol(optarg, &p, 0);
1848 "invalid idle time option: %s\n",
1859 display = wl_display_create();
1864 if (getenv("WAYLAND_DISPLAY"))
1865 backend = "wayland-backend.so";
1866 else if (getenv("DISPLAY"))
1867 backend = "x11-backend.so";
1868 else if (getenv("OPENWFD"))
1869 backend = "openwfd-backend.so";
1871 backend = "drm-backend.so";
1875 shell = "desktop-shell.so";
1877 backend_init = load_module(backend, "backend_init", &backend_module);
1881 shell_init = load_module(shell, "shell_init", &shell_module);
1885 ec = backend_init(display, backend_options);
1887 fprintf(stderr, "failed to create compositor\n");
1891 if (shell_init(ec) < 0)
1894 if (wl_display_add_socket(display, option_socket_name)) {
1895 fprintf(stderr, "failed to add socket: %m\n");
1899 loop = wl_display_get_event_loop(ec->wl_display);
1900 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1901 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1903 wl_display_run(display);
1905 if (ec->has_bind_display)
1906 ec->unbind_display(ec->display, display);
1907 wl_display_destroy(display);