2 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
4 * Copyright © 2012 Collabora, Ltd.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/utsname.h>
45 #include <linux/input.h>
53 #define UNW_LOCAL_ONLY
54 #include <libunwind.h>
57 #include <wayland-server.h>
58 #include "compositor.h"
59 #include "../shared/os-compatibility.h"
60 #include "git-version.h"
63 static struct wl_list child_process_list;
64 static struct weston_compositor *segv_compositor;
67 sigchld_handler(int signal_number, void *data)
69 struct weston_process *p;
73 pid = waitpid(-1, &status, WNOHANG);
77 wl_list_for_each(p, &child_process_list, link) {
82 if (&p->link == &child_process_list) {
83 weston_log("unknown child process exited\n");
87 wl_list_remove(&p->link);
88 p->cleanup(p, status);
94 weston_output_transform_init(struct weston_output *output, uint32_t transform);
97 weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
99 struct weston_seat *seat;
100 pixman_region32_t old_output_region;
103 if (!output->switch_mode)
106 ret = output->switch_mode(output, mode);
110 pixman_region32_init(&old_output_region);
111 pixman_region32_copy(&old_output_region, &output->region);
113 /* Update output region and transformation matrix */
114 weston_output_transform_init(output, output->transform);
116 pixman_region32_init(&output->previous_damage);
117 pixman_region32_init_rect(&output->region, output->x, output->y,
118 output->width, output->height);
120 weston_output_update_matrix(output);
122 /* If a pointer falls outside the outputs new geometry, move it to its
123 * lower-right corner */
124 wl_list_for_each(seat, &output->compositor->seat_list, link) {
125 struct weston_pointer *pointer = seat->pointer;
131 x = wl_fixed_to_int(pointer->x);
132 y = wl_fixed_to_int(pointer->y);
134 if (!pixman_region32_contains_point(&old_output_region,
136 pixman_region32_contains_point(&output->region,
140 if (x >= output->x + output->width)
141 x = output->x + output->width - 1;
142 if (y >= output->y + output->height)
143 y = output->y + output->height - 1;
145 pointer->x = wl_fixed_from_int(x);
146 pointer->y = wl_fixed_from_int(y);
149 pixman_region32_fini(&old_output_region);
155 weston_watch_process(struct weston_process *process)
157 wl_list_insert(&child_process_list, &process->link);
161 child_client_exec(int sockfd, const char *path)
167 /* do not give our signal mask to the new process */
168 sigfillset(&allsigs);
169 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
171 /* Launch clients as the user. */
174 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
175 * non-CLOEXEC fd to pass through exec. */
176 clientfd = dup(sockfd);
177 if (clientfd == -1) {
178 weston_log("compositor: dup failed: %m\n");
182 snprintf(s, sizeof s, "%d", clientfd);
183 setenv("WAYLAND_SOCKET", s, 1);
185 if (execl(path, path, NULL) < 0)
186 weston_log("compositor: executing '%s' failed: %m\n",
190 WL_EXPORT struct wl_client *
191 weston_client_launch(struct weston_compositor *compositor,
192 struct weston_process *proc,
194 weston_process_cleanup_func_t cleanup)
198 struct wl_client *client;
200 weston_log("launching '%s'\n", path);
202 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
203 weston_log("weston_client_launch: "
204 "socketpair failed while launching '%s': %m\n",
213 weston_log("weston_client_launch: "
214 "fork failed while launching '%s': %m\n", path);
219 child_client_exec(sv[1], path);
225 client = wl_client_create(compositor->wl_display, sv[0]);
228 weston_log("weston_client_launch: "
229 "wl_client_create failed while launching '%s'.\n",
235 proc->cleanup = cleanup;
236 weston_watch_process(proc);
242 surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
244 struct weston_surface *surface =
245 container_of(listener, struct weston_surface,
246 pending.buffer_destroy_listener);
248 surface->pending.buffer = NULL;
252 empty_region(pixman_region32_t *region)
254 pixman_region32_fini(region);
255 pixman_region32_init(region);
259 region_init_infinite(pixman_region32_t *region)
261 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
262 UINT32_MAX, UINT32_MAX);
265 WL_EXPORT struct weston_surface *
266 weston_surface_create(struct weston_compositor *compositor)
268 struct weston_surface *surface;
270 surface = calloc(1, sizeof *surface);
274 wl_signal_init(&surface->resource.destroy_signal);
276 wl_list_init(&surface->link);
277 wl_list_init(&surface->layer_link);
279 surface->resource.client = NULL;
281 surface->compositor = compositor;
282 surface->alpha = 1.0;
284 if (compositor->renderer->create_surface(surface) < 0) {
289 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
290 surface->pending.buffer_transform = surface->buffer_transform;
291 surface->output = NULL;
292 surface->plane = &compositor->primary_plane;
293 surface->pending.newly_attached = 0;
295 pixman_region32_init(&surface->damage);
296 pixman_region32_init(&surface->opaque);
297 pixman_region32_init(&surface->clip);
298 region_init_infinite(&surface->input);
299 pixman_region32_init(&surface->transform.opaque);
300 wl_list_init(&surface->frame_callback_list);
302 wl_list_init(&surface->geometry.transformation_list);
303 wl_list_insert(&surface->geometry.transformation_list,
304 &surface->transform.position.link);
305 weston_matrix_init(&surface->transform.position.matrix);
306 wl_list_init(&surface->geometry.child_list);
307 pixman_region32_init(&surface->transform.boundingbox);
308 surface->transform.dirty = 1;
310 surface->pending.buffer_destroy_listener.notify =
311 surface_handle_pending_buffer_destroy;
312 pixman_region32_init(&surface->pending.damage);
313 pixman_region32_init(&surface->pending.opaque);
314 region_init_infinite(&surface->pending.input);
315 wl_list_init(&surface->pending.frame_callback_list);
321 weston_surface_set_color(struct weston_surface *surface,
322 float red, float green, float blue, float alpha)
324 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
328 weston_surface_to_global_float(struct weston_surface *surface,
329 float sx, float sy, float *x, float *y)
331 if (surface->transform.enabled) {
332 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
334 weston_matrix_transform(&surface->transform.matrix, &v);
336 if (fabsf(v.f[3]) < 1e-6) {
337 weston_log("warning: numerical instability in "
338 "%s(), divisor = %g\n", __func__,
345 *x = v.f[0] / v.f[3];
346 *y = v.f[1] / v.f[3];
348 *x = sx + surface->geometry.x;
349 *y = sy + surface->geometry.y;
354 weston_transformed_coord(int width, int height,
355 enum wl_output_transform transform,
356 float sx, float sy, float *bx, float *by)
359 case WL_OUTPUT_TRANSFORM_NORMAL:
364 case WL_OUTPUT_TRANSFORM_FLIPPED:
368 case WL_OUTPUT_TRANSFORM_90:
372 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
376 case WL_OUTPUT_TRANSFORM_180:
380 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
384 case WL_OUTPUT_TRANSFORM_270:
388 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
395 WL_EXPORT pixman_box32_t
396 weston_transformed_rect(int width, int height,
397 enum wl_output_transform transform,
400 float x1, x2, y1, y2;
404 weston_transformed_coord(width, height, transform,
405 rect.x1, rect.y1, &x1, &y1);
406 weston_transformed_coord(width, height, transform,
407 rect.x2, rect.y2, &x2, &y2);
429 weston_surface_to_buffer_float(struct weston_surface *surface,
430 float sx, float sy, float *bx, float *by)
432 weston_transformed_coord(surface->geometry.width,
433 surface->geometry.height,
434 surface->buffer_transform,
438 WL_EXPORT pixman_box32_t
439 weston_surface_to_buffer_rect(struct weston_surface *surface,
442 return weston_transformed_rect(surface->geometry.width,
443 surface->geometry.height,
444 surface->buffer_transform, rect);
448 weston_surface_move_to_plane(struct weston_surface *surface,
449 struct weston_plane *plane)
451 if (surface->plane == plane)
454 weston_surface_damage_below(surface);
455 surface->plane = plane;
456 weston_surface_damage(surface);
460 weston_surface_damage_below(struct weston_surface *surface)
462 pixman_region32_t damage;
464 pixman_region32_init(&damage);
465 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
467 pixman_region32_union(&surface->plane->damage,
468 &surface->plane->damage, &damage);
469 pixman_region32_fini(&damage);
472 static struct wl_resource *
473 find_resource_for_client(struct wl_list *list, struct wl_client *client)
475 struct wl_resource *r;
477 wl_list_for_each(r, list, link) {
478 if (r->client == client)
486 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
488 uint32_t different = es->output_mask ^ mask;
489 uint32_t entered = mask & different;
490 uint32_t left = es->output_mask & different;
491 struct weston_output *output;
492 struct wl_resource *resource = NULL;
493 struct wl_client *client = es->resource.client;
495 es->output_mask = mask;
496 if (es->resource.client == NULL)
501 wl_list_for_each(output, &es->compositor->output_list, link) {
502 if (1 << output->id & different)
504 find_resource_for_client(&output->resource_list,
506 if (resource == NULL)
508 if (1 << output->id & entered)
509 wl_surface_send_enter(&es->resource, resource);
510 if (1 << output->id & left)
511 wl_surface_send_leave(&es->resource, resource);
516 weston_surface_assign_output(struct weston_surface *es)
518 struct weston_compositor *ec = es->compositor;
519 struct weston_output *output, *new_output;
520 pixman_region32_t region;
521 uint32_t max, area, mask;
527 pixman_region32_init(®ion);
528 wl_list_for_each(output, &ec->output_list, link) {
529 pixman_region32_intersect(®ion, &es->transform.boundingbox,
532 e = pixman_region32_extents(®ion);
533 area = (e->x2 - e->x1) * (e->y2 - e->y1);
536 mask |= 1 << output->id;
543 pixman_region32_fini(®ion);
545 es->output = new_output;
546 weston_surface_update_output_mask(es, mask);
550 surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
551 int32_t width, int32_t height,
552 pixman_region32_t *bbox)
554 float min_x = HUGE_VALF, min_y = HUGE_VALF;
555 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
560 { sx + width, sy + height }
565 if (width == 0 || height == 0) {
566 /* avoid rounding empty bbox to 1x1 */
567 pixman_region32_init(bbox);
571 for (i = 0; i < 4; ++i) {
573 weston_surface_to_global_float(surface,
574 s[i][0], s[i][1], &x, &y);
585 int_x = floorf(min_x);
586 int_y = floorf(min_y);
587 pixman_region32_init_rect(bbox, int_x, int_y,
588 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
592 weston_surface_update_transform_disable(struct weston_surface *surface)
594 surface->transform.enabled = 0;
596 /* round off fractions when not transformed */
597 surface->geometry.x = roundf(surface->geometry.x);
598 surface->geometry.y = roundf(surface->geometry.y);
600 /* Otherwise identity matrix, but with x and y translation. */
601 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
602 surface->transform.position.matrix.d[12] = surface->geometry.x;
603 surface->transform.position.matrix.d[13] = surface->geometry.y;
605 surface->transform.matrix = surface->transform.position.matrix;
607 surface->transform.inverse = surface->transform.position.matrix;
608 surface->transform.inverse.d[12] = -surface->geometry.x;
609 surface->transform.inverse.d[13] = -surface->geometry.y;
611 pixman_region32_init_rect(&surface->transform.boundingbox,
614 surface->geometry.width,
615 surface->geometry.height);
617 if (surface->alpha == 1.0) {
618 pixman_region32_copy(&surface->transform.opaque,
620 pixman_region32_translate(&surface->transform.opaque,
622 surface->geometry.y);
627 weston_surface_update_transform_enable(struct weston_surface *surface)
629 struct weston_surface *parent = surface->geometry.parent;
630 struct weston_matrix *matrix = &surface->transform.matrix;
631 struct weston_matrix *inverse = &surface->transform.inverse;
632 struct weston_transform *tform;
634 surface->transform.enabled = 1;
636 /* Otherwise identity matrix, but with x and y translation. */
637 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
638 surface->transform.position.matrix.d[12] = surface->geometry.x;
639 surface->transform.position.matrix.d[13] = surface->geometry.y;
641 weston_matrix_init(matrix);
642 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
643 weston_matrix_multiply(matrix, &tform->matrix);
646 weston_matrix_multiply(matrix, &parent->transform.matrix);
648 if (weston_matrix_invert(inverse, matrix) < 0) {
649 /* Oops, bad total transformation, not invertible */
650 weston_log("error: weston_surface %p"
651 " transformation not invertible.\n", surface);
655 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
656 surface->geometry.height,
657 &surface->transform.boundingbox);
663 weston_surface_update_transform(struct weston_surface *surface)
665 struct weston_surface *parent = surface->geometry.parent;
667 if (!surface->transform.dirty)
671 weston_surface_update_transform(parent);
673 surface->transform.dirty = 0;
675 weston_surface_damage_below(surface);
677 pixman_region32_fini(&surface->transform.boundingbox);
678 pixman_region32_fini(&surface->transform.opaque);
679 pixman_region32_init(&surface->transform.opaque);
681 /* transform.position is always in transformation_list */
682 if (surface->geometry.transformation_list.next ==
683 &surface->transform.position.link &&
684 surface->geometry.transformation_list.prev ==
685 &surface->transform.position.link &&
687 weston_surface_update_transform_disable(surface);
689 if (weston_surface_update_transform_enable(surface) < 0)
690 weston_surface_update_transform_disable(surface);
693 weston_surface_damage_below(surface);
695 weston_surface_assign_output(surface);
699 weston_surface_geometry_dirty(struct weston_surface *surface)
701 struct weston_surface *child;
704 * The invariant: if surface->geometry.dirty, then all surfaces
705 * in surface->geometry.child_list have geometry.dirty too.
706 * Corollary: if not parent->geometry.dirty, then all ancestors
710 if (surface->transform.dirty)
713 surface->transform.dirty = 1;
715 wl_list_for_each(child, &surface->geometry.child_list,
716 geometry.parent_link)
717 weston_surface_geometry_dirty(child);
721 weston_surface_to_global_fixed(struct weston_surface *surface,
722 wl_fixed_t sx, wl_fixed_t sy,
723 wl_fixed_t *x, wl_fixed_t *y)
727 weston_surface_to_global_float(surface,
728 wl_fixed_to_double(sx),
729 wl_fixed_to_double(sy),
731 *x = wl_fixed_from_double(xf);
732 *y = wl_fixed_from_double(yf);
736 weston_surface_from_global_float(struct weston_surface *surface,
737 float x, float y, float *sx, float *sy)
739 if (surface->transform.enabled) {
740 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
742 weston_matrix_transform(&surface->transform.inverse, &v);
744 if (fabsf(v.f[3]) < 1e-6) {
745 weston_log("warning: numerical instability in "
746 "weston_surface_from_global(), divisor = %g\n",
753 *sx = v.f[0] / v.f[3];
754 *sy = v.f[1] / v.f[3];
756 *sx = x - surface->geometry.x;
757 *sy = y - surface->geometry.y;
762 weston_surface_from_global_fixed(struct weston_surface *surface,
763 wl_fixed_t x, wl_fixed_t y,
764 wl_fixed_t *sx, wl_fixed_t *sy)
768 weston_surface_from_global_float(surface,
769 wl_fixed_to_double(x),
770 wl_fixed_to_double(y),
772 *sx = wl_fixed_from_double(sxf);
773 *sy = wl_fixed_from_double(syf);
777 weston_surface_from_global(struct weston_surface *surface,
778 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
782 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
788 weston_surface_schedule_repaint(struct weston_surface *surface)
790 struct weston_output *output;
792 wl_list_for_each(output, &surface->compositor->output_list, link)
793 if (surface->output_mask & (1 << output->id))
794 weston_output_schedule_repaint(output);
798 weston_surface_damage(struct weston_surface *surface)
800 pixman_region32_union_rect(&surface->damage, &surface->damage,
801 0, 0, surface->geometry.width,
802 surface->geometry.height);
804 weston_surface_schedule_repaint(surface);
808 weston_surface_configure(struct weston_surface *surface,
809 float x, float y, int width, int height)
811 surface->geometry.x = x;
812 surface->geometry.y = y;
813 surface->geometry.width = width;
814 surface->geometry.height = height;
815 weston_surface_geometry_dirty(surface);
819 weston_surface_set_position(struct weston_surface *surface,
822 surface->geometry.x = x;
823 surface->geometry.y = y;
824 weston_surface_geometry_dirty(surface);
828 transform_parent_handle_parent_destroy(struct wl_listener *listener,
831 struct weston_surface *surface =
832 container_of(listener, struct weston_surface,
833 geometry.parent_destroy_listener);
835 weston_surface_set_transform_parent(surface, NULL);
839 weston_surface_set_transform_parent(struct weston_surface *surface,
840 struct weston_surface *parent)
842 if (surface->geometry.parent) {
843 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
844 wl_list_remove(&surface->geometry.parent_link);
847 surface->geometry.parent = parent;
849 surface->geometry.parent_destroy_listener.notify =
850 transform_parent_handle_parent_destroy;
852 wl_signal_add(&parent->resource.destroy_signal,
853 &surface->geometry.parent_destroy_listener);
854 wl_list_insert(&parent->geometry.child_list,
855 &surface->geometry.parent_link);
858 weston_surface_geometry_dirty(surface);
862 weston_surface_is_mapped(struct weston_surface *surface)
871 weston_surface_buffer_width(struct weston_surface *surface)
873 switch (surface->buffer_transform) {
874 case WL_OUTPUT_TRANSFORM_90:
875 case WL_OUTPUT_TRANSFORM_270:
876 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
877 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
878 return surface->buffer_ref.buffer->height;
880 return surface->buffer_ref.buffer->width;
885 weston_surface_buffer_height(struct weston_surface *surface)
887 switch (surface->buffer_transform) {
888 case WL_OUTPUT_TRANSFORM_90:
889 case WL_OUTPUT_TRANSFORM_270:
890 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
891 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
892 return surface->buffer_ref.buffer->width;
894 return surface->buffer_ref.buffer->height;
899 weston_compositor_get_time(void)
903 gettimeofday(&tv, NULL);
905 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
908 WL_EXPORT struct weston_surface *
909 weston_compositor_pick_surface(struct weston_compositor *compositor,
910 wl_fixed_t x, wl_fixed_t y,
911 wl_fixed_t *sx, wl_fixed_t *sy)
913 struct weston_surface *surface;
915 wl_list_for_each(surface, &compositor->surface_list, link) {
916 weston_surface_from_global_fixed(surface, x, y, sx, sy);
917 if (pixman_region32_contains_point(&surface->input,
918 wl_fixed_to_int(*sx),
919 wl_fixed_to_int(*sy),
928 weston_compositor_repick(struct weston_compositor *compositor)
930 struct weston_seat *seat;
932 if (!compositor->focus)
935 wl_list_for_each(seat, &compositor->seat_list, link)
936 weston_seat_repick(seat);
940 weston_surface_unmap(struct weston_surface *surface)
942 struct weston_seat *seat;
944 weston_surface_damage_below(surface);
945 surface->output = NULL;
946 wl_list_remove(&surface->layer_link);
948 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
949 if (seat->keyboard && seat->keyboard->focus == surface)
950 weston_keyboard_set_focus(seat->keyboard, NULL);
951 if (seat->pointer && seat->pointer->focus == surface)
952 weston_pointer_set_focus(seat->pointer,
954 wl_fixed_from_int(0),
955 wl_fixed_from_int(0));
958 weston_surface_schedule_repaint(surface);
961 struct weston_frame_callback {
962 struct wl_resource resource;
967 destroy_surface(struct wl_resource *resource)
969 struct weston_surface *surface =
970 container_of(resource, struct weston_surface, resource);
971 struct weston_compositor *compositor = surface->compositor;
972 struct weston_frame_callback *cb, *next;
974 assert(wl_list_empty(&surface->geometry.child_list));
976 if (weston_surface_is_mapped(surface))
977 weston_surface_unmap(surface);
979 wl_list_for_each_safe(cb, next,
980 &surface->pending.frame_callback_list, link)
981 wl_resource_destroy(&cb->resource);
983 pixman_region32_fini(&surface->pending.input);
984 pixman_region32_fini(&surface->pending.opaque);
985 pixman_region32_fini(&surface->pending.damage);
987 if (surface->pending.buffer)
988 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
990 weston_buffer_reference(&surface->buffer_ref, NULL);
992 compositor->renderer->destroy_surface(surface);
994 pixman_region32_fini(&surface->transform.boundingbox);
995 pixman_region32_fini(&surface->damage);
996 pixman_region32_fini(&surface->opaque);
997 pixman_region32_fini(&surface->clip);
998 pixman_region32_fini(&surface->input);
1000 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1001 wl_resource_destroy(&cb->resource);
1003 weston_surface_set_transform_parent(surface, NULL);
1009 weston_surface_destroy(struct weston_surface *surface)
1011 /* Not a valid way to destroy a client surface */
1012 assert(surface->resource.client == NULL);
1014 wl_signal_emit(&surface->resource.destroy_signal, &surface->resource);
1015 destroy_surface(&surface->resource);
1019 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1022 struct weston_buffer_reference *ref =
1023 container_of(listener, struct weston_buffer_reference,
1026 assert((struct wl_buffer *)data == ref->buffer);
1031 weston_buffer_reference(struct weston_buffer_reference *ref,
1032 struct wl_buffer *buffer)
1034 if (ref->buffer && buffer != ref->buffer) {
1035 ref->buffer->busy_count--;
1036 if (ref->buffer->busy_count == 0) {
1037 assert(ref->buffer->resource.client != NULL);
1038 wl_resource_queue_event(&ref->buffer->resource,
1041 wl_list_remove(&ref->destroy_listener.link);
1044 if (buffer && buffer != ref->buffer) {
1045 buffer->busy_count++;
1046 wl_signal_add(&buffer->resource.destroy_signal,
1047 &ref->destroy_listener);
1050 ref->buffer = buffer;
1051 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1055 weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1057 weston_buffer_reference(&surface->buffer_ref, buffer);
1060 if (weston_surface_is_mapped(surface))
1061 weston_surface_unmap(surface);
1064 surface->compositor->renderer->attach(surface, buffer);
1068 weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
1070 wl_list_remove(&surface->layer_link);
1071 wl_list_insert(below, &surface->layer_link);
1072 weston_surface_damage_below(surface);
1073 weston_surface_damage(surface);
1077 weston_compositor_damage_all(struct weston_compositor *compositor)
1079 struct weston_output *output;
1081 wl_list_for_each(output, &compositor->output_list, link)
1082 weston_output_damage(output);
1086 weston_output_damage(struct weston_output *output)
1088 struct weston_compositor *compositor = output->compositor;
1090 pixman_region32_union(&compositor->primary_plane.damage,
1091 &compositor->primary_plane.damage,
1093 weston_output_schedule_repaint(output);
1097 surface_accumulate_damage(struct weston_surface *surface,
1098 pixman_region32_t *opaque)
1100 if (surface->buffer_ref.buffer &&
1101 wl_buffer_is_shm(surface->buffer_ref.buffer))
1102 surface->compositor->renderer->flush_damage(surface);
1104 if (surface->transform.enabled) {
1105 pixman_box32_t *extents;
1107 extents = pixman_region32_extents(&surface->damage);
1108 surface_compute_bbox(surface, extents->x1, extents->y1,
1109 extents->x2 - extents->x1,
1110 extents->y2 - extents->y1,
1112 pixman_region32_translate(&surface->damage,
1114 -surface->plane->y);
1116 pixman_region32_translate(&surface->damage,
1117 surface->geometry.x - surface->plane->x,
1118 surface->geometry.y - surface->plane->y);
1121 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
1122 pixman_region32_union(&surface->plane->damage,
1123 &surface->plane->damage, &surface->damage);
1124 empty_region(&surface->damage);
1125 pixman_region32_copy(&surface->clip, opaque);
1126 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
1130 compositor_accumulate_damage(struct weston_compositor *ec)
1132 struct weston_plane *plane;
1133 struct weston_surface *es;
1134 pixman_region32_t opaque, clip;
1136 pixman_region32_init(&clip);
1138 wl_list_for_each(plane, &ec->plane_list, link) {
1139 pixman_region32_copy(&plane->clip, &clip);
1141 pixman_region32_init(&opaque);
1143 wl_list_for_each(es, &ec->surface_list, link) {
1144 if (es->plane != plane)
1147 surface_accumulate_damage(es, &opaque);
1150 pixman_region32_union(&clip, &clip, &opaque);
1151 pixman_region32_fini(&opaque);
1154 pixman_region32_fini(&clip);
1156 wl_list_for_each(es, &ec->surface_list, link) {
1157 /* Both the renderer and the backend have seen the buffer
1158 * by now. If renderer needs the buffer, it has its own
1159 * reference set. If the backend wants to keep the buffer
1160 * around for migrating the surface into a non-primary plane
1161 * later, keep_buffer is true. Otherwise, drop the core
1162 * reference now, and allow early buffer release. This enables
1163 * clients to use single-buffering.
1165 if (!es->keep_buffer)
1166 weston_buffer_reference(&es->buffer_ref, NULL);
1171 weston_output_repaint(struct weston_output *output, uint32_t msecs)
1173 struct weston_compositor *ec = output->compositor;
1174 struct weston_surface *es;
1175 struct weston_layer *layer;
1176 struct weston_animation *animation, *next;
1177 struct weston_frame_callback *cb, *cnext;
1178 struct wl_list frame_callback_list;
1179 pixman_region32_t output_damage;
1181 /* Rebuild the surface list and update surface transforms up front. */
1182 wl_list_init(&ec->surface_list);
1183 wl_list_init(&frame_callback_list);
1184 wl_list_for_each(layer, &ec->layer_list, link) {
1185 wl_list_for_each(es, &layer->surface_list, layer_link) {
1186 weston_surface_update_transform(es);
1187 wl_list_insert(ec->surface_list.prev, &es->link);
1188 if (es->output == output) {
1189 wl_list_insert_list(&frame_callback_list,
1190 &es->frame_callback_list);
1191 wl_list_init(&es->frame_callback_list);
1196 if (output->assign_planes && !output->disable_planes)
1197 output->assign_planes(output);
1199 wl_list_for_each(es, &ec->surface_list, link)
1200 weston_surface_move_to_plane(es, &ec->primary_plane);
1202 compositor_accumulate_damage(ec);
1204 pixman_region32_init(&output_damage);
1205 pixman_region32_intersect(&output_damage,
1206 &ec->primary_plane.damage, &output->region);
1207 pixman_region32_subtract(&output_damage,
1208 &output_damage, &ec->primary_plane.clip);
1211 weston_output_update_matrix(output);
1213 output->repaint(output, &output_damage);
1215 pixman_region32_fini(&output_damage);
1217 output->repaint_needed = 0;
1219 weston_compositor_repick(ec);
1220 wl_event_loop_dispatch(ec->input_loop, 0);
1222 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
1223 wl_callback_send_done(&cb->resource, msecs);
1224 wl_resource_destroy(&cb->resource);
1227 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
1228 animation->frame_counter++;
1229 animation->frame(animation, output, msecs);
1234 weston_compositor_read_input(int fd, uint32_t mask, void *data)
1236 struct weston_compositor *compositor = data;
1238 wl_event_loop_dispatch(compositor->input_loop, 0);
1244 weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
1246 struct weston_compositor *compositor = output->compositor;
1247 struct wl_event_loop *loop =
1248 wl_display_get_event_loop(compositor->wl_display);
1251 output->frame_time = msecs;
1252 if (output->repaint_needed) {
1253 weston_output_repaint(output, msecs);
1257 output->repaint_scheduled = 0;
1258 if (compositor->input_loop_source)
1261 fd = wl_event_loop_get_fd(compositor->input_loop);
1262 compositor->input_loop_source =
1263 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1264 weston_compositor_read_input, compositor);
1268 idle_repaint(void *data)
1270 struct weston_output *output = data;
1272 output->start_repaint_loop(output);
1276 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1278 wl_list_init(&layer->surface_list);
1280 wl_list_insert(below, &layer->link);
1284 weston_output_schedule_repaint(struct weston_output *output)
1286 struct weston_compositor *compositor = output->compositor;
1287 struct wl_event_loop *loop;
1289 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1290 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
1293 loop = wl_display_get_event_loop(compositor->wl_display);
1294 output->repaint_needed = 1;
1295 if (output->repaint_scheduled)
1298 wl_event_loop_add_idle(loop, idle_repaint, output);
1299 output->repaint_scheduled = 1;
1301 if (compositor->input_loop_source) {
1302 wl_event_source_remove(compositor->input_loop_source);
1303 compositor->input_loop_source = NULL;
1308 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1310 struct weston_output *output;
1312 wl_list_for_each(output, &compositor->output_list, link)
1313 weston_output_schedule_repaint(output);
1317 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1319 wl_resource_destroy(resource);
1323 surface_attach(struct wl_client *client,
1324 struct wl_resource *resource,
1325 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1327 struct weston_surface *surface = resource->data;
1328 struct wl_buffer *buffer = NULL;
1330 if (buffer_resource)
1331 buffer = buffer_resource->data;
1333 /* Attach, attach, without commit in between does not send
1334 * wl_buffer.release. */
1335 if (surface->pending.buffer)
1336 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1338 surface->pending.sx = sx;
1339 surface->pending.sy = sy;
1340 surface->pending.buffer = buffer;
1341 surface->pending.newly_attached = 1;
1343 wl_signal_add(&buffer->resource.destroy_signal,
1344 &surface->pending.buffer_destroy_listener);
1349 surface_damage(struct wl_client *client,
1350 struct wl_resource *resource,
1351 int32_t x, int32_t y, int32_t width, int32_t height)
1353 struct weston_surface *surface = resource->data;
1355 pixman_region32_union_rect(&surface->pending.damage,
1356 &surface->pending.damage,
1357 x, y, width, height);
1361 destroy_frame_callback(struct wl_resource *resource)
1363 struct weston_frame_callback *cb = resource->data;
1365 wl_list_remove(&cb->link);
1370 surface_frame(struct wl_client *client,
1371 struct wl_resource *resource, uint32_t callback)
1373 struct weston_frame_callback *cb;
1374 struct weston_surface *surface = resource->data;
1376 cb = malloc(sizeof *cb);
1378 wl_resource_post_no_memory(resource);
1382 cb->resource.object.interface = &wl_callback_interface;
1383 cb->resource.object.id = callback;
1384 cb->resource.destroy = destroy_frame_callback;
1385 cb->resource.client = client;
1386 cb->resource.data = cb;
1388 wl_client_add_resource(client, &cb->resource);
1389 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
1393 surface_set_opaque_region(struct wl_client *client,
1394 struct wl_resource *resource,
1395 struct wl_resource *region_resource)
1397 struct weston_surface *surface = resource->data;
1398 struct weston_region *region;
1400 if (region_resource) {
1401 region = region_resource->data;
1402 pixman_region32_copy(&surface->pending.opaque,
1405 empty_region(&surface->pending.opaque);
1410 surface_set_input_region(struct wl_client *client,
1411 struct wl_resource *resource,
1412 struct wl_resource *region_resource)
1414 struct weston_surface *surface = resource->data;
1415 struct weston_region *region;
1417 if (region_resource) {
1418 region = region_resource->data;
1419 pixman_region32_copy(&surface->pending.input,
1422 pixman_region32_fini(&surface->pending.input);
1423 region_init_infinite(&surface->pending.input);
1428 surface_commit(struct wl_client *client, struct wl_resource *resource)
1430 struct weston_surface *surface = resource->data;
1431 pixman_region32_t opaque;
1432 int buffer_width = 0;
1433 int buffer_height = 0;
1435 /* wl_surface.set_buffer_rotation */
1436 surface->buffer_transform = surface->pending.buffer_transform;
1438 /* wl_surface.attach */
1439 if (surface->pending.buffer || surface->pending.newly_attached)
1440 weston_surface_attach(surface, surface->pending.buffer);
1442 if (surface->buffer_ref.buffer) {
1443 buffer_width = weston_surface_buffer_width(surface);
1444 buffer_height = weston_surface_buffer_height(surface);
1447 if (surface->configure && surface->pending.newly_attached)
1448 surface->configure(surface,
1449 surface->pending.sx, surface->pending.sy,
1450 buffer_width, buffer_height);
1452 if (surface->pending.buffer)
1453 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1454 surface->pending.buffer = NULL;
1455 surface->pending.sx = 0;
1456 surface->pending.sy = 0;
1457 surface->pending.newly_attached = 0;
1459 /* wl_surface.damage */
1460 pixman_region32_union(&surface->damage, &surface->damage,
1461 &surface->pending.damage);
1462 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1464 surface->geometry.width,
1465 surface->geometry.height);
1466 empty_region(&surface->pending.damage);
1468 /* wl_surface.set_opaque_region */
1469 pixman_region32_init_rect(&opaque, 0, 0,
1470 surface->geometry.width,
1471 surface->geometry.height);
1472 pixman_region32_intersect(&opaque,
1473 &opaque, &surface->pending.opaque);
1475 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1476 pixman_region32_copy(&surface->opaque, &opaque);
1477 weston_surface_geometry_dirty(surface);
1480 pixman_region32_fini(&opaque);
1482 /* wl_surface.set_input_region */
1483 pixman_region32_fini(&surface->input);
1484 pixman_region32_init_rect(&surface->input, 0, 0,
1485 surface->geometry.width,
1486 surface->geometry.height);
1487 pixman_region32_intersect(&surface->input,
1488 &surface->input, &surface->pending.input);
1490 /* wl_surface.frame */
1491 wl_list_insert_list(&surface->frame_callback_list,
1492 &surface->pending.frame_callback_list);
1493 wl_list_init(&surface->pending.frame_callback_list);
1495 weston_surface_schedule_repaint(surface);
1499 surface_set_buffer_transform(struct wl_client *client,
1500 struct wl_resource *resource, int transform)
1502 struct weston_surface *surface = resource->data;
1504 surface->pending.buffer_transform = transform;
1507 static const struct wl_surface_interface surface_interface = {
1512 surface_set_opaque_region,
1513 surface_set_input_region,
1515 surface_set_buffer_transform
1519 compositor_create_surface(struct wl_client *client,
1520 struct wl_resource *resource, uint32_t id)
1522 struct weston_compositor *ec = resource->data;
1523 struct weston_surface *surface;
1525 surface = weston_surface_create(ec);
1526 if (surface == NULL) {
1527 wl_resource_post_no_memory(resource);
1531 surface->resource.destroy = destroy_surface;
1533 surface->resource.object.id = id;
1534 surface->resource.object.interface = &wl_surface_interface;
1535 surface->resource.object.implementation =
1536 (void (**)(void)) &surface_interface;
1537 surface->resource.data = surface;
1539 wl_client_add_resource(client, &surface->resource);
1543 destroy_region(struct wl_resource *resource)
1545 struct weston_region *region =
1546 container_of(resource, struct weston_region, resource);
1548 pixman_region32_fini(®ion->region);
1553 region_destroy(struct wl_client *client, struct wl_resource *resource)
1555 wl_resource_destroy(resource);
1559 region_add(struct wl_client *client, struct wl_resource *resource,
1560 int32_t x, int32_t y, int32_t width, int32_t height)
1562 struct weston_region *region = resource->data;
1564 pixman_region32_union_rect(®ion->region, ®ion->region,
1565 x, y, width, height);
1569 region_subtract(struct wl_client *client, struct wl_resource *resource,
1570 int32_t x, int32_t y, int32_t width, int32_t height)
1572 struct weston_region *region = resource->data;
1573 pixman_region32_t rect;
1575 pixman_region32_init_rect(&rect, x, y, width, height);
1576 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
1577 pixman_region32_fini(&rect);
1580 static const struct wl_region_interface region_interface = {
1587 compositor_create_region(struct wl_client *client,
1588 struct wl_resource *resource, uint32_t id)
1590 struct weston_region *region;
1592 region = malloc(sizeof *region);
1593 if (region == NULL) {
1594 wl_resource_post_no_memory(resource);
1598 region->resource.destroy = destroy_region;
1600 region->resource.object.id = id;
1601 region->resource.object.interface = &wl_region_interface;
1602 region->resource.object.implementation =
1603 (void (**)(void)) ®ion_interface;
1604 region->resource.data = region;
1606 pixman_region32_init(®ion->region);
1608 wl_client_add_resource(client, ®ion->resource);
1611 static const struct wl_compositor_interface compositor_interface = {
1612 compositor_create_surface,
1613 compositor_create_region
1617 weston_compositor_dpms(struct weston_compositor *compositor,
1618 enum dpms_enum state)
1620 struct weston_output *output;
1622 wl_list_for_each(output, &compositor->output_list, link)
1623 if (output->set_dpms)
1624 output->set_dpms(output, state);
1628 weston_compositor_wake(struct weston_compositor *compositor)
1630 switch (compositor->state) {
1631 case WESTON_COMPOSITOR_SLEEPING:
1632 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
1634 case WESTON_COMPOSITOR_IDLE:
1635 case WESTON_COMPOSITOR_OFFSCREEN:
1636 wl_signal_emit(&compositor->wake_signal, compositor);
1639 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1640 wl_event_source_timer_update(compositor->idle_source,
1641 compositor->idle_time * 1000);
1646 weston_compositor_offscreen(struct weston_compositor *compositor)
1648 switch (compositor->state) {
1649 case WESTON_COMPOSITOR_OFFSCREEN:
1651 case WESTON_COMPOSITOR_SLEEPING:
1652 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
1655 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
1656 wl_event_source_timer_update(compositor->idle_source, 0);
1661 weston_compositor_sleep(struct weston_compositor *compositor)
1663 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1666 wl_event_source_timer_update(compositor->idle_source, 0);
1667 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1668 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
1672 idle_handler(void *data)
1674 struct weston_compositor *compositor = data;
1676 if (compositor->idle_inhibit)
1679 compositor->state = WESTON_COMPOSITOR_IDLE;
1680 wl_signal_emit(&compositor->idle_signal, compositor);
1686 weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1688 pixman_region32_init(&plane->damage);
1689 pixman_region32_init(&plane->clip);
1695 weston_plane_release(struct weston_plane *plane)
1697 pixman_region32_fini(&plane->damage);
1698 pixman_region32_fini(&plane->clip);
1702 weston_compositor_stack_plane(struct weston_compositor *ec,
1703 struct weston_plane *plane,
1704 struct weston_plane *above)
1707 wl_list_insert(above->link.prev, &plane->link);
1709 wl_list_insert(&ec->plane_list, &plane->link);
1712 static void unbind_resource(struct wl_resource *resource)
1714 wl_list_remove(&resource->link);
1719 bind_output(struct wl_client *client,
1720 void *data, uint32_t version, uint32_t id)
1722 struct weston_output *output = data;
1723 struct weston_mode *mode;
1724 struct wl_resource *resource;
1726 resource = wl_client_add_object(client,
1727 &wl_output_interface, NULL, id, data);
1729 wl_list_insert(&output->resource_list, &resource->link);
1730 resource->destroy = unbind_resource;
1732 wl_output_send_geometry(resource,
1738 output->make, output->model,
1741 wl_list_for_each (mode, &output->mode_list, link) {
1742 wl_output_send_mode(resource,
1751 weston_output_destroy(struct weston_output *output)
1753 struct weston_compositor *c = output->compositor;
1755 wl_signal_emit(&output->destroy_signal, output);
1758 pixman_region32_fini(&output->region);
1759 pixman_region32_fini(&output->previous_damage);
1760 output->compositor->output_id_pool &= ~(1 << output->id);
1762 wl_display_remove_global(c->wl_display, output->global);
1766 weston_output_compute_transform(struct weston_output *output)
1768 struct weston_matrix transform;
1771 weston_matrix_init(&transform);
1772 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
1774 switch(output->transform) {
1775 case WL_OUTPUT_TRANSFORM_FLIPPED:
1776 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1777 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1778 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1779 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
1787 switch(output->transform) {
1788 case WL_OUTPUT_TRANSFORM_NORMAL:
1789 case WL_OUTPUT_TRANSFORM_FLIPPED:
1790 transform.d[0] = flip;
1795 case WL_OUTPUT_TRANSFORM_90:
1796 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1798 transform.d[1] = -flip;
1802 case WL_OUTPUT_TRANSFORM_180:
1803 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1804 transform.d[0] = -flip;
1807 transform.d[5] = -1;
1809 case WL_OUTPUT_TRANSFORM_270:
1810 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1812 transform.d[1] = flip;
1813 transform.d[4] = -1;
1820 weston_matrix_multiply(&output->matrix, &transform);
1824 weston_output_update_matrix(struct weston_output *output)
1826 float magnification;
1827 struct weston_matrix camera;
1828 struct weston_matrix modelview;
1830 weston_matrix_init(&output->matrix);
1831 weston_matrix_translate(&output->matrix,
1832 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
1833 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
1835 weston_matrix_scale(&output->matrix,
1836 2.0 / (output->width + output->border.left + output->border.right),
1837 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
1839 weston_output_compute_transform(output);
1841 if (output->zoom.active) {
1842 magnification = 1 / (1 - output->zoom.spring_z.current);
1843 weston_matrix_init(&camera);
1844 weston_matrix_init(&modelview);
1845 weston_output_update_zoom(output, output->zoom.type);
1846 weston_matrix_translate(&camera, output->zoom.trans_x,
1847 -output->zoom.trans_y, 0);
1848 weston_matrix_invert(&modelview, &camera);
1849 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
1850 weston_matrix_multiply(&output->matrix, &modelview);
1857 weston_output_transform_init(struct weston_output *output, uint32_t transform)
1859 output->transform = transform;
1861 switch (transform) {
1862 case WL_OUTPUT_TRANSFORM_90:
1863 case WL_OUTPUT_TRANSFORM_270:
1864 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1865 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1866 /* Swap width and height */
1867 output->width = output->current->height;
1868 output->height = output->current->width;
1870 case WL_OUTPUT_TRANSFORM_NORMAL:
1871 case WL_OUTPUT_TRANSFORM_180:
1872 case WL_OUTPUT_TRANSFORM_FLIPPED:
1873 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1874 output->width = output->current->width;
1875 output->height = output->current->height;
1883 weston_output_move(struct weston_output *output, int x, int y)
1888 pixman_region32_init(&output->previous_damage);
1889 pixman_region32_init_rect(&output->region, x, y,
1895 weston_output_init(struct weston_output *output, struct weston_compositor *c,
1896 int x, int y, int width, int height, uint32_t transform)
1898 output->compositor = c;
1901 output->border.top = 0;
1902 output->border.bottom = 0;
1903 output->border.left = 0;
1904 output->border.right = 0;
1905 output->mm_width = width;
1906 output->mm_height = height;
1909 weston_output_transform_init(output, transform);
1910 weston_output_init_zoom(output);
1912 weston_output_move(output, x, y);
1913 weston_output_damage(output);
1915 wl_signal_init(&output->frame_signal);
1916 wl_signal_init(&output->destroy_signal);
1917 wl_list_init(&output->animation_list);
1918 wl_list_init(&output->resource_list);
1920 output->id = ffs(~output->compositor->output_id_pool) - 1;
1921 output->compositor->output_id_pool |= 1 << output->id;
1924 wl_display_add_global(c->wl_display, &wl_output_interface,
1925 output, bind_output);
1926 wl_signal_emit(&c->output_created_signal, output);
1930 compositor_bind(struct wl_client *client,
1931 void *data, uint32_t version, uint32_t id)
1933 struct weston_compositor *compositor = data;
1935 wl_client_add_object(client, &wl_compositor_interface,
1936 &compositor_interface, id, compositor);
1942 struct utsname usys;
1946 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
1947 usys.version, usys.machine);
1951 weston_environment_get_fd(const char *env)
1959 fd = strtol(e, &end, 0);
1963 flags = fcntl(fd, F_GETFD);
1967 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
1974 weston_compositor_init(struct weston_compositor *ec,
1975 struct wl_display *display,
1976 int *argc, char *argv[],
1977 const char *config_file)
1979 struct wl_event_loop *loop;
1980 struct xkb_rule_names xkb_names;
1981 const struct config_key keyboard_config_keys[] = {
1982 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
1983 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
1984 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
1985 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
1986 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
1988 const struct config_section cs[] = {
1990 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
1993 memset(&xkb_names, 0, sizeof(xkb_names));
1994 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
1996 ec->wl_display = display;
1997 wl_signal_init(&ec->destroy_signal);
1998 wl_signal_init(&ec->activate_signal);
1999 wl_signal_init(&ec->kill_signal);
2000 wl_signal_init(&ec->idle_signal);
2001 wl_signal_init(&ec->wake_signal);
2002 wl_signal_init(&ec->show_input_panel_signal);
2003 wl_signal_init(&ec->hide_input_panel_signal);
2004 wl_signal_init(&ec->update_input_panel_signal);
2005 wl_signal_init(&ec->seat_created_signal);
2006 wl_signal_init(&ec->output_created_signal);
2007 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
2009 ec->output_id_pool = 0;
2011 if (!wl_display_add_global(display, &wl_compositor_interface,
2012 ec, compositor_bind))
2015 wl_list_init(&ec->surface_list);
2016 wl_list_init(&ec->plane_list);
2017 wl_list_init(&ec->layer_list);
2018 wl_list_init(&ec->seat_list);
2019 wl_list_init(&ec->output_list);
2020 wl_list_init(&ec->key_binding_list);
2021 wl_list_init(&ec->button_binding_list);
2022 wl_list_init(&ec->axis_binding_list);
2023 wl_list_init(&ec->debug_binding_list);
2025 weston_plane_init(&ec->primary_plane, 0, 0);
2026 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
2028 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2031 ec->ping_handler = NULL;
2033 screenshooter_create(ec);
2034 text_cursor_position_notifier_create(ec);
2035 text_backend_init(ec);
2037 wl_data_device_manager_init(ec->wl_display);
2039 wl_display_init_shm(display);
2041 loop = wl_display_get_event_loop(ec->wl_display);
2042 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2043 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2045 ec->input_loop = wl_event_loop_create();
2047 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2048 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2050 weston_compositor_schedule_repaint(ec);
2056 weston_compositor_shutdown(struct weston_compositor *ec)
2058 struct weston_output *output, *next;
2060 wl_event_source_remove(ec->idle_source);
2061 if (ec->input_loop_source)
2062 wl_event_source_remove(ec->input_loop_source);
2064 /* Destroy all outputs associated with this compositor */
2065 wl_list_for_each_safe(output, next, &ec->output_list, link)
2066 output->destroy(output);
2068 weston_binding_list_destroy_all(&ec->key_binding_list);
2069 weston_binding_list_destroy_all(&ec->button_binding_list);
2070 weston_binding_list_destroy_all(&ec->axis_binding_list);
2071 weston_binding_list_destroy_all(&ec->debug_binding_list);
2073 weston_plane_release(&ec->primary_plane);
2075 wl_event_loop_destroy(ec->input_loop);
2079 weston_version(int *major, int *minor, int *micro)
2081 *major = WESTON_VERSION_MAJOR;
2082 *minor = WESTON_VERSION_MINOR;
2083 *micro = WESTON_VERSION_MICRO;
2086 static int on_term_signal(int signal_number, void *data)
2088 struct wl_display *display = data;
2090 weston_log("caught signal %d\n", signal_number);
2091 wl_display_terminate(display);
2096 #ifdef HAVE_LIBUNWIND
2099 print_backtrace(void)
2101 unw_cursor_t cursor;
2102 unw_context_t context;
2104 unw_proc_info_t pip;
2107 const char *filename;
2110 pip.unwind_info = NULL;
2111 ret = unw_getcontext(&context);
2113 weston_log("unw_getcontext: %d\n", ret);
2117 ret = unw_init_local(&cursor, &context);
2119 weston_log("unw_init_local: %d\n", ret);
2123 ret = unw_step(&cursor);
2125 ret = unw_get_proc_info(&cursor, &pip);
2127 weston_log("unw_get_proc_info: %d\n", ret);
2131 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2132 if (ret && ret != -UNW_ENOMEM) {
2133 if (ret != -UNW_EUNSPEC)
2134 weston_log("unw_get_proc_name: %d\n", ret);
2139 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2141 filename = dlinfo.dli_fname;
2145 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2146 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2148 ret = unw_step(&cursor);
2150 weston_log("unw_step: %d\n", ret);
2157 print_backtrace(void)
2163 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2164 for (i = 0; i < count; i++) {
2165 dladdr(buffer[i], &info);
2166 weston_log(" [%016lx] %s (%s)\n",
2168 info.dli_sname ? info.dli_sname : "--",
2176 on_caught_signal(int s, siginfo_t *siginfo, void *context)
2178 /* This signal handler will do a best-effort backtrace, and
2179 * then call the backend restore function, which will switch
2180 * back to the vt we launched from or ungrab X etc and then
2181 * raise SIGTRAP. If we run weston under gdb from X or a
2182 * different vt, and tell gdb "handle *s* nostop", this
2183 * will allow weston to switch back to gdb on crash and then
2184 * gdb will catch the crash with SIGTRAP.*/
2186 weston_log("caught signal: %d\n", s);
2190 segv_compositor->restore(segv_compositor);
2196 load_module(const char *name, const char *entrypoint)
2198 char path[PATH_MAX];
2199 void *module, *init;
2202 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
2204 snprintf(path, sizeof path, "%s", name);
2206 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2208 weston_log("Module '%s' already loaded\n", path);
2213 weston_log("Loading module '%s'\n", path);
2214 module = dlopen(path, RTLD_NOW);
2216 weston_log("Failed to load module: %s\n", dlerror());
2220 init = dlsym(module, entrypoint);
2222 weston_log("Failed to lookup init function: %s\n", dlerror());
2231 load_modules(struct weston_compositor *ec, const char *modules,
2232 int *argc, char *argv[], const char *config_file)
2234 const char *p, *end;
2236 int (*module_init)(struct weston_compositor *ec,
2237 int *argc, char *argv[], const char *config_file);
2239 if (modules == NULL)
2244 end = strchrnul(p, ',');
2245 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2246 module_init = load_module(buffer, "module_init");
2248 module_init(ec, argc, argv, config_file);
2258 static const char xdg_error_message[] =
2259 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2261 static const char xdg_wrong_message[] =
2262 "fatal: environment variable XDG_RUNTIME_DIR\n"
2263 "is set to \"%s\", which is not a directory.\n";
2265 static const char xdg_wrong_mode_message[] =
2266 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2267 "correctly. Unix access mode must be 0700 but is %o,\n"
2268 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
2269 "owned by UID %d.\n";
2271 static const char xdg_detail_message[] =
2272 "Refer to your distribution on how to get it, or\n"
2273 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2274 "on how to implement it.\n";
2277 verify_xdg_runtime_dir(void)
2279 char *dir = getenv("XDG_RUNTIME_DIR");
2283 weston_log(xdg_error_message);
2284 weston_log_continue(xdg_detail_message);
2288 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2289 weston_log(xdg_wrong_message, dir);
2290 weston_log_continue(xdg_detail_message);
2294 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2295 weston_log(xdg_wrong_mode_message,
2296 dir, s.st_mode & 0777, s.st_uid);
2297 weston_log_continue(xdg_detail_message);
2302 usage(int error_code)
2305 "Usage: weston [OPTIONS]\n\n"
2306 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2307 "Weston supports multiple backends, and depending on which backend is in use\n"
2308 "different options will be accepted.\n\n"
2312 " --version\t\tPrint weston version\n"
2313 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
2314 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
2315 "\t\t\t\twayland-backend.so\n"
2316 " -S, --socket=NAME\tName of socket to listen on\n"
2317 " -i, --idle-time=SECS\tIdle time in seconds\n"
2318 " --modules\t\tLoad the comma-separated list of modules\n"
2319 " --log==FILE\t\tLog to the given file\n"
2320 " -h, --help\t\tThis help message\n\n");
2323 "Options for drm-backend.so:\n\n"
2324 " --connector=ID\tBring up only this connector\n"
2325 " --seat=SEAT\t\tThe seat that weston should run on\n"
2326 " --tty=TTY\t\tThe tty to use\n"
2327 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
2328 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
2331 "Options for fbdev-backend.so:\n\n"
2332 " --tty=TTY\t\tThe tty to use\n"
2333 " --device=DEVICE\tThe framebuffer device to use\n\n");
2336 "Options for x11-backend.so:\n\n"
2337 " --width=WIDTH\t\tWidth of X window\n"
2338 " --height=HEIGHT\tHeight of X window\n"
2339 " --fullscreen\t\tRun in fullscreen mode\n"
2340 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
2341 " --output-count=COUNT\tCreate multiple outputs\n"
2342 " --no-input\t\tDont create input devices\n\n");
2345 "Options for wayland-backend.so:\n\n"
2346 " --width=WIDTH\t\tWidth of Wayland surface\n"
2347 " --height=HEIGHT\tHeight of Wayland surface\n"
2348 " --display=DISPLAY\tWayland display to connect to\n\n");
2356 struct sigaction action;
2358 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
2359 action.sa_sigaction = on_caught_signal;
2360 sigemptyset(&action.sa_mask);
2361 sigaction(SIGSEGV, &action, NULL);
2362 sigaction(SIGABRT, &action, NULL);
2365 int main(int argc, char *argv[])
2367 int ret = EXIT_SUCCESS;
2368 struct wl_display *display;
2369 struct weston_compositor *ec;
2370 struct wl_event_source *signals[4];
2371 struct wl_event_loop *loop;
2372 struct weston_compositor
2373 *(*backend_init)(struct wl_display *display,
2374 int *argc, char *argv[], const char *config_file);
2376 char *backend = NULL;
2377 const char *modules = "desktop-shell.so", *option_modules = NULL;
2379 int32_t idle_time = 300;
2381 char *socket_name = "wayland-0";
2382 int32_t version = 0;
2385 const struct config_key core_config_keys[] = {
2386 { "modules", CONFIG_KEY_STRING, &modules },
2389 const struct config_section cs[] = {
2391 core_config_keys, ARRAY_LENGTH(core_config_keys) },
2394 const struct weston_option core_options[] = {
2395 { WESTON_OPTION_STRING, "backend", 'B', &backend },
2396 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
2397 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
2398 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
2399 { WESTON_OPTION_STRING, "log", 0, &log },
2400 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
2401 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
2404 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
2407 usage(EXIT_SUCCESS);
2410 printf(PACKAGE_STRING "\n");
2411 return EXIT_SUCCESS;
2414 weston_log_file_open(log);
2418 STAMP_SPACE "Bug reports to: %s\n"
2419 STAMP_SPACE "Build: %s\n",
2420 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
2424 verify_xdg_runtime_dir();
2426 display = wl_display_create();
2428 loop = wl_display_get_event_loop(display);
2429 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2431 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2433 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2436 wl_list_init(&child_process_list);
2437 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2441 if (getenv("WAYLAND_DISPLAY"))
2442 backend = "wayland-backend.so";
2443 else if (getenv("DISPLAY"))
2444 backend = "x11-backend.so";
2446 backend = WESTON_NATIVE_BACKEND;
2449 config_file = config_file_path("weston.ini");
2450 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
2452 backend_init = load_module(backend, "backend_init");
2456 ec = backend_init(display, &argc, argv, config_file);
2458 weston_log("fatal: failed to create compositor\n");
2463 segv_compositor = ec;
2465 ec->idle_time = idle_time;
2467 setenv("WAYLAND_DISPLAY", socket_name, 1);
2469 if (load_modules(ec, modules, &argc, argv, config_file) < 0)
2471 if (load_modules(ec, option_modules, &argc, argv, config_file) < 0)
2476 for (i = 1; i < argc; i++)
2477 weston_log("fatal: unhandled option: %s\n", argv[i]);
2483 if (wl_display_add_socket(display, socket_name)) {
2484 weston_log("fatal: failed to add socket: %m\n");
2489 weston_compositor_wake(ec);
2491 wl_display_run(display);
2494 /* prevent further rendering while shutting down */
2495 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
2497 wl_signal_emit(&ec->destroy_signal, ec);
2499 for (i = ARRAY_LENGTH(signals); i;)
2500 wl_event_source_remove(signals[--i]);
2502 weston_compositor_xkb_destroy(ec);
2505 wl_display_destroy(display);
2507 weston_log_file_close();