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);
184 surface->compositor = compositor;
185 surface->visual = NULL;
186 surface->image = EGL_NO_IMAGE_KHR;
187 surface->saved_texture = 0;
190 surface->width = width;
191 surface->height = height;
193 surface->transform = NULL;
199 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
200 int32_t x, int32_t y,
201 int32_t width, int32_t height)
203 struct wlsc_compositor *compositor = surface->compositor;
205 pixman_region32_union_rect(&compositor->damage_region,
206 &compositor->damage_region,
207 surface->x + x, surface->y + y,
209 wlsc_compositor_schedule_repaint(compositor);
213 wlsc_surface_damage(struct wlsc_surface *surface)
215 wlsc_surface_damage_rectangle(surface, 0, 0,
216 surface->width, surface->height);
220 wlsc_compositor_get_time(void)
224 gettimeofday(&tv, NULL);
226 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
230 destroy_surface(struct wl_resource *resource, struct wl_client *client)
232 struct wlsc_surface *surface =
233 container_of(resource, struct wlsc_surface, surface.resource);
234 struct wlsc_compositor *compositor = surface->compositor;
236 wlsc_surface_damage(surface);
238 wl_list_remove(&surface->link);
239 if (surface->saved_texture == 0)
240 glDeleteTextures(1, &surface->texture);
242 glDeleteTextures(1, &surface->saved_texture);
245 if (surface->image != EGL_NO_IMAGE_KHR)
246 compositor->destroy_image(compositor->display,
249 wl_list_remove(&surface->buffer_link);
255 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
257 struct wlsc_surface *es = (struct wlsc_surface *) surface;
258 struct wlsc_compositor *ec = es->compositor;
259 struct wl_list *surfaces_attached_to;
261 if (es->saved_texture != 0)
262 es->texture = es->saved_texture;
264 glBindTexture(GL_TEXTURE_2D, es->texture);
266 if (wl_buffer_is_shm(buffer)) {
267 /* Unbind any EGLImage texture that may be bound, so we don't
269 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
270 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
271 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
272 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
273 es->pitch, buffer->height, 0,
274 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
275 wl_shm_buffer_get_data(buffer));
276 es->visual = buffer->visual;
278 surfaces_attached_to = buffer->user_data;
280 wl_list_remove(&es->buffer_link);
281 wl_list_insert(surfaces_attached_to, &es->buffer_link);
283 if (es->image != EGL_NO_IMAGE_KHR)
284 ec->destroy_image(ec->display, es->image);
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->current->width,
408 output->current->height);
409 if (background == NULL)
412 sprite = create_sprite_from_png(output->compositor, filename, 0);
413 if (sprite == NULL) {
418 wlsc_sprite_attach(sprite, &background->surface);
424 texture_region(struct wlsc_surface *es, pixman_region32_t *region)
426 struct wlsc_compositor *ec = es->compositor;
427 GLfloat *v, inv_width, inv_height;
428 pixman_box32_t *rectangles;
432 rectangles = pixman_region32_rectangles(region, &n);
433 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
434 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
435 inv_width = 1.0 / es->pitch;
436 inv_height = 1.0 / es->height;
438 for (i = 0; i < n; i++, v += 16, p += 6) {
439 v[ 0] = rectangles[i].x1;
440 v[ 1] = rectangles[i].y1;
441 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
442 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
444 v[ 4] = rectangles[i].x1;
445 v[ 5] = rectangles[i].y2;
447 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
449 v[ 8] = rectangles[i].x2;
450 v[ 9] = rectangles[i].y1;
451 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
454 v[12] = rectangles[i].x2;
455 v[13] = rectangles[i].y2;
471 transform_vertex(struct wlsc_surface *surface,
472 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
474 struct wlsc_vector t;
481 wlsc_matrix_transform(&surface->transform->matrix, &t);
490 texture_transformed_surface(struct wlsc_surface *es)
492 struct wlsc_compositor *ec = es->compositor;
496 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
497 p = wl_array_add(&ec->indices, 6 * sizeof *p);
499 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
500 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
501 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
502 transform_vertex(es, es->x + es->width, es->y + es->height,
516 wlsc_surface_draw(struct wlsc_surface *es,
517 struct wlsc_output *output, pixman_region32_t *clip)
519 struct wlsc_compositor *ec = es->compositor;
521 pixman_region32_t repaint;
525 pixman_region32_init_rect(&repaint,
526 es->x, es->y, es->width, es->height);
527 pixman_region32_intersect(&repaint, &repaint, clip);
528 if (!pixman_region32_not_empty(&repaint))
531 if (es->visual == &ec->compositor.argb_visual) {
532 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
534 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
535 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
541 if (es->transform == NULL) {
543 n = texture_region(es, &repaint);
546 n = texture_transformed_surface(es);
549 glBindTexture(GL_TEXTURE_2D, es->texture);
550 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
551 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
553 v = ec->vertices.data;
554 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
555 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
556 glEnableVertexAttribArray(0);
557 glEnableVertexAttribArray(1);
558 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
560 ec->vertices.size = 0;
561 ec->indices.size = 0;
562 pixman_region32_fini(&repaint);
566 wlsc_surface_raise(struct wlsc_surface *surface)
568 struct wlsc_compositor *compositor = surface->compositor;
570 wl_list_remove(&surface->link);
571 wl_list_insert(&compositor->surface_list, &surface->link);
575 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
577 struct wlsc_output *output;
579 wl_list_for_each(output, &compositor->output_list, link)
580 wlsc_output_damage(output);
584 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
586 struct wlsc_compositor *compositor = output->compositor;
587 struct wlsc_surface *es;
588 struct wlsc_animation *animation, *next;
590 wl_list_for_each(es, &compositor->surface_list, link) {
591 if (es->output == output) {
592 wl_display_post_frame(compositor->wl_display,
593 &es->surface, msecs);
597 output->finished = 1;
599 wl_event_source_timer_update(compositor->timer_source, 5);
600 compositor->repaint_on_timeout = 1;
602 wl_list_for_each_safe(animation, next,
603 &compositor->animation_list, link)
604 animation->frame(animation, output, msecs);
608 wlsc_output_damage(struct wlsc_output *output)
610 struct wlsc_compositor *compositor = output->compositor;
612 pixman_region32_union_rect(&compositor->damage_region,
613 &compositor->damage_region,
614 output->x, output->y,
615 output->current->width,
616 output->current->height);
617 wlsc_compositor_schedule_repaint(compositor);
621 fade_frame(struct wlsc_animation *animation,
622 struct wlsc_output *output, uint32_t msecs)
624 struct wlsc_compositor *compositor =
625 container_of(animation,
626 struct wlsc_compositor, fade.animation);
628 wlsc_spring_update(&compositor->fade.spring, msecs);
629 if (wlsc_spring_done(&compositor->fade.spring)) {
630 if (compositor->fade.spring.current > 0.999) {
631 compositor->state = WLSC_COMPOSITOR_SLEEPING;
632 compositor->shell->lock(compositor->shell);
634 compositor->fade.spring.current =
635 compositor->fade.spring.target;
636 wl_list_remove(&animation->link);
637 wl_list_init(&animation->link);
640 wlsc_output_damage(output);
644 fade_output(struct wlsc_output *output,
645 GLfloat tint, pixman_region32_t *region)
647 struct wlsc_compositor *compositor = output->compositor;
648 struct wlsc_surface surface;
649 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
651 surface.compositor = compositor;
652 surface.x = output->x;
653 surface.y = output->y;
654 surface.width = output->current->width;
655 surface.height = output->current->height;
656 surface.texture = GL_NONE;
657 surface.transform = NULL;
661 &compositor->compositor.premultiplied_argb_visual;
663 surface.visual = &compositor->compositor.rgb_visual;
665 glUseProgram(compositor->solid_shader.program);
666 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
667 1, GL_FALSE, output->matrix.d);
668 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
669 wlsc_surface_draw(&surface, output, region);
673 wlsc_output_repaint(struct wlsc_output *output)
675 struct wlsc_compositor *ec = output->compositor;
676 struct wlsc_surface *es;
677 struct wlsc_input_device *eid, *hw_cursor;
678 pixman_region32_t new_damage, total_damage;
680 output->prepare_render(output);
682 glViewport(0, 0, output->current->width, output->current->height);
684 glUseProgram(ec->texture_shader.program);
685 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
686 1, GL_FALSE, output->matrix.d);
687 glUniform1i(ec->texture_shader.tex_uniform, 0);
689 pixman_region32_init(&new_damage);
690 pixman_region32_init(&total_damage);
691 pixman_region32_intersect_rect(&new_damage,
693 output->x, output->y,
694 output->current->width,
695 output->current->height);
696 pixman_region32_subtract(&ec->damage_region,
697 &ec->damage_region, &new_damage);
698 pixman_region32_union(&total_damage, &new_damage,
699 &output->previous_damage_region);
700 pixman_region32_copy(&output->previous_damage_region, &new_damage);
703 if (ec->focus && ec->fade.spring.current < 0.001) {
704 hw_cursor = (struct wlsc_input_device *) ec->input_device;
705 if (output->set_hardware_cursor(output, hw_cursor) < 0)
708 output->set_hardware_cursor(output, NULL);
711 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
713 if (es->visual == &ec->compositor.rgb_visual && hw_cursor) {
714 if (output->prepare_scanout_surface(output, es) == 0) {
715 /* We're drawing nothing now,
716 * draw the damaged regions later. */
717 pixman_region32_union(&ec->damage_region,
724 if (es->fullscreen_output == output) {
725 if (es->width < output->current->width ||
726 es->height < output->current->height)
727 glClear(GL_COLOR_BUFFER_BIT);
728 wlsc_surface_draw(es, output, &total_damage);
730 if (output->background)
731 wlsc_surface_draw(output->background,
732 output, &total_damage);
734 fade_output(output, 1.0, &total_damage);
736 glUseProgram(ec->texture_shader.program);
737 wl_list_for_each_reverse(es, &ec->surface_list, link) {
738 if (ec->overlay == es)
741 wlsc_surface_draw(es, output, &total_damage);
746 wlsc_surface_draw(ec->overlay, output, &total_damage);
749 wl_list_for_each(eid, &ec->input_device_list, link) {
750 if (&eid->input_device != ec->input_device ||
752 wlsc_surface_draw(eid->sprite, output,
756 if (ec->fade.spring.current > 0.001)
757 fade_output(output, ec->fade.spring.current, &total_damage);
763 struct wlsc_compositor *ec = data;
764 struct wlsc_output *output;
765 int repainted_all_outputs = 1;
767 wl_list_for_each(output, &ec->output_list, link) {
768 if (!output->repaint_needed)
771 if (!output->finished) {
772 repainted_all_outputs = 0;
776 wlsc_output_repaint(output);
777 output->finished = 0;
778 output->repaint_needed = 0;
779 output->present(output);
782 if (repainted_all_outputs)
783 ec->repaint_on_timeout = 0;
785 wl_event_source_timer_update(ec->timer_source, 1);
791 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
793 struct wlsc_output *output;
795 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
798 wl_list_for_each(output, &compositor->output_list, link)
799 output->repaint_needed = 1;
801 if (compositor->repaint_on_timeout)
804 wl_event_source_timer_update(compositor->timer_source, 1);
805 compositor->repaint_on_timeout = 1;
809 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
813 done = wlsc_spring_done(&compositor->fade.spring);
814 compositor->fade.spring.target = tint;
815 if (wlsc_spring_done(&compositor->fade.spring))
819 compositor->fade.spring.timestamp =
820 wlsc_compositor_get_time();
822 wlsc_compositor_damage_all(compositor);
823 if (wl_list_empty(&compositor->fade.animation.link))
824 wl_list_insert(compositor->animation_list.prev,
825 &compositor->fade.animation.link);
829 surface_destroy(struct wl_client *client,
830 struct wl_surface *surface)
832 wl_resource_destroy(&surface->resource, client,
833 wlsc_compositor_get_time());
837 wlsc_surface_assign_output(struct wlsc_surface *es)
839 struct wlsc_compositor *ec = es->compositor;
840 struct wlsc_output *output;
842 struct wlsc_output *tmp = es->output;
845 wl_list_for_each(output, &ec->output_list, link) {
846 if (output->x < es->x && es->x < output->x + output->current->width &&
847 output->y < es->y && es->y < output->y + output->current->height) {
849 printf("assiging surface %p to output %p\n",
855 if (es->output == NULL) {
856 printf("no output found\n");
857 es->output = container_of(ec->output_list.next,
858 struct wlsc_output, link);
863 surface_attach(struct wl_client *client,
864 struct wl_surface *surface, struct wl_buffer *buffer,
865 int32_t x, int32_t y)
867 struct wlsc_surface *es = (struct wlsc_surface *) surface;
871 es->width = buffer->width;
872 es->height = buffer->height;
873 if (x != 0 || y != 0)
874 wlsc_surface_assign_output(es);
875 if (es->visual == NULL)
876 wl_list_insert(&es->compositor->surface_list, &es->link);
878 wlsc_buffer_attach(buffer, surface);
880 /* FIXME: This damages the entire old surface, but we should
881 * really just damage the part that's no longer covered by the
882 * surface. Anything covered by the new surface will be
883 * damaged by the client. */
884 wlsc_surface_damage(es);
886 es->compositor->shell->attach(es->compositor->shell, es);
890 surface_damage(struct wl_client *client,
891 struct wl_surface *surface,
892 int32_t x, int32_t y, int32_t width, int32_t height)
894 struct wlsc_surface *es = (struct wlsc_surface *) surface;
896 wlsc_surface_damage_rectangle(es, x, y, width, height);
899 const static struct wl_surface_interface surface_interface = {
906 wlsc_input_device_attach(struct wlsc_input_device *device,
907 int x, int y, int width, int height)
909 wlsc_surface_damage(device->sprite);
911 device->hotspot_x = x;
912 device->hotspot_y = y;
914 device->sprite->x = device->input_device.x - device->hotspot_x;
915 device->sprite->y = device->input_device.y - device->hotspot_y;
916 device->sprite->width = width;
917 device->sprite->height = height;
919 wlsc_surface_damage(device->sprite);
923 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
924 struct wl_buffer *buffer, int x, int y)
926 wlsc_buffer_attach(buffer, &device->sprite->surface);
927 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
931 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
932 struct wlsc_sprite *sprite, int x, int y)
934 wlsc_sprite_attach(sprite, &device->sprite->surface);
935 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
939 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
940 enum wlsc_pointer_type type)
942 struct wlsc_compositor *compositor =
943 (struct wlsc_compositor *) device->input_device.compositor;
945 wlsc_input_device_attach_sprite(device,
946 compositor->pointer_sprites[type],
947 pointer_images[type].hotspot_x,
948 pointer_images[type].hotspot_y);
952 compositor_create_surface(struct wl_client *client,
953 struct wl_compositor *compositor, uint32_t id)
955 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
956 struct wlsc_surface *surface;
958 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
959 if (surface == NULL) {
960 wl_client_post_no_memory(client);
964 surface->surface.resource.destroy = destroy_surface;
966 surface->surface.resource.object.id = id;
967 surface->surface.resource.object.interface = &wl_surface_interface;
968 surface->surface.resource.object.implementation =
969 (void (**)(void)) &surface_interface;
970 surface->surface.client = client;
972 wl_client_add_resource(client, &surface->surface.resource);
975 const static struct wl_compositor_interface compositor_interface = {
976 compositor_create_surface,
980 wlsc_surface_transform(struct wlsc_surface *surface,
981 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
983 *sx = x - surface->x;
984 *sy = y - surface->y;
987 WL_EXPORT struct wlsc_surface *
988 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
990 struct wlsc_compositor *ec =
991 (struct wlsc_compositor *) device->compositor;
992 struct wlsc_surface *es;
994 wl_list_for_each(es, &ec->surface_list, link) {
995 wlsc_surface_transform(es, device->x, device->y, sx, sy);
996 if (0 <= *sx && *sx < es->width &&
997 0 <= *sy && *sy < es->height)
1006 motion_grab_motion(struct wl_grab *grab,
1007 uint32_t time, int32_t x, int32_t y)
1009 struct wlsc_input_device *device =
1010 (struct wlsc_input_device *) grab->input_device;
1011 struct wlsc_surface *es =
1012 (struct wlsc_surface *) device->input_device.pointer_focus;
1015 wlsc_surface_transform(es, x, y, &sx, &sy);
1016 wl_client_post_event(es->surface.client,
1017 &device->input_device.object,
1018 WL_INPUT_DEVICE_MOTION,
1019 time, x, y, sx, sy);
1023 motion_grab_button(struct wl_grab *grab,
1024 uint32_t time, int32_t button, int32_t state)
1026 wl_client_post_event(grab->input_device->pointer_focus->client,
1027 &grab->input_device->object,
1028 WL_INPUT_DEVICE_BUTTON,
1029 time, button, state);
1033 motion_grab_end(struct wl_grab *grab, uint32_t time)
1037 static const struct wl_grab_interface motion_grab_interface = {
1044 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1046 if (compositor->idle_inhibit)
1049 wlsc_compositor_fade(compositor, 0.0);
1050 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1052 wl_event_source_timer_update(compositor->idle_source,
1053 option_idle_time * 1000);
1057 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1059 wlsc_compositor_wake(compositor);
1060 compositor->idle_inhibit++;
1064 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1066 compositor->idle_inhibit--;
1067 wlsc_compositor_wake(compositor);
1071 idle_handler(void *data)
1073 struct wlsc_compositor *compositor = data;
1075 if (compositor->idle_inhibit)
1078 wlsc_compositor_fade(compositor, 1.0);
1084 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1086 struct wlsc_surface *es;
1087 struct wlsc_compositor *ec =
1088 (struct wlsc_compositor *) device->compositor;
1089 struct wlsc_output *output;
1090 const struct wl_grab_interface *interface;
1091 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1093 int x_valid = 0, y_valid = 0;
1094 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1096 wlsc_compositor_wake(ec);
1098 wl_list_for_each(output, &ec->output_list, link) {
1099 if (output->x <= x && x <= output->x + output->current->width)
1102 if (output->y <= y && y <= output->y + output->current->height)
1105 /* FIXME: calculate this only on output addition/deletion */
1106 if (output->x < min_x)
1108 if (output->y < min_y)
1111 if (output->x + output->current->width > max_x)
1112 max_x = output->x + output->current->width;
1113 if (output->y + output->current->height > max_y)
1114 max_y = output->y + output->current->height;
1120 else if (x >= max_x)
1126 else if (y >= max_y)
1134 interface = device->grab->interface;
1135 interface->motion(device->grab, time, x, y);
1137 es = pick_surface(device, &sx, &sy);
1138 wl_input_device_set_pointer_focus(device,
1140 time, x, y, sx, sy);
1142 wl_client_post_event(es->surface.client,
1144 WL_INPUT_DEVICE_MOTION,
1145 time, x, y, sx, sy);
1148 wlsc_surface_damage(wd->sprite);
1150 wd->sprite->x = device->x - wd->hotspot_x;
1151 wd->sprite->y = device->y - wd->hotspot_y;
1153 wlsc_surface_damage(wd->sprite);
1157 wlsc_surface_activate(struct wlsc_surface *surface,
1158 struct wlsc_input_device *device, uint32_t time)
1160 struct wlsc_shell *shell = surface->compositor->shell;
1162 wlsc_surface_raise(surface);
1163 if (device->selection)
1164 shell->set_selection_focus(shell,
1166 &surface->surface, time);
1168 wl_input_device_set_keyboard_focus(&device->input_device,
1173 struct wlsc_binding {
1177 wlsc_binding_handler_t handler;
1179 struct wl_list link;
1183 notify_button(struct wl_input_device *device,
1184 uint32_t time, int32_t button, int32_t state)
1186 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1187 struct wlsc_compositor *compositor =
1188 (struct wlsc_compositor *) device->compositor;
1189 struct wlsc_binding *b;
1190 struct wlsc_surface *surface =
1191 (struct wlsc_surface *) device->pointer_focus;
1194 wlsc_compositor_idle_inhibit(compositor);
1196 wlsc_compositor_idle_release(compositor);
1198 if (state && surface && device->grab == NULL) {
1199 wlsc_surface_activate(surface, wd, time);
1200 wl_input_device_start_grab(device,
1201 &device->motion_grab,
1205 wl_list_for_each(b, &compositor->binding_list, link) {
1206 if (b->button == button &&
1207 b->modifier == wd->modifier_state && state) {
1208 b->handler(&wd->input_device,
1209 time, 0, button, state, b->data);
1215 device->grab->interface->button(device->grab, time,
1218 if (!state && device->grab && device->grab_button == button)
1219 wl_input_device_end_grab(device, time);
1223 terminate_binding(struct wl_input_device *device, uint32_t time,
1224 uint32_t key, uint32_t button, uint32_t state, void *data)
1226 struct wlsc_compositor *compositor = data;
1229 wl_display_terminate(compositor->wl_display);
1232 WL_EXPORT struct wlsc_binding *
1233 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1234 uint32_t key, uint32_t button, uint32_t modifier,
1235 wlsc_binding_handler_t handler, void *data)
1237 struct wlsc_binding *binding;
1239 binding = malloc(sizeof *binding);
1240 if (binding == NULL)
1244 binding->button = button;
1245 binding->modifier = modifier;
1246 binding->handler = handler;
1247 binding->data = data;
1248 wl_list_insert(compositor->binding_list.prev, &binding->link);
1254 wlsc_binding_destroy(struct wlsc_binding *binding)
1256 wl_list_remove(&binding->link);
1261 update_modifier_state(struct wlsc_input_device *device,
1262 uint32_t key, uint32_t state)
1269 modifier = MODIFIER_CTRL;
1274 modifier = MODIFIER_ALT;
1279 modifier = MODIFIER_SUPER;
1288 device->modifier_state |= modifier;
1290 device->modifier_state &= ~modifier;
1294 notify_key(struct wl_input_device *device,
1295 uint32_t time, uint32_t key, uint32_t state)
1297 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1298 struct wlsc_compositor *compositor =
1299 (struct wlsc_compositor *) device->compositor;
1301 struct wlsc_binding *b;
1304 wlsc_compositor_idle_inhibit(compositor);
1306 wlsc_compositor_idle_release(compositor);
1308 wl_list_for_each(b, &compositor->binding_list, link) {
1309 if (b->key == key &&
1310 b->modifier == wd->modifier_state) {
1311 b->handler(&wd->input_device,
1312 time, key, 0, state, b->data);
1317 update_modifier_state(wd, key, state);
1318 end = device->keys.data + device->keys.size;
1319 for (k = device->keys.data; k < end; k++) {
1323 device->keys.size = (void *) end - device->keys.data;
1325 k = wl_array_add(&device->keys, sizeof *k);
1329 if (device->keyboard_focus != NULL)
1330 wl_client_post_event(device->keyboard_focus->client,
1332 WL_INPUT_DEVICE_KEY, time, key, state);
1336 notify_pointer_focus(struct wl_input_device *device,
1337 uint32_t time, struct wlsc_output *output,
1338 int32_t x, int32_t y)
1340 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1341 struct wlsc_compositor *compositor =
1342 (struct wlsc_compositor *) device->compositor;
1343 struct wlsc_surface *es;
1349 es = pick_surface(device, &sx, &sy);
1350 wl_input_device_set_pointer_focus(device,
1352 time, x, y, sx, sy);
1354 compositor->focus = 1;
1356 wd->sprite->x = device->x - wd->hotspot_x;
1357 wd->sprite->y = device->y - wd->hotspot_y;
1359 wl_input_device_set_pointer_focus(device, NULL,
1361 compositor->focus = 0;
1364 wlsc_surface_damage(wd->sprite);
1368 notify_keyboard_focus(struct wl_input_device *device,
1369 uint32_t time, struct wlsc_output *output,
1370 struct wl_array *keys)
1372 struct wlsc_input_device *wd =
1373 (struct wlsc_input_device *) device;
1374 struct wlsc_compositor *compositor =
1375 (struct wlsc_compositor *) device->compositor;
1376 struct wlsc_surface *es;
1379 if (!wl_list_empty(&compositor->surface_list))
1380 es = container_of(compositor->surface_list.next,
1381 struct wlsc_surface, link);
1386 wl_array_copy(&wd->input_device.keys, keys);
1387 wd->modifier_state = 0;
1388 end = device->keys.data + device->keys.size;
1389 for (k = device->keys.data; k < end; k++) {
1390 wlsc_compositor_idle_inhibit(compositor);
1391 update_modifier_state(wd, *k, 1);
1394 wl_input_device_set_keyboard_focus(&wd->input_device,
1395 &es->surface, time);
1397 end = device->keys.data + device->keys.size;
1398 for (k = device->keys.data; k < end; k++)
1399 wlsc_compositor_idle_release(compositor);
1401 wd->modifier_state = 0;
1402 wl_input_device_set_keyboard_focus(&wd->input_device,
1409 input_device_attach(struct wl_client *client,
1410 struct wl_input_device *device_base,
1412 struct wl_buffer *buffer, int32_t x, int32_t y)
1414 struct wlsc_input_device *device =
1415 (struct wlsc_input_device *) device_base;
1417 if (time < device->input_device.pointer_focus_time)
1419 if (device->input_device.pointer_focus == NULL)
1421 if (device->input_device.pointer_focus->client != client)
1424 if (buffer == NULL) {
1425 wlsc_input_device_set_pointer_image(device,
1426 WLSC_POINTER_LEFT_PTR);
1430 wlsc_input_device_attach_buffer(device, buffer, x, y);
1433 const static struct wl_input_device_interface input_device_interface = {
1434 input_device_attach,
1438 wlsc_input_device_init(struct wlsc_input_device *device,
1439 struct wlsc_compositor *ec)
1441 wl_input_device_init(&device->input_device, &ec->compositor);
1443 device->input_device.object.interface = &wl_input_device_interface;
1444 device->input_device.object.implementation =
1445 (void (**)(void)) &input_device_interface;
1446 wl_display_add_object(ec->wl_display, &device->input_device.object);
1447 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1449 device->sprite = wlsc_surface_create(ec,
1450 device->input_device.x,
1451 device->input_device.y, 32, 32);
1452 device->hotspot_x = 16;
1453 device->hotspot_y = 16;
1454 device->modifier_state = 0;
1456 device->input_device.motion_grab.interface = &motion_grab_interface;
1458 wl_list_insert(ec->input_device_list.prev, &device->link);
1460 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1464 wlsc_output_post_geometry(struct wl_client *client,
1465 struct wl_object *global, uint32_t version)
1467 struct wlsc_output *output =
1468 container_of(global, struct wlsc_output, object);
1469 struct wlsc_mode *mode;
1471 wl_client_post_event(client, global,
1478 output->make, output->model);
1480 wl_list_for_each (mode, &output->mode_list, link) {
1481 wl_client_post_event(client, global,
1484 mode->width, mode->height, mode->refresh);
1488 static const char vertex_shader[] =
1489 "uniform mat4 proj;\n"
1490 "attribute vec2 position;\n"
1491 "attribute vec2 texcoord;\n"
1492 "varying vec2 v_texcoord;\n"
1495 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1496 " v_texcoord = texcoord;\n"
1499 static const char texture_fragment_shader[] =
1500 "precision mediump float;\n"
1501 "varying vec2 v_texcoord;\n"
1502 "uniform sampler2D tex;\n"
1505 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1508 static const char solid_fragment_shader[] =
1509 "precision mediump float;\n"
1510 "uniform vec4 color;\n"
1513 " gl_FragColor = color\n;"
1517 compile_shader(GLenum type, const char *source)
1523 s = glCreateShader(type);
1524 glShaderSource(s, 1, &source, NULL);
1526 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1528 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1529 fprintf(stderr, "shader info: %s\n", msg);
1537 wlsc_shader_init(struct wlsc_shader *shader,
1538 const char *vertex_source, const char *fragment_source)
1543 shader->vertex_shader =
1544 compile_shader(GL_VERTEX_SHADER, vertex_source);
1545 shader->fragment_shader =
1546 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1548 shader->program = glCreateProgram();
1549 glAttachShader(shader->program, shader->vertex_shader);
1550 glAttachShader(shader->program, shader->fragment_shader);
1551 glBindAttribLocation(shader->program, 0, "position");
1552 glBindAttribLocation(shader->program, 1, "texcoord");
1554 glLinkProgram(shader->program);
1555 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1557 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1558 fprintf(stderr, "link info: %s\n", msg);
1562 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1563 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1569 init_solid_shader(struct wlsc_shader *shader,
1570 GLuint vertex_shader, const char *fragment_source)
1575 shader->vertex_shader = vertex_shader;
1576 shader->fragment_shader =
1577 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1579 shader->program = glCreateProgram();
1580 glAttachShader(shader->program, shader->vertex_shader);
1581 glAttachShader(shader->program, shader->fragment_shader);
1582 glBindAttribLocation(shader->program, 0, "position");
1583 glBindAttribLocation(shader->program, 1, "texcoord");
1585 glLinkProgram(shader->program);
1586 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1588 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1589 fprintf(stderr, "link info: %s\n", msg);
1593 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1594 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1600 wlsc_output_destroy(struct wlsc_output *output)
1602 destroy_surface(&output->background->surface.resource, NULL);
1606 wlsc_output_move(struct wlsc_output *output, int x, int y)
1608 struct wlsc_compositor *c = output->compositor;
1614 if (output->background) {
1615 output->background->x = x;
1616 output->background->y = y;
1619 pixman_region32_init(&output->previous_damage_region);
1621 wlsc_matrix_init(&output->matrix);
1622 wlsc_matrix_translate(&output->matrix,
1623 -output->x - output->current->width / 2.0,
1624 -output->y - output->current->height / 2.0, 0);
1626 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1627 wlsc_matrix_scale(&output->matrix,
1628 2.0 / output->current->width,
1629 flip * 2.0 / output->current->height, 1);
1631 pixman_region32_union_rect(&c->damage_region,
1634 output->current->width,
1635 output->current->height);
1639 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1640 int x, int y, int width, int height, uint32_t flags)
1642 output->compositor = c;
1645 output->mm_width = width;
1646 output->mm_height = height;
1648 output->background =
1649 background_create(output, option_background);
1651 output->flags = flags;
1652 output->finished = 1;
1653 wlsc_output_move(output, x, y);
1655 output->object.interface = &wl_output_interface;
1656 wl_display_add_object(c->wl_display, &output->object);
1657 wl_display_add_global(c->wl_display, &output->object,
1658 wlsc_output_post_geometry);
1662 shm_buffer_created(struct wl_buffer *buffer)
1664 struct wl_list *surfaces_attached_to;
1666 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1667 if (!surfaces_attached_to) {
1668 buffer->user_data = NULL;
1672 wl_list_init(surfaces_attached_to);
1674 buffer->user_data = surfaces_attached_to;
1678 shm_buffer_damaged(struct wl_buffer *buffer,
1679 int32_t x, int32_t y, int32_t width, int32_t height)
1681 struct wl_list *surfaces_attached_to = buffer->user_data;
1682 struct wlsc_surface *es;
1683 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1685 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1686 glBindTexture(GL_TEXTURE_2D, es->texture);
1687 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1688 tex_width, buffer->height, 0,
1689 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1690 wl_shm_buffer_get_data(buffer));
1691 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1692 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1697 shm_buffer_destroyed(struct wl_buffer *buffer)
1699 struct wl_list *surfaces_attached_to = buffer->user_data;
1700 struct wlsc_surface *es, *next;
1702 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1703 wl_list_remove(&es->buffer_link);
1704 wl_list_init(&es->buffer_link);
1707 free(surfaces_attached_to);
1710 const static struct wl_shm_callbacks shm_callbacks = {
1713 shm_buffer_destroyed
1717 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1719 struct wl_event_loop *loop;
1720 const char *extensions;
1722 ec->wl_display = display;
1724 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1726 ec->shm = wl_shm_init(display, &shm_callbacks);
1728 ec->image_target_texture_2d =
1729 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1730 ec->image_target_renderbuffer_storage = (void *)
1731 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1732 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1733 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1735 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1736 ec->unbind_display =
1737 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1739 extensions = (const char *) glGetString(GL_EXTENSIONS);
1740 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1742 "GL_EXT_texture_format_BGRA8888 not available\n");
1747 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1748 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1749 ec->has_bind_display = 1;
1750 if (ec->has_bind_display)
1751 ec->bind_display(ec->display, ec->wl_display);
1753 wl_list_init(&ec->surface_list);
1754 wl_list_init(&ec->input_device_list);
1755 wl_list_init(&ec->output_list);
1756 wl_list_init(&ec->binding_list);
1757 wl_list_init(&ec->animation_list);
1758 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
1759 ec->fade.animation.frame = fade_frame;
1760 wl_list_init(&ec->fade.animation.link);
1762 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1763 MODIFIER_CTRL | MODIFIER_ALT,
1764 terminate_binding, ec);
1766 create_pointer_images(ec);
1768 screenshooter_create(ec);
1770 glActiveTexture(GL_TEXTURE0);
1772 if (wlsc_shader_init(&ec->texture_shader,
1773 vertex_shader, texture_fragment_shader) < 0)
1775 if (init_solid_shader(&ec->solid_shader,
1776 ec->texture_shader.vertex_shader,
1777 solid_fragment_shader) < 0)
1780 loop = wl_display_get_event_loop(ec->wl_display);
1781 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1782 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1784 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1785 pixman_region32_init(&ec->damage_region);
1786 wlsc_compositor_schedule_repaint(ec);
1791 static int on_term_signal(int signal_number, void *data)
1793 struct wlsc_compositor *ec = data;
1795 wl_display_terminate(ec->wl_display);
1801 load_module(const char *name, const char *entrypoint, void **handle)
1803 char path[PATH_MAX];
1804 void *module, *init;
1807 snprintf(path, sizeof path, MODULEDIR "/%s", name);
1809 snprintf(path, sizeof path, "%s", name);
1811 module = dlopen(path, RTLD_LAZY);
1814 "failed to load module: %s\n", dlerror());
1818 init = dlsym(module, entrypoint);
1821 "failed to lookup init function: %s\n", dlerror());
1828 int main(int argc, char *argv[])
1830 struct wl_display *display;
1831 struct wlsc_compositor *ec;
1832 struct wl_event_loop *loop;
1834 void *shell_module, *backend_module;
1835 int (*shell_init)(struct wlsc_compositor *ec);
1836 struct wlsc_compositor
1837 *(*backend_init)(struct wl_display *display, char *options);
1838 char *backend = NULL;
1839 char *backend_options = "";
1843 static const char opts[] = "B:b:o:S:i:s:";
1844 static const struct option longopts[ ] = {
1845 { "backend", 1, NULL, 'B' },
1846 { "backend-options", 1, NULL, 'o' },
1847 { "background", 1, NULL, 'b' },
1848 { "socket", 1, NULL, 'S' },
1849 { "idle-time", 1, NULL, 'i' },
1850 { "shell", 1, NULL, 's' },
1854 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1857 option_background = optarg;
1863 backend_options = optarg;
1866 option_socket_name = optarg;
1869 option_idle_time = strtol(optarg, &p, 0);
1872 "invalid idle time option: %s\n",
1883 display = wl_display_create();
1888 if (getenv("WAYLAND_DISPLAY"))
1889 backend = "wayland-backend.so";
1890 else if (getenv("DISPLAY"))
1891 backend = "x11-backend.so";
1892 else if (getenv("OPENWFD"))
1893 backend = "openwfd-backend.so";
1895 backend = "drm-backend.so";
1899 shell = "desktop-shell.so";
1901 backend_init = load_module(backend, "backend_init", &backend_module);
1905 shell_init = load_module(shell, "shell_init", &shell_module);
1909 ec = backend_init(display, backend_options);
1911 fprintf(stderr, "failed to create compositor\n");
1915 if (shell_init(ec) < 0)
1918 if (wl_display_add_socket(display, option_socket_name)) {
1919 fprintf(stderr, "failed to add socket: %m\n");
1923 loop = wl_display_get_event_loop(ec->wl_display);
1924 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1925 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1927 wl_display_run(display);
1929 if (ec->has_bind_display)
1930 ec->unbind_display(ec->display, display);
1931 wl_display_destroy(display);