2 * Copyright © 2010-2011 Benjamin Franzke
3 * Copyright © 2013 Jason Ekstrand
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.
33 #include <linux/input.h>
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37 #include <wayland-cursor.h>
39 #include "compositor.h"
40 #include "gl-renderer.h"
41 #include "pixman-renderer.h"
42 #include "../shared/image-loader.h"
43 #include "../shared/os-compatibility.h"
44 #include "../shared/cairo-util.h"
45 #include "fullscreen-shell-client-protocol.h"
47 #define WINDOW_TITLE "Weston Compositor"
49 struct wayland_compositor {
50 struct weston_compositor base;
53 struct wl_display *wl_display;
54 struct wl_registry *registry;
55 struct wl_compositor *compositor;
56 struct wl_shell *shell;
57 struct _wl_fullscreen_shell *fshell;
60 struct wl_list output_list;
62 struct wl_event_source *wl_source;
67 int sprawl_across_outputs;
70 cairo_device_t *frame_device;
71 struct wl_cursor_theme *cursor_theme;
72 struct wl_cursor *cursor;
74 struct wl_list input_list;
77 struct wayland_output {
78 struct weston_output base;
81 int draw_initial_frame;
82 struct wl_surface *surface;
84 struct wl_output *output;
87 struct wl_shell_surface *shell_surface;
88 int configure_width, configure_height;
97 struct wl_egl_window *egl_window;
100 cairo_surface_t *left;
101 cairo_surface_t *right;
102 cairo_surface_t *bottom;
107 struct wl_list buffers;
108 struct wl_list free_buffers;
111 struct weston_mode mode;
115 struct wayland_parent_output {
116 struct wayland_output *output;
119 struct wl_output *global;
125 int32_t width, height;
133 struct wl_list mode_list;
134 struct weston_mode *preferred_mode;
135 struct weston_mode *current_mode;
138 struct wayland_shm_buffer {
139 struct wayland_output *output;
141 struct wl_list free_link;
143 struct wl_buffer *buffer;
146 pixman_region32_t damage;
149 pixman_image_t *pm_image;
150 cairo_surface_t *c_surface;
153 struct wayland_input {
154 struct weston_seat base;
155 struct wayland_compositor *compositor;
159 struct wl_seat *seat;
160 struct wl_pointer *pointer;
161 struct wl_keyboard *keyboard;
162 struct wl_touch *touch;
165 struct wl_surface *surface;
170 enum weston_key_state_update keyboard_state_update;
172 uint32_t enter_serial;
174 struct wayland_output *output;
175 struct wayland_output *keyboard_focus;
178 struct gl_renderer_interface *gl_renderer;
181 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
183 cairo_surface_destroy(buffer->c_surface);
184 pixman_image_unref(buffer->pm_image);
186 wl_buffer_destroy(buffer->buffer);
187 munmap(buffer->data, buffer->size);
189 pixman_region32_fini(&buffer->damage);
191 wl_list_remove(&buffer->link);
192 wl_list_remove(&buffer->free_link);
197 buffer_release(void *data, struct wl_buffer *buffer)
199 struct wayland_shm_buffer *sb = data;
202 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
204 wayland_shm_buffer_destroy(sb);
208 static const struct wl_buffer_listener buffer_listener = {
212 static struct wayland_shm_buffer *
213 wayland_output_get_shm_buffer(struct wayland_output *output)
215 struct wayland_compositor *c =
216 (struct wayland_compositor *) output->base.compositor;
217 struct wl_shm *shm = c->parent.shm;
218 struct wayland_shm_buffer *sb;
220 struct wl_shm_pool *pool;
221 int width, height, stride;
226 if (!wl_list_empty(&output->shm.free_buffers)) {
227 sb = container_of(output->shm.free_buffers.next,
228 struct wayland_shm_buffer, free_link);
229 wl_list_remove(&sb->free_link);
230 wl_list_init(&sb->free_link);
236 width = frame_width(output->frame);
237 height = frame_height(output->frame);
239 width = output->base.current_mode->width;
240 height = output->base.current_mode->height;
243 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
245 fd = os_create_anonymous_file(height * stride);
247 weston_log("could not create an anonymous file buffer: %m\n");
251 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
252 if (data == MAP_FAILED) {
253 weston_log("could not mmap %d memory for data: %m\n", height * stride);
258 sb = zalloc(sizeof *sb);
260 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
267 wl_list_init(&sb->free_link);
268 wl_list_insert(&output->shm.buffers, &sb->link);
270 pixman_region32_init_rect(&sb->damage, 0, 0,
271 output->base.width, output->base.height);
272 sb->frame_damaged = 1;
275 sb->size = height * stride;
277 pool = wl_shm_create_pool(shm, fd, sb->size);
279 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
282 WL_SHM_FORMAT_ARGB8888);
283 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
284 wl_shm_pool_destroy(pool);
287 memset(data, 0, sb->size);
290 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
291 width, height, stride);
296 frame_interior(output->frame, &fx, &fy, 0, 0);
298 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
299 (uint32_t *)(data + fy * stride) + fx,
306 frame_done(void *data, struct wl_callback *callback, uint32_t time)
308 struct weston_output *output = data;
310 wl_callback_destroy(callback);
311 weston_output_finish_frame(output, time);
314 static const struct wl_callback_listener frame_listener = {
319 draw_initial_frame(struct wayland_output *output)
321 struct wayland_shm_buffer *sb;
323 sb = wayland_output_get_shm_buffer(output);
325 /* If we are rendering with GL, then orphan it so that it gets
326 * destroyed immediately */
327 if (output->gl.egl_window)
330 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
331 wl_surface_damage(output->parent.surface, 0, 0,
332 output->base.current_mode->width,
333 output->base.current_mode->height);
337 wayland_output_update_gl_border(struct wayland_output *output)
339 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
344 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
347 fwidth = frame_width(output->frame);
348 fheight = frame_height(output->frame);
349 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
351 if (!output->gl.border.top)
352 output->gl.border.top =
353 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
355 cr = cairo_create(output->gl.border.top);
356 frame_repaint(output->frame, cr);
358 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
360 cairo_image_surface_get_stride(output->gl.border.top) / 4,
361 cairo_image_surface_get_data(output->gl.border.top));
364 if (!output->gl.border.left)
365 output->gl.border.left =
366 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
368 cr = cairo_create(output->gl.border.left);
369 cairo_translate(cr, 0, -iy);
370 frame_repaint(output->frame, cr);
372 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
374 cairo_image_surface_get_stride(output->gl.border.left) / 4,
375 cairo_image_surface_get_data(output->gl.border.left));
378 if (!output->gl.border.right)
379 output->gl.border.right =
380 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
381 fwidth - (ix + iwidth), 1);
382 cr = cairo_create(output->gl.border.right);
383 cairo_translate(cr, -(iwidth + ix), -iy);
384 frame_repaint(output->frame, cr);
386 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
387 fwidth - (ix + iwidth), 1,
388 cairo_image_surface_get_stride(output->gl.border.right) / 4,
389 cairo_image_surface_get_data(output->gl.border.right));
392 if (!output->gl.border.bottom)
393 output->gl.border.bottom =
394 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
395 fwidth, fheight - (iy + iheight));
396 cr = cairo_create(output->gl.border.bottom);
397 cairo_translate(cr, 0, -(iy + iheight));
398 frame_repaint(output->frame, cr);
400 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
401 fwidth, fheight - (iy + iheight),
402 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
403 cairo_image_surface_get_data(output->gl.border.bottom));
407 wayland_output_start_repaint_loop(struct weston_output *output_base)
409 struct wayland_output *output = (struct wayland_output *) output_base;
410 struct wayland_compositor *wc =
411 (struct wayland_compositor *)output->base.compositor;
412 struct wl_callback *callback;
414 /* If this is the initial frame, we need to attach a buffer so that
415 * the compositor can map the surface and include it in its render
416 * loop. If the surface doesn't end up in the render loop, the frame
417 * callback won't be invoked. The buffer is transparent and of the
418 * same size as the future real output buffer. */
419 if (output->parent.draw_initial_frame) {
420 output->parent.draw_initial_frame = 0;
422 draw_initial_frame(output);
425 callback = wl_surface_frame(output->parent.surface);
426 wl_callback_add_listener(callback, &frame_listener, output);
427 wl_surface_commit(output->parent.surface);
428 wl_display_flush(wc->parent.wl_display);
432 wayland_output_repaint_gl(struct weston_output *output_base,
433 pixman_region32_t *damage)
435 struct wayland_output *output = (struct wayland_output *) output_base;
436 struct weston_compositor *ec = output->base.compositor;
437 struct wl_callback *callback;
439 callback = wl_surface_frame(output->parent.surface);
440 wl_callback_add_listener(callback, &frame_listener, output);
442 wayland_output_update_gl_border(output);
444 ec->renderer->repaint_output(&output->base, damage);
446 pixman_region32_subtract(&ec->primary_plane.damage,
447 &ec->primary_plane.damage, damage);
452 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
454 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
457 if (!buffer->output->frame || !buffer->frame_damaged)
460 cr = cairo_create(buffer->c_surface);
462 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
463 fwidth = frame_width(buffer->output->frame);
464 fheight = frame_height(buffer->output->frame);
466 /* Set the clip so we don't unnecisaraly damage the surface */
467 cairo_move_to(cr, ix, iy);
468 cairo_rel_line_to(cr, iwidth, 0);
469 cairo_rel_line_to(cr, 0, iheight);
470 cairo_rel_line_to(cr, -iwidth, 0);
471 cairo_line_to(cr, ix, iy);
472 cairo_line_to(cr, 0, iy);
473 cairo_line_to(cr, 0, fheight);
474 cairo_line_to(cr, fwidth, fheight);
475 cairo_line_to(cr, fwidth, 0);
476 cairo_line_to(cr, 0, 0);
477 cairo_line_to(cr, 0, iy);
478 cairo_close_path(cr);
481 /* Draw using a pattern so that the final result gets clipped */
482 cairo_push_group(cr);
483 frame_repaint(buffer->output->frame, cr);
484 cairo_pop_group_to_source(cr);
485 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
492 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
494 pixman_region32_t damage;
495 pixman_box32_t *rects;
496 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
499 pixman_region32_init(&damage);
500 weston_transformed_region(sb->output->base.width,
501 sb->output->base.height,
502 sb->output->base.transform,
503 sb->output->base.current_scale,
504 &sb->damage, &damage);
506 if (sb->output->frame) {
507 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
508 fwidth = frame_width(sb->output->frame);
509 fheight = frame_height(sb->output->frame);
511 pixman_region32_translate(&damage, ix, iy);
513 if (sb->frame_damaged) {
514 pixman_region32_union_rect(&damage, &damage,
516 pixman_region32_union_rect(&damage, &damage,
518 pixman_region32_union_rect(&damage, &damage,
520 fwidth - (ix + iwidth), iheight);
521 pixman_region32_union_rect(&damage, &damage,
523 fwidth, fheight - (iy + iheight));
527 rects = pixman_region32_rectangles(&damage, &n);
528 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
529 for (i = 0; i < n; ++i)
530 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
531 rects[i].y1, rects[i].x2 - rects[i].x1,
532 rects[i].y2 - rects[i].y1);
534 if (sb->output->frame)
535 pixman_region32_fini(&damage);
539 wayland_output_repaint_pixman(struct weston_output *output_base,
540 pixman_region32_t *damage)
542 struct wayland_output *output = (struct wayland_output *) output_base;
543 struct wayland_compositor *c =
544 (struct wayland_compositor *)output->base.compositor;
545 struct wl_callback *callback;
546 struct wayland_shm_buffer *sb;
549 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
550 wl_list_for_each(sb, &output->shm.buffers, link)
551 sb->frame_damaged = 1;
554 wl_list_for_each(sb, &output->shm.buffers, link)
555 pixman_region32_union(&sb->damage, &sb->damage, damage);
557 sb = wayland_output_get_shm_buffer(output);
559 wayland_output_update_shm_border(sb);
560 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
561 c->base.renderer->repaint_output(output_base, &sb->damage);
563 wayland_shm_buffer_attach(sb);
565 callback = wl_surface_frame(output->parent.surface);
566 wl_callback_add_listener(callback, &frame_listener, output);
567 wl_surface_commit(output->parent.surface);
568 wl_display_flush(c->parent.wl_display);
570 pixman_region32_fini(&sb->damage);
571 pixman_region32_init(&sb->damage);
572 sb->frame_damaged = 0;
574 pixman_region32_subtract(&c->base.primary_plane.damage,
575 &c->base.primary_plane.damage, damage);
580 wayland_output_destroy(struct weston_output *output_base)
582 struct wayland_output *output = (struct wayland_output *) output_base;
583 struct wayland_compositor *c =
584 (struct wayland_compositor *) output->base.compositor;
587 pixman_renderer_output_destroy(output_base);
589 gl_renderer->output_destroy(output_base);
592 wl_egl_window_destroy(output->gl.egl_window);
593 wl_surface_destroy(output->parent.surface);
594 if (output->parent.shell_surface)
595 wl_shell_surface_destroy(output->parent.shell_surface);
598 frame_destroy(output->frame);
600 cairo_surface_destroy(output->gl.border.top);
601 cairo_surface_destroy(output->gl.border.left);
602 cairo_surface_destroy(output->gl.border.right);
603 cairo_surface_destroy(output->gl.border.bottom);
605 weston_output_destroy(&output->base);
611 static const struct wl_shell_surface_listener shell_surface_listener;
614 wayland_output_init_gl_renderer(struct wayland_output *output)
616 int32_t fwidth = 0, fheight = 0;
619 fwidth = frame_width(output->frame);
620 fheight = frame_height(output->frame);
622 fwidth = output->base.current_mode->width;
623 fheight = output->base.current_mode->height;
626 output->gl.egl_window =
627 wl_egl_window_create(output->parent.surface,
629 if (!output->gl.egl_window) {
630 weston_log("failure to create wl_egl_window\n");
634 if (gl_renderer->output_create(&output->base,
635 output->gl.egl_window,
636 gl_renderer->alpha_attribs,
643 wl_egl_window_destroy(output->gl.egl_window);
648 wayland_output_init_pixman_renderer(struct wayland_output *output)
650 return pixman_renderer_output_create(&output->base);
654 wayland_output_resize_surface(struct wayland_output *output)
656 struct wayland_compositor *c =
657 (struct wayland_compositor *)output->base.compositor;
658 struct wayland_shm_buffer *buffer, *next;
659 int32_t ix, iy, iwidth, iheight;
660 int32_t width, height;
661 struct wl_region *region;
663 width = output->base.current_mode->width;
664 height = output->base.current_mode->height;
667 frame_resize_inside(output->frame, width, height);
669 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
670 region = wl_compositor_create_region(c->parent.compositor);
671 wl_region_add(region, ix, iy, iwidth, iheight);
672 wl_surface_set_input_region(output->parent.surface, region);
673 wl_region_destroy(region);
675 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
676 region = wl_compositor_create_region(c->parent.compositor);
677 wl_region_add(region, ix, iy, iwidth, iheight);
678 wl_surface_set_opaque_region(output->parent.surface, region);
679 wl_region_destroy(region);
681 width = frame_width(output->frame);
682 height = frame_height(output->frame);
684 region = wl_compositor_create_region(c->parent.compositor);
685 wl_region_add(region, 0, 0, width, height);
686 wl_surface_set_input_region(output->parent.surface, region);
687 wl_region_destroy(region);
689 region = wl_compositor_create_region(c->parent.compositor);
690 wl_region_add(region, 0, 0, width, height);
691 wl_surface_set_opaque_region(output->parent.surface, region);
692 wl_region_destroy(region);
695 if (output->gl.egl_window) {
696 wl_egl_window_resize(output->gl.egl_window,
697 width, height, 0, 0);
699 /* These will need to be re-created due to the resize */
700 gl_renderer->output_set_border(&output->base,
701 GL_RENDERER_BORDER_TOP,
703 cairo_surface_destroy(output->gl.border.top);
704 output->gl.border.top = NULL;
705 gl_renderer->output_set_border(&output->base,
706 GL_RENDERER_BORDER_LEFT,
708 cairo_surface_destroy(output->gl.border.left);
709 output->gl.border.left = NULL;
710 gl_renderer->output_set_border(&output->base,
711 GL_RENDERER_BORDER_RIGHT,
713 cairo_surface_destroy(output->gl.border.right);
714 output->gl.border.right = NULL;
715 gl_renderer->output_set_border(&output->base,
716 GL_RENDERER_BORDER_BOTTOM,
718 cairo_surface_destroy(output->gl.border.bottom);
719 output->gl.border.bottom = NULL;
722 /* Throw away any remaining SHM buffers */
723 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
724 wayland_shm_buffer_destroy(buffer);
725 /* These will get thrown away when they get released */
726 wl_list_for_each(buffer, &output->shm.buffers, link)
727 buffer->output = NULL;
731 wayland_output_set_windowed(struct wayland_output *output)
733 struct wayland_compositor *c =
734 (struct wayland_compositor *)output->base.compositor;
742 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
743 title = malloc(tlen + 1);
747 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
749 title = strdup(WINDOW_TITLE);
753 c->theme = theme_create();
759 output->frame = frame_create(c->theme, 100, 100,
760 FRAME_BUTTON_CLOSE, title);
765 if (output->keyboard_count)
766 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
768 wayland_output_resize_surface(output);
770 wl_shell_surface_set_toplevel(output->parent.shell_surface);
776 wayland_output_set_fullscreen(struct wayland_output *output,
777 enum wl_shell_surface_fullscreen_method method,
778 uint32_t framerate, struct wl_output *target)
780 struct wayland_compositor *c =
781 (struct wayland_compositor *)output->base.compositor;
784 frame_destroy(output->frame);
785 output->frame = NULL;
788 wayland_output_resize_surface(output);
790 if (output->parent.shell_surface) {
791 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
792 method, framerate, target);
793 } else if (c->parent.fshell) {
794 _wl_fullscreen_shell_present_surface(c->parent.fshell,
795 output->parent.surface,
800 static struct weston_mode *
801 wayland_output_choose_mode(struct wayland_output *output,
802 struct weston_mode *ref_mode)
804 struct weston_mode *mode;
806 /* First look for an exact match */
807 wl_list_for_each(mode, &output->base.mode_list, link)
808 if (mode->width == ref_mode->width &&
809 mode->height == ref_mode->height &&
810 mode->refresh == ref_mode->refresh)
813 /* If we can't find an exact match, ignore refresh and try again */
814 wl_list_for_each(mode, &output->base.mode_list, link)
815 if (mode->width == ref_mode->width &&
816 mode->height == ref_mode->height)
819 /* Yeah, we failed */
831 mode_feedback_successful(void *data,
832 struct _wl_fullscreen_shell_mode_feedback *fb)
834 enum mode_status *value = data;
836 printf("Mode switch successful\n");
838 *value = MODE_STATUS_SUCCESS;
842 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
844 enum mode_status *value = data;
846 printf("Mode switch failed\n");
848 *value = MODE_STATUS_FAIL;
852 mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
854 enum mode_status *value = data;
856 printf("Mode switch cancelled\n");
858 *value = MODE_STATUS_CANCEL;
861 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
862 mode_feedback_successful,
863 mode_feedback_failed,
864 mode_feedback_cancelled,
868 wayland_output_switch_mode(struct weston_output *output_base,
869 struct weston_mode *mode)
871 struct wayland_output *output = (struct wayland_output *) output_base;
872 struct wayland_compositor *c;
873 struct wl_surface *old_surface;
874 struct weston_mode *old_mode;
875 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
876 enum mode_status mode_status;
879 if (output_base == NULL) {
880 weston_log("output is NULL.\n");
885 weston_log("mode is NULL.\n");
889 c = (struct wayland_compositor *)output_base->compositor;
891 if (output->parent.shell_surface || !c->parent.fshell)
894 mode = wayland_output_choose_mode(output, mode);
898 if (output->base.current_mode == mode)
901 old_mode = output->base.current_mode;
902 old_surface = output->parent.surface;
903 output->base.current_mode = mode;
904 output->parent.surface =
905 wl_compositor_create_surface(c->parent.compositor);
906 wl_surface_set_user_data(output->parent.surface, output);
908 /* Blow the old buffers because we changed size/surfaces */
909 wayland_output_resize_surface(output);
912 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
913 output->parent.surface,
914 output->parent.output,
916 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
917 &mode_feedback_listener,
920 /* This should kick-start things again */
921 output->parent.draw_initial_frame = 1;
922 wayland_output_start_repaint_loop(&output->base);
924 mode_status = MODE_STATUS_UNKNOWN;
925 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
926 ret = wl_display_dispatch(c->parent.wl_display);
928 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
930 if (mode_status == MODE_STATUS_FAIL) {
931 output->base.current_mode = old_mode;
932 wl_surface_destroy(output->parent.surface);
933 output->parent.surface = old_surface;
934 wayland_output_resize_surface(output);
939 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
940 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
943 pixman_renderer_output_destroy(output_base);
944 if (wayland_output_init_pixman_renderer(output) < 0)
947 gl_renderer->output_destroy(output_base);
948 wl_egl_window_destroy(output->gl.egl_window);
949 if (wayland_output_init_gl_renderer(output) < 0)
952 wl_surface_destroy(old_surface);
954 weston_output_schedule_repaint(&output->base);
963 static struct wayland_output *
964 wayland_output_create(struct wayland_compositor *c, int x, int y,
965 int width, int height, const char *name, int fullscreen,
966 uint32_t transform, int32_t scale)
968 struct wayland_output *output;
969 int output_width, output_height;
971 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
972 width, height, x, y);
974 output = zalloc(sizeof *output);
978 output->name = name ? strdup(name) : NULL;
979 output->base.make = "waywayland";
980 output->base.model = "none";
982 output_width = width * scale;
983 output_height = height * scale;
985 output->parent.surface =
986 wl_compositor_create_surface(c->parent.compositor);
987 if (!output->parent.surface)
989 wl_surface_set_user_data(output->parent.surface, output);
991 output->parent.draw_initial_frame = 1;
993 if (c->parent.shell) {
994 output->parent.shell_surface =
995 wl_shell_get_shell_surface(c->parent.shell,
996 output->parent.surface);
997 if (!output->parent.shell_surface)
999 wl_shell_surface_add_listener(output->parent.shell_surface,
1000 &shell_surface_listener, output);
1003 if (fullscreen && c->parent.shell) {
1004 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1006 wl_display_roundtrip(c->parent.wl_display);
1008 output_width = output->parent.configure_width;
1010 output_height = output->parent.configure_height;
1013 output->mode.flags =
1014 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1015 output->mode.width = output_width;
1016 output->mode.height = output_height;
1017 output->mode.refresh = 60000;
1018 output->scale = scale;
1019 wl_list_init(&output->base.mode_list);
1020 wl_list_insert(&output->base.mode_list, &output->mode.link);
1021 output->base.current_mode = &output->mode;
1023 wl_list_init(&output->shm.buffers);
1024 wl_list_init(&output->shm.free_buffers);
1026 weston_output_init(&output->base, &c->base, x, y, width, height,
1029 if (c->use_pixman) {
1030 if (wayland_output_init_pixman_renderer(output) < 0)
1032 output->base.repaint = wayland_output_repaint_pixman;
1034 if (wayland_output_init_gl_renderer(output) < 0)
1036 output->base.repaint = wayland_output_repaint_gl;
1039 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1040 output->base.destroy = wayland_output_destroy;
1041 output->base.assign_planes = NULL;
1042 output->base.set_backlight = NULL;
1043 output->base.set_dpms = NULL;
1044 output->base.switch_mode = wayland_output_switch_mode;
1046 wl_list_insert(c->base.output_list.prev, &output->base.link);
1051 weston_output_destroy(&output->base);
1052 if (output->parent.shell_surface)
1053 wl_shell_surface_destroy(output->parent.shell_surface);
1055 wl_surface_destroy(output->parent.surface);
1059 /* FIXME: cleanup weston_output */
1065 static struct wayland_output *
1066 wayland_output_create_for_config(struct wayland_compositor *c,
1067 struct weston_config_section *config_section,
1068 int option_width, int option_height,
1069 int option_scale, int32_t x, int32_t y)
1071 struct wayland_output *output;
1072 char *mode, *t, *name, *str;
1073 int width, height, scale;
1075 unsigned int i, slen;
1077 static const struct { const char *name; uint32_t token; } transform_names[] = {
1078 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1079 { "90", WL_OUTPUT_TRANSFORM_90 },
1080 { "180", WL_OUTPUT_TRANSFORM_180 },
1081 { "270", WL_OUTPUT_TRANSFORM_270 },
1082 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1083 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1084 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1085 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1088 weston_config_section_get_string(config_section, "name", &name, NULL);
1090 slen = strlen(name);
1091 slen += strlen(WINDOW_TITLE " - ");
1092 str = malloc(slen + 1);
1094 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1099 name = strdup(WINDOW_TITLE);
1101 weston_config_section_get_string(config_section,
1102 "mode", &mode, "1024x600");
1103 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1104 weston_log("Invalid mode \"%s\" for output %s\n",
1112 width = option_width;
1114 height = option_height;
1116 weston_config_section_get_int(config_section, "scale", &scale, 1);
1119 scale = option_scale;
1121 weston_config_section_get_string(config_section,
1122 "transform", &t, "normal");
1123 transform = WL_OUTPUT_TRANSFORM_NORMAL;
1124 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
1125 if (strcmp(transform_names[i].name, t) == 0) {
1126 transform = transform_names[i].token;
1130 if (i >= ARRAY_LENGTH(transform_names))
1131 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
1134 output = wayland_output_create(c, x, y, width, height, name, 0,
1141 static struct wayland_output *
1142 wayland_output_create_for_parent_output(struct wayland_compositor *c,
1143 struct wayland_parent_output *poutput)
1145 struct wayland_output *output;
1146 struct weston_mode *mode;
1149 if (poutput->current_mode) {
1150 mode = poutput->current_mode;
1151 } else if (poutput->preferred_mode) {
1152 mode = poutput->preferred_mode;
1153 } else if (!wl_list_empty(&poutput->mode_list)) {
1154 mode = container_of(poutput->mode_list.next,
1155 struct weston_mode, link);
1157 weston_log("No valid modes found. Skipping output");
1161 if (!wl_list_empty(&c->base.output_list)) {
1162 output = container_of(c->base.output_list.prev,
1163 struct wayland_output, base.link);
1164 x = output->base.x + output->base.current_mode->width;
1169 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1171 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1175 output->parent.output = poutput->global;
1177 output->base.make = poutput->physical.make;
1178 output->base.model = poutput->physical.model;
1179 wl_list_init(&output->base.mode_list);
1180 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1181 wl_list_init(&poutput->mode_list);
1183 wayland_output_set_fullscreen(output,
1184 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1185 mode->refresh, poutput->global);
1187 if (output->parent.shell_surface) {
1188 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1189 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1190 mode->refresh, poutput->global);
1191 } else if (c->parent.fshell) {
1192 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1193 output->parent.surface,
1194 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1196 _wl_fullscreen_shell_mode_feedback_destroy(
1197 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1198 output->parent.surface,
1207 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1210 wl_shell_surface_pong(shell_surface, serial);
1214 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1215 uint32_t edges, int32_t width, int32_t height)
1217 struct wayland_output *output = data;
1219 output->parent.configure_width = width;
1220 output->parent.configure_height = height;
1222 /* FIXME: implement resizing */
1226 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1230 static const struct wl_shell_surface_listener shell_surface_listener = {
1232 shell_surface_configure,
1233 shell_surface_popup_done
1236 /* Events received from the wayland-server this compositor is client of: */
1238 /* parent input interface */
1240 input_set_cursor(struct wayland_input *input)
1243 struct wl_buffer *buffer;
1244 struct wl_cursor_image *image;
1246 if (!input->compositor->cursor)
1247 return; /* Couldn't load the cursor. Can't set it */
1249 image = input->compositor->cursor->images[0];
1250 buffer = wl_cursor_image_get_buffer(image);
1254 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1255 input->parent.cursor.surface,
1256 image->hotspot_x, image->hotspot_y);
1258 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1259 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1260 image->width, image->height);
1261 wl_surface_commit(input->parent.cursor.surface);
1265 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1266 uint32_t serial, struct wl_surface *surface,
1267 wl_fixed_t x, wl_fixed_t y)
1269 struct wayland_input *input = data;
1271 enum theme_location location;
1273 /* XXX: If we get a modifier event immediately before the focus,
1274 * we should try to keep the same serial. */
1275 input->enter_serial = serial;
1276 input->output = wl_surface_get_user_data(surface);
1278 if (input->output->frame) {
1279 location = frame_pointer_enter(input->output->frame, input,
1281 wl_fixed_to_int(y));
1282 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1283 x -= wl_fixed_from_int(fx);
1284 y -= wl_fixed_from_int(fy);
1286 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1287 weston_output_schedule_repaint(&input->output->base);
1289 location = THEME_LOCATION_CLIENT_AREA;
1292 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1294 if (location == THEME_LOCATION_CLIENT_AREA) {
1296 notify_pointer_focus(&input->base, &input->output->base, x, y);
1297 wl_pointer_set_cursor(input->parent.pointer,
1298 input->enter_serial, NULL, 0, 0);
1301 notify_pointer_focus(&input->base, NULL, 0, 0);
1302 input_set_cursor(input);
1307 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1308 uint32_t serial, struct wl_surface *surface)
1310 struct wayland_input *input = data;
1312 if (input->output->frame) {
1313 frame_pointer_leave(input->output->frame, input);
1315 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1316 weston_output_schedule_repaint(&input->output->base);
1319 notify_pointer_focus(&input->base, NULL, 0, 0);
1320 input->output = NULL;
1325 input_handle_motion(void *data, struct wl_pointer *pointer,
1326 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1328 struct wayland_input *input = data;
1330 enum theme_location location;
1332 if (input->output->frame) {
1333 location = frame_pointer_motion(input->output->frame, input,
1335 wl_fixed_to_int(y));
1336 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1337 x -= wl_fixed_from_int(fx);
1338 y -= wl_fixed_from_int(fy);
1340 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1341 weston_output_schedule_repaint(&input->output->base);
1343 location = THEME_LOCATION_CLIENT_AREA;
1346 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1348 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1349 input_set_cursor(input);
1350 notify_pointer_focus(&input->base, NULL, 0, 0);
1352 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1353 wl_pointer_set_cursor(input->parent.pointer,
1354 input->enter_serial, NULL, 0, 0);
1355 notify_pointer_focus(&input->base, &input->output->base, x, y);
1359 if (location == THEME_LOCATION_CLIENT_AREA)
1360 notify_motion_absolute(&input->base, time, x, y);
1364 input_handle_button(void *data, struct wl_pointer *pointer,
1365 uint32_t serial, uint32_t time, uint32_t button,
1368 struct wayland_input *input = data;
1369 enum wl_pointer_button_state state = state_w;
1370 enum frame_button_state fstate;
1371 enum theme_location location;
1373 if (input->output->frame) {
1374 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1375 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1377 location = frame_pointer_button(input->output->frame, input,
1380 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1382 wl_shell_surface_move(input->output->parent.shell_surface,
1383 input->parent.seat, serial);
1384 frame_status_clear(input->output->frame,
1389 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1390 wl_display_terminate(input->compositor->base.wl_display);
1392 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1393 weston_output_schedule_repaint(&input->output->base);
1395 location = THEME_LOCATION_CLIENT_AREA;
1398 if (location == THEME_LOCATION_CLIENT_AREA)
1399 notify_button(&input->base, time, button, state);
1403 input_handle_axis(void *data, struct wl_pointer *pointer,
1404 uint32_t time, uint32_t axis, wl_fixed_t value)
1406 struct wayland_input *input = data;
1408 notify_axis(&input->base, time, axis, value);
1411 static const struct wl_pointer_listener pointer_listener = {
1412 input_handle_pointer_enter,
1413 input_handle_pointer_leave,
1414 input_handle_motion,
1415 input_handle_button,
1420 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1421 int fd, uint32_t size)
1423 struct wayland_input *input = data;
1424 struct xkb_keymap *keymap;
1432 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1433 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1434 if (map_str == MAP_FAILED) {
1435 weston_log("mmap failed: %m\n");
1439 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1441 XKB_KEYMAP_FORMAT_TEXT_V1,
1443 munmap(map_str, size);
1446 weston_log("failed to compile keymap\n");
1450 input->keyboard_state_update = STATE_UPDATE_NONE;
1451 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1452 weston_log("No keymap provided; falling back to defalt\n");
1454 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1456 weston_log("Invalid keymap\n");
1462 if (input->base.keyboard)
1463 weston_seat_update_keymap(&input->base, keymap);
1465 weston_seat_init_keyboard(&input->base, keymap);
1467 xkb_keymap_unref(keymap);
1472 wl_keyboard_release(input->parent.keyboard);
1477 input_handle_keyboard_enter(void *data,
1478 struct wl_keyboard *keyboard,
1480 struct wl_surface *surface,
1481 struct wl_array *keys)
1483 struct wayland_input *input = data;
1484 struct wayland_output *focus;
1486 focus = input->keyboard_focus;
1488 /* This shouldn't happen */
1489 focus->keyboard_count--;
1490 if (!focus->keyboard_count && focus->frame)
1491 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1492 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1493 weston_output_schedule_repaint(&focus->base);
1496 input->keyboard_focus = wl_surface_get_user_data(surface);
1497 input->keyboard_focus->keyboard_count++;
1499 focus = input->keyboard_focus;
1501 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1502 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1503 weston_output_schedule_repaint(&focus->base);
1507 /* XXX: If we get a modifier event immediately before the focus,
1508 * we should try to keep the same serial. */
1509 notify_keyboard_focus_in(&input->base, keys,
1510 STATE_UPDATE_AUTOMATIC);
1514 input_handle_keyboard_leave(void *data,
1515 struct wl_keyboard *keyboard,
1517 struct wl_surface *surface)
1519 struct wayland_input *input = data;
1520 struct wayland_output *focus;
1522 notify_keyboard_focus_out(&input->base);
1524 focus = input->keyboard_focus;
1526 return; /* This shouldn't happen */
1528 focus->keyboard_count--;
1529 if (!focus->keyboard_count && focus->frame) {
1530 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1531 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1532 weston_output_schedule_repaint(&focus->base);
1535 input->keyboard_focus = NULL;
1539 input_handle_key(void *data, struct wl_keyboard *keyboard,
1540 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1542 struct wayland_input *input = data;
1544 input->key_serial = serial;
1545 notify_key(&input->base, time, key,
1546 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
1547 WL_KEYBOARD_KEY_STATE_RELEASED,
1548 input->keyboard_state_update);
1552 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1553 uint32_t serial_in, uint32_t mods_depressed,
1554 uint32_t mods_latched, uint32_t mods_locked,
1557 struct wayland_input *input = data;
1558 struct wayland_compositor *c = input->compositor;
1559 uint32_t serial_out;
1561 /* If we get a key event followed by a modifier event with the
1562 * same serial number, then we try to preserve those semantics by
1563 * reusing the same serial number on the way out too. */
1564 if (serial_in == input->key_serial)
1565 serial_out = wl_display_get_serial(c->base.wl_display);
1567 serial_out = wl_display_next_serial(c->base.wl_display);
1569 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
1570 mods_depressed, mods_latched,
1571 mods_locked, 0, 0, group);
1572 notify_modifiers(&input->base, serial_out);
1576 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1577 int32_t rate, int32_t delay)
1579 struct wayland_input *input = data;
1580 struct wayland_compositor *c = input->compositor;
1582 c->base.kb_repeat_rate = rate;
1583 c->base.kb_repeat_delay = delay;
1586 static const struct wl_keyboard_listener keyboard_listener = {
1587 input_handle_keymap,
1588 input_handle_keyboard_enter,
1589 input_handle_keyboard_leave,
1591 input_handle_modifiers,
1592 input_handle_repeat_info,
1596 input_handle_capabilities(void *data, struct wl_seat *seat,
1597 enum wl_seat_capability caps)
1599 struct wayland_input *input = data;
1601 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1602 input->parent.pointer = wl_seat_get_pointer(seat);
1603 wl_pointer_set_user_data(input->parent.pointer, input);
1604 wl_pointer_add_listener(input->parent.pointer,
1605 &pointer_listener, input);
1606 weston_seat_init_pointer(&input->base);
1607 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1608 wl_pointer_destroy(input->parent.pointer);
1609 input->parent.pointer = NULL;
1612 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1613 input->parent.keyboard = wl_seat_get_keyboard(seat);
1614 wl_keyboard_set_user_data(input->parent.keyboard, input);
1615 wl_keyboard_add_listener(input->parent.keyboard,
1616 &keyboard_listener, input);
1617 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1618 wl_keyboard_destroy(input->parent.keyboard);
1619 input->parent.keyboard = NULL;
1624 input_handle_name(void *data, struct wl_seat *seat,
1629 static const struct wl_seat_listener seat_listener = {
1630 input_handle_capabilities,
1635 display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
1637 struct wayland_input *input;
1639 input = zalloc(sizeof *input);
1643 weston_seat_init(&input->base, &c->base, "default");
1644 input->compositor = c;
1645 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1646 &wl_seat_interface, MIN(version, 4));
1647 wl_list_insert(c->input_list.prev, &input->link);
1649 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1650 wl_seat_set_user_data(input->parent.seat, input);
1652 input->parent.cursor.surface =
1653 wl_compositor_create_surface(c->parent.compositor);
1657 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1658 int32_t x, int32_t y,
1659 int32_t physical_width, int32_t physical_height,
1660 int32_t subpixel, const char *make,
1661 const char *model, int32_t transform)
1663 struct wayland_parent_output *output = data;
1667 output->physical.width = physical_width;
1668 output->physical.height = physical_height;
1669 output->physical.subpixel = subpixel;
1671 free(output->physical.make);
1672 output->physical.make = strdup(make);
1673 free(output->physical.model);
1674 output->physical.model = strdup(model);
1676 output->transform = transform;
1679 static struct weston_mode *
1680 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1682 struct weston_mode *mode;
1684 wl_list_for_each(mode, list, link) {
1685 if (mode->width == width && mode->height == height &&
1686 mode->refresh == refresh)
1690 mode = zalloc(sizeof *mode);
1694 mode->width = width;
1695 mode->height = height;
1696 mode->refresh = refresh;
1697 wl_list_insert(list, &mode->link);
1703 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1704 uint32_t flags, int32_t width, int32_t height,
1707 struct wayland_parent_output *output = data;
1708 struct weston_mode *mode;
1710 if (output->output) {
1711 mode = find_mode(&output->output->base.mode_list,
1712 width, height, refresh);
1715 mode->flags = flags;
1716 /* Do a mode-switch on current mode change? */
1718 mode = find_mode(&output->mode_list, width, height, refresh);
1721 mode->flags = flags;
1722 if (flags & WL_OUTPUT_MODE_CURRENT)
1723 output->current_mode = mode;
1724 if (flags & WL_OUTPUT_MODE_PREFERRED)
1725 output->preferred_mode = mode;
1729 static const struct wl_output_listener output_listener = {
1730 wayland_parent_output_geometry,
1731 wayland_parent_output_mode
1735 wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1737 struct wayland_parent_output *output;
1739 output = zalloc(sizeof *output);
1744 output->global = wl_registry_bind(c->parent.registry, id,
1745 &wl_output_interface, 1);
1746 if (!output->global) {
1751 wl_output_add_listener(output->global, &output_listener, output);
1754 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1755 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1756 wl_list_init(&output->mode_list);
1757 wl_list_insert(&c->parent.output_list, &output->link);
1759 if (c->sprawl_across_outputs) {
1760 wl_display_roundtrip(c->parent.wl_display);
1761 wayland_output_create_for_parent_output(c, output);
1766 wayland_parent_output_destroy(struct wayland_parent_output *output)
1768 struct weston_mode *mode, *next;
1771 wayland_output_destroy(&output->output->base);
1773 wl_output_destroy(output->global);
1774 free(output->physical.make);
1775 free(output->physical.model);
1777 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1778 wl_list_remove(&mode->link);
1784 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1785 const char *interface, uint32_t version)
1787 struct wayland_compositor *c = data;
1789 if (strcmp(interface, "wl_compositor") == 0) {
1790 c->parent.compositor =
1791 wl_registry_bind(registry, name,
1792 &wl_compositor_interface, 1);
1793 } else if (strcmp(interface, "wl_shell") == 0) {
1795 wl_registry_bind(registry, name,
1796 &wl_shell_interface, 1);
1797 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1799 wl_registry_bind(registry, name,
1800 &_wl_fullscreen_shell_interface, 1);
1801 } else if (strcmp(interface, "wl_seat") == 0) {
1802 display_add_seat(c, name, version);
1803 } else if (strcmp(interface, "wl_output") == 0) {
1804 wayland_compositor_register_output(c, name);
1805 } else if (strcmp(interface, "wl_shm") == 0) {
1807 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1812 registry_handle_global_remove(void *data, struct wl_registry *registry,
1815 struct wayland_compositor *c = data;
1816 struct wayland_parent_output *output;
1818 wl_list_for_each(output, &c->parent.output_list, link)
1819 if (output->id == name)
1820 wayland_parent_output_destroy(output);
1823 static const struct wl_registry_listener registry_listener = {
1824 registry_handle_global,
1825 registry_handle_global_remove
1829 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1831 struct wayland_compositor *c = data;
1834 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1835 wl_display_terminate(c->base.wl_display);
1839 if (mask & WL_EVENT_READABLE)
1840 count = wl_display_dispatch(c->parent.wl_display);
1841 if (mask & WL_EVENT_WRITABLE)
1842 wl_display_flush(c->parent.wl_display);
1845 count = wl_display_dispatch_pending(c->parent.wl_display);
1846 wl_display_flush(c->parent.wl_display);
1853 wayland_restore(struct weston_compositor *ec)
1858 wayland_destroy(struct weston_compositor *ec)
1860 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1862 weston_compositor_shutdown(ec);
1865 wl_shm_destroy(c->parent.shm);
1870 static const char *left_ptrs[] = {
1878 create_cursor(struct wayland_compositor *c, struct weston_config *config)
1880 struct weston_config_section *s;
1885 s = weston_config_get_section(config, "shell", NULL, NULL);
1886 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1887 weston_config_section_get_int(s, "cursor-size", &size, 32);
1889 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1890 if (!c->cursor_theme) {
1891 fprintf(stderr, "could not load cursor theme\n");
1898 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1899 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1902 fprintf(stderr, "could not load left cursor\n");
1908 fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1911 struct wayland_compositor *c = data;
1912 struct wayland_input *input = NULL;
1914 wl_list_for_each(input, &c->input_list, link)
1915 if (&input->base == seat_base)
1918 if (!input || !input->output)
1921 if (input->output->frame)
1922 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1924 wayland_output_set_windowed(input->output);
1926 weston_output_schedule_repaint(&input->output->base);
1929 static struct wayland_compositor *
1930 wayland_compositor_create(struct wl_display *display, int use_pixman,
1931 const char *display_name, int *argc, char *argv[],
1932 struct weston_config *config)
1934 struct wayland_compositor *c;
1935 struct wl_event_loop *loop;
1938 c = zalloc(sizeof *c);
1942 if (weston_compositor_init(&c->base, display, argc, argv,
1946 c->parent.wl_display = wl_display_connect(display_name);
1948 if (c->parent.wl_display == NULL) {
1949 weston_log("failed to create display: %m\n");
1950 goto err_compositor;
1953 wl_list_init(&c->parent.output_list);
1954 wl_list_init(&c->input_list);
1955 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1956 wl_registry_add_listener(c->parent.registry, ®istry_listener, c);
1957 wl_display_roundtrip(c->parent.wl_display);
1959 create_cursor(c, config);
1961 c->base.wl_display = display;
1963 c->use_pixman = use_pixman;
1965 if (!c->use_pixman) {
1966 gl_renderer = weston_load_module("gl-renderer.so",
1967 "gl_renderer_interface");
1972 if (!c->use_pixman) {
1973 if (gl_renderer->create(&c->base, c->parent.wl_display,
1974 gl_renderer->alpha_attribs,
1976 weston_log("Failed to initialize the GL renderer; "
1977 "falling back to pixman.\n");
1982 if (c->use_pixman) {
1983 if (pixman_renderer_init(&c->base) < 0) {
1984 weston_log("Failed to initialize pixman renderer\n");
1989 c->base.destroy = wayland_destroy;
1990 c->base.restore = wayland_restore;
1992 loop = wl_display_get_event_loop(c->base.wl_display);
1994 fd = wl_display_get_fd(c->parent.wl_display);
1995 c->parent.wl_source =
1996 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1997 wayland_compositor_handle_event, c);
1998 if (c->parent.wl_source == NULL)
2001 wl_event_source_check(c->parent.wl_source);
2005 c->base.renderer->destroy(&c->base);
2007 wl_display_disconnect(c->parent.wl_display);
2009 weston_compositor_shutdown(&c->base);
2016 wayland_compositor_destroy(struct wayland_compositor *c)
2018 struct weston_output *output, *next;
2020 wl_list_for_each_safe(output, next, &c->base.output_list, link)
2021 wayland_output_destroy(output);
2023 c->base.renderer->destroy(&c->base);
2024 wl_display_disconnect(c->parent.wl_display);
2027 theme_destroy(c->theme);
2028 if (c->frame_device)
2029 cairo_device_destroy(c->frame_device);
2030 wl_cursor_theme_destroy(c->cursor_theme);
2032 weston_compositor_shutdown(&c->base);
2036 WL_EXPORT struct weston_compositor *
2037 backend_init(struct wl_display *display, int *argc, char *argv[],
2038 struct weston_config *config)
2040 struct wayland_compositor *c;
2041 struct wayland_output *output;
2042 struct wayland_parent_output *poutput;
2043 struct weston_config_section *section;
2044 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
2045 const char *section_name, *display_name;
2048 const struct weston_option wayland_options[] = {
2049 { WESTON_OPTION_INTEGER, "width", 0, &width },
2050 { WESTON_OPTION_INTEGER, "height", 0, &height },
2051 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
2052 { WESTON_OPTION_STRING, "display", 0, &display_name },
2053 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2054 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
2055 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
2056 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
2062 display_name = NULL;
2067 parse_options(wayland_options,
2068 ARRAY_LENGTH(wayland_options), argc, argv);
2070 c = wayland_compositor_create(display, use_pixman, display_name,
2071 argc, argv, config);
2075 if (sprawl || c->parent.fshell) {
2076 c->sprawl_across_outputs = 1;
2077 wl_display_roundtrip(c->parent.wl_display);
2079 wl_list_for_each(poutput, &c->parent.output_list, link)
2080 wayland_output_create_for_parent_output(c, poutput);
2086 output = wayland_output_create(c, 0, 0, width, height,
2091 wayland_output_set_fullscreen(output, 0, 0, NULL);
2097 while (weston_config_next_section(config, §ion, §ion_name)) {
2098 if (!section_name || strcmp(section_name, "output") != 0)
2100 weston_config_section_get_string(section, "name", &name, NULL);
2104 if (name[0] != 'W' || name[1] != 'L') {
2110 output = wayland_output_create_for_config(c, section, width,
2111 height, scale, x, 0);
2114 if (wayland_output_set_windowed(output))
2117 x += output->base.width;
2128 output = wayland_output_create(c, x, 0, width, height,
2132 if (wayland_output_set_windowed(output))
2139 weston_compositor_add_key_binding(&c->base, KEY_F,
2140 MODIFIER_CTRL | MODIFIER_ALT,
2141 fullscreen_binding, c);
2146 wayland_compositor_destroy(c);