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->friction = 100.0;
120 tweener->current = current;
121 tweener->previous = current;
122 tweener->target = target;
126 wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec)
128 double force, v, current, step;
130 step = (msec - tweener->timestamp) / 500.0;
131 tweener->timestamp = msec;
133 current = tweener->current;
134 v = current - tweener->previous;
135 force = tweener->k * (tweener->target - current) / 10.0 +
136 (tweener->previous - current) - v * tweener->friction;
139 current + (current - tweener->previous) + force * step * step;
140 tweener->previous = current;
142 if (tweener->current >= 1.0) {
143 #ifdef TWEENER_BOUNCE
144 tweener->current = 2.0 - tweener->current;
145 tweener->previous = 2.0 - tweener->previous;
147 tweener->current = 1.0;
148 tweener->previous = 1.0;
152 if (tweener->current <= 0.0) {
153 tweener->current = 0.0;
154 tweener->previous = 0.0;
159 wlsc_tweener_done(struct wlsc_tweener *tweener)
161 return fabs(tweener->previous - tweener->target) < 0.0002 &&
162 fabs(tweener->current - tweener->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->surface.destroy_listener_list);
176 wl_list_init(&surface->link);
177 wl_list_init(&surface->buffer_link);
178 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
180 glGenTextures(1, &surface->texture);
181 glBindTexture(GL_TEXTURE_2D, surface->texture);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
187 surface->compositor = compositor;
188 surface->visual = NULL;
189 surface->image = EGL_NO_IMAGE_KHR;
190 surface->saved_texture = 0;
193 surface->width = width;
194 surface->height = height;
195 wlsc_matrix_init(&surface->matrix);
196 wlsc_matrix_scale(&surface->matrix, width, height, 1);
197 wlsc_matrix_translate(&surface->matrix, x, y, 0);
199 wlsc_matrix_init(&surface->matrix_inv);
200 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
201 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
207 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
208 int32_t x, int32_t y,
209 int32_t width, int32_t height)
211 struct wlsc_compositor *compositor = surface->compositor;
213 pixman_region32_union_rect(&compositor->damage_region,
214 &compositor->damage_region,
215 surface->x + x, surface->y + y,
217 wlsc_compositor_schedule_repaint(compositor);
221 wlsc_surface_damage(struct wlsc_surface *surface)
223 wlsc_surface_damage_rectangle(surface, 0, 0,
224 surface->width, surface->height);
228 wlsc_compositor_get_time(void)
232 gettimeofday(&tv, NULL);
234 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
238 destroy_surface(struct wl_resource *resource, struct wl_client *client)
240 struct wlsc_surface *surface =
241 container_of(resource, struct wlsc_surface, surface.resource);
242 struct wl_listener *l, *next;
243 struct wlsc_compositor *compositor = surface->compositor;
246 wlsc_surface_damage(surface);
248 wl_list_remove(&surface->link);
249 if (surface->saved_texture == 0)
250 glDeleteTextures(1, &surface->texture);
252 glDeleteTextures(1, &surface->saved_texture);
255 if (surface->image != EGL_NO_IMAGE_KHR)
256 compositor->destroy_image(compositor->display,
259 wl_list_remove(&surface->buffer_link);
261 time = wlsc_compositor_get_time();
262 wl_list_for_each_safe(l, next,
263 &surface->surface.destroy_listener_list, link)
264 l->func(l, &surface->surface, time);
270 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
272 struct wlsc_surface *es = (struct wlsc_surface *) surface;
273 struct wlsc_compositor *ec = es->compositor;
274 struct wl_list *surfaces_attached_to;
276 if (es->saved_texture != 0)
277 es->texture = es->saved_texture;
279 glBindTexture(GL_TEXTURE_2D, es->texture);
281 if (wl_buffer_is_shm(buffer)) {
282 /* Unbind any EGLImage texture that may be bound, so we don't
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
285 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
286 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
287 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
288 es->pitch, buffer->height, 0,
289 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
290 wl_shm_buffer_get_data(buffer));
291 es->visual = buffer->visual;
293 surfaces_attached_to = buffer->user_data;
295 wl_list_remove(&es->buffer_link);
296 wl_list_insert(surfaces_attached_to, &es->buffer_link);
298 es->image = ec->create_image(ec->display, NULL,
299 EGL_WAYLAND_BUFFER_WL,
302 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
303 es->visual = buffer->visual;
304 es->pitch = es->width;
309 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
311 struct wlsc_surface *es = (struct wlsc_surface *) surface;
312 struct wlsc_compositor *ec = es->compositor;
314 es->pitch = es->width;
315 es->image = sprite->image;
316 if (sprite->image != EGL_NO_IMAGE_KHR) {
317 glBindTexture(GL_TEXTURE_2D, es->texture);
318 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
320 if (es->saved_texture == 0)
321 es->saved_texture = es->texture;
322 es->texture = sprite->texture;
325 es->visual = sprite->visual;
329 SPRITE_USE_CURSOR = (1 << 0),
332 static struct wlsc_sprite *
333 create_sprite_from_png(struct wlsc_compositor *ec,
334 const char *filename, uint32_t usage)
337 struct wlsc_sprite *sprite;
338 int32_t width, height;
341 pixels = wlsc_load_image(filename, &width, &height, &stride);
345 sprite = malloc(sizeof *sprite);
346 if (sprite == NULL) {
351 sprite->visual = &ec->compositor.premultiplied_argb_visual;
352 sprite->width = width;
353 sprite->height = height;
354 sprite->image = EGL_NO_IMAGE_KHR;
356 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
357 sprite->image = ec->create_cursor_image(ec, width, height);
359 glGenTextures(1, &sprite->texture);
360 glBindTexture(GL_TEXTURE_2D, sprite->texture);
361 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
362 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
366 if (sprite->image != EGL_NO_IMAGE_KHR) {
367 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
368 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
369 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
371 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
372 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
380 static const struct {
381 const char *filename;
382 int hotspot_x, hotspot_y;
383 } pointer_images[] = {
384 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
385 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
386 { DATADIR "/wayland/bottom_side.png", 16, 20 },
387 { DATADIR "/wayland/grabbing.png", 20, 17 },
388 { DATADIR "/wayland/left_ptr.png", 10, 5 },
389 { DATADIR "/wayland/left_side.png", 10, 20 },
390 { DATADIR "/wayland/right_side.png", 30, 19 },
391 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
392 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
393 { DATADIR "/wayland/top_side.png", 18, 8 },
394 { DATADIR "/wayland/xterm.png", 15, 15 }
398 create_pointer_images(struct wlsc_compositor *ec)
402 count = ARRAY_LENGTH(pointer_images);
403 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
404 for (i = 0; i < count; i++) {
405 ec->pointer_sprites[i] =
406 create_sprite_from_png(ec,
407 pointer_images[i].filename,
412 static struct wlsc_surface *
413 background_create(struct wlsc_output *output, const char *filename)
415 struct wlsc_surface *background;
416 struct wlsc_sprite *sprite;
418 background = wlsc_surface_create(output->compositor,
419 output->x, output->y,
420 output->width, output->height);
421 if (background == NULL)
424 sprite = create_sprite_from_png(output->compositor, filename, 0);
425 if (sprite == NULL) {
430 wlsc_sprite_attach(sprite, &background->surface);
436 wlsc_surface_draw(struct wlsc_surface *es,
437 struct wlsc_output *output, pixman_region32_t *clip)
439 struct wlsc_compositor *ec = es->compositor;
440 GLfloat *v, inv_width, inv_height;
442 pixman_region32_t repaint;
443 pixman_box32_t *rectangles;
446 pixman_region32_init_rect(&repaint,
447 es->x, es->y, es->width, es->height);
448 pixman_region32_intersect(&repaint, &repaint, clip);
449 if (!pixman_region32_not_empty(&repaint))
452 if (es->visual == &ec->compositor.argb_visual) {
453 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
455 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
456 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
462 rectangles = pixman_region32_rectangles(&repaint, &n);
463 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
464 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
465 inv_width = 1.0 / es->pitch;
466 inv_height = 1.0 / es->height;
467 for (i = 0; i < n; i++, v += 16, p += 6) {
468 v[ 0] = rectangles[i].x1;
469 v[ 1] = rectangles[i].y1;
470 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
471 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
473 v[ 4] = rectangles[i].x1;
474 v[ 5] = rectangles[i].y2;
476 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
478 v[ 8] = rectangles[i].x2;
479 v[ 9] = rectangles[i].y1;
480 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
483 v[12] = rectangles[i].x2;
484 v[13] = rectangles[i].y2;
496 glBindTexture(GL_TEXTURE_2D, es->texture);
497 v = ec->vertices.data;
498 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
499 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
500 glEnableVertexAttribArray(0);
501 glEnableVertexAttribArray(1);
502 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
504 ec->vertices.size = 0;
505 ec->indices.size = 0;
506 pixman_region32_fini(&repaint);
510 wlsc_surface_raise(struct wlsc_surface *surface)
512 struct wlsc_compositor *compositor = surface->compositor;
514 wl_list_remove(&surface->link);
515 wl_list_insert(&compositor->surface_list, &surface->link);
519 wlsc_surface_update_matrix(struct wlsc_surface *es)
521 wlsc_matrix_init(&es->matrix);
522 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
523 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
525 wlsc_matrix_init(&es->matrix_inv);
526 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
527 wlsc_matrix_scale(&es->matrix_inv,
528 1.0 / es->width, 1.0 / es->height, 1);
532 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
534 struct wlsc_output *output;
536 wl_list_for_each(output, &compositor->output_list, link)
537 wlsc_output_damage(output);
541 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
543 struct wlsc_compositor *compositor = output->compositor;
544 struct wlsc_surface *es;
545 struct wlsc_animation *animation, *next;
547 wl_list_for_each(es, &compositor->surface_list, link) {
548 if (es->output == output) {
549 wl_display_post_frame(compositor->wl_display,
550 &es->surface, msecs);
554 output->finished = 1;
556 wl_event_source_timer_update(compositor->timer_source, 5);
557 compositor->repaint_on_timeout = 1;
559 wl_list_for_each_safe(animation, next,
560 &compositor->animation_list, link)
561 animation->frame(animation, output, msecs);
565 wlsc_output_damage(struct wlsc_output *output)
567 struct wlsc_compositor *compositor = output->compositor;
569 pixman_region32_union_rect(&compositor->damage_region,
570 &compositor->damage_region,
571 output->x, output->y,
572 output->width, output->height);
573 wlsc_compositor_schedule_repaint(compositor);
577 fade_frame(struct wlsc_animation *animation,
578 struct wlsc_output *output, uint32_t msecs)
580 struct wlsc_compositor *compositor =
581 container_of(animation,
582 struct wlsc_compositor, fade.animation);
584 wlsc_tweener_update(&compositor->fade.tweener, msecs);
585 if (wlsc_tweener_done(&compositor->fade.tweener)) {
586 if (compositor->fade.tweener.current > 0.999) {
587 compositor->state = WLSC_COMPOSITOR_SLEEPING;
588 compositor->shell->lock(compositor->shell);
590 compositor->fade.tweener.current =
591 compositor->fade.tweener.target;
592 wl_list_remove(&animation->link);
593 wl_list_init(&animation->link);
596 wlsc_output_damage(output);
600 fade_output(struct wlsc_output *output,
601 GLfloat tint, pixman_region32_t *region)
603 struct wlsc_compositor *compositor = output->compositor;
604 struct wlsc_surface surface;
605 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
607 surface.compositor = compositor;
608 surface.x = output->x;
609 surface.y = output->y;
610 surface.width = output->width;
611 surface.height = output->height;
612 surface.texture = GL_NONE;
616 &compositor->compositor.premultiplied_argb_visual;
618 surface.visual = &compositor->compositor.rgb_visual;
620 glUseProgram(compositor->solid_shader.program);
621 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
622 1, GL_FALSE, output->matrix.d);
623 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
624 wlsc_surface_draw(&surface, output, region);
628 wlsc_output_repaint(struct wlsc_output *output)
630 struct wlsc_compositor *ec = output->compositor;
631 struct wlsc_surface *es;
632 struct wlsc_input_device *eid;
633 pixman_region32_t new_damage, total_damage, repaint;
634 int using_hardware_cursor = 1;
636 output->prepare_render(output);
638 glViewport(0, 0, output->width, output->height);
640 glUseProgram(ec->texture_shader.program);
641 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
642 1, GL_FALSE, output->matrix.d);
643 glUniform1i(ec->texture_shader.tex_uniform, 0);
645 pixman_region32_init(&new_damage);
646 pixman_region32_init(&total_damage);
647 pixman_region32_intersect_rect(&new_damage,
649 output->x, output->y,
650 output->width, output->height);
651 pixman_region32_subtract(&ec->damage_region,
652 &ec->damage_region, &new_damage);
653 pixman_region32_union(&total_damage, &new_damage,
654 &output->previous_damage_region);
655 pixman_region32_copy(&output->previous_damage_region, &new_damage);
658 if (output->set_hardware_cursor(output, ec->input_device) < 0)
659 using_hardware_cursor = 0;
660 if (ec->fade.tweener.current > 0.001)
661 using_hardware_cursor = 0;
663 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
664 if (es->fullscreen_output == output) {
665 if (es->visual == &ec->compositor.rgb_visual &&
666 using_hardware_cursor) {
667 if (output->prepare_scanout_surface(output, es) == 0) {
668 /* We're drawing nothing now,
669 * draw the damaged regions later. */
670 pixman_region32_union(&ec->damage_region,
677 if (es->width < output->width ||
678 es->height < output->height)
679 glClear(GL_COLOR_BUFFER_BIT);
680 wlsc_surface_draw(es, output, &total_damage);
682 wl_list_for_each(es, &ec->surface_list, link) {
683 if (es->visual != &ec->compositor.rgb_visual)
686 pixman_region32_init_rect(&repaint,
688 es->width, es->height);
689 wlsc_surface_draw(es, output, &total_damage);
690 pixman_region32_subtract(&total_damage,
691 &total_damage, &repaint);
694 if (output->background)
695 wlsc_surface_draw(output->background,
696 output, &total_damage);
698 fade_output(output, 1.0, &total_damage);
700 glUseProgram(ec->texture_shader.program);
701 wl_list_for_each_reverse(es, &ec->surface_list, link) {
702 if (ec->overlay == es)
705 if (es->visual == &ec->compositor.rgb_visual) {
706 pixman_region32_union_rect(&total_damage,
709 es->width, es->height);
713 wlsc_surface_draw(es, output, &total_damage);
718 wlsc_surface_draw(ec->overlay, output, &total_damage);
721 wl_list_for_each(eid, &ec->input_device_list, link) {
722 if (&eid->input_device != ec->input_device ||
723 !using_hardware_cursor)
724 wlsc_surface_draw(eid->sprite, output,
728 if (ec->fade.tweener.current > 0.001)
729 fade_output(output, ec->fade.tweener.current, &total_damage);
735 struct wlsc_compositor *ec = data;
736 struct wlsc_output *output;
737 int repainted_all_outputs = 1;
739 wl_list_for_each(output, &ec->output_list, link) {
740 if (!output->repaint_needed)
743 if (!output->finished) {
744 repainted_all_outputs = 0;
748 wlsc_output_repaint(output);
749 output->finished = 0;
750 output->repaint_needed = 0;
751 output->present(output);
754 if (repainted_all_outputs)
755 ec->repaint_on_timeout = 0;
757 wl_event_source_timer_update(ec->timer_source, 1);
763 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
765 struct wlsc_output *output;
767 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
770 wl_list_for_each(output, &compositor->output_list, link)
771 output->repaint_needed = 1;
773 if (compositor->repaint_on_timeout)
776 wl_event_source_timer_update(compositor->timer_source, 1);
777 compositor->repaint_on_timeout = 1;
781 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
785 done = wlsc_tweener_done(&compositor->fade.tweener);
786 compositor->fade.tweener.target = tint;
787 if (wlsc_tweener_done(&compositor->fade.tweener))
791 compositor->fade.tweener.timestamp =
792 wlsc_compositor_get_time();
794 wlsc_compositor_damage_all(compositor);
795 if (wl_list_empty(&compositor->fade.animation.link))
796 wl_list_insert(compositor->animation_list.prev,
797 &compositor->fade.animation.link);
801 surface_destroy(struct wl_client *client,
802 struct wl_surface *surface)
804 wl_resource_destroy(&surface->resource, client);
808 wlsc_surface_assign_output(struct wlsc_surface *es)
810 struct wlsc_compositor *ec = es->compositor;
811 struct wlsc_output *output;
813 struct wlsc_output *tmp = es->output;
816 wl_list_for_each(output, &ec->output_list, link) {
817 if (output->x < es->x && es->x < output->x + output->width &&
818 output->y < es->y && es->y < output->y + output->height) {
820 printf("assiging surface %p to output %p\n",
826 if (es->output == NULL) {
827 printf("no output found\n");
828 es->output = container_of(ec->output_list.next,
829 struct wlsc_output, link);
834 surface_attach(struct wl_client *client,
835 struct wl_surface *surface, struct wl_buffer *buffer,
836 int32_t x, int32_t y)
838 struct wlsc_surface *es = (struct wlsc_surface *) surface;
840 /* FIXME: This damages the entire old surface, but we should
841 * really just damage the part that's no longer covered by the
842 * surface. Anything covered by the new surface will be
843 * damaged by the client. */
844 wlsc_surface_damage(es);
846 wlsc_buffer_attach(buffer, surface);
848 switch (es->map_type) {
849 case WLSC_SURFACE_MAP_FULLSCREEN:
850 es->x = (es->fullscreen_output->width - es->width) / 2;
851 es->y = (es->fullscreen_output->height - es->height) / 2;
858 es->width = buffer->width;
859 es->height = buffer->height;
860 if (x != 0 || y != 0)
861 wlsc_surface_assign_output(es);
862 wlsc_surface_update_matrix(es);
864 es->compositor->shell->attach(es->compositor->shell, es);
868 surface_map_toplevel(struct wl_client *client,
869 struct wl_surface *surface)
871 struct wlsc_surface *es = (struct wlsc_surface *) surface;
872 struct wlsc_compositor *ec = es->compositor;
874 switch (es->map_type) {
875 case WLSC_SURFACE_MAP_UNMAPPED:
876 es->x = 10 + random() % 400;
877 es->y = 10 + random() % 400;
878 wlsc_surface_update_matrix(es);
879 /* assign to first output */
880 es->output = container_of(ec->output_list.next,
881 struct wlsc_output, link);
882 wl_list_insert(&es->compositor->surface_list, &es->link);
884 case WLSC_SURFACE_MAP_TOPLEVEL:
886 case WLSC_SURFACE_MAP_FULLSCREEN:
887 es->fullscreen_output = NULL;
890 wlsc_surface_update_matrix(es);
896 wlsc_surface_damage(es);
897 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
901 surface_map_transient(struct wl_client *client,
902 struct wl_surface *surface, struct wl_surface *parent,
903 int x, int y, uint32_t flags)
905 struct wlsc_surface *es = (struct wlsc_surface *) surface;
906 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
908 switch (es->map_type) {
909 case WLSC_SURFACE_MAP_UNMAPPED:
910 wl_list_insert(&es->compositor->surface_list, &es->link);
911 /* assign to parents output */
912 es->output = pes->output;
914 case WLSC_SURFACE_MAP_FULLSCREEN:
915 es->fullscreen_output = NULL;
924 wlsc_surface_update_matrix(es);
925 wlsc_surface_damage(es);
926 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
930 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
932 struct wlsc_surface *es = (struct wlsc_surface *) surface;
933 struct wlsc_output *output;
935 /* FIXME: Fullscreen on first output */
936 /* FIXME: Handle output going away */
937 output = container_of(es->compositor->output_list.next,
938 struct wlsc_output, link);
940 switch (es->map_type) {
941 case WLSC_SURFACE_MAP_UNMAPPED:
942 es->x = 10 + random() % 400;
943 es->y = 10 + random() % 400;
944 /* assign to first output */
946 wl_list_insert(&es->compositor->surface_list, &es->link);
948 case WLSC_SURFACE_MAP_FULLSCREEN:
956 es->x = (output->width - es->width) / 2;
957 es->y = (output->height - es->height) / 2;
958 es->fullscreen_output = output;
959 wlsc_surface_update_matrix(es);
960 wlsc_surface_damage(es);
961 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
965 surface_damage(struct wl_client *client,
966 struct wl_surface *surface,
967 int32_t x, int32_t y, int32_t width, int32_t height)
969 struct wlsc_surface *es = (struct wlsc_surface *) surface;
971 wlsc_surface_damage_rectangle(es, x, y, width, height);
974 const static struct wl_surface_interface surface_interface = {
977 surface_map_toplevel,
978 surface_map_transient,
979 surface_map_fullscreen,
984 wlsc_input_device_attach(struct wlsc_input_device *device,
985 int x, int y, int width, int height)
987 wlsc_surface_damage(device->sprite);
989 device->hotspot_x = x;
990 device->hotspot_y = y;
992 device->sprite->x = device->input_device.x - device->hotspot_x;
993 device->sprite->y = device->input_device.y - device->hotspot_y;
994 device->sprite->width = width;
995 device->sprite->height = height;
996 wlsc_surface_update_matrix(device->sprite);
998 wlsc_surface_damage(device->sprite);
1002 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1003 struct wl_buffer *buffer, int x, int y)
1005 wlsc_buffer_attach(buffer, &device->sprite->surface);
1006 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1010 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1011 struct wlsc_sprite *sprite, int x, int y)
1013 wlsc_sprite_attach(sprite, &device->sprite->surface);
1014 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1018 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1019 enum wlsc_pointer_type type)
1021 struct wlsc_compositor *compositor =
1022 (struct wlsc_compositor *) device->input_device.compositor;
1024 wlsc_input_device_attach_sprite(device,
1025 compositor->pointer_sprites[type],
1026 pointer_images[type].hotspot_x,
1027 pointer_images[type].hotspot_y);
1031 compositor_create_surface(struct wl_client *client,
1032 struct wl_compositor *compositor, uint32_t id)
1034 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
1035 struct wlsc_surface *surface;
1037 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1038 if (surface == NULL) {
1039 wl_client_post_no_memory(client);
1043 surface->surface.resource.destroy = destroy_surface;
1045 surface->surface.resource.object.id = id;
1046 surface->surface.resource.object.interface = &wl_surface_interface;
1047 surface->surface.resource.object.implementation =
1048 (void (**)(void)) &surface_interface;
1049 surface->surface.client = client;
1051 wl_client_add_resource(client, &surface->surface.resource);
1054 const static struct wl_compositor_interface compositor_interface = {
1055 compositor_create_surface,
1059 wlsc_surface_transform(struct wlsc_surface *surface,
1060 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1062 struct wlsc_vector v = { { x, y, 0, 1 } };
1064 wlsc_matrix_transform(&surface->matrix_inv, &v);
1065 *sx = v.f[0] * surface->width;
1066 *sy = v.f[1] * surface->height;
1069 struct wlsc_surface *
1070 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1072 struct wlsc_compositor *ec =
1073 (struct wlsc_compositor *) device->compositor;
1074 struct wlsc_surface *es;
1076 wl_list_for_each(es, &ec->surface_list, link) {
1077 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1078 if (0 <= *sx && *sx < es->width &&
1079 0 <= *sy && *sy < es->height)
1088 motion_grab_motion(struct wl_grab *grab,
1089 uint32_t time, int32_t x, int32_t y)
1091 struct wlsc_input_device *device =
1092 (struct wlsc_input_device *) grab->input_device;
1093 struct wlsc_surface *es =
1094 (struct wlsc_surface *) device->input_device.pointer_focus;
1097 wlsc_surface_transform(es, x, y, &sx, &sy);
1098 wl_client_post_event(es->surface.client,
1099 &device->input_device.object,
1100 WL_INPUT_DEVICE_MOTION,
1101 time, x, y, sx, sy);
1105 motion_grab_button(struct wl_grab *grab,
1106 uint32_t time, int32_t button, int32_t state)
1108 wl_client_post_event(grab->input_device->pointer_focus->client,
1109 &grab->input_device->object,
1110 WL_INPUT_DEVICE_BUTTON,
1111 time, button, state);
1115 motion_grab_end(struct wl_grab *grab, uint32_t time)
1119 static const struct wl_grab_interface motion_grab_interface = {
1126 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1128 if (compositor->idle_inhibit)
1131 wlsc_compositor_fade(compositor, 0.0);
1132 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1134 wl_event_source_timer_update(compositor->idle_source,
1135 option_idle_time * 1000);
1139 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1141 wlsc_compositor_wake(compositor);
1142 compositor->idle_inhibit++;
1146 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1148 compositor->idle_inhibit--;
1149 wlsc_compositor_wake(compositor);
1153 idle_handler(void *data)
1155 struct wlsc_compositor *compositor = data;
1157 if (compositor->idle_inhibit)
1160 wlsc_compositor_fade(compositor, 1.0);
1166 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1168 struct wlsc_surface *es;
1169 struct wlsc_compositor *ec =
1170 (struct wlsc_compositor *) device->compositor;
1171 struct wlsc_output *output;
1172 const struct wl_grab_interface *interface;
1173 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1175 int x_valid = 0, y_valid = 0;
1176 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1178 wlsc_compositor_wake(ec);
1180 wl_list_for_each(output, &ec->output_list, link) {
1181 if (output->x <= x && x <= output->x + output->width)
1184 if (output->y <= y && y <= output->y + output->height)
1187 /* FIXME: calculate this only on output addition/deletion */
1188 if (output->x < min_x)
1190 if (output->y < min_y)
1193 if (output->x + output->width > max_x)
1194 max_x = output->x + output->width;
1195 if (output->y + output->height > max_y)
1196 max_y = output->y + output->height;
1202 else if (x >= max_x)
1208 else if (y >= max_y)
1216 interface = device->grab->interface;
1217 interface->motion(device->grab, time, x, y);
1219 es = pick_surface(device, &sx, &sy);
1220 wl_input_device_set_pointer_focus(device,
1222 time, x, y, sx, sy);
1224 wl_client_post_event(es->surface.client,
1226 WL_INPUT_DEVICE_MOTION,
1227 time, x, y, sx, sy);
1230 wlsc_surface_damage(wd->sprite);
1232 wd->sprite->x = device->x - wd->hotspot_x;
1233 wd->sprite->y = device->y - wd->hotspot_y;
1234 wlsc_surface_update_matrix(wd->sprite);
1236 wlsc_surface_damage(wd->sprite);
1240 wlsc_surface_activate(struct wlsc_surface *surface,
1241 struct wlsc_input_device *device, uint32_t time)
1243 wlsc_surface_raise(surface);
1244 if (device->selection)
1245 wlsc_selection_set_focus(device->selection,
1246 &surface->surface, time);
1248 wl_input_device_set_keyboard_focus(&device->input_device,
1253 struct wlsc_binding {
1257 wlsc_binding_handler_t handler;
1259 struct wl_list link;
1263 notify_button(struct wl_input_device *device,
1264 uint32_t time, int32_t button, int32_t state)
1266 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1267 struct wlsc_compositor *compositor =
1268 (struct wlsc_compositor *) device->compositor;
1269 struct wlsc_binding *b;
1270 struct wlsc_surface *surface =
1271 (struct wlsc_surface *) device->pointer_focus;
1274 wlsc_compositor_idle_inhibit(compositor);
1276 wlsc_compositor_idle_release(compositor);
1278 if (state && surface && device->grab == NULL) {
1279 wlsc_surface_activate(surface, wd, time);
1280 wl_input_device_start_grab(device,
1281 &device->motion_grab,
1285 wl_list_for_each(b, &compositor->binding_list, link) {
1286 if (b->button == button &&
1287 b->modifier == wd->modifier_state && state) {
1288 b->handler(&wd->input_device,
1289 time, 0, button, state, b->data);
1295 device->grab->interface->button(device->grab, time,
1298 if (!state && device->grab && device->grab_button == button)
1299 wl_input_device_end_grab(device, time);
1303 terminate_binding(struct wl_input_device *device, uint32_t time,
1304 uint32_t key, uint32_t button, uint32_t state, void *data)
1306 struct wlsc_compositor *compositor = data;
1309 wl_display_terminate(compositor->wl_display);
1312 WL_EXPORT struct wlsc_binding *
1313 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1314 uint32_t key, uint32_t button, uint32_t modifier,
1315 wlsc_binding_handler_t handler, void *data)
1317 struct wlsc_binding *binding;
1319 binding = malloc(sizeof *binding);
1320 if (binding == NULL)
1324 binding->button = button;
1325 binding->modifier = modifier;
1326 binding->handler = handler;
1327 binding->data = data;
1328 wl_list_insert(compositor->binding_list.prev, &binding->link);
1334 wlsc_binding_destroy(struct wlsc_binding *binding)
1336 wl_list_remove(&binding->link);
1341 update_modifier_state(struct wlsc_input_device *device,
1342 uint32_t key, uint32_t state)
1349 modifier = MODIFIER_CTRL;
1354 modifier = MODIFIER_ALT;
1359 modifier = MODIFIER_SUPER;
1368 device->modifier_state |= modifier;
1370 device->modifier_state &= ~modifier;
1374 notify_key(struct wl_input_device *device,
1375 uint32_t time, uint32_t key, uint32_t state)
1377 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1378 struct wlsc_compositor *compositor =
1379 (struct wlsc_compositor *) device->compositor;
1381 struct wlsc_binding *b;
1384 wlsc_compositor_idle_inhibit(compositor);
1386 wlsc_compositor_idle_release(compositor);
1388 wl_list_for_each(b, &compositor->binding_list, link) {
1389 if (b->key == key &&
1390 b->modifier == wd->modifier_state) {
1391 b->handler(&wd->input_device,
1392 time, key, 0, state, b->data);
1397 update_modifier_state(wd, key, state);
1398 end = device->keys.data + device->keys.size;
1399 for (k = device->keys.data; k < end; k++) {
1403 device->keys.size = (void *) end - device->keys.data;
1405 k = wl_array_add(&device->keys, sizeof *k);
1409 if (device->keyboard_focus != NULL)
1410 wl_client_post_event(device->keyboard_focus->client,
1412 WL_INPUT_DEVICE_KEY, time, key, state);
1416 notify_pointer_focus(struct wl_input_device *device,
1417 uint32_t time, struct wlsc_output *output,
1418 int32_t x, int32_t y)
1420 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1421 struct wlsc_compositor *compositor =
1422 (struct wlsc_compositor *) device->compositor;
1423 struct wlsc_surface *es;
1429 es = pick_surface(device, &sx, &sy);
1430 wl_input_device_set_pointer_focus(device,
1432 time, x, y, sx, sy);
1434 compositor->focus = 1;
1436 wd->sprite->x = device->x - wd->hotspot_x;
1437 wd->sprite->y = device->y - wd->hotspot_y;
1438 wlsc_surface_update_matrix(wd->sprite);
1440 wl_input_device_set_pointer_focus(device, NULL,
1442 compositor->focus = 0;
1445 wlsc_surface_damage(wd->sprite);
1449 notify_keyboard_focus(struct wl_input_device *device,
1450 uint32_t time, struct wlsc_output *output,
1451 struct wl_array *keys)
1453 struct wlsc_input_device *wd =
1454 (struct wlsc_input_device *) device;
1455 struct wlsc_compositor *compositor =
1456 (struct wlsc_compositor *) device->compositor;
1457 struct wlsc_surface *es;
1460 if (!wl_list_empty(&compositor->surface_list))
1461 es = container_of(compositor->surface_list.next,
1462 struct wlsc_surface, link);
1467 wl_array_copy(&wd->input_device.keys, keys);
1468 wd->modifier_state = 0;
1469 end = device->keys.data + device->keys.size;
1470 for (k = device->keys.data; k < end; k++) {
1471 wlsc_compositor_idle_inhibit(compositor);
1472 update_modifier_state(wd, *k, 1);
1475 wl_input_device_set_keyboard_focus(&wd->input_device,
1476 &es->surface, time);
1478 end = device->keys.data + device->keys.size;
1479 for (k = device->keys.data; k < end; k++)
1480 wlsc_compositor_idle_release(compositor);
1482 wd->modifier_state = 0;
1483 wl_input_device_set_keyboard_focus(&wd->input_device,
1490 input_device_attach(struct wl_client *client,
1491 struct wl_input_device *device_base,
1493 struct wl_buffer *buffer, int32_t x, int32_t y)
1495 struct wlsc_input_device *device =
1496 (struct wlsc_input_device *) device_base;
1498 if (time < device->input_device.pointer_focus_time)
1500 if (device->input_device.pointer_focus == NULL)
1502 if (device->input_device.pointer_focus->client != client)
1505 if (buffer == NULL) {
1506 wlsc_input_device_set_pointer_image(device,
1507 WLSC_POINTER_LEFT_PTR);
1511 wlsc_input_device_attach_buffer(device, buffer, x, y);
1514 const static struct wl_input_device_interface input_device_interface = {
1515 input_device_attach,
1519 wlsc_input_device_init(struct wlsc_input_device *device,
1520 struct wlsc_compositor *ec)
1522 wl_input_device_init(&device->input_device, &ec->compositor);
1524 device->input_device.object.interface = &wl_input_device_interface;
1525 device->input_device.object.implementation =
1526 (void (**)(void)) &input_device_interface;
1527 wl_display_add_object(ec->wl_display, &device->input_device.object);
1528 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1530 device->sprite = wlsc_surface_create(ec,
1531 device->input_device.x,
1532 device->input_device.y, 32, 32);
1533 device->hotspot_x = 16;
1534 device->hotspot_y = 16;
1535 device->modifier_state = 0;
1537 device->input_device.motion_grab.interface = &motion_grab_interface;
1539 wl_list_insert(ec->input_device_list.prev, &device->link);
1541 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1545 wlsc_output_post_geometry(struct wl_client *client,
1546 struct wl_object *global, uint32_t version)
1548 struct wlsc_output *output =
1549 container_of(global, struct wlsc_output, object);
1551 wl_client_post_event(client, global,
1553 output->x, output->y,
1554 output->width, output->height);
1557 static const char vertex_shader[] =
1558 "uniform mat4 proj;\n"
1559 "attribute vec2 position;\n"
1560 "attribute vec2 texcoord;\n"
1561 "varying vec2 v_texcoord;\n"
1564 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1565 " v_texcoord = texcoord;\n"
1568 static const char texture_fragment_shader[] =
1569 "precision mediump float;\n"
1570 "varying vec2 v_texcoord;\n"
1571 "uniform sampler2D tex;\n"
1574 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1577 static const char solid_fragment_shader[] =
1578 "precision mediump float;\n"
1579 "varying vec2 v_texcoord;\n"
1580 "uniform vec4 color;\n"
1583 " gl_FragColor = color\n;"
1587 compile_shader(GLenum type, const char *source)
1593 s = glCreateShader(type);
1594 glShaderSource(s, 1, &source, NULL);
1596 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1598 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1599 fprintf(stderr, "shader info: %s\n", msg);
1607 wlsc_shader_init(struct wlsc_shader *shader,
1608 const char *vertex_source, const char *fragment_source)
1613 shader->vertex_shader =
1614 compile_shader(GL_VERTEX_SHADER, vertex_source);
1615 shader->fragment_shader =
1616 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1618 shader->program = glCreateProgram();
1619 glAttachShader(shader->program, shader->vertex_shader);
1620 glAttachShader(shader->program, shader->fragment_shader);
1621 glBindAttribLocation(shader->program, 0, "position");
1622 glBindAttribLocation(shader->program, 1, "texcoord");
1624 glLinkProgram(shader->program);
1625 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1627 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1628 fprintf(stderr, "link info: %s\n", msg);
1632 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1633 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1639 init_solid_shader(struct wlsc_shader *shader,
1640 GLuint vertex_shader, const char *fragment_source)
1645 shader->vertex_shader = vertex_shader;
1646 shader->fragment_shader =
1647 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1649 shader->program = glCreateProgram();
1650 glAttachShader(shader->program, shader->vertex_shader);
1651 glAttachShader(shader->program, shader->fragment_shader);
1652 glBindAttribLocation(shader->program, 0, "position");
1653 glBindAttribLocation(shader->program, 1, "texcoord");
1655 glLinkProgram(shader->program);
1656 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1658 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1659 fprintf(stderr, "link info: %s\n", msg);
1663 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1664 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1670 wlsc_output_destroy(struct wlsc_output *output)
1672 destroy_surface(&output->background->surface.resource, NULL);
1676 wlsc_output_move(struct wlsc_output *output, int x, int y)
1678 struct wlsc_compositor *c = output->compositor;
1684 if (output->background) {
1685 output->background->x = x;
1686 output->background->y = y;
1687 wlsc_surface_update_matrix(output->background);
1690 pixman_region32_init(&output->previous_damage_region);
1692 wlsc_matrix_init(&output->matrix);
1693 wlsc_matrix_translate(&output->matrix,
1694 -output->x - output->width / 2.0,
1695 -output->y - output->height / 2.0, 0);
1697 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1698 wlsc_matrix_scale(&output->matrix,
1699 2.0 / output->width,
1700 flip * 2.0 / output->height, 1);
1702 pixman_region32_union_rect(&c->damage_region,
1704 x, y, output->width, output->height);
1708 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1709 int x, int y, int width, int height, uint32_t flags)
1711 output->compositor = c;
1714 output->width = width;
1715 output->height = height;
1717 output->background =
1718 background_create(output, option_background);
1720 output->flags = flags;
1721 output->finished = 1;
1722 wlsc_output_move(output, x, y);
1724 output->object.interface = &wl_output_interface;
1725 wl_display_add_object(c->wl_display, &output->object);
1726 wl_display_add_global(c->wl_display, &output->object,
1727 wlsc_output_post_geometry);
1731 shm_buffer_created(struct wl_buffer *buffer)
1733 struct wl_list *surfaces_attached_to;
1735 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1736 if (!surfaces_attached_to) {
1737 buffer->user_data = NULL;
1741 wl_list_init(surfaces_attached_to);
1743 buffer->user_data = surfaces_attached_to;
1747 shm_buffer_damaged(struct wl_buffer *buffer,
1748 int32_t x, int32_t y, int32_t width, int32_t height)
1750 struct wl_list *surfaces_attached_to = buffer->user_data;
1751 struct wlsc_surface *es;
1752 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1754 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1755 glBindTexture(GL_TEXTURE_2D, es->texture);
1756 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1757 tex_width, buffer->height, 0,
1758 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1759 wl_shm_buffer_get_data(buffer));
1760 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1761 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1766 shm_buffer_destroyed(struct wl_buffer *buffer)
1768 struct wl_list *surfaces_attached_to = buffer->user_data;
1769 struct wlsc_surface *es, *next;
1771 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1772 wl_list_remove(&es->buffer_link);
1773 wl_list_init(&es->buffer_link);
1776 free(surfaces_attached_to);
1779 const static struct wl_shm_callbacks shm_callbacks = {
1782 shm_buffer_destroyed
1786 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1788 struct wl_event_loop *loop;
1789 const char *extensions;
1791 ec->wl_display = display;
1793 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1795 ec->shm = wl_shm_init(display, &shm_callbacks);
1797 ec->image_target_texture_2d =
1798 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1799 ec->image_target_renderbuffer_storage = (void *)
1800 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1801 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1802 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1804 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1805 ec->unbind_display =
1806 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1808 extensions = (const char *) glGetString(GL_EXTENSIONS);
1809 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1811 "GL_EXT_texture_format_BGRA8888 not available\n");
1816 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1817 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1818 ec->has_bind_display = 1;
1819 if (ec->has_bind_display)
1820 ec->bind_display(ec->display, ec->wl_display);
1822 wl_list_init(&ec->surface_list);
1823 wl_list_init(&ec->input_device_list);
1824 wl_list_init(&ec->output_list);
1825 wl_list_init(&ec->binding_list);
1826 wl_list_init(&ec->animation_list);
1827 wlsc_tweener_init(&ec->fade.tweener, 0.8, 0.0, 0.0);
1828 ec->fade.animation.frame = fade_frame;
1829 wl_list_init(&ec->fade.animation.link);
1831 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1832 MODIFIER_CTRL | MODIFIER_ALT,
1833 terminate_binding, ec);
1835 create_pointer_images(ec);
1837 screenshooter_create(ec);
1839 glActiveTexture(GL_TEXTURE0);
1841 if (wlsc_shader_init(&ec->texture_shader,
1842 vertex_shader, texture_fragment_shader) < 0)
1844 if (init_solid_shader(&ec->solid_shader,
1845 ec->texture_shader.vertex_shader,
1846 solid_fragment_shader) < 0)
1849 loop = wl_display_get_event_loop(ec->wl_display);
1850 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
1851 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
1853 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1854 pixman_region32_init(&ec->damage_region);
1855 wlsc_compositor_schedule_repaint(ec);
1860 static int on_term_signal(int signal_number, void *data)
1862 struct wlsc_compositor *ec = data;
1864 wl_display_terminate(ec->wl_display);
1869 int main(int argc, char *argv[])
1871 struct wl_display *display;
1872 struct wlsc_compositor *ec;
1873 struct wl_event_loop *loop;
1874 int width, height, o;
1876 int (*shell_init)(struct wlsc_compositor *ec);
1879 static const char opts[] = "b:c:g:S:i:s:";
1880 static const struct option longopts[ ] = {
1881 { "background", 1, NULL, 'b' },
1882 { "connector", 1, NULL, 'c' },
1883 { "geometry", 1, NULL, 'g' },
1884 { "socket", 1, NULL, 'S' },
1885 { "idle-time", 1, NULL, 'i' },
1886 { "shell", 1, NULL, 's' },
1893 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
1896 option_background = optarg;
1899 option_connector = strtol(optarg, &p, 0);
1902 "invalid connection option: %s\n",
1908 if (sscanf(optarg, "%dx%d", &width, &height) != 2) {
1910 "invalid geometry option: %s\n",
1916 option_socket_name = optarg;
1919 option_idle_time = strtol(optarg, &p, 0);
1922 "invalid idle time option: %s\n",
1928 option_shell = optarg;
1933 display = wl_display_create();
1937 shell_init = desktop_shell_init;
1939 shell_module = dlopen(option_shell, RTLD_LAZY);
1940 if (!shell_module) {
1941 fprintf(stderr, "failed to load shell module: %m\n");
1944 shell_init = dlsym(shell_module, "shell_init");
1947 "failed to lookup shell init function: %m\n");
1952 #if BUILD_WAYLAND_COMPOSITOR
1953 if (getenv("WAYLAND_DISPLAY"))
1954 ec = wayland_compositor_create(display, width, height);
1957 #if BUILD_X11_COMPOSITOR
1958 if (ec == NULL && getenv("DISPLAY"))
1959 ec = x11_compositor_create(display, width, height);
1962 #if BUILD_OPENWFD_COMPOSITOR
1963 if (ec == NULL && getenv("OPENWFD"))
1964 ec = wfd_compositor_create(display, option_connector);
1967 #if BUILD_DRM_COMPOSITOR
1969 ec = drm_compositor_create(display, option_connector);
1973 fprintf(stderr, "failed to create compositor\n");
1977 if (shell_init(ec) < 0)
1980 if (wl_display_add_socket(display, option_socket_name)) {
1981 fprintf(stderr, "failed to add socket: %m\n");
1985 loop = wl_display_get_event_loop(ec->wl_display);
1986 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1987 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1989 wl_display_run(display);
1991 if (ec->has_bind_display)
1992 ec->unbind_display(ec->display, display);
1993 wl_display_destroy(display);