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 "git-version.h"
62 static struct wl_list child_process_list;
63 static struct weston_compositor *segv_compositor;
66 sigchld_handler(int signal_number, void *data)
68 struct weston_process *p;
72 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
73 wl_list_for_each(p, &child_process_list, link) {
78 if (&p->link == &child_process_list) {
79 weston_log("unknown child process exited\n");
83 wl_list_remove(&p->link);
84 p->cleanup(p, status);
87 if (pid < 0 && errno != ECHILD)
88 weston_log("waitpid error %m\n");
94 weston_output_transform_scale_init(struct weston_output *output,
95 uint32_t transform, uint32_t scale);
98 weston_compositor_build_view_list(struct weston_compositor *compositor);
101 weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
102 int32_t scale, enum weston_mode_switch_op op)
104 struct weston_seat *seat;
105 struct wl_resource *resource;
106 pixman_region32_t old_output_region;
107 int ret, notify_mode_changed, notify_scale_changed;
108 int temporary_mode, temporary_scale;
110 if (!output->switch_mode)
113 temporary_mode = (output->original_mode != 0);
114 temporary_scale = (output->current_scale != output->original_scale);
117 notify_mode_changed = 0;
118 notify_scale_changed = 0;
120 case WESTON_MODE_SWITCH_SET_NATIVE:
121 output->native_mode = mode;
122 if (!temporary_mode) {
123 notify_mode_changed = 1;
124 ret = output->switch_mode(output, mode);
129 output->native_scale = scale;
131 notify_scale_changed = 1;
133 case WESTON_MODE_SWITCH_SET_TEMPORARY:
135 output->original_mode = output->native_mode;
136 if (!temporary_scale)
137 output->original_scale = output->native_scale;
139 ret = output->switch_mode(output, mode);
143 output->current_scale = scale;
145 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
146 if (!temporary_mode) {
147 weston_log("already in the native mode\n");
151 notify_mode_changed = (output->original_mode != output->native_mode);
153 ret = output->switch_mode(output, mode);
157 if (output->original_scale != output->native_scale) {
158 notify_scale_changed = 1;
159 scale = output->native_scale;
160 output->original_scale = scale;
162 output->original_mode = 0;
164 output->current_scale = output->native_scale;
167 weston_log("unknown weston_switch_mode_op %d\n", op);
171 pixman_region32_init(&old_output_region);
172 pixman_region32_copy(&old_output_region, &output->region);
174 /* Update output region and transformation matrix */
175 weston_output_transform_scale_init(output, output->transform, output->current_scale);
177 pixman_region32_init(&output->previous_damage);
178 pixman_region32_init_rect(&output->region, output->x, output->y,
179 output->width, output->height);
181 weston_output_update_matrix(output);
183 /* If a pointer falls outside the outputs new geometry, move it to its
184 * lower-right corner */
185 wl_list_for_each(seat, &output->compositor->seat_list, link) {
186 struct weston_pointer *pointer = seat->pointer;
192 x = wl_fixed_to_int(pointer->x);
193 y = wl_fixed_to_int(pointer->y);
195 if (!pixman_region32_contains_point(&old_output_region,
197 pixman_region32_contains_point(&output->region,
201 if (x >= output->x + output->width)
202 x = output->x + output->width - 1;
203 if (y >= output->y + output->height)
204 y = output->y + output->height - 1;
206 pointer->x = wl_fixed_from_int(x);
207 pointer->y = wl_fixed_from_int(y);
210 pixman_region32_fini(&old_output_region);
212 /* notify clients of the changes */
213 if (notify_mode_changed || notify_scale_changed) {
214 wl_resource_for_each(resource, &output->resource_list) {
215 if(notify_mode_changed) {
216 wl_output_send_mode(resource,
217 mode->flags | WL_OUTPUT_MODE_CURRENT,
223 if (notify_scale_changed)
224 wl_output_send_scale(resource, scale);
226 if (wl_resource_get_version(resource) >= 2)
227 wl_output_send_done(resource);
235 weston_watch_process(struct weston_process *process)
237 wl_list_insert(&child_process_list, &process->link);
241 child_client_exec(int sockfd, const char *path)
247 /* do not give our signal mask to the new process */
248 sigfillset(&allsigs);
249 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
251 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
252 if (seteuid(getuid()) == -1) {
253 weston_log("compositor: failed seteuid\n");
257 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
258 * non-CLOEXEC fd to pass through exec. */
259 clientfd = dup(sockfd);
260 if (clientfd == -1) {
261 weston_log("compositor: dup failed: %m\n");
265 snprintf(s, sizeof s, "%d", clientfd);
266 setenv("WAYLAND_SOCKET", s, 1);
268 if (execl(path, path, NULL) < 0)
269 weston_log("compositor: executing '%s' failed: %m\n",
273 WL_EXPORT struct wl_client *
274 weston_client_launch(struct weston_compositor *compositor,
275 struct weston_process *proc,
277 weston_process_cleanup_func_t cleanup)
281 struct wl_client *client;
283 weston_log("launching '%s'\n", path);
285 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
286 weston_log("weston_client_launch: "
287 "socketpair failed while launching '%s': %m\n",
296 weston_log("weston_client_launch: "
297 "fork failed while launching '%s': %m\n", path);
302 child_client_exec(sv[1], path);
308 client = wl_client_create(compositor->wl_display, sv[0]);
311 weston_log("weston_client_launch: "
312 "wl_client_create failed while launching '%s'.\n",
318 proc->cleanup = cleanup;
319 weston_watch_process(proc);
324 struct process_info {
325 struct weston_process proc;
330 process_handle_sigchld(struct weston_process *process, int status)
332 struct process_info *pinfo =
333 container_of(process, struct process_info, proc);
336 * There are no guarantees whether this runs before or after
337 * the wl_client destructor.
340 if (WIFEXITED(status)) {
341 weston_log("%s exited with status %d\n", pinfo->path,
342 WEXITSTATUS(status));
343 } else if (WIFSIGNALED(status)) {
344 weston_log("%s died on signal %d\n", pinfo->path,
347 weston_log("%s disappeared\n", pinfo->path);
354 WL_EXPORT struct wl_client *
355 weston_client_start(struct weston_compositor *compositor, const char *path)
357 struct process_info *pinfo;
358 struct wl_client *client;
360 pinfo = zalloc(sizeof *pinfo);
364 pinfo->path = strdup(path);
368 client = weston_client_launch(compositor, &pinfo->proc, path,
369 process_handle_sigchld);
385 region_init_infinite(pixman_region32_t *region)
387 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
388 UINT32_MAX, UINT32_MAX);
391 static struct weston_subsurface *
392 weston_surface_to_subsurface(struct weston_surface *surface);
394 WL_EXPORT struct weston_view *
395 weston_view_create(struct weston_surface *surface)
397 struct weston_view *view;
399 view = calloc(1, sizeof *view);
403 view->surface = surface;
405 /* Assign to surface */
406 wl_list_insert(&surface->views, &view->surface_link);
408 wl_signal_init(&view->destroy_signal);
409 wl_list_init(&view->link);
410 wl_list_init(&view->layer_link.link);
413 view->layer_link.layer = NULL;
414 view->parent_view = NULL;
416 pixman_region32_init(&view->clip);
417 pixman_region32_init(&view->transform.masked_boundingbox);
418 pixman_region32_init(&view->transform.masked_opaque);
421 pixman_region32_init(&view->transform.opaque);
423 wl_list_init(&view->geometry.transformation_list);
424 wl_list_insert(&view->geometry.transformation_list,
425 &view->transform.position.link);
426 weston_matrix_init(&view->transform.position.matrix);
427 wl_list_init(&view->geometry.child_list);
428 pixman_region32_init(&view->transform.boundingbox);
429 view->transform.dirty = 1;
436 struct weston_frame_callback {
437 struct wl_resource *resource;
442 surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
444 struct weston_surface_state *state =
445 container_of(listener, struct weston_surface_state,
446 buffer_destroy_listener);
448 state->buffer = NULL;
452 weston_surface_state_init(struct weston_surface_state *state)
454 state->newly_attached = 0;
455 state->buffer = NULL;
456 state->buffer_destroy_listener.notify =
457 surface_state_handle_buffer_destroy;
461 pixman_region32_init(&state->damage);
462 pixman_region32_init(&state->opaque);
463 region_init_infinite(&state->input);
465 wl_list_init(&state->frame_callback_list);
467 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
468 state->buffer_viewport.buffer.scale = 1;
469 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
470 state->buffer_viewport.surface.width = -1;
471 state->buffer_viewport.changed = 0;
475 weston_surface_state_fini(struct weston_surface_state *state)
477 struct weston_frame_callback *cb, *next;
479 wl_list_for_each_safe(cb, next,
480 &state->frame_callback_list, link)
481 wl_resource_destroy(cb->resource);
483 pixman_region32_fini(&state->input);
484 pixman_region32_fini(&state->opaque);
485 pixman_region32_fini(&state->damage);
488 wl_list_remove(&state->buffer_destroy_listener.link);
489 state->buffer = NULL;
493 weston_surface_state_set_buffer(struct weston_surface_state *state,
494 struct weston_buffer *buffer)
496 if (state->buffer == buffer)
500 wl_list_remove(&state->buffer_destroy_listener.link);
501 state->buffer = buffer;
503 wl_signal_add(&state->buffer->destroy_signal,
504 &state->buffer_destroy_listener);
507 WL_EXPORT struct weston_surface *
508 weston_surface_create(struct weston_compositor *compositor)
510 struct weston_surface *surface;
512 surface = calloc(1, sizeof *surface);
516 wl_signal_init(&surface->destroy_signal);
518 surface->resource = NULL;
520 surface->compositor = compositor;
521 surface->ref_count = 1;
523 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
524 surface->buffer_viewport.buffer.scale = 1;
525 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
526 surface->buffer_viewport.surface.width = -1;
528 weston_surface_state_init(&surface->pending);
530 surface->output = NULL;
532 surface->viewport_resource = NULL;
534 pixman_region32_init(&surface->damage);
535 pixman_region32_init(&surface->opaque);
536 region_init_infinite(&surface->input);
538 wl_list_init(&surface->views);
540 wl_list_init(&surface->frame_callback_list);
542 wl_list_init(&surface->subsurface_list);
543 wl_list_init(&surface->subsurface_list_pending);
549 weston_surface_set_color(struct weston_surface *surface,
550 float red, float green, float blue, float alpha)
552 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
556 weston_view_to_global_float(struct weston_view *view,
557 float sx, float sy, float *x, float *y)
559 if (view->transform.enabled) {
560 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
562 weston_matrix_transform(&view->transform.matrix, &v);
564 if (fabsf(v.f[3]) < 1e-6) {
565 weston_log("warning: numerical instability in "
566 "%s(), divisor = %g\n", __func__,
573 *x = v.f[0] / v.f[3];
574 *y = v.f[1] / v.f[3];
576 *x = sx + view->geometry.x;
577 *y = sy + view->geometry.y;
582 weston_transformed_coord(int width, int height,
583 enum wl_output_transform transform,
585 float sx, float sy, float *bx, float *by)
588 case WL_OUTPUT_TRANSFORM_NORMAL:
593 case WL_OUTPUT_TRANSFORM_FLIPPED:
597 case WL_OUTPUT_TRANSFORM_90:
601 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
605 case WL_OUTPUT_TRANSFORM_180:
609 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
613 case WL_OUTPUT_TRANSFORM_270:
617 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
627 WL_EXPORT pixman_box32_t
628 weston_transformed_rect(int width, int height,
629 enum wl_output_transform transform,
633 float x1, x2, y1, y2;
637 weston_transformed_coord(width, height, transform, scale,
638 rect.x1, rect.y1, &x1, &y1);
639 weston_transformed_coord(width, height, transform, scale,
640 rect.x2, rect.y2, &x2, &y2);
662 weston_transformed_region(int width, int height,
663 enum wl_output_transform transform,
665 pixman_region32_t *src, pixman_region32_t *dest)
667 pixman_box32_t *src_rects, *dest_rects;
670 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
672 pixman_region32_copy(dest, src);
676 src_rects = pixman_region32_rectangles(src, &nrects);
677 dest_rects = malloc(nrects * sizeof(*dest_rects));
681 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
682 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
684 for (i = 0; i < nrects; i++) {
687 case WL_OUTPUT_TRANSFORM_NORMAL:
688 dest_rects[i].x1 = src_rects[i].x1;
689 dest_rects[i].y1 = src_rects[i].y1;
690 dest_rects[i].x2 = src_rects[i].x2;
691 dest_rects[i].y2 = src_rects[i].y2;
693 case WL_OUTPUT_TRANSFORM_90:
694 dest_rects[i].x1 = height - src_rects[i].y2;
695 dest_rects[i].y1 = src_rects[i].x1;
696 dest_rects[i].x2 = height - src_rects[i].y1;
697 dest_rects[i].y2 = src_rects[i].x2;
699 case WL_OUTPUT_TRANSFORM_180:
700 dest_rects[i].x1 = width - src_rects[i].x2;
701 dest_rects[i].y1 = height - src_rects[i].y2;
702 dest_rects[i].x2 = width - src_rects[i].x1;
703 dest_rects[i].y2 = height - src_rects[i].y1;
705 case WL_OUTPUT_TRANSFORM_270:
706 dest_rects[i].x1 = src_rects[i].y1;
707 dest_rects[i].y1 = width - src_rects[i].x2;
708 dest_rects[i].x2 = src_rects[i].y2;
709 dest_rects[i].y2 = width - src_rects[i].x1;
711 case WL_OUTPUT_TRANSFORM_FLIPPED:
712 dest_rects[i].x1 = width - src_rects[i].x2;
713 dest_rects[i].y1 = src_rects[i].y1;
714 dest_rects[i].x2 = width - src_rects[i].x1;
715 dest_rects[i].y2 = src_rects[i].y2;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
718 dest_rects[i].x1 = height - src_rects[i].y2;
719 dest_rects[i].y1 = width - src_rects[i].x2;
720 dest_rects[i].x2 = height - src_rects[i].y1;
721 dest_rects[i].y2 = width - src_rects[i].x1;
723 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
724 dest_rects[i].x1 = src_rects[i].x1;
725 dest_rects[i].y1 = height - src_rects[i].y2;
726 dest_rects[i].x2 = src_rects[i].x2;
727 dest_rects[i].y2 = height - src_rects[i].y1;
729 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
730 dest_rects[i].x1 = src_rects[i].y1;
731 dest_rects[i].y1 = src_rects[i].x1;
732 dest_rects[i].x2 = src_rects[i].y2;
733 dest_rects[i].y2 = src_rects[i].x2;
740 for (i = 0; i < nrects; i++) {
741 dest_rects[i].x1 *= scale;
742 dest_rects[i].x2 *= scale;
743 dest_rects[i].y1 *= scale;
744 dest_rects[i].y2 *= scale;
748 pixman_region32_clear(dest);
749 pixman_region32_init_rects(dest, dest_rects, nrects);
754 scaler_surface_to_buffer(struct weston_surface *surface,
755 float sx, float sy, float *bx, float *by)
757 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
758 double src_width, src_height;
761 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
762 if (vp->surface.width == -1) {
770 src_width = surface->width_from_buffer;
771 src_height = surface->height_from_buffer;
773 src_x = wl_fixed_to_double(vp->buffer.src_x);
774 src_y = wl_fixed_to_double(vp->buffer.src_y);
775 src_width = wl_fixed_to_double(vp->buffer.src_width);
776 src_height = wl_fixed_to_double(vp->buffer.src_height);
779 *bx = sx * src_width / surface->width + src_x;
780 *by = sy * src_height / surface->height + src_y;
784 weston_surface_to_buffer_float(struct weston_surface *surface,
785 float sx, float sy, float *bx, float *by)
787 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
789 /* first transform coordinates if the scaler is set */
790 scaler_surface_to_buffer(surface, sx, sy, bx, by);
792 weston_transformed_coord(surface->width_from_buffer,
793 surface->height_from_buffer,
794 vp->buffer.transform, vp->buffer.scale,
799 weston_surface_to_buffer(struct weston_surface *surface,
800 int sx, int sy, int *bx, int *by)
804 weston_surface_to_buffer_float(surface,
811 WL_EXPORT pixman_box32_t
812 weston_surface_to_buffer_rect(struct weston_surface *surface,
815 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
818 /* first transform box coordinates if the scaler is set */
819 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
820 rect.x1 = floorf(xf);
821 rect.y1 = floorf(yf);
823 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
824 rect.x2 = floorf(xf);
825 rect.y2 = floorf(yf);
827 return weston_transformed_rect(surface->width_from_buffer,
828 surface->height_from_buffer,
829 vp->buffer.transform, vp->buffer.scale,
834 weston_view_move_to_plane(struct weston_view *view,
835 struct weston_plane *plane)
837 if (view->plane == plane)
840 weston_view_damage_below(view);
842 weston_surface_damage(view->surface);
846 weston_view_damage_below(struct weston_view *view)
848 pixman_region32_t damage;
850 pixman_region32_init(&damage);
851 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
854 pixman_region32_union(&view->plane->damage,
855 &view->plane->damage, &damage);
856 pixman_region32_fini(&damage);
857 weston_view_schedule_repaint(view);
861 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
863 uint32_t different = es->output_mask ^ mask;
864 uint32_t entered = mask & different;
865 uint32_t left = es->output_mask & different;
866 struct weston_output *output;
867 struct wl_resource *resource = NULL;
868 struct wl_client *client;
870 es->output_mask = mask;
871 if (es->resource == NULL)
876 client = wl_resource_get_client(es->resource);
878 wl_list_for_each(output, &es->compositor->output_list, link) {
879 if (1 << output->id & different)
881 wl_resource_find_for_client(&output->resource_list,
883 if (resource == NULL)
885 if (1 << output->id & entered)
886 wl_surface_send_enter(es->resource, resource);
887 if (1 << output->id & left)
888 wl_surface_send_leave(es->resource, resource);
894 weston_surface_assign_output(struct weston_surface *es)
896 struct weston_output *new_output;
897 struct weston_view *view;
898 pixman_region32_t region;
899 uint32_t max, area, mask;
905 pixman_region32_init(®ion);
906 wl_list_for_each(view, &es->views, surface_link) {
910 pixman_region32_intersect(®ion, &view->transform.boundingbox,
911 &view->output->region);
913 e = pixman_region32_extents(®ion);
914 area = (e->x2 - e->x1) * (e->y2 - e->y1);
916 mask |= view->output_mask;
919 new_output = view->output;
923 pixman_region32_fini(®ion);
925 es->output = new_output;
926 weston_surface_update_output_mask(es, mask);
930 weston_view_assign_output(struct weston_view *ev)
932 struct weston_compositor *ec = ev->surface->compositor;
933 struct weston_output *output, *new_output;
934 pixman_region32_t region;
935 uint32_t max, area, mask;
941 pixman_region32_init(®ion);
942 wl_list_for_each(output, &ec->output_list, link) {
943 pixman_region32_intersect(®ion, &ev->transform.boundingbox,
946 e = pixman_region32_extents(®ion);
947 area = (e->x2 - e->x1) * (e->y2 - e->y1);
950 mask |= 1 << output->id;
957 pixman_region32_fini(®ion);
959 ev->output = new_output;
960 ev->output_mask = mask;
962 weston_surface_assign_output(ev->surface);
966 view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
967 int32_t width, int32_t height,
968 pixman_region32_t *bbox)
970 float min_x = HUGE_VALF, min_y = HUGE_VALF;
971 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
976 { sx + width, sy + height }
981 if (width == 0 || height == 0) {
982 /* avoid rounding empty bbox to 1x1 */
983 pixman_region32_init(bbox);
987 for (i = 0; i < 4; ++i) {
989 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
1000 int_x = floorf(min_x);
1001 int_y = floorf(min_y);
1002 pixman_region32_init_rect(bbox, int_x, int_y,
1003 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
1007 weston_view_update_transform_disable(struct weston_view *view)
1009 view->transform.enabled = 0;
1011 /* round off fractions when not transformed */
1012 view->geometry.x = roundf(view->geometry.x);
1013 view->geometry.y = roundf(view->geometry.y);
1015 /* Otherwise identity matrix, but with x and y translation. */
1016 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1017 view->transform.position.matrix.d[12] = view->geometry.x;
1018 view->transform.position.matrix.d[13] = view->geometry.y;
1020 view->transform.matrix = view->transform.position.matrix;
1022 view->transform.inverse = view->transform.position.matrix;
1023 view->transform.inverse.d[12] = -view->geometry.x;
1024 view->transform.inverse.d[13] = -view->geometry.y;
1026 pixman_region32_init_rect(&view->transform.boundingbox,
1029 view->surface->width,
1030 view->surface->height);
1032 if (view->alpha == 1.0) {
1033 pixman_region32_copy(&view->transform.opaque,
1034 &view->surface->opaque);
1035 pixman_region32_translate(&view->transform.opaque,
1042 weston_view_update_transform_enable(struct weston_view *view)
1044 struct weston_view *parent = view->geometry.parent;
1045 struct weston_matrix *matrix = &view->transform.matrix;
1046 struct weston_matrix *inverse = &view->transform.inverse;
1047 struct weston_transform *tform;
1049 view->transform.enabled = 1;
1051 /* Otherwise identity matrix, but with x and y translation. */
1052 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1053 view->transform.position.matrix.d[12] = view->geometry.x;
1054 view->transform.position.matrix.d[13] = view->geometry.y;
1056 weston_matrix_init(matrix);
1057 wl_list_for_each(tform, &view->geometry.transformation_list, link)
1058 weston_matrix_multiply(matrix, &tform->matrix);
1061 weston_matrix_multiply(matrix, &parent->transform.matrix);
1063 if (weston_matrix_invert(inverse, matrix) < 0) {
1064 /* Oops, bad total transformation, not invertible */
1065 weston_log("error: weston_view %p"
1066 " transformation not invertible.\n", view);
1070 view_compute_bbox(view, 0, 0,
1071 view->surface->width, view->surface->height,
1072 &view->transform.boundingbox);
1077 static struct weston_layer *
1078 get_view_layer(struct weston_view *view)
1080 if (view->parent_view)
1081 return get_view_layer(view->parent_view);
1082 return view->layer_link.layer;
1086 weston_view_update_transform(struct weston_view *view)
1088 struct weston_view *parent = view->geometry.parent;
1089 struct weston_layer *layer;
1090 pixman_region32_t mask;
1092 if (!view->transform.dirty)
1096 weston_view_update_transform(parent);
1098 view->transform.dirty = 0;
1100 weston_view_damage_below(view);
1102 pixman_region32_fini(&view->transform.boundingbox);
1103 pixman_region32_fini(&view->transform.opaque);
1104 pixman_region32_init(&view->transform.opaque);
1106 /* transform.position is always in transformation_list */
1107 if (view->geometry.transformation_list.next ==
1108 &view->transform.position.link &&
1109 view->geometry.transformation_list.prev ==
1110 &view->transform.position.link &&
1112 weston_view_update_transform_disable(view);
1114 if (weston_view_update_transform_enable(view) < 0)
1115 weston_view_update_transform_disable(view);
1118 layer = get_view_layer(view);
1120 pixman_region32_init_with_extents(&mask, &layer->mask);
1121 pixman_region32_intersect(&view->transform.masked_boundingbox,
1122 &view->transform.boundingbox, &mask);
1123 pixman_region32_intersect(&view->transform.masked_opaque,
1124 &view->transform.opaque, &mask);
1125 pixman_region32_fini(&mask);
1128 weston_view_damage_below(view);
1130 weston_view_assign_output(view);
1132 wl_signal_emit(&view->surface->compositor->transform_signal,
1137 weston_view_geometry_dirty(struct weston_view *view)
1139 struct weston_view *child;
1142 * The invariant: if view->geometry.dirty, then all views
1143 * in view->geometry.child_list have geometry.dirty too.
1144 * Corollary: if not parent->geometry.dirty, then all ancestors
1148 if (view->transform.dirty)
1151 view->transform.dirty = 1;
1153 wl_list_for_each(child, &view->geometry.child_list,
1154 geometry.parent_link)
1155 weston_view_geometry_dirty(child);
1159 weston_view_to_global_fixed(struct weston_view *view,
1160 wl_fixed_t vx, wl_fixed_t vy,
1161 wl_fixed_t *x, wl_fixed_t *y)
1165 weston_view_to_global_float(view,
1166 wl_fixed_to_double(vx),
1167 wl_fixed_to_double(vy),
1169 *x = wl_fixed_from_double(xf);
1170 *y = wl_fixed_from_double(yf);
1174 weston_view_from_global_float(struct weston_view *view,
1175 float x, float y, float *vx, float *vy)
1177 if (view->transform.enabled) {
1178 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1180 weston_matrix_transform(&view->transform.inverse, &v);
1182 if (fabsf(v.f[3]) < 1e-6) {
1183 weston_log("warning: numerical instability in "
1184 "weston_view_from_global(), divisor = %g\n",
1191 *vx = v.f[0] / v.f[3];
1192 *vy = v.f[1] / v.f[3];
1194 *vx = x - view->geometry.x;
1195 *vy = y - view->geometry.y;
1200 weston_view_from_global_fixed(struct weston_view *view,
1201 wl_fixed_t x, wl_fixed_t y,
1202 wl_fixed_t *vx, wl_fixed_t *vy)
1206 weston_view_from_global_float(view,
1207 wl_fixed_to_double(x),
1208 wl_fixed_to_double(y),
1210 *vx = wl_fixed_from_double(vxf);
1211 *vy = wl_fixed_from_double(vyf);
1215 weston_view_from_global(struct weston_view *view,
1216 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
1220 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1226 weston_surface_schedule_repaint(struct weston_surface *surface)
1228 struct weston_output *output;
1230 wl_list_for_each(output, &surface->compositor->output_list, link)
1231 if (surface->output_mask & (1 << output->id))
1232 weston_output_schedule_repaint(output);
1236 weston_view_schedule_repaint(struct weston_view *view)
1238 struct weston_output *output;
1240 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1241 if (view->output_mask & (1 << output->id))
1242 weston_output_schedule_repaint(output);
1246 weston_surface_damage(struct weston_surface *surface)
1248 pixman_region32_union_rect(&surface->damage, &surface->damage,
1249 0, 0, surface->width,
1252 weston_surface_schedule_repaint(surface);
1256 weston_view_set_position(struct weston_view *view, float x, float y)
1258 if (view->geometry.x == x && view->geometry.y == y)
1261 view->geometry.x = x;
1262 view->geometry.y = y;
1263 weston_view_geometry_dirty(view);
1267 transform_parent_handle_parent_destroy(struct wl_listener *listener,
1270 struct weston_view *view =
1271 container_of(listener, struct weston_view,
1272 geometry.parent_destroy_listener);
1274 weston_view_set_transform_parent(view, NULL);
1278 weston_view_set_transform_parent(struct weston_view *view,
1279 struct weston_view *parent)
1281 if (view->geometry.parent) {
1282 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1283 wl_list_remove(&view->geometry.parent_link);
1286 view->geometry.parent = parent;
1288 view->geometry.parent_destroy_listener.notify =
1289 transform_parent_handle_parent_destroy;
1291 wl_signal_add(&parent->destroy_signal,
1292 &view->geometry.parent_destroy_listener);
1293 wl_list_insert(&parent->geometry.child_list,
1294 &view->geometry.parent_link);
1297 weston_view_geometry_dirty(view);
1301 weston_view_is_mapped(struct weston_view *view)
1310 weston_surface_is_mapped(struct weston_surface *surface)
1312 if (surface->output)
1319 surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
1321 struct weston_view *view;
1323 if (surface->width == width && surface->height == height)
1326 surface->width = width;
1327 surface->height = height;
1329 wl_list_for_each(view, &surface->views, surface_link)
1330 weston_view_geometry_dirty(view);
1334 weston_surface_set_size(struct weston_surface *surface,
1335 int32_t width, int32_t height)
1337 assert(!surface->resource);
1338 surface_set_size(surface, width, height);
1342 fixed_round_up_to_int(wl_fixed_t f)
1344 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1348 weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
1350 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1351 int32_t width, height;
1353 if (!surface->buffer_ref.buffer) {
1354 surface->width_from_buffer = 0;
1355 surface->height_from_buffer = 0;
1359 switch (vp->buffer.transform) {
1360 case WL_OUTPUT_TRANSFORM_90:
1361 case WL_OUTPUT_TRANSFORM_270:
1362 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1363 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1364 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1365 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
1368 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1369 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
1373 surface->width_from_buffer = width;
1374 surface->height_from_buffer = height;
1378 weston_surface_update_size(struct weston_surface *surface)
1380 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1381 int32_t width, height;
1383 width = surface->width_from_buffer;
1384 height = surface->height_from_buffer;
1386 if (width != 0 && vp->surface.width != -1) {
1387 surface_set_size(surface,
1388 vp->surface.width, vp->surface.height);
1392 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
1393 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1394 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1396 surface_set_size(surface, w ?: 1, h ?: 1);
1400 surface_set_size(surface, width, height);
1404 weston_compositor_get_time(void)
1408 gettimeofday(&tv, NULL);
1410 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1413 WL_EXPORT struct weston_view *
1414 weston_compositor_pick_view(struct weston_compositor *compositor,
1415 wl_fixed_t x, wl_fixed_t y,
1416 wl_fixed_t *vx, wl_fixed_t *vy)
1418 struct weston_view *view;
1419 int ix = wl_fixed_to_int(x);
1420 int iy = wl_fixed_to_int(y);
1422 wl_list_for_each(view, &compositor->view_list, link) {
1423 weston_view_from_global_fixed(view, x, y, vx, vy);
1424 if (pixman_region32_contains_point(
1425 &view->transform.masked_boundingbox,
1427 pixman_region32_contains_point(&view->surface->input,
1428 wl_fixed_to_int(*vx),
1429 wl_fixed_to_int(*vy),
1438 weston_compositor_repick(struct weston_compositor *compositor)
1440 struct weston_seat *seat;
1442 if (!compositor->session_active)
1445 wl_list_for_each(seat, &compositor->seat_list, link)
1446 weston_seat_repick(seat);
1450 weston_view_unmap(struct weston_view *view)
1452 struct weston_seat *seat;
1454 if (!weston_view_is_mapped(view))
1457 weston_view_damage_below(view);
1458 view->output = NULL;
1460 weston_layer_entry_remove(&view->layer_link);
1461 wl_list_remove(&view->link);
1462 wl_list_init(&view->link);
1463 view->output_mask = 0;
1464 weston_surface_assign_output(view->surface);
1466 if (weston_surface_is_mapped(view->surface))
1469 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1470 if (seat->keyboard && seat->keyboard->focus == view->surface)
1471 weston_keyboard_set_focus(seat->keyboard, NULL);
1472 if (seat->pointer && seat->pointer->focus == view)
1473 weston_pointer_set_focus(seat->pointer,
1475 wl_fixed_from_int(0),
1476 wl_fixed_from_int(0));
1477 if (seat->touch && seat->touch->focus == view)
1478 weston_touch_set_focus(seat, NULL);
1483 weston_surface_unmap(struct weston_surface *surface)
1485 struct weston_view *view;
1487 wl_list_for_each(view, &surface->views, surface_link)
1488 weston_view_unmap(view);
1489 surface->output = NULL;
1493 weston_surface_reset_pending_buffer(struct weston_surface *surface)
1495 weston_surface_state_set_buffer(&surface->pending, NULL);
1496 surface->pending.sx = 0;
1497 surface->pending.sy = 0;
1498 surface->pending.newly_attached = 0;
1499 surface->pending.buffer_viewport.changed = 0;
1503 weston_view_destroy(struct weston_view *view)
1505 wl_signal_emit(&view->destroy_signal, view);
1507 assert(wl_list_empty(&view->geometry.child_list));
1509 if (weston_view_is_mapped(view)) {
1510 weston_view_unmap(view);
1511 weston_compositor_build_view_list(view->surface->compositor);
1514 wl_list_remove(&view->link);
1515 weston_layer_entry_remove(&view->layer_link);
1517 pixman_region32_fini(&view->clip);
1518 pixman_region32_fini(&view->transform.boundingbox);
1519 pixman_region32_fini(&view->transform.masked_boundingbox);
1520 pixman_region32_fini(&view->transform.masked_opaque);
1522 weston_view_set_transform_parent(view, NULL);
1524 wl_list_remove(&view->surface_link);
1530 weston_surface_destroy(struct weston_surface *surface)
1532 struct weston_frame_callback *cb, *next;
1533 struct weston_view *ev, *nv;
1535 if (--surface->ref_count > 0)
1538 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1540 assert(wl_list_empty(&surface->subsurface_list_pending));
1541 assert(wl_list_empty(&surface->subsurface_list));
1543 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1544 weston_view_destroy(ev);
1546 weston_surface_state_fini(&surface->pending);
1548 weston_buffer_reference(&surface->buffer_ref, NULL);
1550 pixman_region32_fini(&surface->damage);
1551 pixman_region32_fini(&surface->opaque);
1552 pixman_region32_fini(&surface->input);
1554 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1555 wl_resource_destroy(cb->resource);
1561 destroy_surface(struct wl_resource *resource)
1563 struct weston_surface *surface = wl_resource_get_user_data(resource);
1565 /* Set the resource to NULL, since we don't want to leave a
1566 * dangling pointer if the surface was refcounted and survives
1567 * the weston_surface_destroy() call. */
1568 surface->resource = NULL;
1569 weston_surface_destroy(surface);
1573 weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1575 struct weston_buffer *buffer =
1576 container_of(listener, struct weston_buffer, destroy_listener);
1578 wl_signal_emit(&buffer->destroy_signal, buffer);
1582 WL_EXPORT struct weston_buffer *
1583 weston_buffer_from_resource(struct wl_resource *resource)
1585 struct weston_buffer *buffer;
1586 struct wl_listener *listener;
1588 listener = wl_resource_get_destroy_listener(resource,
1589 weston_buffer_destroy_handler);
1592 return container_of(listener, struct weston_buffer,
1595 buffer = zalloc(sizeof *buffer);
1599 buffer->resource = resource;
1600 wl_signal_init(&buffer->destroy_signal);
1601 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1602 buffer->y_inverted = 1;
1603 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
1609 weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1612 struct weston_buffer_reference *ref =
1613 container_of(listener, struct weston_buffer_reference,
1616 assert((struct weston_buffer *)data == ref->buffer);
1621 weston_buffer_reference(struct weston_buffer_reference *ref,
1622 struct weston_buffer *buffer)
1624 if (ref->buffer && buffer != ref->buffer) {
1625 ref->buffer->busy_count--;
1626 if (ref->buffer->busy_count == 0) {
1627 assert(wl_resource_get_client(ref->buffer->resource));
1628 wl_resource_queue_event(ref->buffer->resource,
1631 wl_list_remove(&ref->destroy_listener.link);
1634 if (buffer && buffer != ref->buffer) {
1635 buffer->busy_count++;
1636 wl_signal_add(&buffer->destroy_signal,
1637 &ref->destroy_listener);
1640 ref->buffer = buffer;
1641 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1645 weston_surface_attach(struct weston_surface *surface,
1646 struct weston_buffer *buffer)
1648 weston_buffer_reference(&surface->buffer_ref, buffer);
1651 if (weston_surface_is_mapped(surface))
1652 weston_surface_unmap(surface);
1655 surface->compositor->renderer->attach(surface, buffer);
1657 weston_surface_calculate_size_from_buffer(surface);
1661 weston_compositor_damage_all(struct weston_compositor *compositor)
1663 struct weston_output *output;
1665 wl_list_for_each(output, &compositor->output_list, link)
1666 weston_output_damage(output);
1670 weston_output_damage(struct weston_output *output)
1672 struct weston_compositor *compositor = output->compositor;
1674 pixman_region32_union(&compositor->primary_plane.damage,
1675 &compositor->primary_plane.damage,
1677 weston_output_schedule_repaint(output);
1681 surface_flush_damage(struct weston_surface *surface)
1683 if (surface->buffer_ref.buffer &&
1684 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
1685 surface->compositor->renderer->flush_damage(surface);
1687 pixman_region32_clear(&surface->damage);
1691 view_accumulate_damage(struct weston_view *view,
1692 pixman_region32_t *opaque)
1694 pixman_region32_t damage;
1696 pixman_region32_init(&damage);
1697 if (view->transform.enabled) {
1698 pixman_box32_t *extents;
1700 extents = pixman_region32_extents(&view->surface->damage);
1701 view_compute_bbox(view, extents->x1, extents->y1,
1702 extents->x2 - extents->x1,
1703 extents->y2 - extents->y1,
1705 pixman_region32_translate(&damage,
1709 pixman_region32_copy(&damage, &view->surface->damage);
1710 pixman_region32_translate(&damage,
1711 view->geometry.x - view->plane->x,
1712 view->geometry.y - view->plane->y);
1715 pixman_region32_subtract(&damage, &damage, opaque);
1716 pixman_region32_union(&view->plane->damage,
1717 &view->plane->damage, &damage);
1718 pixman_region32_fini(&damage);
1719 pixman_region32_copy(&view->clip, opaque);
1720 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
1724 compositor_accumulate_damage(struct weston_compositor *ec)
1726 struct weston_plane *plane;
1727 struct weston_view *ev;
1728 pixman_region32_t opaque, clip;
1730 pixman_region32_init(&clip);
1732 wl_list_for_each(plane, &ec->plane_list, link) {
1733 pixman_region32_copy(&plane->clip, &clip);
1735 pixman_region32_init(&opaque);
1737 wl_list_for_each(ev, &ec->view_list, link) {
1738 if (ev->plane != plane)
1741 view_accumulate_damage(ev, &opaque);
1744 pixman_region32_union(&clip, &clip, &opaque);
1745 pixman_region32_fini(&opaque);
1748 pixman_region32_fini(&clip);
1750 wl_list_for_each(ev, &ec->view_list, link)
1751 ev->surface->touched = 0;
1753 wl_list_for_each(ev, &ec->view_list, link) {
1754 if (ev->surface->touched)
1756 ev->surface->touched = 1;
1758 surface_flush_damage(ev->surface);
1760 /* Both the renderer and the backend have seen the buffer
1761 * by now. If renderer needs the buffer, it has its own
1762 * reference set. If the backend wants to keep the buffer
1763 * around for migrating the surface into a non-primary plane
1764 * later, keep_buffer is true. Otherwise, drop the core
1765 * reference now, and allow early buffer release. This enables
1766 * clients to use single-buffering.
1768 if (!ev->surface->keep_buffer)
1769 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
1774 surface_stash_subsurface_views(struct weston_surface *surface)
1776 struct weston_subsurface *sub;
1778 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1779 if (sub->surface == surface)
1782 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1783 wl_list_init(&sub->surface->views);
1785 surface_stash_subsurface_views(sub->surface);
1790 surface_free_unused_subsurface_views(struct weston_surface *surface)
1792 struct weston_subsurface *sub;
1793 struct weston_view *view, *nv;
1795 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1796 if (sub->surface == surface)
1799 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1800 weston_view_unmap (view);
1801 weston_view_destroy(view);
1804 surface_free_unused_subsurface_views(sub->surface);
1809 view_list_add_subsurface_view(struct weston_compositor *compositor,
1810 struct weston_subsurface *sub,
1811 struct weston_view *parent)
1813 struct weston_subsurface *child;
1814 struct weston_view *view = NULL, *iv;
1816 if (!weston_surface_is_mapped(sub->surface))
1819 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1820 if (iv->geometry.parent == parent) {
1827 /* Put it back in the surface's list of views */
1828 wl_list_remove(&view->surface_link);
1829 wl_list_insert(&sub->surface->views, &view->surface_link);
1831 view = weston_view_create(sub->surface);
1832 weston_view_set_position(view,
1835 weston_view_set_transform_parent(view, parent);
1838 view->parent_view = parent;
1839 weston_view_update_transform(view);
1841 if (wl_list_empty(&sub->surface->subsurface_list)) {
1842 wl_list_insert(compositor->view_list.prev, &view->link);
1846 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1847 if (child->surface == sub->surface)
1848 wl_list_insert(compositor->view_list.prev, &view->link);
1850 view_list_add_subsurface_view(compositor, child, view);
1855 view_list_add(struct weston_compositor *compositor,
1856 struct weston_view *view)
1858 struct weston_subsurface *sub;
1860 weston_view_update_transform(view);
1862 if (wl_list_empty(&view->surface->subsurface_list)) {
1863 wl_list_insert(compositor->view_list.prev, &view->link);
1867 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1868 if (sub->surface == view->surface)
1869 wl_list_insert(compositor->view_list.prev, &view->link);
1871 view_list_add_subsurface_view(compositor, sub, view);
1876 weston_compositor_build_view_list(struct weston_compositor *compositor)
1878 struct weston_view *view;
1879 struct weston_layer *layer;
1881 wl_list_for_each(layer, &compositor->layer_list, link)
1882 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
1883 surface_stash_subsurface_views(view->surface);
1885 wl_list_init(&compositor->view_list);
1886 wl_list_for_each(layer, &compositor->layer_list, link) {
1887 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
1888 view_list_add(compositor, view);
1892 wl_list_for_each(layer, &compositor->layer_list, link)
1893 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
1894 surface_free_unused_subsurface_views(view->surface);
1898 weston_output_repaint(struct weston_output *output, uint32_t msecs)
1900 struct weston_compositor *ec = output->compositor;
1901 struct weston_view *ev;
1902 struct weston_animation *animation, *next;
1903 struct weston_frame_callback *cb, *cnext;
1904 struct wl_list frame_callback_list;
1905 pixman_region32_t output_damage;
1908 if (output->destroying)
1911 /* Rebuild the surface list and update surface transforms up front. */
1912 weston_compositor_build_view_list(ec);
1914 if (output->assign_planes && !output->disable_planes)
1915 output->assign_planes(output);
1917 wl_list_for_each(ev, &ec->view_list, link)
1918 weston_view_move_to_plane(ev, &ec->primary_plane);
1920 wl_list_init(&frame_callback_list);
1921 wl_list_for_each(ev, &ec->view_list, link) {
1922 /* Note: This operation is safe to do multiple times on the
1925 if (ev->surface->output == output) {
1926 wl_list_insert_list(&frame_callback_list,
1927 &ev->surface->frame_callback_list);
1928 wl_list_init(&ev->surface->frame_callback_list);
1932 compositor_accumulate_damage(ec);
1934 pixman_region32_init(&output_damage);
1935 pixman_region32_intersect(&output_damage,
1936 &ec->primary_plane.damage, &output->region);
1937 pixman_region32_subtract(&output_damage,
1938 &output_damage, &ec->primary_plane.clip);
1941 weston_output_update_matrix(output);
1943 r = output->repaint(output, &output_damage);
1945 pixman_region32_fini(&output_damage);
1947 output->repaint_needed = 0;
1949 weston_compositor_repick(ec);
1950 wl_event_loop_dispatch(ec->input_loop, 0);
1952 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
1953 wl_callback_send_done(cb->resource, msecs);
1954 wl_resource_destroy(cb->resource);
1957 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
1958 animation->frame_counter++;
1959 animation->frame(animation, output, msecs);
1966 weston_compositor_read_input(int fd, uint32_t mask, void *data)
1968 struct weston_compositor *compositor = data;
1970 wl_event_loop_dispatch(compositor->input_loop, 0);
1976 weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
1978 struct weston_compositor *compositor = output->compositor;
1979 struct wl_event_loop *loop =
1980 wl_display_get_event_loop(compositor->wl_display);
1983 output->frame_time = msecs;
1985 if (output->repaint_needed &&
1986 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1987 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
1988 r = weston_output_repaint(output, msecs);
1993 output->repaint_scheduled = 0;
1994 if (compositor->input_loop_source)
1997 fd = wl_event_loop_get_fd(compositor->input_loop);
1998 compositor->input_loop_source =
1999 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2000 weston_compositor_read_input, compositor);
2004 idle_repaint(void *data)
2006 struct weston_output *output = data;
2008 output->start_repaint_loop(output);
2012 weston_layer_entry_insert(struct weston_layer_entry *list,
2013 struct weston_layer_entry *entry)
2015 wl_list_insert(&list->link, &entry->link);
2016 entry->layer = list->layer;
2020 weston_layer_entry_remove(struct weston_layer_entry *entry)
2022 wl_list_remove(&entry->link);
2023 wl_list_init(&entry->link);
2024 entry->layer = NULL;
2028 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2030 wl_list_init(&layer->view_list.link);
2031 layer->view_list.layer = layer;
2032 weston_layer_set_mask_infinite(layer);
2034 wl_list_insert(below, &layer->link);
2038 weston_layer_set_mask(struct weston_layer *layer,
2039 int x, int y, int width, int height)
2041 struct weston_view *view;
2044 layer->mask.x2 = x + width;
2046 layer->mask.y2 = y + height;
2048 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2049 weston_view_geometry_dirty(view);
2054 weston_layer_set_mask_infinite(struct weston_layer *layer)
2056 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2057 UINT32_MAX, UINT32_MAX);
2061 weston_output_schedule_repaint(struct weston_output *output)
2063 struct weston_compositor *compositor = output->compositor;
2064 struct wl_event_loop *loop;
2066 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2067 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2070 loop = wl_display_get_event_loop(compositor->wl_display);
2071 output->repaint_needed = 1;
2072 if (output->repaint_scheduled)
2075 wl_event_loop_add_idle(loop, idle_repaint, output);
2076 output->repaint_scheduled = 1;
2078 if (compositor->input_loop_source) {
2079 wl_event_source_remove(compositor->input_loop_source);
2080 compositor->input_loop_source = NULL;
2085 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2087 struct weston_output *output;
2089 wl_list_for_each(output, &compositor->output_list, link)
2090 weston_output_schedule_repaint(output);
2094 surface_destroy(struct wl_client *client, struct wl_resource *resource)
2096 wl_resource_destroy(resource);
2100 surface_attach(struct wl_client *client,
2101 struct wl_resource *resource,
2102 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2104 struct weston_surface *surface = wl_resource_get_user_data(resource);
2105 struct weston_buffer *buffer = NULL;
2107 if (buffer_resource) {
2108 buffer = weston_buffer_from_resource(buffer_resource);
2109 if (buffer == NULL) {
2110 wl_client_post_no_memory(client);
2115 /* Attach, attach, without commit in between does not send
2116 * wl_buffer.release. */
2117 weston_surface_state_set_buffer(&surface->pending, buffer);
2119 surface->pending.sx = sx;
2120 surface->pending.sy = sy;
2121 surface->pending.newly_attached = 1;
2125 surface_damage(struct wl_client *client,
2126 struct wl_resource *resource,
2127 int32_t x, int32_t y, int32_t width, int32_t height)
2129 struct weston_surface *surface = wl_resource_get_user_data(resource);
2131 pixman_region32_union_rect(&surface->pending.damage,
2132 &surface->pending.damage,
2133 x, y, width, height);
2137 destroy_frame_callback(struct wl_resource *resource)
2139 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
2141 wl_list_remove(&cb->link);
2146 surface_frame(struct wl_client *client,
2147 struct wl_resource *resource, uint32_t callback)
2149 struct weston_frame_callback *cb;
2150 struct weston_surface *surface = wl_resource_get_user_data(resource);
2152 cb = malloc(sizeof *cb);
2154 wl_resource_post_no_memory(resource);
2158 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2160 if (cb->resource == NULL) {
2162 wl_resource_post_no_memory(resource);
2166 wl_resource_set_implementation(cb->resource, NULL, cb,
2167 destroy_frame_callback);
2169 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
2173 surface_set_opaque_region(struct wl_client *client,
2174 struct wl_resource *resource,
2175 struct wl_resource *region_resource)
2177 struct weston_surface *surface = wl_resource_get_user_data(resource);
2178 struct weston_region *region;
2180 if (region_resource) {
2181 region = wl_resource_get_user_data(region_resource);
2182 pixman_region32_copy(&surface->pending.opaque,
2185 pixman_region32_clear(&surface->pending.opaque);
2190 surface_set_input_region(struct wl_client *client,
2191 struct wl_resource *resource,
2192 struct wl_resource *region_resource)
2194 struct weston_surface *surface = wl_resource_get_user_data(resource);
2195 struct weston_region *region;
2197 if (region_resource) {
2198 region = wl_resource_get_user_data(region_resource);
2199 pixman_region32_copy(&surface->pending.input,
2202 pixman_region32_fini(&surface->pending.input);
2203 region_init_infinite(&surface->pending.input);
2208 weston_surface_commit_subsurface_order(struct weston_surface *surface)
2210 struct weston_subsurface *sub;
2212 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2213 parent_link_pending) {
2214 wl_list_remove(&sub->parent_link);
2215 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2220 weston_surface_commit_state(struct weston_surface *surface,
2221 struct weston_surface_state *state)
2223 struct weston_view *view;
2224 pixman_region32_t opaque;
2226 /* wl_surface.set_buffer_transform */
2227 /* wl_surface.set_buffer_scale */
2228 /* wl_viewport.set */
2229 surface->buffer_viewport = state->buffer_viewport;
2231 /* wl_surface.attach */
2232 if (state->newly_attached)
2233 weston_surface_attach(surface, state->buffer);
2234 weston_surface_state_set_buffer(state, NULL);
2236 if (state->newly_attached || state->buffer_viewport.changed) {
2237 weston_surface_update_size(surface);
2238 if (surface->configure)
2239 surface->configure(surface, state->sx, state->sy);
2244 state->newly_attached = 0;
2245 state->buffer_viewport.changed = 0;
2247 /* wl_surface.damage */
2248 pixman_region32_union(&surface->damage, &surface->damage,
2250 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2251 0, 0, surface->width, surface->height);
2252 pixman_region32_clear(&state->damage);
2254 /* wl_surface.set_opaque_region */
2255 pixman_region32_init(&opaque);
2256 pixman_region32_intersect_rect(&opaque, &state->opaque,
2257 0, 0, surface->width, surface->height);
2259 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2260 pixman_region32_copy(&surface->opaque, &opaque);
2261 wl_list_for_each(view, &surface->views, surface_link)
2262 weston_view_geometry_dirty(view);
2265 pixman_region32_fini(&opaque);
2267 /* wl_surface.set_input_region */
2268 pixman_region32_intersect_rect(&surface->input, &state->input,
2269 0, 0, surface->width, surface->height);
2271 /* wl_surface.frame */
2272 wl_list_insert_list(&surface->frame_callback_list,
2273 &state->frame_callback_list);
2274 wl_list_init(&state->frame_callback_list);
2278 weston_surface_commit(struct weston_surface *surface)
2280 weston_surface_commit_state(surface, &surface->pending);
2282 weston_surface_commit_subsurface_order(surface);
2284 weston_surface_schedule_repaint(surface);
2288 weston_subsurface_commit(struct weston_subsurface *sub);
2291 weston_subsurface_parent_commit(struct weston_subsurface *sub,
2292 int parent_is_synchronized);
2295 surface_commit(struct wl_client *client, struct wl_resource *resource)
2297 struct weston_surface *surface = wl_resource_get_user_data(resource);
2298 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2301 weston_subsurface_commit(sub);
2305 weston_surface_commit(surface);
2307 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2308 if (sub->surface != surface)
2309 weston_subsurface_parent_commit(sub, 0);
2314 surface_set_buffer_transform(struct wl_client *client,
2315 struct wl_resource *resource, int transform)
2317 struct weston_surface *surface = wl_resource_get_user_data(resource);
2319 /* if wl_output.transform grows more members this will need to be updated. */
2320 if (transform < 0 ||
2321 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2322 wl_resource_post_error(resource,
2323 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2324 "buffer transform must be a valid transform "
2325 "('%d' specified)", transform);
2329 surface->pending.buffer_viewport.buffer.transform = transform;
2330 surface->pending.buffer_viewport.changed = 1;
2334 surface_set_buffer_scale(struct wl_client *client,
2335 struct wl_resource *resource,
2338 struct weston_surface *surface = wl_resource_get_user_data(resource);
2341 wl_resource_post_error(resource,
2342 WL_SURFACE_ERROR_INVALID_SCALE,
2343 "buffer scale must be at least one "
2344 "('%d' specified)", scale);
2348 surface->pending.buffer_viewport.buffer.scale = scale;
2349 surface->pending.buffer_viewport.changed = 1;
2352 static const struct wl_surface_interface surface_interface = {
2357 surface_set_opaque_region,
2358 surface_set_input_region,
2360 surface_set_buffer_transform,
2361 surface_set_buffer_scale
2365 compositor_create_surface(struct wl_client *client,
2366 struct wl_resource *resource, uint32_t id)
2368 struct weston_compositor *ec = wl_resource_get_user_data(resource);
2369 struct weston_surface *surface;
2371 surface = weston_surface_create(ec);
2372 if (surface == NULL) {
2373 wl_resource_post_no_memory(resource);
2378 wl_resource_create(client, &wl_surface_interface,
2379 wl_resource_get_version(resource), id);
2380 if (surface->resource == NULL) {
2381 weston_surface_destroy(surface);
2382 wl_resource_post_no_memory(resource);
2385 wl_resource_set_implementation(surface->resource, &surface_interface,
2386 surface, destroy_surface);
2388 wl_signal_emit(&ec->create_surface_signal, surface);
2392 destroy_region(struct wl_resource *resource)
2394 struct weston_region *region = wl_resource_get_user_data(resource);
2396 pixman_region32_fini(®ion->region);
2401 region_destroy(struct wl_client *client, struct wl_resource *resource)
2403 wl_resource_destroy(resource);
2407 region_add(struct wl_client *client, struct wl_resource *resource,
2408 int32_t x, int32_t y, int32_t width, int32_t height)
2410 struct weston_region *region = wl_resource_get_user_data(resource);
2412 pixman_region32_union_rect(®ion->region, ®ion->region,
2413 x, y, width, height);
2417 region_subtract(struct wl_client *client, struct wl_resource *resource,
2418 int32_t x, int32_t y, int32_t width, int32_t height)
2420 struct weston_region *region = wl_resource_get_user_data(resource);
2421 pixman_region32_t rect;
2423 pixman_region32_init_rect(&rect, x, y, width, height);
2424 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
2425 pixman_region32_fini(&rect);
2428 static const struct wl_region_interface region_interface = {
2435 compositor_create_region(struct wl_client *client,
2436 struct wl_resource *resource, uint32_t id)
2438 struct weston_region *region;
2440 region = malloc(sizeof *region);
2441 if (region == NULL) {
2442 wl_resource_post_no_memory(resource);
2446 pixman_region32_init(®ion->region);
2449 wl_resource_create(client, &wl_region_interface, 1, id);
2450 if (region->resource == NULL) {
2452 wl_resource_post_no_memory(resource);
2455 wl_resource_set_implementation(region->resource, ®ion_interface,
2456 region, destroy_region);
2459 static const struct wl_compositor_interface compositor_interface = {
2460 compositor_create_surface,
2461 compositor_create_region
2465 weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2467 struct weston_surface *surface = sub->surface;
2469 weston_surface_commit_state(surface, &sub->cached);
2470 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
2472 weston_surface_commit_subsurface_order(surface);
2474 weston_surface_schedule_repaint(surface);
2476 sub->has_cached_data = 0;
2480 weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2482 struct weston_surface *surface = sub->surface;
2485 * If this commit would cause the surface to move by the
2486 * attach(dx, dy) parameters, the old damage region must be
2487 * translated to correspond to the new surface coordinate system
2490 pixman_region32_translate(&sub->cached.damage,
2491 -surface->pending.sx, -surface->pending.sy);
2492 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2493 &surface->pending.damage);
2494 pixman_region32_clear(&surface->pending.damage);
2496 if (surface->pending.newly_attached) {
2497 sub->cached.newly_attached = 1;
2498 weston_surface_state_set_buffer(&sub->cached,
2499 surface->pending.buffer);
2500 weston_buffer_reference(&sub->cached_buffer_ref,
2501 surface->pending.buffer);
2503 sub->cached.sx += surface->pending.sx;
2504 sub->cached.sy += surface->pending.sy;
2506 sub->cached.buffer_viewport.changed |=
2507 surface->pending.buffer_viewport.changed;
2508 sub->cached.buffer_viewport.buffer =
2509 surface->pending.buffer_viewport.buffer;
2510 sub->cached.buffer_viewport.surface =
2511 surface->pending.buffer_viewport.surface;
2513 weston_surface_reset_pending_buffer(surface);
2515 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2517 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2519 wl_list_insert_list(&sub->cached.frame_callback_list,
2520 &surface->pending.frame_callback_list);
2521 wl_list_init(&surface->pending.frame_callback_list);
2523 sub->has_cached_data = 1;
2527 weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2530 if (sub->synchronized)
2536 sub = weston_surface_to_subsurface(sub->parent);
2543 weston_subsurface_commit(struct weston_subsurface *sub)
2545 struct weston_surface *surface = sub->surface;
2546 struct weston_subsurface *tmp;
2548 /* Recursive check for effectively synchronized. */
2549 if (weston_subsurface_is_synchronized(sub)) {
2550 weston_subsurface_commit_to_cache(sub);
2552 if (sub->has_cached_data) {
2553 /* flush accumulated state from cache */
2554 weston_subsurface_commit_to_cache(sub);
2555 weston_subsurface_commit_from_cache(sub);
2557 weston_surface_commit(surface);
2560 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2561 if (tmp->surface != surface)
2562 weston_subsurface_parent_commit(tmp, 0);
2568 weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
2570 struct weston_surface *surface = sub->surface;
2571 struct weston_subsurface *tmp;
2573 /* From now on, commit_from_cache the whole sub-tree, regardless of
2574 * the synchronized mode of each child. This sub-surface or some
2575 * of its ancestors were synchronized, so we are synchronized
2579 if (sub->has_cached_data)
2580 weston_subsurface_commit_from_cache(sub);
2582 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2583 if (tmp->surface != surface)
2584 weston_subsurface_parent_commit(tmp, 1);
2589 weston_subsurface_parent_commit(struct weston_subsurface *sub,
2590 int parent_is_synchronized)
2592 struct weston_view *view;
2593 if (sub->position.set) {
2594 wl_list_for_each(view, &sub->surface->views, surface_link)
2595 weston_view_set_position(view,
2599 sub->position.set = 0;
2602 if (parent_is_synchronized || sub->synchronized)
2603 weston_subsurface_synchronized_commit(sub);
2607 subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
2609 struct weston_compositor *compositor = surface->compositor;
2610 struct weston_view *view;
2612 wl_list_for_each(view, &surface->views, surface_link)
2613 weston_view_set_position(view,
2614 view->geometry.x + dx,
2615 view->geometry.y + dy);
2617 /* No need to check parent mappedness, because if parent is not
2618 * mapped, parent is not in a visible layer, so this sub-surface
2619 * will not be drawn either.
2621 if (!weston_surface_is_mapped(surface)) {
2622 struct weston_output *output;
2624 /* Cannot call weston_surface_update_transform(),
2625 * because that would call it also for the parent surface,
2626 * which might not be mapped yet. That would lead to
2627 * inconsistent state, where the window could never be
2630 * Instead just assing any output, to make
2631 * weston_surface_is_mapped() return true, so that when the
2632 * parent surface does get mapped, this one will get
2633 * included, too. See view_list_add().
2635 assert(!wl_list_empty(&compositor->output_list));
2636 output = container_of(compositor->output_list.next,
2637 struct weston_output, link);
2639 surface->output = output;
2640 weston_surface_update_output_mask(surface, 1 << output->id);
2644 static struct weston_subsurface *
2645 weston_surface_to_subsurface(struct weston_surface *surface)
2647 if (surface->configure == subsurface_configure)
2648 return surface->configure_private;
2653 WL_EXPORT struct weston_surface *
2654 weston_surface_get_main_surface(struct weston_surface *surface)
2656 struct weston_subsurface *sub;
2658 while (surface && (sub = weston_surface_to_subsurface(surface)))
2659 surface = sub->parent;
2665 subsurface_set_position(struct wl_client *client,
2666 struct wl_resource *resource, int32_t x, int32_t y)
2668 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2673 sub->position.x = x;
2674 sub->position.y = y;
2675 sub->position.set = 1;
2678 static struct weston_subsurface *
2679 subsurface_from_surface(struct weston_surface *surface)
2681 struct weston_subsurface *sub;
2683 sub = weston_surface_to_subsurface(surface);
2687 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2688 if (sub->surface == surface)
2694 static struct weston_subsurface *
2695 subsurface_sibling_check(struct weston_subsurface *sub,
2696 struct weston_surface *surface,
2697 const char *request)
2699 struct weston_subsurface *sibling;
2701 sibling = subsurface_from_surface(surface);
2704 wl_resource_post_error(sub->resource,
2705 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2706 "%s: wl_surface@%d is not a parent or sibling",
2707 request, wl_resource_get_id(surface->resource));
2711 if (sibling->parent != sub->parent) {
2712 wl_resource_post_error(sub->resource,
2713 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2714 "%s: wl_surface@%d has a different parent",
2715 request, wl_resource_get_id(surface->resource));
2723 subsurface_place_above(struct wl_client *client,
2724 struct wl_resource *resource,
2725 struct wl_resource *sibling_resource)
2727 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2728 struct weston_surface *surface =
2729 wl_resource_get_user_data(sibling_resource);
2730 struct weston_subsurface *sibling;
2735 sibling = subsurface_sibling_check(sub, surface, "place_above");
2739 wl_list_remove(&sub->parent_link_pending);
2740 wl_list_insert(sibling->parent_link_pending.prev,
2741 &sub->parent_link_pending);
2745 subsurface_place_below(struct wl_client *client,
2746 struct wl_resource *resource,
2747 struct wl_resource *sibling_resource)
2749 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2750 struct weston_surface *surface =
2751 wl_resource_get_user_data(sibling_resource);
2752 struct weston_subsurface *sibling;
2757 sibling = subsurface_sibling_check(sub, surface, "place_below");
2761 wl_list_remove(&sub->parent_link_pending);
2762 wl_list_insert(&sibling->parent_link_pending,
2763 &sub->parent_link_pending);
2767 subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2769 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2772 sub->synchronized = 1;
2776 subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2778 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2780 if (sub && sub->synchronized) {
2781 sub->synchronized = 0;
2783 /* If sub became effectively desynchronized, flush. */
2784 if (!weston_subsurface_is_synchronized(sub))
2785 weston_subsurface_synchronized_commit(sub);
2790 weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2792 wl_list_remove(&sub->parent_link);
2793 wl_list_remove(&sub->parent_link_pending);
2794 wl_list_remove(&sub->parent_destroy_listener.link);
2799 weston_subsurface_destroy(struct weston_subsurface *sub);
2802 subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2804 struct weston_subsurface *sub =
2805 container_of(listener, struct weston_subsurface,
2806 surface_destroy_listener);
2807 assert(data == &sub->surface->resource);
2809 /* The protocol object (wl_resource) is left inert. */
2811 wl_resource_set_user_data(sub->resource, NULL);
2813 weston_subsurface_destroy(sub);
2817 subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2819 struct weston_subsurface *sub =
2820 container_of(listener, struct weston_subsurface,
2821 parent_destroy_listener);
2822 assert(data == &sub->parent->resource);
2823 assert(sub->surface != sub->parent);
2825 if (weston_surface_is_mapped(sub->surface))
2826 weston_surface_unmap(sub->surface);
2828 weston_subsurface_unlink_parent(sub);
2832 subsurface_resource_destroy(struct wl_resource *resource)
2834 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
2837 weston_subsurface_destroy(sub);
2841 subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2843 wl_resource_destroy(resource);
2847 weston_subsurface_link_parent(struct weston_subsurface *sub,
2848 struct weston_surface *parent)
2850 sub->parent = parent;
2851 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
2852 wl_signal_add(&parent->destroy_signal,
2853 &sub->parent_destroy_listener);
2855 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2856 wl_list_insert(&parent->subsurface_list_pending,
2857 &sub->parent_link_pending);
2861 weston_subsurface_link_surface(struct weston_subsurface *sub,
2862 struct weston_surface *surface)
2864 sub->surface = surface;
2865 sub->surface_destroy_listener.notify =
2866 subsurface_handle_surface_destroy;
2867 wl_signal_add(&surface->destroy_signal,
2868 &sub->surface_destroy_listener);
2872 weston_subsurface_destroy(struct weston_subsurface *sub)
2874 struct weston_view *view, *next;
2876 assert(sub->surface);
2878 if (sub->resource) {
2879 assert(weston_surface_to_subsurface(sub->surface) == sub);
2880 assert(sub->parent_destroy_listener.notify ==
2881 subsurface_handle_parent_destroy);
2883 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2884 weston_view_unmap(view);
2885 weston_view_destroy(view);
2889 weston_subsurface_unlink_parent(sub);
2891 weston_surface_state_fini(&sub->cached);
2892 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
2894 sub->surface->configure = NULL;
2895 sub->surface->configure_private = NULL;
2897 /* the dummy weston_subsurface for the parent itself */
2898 assert(sub->parent_destroy_listener.notify == NULL);
2899 wl_list_remove(&sub->parent_link);
2900 wl_list_remove(&sub->parent_link_pending);
2903 wl_list_remove(&sub->surface_destroy_listener.link);
2907 static const struct wl_subsurface_interface subsurface_implementation = {
2909 subsurface_set_position,
2910 subsurface_place_above,
2911 subsurface_place_below,
2912 subsurface_set_sync,
2913 subsurface_set_desync
2916 static struct weston_subsurface *
2917 weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2918 struct weston_surface *parent)
2920 struct weston_subsurface *sub;
2921 struct wl_client *client = wl_resource_get_client(surface->resource);
2923 sub = calloc(1, sizeof *sub);
2927 wl_list_init(&sub->unused_views);
2930 wl_resource_create(client, &wl_subsurface_interface, 1, id);
2931 if (!sub->resource) {
2936 wl_resource_set_implementation(sub->resource,
2937 &subsurface_implementation,
2938 sub, subsurface_resource_destroy);
2939 weston_subsurface_link_surface(sub, surface);
2940 weston_subsurface_link_parent(sub, parent);
2941 weston_surface_state_init(&sub->cached);
2942 sub->cached_buffer_ref.buffer = NULL;
2943 sub->synchronized = 1;
2948 /* Create a dummy subsurface for having the parent itself in its
2949 * sub-surface lists. Makes stacking order manipulation easy.
2951 static struct weston_subsurface *
2952 weston_subsurface_create_for_parent(struct weston_surface *parent)
2954 struct weston_subsurface *sub;
2956 sub = calloc(1, sizeof *sub);
2960 weston_subsurface_link_surface(sub, parent);
2961 sub->parent = parent;
2962 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2963 wl_list_insert(&parent->subsurface_list_pending,
2964 &sub->parent_link_pending);
2970 subcompositor_get_subsurface(struct wl_client *client,
2971 struct wl_resource *resource,
2973 struct wl_resource *surface_resource,
2974 struct wl_resource *parent_resource)
2976 struct weston_surface *surface =
2977 wl_resource_get_user_data(surface_resource);
2978 struct weston_surface *parent =
2979 wl_resource_get_user_data(parent_resource);
2980 struct weston_subsurface *sub;
2981 static const char where[] = "get_subsurface: wl_subsurface@";
2983 if (surface == parent) {
2984 wl_resource_post_error(resource,
2985 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2986 "%s%d: wl_surface@%d cannot be its own parent",
2987 where, id, wl_resource_get_id(surface_resource));
2991 if (weston_surface_to_subsurface(surface)) {
2992 wl_resource_post_error(resource,
2993 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2994 "%s%d: wl_surface@%d is already a sub-surface",
2995 where, id, wl_resource_get_id(surface_resource));
2999 if (surface->configure) {
3000 wl_resource_post_error(resource,
3001 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3002 "%s%d: wl_surface@%d already has a role",
3003 where, id, wl_resource_get_id(surface_resource));
3007 if (weston_surface_get_main_surface(parent) == surface) {
3008 wl_resource_post_error(resource,
3009 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3010 "%s%d: wl_surface@%d is an ancestor of parent",
3011 where, id, wl_resource_get_id(surface_resource));
3015 /* make sure the parent is in its own list */
3016 if (wl_list_empty(&parent->subsurface_list)) {
3017 if (!weston_subsurface_create_for_parent(parent)) {
3018 wl_resource_post_no_memory(resource);
3023 sub = weston_subsurface_create(id, surface, parent);
3025 wl_resource_post_no_memory(resource);
3029 surface->configure = subsurface_configure;
3030 surface->configure_private = sub;
3034 subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3036 wl_resource_destroy(resource);
3039 static const struct wl_subcompositor_interface subcompositor_interface = {
3040 subcompositor_destroy,
3041 subcompositor_get_subsurface
3045 bind_subcompositor(struct wl_client *client,
3046 void *data, uint32_t version, uint32_t id)
3048 struct weston_compositor *compositor = data;
3049 struct wl_resource *resource;
3052 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
3053 if (resource == NULL) {
3054 wl_client_post_no_memory(client);
3057 wl_resource_set_implementation(resource, &subcompositor_interface,
3062 weston_compositor_dpms(struct weston_compositor *compositor,
3063 enum dpms_enum state)
3065 struct weston_output *output;
3067 wl_list_for_each(output, &compositor->output_list, link)
3068 if (output->set_dpms)
3069 output->set_dpms(output, state);
3073 weston_compositor_wake(struct weston_compositor *compositor)
3075 uint32_t old_state = compositor->state;
3077 /* The state needs to be changed before emitting the wake
3078 * signal because that may try to schedule a repaint which
3079 * will not work if the compositor is still sleeping */
3080 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3082 switch (old_state) {
3083 case WESTON_COMPOSITOR_SLEEPING:
3084 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3086 case WESTON_COMPOSITOR_IDLE:
3087 case WESTON_COMPOSITOR_OFFSCREEN:
3088 wl_signal_emit(&compositor->wake_signal, compositor);
3091 wl_event_source_timer_update(compositor->idle_source,
3092 compositor->idle_time * 1000);
3097 weston_compositor_offscreen(struct weston_compositor *compositor)
3099 switch (compositor->state) {
3100 case WESTON_COMPOSITOR_OFFSCREEN:
3102 case WESTON_COMPOSITOR_SLEEPING:
3103 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3106 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3107 wl_event_source_timer_update(compositor->idle_source, 0);
3112 weston_compositor_sleep(struct weston_compositor *compositor)
3114 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3117 wl_event_source_timer_update(compositor->idle_source, 0);
3118 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3119 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3123 idle_handler(void *data)
3125 struct weston_compositor *compositor = data;
3127 if (compositor->idle_inhibit)
3130 compositor->state = WESTON_COMPOSITOR_IDLE;
3131 wl_signal_emit(&compositor->idle_signal, compositor);
3137 weston_plane_init(struct weston_plane *plane,
3138 struct weston_compositor *ec,
3139 int32_t x, int32_t y)
3141 pixman_region32_init(&plane->damage);
3142 pixman_region32_init(&plane->clip);
3145 plane->compositor = ec;
3147 /* Init the link so that the call to wl_list_remove() when releasing
3148 * the plane without ever stacking doesn't lead to a crash */
3149 wl_list_init(&plane->link);
3153 weston_plane_release(struct weston_plane *plane)
3155 struct weston_view *view;
3157 pixman_region32_fini(&plane->damage);
3158 pixman_region32_fini(&plane->clip);
3160 wl_list_for_each(view, &plane->compositor->view_list, link) {
3161 if (view->plane == plane)
3165 wl_list_remove(&plane->link);
3169 weston_compositor_stack_plane(struct weston_compositor *ec,
3170 struct weston_plane *plane,
3171 struct weston_plane *above)
3174 wl_list_insert(above->link.prev, &plane->link);
3176 wl_list_insert(&ec->plane_list, &plane->link);
3179 static void unbind_resource(struct wl_resource *resource)
3181 wl_list_remove(wl_resource_get_link(resource));
3185 bind_output(struct wl_client *client,
3186 void *data, uint32_t version, uint32_t id)
3188 struct weston_output *output = data;
3189 struct weston_mode *mode;
3190 struct wl_resource *resource;
3192 resource = wl_resource_create(client, &wl_output_interface,
3193 MIN(version, 2), id);
3194 if (resource == NULL) {
3195 wl_client_post_no_memory(client);
3199 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
3200 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
3202 wl_output_send_geometry(resource,
3208 output->make, output->model,
3210 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
3211 wl_output_send_scale(resource,
3212 output->current_scale);
3214 wl_list_for_each (mode, &output->mode_list, link) {
3215 wl_output_send_mode(resource,
3222 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
3223 wl_output_send_done(resource);
3226 /* Move other outputs when one is removed so the space remains contiguos. */
3228 weston_compositor_remove_output(struct weston_compositor *compositor,
3229 struct weston_output *remove_output)
3231 struct weston_output *output;
3234 wl_list_for_each(output, &compositor->output_list, link) {
3235 if (output == remove_output) {
3236 offset = output->width;
3241 weston_output_move(output,
3242 output->x - offset, output->y);
3249 weston_output_destroy(struct weston_output *output)
3251 output->destroying = 1;
3253 weston_compositor_remove_output(output->compositor, output);
3254 wl_list_remove(&output->link);
3256 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
3257 wl_signal_emit(&output->destroy_signal, output);
3260 pixman_region32_fini(&output->region);
3261 pixman_region32_fini(&output->previous_damage);
3262 output->compositor->output_id_pool &= ~(1 << output->id);
3264 wl_global_destroy(output->global);
3268 weston_output_compute_transform(struct weston_output *output)
3270 struct weston_matrix transform;
3273 weston_matrix_init(&transform);
3274 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
3276 switch(output->transform) {
3277 case WL_OUTPUT_TRANSFORM_FLIPPED:
3278 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3279 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3280 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3281 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
3289 switch(output->transform) {
3290 case WL_OUTPUT_TRANSFORM_NORMAL:
3291 case WL_OUTPUT_TRANSFORM_FLIPPED:
3292 transform.d[0] = flip;
3297 case WL_OUTPUT_TRANSFORM_90:
3298 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3300 transform.d[1] = -flip;
3304 case WL_OUTPUT_TRANSFORM_180:
3305 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3306 transform.d[0] = -flip;
3309 transform.d[5] = -1;
3311 case WL_OUTPUT_TRANSFORM_270:
3312 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3314 transform.d[1] = flip;
3315 transform.d[4] = -1;
3322 weston_matrix_multiply(&output->matrix, &transform);
3326 weston_output_update_matrix(struct weston_output *output)
3328 float magnification;
3330 weston_matrix_init(&output->matrix);
3331 weston_matrix_translate(&output->matrix,
3332 -(output->x + output->width / 2.0),
3333 -(output->y + output->height / 2.0), 0);
3335 weston_matrix_scale(&output->matrix,
3336 2.0 / output->width,
3337 -2.0 / output->height, 1);
3339 if (output->zoom.active) {
3340 magnification = 1 / (1 - output->zoom.spring_z.current);
3341 weston_output_update_zoom(output);
3342 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3343 output->zoom.trans_y, 0);
3344 weston_matrix_scale(&output->matrix, magnification,
3345 magnification, 1.0);
3348 weston_output_compute_transform(output);
3354 weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
3356 output->transform = transform;
3358 switch (transform) {
3359 case WL_OUTPUT_TRANSFORM_90:
3360 case WL_OUTPUT_TRANSFORM_270:
3361 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3362 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3363 /* Swap width and height */
3364 output->width = output->current_mode->height;
3365 output->height = output->current_mode->width;
3367 case WL_OUTPUT_TRANSFORM_NORMAL:
3368 case WL_OUTPUT_TRANSFORM_180:
3369 case WL_OUTPUT_TRANSFORM_FLIPPED:
3370 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3371 output->width = output->current_mode->width;
3372 output->height = output->current_mode->height;
3378 output->native_scale = output->current_scale = scale;
3379 output->width /= scale;
3380 output->height /= scale;
3384 weston_output_init_geometry(struct weston_output *output, int x, int y)
3389 pixman_region32_init(&output->previous_damage);
3390 pixman_region32_init_rect(&output->region, x, y,
3396 weston_output_move(struct weston_output *output, int x, int y)
3398 pixman_region32_t old_region;
3399 struct wl_resource *resource;
3401 output->move_x = x - output->x;
3402 output->move_y = y - output->y;
3404 if (output->move_x == 0 && output->move_y == 0)
3407 pixman_region32_init(&old_region);
3408 pixman_region32_copy(&old_region, &output->region);
3410 weston_output_init_geometry(output, x, y);
3414 /* Move views on this output. */
3415 wl_signal_emit(&output->compositor->output_moved_signal, output);
3417 /* Notify clients of the change for output position. */
3418 wl_resource_for_each(resource, &output->resource_list) {
3419 wl_output_send_geometry(resource,
3429 if (wl_resource_get_version(resource) >= 2)
3430 wl_output_send_done(resource);
3435 weston_output_init(struct weston_output *output, struct weston_compositor *c,
3436 int x, int y, int mm_width, int mm_height, uint32_t transform,
3439 output->compositor = c;
3442 output->mm_width = mm_width;
3443 output->mm_height = mm_height;
3445 output->original_scale = scale;
3447 weston_output_transform_scale_init(output, transform, scale);
3448 weston_output_init_zoom(output);
3450 weston_output_init_geometry(output, x, y);
3451 weston_output_damage(output);
3453 wl_signal_init(&output->frame_signal);
3454 wl_signal_init(&output->destroy_signal);
3455 wl_list_init(&output->animation_list);
3456 wl_list_init(&output->resource_list);
3458 output->id = ffs(~output->compositor->output_id_pool) - 1;
3459 output->compositor->output_id_pool |= 1 << output->id;
3462 wl_global_create(c->wl_display, &wl_output_interface, 2,
3463 output, bind_output);
3464 wl_signal_emit(&c->output_created_signal, output);
3468 weston_output_transform_coordinate(struct weston_output *output,
3469 wl_fixed_t device_x, wl_fixed_t device_y,
3470 wl_fixed_t *x, wl_fixed_t *y)
3473 wl_fixed_t width, height;
3474 float zoom_scale, zx, zy;
3476 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3477 height = wl_fixed_from_int(output->height * output->current_scale - 1);
3479 switch(output->transform) {
3480 case WL_OUTPUT_TRANSFORM_NORMAL:
3485 case WL_OUTPUT_TRANSFORM_90:
3487 ty = height - device_x;
3489 case WL_OUTPUT_TRANSFORM_180:
3490 tx = width - device_x;
3491 ty = height - device_y;
3493 case WL_OUTPUT_TRANSFORM_270:
3494 tx = width - device_y;
3497 case WL_OUTPUT_TRANSFORM_FLIPPED:
3498 tx = width - device_x;
3501 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3502 tx = width - device_y;
3503 ty = height - device_x;
3505 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3507 ty = height - device_y;
3509 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3515 tx /= output->current_scale;
3516 ty /= output->current_scale;
3518 if (output->zoom.active) {
3519 zoom_scale = output->zoom.spring_z.current;
3520 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3521 output->width / 2.0f *
3522 (zoom_scale + output->zoom.trans_x));
3523 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3524 output->height / 2.0f *
3525 (zoom_scale + output->zoom.trans_y));
3526 tx = wl_fixed_from_double(zx);
3527 ty = wl_fixed_from_double(zy);
3530 *x = tx + wl_fixed_from_int(output->x);
3531 *y = ty + wl_fixed_from_int(output->y);
3535 destroy_viewport(struct wl_resource *resource)
3537 struct weston_surface *surface =
3538 wl_resource_get_user_data(resource);
3540 surface->viewport_resource = NULL;
3541 surface->pending.buffer_viewport.buffer.src_width =
3542 wl_fixed_from_int(-1);
3543 surface->pending.buffer_viewport.surface.width = -1;
3544 surface->pending.buffer_viewport.changed = 1;
3548 viewport_destroy(struct wl_client *client,
3549 struct wl_resource *resource)
3551 wl_resource_destroy(resource);
3555 viewport_set(struct wl_client *client,
3556 struct wl_resource *resource,
3559 wl_fixed_t src_width,
3560 wl_fixed_t src_height,
3564 struct weston_surface *surface =
3565 wl_resource_get_user_data(resource);
3567 assert(surface->viewport_resource != NULL);
3569 if (wl_fixed_to_double(src_width) < 0 ||
3570 wl_fixed_to_double(src_height) < 0) {
3571 wl_resource_post_error(resource,
3572 WL_VIEWPORT_ERROR_BAD_VALUE,
3573 "source dimensions must be non-negative (%fx%f)",
3574 wl_fixed_to_double(src_width),
3575 wl_fixed_to_double(src_height));
3579 if (dst_width <= 0 || dst_height <= 0) {
3580 wl_resource_post_error(resource,
3581 WL_VIEWPORT_ERROR_BAD_VALUE,
3582 "destination dimensions must be positive (%dx%d)",
3583 dst_width, dst_height);
3587 surface->pending.buffer_viewport.buffer.src_x = src_x;
3588 surface->pending.buffer_viewport.buffer.src_y = src_y;
3589 surface->pending.buffer_viewport.buffer.src_width = src_width;
3590 surface->pending.buffer_viewport.buffer.src_height = src_height;
3591 surface->pending.buffer_viewport.surface.width = dst_width;
3592 surface->pending.buffer_viewport.surface.height = dst_height;
3593 surface->pending.buffer_viewport.changed = 1;
3597 viewport_set_source(struct wl_client *client,
3598 struct wl_resource *resource,
3601 wl_fixed_t src_width,
3602 wl_fixed_t src_height)
3604 struct weston_surface *surface =
3605 wl_resource_get_user_data(resource);
3607 assert(surface->viewport_resource != NULL);
3609 if (src_width == wl_fixed_from_int(-1) &&
3610 src_height == wl_fixed_from_int(-1)) {
3611 /* unset source size */
3612 surface->pending.buffer_viewport.buffer.src_width =
3613 wl_fixed_from_int(-1);
3614 surface->pending.buffer_viewport.changed = 1;
3618 if (src_width <= 0 || src_height <= 0) {
3619 wl_resource_post_error(resource,
3620 WL_VIEWPORT_ERROR_BAD_VALUE,
3621 "source size must be positive (%fx%f)",
3622 wl_fixed_to_double(src_width),
3623 wl_fixed_to_double(src_height));
3627 surface->pending.buffer_viewport.buffer.src_x = src_x;
3628 surface->pending.buffer_viewport.buffer.src_y = src_y;
3629 surface->pending.buffer_viewport.buffer.src_width = src_width;
3630 surface->pending.buffer_viewport.buffer.src_height = src_height;
3631 surface->pending.buffer_viewport.changed = 1;
3635 viewport_set_destination(struct wl_client *client,
3636 struct wl_resource *resource,
3640 struct weston_surface *surface =
3641 wl_resource_get_user_data(resource);
3643 assert(surface->viewport_resource != NULL);
3645 if (dst_width == -1 && dst_height == -1) {
3646 /* unset destination size */
3647 surface->pending.buffer_viewport.surface.width = -1;
3648 surface->pending.buffer_viewport.changed = 1;
3652 if (dst_width <= 0 || dst_height <= 0) {
3653 wl_resource_post_error(resource,
3654 WL_VIEWPORT_ERROR_BAD_VALUE,
3655 "destination size must be positive (%dx%d)",
3656 dst_width, dst_height);
3660 surface->pending.buffer_viewport.surface.width = dst_width;
3661 surface->pending.buffer_viewport.surface.height = dst_height;
3662 surface->pending.buffer_viewport.changed = 1;
3665 static const struct wl_viewport_interface viewport_interface = {
3668 viewport_set_source,
3669 viewport_set_destination
3673 scaler_destroy(struct wl_client *client,
3674 struct wl_resource *resource)
3676 wl_resource_destroy(resource);
3680 scaler_get_viewport(struct wl_client *client,
3681 struct wl_resource *scaler,
3683 struct wl_resource *surface_resource)
3685 int version = wl_resource_get_version(scaler);
3686 struct weston_surface *surface =
3687 wl_resource_get_user_data(surface_resource);
3688 struct wl_resource *resource;
3690 if (surface->viewport_resource) {
3691 wl_resource_post_error(scaler,
3692 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3693 "a viewport for that surface already exists");
3697 resource = wl_resource_create(client, &wl_viewport_interface,
3699 if (resource == NULL) {
3700 wl_client_post_no_memory(client);
3704 wl_resource_set_implementation(resource, &viewport_interface,
3705 surface, destroy_viewport);
3707 surface->viewport_resource = resource;
3710 static const struct wl_scaler_interface scaler_interface = {
3716 bind_scaler(struct wl_client *client,
3717 void *data, uint32_t version, uint32_t id)
3719 struct wl_resource *resource;
3721 resource = wl_resource_create(client, &wl_scaler_interface,
3722 MIN(version, 2), id);
3723 if (resource == NULL) {
3724 wl_client_post_no_memory(client);
3728 wl_resource_set_implementation(resource, &scaler_interface,
3733 compositor_bind(struct wl_client *client,
3734 void *data, uint32_t version, uint32_t id)
3736 struct weston_compositor *compositor = data;
3737 struct wl_resource *resource;
3739 resource = wl_resource_create(client, &wl_compositor_interface,
3740 MIN(version, 3), id);
3741 if (resource == NULL) {
3742 wl_client_post_no_memory(client);
3746 wl_resource_set_implementation(resource, &compositor_interface,
3753 struct utsname usys;
3757 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3758 usys.version, usys.machine);
3762 weston_environment_get_fd(const char *env)
3770 fd = strtol(e, &end, 0);
3774 flags = fcntl(fd, F_GETFD);
3778 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3785 weston_compositor_init(struct weston_compositor *ec,
3786 struct wl_display *display,
3787 int *argc, char *argv[],
3788 struct weston_config *config)
3790 struct wl_event_loop *loop;
3791 struct xkb_rule_names xkb_names;
3792 struct weston_config_section *s;
3794 ec->config = config;
3795 ec->wl_display = display;
3796 wl_signal_init(&ec->destroy_signal);
3797 wl_signal_init(&ec->create_surface_signal);
3798 wl_signal_init(&ec->activate_signal);
3799 wl_signal_init(&ec->transform_signal);
3800 wl_signal_init(&ec->kill_signal);
3801 wl_signal_init(&ec->idle_signal);
3802 wl_signal_init(&ec->wake_signal);
3803 wl_signal_init(&ec->show_input_panel_signal);
3804 wl_signal_init(&ec->hide_input_panel_signal);
3805 wl_signal_init(&ec->update_input_panel_signal);
3806 wl_signal_init(&ec->seat_created_signal);
3807 wl_signal_init(&ec->output_created_signal);
3808 wl_signal_init(&ec->output_destroyed_signal);
3809 wl_signal_init(&ec->output_moved_signal);
3810 wl_signal_init(&ec->session_signal);
3811 ec->session_active = 1;
3813 ec->output_id_pool = 0;
3815 if (!wl_global_create(display, &wl_compositor_interface, 3,
3816 ec, compositor_bind))
3819 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3820 ec, bind_subcompositor))
3823 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
3827 wl_list_init(&ec->view_list);
3828 wl_list_init(&ec->plane_list);
3829 wl_list_init(&ec->layer_list);
3830 wl_list_init(&ec->seat_list);
3831 wl_list_init(&ec->output_list);
3832 wl_list_init(&ec->key_binding_list);
3833 wl_list_init(&ec->modifier_binding_list);
3834 wl_list_init(&ec->button_binding_list);
3835 wl_list_init(&ec->touch_binding_list);
3836 wl_list_init(&ec->axis_binding_list);
3837 wl_list_init(&ec->debug_binding_list);
3839 weston_plane_init(&ec->primary_plane, ec, 0, 0);
3840 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
3842 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3843 weston_config_section_get_string(s, "keymap_rules",
3844 (char **) &xkb_names.rules, NULL);
3845 weston_config_section_get_string(s, "keymap_model",
3846 (char **) &xkb_names.model, NULL);
3847 weston_config_section_get_string(s, "keymap_layout",
3848 (char **) &xkb_names.layout, NULL);
3849 weston_config_section_get_string(s, "keymap_variant",
3850 (char **) &xkb_names.variant, NULL);
3851 weston_config_section_get_string(s, "keymap_options",
3852 (char **) &xkb_names.options, NULL);
3854 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3857 weston_config_section_get_int(s, "repeat-rate",
3858 &ec->kb_repeat_rate, 40);
3859 weston_config_section_get_int(s, "repeat-delay",
3860 &ec->kb_repeat_delay, 400);
3862 text_backend_init(ec);
3864 wl_data_device_manager_init(ec->wl_display);
3866 wl_display_init_shm(display);
3868 loop = wl_display_get_event_loop(ec->wl_display);
3869 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3870 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3872 ec->input_loop = wl_event_loop_create();
3874 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3875 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3877 weston_compositor_schedule_repaint(ec);
3883 weston_compositor_shutdown(struct weston_compositor *ec)
3885 struct weston_output *output, *next;
3887 wl_event_source_remove(ec->idle_source);
3888 if (ec->input_loop_source)
3889 wl_event_source_remove(ec->input_loop_source);
3891 /* Destroy all outputs associated with this compositor */
3892 wl_list_for_each_safe(output, next, &ec->output_list, link)
3893 output->destroy(output);
3896 ec->renderer->destroy(ec);
3898 weston_binding_list_destroy_all(&ec->key_binding_list);
3899 weston_binding_list_destroy_all(&ec->button_binding_list);
3900 weston_binding_list_destroy_all(&ec->touch_binding_list);
3901 weston_binding_list_destroy_all(&ec->axis_binding_list);
3902 weston_binding_list_destroy_all(&ec->debug_binding_list);
3904 weston_plane_release(&ec->primary_plane);
3906 wl_event_loop_destroy(ec->input_loop);
3908 weston_config_destroy(ec->config);
3912 weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3913 const struct weston_pointer_grab_interface *interface)
3915 struct weston_seat *seat;
3917 ec->default_pointer_grab = interface;
3918 wl_list_for_each(seat, &ec->seat_list, link) {
3919 if (seat->pointer) {
3920 weston_pointer_set_default_grab(seat->pointer,
3927 weston_version(int *major, int *minor, int *micro)
3929 *major = WESTON_VERSION_MAJOR;
3930 *minor = WESTON_VERSION_MINOR;
3931 *micro = WESTON_VERSION_MICRO;
3934 static const struct {
3935 uint32_t bit; /* enum weston_capability */
3937 } capability_strings[] = {
3938 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
3939 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
3943 weston_compositor_log_capabilities(struct weston_compositor *compositor)
3948 weston_log("Compositor capabilities:\n");
3949 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3950 yes = compositor->capabilities & capability_strings[i].bit;
3951 weston_log_continue(STAMP_SPACE "%s %s\n",
3952 capability_strings[i].desc,
3953 yes ? "yes" : "no");
3957 static int on_term_signal(int signal_number, void *data)
3959 struct wl_display *display = data;
3961 weston_log("caught signal %d\n", signal_number);
3962 wl_display_terminate(display);
3967 #ifdef HAVE_LIBUNWIND
3970 print_backtrace(void)
3972 unw_cursor_t cursor;
3973 unw_context_t context;
3975 unw_proc_info_t pip;
3978 const char *filename;
3981 pip.unwind_info = NULL;
3982 ret = unw_getcontext(&context);
3984 weston_log("unw_getcontext: %d\n", ret);
3988 ret = unw_init_local(&cursor, &context);
3990 weston_log("unw_init_local: %d\n", ret);
3994 ret = unw_step(&cursor);
3996 ret = unw_get_proc_info(&cursor, &pip);
3998 weston_log("unw_get_proc_info: %d\n", ret);
4002 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4003 if (ret && ret != -UNW_ENOMEM) {
4004 if (ret != -UNW_EUNSPEC)
4005 weston_log("unw_get_proc_name: %d\n", ret);
4010 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4012 filename = dlinfo.dli_fname;
4016 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4017 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4019 ret = unw_step(&cursor);
4021 weston_log("unw_step: %d\n", ret);
4028 print_backtrace(void)
4034 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4035 for (i = 0; i < count; i++) {
4036 dladdr(buffer[i], &info);
4037 weston_log(" [%016lx] %s (%s)\n",
4039 info.dli_sname ? info.dli_sname : "--",
4047 on_caught_signal(int s, siginfo_t *siginfo, void *context)
4049 /* This signal handler will do a best-effort backtrace, and
4050 * then call the backend restore function, which will switch
4051 * back to the vt we launched from or ungrab X etc and then
4052 * raise SIGTRAP. If we run weston under gdb from X or a
4053 * different vt, and tell gdb "handle *s* nostop", this
4054 * will allow weston to switch back to gdb on crash and then
4055 * gdb will catch the crash with SIGTRAP.*/
4057 weston_log("caught signal: %d\n", s);
4061 segv_compositor->restore(segv_compositor);
4067 weston_load_module(const char *name, const char *entrypoint)
4069 char path[PATH_MAX];
4070 void *module, *init;
4076 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
4078 snprintf(path, sizeof path, "%s", name);
4080 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4082 weston_log("Module '%s' already loaded\n", path);
4087 weston_log("Loading module '%s'\n", path);
4088 module = dlopen(path, RTLD_NOW);
4090 weston_log("Failed to load module: %s\n", dlerror());
4094 init = dlsym(module, entrypoint);
4096 weston_log("Failed to lookup init function: %s\n", dlerror());
4105 load_modules(struct weston_compositor *ec, const char *modules,
4106 int *argc, char *argv[])
4108 const char *p, *end;
4110 int (*module_init)(struct weston_compositor *ec,
4111 int *argc, char *argv[]);
4113 if (modules == NULL)
4118 end = strchrnul(p, ',');
4119 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
4120 module_init = weston_load_module(buffer, "module_init");
4122 module_init(ec, argc, argv);
4132 static const char xdg_error_message[] =
4133 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4135 static const char xdg_wrong_message[] =
4136 "fatal: environment variable XDG_RUNTIME_DIR\n"
4137 "is set to \"%s\", which is not a directory.\n";
4139 static const char xdg_wrong_mode_message[] =
4140 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
4141 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4142 "and must be owned by the user (current owner is UID %d).\n";
4144 static const char xdg_detail_message[] =
4145 "Refer to your distribution on how to get it, or\n"
4146 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4147 "on how to implement it.\n";
4150 verify_xdg_runtime_dir(void)
4152 char *dir = getenv("XDG_RUNTIME_DIR");
4156 weston_log(xdg_error_message);
4157 weston_log_continue(xdg_detail_message);
4161 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4162 weston_log(xdg_wrong_message, dir);
4163 weston_log_continue(xdg_detail_message);
4167 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4168 weston_log(xdg_wrong_mode_message,
4169 dir, s.st_mode & 0777, s.st_uid);
4170 weston_log_continue(xdg_detail_message);
4175 usage(int error_code)
4178 "Usage: weston [OPTIONS]\n\n"
4179 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4180 "Weston supports multiple backends, and depending on which backend is in use\n"
4181 "different options will be accepted.\n\n"
4185 " --version\t\tPrint weston version\n"
4186 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
4187 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4188 "\t\t\t\twayland-backend.so\n"
4189 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
4190 " -S, --socket=NAME\tName of socket to listen on\n"
4191 " -i, --idle-time=SECS\tIdle time in seconds\n"
4192 " --modules\t\tLoad the comma-separated list of modules\n"
4193 " --log==FILE\t\tLog to the given file\n"
4194 " --no-config\t\tDo not read weston.ini\n"
4195 " -h, --help\t\tThis help message\n\n");
4198 "Options for drm-backend.so:\n\n"
4199 " --connector=ID\tBring up only this connector\n"
4200 " --seat=SEAT\t\tThe seat that weston should run on\n"
4201 " --tty=TTY\t\tThe tty to use\n"
4202 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
4203 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4206 "Options for fbdev-backend.so:\n\n"
4207 " --tty=TTY\t\tThe tty to use\n"
4208 " --device=DEVICE\tThe framebuffer device to use\n\n");
4211 "Options for x11-backend.so:\n\n"
4212 " --width=WIDTH\t\tWidth of X window\n"
4213 " --height=HEIGHT\tHeight of X window\n"
4214 " --fullscreen\t\tRun in fullscreen mode\n"
4215 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
4216 " --output-count=COUNT\tCreate multiple outputs\n"
4217 " --no-input\t\tDont create input devices\n\n");
4220 "Options for wayland-backend.so:\n\n"
4221 " --width=WIDTH\t\tWidth of Wayland surface\n"
4222 " --height=HEIGHT\tHeight of Wayland surface\n"
4223 " --scale=SCALE\tScale factor of ouput\n"
4224 " --fullscreen\t\tRun in fullscreen mode\n"
4225 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
4226 " --output-count=COUNT\tCreate multiple outputs\n"
4227 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
4228 " --display=DISPLAY\tWayland display to connect to\n\n");
4230 #if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4232 "Options for rpi-backend.so:\n\n"
4233 " --tty=TTY\t\tThe tty to use\n"
4234 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4235 " --transform=TR\tThe output transformation, TR is one of:\n"
4236 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4237 " --opaque-regions\tEnable support for opaque regions, can be "
4238 "very slow without support in the GPU firmware.\n"
4242 #if defined(BUILD_RDP_COMPOSITOR)
4244 "Options for rdp-backend.so:\n\n"
4245 " --width=WIDTH\t\tWidth of desktop\n"
4246 " --height=HEIGHT\tHeight of desktop\n"
4247 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4248 " --address=ADDR\tThe address to bind\n"
4249 " --port=PORT\tThe port to listen on\n"
4250 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4251 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4252 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4253 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4263 struct sigaction action;
4265 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4266 action.sa_sigaction = on_caught_signal;
4267 sigemptyset(&action.sa_mask);
4268 sigaction(SIGSEGV, &action, NULL);
4269 sigaction(SIGABRT, &action, NULL);
4273 handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4275 struct wl_client *client = data;
4277 weston_log("Primary client died. Closing...\n");
4279 wl_display_terminate(wl_client_get_display(client));
4283 weston_choose_default_backend(void)
4285 char *backend = NULL;
4287 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4288 backend = strdup("wayland-backend.so");
4289 else if (getenv("DISPLAY"))
4290 backend = strdup("x11-backend.so");
4292 backend = strdup(WESTON_NATIVE_BACKEND);
4298 weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4301 if (wl_display_add_socket(display, socket_name)) {
4302 weston_log("fatal: failed to add socket: %m\n");
4306 socket_name = wl_display_add_socket_auto(display);
4308 weston_log("fatal: failed to add socket: %m\n");
4313 setenv("WAYLAND_DISPLAY", socket_name, 1);
4318 int main(int argc, char *argv[])
4320 int ret = EXIT_SUCCESS;
4321 struct wl_display *display;
4322 struct weston_compositor *ec;
4323 struct wl_event_source *signals[4];
4324 struct wl_event_loop *loop;
4325 struct weston_compositor
4326 *(*backend_init)(struct wl_display *display,
4327 int *argc, char *argv[],
4328 struct weston_config *config);
4330 char *backend = NULL;
4331 char *option_backend = NULL;
4333 char *option_shell = NULL;
4334 char *modules, *option_modules = NULL;
4336 char *server_socket = NULL, *end;
4337 int32_t idle_time = 300;
4339 const char *socket_name = NULL;
4340 int32_t version = 0;
4341 int32_t noconfig = 0;
4343 struct weston_config *config = NULL;
4344 struct weston_config_section *section;
4345 struct wl_client *primary_client;
4346 struct wl_listener primary_client_destroyed;
4347 struct weston_seat *seat;
4349 const struct weston_option core_options[] = {
4350 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
4351 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
4352 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4353 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
4354 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
4355 { WESTON_OPTION_STRING, "log", 0, &log },
4356 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
4357 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
4358 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
4361 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
4364 usage(EXIT_SUCCESS);
4367 printf(PACKAGE_STRING "\n");
4368 return EXIT_SUCCESS;
4371 weston_log_file_open(log);
4375 STAMP_SPACE "Bug reports to: %s\n"
4376 STAMP_SPACE "Build: %s\n",
4377 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
4381 verify_xdg_runtime_dir();
4383 display = wl_display_create();
4385 loop = wl_display_get_event_loop(display);
4386 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4388 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4390 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4393 wl_list_init(&child_process_list);
4394 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4397 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4403 config = weston_config_parse("weston.ini");
4404 if (config != NULL) {
4405 weston_log("Using config file '%s'\n",
4406 weston_config_get_full_path(config));
4408 weston_log("Starting with no config file.\n");
4410 section = weston_config_get_section(config, "core", NULL, NULL);
4413 backend = strdup(option_backend);
4415 weston_config_section_get_string(section, "backend", &backend,
4419 backend = weston_choose_default_backend();
4421 backend_init = weston_load_module(backend, "backend_init");
4423 if (!backend_init) {
4428 ec = backend_init(display, &argc, argv, config);
4430 weston_log("fatal: failed to create compositor\n");
4436 segv_compositor = ec;
4438 ec->idle_time = idle_time;
4439 ec->default_pointer_grab = NULL;
4441 for (i = 1; i < argc; i++)
4442 weston_log("fatal: unhandled option: %s\n", argv[i]);
4448 weston_compositor_log_capabilities(ec);
4450 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4451 if (server_socket) {
4452 weston_log("Running with single client\n");
4453 fd = strtol(server_socket, &end, 0);
4461 primary_client = wl_client_create(display, fd);
4462 if (!primary_client) {
4463 weston_log("fatal: failed to add client: %m\n");
4467 primary_client_destroyed.notify =
4468 handle_primary_client_destroyed;
4469 wl_client_add_destroy_listener(primary_client,
4470 &primary_client_destroyed);
4471 } else if (weston_create_listening_socket(display, socket_name)) {
4477 shell = strdup(option_shell);
4479 weston_config_section_get_string(section, "shell", &shell,
4480 "desktop-shell.so");
4482 if (load_modules(ec, shell, &argc, argv) < 0) {
4488 weston_config_section_get_string(section, "modules", &modules, "");
4489 if (load_modules(ec, modules, &argc, argv) < 0) {
4495 if (load_modules(ec, option_modules, &argc, argv) < 0)
4498 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4499 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4501 wl_list_for_each(seat, &ec->seat_list, link) {
4503 weston_keyboard_set_locks(seat->keyboard,
4509 weston_compositor_wake(ec);
4511 wl_display_run(display);
4514 /* prevent further rendering while shutting down */
4515 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
4517 wl_signal_emit(&ec->destroy_signal, ec);
4519 weston_compositor_xkb_destroy(ec);
4524 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4526 wl_event_source_remove(signals[i]);
4528 wl_display_destroy(display);
4530 weston_log_file_close();