2 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
4 * Copyright © 2012 Collabora, Ltd.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/utsname.h>
45 #include <linux/input.h>
52 #include <wayland-server.h>
53 #include "compositor.h"
54 #include "../shared/os-compatibility.h"
55 #include "git-version.h"
57 static struct wl_list child_process_list;
58 static struct weston_compositor *segv_compositor;
61 sigchld_handler(int signal_number, void *data)
63 struct weston_process *p;
67 pid = waitpid(-1, &status, WNOHANG);
71 wl_list_for_each(p, &child_process_list, link) {
76 if (&p->link == &child_process_list) {
77 weston_log("unknown child process exited\n");
81 wl_list_remove(&p->link);
82 p->cleanup(p, status);
88 weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
90 if (!output->switch_mode)
93 return output->switch_mode(output, mode);
97 weston_watch_process(struct weston_process *process)
99 wl_list_insert(&child_process_list, &process->link);
103 child_client_exec(int sockfd, const char *path)
109 /* do not give our signal mask to the new process */
110 sigfillset(&allsigs);
111 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
113 /* Launch clients as the user. */
116 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
117 * non-CLOEXEC fd to pass through exec. */
118 clientfd = dup(sockfd);
119 if (clientfd == -1) {
120 weston_log("compositor: dup failed: %m\n");
124 snprintf(s, sizeof s, "%d", clientfd);
125 setenv("WAYLAND_SOCKET", s, 1);
127 if (execl(path, path, NULL) < 0)
128 weston_log("compositor: executing '%s' failed: %m\n",
132 WL_EXPORT struct wl_client *
133 weston_client_launch(struct weston_compositor *compositor,
134 struct weston_process *proc,
136 weston_process_cleanup_func_t cleanup)
140 struct wl_client *client;
142 weston_log("launching '%s'\n", path);
144 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
145 weston_log("weston_client_launch: "
146 "socketpair failed while launching '%s': %m\n",
155 weston_log("weston_client_launch: "
156 "fork failed while launching '%s': %m\n", path);
161 child_client_exec(sv[1], path);
167 client = wl_client_create(compositor->wl_display, sv[0]);
170 weston_log("weston_client_launch: "
171 "wl_client_create failed while launching '%s'.\n",
177 proc->cleanup = cleanup;
178 weston_watch_process(proc);
184 surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
186 struct weston_surface *es =
187 container_of(listener, struct weston_surface,
188 buffer_destroy_listener);
190 if (es->buffer && wl_buffer_is_shm(es->buffer))
191 es->compositor->renderer->flush_damage(es);
197 surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
199 struct weston_surface *surface =
200 container_of(listener, struct weston_surface,
201 pending.buffer_destroy_listener);
203 surface->pending.buffer = NULL;
207 empty_region(pixman_region32_t *region)
209 pixman_region32_fini(region);
210 pixman_region32_init(region);
214 region_init_infinite(pixman_region32_t *region)
216 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
217 UINT32_MAX, UINT32_MAX);
220 WL_EXPORT struct weston_surface *
221 weston_surface_create(struct weston_compositor *compositor)
223 struct weston_surface *surface;
225 surface = calloc(1, sizeof *surface);
229 wl_signal_init(&surface->surface.resource.destroy_signal);
231 wl_list_init(&surface->link);
232 wl_list_init(&surface->layer_link);
234 surface->surface.resource.client = NULL;
236 surface->compositor = compositor;
237 surface->alpha = 1.0;
240 surface->num_textures = 0;
241 surface->num_images = 0;
243 surface->buffer = NULL;
244 surface->output = NULL;
245 surface->plane = &compositor->primary_plane;
247 pixman_region32_init(&surface->damage);
248 pixman_region32_init(&surface->opaque);
249 pixman_region32_init(&surface->clip);
250 region_init_infinite(&surface->input);
251 pixman_region32_init(&surface->transform.opaque);
252 wl_list_init(&surface->frame_callback_list);
254 surface->buffer_destroy_listener.notify =
255 surface_handle_buffer_destroy;
257 wl_list_init(&surface->geometry.transformation_list);
258 wl_list_insert(&surface->geometry.transformation_list,
259 &surface->transform.position.link);
260 weston_matrix_init(&surface->transform.position.matrix);
261 pixman_region32_init(&surface->transform.boundingbox);
262 surface->geometry.dirty = 1;
264 surface->pending.buffer_destroy_listener.notify =
265 surface_handle_pending_buffer_destroy;
266 pixman_region32_init(&surface->pending.damage);
267 pixman_region32_init(&surface->pending.opaque);
268 region_init_infinite(&surface->pending.input);
269 wl_list_init(&surface->pending.frame_callback_list);
275 weston_surface_set_color(struct weston_surface *surface,
276 float red, float green, float blue, float alpha)
278 surface->color[0] = red;
279 surface->color[1] = green;
280 surface->color[2] = blue;
281 surface->color[3] = alpha;
282 surface->shader = &surface->compositor->solid_shader;
286 weston_surface_to_global_float(struct weston_surface *surface,
287 float sx, float sy, float *x, float *y)
289 if (surface->transform.enabled) {
290 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
292 weston_matrix_transform(&surface->transform.matrix, &v);
294 if (fabsf(v.f[3]) < 1e-6) {
295 weston_log("warning: numerical instability in "
296 "weston_surface_to_global(), divisor = %g\n",
303 *x = v.f[0] / v.f[3];
304 *y = v.f[1] / v.f[3];
306 *x = sx + surface->geometry.x;
307 *y = sy + surface->geometry.y;
312 weston_surface_move_to_plane(struct weston_surface *surface,
313 struct weston_plane *plane)
315 if (surface->plane == plane)
318 weston_surface_damage_below(surface);
319 surface->plane = plane;
320 weston_surface_damage(surface);
324 weston_surface_damage_below(struct weston_surface *surface)
326 pixman_region32_t damage;
328 pixman_region32_init(&damage);
329 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
331 pixman_region32_union(&surface->plane->damage,
332 &surface->plane->damage, &damage);
333 pixman_region32_fini(&damage);
336 static struct wl_resource *
337 find_resource_for_client(struct wl_list *list, struct wl_client *client)
339 struct wl_resource *r;
341 wl_list_for_each(r, list, link) {
342 if (r->client == client)
350 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
352 uint32_t different = es->output_mask ^ mask;
353 uint32_t entered = mask & different;
354 uint32_t left = es->output_mask & different;
355 struct weston_output *output;
356 struct wl_resource *resource = NULL;
357 struct wl_client *client = es->surface.resource.client;
359 es->output_mask = mask;
360 if (es->surface.resource.client == NULL)
365 wl_list_for_each(output, &es->compositor->output_list, link) {
366 if (1 << output->id & different)
368 find_resource_for_client(&output->resource_list,
370 if (resource == NULL)
372 if (1 << output->id & entered)
373 wl_surface_send_enter(&es->surface.resource, resource);
374 if (1 << output->id & left)
375 wl_surface_send_leave(&es->surface.resource, resource);
380 weston_surface_assign_output(struct weston_surface *es)
382 struct weston_compositor *ec = es->compositor;
383 struct weston_output *output, *new_output;
384 pixman_region32_t region;
385 uint32_t max, area, mask;
391 pixman_region32_init(®ion);
392 wl_list_for_each(output, &ec->output_list, link) {
393 pixman_region32_intersect(®ion, &es->transform.boundingbox,
396 e = pixman_region32_extents(®ion);
397 area = (e->x2 - e->x1) * (e->y2 - e->y1);
400 mask |= 1 << output->id;
407 pixman_region32_fini(®ion);
409 es->output = new_output;
410 weston_surface_update_output_mask(es, mask);
414 surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
415 int32_t width, int32_t height,
416 pixman_region32_t *bbox)
418 float min_x = HUGE_VALF, min_y = HUGE_VALF;
419 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
424 { sx + width, sy + height }
429 if (width == 0 || height == 0) {
430 /* avoid rounding empty bbox to 1x1 */
431 pixman_region32_init(bbox);
435 for (i = 0; i < 4; ++i) {
437 weston_surface_to_global_float(surface,
438 s[i][0], s[i][1], &x, &y);
449 int_x = floorf(min_x);
450 int_y = floorf(min_y);
451 pixman_region32_init_rect(bbox, int_x, int_y,
452 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
456 weston_surface_update_transform_disable(struct weston_surface *surface)
458 surface->transform.enabled = 0;
460 /* round off fractions when not transformed */
461 surface->geometry.x = roundf(surface->geometry.x);
462 surface->geometry.y = roundf(surface->geometry.y);
464 pixman_region32_init_rect(&surface->transform.boundingbox,
467 surface->geometry.width,
468 surface->geometry.height);
470 if (surface->alpha == 1.0) {
471 pixman_region32_copy(&surface->transform.opaque,
473 pixman_region32_translate(&surface->transform.opaque,
475 surface->geometry.y);
480 weston_surface_update_transform_enable(struct weston_surface *surface)
482 struct weston_matrix *matrix = &surface->transform.matrix;
483 struct weston_matrix *inverse = &surface->transform.inverse;
484 struct weston_transform *tform;
486 surface->transform.enabled = 1;
488 /* Otherwise identity matrix, but with x and y translation. */
489 surface->transform.position.matrix.d[12] = surface->geometry.x;
490 surface->transform.position.matrix.d[13] = surface->geometry.y;
492 weston_matrix_init(matrix);
493 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
494 weston_matrix_multiply(matrix, &tform->matrix);
496 if (weston_matrix_invert(inverse, matrix) < 0) {
497 /* Oops, bad total transformation, not invertible */
498 weston_log("error: weston_surface %p"
499 " transformation not invertible.\n", surface);
503 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
504 surface->geometry.height,
505 &surface->transform.boundingbox);
511 weston_surface_update_transform(struct weston_surface *surface)
513 if (!surface->geometry.dirty)
516 surface->geometry.dirty = 0;
518 weston_surface_damage_below(surface);
520 pixman_region32_fini(&surface->transform.boundingbox);
521 pixman_region32_fini(&surface->transform.opaque);
522 pixman_region32_init(&surface->transform.opaque);
524 /* transform.position is always in transformation_list */
525 if (surface->geometry.transformation_list.next ==
526 &surface->transform.position.link &&
527 surface->geometry.transformation_list.prev ==
528 &surface->transform.position.link) {
529 weston_surface_update_transform_disable(surface);
531 if (weston_surface_update_transform_enable(surface) < 0)
532 weston_surface_update_transform_disable(surface);
535 weston_surface_damage_below(surface);
537 weston_surface_assign_output(surface);
541 weston_surface_to_global_fixed(struct weston_surface *surface,
542 wl_fixed_t sx, wl_fixed_t sy,
543 wl_fixed_t *x, wl_fixed_t *y)
547 weston_surface_to_global_float(surface,
548 wl_fixed_to_double(sx),
549 wl_fixed_to_double(sy),
551 *x = wl_fixed_from_double(xf);
552 *y = wl_fixed_from_double(yf);
556 weston_surface_from_global_float(struct weston_surface *surface,
557 float x, float y, float *sx, float *sy)
559 if (surface->transform.enabled) {
560 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
562 weston_matrix_transform(&surface->transform.inverse, &v);
564 if (fabsf(v.f[3]) < 1e-6) {
565 weston_log("warning: numerical instability in "
566 "weston_surface_from_global(), divisor = %g\n",
573 *sx = v.f[0] / v.f[3];
574 *sy = v.f[1] / v.f[3];
576 *sx = x - surface->geometry.x;
577 *sy = y - surface->geometry.y;
582 weston_surface_from_global_fixed(struct weston_surface *surface,
583 wl_fixed_t x, wl_fixed_t y,
584 wl_fixed_t *sx, wl_fixed_t *sy)
588 weston_surface_from_global_float(surface,
589 wl_fixed_to_double(x),
590 wl_fixed_to_double(y),
592 *sx = wl_fixed_from_double(sxf);
593 *sy = wl_fixed_from_double(syf);
597 weston_surface_from_global(struct weston_surface *surface,
598 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
602 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
608 weston_surface_schedule_repaint(struct weston_surface *surface)
610 struct weston_output *output;
612 wl_list_for_each(output, &surface->compositor->output_list, link)
613 if (surface->output_mask & (1 << output->id))
614 weston_output_schedule_repaint(output);
618 weston_surface_damage(struct weston_surface *surface)
620 pixman_region32_union_rect(&surface->damage, &surface->damage,
621 0, 0, surface->geometry.width,
622 surface->geometry.height);
624 weston_surface_schedule_repaint(surface);
628 weston_surface_configure(struct weston_surface *surface,
629 float x, float y, int width, int height)
631 surface->geometry.x = x;
632 surface->geometry.y = y;
633 surface->geometry.width = width;
634 surface->geometry.height = height;
635 surface->geometry.dirty = 1;
639 weston_surface_set_position(struct weston_surface *surface,
642 surface->geometry.x = x;
643 surface->geometry.y = y;
644 surface->geometry.dirty = 1;
648 weston_surface_is_mapped(struct weston_surface *surface)
657 weston_compositor_get_time(void)
661 gettimeofday(&tv, NULL);
663 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
666 static struct weston_surface *
667 weston_compositor_pick_surface(struct weston_compositor *compositor,
668 wl_fixed_t x, wl_fixed_t y,
669 wl_fixed_t *sx, wl_fixed_t *sy)
671 struct weston_surface *surface;
673 wl_list_for_each(surface, &compositor->surface_list, link) {
674 weston_surface_from_global_fixed(surface, x, y, sx, sy);
675 if (pixman_region32_contains_point(&surface->input,
676 wl_fixed_to_int(*sx),
677 wl_fixed_to_int(*sy),
686 weston_device_repick(struct weston_seat *seat)
688 const struct wl_pointer_grab_interface *interface;
689 struct weston_surface *surface, *focus;
690 struct wl_pointer *pointer = seat->seat.pointer;
695 surface = weston_compositor_pick_surface(seat->compositor,
699 &pointer->current_y);
701 if (&surface->surface != pointer->current) {
702 interface = pointer->grab->interface;
703 pointer->current = &surface->surface;
704 interface->focus(pointer->grab, &surface->surface,
709 focus = (struct weston_surface *) pointer->grab->focus;
711 weston_surface_from_global_fixed(focus,
719 weston_compositor_repick(struct weston_compositor *compositor)
721 struct weston_seat *seat;
723 if (!compositor->focus)
726 wl_list_for_each(seat, &compositor->seat_list, link)
727 weston_device_repick(seat);
731 weston_surface_unmap(struct weston_surface *surface)
733 struct weston_seat *seat;
735 weston_surface_damage_below(surface);
736 surface->output = NULL;
737 wl_list_remove(&surface->layer_link);
739 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
740 if (seat->seat.keyboard &&
741 seat->seat.keyboard->focus == &surface->surface)
742 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
743 if (seat->seat.pointer &&
744 seat->seat.pointer->focus == &surface->surface)
745 wl_pointer_set_focus(seat->seat.pointer,
747 wl_fixed_from_int(0),
748 wl_fixed_from_int(0));
751 weston_surface_schedule_repaint(surface);
754 struct weston_frame_callback {
755 struct wl_resource resource;
760 destroy_surface(struct wl_resource *resource)
762 struct weston_surface *surface =
763 container_of(resource,
764 struct weston_surface, surface.resource);
765 struct weston_compositor *compositor = surface->compositor;
766 struct weston_frame_callback *cb, *next;
768 if (weston_surface_is_mapped(surface))
769 weston_surface_unmap(surface);
771 wl_list_for_each_safe(cb, next,
772 &surface->pending.frame_callback_list, link)
773 wl_resource_destroy(&cb->resource);
775 pixman_region32_fini(&surface->pending.input);
776 pixman_region32_fini(&surface->pending.opaque);
777 pixman_region32_fini(&surface->pending.damage);
779 if (surface->pending.buffer)
780 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
783 wl_list_remove(&surface->buffer_destroy_listener.link);
785 compositor->renderer->destroy_surface(surface);
787 pixman_region32_fini(&surface->transform.boundingbox);
788 pixman_region32_fini(&surface->damage);
789 pixman_region32_fini(&surface->opaque);
790 pixman_region32_fini(&surface->clip);
791 pixman_region32_fini(&surface->input);
793 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
794 wl_resource_destroy(&cb->resource);
800 weston_surface_destroy(struct weston_surface *surface)
802 /* Not a valid way to destroy a client surface */
803 assert(surface->surface.resource.client == NULL);
805 wl_signal_emit(&surface->surface.resource.destroy_signal,
806 &surface->surface.resource);
807 destroy_surface(&surface->surface.resource);
811 weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
813 if (surface->buffer) {
814 weston_buffer_post_release(surface->buffer);
815 wl_list_remove(&surface->buffer_destroy_listener.link);
819 buffer->busy_count++;
820 wl_signal_add(&buffer->resource.destroy_signal,
821 &surface->buffer_destroy_listener);
823 if (weston_surface_is_mapped(surface))
824 weston_surface_unmap(surface);
827 surface->compositor->renderer->attach(surface, buffer);
828 surface->buffer = buffer;
832 weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
834 wl_list_remove(&surface->layer_link);
835 wl_list_insert(below, &surface->layer_link);
836 weston_surface_damage_below(surface);
837 weston_surface_damage(surface);
841 weston_compositor_damage_all(struct weston_compositor *compositor)
843 struct weston_output *output;
845 wl_list_for_each(output, &compositor->output_list, link)
846 weston_output_damage(output);
850 weston_buffer_post_release(struct wl_buffer *buffer)
852 if (--buffer->busy_count > 0)
855 assert(buffer->resource.client != NULL);
856 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
860 weston_output_damage(struct weston_output *output)
862 struct weston_compositor *compositor = output->compositor;
864 pixman_region32_union(&compositor->primary_plane.damage,
865 &compositor->primary_plane.damage,
867 weston_output_schedule_repaint(output);
871 fade_frame(struct weston_animation *animation,
872 struct weston_output *output, uint32_t msecs)
874 struct weston_compositor *compositor =
875 container_of(animation,
876 struct weston_compositor, fade.animation);
877 struct weston_surface *surface;
879 if (animation->frame_counter <= 1)
880 compositor->fade.spring.timestamp = msecs;
882 surface = compositor->fade.surface;
883 weston_spring_update(&compositor->fade.spring, msecs);
884 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
885 compositor->fade.spring.current);
886 weston_surface_damage(surface);
888 if (weston_spring_done(&compositor->fade.spring)) {
889 compositor->fade.spring.current =
890 compositor->fade.spring.target;
891 wl_list_remove(&animation->link);
892 wl_list_init(&animation->link);
894 if (compositor->fade.spring.current < 0.001) {
895 destroy_surface(&surface->surface.resource);
896 compositor->fade.surface = NULL;
897 } else if (compositor->fade.spring.current > 0.999) {
898 compositor->state = WESTON_COMPOSITOR_SLEEPING;
899 wl_signal_emit(&compositor->lock_signal, compositor);
905 surface_accumulate_damage(struct weston_surface *surface,
906 pixman_region32_t *opaque)
908 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
909 surface->compositor->renderer->flush_damage(surface);
911 if (surface->transform.enabled) {
912 pixman_box32_t *extents;
914 extents = pixman_region32_extents(&surface->damage);
915 surface_compute_bbox(surface, extents->x1, extents->y1,
916 extents->x2 - extents->x1,
917 extents->y2 - extents->y1,
919 pixman_region32_translate(&surface->damage,
923 pixman_region32_translate(&surface->damage,
924 surface->geometry.x - surface->plane->x,
925 surface->geometry.y - surface->plane->y);
928 pixman_region32_union(&surface->plane->damage,
929 &surface->plane->damage, &surface->damage);
930 empty_region(&surface->damage);
931 pixman_region32_copy(&surface->clip, opaque);
932 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
936 weston_output_repaint(struct weston_output *output, uint32_t msecs)
938 struct weston_compositor *ec = output->compositor;
939 struct weston_surface *es;
940 struct weston_layer *layer;
941 struct weston_animation *animation, *next;
942 struct weston_frame_callback *cb, *cnext;
943 struct wl_list frame_callback_list;
944 pixman_region32_t opaque, output_damage;
946 weston_compositor_update_drag_surfaces(ec);
948 /* Rebuild the surface list and update surface transforms up front. */
949 wl_list_init(&ec->surface_list);
950 wl_list_init(&frame_callback_list);
951 wl_list_for_each(layer, &ec->layer_list, link) {
952 wl_list_for_each(es, &layer->surface_list, layer_link) {
953 weston_surface_update_transform(es);
954 wl_list_insert(ec->surface_list.prev, &es->link);
955 if (es->output == output) {
956 wl_list_insert_list(&frame_callback_list,
957 &es->frame_callback_list);
958 wl_list_init(&es->frame_callback_list);
963 if (output->assign_planes && !output->disable_planes)
964 output->assign_planes(output);
966 wl_list_for_each(es, &ec->surface_list, link)
967 weston_surface_move_to_plane(es, &ec->primary_plane);
969 pixman_region32_init(&opaque);
971 wl_list_for_each(es, &ec->surface_list, link)
972 surface_accumulate_damage(es, &opaque);
974 pixman_region32_fini(&opaque);
976 pixman_region32_init(&output_damage);
977 pixman_region32_intersect(&output_damage,
978 &ec->primary_plane.damage, &output->region);
979 pixman_region32_subtract(&ec->primary_plane.damage,
980 &ec->primary_plane.damage, &output->region);
983 weston_output_update_matrix(output);
985 output->repaint(output, &output_damage);
987 pixman_region32_fini(&output_damage);
989 output->repaint_needed = 0;
991 weston_compositor_repick(ec);
992 wl_event_loop_dispatch(ec->input_loop, 0);
994 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
995 wl_callback_send_done(&cb->resource, msecs);
996 wl_resource_destroy(&cb->resource);
999 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
1000 animation->frame_counter++;
1001 animation->frame(animation, output, msecs);
1006 weston_compositor_read_input(int fd, uint32_t mask, void *data)
1008 struct weston_compositor *compositor = data;
1010 wl_event_loop_dispatch(compositor->input_loop, 0);
1016 weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
1018 struct weston_compositor *compositor = output->compositor;
1019 struct wl_event_loop *loop =
1020 wl_display_get_event_loop(compositor->wl_display);
1023 output->frame_time = msecs;
1024 if (output->repaint_needed) {
1025 weston_output_repaint(output, msecs);
1029 output->repaint_scheduled = 0;
1030 if (compositor->input_loop_source)
1033 fd = wl_event_loop_get_fd(compositor->input_loop);
1034 compositor->input_loop_source =
1035 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1036 weston_compositor_read_input, compositor);
1040 idle_repaint(void *data)
1042 struct weston_output *output = data;
1044 weston_output_finish_frame(output, weston_compositor_get_time());
1048 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1050 wl_list_init(&layer->surface_list);
1052 wl_list_insert(below, &layer->link);
1056 weston_output_schedule_repaint(struct weston_output *output)
1058 struct weston_compositor *compositor = output->compositor;
1059 struct wl_event_loop *loop;
1061 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1064 loop = wl_display_get_event_loop(compositor->wl_display);
1065 output->repaint_needed = 1;
1066 if (output->repaint_scheduled)
1069 wl_event_loop_add_idle(loop, idle_repaint, output);
1070 output->repaint_scheduled = 1;
1072 if (compositor->input_loop_source) {
1073 wl_event_source_remove(compositor->input_loop_source);
1074 compositor->input_loop_source = NULL;
1079 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1081 struct weston_output *output;
1083 wl_list_for_each(output, &compositor->output_list, link)
1084 weston_output_schedule_repaint(output);
1088 weston_compositor_fade(struct weston_compositor *compositor, float tint)
1090 struct weston_output *output;
1091 struct weston_surface *surface;
1093 output = container_of(compositor->output_list.next,
1094 struct weston_output, link);
1096 compositor->fade.spring.target = tint;
1097 if (weston_spring_done(&compositor->fade.spring))
1100 if (compositor->fade.surface == NULL) {
1101 surface = weston_surface_create(compositor);
1105 weston_surface_configure(surface, 0, 0, 8192, 8192);
1106 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
1107 wl_list_insert(&compositor->fade_layer.surface_list,
1108 &surface->layer_link);
1109 weston_surface_update_transform(surface);
1110 compositor->fade.surface = surface;
1111 pixman_region32_init(&surface->input);
1114 weston_surface_damage(compositor->fade.surface);
1115 if (wl_list_empty(&compositor->fade.animation.link)) {
1116 compositor->fade.animation.frame_counter = 0;
1117 wl_list_insert(output->animation_list.prev,
1118 &compositor->fade.animation.link);
1123 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1125 wl_resource_destroy(resource);
1129 surface_attach(struct wl_client *client,
1130 struct wl_resource *resource,
1131 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1133 struct weston_surface *surface = resource->data;
1134 struct wl_buffer *buffer = NULL;
1136 if (buffer_resource)
1137 buffer = buffer_resource->data;
1139 if (surface->pending.buffer)
1140 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1142 surface->pending.sx = sx;
1143 surface->pending.sy = sy;
1144 surface->pending.buffer = buffer;
1146 wl_signal_add(&buffer->resource.destroy_signal,
1147 &surface->pending.buffer_destroy_listener);
1148 surface->pending.remove_contents = 0;
1150 surface->pending.remove_contents = 1;
1155 surface_damage(struct wl_client *client,
1156 struct wl_resource *resource,
1157 int32_t x, int32_t y, int32_t width, int32_t height)
1159 struct weston_surface *surface = resource->data;
1161 pixman_region32_union_rect(&surface->pending.damage,
1162 &surface->pending.damage,
1163 x, y, width, height);
1167 destroy_frame_callback(struct wl_resource *resource)
1169 struct weston_frame_callback *cb = resource->data;
1171 wl_list_remove(&cb->link);
1176 surface_frame(struct wl_client *client,
1177 struct wl_resource *resource, uint32_t callback)
1179 struct weston_frame_callback *cb;
1180 struct weston_surface *surface = resource->data;
1182 cb = malloc(sizeof *cb);
1184 wl_resource_post_no_memory(resource);
1188 cb->resource.object.interface = &wl_callback_interface;
1189 cb->resource.object.id = callback;
1190 cb->resource.destroy = destroy_frame_callback;
1191 cb->resource.client = client;
1192 cb->resource.data = cb;
1194 wl_client_add_resource(client, &cb->resource);
1195 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
1199 surface_set_opaque_region(struct wl_client *client,
1200 struct wl_resource *resource,
1201 struct wl_resource *region_resource)
1203 struct weston_surface *surface = resource->data;
1204 struct weston_region *region;
1206 if (region_resource) {
1207 region = region_resource->data;
1208 pixman_region32_copy(&surface->pending.opaque,
1211 empty_region(&surface->pending.opaque);
1216 surface_set_input_region(struct wl_client *client,
1217 struct wl_resource *resource,
1218 struct wl_resource *region_resource)
1220 struct weston_surface *surface = resource->data;
1221 struct weston_region *region;
1223 if (region_resource) {
1224 region = region_resource->data;
1225 pixman_region32_copy(&surface->pending.input,
1228 pixman_region32_fini(&surface->pending.input);
1229 region_init_infinite(&surface->pending.input);
1234 surface_commit(struct wl_client *client, struct wl_resource *resource)
1236 struct weston_surface *surface = resource->data;
1238 /* wl_surface.attach */
1239 if (surface->pending.buffer || surface->pending.remove_contents)
1240 weston_surface_attach(surface, surface->pending.buffer);
1242 if (surface->buffer && surface->configure)
1243 surface->configure(surface, surface->pending.sx,
1244 surface->pending.sy);
1246 /* wl_surface.damage */
1247 pixman_region32_union(&surface->damage, &surface->damage,
1248 &surface->pending.damage);
1249 empty_region(&surface->pending.damage);
1251 /* wl_surface.set_opaque_region */
1252 pixman_region32_fini(&surface->opaque);
1253 pixman_region32_init_rect(&surface->opaque, 0, 0,
1254 surface->geometry.width,
1255 surface->geometry.height);
1256 pixman_region32_intersect(&surface->opaque,
1257 &surface->opaque, &surface->pending.opaque);
1258 surface->geometry.dirty = 1;
1260 /* wl_surface.set_input_region */
1261 pixman_region32_fini(&surface->input);
1262 pixman_region32_init_rect(&surface->input, 0, 0,
1263 surface->geometry.width,
1264 surface->geometry.height);
1265 pixman_region32_intersect(&surface->input,
1266 &surface->input, &surface->pending.input);
1268 /* wl_surface.frame */
1269 wl_list_insert_list(&surface->frame_callback_list,
1270 &surface->pending.frame_callback_list);
1271 wl_list_init(&surface->pending.frame_callback_list);
1273 weston_surface_schedule_repaint(surface);
1276 static const struct wl_surface_interface surface_interface = {
1281 surface_set_opaque_region,
1282 surface_set_input_region,
1287 compositor_create_surface(struct wl_client *client,
1288 struct wl_resource *resource, uint32_t id)
1290 struct weston_compositor *ec = resource->data;
1291 struct weston_surface *surface;
1293 surface = weston_surface_create(ec);
1294 if (surface == NULL) {
1295 wl_resource_post_no_memory(resource);
1299 surface->surface.resource.destroy = destroy_surface;
1301 surface->surface.resource.object.id = id;
1302 surface->surface.resource.object.interface = &wl_surface_interface;
1303 surface->surface.resource.object.implementation =
1304 (void (**)(void)) &surface_interface;
1305 surface->surface.resource.data = surface;
1307 wl_client_add_resource(client, &surface->surface.resource);
1311 destroy_region(struct wl_resource *resource)
1313 struct weston_region *region =
1314 container_of(resource, struct weston_region, resource);
1316 pixman_region32_fini(®ion->region);
1321 region_destroy(struct wl_client *client, struct wl_resource *resource)
1323 wl_resource_destroy(resource);
1327 region_add(struct wl_client *client, struct wl_resource *resource,
1328 int32_t x, int32_t y, int32_t width, int32_t height)
1330 struct weston_region *region = resource->data;
1332 pixman_region32_union_rect(®ion->region, ®ion->region,
1333 x, y, width, height);
1337 region_subtract(struct wl_client *client, struct wl_resource *resource,
1338 int32_t x, int32_t y, int32_t width, int32_t height)
1340 struct weston_region *region = resource->data;
1341 pixman_region32_t rect;
1343 pixman_region32_init_rect(&rect, x, y, width, height);
1344 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
1345 pixman_region32_fini(&rect);
1348 static const struct wl_region_interface region_interface = {
1355 compositor_create_region(struct wl_client *client,
1356 struct wl_resource *resource, uint32_t id)
1358 struct weston_region *region;
1360 region = malloc(sizeof *region);
1361 if (region == NULL) {
1362 wl_resource_post_no_memory(resource);
1366 region->resource.destroy = destroy_region;
1368 region->resource.object.id = id;
1369 region->resource.object.interface = &wl_region_interface;
1370 region->resource.object.implementation =
1371 (void (**)(void)) ®ion_interface;
1372 region->resource.data = region;
1374 pixman_region32_init(®ion->region);
1376 wl_client_add_resource(client, ®ion->resource);
1379 static const struct wl_compositor_interface compositor_interface = {
1380 compositor_create_surface,
1381 compositor_create_region
1385 weston_compositor_wake(struct weston_compositor *compositor)
1387 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1388 weston_compositor_fade(compositor, 0.0);
1390 wl_event_source_timer_update(compositor->idle_source,
1391 compositor->idle_time * 1000);
1395 weston_compositor_dpms_on(struct weston_compositor *compositor)
1397 struct weston_output *output;
1399 wl_list_for_each(output, &compositor->output_list, link)
1400 if (output->set_dpms)
1401 output->set_dpms(output, WESTON_DPMS_ON);
1405 weston_compositor_activity(struct weston_compositor *compositor)
1407 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1408 weston_compositor_wake(compositor);
1410 weston_compositor_dpms_on(compositor);
1411 wl_signal_emit(&compositor->unlock_signal, compositor);
1416 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
1418 weston_compositor_activity(compositor);
1419 compositor->idle_inhibit++;
1423 weston_compositor_idle_release(struct weston_compositor *compositor)
1425 compositor->idle_inhibit--;
1426 weston_compositor_activity(compositor);
1430 idle_handler(void *data)
1432 struct weston_compositor *compositor = data;
1434 if (compositor->idle_inhibit)
1437 weston_compositor_fade(compositor, 1.0);
1443 weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1445 pixman_region32_init(&plane->damage);
1451 weston_plane_release(struct weston_plane *plane)
1453 pixman_region32_fini(&plane->damage);
1457 weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
1460 clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
1462 struct weston_compositor *ec = seat->compositor;
1463 struct weston_output *output, *prev = NULL;
1464 int x, y, old_x, old_y, valid = 0;
1466 x = wl_fixed_to_int(*fx);
1467 y = wl_fixed_to_int(*fy);
1468 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1469 old_y = wl_fixed_to_int(seat->seat.pointer->y);
1471 wl_list_for_each(output, &ec->output_list, link) {
1472 if (pixman_region32_contains_point(&output->region,
1475 if (pixman_region32_contains_point(&output->region,
1476 old_x, old_y, NULL))
1482 *fx = wl_fixed_from_int(prev->x);
1483 else if (x >= prev->x + prev->width)
1484 *fx = wl_fixed_from_int(prev->x +
1487 *fy = wl_fixed_from_int(prev->y);
1488 else if (y >= prev->y + prev->current->height)
1489 *fy = wl_fixed_from_int(prev->y +
1495 notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
1497 const struct wl_pointer_grab_interface *interface;
1498 struct weston_compositor *ec = seat->compositor;
1499 struct weston_output *output;
1500 struct wl_pointer *pointer = seat->seat.pointer;
1503 weston_compositor_activity(ec);
1505 clip_pointer_motion(seat, &x, &y);
1507 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
1512 ix = wl_fixed_to_int(x);
1513 iy = wl_fixed_to_int(y);
1515 wl_list_for_each(output, &ec->output_list, link)
1516 if (output->zoom.active &&
1517 pixman_region32_contains_point(&output->region,
1519 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
1521 weston_device_repick(seat);
1522 interface = pointer->grab->interface;
1523 interface->motion(pointer->grab, time,
1524 pointer->grab->x, pointer->grab->y);
1527 weston_surface_set_position(seat->sprite,
1528 ix - seat->hotspot_x,
1529 iy - seat->hotspot_y);
1530 weston_surface_schedule_repaint(seat->sprite);
1535 weston_surface_activate(struct weston_surface *surface,
1536 struct weston_seat *seat)
1538 struct weston_compositor *compositor = seat->compositor;
1540 if (seat->seat.keyboard) {
1541 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1542 wl_data_device_set_keyboard_focus(&seat->seat);
1545 wl_signal_emit(&compositor->activate_signal, surface);
1549 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
1550 enum wl_pointer_button_state state)
1552 struct weston_compositor *compositor = seat->compositor;
1553 struct wl_pointer *pointer = seat->seat.pointer;
1554 struct weston_surface *focus =
1555 (struct weston_surface *) pointer->focus;
1556 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1558 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1559 if (compositor->ping_handler && focus)
1560 compositor->ping_handler(focus, serial);
1561 weston_compositor_idle_inhibit(compositor);
1562 if (pointer->button_count == 0) {
1563 pointer->grab_button = button;
1564 pointer->grab_time = time;
1565 pointer->grab_x = pointer->x;
1566 pointer->grab_y = pointer->y;
1568 pointer->button_count++;
1570 weston_compositor_idle_release(compositor);
1571 pointer->button_count--;
1574 weston_compositor_run_button_binding(compositor, seat, time, button,
1577 pointer->grab->interface->button(pointer->grab, time, button, state);
1579 if (pointer->button_count == 1)
1580 pointer->grab_serial =
1581 wl_display_get_serial(compositor->wl_display);
1585 notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
1588 struct weston_compositor *compositor = seat->compositor;
1589 struct wl_pointer *pointer = seat->seat.pointer;
1590 struct weston_surface *focus =
1591 (struct weston_surface *) pointer->focus;
1592 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1594 if (compositor->ping_handler && focus)
1595 compositor->ping_handler(focus, serial);
1597 weston_compositor_activity(compositor);
1600 weston_compositor_run_axis_binding(compositor, seat,
1605 if (pointer->focus_resource)
1606 wl_pointer_send_axis(pointer->focus_resource, time, axis,
1611 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1613 struct wl_keyboard *keyboard = &seat->keyboard;
1614 struct wl_keyboard_grab *grab = keyboard->grab;
1615 uint32_t mods_depressed, mods_latched, mods_locked, group;
1616 uint32_t mods_lookup;
1617 enum weston_led leds = 0;
1620 /* Serialize and update our internal state, checking to see if it's
1621 * different to the previous state. */
1622 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1623 XKB_STATE_DEPRESSED);
1624 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1626 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1628 group = xkb_state_serialize_group(seat->xkb_state.state,
1629 XKB_STATE_EFFECTIVE);
1631 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1632 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1633 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1634 group != seat->seat.keyboard->modifiers.group)
1637 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1638 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1639 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1640 seat->seat.keyboard->modifiers.group = group;
1642 /* And update the modifier_state for bindings. */
1643 mods_lookup = mods_depressed | mods_latched;
1644 seat->modifier_state = 0;
1645 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
1646 seat->modifier_state |= MODIFIER_CTRL;
1647 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
1648 seat->modifier_state |= MODIFIER_ALT;
1649 if (mods_lookup & (1 << seat->xkb_info.super_mod))
1650 seat->modifier_state |= MODIFIER_SUPER;
1651 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1652 seat->modifier_state |= MODIFIER_SHIFT;
1654 /* Finally, notify the compositor that LEDs have changed. */
1655 if (xkb_state_led_index_is_active(seat->xkb_state.state,
1656 seat->xkb_info.num_led))
1657 leds |= LED_NUM_LOCK;
1658 if (xkb_state_led_index_is_active(seat->xkb_state.state,
1659 seat->xkb_info.caps_led))
1660 leds |= LED_CAPS_LOCK;
1661 if (xkb_state_led_index_is_active(seat->xkb_state.state,
1662 seat->xkb_info.scroll_led))
1663 leds |= LED_SCROLL_LOCK;
1664 if (leds != seat->xkb_state.leds && seat->led_update)
1665 seat->led_update(seat, leds);
1666 seat->xkb_state.leds = leds;
1669 grab->interface->modifiers(grab,
1671 keyboard->modifiers.mods_depressed,
1672 keyboard->modifiers.mods_latched,
1673 keyboard->modifiers.mods_locked,
1674 keyboard->modifiers.group);
1679 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1680 enum wl_keyboard_key_state state)
1682 enum xkb_key_direction direction;
1684 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1685 direction = XKB_KEY_DOWN;
1687 direction = XKB_KEY_UP;
1689 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1690 * broken keycode system, which starts at 8. */
1691 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1693 notify_modifiers(seat, serial);
1697 notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1698 enum wl_keyboard_key_state state,
1699 enum weston_key_state_update update_state)
1701 struct weston_compositor *compositor = seat->compositor;
1702 struct wl_keyboard *keyboard = seat->seat.keyboard;
1703 struct weston_surface *focus =
1704 (struct weston_surface *) keyboard->focus;
1705 struct wl_keyboard_grab *grab = keyboard->grab;
1706 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1709 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1710 if (compositor->ping_handler && focus)
1711 compositor->ping_handler(focus, serial);
1713 weston_compositor_idle_inhibit(compositor);
1714 keyboard->grab_key = key;
1715 keyboard->grab_time = time;
1717 weston_compositor_idle_release(compositor);
1720 end = keyboard->keys.data + keyboard->keys.size;
1721 for (k = keyboard->keys.data; k < end; k++) {
1723 /* Ignore server-generated repeats. */
1724 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1729 keyboard->keys.size = (void *) end - keyboard->keys.data;
1730 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1731 k = wl_array_add(&keyboard->keys, sizeof *k);
1735 if (grab == &keyboard->default_grab) {
1736 weston_compositor_run_key_binding(compositor, seat, time, key,
1738 grab = keyboard->grab;
1741 grab->interface->key(grab, time, key, state);
1743 if (update_state == STATE_UPDATE_AUTOMATIC) {
1744 update_modifier_state(seat,
1745 wl_display_get_serial(compositor->wl_display),
1752 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1753 wl_fixed_t x, wl_fixed_t y)
1755 struct weston_compositor *compositor = seat->compositor;
1756 struct wl_pointer *pointer = seat->seat.pointer;
1759 weston_seat_update_drag_surface(seat,
1765 compositor->focus = 1;
1766 weston_compositor_repick(compositor);
1768 compositor->focus = 0;
1769 /* FIXME: We should call wl_pointer_set_focus(seat,
1770 * NULL) here, but somehow that breaks re-entry... */
1775 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1777 struct weston_seat *ws;
1779 ws = container_of(listener, struct weston_seat,
1780 saved_kbd_focus_listener);
1782 ws->saved_kbd_focus = NULL;
1786 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1787 enum weston_key_state_update update_state)
1789 struct weston_compositor *compositor = seat->compositor;
1790 struct wl_keyboard *keyboard = seat->seat.keyboard;
1791 struct wl_surface *surface;
1792 uint32_t *k, serial;
1794 serial = wl_display_next_serial(compositor->wl_display);
1795 wl_array_copy(&keyboard->keys, keys);
1796 wl_array_for_each(k, &keyboard->keys) {
1797 weston_compositor_idle_inhibit(compositor);
1798 if (update_state == STATE_UPDATE_AUTOMATIC)
1799 update_modifier_state(seat, serial, *k,
1800 WL_KEYBOARD_KEY_STATE_PRESSED);
1803 /* Run key bindings after we've updated the state. */
1804 wl_array_for_each(k, &keyboard->keys) {
1805 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1806 WL_KEYBOARD_KEY_STATE_PRESSED);
1809 surface = seat->saved_kbd_focus;
1812 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1813 wl_keyboard_set_focus(keyboard, surface);
1814 seat->saved_kbd_focus = NULL;
1819 notify_keyboard_focus_out(struct weston_seat *seat)
1821 struct weston_compositor *compositor = seat->compositor;
1822 struct wl_keyboard *keyboard = seat->seat.keyboard;
1823 uint32_t *k, serial;
1825 serial = wl_display_next_serial(compositor->wl_display);
1826 wl_array_for_each(k, &keyboard->keys) {
1827 weston_compositor_idle_release(compositor);
1828 update_modifier_state(seat, serial, *k,
1829 WL_KEYBOARD_KEY_STATE_RELEASED);
1832 seat->modifier_state = 0;
1834 if (keyboard->focus) {
1835 seat->saved_kbd_focus = keyboard->focus;
1836 seat->saved_kbd_focus_listener.notify =
1837 destroy_device_saved_kbd_focus;
1838 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1839 &seat->saved_kbd_focus_listener);
1842 wl_keyboard_set_focus(keyboard, NULL);
1843 /* FIXME: We really need keyboard grab cancel here to
1844 * let the grab shut down properly. As it is we leak
1846 wl_keyboard_end_grab(keyboard);
1850 touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
1852 struct wl_seat *seat = &ws->seat;
1853 struct wl_resource *resource;
1855 if (seat->touch->focus == surface)
1858 if (seat->touch->focus_resource)
1859 wl_list_remove(&seat->touch->focus_listener.link);
1860 seat->touch->focus = NULL;
1861 seat->touch->focus_resource = NULL;
1865 find_resource_for_client(&seat->touch->resource_list,
1866 surface->resource.client);
1868 weston_log("couldn't find resource\n");
1872 seat->touch->focus = surface;
1873 seat->touch->focus_resource = resource;
1874 wl_signal_add(&resource->destroy_signal,
1875 &seat->touch->focus_listener);
1880 * notify_touch - emulates button touches and notifies surfaces accordingly.
1882 * It assumes always the correct cycle sequence until it gets here: touch_down
1883 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1884 * for sending along such order.
1888 notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1889 wl_fixed_t x, wl_fixed_t y, int touch_type)
1891 struct weston_compositor *ec = seat->compositor;
1892 struct wl_touch *touch = seat->seat.touch;
1893 struct wl_touch_grab *grab = touch->grab;
1894 struct weston_surface *es;
1897 /* Update grab's global coordinates. */
1901 switch (touch_type) {
1903 weston_compositor_idle_inhibit(ec);
1907 /* the first finger down picks the surface, and all further go
1908 * to that surface for the remainder of the touch session i.e.
1909 * until all touch points are up again. */
1910 if (seat->num_tp == 1) {
1911 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1912 touch_set_focus(seat, &es->surface);
1913 } else if (touch->focus) {
1914 es = (struct weston_surface *) touch->focus;
1915 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1918 grab->interface->down(grab, time, touch_id, sx, sy);
1920 case WL_TOUCH_MOTION:
1921 es = (struct weston_surface *) touch->focus;
1925 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1926 grab->interface->motion(grab, time, touch_id, sx, sy);
1929 weston_compositor_idle_release(ec);
1932 grab->interface->up(grab, time, touch_id);
1933 if (seat->num_tp == 0)
1934 touch_set_focus(seat, NULL);
1940 pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1942 struct weston_seat *seat = container_of(listener, struct weston_seat,
1943 sprite_destroy_listener);
1945 seat->sprite = NULL;
1949 pointer_cursor_surface_configure(struct weston_surface *es,
1950 int32_t dx, int32_t dy)
1952 struct weston_seat *seat = es->private;
1955 assert(es == seat->sprite);
1957 seat->hotspot_x -= dx;
1958 seat->hotspot_y -= dy;
1960 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1961 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1963 weston_surface_configure(seat->sprite, x, y,
1964 es->buffer->width, es->buffer->height);
1966 empty_region(&es->pending.input);
1968 if (!weston_surface_is_mapped(es)) {
1969 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1971 weston_surface_update_transform(es);
1976 pointer_unmap_sprite(struct weston_seat *seat)
1978 if (weston_surface_is_mapped(seat->sprite))
1979 weston_surface_unmap(seat->sprite);
1981 wl_list_remove(&seat->sprite_destroy_listener.link);
1982 seat->sprite->configure = NULL;
1983 seat->sprite->private = NULL;
1984 seat->sprite = NULL;
1988 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1989 uint32_t serial, struct wl_resource *surface_resource,
1990 int32_t x, int32_t y)
1992 struct weston_seat *seat = resource->data;
1993 struct weston_surface *surface = NULL;
1995 if (surface_resource)
1996 surface = surface_resource->data;
1998 if (seat->seat.pointer->focus == NULL)
2000 if (seat->seat.pointer->focus->resource.client != client)
2002 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
2005 if (surface && surface != seat->sprite) {
2006 if (surface->configure) {
2007 wl_resource_post_error(&surface->surface.resource,
2008 WL_DISPLAY_ERROR_INVALID_OBJECT,
2009 "surface->configure already "
2016 pointer_unmap_sprite(seat);
2021 wl_signal_add(&surface->surface.resource.destroy_signal,
2022 &seat->sprite_destroy_listener);
2024 surface->configure = pointer_cursor_surface_configure;
2025 surface->private = seat;
2026 seat->sprite = surface;
2027 seat->hotspot_x = x;
2028 seat->hotspot_y = y;
2030 if (surface->buffer)
2031 pointer_cursor_surface_configure(surface, 0, 0);
2034 static const struct wl_pointer_interface pointer_interface = {
2039 handle_drag_surface_destroy(struct wl_listener *listener, void *data)
2041 struct weston_seat *seat;
2043 seat = container_of(listener, struct weston_seat,
2044 drag_surface_destroy_listener);
2046 seat->drag_surface = NULL;
2049 static void unbind_resource(struct wl_resource *resource)
2051 wl_list_remove(&resource->link);
2056 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2059 struct weston_seat *seat = resource->data;
2060 struct wl_resource *cr;
2062 if (!seat->seat.pointer)
2065 cr = wl_client_add_object(client, &wl_pointer_interface,
2066 &pointer_interface, id, seat);
2067 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
2068 cr->destroy = unbind_resource;
2070 if (seat->seat.pointer->focus &&
2071 seat->seat.pointer->focus->resource.client == client) {
2072 struct weston_surface *surface;
2075 surface = (struct weston_surface *) seat->seat.pointer->focus;
2076 weston_surface_from_global_fixed(surface,
2077 seat->seat.pointer->x,
2078 seat->seat.pointer->y,
2081 wl_pointer_set_focus(seat->seat.pointer,
2082 seat->seat.pointer->focus,
2089 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2092 struct weston_seat *seat = resource->data;
2093 struct wl_resource *cr;
2095 if (!seat->seat.keyboard)
2098 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2100 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
2101 cr->destroy = unbind_resource;
2103 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2104 seat->xkb_info.keymap_fd,
2105 seat->xkb_info.keymap_size);
2107 if (seat->seat.keyboard->focus &&
2108 seat->seat.keyboard->focus->resource.client == client) {
2109 wl_keyboard_set_focus(seat->seat.keyboard,
2110 seat->seat.keyboard->focus);
2115 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2118 struct weston_seat *seat = resource->data;
2119 struct wl_resource *cr;
2121 if (!seat->seat.touch)
2124 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2125 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
2126 cr->destroy = unbind_resource;
2129 static const struct wl_seat_interface seat_interface = {
2136 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2138 struct wl_seat *seat = data;
2139 struct wl_resource *resource;
2140 enum wl_seat_capability caps = 0;
2142 resource = wl_client_add_object(client, &wl_seat_interface,
2143 &seat_interface, id, data);
2144 wl_list_insert(&seat->base_resource_list, &resource->link);
2145 resource->destroy = unbind_resource;
2148 caps |= WL_SEAT_CAPABILITY_POINTER;
2150 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2152 caps |= WL_SEAT_CAPABILITY_TOUCH;
2154 wl_seat_send_capabilities(resource, caps);
2158 device_handle_new_drag_icon(struct wl_listener *listener, void *data)
2160 struct weston_seat *seat;
2162 seat = container_of(listener, struct weston_seat,
2163 new_drag_icon_listener);
2165 weston_seat_update_drag_surface(seat, 0, 0);
2168 static void weston_compositor_xkb_init(struct weston_compositor *ec,
2169 struct xkb_rule_names *names)
2171 if (ec->xkb_context == NULL) {
2172 ec->xkb_context = xkb_context_new(0);
2173 if (ec->xkb_context == NULL) {
2174 weston_log("failed to create XKB context\n");
2180 ec->xkb_names = *names;
2181 if (!ec->xkb_names.rules)
2182 ec->xkb_names.rules = strdup("evdev");
2183 if (!ec->xkb_names.model)
2184 ec->xkb_names.model = strdup("pc105");
2185 if (!ec->xkb_names.layout)
2186 ec->xkb_names.layout = strdup("us");
2189 static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2191 if (xkb_info->keymap)
2192 xkb_map_unref(xkb_info->keymap);
2194 if (xkb_info->keymap_area)
2195 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2196 if (xkb_info->keymap_fd >= 0)
2197 close(xkb_info->keymap_fd);
2200 static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2202 free((char *) ec->xkb_names.rules);
2203 free((char *) ec->xkb_names.model);
2204 free((char *) ec->xkb_names.layout);
2205 free((char *) ec->xkb_names.variant);
2206 free((char *) ec->xkb_names.options);
2208 xkb_info_destroy(&ec->xkb_info);
2209 xkb_context_unref(ec->xkb_context);
2213 weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
2217 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2218 XKB_MOD_NAME_SHIFT);
2219 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2221 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2223 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2225 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2226 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
2227 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2229 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
2231 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2233 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2235 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2236 XKB_LED_NAME_SCROLL);
2238 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2239 if (keymap_str == NULL) {
2240 weston_log("failed to get string version of keymap\n");
2243 xkb_info->keymap_size = strlen(keymap_str) + 1;
2245 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2246 if (xkb_info->keymap_fd < 0) {
2247 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2248 (unsigned long) xkb_info->keymap_size);
2249 goto err_keymap_str;
2252 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2253 PROT_READ | PROT_WRITE,
2254 MAP_SHARED, xkb_info->keymap_fd, 0);
2255 if (xkb_info->keymap_area == MAP_FAILED) {
2256 weston_log("failed to mmap() %lu bytes\n",
2257 (unsigned long) xkb_info->keymap_size);
2260 strcpy(xkb_info->keymap_area, keymap_str);
2266 close(xkb_info->keymap_fd);
2267 xkb_info->keymap_fd = -1;
2274 weston_compositor_build_global_keymap(struct weston_compositor *ec)
2276 if (ec->xkb_info.keymap != NULL)
2279 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
2282 if (ec->xkb_info.keymap == NULL) {
2283 weston_log("failed to compile global XKB keymap\n");
2284 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2286 ec->xkb_names.rules, ec->xkb_names.model,
2287 ec->xkb_names.layout, ec->xkb_names.variant,
2288 ec->xkb_names.options);
2292 weston_xkb_info_new_keymap(&ec->xkb_info);
2296 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2298 if (seat->has_keyboard)
2301 if (keymap != NULL) {
2302 seat->xkb_info.keymap = xkb_map_ref(keymap);
2303 weston_xkb_info_new_keymap(&seat->xkb_info);
2306 weston_compositor_build_global_keymap(seat->compositor);
2307 seat->xkb_info = seat->compositor->xkb_info;
2308 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2311 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2312 if (seat->xkb_state.state == NULL) {
2313 weston_log("failed to initialise XKB state\n");
2317 seat->xkb_state.leds = 0;
2319 wl_keyboard_init(&seat->keyboard);
2320 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2322 seat->has_keyboard = 1;
2326 weston_seat_init_pointer(struct weston_seat *seat)
2328 if (seat->has_pointer)
2331 wl_pointer_init(&seat->pointer);
2332 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2334 seat->has_pointer = 1;
2338 weston_seat_init_touch(struct weston_seat *seat)
2340 if (seat->has_touch)
2343 wl_touch_init(&seat->touch);
2344 wl_seat_set_touch(&seat->seat, &seat->touch);
2346 seat->has_touch = 1;
2350 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
2352 wl_seat_init(&seat->seat);
2353 seat->has_pointer = 0;
2354 seat->has_keyboard = 0;
2355 seat->has_touch = 0;
2357 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2360 seat->sprite = NULL;
2361 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
2363 seat->compositor = ec;
2364 seat->hotspot_x = 16;
2365 seat->hotspot_y = 16;
2366 seat->modifier_state = 0;
2369 seat->drag_surface_destroy_listener.notify =
2370 handle_drag_surface_destroy;
2372 wl_list_insert(ec->seat_list.prev, &seat->link);
2374 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2375 wl_signal_add(&seat->seat.drag_icon_signal,
2376 &seat->new_drag_icon_listener);
2378 clipboard_create(seat);
2379 input_method_create(ec, seat);
2383 weston_seat_release(struct weston_seat *seat)
2385 wl_list_remove(&seat->link);
2386 /* The global object is destroyed at wl_display_destroy() time. */
2389 pointer_unmap_sprite(seat);
2391 if (seat->xkb_state.state != NULL)
2392 xkb_state_unref(seat->xkb_state.state);
2393 xkb_info_destroy(&seat->xkb_info);
2395 wl_seat_release(&seat->seat);
2399 drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2401 empty_region(&es->pending.input);
2403 weston_surface_configure(es,
2404 es->geometry.x + sx, es->geometry.y + sy,
2405 es->buffer->width, es->buffer->height);
2409 device_setup_new_drag_surface(struct weston_seat *ws,
2410 struct weston_surface *surface)
2412 struct wl_seat *seat = &ws->seat;
2414 if (surface->configure) {
2415 wl_resource_post_error(&surface->surface.resource,
2416 WL_DISPLAY_ERROR_INVALID_OBJECT,
2417 "surface->configure already set");
2421 ws->drag_surface = surface;
2423 weston_surface_set_position(ws->drag_surface,
2424 wl_fixed_to_double(seat->pointer->x),
2425 wl_fixed_to_double(seat->pointer->y));
2427 surface->configure = drag_surface_configure;
2429 wl_signal_add(&surface->surface.resource.destroy_signal,
2430 &ws->drag_surface_destroy_listener);
2436 device_release_drag_surface(struct weston_seat *seat)
2438 if (weston_surface_is_mapped(seat->drag_surface))
2439 weston_surface_unmap(seat->drag_surface);
2441 seat->drag_surface->configure = NULL;
2442 empty_region(&seat->drag_surface->pending.input);
2443 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2444 seat->drag_surface = NULL;
2448 device_map_drag_surface(struct weston_seat *seat)
2450 struct wl_list *list;
2452 if (weston_surface_is_mapped(seat->drag_surface) ||
2453 !seat->drag_surface->buffer)
2456 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2457 list = &seat->sprite->layer_link;
2459 list = &seat->compositor->cursor_layer.surface_list;
2461 wl_list_insert(list, &seat->drag_surface->layer_link);
2462 weston_surface_update_transform(seat->drag_surface);
2463 empty_region(&seat->drag_surface->input);
2467 weston_seat_update_drag_surface(struct weston_seat *seat,
2470 int surface_changed = 0;
2472 if (!seat->drag_surface && !seat->seat.drag_surface)
2475 if (seat->drag_surface && seat->seat.drag_surface &&
2476 (&seat->drag_surface->surface.resource !=
2477 &seat->seat.drag_surface->resource))
2478 /* between calls to this funcion we got a new drag_surface */
2479 surface_changed = 1;
2481 if (!seat->seat.drag_surface || surface_changed) {
2482 device_release_drag_surface(seat);
2483 if (!surface_changed)
2487 if (!seat->drag_surface || surface_changed) {
2488 struct weston_surface *surface = (struct weston_surface *)
2489 seat->seat.drag_surface;
2490 if (!device_setup_new_drag_surface(seat, surface))
2494 /* the client may not have attached a buffer to the drag surface
2495 * when we setup it up, so check if map is needed on every update */
2496 device_map_drag_surface(seat);
2501 weston_surface_set_position(seat->drag_surface,
2502 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2503 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
2507 weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2509 struct weston_seat *seat;
2511 wl_list_for_each(seat, &compositor->seat_list, link)
2512 weston_seat_update_drag_surface(seat, 0, 0);
2516 bind_output(struct wl_client *client,
2517 void *data, uint32_t version, uint32_t id)
2519 struct weston_output *output = data;
2520 struct weston_mode *mode;
2521 struct wl_resource *resource;
2523 resource = wl_client_add_object(client,
2524 &wl_output_interface, NULL, id, data);
2526 wl_list_insert(&output->resource_list, &resource->link);
2527 resource->destroy = unbind_resource;
2529 wl_output_send_geometry(resource,
2535 output->make, output->model,
2538 wl_list_for_each (mode, &output->mode_list, link) {
2539 wl_output_send_mode(resource,
2548 weston_output_destroy(struct weston_output *output)
2550 struct weston_compositor *c = output->compositor;
2553 pixman_region32_fini(&output->region);
2555 for (i = 0; i < 2; i++)
2556 pixman_region32_fini(&output->buffer_damage[i]);
2558 output->compositor->output_id_pool &= ~(1 << output->id);
2560 wl_display_remove_global(c->wl_display, output->global);
2564 weston_output_compute_transform(struct weston_output *output)
2566 struct weston_matrix transform;
2569 weston_matrix_init(&transform);
2571 switch(output->transform) {
2572 case WL_OUTPUT_TRANSFORM_FLIPPED:
2573 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2574 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2575 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2583 switch(output->transform) {
2584 case WL_OUTPUT_TRANSFORM_NORMAL:
2585 case WL_OUTPUT_TRANSFORM_FLIPPED:
2586 transform.d[0] = flip;
2591 case WL_OUTPUT_TRANSFORM_90:
2592 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2594 transform.d[1] = -flip;
2598 case WL_OUTPUT_TRANSFORM_180:
2599 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2600 transform.d[0] = -flip;
2603 transform.d[5] = -1;
2605 case WL_OUTPUT_TRANSFORM_270:
2606 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2608 transform.d[1] = flip;
2609 transform.d[4] = -1;
2616 weston_matrix_multiply(&output->matrix, &transform);
2620 weston_output_update_matrix(struct weston_output *output)
2622 float magnification;
2623 struct weston_matrix camera;
2624 struct weston_matrix modelview;
2626 weston_matrix_init(&output->matrix);
2627 weston_matrix_translate(&output->matrix,
2628 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2629 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
2631 weston_matrix_scale(&output->matrix,
2632 2.0 / (output->width + output->border.left + output->border.right),
2633 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2635 weston_output_compute_transform(output);
2637 if (output->zoom.active) {
2638 magnification = 1 / (1 - output->zoom.spring_z.current);
2639 weston_matrix_init(&camera);
2640 weston_matrix_init(&modelview);
2641 weston_output_update_zoom(output, output->zoom.type);
2642 weston_matrix_translate(&camera, output->zoom.trans_x,
2643 -output->zoom.trans_y, 0);
2644 weston_matrix_invert(&modelview, &camera);
2645 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
2646 weston_matrix_multiply(&output->matrix, &modelview);
2653 weston_output_transform_init(struct weston_output *output, uint32_t transform)
2655 output->transform = transform;
2657 switch (transform) {
2658 case WL_OUTPUT_TRANSFORM_90:
2659 case WL_OUTPUT_TRANSFORM_270:
2660 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2661 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2662 /* Swap width and height */
2663 output->width = output->current->height;
2664 output->height = output->current->width;
2666 case WL_OUTPUT_TRANSFORM_NORMAL:
2667 case WL_OUTPUT_TRANSFORM_180:
2668 case WL_OUTPUT_TRANSFORM_FLIPPED:
2669 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2670 output->width = output->current->width;
2671 output->height = output->current->height;
2679 weston_output_move(struct weston_output *output, int x, int y)
2686 output->current_buffer = 0;
2687 for (i = 0; i < 2; i++)
2688 pixman_region32_init(&output->buffer_damage[i]);
2690 pixman_region32_init_rect(&output->region, x, y,
2696 weston_output_init(struct weston_output *output, struct weston_compositor *c,
2697 int x, int y, int width, int height, uint32_t transform)
2699 output->compositor = c;
2702 output->border.top = 0;
2703 output->border.bottom = 0;
2704 output->border.left = 0;
2705 output->border.right = 0;
2706 output->mm_width = width;
2707 output->mm_height = height;
2710 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2711 output->disable_planes++;
2713 weston_output_transform_init(output, transform);
2714 weston_output_init_zoom(output);
2716 weston_output_move(output, x, y);
2717 weston_output_damage(output);
2719 wl_signal_init(&output->frame_signal);
2720 wl_list_init(&output->animation_list);
2721 wl_list_init(&output->resource_list);
2723 output->id = ffs(~output->compositor->output_id_pool) - 1;
2724 output->compositor->output_id_pool |= 1 << output->id;
2727 wl_display_add_global(c->wl_display, &wl_output_interface,
2728 output, bind_output);
2732 compositor_bind(struct wl_client *client,
2733 void *data, uint32_t version, uint32_t id)
2735 struct weston_compositor *compositor = data;
2737 wl_client_add_object(client, &wl_compositor_interface,
2738 &compositor_interface, id, compositor);
2744 struct utsname usys;
2748 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2749 usys.version, usys.machine);
2753 weston_compositor_init(struct weston_compositor *ec,
2754 struct wl_display *display,
2757 const char *config_file)
2759 struct wl_event_loop *loop;
2760 struct xkb_rule_names xkb_names;
2761 const struct config_key keyboard_config_keys[] = {
2762 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2763 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2764 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2765 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2766 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2768 const struct config_section cs[] = {
2770 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2773 memset(&xkb_names, 0, sizeof(xkb_names));
2774 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
2776 ec->wl_display = display;
2777 wl_signal_init(&ec->destroy_signal);
2778 wl_signal_init(&ec->activate_signal);
2779 wl_signal_init(&ec->kill_signal);
2780 wl_signal_init(&ec->lock_signal);
2781 wl_signal_init(&ec->unlock_signal);
2782 wl_signal_init(&ec->show_input_panel_signal);
2783 wl_signal_init(&ec->hide_input_panel_signal);
2784 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
2786 ec->output_id_pool = 0;
2788 if (!wl_display_add_global(display, &wl_compositor_interface,
2789 ec, compositor_bind))
2792 wl_list_init(&ec->surface_list);
2793 wl_list_init(&ec->layer_list);
2794 wl_list_init(&ec->seat_list);
2795 wl_list_init(&ec->output_list);
2796 wl_list_init(&ec->key_binding_list);
2797 wl_list_init(&ec->button_binding_list);
2798 wl_list_init(&ec->axis_binding_list);
2799 wl_list_init(&ec->fade.animation.link);
2801 weston_plane_init(&ec->primary_plane, 0, 0);
2803 weston_compositor_xkb_init(ec, &xkb_names);
2805 ec->ping_handler = NULL;
2807 screenshooter_create(ec);
2808 text_cursor_position_notifier_create(ec);
2809 text_model_factory_create(ec);
2811 wl_data_device_manager_init(ec->wl_display);
2813 wl_display_init_shm(display);
2815 loop = wl_display_get_event_loop(ec->wl_display);
2816 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2817 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2819 ec->input_loop = wl_event_loop_create();
2821 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2822 ec->fade.animation.frame = fade_frame;
2824 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2825 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2827 weston_compositor_schedule_repaint(ec);
2833 weston_compositor_shutdown(struct weston_compositor *ec)
2835 struct weston_output *output, *next;
2837 wl_event_source_remove(ec->idle_source);
2838 if (ec->input_loop_source)
2839 wl_event_source_remove(ec->input_loop_source);
2841 /* Destroy all outputs associated with this compositor */
2842 wl_list_for_each_safe(output, next, &ec->output_list, link)
2843 output->destroy(output);
2845 weston_binding_list_destroy_all(&ec->key_binding_list);
2846 weston_binding_list_destroy_all(&ec->button_binding_list);
2847 weston_binding_list_destroy_all(&ec->axis_binding_list);
2849 weston_plane_release(&ec->primary_plane);
2851 wl_array_release(&ec->vertices);
2852 wl_array_release(&ec->indices);
2853 wl_array_release(&ec->vtxcnt);
2855 wl_event_loop_destroy(ec->input_loop);
2858 static int on_term_signal(int signal_number, void *data)
2860 struct wl_display *display = data;
2862 weston_log("caught signal %d\n", signal_number);
2863 wl_display_terminate(display);
2869 on_segv_signal(int s, siginfo_t *siginfo, void *context)
2875 /* This SIGSEGV handler will do a best-effort backtrace, and
2876 * then call the backend restore function, which will switch
2877 * back to the vt we launched from or ungrab X etc and then
2878 * raise SIGTRAP. If we run weston under gdb from X or a
2879 * different vt, and tell gdb "handle SIGSEGV nostop", this
2880 * will allow weston to switch back to gdb on crash and then
2881 * gdb will catch the crash with SIGTRAP. */
2883 weston_log("caught segv\n");
2885 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2886 for (i = 0; i < count; i++) {
2887 dladdr(buffer[i], &info);
2888 weston_log(" [%016lx] %s (%s)\n",
2890 info.dli_sname ? info.dli_sname : "--",
2894 segv_compositor->restore(segv_compositor);
2901 load_module(const char *name, const char *entrypoint)
2903 char path[PATH_MAX];
2904 void *module, *init;
2907 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
2909 snprintf(path, sizeof path, "%s", name);
2911 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2913 weston_log("Module '%s' already loaded\n", path);
2918 weston_log("Loading module '%s'\n", path);
2919 module = dlopen(path, RTLD_NOW);
2921 weston_log("Failed to load module: %s\n", dlerror());
2925 init = dlsym(module, entrypoint);
2927 weston_log("Failed to lookup init function: %s\n", dlerror());
2935 load_modules(struct weston_compositor *ec, const char *modules)
2937 const char *p, *end;
2939 int (*module_init)(struct weston_compositor *ec);
2941 if (modules == NULL)
2946 end = strchrnul(p, ',');
2947 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2948 module_init = load_module(buffer, "module_init");
2960 static const char xdg_error_message[] =
2961 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2963 static const char xdg_wrong_message[] =
2964 "fatal: environment variable XDG_RUNTIME_DIR\n"
2965 "is set to \"%s\", which is not a directory.\n";
2967 static const char xdg_wrong_mode_message[] =
2968 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2969 "correctly. Unix access mode must be 0700 but is %o,\n"
2970 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
2971 "owned by UID %d.\n";
2973 static const char xdg_detail_message[] =
2974 "Refer to your distribution on how to get it, or\n"
2975 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2976 "on how to implement it.\n";
2979 verify_xdg_runtime_dir(void)
2981 char *dir = getenv("XDG_RUNTIME_DIR");
2985 weston_log(xdg_error_message);
2986 weston_log_continue(xdg_detail_message);
2990 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2991 weston_log(xdg_wrong_message, dir);
2992 weston_log_continue(xdg_detail_message);
2996 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2997 weston_log(xdg_wrong_mode_message,
2998 dir, s.st_mode & 0777, s.st_uid);
2999 weston_log_continue(xdg_detail_message);
3004 usage(int error_code)
3007 "Usage: weston [OPTIONS]\n\n"
3008 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3009 "Weston supports multiple backends, and depending on which backend is in use\n"
3010 "different options will be accepted.\n\n"
3014 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3015 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3016 " -S, --socket=NAME\tName of socket to listen on\n"
3017 " -i, --idle-time=SECS\tIdle time in seconds\n"
3018 " --modules\t\tLoad the comma-separated list of modules\n"
3019 " --log==FILE\t\tLog to the given file\n"
3020 " -h, --help\t\tThis help message\n\n");
3023 "Options for drm-backend.so:\n\n"
3024 " --connector=ID\tBring up only this connector\n"
3025 " --seat=SEAT\t\tThe seat that weston should run on\n"
3026 " --tty=TTY\t\tThe tty to use\n"
3027 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3030 "Options for x11-backend.so:\n\n"
3031 " --width=WIDTH\t\tWidth of X window\n"
3032 " --height=HEIGHT\tHeight of X window\n"
3033 " --fullscreen\t\tRun in fullscreen mode\n"
3034 " --output-count=COUNT\tCreate multiple outputs\n"
3035 " --no-input\t\tDont create input devices\n\n");
3038 "Options for wayland-backend.so:\n\n"
3039 " --width=WIDTH\t\tWidth of Wayland surface\n"
3040 " --height=HEIGHT\tHeight of Wayland surface\n"
3041 " --display=DISPLAY\tWayland display to connect to\n\n");
3046 int main(int argc, char *argv[])
3048 int ret = EXIT_SUCCESS;
3049 struct wl_display *display;
3050 struct weston_compositor *ec;
3051 struct wl_event_source *signals[4];
3052 struct wl_event_loop *loop;
3053 struct sigaction segv_action;
3054 struct weston_compositor
3055 *(*backend_init)(struct wl_display *display,
3056 int argc, char *argv[], const char *config_file);
3058 char *backend = NULL;
3059 const char *modules = "desktop-shell.so", *option_modules = NULL;
3061 int32_t idle_time = 300;
3063 char *socket_name = "wayland-0";
3066 const struct config_key core_config_keys[] = {
3067 { "modules", CONFIG_KEY_STRING, &modules },
3070 const struct config_section cs[] = {
3072 core_config_keys, ARRAY_LENGTH(core_config_keys) },
3075 const struct weston_option core_options[] = {
3076 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3077 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3078 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
3079 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
3080 { WESTON_OPTION_STRING, "log", 0, &log },
3081 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
3084 argc = parse_options(core_options,
3085 ARRAY_LENGTH(core_options), argc, argv);
3088 usage(EXIT_SUCCESS);
3090 weston_log_file_open(log);
3094 STAMP_SPACE "Bug reports to: %s\n"
3095 STAMP_SPACE "Build: %s\n",
3096 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
3100 verify_xdg_runtime_dir();
3102 display = wl_display_create();
3104 loop = wl_display_get_event_loop(display);
3105 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3107 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3109 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3112 wl_list_init(&child_process_list);
3113 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3117 if (getenv("WAYLAND_DISPLAY"))
3118 backend = "wayland-backend.so";
3119 else if (getenv("DISPLAY"))
3120 backend = "x11-backend.so";
3122 backend = "drm-backend.so";
3125 config_file = config_file_path("weston.ini");
3126 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
3128 backend_init = load_module(backend, "backend_init");
3132 ec = backend_init(display, argc, argv, config_file);
3134 weston_log("fatal: failed to create compositor\n");
3138 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3139 segv_action.sa_sigaction = on_segv_signal;
3140 sigemptyset(&segv_action.sa_mask);
3141 sigaction(SIGSEGV, &segv_action, NULL);
3142 segv_compositor = ec;
3144 for (i = 1; argv[i]; i++)
3145 weston_log("fatal: unhandled option: %s\n", argv[i]);
3153 ec->option_idle_time = idle_time;
3154 ec->idle_time = idle_time;
3156 setenv("WAYLAND_DISPLAY", socket_name, 1);
3158 if (load_modules(ec, modules) < 0)
3160 if (load_modules(ec, option_modules) < 0)
3163 if (wl_display_add_socket(display, socket_name)) {
3164 weston_log("fatal: failed to add socket: %m\n");
3169 weston_compositor_dpms_on(ec);
3170 weston_compositor_wake(ec);
3172 wl_display_run(display);
3175 /* prevent further rendering while shutting down */
3176 ec->state = WESTON_COMPOSITOR_SLEEPING;
3178 wl_signal_emit(&ec->destroy_signal, ec);
3180 for (i = ARRAY_LENGTH(signals); i;)
3181 wl_event_source_remove(signals[--i]);
3183 weston_compositor_xkb_destroy(ec);
3186 wl_display_destroy(display);
3188 weston_log_file_close();