2 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 #include <sys/ioctl.h>
40 #include <linux/input.h>
45 #include "wayland-server.h"
46 #include "compositor.h"
48 /* The plan here is to generate a random anonymous socket name and
49 * advertise that through a service on the session dbus.
51 static const char *option_socket_name = NULL;
52 static int option_idle_time = 300;
54 static struct wl_list child_process_list;
57 sigchld_handler(int signal_number, void *data)
59 struct wlsc_process *p;
64 wl_list_for_each(p, &child_process_list, link) {
69 if (&p->link == &child_process_list) {
70 fprintf(stderr, "unknown child process exited\n");
74 wl_list_remove(&p->link);
75 p->cleanup(p, status);
81 wlsc_watch_process(struct wlsc_process *process)
83 wl_list_insert(&child_process_list, &process->link);
87 wlsc_matrix_init(struct wlsc_matrix *matrix)
89 static const struct wlsc_matrix identity = {
90 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
93 memcpy(matrix, &identity, sizeof identity);
97 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
99 struct wlsc_matrix tmp;
100 const GLfloat *row, *column;
104 for (i = 0; i < 16; i++) {
107 row = m->d + d.quot * 4;
108 column = n->d + d.rem;
109 for (j = 0; j < 4; j++)
110 tmp.d[i] += row[j] * column[j * 4];
112 memcpy(m, &tmp, sizeof tmp);
116 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
118 struct wlsc_matrix translate = {
119 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
122 wlsc_matrix_multiply(matrix, &translate);
126 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
128 struct wlsc_matrix scale = {
129 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
132 wlsc_matrix_multiply(matrix, &scale);
136 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
139 struct wlsc_vector t;
141 for (i = 0; i < 4; i++) {
143 for (j = 0; j < 4; j++)
144 t.f[i] += v->f[j] * matrix->d[i + j * 4];
151 wlsc_spring_init(struct wlsc_spring *spring,
152 double k, double current, double target)
155 spring->friction = 100.0;
156 spring->current = current;
157 spring->previous = current;
158 spring->target = target;
162 wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec)
164 double force, v, current, step;
166 step = (msec - spring->timestamp) / 300.0;
167 spring->timestamp = msec;
169 current = spring->current;
170 v = current - spring->previous;
171 force = spring->k * (spring->target - current) / 10.0 +
172 (spring->previous - current) - v * spring->friction;
175 current + (current - spring->previous) + force * step * step;
176 spring->previous = current;
179 if (spring->current >= 1.0) {
180 #ifdef TWEENER_BOUNCE
181 spring->current = 2.0 - spring->current;
182 spring->previous = 2.0 - spring->previous;
184 spring->current = 1.0;
185 spring->previous = 1.0;
189 if (spring->current <= 0.0) {
190 spring->current = 0.0;
191 spring->previous = 0.0;
197 wlsc_spring_done(struct wlsc_spring *spring)
199 return fabs(spring->previous - spring->target) < 0.0002 &&
200 fabs(spring->current - spring->target) < 0.0002;
204 surface_handle_buffer_destroy(struct wl_listener *listener,
205 struct wl_resource *resource, uint32_t time)
207 struct wlsc_surface *es = container_of(listener, struct wlsc_surface,
208 buffer_destroy_listener);
209 struct wl_buffer *buffer = (struct wl_buffer *) resource;
211 if (es->buffer == buffer)
216 output_handle_scanout_buffer_destroy(struct wl_listener *listener,
217 struct wl_resource *resource,
220 struct wlsc_output *output =
221 container_of(listener, struct wlsc_output,
222 scanout_buffer_destroy_listener);
223 struct wl_buffer *buffer = (struct wl_buffer *) resource;
225 if (output->scanout_buffer == buffer)
226 output->scanout_buffer = NULL;
229 WL_EXPORT struct wlsc_surface *
230 wlsc_surface_create(struct wlsc_compositor *compositor,
231 int32_t x, int32_t y, int32_t width, int32_t height)
233 struct wlsc_surface *surface;
235 surface = malloc(sizeof *surface);
239 wl_list_init(&surface->link);
240 wl_list_init(&surface->buffer_link);
241 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
243 glGenTextures(1, &surface->texture);
244 glBindTexture(GL_TEXTURE_2D, surface->texture);
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
248 surface->surface.resource.client = NULL;
250 surface->compositor = compositor;
251 surface->visual = WLSC_NONE_VISUAL;
252 surface->image = EGL_NO_IMAGE_KHR;
253 surface->saved_texture = 0;
256 surface->width = width;
257 surface->height = height;
259 surface->buffer = NULL;
260 surface->output = NULL;
262 pixman_region32_init(&surface->damage);
263 pixman_region32_init(&surface->opaque);
265 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
266 wl_list_init(&surface->buffer_destroy_listener.link);
268 surface->transform = NULL;
274 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
275 int32_t x, int32_t y,
276 int32_t width, int32_t height)
278 struct wlsc_compositor *compositor = surface->compositor;
280 pixman_region32_union_rect(&surface->damage,
282 surface->x + x, surface->y + y,
284 wlsc_compositor_schedule_repaint(compositor);
288 wlsc_surface_damage(struct wlsc_surface *surface)
290 wlsc_surface_damage_rectangle(surface, 0, 0,
291 surface->width, surface->height);
295 wlsc_surface_damage_below(struct wlsc_surface *surface)
297 struct wlsc_surface *below;
299 if (surface->link.next == &surface->compositor->surface_list)
302 below = container_of(surface->link.next, struct wlsc_surface, link);
304 pixman_region32_union_rect(&below->damage,
306 surface->x, surface->y,
307 surface->width, surface->height);
308 wlsc_compositor_schedule_repaint(surface->compositor);
312 wlsc_surface_configure(struct wlsc_surface *surface,
313 int x, int y, int width, int height)
315 wlsc_surface_damage_below(surface);
319 surface->width = width;
320 surface->height = height;
322 wlsc_surface_assign_output(surface);
323 wlsc_surface_damage(surface);
325 pixman_region32_fini(&surface->opaque);
326 if (surface->visual == WLSC_RGB_VISUAL)
327 pixman_region32_init_rect(&surface->opaque,
328 surface->x, surface->y,
329 surface->width, surface->height);
331 pixman_region32_init(&surface->opaque);
335 wlsc_compositor_get_time(void)
339 gettimeofday(&tv, NULL);
341 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
345 wlsc_compositor_repick(struct wlsc_compositor *compositor)
347 struct wlsc_input_device *device;
348 struct wlsc_surface *surface;
352 time = wlsc_compositor_get_time();
353 wl_list_for_each(device, &compositor->input_device_list, link) {
354 if (device->input_device.grab)
356 surface = pick_surface(&device->input_device, &sx, &sy);
357 wl_input_device_set_pointer_focus(&device->input_device,
360 device->input_device.x,
361 device->input_device.y,
367 destroy_surface(struct wl_resource *resource)
369 struct wlsc_surface *surface =
370 container_of(resource, struct wlsc_surface, surface.resource);
371 struct wlsc_compositor *compositor = surface->compositor;
373 wlsc_surface_damage_below(surface);
375 wl_list_remove(&surface->link);
376 wlsc_compositor_repick(compositor);
378 if (surface->saved_texture == 0)
379 glDeleteTextures(1, &surface->texture);
381 glDeleteTextures(1, &surface->saved_texture);
384 wl_list_remove(&surface->buffer_destroy_listener.link);
386 if (surface->image != EGL_NO_IMAGE_KHR)
387 compositor->destroy_image(compositor->display,
390 wl_list_remove(&surface->buffer_link);
396 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
398 struct wlsc_surface *es = (struct wlsc_surface *) surface;
399 struct wlsc_compositor *ec = es->compositor;
400 struct wl_list *surfaces_attached_to;
402 if (es->saved_texture != 0)
403 es->texture = es->saved_texture;
405 glBindTexture(GL_TEXTURE_2D, es->texture);
407 if (wl_buffer_is_shm(buffer)) {
408 /* Unbind any EGLImage texture that may be bound, so we don't
410 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
411 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
412 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
413 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
414 es->pitch, buffer->height, 0,
415 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
416 wl_shm_buffer_get_data(buffer));
418 switch (wl_shm_buffer_get_format(buffer)) {
419 case WL_SHM_FORMAT_ARGB32:
420 es->visual = WLSC_ARGB_VISUAL;
422 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
423 es->visual = WLSC_PREMUL_ARGB_VISUAL;
425 case WL_SHM_FORMAT_XRGB32:
426 es->visual = WLSC_RGB_VISUAL;
430 surfaces_attached_to = buffer->user_data;
432 wl_list_remove(&es->buffer_link);
433 wl_list_insert(surfaces_attached_to, &es->buffer_link);
435 if (es->image != EGL_NO_IMAGE_KHR)
436 ec->destroy_image(ec->display, es->image);
437 es->image = ec->create_image(ec->display, NULL,
438 EGL_WAYLAND_BUFFER_WL,
441 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
443 /* FIXME: we need to get the visual from the wl_buffer */
444 es->visual = WLSC_PREMUL_ARGB_VISUAL;
445 es->pitch = es->width;
450 wlsc_sprite_attach(struct wlsc_sprite *sprite, struct wl_surface *surface)
452 struct wlsc_surface *es = (struct wlsc_surface *) surface;
453 struct wlsc_compositor *ec = es->compositor;
455 es->pitch = es->width;
456 es->image = sprite->image;
457 if (sprite->image != EGL_NO_IMAGE_KHR) {
458 glBindTexture(GL_TEXTURE_2D, es->texture);
459 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
461 if (es->saved_texture == 0)
462 es->saved_texture = es->texture;
463 es->texture = sprite->texture;
466 es->visual = sprite->visual;
473 SPRITE_USE_CURSOR = (1 << 0),
476 static struct wlsc_sprite *
477 create_sprite_from_png(struct wlsc_compositor *ec,
478 const char *filename, uint32_t usage)
481 struct wlsc_sprite *sprite;
482 int32_t width, height;
485 pixels = wlsc_load_image(filename, &width, &height, &stride);
489 sprite = malloc(sizeof *sprite);
490 if (sprite == NULL) {
495 sprite->visual = WLSC_PREMUL_ARGB_VISUAL;
496 sprite->width = width;
497 sprite->height = height;
498 sprite->image = EGL_NO_IMAGE_KHR;
500 if (usage & SPRITE_USE_CURSOR && ec->create_cursor_image != NULL)
501 sprite->image = ec->create_cursor_image(ec, width, height);
503 glGenTextures(1, &sprite->texture);
504 glBindTexture(GL_TEXTURE_2D, sprite->texture);
505 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
506 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
507 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
508 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
510 if (sprite->image != EGL_NO_IMAGE_KHR) {
511 ec->image_target_texture_2d(GL_TEXTURE_2D, sprite->image);
512 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
513 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
515 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0,
516 GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
524 static const struct {
525 const char *filename;
526 int hotspot_x, hotspot_y;
527 } pointer_images[] = {
528 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
529 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
530 { DATADIR "/wayland/bottom_side.png", 16, 20 },
531 { DATADIR "/wayland/grabbing.png", 20, 17 },
532 { DATADIR "/wayland/left_ptr.png", 10, 5 },
533 { DATADIR "/wayland/left_side.png", 10, 20 },
534 { DATADIR "/wayland/right_side.png", 30, 19 },
535 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
536 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
537 { DATADIR "/wayland/top_side.png", 18, 8 },
538 { DATADIR "/wayland/xterm.png", 15, 15 }
542 create_pointer_images(struct wlsc_compositor *ec)
546 count = ARRAY_LENGTH(pointer_images);
547 ec->pointer_sprites = malloc(count * sizeof *ec->pointer_sprites);
548 for (i = 0; i < count; i++) {
549 ec->pointer_sprites[i] =
550 create_sprite_from_png(ec,
551 pointer_images[i].filename,
557 texture_region(struct wlsc_surface *es, pixman_region32_t *region)
559 struct wlsc_compositor *ec = es->compositor;
560 GLfloat *v, inv_width, inv_height;
561 pixman_box32_t *rectangles;
565 rectangles = pixman_region32_rectangles(region, &n);
566 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
567 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
568 inv_width = 1.0 / es->pitch;
569 inv_height = 1.0 / es->height;
571 for (i = 0; i < n; i++, v += 16, p += 6) {
572 v[ 0] = rectangles[i].x1;
573 v[ 1] = rectangles[i].y1;
574 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
575 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
577 v[ 4] = rectangles[i].x1;
578 v[ 5] = rectangles[i].y2;
580 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
582 v[ 8] = rectangles[i].x2;
583 v[ 9] = rectangles[i].y1;
584 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
587 v[12] = rectangles[i].x2;
588 v[13] = rectangles[i].y2;
604 transform_vertex(struct wlsc_surface *surface,
605 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
607 struct wlsc_vector t;
614 wlsc_matrix_transform(&surface->transform->matrix, &t);
623 texture_transformed_surface(struct wlsc_surface *es)
625 struct wlsc_compositor *ec = es->compositor;
629 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
630 p = wl_array_add(&ec->indices, 6 * sizeof *p);
632 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
633 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
634 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
635 transform_vertex(es, es->x + es->width, es->y + es->height,
649 wlsc_surface_draw(struct wlsc_surface *es,
650 struct wlsc_output *output, pixman_region32_t *clip)
652 struct wlsc_compositor *ec = es->compositor;
654 pixman_region32_t repaint;
658 pixman_region32_init_rect(&repaint,
659 es->x, es->y, es->width, es->height);
660 pixman_region32_intersect(&repaint, &repaint, clip);
661 if (!pixman_region32_not_empty(&repaint))
664 switch (es->visual) {
665 case WLSC_ARGB_VISUAL:
666 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
669 case WLSC_PREMUL_ARGB_VISUAL:
670 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
673 case WLSC_RGB_VISUAL:
677 fprintf(stderr, "bogus visual\n");
681 if (es->transform == NULL) {
683 n = texture_region(es, &repaint);
686 n = texture_transformed_surface(es);
689 glBindTexture(GL_TEXTURE_2D, es->texture);
690 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
691 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
693 v = ec->vertices.data;
694 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
695 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
696 glEnableVertexAttribArray(0);
697 glEnableVertexAttribArray(1);
698 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
700 ec->vertices.size = 0;
701 ec->indices.size = 0;
702 pixman_region32_fini(&repaint);
706 wlsc_surface_raise(struct wlsc_surface *surface)
708 struct wlsc_compositor *compositor = surface->compositor;
710 wl_list_remove(&surface->link);
711 wl_list_insert(&compositor->surface_list, &surface->link);
712 wlsc_compositor_repick(compositor);
713 wlsc_surface_damage(surface);
717 wlsc_compositor_damage_all(struct wlsc_compositor *compositor)
719 struct wlsc_output *output;
721 wl_list_for_each(output, &compositor->output_list, link)
722 wlsc_output_damage(output);
726 wlsc_buffer_post_release(struct wl_buffer *buffer)
728 if (buffer == NULL || --buffer->busy_count > 0)
731 assert(buffer->resource.client != NULL);
732 wl_resource_post_event(&buffer->resource, WL_BUFFER_RELEASE);
736 wlsc_output_damage(struct wlsc_output *output)
738 struct wlsc_compositor *compositor = output->compositor;
740 pixman_region32_union(&compositor->damage,
741 &compositor->damage, &output->region);
742 wlsc_compositor_schedule_repaint(compositor);
746 fade_frame(struct wlsc_animation *animation,
747 struct wlsc_output *output, uint32_t msecs)
749 struct wlsc_compositor *compositor =
750 container_of(animation,
751 struct wlsc_compositor, fade.animation);
753 wlsc_spring_update(&compositor->fade.spring, msecs);
754 if (wlsc_spring_done(&compositor->fade.spring)) {
755 if (compositor->fade.spring.current > 0.999) {
756 compositor->state = WLSC_COMPOSITOR_SLEEPING;
757 compositor->shell->lock(compositor->shell);
759 compositor->fade.spring.current =
760 compositor->fade.spring.target;
761 wl_list_remove(&animation->link);
762 wl_list_init(&animation->link);
765 wlsc_output_damage(output);
769 fade_output(struct wlsc_output *output,
770 GLfloat tint, pixman_region32_t *region)
772 struct wlsc_compositor *compositor = output->compositor;
773 struct wlsc_surface surface;
774 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
776 surface.compositor = compositor;
777 surface.x = output->x;
778 surface.y = output->y;
779 surface.pitch = output->current->width;
780 surface.width = output->current->width;
781 surface.height = output->current->height;
782 surface.texture = GL_NONE;
783 surface.transform = NULL;
786 surface.visual = WLSC_PREMUL_ARGB_VISUAL;
788 surface.visual = WLSC_RGB_VISUAL;
790 glUseProgram(compositor->solid_shader.program);
791 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
792 1, GL_FALSE, output->matrix.d);
793 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
794 wlsc_surface_draw(&surface, output, region);
798 wlsc_output_set_cursor(struct wlsc_output *output,
799 struct wl_input_device *dev, int force_sw)
801 struct wlsc_compositor *ec = output->compositor;
802 struct wlsc_input_device *device = (struct wlsc_input_device *) dev;
803 pixman_region32_t cursor_region;
804 int use_hardware_cursor = 1, prior_was_hardware;
806 pixman_region32_init_rect(&cursor_region,
807 device->sprite->x, device->sprite->y,
808 device->sprite->width,
809 device->sprite->height);
811 pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
813 if (!pixman_region32_not_empty(&cursor_region)) {
814 output->set_hardware_cursor(output, NULL);
818 prior_was_hardware = wl_list_empty(&device->sprite->link);
819 if (force_sw || output->set_hardware_cursor(output, device) < 0) {
820 if (prior_was_hardware) {
821 wlsc_surface_damage(device->sprite);
822 output->set_hardware_cursor(output, NULL);
824 use_hardware_cursor = 0;
825 } else if (!prior_was_hardware) {
826 wlsc_surface_damage_below(device->sprite);
829 /* Remove always to be on top. */
830 wl_list_remove(&device->sprite->link);
831 if (!use_hardware_cursor && ec->focus)
832 wl_list_insert(&ec->surface_list, &device->sprite->link);
834 wl_list_init(&device->sprite->link);
837 pixman_region32_fini(&cursor_region);
841 wlsc_output_repaint(struct wlsc_output *output)
843 struct wlsc_compositor *ec = output->compositor;
844 struct wlsc_surface *es;
845 pixman_region32_t opaque, new_damage, total_damage, repaint;
847 output->prepare_render(output);
849 glViewport(0, 0, output->current->width, output->current->height);
851 glUseProgram(ec->texture_shader.program);
852 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
853 1, GL_FALSE, output->matrix.d);
854 glUniform1i(ec->texture_shader.tex_uniform, 0);
856 wlsc_output_set_cursor(output, ec->input_device,
857 ec->fade.spring.current >= 0.001);
859 pixman_region32_init(&new_damage);
860 pixman_region32_copy(&new_damage, &ec->damage);
861 pixman_region32_init(&opaque);
863 wl_list_for_each(es, &ec->surface_list, link) {
864 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
865 pixman_region32_union(&new_damage, &new_damage, &es->damage);
866 pixman_region32_union(&opaque, &opaque, &es->opaque);
869 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
871 pixman_region32_init(&total_damage);
872 pixman_region32_union(&total_damage, &new_damage,
873 &output->previous_damage);
874 pixman_region32_intersect(&output->previous_damage,
875 &new_damage, &output->region);
877 pixman_region32_fini(&opaque);
878 pixman_region32_fini(&new_damage);
880 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
882 if (es->visual == WLSC_RGB_VISUAL &&
883 output->prepare_scanout_surface(output, es) == 0) {
884 /* We're drawing nothing now,
885 * draw the damaged regions later. */
886 pixman_region32_union(&ec->damage, &ec->damage, &total_damage);
888 output->scanout_buffer = es->buffer;
889 output->scanout_buffer->busy_count++;
891 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
892 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
893 &output->scanout_buffer_destroy_listener.link);
898 if (es->fullscreen_output == output) {
899 if (es->width < output->current->width ||
900 es->height < output->current->height)
901 glClear(GL_COLOR_BUFFER_BIT);
902 wlsc_surface_draw(es, output, &total_damage);
904 wl_list_for_each(es, &ec->surface_list, link) {
905 pixman_region32_copy(&es->damage, &total_damage);
906 pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
909 wl_list_for_each_reverse(es, &ec->surface_list, link) {
910 pixman_region32_init(&repaint);
911 pixman_region32_intersect(&repaint, &output->region,
913 wlsc_surface_draw(es, output, &repaint);
914 pixman_region32_subtract(&es->damage,
915 &es->damage, &output->region);
919 if (ec->fade.spring.current > 0.001)
920 fade_output(output, ec->fade.spring.current, &total_damage);
923 struct wlsc_frame_callback {
924 struct wl_resource resource;
929 repaint(void *data, int msecs)
931 struct wlsc_output *output = data;
932 struct wlsc_compositor *compositor = output->compositor;
933 struct wlsc_animation *animation, *next;
934 struct wlsc_frame_callback *cb, *cnext;
936 wlsc_output_repaint(output);
937 output->repaint_needed = 0;
938 output->repaint_scheduled = 1;
939 output->present(output);
941 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
942 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
943 wl_resource_destroy(&cb->resource, 0);
946 wl_list_for_each_safe(animation, next,
947 &compositor->animation_list, link)
948 animation->frame(animation, output, msecs);
952 idle_repaint(void *data)
954 repaint(data, wlsc_compositor_get_time());
958 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
960 wlsc_buffer_post_release(output->scanout_buffer);
961 output->scanout_buffer = NULL;
962 output->repaint_scheduled = 0;
964 if (output->repaint_needed)
965 repaint(output, msecs);
969 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
971 struct wlsc_output *output;
972 struct wl_event_loop *loop;
974 if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
977 loop = wl_display_get_event_loop(compositor->wl_display);
978 wl_list_for_each(output, &compositor->output_list, link) {
979 output->repaint_needed = 1;
980 if (output->repaint_scheduled)
983 wl_event_loop_add_idle(loop, idle_repaint, output);
984 output->repaint_scheduled = 1;
989 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
993 done = wlsc_spring_done(&compositor->fade.spring);
994 compositor->fade.spring.target = tint;
995 if (wlsc_spring_done(&compositor->fade.spring))
999 compositor->fade.spring.timestamp =
1000 wlsc_compositor_get_time();
1002 wlsc_compositor_damage_all(compositor);
1003 if (wl_list_empty(&compositor->fade.animation.link))
1004 wl_list_insert(compositor->animation_list.prev,
1005 &compositor->fade.animation.link);
1009 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1011 wl_resource_destroy(resource, wlsc_compositor_get_time());
1015 wlsc_surface_assign_output(struct wlsc_surface *es)
1017 struct wlsc_compositor *ec = es->compositor;
1018 struct wlsc_output *output;
1019 pixman_region32_t region;
1024 wl_list_for_each(output, &ec->output_list, link) {
1025 pixman_region32_init_rect(®ion,
1026 es->x, es->y, es->width, es->height);
1027 pixman_region32_intersect(®ion, ®ion, &output->region);
1029 e = pixman_region32_extents(®ion);
1030 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1033 es->output = output;
1040 surface_attach(struct wl_client *client,
1041 struct wl_resource *resource,
1042 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1044 struct wlsc_surface *es = resource->data;
1045 struct wl_buffer *buffer = buffer_resource->data;
1048 buffer->busy_count++;
1049 wlsc_buffer_post_release(es->buffer);
1051 es->buffer = buffer;
1052 wl_list_remove(&es->buffer_destroy_listener.link);
1053 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1054 &es->buffer_destroy_listener.link);
1056 if (es->visual == WLSC_NONE_VISUAL) {
1057 wl_list_insert(&es->compositor->surface_list, &es->link);
1061 if (x != 0 || y != 0 ||
1062 es->width != buffer->width || es->height != buffer->height) {
1063 wlsc_surface_configure(es, es->x + x, es->y + y,
1064 buffer->width, buffer->height);
1068 wlsc_buffer_attach(buffer, &es->surface);
1070 es->compositor->shell->attach(es->compositor->shell, es);
1073 wlsc_compositor_repick(es->compositor);
1077 surface_damage(struct wl_client *client,
1078 struct wl_resource *resource,
1079 int32_t x, int32_t y, int32_t width, int32_t height)
1081 struct wlsc_surface *es = resource->data;
1083 wlsc_surface_damage_rectangle(es, x, y, width, height);
1087 destroy_frame_callback(struct wl_resource *resource)
1089 struct wlsc_frame_callback *cb = resource->data;
1091 wl_list_remove(&cb->link);
1096 surface_frame(struct wl_client *client,
1097 struct wl_resource *resource, uint32_t callback)
1099 struct wlsc_frame_callback *cb;
1100 struct wlsc_surface *es = resource->data;
1102 cb = malloc(sizeof *cb);
1104 wl_resource_post_no_memory(resource);
1108 cb->resource.object.interface = &wl_callback_interface;
1109 cb->resource.object.id = callback;
1110 cb->resource.destroy = destroy_frame_callback;
1111 cb->resource.client = client;
1112 cb->resource.data = cb;
1114 wl_client_add_resource(client, &cb->resource);
1117 wl_list_insert(es->output->frame_callback_list.prev,
1120 wl_list_init(&cb->link);
1121 wl_resource_destroy(&cb->resource, 0);
1125 const static struct wl_surface_interface surface_interface = {
1133 wlsc_input_device_attach(struct wlsc_input_device *device,
1134 int x, int y, int width, int height)
1136 wlsc_surface_damage_below(device->sprite);
1138 device->hotspot_x = x;
1139 device->hotspot_y = y;
1141 device->sprite->x = device->input_device.x - device->hotspot_x;
1142 device->sprite->y = device->input_device.y - device->hotspot_y;
1143 device->sprite->width = width;
1144 device->sprite->height = height;
1146 wlsc_surface_damage(device->sprite);
1150 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1151 struct wl_buffer *buffer, int x, int y)
1153 wlsc_buffer_attach(buffer, &device->sprite->surface);
1154 wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1158 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1159 struct wlsc_sprite *sprite, int x, int y)
1161 wlsc_sprite_attach(sprite, &device->sprite->surface);
1162 wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1166 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1167 enum wlsc_pointer_type type)
1169 struct wlsc_compositor *compositor =
1170 (struct wlsc_compositor *) device->input_device.compositor;
1172 wlsc_input_device_attach_sprite(device,
1173 compositor->pointer_sprites[type],
1174 pointer_images[type].hotspot_x,
1175 pointer_images[type].hotspot_y);
1179 compositor_create_surface(struct wl_client *client,
1180 struct wl_resource *resource, uint32_t id)
1182 struct wlsc_compositor *ec = resource->data;
1183 struct wlsc_surface *surface;
1185 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1186 if (surface == NULL) {
1187 wl_resource_post_no_memory(resource);
1191 surface->surface.resource.destroy = destroy_surface;
1193 surface->surface.resource.object.id = id;
1194 surface->surface.resource.object.interface = &wl_surface_interface;
1195 surface->surface.resource.object.implementation =
1196 (void (**)(void)) &surface_interface;
1197 surface->surface.resource.data = surface;
1199 wl_client_add_resource(client, &surface->surface.resource);
1202 const static struct wl_compositor_interface compositor_interface = {
1203 compositor_create_surface,
1207 wlsc_surface_transform(struct wlsc_surface *surface,
1208 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1210 *sx = x - surface->x;
1211 *sy = y - surface->y;
1214 WL_EXPORT struct wlsc_surface *
1215 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1217 struct wlsc_compositor *ec =
1218 (struct wlsc_compositor *) device->compositor;
1219 struct wlsc_surface *es;
1221 wl_list_for_each(es, &ec->surface_list, link) {
1222 if (es->surface.resource.client == NULL)
1224 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1225 if (0 <= *sx && *sx < es->width &&
1226 0 <= *sy && *sy < es->height)
1235 implicit_grab_motion(struct wl_grab *grab,
1236 uint32_t time, int32_t x, int32_t y)
1238 struct wlsc_input_device *device =
1239 (struct wlsc_input_device *) grab->input_device;
1240 struct wlsc_surface *es =
1241 (struct wlsc_surface *) device->input_device.pointer_focus;
1243 struct wl_resource *resource;
1245 resource = grab->input_device->pointer_focus_resource;
1247 wlsc_surface_transform(es, x, y, &sx, &sy);
1248 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1249 time, x, y, sx, sy);
1254 implicit_grab_button(struct wl_grab *grab,
1255 uint32_t time, int32_t button, int32_t state)
1257 struct wl_resource *resource;
1259 resource = grab->input_device->pointer_focus_resource;
1261 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1262 time, button, state);
1266 implicit_grab_end(struct wl_grab *grab, uint32_t time)
1270 static const struct wl_grab_interface implicit_grab_interface = {
1271 implicit_grab_motion,
1272 implicit_grab_button,
1277 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1279 if (compositor->idle_inhibit)
1282 wlsc_compositor_fade(compositor, 0.0);
1283 compositor->state = WLSC_COMPOSITOR_ACTIVE;
1285 wl_event_source_timer_update(compositor->idle_source,
1286 option_idle_time * 1000);
1290 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1292 wlsc_compositor_wake(compositor);
1293 compositor->idle_inhibit++;
1297 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1299 compositor->idle_inhibit--;
1300 wlsc_compositor_wake(compositor);
1304 idle_handler(void *data)
1306 struct wlsc_compositor *compositor = data;
1308 if (compositor->idle_inhibit)
1311 wlsc_compositor_fade(compositor, 1.0);
1317 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1319 struct wlsc_surface *es;
1320 struct wlsc_compositor *ec =
1321 (struct wlsc_compositor *) device->compositor;
1322 struct wlsc_output *output;
1323 const struct wl_grab_interface *interface;
1324 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1326 int x_valid = 0, y_valid = 0;
1327 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1329 wlsc_compositor_wake(ec);
1331 wl_list_for_each(output, &ec->output_list, link) {
1332 if (output->x <= x && x <= output->x + output->current->width)
1335 if (output->y <= y && y <= output->y + output->current->height)
1338 /* FIXME: calculate this only on output addition/deletion */
1339 if (output->x < min_x)
1341 if (output->y < min_y)
1344 if (output->x + output->current->width > max_x)
1345 max_x = output->x + output->current->width;
1346 if (output->y + output->current->height > max_y)
1347 max_y = output->y + output->current->height;
1353 else if (x >= max_x)
1359 else if (y >= max_y)
1367 interface = device->grab->interface;
1368 interface->motion(device->grab, time, x, y);
1370 es = pick_surface(device, &sx, &sy);
1371 wl_input_device_set_pointer_focus(device,
1373 time, x, y, sx, sy);
1374 if (device->pointer_focus_resource)
1375 wl_resource_post_event(device->pointer_focus_resource,
1376 WL_INPUT_DEVICE_MOTION,
1377 time, x, y, sx, sy);
1380 wlsc_surface_damage_below(wd->sprite);
1382 wd->sprite->x = device->x - wd->hotspot_x;
1383 wd->sprite->y = device->y - wd->hotspot_y;
1385 wlsc_surface_damage(wd->sprite);
1389 wlsc_surface_activate(struct wlsc_surface *surface,
1390 struct wlsc_input_device *device, uint32_t time)
1392 struct wlsc_shell *shell = surface->compositor->shell;
1394 wlsc_surface_raise(surface);
1395 if (device->selection)
1396 shell->set_selection_focus(shell,
1398 &surface->surface, time);
1400 wl_input_device_set_keyboard_focus(&device->input_device,
1405 struct wlsc_binding {
1409 wlsc_binding_handler_t handler;
1411 struct wl_list link;
1415 notify_button(struct wl_input_device *device,
1416 uint32_t time, int32_t button, int32_t state)
1418 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1419 struct wlsc_compositor *compositor =
1420 (struct wlsc_compositor *) device->compositor;
1421 struct wlsc_binding *b;
1422 struct wlsc_surface *surface =
1423 (struct wlsc_surface *) device->pointer_focus;
1427 wlsc_compositor_idle_inhibit(compositor);
1429 wlsc_compositor_idle_release(compositor);
1431 if (state && surface && device->grab == NULL) {
1432 compositor->shell->activate(compositor->shell,
1434 wl_input_device_start_grab(device,
1435 &device->implicit_grab,
1439 wl_list_for_each(b, &compositor->binding_list, link) {
1440 if (b->button == button &&
1441 b->modifier == wd->modifier_state && state) {
1442 b->handler(&wd->input_device,
1443 time, 0, button, state, b->data);
1449 device->grab->interface->button(device->grab, time,
1452 if (!state && device->grab && device->grab_button == button) {
1453 wl_input_device_end_grab(device, time);
1454 surface = pick_surface(device, &sx, &sy);
1455 wl_input_device_set_pointer_focus(device, &surface->surface,
1456 time, device->x, device->y,
1462 terminate_binding(struct wl_input_device *device, uint32_t time,
1463 uint32_t key, uint32_t button, uint32_t state, void *data)
1465 struct wlsc_compositor *compositor = data;
1468 wl_display_terminate(compositor->wl_display);
1471 WL_EXPORT struct wlsc_binding *
1472 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1473 uint32_t key, uint32_t button, uint32_t modifier,
1474 wlsc_binding_handler_t handler, void *data)
1476 struct wlsc_binding *binding;
1478 binding = malloc(sizeof *binding);
1479 if (binding == NULL)
1483 binding->button = button;
1484 binding->modifier = modifier;
1485 binding->handler = handler;
1486 binding->data = data;
1487 wl_list_insert(compositor->binding_list.prev, &binding->link);
1493 wlsc_binding_destroy(struct wlsc_binding *binding)
1495 wl_list_remove(&binding->link);
1500 update_modifier_state(struct wlsc_input_device *device,
1501 uint32_t key, uint32_t state)
1508 modifier = MODIFIER_CTRL;
1513 modifier = MODIFIER_ALT;
1518 modifier = MODIFIER_SUPER;
1527 device->modifier_state |= modifier;
1529 device->modifier_state &= ~modifier;
1533 notify_key(struct wl_input_device *device,
1534 uint32_t time, uint32_t key, uint32_t state)
1536 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1537 struct wlsc_compositor *compositor =
1538 (struct wlsc_compositor *) device->compositor;
1540 struct wlsc_binding *b;
1543 wlsc_compositor_idle_inhibit(compositor);
1545 wlsc_compositor_idle_release(compositor);
1547 wl_list_for_each(b, &compositor->binding_list, link) {
1548 if (b->key == key &&
1549 b->modifier == wd->modifier_state) {
1550 b->handler(&wd->input_device,
1551 time, key, 0, state, b->data);
1556 update_modifier_state(wd, key, state);
1557 end = device->keys.data + device->keys.size;
1558 for (k = device->keys.data; k < end; k++) {
1562 device->keys.size = (void *) end - device->keys.data;
1564 k = wl_array_add(&device->keys, sizeof *k);
1568 if (device->keyboard_focus_resource)
1569 wl_resource_post_event(device->keyboard_focus_resource,
1570 WL_INPUT_DEVICE_KEY, time, key, state);
1574 notify_pointer_focus(struct wl_input_device *device,
1575 uint32_t time, struct wlsc_output *output,
1576 int32_t x, int32_t y)
1578 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1579 struct wlsc_compositor *compositor =
1580 (struct wlsc_compositor *) device->compositor;
1581 struct wlsc_surface *es;
1587 es = pick_surface(device, &sx, &sy);
1588 wl_input_device_set_pointer_focus(device,
1590 time, x, y, sx, sy);
1592 compositor->focus = 1;
1594 wd->sprite->x = device->x - wd->hotspot_x;
1595 wd->sprite->y = device->y - wd->hotspot_y;
1597 wl_input_device_set_pointer_focus(device, NULL,
1599 compositor->focus = 0;
1602 wlsc_surface_damage(wd->sprite);
1606 notify_keyboard_focus(struct wl_input_device *device,
1607 uint32_t time, struct wlsc_output *output,
1608 struct wl_array *keys)
1610 struct wlsc_input_device *wd =
1611 (struct wlsc_input_device *) device;
1612 struct wlsc_compositor *compositor =
1613 (struct wlsc_compositor *) device->compositor;
1614 struct wlsc_surface *es;
1617 if (!wl_list_empty(&compositor->surface_list))
1618 es = container_of(compositor->surface_list.next,
1619 struct wlsc_surface, link);
1624 wl_array_copy(&wd->input_device.keys, keys);
1625 wd->modifier_state = 0;
1626 end = device->keys.data + device->keys.size;
1627 for (k = device->keys.data; k < end; k++) {
1628 wlsc_compositor_idle_inhibit(compositor);
1629 update_modifier_state(wd, *k, 1);
1632 if (es && es->surface.resource.client)
1633 wl_input_device_set_keyboard_focus(&wd->input_device,
1634 &es->surface, time);
1636 end = device->keys.data + device->keys.size;
1637 for (k = device->keys.data; k < end; k++)
1638 wlsc_compositor_idle_release(compositor);
1640 wd->modifier_state = 0;
1641 wl_input_device_set_keyboard_focus(&wd->input_device,
1648 input_device_attach(struct wl_client *client,
1649 struct wl_resource *resource,
1651 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1653 struct wlsc_input_device *device = resource->data;
1654 struct wl_buffer *buffer;
1656 if (time < device->input_device.pointer_focus_time)
1658 if (device->input_device.pointer_focus == NULL)
1660 if (device->input_device.pointer_focus->resource.client != client)
1663 if (buffer_resource) {
1664 buffer = buffer_resource->data;
1665 wlsc_input_device_attach_buffer(device, buffer, x, y);
1667 wlsc_input_device_set_pointer_image(device,
1668 WLSC_POINTER_LEFT_PTR);
1673 const static struct wl_input_device_interface input_device_interface = {
1674 input_device_attach,
1677 static void unbind_input_device(struct wl_resource *resource)
1679 wl_list_remove(&resource->link);
1684 bind_input_device(struct wl_client *client,
1685 void *data, uint32_t version, uint32_t id)
1687 struct wl_input_device *device = data;
1688 struct wl_resource *resource;
1690 resource = wl_client_add_object(client, &wl_input_device_interface,
1691 &input_device_interface, id, data);
1692 wl_list_insert(&device->resource_list, &resource->link);
1693 resource->destroy = unbind_input_device;
1697 wlsc_input_device_init(struct wlsc_input_device *device,
1698 struct wlsc_compositor *ec)
1700 wl_input_device_init(&device->input_device, &ec->compositor);
1702 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1703 device, bind_input_device);
1705 device->sprite = wlsc_surface_create(ec,
1706 device->input_device.x,
1707 device->input_device.y, 32, 32);
1708 wl_list_insert(&ec->surface_list, &device->sprite->link);
1710 device->hotspot_x = 16;
1711 device->hotspot_y = 16;
1712 device->modifier_state = 0;
1714 device->input_device.implicit_grab.interface = &implicit_grab_interface;
1716 wl_list_insert(ec->input_device_list.prev, &device->link);
1718 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1722 bind_output(struct wl_client *client,
1723 void *data, uint32_t version, uint32_t id)
1725 struct wlsc_output *output = data;
1726 struct wlsc_mode *mode;
1727 struct wl_resource *resource;
1729 resource = wl_client_add_object(client,
1730 &wl_output_interface, NULL, id, data);
1732 wl_resource_post_event(resource,
1739 output->make, output->model);
1741 wl_list_for_each (mode, &output->mode_list, link) {
1742 wl_resource_post_event(resource,
1751 static const char vertex_shader[] =
1752 "uniform mat4 proj;\n"
1753 "attribute vec2 position;\n"
1754 "attribute vec2 texcoord;\n"
1755 "varying vec2 v_texcoord;\n"
1758 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1759 " v_texcoord = texcoord;\n"
1762 static const char texture_fragment_shader[] =
1763 "precision mediump float;\n"
1764 "varying vec2 v_texcoord;\n"
1765 "uniform sampler2D tex;\n"
1768 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1771 static const char solid_fragment_shader[] =
1772 "precision mediump float;\n"
1773 "uniform vec4 color;\n"
1776 " gl_FragColor = color\n;"
1780 compile_shader(GLenum type, const char *source)
1786 s = glCreateShader(type);
1787 glShaderSource(s, 1, &source, NULL);
1789 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1791 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1792 fprintf(stderr, "shader info: %s\n", msg);
1800 wlsc_shader_init(struct wlsc_shader *shader,
1801 const char *vertex_source, const char *fragment_source)
1806 shader->vertex_shader =
1807 compile_shader(GL_VERTEX_SHADER, vertex_source);
1808 shader->fragment_shader =
1809 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1811 shader->program = glCreateProgram();
1812 glAttachShader(shader->program, shader->vertex_shader);
1813 glAttachShader(shader->program, shader->fragment_shader);
1814 glBindAttribLocation(shader->program, 0, "position");
1815 glBindAttribLocation(shader->program, 1, "texcoord");
1817 glLinkProgram(shader->program);
1818 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1820 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1821 fprintf(stderr, "link info: %s\n", msg);
1825 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1826 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1832 init_solid_shader(struct wlsc_shader *shader,
1833 GLuint vertex_shader, const char *fragment_source)
1838 shader->vertex_shader = vertex_shader;
1839 shader->fragment_shader =
1840 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1842 shader->program = glCreateProgram();
1843 glAttachShader(shader->program, shader->vertex_shader);
1844 glAttachShader(shader->program, shader->fragment_shader);
1845 glBindAttribLocation(shader->program, 0, "position");
1846 glBindAttribLocation(shader->program, 1, "texcoord");
1848 glLinkProgram(shader->program);
1849 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1851 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1852 fprintf(stderr, "link info: %s\n", msg);
1856 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1857 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1863 wlsc_output_destroy(struct wlsc_output *output)
1865 pixman_region32_fini(&output->region);
1866 pixman_region32_fini(&output->previous_damage);
1870 wlsc_output_move(struct wlsc_output *output, int x, int y)
1872 struct wlsc_compositor *c = output->compositor;
1878 pixman_region32_init(&output->previous_damage);
1879 pixman_region32_init_rect(&output->region, x, y,
1880 output->current->width,
1881 output->current->height);
1883 wlsc_matrix_init(&output->matrix);
1884 wlsc_matrix_translate(&output->matrix,
1885 -output->x - output->current->width / 2.0,
1886 -output->y - output->current->height / 2.0, 0);
1888 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1889 wlsc_matrix_scale(&output->matrix,
1890 2.0 / output->current->width,
1891 flip * 2.0 / output->current->height, 1);
1893 pixman_region32_union(&c->damage, &c->damage, &output->region);
1897 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1898 int x, int y, int width, int height, uint32_t flags)
1900 output->compositor = c;
1903 output->mm_width = width;
1904 output->mm_height = height;
1906 output->flags = flags;
1907 wlsc_output_move(output, x, y);
1909 output->scanout_buffer_destroy_listener.func =
1910 output_handle_scanout_buffer_destroy;
1911 wl_list_init(&output->scanout_buffer_destroy_listener.link);
1912 wl_list_init(&output->frame_callback_list);
1914 output->resource.object.interface = &wl_output_interface;
1915 wl_display_add_global(c->wl_display,
1916 &wl_output_interface, output, bind_output);
1920 shm_buffer_created(struct wl_buffer *buffer)
1922 struct wl_list *surfaces_attached_to;
1924 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1925 if (!surfaces_attached_to) {
1926 buffer->user_data = NULL;
1930 wl_list_init(surfaces_attached_to);
1932 buffer->user_data = surfaces_attached_to;
1936 shm_buffer_damaged(struct wl_buffer *buffer,
1937 int32_t x, int32_t y, int32_t width, int32_t height)
1939 struct wl_list *surfaces_attached_to = buffer->user_data;
1940 struct wlsc_surface *es;
1941 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1943 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1944 glBindTexture(GL_TEXTURE_2D, es->texture);
1945 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1946 tex_width, buffer->height, 0,
1947 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1948 wl_shm_buffer_get_data(buffer));
1949 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1950 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1955 shm_buffer_destroyed(struct wl_buffer *buffer)
1957 struct wl_list *surfaces_attached_to = buffer->user_data;
1958 struct wlsc_surface *es, *next;
1960 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1961 wl_list_remove(&es->buffer_link);
1962 wl_list_init(&es->buffer_link);
1965 free(surfaces_attached_to);
1968 const static struct wl_shm_callbacks shm_callbacks = {
1971 shm_buffer_destroyed
1975 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1977 struct wl_event_loop *loop;
1978 const char *extensions;
1980 ec->wl_display = display;
1982 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1984 ec->shm = wl_shm_init(display, &shm_callbacks);
1986 ec->image_target_texture_2d =
1987 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1988 ec->image_target_renderbuffer_storage = (void *)
1989 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1990 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1991 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1993 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1994 ec->unbind_display =
1995 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1997 extensions = (const char *) glGetString(GL_EXTENSIONS);
1998 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2000 "GL_EXT_texture_format_BGRA8888 not available\n");
2005 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
2006 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2007 ec->has_bind_display = 1;
2008 if (ec->has_bind_display)
2009 ec->bind_display(ec->display, ec->wl_display);
2011 wl_list_init(&ec->surface_list);
2012 wl_list_init(&ec->input_device_list);
2013 wl_list_init(&ec->output_list);
2014 wl_list_init(&ec->binding_list);
2015 wl_list_init(&ec->animation_list);
2016 wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
2017 ec->fade.animation.frame = fade_frame;
2018 wl_list_init(&ec->fade.animation.link);
2020 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
2021 MODIFIER_CTRL | MODIFIER_ALT,
2022 terminate_binding, ec);
2024 create_pointer_images(ec);
2026 screenshooter_create(ec);
2028 glActiveTexture(GL_TEXTURE0);
2030 if (wlsc_shader_init(&ec->texture_shader,
2031 vertex_shader, texture_fragment_shader) < 0)
2033 if (init_solid_shader(&ec->solid_shader,
2034 ec->texture_shader.vertex_shader,
2035 solid_fragment_shader) < 0)
2038 loop = wl_display_get_event_loop(ec->wl_display);
2039 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2040 wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
2042 pixman_region32_init(&ec->damage);
2043 wlsc_compositor_schedule_repaint(ec);
2049 wlsc_compositor_shutdown(struct wlsc_compositor *ec)
2051 struct wlsc_output *output;
2053 /* Destroy all outputs associated with this compositor */
2054 wl_list_for_each(output, &ec->output_list, link)
2055 output->destroy(output);
2058 static int on_term_signal(int signal_number, void *data)
2060 struct wl_display *display = data;
2062 fprintf(stderr, "caught signal %d\n", signal_number);
2063 wl_display_terminate(display);
2069 load_module(const char *name, const char *entrypoint, void **handle)
2071 char path[PATH_MAX];
2072 void *module, *init;
2075 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2077 snprintf(path, sizeof path, "%s", name);
2079 module = dlopen(path, RTLD_LAZY);
2082 "failed to load module: %s\n", dlerror());
2086 init = dlsym(module, entrypoint);
2089 "failed to lookup init function: %s\n", dlerror());
2096 int main(int argc, char *argv[])
2098 struct wl_display *display;
2099 struct wlsc_compositor *ec;
2100 struct wl_event_loop *loop;
2102 void *shell_module, *backend_module;
2103 int (*shell_init)(struct wlsc_compositor *ec);
2104 struct wlsc_compositor
2105 *(*backend_init)(struct wl_display *display, char *options);
2106 char *backend = NULL;
2107 char *backend_options = "";
2111 static const char opts[] = "B:b:o:S:i:s:x";
2112 static const struct option longopts[ ] = {
2113 { "backend", 1, NULL, 'B' },
2114 { "backend-options", 1, NULL, 'o' },
2115 { "socket", 1, NULL, 'S' },
2116 { "idle-time", 1, NULL, 'i' },
2117 { "shell", 1, NULL, 's' },
2118 { "xserver", 0, NULL, 'x' },
2122 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2128 backend_options = optarg;
2131 option_socket_name = optarg;
2134 option_idle_time = strtol(optarg, &p, 0);
2137 "invalid idle time option: %s\n",
2151 display = wl_display_create();
2153 loop = wl_display_get_event_loop(display);
2154 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2155 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2156 wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
2158 wl_list_init(&child_process_list);
2159 wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
2162 if (getenv("WAYLAND_DISPLAY"))
2163 backend = "wayland-backend.so";
2164 else if (getenv("DISPLAY"))
2165 backend = "x11-backend.so";
2166 else if (getenv("OPENWFD"))
2167 backend = "openwfd-backend.so";
2169 backend = "drm-backend.so";
2173 shell = "desktop-shell.so";
2175 backend_init = load_module(backend, "backend_init", &backend_module);
2179 shell_init = load_module(shell, "shell_init", &shell_module);
2183 ec = backend_init(display, backend_options);
2185 fprintf(stderr, "failed to create compositor\n");
2189 if (shell_init(ec) < 0)
2193 wlsc_xserver_init(ec);
2195 if (wl_display_add_socket(display, option_socket_name)) {
2196 fprintf(stderr, "failed to add socket: %m\n");
2200 wl_display_run(display);
2203 wlsc_xserver_destroy(ec);
2205 if (ec->has_bind_display)
2206 ec->unbind_display(ec->display, display);
2207 wl_display_destroy(display);