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.
35 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <sys/utsname.h>
43 #include <linux/input.h>
52 #define UNW_LOCAL_ONLY
53 #include <libunwind.h>
56 #include "compositor.h"
57 #include "scaler-server-protocol.h"
58 #include "../shared/os-compatibility.h"
59 #include "../shared/str-util.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 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
74 wl_list_for_each(p, &child_process_list, link) {
79 if (&p->link == &child_process_list) {
80 weston_log("unknown child process exited\n");
84 wl_list_remove(&p->link);
85 p->cleanup(p, status);
88 if (pid < 0 && errno != ECHILD)
89 weston_log("waitpid error %m\n");
95 weston_output_transform_scale_init(struct weston_output *output,
96 uint32_t transform, uint32_t scale);
99 weston_compositor_build_view_list(struct weston_compositor *compositor);
102 weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
103 int32_t scale, enum weston_mode_switch_op op)
105 struct weston_seat *seat;
106 struct wl_resource *resource;
107 pixman_region32_t old_output_region;
108 int ret, notify_mode_changed, notify_scale_changed;
109 int temporary_mode, temporary_scale;
111 if (!output->switch_mode)
114 temporary_mode = (output->original_mode != 0);
115 temporary_scale = (output->current_scale != output->original_scale);
118 notify_mode_changed = 0;
119 notify_scale_changed = 0;
121 case WESTON_MODE_SWITCH_SET_NATIVE:
122 output->native_mode = mode;
123 if (!temporary_mode) {
124 notify_mode_changed = 1;
125 ret = output->switch_mode(output, mode);
130 output->native_scale = scale;
132 notify_scale_changed = 1;
134 case WESTON_MODE_SWITCH_SET_TEMPORARY:
136 output->original_mode = output->native_mode;
137 if (!temporary_scale)
138 output->original_scale = output->native_scale;
140 ret = output->switch_mode(output, mode);
144 output->current_scale = scale;
146 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
147 if (!temporary_mode) {
148 weston_log("already in the native mode\n");
152 notify_mode_changed = (output->original_mode != output->native_mode);
154 ret = output->switch_mode(output, mode);
158 if (output->original_scale != output->native_scale) {
159 notify_scale_changed = 1;
160 scale = output->native_scale;
161 output->original_scale = scale;
163 output->original_mode = 0;
165 output->current_scale = output->native_scale;
168 weston_log("unknown weston_switch_mode_op %d\n", op);
172 pixman_region32_init(&old_output_region);
173 pixman_region32_copy(&old_output_region, &output->region);
175 /* Update output region and transformation matrix */
176 weston_output_transform_scale_init(output, output->transform, output->current_scale);
178 pixman_region32_init(&output->previous_damage);
179 pixman_region32_init_rect(&output->region, output->x, output->y,
180 output->width, output->height);
182 weston_output_update_matrix(output);
184 /* If a pointer falls outside the outputs new geometry, move it to its
185 * lower-right corner */
186 wl_list_for_each(seat, &output->compositor->seat_list, link) {
187 struct weston_pointer *pointer = seat->pointer;
193 x = wl_fixed_to_int(pointer->x);
194 y = wl_fixed_to_int(pointer->y);
196 if (!pixman_region32_contains_point(&old_output_region,
198 pixman_region32_contains_point(&output->region,
202 if (x >= output->x + output->width)
203 x = output->x + output->width - 1;
204 if (y >= output->y + output->height)
205 y = output->y + output->height - 1;
207 pointer->x = wl_fixed_from_int(x);
208 pointer->y = wl_fixed_from_int(y);
211 pixman_region32_fini(&old_output_region);
213 /* notify clients of the changes */
214 if (notify_mode_changed || notify_scale_changed) {
215 wl_resource_for_each(resource, &output->resource_list) {
216 if(notify_mode_changed) {
217 wl_output_send_mode(resource,
218 mode->flags | WL_OUTPUT_MODE_CURRENT,
224 if (notify_scale_changed)
225 wl_output_send_scale(resource, scale);
227 if (wl_resource_get_version(resource) >= 2)
228 wl_output_send_done(resource);
236 weston_watch_process(struct weston_process *process)
238 wl_list_insert(&child_process_list, &process->link);
242 child_client_exec(int sockfd, const char *path)
248 /* do not give our signal mask to the new process */
249 sigfillset(&allsigs);
250 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
252 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
253 if (seteuid(getuid()) == -1) {
254 weston_log("compositor: failed seteuid\n");
258 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
259 * non-CLOEXEC fd to pass through exec. */
260 clientfd = dup(sockfd);
261 if (clientfd == -1) {
262 weston_log("compositor: dup failed: %m\n");
266 snprintf(s, sizeof s, "%d", clientfd);
267 setenv("WAYLAND_SOCKET", s, 1);
269 if (execl(path, path, NULL) < 0)
270 weston_log("compositor: executing '%s' failed: %m\n",
274 WL_EXPORT struct wl_client *
275 weston_client_launch(struct weston_compositor *compositor,
276 struct weston_process *proc,
278 weston_process_cleanup_func_t cleanup)
282 struct wl_client *client;
284 weston_log("launching '%s'\n", path);
286 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
287 weston_log("weston_client_launch: "
288 "socketpair failed while launching '%s': %m\n",
297 weston_log("weston_client_launch: "
298 "fork failed while launching '%s': %m\n", path);
303 child_client_exec(sv[1], path);
309 client = wl_client_create(compositor->wl_display, sv[0]);
312 weston_log("weston_client_launch: "
313 "wl_client_create failed while launching '%s'.\n",
319 proc->cleanup = cleanup;
320 weston_watch_process(proc);
325 struct process_info {
326 struct weston_process proc;
331 process_handle_sigchld(struct weston_process *process, int status)
333 struct process_info *pinfo =
334 container_of(process, struct process_info, proc);
337 * There are no guarantees whether this runs before or after
338 * the wl_client destructor.
341 if (WIFEXITED(status)) {
342 weston_log("%s exited with status %d\n", pinfo->path,
343 WEXITSTATUS(status));
344 } else if (WIFSIGNALED(status)) {
345 weston_log("%s died on signal %d\n", pinfo->path,
348 weston_log("%s disappeared\n", pinfo->path);
355 WL_EXPORT struct wl_client *
356 weston_client_start(struct weston_compositor *compositor, const char *path)
358 struct process_info *pinfo;
359 struct wl_client *client;
361 pinfo = zalloc(sizeof *pinfo);
365 pinfo->path = strdup(path);
369 client = weston_client_launch(compositor, &pinfo->proc, path,
370 process_handle_sigchld);
386 region_init_infinite(pixman_region32_t *region)
388 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
389 UINT32_MAX, UINT32_MAX);
392 static struct weston_subsurface *
393 weston_surface_to_subsurface(struct weston_surface *surface);
395 WL_EXPORT struct weston_view *
396 weston_view_create(struct weston_surface *surface)
398 struct weston_view *view;
400 view = calloc(1, sizeof *view);
404 view->surface = surface;
406 /* Assign to surface */
407 wl_list_insert(&surface->views, &view->surface_link);
409 wl_signal_init(&view->destroy_signal);
410 wl_list_init(&view->link);
411 wl_list_init(&view->layer_link.link);
414 view->layer_link.layer = NULL;
415 view->parent_view = NULL;
417 pixman_region32_init(&view->clip);
418 pixman_region32_init(&view->transform.masked_boundingbox);
419 pixman_region32_init(&view->transform.masked_opaque);
422 pixman_region32_init(&view->transform.opaque);
424 wl_list_init(&view->geometry.transformation_list);
425 wl_list_insert(&view->geometry.transformation_list,
426 &view->transform.position.link);
427 weston_matrix_init(&view->transform.position.matrix);
428 wl_list_init(&view->geometry.child_list);
429 pixman_region32_init(&view->transform.boundingbox);
430 view->transform.dirty = 1;
437 struct weston_frame_callback {
438 struct wl_resource *resource;
443 surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
445 struct weston_surface_state *state =
446 container_of(listener, struct weston_surface_state,
447 buffer_destroy_listener);
449 state->buffer = NULL;
453 weston_surface_state_init(struct weston_surface_state *state)
455 state->newly_attached = 0;
456 state->buffer = NULL;
457 state->buffer_destroy_listener.notify =
458 surface_state_handle_buffer_destroy;
462 pixman_region32_init(&state->damage);
463 pixman_region32_init(&state->opaque);
464 region_init_infinite(&state->input);
466 wl_list_init(&state->frame_callback_list);
468 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
469 state->buffer_viewport.buffer.scale = 1;
470 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
471 state->buffer_viewport.surface.width = -1;
472 state->buffer_viewport.changed = 0;
476 weston_surface_state_fini(struct weston_surface_state *state)
478 struct weston_frame_callback *cb, *next;
480 wl_list_for_each_safe(cb, next,
481 &state->frame_callback_list, link)
482 wl_resource_destroy(cb->resource);
484 pixman_region32_fini(&state->input);
485 pixman_region32_fini(&state->opaque);
486 pixman_region32_fini(&state->damage);
489 wl_list_remove(&state->buffer_destroy_listener.link);
490 state->buffer = NULL;
494 weston_surface_state_set_buffer(struct weston_surface_state *state,
495 struct weston_buffer *buffer)
497 if (state->buffer == buffer)
501 wl_list_remove(&state->buffer_destroy_listener.link);
502 state->buffer = buffer;
504 wl_signal_add(&state->buffer->destroy_signal,
505 &state->buffer_destroy_listener);
508 WL_EXPORT struct weston_surface *
509 weston_surface_create(struct weston_compositor *compositor)
511 struct weston_surface *surface;
513 surface = calloc(1, sizeof *surface);
517 wl_signal_init(&surface->destroy_signal);
519 surface->resource = NULL;
521 surface->compositor = compositor;
522 surface->ref_count = 1;
524 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
525 surface->buffer_viewport.buffer.scale = 1;
526 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
527 surface->buffer_viewport.surface.width = -1;
529 weston_surface_state_init(&surface->pending);
531 surface->output = NULL;
533 surface->viewport_resource = NULL;
535 pixman_region32_init(&surface->damage);
536 pixman_region32_init(&surface->opaque);
537 region_init_infinite(&surface->input);
539 wl_list_init(&surface->views);
541 wl_list_init(&surface->frame_callback_list);
543 wl_list_init(&surface->subsurface_list);
544 wl_list_init(&surface->subsurface_list_pending);
550 weston_surface_set_color(struct weston_surface *surface,
551 float red, float green, float blue, float alpha)
553 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
557 weston_view_to_global_float(struct weston_view *view,
558 float sx, float sy, float *x, float *y)
560 if (view->transform.enabled) {
561 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
563 weston_matrix_transform(&view->transform.matrix, &v);
565 if (fabsf(v.f[3]) < 1e-6) {
566 weston_log("warning: numerical instability in "
567 "%s(), divisor = %g\n", __func__,
574 *x = v.f[0] / v.f[3];
575 *y = v.f[1] / v.f[3];
577 *x = sx + view->geometry.x;
578 *y = sy + view->geometry.y;
583 weston_transformed_coord(int width, int height,
584 enum wl_output_transform transform,
586 float sx, float sy, float *bx, float *by)
589 case WL_OUTPUT_TRANSFORM_NORMAL:
594 case WL_OUTPUT_TRANSFORM_FLIPPED:
598 case WL_OUTPUT_TRANSFORM_90:
602 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
606 case WL_OUTPUT_TRANSFORM_180:
610 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
614 case WL_OUTPUT_TRANSFORM_270:
618 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
628 WL_EXPORT pixman_box32_t
629 weston_transformed_rect(int width, int height,
630 enum wl_output_transform transform,
634 float x1, x2, y1, y2;
638 weston_transformed_coord(width, height, transform, scale,
639 rect.x1, rect.y1, &x1, &y1);
640 weston_transformed_coord(width, height, transform, scale,
641 rect.x2, rect.y2, &x2, &y2);
663 weston_transformed_region(int width, int height,
664 enum wl_output_transform transform,
666 pixman_region32_t *src, pixman_region32_t *dest)
668 pixman_box32_t *src_rects, *dest_rects;
671 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
673 pixman_region32_copy(dest, src);
677 src_rects = pixman_region32_rectangles(src, &nrects);
678 dest_rects = malloc(nrects * sizeof(*dest_rects));
682 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
683 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
685 for (i = 0; i < nrects; i++) {
688 case WL_OUTPUT_TRANSFORM_NORMAL:
689 dest_rects[i].x1 = src_rects[i].x1;
690 dest_rects[i].y1 = src_rects[i].y1;
691 dest_rects[i].x2 = src_rects[i].x2;
692 dest_rects[i].y2 = src_rects[i].y2;
694 case WL_OUTPUT_TRANSFORM_90:
695 dest_rects[i].x1 = height - src_rects[i].y2;
696 dest_rects[i].y1 = src_rects[i].x1;
697 dest_rects[i].x2 = height - src_rects[i].y1;
698 dest_rects[i].y2 = src_rects[i].x2;
700 case WL_OUTPUT_TRANSFORM_180:
701 dest_rects[i].x1 = width - src_rects[i].x2;
702 dest_rects[i].y1 = height - src_rects[i].y2;
703 dest_rects[i].x2 = width - src_rects[i].x1;
704 dest_rects[i].y2 = height - src_rects[i].y1;
706 case WL_OUTPUT_TRANSFORM_270:
707 dest_rects[i].x1 = src_rects[i].y1;
708 dest_rects[i].y1 = width - src_rects[i].x2;
709 dest_rects[i].x2 = src_rects[i].y2;
710 dest_rects[i].y2 = width - src_rects[i].x1;
712 case WL_OUTPUT_TRANSFORM_FLIPPED:
713 dest_rects[i].x1 = width - src_rects[i].x2;
714 dest_rects[i].y1 = src_rects[i].y1;
715 dest_rects[i].x2 = width - src_rects[i].x1;
716 dest_rects[i].y2 = src_rects[i].y2;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
719 dest_rects[i].x1 = height - src_rects[i].y2;
720 dest_rects[i].y1 = width - src_rects[i].x2;
721 dest_rects[i].x2 = height - src_rects[i].y1;
722 dest_rects[i].y2 = width - src_rects[i].x1;
724 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
725 dest_rects[i].x1 = src_rects[i].x1;
726 dest_rects[i].y1 = height - src_rects[i].y2;
727 dest_rects[i].x2 = src_rects[i].x2;
728 dest_rects[i].y2 = height - src_rects[i].y1;
730 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
731 dest_rects[i].x1 = src_rects[i].y1;
732 dest_rects[i].y1 = src_rects[i].x1;
733 dest_rects[i].x2 = src_rects[i].y2;
734 dest_rects[i].y2 = src_rects[i].x2;
741 for (i = 0; i < nrects; i++) {
742 dest_rects[i].x1 *= scale;
743 dest_rects[i].x2 *= scale;
744 dest_rects[i].y1 *= scale;
745 dest_rects[i].y2 *= scale;
749 pixman_region32_clear(dest);
750 pixman_region32_init_rects(dest, dest_rects, nrects);
755 scaler_surface_to_buffer(struct weston_surface *surface,
756 float sx, float sy, float *bx, float *by)
758 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
759 double src_width, src_height;
762 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
763 if (vp->surface.width == -1) {
771 src_width = surface->width_from_buffer;
772 src_height = surface->height_from_buffer;
774 src_x = wl_fixed_to_double(vp->buffer.src_x);
775 src_y = wl_fixed_to_double(vp->buffer.src_y);
776 src_width = wl_fixed_to_double(vp->buffer.src_width);
777 src_height = wl_fixed_to_double(vp->buffer.src_height);
780 *bx = sx * src_width / surface->width + src_x;
781 *by = sy * src_height / surface->height + src_y;
785 weston_surface_to_buffer_float(struct weston_surface *surface,
786 float sx, float sy, float *bx, float *by)
788 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
790 /* first transform coordinates if the scaler is set */
791 scaler_surface_to_buffer(surface, sx, sy, bx, by);
793 weston_transformed_coord(surface->width_from_buffer,
794 surface->height_from_buffer,
795 vp->buffer.transform, vp->buffer.scale,
800 weston_surface_to_buffer(struct weston_surface *surface,
801 int sx, int sy, int *bx, int *by)
805 weston_surface_to_buffer_float(surface,
812 WL_EXPORT pixman_box32_t
813 weston_surface_to_buffer_rect(struct weston_surface *surface,
816 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
819 /* first transform box coordinates if the scaler is set */
820 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
821 rect.x1 = floorf(xf);
822 rect.y1 = floorf(yf);
824 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
825 rect.x2 = floorf(xf);
826 rect.y2 = floorf(yf);
828 return weston_transformed_rect(surface->width_from_buffer,
829 surface->height_from_buffer,
830 vp->buffer.transform, vp->buffer.scale,
835 weston_view_move_to_plane(struct weston_view *view,
836 struct weston_plane *plane)
838 if (view->plane == plane)
841 weston_view_damage_below(view);
843 weston_surface_damage(view->surface);
847 weston_view_damage_below(struct weston_view *view)
849 pixman_region32_t damage;
851 pixman_region32_init(&damage);
852 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
855 pixman_region32_union(&view->plane->damage,
856 &view->plane->damage, &damage);
857 pixman_region32_fini(&damage);
858 weston_view_schedule_repaint(view);
862 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
864 uint32_t different = es->output_mask ^ mask;
865 uint32_t entered = mask & different;
866 uint32_t left = es->output_mask & different;
867 struct weston_output *output;
868 struct wl_resource *resource = NULL;
869 struct wl_client *client;
871 es->output_mask = mask;
872 if (es->resource == NULL)
877 client = wl_resource_get_client(es->resource);
879 wl_list_for_each(output, &es->compositor->output_list, link) {
880 if (1 << output->id & different)
882 wl_resource_find_for_client(&output->resource_list,
884 if (resource == NULL)
886 if (1 << output->id & entered)
887 wl_surface_send_enter(es->resource, resource);
888 if (1 << output->id & left)
889 wl_surface_send_leave(es->resource, resource);
895 weston_surface_assign_output(struct weston_surface *es)
897 struct weston_output *new_output;
898 struct weston_view *view;
899 pixman_region32_t region;
900 uint32_t max, area, mask;
906 pixman_region32_init(®ion);
907 wl_list_for_each(view, &es->views, surface_link) {
911 pixman_region32_intersect(®ion, &view->transform.boundingbox,
912 &view->output->region);
914 e = pixman_region32_extents(®ion);
915 area = (e->x2 - e->x1) * (e->y2 - e->y1);
917 mask |= view->output_mask;
920 new_output = view->output;
924 pixman_region32_fini(®ion);
926 es->output = new_output;
927 weston_surface_update_output_mask(es, mask);
931 weston_view_assign_output(struct weston_view *ev)
933 struct weston_compositor *ec = ev->surface->compositor;
934 struct weston_output *output, *new_output;
935 pixman_region32_t region;
936 uint32_t max, area, mask;
942 pixman_region32_init(®ion);
943 wl_list_for_each(output, &ec->output_list, link) {
944 pixman_region32_intersect(®ion, &ev->transform.boundingbox,
947 e = pixman_region32_extents(®ion);
948 area = (e->x2 - e->x1) * (e->y2 - e->y1);
951 mask |= 1 << output->id;
958 pixman_region32_fini(®ion);
960 ev->output = new_output;
961 ev->output_mask = mask;
963 weston_surface_assign_output(ev->surface);
967 view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
968 int32_t width, int32_t height,
969 pixman_region32_t *bbox)
971 float min_x = HUGE_VALF, min_y = HUGE_VALF;
972 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
977 { sx + width, sy + height }
982 if (width == 0 || height == 0) {
983 /* avoid rounding empty bbox to 1x1 */
984 pixman_region32_init(bbox);
988 for (i = 0; i < 4; ++i) {
990 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
1001 int_x = floorf(min_x);
1002 int_y = floorf(min_y);
1003 pixman_region32_init_rect(bbox, int_x, int_y,
1004 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
1008 weston_view_update_transform_disable(struct weston_view *view)
1010 view->transform.enabled = 0;
1012 /* round off fractions when not transformed */
1013 view->geometry.x = roundf(view->geometry.x);
1014 view->geometry.y = roundf(view->geometry.y);
1016 /* Otherwise identity matrix, but with x and y translation. */
1017 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1018 view->transform.position.matrix.d[12] = view->geometry.x;
1019 view->transform.position.matrix.d[13] = view->geometry.y;
1021 view->transform.matrix = view->transform.position.matrix;
1023 view->transform.inverse = view->transform.position.matrix;
1024 view->transform.inverse.d[12] = -view->geometry.x;
1025 view->transform.inverse.d[13] = -view->geometry.y;
1027 pixman_region32_init_rect(&view->transform.boundingbox,
1030 view->surface->width,
1031 view->surface->height);
1033 if (view->alpha == 1.0) {
1034 pixman_region32_copy(&view->transform.opaque,
1035 &view->surface->opaque);
1036 pixman_region32_translate(&view->transform.opaque,
1043 weston_view_update_transform_enable(struct weston_view *view)
1045 struct weston_view *parent = view->geometry.parent;
1046 struct weston_matrix *matrix = &view->transform.matrix;
1047 struct weston_matrix *inverse = &view->transform.inverse;
1048 struct weston_transform *tform;
1050 view->transform.enabled = 1;
1052 /* Otherwise identity matrix, but with x and y translation. */
1053 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1054 view->transform.position.matrix.d[12] = view->geometry.x;
1055 view->transform.position.matrix.d[13] = view->geometry.y;
1057 weston_matrix_init(matrix);
1058 wl_list_for_each(tform, &view->geometry.transformation_list, link)
1059 weston_matrix_multiply(matrix, &tform->matrix);
1062 weston_matrix_multiply(matrix, &parent->transform.matrix);
1064 if (weston_matrix_invert(inverse, matrix) < 0) {
1065 /* Oops, bad total transformation, not invertible */
1066 weston_log("error: weston_view %p"
1067 " transformation not invertible.\n", view);
1071 view_compute_bbox(view, 0, 0,
1072 view->surface->width, view->surface->height,
1073 &view->transform.boundingbox);
1078 static struct weston_layer *
1079 get_view_layer(struct weston_view *view)
1081 if (view->parent_view)
1082 return get_view_layer(view->parent_view);
1083 return view->layer_link.layer;
1087 weston_view_update_transform(struct weston_view *view)
1089 struct weston_view *parent = view->geometry.parent;
1090 struct weston_layer *layer;
1091 pixman_region32_t mask;
1093 if (!view->transform.dirty)
1097 weston_view_update_transform(parent);
1099 view->transform.dirty = 0;
1101 weston_view_damage_below(view);
1103 pixman_region32_fini(&view->transform.boundingbox);
1104 pixman_region32_fini(&view->transform.opaque);
1105 pixman_region32_init(&view->transform.opaque);
1107 /* transform.position is always in transformation_list */
1108 if (view->geometry.transformation_list.next ==
1109 &view->transform.position.link &&
1110 view->geometry.transformation_list.prev ==
1111 &view->transform.position.link &&
1113 weston_view_update_transform_disable(view);
1115 if (weston_view_update_transform_enable(view) < 0)
1116 weston_view_update_transform_disable(view);
1119 layer = get_view_layer(view);
1121 pixman_region32_init_with_extents(&mask, &layer->mask);
1122 pixman_region32_intersect(&view->transform.masked_boundingbox,
1123 &view->transform.boundingbox, &mask);
1124 pixman_region32_intersect(&view->transform.masked_opaque,
1125 &view->transform.opaque, &mask);
1126 pixman_region32_fini(&mask);
1129 weston_view_damage_below(view);
1131 weston_view_assign_output(view);
1133 wl_signal_emit(&view->surface->compositor->transform_signal,
1138 weston_view_geometry_dirty(struct weston_view *view)
1140 struct weston_view *child;
1143 * The invariant: if view->geometry.dirty, then all views
1144 * in view->geometry.child_list have geometry.dirty too.
1145 * Corollary: if not parent->geometry.dirty, then all ancestors
1149 if (view->transform.dirty)
1152 view->transform.dirty = 1;
1154 wl_list_for_each(child, &view->geometry.child_list,
1155 geometry.parent_link)
1156 weston_view_geometry_dirty(child);
1160 weston_view_to_global_fixed(struct weston_view *view,
1161 wl_fixed_t vx, wl_fixed_t vy,
1162 wl_fixed_t *x, wl_fixed_t *y)
1166 weston_view_to_global_float(view,
1167 wl_fixed_to_double(vx),
1168 wl_fixed_to_double(vy),
1170 *x = wl_fixed_from_double(xf);
1171 *y = wl_fixed_from_double(yf);
1175 weston_view_from_global_float(struct weston_view *view,
1176 float x, float y, float *vx, float *vy)
1178 if (view->transform.enabled) {
1179 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1181 weston_matrix_transform(&view->transform.inverse, &v);
1183 if (fabsf(v.f[3]) < 1e-6) {
1184 weston_log("warning: numerical instability in "
1185 "weston_view_from_global(), divisor = %g\n",
1192 *vx = v.f[0] / v.f[3];
1193 *vy = v.f[1] / v.f[3];
1195 *vx = x - view->geometry.x;
1196 *vy = y - view->geometry.y;
1201 weston_view_from_global_fixed(struct weston_view *view,
1202 wl_fixed_t x, wl_fixed_t y,
1203 wl_fixed_t *vx, wl_fixed_t *vy)
1207 weston_view_from_global_float(view,
1208 wl_fixed_to_double(x),
1209 wl_fixed_to_double(y),
1211 *vx = wl_fixed_from_double(vxf);
1212 *vy = wl_fixed_from_double(vyf);
1216 weston_view_from_global(struct weston_view *view,
1217 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
1221 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1227 weston_surface_schedule_repaint(struct weston_surface *surface)
1229 struct weston_output *output;
1231 wl_list_for_each(output, &surface->compositor->output_list, link)
1232 if (surface->output_mask & (1 << output->id))
1233 weston_output_schedule_repaint(output);
1237 weston_view_schedule_repaint(struct weston_view *view)
1239 struct weston_output *output;
1241 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1242 if (view->output_mask & (1 << output->id))
1243 weston_output_schedule_repaint(output);
1247 weston_surface_damage(struct weston_surface *surface)
1249 pixman_region32_union_rect(&surface->damage, &surface->damage,
1250 0, 0, surface->width,
1253 weston_surface_schedule_repaint(surface);
1257 weston_view_set_position(struct weston_view *view, float x, float y)
1259 if (view->geometry.x == x && view->geometry.y == y)
1262 view->geometry.x = x;
1263 view->geometry.y = y;
1264 weston_view_geometry_dirty(view);
1268 transform_parent_handle_parent_destroy(struct wl_listener *listener,
1271 struct weston_view *view =
1272 container_of(listener, struct weston_view,
1273 geometry.parent_destroy_listener);
1275 weston_view_set_transform_parent(view, NULL);
1279 weston_view_set_transform_parent(struct weston_view *view,
1280 struct weston_view *parent)
1282 if (view->geometry.parent) {
1283 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1284 wl_list_remove(&view->geometry.parent_link);
1287 view->geometry.parent = parent;
1289 view->geometry.parent_destroy_listener.notify =
1290 transform_parent_handle_parent_destroy;
1292 wl_signal_add(&parent->destroy_signal,
1293 &view->geometry.parent_destroy_listener);
1294 wl_list_insert(&parent->geometry.child_list,
1295 &view->geometry.parent_link);
1298 weston_view_geometry_dirty(view);
1302 weston_view_is_mapped(struct weston_view *view)
1311 weston_surface_is_mapped(struct weston_surface *surface)
1313 if (surface->output)
1320 surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
1322 struct weston_view *view;
1324 if (surface->width == width && surface->height == height)
1327 surface->width = width;
1328 surface->height = height;
1330 wl_list_for_each(view, &surface->views, surface_link)
1331 weston_view_geometry_dirty(view);
1335 weston_surface_set_size(struct weston_surface *surface,
1336 int32_t width, int32_t height)
1338 assert(!surface->resource);
1339 surface_set_size(surface, width, height);
1343 fixed_round_up_to_int(wl_fixed_t f)
1345 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1349 weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
1351 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1352 int32_t width, height;
1354 if (!surface->buffer_ref.buffer) {
1355 surface->width_from_buffer = 0;
1356 surface->height_from_buffer = 0;
1360 switch (vp->buffer.transform) {
1361 case WL_OUTPUT_TRANSFORM_90:
1362 case WL_OUTPUT_TRANSFORM_270:
1363 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1364 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1365 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1366 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
1369 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1370 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
1374 surface->width_from_buffer = width;
1375 surface->height_from_buffer = height;
1379 weston_surface_update_size(struct weston_surface *surface)
1381 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1382 int32_t width, height;
1384 width = surface->width_from_buffer;
1385 height = surface->height_from_buffer;
1387 if (width != 0 && vp->surface.width != -1) {
1388 surface_set_size(surface,
1389 vp->surface.width, vp->surface.height);
1393 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
1394 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1395 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1397 surface_set_size(surface, w ?: 1, h ?: 1);
1401 surface_set_size(surface, width, height);
1405 weston_compositor_get_time(void)
1409 gettimeofday(&tv, NULL);
1411 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1414 WL_EXPORT struct weston_view *
1415 weston_compositor_pick_view(struct weston_compositor *compositor,
1416 wl_fixed_t x, wl_fixed_t y,
1417 wl_fixed_t *vx, wl_fixed_t *vy)
1419 struct weston_view *view;
1420 int ix = wl_fixed_to_int(x);
1421 int iy = wl_fixed_to_int(y);
1423 wl_list_for_each(view, &compositor->view_list, link) {
1424 weston_view_from_global_fixed(view, x, y, vx, vy);
1425 if (pixman_region32_contains_point(
1426 &view->transform.masked_boundingbox,
1428 pixman_region32_contains_point(&view->surface->input,
1429 wl_fixed_to_int(*vx),
1430 wl_fixed_to_int(*vy),
1439 weston_compositor_repick(struct weston_compositor *compositor)
1441 struct weston_seat *seat;
1443 if (!compositor->session_active)
1446 wl_list_for_each(seat, &compositor->seat_list, link)
1447 weston_seat_repick(seat);
1451 weston_view_unmap(struct weston_view *view)
1453 struct weston_seat *seat;
1455 if (!weston_view_is_mapped(view))
1458 weston_view_damage_below(view);
1459 view->output = NULL;
1461 weston_layer_entry_remove(&view->layer_link);
1462 wl_list_remove(&view->link);
1463 wl_list_init(&view->link);
1464 view->output_mask = 0;
1465 weston_surface_assign_output(view->surface);
1467 if (weston_surface_is_mapped(view->surface))
1470 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1471 if (seat->keyboard && seat->keyboard->focus == view->surface)
1472 weston_keyboard_set_focus(seat->keyboard, NULL);
1473 if (seat->pointer && seat->pointer->focus == view)
1474 weston_pointer_set_focus(seat->pointer,
1476 wl_fixed_from_int(0),
1477 wl_fixed_from_int(0));
1478 if (seat->touch && seat->touch->focus == view)
1479 weston_touch_set_focus(seat, NULL);
1484 weston_surface_unmap(struct weston_surface *surface)
1486 struct weston_view *view;
1488 wl_list_for_each(view, &surface->views, surface_link)
1489 weston_view_unmap(view);
1490 surface->output = NULL;
1494 weston_surface_reset_pending_buffer(struct weston_surface *surface)
1496 weston_surface_state_set_buffer(&surface->pending, NULL);
1497 surface->pending.sx = 0;
1498 surface->pending.sy = 0;
1499 surface->pending.newly_attached = 0;
1500 surface->pending.buffer_viewport.changed = 0;
1504 weston_view_destroy(struct weston_view *view)
1506 wl_signal_emit(&view->destroy_signal, view);
1508 assert(wl_list_empty(&view->geometry.child_list));
1510 if (weston_view_is_mapped(view)) {
1511 weston_view_unmap(view);
1512 weston_compositor_build_view_list(view->surface->compositor);
1515 wl_list_remove(&view->link);
1516 weston_layer_entry_remove(&view->layer_link);
1518 pixman_region32_fini(&view->clip);
1519 pixman_region32_fini(&view->transform.boundingbox);
1520 pixman_region32_fini(&view->transform.masked_boundingbox);
1521 pixman_region32_fini(&view->transform.masked_opaque);
1523 weston_view_set_transform_parent(view, NULL);
1525 wl_list_remove(&view->surface_link);
1531 weston_surface_destroy(struct weston_surface *surface)
1533 struct weston_frame_callback *cb, *next;
1534 struct weston_view *ev, *nv;
1536 if (--surface->ref_count > 0)
1539 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1541 assert(wl_list_empty(&surface->subsurface_list_pending));
1542 assert(wl_list_empty(&surface->subsurface_list));
1544 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1545 weston_view_destroy(ev);
1547 weston_surface_state_fini(&surface->pending);
1549 weston_buffer_reference(&surface->buffer_ref, NULL);
1551 pixman_region32_fini(&surface->damage);
1552 pixman_region32_fini(&surface->opaque);
1553 pixman_region32_fini(&surface->input);
1555 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1556 wl_resource_destroy(cb->resource);
1562 destroy_surface(struct wl_resource *resource)
1564 struct weston_surface *surface = wl_resource_get_user_data(resource);
1566 /* Set the resource to NULL, since we don't want to leave a
1567 * dangling pointer if the surface was refcounted and survives
1568 * the weston_surface_destroy() call. */
1569 surface->resource = NULL;
1570 weston_surface_destroy(surface);
1574 weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1576 struct weston_buffer *buffer =
1577 container_of(listener, struct weston_buffer, destroy_listener);
1579 wl_signal_emit(&buffer->destroy_signal, buffer);
1583 WL_EXPORT struct weston_buffer *
1584 weston_buffer_from_resource(struct wl_resource *resource)
1586 struct weston_buffer *buffer;
1587 struct wl_listener *listener;
1589 listener = wl_resource_get_destroy_listener(resource,
1590 weston_buffer_destroy_handler);
1593 return container_of(listener, struct weston_buffer,
1596 buffer = zalloc(sizeof *buffer);
1600 buffer->resource = resource;
1601 wl_signal_init(&buffer->destroy_signal);
1602 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1603 buffer->y_inverted = 1;
1604 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
1610 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1613 struct weston_buffer_reference *ref =
1614 container_of(listener, struct weston_buffer_reference,
1617 assert((struct weston_buffer *)data == ref->buffer);
1622 weston_buffer_reference(struct weston_buffer_reference *ref,
1623 struct weston_buffer *buffer)
1625 if (ref->buffer && buffer != ref->buffer) {
1626 ref->buffer->busy_count--;
1627 if (ref->buffer->busy_count == 0) {
1628 assert(wl_resource_get_client(ref->buffer->resource));
1629 wl_resource_queue_event(ref->buffer->resource,
1632 wl_list_remove(&ref->destroy_listener.link);
1635 if (buffer && buffer != ref->buffer) {
1636 buffer->busy_count++;
1637 wl_signal_add(&buffer->destroy_signal,
1638 &ref->destroy_listener);
1641 ref->buffer = buffer;
1642 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1646 weston_surface_attach(struct weston_surface *surface,
1647 struct weston_buffer *buffer)
1649 weston_buffer_reference(&surface->buffer_ref, buffer);
1652 if (weston_surface_is_mapped(surface))
1653 weston_surface_unmap(surface);
1656 surface->compositor->renderer->attach(surface, buffer);
1658 weston_surface_calculate_size_from_buffer(surface);
1662 weston_compositor_damage_all(struct weston_compositor *compositor)
1664 struct weston_output *output;
1666 wl_list_for_each(output, &compositor->output_list, link)
1667 weston_output_damage(output);
1671 weston_output_damage(struct weston_output *output)
1673 struct weston_compositor *compositor = output->compositor;
1675 pixman_region32_union(&compositor->primary_plane.damage,
1676 &compositor->primary_plane.damage,
1678 weston_output_schedule_repaint(output);
1682 surface_flush_damage(struct weston_surface *surface)
1684 if (surface->buffer_ref.buffer &&
1685 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
1686 surface->compositor->renderer->flush_damage(surface);
1688 pixman_region32_clear(&surface->damage);
1692 view_accumulate_damage(struct weston_view *view,
1693 pixman_region32_t *opaque)
1695 pixman_region32_t damage;
1697 pixman_region32_init(&damage);
1698 if (view->transform.enabled) {
1699 pixman_box32_t *extents;
1701 extents = pixman_region32_extents(&view->surface->damage);
1702 view_compute_bbox(view, extents->x1, extents->y1,
1703 extents->x2 - extents->x1,
1704 extents->y2 - extents->y1,
1706 pixman_region32_translate(&damage,
1710 pixman_region32_copy(&damage, &view->surface->damage);
1711 pixman_region32_translate(&damage,
1712 view->geometry.x - view->plane->x,
1713 view->geometry.y - view->plane->y);
1716 pixman_region32_subtract(&damage, &damage, opaque);
1717 pixman_region32_union(&view->plane->damage,
1718 &view->plane->damage, &damage);
1719 pixman_region32_fini(&damage);
1720 pixman_region32_copy(&view->clip, opaque);
1721 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
1725 compositor_accumulate_damage(struct weston_compositor *ec)
1727 struct weston_plane *plane;
1728 struct weston_view *ev;
1729 pixman_region32_t opaque, clip;
1731 pixman_region32_init(&clip);
1733 wl_list_for_each(plane, &ec->plane_list, link) {
1734 pixman_region32_copy(&plane->clip, &clip);
1736 pixman_region32_init(&opaque);
1738 wl_list_for_each(ev, &ec->view_list, link) {
1739 if (ev->plane != plane)
1742 view_accumulate_damage(ev, &opaque);
1745 pixman_region32_union(&clip, &clip, &opaque);
1746 pixman_region32_fini(&opaque);
1749 pixman_region32_fini(&clip);
1751 wl_list_for_each(ev, &ec->view_list, link)
1752 ev->surface->touched = 0;
1754 wl_list_for_each(ev, &ec->view_list, link) {
1755 if (ev->surface->touched)
1757 ev->surface->touched = 1;
1759 surface_flush_damage(ev->surface);
1761 /* Both the renderer and the backend have seen the buffer
1762 * by now. If renderer needs the buffer, it has its own
1763 * reference set. If the backend wants to keep the buffer
1764 * around for migrating the surface into a non-primary plane
1765 * later, keep_buffer is true. Otherwise, drop the core
1766 * reference now, and allow early buffer release. This enables
1767 * clients to use single-buffering.
1769 if (!ev->surface->keep_buffer)
1770 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
1775 surface_stash_subsurface_views(struct weston_surface *surface)
1777 struct weston_subsurface *sub;
1779 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1780 if (sub->surface == surface)
1783 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1784 wl_list_init(&sub->surface->views);
1786 surface_stash_subsurface_views(sub->surface);
1791 surface_free_unused_subsurface_views(struct weston_surface *surface)
1793 struct weston_subsurface *sub;
1794 struct weston_view *view, *nv;
1796 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1797 if (sub->surface == surface)
1800 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1801 weston_view_unmap (view);
1802 weston_view_destroy(view);
1805 surface_free_unused_subsurface_views(sub->surface);
1810 view_list_add_subsurface_view(struct weston_compositor *compositor,
1811 struct weston_subsurface *sub,
1812 struct weston_view *parent)
1814 struct weston_subsurface *child;
1815 struct weston_view *view = NULL, *iv;
1817 if (!weston_surface_is_mapped(sub->surface))
1820 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1821 if (iv->geometry.parent == parent) {
1828 /* Put it back in the surface's list of views */
1829 wl_list_remove(&view->surface_link);
1830 wl_list_insert(&sub->surface->views, &view->surface_link);
1832 view = weston_view_create(sub->surface);
1833 weston_view_set_position(view,
1836 weston_view_set_transform_parent(view, parent);
1839 view->parent_view = parent;
1840 weston_view_update_transform(view);
1842 if (wl_list_empty(&sub->surface->subsurface_list)) {
1843 wl_list_insert(compositor->view_list.prev, &view->link);
1847 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1848 if (child->surface == sub->surface)
1849 wl_list_insert(compositor->view_list.prev, &view->link);
1851 view_list_add_subsurface_view(compositor, child, view);
1856 view_list_add(struct weston_compositor *compositor,
1857 struct weston_view *view)
1859 struct weston_subsurface *sub;
1861 weston_view_update_transform(view);
1863 if (wl_list_empty(&view->surface->subsurface_list)) {
1864 wl_list_insert(compositor->view_list.prev, &view->link);
1868 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1869 if (sub->surface == view->surface)
1870 wl_list_insert(compositor->view_list.prev, &view->link);
1872 view_list_add_subsurface_view(compositor, sub, view);
1877 weston_compositor_build_view_list(struct weston_compositor *compositor)
1879 struct weston_view *view;
1880 struct weston_layer *layer;
1882 wl_list_for_each(layer, &compositor->layer_list, link)
1883 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
1884 surface_stash_subsurface_views(view->surface);
1886 wl_list_init(&compositor->view_list);
1887 wl_list_for_each(layer, &compositor->layer_list, link) {
1888 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
1889 view_list_add(compositor, view);
1893 wl_list_for_each(layer, &compositor->layer_list, link)
1894 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
1895 surface_free_unused_subsurface_views(view->surface);
1899 weston_output_repaint(struct weston_output *output, uint32_t msecs)
1901 struct weston_compositor *ec = output->compositor;
1902 struct weston_view *ev;
1903 struct weston_animation *animation, *next;
1904 struct weston_frame_callback *cb, *cnext;
1905 struct wl_list frame_callback_list;
1906 pixman_region32_t output_damage;
1909 if (output->destroying)
1912 /* Rebuild the surface list and update surface transforms up front. */
1913 weston_compositor_build_view_list(ec);
1915 if (output->assign_planes && !output->disable_planes)
1916 output->assign_planes(output);
1918 wl_list_for_each(ev, &ec->view_list, link)
1919 weston_view_move_to_plane(ev, &ec->primary_plane);
1921 wl_list_init(&frame_callback_list);
1922 wl_list_for_each(ev, &ec->view_list, link) {
1923 /* Note: This operation is safe to do multiple times on the
1926 if (ev->surface->output == output) {
1927 wl_list_insert_list(&frame_callback_list,
1928 &ev->surface->frame_callback_list);
1929 wl_list_init(&ev->surface->frame_callback_list);
1933 compositor_accumulate_damage(ec);
1935 pixman_region32_init(&output_damage);
1936 pixman_region32_intersect(&output_damage,
1937 &ec->primary_plane.damage, &output->region);
1938 pixman_region32_subtract(&output_damage,
1939 &output_damage, &ec->primary_plane.clip);
1942 weston_output_update_matrix(output);
1944 r = output->repaint(output, &output_damage);
1946 pixman_region32_fini(&output_damage);
1948 output->repaint_needed = 0;
1950 weston_compositor_repick(ec);
1951 wl_event_loop_dispatch(ec->input_loop, 0);
1953 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
1954 wl_callback_send_done(cb->resource, msecs);
1955 wl_resource_destroy(cb->resource);
1958 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
1959 animation->frame_counter++;
1960 animation->frame(animation, output, msecs);
1967 weston_compositor_read_input(int fd, uint32_t mask, void *data)
1969 struct weston_compositor *compositor = data;
1971 wl_event_loop_dispatch(compositor->input_loop, 0);
1977 weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
1979 struct weston_compositor *compositor = output->compositor;
1980 struct wl_event_loop *loop =
1981 wl_display_get_event_loop(compositor->wl_display);
1984 output->frame_time = msecs;
1986 if (output->repaint_needed &&
1987 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1988 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
1989 r = weston_output_repaint(output, msecs);
1994 output->repaint_scheduled = 0;
1995 if (compositor->input_loop_source)
1998 fd = wl_event_loop_get_fd(compositor->input_loop);
1999 compositor->input_loop_source =
2000 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2001 weston_compositor_read_input, compositor);
2005 idle_repaint(void *data)
2007 struct weston_output *output = data;
2009 output->start_repaint_loop(output);
2013 weston_layer_entry_insert(struct weston_layer_entry *list,
2014 struct weston_layer_entry *entry)
2016 wl_list_insert(&list->link, &entry->link);
2017 entry->layer = list->layer;
2021 weston_layer_entry_remove(struct weston_layer_entry *entry)
2023 wl_list_remove(&entry->link);
2024 wl_list_init(&entry->link);
2025 entry->layer = NULL;
2029 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2031 wl_list_init(&layer->view_list.link);
2032 layer->view_list.layer = layer;
2033 weston_layer_set_mask_infinite(layer);
2035 wl_list_insert(below, &layer->link);
2039 weston_layer_set_mask(struct weston_layer *layer,
2040 int x, int y, int width, int height)
2042 struct weston_view *view;
2045 layer->mask.x2 = x + width;
2047 layer->mask.y2 = y + height;
2049 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2050 weston_view_geometry_dirty(view);
2055 weston_layer_set_mask_infinite(struct weston_layer *layer)
2057 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2058 UINT32_MAX, UINT32_MAX);
2062 weston_output_schedule_repaint(struct weston_output *output)
2064 struct weston_compositor *compositor = output->compositor;
2065 struct wl_event_loop *loop;
2067 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2068 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2071 loop = wl_display_get_event_loop(compositor->wl_display);
2072 output->repaint_needed = 1;
2073 if (output->repaint_scheduled)
2076 wl_event_loop_add_idle(loop, idle_repaint, output);
2077 output->repaint_scheduled = 1;
2079 if (compositor->input_loop_source) {
2080 wl_event_source_remove(compositor->input_loop_source);
2081 compositor->input_loop_source = NULL;
2086 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2088 struct weston_output *output;
2090 wl_list_for_each(output, &compositor->output_list, link)
2091 weston_output_schedule_repaint(output);
2095 surface_destroy(struct wl_client *client, struct wl_resource *resource)
2097 wl_resource_destroy(resource);
2101 surface_attach(struct wl_client *client,
2102 struct wl_resource *resource,
2103 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2105 struct weston_surface *surface = wl_resource_get_user_data(resource);
2106 struct weston_buffer *buffer = NULL;
2108 if (buffer_resource) {
2109 buffer = weston_buffer_from_resource(buffer_resource);
2110 if (buffer == NULL) {
2111 wl_client_post_no_memory(client);
2116 /* Attach, attach, without commit in between does not send
2117 * wl_buffer.release. */
2118 weston_surface_state_set_buffer(&surface->pending, buffer);
2120 surface->pending.sx = sx;
2121 surface->pending.sy = sy;
2122 surface->pending.newly_attached = 1;
2126 surface_damage(struct wl_client *client,
2127 struct wl_resource *resource,
2128 int32_t x, int32_t y, int32_t width, int32_t height)
2130 struct weston_surface *surface = wl_resource_get_user_data(resource);
2132 pixman_region32_union_rect(&surface->pending.damage,
2133 &surface->pending.damage,
2134 x, y, width, height);
2138 destroy_frame_callback(struct wl_resource *resource)
2140 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
2142 wl_list_remove(&cb->link);
2147 surface_frame(struct wl_client *client,
2148 struct wl_resource *resource, uint32_t callback)
2150 struct weston_frame_callback *cb;
2151 struct weston_surface *surface = wl_resource_get_user_data(resource);
2153 cb = malloc(sizeof *cb);
2155 wl_resource_post_no_memory(resource);
2159 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2161 if (cb->resource == NULL) {
2163 wl_resource_post_no_memory(resource);
2167 wl_resource_set_implementation(cb->resource, NULL, cb,
2168 destroy_frame_callback);
2170 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
2174 surface_set_opaque_region(struct wl_client *client,
2175 struct wl_resource *resource,
2176 struct wl_resource *region_resource)
2178 struct weston_surface *surface = wl_resource_get_user_data(resource);
2179 struct weston_region *region;
2181 if (region_resource) {
2182 region = wl_resource_get_user_data(region_resource);
2183 pixman_region32_copy(&surface->pending.opaque,
2186 pixman_region32_clear(&surface->pending.opaque);
2191 surface_set_input_region(struct wl_client *client,
2192 struct wl_resource *resource,
2193 struct wl_resource *region_resource)
2195 struct weston_surface *surface = wl_resource_get_user_data(resource);
2196 struct weston_region *region;
2198 if (region_resource) {
2199 region = wl_resource_get_user_data(region_resource);
2200 pixman_region32_copy(&surface->pending.input,
2203 pixman_region32_fini(&surface->pending.input);
2204 region_init_infinite(&surface->pending.input);
2209 weston_surface_commit_subsurface_order(struct weston_surface *surface)
2211 struct weston_subsurface *sub;
2213 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2214 parent_link_pending) {
2215 wl_list_remove(&sub->parent_link);
2216 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2221 weston_surface_commit_state(struct weston_surface *surface,
2222 struct weston_surface_state *state)
2224 struct weston_view *view;
2225 pixman_region32_t opaque;
2227 /* wl_surface.set_buffer_transform */
2228 /* wl_surface.set_buffer_scale */
2229 /* wl_viewport.set */
2230 surface->buffer_viewport = state->buffer_viewport;
2232 /* wl_surface.attach */
2233 if (state->newly_attached)
2234 weston_surface_attach(surface, state->buffer);
2235 weston_surface_state_set_buffer(state, NULL);
2237 if (state->newly_attached || state->buffer_viewport.changed) {
2238 weston_surface_update_size(surface);
2239 if (surface->configure)
2240 surface->configure(surface, state->sx, state->sy);
2245 state->newly_attached = 0;
2246 state->buffer_viewport.changed = 0;
2248 /* wl_surface.damage */
2249 pixman_region32_union(&surface->damage, &surface->damage,
2251 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2252 0, 0, surface->width, surface->height);
2253 pixman_region32_clear(&state->damage);
2255 /* wl_surface.set_opaque_region */
2256 pixman_region32_init(&opaque);
2257 pixman_region32_intersect_rect(&opaque, &state->opaque,
2258 0, 0, surface->width, surface->height);
2260 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2261 pixman_region32_copy(&surface->opaque, &opaque);
2262 wl_list_for_each(view, &surface->views, surface_link)
2263 weston_view_geometry_dirty(view);
2266 pixman_region32_fini(&opaque);
2268 /* wl_surface.set_input_region */
2269 pixman_region32_intersect_rect(&surface->input, &state->input,
2270 0, 0, surface->width, surface->height);
2272 /* wl_surface.frame */
2273 wl_list_insert_list(&surface->frame_callback_list,
2274 &state->frame_callback_list);
2275 wl_list_init(&state->frame_callback_list);
2279 weston_surface_commit(struct weston_surface *surface)
2281 weston_surface_commit_state(surface, &surface->pending);
2283 weston_surface_commit_subsurface_order(surface);
2285 weston_surface_schedule_repaint(surface);
2289 weston_subsurface_commit(struct weston_subsurface *sub);
2292 weston_subsurface_parent_commit(struct weston_subsurface *sub,
2293 int parent_is_synchronized);
2296 surface_commit(struct wl_client *client, struct wl_resource *resource)
2298 struct weston_surface *surface = wl_resource_get_user_data(resource);
2299 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2302 weston_subsurface_commit(sub);
2306 weston_surface_commit(surface);
2308 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2309 if (sub->surface != surface)
2310 weston_subsurface_parent_commit(sub, 0);
2315 surface_set_buffer_transform(struct wl_client *client,
2316 struct wl_resource *resource, int transform)
2318 struct weston_surface *surface = wl_resource_get_user_data(resource);
2320 /* if wl_output.transform grows more members this will need to be updated. */
2321 if (transform < 0 ||
2322 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2323 wl_resource_post_error(resource,
2324 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2325 "buffer transform must be a valid transform "
2326 "('%d' specified)", transform);
2330 surface->pending.buffer_viewport.buffer.transform = transform;
2331 surface->pending.buffer_viewport.changed = 1;
2335 surface_set_buffer_scale(struct wl_client *client,
2336 struct wl_resource *resource,
2339 struct weston_surface *surface = wl_resource_get_user_data(resource);
2342 wl_resource_post_error(resource,
2343 WL_SURFACE_ERROR_INVALID_SCALE,
2344 "buffer scale must be at least one "
2345 "('%d' specified)", scale);
2349 surface->pending.buffer_viewport.buffer.scale = scale;
2350 surface->pending.buffer_viewport.changed = 1;
2353 static const struct wl_surface_interface surface_interface = {
2358 surface_set_opaque_region,
2359 surface_set_input_region,
2361 surface_set_buffer_transform,
2362 surface_set_buffer_scale
2366 compositor_create_surface(struct wl_client *client,
2367 struct wl_resource *resource, uint32_t id)
2369 struct weston_compositor *ec = wl_resource_get_user_data(resource);
2370 struct weston_surface *surface;
2372 surface = weston_surface_create(ec);
2373 if (surface == NULL) {
2374 wl_resource_post_no_memory(resource);
2379 wl_resource_create(client, &wl_surface_interface,
2380 wl_resource_get_version(resource), id);
2381 if (surface->resource == NULL) {
2382 weston_surface_destroy(surface);
2383 wl_resource_post_no_memory(resource);
2386 wl_resource_set_implementation(surface->resource, &surface_interface,
2387 surface, destroy_surface);
2389 wl_signal_emit(&ec->create_surface_signal, surface);
2393 destroy_region(struct wl_resource *resource)
2395 struct weston_region *region = wl_resource_get_user_data(resource);
2397 pixman_region32_fini(®ion->region);
2402 region_destroy(struct wl_client *client, struct wl_resource *resource)
2404 wl_resource_destroy(resource);
2408 region_add(struct wl_client *client, struct wl_resource *resource,
2409 int32_t x, int32_t y, int32_t width, int32_t height)
2411 struct weston_region *region = wl_resource_get_user_data(resource);
2413 pixman_region32_union_rect(®ion->region, ®ion->region,
2414 x, y, width, height);
2418 region_subtract(struct wl_client *client, struct wl_resource *resource,
2419 int32_t x, int32_t y, int32_t width, int32_t height)
2421 struct weston_region *region = wl_resource_get_user_data(resource);
2422 pixman_region32_t rect;
2424 pixman_region32_init_rect(&rect, x, y, width, height);
2425 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
2426 pixman_region32_fini(&rect);
2429 static const struct wl_region_interface region_interface = {
2436 compositor_create_region(struct wl_client *client,
2437 struct wl_resource *resource, uint32_t id)
2439 struct weston_region *region;
2441 region = malloc(sizeof *region);
2442 if (region == NULL) {
2443 wl_resource_post_no_memory(resource);
2447 pixman_region32_init(®ion->region);
2450 wl_resource_create(client, &wl_region_interface, 1, id);
2451 if (region->resource == NULL) {
2453 wl_resource_post_no_memory(resource);
2456 wl_resource_set_implementation(region->resource, ®ion_interface,
2457 region, destroy_region);
2460 static const struct wl_compositor_interface compositor_interface = {
2461 compositor_create_surface,
2462 compositor_create_region
2466 weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2468 struct weston_surface *surface = sub->surface;
2470 weston_surface_commit_state(surface, &sub->cached);
2471 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
2473 weston_surface_commit_subsurface_order(surface);
2475 weston_surface_schedule_repaint(surface);
2477 sub->has_cached_data = 0;
2481 weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2483 struct weston_surface *surface = sub->surface;
2486 * If this commit would cause the surface to move by the
2487 * attach(dx, dy) parameters, the old damage region must be
2488 * translated to correspond to the new surface coordinate system
2491 pixman_region32_translate(&sub->cached.damage,
2492 -surface->pending.sx, -surface->pending.sy);
2493 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2494 &surface->pending.damage);
2495 pixman_region32_clear(&surface->pending.damage);
2497 if (surface->pending.newly_attached) {
2498 sub->cached.newly_attached = 1;
2499 weston_surface_state_set_buffer(&sub->cached,
2500 surface->pending.buffer);
2501 weston_buffer_reference(&sub->cached_buffer_ref,
2502 surface->pending.buffer);
2504 sub->cached.sx += surface->pending.sx;
2505 sub->cached.sy += surface->pending.sy;
2507 sub->cached.buffer_viewport.changed |=
2508 surface->pending.buffer_viewport.changed;
2509 sub->cached.buffer_viewport.buffer =
2510 surface->pending.buffer_viewport.buffer;
2511 sub->cached.buffer_viewport.surface =
2512 surface->pending.buffer_viewport.surface;
2514 weston_surface_reset_pending_buffer(surface);
2516 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2518 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2520 wl_list_insert_list(&sub->cached.frame_callback_list,
2521 &surface->pending.frame_callback_list);
2522 wl_list_init(&surface->pending.frame_callback_list);
2524 sub->has_cached_data = 1;
2528 weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2531 if (sub->synchronized)
2537 sub = weston_surface_to_subsurface(sub->parent);
2544 weston_subsurface_commit(struct weston_subsurface *sub)
2546 struct weston_surface *surface = sub->surface;
2547 struct weston_subsurface *tmp;
2549 /* Recursive check for effectively synchronized. */
2550 if (weston_subsurface_is_synchronized(sub)) {
2551 weston_subsurface_commit_to_cache(sub);
2553 if (sub->has_cached_data) {
2554 /* flush accumulated state from cache */
2555 weston_subsurface_commit_to_cache(sub);
2556 weston_subsurface_commit_from_cache(sub);
2558 weston_surface_commit(surface);
2561 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2562 if (tmp->surface != surface)
2563 weston_subsurface_parent_commit(tmp, 0);
2569 weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
2571 struct weston_surface *surface = sub->surface;
2572 struct weston_subsurface *tmp;
2574 /* From now on, commit_from_cache the whole sub-tree, regardless of
2575 * the synchronized mode of each child. This sub-surface or some
2576 * of its ancestors were synchronized, so we are synchronized
2580 if (sub->has_cached_data)
2581 weston_subsurface_commit_from_cache(sub);
2583 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2584 if (tmp->surface != surface)
2585 weston_subsurface_parent_commit(tmp, 1);
2590 weston_subsurface_parent_commit(struct weston_subsurface *sub,
2591 int parent_is_synchronized)
2593 struct weston_view *view;
2594 if (sub->position.set) {
2595 wl_list_for_each(view, &sub->surface->views, surface_link)
2596 weston_view_set_position(view,
2600 sub->position.set = 0;
2603 if (parent_is_synchronized || sub->synchronized)
2604 weston_subsurface_synchronized_commit(sub);
2608 subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
2610 struct weston_compositor *compositor = surface->compositor;
2611 struct weston_view *view;
2613 wl_list_for_each(view, &surface->views, surface_link)
2614 weston_view_set_position(view,
2615 view->geometry.x + dx,
2616 view->geometry.y + dy);
2618 /* No need to check parent mappedness, because if parent is not
2619 * mapped, parent is not in a visible layer, so this sub-surface
2620 * will not be drawn either.
2622 if (!weston_surface_is_mapped(surface)) {
2623 struct weston_output *output;
2625 /* Cannot call weston_view_update_transform(),
2626 * because that would call it also for the parent surface,
2627 * which might not be mapped yet. That would lead to
2628 * inconsistent state, where the window could never be
2631 * Instead just assign any output, to make
2632 * weston_surface_is_mapped() return true, so that when the
2633 * parent surface does get mapped, this one will get
2634 * included, too. See view_list_add().
2636 assert(!wl_list_empty(&compositor->output_list));
2637 output = container_of(compositor->output_list.next,
2638 struct weston_output, link);
2640 surface->output = output;
2641 weston_surface_update_output_mask(surface, 1 << output->id);
2645 static struct weston_subsurface *
2646 weston_surface_to_subsurface(struct weston_surface *surface)
2648 if (surface->configure == subsurface_configure)
2649 return surface->configure_private;
2654 WL_EXPORT struct weston_surface *
2655 weston_surface_get_main_surface(struct weston_surface *surface)
2657 struct weston_subsurface *sub;
2659 while (surface && (sub = weston_surface_to_subsurface(surface)))
2660 surface = sub->parent;
2666 subsurface_set_position(struct wl_client *client,
2667 struct wl_resource *resource, int32_t x, int32_t y)
2669 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2674 sub->position.x = x;
2675 sub->position.y = y;
2676 sub->position.set = 1;
2679 static struct weston_subsurface *
2680 subsurface_from_surface(struct weston_surface *surface)
2682 struct weston_subsurface *sub;
2684 sub = weston_surface_to_subsurface(surface);
2688 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2689 if (sub->surface == surface)
2695 static struct weston_subsurface *
2696 subsurface_sibling_check(struct weston_subsurface *sub,
2697 struct weston_surface *surface,
2698 const char *request)
2700 struct weston_subsurface *sibling;
2702 sibling = subsurface_from_surface(surface);
2705 wl_resource_post_error(sub->resource,
2706 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2707 "%s: wl_surface@%d is not a parent or sibling",
2708 request, wl_resource_get_id(surface->resource));
2712 if (sibling->parent != sub->parent) {
2713 wl_resource_post_error(sub->resource,
2714 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2715 "%s: wl_surface@%d has a different parent",
2716 request, wl_resource_get_id(surface->resource));
2724 subsurface_place_above(struct wl_client *client,
2725 struct wl_resource *resource,
2726 struct wl_resource *sibling_resource)
2728 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2729 struct weston_surface *surface =
2730 wl_resource_get_user_data(sibling_resource);
2731 struct weston_subsurface *sibling;
2736 sibling = subsurface_sibling_check(sub, surface, "place_above");
2740 wl_list_remove(&sub->parent_link_pending);
2741 wl_list_insert(sibling->parent_link_pending.prev,
2742 &sub->parent_link_pending);
2746 subsurface_place_below(struct wl_client *client,
2747 struct wl_resource *resource,
2748 struct wl_resource *sibling_resource)
2750 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2751 struct weston_surface *surface =
2752 wl_resource_get_user_data(sibling_resource);
2753 struct weston_subsurface *sibling;
2758 sibling = subsurface_sibling_check(sub, surface, "place_below");
2762 wl_list_remove(&sub->parent_link_pending);
2763 wl_list_insert(&sibling->parent_link_pending,
2764 &sub->parent_link_pending);
2768 subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2770 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2773 sub->synchronized = 1;
2777 subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2779 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2781 if (sub && sub->synchronized) {
2782 sub->synchronized = 0;
2784 /* If sub became effectively desynchronized, flush. */
2785 if (!weston_subsurface_is_synchronized(sub))
2786 weston_subsurface_synchronized_commit(sub);
2791 weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2793 wl_list_remove(&sub->parent_link);
2794 wl_list_remove(&sub->parent_link_pending);
2795 wl_list_remove(&sub->parent_destroy_listener.link);
2800 weston_subsurface_destroy(struct weston_subsurface *sub);
2803 subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2805 struct weston_subsurface *sub =
2806 container_of(listener, struct weston_subsurface,
2807 surface_destroy_listener);
2808 assert(data == &sub->surface->resource);
2810 /* The protocol object (wl_resource) is left inert. */
2812 wl_resource_set_user_data(sub->resource, NULL);
2814 weston_subsurface_destroy(sub);
2818 subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2820 struct weston_subsurface *sub =
2821 container_of(listener, struct weston_subsurface,
2822 parent_destroy_listener);
2823 assert(data == &sub->parent->resource);
2824 assert(sub->surface != sub->parent);
2826 if (weston_surface_is_mapped(sub->surface))
2827 weston_surface_unmap(sub->surface);
2829 weston_subsurface_unlink_parent(sub);
2833 subsurface_resource_destroy(struct wl_resource *resource)
2835 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2838 weston_subsurface_destroy(sub);
2842 subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2844 wl_resource_destroy(resource);
2848 weston_subsurface_link_parent(struct weston_subsurface *sub,
2849 struct weston_surface *parent)
2851 sub->parent = parent;
2852 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
2853 wl_signal_add(&parent->destroy_signal,
2854 &sub->parent_destroy_listener);
2856 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2857 wl_list_insert(&parent->subsurface_list_pending,
2858 &sub->parent_link_pending);
2862 weston_subsurface_link_surface(struct weston_subsurface *sub,
2863 struct weston_surface *surface)
2865 sub->surface = surface;
2866 sub->surface_destroy_listener.notify =
2867 subsurface_handle_surface_destroy;
2868 wl_signal_add(&surface->destroy_signal,
2869 &sub->surface_destroy_listener);
2873 weston_subsurface_destroy(struct weston_subsurface *sub)
2875 struct weston_view *view, *next;
2877 assert(sub->surface);
2879 if (sub->resource) {
2880 assert(weston_surface_to_subsurface(sub->surface) == sub);
2881 assert(sub->parent_destroy_listener.notify ==
2882 subsurface_handle_parent_destroy);
2884 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2885 weston_view_unmap(view);
2886 weston_view_destroy(view);
2890 weston_subsurface_unlink_parent(sub);
2892 weston_surface_state_fini(&sub->cached);
2893 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
2895 sub->surface->configure = NULL;
2896 sub->surface->configure_private = NULL;
2898 /* the dummy weston_subsurface for the parent itself */
2899 assert(sub->parent_destroy_listener.notify == NULL);
2900 wl_list_remove(&sub->parent_link);
2901 wl_list_remove(&sub->parent_link_pending);
2904 wl_list_remove(&sub->surface_destroy_listener.link);
2908 static const struct wl_subsurface_interface subsurface_implementation = {
2910 subsurface_set_position,
2911 subsurface_place_above,
2912 subsurface_place_below,
2913 subsurface_set_sync,
2914 subsurface_set_desync
2917 static struct weston_subsurface *
2918 weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2919 struct weston_surface *parent)
2921 struct weston_subsurface *sub;
2922 struct wl_client *client = wl_resource_get_client(surface->resource);
2924 sub = calloc(1, sizeof *sub);
2928 wl_list_init(&sub->unused_views);
2931 wl_resource_create(client, &wl_subsurface_interface, 1, id);
2932 if (!sub->resource) {
2937 wl_resource_set_implementation(sub->resource,
2938 &subsurface_implementation,
2939 sub, subsurface_resource_destroy);
2940 weston_subsurface_link_surface(sub, surface);
2941 weston_subsurface_link_parent(sub, parent);
2942 weston_surface_state_init(&sub->cached);
2943 sub->cached_buffer_ref.buffer = NULL;
2944 sub->synchronized = 1;
2949 /* Create a dummy subsurface for having the parent itself in its
2950 * sub-surface lists. Makes stacking order manipulation easy.
2952 static struct weston_subsurface *
2953 weston_subsurface_create_for_parent(struct weston_surface *parent)
2955 struct weston_subsurface *sub;
2957 sub = calloc(1, sizeof *sub);
2961 weston_subsurface_link_surface(sub, parent);
2962 sub->parent = parent;
2963 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2964 wl_list_insert(&parent->subsurface_list_pending,
2965 &sub->parent_link_pending);
2971 subcompositor_get_subsurface(struct wl_client *client,
2972 struct wl_resource *resource,
2974 struct wl_resource *surface_resource,
2975 struct wl_resource *parent_resource)
2977 struct weston_surface *surface =
2978 wl_resource_get_user_data(surface_resource);
2979 struct weston_surface *parent =
2980 wl_resource_get_user_data(parent_resource);
2981 struct weston_subsurface *sub;
2982 static const char where[] = "get_subsurface: wl_subsurface@";
2984 if (surface == parent) {
2985 wl_resource_post_error(resource,
2986 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2987 "%s%d: wl_surface@%d cannot be its own parent",
2988 where, id, wl_resource_get_id(surface_resource));
2992 if (weston_surface_to_subsurface(surface)) {
2993 wl_resource_post_error(resource,
2994 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2995 "%s%d: wl_surface@%d is already a sub-surface",
2996 where, id, wl_resource_get_id(surface_resource));
3000 if (surface->configure) {
3001 wl_resource_post_error(resource,
3002 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3003 "%s%d: wl_surface@%d already has a role",
3004 where, id, wl_resource_get_id(surface_resource));
3008 if (weston_surface_get_main_surface(parent) == surface) {
3009 wl_resource_post_error(resource,
3010 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3011 "%s%d: wl_surface@%d is an ancestor of parent",
3012 where, id, wl_resource_get_id(surface_resource));
3016 /* make sure the parent is in its own list */
3017 if (wl_list_empty(&parent->subsurface_list)) {
3018 if (!weston_subsurface_create_for_parent(parent)) {
3019 wl_resource_post_no_memory(resource);
3024 sub = weston_subsurface_create(id, surface, parent);
3026 wl_resource_post_no_memory(resource);
3030 surface->configure = subsurface_configure;
3031 surface->configure_private = sub;
3035 subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3037 wl_resource_destroy(resource);
3040 static const struct wl_subcompositor_interface subcompositor_interface = {
3041 subcompositor_destroy,
3042 subcompositor_get_subsurface
3046 bind_subcompositor(struct wl_client *client,
3047 void *data, uint32_t version, uint32_t id)
3049 struct weston_compositor *compositor = data;
3050 struct wl_resource *resource;
3053 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
3054 if (resource == NULL) {
3055 wl_client_post_no_memory(client);
3058 wl_resource_set_implementation(resource, &subcompositor_interface,
3063 weston_compositor_dpms(struct weston_compositor *compositor,
3064 enum dpms_enum state)
3066 struct weston_output *output;
3068 wl_list_for_each(output, &compositor->output_list, link)
3069 if (output->set_dpms)
3070 output->set_dpms(output, state);
3074 weston_compositor_wake(struct weston_compositor *compositor)
3076 uint32_t old_state = compositor->state;
3078 /* The state needs to be changed before emitting the wake
3079 * signal because that may try to schedule a repaint which
3080 * will not work if the compositor is still sleeping */
3081 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3083 switch (old_state) {
3084 case WESTON_COMPOSITOR_SLEEPING:
3085 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3087 case WESTON_COMPOSITOR_IDLE:
3088 case WESTON_COMPOSITOR_OFFSCREEN:
3089 wl_signal_emit(&compositor->wake_signal, compositor);
3092 wl_event_source_timer_update(compositor->idle_source,
3093 compositor->idle_time * 1000);
3098 weston_compositor_offscreen(struct weston_compositor *compositor)
3100 switch (compositor->state) {
3101 case WESTON_COMPOSITOR_OFFSCREEN:
3103 case WESTON_COMPOSITOR_SLEEPING:
3104 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3107 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3108 wl_event_source_timer_update(compositor->idle_source, 0);
3113 weston_compositor_sleep(struct weston_compositor *compositor)
3115 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3118 wl_event_source_timer_update(compositor->idle_source, 0);
3119 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3120 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3124 idle_handler(void *data)
3126 struct weston_compositor *compositor = data;
3128 if (compositor->idle_inhibit)
3131 compositor->state = WESTON_COMPOSITOR_IDLE;
3132 wl_signal_emit(&compositor->idle_signal, compositor);
3138 weston_plane_init(struct weston_plane *plane,
3139 struct weston_compositor *ec,
3140 int32_t x, int32_t y)
3142 pixman_region32_init(&plane->damage);
3143 pixman_region32_init(&plane->clip);
3146 plane->compositor = ec;
3148 /* Init the link so that the call to wl_list_remove() when releasing
3149 * the plane without ever stacking doesn't lead to a crash */
3150 wl_list_init(&plane->link);
3154 weston_plane_release(struct weston_plane *plane)
3156 struct weston_view *view;
3158 pixman_region32_fini(&plane->damage);
3159 pixman_region32_fini(&plane->clip);
3161 wl_list_for_each(view, &plane->compositor->view_list, link) {
3162 if (view->plane == plane)
3166 wl_list_remove(&plane->link);
3170 weston_compositor_stack_plane(struct weston_compositor *ec,
3171 struct weston_plane *plane,
3172 struct weston_plane *above)
3175 wl_list_insert(above->link.prev, &plane->link);
3177 wl_list_insert(&ec->plane_list, &plane->link);
3180 static void unbind_resource(struct wl_resource *resource)
3182 wl_list_remove(wl_resource_get_link(resource));
3186 bind_output(struct wl_client *client,
3187 void *data, uint32_t version, uint32_t id)
3189 struct weston_output *output = data;
3190 struct weston_mode *mode;
3191 struct wl_resource *resource;
3193 resource = wl_resource_create(client, &wl_output_interface,
3200 if (resource == NULL) {
3201 wl_client_post_no_memory(client);
3205 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
3206 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
3208 wl_output_send_geometry(resource,
3214 output->make, output->model,
3216 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
3217 wl_output_send_scale(resource,
3218 output->current_scale);
3221 if (version >= WL_OUTPUT_NAME_SINCE_VERSION)
3222 wl_output_send_name(resource, output->name);
3224 if (version >= WL_OUTPUT_SEATNAME_SINCE_VERSION)
3225 wl_output_send_seatname(resource, output->seat_data.seatname);
3228 wl_list_for_each (mode, &output->mode_list, link) {
3229 wl_output_send_mode(resource,
3236 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
3237 wl_output_send_done(resource);
3240 /* Move other outputs when one is removed so the space remains contiguos. */
3242 weston_compositor_remove_output(struct weston_compositor *compositor,
3243 struct weston_output *remove_output)
3245 struct weston_output *output;
3248 wl_list_for_each(output, &compositor->output_list, link) {
3249 if (output == remove_output) {
3250 offset = output->width;
3255 weston_output_move(output,
3256 output->x - offset, output->y);
3263 weston_output_destroy(struct weston_output *output)
3265 struct wl_resource *resource;
3267 output->destroying = 1;
3269 weston_compositor_remove_output(output->compositor, output);
3270 wl_list_remove(&output->link);
3272 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
3273 wl_signal_emit(&output->destroy_signal, output);
3276 free(output->seat_data.seatname);
3277 pixman_region32_fini(&output->region);
3278 pixman_region32_fini(&output->previous_damage);
3279 output->compositor->output_id_pool &= ~(1 << output->id);
3281 wl_resource_for_each(resource, &output->resource_list) {
3282 wl_resource_set_destructor(resource, NULL);
3285 wl_global_destroy(output->global);
3289 weston_output_compute_transform(struct weston_output *output)
3291 struct weston_matrix transform;
3294 weston_matrix_init(&transform);
3295 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
3297 switch(output->transform) {
3298 case WL_OUTPUT_TRANSFORM_FLIPPED:
3299 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3300 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3301 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3302 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
3310 switch(output->transform) {
3311 case WL_OUTPUT_TRANSFORM_NORMAL:
3312 case WL_OUTPUT_TRANSFORM_FLIPPED:
3313 transform.d[0] = flip;
3318 case WL_OUTPUT_TRANSFORM_90:
3319 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3321 transform.d[1] = -flip;
3325 case WL_OUTPUT_TRANSFORM_180:
3326 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3327 transform.d[0] = -flip;
3330 transform.d[5] = -1;
3332 case WL_OUTPUT_TRANSFORM_270:
3333 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3335 transform.d[1] = flip;
3336 transform.d[4] = -1;
3343 weston_matrix_multiply(&output->matrix, &transform);
3347 weston_output_update_matrix(struct weston_output *output)
3349 float magnification;
3351 weston_matrix_init(&output->matrix);
3352 weston_matrix_translate(&output->matrix,
3353 -(output->x + output->width / 2.0),
3354 -(output->y + output->height / 2.0), 0);
3356 weston_matrix_scale(&output->matrix,
3357 2.0 / output->width,
3358 -2.0 / output->height, 1);
3360 if (output->zoom.active) {
3361 magnification = 1 / (1 - output->zoom.spring_z.current);
3362 weston_output_update_zoom(output);
3363 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3364 output->zoom.trans_y, 0);
3365 weston_matrix_scale(&output->matrix, magnification,
3366 magnification, 1.0);
3369 weston_output_compute_transform(output);
3375 weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
3377 output->transform = transform;
3379 switch (transform) {
3380 case WL_OUTPUT_TRANSFORM_90:
3381 case WL_OUTPUT_TRANSFORM_270:
3382 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3383 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3384 /* Swap width and height */
3385 output->width = output->current_mode->height;
3386 output->height = output->current_mode->width;
3388 case WL_OUTPUT_TRANSFORM_NORMAL:
3389 case WL_OUTPUT_TRANSFORM_180:
3390 case WL_OUTPUT_TRANSFORM_FLIPPED:
3391 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3392 output->width = output->current_mode->width;
3393 output->height = output->current_mode->height;
3399 output->native_scale = output->current_scale = scale;
3400 output->width /= scale;
3401 output->height /= scale;
3405 weston_output_init_geometry(struct weston_output *output, int x, int y)
3410 pixman_region32_init(&output->previous_damage);
3411 pixman_region32_init_rect(&output->region, x, y,
3417 weston_output_move(struct weston_output *output, int x, int y)
3419 pixman_region32_t old_region;
3420 struct wl_resource *resource;
3422 output->move_x = x - output->x;
3423 output->move_y = y - output->y;
3425 if (output->move_x == 0 && output->move_y == 0)
3428 pixman_region32_init(&old_region);
3429 pixman_region32_copy(&old_region, &output->region);
3431 weston_output_init_geometry(output, x, y);
3435 /* Move views on this output. */
3436 wl_signal_emit(&output->compositor->output_moved_signal, output);
3438 /* Notify clients of the change for output position. */
3439 wl_resource_for_each(resource, &output->resource_list) {
3440 wl_output_send_geometry(resource,
3450 if (wl_resource_get_version(resource) >= 2)
3451 wl_output_send_done(resource);
3456 weston_output_init(struct weston_output *output, struct weston_compositor *c,
3457 int x, int y, int mm_width, int mm_height, uint32_t transform,
3461 const int version = 3;
3463 const int version = 2;
3465 output->compositor = c;
3468 output->mm_width = mm_width;
3469 output->mm_height = mm_height;
3471 output->original_scale = scale;
3473 weston_output_transform_scale_init(output, transform, scale);
3474 weston_output_init_zoom(output);
3476 weston_output_init_geometry(output, x, y);
3477 weston_output_damage(output);
3479 wl_signal_init(&output->frame_signal);
3480 wl_signal_init(&output->destroy_signal);
3481 wl_list_init(&output->animation_list);
3482 wl_list_init(&output->resource_list);
3484 output->id = ffs(~output->compositor->output_id_pool) - 1;
3485 output->compositor->output_id_pool |= 1 << output->id;
3488 wl_global_create(c->wl_display, &wl_output_interface, version,
3489 output, bind_output);
3490 wl_signal_emit(&c->output_created_signal, output);
3494 weston_output_transform_coordinate(struct weston_output *output,
3495 wl_fixed_t device_x, wl_fixed_t device_y,
3496 wl_fixed_t *x, wl_fixed_t *y)
3499 wl_fixed_t width, height;
3500 float zoom_scale, zx, zy;
3502 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3503 height = wl_fixed_from_int(output->height * output->current_scale - 1);
3505 switch(output->transform) {
3506 case WL_OUTPUT_TRANSFORM_NORMAL:
3511 case WL_OUTPUT_TRANSFORM_90:
3513 ty = height - device_x;
3515 case WL_OUTPUT_TRANSFORM_180:
3516 tx = width - device_x;
3517 ty = height - device_y;
3519 case WL_OUTPUT_TRANSFORM_270:
3520 tx = width - device_y;
3523 case WL_OUTPUT_TRANSFORM_FLIPPED:
3524 tx = width - device_x;
3527 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3528 tx = width - device_y;
3529 ty = height - device_x;
3531 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3533 ty = height - device_y;
3535 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3541 tx /= output->current_scale;
3542 ty /= output->current_scale;
3544 if (output->zoom.active) {
3545 zoom_scale = output->zoom.spring_z.current;
3546 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3547 output->width / 2.0f *
3548 (zoom_scale + output->zoom.trans_x));
3549 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3550 output->height / 2.0f *
3551 (zoom_scale + output->zoom.trans_y));
3552 tx = wl_fixed_from_double(zx);
3553 ty = wl_fixed_from_double(zy);
3556 *x = tx + wl_fixed_from_int(output->x);
3557 *y = ty + wl_fixed_from_int(output->y);
3561 destroy_viewport(struct wl_resource *resource)
3563 struct weston_surface *surface =
3564 wl_resource_get_user_data(resource);
3566 surface->viewport_resource = NULL;
3567 surface->pending.buffer_viewport.buffer.src_width =
3568 wl_fixed_from_int(-1);
3569 surface->pending.buffer_viewport.surface.width = -1;
3570 surface->pending.buffer_viewport.changed = 1;
3574 viewport_destroy(struct wl_client *client,
3575 struct wl_resource *resource)
3577 wl_resource_destroy(resource);
3581 viewport_set(struct wl_client *client,
3582 struct wl_resource *resource,
3585 wl_fixed_t src_width,
3586 wl_fixed_t src_height,
3590 struct weston_surface *surface =
3591 wl_resource_get_user_data(resource);
3593 assert(surface->viewport_resource != NULL);
3595 if (wl_fixed_to_double(src_width) < 0 ||
3596 wl_fixed_to_double(src_height) < 0) {
3597 wl_resource_post_error(resource,
3598 WL_VIEWPORT_ERROR_BAD_VALUE,
3599 "source dimensions must be non-negative (%fx%f)",
3600 wl_fixed_to_double(src_width),
3601 wl_fixed_to_double(src_height));
3605 if (dst_width <= 0 || dst_height <= 0) {
3606 wl_resource_post_error(resource,
3607 WL_VIEWPORT_ERROR_BAD_VALUE,
3608 "destination dimensions must be positive (%dx%d)",
3609 dst_width, dst_height);
3613 surface->pending.buffer_viewport.buffer.src_x = src_x;
3614 surface->pending.buffer_viewport.buffer.src_y = src_y;
3615 surface->pending.buffer_viewport.buffer.src_width = src_width;
3616 surface->pending.buffer_viewport.buffer.src_height = src_height;
3617 surface->pending.buffer_viewport.surface.width = dst_width;
3618 surface->pending.buffer_viewport.surface.height = dst_height;
3619 surface->pending.buffer_viewport.changed = 1;
3623 viewport_set_source(struct wl_client *client,
3624 struct wl_resource *resource,
3627 wl_fixed_t src_width,
3628 wl_fixed_t src_height)
3630 struct weston_surface *surface =
3631 wl_resource_get_user_data(resource);
3633 assert(surface->viewport_resource != NULL);
3635 if (src_width == wl_fixed_from_int(-1) &&
3636 src_height == wl_fixed_from_int(-1)) {
3637 /* unset source size */
3638 surface->pending.buffer_viewport.buffer.src_width =
3639 wl_fixed_from_int(-1);
3640 surface->pending.buffer_viewport.changed = 1;
3644 if (src_width <= 0 || src_height <= 0) {
3645 wl_resource_post_error(resource,
3646 WL_VIEWPORT_ERROR_BAD_VALUE,
3647 "source size must be positive (%fx%f)",
3648 wl_fixed_to_double(src_width),
3649 wl_fixed_to_double(src_height));
3653 surface->pending.buffer_viewport.buffer.src_x = src_x;
3654 surface->pending.buffer_viewport.buffer.src_y = src_y;
3655 surface->pending.buffer_viewport.buffer.src_width = src_width;
3656 surface->pending.buffer_viewport.buffer.src_height = src_height;
3657 surface->pending.buffer_viewport.changed = 1;
3661 viewport_set_destination(struct wl_client *client,
3662 struct wl_resource *resource,
3666 struct weston_surface *surface =
3667 wl_resource_get_user_data(resource);
3669 assert(surface->viewport_resource != NULL);
3671 if (dst_width == -1 && dst_height == -1) {
3672 /* unset destination size */
3673 surface->pending.buffer_viewport.surface.width = -1;
3674 surface->pending.buffer_viewport.changed = 1;
3678 if (dst_width <= 0 || dst_height <= 0) {
3679 wl_resource_post_error(resource,
3680 WL_VIEWPORT_ERROR_BAD_VALUE,
3681 "destination size must be positive (%dx%d)",
3682 dst_width, dst_height);
3686 surface->pending.buffer_viewport.surface.width = dst_width;
3687 surface->pending.buffer_viewport.surface.height = dst_height;
3688 surface->pending.buffer_viewport.changed = 1;
3691 static const struct wl_viewport_interface viewport_interface = {
3694 viewport_set_source,
3695 viewport_set_destination
3699 scaler_destroy(struct wl_client *client,
3700 struct wl_resource *resource)
3702 wl_resource_destroy(resource);
3706 scaler_get_viewport(struct wl_client *client,
3707 struct wl_resource *scaler,
3709 struct wl_resource *surface_resource)
3711 int version = wl_resource_get_version(scaler);
3712 struct weston_surface *surface =
3713 wl_resource_get_user_data(surface_resource);
3714 struct wl_resource *resource;
3716 if (surface->viewport_resource) {
3717 wl_resource_post_error(scaler,
3718 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3719 "a viewport for that surface already exists");
3723 resource = wl_resource_create(client, &wl_viewport_interface,
3725 if (resource == NULL) {
3726 wl_client_post_no_memory(client);
3730 wl_resource_set_implementation(resource, &viewport_interface,
3731 surface, destroy_viewport);
3733 surface->viewport_resource = resource;
3736 static const struct wl_scaler_interface scaler_interface = {
3742 bind_scaler(struct wl_client *client,
3743 void *data, uint32_t version, uint32_t id)
3745 struct wl_resource *resource;
3747 resource = wl_resource_create(client, &wl_scaler_interface,
3748 MIN(version, 2), id);
3749 if (resource == NULL) {
3750 wl_client_post_no_memory(client);
3754 wl_resource_set_implementation(resource, &scaler_interface,
3759 compositor_bind(struct wl_client *client,
3760 void *data, uint32_t version, uint32_t id)
3762 struct weston_compositor *compositor = data;
3763 struct wl_resource *resource;
3765 resource = wl_resource_create(client, &wl_compositor_interface,
3766 MIN(version, 3), id);
3767 if (resource == NULL) {
3768 wl_client_post_no_memory(client);
3772 wl_resource_set_implementation(resource, &compositor_interface,
3779 struct utsname usys;
3783 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3784 usys.version, usys.machine);
3788 weston_environment_get_fd(const char *env)
3796 if (!weston_strtoi(e, NULL, 0, &fd))
3799 flags = fcntl(fd, F_GETFD);
3803 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3810 weston_compositor_init(struct weston_compositor *ec,
3811 struct wl_display *display,
3812 int *argc, char *argv[],
3813 struct weston_config *config)
3815 struct wl_event_loop *loop;
3816 struct xkb_rule_names xkb_names;
3817 struct weston_config_section *s;
3819 ec->config = config;
3820 ec->wl_display = display;
3821 wl_signal_init(&ec->destroy_signal);
3822 wl_signal_init(&ec->create_surface_signal);
3823 wl_signal_init(&ec->activate_signal);
3824 wl_signal_init(&ec->transform_signal);
3825 wl_signal_init(&ec->kill_signal);
3826 wl_signal_init(&ec->idle_signal);
3827 wl_signal_init(&ec->wake_signal);
3828 wl_signal_init(&ec->show_input_panel_signal);
3829 wl_signal_init(&ec->hide_input_panel_signal);
3830 wl_signal_init(&ec->update_input_panel_signal);
3831 wl_signal_init(&ec->seat_created_signal);
3832 wl_signal_init(&ec->output_created_signal);
3833 wl_signal_init(&ec->output_destroyed_signal);
3834 wl_signal_init(&ec->output_moved_signal);
3835 wl_signal_init(&ec->session_signal);
3836 ec->session_active = 1;
3838 ec->output_id_pool = 0;
3840 if (!wl_global_create(display, &wl_compositor_interface, 3,
3841 ec, compositor_bind))
3844 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3845 ec, bind_subcompositor))
3848 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
3852 wl_list_init(&ec->view_list);
3853 wl_list_init(&ec->plane_list);
3854 wl_list_init(&ec->layer_list);
3855 wl_list_init(&ec->seat_list);
3856 wl_list_init(&ec->output_list);
3857 wl_list_init(&ec->key_binding_list);
3858 wl_list_init(&ec->modifier_binding_list);
3859 wl_list_init(&ec->button_binding_list);
3860 wl_list_init(&ec->touch_binding_list);
3861 wl_list_init(&ec->axis_binding_list);
3862 wl_list_init(&ec->debug_binding_list);
3864 weston_plane_init(&ec->primary_plane, ec, 0, 0);
3865 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
3867 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3868 weston_config_section_get_string(s, "keymap_rules",
3869 (char **) &xkb_names.rules, NULL);
3870 weston_config_section_get_string(s, "keymap_model",
3871 (char **) &xkb_names.model, NULL);
3872 weston_config_section_get_string(s, "keymap_layout",
3873 (char **) &xkb_names.layout, NULL);
3874 weston_config_section_get_string(s, "keymap_variant",
3875 (char **) &xkb_names.variant, NULL);
3876 weston_config_section_get_string(s, "keymap_options",
3877 (char **) &xkb_names.options, NULL);
3879 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3882 weston_config_section_get_int(s, "repeat-rate",
3883 &ec->kb_repeat_rate, 40);
3884 weston_config_section_get_int(s, "repeat-delay",
3885 &ec->kb_repeat_delay, 400);
3887 text_backend_init(ec);
3889 wl_data_device_manager_init(ec->wl_display);
3891 wl_display_init_shm(display);
3893 loop = wl_display_get_event_loop(ec->wl_display);
3894 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3895 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3897 ec->input_loop = wl_event_loop_create();
3899 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3900 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3902 weston_compositor_schedule_repaint(ec);
3908 weston_compositor_shutdown(struct weston_compositor *ec)
3910 struct weston_output *output, *next;
3912 wl_event_source_remove(ec->idle_source);
3913 if (ec->input_loop_source)
3914 wl_event_source_remove(ec->input_loop_source);
3916 /* Destroy all outputs associated with this compositor */
3917 wl_list_for_each_safe(output, next, &ec->output_list, link)
3918 output->destroy(output);
3921 ec->renderer->destroy(ec);
3923 weston_binding_list_destroy_all(&ec->key_binding_list);
3924 weston_binding_list_destroy_all(&ec->button_binding_list);
3925 weston_binding_list_destroy_all(&ec->touch_binding_list);
3926 weston_binding_list_destroy_all(&ec->axis_binding_list);
3927 weston_binding_list_destroy_all(&ec->debug_binding_list);
3929 weston_plane_release(&ec->primary_plane);
3931 wl_event_loop_destroy(ec->input_loop);
3933 weston_config_destroy(ec->config);
3937 weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3938 const struct weston_pointer_grab_interface *interface)
3940 struct weston_seat *seat;
3942 ec->default_pointer_grab = interface;
3943 wl_list_for_each(seat, &ec->seat_list, link) {
3944 if (seat->pointer) {
3945 weston_pointer_set_default_grab(seat->pointer,
3952 weston_version(int *major, int *minor, int *micro)
3954 *major = WESTON_VERSION_MAJOR;
3955 *minor = WESTON_VERSION_MINOR;
3956 *micro = WESTON_VERSION_MICRO;
3959 static const struct {
3960 uint32_t bit; /* enum weston_capability */
3962 } capability_strings[] = {
3963 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
3964 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
3968 weston_compositor_log_capabilities(struct weston_compositor *compositor)
3973 weston_log("Compositor capabilities:\n");
3974 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3975 yes = compositor->capabilities & capability_strings[i].bit;
3976 weston_log_continue(STAMP_SPACE "%s %s\n",
3977 capability_strings[i].desc,
3978 yes ? "yes" : "no");
3982 static int on_term_signal(int signal_number, void *data)
3984 struct wl_display *display = data;
3986 weston_log("caught signal %d\n", signal_number);
3987 wl_display_terminate(display);
3992 #ifdef HAVE_LIBUNWIND
3995 print_backtrace(void)
3997 unw_cursor_t cursor;
3998 unw_context_t context;
4000 unw_proc_info_t pip;
4003 const char *filename;
4006 pip.unwind_info = NULL;
4007 ret = unw_getcontext(&context);
4009 weston_log("unw_getcontext: %d\n", ret);
4013 ret = unw_init_local(&cursor, &context);
4015 weston_log("unw_init_local: %d\n", ret);
4019 ret = unw_step(&cursor);
4021 ret = unw_get_proc_info(&cursor, &pip);
4023 weston_log("unw_get_proc_info: %d\n", ret);
4027 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4028 if (ret && ret != -UNW_ENOMEM) {
4029 if (ret != -UNW_EUNSPEC)
4030 weston_log("unw_get_proc_name: %d\n", ret);
4035 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4037 filename = dlinfo.dli_fname;
4041 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4042 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4044 ret = unw_step(&cursor);
4046 weston_log("unw_step: %d\n", ret);
4053 print_backtrace(void)
4059 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4060 for (i = 0; i < count; i++) {
4061 dladdr(buffer[i], &info);
4062 weston_log(" [%016lx] %s (%s)\n",
4064 info.dli_sname ? info.dli_sname : "--",
4072 on_caught_signal(int s, siginfo_t *siginfo, void *context)
4074 /* This signal handler will do a best-effort backtrace, and
4075 * then call the backend restore function, which will switch
4076 * back to the vt we launched from or ungrab X etc and then
4077 * raise SIGTRAP. If we run weston under gdb from X or a
4078 * different vt, and tell gdb "handle *s* nostop", this
4079 * will allow weston to switch back to gdb on crash and then
4080 * gdb will catch the crash with SIGTRAP.*/
4082 weston_log("caught signal: %d\n", s);
4086 segv_compositor->restore(segv_compositor);
4092 weston_load_module(const char *name, const char *entrypoint)
4094 char path[PATH_MAX];
4095 void *module, *init;
4101 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4103 snprintf(path, sizeof path, "%s", name);
4105 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4107 weston_log("Module '%s' already loaded\n", path);
4112 weston_log("Loading module '%s'\n", path);
4113 module = dlopen(path, RTLD_NOW);
4115 weston_log("Failed to load module: %s\n", dlerror());
4119 init = dlsym(module, entrypoint);
4121 weston_log("Failed to lookup init function: %s\n", dlerror());
4130 load_modules(struct weston_compositor *ec, const char *modules,
4131 int *argc, char *argv[])
4133 const char *p, *end;
4135 int (*module_init)(struct weston_compositor *ec,
4136 int *argc, char *argv[]);
4138 if (modules == NULL)
4143 end = strchrnul(p, ',');
4144 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
4145 module_init = weston_load_module(buffer, "module_init");
4147 module_init(ec, argc, argv);
4157 static const char xdg_error_message[] =
4158 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4160 static const char xdg_wrong_message[] =
4161 "fatal: environment variable XDG_RUNTIME_DIR\n"
4162 "is set to \"%s\", which is not a directory.\n";
4164 static const char xdg_wrong_mode_message[] =
4165 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
4166 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4167 "and must be owned by the user (current owner is UID %d).\n";
4169 static const char xdg_detail_message[] =
4170 "Refer to your distribution on how to get it, or\n"
4171 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4172 "on how to implement it.\n";
4175 verify_xdg_runtime_dir(void)
4177 char *dir = getenv("XDG_RUNTIME_DIR");
4181 weston_log(xdg_error_message);
4182 weston_log_continue(xdg_detail_message);
4186 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4187 weston_log(xdg_wrong_message, dir);
4188 weston_log_continue(xdg_detail_message);
4192 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4193 weston_log(xdg_wrong_mode_message,
4194 dir, s.st_mode & 0777, s.st_uid);
4195 weston_log_continue(xdg_detail_message);
4200 usage(int error_code)
4203 "Usage: weston [OPTIONS]\n\n"
4204 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4205 "Weston supports multiple backends, and depending on which backend is in use\n"
4206 "different options will be accepted.\n\n"
4210 " --version\t\tPrint weston version\n"
4211 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
4212 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4213 "\t\t\t\twayland-backend.so\n"
4214 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
4215 " -S, --socket=NAME\tName of socket to listen on\n"
4216 " -i, --idle-time=SECS\tIdle time in seconds\n"
4217 " --modules\t\tLoad the comma-separated list of modules\n"
4218 " --log==FILE\t\tLog to the given file\n"
4219 " --no-config\t\tDo not read weston.ini\n"
4220 " -h, --help\t\tThis help message\n\n");
4223 "Options for drm-backend.so:\n\n"
4224 " --connector=ID\tBring up only this connector\n"
4225 " --seat=SEAT\t\tThe seat that weston should run on\n"
4226 " --tty=TTY\t\tThe tty to use\n"
4227 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
4228 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4231 "Options for fbdev-backend.so:\n\n"
4232 " --tty=TTY\t\tThe tty to use\n"
4233 " --device=DEVICE\tThe framebuffer device to use\n\n");
4236 "Options for x11-backend.so:\n\n"
4237 " --width=WIDTH\t\tWidth of X window\n"
4238 " --height=HEIGHT\tHeight of X window\n"
4239 " --fullscreen\t\tRun in fullscreen mode\n"
4240 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
4241 " --output-count=COUNT\tCreate multiple outputs\n"
4242 " --no-input\t\tDont create input devices\n\n");
4245 "Options for wayland-backend.so:\n\n"
4246 " --width=WIDTH\t\tWidth of Wayland surface\n"
4247 " --height=HEIGHT\tHeight of Wayland surface\n"
4248 " --scale=SCALE\tScale factor of ouput\n"
4249 " --fullscreen\t\tRun in fullscreen mode\n"
4250 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
4251 " --output-count=COUNT\tCreate multiple outputs\n"
4252 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
4253 " --display=DISPLAY\tWayland display to connect to\n\n");
4255 #if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4257 "Options for rpi-backend.so:\n\n"
4258 " --tty=TTY\t\tThe tty to use\n"
4259 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4260 " --transform=TR\tThe output transformation, TR is one of:\n"
4261 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4262 " --opaque-regions\tEnable support for opaque regions, can be "
4263 "very slow without support in the GPU firmware.\n"
4267 #if defined(BUILD_RDP_COMPOSITOR)
4269 "Options for rdp-backend.so:\n\n"
4270 " --width=WIDTH\t\tWidth of desktop\n"
4271 " --height=HEIGHT\tHeight of desktop\n"
4272 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4273 " --address=ADDR\tThe address to bind\n"
4274 " --port=PORT\tThe port to listen on\n"
4275 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4276 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4277 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4278 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4288 struct sigaction action;
4290 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4291 action.sa_sigaction = on_caught_signal;
4292 sigemptyset(&action.sa_mask);
4293 sigaction(SIGSEGV, &action, NULL);
4294 sigaction(SIGABRT, &action, NULL);
4298 handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4300 struct wl_client *client = data;
4302 weston_log("Primary client died. Closing...\n");
4304 wl_display_terminate(wl_client_get_display(client));
4308 weston_choose_default_backend(void)
4310 char *backend = NULL;
4312 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4313 backend = strdup("wayland-backend.so");
4314 else if (getenv("DISPLAY"))
4315 backend = strdup("x11-backend.so");
4317 backend = strdup(WESTON_NATIVE_BACKEND);
4323 weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4326 if (wl_display_add_socket(display, socket_name)) {
4327 weston_log("fatal: failed to add socket: %m\n");
4331 socket_name = wl_display_add_socket_auto(display);
4333 weston_log("fatal: failed to add socket: %m\n");
4338 setenv("WAYLAND_DISPLAY", socket_name, 1);
4343 int main(int argc, char *argv[])
4345 int ret = EXIT_SUCCESS;
4346 struct wl_display *display;
4347 struct weston_compositor *ec;
4348 struct wl_event_source *signals[4];
4349 struct wl_event_loop *loop;
4350 struct weston_compositor
4351 *(*backend_init)(struct wl_display *display,
4352 int *argc, char *argv[],
4353 struct weston_config *config);
4355 char *backend = NULL;
4357 char *modules = NULL;
4358 char *option_modules = NULL;
4360 char *server_socket = NULL;
4361 int32_t idle_time = 300;
4363 char *socket_name = NULL;
4364 int32_t version = 0;
4365 int32_t noconfig = 0;
4367 struct weston_config *config = NULL;
4368 struct weston_config_section *section;
4369 struct wl_client *primary_client;
4370 struct wl_listener primary_client_destroyed;
4371 struct weston_seat *seat;
4372 const char *conf_file = NULL;
4374 const struct weston_option core_options[] = {
4375 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4376 { WESTON_OPTION_STRING, "shell", 0, &shell },
4377 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4378 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
4379 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
4380 { WESTON_OPTION_STRING, "log", 0, &log },
4381 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
4382 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
4383 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
4386 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
4389 usage(EXIT_SUCCESS);
4392 printf(PACKAGE_STRING "\n");
4393 return EXIT_SUCCESS;
4396 weston_log_file_open(log);
4400 STAMP_SPACE "Bug reports to: %s\n"
4401 STAMP_SPACE "Build: %s\n",
4402 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
4406 verify_xdg_runtime_dir();
4408 display = wl_display_create();
4410 loop = wl_display_get_event_loop(display);
4411 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4413 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4415 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4418 wl_list_init(&child_process_list);
4419 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4422 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4428 conf_file = getenv("WESTON_CONFIG");
4429 if (conf_file == NULL)
4430 conf_file = "weston.ini";
4431 config = weston_config_parse(conf_file);
4432 if (config != NULL) {
4433 weston_log("Using config file '%s'\n",
4434 weston_config_get_full_path(config));
4436 weston_log("Starting with no config file.\n");
4438 section = weston_config_get_section(config, "core", NULL, NULL);
4441 weston_config_section_get_string(section, "backend", &backend,
4444 backend = weston_choose_default_backend();
4447 backend_init = weston_load_module(backend, "backend_init");
4448 if (!backend_init) {
4453 ec = backend_init(display, &argc, argv, config);
4455 weston_log("fatal: failed to create compositor\n");
4461 segv_compositor = ec;
4463 ec->idle_time = idle_time;
4464 ec->default_pointer_grab = NULL;
4466 for (i = 1; i < argc; i++)
4467 weston_log("fatal: unhandled option: %s\n", argv[i]);
4473 weston_compositor_log_capabilities(ec);
4475 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4476 if (server_socket) {
4477 weston_log("Running with single client\n");
4478 if (!weston_strtoi(server_socket, NULL, 0, &fd))
4485 primary_client = wl_client_create(display, fd);
4486 if (!primary_client) {
4487 weston_log("fatal: failed to add client: %m\n");
4491 primary_client_destroyed.notify =
4492 handle_primary_client_destroyed;
4493 wl_client_add_destroy_listener(primary_client,
4494 &primary_client_destroyed);
4495 } else if (weston_create_listening_socket(display, socket_name)) {
4501 weston_config_section_get_string(section, "shell", &shell,
4502 "desktop-shell.so");
4504 if (load_modules(ec, shell, &argc, argv) < 0)
4507 weston_config_section_get_string(section, "modules", &modules, "");
4508 if (load_modules(ec, modules, &argc, argv) < 0)
4511 if (load_modules(ec, option_modules, &argc, argv) < 0)
4514 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4515 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4517 wl_list_for_each(seat, &ec->seat_list, link) {
4519 weston_keyboard_set_locks(seat->keyboard,
4525 weston_compositor_wake(ec);
4527 wl_display_run(display);
4530 /* prevent further rendering while shutting down */
4531 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
4533 wl_signal_emit(&ec->destroy_signal, ec);
4535 weston_compositor_xkb_destroy(ec);
4540 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4542 wl_event_source_remove(signals[i]);
4544 wl_display_destroy(display);
4546 weston_log_file_close();
4551 free(option_modules);