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);
905 struct weston_frame_callback {
906 struct wl_resource resource;
911 weston_output_repaint(struct weston_output *output, int msecs)
913 struct weston_compositor *ec = output->compositor;
914 struct weston_surface *es;
915 struct weston_animation *animation, *next;
916 struct weston_frame_callback *cb, *cnext;
917 pixman_region32_t opaque, new_damage, total_damage,
918 overlap, surface_overlap;
919 int32_t width, height;
921 weston_compositor_update_drag_surfaces(ec);
923 width = output->current->width +
924 output->border.left + output->border.right;
925 height = output->current->height +
926 output->border.top + output->border.bottom;
927 glViewport(0, 0, width, height);
929 pixman_region32_init(&new_damage);
930 pixman_region32_init(&opaque);
931 pixman_region32_init(&overlap);
933 wl_list_for_each(es, &ec->surface_list, link) {
934 /* Update surface transform now to avoid calling it ever
935 * again from the repaint sub-functions. */
936 weston_surface_update_transform(es);
938 pixman_region32_init(&surface_overlap);
939 pixman_region32_intersect(&surface_overlap, &overlap,
940 &es->transform.boundingbox);
941 es->overlapped = pixman_region32_not_empty(&surface_overlap);
942 pixman_region32_fini(&surface_overlap);
943 pixman_region32_union(&overlap, &overlap,
944 &es->transform.boundingbox);
947 if (output->assign_planes)
949 * This will queue flips for the fbs and sprites where
950 * applicable and clear the damage for those surfaces.
951 * The repaint loop below will repaint everything
954 output->assign_planes(output);
956 wl_list_for_each(es, &ec->surface_list, link) {
957 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
958 pixman_region32_union(&new_damage, &new_damage, &es->damage);
959 pixman_region32_union(&opaque, &opaque, &es->transform.opaque);
962 pixman_region32_init(&total_damage);
963 pixman_region32_union(&total_damage, &new_damage,
964 &output->previous_damage);
965 pixman_region32_intersect(&output->previous_damage,
966 &new_damage, &output->region);
968 pixman_region32_fini(&opaque);
969 pixman_region32_fini(&overlap);
970 pixman_region32_fini(&new_damage);
972 wl_list_for_each(es, &ec->surface_list, link) {
973 pixman_region32_copy(&es->damage, &total_damage);
974 pixman_region32_subtract(&total_damage,
975 &total_damage, &es->transform.opaque);
979 weston_output_update_matrix(output);
981 output->repaint(output);
983 pixman_region32_fini(&total_damage);
985 output->repaint_needed = 0;
987 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
988 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
989 wl_resource_destroy(&cb->resource, 0);
992 wl_list_for_each_safe(animation, next, &ec->animation_list, link)
993 animation->frame(animation, output, msecs);
997 idle_repaint(void *data)
999 struct weston_output *output = data;
1001 /* An idle repaint may have been cancelled by vt switching away. */
1002 if (output->repaint_needed)
1003 weston_output_repaint(output, weston_compositor_get_time());
1005 output->repaint_scheduled = 0;
1009 weston_output_finish_frame(struct weston_output *output, int msecs)
1011 if (output->repaint_needed)
1012 weston_output_repaint(output, msecs);
1014 output->repaint_scheduled = 0;
1018 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1020 struct weston_output *output;
1021 struct wl_event_loop *loop;
1023 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
1026 loop = wl_display_get_event_loop(compositor->wl_display);
1027 wl_list_for_each(output, &compositor->output_list, link) {
1028 output->repaint_needed = 1;
1029 if (output->repaint_scheduled)
1032 wl_event_loop_add_idle(loop, idle_repaint, output);
1033 output->repaint_scheduled = 1;
1038 weston_compositor_fade(struct weston_compositor *compositor, float tint)
1040 struct weston_surface *surface;
1043 done = weston_spring_done(&compositor->fade.spring);
1044 compositor->fade.spring.target = tint;
1045 if (weston_spring_done(&compositor->fade.spring))
1049 compositor->fade.spring.timestamp =
1050 weston_compositor_get_time();
1052 if (compositor->fade.surface == NULL) {
1053 surface = weston_surface_create(compositor);
1054 weston_surface_configure(surface, 0, 0, 8192, 8192);
1055 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
1056 wl_list_insert(&compositor->surface_list, &surface->link);
1057 weston_surface_assign_output(surface);
1058 compositor->fade.surface = surface;
1061 weston_surface_damage(compositor->fade.surface);
1062 if (wl_list_empty(&compositor->fade.animation.link))
1063 wl_list_insert(compositor->animation_list.prev,
1064 &compositor->fade.animation.link);
1068 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1070 wl_resource_destroy(resource, weston_compositor_get_time());
1074 weston_surface_assign_output(struct weston_surface *es)
1076 struct weston_compositor *ec = es->compositor;
1077 struct weston_output *output, *new_output;
1078 pixman_region32_t region;
1082 weston_surface_update_transform(es);
1086 pixman_region32_init(®ion);
1087 wl_list_for_each(output, &ec->output_list, link) {
1088 pixman_region32_intersect(®ion, &es->transform.boundingbox,
1091 e = pixman_region32_extents(®ion);
1092 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1095 new_output = output;
1099 pixman_region32_fini(®ion);
1101 es->output = new_output;
1102 if (!wl_list_empty(&es->frame_callback_list)) {
1103 wl_list_insert_list(new_output->frame_callback_list.prev,
1104 &es->frame_callback_list);
1105 wl_list_init(&es->frame_callback_list);
1110 surface_attach(struct wl_client *client,
1111 struct wl_resource *resource,
1112 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1114 struct weston_surface *es = resource->data;
1115 struct weston_shell *shell = es->compositor->shell;
1116 struct wl_buffer *buffer;
1118 if (!buffer_resource && !es->output)
1122 weston_buffer_post_release(es->buffer);
1123 wl_list_remove(&es->buffer_destroy_listener.link);
1126 if (!buffer_resource && es->output) {
1127 weston_surface_unmap(es);
1132 buffer = buffer_resource->data;
1133 buffer->busy_count++;
1134 es->buffer = buffer;
1135 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1136 &es->buffer_destroy_listener.link);
1138 if (es->geometry.width != buffer->width ||
1139 es->geometry.height != buffer->height) {
1140 undef_region(&es->input);
1141 pixman_region32_fini(&es->opaque);
1142 pixman_region32_init(&es->opaque);
1145 if (es->output == NULL) {
1146 shell->map(shell, es, buffer->width, buffer->height, sx, sy);
1147 } else if (sx != 0 || sy != 0 ||
1148 es->geometry.width != buffer->width ||
1149 es->geometry.height != buffer->height) {
1150 GLfloat from_x, from_y;
1153 surface_to_global_float(es, 0, 0, &from_x, &from_y);
1154 surface_to_global_float(es, sx, sy, &to_x, &to_y);
1155 shell->configure(shell, es,
1156 es->geometry.x + to_x - from_x,
1157 es->geometry.y + to_y - from_y,
1158 buffer->width, buffer->height);
1161 weston_buffer_attach(buffer, &es->surface);
1165 surface_damage(struct wl_client *client,
1166 struct wl_resource *resource,
1167 int32_t x, int32_t y, int32_t width, int32_t height)
1169 struct weston_surface *es = resource->data;
1171 weston_surface_damage_rectangle(es, x, y, width, height);
1175 destroy_frame_callback(struct wl_resource *resource)
1177 struct weston_frame_callback *cb = resource->data;
1179 wl_list_remove(&cb->link);
1184 surface_frame(struct wl_client *client,
1185 struct wl_resource *resource, uint32_t callback)
1187 struct weston_frame_callback *cb;
1188 struct weston_surface *es = resource->data;
1190 cb = malloc(sizeof *cb);
1192 wl_resource_post_no_memory(resource);
1196 cb->resource.object.interface = &wl_callback_interface;
1197 cb->resource.object.id = callback;
1198 cb->resource.destroy = destroy_frame_callback;
1199 cb->resource.client = client;
1200 cb->resource.data = cb;
1202 wl_client_add_resource(client, &cb->resource);
1205 wl_list_insert(es->output->frame_callback_list.prev,
1208 wl_list_insert(es->frame_callback_list.prev, &cb->link);
1213 surface_set_opaque_region(struct wl_client *client,
1214 struct wl_resource *resource,
1215 struct wl_resource *region_resource)
1217 struct weston_surface *surface = resource->data;
1218 struct weston_region *region;
1220 pixman_region32_fini(&surface->opaque);
1222 if (region_resource) {
1223 region = region_resource->data;
1224 pixman_region32_init_rect(&surface->opaque, 0, 0,
1225 surface->geometry.width,
1226 surface->geometry.height);
1227 pixman_region32_intersect(&surface->opaque,
1228 &surface->opaque, ®ion->region);
1230 pixman_region32_init(&surface->opaque);
1233 surface->geometry.dirty = 1;
1237 surface_set_input_region(struct wl_client *client,
1238 struct wl_resource *resource,
1239 struct wl_resource *region_resource)
1241 struct weston_surface *surface = resource->data;
1242 struct weston_region *region;
1244 if (region_resource) {
1245 region = region_resource->data;
1246 pixman_region32_init_rect(&surface->input, 0, 0,
1247 surface->geometry.width,
1248 surface->geometry.height);
1249 pixman_region32_intersect(&surface->input,
1250 &surface->input, ®ion->region);
1252 pixman_region32_init_rect(&surface->input, 0, 0,
1253 surface->geometry.width,
1254 surface->geometry.height);
1257 weston_compositor_repick(surface->compositor);
1260 const static struct wl_surface_interface surface_interface = {
1265 surface_set_opaque_region,
1266 surface_set_input_region
1270 compositor_create_surface(struct wl_client *client,
1271 struct wl_resource *resource, uint32_t id)
1273 struct weston_compositor *ec = resource->data;
1274 struct weston_surface *surface;
1276 surface = weston_surface_create(ec);
1277 if (surface == NULL) {
1278 wl_resource_post_no_memory(resource);
1282 surface->surface.resource.destroy = destroy_surface;
1284 surface->surface.resource.object.id = id;
1285 surface->surface.resource.object.interface = &wl_surface_interface;
1286 surface->surface.resource.object.implementation =
1287 (void (**)(void)) &surface_interface;
1288 surface->surface.resource.data = surface;
1290 wl_client_add_resource(client, &surface->surface.resource);
1294 destroy_region(struct wl_resource *resource)
1296 struct weston_region *region =
1297 container_of(resource, struct weston_region, resource);
1299 pixman_region32_fini(®ion->region);
1304 region_destroy(struct wl_client *client, struct wl_resource *resource)
1306 wl_resource_destroy(resource, weston_compositor_get_time());
1310 region_add(struct wl_client *client, struct wl_resource *resource,
1311 int32_t x, int32_t y, int32_t width, int32_t height)
1313 struct weston_region *region = resource->data;
1315 pixman_region32_union_rect(®ion->region, ®ion->region,
1316 x, y, width, height);
1320 region_subtract(struct wl_client *client, struct wl_resource *resource,
1321 int32_t x, int32_t y, int32_t width, int32_t height)
1323 struct weston_region *region = resource->data;
1324 pixman_region32_t rect;
1326 pixman_region32_init_rect(&rect, x, y, width, height);
1327 pixman_region32_subtract(®ion->region, ®ion->region, &rect);
1328 pixman_region32_fini(&rect);
1331 static const struct wl_region_interface region_interface = {
1338 compositor_create_region(struct wl_client *client,
1339 struct wl_resource *resource, uint32_t id)
1341 struct weston_region *region;
1343 region = malloc(sizeof *region);
1344 if (region == NULL) {
1345 wl_resource_post_no_memory(resource);
1349 region->resource.destroy = destroy_region;
1351 region->resource.object.id = id;
1352 region->resource.object.interface = &wl_region_interface;
1353 region->resource.object.implementation =
1354 (void (**)(void)) ®ion_interface;
1355 region->resource.data = region;
1357 pixman_region32_init(®ion->region);
1359 wl_client_add_resource(client, ®ion->resource);
1362 const static struct wl_compositor_interface compositor_interface = {
1363 compositor_create_surface,
1364 compositor_create_region
1368 weston_compositor_wake(struct weston_compositor *compositor)
1370 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1371 weston_compositor_fade(compositor, 0.0);
1373 wl_event_source_timer_update(compositor->idle_source,
1374 compositor->idle_time * 1000);
1378 weston_compositor_activity(struct weston_compositor *compositor)
1380 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1381 weston_compositor_wake(compositor);
1383 compositor->shell->unlock(compositor->shell);
1388 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
1390 weston_compositor_activity(compositor);
1391 compositor->idle_inhibit++;
1395 weston_compositor_idle_release(struct weston_compositor *compositor)
1397 compositor->idle_inhibit--;
1398 weston_compositor_activity(compositor);
1402 idle_handler(void *data)
1404 struct weston_compositor *compositor = data;
1406 if (compositor->idle_inhibit)
1409 weston_compositor_fade(compositor, 1.0);
1415 weston_input_update_drag_surface(struct wl_input_device *input_device,
1419 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1421 struct weston_output *output;
1422 const struct wl_pointer_grab_interface *interface;
1423 struct weston_input_device *wd = (struct weston_input_device *) device;
1424 struct weston_compositor *ec = wd->compositor;
1425 int x_valid = 0, y_valid = 0;
1426 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1428 weston_compositor_activity(ec);
1430 wl_list_for_each(output, &ec->output_list, link) {
1431 if (output->x <= x && x < output->x + output->current->width)
1434 if (output->y <= y && y < output->y + output->current->height)
1437 /* FIXME: calculate this only on output addition/deletion */
1438 if (output->x < min_x)
1440 if (output->y < min_y)
1443 if (output->x + output->current->width > max_x)
1444 max_x = output->x + output->current->width - 1;
1445 if (output->y + output->current->height > max_y)
1446 max_y = output->y + output->current->height - 1;
1452 else if (x >= max_x)
1458 else if (y >= max_y)
1462 weston_input_update_drag_surface(device,
1463 x - device->x, y - device->y);
1468 wl_list_for_each(output, &ec->output_list, link)
1469 if (output->zoom.active &&
1470 pixman_region32_contains_point(&output->region, x, y, NULL))
1471 weston_output_update_zoom(output, x, y);
1473 weston_device_repick(device, time);
1474 interface = device->pointer_grab->interface;
1475 interface->motion(device->pointer_grab, time,
1476 device->pointer_grab->x, device->pointer_grab->y);
1479 weston_surface_set_position(wd->sprite,
1480 device->x - wd->hotspot_x,
1481 device->y - wd->hotspot_y);
1483 weston_compositor_schedule_repaint(ec);
1488 weston_surface_activate(struct weston_surface *surface,
1489 struct weston_input_device *device, uint32_t time)
1491 weston_surface_raise(surface);
1492 wl_input_device_set_keyboard_focus(&device->input_device,
1493 &surface->surface, time);
1494 wl_data_device_set_keyboard_focus(&device->input_device);
1498 notify_button(struct wl_input_device *device,
1499 uint32_t time, int32_t button, int32_t state)
1501 struct weston_input_device *wd = (struct weston_input_device *) device;
1502 struct weston_compositor *compositor = wd->compositor;
1505 weston_compositor_idle_inhibit(compositor);
1506 if (device->button_count == 0) {
1507 device->grab_button = button;
1508 device->grab_time = time;
1509 device->grab_x = device->x;
1510 device->grab_y = device->y;
1512 device->button_count++;
1514 weston_compositor_idle_release(compositor);
1515 device->button_count--;
1518 weston_compositor_run_binding(compositor, wd, time, 0, button, state);
1520 device->pointer_grab->interface->button(device->pointer_grab, time, button, state);
1525 update_modifier_state(struct weston_input_device *device,
1526 uint32_t key, uint32_t state)
1533 modifier = MODIFIER_CTRL;
1538 modifier = MODIFIER_ALT;
1543 modifier = MODIFIER_SUPER;
1552 device->modifier_state |= modifier;
1554 device->modifier_state &= ~modifier;
1558 notify_key(struct wl_input_device *device,
1559 uint32_t time, uint32_t key, uint32_t state)
1561 struct weston_input_device *wd = (struct weston_input_device *) device;
1562 struct weston_compositor *compositor = wd->compositor;
1566 weston_compositor_idle_inhibit(compositor);
1567 device->grab_key = key;
1568 device->grab_time = time;
1570 weston_compositor_idle_release(compositor);
1573 if (device->keyboard_grab == &device->default_keyboard_grab)
1574 weston_compositor_run_binding(compositor, wd,
1575 time, key, 0, state);
1577 update_modifier_state(wd, key, state);
1578 end = device->keys.data + device->keys.size;
1579 for (k = device->keys.data; k < end; k++) {
1583 device->keys.size = (void *) end - device->keys.data;
1585 k = wl_array_add(&device->keys, sizeof *k);
1589 device->keyboard_grab->interface->key(device->keyboard_grab,
1594 notify_pointer_focus(struct wl_input_device *device,
1595 uint32_t time, struct weston_output *output,
1596 int32_t x, int32_t y)
1598 struct weston_input_device *wd = (struct weston_input_device *) device;
1599 struct weston_compositor *compositor = wd->compositor;
1602 weston_input_update_drag_surface(device, x - device->x,
1607 compositor->focus = 1;
1608 weston_compositor_repick(compositor);
1610 compositor->focus = 0;
1611 weston_compositor_repick(compositor);
1616 notify_keyboard_focus(struct wl_input_device *device,
1617 uint32_t time, struct weston_output *output,
1618 struct wl_array *keys)
1620 struct weston_input_device *wd =
1621 (struct weston_input_device *) device;
1622 struct weston_compositor *compositor = wd->compositor;
1623 struct weston_surface *es;
1626 if (!wl_list_empty(&compositor->surface_list))
1627 es = container_of(compositor->surface_list.next,
1628 struct weston_surface, link);
1633 wl_array_copy(&wd->input_device.keys, keys);
1634 wd->modifier_state = 0;
1635 end = device->keys.data + device->keys.size;
1636 for (k = device->keys.data; k < end; k++) {
1637 weston_compositor_idle_inhibit(compositor);
1638 update_modifier_state(wd, *k, 1);
1641 if (es && es->surface.resource.client)
1642 wl_input_device_set_keyboard_focus(&wd->input_device,
1643 &es->surface, time);
1645 end = device->keys.data + device->keys.size;
1646 for (k = device->keys.data; k < end; k++)
1647 weston_compositor_idle_release(compositor);
1649 wd->modifier_state = 0;
1650 wl_input_device_set_keyboard_focus(&wd->input_device,
1655 /* TODO: share this function with wayland-server.c */
1656 static struct wl_resource *
1657 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
1659 struct wl_resource *r;
1664 wl_list_for_each(r, list, link) {
1665 if (r->client == surface->resource.client)
1673 lose_touch_focus_resource(struct wl_listener *listener,
1674 struct wl_resource *resource, uint32_t time)
1676 struct weston_input_device *device =
1677 container_of(listener, struct weston_input_device,
1678 touch_focus_resource_listener);
1680 device->touch_focus_resource = NULL;
1684 lose_touch_focus(struct wl_listener *listener,
1685 struct wl_resource *resource, uint32_t time)
1687 struct weston_input_device *device =
1688 container_of(listener, struct weston_input_device,
1689 touch_focus_listener);
1691 device->touch_focus = NULL;
1695 touch_set_focus(struct weston_input_device *device,
1696 struct wl_surface *surface, uint32_t time)
1698 struct wl_input_device *input_device = &device->input_device;
1699 struct wl_resource *resource;
1701 if (device->touch_focus == surface)
1706 find_resource_for_surface(&input_device->resource_list,
1709 fprintf(stderr, "couldn't find resource\n");
1713 device->touch_focus_resource_listener.func =
1714 lose_touch_focus_resource;
1715 wl_list_insert(resource->destroy_listener_list.prev,
1716 &device->touch_focus_resource_listener.link);
1717 device->touch_focus_listener.func = lose_touch_focus;
1718 wl_list_insert(surface->resource.destroy_listener_list.prev,
1719 &device->touch_focus_listener.link);
1721 device->touch_focus = surface;
1722 device->touch_focus_resource = resource;
1724 if (device->touch_focus)
1725 wl_list_remove(&device->touch_focus_listener.link);
1726 if (device->touch_focus_resource)
1727 wl_list_remove(&device->touch_focus_resource_listener.link);
1728 device->touch_focus = NULL;
1729 device->touch_focus_resource = NULL;
1734 * notify_touch - emulates button touches and notifies surfaces accordingly.
1736 * It assumes always the correct cycle sequence until it gets here: touch_down
1737 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1738 * for sending along such order.
1742 notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
1743 int x, int y, int touch_type)
1745 struct weston_input_device *wd = (struct weston_input_device *) device;
1746 struct weston_compositor *ec = wd->compositor;
1747 struct weston_surface *es;
1750 switch (touch_type) {
1751 case WL_INPUT_DEVICE_TOUCH_DOWN:
1752 weston_compositor_idle_inhibit(ec);
1756 /* the first finger down picks the surface, and all further go
1757 * to that surface for the remainder of the touch session i.e.
1758 * until all touch points are up again. */
1759 if (wd->num_tp == 1) {
1760 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1761 touch_set_focus(wd, &es->surface, time);
1762 } else if (wd->touch_focus) {
1763 es = (struct weston_surface *) wd->touch_focus;
1764 weston_surface_from_global(es, x, y, &sx, &sy);
1767 if (wd->touch_focus_resource && wd->touch_focus)
1768 wl_resource_post_event(wd->touch_focus_resource,
1773 case WL_INPUT_DEVICE_TOUCH_MOTION:
1774 es = (struct weston_surface *) wd->touch_focus;
1778 weston_surface_from_global(es, x, y, &sx, &sy);
1779 if (wd->touch_focus_resource)
1780 wl_resource_post_event(wd->touch_focus_resource,
1784 case WL_INPUT_DEVICE_TOUCH_UP:
1785 weston_compositor_idle_release(ec);
1788 if (wd->touch_focus_resource)
1789 wl_resource_post_event(wd->touch_focus_resource,
1790 touch_type, time, touch_id);
1791 if (wd->num_tp == 0)
1792 touch_set_focus(wd, NULL, time);
1798 input_device_attach(struct wl_client *client,
1799 struct wl_resource *resource,
1801 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1803 struct weston_input_device *device = resource->data;
1804 struct weston_compositor *compositor = device->compositor;
1805 struct wl_buffer *buffer;
1807 if (time < device->input_device.pointer_focus_time)
1809 if (device->input_device.pointer_focus == NULL)
1811 if (device->input_device.pointer_focus->resource.client != client)
1814 if (!buffer_resource && device->sprite->output) {
1815 wl_list_remove(&device->sprite->link);
1816 device->sprite->output = NULL;
1820 if (!device->sprite->output) {
1821 wl_list_insert(&compositor->surface_list,
1822 &device->sprite->link);
1823 weston_surface_assign_output(device->sprite);
1826 buffer = buffer_resource->data;
1827 device->hotspot_x = x;
1828 device->hotspot_y = y;
1829 weston_surface_configure(device->sprite,
1830 device->input_device.x - device->hotspot_x,
1831 device->input_device.y - device->hotspot_y,
1832 buffer->width, buffer->height);
1834 weston_buffer_attach(buffer, &device->sprite->surface);
1837 const static struct wl_input_device_interface input_device_interface = {
1838 input_device_attach,
1841 static void unbind_input_device(struct wl_resource *resource)
1843 wl_list_remove(&resource->link);
1848 bind_input_device(struct wl_client *client,
1849 void *data, uint32_t version, uint32_t id)
1851 struct wl_input_device *device = data;
1852 struct wl_resource *resource;
1854 resource = wl_client_add_object(client, &wl_input_device_interface,
1855 &input_device_interface, id, data);
1856 wl_list_insert(&device->resource_list, &resource->link);
1857 resource->destroy = unbind_input_device;
1861 weston_input_device_init(struct weston_input_device *device,
1862 struct weston_compositor *ec)
1864 wl_input_device_init(&device->input_device);
1866 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1867 device, bind_input_device);
1869 device->sprite = weston_surface_create(ec);
1871 device->compositor = ec;
1872 device->hotspot_x = 16;
1873 device->hotspot_y = 16;
1874 device->modifier_state = 0;
1877 wl_list_insert(ec->input_device_list.prev, &device->link);
1881 weston_input_device_release(struct weston_input_device *device)
1883 wl_list_remove(&device->link);
1884 /* The global object is destroyed at wl_display_destroy() time. */
1887 destroy_surface(&device->sprite->surface.resource);
1889 wl_input_device_release(&device->input_device);
1893 weston_input_update_drag_surface(struct wl_input_device *input_device,
1896 int surface_changed = 0;
1897 struct weston_input_device *device = (struct weston_input_device *)
1900 if (!device->drag_surface && !input_device->drag_surface)
1903 if (device->drag_surface && input_device->drag_surface &&
1904 (&device->drag_surface->surface.resource !=
1905 &input_device->drag_surface->resource))
1906 /* between calls to this funcion we got a new drag_surface */
1907 surface_changed = 1;
1909 if (!input_device->drag_surface || surface_changed) {
1910 device->drag_surface = NULL;
1911 if (!surface_changed)
1915 if (!device->drag_surface || surface_changed) {
1916 device->drag_surface = (struct weston_surface *)
1917 input_device->drag_surface;
1919 weston_surface_set_position(device->drag_surface,
1920 input_device->x, input_device->y);
1923 if (device->drag_surface->output == NULL &&
1924 device->drag_surface->buffer) {
1925 wl_list_insert(weston_compositor_top(device->compositor),
1926 &device->drag_surface->link);
1927 weston_surface_assign_output(device->drag_surface);
1933 weston_surface_set_position(device->drag_surface,
1934 device->drag_surface->geometry.x + dx,
1935 device->drag_surface->geometry.y + dy);
1939 weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
1941 weston_input_update_drag_surface(compositor->input_device, 0, 0);
1945 bind_output(struct wl_client *client,
1946 void *data, uint32_t version, uint32_t id)
1948 struct weston_output *output = data;
1949 struct weston_mode *mode;
1950 struct wl_resource *resource;
1952 resource = wl_client_add_object(client,
1953 &wl_output_interface, NULL, id, data);
1955 wl_resource_post_event(resource,
1962 output->make, output->model);
1964 wl_list_for_each (mode, &output->mode_list, link) {
1965 wl_resource_post_event(resource,
1974 static const char vertex_shader[] =
1975 "uniform mat4 proj;\n"
1976 "attribute vec2 position;\n"
1977 "attribute vec2 texcoord;\n"
1978 "varying vec2 v_texcoord;\n"
1981 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1982 " v_texcoord = texcoord;\n"
1985 static const char texture_fragment_shader[] =
1986 "precision mediump float;\n"
1987 "varying vec2 v_texcoord;\n"
1988 "uniform sampler2D tex;\n"
1989 "uniform float alpha;\n"
1990 "uniform float texwidth;\n"
1993 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
1994 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
1996 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1997 " gl_FragColor = alpha * gl_FragColor;\n"
2000 static const char solid_fragment_shader[] =
2001 "precision mediump float;\n"
2002 "uniform vec4 color;\n"
2005 " gl_FragColor = color\n;"
2009 compile_shader(GLenum type, const char *source)
2015 s = glCreateShader(type);
2016 glShaderSource(s, 1, &source, NULL);
2018 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2020 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2021 fprintf(stderr, "shader info: %s\n", msg);
2029 weston_shader_init(struct weston_shader *shader,
2030 const char *vertex_source, const char *fragment_source)
2035 shader->vertex_shader =
2036 compile_shader(GL_VERTEX_SHADER, vertex_source);
2037 shader->fragment_shader =
2038 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2040 shader->program = glCreateProgram();
2041 glAttachShader(shader->program, shader->vertex_shader);
2042 glAttachShader(shader->program, shader->fragment_shader);
2043 glBindAttribLocation(shader->program, 0, "position");
2044 glBindAttribLocation(shader->program, 1, "texcoord");
2046 glLinkProgram(shader->program);
2047 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2049 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2050 fprintf(stderr, "link info: %s\n", msg);
2054 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2055 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
2056 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2057 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2058 shader->texwidth_uniform = glGetUniformLocation(shader->program,
2065 weston_output_destroy(struct weston_output *output)
2067 pixman_region32_fini(&output->region);
2068 pixman_region32_fini(&output->previous_damage);
2072 weston_output_update_zoom(struct weston_output *output, int x, int y)
2076 if (output->zoom.level <= 0)
2079 output->zoom.magnification = 1 / output->zoom.level;
2080 ratio = 1 - (1 / output->zoom.magnification);
2082 output->zoom.trans_x = (((float)(x - output->x) / output->current->width) * (ratio * 2)) - ratio;
2083 output->zoom.trans_y = (((float)(y - output->y) / output->current->height) * (ratio * 2)) - ratio;
2086 weston_output_damage(output);
2090 weston_output_update_matrix(struct weston_output *output)
2093 struct weston_matrix camera;
2094 struct weston_matrix modelview;
2096 weston_matrix_init(&output->matrix);
2097 weston_matrix_translate(&output->matrix,
2098 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2099 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
2101 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
2102 weston_matrix_scale(&output->matrix,
2103 2.0 / (output->current->width + output->border.left + output->border.right),
2104 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
2105 if (output->zoom.active) {
2106 weston_matrix_init(&camera);
2107 weston_matrix_init(&modelview);
2108 weston_matrix_translate(&camera, output->zoom.trans_x, flip * output->zoom.trans_y, 0);
2109 weston_matrix_invert(&modelview, &camera);
2110 weston_matrix_scale(&modelview, output->zoom.magnification, output->zoom.magnification, 1.0);
2111 weston_matrix_multiply(&output->matrix, &modelview);
2118 weston_output_move(struct weston_output *output, int x, int y)
2123 pixman_region32_init(&output->previous_damage);
2124 pixman_region32_init_rect(&output->region, x, y,
2125 output->current->width,
2126 output->current->height);
2130 weston_output_init(struct weston_output *output, struct weston_compositor *c,
2131 int x, int y, int width, int height, uint32_t flags)
2133 output->compositor = c;
2136 output->border.top = 0;
2137 output->border.bottom = 0;
2138 output->border.left = 0;
2139 output->border.right = 0;
2140 output->mm_width = width;
2141 output->mm_height = height;
2144 output->zoom.active = 0;
2145 output->zoom.increment = 0.05;
2146 output->zoom.level = 1.0;
2147 output->zoom.magnification = 1.0;
2148 output->zoom.trans_x = 0.0;
2149 output->zoom.trans_y = 0.0;
2151 output->flags = flags;
2152 weston_output_move(output, x, y);
2154 wl_list_init(&output->frame_callback_list);
2156 wl_display_add_global(c->wl_display,
2157 &wl_output_interface, output, bind_output);
2161 shm_buffer_created(struct wl_buffer *buffer)
2163 struct wl_list *surfaces_attached_to;
2165 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
2166 if (!surfaces_attached_to) {
2167 buffer->user_data = NULL;
2171 wl_list_init(surfaces_attached_to);
2173 buffer->user_data = surfaces_attached_to;
2177 shm_buffer_damaged(struct wl_buffer *buffer,
2178 int32_t x, int32_t y, int32_t width, int32_t height)
2180 struct wl_list *surfaces_attached_to = buffer->user_data;
2181 struct weston_surface *es;
2182 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
2184 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
2185 glBindTexture(GL_TEXTURE_2D, es->texture);
2186 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
2187 tex_width, buffer->height, 0,
2188 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
2189 wl_shm_buffer_get_data(buffer));
2190 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
2191 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
2196 shm_buffer_destroyed(struct wl_buffer *buffer)
2198 struct wl_list *surfaces_attached_to = buffer->user_data;
2199 struct weston_surface *es, *next;
2201 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
2202 wl_list_remove(&es->buffer_link);
2203 wl_list_init(&es->buffer_link);
2206 free(surfaces_attached_to);
2209 const static struct wl_shm_callbacks shm_callbacks = {
2212 shm_buffer_destroyed
2216 compositor_bind(struct wl_client *client,
2217 void *data, uint32_t version, uint32_t id)
2219 struct weston_compositor *compositor = data;
2221 wl_client_add_object(client, &wl_compositor_interface,
2222 &compositor_interface, id, compositor);
2226 weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
2228 struct wl_event_loop *loop;
2229 const char *extensions;
2231 ec->wl_display = display;
2233 if (!wl_display_add_global(display, &wl_compositor_interface,
2234 ec, compositor_bind))
2237 ec->shm = wl_shm_init(display, &shm_callbacks);
2239 ec->image_target_texture_2d =
2240 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2241 ec->image_target_renderbuffer_storage = (void *)
2242 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2243 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2244 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2246 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2247 ec->unbind_display =
2248 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2250 extensions = (const char *) glGetString(GL_EXTENSIONS);
2251 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2253 "GL_EXT_texture_format_BGRA8888 not available\n");
2258 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
2259 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2260 ec->has_bind_display = 1;
2261 if (ec->has_bind_display)
2262 ec->bind_display(ec->display, ec->wl_display);
2264 wl_list_init(&ec->surface_list);
2265 wl_list_init(&ec->input_device_list);
2266 wl_list_init(&ec->output_list);
2267 wl_list_init(&ec->binding_list);
2268 wl_list_init(&ec->animation_list);
2269 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2270 ec->fade.animation.frame = fade_frame;
2271 wl_list_init(&ec->fade.animation.link);
2273 ec->screenshooter = screenshooter_create(ec);
2275 wl_data_device_manager_init(ec->wl_display);
2277 glActiveTexture(GL_TEXTURE0);
2279 if (weston_shader_init(&ec->texture_shader,
2280 vertex_shader, texture_fragment_shader) < 0)
2282 if (weston_shader_init(&ec->solid_shader,
2283 vertex_shader, solid_fragment_shader) < 0)
2286 loop = wl_display_get_event_loop(ec->wl_display);
2287 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2288 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2290 weston_compositor_schedule_repaint(ec);
2296 weston_compositor_shutdown(struct weston_compositor *ec)
2298 struct weston_output *output, *next;
2300 wl_event_source_remove(ec->idle_source);
2302 if (ec->screenshooter)
2303 screenshooter_destroy(ec->screenshooter);
2305 /* Destroy all outputs associated with this compositor */
2306 wl_list_for_each_safe(output, next, &ec->output_list, link)
2307 output->destroy(output);
2309 weston_binding_list_destroy_all(&ec->binding_list);
2311 wl_shm_finish(ec->shm);
2313 wl_array_release(&ec->vertices);
2314 wl_array_release(&ec->indices);
2317 static int on_term_signal(int signal_number, void *data)
2319 struct wl_display *display = data;
2321 fprintf(stderr, "caught signal %d\n", signal_number);
2322 wl_display_terminate(display);
2328 on_segv_signal(int s, siginfo_t *siginfo, void *context)
2334 fprintf(stderr, "caught segv\n");
2336 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2337 for (i = 0; i < count; i++) {
2338 dladdr(buffer[i], &info);
2339 fprintf(stderr, " [%016lx] %s (%s)\n",
2341 info.dli_sname ? info.dli_sname : "--",
2345 longjmp(segv_jmp_buf, 1);
2350 load_module(const char *name, const char *entrypoint, void **handle)
2352 char path[PATH_MAX];
2353 void *module, *init;
2356 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2358 snprintf(path, sizeof path, "%s", name);
2360 module = dlopen(path, RTLD_LAZY);
2363 "failed to load module: %s\n", dlerror());
2367 init = dlsym(module, entrypoint);
2370 "failed to lookup init function: %s\n", dlerror());
2377 int main(int argc, char *argv[])
2379 struct wl_display *display;
2380 struct weston_compositor *ec;
2381 struct wl_event_source *signals[4];
2382 struct wl_event_loop *loop;
2383 struct sigaction segv_action;
2385 void *shell_module, *backend_module;
2386 int (*shell_init)(struct weston_compositor *ec);
2387 struct weston_compositor
2388 *(*backend_init)(struct wl_display *display, char *options);
2389 char *backend = NULL;
2390 char *backend_options = "";
2393 int option_idle_time = 300;
2396 static const char opts[] = "B:b:o:S:i:s:x";
2397 static const struct option longopts[ ] = {
2398 { "backend", 1, NULL, 'B' },
2399 { "backend-options", 1, NULL, 'o' },
2400 { "socket", 1, NULL, 'S' },
2401 { "idle-time", 1, NULL, 'i' },
2402 { "shell", 1, NULL, 's' },
2403 { "xserver", 0, NULL, 'x' },
2407 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2413 backend_options = optarg;
2416 option_socket_name = optarg;
2419 option_idle_time = strtol(optarg, &p, 0);
2422 "invalid idle time option: %s\n",
2436 display = wl_display_create();
2438 loop = wl_display_get_event_loop(display);
2439 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2441 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
2443 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
2446 wl_list_init(&child_process_list);
2447 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2450 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
2451 segv_action.sa_sigaction = on_segv_signal;
2452 sigemptyset(&segv_action.sa_mask);
2453 sigaction(SIGSEGV, &segv_action, NULL);
2456 if (getenv("WAYLAND_DISPLAY"))
2457 backend = "wayland-backend.so";
2458 else if (getenv("DISPLAY"))
2459 backend = "x11-backend.so";
2460 else if (getenv("OPENWFD"))
2461 backend = "openwfd-backend.so";
2463 backend = "drm-backend.so";
2467 shell = "desktop-shell.so";
2469 backend_init = load_module(backend, "backend_init", &backend_module);
2473 shell_init = load_module(shell, "shell_init", &shell_module);
2477 ec = backend_init(display, backend_options);
2479 fprintf(stderr, "failed to create compositor\n");
2483 ec->option_idle_time = option_idle_time;
2484 ec->idle_time = option_idle_time;
2487 weston_xserver_init(ec);
2489 if (shell_init(ec) < 0)
2492 if (wl_display_add_socket(display, option_socket_name)) {
2493 fprintf(stderr, "failed to add socket: %m\n");
2497 weston_compositor_wake(ec);
2498 if (setjmp(segv_jmp_buf) == 0)
2499 wl_display_run(display);
2501 /* prevent further rendering while shutting down */
2502 ec->state = WESTON_COMPOSITOR_SLEEPING;
2505 weston_xserver_destroy(ec);
2507 ec->shell->destroy(ec->shell);
2509 if (ec->has_bind_display)
2510 ec->unbind_display(ec->display, display);
2512 for (i = ARRAY_LENGTH(signals); i;)
2513 wl_event_source_remove(signals[--i]);
2516 wl_display_destroy(display);