2 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
42 #include <linux/input.h>
47 #include <wayland-server.h>
48 #include "compositor.h"
50 static const char *option_socket_name = NULL;
52 static struct wl_list child_process_list;
55 sigchld_handler(int signal_number, void *data)
57 struct weston_process *p;
62 wl_list_for_each(p, &child_process_list, link) {
67 if (&p->link == &child_process_list) {
68 fprintf(stderr, "unknown child process exited\n");
72 wl_list_remove(&p->link);
73 p->cleanup(p, status);
79 weston_watch_process(struct weston_process *process)
81 wl_list_insert(&child_process_list, &process->link);
85 child_client_exec(int sockfd, const char *path)
91 /* do not give our signal mask to the new process */
93 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
95 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
96 * non-CLOEXEC fd to pass through exec. */
97 clientfd = dup(sockfd);
99 fprintf(stderr, "compositor: dup failed: %m\n");
103 snprintf(s, sizeof s, "%d", clientfd);
104 setenv("WAYLAND_SOCKET", s, 1);
106 if (execl(path, path, NULL) < 0)
107 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
111 WL_EXPORT struct wl_client *
112 weston_client_launch(struct weston_compositor *compositor,
113 struct weston_process *proc,
115 weston_process_cleanup_func_t cleanup)
119 struct wl_client *client;
121 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
122 fprintf(stderr, "weston_client_launch: "
123 "socketpair failed while launching '%s': %m\n",
132 fprintf(stderr, "weston_client_launch: "
133 "fork failed while launching '%s': %m\n", path);
138 child_client_exec(sv[1], path);
144 client = wl_client_create(compositor->wl_display, sv[0]);
147 fprintf(stderr, "weston_client_launch: "
148 "wl_client_create failed while launching '%s'.\n",
154 proc->cleanup = cleanup;
155 weston_watch_process(proc);
161 surface_handle_buffer_destroy(struct wl_listener *listener,
162 struct wl_resource *resource, uint32_t time)
164 struct weston_surface *es =
165 container_of(listener, struct weston_surface,
166 buffer_destroy_listener);
172 output_handle_scanout_buffer_destroy(struct wl_listener *listener,
173 struct wl_resource *resource,
176 struct weston_output *output =
177 container_of(listener, struct weston_output,
178 scanout_buffer_destroy_listener);
180 output->scanout_buffer = NULL;
182 if (!output->pending_scanout_buffer)
183 weston_compositor_schedule_repaint(output->compositor);
187 output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
188 struct wl_resource *resource,
191 struct weston_output *output =
192 container_of(listener, struct weston_output,
193 pending_scanout_buffer_destroy_listener);
195 output->pending_scanout_buffer = NULL;
197 weston_compositor_schedule_repaint(output->compositor);
201 WL_EXPORT struct weston_surface *
202 weston_surface_create(struct weston_compositor *compositor,
203 int32_t x, int32_t y, int32_t width, int32_t height)
205 struct weston_surface *surface;
207 surface = calloc(1, sizeof *surface);
211 wl_list_init(&surface->link);
212 wl_list_init(&surface->buffer_link);
214 glGenTextures(1, &surface->texture);
215 glBindTexture(GL_TEXTURE_2D, surface->texture);
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
219 surface->surface.resource.client = NULL;
221 surface->compositor = compositor;
222 surface->visual = WESTON_NONE_VISUAL;
223 surface->image = EGL_NO_IMAGE_KHR;
224 surface->saved_texture = 0;
227 surface->width = width;
228 surface->height = height;
229 surface->alpha = 255;
231 surface->fullscreen_output = NULL;
232 surface->buffer = NULL;
233 surface->output = NULL;
235 pixman_region32_init(&surface->damage);
236 pixman_region32_init(&surface->opaque);
237 wl_list_init(&surface->frame_callback_list);
239 surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
241 surface->transform = NULL;
247 weston_surface_damage_rectangle(struct weston_surface *surface,
248 int32_t x, int32_t y,
249 int32_t width, int32_t height)
251 struct weston_compositor *compositor = surface->compositor;
253 pixman_region32_union_rect(&surface->damage,
255 surface->x + x, surface->y + y,
257 weston_compositor_schedule_repaint(compositor);
261 weston_surface_damage(struct weston_surface *surface)
263 weston_surface_damage_rectangle(surface, 0, 0,
264 surface->width, surface->height);
268 weston_surface_damage_below(struct weston_surface *surface)
270 struct weston_surface *below;
272 if (surface->output == NULL)
275 if (surface->link.next == &surface->compositor->surface_list)
278 below = container_of(surface->link.next, struct weston_surface, link);
280 pixman_region32_union_rect(&below->damage,
282 surface->x, surface->y,
283 surface->width, surface->height);
284 weston_compositor_schedule_repaint(surface->compositor);
288 weston_surface_configure(struct weston_surface *surface,
289 int x, int y, int width, int height)
291 weston_surface_damage_below(surface);
295 surface->width = width;
296 surface->height = height;
298 weston_surface_assign_output(surface);
299 weston_surface_damage(surface);
301 pixman_region32_fini(&surface->opaque);
302 if (surface->visual == WESTON_RGB_VISUAL)
303 pixman_region32_init_rect(&surface->opaque,
304 surface->x, surface->y,
305 surface->width, surface->height);
307 pixman_region32_init(&surface->opaque);
311 weston_compositor_get_time(void)
315 gettimeofday(&tv, NULL);
317 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
321 weston_compositor_repick(struct weston_compositor *compositor)
323 struct weston_input_device *device;
324 struct weston_surface *surface;
328 if (!compositor->focus)
331 time = weston_compositor_get_time();
332 wl_list_for_each(device, &compositor->input_device_list, link) {
333 if (device->input_device.grab)
335 surface = pick_surface(&device->input_device, &sx, &sy);
336 wl_input_device_set_pointer_focus(&device->input_device,
339 device->input_device.x,
340 device->input_device.y,
346 destroy_surface(struct wl_resource *resource)
348 struct weston_surface *surface =
349 container_of(resource,
350 struct weston_surface, surface.resource);
351 struct weston_compositor *compositor = surface->compositor;
353 weston_surface_damage_below(surface);
355 wl_list_remove(&surface->link);
356 weston_compositor_repick(compositor);
358 if (surface->saved_texture == 0)
359 glDeleteTextures(1, &surface->texture);
361 glDeleteTextures(1, &surface->saved_texture);
364 wl_list_remove(&surface->buffer_destroy_listener.link);
366 if (surface->image != EGL_NO_IMAGE_KHR)
367 compositor->destroy_image(compositor->display,
370 wl_list_remove(&surface->buffer_link);
376 weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
378 struct weston_surface *es = (struct weston_surface *) surface;
379 struct weston_compositor *ec = es->compositor;
380 struct wl_list *surfaces_attached_to;
382 if (es->saved_texture != 0)
383 es->texture = es->saved_texture;
385 glBindTexture(GL_TEXTURE_2D, es->texture);
387 if (wl_buffer_is_shm(buffer)) {
388 /* Unbind any EGLImage texture that may be bound, so we don't
390 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
391 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
392 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
393 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
394 es->pitch, buffer->height, 0,
395 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
396 wl_shm_buffer_get_data(buffer));
398 switch (wl_shm_buffer_get_format(buffer)) {
399 case WL_SHM_FORMAT_ARGB32:
400 es->visual = WESTON_ARGB_VISUAL;
402 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
403 es->visual = WESTON_PREMUL_ARGB_VISUAL;
405 case WL_SHM_FORMAT_XRGB32:
406 es->visual = WESTON_RGB_VISUAL;
410 surfaces_attached_to = buffer->user_data;
412 wl_list_remove(&es->buffer_link);
413 wl_list_insert(surfaces_attached_to, &es->buffer_link);
415 if (es->image != EGL_NO_IMAGE_KHR)
416 ec->destroy_image(ec->display, es->image);
417 es->image = ec->create_image(ec->display, NULL,
418 EGL_WAYLAND_BUFFER_WL,
421 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
423 /* FIXME: we need to get the visual from the wl_buffer */
424 es->visual = WESTON_PREMUL_ARGB_VISUAL;
425 es->pitch = es->width;
430 texture_region(struct weston_surface *es, pixman_region32_t *region)
432 struct weston_compositor *ec = es->compositor;
433 GLfloat *v, inv_width, inv_height;
434 pixman_box32_t *rectangles;
438 rectangles = pixman_region32_rectangles(region, &n);
439 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
440 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
441 inv_width = 1.0 / es->pitch;
442 inv_height = 1.0 / es->height;
444 for (i = 0; i < n; i++, v += 16, p += 6) {
445 v[ 0] = rectangles[i].x1;
446 v[ 1] = rectangles[i].y1;
447 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
448 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
450 v[ 4] = rectangles[i].x1;
451 v[ 5] = rectangles[i].y2;
453 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
455 v[ 8] = rectangles[i].x2;
456 v[ 9] = rectangles[i].y1;
457 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
460 v[12] = rectangles[i].x2;
461 v[13] = rectangles[i].y2;
477 transform_vertex(struct weston_surface *surface,
478 GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
480 struct weston_vector t;
487 weston_matrix_transform(&surface->transform->matrix, &t);
496 texture_transformed_surface(struct weston_surface *es)
498 struct weston_compositor *ec = es->compositor;
502 v = wl_array_add(&ec->vertices, 16 * sizeof *v);
503 p = wl_array_add(&ec->indices, 6 * sizeof *p);
505 transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
506 transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
507 transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
508 transform_vertex(es, es->x + es->width, es->y + es->height,
522 weston_surface_draw(struct weston_surface *es,
523 struct weston_output *output, pixman_region32_t *clip)
525 struct weston_compositor *ec = es->compositor;
527 pixman_region32_t repaint;
531 pixman_region32_init_rect(&repaint,
532 es->x, es->y, es->width, es->height);
533 pixman_region32_intersect(&repaint, &repaint, clip);
534 if (!pixman_region32_not_empty(&repaint))
537 switch (es->visual) {
538 case WESTON_ARGB_VISUAL:
539 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
542 case WESTON_PREMUL_ARGB_VISUAL:
543 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
546 case WESTON_RGB_VISUAL:
550 fprintf(stderr, "bogus visual\n");
554 if (es->alpha != ec->current_alpha) {
555 glUniform1f(ec->texture_shader.alpha_uniform,
557 ec->current_alpha = es->alpha;
560 if (es->transform == NULL) {
562 n = texture_region(es, &repaint);
565 n = texture_transformed_surface(es);
568 glBindTexture(GL_TEXTURE_2D, es->texture);
569 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
570 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
572 v = ec->vertices.data;
573 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
574 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
575 glEnableVertexAttribArray(0);
576 glEnableVertexAttribArray(1);
577 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
579 ec->vertices.size = 0;
580 ec->indices.size = 0;
581 pixman_region32_fini(&repaint);
585 weston_surface_raise(struct weston_surface *surface)
587 struct weston_compositor *compositor = surface->compositor;
589 wl_list_remove(&surface->link);
590 wl_list_insert(&compositor->surface_list, &surface->link);
591 weston_compositor_repick(compositor);
592 weston_surface_damage(surface);
596 weston_compositor_damage_all(struct weston_compositor *compositor)
598 struct weston_output *output;
600 wl_list_for_each(output, &compositor->output_list, link)
601 weston_output_damage(output);
605 weston_buffer_post_release(struct wl_buffer *buffer)
607 if (--buffer->busy_count > 0)
610 assert(buffer->resource.client != NULL);
611 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
615 weston_output_damage(struct weston_output *output)
617 struct weston_compositor *compositor = output->compositor;
618 struct weston_surface *es;
620 if (wl_list_empty(&compositor->surface_list))
623 es = container_of(compositor->surface_list.next,
624 struct weston_surface, link);
625 pixman_region32_union(&es->damage, &es->damage, &output->region);
626 weston_compositor_schedule_repaint(compositor);
630 fade_frame(struct weston_animation *animation,
631 struct weston_output *output, uint32_t msecs)
633 struct weston_compositor *compositor =
634 container_of(animation,
635 struct weston_compositor, fade.animation);
637 weston_spring_update(&compositor->fade.spring, msecs);
638 if (weston_spring_done(&compositor->fade.spring)) {
639 compositor->fade.spring.current =
640 compositor->fade.spring.target;
641 wl_list_remove(&animation->link);
642 wl_list_init(&animation->link);
644 if (compositor->fade.spring.current > 0.999) {
645 compositor->state = WESTON_COMPOSITOR_SLEEPING;
646 compositor->shell->lock(compositor->shell);
650 weston_output_damage(output);
654 fade_output(struct weston_output *output,
655 GLfloat tint, pixman_region32_t *region)
657 struct weston_compositor *compositor = output->compositor;
658 struct weston_surface surface;
659 GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
661 surface.compositor = compositor;
662 surface.x = output->x;
663 surface.y = output->y;
664 surface.pitch = output->current->width;
665 surface.width = output->current->width;
666 surface.height = output->current->height;
667 surface.texture = GL_NONE;
668 surface.transform = NULL;
669 surface.alpha = compositor->current_alpha;
672 surface.visual = WESTON_PREMUL_ARGB_VISUAL;
674 surface.visual = WESTON_RGB_VISUAL;
676 glUseProgram(compositor->solid_shader.program);
677 glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
678 1, GL_FALSE, output->matrix.d);
679 glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
680 weston_surface_draw(&surface, output, region);
684 weston_output_set_cursor(struct weston_output *output,
685 struct wl_input_device *dev, int force_sw)
687 struct weston_compositor *ec = output->compositor;
688 struct weston_input_device *device =
689 (struct weston_input_device *) dev;
690 pixman_region32_t cursor_region;
691 int use_hardware_cursor = 1, prior_was_hardware;
693 if (device->sprite == NULL)
696 pixman_region32_init_rect(&cursor_region,
697 device->sprite->x, device->sprite->y,
698 device->sprite->width,
699 device->sprite->height);
701 pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
703 if (!pixman_region32_not_empty(&cursor_region)) {
704 output->set_hardware_cursor(output, NULL);
708 prior_was_hardware = wl_list_empty(&device->sprite->link);
709 if (force_sw || output->set_hardware_cursor(output, device) < 0) {
710 if (prior_was_hardware) {
711 weston_surface_damage(device->sprite);
712 output->set_hardware_cursor(output, NULL);
714 use_hardware_cursor = 0;
715 } else if (!prior_was_hardware) {
716 weston_surface_damage_below(device->sprite);
719 /* Remove always to be on top. */
720 wl_list_remove(&device->sprite->link);
721 if (!use_hardware_cursor && ec->focus) {
722 wl_list_insert(&ec->surface_list, &device->sprite->link);
723 device->sprite->output = output;
725 wl_list_init(&device->sprite->link);
726 device->sprite->output = NULL;
730 pixman_region32_fini(&cursor_region);
734 setup_scanout_surface(struct weston_output *output, struct weston_surface *es)
736 if (es->visual != WESTON_RGB_VISUAL ||
737 output->prepare_scanout_surface(output, es) != 0)
740 /* assert output->pending_scanout_buffer == NULL */
741 output->pending_scanout_buffer = es->buffer;
742 output->pending_scanout_buffer->busy_count++;
744 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
745 &output->pending_scanout_buffer_destroy_listener.link);
751 weston_output_repaint(struct weston_output *output)
753 struct weston_compositor *ec = output->compositor;
754 struct weston_surface *es;
755 pixman_region32_t opaque, new_damage, total_damage, repaint;
757 output->prepare_render(output);
759 glViewport(0, 0, output->current->width, output->current->height);
761 glUseProgram(ec->texture_shader.program);
762 glUniformMatrix4fv(ec->texture_shader.proj_uniform,
763 1, GL_FALSE, output->matrix.d);
764 glUniform1i(ec->texture_shader.tex_uniform, 0);
766 weston_output_set_cursor(output, ec->input_device,
767 ec->fade.spring.current >= 0.001);
769 pixman_region32_init(&new_damage);
770 pixman_region32_init(&opaque);
772 wl_list_for_each(es, &ec->surface_list, link) {
773 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
774 pixman_region32_union(&new_damage, &new_damage, &es->damage);
775 pixman_region32_union(&opaque, &opaque, &es->opaque);
778 pixman_region32_init(&total_damage);
779 pixman_region32_union(&total_damage, &new_damage,
780 &output->previous_damage);
781 pixman_region32_intersect(&output->previous_damage,
782 &new_damage, &output->region);
784 pixman_region32_fini(&opaque);
785 pixman_region32_fini(&new_damage);
787 es = container_of(ec->surface_list.next, struct weston_surface, link);
789 if (setup_scanout_surface(output, es) == 0)
790 /* We're drawing nothing, just let the damage accumulate */
793 if (es->fullscreen_output == output) {
794 if (es->width < output->current->width ||
795 es->height < output->current->height)
796 glClear(GL_COLOR_BUFFER_BIT);
797 weston_surface_draw(es, output, &total_damage);
799 wl_list_for_each(es, &ec->surface_list, link) {
800 pixman_region32_copy(&es->damage, &total_damage);
801 pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
804 wl_list_for_each_reverse(es, &ec->surface_list, link) {
805 pixman_region32_init(&repaint);
806 pixman_region32_intersect(&repaint, &output->region,
808 weston_surface_draw(es, output, &repaint);
809 pixman_region32_subtract(&es->damage,
810 &es->damage, &output->region);
814 if (ec->fade.spring.current > 0.001)
815 fade_output(output, ec->fade.spring.current, &total_damage);
818 struct weston_frame_callback {
819 struct wl_resource resource;
824 repaint(void *data, int msecs)
826 struct weston_output *output = data;
827 struct weston_compositor *compositor = output->compositor;
828 struct weston_animation *animation, *next;
829 struct weston_frame_callback *cb, *cnext;
831 weston_output_repaint(output);
832 output->repaint_needed = 0;
833 output->repaint_scheduled = 1;
834 output->present(output);
836 wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
837 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
838 wl_resource_destroy(&cb->resource, 0);
841 wl_list_for_each_safe(animation, next,
842 &compositor->animation_list, link)
843 animation->frame(animation, output, msecs);
847 idle_repaint(void *data)
849 repaint(data, weston_compositor_get_time());
853 weston_output_finish_frame(struct weston_output *output, int msecs)
855 if (output->scanout_buffer) {
856 weston_buffer_post_release(output->scanout_buffer);
857 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
858 output->scanout_buffer = NULL;
861 if (output->pending_scanout_buffer) {
862 output->scanout_buffer = output->pending_scanout_buffer;
863 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
864 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
865 &output->scanout_buffer_destroy_listener.link);
866 output->pending_scanout_buffer = NULL;
869 if (output->repaint_needed)
870 repaint(output, msecs);
872 output->repaint_scheduled = 0;
876 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
878 struct weston_output *output;
879 struct wl_event_loop *loop;
881 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
884 loop = wl_display_get_event_loop(compositor->wl_display);
885 wl_list_for_each(output, &compositor->output_list, link) {
886 output->repaint_needed = 1;
887 if (output->repaint_scheduled)
890 wl_event_loop_add_idle(loop, idle_repaint, output);
891 output->repaint_scheduled = 1;
896 weston_compositor_fade(struct weston_compositor *compositor, float tint)
900 done = weston_spring_done(&compositor->fade.spring);
901 compositor->fade.spring.target = tint;
902 if (weston_spring_done(&compositor->fade.spring))
906 compositor->fade.spring.timestamp =
907 weston_compositor_get_time();
909 weston_compositor_damage_all(compositor);
910 if (wl_list_empty(&compositor->fade.animation.link))
911 wl_list_insert(compositor->animation_list.prev,
912 &compositor->fade.animation.link);
916 surface_destroy(struct wl_client *client, struct wl_resource *resource)
918 wl_resource_destroy(resource, weston_compositor_get_time());
922 weston_surface_assign_output(struct weston_surface *es)
924 struct weston_compositor *ec = es->compositor;
925 struct weston_output *output, *new_output;
926 pixman_region32_t region;
932 wl_list_for_each(output, &ec->output_list, link) {
933 pixman_region32_init_rect(®ion,
934 es->x, es->y, es->width, es->height);
935 pixman_region32_intersect(®ion, ®ion, &output->region);
937 e = pixman_region32_extents(®ion);
938 area = (e->x2 - e->x1) * (e->y2 - e->y1);
946 es->output = new_output;
947 if (!wl_list_empty(&es->frame_callback_list)) {
948 wl_list_insert_list(new_output->frame_callback_list.prev,
949 &es->frame_callback_list);
950 wl_list_init(&es->frame_callback_list);
955 surface_attach(struct wl_client *client,
956 struct wl_resource *resource,
957 struct wl_resource *buffer_resource, int32_t x, int32_t y)
959 struct weston_surface *es = resource->data;
960 struct weston_shell *shell = es->compositor->shell;
961 struct wl_buffer *buffer = buffer_resource->data;
964 weston_buffer_post_release(es->buffer);
965 wl_list_remove(&es->buffer_destroy_listener.link);
968 buffer->busy_count++;
970 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
971 &es->buffer_destroy_listener.link);
973 if (es->visual == WESTON_NONE_VISUAL) {
974 shell->map(shell, es, buffer->width, buffer->height);
975 } else if (x != 0 || y != 0 ||
976 es->width != buffer->width ||
977 es->height != buffer->height) {
978 shell->configure(shell, es, es->x + x, es->y + y,
979 buffer->width, buffer->height);
982 weston_buffer_attach(buffer, &es->surface);
986 surface_damage(struct wl_client *client,
987 struct wl_resource *resource,
988 int32_t x, int32_t y, int32_t width, int32_t height)
990 struct weston_surface *es = resource->data;
992 weston_surface_damage_rectangle(es, x, y, width, height);
996 destroy_frame_callback(struct wl_resource *resource)
998 struct weston_frame_callback *cb = resource->data;
1000 wl_list_remove(&cb->link);
1005 surface_frame(struct wl_client *client,
1006 struct wl_resource *resource, uint32_t callback)
1008 struct weston_frame_callback *cb;
1009 struct weston_surface *es = resource->data;
1011 cb = malloc(sizeof *cb);
1013 wl_resource_post_no_memory(resource);
1017 cb->resource.object.interface = &wl_callback_interface;
1018 cb->resource.object.id = callback;
1019 cb->resource.destroy = destroy_frame_callback;
1020 cb->resource.client = client;
1021 cb->resource.data = cb;
1023 wl_client_add_resource(client, &cb->resource);
1026 wl_list_insert(es->output->frame_callback_list.prev,
1029 wl_list_insert(es->frame_callback_list.prev, &cb->link);
1033 const static struct wl_surface_interface surface_interface = {
1041 compositor_create_surface(struct wl_client *client,
1042 struct wl_resource *resource, uint32_t id)
1044 struct weston_compositor *ec = resource->data;
1045 struct weston_surface *surface;
1047 surface = weston_surface_create(ec, 0, 0, 0, 0);
1048 if (surface == NULL) {
1049 wl_resource_post_no_memory(resource);
1053 surface->surface.resource.destroy = destroy_surface;
1055 surface->surface.resource.object.id = id;
1056 surface->surface.resource.object.interface = &wl_surface_interface;
1057 surface->surface.resource.object.implementation =
1058 (void (**)(void)) &surface_interface;
1059 surface->surface.resource.data = surface;
1061 wl_client_add_resource(client, &surface->surface.resource);
1064 const static struct wl_compositor_interface compositor_interface = {
1065 compositor_create_surface,
1069 weston_surface_transform(struct weston_surface *surface,
1070 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1072 *sx = x - surface->x;
1073 *sy = y - surface->y;
1076 static struct weston_surface *
1077 weston_compositor_pick_surface(struct weston_compositor *compositor,
1078 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1080 struct weston_surface *surface;
1082 wl_list_for_each(surface, &compositor->surface_list, link) {
1083 if (surface->surface.resource.client == NULL)
1085 weston_surface_transform(surface, x, y, sx, sy);
1086 if (0 <= *sx && *sx < surface->width &&
1087 0 <= *sy && *sy < surface->height)
1094 WL_EXPORT struct weston_surface *
1095 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1097 struct weston_input_device *wd = (struct weston_input_device *) device;
1099 return weston_compositor_pick_surface(wd->compositor,
1100 device->x, device->y, sx, sy);
1105 implicit_grab_motion(struct wl_grab *grab,
1106 uint32_t time, int32_t x, int32_t y)
1108 struct weston_input_device *device =
1109 (struct weston_input_device *) grab->input_device;
1110 struct weston_surface *es =
1111 (struct weston_surface *) device->input_device.pointer_focus;
1113 struct wl_resource *resource;
1115 resource = grab->input_device->pointer_focus_resource;
1117 weston_surface_transform(es, x, y, &sx, &sy);
1118 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1119 time, x, y, sx, sy);
1124 implicit_grab_button(struct wl_grab *grab,
1125 uint32_t time, int32_t button, int32_t state)
1127 struct wl_resource *resource;
1129 resource = grab->input_device->pointer_focus_resource;
1131 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1132 time, button, state);
1136 implicit_grab_end(struct wl_grab *grab, uint32_t time)
1140 static const struct wl_grab_interface implicit_grab_interface = {
1141 implicit_grab_motion,
1142 implicit_grab_button,
1147 weston_compositor_wake(struct weston_compositor *compositor)
1149 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1150 weston_compositor_fade(compositor, 0.0);
1152 wl_event_source_timer_update(compositor->idle_source,
1153 compositor->idle_time * 1000);
1157 weston_compositor_activity(struct weston_compositor *compositor)
1159 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1160 weston_compositor_wake(compositor);
1162 compositor->shell->unlock(compositor->shell);
1167 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
1169 weston_compositor_activity(compositor);
1170 compositor->idle_inhibit++;
1174 weston_compositor_idle_release(struct weston_compositor *compositor)
1176 compositor->idle_inhibit--;
1177 weston_compositor_activity(compositor);
1181 idle_handler(void *data)
1183 struct weston_compositor *compositor = data;
1185 if (compositor->idle_inhibit)
1188 weston_compositor_fade(compositor, 1.0);
1194 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1196 struct weston_surface *es;
1197 struct weston_output *output;
1198 const struct wl_grab_interface *interface;
1199 struct weston_input_device *wd = (struct weston_input_device *) device;
1200 struct weston_compositor *ec = wd->compositor;
1202 int x_valid = 0, y_valid = 0;
1203 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1205 weston_compositor_activity(ec);
1207 wl_list_for_each(output, &ec->output_list, link) {
1208 if (output->x <= x && x <= output->x + output->current->width)
1211 if (output->y <= y && y <= output->y + output->current->height)
1214 /* FIXME: calculate this only on output addition/deletion */
1215 if (output->x < min_x)
1217 if (output->y < min_y)
1220 if (output->x + output->current->width > max_x)
1221 max_x = output->x + output->current->width;
1222 if (output->y + output->current->height > max_y)
1223 max_y = output->y + output->current->height;
1229 else if (x >= max_x)
1235 else if (y >= max_y)
1243 interface = device->grab->interface;
1244 interface->motion(device->grab, time, x, y);
1246 es = pick_surface(device, &sx, &sy);
1247 wl_input_device_set_pointer_focus(device,
1249 time, x, y, sx, sy);
1250 if (device->pointer_focus_resource)
1251 wl_resource_post_event(device->pointer_focus_resource,
1252 WL_INPUT_DEVICE_MOTION,
1253 time, x, y, sx, sy);
1257 weston_surface_damage_below(wd->sprite);
1259 wd->sprite->x = device->x - wd->hotspot_x;
1260 wd->sprite->y = device->y - wd->hotspot_y;
1262 weston_surface_damage(wd->sprite);
1267 weston_surface_activate(struct weston_surface *surface,
1268 struct weston_input_device *device, uint32_t time)
1270 weston_surface_raise(surface);
1271 wl_input_device_set_keyboard_focus(&device->input_device,
1272 &surface->surface, time);
1273 weston_data_device_set_keyboard_focus(device);
1277 notify_button(struct wl_input_device *device,
1278 uint32_t time, int32_t button, int32_t state)
1280 struct weston_input_device *wd = (struct weston_input_device *) device;
1281 struct weston_compositor *compositor = wd->compositor;
1282 struct weston_surface *surface =
1283 (struct weston_surface *) device->pointer_focus;
1287 weston_compositor_idle_inhibit(compositor);
1289 weston_compositor_idle_release(compositor);
1291 weston_compositor_run_binding(compositor, wd, time, 0, button, state);
1293 if (state && surface && device->grab == NULL) {
1294 wl_input_device_start_grab(device,
1295 &device->implicit_grab,
1300 device->grab->interface->button(device->grab, time,
1303 if (!state && device->grab && device->grab_button == button) {
1304 wl_input_device_end_grab(device, time);
1305 surface = pick_surface(device, &sx, &sy);
1306 wl_input_device_set_pointer_focus(device, &surface->surface,
1307 time, device->x, device->y,
1313 update_modifier_state(struct weston_input_device *device,
1314 uint32_t key, uint32_t state)
1321 modifier = MODIFIER_CTRL;
1326 modifier = MODIFIER_ALT;
1331 modifier = MODIFIER_SUPER;
1340 device->modifier_state |= modifier;
1342 device->modifier_state &= ~modifier;
1346 notify_key(struct wl_input_device *device,
1347 uint32_t time, uint32_t key, uint32_t state)
1349 struct weston_input_device *wd = (struct weston_input_device *) device;
1350 struct weston_compositor *compositor = wd->compositor;
1354 weston_compositor_idle_inhibit(compositor);
1356 weston_compositor_idle_release(compositor);
1358 weston_compositor_run_binding(compositor, wd, time, key, 0, state);
1360 update_modifier_state(wd, key, state);
1361 end = device->keys.data + device->keys.size;
1362 for (k = device->keys.data; k < end; k++) {
1366 device->keys.size = (void *) end - device->keys.data;
1368 k = wl_array_add(&device->keys, sizeof *k);
1372 if (device->keyboard_focus_resource)
1373 wl_resource_post_event(device->keyboard_focus_resource,
1374 WL_INPUT_DEVICE_KEY, time, key, state);
1378 notify_pointer_focus(struct wl_input_device *device,
1379 uint32_t time, struct weston_output *output,
1380 int32_t x, int32_t y)
1382 struct weston_input_device *wd = (struct weston_input_device *) device;
1383 struct weston_compositor *compositor = wd->compositor;
1384 struct weston_surface *es;
1390 es = pick_surface(device, &sx, &sy);
1391 wl_input_device_set_pointer_focus(device,
1393 time, x, y, sx, sy);
1395 compositor->focus = 1;
1397 wl_input_device_set_pointer_focus(device, NULL,
1399 compositor->focus = 0;
1404 notify_keyboard_focus(struct wl_input_device *device,
1405 uint32_t time, struct weston_output *output,
1406 struct wl_array *keys)
1408 struct weston_input_device *wd =
1409 (struct weston_input_device *) device;
1410 struct weston_compositor *compositor = wd->compositor;
1411 struct weston_surface *es;
1414 if (!wl_list_empty(&compositor->surface_list))
1415 es = container_of(compositor->surface_list.next,
1416 struct weston_surface, link);
1421 wl_array_copy(&wd->input_device.keys, keys);
1422 wd->modifier_state = 0;
1423 end = device->keys.data + device->keys.size;
1424 for (k = device->keys.data; k < end; k++) {
1425 weston_compositor_idle_inhibit(compositor);
1426 update_modifier_state(wd, *k, 1);
1429 if (es && es->surface.resource.client)
1430 wl_input_device_set_keyboard_focus(&wd->input_device,
1431 &es->surface, time);
1433 end = device->keys.data + device->keys.size;
1434 for (k = device->keys.data; k < end; k++)
1435 weston_compositor_idle_release(compositor);
1437 wd->modifier_state = 0;
1438 wl_input_device_set_keyboard_focus(&wd->input_device,
1443 /* TODO: share this function with wayland-server.c */
1444 static struct wl_resource *
1445 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
1447 struct wl_resource *r;
1452 wl_list_for_each(r, list, link) {
1453 if (r->client == surface->resource.client)
1461 lose_touch_focus_resource(struct wl_listener *listener,
1462 struct wl_resource *resource, uint32_t time)
1464 struct weston_input_device *device =
1465 container_of(listener, struct weston_input_device,
1466 touch_focus_resource_listener);
1468 device->touch_focus_resource = NULL;
1472 lose_touch_focus(struct wl_listener *listener,
1473 struct wl_resource *resource, uint32_t time)
1475 struct weston_input_device *device =
1476 container_of(listener, struct weston_input_device,
1477 touch_focus_listener);
1479 device->touch_focus = NULL;
1483 touch_set_focus(struct weston_input_device *device,
1484 struct wl_surface *surface, uint32_t time)
1486 struct wl_input_device *input_device = &device->input_device;
1487 struct wl_resource *resource;
1489 if (device->touch_focus == surface)
1494 find_resource_for_surface(&input_device->resource_list,
1497 fprintf(stderr, "couldn't find resource\n");
1501 device->touch_focus_resource_listener.func =
1502 lose_touch_focus_resource;
1503 wl_list_insert(resource->destroy_listener_list.prev,
1504 &device->touch_focus_resource_listener.link);
1505 device->touch_focus_listener.func = lose_touch_focus;
1506 wl_list_insert(surface->resource.destroy_listener_list.prev,
1507 &device->touch_focus_listener.link);
1509 device->touch_focus = surface;
1510 device->touch_focus_resource = resource;
1512 if (device->touch_focus)
1513 wl_list_remove(&device->touch_focus_listener.link);
1514 if (device->touch_focus_resource)
1515 wl_list_remove(&device->touch_focus_resource_listener.link);
1516 device->touch_focus = NULL;
1517 device->touch_focus_resource = NULL;
1522 * notify_touch - emulates button touches and notifies surfaces accordingly.
1524 * It assumes always the correct cycle sequence until it gets here: touch_down
1525 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1526 * for sending along such order.
1530 notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
1531 int x, int y, int touch_type)
1533 struct weston_input_device *wd = (struct weston_input_device *) device;
1534 struct weston_compositor *ec = wd->compositor;
1535 struct weston_surface *es;
1538 switch (touch_type) {
1539 case WL_INPUT_DEVICE_TOUCH_DOWN:
1540 weston_compositor_idle_inhibit(ec);
1544 /* the first finger down picks the surface, and all further go
1545 * to that surface for the remainder of the touch session i.e.
1546 * until all touch points are up again. */
1547 if (wd->num_tp == 1) {
1548 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1549 touch_set_focus(wd, &es->surface, time);
1550 } else if (wd->touch_focus) {
1551 es = (struct weston_surface *) wd->touch_focus;
1552 weston_surface_transform(es, x, y, &sx, &sy);
1555 if (wd->touch_focus_resource && wd->touch_focus)
1556 wl_resource_post_event(wd->touch_focus_resource,
1561 case WL_INPUT_DEVICE_TOUCH_MOTION:
1562 es = (struct weston_surface *) wd->touch_focus;
1566 weston_surface_transform(es, x, y, &sx, &sy);
1567 if (wd->touch_focus_resource)
1568 wl_resource_post_event(wd->touch_focus_resource,
1572 case WL_INPUT_DEVICE_TOUCH_UP:
1573 weston_compositor_idle_release(ec);
1576 if (wd->touch_focus_resource)
1577 wl_resource_post_event(wd->touch_focus_resource,
1578 touch_type, time, touch_id);
1579 if (wd->num_tp == 0)
1580 touch_set_focus(wd, NULL, time);
1586 input_device_attach(struct wl_client *client,
1587 struct wl_resource *resource,
1589 struct wl_resource *buffer_resource, int32_t x, int32_t y)
1591 struct weston_input_device *device = resource->data;
1592 struct weston_compositor *compositor = device->compositor;
1593 struct wl_buffer *buffer;
1595 if (time < device->input_device.pointer_focus_time)
1597 if (device->input_device.pointer_focus == NULL)
1599 if (device->input_device.pointer_focus->resource.client != client)
1603 weston_surface_damage_below(device->sprite);
1605 if (!buffer_resource) {
1606 destroy_surface(&device->sprite->surface.resource);
1607 device->sprite = NULL;
1611 if (!device->sprite) {
1613 weston_surface_create(compositor,
1614 device->input_device.x,
1615 device->input_device.y, 32, 32);
1616 wl_list_init(&device->sprite->link);
1619 buffer = buffer_resource->data;
1620 weston_buffer_attach(buffer, &device->sprite->surface);
1622 device->hotspot_x = x;
1623 device->hotspot_y = y;
1624 device->sprite->width = buffer->width;
1625 device->sprite->height = buffer->height;
1626 device->sprite->x = device->input_device.x - device->hotspot_x;
1627 device->sprite->y = device->input_device.y - device->hotspot_y;
1629 weston_surface_damage(device->sprite);
1632 const static struct wl_input_device_interface input_device_interface = {
1633 input_device_attach,
1636 static void unbind_input_device(struct wl_resource *resource)
1638 wl_list_remove(&resource->link);
1643 bind_input_device(struct wl_client *client,
1644 void *data, uint32_t version, uint32_t id)
1646 struct wl_input_device *device = data;
1647 struct wl_resource *resource;
1649 resource = wl_client_add_object(client, &wl_input_device_interface,
1650 &input_device_interface, id, data);
1651 wl_list_insert(&device->resource_list, &resource->link);
1652 resource->destroy = unbind_input_device;
1656 weston_input_device_init(struct weston_input_device *device,
1657 struct weston_compositor *ec)
1659 wl_input_device_init(&device->input_device);
1660 wl_list_init(&device->drag_resource_list);
1662 wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1663 device, bind_input_device);
1665 device->sprite = NULL;
1667 device->compositor = ec;
1668 device->hotspot_x = 16;
1669 device->hotspot_y = 16;
1670 device->modifier_state = 0;
1673 device->input_device.implicit_grab.interface = &implicit_grab_interface;
1675 wl_list_insert(ec->input_device_list.prev, &device->link);
1677 device->selection_data_source = NULL;
1681 bind_output(struct wl_client *client,
1682 void *data, uint32_t version, uint32_t id)
1684 struct weston_output *output = data;
1685 struct weston_mode *mode;
1686 struct wl_resource *resource;
1688 resource = wl_client_add_object(client,
1689 &wl_output_interface, NULL, id, data);
1691 wl_resource_post_event(resource,
1698 output->make, output->model);
1700 wl_list_for_each (mode, &output->mode_list, link) {
1701 wl_resource_post_event(resource,
1710 static const char vertex_shader[] =
1711 "uniform mat4 proj;\n"
1712 "attribute vec2 position;\n"
1713 "attribute vec2 texcoord;\n"
1714 "varying vec2 v_texcoord;\n"
1717 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1718 " v_texcoord = texcoord;\n"
1721 static const char texture_fragment_shader[] =
1722 "precision mediump float;\n"
1723 "varying vec2 v_texcoord;\n"
1724 "uniform sampler2D tex;\n"
1725 "uniform float alpha;\n"
1728 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1729 " gl_FragColor = alpha * gl_FragColor;\n"
1732 static const char solid_fragment_shader[] =
1733 "precision mediump float;\n"
1734 "uniform vec4 color;\n"
1737 " gl_FragColor = color\n;"
1741 compile_shader(GLenum type, const char *source)
1747 s = glCreateShader(type);
1748 glShaderSource(s, 1, &source, NULL);
1750 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1752 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1753 fprintf(stderr, "shader info: %s\n", msg);
1761 weston_shader_init(struct weston_shader *shader,
1762 const char *vertex_source, const char *fragment_source)
1767 shader->vertex_shader =
1768 compile_shader(GL_VERTEX_SHADER, vertex_source);
1769 shader->fragment_shader =
1770 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1772 shader->program = glCreateProgram();
1773 glAttachShader(shader->program, shader->vertex_shader);
1774 glAttachShader(shader->program, shader->fragment_shader);
1775 glBindAttribLocation(shader->program, 0, "position");
1776 glBindAttribLocation(shader->program, 1, "texcoord");
1778 glLinkProgram(shader->program);
1779 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1781 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1782 fprintf(stderr, "link info: %s\n", msg);
1786 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1787 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1788 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1794 init_solid_shader(struct weston_shader *shader,
1795 GLuint vertex_shader, const char *fragment_source)
1800 shader->vertex_shader = vertex_shader;
1801 shader->fragment_shader =
1802 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1804 shader->program = glCreateProgram();
1805 glAttachShader(shader->program, shader->vertex_shader);
1806 glAttachShader(shader->program, shader->fragment_shader);
1807 glBindAttribLocation(shader->program, 0, "position");
1808 glBindAttribLocation(shader->program, 1, "texcoord");
1810 glLinkProgram(shader->program);
1811 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1813 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1814 fprintf(stderr, "link info: %s\n", msg);
1818 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1819 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1825 weston_output_destroy(struct weston_output *output)
1827 pixman_region32_fini(&output->region);
1828 pixman_region32_fini(&output->previous_damage);
1832 weston_output_move(struct weston_output *output, int x, int y)
1839 pixman_region32_init(&output->previous_damage);
1840 pixman_region32_init_rect(&output->region, x, y,
1841 output->current->width,
1842 output->current->height);
1844 weston_matrix_init(&output->matrix);
1845 weston_matrix_translate(&output->matrix,
1846 -output->x - output->current->width / 2.0,
1847 -output->y - output->current->height / 2.0, 0);
1849 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1850 weston_matrix_scale(&output->matrix,
1851 2.0 / output->current->width,
1852 flip * 2.0 / output->current->height, 1);
1854 weston_output_damage(output);
1858 weston_output_init(struct weston_output *output, struct weston_compositor *c,
1859 int x, int y, int width, int height, uint32_t flags)
1861 output->compositor = c;
1864 output->mm_width = width;
1865 output->mm_height = height;
1867 output->flags = flags;
1868 weston_output_move(output, x, y);
1870 output->scanout_buffer_destroy_listener.func =
1871 output_handle_scanout_buffer_destroy;
1872 output->pending_scanout_buffer_destroy_listener.func =
1873 output_handle_pending_scanout_buffer_destroy;
1875 wl_list_init(&output->frame_callback_list);
1877 wl_display_add_global(c->wl_display,
1878 &wl_output_interface, output, bind_output);
1882 shm_buffer_created(struct wl_buffer *buffer)
1884 struct wl_list *surfaces_attached_to;
1886 surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1887 if (!surfaces_attached_to) {
1888 buffer->user_data = NULL;
1892 wl_list_init(surfaces_attached_to);
1894 buffer->user_data = surfaces_attached_to;
1898 shm_buffer_damaged(struct wl_buffer *buffer,
1899 int32_t x, int32_t y, int32_t width, int32_t height)
1901 struct wl_list *surfaces_attached_to = buffer->user_data;
1902 struct weston_surface *es;
1903 GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1905 wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1906 glBindTexture(GL_TEXTURE_2D, es->texture);
1907 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1908 tex_width, buffer->height, 0,
1909 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1910 wl_shm_buffer_get_data(buffer));
1911 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1912 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1917 shm_buffer_destroyed(struct wl_buffer *buffer)
1919 struct wl_list *surfaces_attached_to = buffer->user_data;
1920 struct weston_surface *es, *next;
1922 wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1923 wl_list_remove(&es->buffer_link);
1924 wl_list_init(&es->buffer_link);
1927 free(surfaces_attached_to);
1930 const static struct wl_shm_callbacks shm_callbacks = {
1933 shm_buffer_destroyed
1937 compositor_bind(struct wl_client *client,
1938 void *data, uint32_t version, uint32_t id)
1940 struct weston_compositor *compositor = data;
1942 wl_client_add_object(client, &wl_compositor_interface,
1943 &compositor_interface, id, compositor);
1947 weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
1949 struct wl_event_loop *loop;
1950 const char *extensions;
1952 ec->wl_display = display;
1954 if (!wl_display_add_global(display, &wl_compositor_interface,
1955 ec, compositor_bind))
1958 ec->shm = wl_shm_init(display, &shm_callbacks);
1960 ec->image_target_texture_2d =
1961 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1962 ec->image_target_renderbuffer_storage = (void *)
1963 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1964 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1965 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1967 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1968 ec->unbind_display =
1969 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1971 extensions = (const char *) glGetString(GL_EXTENSIONS);
1972 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1974 "GL_EXT_texture_format_BGRA8888 not available\n");
1979 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1980 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1981 ec->has_bind_display = 1;
1982 if (ec->has_bind_display)
1983 ec->bind_display(ec->display, ec->wl_display);
1985 wl_list_init(&ec->surface_list);
1986 wl_list_init(&ec->input_device_list);
1987 wl_list_init(&ec->output_list);
1988 wl_list_init(&ec->binding_list);
1989 wl_list_init(&ec->animation_list);
1990 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
1991 ec->fade.animation.frame = fade_frame;
1992 wl_list_init(&ec->fade.animation.link);
1994 screenshooter_create(ec);
1996 weston_data_device_manager_init(ec);
1998 glActiveTexture(GL_TEXTURE0);
2000 if (weston_shader_init(&ec->texture_shader,
2001 vertex_shader, texture_fragment_shader) < 0)
2003 if (init_solid_shader(&ec->solid_shader,
2004 ec->texture_shader.vertex_shader,
2005 solid_fragment_shader) < 0)
2008 loop = wl_display_get_event_loop(ec->wl_display);
2009 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2010 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2012 weston_compositor_schedule_repaint(ec);
2018 weston_compositor_shutdown(struct weston_compositor *ec)
2020 struct weston_output *output, *next;
2022 /* Destroy all outputs associated with this compositor */
2023 wl_list_for_each_safe(output, next, &ec->output_list, link)
2024 output->destroy(output);
2027 static int on_term_signal(int signal_number, void *data)
2029 struct wl_display *display = data;
2031 fprintf(stderr, "caught signal %d\n", signal_number);
2032 wl_display_terminate(display);
2038 load_module(const char *name, const char *entrypoint, void **handle)
2040 char path[PATH_MAX];
2041 void *module, *init;
2044 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2046 snprintf(path, sizeof path, "%s", name);
2048 module = dlopen(path, RTLD_LAZY);
2051 "failed to load module: %s\n", dlerror());
2055 init = dlsym(module, entrypoint);
2058 "failed to lookup init function: %s\n", dlerror());
2065 int main(int argc, char *argv[])
2067 struct wl_display *display;
2068 struct weston_compositor *ec;
2069 struct wl_event_loop *loop;
2071 void *shell_module, *backend_module;
2072 int (*shell_init)(struct weston_compositor *ec);
2073 struct weston_compositor
2074 *(*backend_init)(struct wl_display *display, char *options);
2075 char *backend = NULL;
2076 char *backend_options = "";
2079 int option_idle_time = 300;
2081 static const char opts[] = "B:b:o:S:i:s:x";
2082 static const struct option longopts[ ] = {
2083 { "backend", 1, NULL, 'B' },
2084 { "backend-options", 1, NULL, 'o' },
2085 { "socket", 1, NULL, 'S' },
2086 { "idle-time", 1, NULL, 'i' },
2087 { "shell", 1, NULL, 's' },
2088 { "xserver", 0, NULL, 'x' },
2092 while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2098 backend_options = optarg;
2101 option_socket_name = optarg;
2104 option_idle_time = strtol(optarg, &p, 0);
2107 "invalid idle time option: %s\n",
2121 display = wl_display_create();
2123 loop = wl_display_get_event_loop(display);
2124 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2125 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2126 wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
2128 wl_list_init(&child_process_list);
2129 wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
2132 if (getenv("WAYLAND_DISPLAY"))
2133 backend = "wayland-backend.so";
2134 else if (getenv("DISPLAY"))
2135 backend = "x11-backend.so";
2136 else if (getenv("OPENWFD"))
2137 backend = "openwfd-backend.so";
2139 backend = "drm-backend.so";
2143 shell = "desktop-shell.so";
2145 backend_init = load_module(backend, "backend_init", &backend_module);
2149 shell_init = load_module(shell, "shell_init", &shell_module);
2153 ec = backend_init(display, backend_options);
2155 fprintf(stderr, "failed to create compositor\n");
2159 ec->option_idle_time = option_idle_time;
2160 ec->idle_time = option_idle_time;
2162 if (shell_init(ec) < 0)
2166 weston_xserver_init(ec);
2168 if (wl_display_add_socket(display, option_socket_name)) {
2169 fprintf(stderr, "failed to add socket: %m\n");
2173 wl_display_run(display);
2176 weston_xserver_destroy(ec);
2178 if (ec->has_bind_display)
2179 ec->unbind_display(ec->display, display);
2182 wl_display_destroy(display);