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->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->surface.resource.destroy_signal);
276 wl_list_init(&surface->link);
277 wl_list_init(&surface->layer_link);
279 surface->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->surface.resource.client;
495 es->output_mask = mask;
496 if (es->surface.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->surface.resource, resource);
510 if (1 << output->id & left)
511 wl_surface_send_leave(&es->surface.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->surface.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 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->seat.keyboard &&
950 seat->seat.keyboard->focus == &surface->surface)
951 weston_keyboard_set_focus(seat->seat.keyboard, NULL);
952 if (seat->seat.pointer &&
953 seat->seat.pointer->focus == &surface->surface)
954 weston_pointer_set_focus(seat->seat.pointer,
956 wl_fixed_from_int(0),
957 wl_fixed_from_int(0));
960 weston_surface_schedule_repaint(surface);
963 struct weston_frame_callback {
964 struct wl_resource resource;
969 destroy_surface(struct wl_resource *resource)
971 struct weston_surface *surface =
972 container_of(resource,
973 struct weston_surface, surface.resource);
974 struct weston_compositor *compositor = surface->compositor;
975 struct weston_frame_callback *cb, *next;
977 assert(wl_list_empty(&surface->geometry.child_list));
979 if (weston_surface_is_mapped(surface))
980 weston_surface_unmap(surface);
982 wl_list_for_each_safe(cb, next,
983 &surface->pending.frame_callback_list, link)
984 wl_resource_destroy(&cb->resource);
986 pixman_region32_fini(&surface->pending.input);
987 pixman_region32_fini(&surface->pending.opaque);
988 pixman_region32_fini(&surface->pending.damage);
990 if (surface->pending.buffer)
991 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
993 weston_buffer_reference(&surface->buffer_ref, NULL);
995 compositor->renderer->destroy_surface(surface);
997 pixman_region32_fini(&surface->transform.boundingbox);
998 pixman_region32_fini(&surface->damage);
999 pixman_region32_fini(&surface->opaque);
1000 pixman_region32_fini(&surface->clip);
1001 pixman_region32_fini(&surface->input);
1003 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1004 wl_resource_destroy(&cb->resource);
1006 weston_surface_set_transform_parent(surface, NULL);
1012 weston_surface_destroy(struct weston_surface *surface)
1014 /* Not a valid way to destroy a client surface */
1015 assert(surface->surface.resource.client == NULL);
1017 wl_signal_emit(&surface->surface.resource.destroy_signal,
1018 &surface->surface.resource);
1019 destroy_surface(&surface->surface.resource);
1023 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1026 struct weston_buffer_reference *ref =
1027 container_of(listener, struct weston_buffer_reference,
1030 assert((struct wl_buffer *)data == ref->buffer);
1035 weston_buffer_reference(struct weston_buffer_reference *ref,
1036 struct wl_buffer *buffer)
1038 if (ref->buffer && buffer != ref->buffer) {
1039 ref->buffer->busy_count--;
1040 if (ref->buffer->busy_count == 0) {
1041 assert(ref->buffer->resource.client != NULL);
1042 wl_resource_queue_event(&ref->buffer->resource,
1045 wl_list_remove(&ref->destroy_listener.link);
1048 if (buffer && buffer != ref->buffer) {
1049 buffer->busy_count++;
1050 wl_signal_add(&buffer->resource.destroy_signal,
1051 &ref->destroy_listener);
1054 ref->buffer = buffer;
1055 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1059 weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1061 weston_buffer_reference(&surface->buffer_ref, buffer);
1064 if (weston_surface_is_mapped(surface))
1065 weston_surface_unmap(surface);
1068 surface->compositor->renderer->attach(surface, buffer);
1072 weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
1074 wl_list_remove(&surface->layer_link);
1075 wl_list_insert(below, &surface->layer_link);
1076 weston_surface_damage_below(surface);
1077 weston_surface_damage(surface);
1081 weston_compositor_damage_all(struct weston_compositor *compositor)
1083 struct weston_output *output;
1085 wl_list_for_each(output, &compositor->output_list, link)
1086 weston_output_damage(output);
1090 weston_output_damage(struct weston_output *output)
1092 struct weston_compositor *compositor = output->compositor;
1094 pixman_region32_union(&compositor->primary_plane.damage,
1095 &compositor->primary_plane.damage,
1097 weston_output_schedule_repaint(output);
1101 surface_accumulate_damage(struct weston_surface *surface,
1102 pixman_region32_t *opaque)
1104 if (surface->buffer_ref.buffer &&
1105 wl_buffer_is_shm(surface->buffer_ref.buffer))
1106 surface->compositor->renderer->flush_damage(surface);
1108 if (surface->transform.enabled) {
1109 pixman_box32_t *extents;
1111 extents = pixman_region32_extents(&surface->damage);
1112 surface_compute_bbox(surface, extents->x1, extents->y1,
1113 extents->x2 - extents->x1,
1114 extents->y2 - extents->y1,
1116 pixman_region32_translate(&surface->damage,
1118 -surface->plane->y);
1120 pixman_region32_translate(&surface->damage,
1121 surface->geometry.x - surface->plane->x,
1122 surface->geometry.y - surface->plane->y);
1125 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
1126 pixman_region32_union(&surface->plane->damage,
1127 &surface->plane->damage, &surface->damage);
1128 empty_region(&surface->damage);
1129 pixman_region32_copy(&surface->clip, opaque);
1130 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
1134 compositor_accumulate_damage(struct weston_compositor *ec)
1136 struct weston_plane *plane;
1137 struct weston_surface *es;
1138 pixman_region32_t opaque, clip;
1140 pixman_region32_init(&clip);
1142 wl_list_for_each(plane, &ec->plane_list, link) {
1143 pixman_region32_copy(&plane->clip, &clip);
1145 pixman_region32_init(&opaque);
1147 wl_list_for_each(es, &ec->surface_list, link) {
1148 if (es->plane != plane)
1151 surface_accumulate_damage(es, &opaque);
1154 pixman_region32_union(&clip, &clip, &opaque);
1155 pixman_region32_fini(&opaque);
1158 pixman_region32_fini(&clip);
1160 wl_list_for_each(es, &ec->surface_list, link) {
1161 /* Both the renderer and the backend have seen the buffer
1162 * by now. If renderer needs the buffer, it has its own
1163 * reference set. If the backend wants to keep the buffer
1164 * around for migrating the surface into a non-primary plane
1165 * later, keep_buffer is true. Otherwise, drop the core
1166 * reference now, and allow early buffer release. This enables
1167 * clients to use single-buffering.
1169 if (!es->keep_buffer)
1170 weston_buffer_reference(&es->buffer_ref, NULL);
1175 weston_output_repaint(struct weston_output *output, uint32_t msecs)
1177 struct weston_compositor *ec = output->compositor;
1178 struct weston_surface *es;
1179 struct weston_layer *layer;
1180 struct weston_animation *animation, *next;
1181 struct weston_frame_callback *cb, *cnext;
1182 struct wl_list frame_callback_list;
1183 pixman_region32_t output_damage;
1185 weston_compositor_update_drag_surfaces(ec);
1187 /* Rebuild the surface list and update surface transforms up front. */
1188 wl_list_init(&ec->surface_list);
1189 wl_list_init(&frame_callback_list);
1190 wl_list_for_each(layer, &ec->layer_list, link) {
1191 wl_list_for_each(es, &layer->surface_list, layer_link) {
1192 weston_surface_update_transform(es);
1193 wl_list_insert(ec->surface_list.prev, &es->link);
1194 if (es->output == output) {
1195 wl_list_insert_list(&frame_callback_list,
1196 &es->frame_callback_list);
1197 wl_list_init(&es->frame_callback_list);
1202 if (output->assign_planes && !output->disable_planes)
1203 output->assign_planes(output);
1205 wl_list_for_each(es, &ec->surface_list, link)
1206 weston_surface_move_to_plane(es, &ec->primary_plane);
1208 compositor_accumulate_damage(ec);
1210 pixman_region32_init(&output_damage);
1211 pixman_region32_intersect(&output_damage,
1212 &ec->primary_plane.damage, &output->region);
1213 pixman_region32_subtract(&output_damage,
1214 &output_damage, &ec->primary_plane.clip);
1217 weston_output_update_matrix(output);
1219 output->repaint(output, &output_damage);
1221 pixman_region32_fini(&output_damage);
1223 output->repaint_needed = 0;
1225 weston_compositor_repick(ec);
1226 wl_event_loop_dispatch(ec->input_loop, 0);
1228 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
1229 wl_callback_send_done(&cb->resource, msecs);
1230 wl_resource_destroy(&cb->resource);
1233 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
1234 animation->frame_counter++;
1235 animation->frame(animation, output, msecs);
1240 weston_compositor_read_input(int fd, uint32_t mask, void *data)
1242 struct weston_compositor *compositor = data;
1244 wl_event_loop_dispatch(compositor->input_loop, 0);
1250 weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
1252 struct weston_compositor *compositor = output->compositor;
1253 struct wl_event_loop *loop =
1254 wl_display_get_event_loop(compositor->wl_display);
1257 output->frame_time = msecs;
1258 if (output->repaint_needed) {
1259 weston_output_repaint(output, msecs);
1263 output->repaint_scheduled = 0;
1264 if (compositor->input_loop_source)
1267 fd = wl_event_loop_get_fd(compositor->input_loop);
1268 compositor->input_loop_source =
1269 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1270 weston_compositor_read_input, compositor);
1274 idle_repaint(void *data)
1276 struct weston_output *output = data;
1278 output->start_repaint_loop(output);
1282 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1284 wl_list_init(&layer->surface_list);
1286 wl_list_insert(below, &layer->link);
1290 weston_output_schedule_repaint(struct weston_output *output)
1292 struct weston_compositor *compositor = output->compositor;
1293 struct wl_event_loop *loop;
1295 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1296 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
1299 loop = wl_display_get_event_loop(compositor->wl_display);
1300 output->repaint_needed = 1;
1301 if (output->repaint_scheduled)
1304 wl_event_loop_add_idle(loop, idle_repaint, output);
1305 output->repaint_scheduled = 1;
1307 if (compositor->input_loop_source) {
1308 wl_event_source_remove(compositor->input_loop_source);
1309 compositor->input_loop_source = NULL;
1314 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1316 struct weston_output *output;
1318 wl_list_for_each(output, &compositor->output_list, link)
1319 weston_output_schedule_repaint(output);
1323 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1325 wl_resource_destroy(resource);
1329 surface_attach(struct wl_client *client,
1330 struct wl_resource *resource,
1331 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1333 struct weston_surface *surface = resource->data;
1334 struct wl_buffer *buffer = NULL;
1336 if (buffer_resource)
1337 buffer = buffer_resource->data;
1339 /* Attach, attach, without commit in between does not send
1340 * wl_buffer.release. */
1341 if (surface->pending.buffer)
1342 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1344 surface->pending.sx = sx;
1345 surface->pending.sy = sy;
1346 surface->pending.buffer = buffer;
1347 surface->pending.newly_attached = 1;
1349 wl_signal_add(&buffer->resource.destroy_signal,
1350 &surface->pending.buffer_destroy_listener);
1355 surface_damage(struct wl_client *client,
1356 struct wl_resource *resource,
1357 int32_t x, int32_t y, int32_t width, int32_t height)
1359 struct weston_surface *surface = resource->data;
1361 pixman_region32_union_rect(&surface->pending.damage,
1362 &surface->pending.damage,
1363 x, y, width, height);
1367 destroy_frame_callback(struct wl_resource *resource)
1369 struct weston_frame_callback *cb = resource->data;
1371 wl_list_remove(&cb->link);
1376 surface_frame(struct wl_client *client,
1377 struct wl_resource *resource, uint32_t callback)
1379 struct weston_frame_callback *cb;
1380 struct weston_surface *surface = resource->data;
1382 cb = malloc(sizeof *cb);
1384 wl_resource_post_no_memory(resource);
1388 cb->resource.object.interface = &wl_callback_interface;
1389 cb->resource.object.id = callback;
1390 cb->resource.destroy = destroy_frame_callback;
1391 cb->resource.client = client;
1392 cb->resource.data = cb;
1394 wl_client_add_resource(client, &cb->resource);
1395 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
1399 surface_set_opaque_region(struct wl_client *client,
1400 struct wl_resource *resource,
1401 struct wl_resource *region_resource)
1403 struct weston_surface *surface = resource->data;
1404 struct weston_region *region;
1406 if (region_resource) {
1407 region = region_resource->data;
1408 pixman_region32_copy(&surface->pending.opaque,
1411 empty_region(&surface->pending.opaque);
1416 surface_set_input_region(struct wl_client *client,
1417 struct wl_resource *resource,
1418 struct wl_resource *region_resource)
1420 struct weston_surface *surface = resource->data;
1421 struct weston_region *region;
1423 if (region_resource) {
1424 region = region_resource->data;
1425 pixman_region32_copy(&surface->pending.input,
1428 pixman_region32_fini(&surface->pending.input);
1429 region_init_infinite(&surface->pending.input);
1434 surface_commit(struct wl_client *client, struct wl_resource *resource)
1436 struct weston_surface *surface = resource->data;
1437 pixman_region32_t opaque;
1438 int buffer_width = 0;
1439 int buffer_height = 0;
1441 /* wl_surface.set_buffer_rotation */
1442 surface->buffer_transform = surface->pending.buffer_transform;
1444 /* wl_surface.attach */
1445 if (surface->pending.buffer || surface->pending.newly_attached)
1446 weston_surface_attach(surface, surface->pending.buffer);
1448 if (surface->buffer_ref.buffer) {
1449 buffer_width = weston_surface_buffer_width(surface);
1450 buffer_height = weston_surface_buffer_height(surface);
1453 if (surface->configure && surface->pending.newly_attached)
1454 surface->configure(surface,
1455 surface->pending.sx, surface->pending.sy,
1456 buffer_width, buffer_height);
1458 if (surface->pending.buffer)
1459 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1460 surface->pending.buffer = NULL;
1461 surface->pending.sx = 0;
1462 surface->pending.sy = 0;
1463 surface->pending.newly_attached = 0;
1465 /* wl_surface.damage */
1466 pixman_region32_union(&surface->damage, &surface->damage,
1467 &surface->pending.damage);
1468 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1470 surface->geometry.width,
1471 surface->geometry.height);
1472 empty_region(&surface->pending.damage);
1474 /* wl_surface.set_opaque_region */
1475 pixman_region32_init_rect(&opaque, 0, 0,
1476 surface->geometry.width,
1477 surface->geometry.height);
1478 pixman_region32_intersect(&opaque,
1479 &opaque, &surface->pending.opaque);
1481 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1482 pixman_region32_copy(&surface->opaque, &opaque);
1483 weston_surface_geometry_dirty(surface);
1486 pixman_region32_fini(&opaque);
1488 /* wl_surface.set_input_region */
1489 pixman_region32_fini(&surface->input);
1490 pixman_region32_init_rect(&surface->input, 0, 0,
1491 surface->geometry.width,
1492 surface->geometry.height);
1493 pixman_region32_intersect(&surface->input,
1494 &surface->input, &surface->pending.input);
1496 /* wl_surface.frame */
1497 wl_list_insert_list(&surface->frame_callback_list,
1498 &surface->pending.frame_callback_list);
1499 wl_list_init(&surface->pending.frame_callback_list);
1501 weston_surface_schedule_repaint(surface);
1505 surface_set_buffer_transform(struct wl_client *client,
1506 struct wl_resource *resource, int transform)
1508 struct weston_surface *surface = resource->data;
1510 surface->pending.buffer_transform = transform;
1513 static const struct wl_surface_interface surface_interface = {
1518 surface_set_opaque_region,
1519 surface_set_input_region,
1521 surface_set_buffer_transform
1525 compositor_create_surface(struct wl_client *client,
1526 struct wl_resource *resource, uint32_t id)
1528 struct weston_compositor *ec = resource->data;
1529 struct weston_surface *surface;
1531 surface = weston_surface_create(ec);
1532 if (surface == NULL) {
1533 wl_resource_post_no_memory(resource);
1537 surface->surface.resource.destroy = destroy_surface;
1539 surface->surface.resource.object.id = id;
1540 surface->surface.resource.object.interface = &wl_surface_interface;
1541 surface->surface.resource.object.implementation =
1542 (void (**)(void)) &surface_interface;
1543 surface->surface.resource.data = surface;
1545 wl_client_add_resource(client, &surface->surface.resource);
1549 destroy_region(struct wl_resource *resource)
1551 struct weston_region *region =
1552 container_of(resource, struct weston_region, resource);
1554 pixman_region32_fini(®ion->region);
1559 region_destroy(struct wl_client *client, struct wl_resource *resource)
1561 wl_resource_destroy(resource);
1565 region_add(struct wl_client *client, struct wl_resource *resource,
1566 int32_t x, int32_t y, int32_t width, int32_t height)
1568 struct weston_region *region = resource->data;
1570 pixman_region32_union_rect(®ion->region, ®ion->region,
1571 x, y, width, height);
1575 region_subtract(struct wl_client *client, struct wl_resource *resource,
1576 int32_t x, int32_t y, int32_t width, int32_t height)
1578 struct weston_region *region = resource->data;
1579 pixman_region32_t rect;
1581 pixman_region32_init_rect(&rect, x, y, width, height);
1582 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
1583 pixman_region32_fini(&rect);
1586 static const struct wl_region_interface region_interface = {
1593 compositor_create_region(struct wl_client *client,
1594 struct wl_resource *resource, uint32_t id)
1596 struct weston_region *region;
1598 region = malloc(sizeof *region);
1599 if (region == NULL) {
1600 wl_resource_post_no_memory(resource);
1604 region->resource.destroy = destroy_region;
1606 region->resource.object.id = id;
1607 region->resource.object.interface = &wl_region_interface;
1608 region->resource.object.implementation =
1609 (void (**)(void)) ®ion_interface;
1610 region->resource.data = region;
1612 pixman_region32_init(®ion->region);
1614 wl_client_add_resource(client, ®ion->resource);
1617 static const struct wl_compositor_interface compositor_interface = {
1618 compositor_create_surface,
1619 compositor_create_region
1623 weston_compositor_dpms(struct weston_compositor *compositor,
1624 enum dpms_enum state)
1626 struct weston_output *output;
1628 wl_list_for_each(output, &compositor->output_list, link)
1629 if (output->set_dpms)
1630 output->set_dpms(output, state);
1634 weston_compositor_wake(struct weston_compositor *compositor)
1636 switch (compositor->state) {
1637 case WESTON_COMPOSITOR_SLEEPING:
1638 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
1640 case WESTON_COMPOSITOR_IDLE:
1641 case WESTON_COMPOSITOR_OFFSCREEN:
1642 wl_signal_emit(&compositor->wake_signal, compositor);
1645 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1646 wl_event_source_timer_update(compositor->idle_source,
1647 compositor->idle_time * 1000);
1652 weston_compositor_offscreen(struct weston_compositor *compositor)
1654 switch (compositor->state) {
1655 case WESTON_COMPOSITOR_OFFSCREEN:
1657 case WESTON_COMPOSITOR_SLEEPING:
1658 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
1661 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
1662 wl_event_source_timer_update(compositor->idle_source, 0);
1667 weston_compositor_sleep(struct weston_compositor *compositor)
1669 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1672 wl_event_source_timer_update(compositor->idle_source, 0);
1673 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1674 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
1678 idle_handler(void *data)
1680 struct weston_compositor *compositor = data;
1682 if (compositor->idle_inhibit)
1685 compositor->state = WESTON_COMPOSITOR_IDLE;
1686 wl_signal_emit(&compositor->idle_signal, compositor);
1692 weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1694 pixman_region32_init(&plane->damage);
1695 pixman_region32_init(&plane->clip);
1701 weston_plane_release(struct weston_plane *plane)
1703 pixman_region32_fini(&plane->damage);
1704 pixman_region32_fini(&plane->clip);
1708 weston_compositor_stack_plane(struct weston_compositor *ec,
1709 struct weston_plane *plane,
1710 struct weston_plane *above)
1713 wl_list_insert(above->link.prev, &plane->link);
1715 wl_list_insert(&ec->plane_list, &plane->link);
1718 static void unbind_resource(struct wl_resource *resource)
1720 wl_list_remove(&resource->link);
1725 bind_output(struct wl_client *client,
1726 void *data, uint32_t version, uint32_t id)
1728 struct weston_output *output = data;
1729 struct weston_mode *mode;
1730 struct wl_resource *resource;
1732 resource = wl_client_add_object(client,
1733 &wl_output_interface, NULL, id, data);
1735 wl_list_insert(&output->resource_list, &resource->link);
1736 resource->destroy = unbind_resource;
1738 wl_output_send_geometry(resource,
1744 output->make, output->model,
1747 wl_list_for_each (mode, &output->mode_list, link) {
1748 wl_output_send_mode(resource,
1757 weston_output_destroy(struct weston_output *output)
1759 struct weston_compositor *c = output->compositor;
1761 wl_signal_emit(&output->destroy_signal, output);
1764 pixman_region32_fini(&output->region);
1765 pixman_region32_fini(&output->previous_damage);
1766 output->compositor->output_id_pool &= ~(1 << output->id);
1768 wl_display_remove_global(c->wl_display, output->global);
1772 weston_output_compute_transform(struct weston_output *output)
1774 struct weston_matrix transform;
1777 weston_matrix_init(&transform);
1778 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
1780 switch(output->transform) {
1781 case WL_OUTPUT_TRANSFORM_FLIPPED:
1782 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1783 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1784 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1785 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
1793 switch(output->transform) {
1794 case WL_OUTPUT_TRANSFORM_NORMAL:
1795 case WL_OUTPUT_TRANSFORM_FLIPPED:
1796 transform.d[0] = flip;
1801 case WL_OUTPUT_TRANSFORM_90:
1802 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1804 transform.d[1] = -flip;
1808 case WL_OUTPUT_TRANSFORM_180:
1809 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1810 transform.d[0] = -flip;
1813 transform.d[5] = -1;
1815 case WL_OUTPUT_TRANSFORM_270:
1816 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1818 transform.d[1] = flip;
1819 transform.d[4] = -1;
1826 weston_matrix_multiply(&output->matrix, &transform);
1830 weston_output_update_matrix(struct weston_output *output)
1832 float magnification;
1833 struct weston_matrix camera;
1834 struct weston_matrix modelview;
1836 weston_matrix_init(&output->matrix);
1837 weston_matrix_translate(&output->matrix,
1838 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
1839 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
1841 weston_matrix_scale(&output->matrix,
1842 2.0 / (output->width + output->border.left + output->border.right),
1843 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
1845 weston_output_compute_transform(output);
1847 if (output->zoom.active) {
1848 magnification = 1 / (1 - output->zoom.spring_z.current);
1849 weston_matrix_init(&camera);
1850 weston_matrix_init(&modelview);
1851 weston_output_update_zoom(output, output->zoom.type);
1852 weston_matrix_translate(&camera, output->zoom.trans_x,
1853 -output->zoom.trans_y, 0);
1854 weston_matrix_invert(&modelview, &camera);
1855 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
1856 weston_matrix_multiply(&output->matrix, &modelview);
1863 weston_output_transform_init(struct weston_output *output, uint32_t transform)
1865 output->transform = transform;
1867 switch (transform) {
1868 case WL_OUTPUT_TRANSFORM_90:
1869 case WL_OUTPUT_TRANSFORM_270:
1870 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1871 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1872 /* Swap width and height */
1873 output->width = output->current->height;
1874 output->height = output->current->width;
1876 case WL_OUTPUT_TRANSFORM_NORMAL:
1877 case WL_OUTPUT_TRANSFORM_180:
1878 case WL_OUTPUT_TRANSFORM_FLIPPED:
1879 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1880 output->width = output->current->width;
1881 output->height = output->current->height;
1889 weston_output_move(struct weston_output *output, int x, int y)
1894 pixman_region32_init(&output->previous_damage);
1895 pixman_region32_init_rect(&output->region, x, y,
1901 weston_output_init(struct weston_output *output, struct weston_compositor *c,
1902 int x, int y, int width, int height, uint32_t transform)
1904 output->compositor = c;
1907 output->border.top = 0;
1908 output->border.bottom = 0;
1909 output->border.left = 0;
1910 output->border.right = 0;
1911 output->mm_width = width;
1912 output->mm_height = height;
1915 weston_output_transform_init(output, transform);
1916 weston_output_init_zoom(output);
1918 weston_output_move(output, x, y);
1919 weston_output_damage(output);
1921 wl_signal_init(&output->frame_signal);
1922 wl_signal_init(&output->destroy_signal);
1923 wl_list_init(&output->animation_list);
1924 wl_list_init(&output->resource_list);
1926 output->id = ffs(~output->compositor->output_id_pool) - 1;
1927 output->compositor->output_id_pool |= 1 << output->id;
1930 wl_display_add_global(c->wl_display, &wl_output_interface,
1931 output, bind_output);
1932 wl_signal_emit(&c->output_created_signal, output);
1936 compositor_bind(struct wl_client *client,
1937 void *data, uint32_t version, uint32_t id)
1939 struct weston_compositor *compositor = data;
1941 wl_client_add_object(client, &wl_compositor_interface,
1942 &compositor_interface, id, compositor);
1948 struct utsname usys;
1952 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
1953 usys.version, usys.machine);
1957 weston_environment_get_fd(const char *env)
1965 fd = strtol(e, &end, 0);
1969 flags = fcntl(fd, F_GETFD);
1973 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
1980 weston_compositor_init(struct weston_compositor *ec,
1981 struct wl_display *display,
1982 int *argc, char *argv[],
1983 const char *config_file)
1985 struct wl_event_loop *loop;
1986 struct xkb_rule_names xkb_names;
1987 const struct config_key keyboard_config_keys[] = {
1988 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
1989 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
1990 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
1991 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
1992 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
1994 const struct config_section cs[] = {
1996 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
1999 memset(&xkb_names, 0, sizeof(xkb_names));
2000 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
2002 ec->wl_display = display;
2003 wl_signal_init(&ec->destroy_signal);
2004 wl_signal_init(&ec->activate_signal);
2005 wl_signal_init(&ec->kill_signal);
2006 wl_signal_init(&ec->idle_signal);
2007 wl_signal_init(&ec->wake_signal);
2008 wl_signal_init(&ec->show_input_panel_signal);
2009 wl_signal_init(&ec->hide_input_panel_signal);
2010 wl_signal_init(&ec->update_input_panel_signal);
2011 wl_signal_init(&ec->seat_created_signal);
2012 wl_signal_init(&ec->output_created_signal);
2013 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
2015 ec->output_id_pool = 0;
2017 if (!wl_display_add_global(display, &wl_compositor_interface,
2018 ec, compositor_bind))
2021 wl_list_init(&ec->surface_list);
2022 wl_list_init(&ec->plane_list);
2023 wl_list_init(&ec->layer_list);
2024 wl_list_init(&ec->seat_list);
2025 wl_list_init(&ec->output_list);
2026 wl_list_init(&ec->key_binding_list);
2027 wl_list_init(&ec->button_binding_list);
2028 wl_list_init(&ec->axis_binding_list);
2029 wl_list_init(&ec->debug_binding_list);
2031 weston_plane_init(&ec->primary_plane, 0, 0);
2032 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
2034 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2037 ec->ping_handler = NULL;
2039 screenshooter_create(ec);
2040 text_cursor_position_notifier_create(ec);
2041 text_backend_init(ec);
2043 wl_data_device_manager_init(ec->wl_display);
2045 wl_display_init_shm(display);
2047 loop = wl_display_get_event_loop(ec->wl_display);
2048 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2049 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2051 ec->input_loop = wl_event_loop_create();
2053 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2054 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2056 weston_compositor_schedule_repaint(ec);
2062 weston_compositor_shutdown(struct weston_compositor *ec)
2064 struct weston_output *output, *next;
2066 wl_event_source_remove(ec->idle_source);
2067 if (ec->input_loop_source)
2068 wl_event_source_remove(ec->input_loop_source);
2070 /* Destroy all outputs associated with this compositor */
2071 wl_list_for_each_safe(output, next, &ec->output_list, link)
2072 output->destroy(output);
2074 weston_binding_list_destroy_all(&ec->key_binding_list);
2075 weston_binding_list_destroy_all(&ec->button_binding_list);
2076 weston_binding_list_destroy_all(&ec->axis_binding_list);
2077 weston_binding_list_destroy_all(&ec->debug_binding_list);
2079 weston_plane_release(&ec->primary_plane);
2081 wl_array_release(&ec->vertices);
2082 wl_array_release(&ec->indices);
2083 wl_array_release(&ec->vtxcnt);
2085 wl_event_loop_destroy(ec->input_loop);
2089 weston_version(int *major, int *minor, int *micro)
2091 *major = WESTON_VERSION_MAJOR;
2092 *minor = WESTON_VERSION_MINOR;
2093 *micro = WESTON_VERSION_MICRO;
2096 static int on_term_signal(int signal_number, void *data)
2098 struct wl_display *display = data;
2100 weston_log("caught signal %d\n", signal_number);
2101 wl_display_terminate(display);
2106 #ifdef HAVE_LIBUNWIND
2109 print_backtrace(void)
2111 unw_cursor_t cursor;
2112 unw_context_t context;
2114 unw_proc_info_t pip;
2117 const char *filename;
2120 pip.unwind_info = NULL;
2121 ret = unw_getcontext(&context);
2123 weston_log("unw_getcontext: %d\n", ret);
2127 ret = unw_init_local(&cursor, &context);
2129 weston_log("unw_init_local: %d\n", ret);
2133 ret = unw_step(&cursor);
2135 ret = unw_get_proc_info(&cursor, &pip);
2137 weston_log("unw_get_proc_info: %d\n", ret);
2141 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2142 if (ret && ret != -UNW_ENOMEM) {
2143 if (ret != -UNW_EUNSPEC)
2144 weston_log("unw_get_proc_name: %d\n", ret);
2149 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2151 filename = dlinfo.dli_fname;
2155 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2156 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2158 ret = unw_step(&cursor);
2160 weston_log("unw_step: %d\n", ret);
2167 print_backtrace(void)
2173 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2174 for (i = 0; i < count; i++) {
2175 dladdr(buffer[i], &info);
2176 weston_log(" [%016lx] %s (%s)\n",
2178 info.dli_sname ? info.dli_sname : "--",
2186 on_caught_signal(int s, siginfo_t *siginfo, void *context)
2188 /* This signal handler will do a best-effort backtrace, and
2189 * then call the backend restore function, which will switch
2190 * back to the vt we launched from or ungrab X etc and then
2191 * raise SIGTRAP. If we run weston under gdb from X or a
2192 * different vt, and tell gdb "handle *s* nostop", this
2193 * will allow weston to switch back to gdb on crash and then
2194 * gdb will catch the crash with SIGTRAP.*/
2196 weston_log("caught signal: %d\n", s);
2200 segv_compositor->restore(segv_compositor);
2206 load_module(const char *name, const char *entrypoint)
2208 char path[PATH_MAX];
2209 void *module, *init;
2212 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
2214 snprintf(path, sizeof path, "%s", name);
2216 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2218 weston_log("Module '%s' already loaded\n", path);
2223 weston_log("Loading module '%s'\n", path);
2224 module = dlopen(path, RTLD_NOW);
2226 weston_log("Failed to load module: %s\n", dlerror());
2230 init = dlsym(module, entrypoint);
2232 weston_log("Failed to lookup init function: %s\n", dlerror());
2241 load_modules(struct weston_compositor *ec, const char *modules,
2242 int *argc, char *argv[], const char *config_file)
2244 const char *p, *end;
2246 int (*module_init)(struct weston_compositor *ec,
2247 int *argc, char *argv[], const char *config_file);
2249 if (modules == NULL)
2254 end = strchrnul(p, ',');
2255 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2256 module_init = load_module(buffer, "module_init");
2258 module_init(ec, argc, argv, config_file);
2268 static const char xdg_error_message[] =
2269 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2271 static const char xdg_wrong_message[] =
2272 "fatal: environment variable XDG_RUNTIME_DIR\n"
2273 "is set to \"%s\", which is not a directory.\n";
2275 static const char xdg_wrong_mode_message[] =
2276 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2277 "correctly. Unix access mode must be 0700 but is %o,\n"
2278 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
2279 "owned by UID %d.\n";
2281 static const char xdg_detail_message[] =
2282 "Refer to your distribution on how to get it, or\n"
2283 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2284 "on how to implement it.\n";
2287 verify_xdg_runtime_dir(void)
2289 char *dir = getenv("XDG_RUNTIME_DIR");
2293 weston_log(xdg_error_message);
2294 weston_log_continue(xdg_detail_message);
2298 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2299 weston_log(xdg_wrong_message, dir);
2300 weston_log_continue(xdg_detail_message);
2304 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2305 weston_log(xdg_wrong_mode_message,
2306 dir, s.st_mode & 0777, s.st_uid);
2307 weston_log_continue(xdg_detail_message);
2312 usage(int error_code)
2315 "Usage: weston [OPTIONS]\n\n"
2316 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2317 "Weston supports multiple backends, and depending on which backend is in use\n"
2318 "different options will be accepted.\n\n"
2322 " --version\t\tPrint weston version\n"
2323 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
2324 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
2325 "\t\t\t\twayland-backend.so\n"
2326 " -S, --socket=NAME\tName of socket to listen on\n"
2327 " -i, --idle-time=SECS\tIdle time in seconds\n"
2328 " --modules\t\tLoad the comma-separated list of modules\n"
2329 " --log==FILE\t\tLog to the given file\n"
2330 " -h, --help\t\tThis help message\n\n");
2333 "Options for drm-backend.so:\n\n"
2334 " --connector=ID\tBring up only this connector\n"
2335 " --seat=SEAT\t\tThe seat that weston should run on\n"
2336 " --tty=TTY\t\tThe tty to use\n"
2337 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
2338 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
2341 "Options for fbdev-backend.so:\n\n"
2342 " --tty=TTY\t\tThe tty to use\n"
2343 " --device=DEVICE\tThe framebuffer device to use\n\n");
2346 "Options for x11-backend.so:\n\n"
2347 " --width=WIDTH\t\tWidth of X window\n"
2348 " --height=HEIGHT\tHeight of X window\n"
2349 " --fullscreen\t\tRun in fullscreen mode\n"
2350 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
2351 " --output-count=COUNT\tCreate multiple outputs\n"
2352 " --no-input\t\tDont create input devices\n\n");
2355 "Options for wayland-backend.so:\n\n"
2356 " --width=WIDTH\t\tWidth of Wayland surface\n"
2357 " --height=HEIGHT\tHeight of Wayland surface\n"
2358 " --display=DISPLAY\tWayland display to connect to\n\n");
2366 struct sigaction action;
2368 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
2369 action.sa_sigaction = on_caught_signal;
2370 sigemptyset(&action.sa_mask);
2371 sigaction(SIGSEGV, &action, NULL);
2372 sigaction(SIGABRT, &action, NULL);
2375 int main(int argc, char *argv[])
2377 int ret = EXIT_SUCCESS;
2378 struct wl_display *display;
2379 struct weston_compositor *ec;
2380 struct wl_event_source *signals[4];
2381 struct wl_event_loop *loop;
2382 struct weston_compositor
2383 *(*backend_init)(struct wl_display *display,
2384 int *argc, char *argv[], const char *config_file);
2386 char *backend = NULL;
2387 const char *modules = "desktop-shell.so", *option_modules = NULL;
2389 int32_t idle_time = 300;
2391 char *socket_name = "wayland-0";
2392 int32_t version = 0;
2395 const struct config_key core_config_keys[] = {
2396 { "modules", CONFIG_KEY_STRING, &modules },
2399 const struct config_section cs[] = {
2401 core_config_keys, ARRAY_LENGTH(core_config_keys) },
2404 const struct weston_option core_options[] = {
2405 { WESTON_OPTION_STRING, "backend", 'B', &backend },
2406 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
2407 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
2408 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
2409 { WESTON_OPTION_STRING, "log", 0, &log },
2410 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
2411 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
2414 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
2417 usage(EXIT_SUCCESS);
2420 printf(PACKAGE_STRING "\n");
2421 return EXIT_SUCCESS;
2424 weston_log_file_open(log);
2428 STAMP_SPACE "Bug reports to: %s\n"
2429 STAMP_SPACE "Build: %s\n",
2430 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
2434 verify_xdg_runtime_dir();
2436 display = wl_display_create();
2438 loop = wl_display_get_event_loop(display);
2439 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2441 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2443 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2446 wl_list_init(&child_process_list);
2447 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2451 if (getenv("WAYLAND_DISPLAY"))
2452 backend = "wayland-backend.so";
2453 else if (getenv("DISPLAY"))
2454 backend = "x11-backend.so";
2456 backend = WESTON_NATIVE_BACKEND;
2459 config_file = config_file_path("weston.ini");
2460 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
2462 backend_init = load_module(backend, "backend_init");
2466 ec = backend_init(display, &argc, argv, config_file);
2468 weston_log("fatal: failed to create compositor\n");
2473 segv_compositor = ec;
2475 ec->idle_time = idle_time;
2477 setenv("WAYLAND_DISPLAY", socket_name, 1);
2479 if (load_modules(ec, modules, &argc, argv, config_file) < 0)
2481 if (load_modules(ec, option_modules, &argc, argv, config_file) < 0)
2486 for (i = 1; i < argc; i++)
2487 weston_log("fatal: unhandled option: %s\n", argv[i]);
2493 if (wl_display_add_socket(display, socket_name)) {
2494 weston_log("fatal: failed to add socket: %m\n");
2499 weston_compositor_wake(ec);
2501 wl_display_run(display);
2504 /* prevent further rendering while shutting down */
2505 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
2507 wl_signal_emit(&ec->destroy_signal, ec);
2509 for (i = ARRAY_LENGTH(signals); i;)
2510 wl_event_source_remove(signals[--i]);
2512 weston_compositor_xkb_destroy(ec);
2515 wl_display_destroy(display);
2517 weston_log_file_close();