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);
196 static const pixman_region32_data_t undef_region_data;
199 undef_region(pixman_region32_t *region)
201 if (region->data != &undef_region_data)
202 pixman_region32_fini(region);
203 region->data = (pixman_region32_data_t *) &undef_region_data;
207 region_is_undefined(pixman_region32_t *region)
209 return region->data == &undef_region_data;
213 empty_region(pixman_region32_t *region)
215 if (!region_is_undefined(region))
216 pixman_region32_fini(region);
218 pixman_region32_init(region);
221 WL_EXPORT struct weston_surface *
222 weston_surface_create(struct weston_compositor *compositor)
224 struct weston_surface *surface;
226 surface = calloc(1, sizeof *surface);
230 wl_signal_init(&surface->surface.resource.destroy_signal);
232 wl_list_init(&surface->link);
233 wl_list_init(&surface->layer_link);
235 surface->surface.resource.client = NULL;
237 surface->compositor = compositor;
238 surface->alpha = 1.0;
241 surface->num_textures = 0;
242 surface->num_images = 0;
244 surface->buffer = NULL;
245 surface->output = NULL;
246 surface->plane = &compositor->primary_plane;
248 pixman_region32_init(&surface->damage);
249 pixman_region32_init(&surface->opaque);
250 pixman_region32_init(&surface->clip);
251 undef_region(&surface->input);
252 pixman_region32_init(&surface->transform.opaque);
253 wl_list_init(&surface->frame_callback_list);
255 surface->buffer_destroy_listener.notify =
256 surface_handle_buffer_destroy;
258 wl_list_init(&surface->geometry.transformation_list);
259 wl_list_insert(&surface->geometry.transformation_list,
260 &surface->transform.position.link);
261 weston_matrix_init(&surface->transform.position.matrix);
262 pixman_region32_init(&surface->transform.boundingbox);
263 surface->geometry.dirty = 1;
269 weston_surface_set_color(struct weston_surface *surface,
270 float red, float green, float blue, float alpha)
272 surface->color[0] = red;
273 surface->color[1] = green;
274 surface->color[2] = blue;
275 surface->color[3] = alpha;
276 surface->shader = &surface->compositor->solid_shader;
280 weston_surface_to_global_float(struct weston_surface *surface,
281 float sx, float sy, float *x, float *y)
283 if (surface->transform.enabled) {
284 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
286 weston_matrix_transform(&surface->transform.matrix, &v);
288 if (fabsf(v.f[3]) < 1e-6) {
289 weston_log("warning: numerical instability in "
290 "weston_surface_to_global(), divisor = %g\n",
297 *x = v.f[0] / v.f[3];
298 *y = v.f[1] / v.f[3];
300 *x = sx + surface->geometry.x;
301 *y = sy + surface->geometry.y;
306 weston_surface_move_to_plane(struct weston_surface *surface,
307 struct weston_plane *plane)
309 if (surface->plane == plane)
312 weston_surface_damage_below(surface);
313 surface->plane = plane;
314 weston_surface_damage(surface);
318 weston_surface_damage_below(struct weston_surface *surface)
320 pixman_region32_t damage;
322 pixman_region32_init(&damage);
323 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
325 pixman_region32_union(&surface->plane->damage,
326 &surface->plane->damage, &damage);
327 pixman_region32_fini(&damage);
330 static struct wl_resource *
331 find_resource_for_client(struct wl_list *list, struct wl_client *client)
333 struct wl_resource *r;
335 wl_list_for_each(r, list, link) {
336 if (r->client == client)
344 weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
346 uint32_t different = es->output_mask ^ mask;
347 uint32_t entered = mask & different;
348 uint32_t left = es->output_mask & different;
349 struct weston_output *output;
350 struct wl_resource *resource = NULL;
351 struct wl_client *client = es->surface.resource.client;
353 es->output_mask = mask;
354 if (es->surface.resource.client == NULL)
359 wl_list_for_each(output, &es->compositor->output_list, link) {
360 if (1 << output->id & different)
362 find_resource_for_client(&output->resource_list,
364 if (resource == NULL)
366 if (1 << output->id & entered)
367 wl_surface_send_enter(&es->surface.resource, resource);
368 if (1 << output->id & left)
369 wl_surface_send_leave(&es->surface.resource, resource);
374 weston_surface_assign_output(struct weston_surface *es)
376 struct weston_compositor *ec = es->compositor;
377 struct weston_output *output, *new_output;
378 pixman_region32_t region;
379 uint32_t max, area, mask;
385 pixman_region32_init(®ion);
386 wl_list_for_each(output, &ec->output_list, link) {
387 pixman_region32_intersect(®ion, &es->transform.boundingbox,
390 e = pixman_region32_extents(®ion);
391 area = (e->x2 - e->x1) * (e->y2 - e->y1);
394 mask |= 1 << output->id;
401 pixman_region32_fini(®ion);
403 es->output = new_output;
404 weston_surface_update_output_mask(es, mask);
408 surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
409 int32_t width, int32_t height,
410 pixman_region32_t *bbox)
412 float min_x = HUGE_VALF, min_y = HUGE_VALF;
413 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
418 { sx + width, sy + height }
423 if (width == 0 || height == 0) {
424 /* avoid rounding empty bbox to 1x1 */
425 pixman_region32_init(bbox);
429 for (i = 0; i < 4; ++i) {
431 weston_surface_to_global_float(surface,
432 s[i][0], s[i][1], &x, &y);
443 int_x = floorf(min_x);
444 int_y = floorf(min_y);
445 pixman_region32_init_rect(bbox, int_x, int_y,
446 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
450 weston_surface_update_transform_disable(struct weston_surface *surface)
452 surface->transform.enabled = 0;
454 /* round off fractions when not transformed */
455 surface->geometry.x = roundf(surface->geometry.x);
456 surface->geometry.y = roundf(surface->geometry.y);
458 pixman_region32_init_rect(&surface->transform.boundingbox,
461 surface->geometry.width,
462 surface->geometry.height);
464 if (surface->alpha == 1.0) {
465 pixman_region32_copy(&surface->transform.opaque,
467 pixman_region32_translate(&surface->transform.opaque,
469 surface->geometry.y);
474 weston_surface_update_transform_enable(struct weston_surface *surface)
476 struct weston_matrix *matrix = &surface->transform.matrix;
477 struct weston_matrix *inverse = &surface->transform.inverse;
478 struct weston_transform *tform;
480 surface->transform.enabled = 1;
482 /* Otherwise identity matrix, but with x and y translation. */
483 surface->transform.position.matrix.d[12] = surface->geometry.x;
484 surface->transform.position.matrix.d[13] = surface->geometry.y;
486 weston_matrix_init(matrix);
487 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
488 weston_matrix_multiply(matrix, &tform->matrix);
490 if (weston_matrix_invert(inverse, matrix) < 0) {
491 /* Oops, bad total transformation, not invertible */
492 weston_log("error: weston_surface %p"
493 " transformation not invertible.\n", surface);
497 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
498 surface->geometry.height,
499 &surface->transform.boundingbox);
505 weston_surface_update_transform(struct weston_surface *surface)
507 if (!surface->geometry.dirty)
510 surface->geometry.dirty = 0;
512 weston_surface_damage_below(surface);
514 pixman_region32_fini(&surface->transform.boundingbox);
515 pixman_region32_fini(&surface->transform.opaque);
516 pixman_region32_init(&surface->transform.opaque);
518 if (region_is_undefined(&surface->input))
519 pixman_region32_init_rect(&surface->input, 0, 0,
520 surface->geometry.width,
521 surface->geometry.height);
523 /* transform.position is always in transformation_list */
524 if (surface->geometry.transformation_list.next ==
525 &surface->transform.position.link &&
526 surface->geometry.transformation_list.prev ==
527 &surface->transform.position.link) {
528 weston_surface_update_transform_disable(surface);
530 if (weston_surface_update_transform_enable(surface) < 0)
531 weston_surface_update_transform_disable(surface);
534 weston_surface_damage_below(surface);
536 weston_surface_assign_output(surface);
540 weston_surface_to_global_fixed(struct weston_surface *surface,
541 wl_fixed_t sx, wl_fixed_t sy,
542 wl_fixed_t *x, wl_fixed_t *y)
546 weston_surface_to_global_float(surface,
547 wl_fixed_to_double(sx),
548 wl_fixed_to_double(sy),
550 *x = wl_fixed_from_double(xf);
551 *y = wl_fixed_from_double(yf);
555 weston_surface_from_global_float(struct weston_surface *surface,
556 float x, float y, float *sx, float *sy)
558 if (surface->transform.enabled) {
559 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
561 weston_matrix_transform(&surface->transform.inverse, &v);
563 if (fabsf(v.f[3]) < 1e-6) {
564 weston_log("warning: numerical instability in "
565 "weston_surface_from_global(), divisor = %g\n",
572 *sx = v.f[0] / v.f[3];
573 *sy = v.f[1] / v.f[3];
575 *sx = x - surface->geometry.x;
576 *sy = y - surface->geometry.y;
581 weston_surface_from_global_fixed(struct weston_surface *surface,
582 wl_fixed_t x, wl_fixed_t y,
583 wl_fixed_t *sx, wl_fixed_t *sy)
587 weston_surface_from_global_float(surface,
588 wl_fixed_to_double(x),
589 wl_fixed_to_double(y),
591 *sx = wl_fixed_from_double(sxf);
592 *sy = wl_fixed_from_double(syf);
596 weston_surface_from_global(struct weston_surface *surface,
597 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
601 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
607 weston_surface_schedule_repaint(struct weston_surface *surface)
609 struct weston_output *output;
611 wl_list_for_each(output, &surface->compositor->output_list, link)
612 if (surface->output_mask & (1 << output->id))
613 weston_output_schedule_repaint(output);
617 weston_surface_damage(struct weston_surface *surface)
619 pixman_region32_union_rect(&surface->damage, &surface->damage,
620 0, 0, surface->geometry.width,
621 surface->geometry.height);
623 weston_surface_schedule_repaint(surface);
627 weston_surface_configure(struct weston_surface *surface,
628 float x, float y, int width, int height)
630 surface->geometry.x = x;
631 surface->geometry.y = y;
632 surface->geometry.width = width;
633 surface->geometry.height = height;
634 surface->geometry.dirty = 1;
638 weston_surface_set_position(struct weston_surface *surface,
641 surface->geometry.x = x;
642 surface->geometry.y = y;
643 surface->geometry.dirty = 1;
647 weston_surface_is_mapped(struct weston_surface *surface)
656 weston_compositor_get_time(void)
660 gettimeofday(&tv, NULL);
662 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
665 static struct weston_surface *
666 weston_compositor_pick_surface(struct weston_compositor *compositor,
667 wl_fixed_t x, wl_fixed_t y,
668 wl_fixed_t *sx, wl_fixed_t *sy)
670 struct weston_surface *surface;
672 wl_list_for_each(surface, &compositor->surface_list, link) {
673 weston_surface_from_global_fixed(surface, x, y, sx, sy);
674 if (pixman_region32_contains_point(&surface->input,
675 wl_fixed_to_int(*sx),
676 wl_fixed_to_int(*sy),
685 weston_device_repick(struct weston_seat *seat)
687 const struct wl_pointer_grab_interface *interface;
688 struct weston_surface *surface, *focus;
689 struct wl_pointer *pointer = seat->seat.pointer;
694 surface = weston_compositor_pick_surface(seat->compositor,
698 &pointer->current_y);
700 if (&surface->surface != pointer->current) {
701 interface = pointer->grab->interface;
702 pointer->current = &surface->surface;
703 interface->focus(pointer->grab, &surface->surface,
708 focus = (struct weston_surface *) pointer->grab->focus;
710 weston_surface_from_global_fixed(focus,
718 weston_compositor_repick(struct weston_compositor *compositor)
720 struct weston_seat *seat;
722 if (!compositor->focus)
725 wl_list_for_each(seat, &compositor->seat_list, link)
726 weston_device_repick(seat);
730 weston_surface_unmap(struct weston_surface *surface)
732 struct weston_seat *seat;
734 weston_surface_damage_below(surface);
735 surface->output = NULL;
736 wl_list_remove(&surface->layer_link);
738 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
739 if (seat->seat.keyboard &&
740 seat->seat.keyboard->focus == &surface->surface)
741 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
742 if (seat->seat.pointer &&
743 seat->seat.pointer->focus == &surface->surface)
744 wl_pointer_set_focus(seat->seat.pointer,
746 wl_fixed_from_int(0),
747 wl_fixed_from_int(0));
750 weston_surface_schedule_repaint(surface);
753 struct weston_frame_callback {
754 struct wl_resource resource;
759 destroy_surface(struct wl_resource *resource)
761 struct weston_surface *surface =
762 container_of(resource,
763 struct weston_surface, surface.resource);
764 struct weston_compositor *compositor = surface->compositor;
765 struct weston_frame_callback *cb, *next;
767 if (weston_surface_is_mapped(surface))
768 weston_surface_unmap(surface);
771 wl_list_remove(&surface->buffer_destroy_listener.link);
773 compositor->renderer->destroy_surface(surface);
775 pixman_region32_fini(&surface->transform.boundingbox);
776 pixman_region32_fini(&surface->damage);
777 pixman_region32_fini(&surface->opaque);
778 pixman_region32_fini(&surface->clip);
779 if (!region_is_undefined(&surface->input))
780 pixman_region32_fini(&surface->input);
782 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
783 wl_resource_destroy(&cb->resource);
789 weston_surface_destroy(struct weston_surface *surface)
791 /* Not a valid way to destroy a client surface */
792 assert(surface->surface.resource.client == NULL);
794 wl_signal_emit(&surface->surface.resource.destroy_signal,
795 &surface->surface.resource);
796 destroy_surface(&surface->surface.resource);
800 weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
802 struct weston_surface *es = (struct weston_surface *) surface;
805 weston_buffer_post_release(es->buffer);
806 wl_list_remove(&es->buffer_destroy_listener.link);
810 buffer->busy_count++;
811 wl_signal_add(&buffer->resource.destroy_signal,
812 &es->buffer_destroy_listener);
814 if (es->geometry.width != buffer->width ||
815 es->geometry.height != buffer->height) {
816 undef_region(&es->input);
817 pixman_region32_fini(&es->opaque);
818 pixman_region32_init(&es->opaque);
821 if (weston_surface_is_mapped(es))
822 weston_surface_unmap(es);
825 es->compositor->renderer->attach(es, buffer);
830 weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
832 wl_list_remove(&surface->layer_link);
833 wl_list_insert(below, &surface->layer_link);
834 weston_surface_damage_below(surface);
835 weston_surface_damage(surface);
839 weston_compositor_damage_all(struct weston_compositor *compositor)
841 struct weston_output *output;
843 wl_list_for_each(output, &compositor->output_list, link)
844 weston_output_damage(output);
848 weston_buffer_post_release(struct wl_buffer *buffer)
850 if (--buffer->busy_count > 0)
853 assert(buffer->resource.client != NULL);
854 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
858 weston_output_damage(struct weston_output *output)
860 struct weston_compositor *compositor = output->compositor;
862 pixman_region32_union(&compositor->primary_plane.damage,
863 &compositor->primary_plane.damage,
865 weston_output_schedule_repaint(output);
869 fade_frame(struct weston_animation *animation,
870 struct weston_output *output, uint32_t msecs)
872 struct weston_compositor *compositor =
873 container_of(animation,
874 struct weston_compositor, fade.animation);
875 struct weston_surface *surface;
877 if (animation->frame_counter <= 1)
878 compositor->fade.spring.timestamp = msecs;
880 surface = compositor->fade.surface;
881 weston_spring_update(&compositor->fade.spring, msecs);
882 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
883 compositor->fade.spring.current);
884 weston_surface_damage(surface);
886 if (weston_spring_done(&compositor->fade.spring)) {
887 compositor->fade.spring.current =
888 compositor->fade.spring.target;
889 wl_list_remove(&animation->link);
890 wl_list_init(&animation->link);
892 if (compositor->fade.spring.current < 0.001) {
893 destroy_surface(&surface->surface.resource);
894 compositor->fade.surface = NULL;
895 } else if (compositor->fade.spring.current > 0.999) {
896 compositor->state = WESTON_COMPOSITOR_SLEEPING;
897 wl_signal_emit(&compositor->lock_signal, compositor);
903 surface_accumulate_damage(struct weston_surface *surface,
904 pixman_region32_t *opaque)
906 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
907 surface->compositor->renderer->flush_damage(surface);
909 if (surface->transform.enabled) {
910 pixman_box32_t *extents;
912 extents = pixman_region32_extents(&surface->damage);
913 surface_compute_bbox(surface, extents->x1, extents->y1,
914 extents->x2 - extents->x1,
915 extents->y2 - extents->y1,
917 pixman_region32_translate(&surface->damage,
921 pixman_region32_translate(&surface->damage,
922 surface->geometry.x - surface->plane->x,
923 surface->geometry.y - surface->plane->y);
926 pixman_region32_union(&surface->plane->damage,
927 &surface->plane->damage, &surface->damage);
928 empty_region(&surface->damage);
929 pixman_region32_copy(&surface->clip, opaque);
930 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
934 weston_output_repaint(struct weston_output *output, uint32_t msecs)
936 struct weston_compositor *ec = output->compositor;
937 struct weston_surface *es;
938 struct weston_layer *layer;
939 struct weston_animation *animation, *next;
940 struct weston_frame_callback *cb, *cnext;
941 struct wl_list frame_callback_list;
942 pixman_region32_t opaque, output_damage;
944 weston_compositor_update_drag_surfaces(ec);
946 /* Rebuild the surface list and update surface transforms up front. */
947 wl_list_init(&ec->surface_list);
948 wl_list_init(&frame_callback_list);
949 wl_list_for_each(layer, &ec->layer_list, link) {
950 wl_list_for_each(es, &layer->surface_list, layer_link) {
951 weston_surface_update_transform(es);
952 wl_list_insert(ec->surface_list.prev, &es->link);
953 if (es->output == output) {
954 wl_list_insert_list(&frame_callback_list,
955 &es->frame_callback_list);
956 wl_list_init(&es->frame_callback_list);
961 if (output->assign_planes && !output->disable_planes)
962 output->assign_planes(output);
964 wl_list_for_each(es, &ec->surface_list, link)
965 weston_surface_move_to_plane(es, &ec->primary_plane);
967 pixman_region32_init(&opaque);
969 wl_list_for_each(es, &ec->surface_list, link)
970 surface_accumulate_damage(es, &opaque);
972 pixman_region32_fini(&opaque);
974 pixman_region32_init(&output_damage);
975 pixman_region32_intersect(&output_damage,
976 &ec->primary_plane.damage, &output->region);
977 pixman_region32_subtract(&ec->primary_plane.damage,
978 &ec->primary_plane.damage, &output->region);
981 weston_output_update_matrix(output);
983 output->repaint(output, &output_damage);
985 pixman_region32_fini(&output_damage);
987 output->repaint_needed = 0;
989 weston_compositor_repick(ec);
990 wl_event_loop_dispatch(ec->input_loop, 0);
992 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
993 wl_callback_send_done(&cb->resource, msecs);
994 wl_resource_destroy(&cb->resource);
996 wl_list_init(&frame_callback_list);
998 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
999 animation->frame_counter++;
1000 animation->frame(animation, output, msecs);
1005 weston_compositor_read_input(int fd, uint32_t mask, void *data)
1007 struct weston_compositor *compositor = data;
1009 wl_event_loop_dispatch(compositor->input_loop, 0);
1015 weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
1017 struct weston_compositor *compositor = output->compositor;
1018 struct wl_event_loop *loop =
1019 wl_display_get_event_loop(compositor->wl_display);
1022 output->frame_time = msecs;
1023 if (output->repaint_needed) {
1024 weston_output_repaint(output, msecs);
1028 output->repaint_scheduled = 0;
1029 if (compositor->input_loop_source)
1032 fd = wl_event_loop_get_fd(compositor->input_loop);
1033 compositor->input_loop_source =
1034 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1035 weston_compositor_read_input, compositor);
1039 idle_repaint(void *data)
1041 struct weston_output *output = data;
1043 weston_output_finish_frame(output, weston_compositor_get_time());
1047 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1049 wl_list_init(&layer->surface_list);
1051 wl_list_insert(below, &layer->link);
1055 weston_output_schedule_repaint(struct weston_output *output)
1057 struct weston_compositor *compositor = output->compositor;
1058 struct wl_event_loop *loop;
1060 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1063 loop = wl_display_get_event_loop(compositor->wl_display);
1064 output->repaint_needed = 1;
1065 if (output->repaint_scheduled)
1068 wl_event_loop_add_idle(loop, idle_repaint, output);
1069 output->repaint_scheduled = 1;
1071 if (compositor->input_loop_source) {
1072 wl_event_source_remove(compositor->input_loop_source);
1073 compositor->input_loop_source = NULL;
1078 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1080 struct weston_output *output;
1082 wl_list_for_each(output, &compositor->output_list, link)
1083 weston_output_schedule_repaint(output);
1087 weston_compositor_fade(struct weston_compositor *compositor, float tint)
1089 struct weston_output *output;
1090 struct weston_surface *surface;
1092 output = container_of(compositor->output_list.next,
1093 struct weston_output, link);
1095 compositor->fade.spring.target = tint;
1096 if (weston_spring_done(&compositor->fade.spring))
1099 if (compositor->fade.surface == NULL) {
1100 surface = weston_surface_create(compositor);
1101 weston_surface_configure(surface, 0, 0, 8192, 8192);
1102 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
1103 wl_list_insert(&compositor->fade_layer.surface_list,
1104 &surface->layer_link);
1105 weston_surface_update_transform(surface);
1106 compositor->fade.surface = surface;
1107 pixman_region32_init(&surface->input);
1110 weston_surface_damage(compositor->fade.surface);
1111 if (wl_list_empty(&compositor->fade.animation.link)) {
1112 compositor->fade.animation.frame_counter = 0;
1113 wl_list_insert(output->animation_list.prev,
1114 &compositor->fade.animation.link);
1119 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1121 wl_resource_destroy(resource);
1125 surface_attach(struct wl_client *client,
1126 struct wl_resource *resource,
1127 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1129 struct weston_surface *es = resource->data;
1130 struct wl_buffer *buffer = NULL;
1132 if (buffer_resource)
1133 buffer = buffer_resource->data;
1135 weston_surface_attach(&es->surface, buffer);
1137 if (buffer && es->configure)
1138 es->configure(es, sx, sy);
1142 surface_damage(struct wl_client *client,
1143 struct wl_resource *resource,
1144 int32_t x, int32_t y, int32_t width, int32_t height)
1146 struct weston_surface *es = resource->data;
1148 pixman_region32_union_rect(&es->damage, &es->damage,
1149 x, y, width, height);
1150 weston_surface_schedule_repaint(es);
1154 destroy_frame_callback(struct wl_resource *resource)
1156 struct weston_frame_callback *cb = resource->data;
1158 wl_list_remove(&cb->link);
1163 surface_frame(struct wl_client *client,
1164 struct wl_resource *resource, uint32_t callback)
1166 struct weston_frame_callback *cb;
1167 struct weston_surface *es = resource->data;
1169 cb = malloc(sizeof *cb);
1171 wl_resource_post_no_memory(resource);
1175 cb->resource.object.interface = &wl_callback_interface;
1176 cb->resource.object.id = callback;
1177 cb->resource.destroy = destroy_frame_callback;
1178 cb->resource.client = client;
1179 cb->resource.data = cb;
1181 wl_client_add_resource(client, &cb->resource);
1182 wl_list_insert(es->frame_callback_list.prev, &cb->link);
1186 surface_set_opaque_region(struct wl_client *client,
1187 struct wl_resource *resource,
1188 struct wl_resource *region_resource)
1190 struct weston_surface *surface = resource->data;
1191 struct weston_region *region;
1193 pixman_region32_fini(&surface->opaque);
1195 if (region_resource) {
1196 region = region_resource->data;
1197 pixman_region32_init_rect(&surface->opaque, 0, 0,
1198 surface->geometry.width,
1199 surface->geometry.height);
1200 pixman_region32_intersect(&surface->opaque,
1201 &surface->opaque, ®ion->region);
1203 pixman_region32_init(&surface->opaque);
1206 surface->geometry.dirty = 1;
1210 surface_set_input_region(struct wl_client *client,
1211 struct wl_resource *resource,
1212 struct wl_resource *region_resource)
1214 struct weston_surface *surface = resource->data;
1215 struct weston_region *region;
1217 if (region_resource) {
1218 region = region_resource->data;
1219 pixman_region32_init_rect(&surface->input, 0, 0,
1220 surface->geometry.width,
1221 surface->geometry.height);
1222 pixman_region32_intersect(&surface->input,
1223 &surface->input, ®ion->region);
1225 pixman_region32_init_rect(&surface->input, 0, 0,
1226 surface->geometry.width,
1227 surface->geometry.height);
1230 weston_surface_schedule_repaint(surface);
1233 static const struct wl_surface_interface surface_interface = {
1238 surface_set_opaque_region,
1239 surface_set_input_region
1243 compositor_create_surface(struct wl_client *client,
1244 struct wl_resource *resource, uint32_t id)
1246 struct weston_compositor *ec = resource->data;
1247 struct weston_surface *surface;
1249 surface = weston_surface_create(ec);
1250 if (surface == NULL) {
1251 wl_resource_post_no_memory(resource);
1255 surface->surface.resource.destroy = destroy_surface;
1257 surface->surface.resource.object.id = id;
1258 surface->surface.resource.object.interface = &wl_surface_interface;
1259 surface->surface.resource.object.implementation =
1260 (void (**)(void)) &surface_interface;
1261 surface->surface.resource.data = surface;
1263 wl_client_add_resource(client, &surface->surface.resource);
1267 destroy_region(struct wl_resource *resource)
1269 struct weston_region *region =
1270 container_of(resource, struct weston_region, resource);
1272 pixman_region32_fini(®ion->region);
1277 region_destroy(struct wl_client *client, struct wl_resource *resource)
1279 wl_resource_destroy(resource);
1283 region_add(struct wl_client *client, struct wl_resource *resource,
1284 int32_t x, int32_t y, int32_t width, int32_t height)
1286 struct weston_region *region = resource->data;
1288 pixman_region32_union_rect(®ion->region, ®ion->region,
1289 x, y, width, height);
1293 region_subtract(struct wl_client *client, struct wl_resource *resource,
1294 int32_t x, int32_t y, int32_t width, int32_t height)
1296 struct weston_region *region = resource->data;
1297 pixman_region32_t rect;
1299 pixman_region32_init_rect(&rect, x, y, width, height);
1300 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
1301 pixman_region32_fini(&rect);
1304 static const struct wl_region_interface region_interface = {
1311 compositor_create_region(struct wl_client *client,
1312 struct wl_resource *resource, uint32_t id)
1314 struct weston_region *region;
1316 region = malloc(sizeof *region);
1317 if (region == NULL) {
1318 wl_resource_post_no_memory(resource);
1322 region->resource.destroy = destroy_region;
1324 region->resource.object.id = id;
1325 region->resource.object.interface = &wl_region_interface;
1326 region->resource.object.implementation =
1327 (void (**)(void)) ®ion_interface;
1328 region->resource.data = region;
1330 pixman_region32_init(®ion->region);
1332 wl_client_add_resource(client, ®ion->resource);
1335 static const struct wl_compositor_interface compositor_interface = {
1336 compositor_create_surface,
1337 compositor_create_region
1341 weston_compositor_wake(struct weston_compositor *compositor)
1343 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1344 weston_compositor_fade(compositor, 0.0);
1346 wl_event_source_timer_update(compositor->idle_source,
1347 compositor->idle_time * 1000);
1351 weston_compositor_dpms_on(struct weston_compositor *compositor)
1353 struct weston_output *output;
1355 wl_list_for_each(output, &compositor->output_list, link)
1356 if (output->set_dpms)
1357 output->set_dpms(output, WESTON_DPMS_ON);
1361 weston_compositor_activity(struct weston_compositor *compositor)
1363 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1364 weston_compositor_wake(compositor);
1366 weston_compositor_dpms_on(compositor);
1367 wl_signal_emit(&compositor->unlock_signal, compositor);
1372 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
1374 weston_compositor_activity(compositor);
1375 compositor->idle_inhibit++;
1379 weston_compositor_idle_release(struct weston_compositor *compositor)
1381 compositor->idle_inhibit--;
1382 weston_compositor_activity(compositor);
1386 idle_handler(void *data)
1388 struct weston_compositor *compositor = data;
1390 if (compositor->idle_inhibit)
1393 weston_compositor_fade(compositor, 1.0);
1399 weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1401 pixman_region32_init(&plane->damage);
1407 weston_plane_release(struct weston_plane *plane)
1409 pixman_region32_fini(&plane->damage);
1413 weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
1416 clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
1418 struct weston_compositor *ec = seat->compositor;
1419 struct weston_output *output, *prev = NULL;
1420 int x, y, old_x, old_y, valid = 0;
1422 x = wl_fixed_to_int(*fx);
1423 y = wl_fixed_to_int(*fy);
1424 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1425 old_y = wl_fixed_to_int(seat->seat.pointer->y);
1427 wl_list_for_each(output, &ec->output_list, link) {
1428 if (pixman_region32_contains_point(&output->region,
1431 if (pixman_region32_contains_point(&output->region,
1432 old_x, old_y, NULL))
1438 *fx = wl_fixed_from_int(prev->x);
1439 else if (x >= prev->x + prev->width)
1440 *fx = wl_fixed_from_int(prev->x +
1443 *fy = wl_fixed_from_int(prev->y);
1444 else if (y >= prev->y + prev->current->height)
1445 *fy = wl_fixed_from_int(prev->y +
1451 notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
1453 const struct wl_pointer_grab_interface *interface;
1454 struct weston_compositor *ec = seat->compositor;
1455 struct weston_output *output;
1456 struct wl_pointer *pointer = seat->seat.pointer;
1459 weston_compositor_activity(ec);
1461 clip_pointer_motion(seat, &x, &y);
1463 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
1468 ix = wl_fixed_to_int(x);
1469 iy = wl_fixed_to_int(y);
1471 wl_list_for_each(output, &ec->output_list, link)
1472 if (output->zoom.active &&
1473 pixman_region32_contains_point(&output->region,
1475 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
1477 weston_device_repick(seat);
1478 interface = pointer->grab->interface;
1479 interface->motion(pointer->grab, time,
1480 pointer->grab->x, pointer->grab->y);
1483 weston_surface_set_position(seat->sprite,
1484 ix - seat->hotspot_x,
1485 iy - seat->hotspot_y);
1486 weston_surface_schedule_repaint(seat->sprite);
1491 weston_surface_activate(struct weston_surface *surface,
1492 struct weston_seat *seat)
1494 struct weston_compositor *compositor = seat->compositor;
1496 if (seat->seat.keyboard) {
1497 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1498 wl_data_device_set_keyboard_focus(&seat->seat);
1501 wl_signal_emit(&compositor->activate_signal, surface);
1505 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
1506 enum wl_pointer_button_state state)
1508 struct weston_compositor *compositor = seat->compositor;
1509 struct wl_pointer *pointer = seat->seat.pointer;
1510 struct weston_surface *focus =
1511 (struct weston_surface *) pointer->focus;
1512 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1514 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1515 if (compositor->ping_handler && focus)
1516 compositor->ping_handler(focus, serial);
1517 weston_compositor_idle_inhibit(compositor);
1518 if (pointer->button_count == 0) {
1519 pointer->grab_button = button;
1520 pointer->grab_time = time;
1521 pointer->grab_x = pointer->x;
1522 pointer->grab_y = pointer->y;
1524 pointer->button_count++;
1526 weston_compositor_idle_release(compositor);
1527 pointer->button_count--;
1530 weston_compositor_run_button_binding(compositor, seat, time, button,
1533 pointer->grab->interface->button(pointer->grab, time, button, state);
1535 if (pointer->button_count == 1)
1536 pointer->grab_serial =
1537 wl_display_get_serial(compositor->wl_display);
1541 notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
1544 struct weston_compositor *compositor = seat->compositor;
1545 struct wl_pointer *pointer = seat->seat.pointer;
1546 struct weston_surface *focus =
1547 (struct weston_surface *) pointer->focus;
1548 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1550 if (compositor->ping_handler && focus)
1551 compositor->ping_handler(focus, serial);
1553 weston_compositor_activity(compositor);
1556 weston_compositor_run_axis_binding(compositor, seat,
1561 if (pointer->focus_resource)
1562 wl_pointer_send_axis(pointer->focus_resource, time, axis,
1567 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1569 struct wl_keyboard *keyboard = &seat->keyboard;
1570 struct wl_keyboard_grab *grab = keyboard->grab;
1571 uint32_t mods_depressed, mods_latched, mods_locked, group;
1572 uint32_t mods_lookup;
1573 enum weston_led leds = 0;
1576 /* Serialize and update our internal state, checking to see if it's
1577 * different to the previous state. */
1578 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1579 XKB_STATE_DEPRESSED);
1580 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1582 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1584 group = xkb_state_serialize_group(seat->xkb_state.state,
1585 XKB_STATE_EFFECTIVE);
1587 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1588 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1589 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1590 group != seat->seat.keyboard->modifiers.group)
1593 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1594 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1595 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1596 seat->seat.keyboard->modifiers.group = group;
1598 /* And update the modifier_state for bindings. */
1599 mods_lookup = mods_depressed | mods_latched;
1600 seat->modifier_state = 0;
1601 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
1602 seat->modifier_state |= MODIFIER_CTRL;
1603 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
1604 seat->modifier_state |= MODIFIER_ALT;
1605 if (mods_lookup & (1 << seat->xkb_info.super_mod))
1606 seat->modifier_state |= MODIFIER_SUPER;
1607 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1608 seat->modifier_state |= MODIFIER_SHIFT;
1610 /* Finally, notify the compositor that LEDs have changed. */
1611 if (xkb_state_led_index_is_active(seat->xkb_state.state,
1612 seat->xkb_info.num_led))
1613 leds |= LED_NUM_LOCK;
1614 if (xkb_state_led_index_is_active(seat->xkb_state.state,
1615 seat->xkb_info.caps_led))
1616 leds |= LED_CAPS_LOCK;
1617 if (xkb_state_led_index_is_active(seat->xkb_state.state,
1618 seat->xkb_info.scroll_led))
1619 leds |= LED_SCROLL_LOCK;
1620 if (leds != seat->xkb_state.leds && seat->led_update)
1621 seat->led_update(seat, leds);
1622 seat->xkb_state.leds = leds;
1625 grab->interface->modifiers(grab,
1627 keyboard->modifiers.mods_depressed,
1628 keyboard->modifiers.mods_latched,
1629 keyboard->modifiers.mods_locked,
1630 keyboard->modifiers.group);
1635 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1636 enum wl_keyboard_key_state state)
1638 enum xkb_key_direction direction;
1640 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1641 direction = XKB_KEY_DOWN;
1643 direction = XKB_KEY_UP;
1645 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1646 * broken keycode system, which starts at 8. */
1647 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1649 notify_modifiers(seat, serial);
1653 notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1654 enum wl_keyboard_key_state state,
1655 enum weston_key_state_update update_state)
1657 struct weston_compositor *compositor = seat->compositor;
1658 struct wl_keyboard *keyboard = seat->seat.keyboard;
1659 struct weston_surface *focus =
1660 (struct weston_surface *) keyboard->focus;
1661 struct wl_keyboard_grab *grab = keyboard->grab;
1662 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1665 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1666 if (compositor->ping_handler && focus)
1667 compositor->ping_handler(focus, serial);
1669 weston_compositor_idle_inhibit(compositor);
1670 keyboard->grab_key = key;
1671 keyboard->grab_time = time;
1673 weston_compositor_idle_release(compositor);
1676 end = keyboard->keys.data + keyboard->keys.size;
1677 for (k = keyboard->keys.data; k < end; k++) {
1679 /* Ignore server-generated repeats. */
1680 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1685 keyboard->keys.size = (void *) end - keyboard->keys.data;
1686 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1687 k = wl_array_add(&keyboard->keys, sizeof *k);
1691 if (grab == &keyboard->default_grab) {
1692 weston_compositor_run_key_binding(compositor, seat, time, key,
1694 grab = keyboard->grab;
1697 grab->interface->key(grab, time, key, state);
1699 if (update_state == STATE_UPDATE_AUTOMATIC) {
1700 update_modifier_state(seat,
1701 wl_display_get_serial(compositor->wl_display),
1708 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1709 wl_fixed_t x, wl_fixed_t y)
1711 struct weston_compositor *compositor = seat->compositor;
1712 struct wl_pointer *pointer = seat->seat.pointer;
1715 weston_seat_update_drag_surface(seat,
1721 compositor->focus = 1;
1722 weston_compositor_repick(compositor);
1724 compositor->focus = 0;
1725 /* FIXME: We should call wl_pointer_set_focus(seat,
1726 * NULL) here, but somehow that breaks re-entry... */
1731 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1733 struct weston_seat *ws;
1735 ws = container_of(listener, struct weston_seat,
1736 saved_kbd_focus_listener);
1738 ws->saved_kbd_focus = NULL;
1742 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1743 enum weston_key_state_update update_state)
1745 struct weston_compositor *compositor = seat->compositor;
1746 struct wl_keyboard *keyboard = seat->seat.keyboard;
1747 struct wl_surface *surface;
1748 uint32_t *k, serial;
1750 serial = wl_display_next_serial(compositor->wl_display);
1751 wl_array_copy(&keyboard->keys, keys);
1752 wl_array_for_each(k, &keyboard->keys) {
1753 weston_compositor_idle_inhibit(compositor);
1754 if (update_state == STATE_UPDATE_AUTOMATIC)
1755 update_modifier_state(seat, serial, *k,
1756 WL_KEYBOARD_KEY_STATE_PRESSED);
1759 /* Run key bindings after we've updated the state. */
1760 wl_array_for_each(k, &keyboard->keys) {
1761 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1762 WL_KEYBOARD_KEY_STATE_PRESSED);
1765 surface = seat->saved_kbd_focus;
1768 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1769 wl_keyboard_set_focus(keyboard, surface);
1770 seat->saved_kbd_focus = NULL;
1775 notify_keyboard_focus_out(struct weston_seat *seat)
1777 struct weston_compositor *compositor = seat->compositor;
1778 struct wl_keyboard *keyboard = seat->seat.keyboard;
1779 uint32_t *k, serial;
1781 serial = wl_display_next_serial(compositor->wl_display);
1782 wl_array_for_each(k, &keyboard->keys) {
1783 weston_compositor_idle_release(compositor);
1784 update_modifier_state(seat, serial, *k,
1785 WL_KEYBOARD_KEY_STATE_RELEASED);
1788 seat->modifier_state = 0;
1790 if (keyboard->focus) {
1791 seat->saved_kbd_focus = keyboard->focus;
1792 seat->saved_kbd_focus_listener.notify =
1793 destroy_device_saved_kbd_focus;
1794 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1795 &seat->saved_kbd_focus_listener);
1798 wl_keyboard_set_focus(keyboard, NULL);
1799 /* FIXME: We really need keyboard grab cancel here to
1800 * let the grab shut down properly. As it is we leak
1802 wl_keyboard_end_grab(keyboard);
1806 touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
1808 struct wl_seat *seat = &ws->seat;
1809 struct wl_resource *resource;
1811 if (seat->touch->focus == surface)
1814 if (seat->touch->focus_resource)
1815 wl_list_remove(&seat->touch->focus_listener.link);
1816 seat->touch->focus = NULL;
1817 seat->touch->focus_resource = NULL;
1821 find_resource_for_client(&seat->touch->resource_list,
1822 surface->resource.client);
1824 weston_log("couldn't find resource\n");
1828 seat->touch->focus = surface;
1829 seat->touch->focus_resource = resource;
1830 wl_signal_add(&resource->destroy_signal,
1831 &seat->touch->focus_listener);
1836 * notify_touch - emulates button touches and notifies surfaces accordingly.
1838 * It assumes always the correct cycle sequence until it gets here: touch_down
1839 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1840 * for sending along such order.
1844 notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1845 wl_fixed_t x, wl_fixed_t y, int touch_type)
1847 struct weston_compositor *ec = seat->compositor;
1848 struct wl_touch *touch = seat->seat.touch;
1849 struct weston_surface *es;
1851 uint32_t serial = 0;
1853 switch (touch_type) {
1855 weston_compositor_idle_inhibit(ec);
1859 /* the first finger down picks the surface, and all further go
1860 * to that surface for the remainder of the touch session i.e.
1861 * until all touch points are up again. */
1862 if (seat->num_tp == 1) {
1863 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1864 touch_set_focus(seat, &es->surface);
1865 } else if (touch->focus) {
1866 es = (struct weston_surface *) touch->focus;
1867 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1870 if (touch->focus_resource && touch->focus)
1871 wl_touch_send_down(touch->focus_resource,
1873 &touch->focus->resource,
1876 case WL_TOUCH_MOTION:
1877 es = (struct weston_surface *) touch->focus;
1881 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1882 if (touch->focus_resource)
1883 wl_touch_send_motion(touch->focus_resource,
1884 time, touch_id, sx, sy);
1887 weston_compositor_idle_release(ec);
1890 if (touch->focus_resource)
1891 wl_touch_send_up(touch->focus_resource,
1892 serial, time, touch_id);
1893 if (seat->num_tp == 0)
1894 touch_set_focus(seat, NULL);
1900 pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1902 struct weston_seat *seat = container_of(listener, struct weston_seat,
1903 sprite_destroy_listener);
1905 seat->sprite = NULL;
1909 pointer_cursor_surface_configure(struct weston_surface *es,
1910 int32_t dx, int32_t dy)
1912 struct weston_seat *seat = es->private;
1915 assert(es == seat->sprite);
1917 seat->hotspot_x -= dx;
1918 seat->hotspot_y -= dy;
1920 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
1921 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
1923 weston_surface_configure(seat->sprite, x, y,
1924 es->buffer->width, es->buffer->height);
1926 empty_region(&es->input);
1928 if (!weston_surface_is_mapped(es)) {
1929 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1931 weston_surface_update_transform(es);
1936 pointer_unmap_sprite(struct weston_seat *seat)
1938 if (weston_surface_is_mapped(seat->sprite))
1939 weston_surface_unmap(seat->sprite);
1941 wl_list_remove(&seat->sprite_destroy_listener.link);
1942 seat->sprite->configure = NULL;
1943 seat->sprite->private = NULL;
1944 seat->sprite = NULL;
1948 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1949 uint32_t serial, struct wl_resource *surface_resource,
1950 int32_t x, int32_t y)
1952 struct weston_seat *seat = resource->data;
1953 struct weston_surface *surface = NULL;
1955 if (surface_resource)
1956 surface = container_of(surface_resource->data,
1957 struct weston_surface, surface);
1959 if (seat->seat.pointer->focus == NULL)
1961 if (seat->seat.pointer->focus->resource.client != client)
1963 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
1966 if (surface && surface != seat->sprite) {
1967 if (surface->configure) {
1968 wl_resource_post_error(&surface->surface.resource,
1969 WL_DISPLAY_ERROR_INVALID_OBJECT,
1970 "surface->configure already "
1977 pointer_unmap_sprite(seat);
1982 wl_signal_add(&surface->surface.resource.destroy_signal,
1983 &seat->sprite_destroy_listener);
1985 surface->configure = pointer_cursor_surface_configure;
1986 surface->private = seat;
1987 seat->sprite = surface;
1988 seat->hotspot_x = x;
1989 seat->hotspot_y = y;
1991 if (surface->buffer)
1992 pointer_cursor_surface_configure(surface, 0, 0);
1995 static const struct wl_pointer_interface pointer_interface = {
2000 handle_drag_surface_destroy(struct wl_listener *listener, void *data)
2002 struct weston_seat *seat;
2004 seat = container_of(listener, struct weston_seat,
2005 drag_surface_destroy_listener);
2007 seat->drag_surface = NULL;
2010 static void unbind_resource(struct wl_resource *resource)
2012 wl_list_remove(&resource->link);
2017 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2020 struct weston_seat *seat = resource->data;
2021 struct wl_resource *cr;
2023 if (!seat->seat.pointer)
2026 cr = wl_client_add_object(client, &wl_pointer_interface,
2027 &pointer_interface, id, seat);
2028 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
2029 cr->destroy = unbind_resource;
2031 if (seat->seat.pointer->focus &&
2032 seat->seat.pointer->focus->resource.client == client) {
2033 struct weston_surface *surface;
2036 surface = (struct weston_surface *) seat->seat.pointer->focus;
2037 weston_surface_from_global_fixed(surface,
2038 seat->seat.pointer->x,
2039 seat->seat.pointer->y,
2042 wl_pointer_set_focus(seat->seat.pointer,
2043 seat->seat.pointer->focus,
2050 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2053 struct weston_seat *seat = resource->data;
2054 struct wl_resource *cr;
2056 if (!seat->seat.keyboard)
2059 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2061 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
2062 cr->destroy = unbind_resource;
2064 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2065 seat->xkb_info.keymap_fd,
2066 seat->xkb_info.keymap_size);
2068 if (seat->seat.keyboard->focus &&
2069 seat->seat.keyboard->focus->resource.client == client) {
2070 wl_keyboard_set_focus(seat->seat.keyboard,
2071 seat->seat.keyboard->focus);
2076 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2079 struct weston_seat *seat = resource->data;
2080 struct wl_resource *cr;
2082 if (!seat->seat.touch)
2085 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2086 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
2087 cr->destroy = unbind_resource;
2090 static const struct wl_seat_interface seat_interface = {
2097 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2099 struct wl_seat *seat = data;
2100 struct wl_resource *resource;
2101 enum wl_seat_capability caps = 0;
2103 resource = wl_client_add_object(client, &wl_seat_interface,
2104 &seat_interface, id, data);
2105 wl_list_insert(&seat->base_resource_list, &resource->link);
2106 resource->destroy = unbind_resource;
2109 caps |= WL_SEAT_CAPABILITY_POINTER;
2111 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2113 caps |= WL_SEAT_CAPABILITY_TOUCH;
2115 wl_seat_send_capabilities(resource, caps);
2119 device_handle_new_drag_icon(struct wl_listener *listener, void *data)
2121 struct weston_seat *seat;
2123 seat = container_of(listener, struct weston_seat,
2124 new_drag_icon_listener);
2126 weston_seat_update_drag_surface(seat, 0, 0);
2129 static void weston_compositor_xkb_init(struct weston_compositor *ec,
2130 struct xkb_rule_names *names)
2132 if (ec->xkb_context == NULL) {
2133 ec->xkb_context = xkb_context_new(0);
2134 if (ec->xkb_context == NULL) {
2135 weston_log("failed to create XKB context\n");
2141 ec->xkb_names = *names;
2142 if (!ec->xkb_names.rules)
2143 ec->xkb_names.rules = strdup("evdev");
2144 if (!ec->xkb_names.model)
2145 ec->xkb_names.model = strdup("pc105");
2146 if (!ec->xkb_names.layout)
2147 ec->xkb_names.layout = strdup("us");
2150 static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2152 if (xkb_info->keymap)
2153 xkb_map_unref(xkb_info->keymap);
2155 if (xkb_info->keymap_area)
2156 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2157 if (xkb_info->keymap_fd >= 0)
2158 close(xkb_info->keymap_fd);
2161 static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2163 free((char *) ec->xkb_names.rules);
2164 free((char *) ec->xkb_names.model);
2165 free((char *) ec->xkb_names.layout);
2166 free((char *) ec->xkb_names.variant);
2167 free((char *) ec->xkb_names.options);
2169 xkb_info_destroy(&ec->xkb_info);
2170 xkb_context_unref(ec->xkb_context);
2174 weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
2178 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2179 XKB_MOD_NAME_SHIFT);
2180 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2182 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2184 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2186 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2187 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
2188 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2190 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
2192 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2194 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2196 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2197 XKB_LED_NAME_SCROLL);
2199 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2200 if (keymap_str == NULL) {
2201 weston_log("failed to get string version of keymap\n");
2204 xkb_info->keymap_size = strlen(keymap_str) + 1;
2206 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2207 if (xkb_info->keymap_fd < 0) {
2208 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2209 (unsigned long) xkb_info->keymap_size);
2210 goto err_keymap_str;
2213 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2214 PROT_READ | PROT_WRITE,
2215 MAP_SHARED, xkb_info->keymap_fd, 0);
2216 if (xkb_info->keymap_area == MAP_FAILED) {
2217 weston_log("failed to mmap() %lu bytes\n",
2218 (unsigned long) xkb_info->keymap_size);
2221 strcpy(xkb_info->keymap_area, keymap_str);
2227 close(xkb_info->keymap_fd);
2228 xkb_info->keymap_fd = -1;
2235 weston_compositor_build_global_keymap(struct weston_compositor *ec)
2237 if (ec->xkb_info.keymap != NULL)
2240 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
2243 if (ec->xkb_info.keymap == NULL) {
2244 weston_log("failed to compile global XKB keymap\n");
2245 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2247 ec->xkb_names.rules, ec->xkb_names.model,
2248 ec->xkb_names.layout, ec->xkb_names.variant,
2249 ec->xkb_names.options);
2253 weston_xkb_info_new_keymap(&ec->xkb_info);
2257 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2259 if (seat->has_keyboard)
2262 if (keymap != NULL) {
2263 seat->xkb_info.keymap = xkb_map_ref(keymap);
2264 weston_xkb_info_new_keymap(&seat->xkb_info);
2267 weston_compositor_build_global_keymap(seat->compositor);
2268 seat->xkb_info = seat->compositor->xkb_info;
2269 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2272 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2273 if (seat->xkb_state.state == NULL) {
2274 weston_log("failed to initialise XKB state\n");
2278 seat->xkb_state.leds = 0;
2280 wl_keyboard_init(&seat->keyboard);
2281 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2283 seat->has_keyboard = 1;
2287 weston_seat_init_pointer(struct weston_seat *seat)
2289 if (seat->has_pointer)
2292 wl_pointer_init(&seat->pointer);
2293 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2295 seat->has_pointer = 1;
2299 weston_seat_init_touch(struct weston_seat *seat)
2301 if (seat->has_touch)
2304 wl_touch_init(&seat->touch);
2305 wl_seat_set_touch(&seat->seat, &seat->touch);
2307 seat->has_touch = 1;
2311 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
2313 wl_seat_init(&seat->seat);
2314 seat->has_pointer = 0;
2315 seat->has_keyboard = 0;
2316 seat->has_touch = 0;
2318 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2321 seat->sprite = NULL;
2322 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
2324 seat->compositor = ec;
2325 seat->hotspot_x = 16;
2326 seat->hotspot_y = 16;
2327 seat->modifier_state = 0;
2330 seat->drag_surface_destroy_listener.notify =
2331 handle_drag_surface_destroy;
2333 wl_list_insert(ec->seat_list.prev, &seat->link);
2335 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2336 wl_signal_add(&seat->seat.drag_icon_signal,
2337 &seat->new_drag_icon_listener);
2339 clipboard_create(seat);
2340 input_method_create(ec, seat);
2344 weston_seat_release(struct weston_seat *seat)
2346 wl_list_remove(&seat->link);
2347 /* The global object is destroyed at wl_display_destroy() time. */
2350 pointer_unmap_sprite(seat);
2352 if (seat->xkb_state.state != NULL)
2353 xkb_state_unref(seat->xkb_state.state);
2354 xkb_info_destroy(&seat->xkb_info);
2356 wl_seat_release(&seat->seat);
2360 drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2362 weston_surface_configure(es,
2363 es->geometry.x + sx, es->geometry.y + sy,
2364 es->buffer->width, es->buffer->height);
2368 device_setup_new_drag_surface(struct weston_seat *ws,
2369 struct weston_surface *surface)
2371 struct wl_seat *seat = &ws->seat;
2373 if (surface->configure) {
2374 wl_resource_post_error(&surface->surface.resource,
2375 WL_DISPLAY_ERROR_INVALID_OBJECT,
2376 "surface->configure already set");
2380 ws->drag_surface = surface;
2382 weston_surface_set_position(ws->drag_surface,
2383 wl_fixed_to_double(seat->pointer->x),
2384 wl_fixed_to_double(seat->pointer->y));
2386 surface->configure = drag_surface_configure;
2388 wl_signal_add(&surface->surface.resource.destroy_signal,
2389 &ws->drag_surface_destroy_listener);
2395 device_release_drag_surface(struct weston_seat *seat)
2397 seat->drag_surface->configure = NULL;
2398 undef_region(&seat->drag_surface->input);
2399 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2400 seat->drag_surface = NULL;
2404 device_map_drag_surface(struct weston_seat *seat)
2406 struct wl_list *list;
2408 if (weston_surface_is_mapped(seat->drag_surface) ||
2409 !seat->drag_surface->buffer)
2412 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2413 list = &seat->sprite->layer_link;
2415 list = &seat->compositor->cursor_layer.surface_list;
2417 wl_list_insert(list, &seat->drag_surface->layer_link);
2418 weston_surface_update_transform(seat->drag_surface);
2419 empty_region(&seat->drag_surface->input);
2423 weston_seat_update_drag_surface(struct weston_seat *seat,
2426 int surface_changed = 0;
2428 if (!seat->drag_surface && !seat->seat.drag_surface)
2431 if (seat->drag_surface && seat->seat.drag_surface &&
2432 (&seat->drag_surface->surface.resource !=
2433 &seat->seat.drag_surface->resource))
2434 /* between calls to this funcion we got a new drag_surface */
2435 surface_changed = 1;
2437 if (!seat->seat.drag_surface || surface_changed) {
2438 device_release_drag_surface(seat);
2439 if (!surface_changed)
2443 if (!seat->drag_surface || surface_changed) {
2444 struct weston_surface *surface = (struct weston_surface *)
2445 seat->seat.drag_surface;
2446 if (!device_setup_new_drag_surface(seat, surface))
2450 /* the client may not have attached a buffer to the drag surface
2451 * when we setup it up, so check if map is needed on every update */
2452 device_map_drag_surface(seat);
2454 /* the client may have attached a buffer with a different size to
2455 * the drag surface, causing the input region to be reset */
2456 if (region_is_undefined(&seat->drag_surface->input))
2457 empty_region(&seat->drag_surface->input);
2462 weston_surface_set_position(seat->drag_surface,
2463 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2464 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
2468 weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2470 struct weston_seat *seat;
2472 wl_list_for_each(seat, &compositor->seat_list, link)
2473 weston_seat_update_drag_surface(seat, 0, 0);
2477 bind_output(struct wl_client *client,
2478 void *data, uint32_t version, uint32_t id)
2480 struct weston_output *output = data;
2481 struct weston_mode *mode;
2482 struct wl_resource *resource;
2484 resource = wl_client_add_object(client,
2485 &wl_output_interface, NULL, id, data);
2487 wl_list_insert(&output->resource_list, &resource->link);
2488 resource->destroy = unbind_resource;
2490 wl_output_send_geometry(resource,
2496 output->make, output->model,
2499 wl_list_for_each (mode, &output->mode_list, link) {
2500 wl_output_send_mode(resource,
2509 weston_output_destroy(struct weston_output *output)
2511 struct weston_compositor *c = output->compositor;
2514 pixman_region32_fini(&output->region);
2516 for (i = 0; i < 2; i++)
2517 pixman_region32_fini(&output->buffer_damage[i]);
2519 output->compositor->output_id_pool &= ~(1 << output->id);
2521 wl_display_remove_global(c->wl_display, output->global);
2525 weston_output_compute_transform(struct weston_output *output)
2527 struct weston_matrix transform;
2530 weston_matrix_init(&transform);
2532 switch(output->transform) {
2533 case WL_OUTPUT_TRANSFORM_FLIPPED:
2534 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2535 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2536 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2544 switch(output->transform) {
2545 case WL_OUTPUT_TRANSFORM_NORMAL:
2546 case WL_OUTPUT_TRANSFORM_FLIPPED:
2547 transform.d[0] = flip;
2552 case WL_OUTPUT_TRANSFORM_90:
2553 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2555 transform.d[1] = -flip;
2559 case WL_OUTPUT_TRANSFORM_180:
2560 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2561 transform.d[0] = -flip;
2564 transform.d[5] = -1;
2566 case WL_OUTPUT_TRANSFORM_270:
2567 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2569 transform.d[1] = flip;
2570 transform.d[4] = -1;
2577 weston_matrix_multiply(&output->matrix, &transform);
2581 weston_output_update_matrix(struct weston_output *output)
2583 float magnification;
2584 struct weston_matrix camera;
2585 struct weston_matrix modelview;
2587 weston_matrix_init(&output->matrix);
2588 weston_matrix_translate(&output->matrix,
2589 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2590 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
2592 weston_matrix_scale(&output->matrix,
2593 2.0 / (output->width + output->border.left + output->border.right),
2594 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2596 weston_output_compute_transform(output);
2598 if (output->zoom.active) {
2599 magnification = 1 / (1 - output->zoom.spring_z.current);
2600 weston_matrix_init(&camera);
2601 weston_matrix_init(&modelview);
2602 weston_output_update_zoom(output, output->zoom.type);
2603 weston_matrix_translate(&camera, output->zoom.trans_x,
2604 -output->zoom.trans_y, 0);
2605 weston_matrix_invert(&modelview, &camera);
2606 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
2607 weston_matrix_multiply(&output->matrix, &modelview);
2614 weston_output_transform_init(struct weston_output *output, uint32_t transform)
2616 output->transform = transform;
2618 switch (transform) {
2619 case WL_OUTPUT_TRANSFORM_90:
2620 case WL_OUTPUT_TRANSFORM_270:
2621 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2622 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2623 /* Swap width and height */
2624 output->width = output->current->height;
2625 output->height = output->current->width;
2627 case WL_OUTPUT_TRANSFORM_NORMAL:
2628 case WL_OUTPUT_TRANSFORM_180:
2629 case WL_OUTPUT_TRANSFORM_FLIPPED:
2630 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2631 output->width = output->current->width;
2632 output->height = output->current->height;
2640 weston_output_move(struct weston_output *output, int x, int y)
2647 output->current_buffer = 0;
2648 for (i = 0; i < 2; i++)
2649 pixman_region32_init(&output->buffer_damage[i]);
2651 pixman_region32_init_rect(&output->region, x, y,
2657 weston_output_init(struct weston_output *output, struct weston_compositor *c,
2658 int x, int y, int width, int height, uint32_t transform)
2660 output->compositor = c;
2663 output->border.top = 0;
2664 output->border.bottom = 0;
2665 output->border.left = 0;
2666 output->border.right = 0;
2667 output->mm_width = width;
2668 output->mm_height = height;
2671 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
2672 output->disable_planes++;
2674 weston_output_transform_init(output, transform);
2675 weston_output_init_zoom(output);
2677 weston_output_move(output, x, y);
2678 weston_output_damage(output);
2680 wl_signal_init(&output->frame_signal);
2681 wl_list_init(&output->animation_list);
2682 wl_list_init(&output->resource_list);
2684 output->id = ffs(~output->compositor->output_id_pool) - 1;
2685 output->compositor->output_id_pool |= 1 << output->id;
2688 wl_display_add_global(c->wl_display, &wl_output_interface,
2689 output, bind_output);
2693 compositor_bind(struct wl_client *client,
2694 void *data, uint32_t version, uint32_t id)
2696 struct weston_compositor *compositor = data;
2698 wl_client_add_object(client, &wl_compositor_interface,
2699 &compositor_interface, id, compositor);
2705 struct utsname usys;
2709 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2710 usys.version, usys.machine);
2714 weston_compositor_init(struct weston_compositor *ec,
2715 struct wl_display *display,
2718 const char *config_file)
2720 struct wl_event_loop *loop;
2721 struct xkb_rule_names xkb_names;
2722 const struct config_key keyboard_config_keys[] = {
2723 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2724 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2725 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2726 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2727 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2729 const struct config_section cs[] = {
2731 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2734 memset(&xkb_names, 0, sizeof(xkb_names));
2735 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
2737 ec->wl_display = display;
2738 wl_signal_init(&ec->destroy_signal);
2739 wl_signal_init(&ec->activate_signal);
2740 wl_signal_init(&ec->kill_signal);
2741 wl_signal_init(&ec->lock_signal);
2742 wl_signal_init(&ec->unlock_signal);
2743 wl_signal_init(&ec->show_input_panel_signal);
2744 wl_signal_init(&ec->hide_input_panel_signal);
2745 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
2747 ec->output_id_pool = 0;
2749 if (!wl_display_add_global(display, &wl_compositor_interface,
2750 ec, compositor_bind))
2753 wl_list_init(&ec->surface_list);
2754 wl_list_init(&ec->layer_list);
2755 wl_list_init(&ec->seat_list);
2756 wl_list_init(&ec->output_list);
2757 wl_list_init(&ec->key_binding_list);
2758 wl_list_init(&ec->button_binding_list);
2759 wl_list_init(&ec->axis_binding_list);
2760 wl_list_init(&ec->fade.animation.link);
2762 weston_plane_init(&ec->primary_plane, 0, 0);
2764 weston_compositor_xkb_init(ec, &xkb_names);
2766 ec->ping_handler = NULL;
2768 screenshooter_create(ec);
2769 text_cursor_position_notifier_create(ec);
2770 text_model_factory_create(ec);
2772 wl_data_device_manager_init(ec->wl_display);
2774 wl_display_init_shm(display);
2776 loop = wl_display_get_event_loop(ec->wl_display);
2777 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2778 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2780 ec->input_loop = wl_event_loop_create();
2782 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2783 ec->fade.animation.frame = fade_frame;
2785 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2786 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2788 weston_compositor_schedule_repaint(ec);
2794 weston_compositor_shutdown(struct weston_compositor *ec)
2796 struct weston_output *output, *next;
2798 wl_event_source_remove(ec->idle_source);
2799 if (ec->input_loop_source)
2800 wl_event_source_remove(ec->input_loop_source);
2802 /* Destroy all outputs associated with this compositor */
2803 wl_list_for_each_safe(output, next, &ec->output_list, link)
2804 output->destroy(output);
2806 weston_binding_list_destroy_all(&ec->key_binding_list);
2807 weston_binding_list_destroy_all(&ec->button_binding_list);
2808 weston_binding_list_destroy_all(&ec->axis_binding_list);
2810 weston_plane_release(&ec->primary_plane);
2812 wl_array_release(&ec->vertices);
2813 wl_array_release(&ec->indices);
2814 wl_array_release(&ec->vtxcnt);
2816 wl_event_loop_destroy(ec->input_loop);
2819 static int on_term_signal(int signal_number, void *data)
2821 struct wl_display *display = data;
2823 weston_log("caught signal %d\n", signal_number);
2824 wl_display_terminate(display);
2830 on_segv_signal(int s, siginfo_t *siginfo, void *context)
2836 /* This SIGSEGV handler will do a best-effort backtrace, and
2837 * then call the backend restore function, which will switch
2838 * back to the vt we launched from or ungrab X etc and then
2839 * raise SIGTRAP. If we run weston under gdb from X or a
2840 * different vt, and tell gdb "handle SIGSEGV nostop", this
2841 * will allow weston to switch back to gdb on crash and then
2842 * gdb will catch the crash with SIGTRAP. */
2844 weston_log("caught segv\n");
2846 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2847 for (i = 0; i < count; i++) {
2848 dladdr(buffer[i], &info);
2849 weston_log(" [%016lx] %s (%s)\n",
2851 info.dli_sname ? info.dli_sname : "--",
2855 segv_compositor->restore(segv_compositor);
2862 load_module(const char *name, const char *entrypoint)
2864 char path[PATH_MAX];
2865 void *module, *init;
2868 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
2870 snprintf(path, sizeof path, "%s", name);
2872 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2874 weston_log("Module '%s' already loaded\n", path);
2879 weston_log("Loading module '%s'\n", path);
2880 module = dlopen(path, RTLD_NOW);
2882 weston_log("Failed to load module: %s\n", dlerror());
2886 init = dlsym(module, entrypoint);
2888 weston_log("Failed to lookup init function: %s\n", dlerror());
2896 load_modules(struct weston_compositor *ec, const char *modules)
2898 const char *p, *end;
2900 int (*module_init)(struct weston_compositor *ec);
2902 if (modules == NULL)
2907 end = strchrnul(p, ',');
2908 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2909 module_init = load_module(buffer, "module_init");
2921 static const char xdg_error_message[] =
2922 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2924 static const char xdg_wrong_message[] =
2925 "fatal: environment variable XDG_RUNTIME_DIR\n"
2926 "is set to \"%s\", which is not a directory.\n";
2928 static const char xdg_wrong_mode_message[] =
2929 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2930 "correctly. Unix access mode must be 0700 but is %o,\n"
2931 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
2932 "owned by UID %d.\n";
2934 static const char xdg_detail_message[] =
2935 "Refer to your distribution on how to get it, or\n"
2936 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2937 "on how to implement it.\n";
2940 verify_xdg_runtime_dir(void)
2942 char *dir = getenv("XDG_RUNTIME_DIR");
2946 weston_log(xdg_error_message);
2947 weston_log_continue(xdg_detail_message);
2951 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
2952 weston_log(xdg_wrong_message, dir);
2953 weston_log_continue(xdg_detail_message);
2957 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
2958 weston_log(xdg_wrong_mode_message,
2959 dir, s.st_mode & 0777, s.st_uid);
2960 weston_log_continue(xdg_detail_message);
2965 usage(int error_code)
2968 "Usage: weston [OPTIONS]\n\n"
2969 "This is weston version " VERSION ", the Wayland reference compositor.\n"
2970 "Weston supports multiple backends, and depending on which backend is in use\n"
2971 "different options will be accepted.\n\n"
2975 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
2976 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
2977 " -S, --socket=NAME\tName of socket to listen on\n"
2978 " -i, --idle-time=SECS\tIdle time in seconds\n"
2979 " --modules\t\tLoad the comma-separated list of modules\n"
2980 " --log==FILE\t\tLog to the given file\n"
2981 " -h, --help\t\tThis help message\n\n");
2984 "Options for drm-backend.so:\n\n"
2985 " --connector=ID\tBring up only this connector\n"
2986 " --seat=SEAT\t\tThe seat that weston should run on\n"
2987 " --tty=TTY\t\tThe tty to use\n"
2988 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
2991 "Options for x11-backend.so:\n\n"
2992 " --width=WIDTH\t\tWidth of X window\n"
2993 " --height=HEIGHT\tHeight of X window\n"
2994 " --fullscreen\t\tRun in fullscreen mode\n"
2995 " --output-count=COUNT\tCreate multiple outputs\n"
2996 " --no-input\t\tDont create input devices\n\n");
2999 "Options for wayland-backend.so:\n\n"
3000 " --width=WIDTH\t\tWidth of Wayland surface\n"
3001 " --height=HEIGHT\tHeight of Wayland surface\n"
3002 " --display=DISPLAY\tWayland display to connect to\n\n");
3007 int main(int argc, char *argv[])
3009 int ret = EXIT_SUCCESS;
3010 struct wl_display *display;
3011 struct weston_compositor *ec;
3012 struct wl_event_source *signals[4];
3013 struct wl_event_loop *loop;
3014 struct sigaction segv_action;
3015 struct weston_compositor
3016 *(*backend_init)(struct wl_display *display,
3017 int argc, char *argv[], const char *config_file);
3019 char *backend = NULL;
3020 const char *modules = "desktop-shell.so", *option_modules = NULL;
3022 int32_t idle_time = 300;
3024 char *socket_name = "wayland-0";
3027 const struct config_key core_config_keys[] = {
3028 { "modules", CONFIG_KEY_STRING, &modules },
3031 const struct config_section cs[] = {
3033 core_config_keys, ARRAY_LENGTH(core_config_keys) },
3036 const struct weston_option core_options[] = {
3037 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3038 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3039 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
3040 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
3041 { WESTON_OPTION_STRING, "log", 0, &log },
3042 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
3045 argc = parse_options(core_options,
3046 ARRAY_LENGTH(core_options), argc, argv);
3049 usage(EXIT_SUCCESS);
3051 weston_log_file_open(log);
3055 STAMP_SPACE "Bug reports to: %s\n"
3056 STAMP_SPACE "Build: %s\n",
3057 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
3061 verify_xdg_runtime_dir();
3063 display = wl_display_create();
3065 loop = wl_display_get_event_loop(display);
3066 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3068 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3070 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3073 wl_list_init(&child_process_list);
3074 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3078 if (getenv("WAYLAND_DISPLAY"))
3079 backend = "wayland-backend.so";
3080 else if (getenv("DISPLAY"))
3081 backend = "x11-backend.so";
3083 backend = "drm-backend.so";
3086 config_file = config_file_path("weston.ini");
3087 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
3089 backend_init = load_module(backend, "backend_init");
3093 ec = backend_init(display, argc, argv, config_file);
3095 weston_log("fatal: failed to create compositor\n");
3099 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3100 segv_action.sa_sigaction = on_segv_signal;
3101 sigemptyset(&segv_action.sa_mask);
3102 sigaction(SIGSEGV, &segv_action, NULL);
3103 segv_compositor = ec;
3105 for (i = 1; argv[i]; i++)
3106 weston_log("fatal: unhandled option: %s\n", argv[i]);
3114 ec->option_idle_time = idle_time;
3115 ec->idle_time = idle_time;
3117 setenv("WAYLAND_DISPLAY", socket_name, 1);
3119 if (load_modules(ec, modules) < 0)
3121 if (load_modules(ec, option_modules) < 0)
3124 if (wl_display_add_socket(display, socket_name)) {
3125 weston_log("fatal: failed to add socket: %m\n");
3130 weston_compositor_dpms_on(ec);
3131 weston_compositor_wake(ec);
3133 wl_display_run(display);
3136 /* prevent further rendering while shutting down */
3137 ec->state = WESTON_COMPOSITOR_SLEEPING;
3139 wl_signal_emit(&ec->destroy_signal, ec);
3141 for (i = ARRAY_LENGTH(signals); i;)
3142 wl_event_source_remove(signals[--i]);
3144 weston_compositor_xkb_destroy(ec);
3147 wl_display_destroy(display);
3149 weston_log_file_close();