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.
36 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
43 #include <linux/input.h>
50 #include <wayland-server.h>
51 #include "compositor.h"
53 static const char *option_socket_name = NULL;
55 static struct wl_list child_process_list;
56 static jmp_buf segv_jmp_buf;
59 sigchld_handler(int signal_number, void *data)
61 struct weston_process *p;
66 wl_list_for_each(p, &child_process_list, link) {
71 if (&p->link == &child_process_list) {
72 fprintf(stderr, "unknown child process exited\n");
76 wl_list_remove(&p->link);
77 p->cleanup(p, status);
83 weston_watch_process(struct weston_process *process)
85 wl_list_insert(&child_process_list, &process->link);
89 child_client_exec(int sockfd, const char *path)
95 /* do not give our signal mask to the new process */
97 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
99 /* Launch clients as the user. */
102 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
103 * non-CLOEXEC fd to pass through exec. */
104 clientfd = dup(sockfd);
105 if (clientfd == -1) {
106 fprintf(stderr, "compositor: dup failed: %m\n");
110 snprintf(s, sizeof s, "%d", clientfd);
111 setenv("WAYLAND_SOCKET", s, 1);
113 if (execl(path, path, NULL) < 0)
114 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
118 WL_EXPORT struct wl_client *
119 weston_client_launch(struct weston_compositor *compositor,
120 struct weston_process *proc,
122 weston_process_cleanup_func_t cleanup)
126 struct wl_client *client;
128 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
129 fprintf(stderr, "weston_client_launch: "
130 "socketpair failed while launching '%s': %m\n",
139 fprintf(stderr, "weston_client_launch: "
140 "fork failed while launching '%s': %m\n", path);
145 child_client_exec(sv[1], path);
151 client = wl_client_create(compositor->wl_display, sv[0]);
154 fprintf(stderr, "weston_client_launch: "
155 "wl_client_create failed while launching '%s'.\n",
161 proc->cleanup = cleanup;
162 weston_watch_process(proc);
168 surface_handle_buffer_destroy(struct wl_listener *listener,
169 struct wl_resource *resource, uint32_t time)
171 struct weston_surface *es =
172 container_of(listener, struct weston_surface,
173 buffer_destroy_listener);
178 static const pixman_region32_data_t undef_region_data;
181 undef_region(pixman_region32_t *region)
183 pixman_region32_fini(region);
184 region->data = (pixman_region32_data_t *) &undef_region_data;
188 region_is_undefined(pixman_region32_t *region)
190 return region->data == &undef_region_data;
193 WL_EXPORT struct weston_surface *
194 weston_surface_create(struct weston_compositor *compositor)
196 struct weston_surface *surface;
198 surface = calloc(1, sizeof *surface);
202 wl_list_init(&surface->surface.resource.destroy_listener_list);
204 wl_list_init(&surface->link);
205 wl_list_init(&surface->buffer_link);
207 surface->surface.resource.client = NULL;
209 surface->compositor = compositor;
210 surface->image = EGL_NO_IMAGE_KHR;
211 surface->alpha = 255;
213 surface->buffer = NULL;
214 surface->output = NULL;
216 pixman_region32_init(&surface->damage);
217 pixman_region32_init(&surface->opaque);
218 undef_region(&surface->input);
219 pixman_region32_init(&surface->transform.opaque);
220 wl_list_init(&surface->frame_callback_list);
222 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
224 wl_list_init(&surface->geometry.transformation_list);
225 wl_list_insert(&surface->geometry.transformation_list,
226 &surface->transform.position.link);
227 weston_matrix_init(&surface->transform.position.matrix);
228 pixman_region32_init(&surface->transform.boundingbox);
229 surface->geometry.dirty = 1;
235 weston_surface_set_color(struct weston_surface *surface,
236 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
238 surface->color[0] = red;
239 surface->color[1] = green;
240 surface->color[2] = blue;
241 surface->color[3] = alpha;
242 surface->shader = &surface->compositor->solid_shader;
246 surface_to_global_float(struct weston_surface *surface,
247 int32_t sx, int32_t sy, GLfloat *x, GLfloat *y)
249 if (surface->transform.enabled) {
250 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
252 weston_matrix_transform(&surface->transform.matrix, &v);
254 if (fabsf(v.f[3]) < 1e-6) {
255 fprintf(stderr, "warning: numerical instability in "
256 "weston_surface_to_global(), divisor = %g\n",
263 *x = v.f[0] / v.f[3];
264 *y = v.f[1] / v.f[3];
266 *x = sx + surface->geometry.x;
267 *y = sy + surface->geometry.y;
272 weston_surface_damage_below(struct weston_surface *surface)
274 struct weston_surface *below;
276 if (surface->output == NULL)
279 if (surface->link.next == &surface->compositor->surface_list)
282 below = container_of(surface->link.next, struct weston_surface, link);
283 pixman_region32_union(&below->damage, &below->damage,
284 &surface->transform.boundingbox);
288 surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
289 int32_t width, int32_t height,
290 pixman_region32_t *bbox)
292 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
293 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
298 { sx + width, sy + height }
300 GLfloat int_x, int_y;
303 for (i = 0; i < 4; ++i) {
305 surface_to_global_float(surface, s[i][0], s[i][1], &x, &y);
316 int_x = floorf(min_x);
317 int_y = floorf(min_y);
318 pixman_region32_init_rect(bbox, int_x, int_y,
319 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
323 weston_surface_update_transform_disable(struct weston_surface *surface)
325 surface->transform.enabled = 0;
327 /* round off fractions when not transformed */
328 surface->geometry.x = roundf(surface->geometry.x);
329 surface->geometry.y = roundf(surface->geometry.y);
331 pixman_region32_init_rect(&surface->transform.boundingbox,
334 surface->geometry.width,
335 surface->geometry.height);
337 pixman_region32_copy(&surface->transform.opaque, &surface->opaque);
338 pixman_region32_translate(&surface->transform.opaque,
339 surface->geometry.x, surface->geometry.y);
343 weston_surface_update_transform_enable(struct weston_surface *surface)
345 struct weston_matrix *matrix = &surface->transform.matrix;
346 struct weston_matrix *inverse = &surface->transform.inverse;
347 struct weston_transform *tform;
349 surface->transform.enabled = 1;
351 /* Otherwise identity matrix, but with x and y translation. */
352 surface->transform.position.matrix.d[12] = surface->geometry.x;
353 surface->transform.position.matrix.d[13] = surface->geometry.y;
355 weston_matrix_init(matrix);
356 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
357 weston_matrix_multiply(matrix, &tform->matrix);
359 if (weston_matrix_invert(inverse, matrix) < 0) {
360 /* Oops, bad total transformation, not invertible */
361 fprintf(stderr, "error: weston_surface %p"
362 " transformation not invertible.\n", surface);
366 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
367 surface->geometry.height,
368 &surface->transform.boundingbox);
374 weston_surface_update_transform(struct weston_surface *surface)
376 if (!surface->geometry.dirty)
379 surface->geometry.dirty = 0;
381 weston_surface_damage_below(surface);
383 pixman_region32_fini(&surface->transform.boundingbox);
384 pixman_region32_fini(&surface->transform.opaque);
385 pixman_region32_init(&surface->transform.opaque);
387 if (region_is_undefined(&surface->input))
388 pixman_region32_init_rect(&surface->input, 0, 0,
389 surface->geometry.width,
390 surface->geometry.height);
392 /* transform.position is always in transformation_list */
393 if (surface->geometry.transformation_list.next ==
394 &surface->transform.position.link &&
395 surface->geometry.transformation_list.prev ==
396 &surface->transform.position.link) {
397 weston_surface_update_transform_disable(surface);
399 if (weston_surface_update_transform_enable(surface) < 0)
400 weston_surface_update_transform_disable(surface);
403 /* weston_surface_damage() without update */
404 pixman_region32_union(&surface->damage, &surface->damage,
405 &surface->transform.boundingbox);
408 weston_surface_assign_output(surface);
410 weston_compositor_schedule_repaint(surface->compositor);
414 weston_surface_to_global(struct weston_surface *surface,
415 int32_t sx, int32_t sy, int32_t *x, int32_t *y)
419 weston_surface_update_transform(surface);
421 surface_to_global_float(surface, sx, sy, &xf, &yf);
427 surface_from_global_float(struct weston_surface *surface,
428 int32_t x, int32_t y, GLfloat *sx, GLfloat *sy)
430 if (surface->transform.enabled) {
431 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
433 weston_matrix_transform(&surface->transform.inverse, &v);
435 if (fabsf(v.f[3]) < 1e-6) {
436 fprintf(stderr, "warning: numerical instability in "
437 "weston_surface_from_global(), divisor = %g\n",
444 *sx = v.f[0] / v.f[3];
445 *sy = v.f[1] / v.f[3];
447 *sx = x - surface->geometry.x;
448 *sy = y - surface->geometry.y;
453 weston_surface_from_global(struct weston_surface *surface,
454 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
458 weston_surface_update_transform(surface);
460 surface_from_global_float(surface, x, y, &sxf, &syf);
466 weston_surface_damage_rectangle(struct weston_surface *surface,
467 int32_t sx, int32_t sy,
468 int32_t width, int32_t height)
470 weston_surface_update_transform(surface);
472 if (surface->transform.enabled) {
473 pixman_region32_t box;
474 surface_compute_bbox(surface, sx, sy, width, height, &box);
475 pixman_region32_union(&surface->damage, &surface->damage,
477 pixman_region32_fini(&box);
479 pixman_region32_union_rect(&surface->damage, &surface->damage,
480 surface->geometry.x + sx,
481 surface->geometry.y + sy,
485 weston_compositor_schedule_repaint(surface->compositor);
489 weston_surface_damage(struct weston_surface *surface)
491 weston_surface_update_transform(surface);
493 pixman_region32_union(&surface->damage, &surface->damage,
494 &surface->transform.boundingbox);
496 weston_compositor_schedule_repaint(surface->compositor);
500 weston_surface_flush_damage(struct weston_surface *surface)
502 struct weston_surface *below;
504 if (surface->output &&
505 surface->link.next != &surface->compositor->surface_list) {
506 below = container_of(surface->link.next,
507 struct weston_surface, link);
509 pixman_region32_union(&below->damage,
510 &below->damage, &surface->damage);
515 weston_surface_configure(struct weston_surface *surface,
516 GLfloat x, GLfloat y, int width, int height)
518 surface->geometry.x = x;
519 surface->geometry.y = y;
520 surface->geometry.width = width;
521 surface->geometry.height = height;
522 surface->geometry.dirty = 1;
526 weston_surface_set_position(struct weston_surface *surface,
527 GLfloat x, GLfloat y)
529 surface->geometry.x = x;
530 surface->geometry.y = y;
531 surface->geometry.dirty = 1;
535 weston_compositor_get_time(void)
539 gettimeofday(&tv, NULL);
541 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
544 static struct weston_surface *
545 weston_compositor_pick_surface(struct weston_compositor *compositor,
546 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
548 struct weston_surface *surface;
550 wl_list_for_each(surface, &compositor->surface_list, link) {
551 weston_surface_from_global(surface, x, y, sx, sy);
552 if (pixman_region32_contains_point(&surface->input,
561 weston_device_repick(struct wl_input_device *device, uint32_t time)
563 struct weston_input_device *wd = (struct weston_input_device *) device;
564 const struct wl_pointer_grab_interface *interface;
565 struct weston_surface *surface, *focus;
567 surface = weston_compositor_pick_surface(wd->compositor,
568 device->x, device->y,
572 if (&surface->surface != device->current) {
573 interface = device->pointer_grab->interface;
574 interface->focus(device->pointer_grab, time, &surface->surface,
575 device->current_x, device->current_y);
576 device->current = &surface->surface;
579 focus = (struct weston_surface *) device->pointer_grab->focus;
581 weston_surface_from_global(focus, device->x, device->y,
582 &device->pointer_grab->x, &device->pointer_grab->y);
586 weston_compositor_repick(struct weston_compositor *compositor)
588 struct weston_input_device *device;
591 if (!compositor->focus)
594 time = weston_compositor_get_time();
595 wl_list_for_each(device, &compositor->input_device_list, link)
596 weston_device_repick(&device->input_device, time);
600 weston_surface_unmap(struct weston_surface *surface)
602 weston_surface_damage_below(surface);
603 weston_surface_flush_damage(surface);
604 surface->output = NULL;
605 wl_list_remove(&surface->link);
606 weston_compositor_repick(surface->compositor);
607 weston_compositor_schedule_repaint(surface->compositor);
611 destroy_surface(struct wl_resource *resource)
613 struct weston_surface *surface =
614 container_of(resource,
615 struct weston_surface, surface.resource);
616 struct weston_compositor *compositor = surface->compositor;
619 weston_surface_unmap(surface);
621 if (surface->texture)
622 glDeleteTextures(1, &surface->texture);
625 wl_list_remove(&surface->buffer_destroy_listener.link);
627 if (surface->image != EGL_NO_IMAGE_KHR)
628 compositor->destroy_image(compositor->display,
631 wl_list_remove(&surface->buffer_link);
633 pixman_region32_fini(&surface->transform.boundingbox);
634 pixman_region32_fini(&surface->damage);
635 pixman_region32_fini(&surface->opaque);
636 if (!region_is_undefined(&surface->input))
637 pixman_region32_fini(&surface->input);
643 weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
645 struct weston_surface *es = (struct weston_surface *) surface;
646 struct weston_compositor *ec = es->compositor;
647 struct wl_list *surfaces_attached_to;
650 glGenTextures(1, &es->texture);
651 glBindTexture(GL_TEXTURE_2D, es->texture);
652 glTexParameteri(GL_TEXTURE_2D,
653 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
654 glTexParameteri(GL_TEXTURE_2D,
655 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
656 es->shader = &ec->texture_shader;
658 glBindTexture(GL_TEXTURE_2D, es->texture);
661 if (wl_buffer_is_shm(buffer)) {
662 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
663 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
664 es->pitch, buffer->height, 0,
665 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
666 wl_shm_buffer_get_data(buffer));
668 surfaces_attached_to = buffer->user_data;
670 wl_list_remove(&es->buffer_link);
671 wl_list_insert(surfaces_attached_to, &es->buffer_link);
673 if (es->image != EGL_NO_IMAGE_KHR)
674 ec->destroy_image(ec->display, es->image);
675 es->image = ec->create_image(ec->display, NULL,
676 EGL_WAYLAND_BUFFER_WL,
679 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
681 es->pitch = es->geometry.width;
686 texture_region(struct weston_surface *es, pixman_region32_t *region)
688 struct weston_compositor *ec = es->compositor;
689 GLfloat *v, inv_width, inv_height;
691 pixman_box32_t *rectangles;
695 rectangles = pixman_region32_rectangles(region, &n);
696 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
697 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
698 inv_width = 1.0 / es->pitch;
699 inv_height = 1.0 / es->geometry.height;
701 for (i = 0; i < n; i++, v += 16, p += 6) {
702 surface_from_global_float(es, rectangles[i].x1,
703 rectangles[i].y1, &sx, &sy);
704 v[ 0] = rectangles[i].x1;
705 v[ 1] = rectangles[i].y1;
706 v[ 2] = sx * inv_width;
707 v[ 3] = sy * inv_height;
709 surface_from_global_float(es, rectangles[i].x1,
710 rectangles[i].y2, &sx, &sy);
711 v[ 4] = rectangles[i].x1;
712 v[ 5] = rectangles[i].y2;
713 v[ 6] = sx * inv_width;
714 v[ 7] = sy * inv_height;
716 surface_from_global_float(es, rectangles[i].x2,
717 rectangles[i].y1, &sx, &sy);
718 v[ 8] = rectangles[i].x2;
719 v[ 9] = rectangles[i].y1;
720 v[10] = sx * inv_width;
721 v[11] = sy * inv_height;
723 surface_from_global_float(es, rectangles[i].x2,
724 rectangles[i].y2, &sx, &sy);
725 v[12] = rectangles[i].x2;
726 v[13] = rectangles[i].y2;
727 v[14] = sx * inv_width;
728 v[15] = sy * inv_height;
742 weston_surface_draw(struct weston_surface *es, struct weston_output *output)
744 struct weston_compositor *ec = es->compositor;
746 pixman_region32_t repaint;
750 pixman_region32_init(&repaint);
751 pixman_region32_intersect(&repaint, &es->transform.boundingbox,
753 pixman_region32_intersect(&repaint, &repaint, &es->damage);
755 /* Clear damage, assume outputs do not overlap. */
756 pixman_region32_subtract(&es->damage, &es->damage, &output->region);
758 if (!pixman_region32_not_empty(&repaint))
761 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
764 if (ec->current_shader != es->shader) {
765 glUseProgram(es->shader->program);
766 ec->current_shader = es->shader;
769 glUniformMatrix4fv(es->shader->proj_uniform,
770 1, GL_FALSE, output->matrix.d);
771 glUniform1i(es->shader->tex_uniform, 0);
772 glUniform4fv(es->shader->color_uniform, 1, es->color);
773 glUniform1f(es->shader->alpha_uniform, es->alpha / 255.0);
774 glUniform1f(es->shader->texwidth_uniform,
775 (GLfloat)es->geometry.width / es->pitch);
777 if (es->transform.enabled || output->zoom.active)
782 n = texture_region(es, &repaint);
784 glBindTexture(GL_TEXTURE_2D, es->texture);
785 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
786 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
788 v = ec->vertices.data;
789 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
790 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
791 glEnableVertexAttribArray(0);
792 glEnableVertexAttribArray(1);
794 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
796 glDisableVertexAttribArray(1);
797 glDisableVertexAttribArray(0);
799 ec->vertices.size = 0;
800 ec->indices.size = 0;
803 pixman_region32_fini(&repaint);
806 WL_EXPORT struct wl_list *
807 weston_compositor_top(struct weston_compositor *compositor)
809 struct weston_input_device *input_device;
810 struct wl_list *list;
812 input_device = (struct weston_input_device *) compositor->input_device;
814 /* Insert below pointer */
815 list = &compositor->surface_list;
816 if (compositor->fade.surface &&
817 list->next == &compositor->fade.surface->link)
819 if (list->next == &input_device->sprite->link)
821 if (input_device->drag_surface &&
822 list->next == &input_device->drag_surface->link)
829 weston_surface_raise(struct weston_surface *surface)
831 struct weston_compositor *compositor = surface->compositor;
832 struct wl_list *list = weston_compositor_top(compositor);
834 wl_list_remove(&surface->link);
835 wl_list_insert(list, &surface->link);
836 weston_compositor_repick(compositor);
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;
863 struct weston_surface *es;
865 if (wl_list_empty(&compositor->surface_list))
868 es = container_of(compositor->surface_list.next,
869 struct weston_surface, link);
870 pixman_region32_union(&es->damage, &es->damage, &output->region);
871 weston_compositor_schedule_repaint(compositor);
875 fade_frame(struct weston_animation *animation,
876 struct weston_output *output, uint32_t msecs)
878 struct weston_compositor *compositor =
879 container_of(animation,
880 struct weston_compositor, fade.animation);
881 struct weston_surface *surface;
883 surface = compositor->fade.surface;
884 weston_spring_update(&compositor->fade.spring, msecs);
885 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
886 compositor->fade.spring.current);
887 weston_surface_damage(surface);
889 if (weston_spring_done(&compositor->fade.spring)) {
890 compositor->fade.spring.current =
891 compositor->fade.spring.target;
892 wl_list_remove(&animation->link);
893 wl_list_init(&animation->link);
895 if (compositor->fade.spring.current < 0.001) {
896 destroy_surface(&surface->surface.resource);
897 compositor->fade.surface = NULL;
898 } else if (compositor->fade.spring.current > 0.999) {
899 compositor->state = WESTON_COMPOSITOR_SLEEPING;
900 compositor->shell->lock(compositor->shell);
906 weston_output_set_cursor(struct weston_output *output,
907 struct wl_input_device *dev)
909 struct weston_input_device *device =
910 (struct weston_input_device *) dev;
911 pixman_region32_t cursor_region;
912 int prior_was_hardware;
914 if (device->sprite == NULL)
917 pixman_region32_init(&cursor_region);
918 pixman_region32_intersect(&cursor_region,
919 &device->sprite->transform.boundingbox,
922 if (!pixman_region32_not_empty(&cursor_region)) {
923 output->set_hardware_cursor(output, NULL);
927 prior_was_hardware = device->hw_cursor;
928 if (device->sprite->overlapped ||
929 output->set_hardware_cursor(output, device) < 0) {
930 if (prior_was_hardware) {
931 weston_surface_damage(device->sprite);
932 output->set_hardware_cursor(output, NULL);
934 device->hw_cursor = 0;
936 if (!prior_was_hardware)
937 weston_surface_damage_below(device->sprite);
938 pixman_region32_fini(&device->sprite->damage);
939 pixman_region32_init(&device->sprite->damage);
940 device->hw_cursor = 1;
944 pixman_region32_fini(&cursor_region);
947 struct weston_frame_callback {
948 struct wl_resource resource;
953 weston_output_repaint(struct weston_output *output, int msecs)
955 struct weston_compositor *ec = output->compositor;
956 struct weston_surface *es;
957 struct weston_animation *animation, *next;
958 struct weston_frame_callback *cb, *cnext;
959 pixman_region32_t opaque, new_damage, total_damage,
960 overlap, surface_overlap;
961 int32_t width, height;
963 weston_compositor_update_drag_surfaces(ec);
965 width = output->current->width +
966 output->border.left + output->border.right;
967 height = output->current->height +
968 output->border.top + output->border.bottom;
969 glViewport(0, 0, width, height);
971 pixman_region32_init(&new_damage);
972 pixman_region32_init(&opaque);
973 pixman_region32_init(&overlap);
975 wl_list_for_each(es, &ec->surface_list, link) {
976 /* Update surface transform now to avoid calling it ever
977 * again from the repaint sub-functions. */
978 weston_surface_update_transform(es);
980 pixman_region32_init(&surface_overlap);
981 pixman_region32_intersect(&surface_overlap, &overlap,
982 &es->transform.boundingbox);
983 es->overlapped = pixman_region32_not_empty(&surface_overlap);
984 pixman_region32_fini(&surface_overlap);
985 pixman_region32_union(&overlap, &overlap,
986 &es->transform.boundingbox);
989 if (output->assign_planes)
991 * This will queue flips for the fbs and sprites where
992 * applicable and clear the damage for those surfaces.
993 * The repaint loop below will repaint everything
996 output->assign_planes(output);
998 weston_output_set_cursor(output, ec->input_device);
1000 wl_list_for_each(es, &ec->surface_list, link) {
1001 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
1002 pixman_region32_union(&new_damage, &new_damage, &es->damage);
1003 pixman_region32_union(&opaque, &opaque, &es->transform.opaque);
1006 pixman_region32_init(&total_damage);
1007 pixman_region32_union(&total_damage, &new_damage,
1008 &output->previous_damage);
1009 pixman_region32_intersect(&output->previous_damage,
1010 &new_damage, &output->region);
1012 pixman_region32_fini(&opaque);
1013 pixman_region32_fini(&overlap);
1014 pixman_region32_fini(&new_damage);
1016 wl_list_for_each(es, &ec->surface_list, link) {
1017 pixman_region32_copy(&es->damage, &total_damage);
1018 pixman_region32_subtract(&total_damage,
1019 &total_damage, &es->transform.opaque);
1023 weston_output_update_matrix(output);
1025 output->repaint(output);
1027 pixman_region32_fini(&total_damage);
1029 output->repaint_needed = 0;
1031 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
1032 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
1033 wl_resource_destroy(&cb->resource, 0);
1036 wl_list_for_each_safe(animation, next, &ec->animation_list, link)
1037 animation->frame(animation, output, msecs);
1041 idle_repaint(void *data)
1043 struct weston_output *output = data;
1045 /* An idle repaint may have been cancelled by vt switching away. */
1046 if (output->repaint_needed)
1047 weston_output_repaint(output, weston_compositor_get_time());
1049 output->repaint_scheduled = 0;
1053 weston_output_finish_frame(struct weston_output *output, int msecs)
1055 if (output->repaint_needed)
1056 weston_output_repaint(output, msecs);
1058 output->repaint_scheduled = 0;
1062 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1064 struct weston_output *output;
1065 struct wl_event_loop *loop;
1067 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1070 loop = wl_display_get_event_loop(compositor->wl_display);
1071 wl_list_for_each(output, &compositor->output_list, link) {
1072 output->repaint_needed = 1;
1073 if (output->repaint_scheduled)
1076 wl_event_loop_add_idle(loop, idle_repaint, output);
1077 output->repaint_scheduled = 1;
1082 weston_compositor_fade(struct weston_compositor *compositor, float tint)
1084 struct weston_surface *surface;
1087 done = weston_spring_done(&compositor->fade.spring);
1088 compositor->fade.spring.target = tint;
1089 if (weston_spring_done(&compositor->fade.spring))
1093 compositor->fade.spring.timestamp =
1094 weston_compositor_get_time();
1096 if (compositor->fade.surface == NULL) {
1097 surface = weston_surface_create(compositor);
1098 weston_surface_configure(surface, 0, 0, 8192, 8192);
1099 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
1100 wl_list_insert(&compositor->surface_list, &surface->link);
1101 weston_surface_assign_output(surface);
1102 compositor->fade.surface = surface;
1105 weston_surface_damage(compositor->fade.surface);
1106 if (wl_list_empty(&compositor->fade.animation.link))
1107 wl_list_insert(compositor->animation_list.prev,
1108 &compositor->fade.animation.link);
1112 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1114 wl_resource_destroy(resource, weston_compositor_get_time());
1118 weston_surface_assign_output(struct weston_surface *es)
1120 struct weston_compositor *ec = es->compositor;
1121 struct weston_output *output, *new_output;
1122 pixman_region32_t region;
1126 weston_surface_update_transform(es);
1130 pixman_region32_init(®ion);
1131 wl_list_for_each(output, &ec->output_list, link) {
1132 pixman_region32_intersect(®ion, &es->transform.boundingbox,
1135 e = pixman_region32_extents(®ion);
1136 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1139 new_output = output;
1143 pixman_region32_fini(®ion);
1145 es->output = new_output;
1146 if (!wl_list_empty(&es->frame_callback_list)) {
1147 wl_list_insert_list(new_output->frame_callback_list.prev,
1148 &es->frame_callback_list);
1149 wl_list_init(&es->frame_callback_list);
1154 surface_attach(struct wl_client *client,
1155 struct wl_resource *resource,
1156 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1158 struct weston_surface *es = resource->data;
1159 struct weston_shell *shell = es->compositor->shell;
1160 struct wl_buffer *buffer;
1162 if (!buffer_resource && !es->output)
1166 weston_buffer_post_release(es->buffer);
1167 wl_list_remove(&es->buffer_destroy_listener.link);
1170 if (!buffer_resource && es->output) {
1171 weston_surface_unmap(es);
1176 buffer = buffer_resource->data;
1177 buffer->busy_count++;
1178 es->buffer = buffer;
1179 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1180 &es->buffer_destroy_listener.link);
1182 if (es->geometry.width != buffer->width ||
1183 es->geometry.height != buffer->height) {
1184 undef_region(&es->input);
1185 pixman_region32_fini(&es->opaque);
1186 pixman_region32_init(&es->opaque);
1189 if (es->output == NULL) {
1190 shell->map(shell, es, buffer->width, buffer->height, sx, sy);
1191 } else if (sx != 0 || sy != 0 ||
1192 es->geometry.width != buffer->width ||
1193 es->geometry.height != buffer->height) {
1194 GLfloat from_x, from_y;
1197 surface_to_global_float(es, 0, 0, &from_x, &from_y);
1198 surface_to_global_float(es, sx, sy, &to_x, &to_y);
1199 shell->configure(shell, es,
1200 es->geometry.x + to_x - from_x,
1201 es->geometry.y + to_y - from_y,
1202 buffer->width, buffer->height);
1205 weston_buffer_attach(buffer, &es->surface);
1209 surface_damage(struct wl_client *client,
1210 struct wl_resource *resource,
1211 int32_t x, int32_t y, int32_t width, int32_t height)
1213 struct weston_surface *es = resource->data;
1215 weston_surface_damage_rectangle(es, x, y, width, height);
1219 destroy_frame_callback(struct wl_resource *resource)
1221 struct weston_frame_callback *cb = resource->data;
1223 wl_list_remove(&cb->link);
1228 surface_frame(struct wl_client *client,
1229 struct wl_resource *resource, uint32_t callback)
1231 struct weston_frame_callback *cb;
1232 struct weston_surface *es = resource->data;
1234 cb = malloc(sizeof *cb);
1236 wl_resource_post_no_memory(resource);
1240 cb->resource.object.interface = &wl_callback_interface;
1241 cb->resource.object.id = callback;
1242 cb->resource.destroy = destroy_frame_callback;
1243 cb->resource.client = client;
1244 cb->resource.data = cb;
1246 wl_client_add_resource(client, &cb->resource);
1249 wl_list_insert(es->output->frame_callback_list.prev,
1252 wl_list_insert(es->frame_callback_list.prev, &cb->link);
1257 surface_set_opaque_region(struct wl_client *client,
1258 struct wl_resource *resource,
1259 struct wl_resource *region_resource)
1261 struct weston_surface *surface = resource->data;
1262 struct weston_region *region;
1264 pixman_region32_fini(&surface->opaque);
1266 if (region_resource) {
1267 region = region_resource->data;
1268 pixman_region32_init_rect(&surface->opaque, 0, 0,
1269 surface->geometry.width,
1270 surface->geometry.height);
1271 pixman_region32_intersect(&surface->opaque,
1272 &surface->opaque, ®ion->region);
1274 pixman_region32_init(&surface->opaque);
1277 surface->geometry.dirty = 1;
1281 surface_set_input_region(struct wl_client *client,
1282 struct wl_resource *resource,
1283 struct wl_resource *region_resource)
1285 struct weston_surface *surface = resource->data;
1286 struct weston_region *region;
1288 if (region_resource) {
1289 region = region_resource->data;
1290 pixman_region32_init_rect(&surface->input, 0, 0,
1291 surface->geometry.width,
1292 surface->geometry.height);
1293 pixman_region32_intersect(&surface->input,
1294 &surface->input, ®ion->region);
1296 pixman_region32_init_rect(&surface->input, 0, 0,
1297 surface->geometry.width,
1298 surface->geometry.height);
1301 weston_compositor_repick(surface->compositor);
1304 const static struct wl_surface_interface surface_interface = {
1309 surface_set_opaque_region,
1310 surface_set_input_region
1314 compositor_create_surface(struct wl_client *client,
1315 struct wl_resource *resource, uint32_t id)
1317 struct weston_compositor *ec = resource->data;
1318 struct weston_surface *surface;
1320 surface = weston_surface_create(ec);
1321 if (surface == NULL) {
1322 wl_resource_post_no_memory(resource);
1326 surface->surface.resource.destroy = destroy_surface;
1328 surface->surface.resource.object.id = id;
1329 surface->surface.resource.object.interface = &wl_surface_interface;
1330 surface->surface.resource.object.implementation =
1331 (void (**)(void)) &surface_interface;
1332 surface->surface.resource.data = surface;
1334 wl_client_add_resource(client, &surface->surface.resource);
1338 destroy_region(struct wl_resource *resource)
1340 struct weston_region *region =
1341 container_of(resource, struct weston_region, resource);
1343 pixman_region32_fini(®ion->region);
1348 region_destroy(struct wl_client *client, struct wl_resource *resource)
1350 wl_resource_destroy(resource, weston_compositor_get_time());
1354 region_add(struct wl_client *client, struct wl_resource *resource,
1355 int32_t x, int32_t y, int32_t width, int32_t height)
1357 struct weston_region *region = resource->data;
1359 pixman_region32_union_rect(®ion->region, ®ion->region,
1360 x, y, width, height);
1364 region_subtract(struct wl_client *client, struct wl_resource *resource,
1365 int32_t x, int32_t y, int32_t width, int32_t height)
1367 struct weston_region *region = resource->data;
1368 pixman_region32_t rect;
1370 pixman_region32_init_rect(&rect, x, y, width, height);
1371 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
1372 pixman_region32_fini(&rect);
1375 static const struct wl_region_interface region_interface = {
1382 compositor_create_region(struct wl_client *client,
1383 struct wl_resource *resource, uint32_t id)
1385 struct weston_region *region;
1387 region = malloc(sizeof *region);
1388 if (region == NULL) {
1389 wl_resource_post_no_memory(resource);
1393 region->resource.destroy = destroy_region;
1395 region->resource.object.id = id;
1396 region->resource.object.interface = &wl_region_interface;
1397 region->resource.object.implementation =
1398 (void (**)(void)) ®ion_interface;
1399 region->resource.data = region;
1401 pixman_region32_init(®ion->region);
1403 wl_client_add_resource(client, ®ion->resource);
1406 const static struct wl_compositor_interface compositor_interface = {
1407 compositor_create_surface,
1408 compositor_create_region
1412 weston_compositor_wake(struct weston_compositor *compositor)
1414 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1415 weston_compositor_fade(compositor, 0.0);
1417 wl_event_source_timer_update(compositor->idle_source,
1418 compositor->idle_time * 1000);
1422 weston_compositor_activity(struct weston_compositor *compositor)
1424 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1425 weston_compositor_wake(compositor);
1427 compositor->shell->unlock(compositor->shell);
1432 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
1434 weston_compositor_activity(compositor);
1435 compositor->idle_inhibit++;
1439 weston_compositor_idle_release(struct weston_compositor *compositor)
1441 compositor->idle_inhibit--;
1442 weston_compositor_activity(compositor);
1446 idle_handler(void *data)
1448 struct weston_compositor *compositor = data;
1450 if (compositor->idle_inhibit)
1453 weston_compositor_fade(compositor, 1.0);
1459 weston_input_update_drag_surface(struct wl_input_device *input_device,
1463 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1465 struct weston_output *output;
1466 const struct wl_pointer_grab_interface *interface;
1467 struct weston_input_device *wd = (struct weston_input_device *) device;
1468 struct weston_compositor *ec = wd->compositor;
1469 int x_valid = 0, y_valid = 0;
1470 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1472 weston_compositor_activity(ec);
1474 wl_list_for_each(output, &ec->output_list, link) {
1475 if (output->x <= x && x < output->x + output->current->width)
1478 if (output->y <= y && y < output->y + output->current->height)
1481 /* FIXME: calculate this only on output addition/deletion */
1482 if (output->x < min_x)
1484 if (output->y < min_y)
1487 if (output->x + output->current->width > max_x)
1488 max_x = output->x + output->current->width - 1;
1489 if (output->y + output->current->height > max_y)
1490 max_y = output->y + output->current->height - 1;
1496 else if (x >= max_x)
1502 else if (y >= max_y)
1506 weston_input_update_drag_surface(device,
1507 x - device->x, y - device->y);
1512 wl_list_for_each(output, &ec->output_list, link)
1513 if (output->zoom.active &&
1514 pixman_region32_contains_point(&output->region, x, y, NULL))
1515 weston_output_update_zoom(output, x, y);
1517 weston_device_repick(device, time);
1518 interface = device->pointer_grab->interface;
1519 interface->motion(device->pointer_grab, time,
1520 device->pointer_grab->x, device->pointer_grab->y);
1523 weston_surface_set_position(wd->sprite,
1524 device->x - wd->hotspot_x,
1525 device->y - wd->hotspot_y);
1527 weston_compositor_schedule_repaint(ec);
1532 weston_surface_activate(struct weston_surface *surface,
1533 struct weston_input_device *device, uint32_t time)
1535 weston_surface_raise(surface);
1536 wl_input_device_set_keyboard_focus(&device->input_device,
1537 &surface->surface, time);
1538 wl_data_device_set_keyboard_focus(&device->input_device);
1542 notify_button(struct wl_input_device *device,
1543 uint32_t time, int32_t button, int32_t state)
1545 struct weston_input_device *wd = (struct weston_input_device *) device;
1546 struct weston_compositor *compositor = wd->compositor;
1549 weston_compositor_idle_inhibit(compositor);
1550 if (device->button_count == 0) {
1551 device->grab_button = button;
1552 device->grab_time = time;
1553 device->grab_x = device->x;
1554 device->grab_y = device->y;
1556 device->button_count++;
1558 weston_compositor_idle_release(compositor);
1559 device->button_count--;
1562 weston_compositor_run_binding(compositor, wd, time, 0, button, state);
1564 device->pointer_grab->interface->button(device->pointer_grab, time, button, state);
1569 update_modifier_state(struct weston_input_device *device,
1570 uint32_t key, uint32_t state)
1577 modifier = MODIFIER_CTRL;
1582 modifier = MODIFIER_ALT;
1587 modifier = MODIFIER_SUPER;
1596 device->modifier_state |= modifier;
1598 device->modifier_state &= ~modifier;
1602 notify_key(struct wl_input_device *device,
1603 uint32_t time, uint32_t key, uint32_t state)
1605 struct weston_input_device *wd = (struct weston_input_device *) device;
1606 struct weston_compositor *compositor = wd->compositor;
1610 weston_compositor_idle_inhibit(compositor);
1611 device->grab_key = key;
1612 device->grab_time = time;
1614 weston_compositor_idle_release(compositor);
1617 if (device->keyboard_grab == &device->default_keyboard_grab)
1618 weston_compositor_run_binding(compositor, wd,
1619 time, key, 0, state);
1621 update_modifier_state(wd, key, state);
1622 end = device->keys.data + device->keys.size;
1623 for (k = device->keys.data; k < end; k++) {
1627 device->keys.size = (void *) end - device->keys.data;
1629 k = wl_array_add(&device->keys, sizeof *k);
1633 device->keyboard_grab->interface->key(device->keyboard_grab,
1638 notify_pointer_focus(struct wl_input_device *device,
1639 uint32_t time, struct weston_output *output,
1640 int32_t x, int32_t y)
1642 struct weston_input_device *wd = (struct weston_input_device *) device;
1643 struct weston_compositor *compositor = wd->compositor;
1646 weston_input_update_drag_surface(device, x - device->x,
1651 compositor->focus = 1;
1652 weston_compositor_repick(compositor);
1654 compositor->focus = 0;
1655 weston_compositor_repick(compositor);
1660 notify_keyboard_focus(struct wl_input_device *device,
1661 uint32_t time, struct weston_output *output,
1662 struct wl_array *keys)
1664 struct weston_input_device *wd =
1665 (struct weston_input_device *) device;
1666 struct weston_compositor *compositor = wd->compositor;
1667 struct weston_surface *es;
1670 if (!wl_list_empty(&compositor->surface_list))
1671 es = container_of(compositor->surface_list.next,
1672 struct weston_surface, link);
1677 wl_array_copy(&wd->input_device.keys, keys);
1678 wd->modifier_state = 0;
1679 end = device->keys.data + device->keys.size;
1680 for (k = device->keys.data; k < end; k++) {
1681 weston_compositor_idle_inhibit(compositor);
1682 update_modifier_state(wd, *k, 1);
1685 if (es && es->surface.resource.client)
1686 wl_input_device_set_keyboard_focus(&wd->input_device,
1687 &es->surface, time);
1689 end = device->keys.data + device->keys.size;
1690 for (k = device->keys.data; k < end; k++)
1691 weston_compositor_idle_release(compositor);
1693 wd->modifier_state = 0;
1694 wl_input_device_set_keyboard_focus(&wd->input_device,
1699 /* TODO: share this function with wayland-server.c */
1700 static struct wl_resource *
1701 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
1703 struct wl_resource *r;
1708 wl_list_for_each(r, list, link) {
1709 if (r->client == surface->resource.client)
1717 lose_touch_focus_resource(struct wl_listener *listener,
1718 struct wl_resource *resource, uint32_t time)
1720 struct weston_input_device *device =
1721 container_of(listener, struct weston_input_device,
1722 touch_focus_resource_listener);
1724 device->touch_focus_resource = NULL;
1728 lose_touch_focus(struct wl_listener *listener,
1729 struct wl_resource *resource, uint32_t time)
1731 struct weston_input_device *device =
1732 container_of(listener, struct weston_input_device,
1733 touch_focus_listener);
1735 device->touch_focus = NULL;
1739 touch_set_focus(struct weston_input_device *device,
1740 struct wl_surface *surface, uint32_t time)
1742 struct wl_input_device *input_device = &device->input_device;
1743 struct wl_resource *resource;
1745 if (device->touch_focus == surface)
1750 find_resource_for_surface(&input_device->resource_list,
1753 fprintf(stderr, "couldn't find resource\n");
1757 device->touch_focus_resource_listener.func =
1758 lose_touch_focus_resource;
1759 wl_list_insert(resource->destroy_listener_list.prev,
1760 &device->touch_focus_resource_listener.link);
1761 device->touch_focus_listener.func = lose_touch_focus;
1762 wl_list_insert(surface->resource.destroy_listener_list.prev,
1763 &device->touch_focus_listener.link);
1765 device->touch_focus = surface;
1766 device->touch_focus_resource = resource;
1768 if (device->touch_focus)
1769 wl_list_remove(&device->touch_focus_listener.link);
1770 if (device->touch_focus_resource)
1771 wl_list_remove(&device->touch_focus_resource_listener.link);
1772 device->touch_focus = NULL;
1773 device->touch_focus_resource = NULL;
1778 * notify_touch - emulates button touches and notifies surfaces accordingly.
1780 * It assumes always the correct cycle sequence until it gets here: touch_down
1781 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1782 * for sending along such order.
1786 notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
1787 int x, int y, int touch_type)
1789 struct weston_input_device *wd = (struct weston_input_device *) device;
1790 struct weston_compositor *ec = wd->compositor;
1791 struct weston_surface *es;
1794 switch (touch_type) {
1795 case WL_INPUT_DEVICE_TOUCH_DOWN:
1796 weston_compositor_idle_inhibit(ec);
1800 /* the first finger down picks the surface, and all further go
1801 * to that surface for the remainder of the touch session i.e.
1802 * until all touch points are up again. */
1803 if (wd->num_tp == 1) {
1804 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1805 touch_set_focus(wd, &es->surface, time);
1806 } else if (wd->touch_focus) {
1807 es = (struct weston_surface *) wd->touch_focus;
1808 weston_surface_from_global(es, x, y, &sx, &sy);
1811 if (wd->touch_focus_resource && wd->touch_focus)
1812 wl_resource_post_event(wd->touch_focus_resource,
1817 case WL_INPUT_DEVICE_TOUCH_MOTION:
1818 es = (struct weston_surface *) wd->touch_focus;
1822 weston_surface_from_global(es, x, y, &sx, &sy);
1823 if (wd->touch_focus_resource)
1824 wl_resource_post_event(wd->touch_focus_resource,
1828 case WL_INPUT_DEVICE_TOUCH_UP:
1829 weston_compositor_idle_release(ec);
1832 if (wd->touch_focus_resource)
1833 wl_resource_post_event(wd->touch_focus_resource,
1834 touch_type, time, touch_id);
1835 if (wd->num_tp == 0)
1836 touch_set_focus(wd, NULL, time);
1842 input_device_attach(struct wl_client *client,
1843 struct wl_resource *resource,
1845 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1847 struct weston_input_device *device = resource->data;
1848 struct weston_compositor *compositor = device->compositor;
1849 struct wl_buffer *buffer;
1851 if (time < device->input_device.pointer_focus_time)
1853 if (device->input_device.pointer_focus == NULL)
1855 if (device->input_device.pointer_focus->resource.client != client)
1858 if (!buffer_resource && device->sprite->output) {
1859 wl_list_remove(&device->sprite->link);
1860 device->sprite->output = NULL;
1864 if (!device->sprite->output) {
1865 wl_list_insert(&compositor->surface_list,
1866 &device->sprite->link);
1867 weston_surface_assign_output(device->sprite);
1870 buffer = buffer_resource->data;
1871 device->hotspot_x = x;
1872 device->hotspot_y = y;
1873 weston_surface_configure(device->sprite,
1874 device->input_device.x - device->hotspot_x,
1875 device->input_device.y - device->hotspot_y,
1876 buffer->width, buffer->height);
1878 weston_buffer_attach(buffer, &device->sprite->surface);
1881 const static struct wl_input_device_interface input_device_interface = {
1882 input_device_attach,
1885 static void unbind_input_device(struct wl_resource *resource)
1887 wl_list_remove(&resource->link);
1892 bind_input_device(struct wl_client *client,
1893 void *data, uint32_t version, uint32_t id)
1895 struct wl_input_device *device = data;
1896 struct wl_resource *resource;
1898 resource = wl_client_add_object(client, &wl_input_device_interface,
1899 &input_device_interface, id, data);
1900 wl_list_insert(&device->resource_list, &resource->link);
1901 resource->destroy = unbind_input_device;
1905 weston_input_device_init(struct weston_input_device *device,
1906 struct weston_compositor *ec)
1908 wl_input_device_init(&device->input_device);
1910 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1911 device, bind_input_device);
1913 device->sprite = weston_surface_create(ec);
1915 device->compositor = ec;
1916 device->hotspot_x = 16;
1917 device->hotspot_y = 16;
1918 device->modifier_state = 0;
1921 wl_list_insert(ec->input_device_list.prev, &device->link);
1925 weston_input_device_release(struct weston_input_device *device)
1927 wl_list_remove(&device->link);
1928 /* The global object is destroyed at wl_display_destroy() time. */
1931 destroy_surface(&device->sprite->surface.resource);
1933 wl_input_device_release(&device->input_device);
1937 weston_input_update_drag_surface(struct wl_input_device *input_device,
1940 int surface_changed = 0;
1941 struct weston_input_device *device = (struct weston_input_device *)
1944 if (!device->drag_surface && !input_device->drag_surface)
1947 if (device->drag_surface && input_device->drag_surface &&
1948 (&device->drag_surface->surface.resource !=
1949 &input_device->drag_surface->resource))
1950 /* between calls to this funcion we got a new drag_surface */
1951 surface_changed = 1;
1953 if (!input_device->drag_surface || surface_changed) {
1954 device->drag_surface = NULL;
1955 if (!surface_changed)
1959 if (!device->drag_surface || surface_changed) {
1960 device->drag_surface = (struct weston_surface *)
1961 input_device->drag_surface;
1963 weston_surface_set_position(device->drag_surface,
1964 input_device->x, input_device->y);
1967 if (device->drag_surface->output == NULL &&
1968 device->drag_surface->buffer) {
1969 wl_list_insert(weston_compositor_top(device->compositor),
1970 &device->drag_surface->link);
1971 weston_surface_assign_output(device->drag_surface);
1977 weston_surface_set_position(device->drag_surface,
1978 device->drag_surface->geometry.x + dx,
1979 device->drag_surface->geometry.y + dy);
1983 weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
1985 weston_input_update_drag_surface(compositor->input_device, 0, 0);
1989 bind_output(struct wl_client *client,
1990 void *data, uint32_t version, uint32_t id)
1992 struct weston_output *output = data;
1993 struct weston_mode *mode;
1994 struct wl_resource *resource;
1996 resource = wl_client_add_object(client,
1997 &wl_output_interface, NULL, id, data);
1999 wl_resource_post_event(resource,
2006 output->make, output->model);
2008 wl_list_for_each (mode, &output->mode_list, link) {
2009 wl_resource_post_event(resource,
2018 static const char vertex_shader[] =
2019 "uniform mat4 proj;\n"
2020 "attribute vec2 position;\n"
2021 "attribute vec2 texcoord;\n"
2022 "varying vec2 v_texcoord;\n"
2025 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2026 " v_texcoord = texcoord;\n"
2029 static const char texture_fragment_shader[] =
2030 "precision mediump float;\n"
2031 "varying vec2 v_texcoord;\n"
2032 "uniform sampler2D tex;\n"
2033 "uniform float alpha;\n"
2034 "uniform float texwidth;\n"
2037 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
2038 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
2040 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
2041 " gl_FragColor = alpha * gl_FragColor;\n"
2044 static const char solid_fragment_shader[] =
2045 "precision mediump float;\n"
2046 "uniform vec4 color;\n"
2049 " gl_FragColor = color\n;"
2053 compile_shader(GLenum type, const char *source)
2059 s = glCreateShader(type);
2060 glShaderSource(s, 1, &source, NULL);
2062 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2064 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2065 fprintf(stderr, "shader info: %s\n", msg);
2073 weston_shader_init(struct weston_shader *shader,
2074 const char *vertex_source, const char *fragment_source)
2079 shader->vertex_shader =
2080 compile_shader(GL_VERTEX_SHADER, vertex_source);
2081 shader->fragment_shader =
2082 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2084 shader->program = glCreateProgram();
2085 glAttachShader(shader->program, shader->vertex_shader);
2086 glAttachShader(shader->program, shader->fragment_shader);
2087 glBindAttribLocation(shader->program, 0, "position");
2088 glBindAttribLocation(shader->program, 1, "texcoord");
2090 glLinkProgram(shader->program);
2091 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2093 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2094 fprintf(stderr, "link info: %s\n", msg);
2098 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2099 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
2100 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2101 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2102 shader->texwidth_uniform = glGetUniformLocation(shader->program,
2109 weston_output_destroy(struct weston_output *output)
2111 pixman_region32_fini(&output->region);
2112 pixman_region32_fini(&output->previous_damage);
2116 weston_output_update_zoom(struct weston_output *output, int x, int y)
2120 if (output->zoom.level <= 0)
2123 output->zoom.magnification = 1 / output->zoom.level;
2124 ratio = 1 - (1 / output->zoom.magnification);
2126 output->zoom.trans_x = (((float)(x - output->x) / output->current->width) * (ratio * 2)) - ratio;
2127 output->zoom.trans_y = (((float)(y - output->y) / output->current->height) * (ratio * 2)) - ratio;
2130 weston_output_damage(output);
2134 weston_output_update_matrix(struct weston_output *output)
2137 struct weston_matrix camera;
2138 struct weston_matrix modelview;
2140 weston_matrix_init(&output->matrix);
2141 weston_matrix_translate(&output->matrix,
2142 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2143 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
2145 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
2146 weston_matrix_scale(&output->matrix,
2147 2.0 / (output->current->width + output->border.left + output->border.right),
2148 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
2149 if (output->zoom.active) {
2150 weston_matrix_init(&camera);
2151 weston_matrix_init(&modelview);
2152 weston_matrix_translate(&camera, output->zoom.trans_x, flip * output->zoom.trans_y, 0);
2153 weston_matrix_invert(&modelview, &camera);
2154 weston_matrix_scale(&modelview, output->zoom.magnification, output->zoom.magnification, 1.0);
2155 weston_matrix_multiply(&output->matrix, &modelview);
2162 weston_output_move(struct weston_output *output, int x, int y)
2167 pixman_region32_init(&output->previous_damage);
2168 pixman_region32_init_rect(&output->region, x, y,
2169 output->current->width,
2170 output->current->height);
2174 weston_output_init(struct weston_output *output, struct weston_compositor *c,
2175 int x, int y, int width, int height, uint32_t flags)
2177 output->compositor = c;
2180 output->border.top = 0;
2181 output->border.bottom = 0;
2182 output->border.left = 0;
2183 output->border.right = 0;
2184 output->mm_width = width;
2185 output->mm_height = height;
2188 output->zoom.active = 0;
2189 output->zoom.increment = 0.05;
2190 output->zoom.level = 1.0;
2191 output->zoom.magnification = 1.0;
2192 output->zoom.trans_x = 0.0;
2193 output->zoom.trans_y = 0.0;
2195 output->flags = flags;
2196 weston_output_move(output, x, y);
2198 wl_list_init(&output->frame_callback_list);
2200 wl_display_add_global(c->wl_display,
2201 &wl_output_interface, output, bind_output);
2205 shm_buffer_created(struct wl_buffer *buffer)
2207 struct wl_list *surfaces_attached_to;
2209 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
2210 if (!surfaces_attached_to) {
2211 buffer->user_data = NULL;
2215 wl_list_init(surfaces_attached_to);
2217 buffer->user_data = surfaces_attached_to;
2221 shm_buffer_damaged(struct wl_buffer *buffer,
2222 int32_t x, int32_t y, int32_t width, int32_t height)
2224 struct wl_list *surfaces_attached_to = buffer->user_data;
2225 struct weston_surface *es;
2226 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
2228 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
2229 glBindTexture(GL_TEXTURE_2D, es->texture);
2230 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
2231 tex_width, buffer->height, 0,
2232 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
2233 wl_shm_buffer_get_data(buffer));
2234 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
2235 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
2240 shm_buffer_destroyed(struct wl_buffer *buffer)
2242 struct wl_list *surfaces_attached_to = buffer->user_data;
2243 struct weston_surface *es, *next;
2245 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
2246 wl_list_remove(&es->buffer_link);
2247 wl_list_init(&es->buffer_link);
2250 free(surfaces_attached_to);
2253 const static struct wl_shm_callbacks shm_callbacks = {
2256 shm_buffer_destroyed
2260 compositor_bind(struct wl_client *client,
2261 void *data, uint32_t version, uint32_t id)
2263 struct weston_compositor *compositor = data;
2265 wl_client_add_object(client, &wl_compositor_interface,
2266 &compositor_interface, id, compositor);
2270 weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
2272 struct wl_event_loop *loop;
2273 const char *extensions;
2275 ec->wl_display = display;
2277 if (!wl_display_add_global(display, &wl_compositor_interface,
2278 ec, compositor_bind))
2281 ec->shm = wl_shm_init(display, &shm_callbacks);
2283 ec->image_target_texture_2d =
2284 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2285 ec->image_target_renderbuffer_storage = (void *)
2286 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2287 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2288 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2290 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2291 ec->unbind_display =
2292 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2294 extensions = (const char *) glGetString(GL_EXTENSIONS);
2295 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2297 "GL_EXT_texture_format_BGRA8888 not available\n");
2302 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
2303 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2304 ec->has_bind_display = 1;
2305 if (ec->has_bind_display)
2306 ec->bind_display(ec->display, ec->wl_display);
2308 wl_list_init(&ec->surface_list);
2309 wl_list_init(&ec->input_device_list);
2310 wl_list_init(&ec->output_list);
2311 wl_list_init(&ec->binding_list);
2312 wl_list_init(&ec->animation_list);
2313 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2314 ec->fade.animation.frame = fade_frame;
2315 wl_list_init(&ec->fade.animation.link);
2317 ec->screenshooter = screenshooter_create(ec);
2319 wl_data_device_manager_init(ec->wl_display);
2321 glActiveTexture(GL_TEXTURE0);
2323 if (weston_shader_init(&ec->texture_shader,
2324 vertex_shader, texture_fragment_shader) < 0)
2326 if (weston_shader_init(&ec->solid_shader,
2327 vertex_shader, solid_fragment_shader) < 0)
2330 loop = wl_display_get_event_loop(ec->wl_display);
2331 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2332 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2334 weston_compositor_schedule_repaint(ec);
2340 weston_compositor_shutdown(struct weston_compositor *ec)
2342 struct weston_output *output, *next;
2344 wl_event_source_remove(ec->idle_source);
2346 if (ec->screenshooter)
2347 screenshooter_destroy(ec->screenshooter);
2349 /* Destroy all outputs associated with this compositor */
2350 wl_list_for_each_safe(output, next, &ec->output_list, link)
2351 output->destroy(output);
2353 weston_binding_list_destroy_all(&ec->binding_list);
2355 wl_shm_finish(ec->shm);
2357 wl_array_release(&ec->vertices);
2358 wl_array_release(&ec->indices);
2361 static int on_term_signal(int signal_number, void *data)
2363 struct wl_display *display = data;
2365 fprintf(stderr, "caught signal %d\n", signal_number);
2366 wl_display_terminate(display);
2372 on_segv_signal(int s, siginfo_t *siginfo, void *context)
2378 fprintf(stderr, "caught segv\n");
2380 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2381 for (i = 0; i < count; i++) {
2382 dladdr(buffer[i], &info);
2383 fprintf(stderr, " [%016lx] %s (%s)\n",
2385 info.dli_sname ? info.dli_sname : "--",
2389 longjmp(segv_jmp_buf, 1);
2394 load_module(const char *name, const char *entrypoint, void **handle)
2396 char path[PATH_MAX];
2397 void *module, *init;
2400 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2402 snprintf(path, sizeof path, "%s", name);
2404 module = dlopen(path, RTLD_LAZY);
2407 "failed to load module: %s\n", dlerror());
2411 init = dlsym(module, entrypoint);
2414 "failed to lookup init function: %s\n", dlerror());
2421 int main(int argc, char *argv[])
2423 struct wl_display *display;
2424 struct weston_compositor *ec;
2425 struct wl_event_source *signals[4];
2426 struct wl_event_loop *loop;
2427 struct sigaction segv_action;
2429 void *shell_module, *backend_module;
2430 int (*shell_init)(struct weston_compositor *ec);
2431 struct weston_compositor
2432 *(*backend_init)(struct wl_display *display, char *options);
2433 char *backend = NULL;
2434 char *backend_options = "";
2437 int option_idle_time = 300;
2440 static const char opts[] = "B:b:o:S:i:s:x";
2441 static const struct option longopts[ ] = {
2442 { "backend", 1, NULL, 'B' },
2443 { "backend-options", 1, NULL, 'o' },
2444 { "socket", 1, NULL, 'S' },
2445 { "idle-time", 1, NULL, 'i' },
2446 { "shell", 1, NULL, 's' },
2447 { "xserver", 0, NULL, 'x' },
2451 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2457 backend_options = optarg;
2460 option_socket_name = optarg;
2463 option_idle_time = strtol(optarg, &p, 0);
2466 "invalid idle time option: %s\n",
2480 display = wl_display_create();
2482 loop = wl_display_get_event_loop(display);
2483 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2485 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2487 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2490 wl_list_init(&child_process_list);
2491 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2494 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
2495 segv_action.sa_sigaction = on_segv_signal;
2496 sigemptyset(&segv_action.sa_mask);
2497 sigaction(SIGSEGV, &segv_action, NULL);
2500 if (getenv("WAYLAND_DISPLAY"))
2501 backend = "wayland-backend.so";
2502 else if (getenv("DISPLAY"))
2503 backend = "x11-backend.so";
2504 else if (getenv("OPENWFD"))
2505 backend = "openwfd-backend.so";
2507 backend = "drm-backend.so";
2511 shell = "desktop-shell.so";
2513 backend_init = load_module(backend, "backend_init", &backend_module);
2517 shell_init = load_module(shell, "shell_init", &shell_module);
2521 ec = backend_init(display, backend_options);
2523 fprintf(stderr, "failed to create compositor\n");
2527 ec->option_idle_time = option_idle_time;
2528 ec->idle_time = option_idle_time;
2531 weston_xserver_init(ec);
2533 if (shell_init(ec) < 0)
2536 if (wl_display_add_socket(display, option_socket_name)) {
2537 fprintf(stderr, "failed to add socket: %m\n");
2541 weston_compositor_wake(ec);
2542 if (setjmp(segv_jmp_buf) == 0)
2543 wl_display_run(display);
2545 /* prevent further rendering while shutting down */
2546 ec->state = WESTON_COMPOSITOR_SLEEPING;
2549 weston_xserver_destroy(ec);
2551 ec->shell->destroy(ec->shell);
2553 if (ec->has_bind_display)
2554 ec->unbind_display(ec->display, display);
2556 for (i = ARRAY_LENGTH(signals); i;)
2557 wl_event_source_remove(signals[--i]);
2560 wl_display_destroy(display);