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 const char *option_shell = NULL;
47 static int option_idle_time = 300;
48 static int option_connector = 0;
51 wlsc_matrix_init(struct wlsc_matrix *matrix)
53 static const struct wlsc_matrix identity = {
54 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
57 memcpy(matrix, &identity, sizeof identity);
61 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
63 struct wlsc_matrix tmp;
64 const GLfloat *row, *column;
68 for (i = 0; i < 16; i++) {
71 row = m->d + d.quot * 4;
72 column = n->d + d.rem;
73 for (j = 0; j < 4; j++)
74 tmp.d[i] += row[j] * column[j * 4];
76 memcpy(m, &tmp, sizeof tmp);
80 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
82 struct wlsc_matrix translate = {
83 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
86 wlsc_matrix_multiply(matrix, &translate);
90 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
92 struct wlsc_matrix scale = {
93 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
96 wlsc_matrix_multiply(matrix, &scale);
100 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
103 struct wlsc_vector t;
105 for (i = 0; i < 4; i++) {
107 for (j = 0; j < 4; j++)
108 t.f[i] += v->f[j] * matrix->d[i + j * 4];
115 wlsc_tweener_init(struct wlsc_tweener *tweener,
116 double k, double current, double target)
119 tweener->current = current;
120 tweener->previous = current;
121 tweener->target = target;
125 wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
127 double force, current, step;
129 step = (msec - tweener->timestamp) / 100.0;
130 tweener->timestamp = msec;
132 current = tweener->current;
133 force = tweener->k * (tweener->target - current) / 10.0 +
134 (tweener->previous - current);
137 current + (current - tweener->previous) + force * step * step;
138 tweener->previous = current;
140 if (tweener->current >= 1.0) {
141 tweener->current = 1.0;
142 tweener->previous = 1.0;
145 if (tweener->current <= 0.0) {
146 tweener->current = 0.0;
147 tweener->previous = 0.0;
152 wlsc_tweener_done(struct wlsc_tweener *tweener)
154 return fabs(tweener->previous - tweener->target) < 0.0002 &&
155 fabs(tweener->current - tweener->target) < 0.0002;
158 WL_EXPORT struct wlsc_surface *
159 wlsc_surface_create(struct wlsc_compositor *compositor,
160 int32_t x, int32_t y, int32_t width, int32_t height)
162 struct wlsc_surface *surface;
164 surface = malloc(sizeof *surface);
168 wl_list_init(&surface->surface.destroy_listener_list);
169 wl_list_init(&surface->link);
170 wl_list_init(&surface->buffer_link);
171 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
173 glGenTextures(1, &surface->texture);
174 glBindTexture(GL_TEXTURE_2D, surface->texture);
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
180 surface->compositor = compositor;
181 surface->visual = NULL;
182 surface->image = EGL_NO_IMAGE_KHR;
183 surface->saved_texture = 0;
184 surface->buffer = NULL;
187 surface->width = width;
188 surface->height = height;
189 wlsc_matrix_init(&surface->matrix);
190 wlsc_matrix_scale(&surface->matrix, width, height, 1);
191 wlsc_matrix_translate(&surface->matrix, x, y, 0);
193 wlsc_matrix_init(&surface->matrix_inv);
194 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
195 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
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 wl_listener *l, *next;
237 struct wlsc_compositor *compositor = surface->compositor;
240 wlsc_surface_damage(surface);
242 wl_list_remove(&surface->link);
243 if (surface->saved_texture == 0)
244 glDeleteTextures(1, &surface->texture);
246 glDeleteTextures(1, &surface->saved_texture);
249 if (surface->image != EGL_NO_IMAGE_KHR)
250 compositor->destroy_image(compositor->display,
253 wl_list_remove(&surface->buffer_link);
255 time = wlsc_compositor_get_time();
256 wl_list_for_each_safe(l, next,
257 &surface->surface.destroy_listener_list, link)
258 l->func(l, &surface->surface, time);
264 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
266 struct wlsc_surface *es = (struct wlsc_surface *) surface;
267 struct wlsc_compositor *ec = es->compositor;
268 struct wl_list *surfaces_attached_to;
270 if (es->saved_texture != 0)
271 es->texture = es->saved_texture;
273 glBindTexture(GL_TEXTURE_2D, es->texture);
275 if (wl_buffer_is_shm(buffer)) {
276 /* Unbind any EGLImage texture that may be bound, so we don't
278 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
279 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
280 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
281 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
282 es->pitch, buffer->height, 0,
283 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
284 wl_shm_buffer_get_data(buffer));
285 es->visual = buffer->visual;
287 surfaces_attached_to = buffer->user_data;
290 wl_list_remove(&es->buffer_link);
291 wl_list_insert(surfaces_attached_to, &es->buffer_link);
293 es->image = ec->create_image(ec->display, NULL,
294 EGL_WAYLAND_BUFFER_WL,
297 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
298 es->visual = buffer->visual;
299 es->pitch = es->width;
304 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
306 struct wlsc_surface *es = (struct wlsc_surface *) surface;
307 struct wlsc_compositor *ec = es->compositor;
309 es->pitch = es->width;
310 es->image = sprite->image;
311 if (sprite->image != EGL_NO_IMAGE_KHR) {
312 glBindTexture(GL_TEXTURE_2D, es->texture);
313 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
315 if (es->saved_texture == 0)
316 es->saved_texture = es->texture;
317 es->texture = sprite->texture;
320 es->visual = sprite->visual;
324 SPRITE_USE_CURSOR = (1 << 0),
327 static struct wlsc_sprite *
328 create_sprite_from_png(struct wlsc_compositor *ec,
329 const char *filename, uint32_t usage)
332 struct wlsc_sprite *sprite;
333 int32_t width, height;
336 pixels = wlsc_load_image(filename, &width, &height, &stride);
340 sprite = malloc(sizeof *sprite);
341 if (sprite == NULL) {
346 sprite->visual = &ec->compositor.premultiplied_argb_visual;
347 sprite->width = width;
348 sprite->height = height;
349 sprite->image = EGL_NO_IMAGE_KHR;
351 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
352 sprite->image = ec->create_cursor_image(ec, width, height);
354 glGenTextures(1, &sprite->texture);
355 glBindTexture(GL_TEXTURE_2D, sprite->texture);
356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
361 if (sprite->image != EGL_NO_IMAGE_KHR) {
362 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
363 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
364 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
366 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
367 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
375 static const struct {
376 const char *filename;
377 int hotspot_x, hotspot_y;
378 } pointer_images[] = {
379 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
380 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
381 { DATADIR "/wayland/bottom_side.png", 16, 20 },
382 { DATADIR "/wayland/grabbing.png", 20, 17 },
383 { DATADIR "/wayland/left_ptr.png", 10, 5 },
384 { DATADIR "/wayland/left_side.png", 10, 20 },
385 { DATADIR "/wayland/right_side.png", 30, 19 },
386 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
387 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
388 { DATADIR "/wayland/top_side.png", 18, 8 },
389 { DATADIR "/wayland/xterm.png", 15, 15 }
393 create_pointer_images(struct wlsc_compositor *ec)
397 count = ARRAY_LENGTH(pointer_images);
398 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
399 for (i = 0; i < count; i++) {
400 ec->pointer_sprites[i] =
401 create_sprite_from_png(ec,
402 pointer_images[i].filename,
407 static struct wlsc_surface *
408 background_create(struct wlsc_output *output, const char *filename)
410 struct wlsc_surface *background;
411 struct wlsc_sprite *sprite;
413 background = wlsc_surface_create(output->compositor,
414 output->x, output->y,
415 output->width, output->height);
416 if (background == NULL)
419 sprite = create_sprite_from_png(output->compositor, filename, 0);
420 if (sprite == NULL) {
425 wlsc_sprite_attach(sprite, &background->surface);
431 wlsc_surface_draw(struct wlsc_surface *es,
432 struct wlsc_output *output, pixman_region32_t *clip)
434 struct wlsc_compositor *ec = es->compositor;
435 GLfloat *v, inv_width, inv_height;
437 pixman_region32_t repaint;
438 pixman_box32_t *rectangles;
441 pixman_region32_init_rect(&repaint,
442 es->x, es->y, es->width, es->height);
443 pixman_region32_intersect(&repaint, &repaint, clip);
444 if (!pixman_region32_not_empty(&repaint))
447 if (es->visual == &ec->compositor.argb_visual) {
448 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
450 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
451 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
457 rectangles = pixman_region32_rectangles(&repaint, &n);
458 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
459 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
460 inv_width = 1.0 / es->pitch;
461 inv_height = 1.0 / es->height;
462 for (i = 0; i < n; i++, v += 16, p += 6) {
463 v[ 0] = rectangles[i].x1;
464 v[ 1] = rectangles[i].y1;
465 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
466 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
468 v[ 4] = rectangles[i].x1;
469 v[ 5] = rectangles[i].y2;
471 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
473 v[ 8] = rectangles[i].x2;
474 v[ 9] = rectangles[i].y1;
475 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
478 v[12] = rectangles[i].x2;
479 v[13] = rectangles[i].y2;
491 glBindTexture(GL_TEXTURE_2D, es->texture);
492 v = ec->vertices.data;
493 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
494 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
495 glEnableVertexAttribArray(0);
496 glEnableVertexAttribArray(1);
497 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
499 ec->vertices.size = 0;
500 ec->indices.size = 0;
501 pixman_region32_fini(&repaint);
505 wlsc_surface_raise(struct wlsc_surface *surface)
507 struct wlsc_compositor *compositor = surface->compositor;
509 wl_list_remove(&surface->link);
510 wl_list_insert(&compositor->surface_list, &surface->link);
514 wlsc_surface_update_matrix(struct wlsc_surface *es)
516 wlsc_matrix_init(&es->matrix);
517 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
518 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
520 wlsc_matrix_init(&es->matrix_inv);
521 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
522 wlsc_matrix_scale(&es->matrix_inv,
523 1.0 / es->width, 1.0 / es->height, 1);
527 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
529 struct wlsc_output *output;
531 wl_list_for_each(output, &compositor->output_list, link)
532 wlsc_output_damage(output);
536 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
538 struct wlsc_compositor *compositor = output->compositor;
539 struct wlsc_surface *es;
540 struct wlsc_animation *animation, *next;
542 wl_list_for_each(es, &compositor->surface_list, link) {
543 if (es->output == output) {
544 wl_display_post_frame(compositor->wl_display,
545 &es->surface, msecs);
549 output->finished = 1;
551 wl_event_source_timer_update(compositor->timer_source, 5);
552 compositor->repaint_on_timeout = 1;
554 wl_list_for_each_safe(animation, next,
555 &compositor->animation_list, link)
556 animation->frame(animation, output, msecs);
560 wlsc_output_damage(struct wlsc_output *output)
562 struct wlsc_compositor *compositor = output->compositor;
564 pixman_region32_union_rect(&compositor->damage_region,
565 &compositor->damage_region,
566 output->x, output->y,
567 output->width, output->height);
568 wlsc_compositor_schedule_repaint(compositor);
572 fade_frame(struct wlsc_animation *animation,
573 struct wlsc_output *output, uint32_t msecs)
575 struct wlsc_compositor *compositor =
576 container_of(animation,
577 struct wlsc_compositor, fade.animation);
579 wlsc_tweener_update(&compositor->fade.tweener, msecs);
580 if (wlsc_tweener_done(&compositor->fade.tweener)) {
581 if (compositor->fade.tweener.current > 0.999) {
582 compositor->state = WLSC_COMPOSITOR_SLEEPING;
583 compositor->shell->lock(compositor->shell);
585 compositor->fade.tweener.current =
586 compositor->fade.tweener.target;
587 wl_list_remove(&animation->link);
588 wl_list_init(&animation->link);
591 wlsc_output_damage(output);
595 fade_output(struct wlsc_output *output,
596 GLfloat tint, pixman_region32_t *region)
598 struct wlsc_compositor *compositor = output->compositor;
599 struct wlsc_surface surface;
600 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
602 surface.compositor = compositor;
603 surface.x = output->x;
604 surface.y = output->y;
605 surface.width = output->width;
606 surface.height = output->height;
607 surface.texture = GL_NONE;
611 &compositor->compositor.premultiplied_argb_visual;
613 surface.visual = &compositor->compositor.rgb_visual;
615 glUseProgram(compositor->solid_shader.program);
616 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
617 1, GL_FALSE, output->matrix.d);
618 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
619 wlsc_surface_draw(&surface, output, region);
623 wlsc_output_repaint(struct wlsc_output *output)
625 struct wlsc_compositor *ec = output->compositor;
626 struct wlsc_surface *es;
627 struct wlsc_input_device *eid;
628 pixman_region32_t new_damage, total_damage, repaint;
629 int using_hardware_cursor = 1;
631 output->prepare_render(output);
633 glViewport(0, 0, output->width, output->height);
635 glUseProgram(ec->texture_shader.program);
636 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
637 1, GL_FALSE, output->matrix.d);
638 glUniform1i(ec->texture_shader.tex_uniform, 0);
640 pixman_region32_init(&new_damage);
641 pixman_region32_init(&total_damage);
642 pixman_region32_intersect_rect(&new_damage,
644 output->x, output->y,
645 output->width, output->height);
646 pixman_region32_subtract(&ec->damage_region,
647 &ec->damage_region, &new_damage);
648 pixman_region32_union(&total_damage, &new_damage,
649 &output->previous_damage_region);
650 pixman_region32_copy(&output->previous_damage_region, &new_damage);
653 if (output->set_hardware_cursor(output, ec->input_device) < 0)
654 using_hardware_cursor = 0;
655 if (ec->fade.tweener.current > 0.001)
656 using_hardware_cursor = 0;
658 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
659 if (es->fullscreen_output == output) {
660 if (es->visual == &ec->compositor.rgb_visual &&
661 using_hardware_cursor) {
662 if (output->prepare_scanout_surface(output, es) == 0) {
663 /* We're drawing nothing now,
664 * draw the damaged regions later. */
665 pixman_region32_union(&ec->damage_region,
672 if (es->width < output->width ||
673 es->height < output->height)
674 glClear(GL_COLOR_BUFFER_BIT);
675 wlsc_surface_draw(es, output, &total_damage);
677 wl_list_for_each(es, &ec->surface_list, link) {
678 if (es->visual != &ec->compositor.rgb_visual)
681 pixman_region32_init_rect(&repaint,
683 es->width, es->height);
684 wlsc_surface_draw(es, output, &total_damage);
685 pixman_region32_subtract(&total_damage,
686 &total_damage, &repaint);
689 if (output->background)
690 wlsc_surface_draw(output->background,
691 output, &total_damage);
693 fade_output(output, 1.0, &total_damage);
695 glUseProgram(ec->texture_shader.program);
696 wl_list_for_each_reverse(es, &ec->surface_list, link) {
697 if (ec->overlay == es)
700 if (es->visual == &ec->compositor.rgb_visual) {
701 pixman_region32_union_rect(&total_damage,
704 es->width, es->height);
708 wlsc_surface_draw(es, output, &total_damage);
713 wlsc_surface_draw(ec->overlay, output, &total_damage);
716 wl_list_for_each(eid, &ec->input_device_list, link) {
717 if (&eid->input_device != ec->input_device ||
718 !using_hardware_cursor)
719 wlsc_surface_draw(eid->sprite, output,
723 if (ec->fade.tweener.current > 0.001)
724 fade_output(output, ec->fade.tweener.current, &total_damage);
730 struct wlsc_compositor *ec = data;
731 struct wlsc_output *output;
732 int repainted_all_outputs = 1;
734 wl_list_for_each(output, &ec->output_list, link) {
735 if (!output->repaint_needed)
738 if (!output->finished) {
739 repainted_all_outputs = 0;
743 wlsc_output_repaint(output);
744 output->finished = 0;
745 output->repaint_needed = 0;
746 output->present(output);
749 if (repainted_all_outputs)
750 ec->repaint_on_timeout = 0;
752 wl_event_source_timer_update(ec->timer_source, 1);
758 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
760 struct wlsc_output *output;
762 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
765 wl_list_for_each(output, &compositor->output_list, link)
766 output->repaint_needed = 1;
768 if (compositor->repaint_on_timeout)
771 wl_event_source_timer_update(compositor->timer_source, 1);
772 compositor->repaint_on_timeout = 1;
776 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
780 done = wlsc_tweener_done(&compositor->fade.tweener);
781 compositor->fade.tweener.target = tint;
782 if (wlsc_tweener_done(&compositor->fade.tweener))
786 compositor->fade.tweener.timestamp =
787 wlsc_compositor_get_time();
789 wlsc_compositor_damage_all(compositor);
790 if (wl_list_empty(&compositor->fade.animation.link))
791 wl_list_insert(compositor->animation_list.prev,
792 &compositor->fade.animation.link);
796 surface_destroy(struct wl_client *client,
797 struct wl_surface *surface)
799 wl_resource_destroy(&surface->resource, client);
803 wlsc_surface_assign_output(struct wlsc_surface *es)
805 struct wlsc_compositor *ec = es->compositor;
806 struct wlsc_output *output;
808 struct wlsc_output *tmp = es->output;
811 wl_list_for_each(output, &ec->output_list, link) {
812 if (output->x < es->x && es->x < output->x + output->width &&
813 output->y < es->y && es->y < output->y + output->height) {
815 printf("assiging surface %p to output %p\n",
821 if (es->output == NULL) {
822 printf("no output found\n");
823 es->output = container_of(ec->output_list.next,
824 struct wlsc_output, link);
829 surface_attach(struct wl_client *client,
830 struct wl_surface *surface, struct wl_buffer *buffer,
831 int32_t x, int32_t y)
833 struct wlsc_surface *es = (struct wlsc_surface *) surface;
835 /* FIXME: This damages the entire old surface, but we should
836 * really just damage the part that's no longer covered by the
837 * surface. Anything covered by the new surface will be
838 * damaged by the client. */
839 wlsc_surface_damage(es);
841 wlsc_buffer_attach(buffer, surface);
844 switch (es->map_type) {
845 case WLSC_SURFACE_MAP_FULLSCREEN:
846 es->x = (es->fullscreen_output->width - es->width) / 2;
847 es->y = (es->fullscreen_output->height - es->height) / 2;
854 es->width = buffer->width;
855 es->height = buffer->height;
856 if (x != 0 || y != 0)
857 wlsc_surface_assign_output(es);
858 wlsc_surface_update_matrix(es);
860 es->compositor->shell->attach(es->compositor->shell, es);
864 surface_map_toplevel(struct wl_client *client,
865 struct wl_surface *surface)
867 struct wlsc_surface *es = (struct wlsc_surface *) surface;
868 struct wlsc_compositor *ec = es->compositor;
870 switch (es->map_type) {
871 case WLSC_SURFACE_MAP_UNMAPPED:
872 es->x = 10 + random() % 400;
873 es->y = 10 + random() % 400;
874 wlsc_surface_update_matrix(es);
875 /* assign to first output */
876 es->output = container_of(ec->output_list.next,
877 struct wlsc_output, link);
878 wl_list_insert(&es->compositor->surface_list, &es->link);
880 case WLSC_SURFACE_MAP_TOPLEVEL:
882 case WLSC_SURFACE_MAP_FULLSCREEN:
883 es->fullscreen_output = NULL;
886 wlsc_surface_update_matrix(es);
892 wlsc_surface_damage(es);
893 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
897 surface_map_transient(struct wl_client *client,
898 struct wl_surface *surface, struct wl_surface *parent,
899 int x, int y, uint32_t flags)
901 struct wlsc_surface *es = (struct wlsc_surface *) surface;
902 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
904 switch (es->map_type) {
905 case WLSC_SURFACE_MAP_UNMAPPED:
906 wl_list_insert(&es->compositor->surface_list, &es->link);
907 /* assign to parents output */
908 es->output = pes->output;
910 case WLSC_SURFACE_MAP_FULLSCREEN:
911 es->fullscreen_output = NULL;
920 wlsc_surface_update_matrix(es);
921 wlsc_surface_damage(es);
922 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
926 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
928 struct wlsc_surface *es = (struct wlsc_surface *) surface;
929 struct wlsc_output *output;
931 /* FIXME: Fullscreen on first output */
932 /* FIXME: Handle output going away */
933 output = container_of(es->compositor->output_list.next,
934 struct wlsc_output, link);
936 switch (es->map_type) {
937 case WLSC_SURFACE_MAP_UNMAPPED:
938 es->x = 10 + random() % 400;
939 es->y = 10 + random() % 400;
940 /* assign to first output */
942 wl_list_insert(&es->compositor->surface_list, &es->link);
944 case WLSC_SURFACE_MAP_FULLSCREEN:
952 es->x = (output->width - es->width) / 2;
953 es->y = (output->height - es->height) / 2;
954 es->fullscreen_output = output;
955 wlsc_surface_update_matrix(es);
956 wlsc_surface_damage(es);
957 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
961 surface_damage(struct wl_client *client,
962 struct wl_surface *surface,
963 int32_t x, int32_t y, int32_t width, int32_t height)
965 struct wlsc_surface *es = (struct wlsc_surface *) surface;
967 wlsc_surface_damage_rectangle(es, x, y, width, height);
970 const static struct wl_surface_interface surface_interface = {
973 surface_map_toplevel,
974 surface_map_transient,
975 surface_map_fullscreen,
980 wlsc_input_device_attach(struct wlsc_input_device *device,
981 int x, int y, int width, int height)
983 wlsc_surface_damage(device->sprite);
985 device->hotspot_x = x;
986 device->hotspot_y = y;
988 device->sprite->x = device->input_device.x - device->hotspot_x;
989 device->sprite->y = device->input_device.y - device->hotspot_y;
990 device->sprite->width = width;
991 device->sprite->height = height;
992 wlsc_surface_update_matrix(device->sprite);
994 wlsc_surface_damage(device->sprite);
998 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
999 struct wl_buffer *buffer, int x, int y)
1001 wlsc_buffer_attach(buffer, &device->sprite->surface);
1002 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1006 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1007 struct wlsc_sprite *sprite, int x, int y)
1009 wlsc_sprite_attach(sprite, &device->sprite->surface);
1010 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1014 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1015 enum wlsc_pointer_type type)
1017 struct wlsc_compositor *compositor =
1018 (struct wlsc_compositor *) device->input_device.compositor;
1020 wlsc_input_device_attach_sprite(device,
1021 compositor->pointer_sprites[type],
1022 pointer_images[type].hotspot_x,
1023 pointer_images[type].hotspot_y);
1027 compositor_create_surface(struct wl_client *client,
1028 struct wl_compositor *compositor, uint32_t id)
1030 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1031 struct wlsc_surface *surface;
1033 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1034 if (surface == NULL) {
1035 wl_client_post_no_memory(client);
1039 surface->surface.resource.destroy = destroy_surface;
1041 surface->surface.resource.object.id = id;
1042 surface->surface.resource.object.interface = &wl_surface_interface;
1043 surface->surface.resource.object.implementation =
1044 (void (**)(void)) &surface_interface;
1045 surface->surface.client = client;
1047 wl_client_add_resource(client, &surface->surface.resource);
1050 const static struct wl_compositor_interface compositor_interface = {
1051 compositor_create_surface,
1055 wlsc_surface_transform(struct wlsc_surface *surface,
1056 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1058 struct wlsc_vector v = { { x, y, 0, 1 } };
1060 wlsc_matrix_transform(&surface->matrix_inv, &v);
1061 *sx = v.f[0] * surface->width;
1062 *sy = v.f[1] * surface->height;
1065 struct wlsc_surface *
1066 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1068 struct wlsc_compositor *ec =
1069 (struct wlsc_compositor *) device->compositor;
1070 struct wlsc_surface *es;
1072 wl_list_for_each(es, &ec->surface_list, link) {
1073 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1074 if (0 <= *sx && *sx < es->width &&
1075 0 <= *sy && *sy < es->height)
1084 motion_grab_motion(struct wl_grab *grab,
1085 uint32_t time, int32_t x, int32_t y)
1087 struct wlsc_input_device *device =
1088 (struct wlsc_input_device *) grab->input_device;
1089 struct wlsc_surface *es =
1090 (struct wlsc_surface *) device->input_device.pointer_focus;
1093 wlsc_surface_transform(es, x, y, &sx, &sy);
1094 wl_client_post_event(es->surface.client,
1095 &device->input_device.object,
1096 WL_INPUT_DEVICE_MOTION,
1097 time, x, y, sx, sy);
1101 motion_grab_button(struct wl_grab *grab,
1102 uint32_t time, int32_t button, int32_t state)
1104 wl_client_post_event(grab->input_device->pointer_focus->client,
1105 &grab->input_device->object,
1106 WL_INPUT_DEVICE_BUTTON,
1107 time, button, state);
1111 motion_grab_end(struct wl_grab *grab, uint32_t time)
1115 static const struct wl_grab_interface motion_grab_interface = {
1122 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1124 if (compositor->idle_inhibit)
1127 wlsc_compositor_fade(compositor, 0.0);
1128 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1130 wl_event_source_timer_update(compositor->idle_source,
1131 option_idle_time * 1000);
1135 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1137 wlsc_compositor_wake(compositor);
1138 compositor->idle_inhibit++;
1142 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1144 compositor->idle_inhibit--;
1145 wlsc_compositor_wake(compositor);
1149 idle_handler(void *data)
1151 struct wlsc_compositor *compositor = data;
1153 if (compositor->idle_inhibit)
1156 wlsc_compositor_fade(compositor, 1.0);
1162 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1164 struct wlsc_surface *es;
1165 struct wlsc_compositor *ec =
1166 (struct wlsc_compositor *) device->compositor;
1167 struct wlsc_output *output;
1168 const struct wl_grab_interface *interface;
1169 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1171 int x_valid = 0, y_valid = 0;
1172 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1174 wlsc_compositor_wake(ec);
1176 wl_list_for_each(output, &ec->output_list, link) {
1177 if (output->x <= x && x <= output->x + output->width)
1180 if (output->y <= y && y <= output->y + output->height)
1183 /* FIXME: calculate this only on output addition/deletion */
1184 if (output->x < min_x)
1186 if (output->y < min_y)
1189 if (output->x + output->width > max_x)
1190 max_x = output->x + output->width;
1191 if (output->y + output->height > max_y)
1192 max_y = output->y + output->height;
1198 else if (x >= max_x)
1204 else if (y >= max_y)
1212 interface = device->grab->interface;
1213 interface->motion(device->grab, time, x, y);
1215 es = pick_surface(device, &sx, &sy);
1216 wl_input_device_set_pointer_focus(device,
1218 time, x, y, sx, sy);
1220 wl_client_post_event(es->surface.client,
1222 WL_INPUT_DEVICE_MOTION,
1223 time, x, y, sx, sy);
1226 wlsc_surface_damage(wd->sprite);
1228 wd->sprite->x = device->x - wd->hotspot_x;
1229 wd->sprite->y = device->y - wd->hotspot_y;
1230 wlsc_surface_update_matrix(wd->sprite);
1232 wlsc_surface_damage(wd->sprite);
1236 wlsc_surface_activate(struct wlsc_surface *surface,
1237 struct wlsc_input_device *device, uint32_t time)
1239 wlsc_surface_raise(surface);
1240 if (device->selection)
1241 wlsc_selection_set_focus(device->selection,
1242 &surface->surface, time);
1244 wl_input_device_set_keyboard_focus(&device->input_device,
1249 struct wlsc_binding {
1253 wlsc_binding_handler_t handler;
1255 struct wl_list link;
1259 notify_button(struct wl_input_device *device,
1260 uint32_t time, int32_t button, int32_t state)
1262 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1263 struct wlsc_compositor *compositor =
1264 (struct wlsc_compositor *) device->compositor;
1265 struct wlsc_binding *b;
1266 struct wlsc_surface *surface =
1267 (struct wlsc_surface *) device->pointer_focus;
1270 wlsc_compositor_idle_inhibit(compositor);
1272 wlsc_compositor_idle_release(compositor);
1274 if (state && surface && device->grab == NULL) {
1275 wlsc_surface_activate(surface, wd, time);
1276 wl_input_device_start_grab(device,
1277 &device->motion_grab,
1281 wl_list_for_each(b, &compositor->binding_list, link) {
1282 if (b->button == button &&
1283 b->modifier == wd->modifier_state && state) {
1284 b->handler(&wd->input_device,
1285 time, 0, button, state, b->data);
1291 device->grab->interface->button(device->grab, time,
1294 if (!state && device->grab && device->grab_button == button)
1295 wl_input_device_end_grab(device, time);
1299 terminate_binding(struct wl_input_device *device, uint32_t time,
1300 uint32_t key, uint32_t button, uint32_t state, void *data)
1302 struct wlsc_compositor *compositor = data;
1305 wl_display_terminate(compositor->wl_display);
1308 WL_EXPORT struct wlsc_binding *
1309 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1310 uint32_t key, uint32_t button, uint32_t modifier,
1311 wlsc_binding_handler_t handler, void *data)
1313 struct wlsc_binding *binding;
1315 binding = malloc(sizeof *binding);
1316 if (binding == NULL)
1320 binding->button = button;
1321 binding->modifier = modifier;
1322 binding->handler = handler;
1323 binding->data = data;
1324 wl_list_insert(compositor->binding_list.prev, &binding->link);
1330 wlsc_binding_destroy(struct wlsc_binding *binding)
1332 wl_list_remove(&binding->link);
1337 update_modifier_state(struct wlsc_input_device *device,
1338 uint32_t key, uint32_t state)
1345 modifier = MODIFIER_CTRL;
1350 modifier = MODIFIER_ALT;
1355 modifier = MODIFIER_SUPER;
1364 device->modifier_state |= modifier;
1366 device->modifier_state &= ~modifier;
1370 notify_key(struct wl_input_device *device,
1371 uint32_t time, uint32_t key, uint32_t state)
1373 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1374 struct wlsc_compositor *compositor =
1375 (struct wlsc_compositor *) device->compositor;
1377 struct wlsc_binding *b;
1380 wlsc_compositor_idle_inhibit(compositor);
1382 wlsc_compositor_idle_release(compositor);
1384 wl_list_for_each(b, &compositor->binding_list, link) {
1385 if (b->key == key &&
1386 b->modifier == wd->modifier_state) {
1387 b->handler(&wd->input_device,
1388 time, key, 0, state, b->data);
1393 update_modifier_state(wd, key, state);
1394 end = device->keys.data + device->keys.size;
1395 for (k = device->keys.data; k < end; k++) {
1399 device->keys.size = (void *) end - device->keys.data;
1401 k = wl_array_add(&device->keys, sizeof *k);
1405 if (device->keyboard_focus != NULL)
1406 wl_client_post_event(device->keyboard_focus->client,
1408 WL_INPUT_DEVICE_KEY, time, key, state);
1412 notify_pointer_focus(struct wl_input_device *device,
1413 uint32_t time, struct wlsc_output *output,
1414 int32_t x, int32_t y)
1416 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1417 struct wlsc_compositor *compositor =
1418 (struct wlsc_compositor *) device->compositor;
1419 struct wlsc_surface *es;
1425 es = pick_surface(device, &sx, &sy);
1426 wl_input_device_set_pointer_focus(device,
1428 time, x, y, sx, sy);
1430 compositor->focus = 1;
1432 wd->sprite->x = device->x - wd->hotspot_x;
1433 wd->sprite->y = device->y - wd->hotspot_y;
1434 wlsc_surface_update_matrix(wd->sprite);
1436 wl_input_device_set_pointer_focus(device, NULL,
1438 compositor->focus = 0;
1441 wlsc_surface_damage(wd->sprite);
1445 notify_keyboard_focus(struct wl_input_device *device,
1446 uint32_t time, struct wlsc_output *output,
1447 struct wl_array *keys)
1449 struct wlsc_input_device *wd =
1450 (struct wlsc_input_device *) device;
1451 struct wlsc_compositor *compositor =
1452 (struct wlsc_compositor *) device->compositor;
1453 struct wlsc_surface *es;
1456 if (!wl_list_empty(&compositor->surface_list))
1457 es = container_of(compositor->surface_list.next,
1458 struct wlsc_surface, link);
1463 wl_array_copy(&wd->input_device.keys, keys);
1464 wd->modifier_state = 0;
1465 end = device->keys.data + device->keys.size;
1466 for (k = device->keys.data; k < end; k++) {
1467 wlsc_compositor_idle_inhibit(compositor);
1468 update_modifier_state(wd, *k, 1);
1471 wl_input_device_set_keyboard_focus(&wd->input_device,
1472 &es->surface, time);
1474 end = device->keys.data + device->keys.size;
1475 for (k = device->keys.data; k < end; k++)
1476 wlsc_compositor_idle_release(compositor);
1478 wd->modifier_state = 0;
1479 wl_input_device_set_keyboard_focus(&wd->input_device,
1486 input_device_attach(struct wl_client *client,
1487 struct wl_input_device *device_base,
1489 struct wl_buffer *buffer, int32_t x, int32_t y)
1491 struct wlsc_input_device *device =
1492 (struct wlsc_input_device *) device_base;
1494 if (time < device->input_device.pointer_focus_time)
1496 if (device->input_device.pointer_focus == NULL)
1498 if (device->input_device.pointer_focus->client != client)
1501 if (buffer == NULL) {
1502 wlsc_input_device_set_pointer_image(device,
1503 WLSC_POINTER_LEFT_PTR);
1507 wlsc_input_device_attach_buffer(device, buffer, x, y);
1510 const static struct wl_input_device_interface input_device_interface = {
1511 input_device_attach,
1515 wlsc_input_device_init(struct wlsc_input_device *device,
1516 struct wlsc_compositor *ec)
1518 wl_input_device_init(&device->input_device, &ec->compositor);
1520 device->input_device.object.interface = &wl_input_device_interface;
1521 device->input_device.object.implementation =
1522 (void (**)(void)) &input_device_interface;
1523 wl_display_add_object(ec->wl_display, &device->input_device.object);
1524 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1526 device->sprite = wlsc_surface_create(ec,
1527 device->input_device.x,
1528 device->input_device.y, 32, 32);
1529 device->hotspot_x = 16;
1530 device->hotspot_y = 16;
1531 device->modifier_state = 0;
1533 device->input_device.motion_grab.interface = &motion_grab_interface;
1535 wl_list_insert(ec->input_device_list.prev, &device->link);
1537 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1541 wlsc_output_post_geometry(struct wl_client *client,
1542 struct wl_object *global, uint32_t version)
1544 struct wlsc_output *output =
1545 container_of(global, struct wlsc_output, object);
1547 wl_client_post_event(client, global,
1549 output->x, output->y,
1550 output->width, output->height);
1553 static const char vertex_shader[] =
1554 "uniform mat4 proj;\n"
1555 "attribute vec2 position;\n"
1556 "attribute vec2 texcoord;\n"
1557 "varying vec2 v_texcoord;\n"
1560 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1561 " v_texcoord = texcoord;\n"
1564 static const char texture_fragment_shader[] =
1565 "precision mediump float;\n"
1566 "varying vec2 v_texcoord;\n"
1567 "uniform sampler2D tex;\n"
1570 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1573 static const char solid_fragment_shader[] =
1574 "precision mediump float;\n"
1575 "varying vec2 v_texcoord;\n"
1576 "uniform vec4 color;\n"
1579 " gl_FragColor = color\n;"
1583 compile_shader(GLenum type, const char *source)
1589 s = glCreateShader(type);
1590 glShaderSource(s, 1, &source, NULL);
1592 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1594 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1595 fprintf(stderr, "shader info: %s\n", msg);
1603 wlsc_shader_init(struct wlsc_shader *shader,
1604 const char *vertex_source, const char *fragment_source)
1609 shader->vertex_shader =
1610 compile_shader(GL_VERTEX_SHADER, vertex_source);
1611 shader->fragment_shader =
1612 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1614 shader->program = glCreateProgram();
1615 glAttachShader(shader->program, shader->vertex_shader);
1616 glAttachShader(shader->program, shader->fragment_shader);
1617 glBindAttribLocation(shader->program, 0, "position");
1618 glBindAttribLocation(shader->program, 1, "texcoord");
1620 glLinkProgram(shader->program);
1621 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1623 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1624 fprintf(stderr, "link info: %s\n", msg);
1628 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1629 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1635 init_solid_shader(struct wlsc_shader *shader,
1636 GLuint vertex_shader, const char *fragment_source)
1641 shader->vertex_shader = vertex_shader;
1642 shader->fragment_shader =
1643 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1645 shader->program = glCreateProgram();
1646 glAttachShader(shader->program, shader->vertex_shader);
1647 glAttachShader(shader->program, shader->fragment_shader);
1648 glBindAttribLocation(shader->program, 0, "position");
1649 glBindAttribLocation(shader->program, 1, "texcoord");
1651 glLinkProgram(shader->program);
1652 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1654 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1655 fprintf(stderr, "link info: %s\n", msg);
1659 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1660 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1666 wlsc_output_destroy(struct wlsc_output *output)
1668 destroy_surface(&output->background->surface.resource, NULL);
1672 wlsc_output_move(struct wlsc_output *output, int x, int y)
1674 struct wlsc_compositor *c = output->compositor;
1680 if (output->background) {
1681 output->background->x = x;
1682 output->background->y = y;
1683 wlsc_surface_update_matrix(output->background);
1686 pixman_region32_init(&output->previous_damage_region);
1688 wlsc_matrix_init(&output->matrix);
1689 wlsc_matrix_translate(&output->matrix,
1690 -output->x - output->width / 2.0,
1691 -output->y - output->height / 2.0, 0);
1693 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1694 wlsc_matrix_scale(&output->matrix,
1695 2.0 / output->width,
1696 flip * 2.0 / output->height, 1);
1698 pixman_region32_union_rect(&c->damage_region,
1700 x, y, output->width, output->height);
1704 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1705 int x, int y, int width, int height, uint32_t flags)
1707 output->compositor = c;
1710 output->width = width;
1711 output->height = height;
1713 output->background =
1714 background_create(output, option_background);
1716 output->flags = flags;
1717 output->finished = 1;
1718 wlsc_output_move(output, x, y);
1720 output->object.interface = &wl_output_interface;
1721 wl_display_add_object(c->wl_display, &output->object);
1722 wl_display_add_global(c->wl_display, &output->object,
1723 wlsc_output_post_geometry);
1727 shm_buffer_created(struct wl_buffer *buffer)
1729 struct wl_list *surfaces_attached_to;
1731 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1732 if (!surfaces_attached_to) {
1733 buffer->user_data = NULL;
1737 wl_list_init(surfaces_attached_to);
1739 buffer->user_data = surfaces_attached_to;
1743 shm_buffer_damaged(struct wl_buffer *buffer,
1744 int32_t x, int32_t y, int32_t width, int32_t height)
1746 struct wl_list *surfaces_attached_to = buffer->user_data;
1747 struct wlsc_surface *es;
1748 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1750 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1751 glBindTexture(GL_TEXTURE_2D, es->texture);
1752 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1753 tex_width, buffer->height, 0,
1754 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1755 wl_shm_buffer_get_data(buffer));
1756 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1757 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1762 shm_buffer_destroyed(struct wl_buffer *buffer)
1764 struct wl_list *surfaces_attached_to = buffer->user_data;
1765 struct wlsc_surface *es, *next;
1767 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1769 wl_list_remove(&es->buffer_link);
1772 free(surfaces_attached_to);
1775 const static struct wl_shm_callbacks shm_callbacks = {
1778 shm_buffer_destroyed
1782 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1784 struct wl_event_loop *loop;
1785 const char *extensions;
1787 ec->wl_display = display;
1789 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1791 ec->shm = wl_shm_init(display, &shm_callbacks);
1793 ec->image_target_texture_2d =
1794 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1795 ec->image_target_renderbuffer_storage = (void *)
1796 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1797 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1798 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1800 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1801 ec->unbind_display =
1802 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1804 extensions = (const char *) glGetString(GL_EXTENSIONS);
1805 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1807 "GL_EXT_texture_format_BGRA8888 not available\n");
1812 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1813 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1814 ec->has_bind_display = 1;
1815 if (ec->has_bind_display)
1816 ec->bind_display(ec->display, ec->wl_display);
1818 wl_list_init(&ec->surface_list);
1819 wl_list_init(&ec->input_device_list);
1820 wl_list_init(&ec->output_list);
1821 wl_list_init(&ec->binding_list);
1822 wl_list_init(&ec->animation_list);
1823 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1824 ec->fade.animation.frame = fade_frame;
1825 wl_list_init(&ec->fade.animation.link);
1827 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1828 MODIFIER_CTRL | MODIFIER_ALT,
1829 terminate_binding, ec);
1831 create_pointer_images(ec);
1833 screenshooter_create(ec);
1835 glActiveTexture(GL_TEXTURE0);
1837 if (wlsc_shader_init(&ec->texture_shader,
1838 vertex_shader, texture_fragment_shader) < 0)
1840 if (init_solid_shader(&ec->solid_shader,
1841 ec->texture_shader.vertex_shader,
1842 solid_fragment_shader) < 0)
1845 loop = wl_display_get_event_loop(ec->wl_display);
1846 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1847 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1849 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1850 pixman_region32_init(&ec->damage_region);
1851 wlsc_compositor_schedule_repaint(ec);
1856 static int on_term_signal(int signal_number, void *data)
1858 struct wlsc_compositor *ec = data;
1860 wl_display_terminate(ec->wl_display);
1865 int main(int argc, char *argv[])
1867 struct wl_display *display;
1868 struct wlsc_compositor *ec;
1869 struct wl_event_loop *loop;
1870 int width, height, o;
1872 int (*shell_init)(struct wlsc_compositor *ec);
1875 static const char opts[] = "b:c:g:S:i:s:";
1876 static const struct option longopts[ ] = {
1877 { "background", 1, NULL, 'b' },
1878 { "connector", 1, NULL, 'c' },
1879 { "geometry", 1, NULL, 'g' },
1880 { "socket", 1, NULL, 'S' },
1881 { "idle-time", 1, NULL, 'i' },
1882 { "shell", 1, NULL, 's' },
1889 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1892 option_background = optarg;
1895 option_connector = strtol(optarg, &p, 0);
1898 "invalid connection option: %s\n",
1904 if (sscanf(optarg, "%dx%d", &width, &height) != 2) {
1906 "invalid geometry option: %s\n",
1912 option_socket_name = optarg;
1915 option_idle_time = strtol(optarg, &p, 0);
1918 "invalid idle time option: %s\n",
1924 option_shell = optarg;
1929 display = wl_display_create();
1933 shell_init = desktop_shell_init;
1935 shell_module = dlopen(option_shell, RTLD_LAZY);
1936 if (!shell_module) {
1937 fprintf(stderr, "failed to load shell module: %m\n");
1940 shell_init = dlsym(shell_module, "shell_init");
1943 "failed to lookup shell init function: %m\n");
1948 #if BUILD_WAYLAND_COMPOSITOR
1949 if (getenv("WAYLAND_DISPLAY"))
1950 ec = wayland_compositor_create(display, width, height);
1953 #if BUILD_X11_COMPOSITOR
1954 if (ec == NULL && getenv("DISPLAY"))
1955 ec = x11_compositor_create(display, width, height);
1958 #if BUILD_OPENWFD_COMPOSITOR
1959 if (ec == NULL && getenv("OPENWFD"))
1960 ec = wfd_compositor_create(display, option_connector);
1963 #if BUILD_DRM_COMPOSITOR
1965 ec = drm_compositor_create(display, option_connector);
1969 fprintf(stderr, "failed to create compositor\n");
1973 if (shell_init(ec) < 0)
1976 if (wl_display_add_socket(display, option_socket_name)) {
1977 fprintf(stderr, "failed to add socket: %m\n");
1981 loop = wl_display_get_event_loop(ec->wl_display);
1982 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1983 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1985 wl_display_run(display);
1987 if (ec->has_bind_display)
1988 ec->unbind_display(ec->display, display);
1989 wl_display_destroy(display);