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;
72 cairo_device_t *frame_device;
73 struct wl_cursor_theme *cursor_theme;
74 struct wl_cursor *cursor;
76 struct wl_list input_list;
79 struct wayland_output {
80 struct weston_output base;
83 int draw_initial_frame;
84 struct wl_surface *surface;
86 struct wl_output *output;
89 struct wl_shell_surface *shell_surface;
90 int configure_width, configure_height;
99 struct wl_egl_window *egl_window;
101 cairo_surface_t *top;
102 cairo_surface_t *left;
103 cairo_surface_t *right;
104 cairo_surface_t *bottom;
109 struct wl_list buffers;
110 struct wl_list free_buffers;
113 struct weston_mode mode;
117 struct wayland_parent_output {
118 struct wayland_output *output;
119 struct wayland_compositor *c;
122 struct wl_output *global;
128 int32_t width, height;
136 struct wl_list mode_list;
137 struct weston_mode *preferred_mode;
138 struct weston_mode *current_mode;
142 struct wayland_shm_buffer {
143 struct wayland_output *output;
146 struct wl_list free_link;
148 struct wl_buffer *buffer;
151 pixman_region32_t damage;
154 pixman_image_t *pm_image;
155 cairo_surface_t *c_surface;
158 struct wayland_input {
159 struct weston_seat base;
160 struct wayland_compositor *compositor;
164 struct wl_seat *seat;
165 struct wl_pointer *pointer;
166 struct wl_keyboard *keyboard;
167 struct wl_touch *touch;
170 struct wl_surface *surface;
175 enum weston_key_state_update keyboard_state_update;
177 uint32_t enter_serial;
179 struct wayland_output *output;
180 struct wayland_output *keyboard_focus;
182 enum wl_seat_capability caps;
184 int caps_update_required;
187 struct gl_renderer_interface *gl_renderer;
190 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
192 cairo_surface_destroy(buffer->c_surface);
193 pixman_image_unref(buffer->pm_image);
195 wl_buffer_destroy(buffer->buffer);
196 munmap(buffer->data, buffer->size);
198 pixman_region32_fini(&buffer->damage);
200 wl_list_remove(&buffer->link);
201 wl_list_remove(&buffer->free_link);
206 buffer_release(void *data, struct wl_buffer *buffer)
208 struct wayland_shm_buffer *sb = data;
211 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
213 wayland_shm_buffer_destroy(sb);
217 static const struct wl_buffer_listener buffer_listener = {
221 static struct wayland_shm_buffer *
222 wayland_output_get_shm_buffer(struct wayland_output *output)
224 struct wayland_compositor *c =
225 (struct wayland_compositor *) output->base.compositor;
226 struct wl_shm *shm = c->parent.shm;
227 struct wayland_shm_buffer *sb;
229 struct wl_shm_pool *pool;
230 int width, height, stride;
235 if (!wl_list_empty(&output->shm.free_buffers)) {
236 sb = container_of(output->shm.free_buffers.next,
237 struct wayland_shm_buffer, free_link);
238 wl_list_remove(&sb->free_link);
239 wl_list_init(&sb->free_link);
245 width = frame_width(output->frame);
246 height = frame_height(output->frame);
248 width = output->base.current_mode->width;
249 height = output->base.current_mode->height;
252 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
254 fd = os_create_anonymous_file(height * stride);
256 weston_log("could not create an anonymous file buffer: %m\n");
260 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
261 if (data == MAP_FAILED) {
262 weston_log("could not mmap %d memory for data: %m\n", height * stride);
267 sb = zalloc(sizeof *sb);
269 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
276 wl_list_init(&sb->free_link);
277 wl_list_insert(&output->shm.buffers, &sb->link);
279 pixman_region32_init_rect(&sb->damage, 0, 0,
280 output->base.width, output->base.height);
281 sb->frame_damaged = 1;
284 sb->size = height * stride;
286 pool = wl_shm_create_pool(shm, fd, sb->size);
288 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
291 WL_SHM_FORMAT_ARGB8888);
292 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
293 wl_shm_pool_destroy(pool);
296 memset(data, 0, sb->size);
299 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
300 width, height, stride);
305 frame_interior(output->frame, &fx, &fy, 0, 0);
307 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
308 (uint32_t *)(data + fy * stride) + fx,
315 frame_done(void *data, struct wl_callback *callback, uint32_t time)
317 struct weston_output *output = data;
319 wl_callback_destroy(callback);
320 weston_output_finish_frame(output, time);
323 static const struct wl_callback_listener frame_listener = {
328 draw_initial_frame(struct wayland_output *output)
330 struct wayland_shm_buffer *sb;
332 sb = wayland_output_get_shm_buffer(output);
334 /* If we are rendering with GL, then orphan it so that it gets
335 * destroyed immediately */
336 if (output->gl.egl_window)
339 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
340 wl_surface_damage(output->parent.surface, 0, 0,
341 output->base.current_mode->width,
342 output->base.current_mode->height);
346 wayland_output_update_gl_border(struct wayland_output *output)
348 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
353 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
356 fwidth = frame_width(output->frame);
357 fheight = frame_height(output->frame);
358 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
360 if (!output->gl.border.top)
361 output->gl.border.top =
362 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
364 cr = cairo_create(output->gl.border.top);
365 frame_repaint(output->frame, cr);
367 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
369 cairo_image_surface_get_stride(output->gl.border.top) / 4,
370 cairo_image_surface_get_data(output->gl.border.top));
373 if (!output->gl.border.left)
374 output->gl.border.left =
375 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
377 cr = cairo_create(output->gl.border.left);
378 cairo_translate(cr, 0, -iy);
379 frame_repaint(output->frame, cr);
381 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
383 cairo_image_surface_get_stride(output->gl.border.left) / 4,
384 cairo_image_surface_get_data(output->gl.border.left));
387 if (!output->gl.border.right)
388 output->gl.border.right =
389 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
390 fwidth - (ix + iwidth), 1);
391 cr = cairo_create(output->gl.border.right);
392 cairo_translate(cr, -(iwidth + ix), -iy);
393 frame_repaint(output->frame, cr);
395 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
396 fwidth - (ix + iwidth), 1,
397 cairo_image_surface_get_stride(output->gl.border.right) / 4,
398 cairo_image_surface_get_data(output->gl.border.right));
401 if (!output->gl.border.bottom)
402 output->gl.border.bottom =
403 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
404 fwidth, fheight - (iy + iheight));
405 cr = cairo_create(output->gl.border.bottom);
406 cairo_translate(cr, 0, -(iy + iheight));
407 frame_repaint(output->frame, cr);
409 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
410 fwidth, fheight - (iy + iheight),
411 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
412 cairo_image_surface_get_data(output->gl.border.bottom));
416 wayland_output_start_repaint_loop(struct weston_output *output_base)
418 struct wayland_output *output = (struct wayland_output *) output_base;
419 struct wayland_compositor *wc =
420 (struct wayland_compositor *)output->base.compositor;
421 struct wl_callback *callback;
423 /* If this is the initial frame, we need to attach a buffer so that
424 * the compositor can map the surface and include it in its render
425 * loop. If the surface doesn't end up in the render loop, the frame
426 * callback won't be invoked. The buffer is transparent and of the
427 * same size as the future real output buffer. */
428 if (output->parent.draw_initial_frame) {
429 output->parent.draw_initial_frame = 0;
431 draw_initial_frame(output);
434 callback = wl_surface_frame(output->parent.surface);
435 wl_callback_add_listener(callback, &frame_listener, output);
436 wl_surface_commit(output->parent.surface);
437 wl_display_flush(wc->parent.wl_display);
441 wayland_output_repaint_gl(struct weston_output *output_base,
442 pixman_region32_t *damage)
444 struct wayland_output *output = (struct wayland_output *) output_base;
445 struct weston_compositor *ec = output->base.compositor;
446 struct wl_callback *callback;
448 callback = wl_surface_frame(output->parent.surface);
449 wl_callback_add_listener(callback, &frame_listener, output);
451 wayland_output_update_gl_border(output);
453 ec->renderer->repaint_output(&output->base, damage);
455 pixman_region32_subtract(&ec->primary_plane.damage,
456 &ec->primary_plane.damage, damage);
461 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
463 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
466 if (!buffer->output->frame || !buffer->frame_damaged)
469 cr = cairo_create(buffer->c_surface);
471 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
472 fwidth = frame_width(buffer->output->frame);
473 fheight = frame_height(buffer->output->frame);
475 /* Set the clip so we don't unnecisaraly damage the surface */
476 cairo_move_to(cr, ix, iy);
477 cairo_rel_line_to(cr, iwidth, 0);
478 cairo_rel_line_to(cr, 0, iheight);
479 cairo_rel_line_to(cr, -iwidth, 0);
480 cairo_line_to(cr, ix, iy);
481 cairo_line_to(cr, 0, iy);
482 cairo_line_to(cr, 0, fheight);
483 cairo_line_to(cr, fwidth, fheight);
484 cairo_line_to(cr, fwidth, 0);
485 cairo_line_to(cr, 0, 0);
486 cairo_line_to(cr, 0, iy);
487 cairo_close_path(cr);
490 /* Draw using a pattern so that the final result gets clipped */
491 cairo_push_group(cr);
492 frame_repaint(buffer->output->frame, cr);
493 cairo_pop_group_to_source(cr);
494 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
501 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
503 pixman_region32_t damage;
504 pixman_box32_t *rects;
505 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
508 pixman_region32_init(&damage);
509 weston_transformed_region(sb->output->base.width,
510 sb->output->base.height,
511 sb->output->base.transform,
512 sb->output->base.current_scale,
513 &sb->damage, &damage);
515 if (sb->output->frame) {
516 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
517 fwidth = frame_width(sb->output->frame);
518 fheight = frame_height(sb->output->frame);
520 pixman_region32_translate(&damage, ix, iy);
522 if (sb->frame_damaged) {
523 pixman_region32_union_rect(&damage, &damage,
525 pixman_region32_union_rect(&damage, &damage,
527 pixman_region32_union_rect(&damage, &damage,
529 fwidth - (ix + iwidth), iheight);
530 pixman_region32_union_rect(&damage, &damage,
532 fwidth, fheight - (iy + iheight));
536 rects = pixman_region32_rectangles(&damage, &n);
537 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
538 for (i = 0; i < n; ++i)
539 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
540 rects[i].y1, rects[i].x2 - rects[i].x1,
541 rects[i].y2 - rects[i].y1);
543 if (sb->output->frame)
544 pixman_region32_fini(&damage);
548 wayland_output_repaint_pixman(struct weston_output *output_base,
549 pixman_region32_t *damage)
551 struct wayland_output *output = (struct wayland_output *) output_base;
552 struct wayland_compositor *c =
553 (struct wayland_compositor *)output->base.compositor;
554 struct wl_callback *callback;
555 struct wayland_shm_buffer *sb;
558 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
559 wl_list_for_each(sb, &output->shm.buffers, link)
560 sb->frame_damaged = 1;
563 wl_list_for_each(sb, &output->shm.buffers, link)
564 pixman_region32_union(&sb->damage, &sb->damage, damage);
566 sb = wayland_output_get_shm_buffer(output);
568 wayland_output_update_shm_border(sb);
569 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
570 c->base.renderer->repaint_output(output_base, &sb->damage);
572 wayland_shm_buffer_attach(sb);
574 callback = wl_surface_frame(output->parent.surface);
575 wl_callback_add_listener(callback, &frame_listener, output);
576 wl_surface_commit(output->parent.surface);
577 wl_display_flush(c->parent.wl_display);
579 pixman_region32_fini(&sb->damage);
580 pixman_region32_init(&sb->damage);
581 sb->frame_damaged = 0;
583 pixman_region32_subtract(&c->base.primary_plane.damage,
584 &c->base.primary_plane.damage, damage);
589 wayland_output_destroy(struct weston_output *output_base)
591 struct wayland_output *output = (struct wayland_output *) output_base;
592 struct wayland_compositor *c =
593 (struct wayland_compositor *) output->base.compositor;
596 pixman_renderer_output_destroy(output_base);
598 gl_renderer->output_destroy(output_base);
601 wl_egl_window_destroy(output->gl.egl_window);
602 wl_surface_destroy(output->parent.surface);
603 if (output->parent.shell_surface)
604 wl_shell_surface_destroy(output->parent.shell_surface);
607 frame_destroy(output->frame);
610 cairo_surface_destroy(output->gl.border.top);
611 cairo_surface_destroy(output->gl.border.left);
612 cairo_surface_destroy(output->gl.border.right);
613 cairo_surface_destroy(output->gl.border.bottom);
615 weston_output_destroy(&output->base);
621 static const struct wl_shell_surface_listener shell_surface_listener;
624 wayland_output_init_gl_renderer(struct wayland_output *output)
626 int32_t fwidth = 0, fheight = 0;
629 fwidth = frame_width(output->frame);
630 fheight = frame_height(output->frame);
632 fwidth = output->base.current_mode->width;
633 fheight = output->base.current_mode->height;
636 output->gl.egl_window =
637 wl_egl_window_create(output->parent.surface,
639 if (!output->gl.egl_window) {
640 weston_log("failure to create wl_egl_window\n");
644 if (gl_renderer->output_create(&output->base,
645 output->gl.egl_window,
646 gl_renderer->alpha_attribs,
653 wl_egl_window_destroy(output->gl.egl_window);
658 wayland_output_init_pixman_renderer(struct wayland_output *output)
660 return pixman_renderer_output_create(&output->base);
664 wayland_output_resize_surface(struct wayland_output *output)
666 struct wayland_compositor *c =
667 (struct wayland_compositor *)output->base.compositor;
668 struct wayland_shm_buffer *buffer, *next;
669 int32_t ix, iy, iwidth, iheight;
670 int32_t width, height;
671 struct wl_region *region;
673 width = output->base.current_mode->width;
674 height = output->base.current_mode->height;
677 frame_resize_inside(output->frame, width, height);
679 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
680 region = wl_compositor_create_region(c->parent.compositor);
681 wl_region_add(region, ix, iy, iwidth, iheight);
682 wl_surface_set_input_region(output->parent.surface, region);
683 wl_region_destroy(region);
685 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
686 region = wl_compositor_create_region(c->parent.compositor);
687 wl_region_add(region, ix, iy, iwidth, iheight);
688 wl_surface_set_opaque_region(output->parent.surface, region);
689 wl_region_destroy(region);
691 width = frame_width(output->frame);
692 height = frame_height(output->frame);
694 region = wl_compositor_create_region(c->parent.compositor);
695 wl_region_add(region, 0, 0, width, height);
696 wl_surface_set_input_region(output->parent.surface, region);
697 wl_region_destroy(region);
699 region = wl_compositor_create_region(c->parent.compositor);
700 wl_region_add(region, 0, 0, width, height);
701 wl_surface_set_opaque_region(output->parent.surface, region);
702 wl_region_destroy(region);
705 if (output->gl.egl_window) {
706 wl_egl_window_resize(output->gl.egl_window,
707 width, height, 0, 0);
709 /* These will need to be re-created due to the resize */
710 gl_renderer->output_set_border(&output->base,
711 GL_RENDERER_BORDER_TOP,
713 cairo_surface_destroy(output->gl.border.top);
714 output->gl.border.top = NULL;
715 gl_renderer->output_set_border(&output->base,
716 GL_RENDERER_BORDER_LEFT,
718 cairo_surface_destroy(output->gl.border.left);
719 output->gl.border.left = NULL;
720 gl_renderer->output_set_border(&output->base,
721 GL_RENDERER_BORDER_RIGHT,
723 cairo_surface_destroy(output->gl.border.right);
724 output->gl.border.right = NULL;
725 gl_renderer->output_set_border(&output->base,
726 GL_RENDERER_BORDER_BOTTOM,
728 cairo_surface_destroy(output->gl.border.bottom);
729 output->gl.border.bottom = NULL;
732 /* Throw away any remaining SHM buffers */
733 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
734 wayland_shm_buffer_destroy(buffer);
735 /* These will get thrown away when they get released */
736 wl_list_for_each(buffer, &output->shm.buffers, link)
737 buffer->output = NULL;
741 wayland_output_set_windowed(struct wayland_output *output)
743 struct wayland_compositor *c =
744 (struct wayland_compositor *)output->base.compositor;
752 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
753 title = malloc(tlen + 1);
757 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
759 title = strdup(WINDOW_TITLE);
763 c->theme = theme_create();
769 output->frame = frame_create(c->theme, 100, 100,
770 FRAME_BUTTON_CLOSE, title);
775 if (output->keyboard_count)
776 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
778 wayland_output_resize_surface(output);
780 wl_shell_surface_set_toplevel(output->parent.shell_surface);
786 wayland_output_set_fullscreen(struct wayland_output *output,
787 enum wl_shell_surface_fullscreen_method method,
788 uint32_t framerate, struct wl_output *target)
790 struct wayland_compositor *c =
791 (struct wayland_compositor *)output->base.compositor;
794 frame_destroy(output->frame);
795 output->frame = NULL;
798 wayland_output_resize_surface(output);
800 if (output->parent.shell_surface) {
801 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
802 method, framerate, target);
803 } else if (c->parent.fshell) {
804 _wl_fullscreen_shell_present_surface(c->parent.fshell,
805 output->parent.surface,
810 static struct weston_mode *
811 wayland_output_choose_mode(struct wayland_output *output,
812 struct weston_mode *ref_mode)
814 struct weston_mode *mode;
816 /* First look for an exact match */
817 wl_list_for_each(mode, &output->base.mode_list, link)
818 if (mode->width == ref_mode->width &&
819 mode->height == ref_mode->height &&
820 mode->refresh == ref_mode->refresh)
823 /* If we can't find an exact match, ignore refresh and try again */
824 wl_list_for_each(mode, &output->base.mode_list, link)
825 if (mode->width == ref_mode->width &&
826 mode->height == ref_mode->height)
829 /* Yeah, we failed */
841 mode_feedback_successful(void *data,
842 struct _wl_fullscreen_shell_mode_feedback *fb)
844 enum mode_status *value = data;
846 printf("Mode switch successful\n");
848 *value = MODE_STATUS_SUCCESS;
852 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
854 enum mode_status *value = data;
856 printf("Mode switch failed\n");
858 *value = MODE_STATUS_FAIL;
862 mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
864 enum mode_status *value = data;
866 printf("Mode switch cancelled\n");
868 *value = MODE_STATUS_CANCEL;
871 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
872 mode_feedback_successful,
873 mode_feedback_failed,
874 mode_feedback_cancelled,
878 wayland_output_switch_mode(struct weston_output *output_base,
879 struct weston_mode *mode)
881 struct wayland_output *output = (struct wayland_output *) output_base;
882 struct wayland_compositor *c;
883 struct wl_surface *old_surface;
884 struct weston_mode *old_mode;
885 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
886 enum mode_status mode_status;
889 if (output_base == NULL) {
890 weston_log("output is NULL.\n");
895 weston_log("mode is NULL.\n");
899 c = (struct wayland_compositor *)output_base->compositor;
901 if (output->parent.shell_surface || !c->parent.fshell)
904 mode = wayland_output_choose_mode(output, mode);
908 if (output->base.current_mode == mode)
911 old_mode = output->base.current_mode;
912 old_surface = output->parent.surface;
913 output->base.current_mode = mode;
914 output->parent.surface =
915 wl_compositor_create_surface(c->parent.compositor);
916 wl_surface_set_user_data(output->parent.surface, output);
918 /* Blow the old buffers because we changed size/surfaces */
919 wayland_output_resize_surface(output);
922 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
923 output->parent.surface,
924 output->parent.output,
926 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
927 &mode_feedback_listener,
930 /* This should kick-start things again */
931 output->parent.draw_initial_frame = 1;
932 wayland_output_start_repaint_loop(&output->base);
934 mode_status = MODE_STATUS_UNKNOWN;
935 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
936 ret = wl_display_dispatch(c->parent.wl_display);
938 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
940 if (mode_status == MODE_STATUS_FAIL) {
941 output->base.current_mode = old_mode;
942 wl_surface_destroy(output->parent.surface);
943 output->parent.surface = old_surface;
944 wayland_output_resize_surface(output);
949 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
950 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
953 pixman_renderer_output_destroy(output_base);
954 if (wayland_output_init_pixman_renderer(output) < 0)
957 gl_renderer->output_destroy(output_base);
958 wl_egl_window_destroy(output->gl.egl_window);
959 if (wayland_output_init_gl_renderer(output) < 0)
962 wl_surface_destroy(old_surface);
964 weston_output_schedule_repaint(&output->base);
973 static struct wayland_output *
974 wayland_output_create(struct wayland_compositor *c, int x, int y,
975 int width, int height, const char *name, int fullscreen,
976 uint32_t transform, int32_t scale)
978 struct wayland_output *output;
979 int output_width, output_height;
981 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
982 width, height, x, y);
984 output = zalloc(sizeof *output);
988 output->name = name ? strdup(name) : NULL;
989 output->base.make = "waywayland";
990 output->base.model = "none";
992 output_width = width * scale;
993 output_height = height * scale;
995 output->parent.surface =
996 wl_compositor_create_surface(c->parent.compositor);
997 if (!output->parent.surface)
999 wl_surface_set_user_data(output->parent.surface, output);
1001 output->parent.draw_initial_frame = 1;
1003 if (c->parent.shell) {
1004 output->parent.shell_surface =
1005 wl_shell_get_shell_surface(c->parent.shell,
1006 output->parent.surface);
1007 if (!output->parent.shell_surface)
1009 wl_shell_surface_add_listener(output->parent.shell_surface,
1010 &shell_surface_listener, output);
1013 if (fullscreen && c->parent.shell) {
1014 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1016 wl_display_roundtrip(c->parent.wl_display);
1018 output_width = output->parent.configure_width;
1020 output_height = output->parent.configure_height;
1023 output->mode.flags =
1024 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1025 output->mode.width = output_width;
1026 output->mode.height = output_height;
1027 output->mode.refresh = 60000;
1028 output->scale = scale;
1029 wl_list_init(&output->base.mode_list);
1030 wl_list_insert(&output->base.mode_list, &output->mode.link);
1031 output->base.current_mode = &output->mode;
1033 wl_list_init(&output->shm.buffers);
1034 wl_list_init(&output->shm.free_buffers);
1036 weston_output_init(&output->base, &c->base, x, y, width, height,
1039 if (c->use_pixman) {
1040 if (wayland_output_init_pixman_renderer(output) < 0)
1042 output->base.repaint = wayland_output_repaint_pixman;
1044 if (wayland_output_init_gl_renderer(output) < 0)
1046 output->base.repaint = wayland_output_repaint_gl;
1049 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1050 output->base.destroy = wayland_output_destroy;
1051 output->base.assign_planes = NULL;
1052 output->base.set_backlight = NULL;
1053 output->base.set_dpms = NULL;
1054 output->base.switch_mode = wayland_output_switch_mode;
1056 wl_list_insert(c->base.output_list.prev, &output->base.link);
1061 weston_output_destroy(&output->base);
1062 if (output->parent.shell_surface)
1063 wl_shell_surface_destroy(output->parent.shell_surface);
1065 wl_surface_destroy(output->parent.surface);
1069 /* FIXME: cleanup weston_output */
1075 static struct wayland_output *
1076 wayland_output_create_for_config(struct wayland_compositor *c,
1077 struct weston_config_section *config_section,
1078 int option_width, int option_height,
1079 int option_scale, int32_t x, int32_t y)
1081 struct wayland_output *output;
1082 char *mode, *t, *name, *str;
1083 int width, height, scale;
1085 unsigned int i, slen;
1087 static const struct { const char *name; uint32_t token; } transform_names[] = {
1088 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1089 { "90", WL_OUTPUT_TRANSFORM_90 },
1090 { "180", WL_OUTPUT_TRANSFORM_180 },
1091 { "270", WL_OUTPUT_TRANSFORM_270 },
1092 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1093 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1094 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1095 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1098 weston_config_section_get_string(config_section, "name", &name, NULL);
1100 slen = strlen(name);
1101 slen += strlen(WINDOW_TITLE " - ");
1102 str = malloc(slen + 1);
1104 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1109 name = strdup(WINDOW_TITLE);
1111 weston_config_section_get_string(config_section,
1112 "mode", &mode, "1024x600");
1113 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1114 weston_log("Invalid mode \"%s\" for output %s\n",
1122 width = option_width;
1124 height = option_height;
1126 weston_config_section_get_int(config_section, "scale", &scale, 1);
1129 scale = option_scale;
1131 weston_config_section_get_string(config_section,
1132 "transform", &t, "normal");
1133 transform = WL_OUTPUT_TRANSFORM_NORMAL;
1134 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
1135 if (strcmp(transform_names[i].name, t) == 0) {
1136 transform = transform_names[i].token;
1140 if (i >= ARRAY_LENGTH(transform_names))
1141 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
1144 output = wayland_output_create(c, x, y, width, height, name, 0,
1151 static struct wayland_output *
1152 wayland_output_create_for_parent_output(struct wayland_compositor *c,
1153 struct wayland_parent_output *poutput)
1155 struct wayland_output *output;
1156 struct weston_mode *mode;
1159 if(poutput->output) {
1160 weston_log("parent output already exists\n");
1161 return poutput->output;
1164 if (poutput->current_mode) {
1165 mode = poutput->current_mode;
1166 } else if (poutput->preferred_mode) {
1167 mode = poutput->preferred_mode;
1168 } else if (!wl_list_empty(&poutput->mode_list)) {
1169 mode = container_of(poutput->mode_list.next,
1170 struct weston_mode, link);
1172 weston_log("No valid modes found. Skipping output\n");
1176 if (!wl_list_empty(&c->base.output_list)) {
1177 output = container_of(c->base.output_list.prev,
1178 struct wayland_output, base.link);
1179 x = output->base.x + output->base.current_mode->width;
1184 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1186 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1190 poutput->output = output;
1191 output->parent.output = poutput->global;
1193 output->base.make = poutput->physical.make;
1194 output->base.model = poutput->physical.model;
1195 wl_list_init(&output->base.mode_list);
1196 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1197 wl_list_init(&poutput->mode_list);
1199 wayland_output_set_fullscreen(output,
1200 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1201 mode->refresh, poutput->global);
1203 if (output->parent.shell_surface) {
1204 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1205 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1206 mode->refresh, poutput->global);
1207 } else if (c->parent.fshell) {
1208 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1209 output->parent.surface,
1210 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1212 _wl_fullscreen_shell_mode_feedback_destroy(
1213 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1214 output->parent.surface,
1223 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1226 wl_shell_surface_pong(shell_surface, serial);
1230 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1231 uint32_t edges, int32_t width, int32_t height)
1233 struct wayland_output *output = data;
1235 output->parent.configure_width = width;
1236 output->parent.configure_height = height;
1238 /* FIXME: implement resizing */
1242 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1246 static const struct wl_shell_surface_listener shell_surface_listener = {
1248 shell_surface_configure,
1249 shell_surface_popup_done
1252 /* Events received from the wayland-server this compositor is client of: */
1254 /* parent input interface */
1256 input_set_cursor(struct wayland_input *input)
1259 struct wl_buffer *buffer;
1260 struct wl_cursor_image *image;
1262 if (!input->compositor->cursor)
1263 return; /* Couldn't load the cursor. Can't set it */
1265 image = input->compositor->cursor->images[0];
1266 buffer = wl_cursor_image_get_buffer(image);
1270 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1271 input->parent.cursor.surface,
1272 image->hotspot_x, image->hotspot_y);
1274 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1275 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1276 image->width, image->height);
1277 wl_surface_commit(input->parent.cursor.surface);
1281 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1282 uint32_t serial, struct wl_surface *surface,
1283 wl_fixed_t x, wl_fixed_t y)
1285 struct wayland_input *input = data;
1287 enum theme_location location;
1289 /* XXX: If we get a modifier event immediately before the focus,
1290 * we should try to keep the same serial. */
1291 input->enter_serial = serial;
1292 input->output = wl_surface_get_user_data(surface);
1294 if (input->output->frame) {
1295 location = frame_pointer_enter(input->output->frame, input,
1297 wl_fixed_to_int(y));
1298 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1299 x -= wl_fixed_from_int(fx);
1300 y -= wl_fixed_from_int(fy);
1302 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1303 weston_output_schedule_repaint(&input->output->base);
1305 location = THEME_LOCATION_CLIENT_AREA;
1308 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1310 if (location == THEME_LOCATION_CLIENT_AREA) {
1312 notify_pointer_focus(&input->base, &input->output->base, x, y);
1313 wl_pointer_set_cursor(input->parent.pointer,
1314 input->enter_serial, NULL, 0, 0);
1317 notify_pointer_focus(&input->base, NULL, 0, 0);
1318 input_set_cursor(input);
1323 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1324 uint32_t serial, struct wl_surface *surface)
1326 struct wayland_input *input = data;
1328 if (input->output->frame) {
1329 frame_pointer_leave(input->output->frame, input);
1331 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1332 weston_output_schedule_repaint(&input->output->base);
1335 notify_pointer_focus(&input->base, NULL, 0, 0);
1336 input->output = NULL;
1341 input_handle_motion(void *data, struct wl_pointer *pointer,
1342 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1344 struct wayland_input *input = data;
1346 enum theme_location location;
1348 if (input->output->frame) {
1349 location = frame_pointer_motion(input->output->frame, input,
1351 wl_fixed_to_int(y));
1352 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1353 x -= wl_fixed_from_int(fx);
1354 y -= wl_fixed_from_int(fy);
1356 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1357 weston_output_schedule_repaint(&input->output->base);
1359 location = THEME_LOCATION_CLIENT_AREA;
1362 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1364 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1365 input_set_cursor(input);
1366 notify_pointer_focus(&input->base, NULL, 0, 0);
1368 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1369 wl_pointer_set_cursor(input->parent.pointer,
1370 input->enter_serial, NULL, 0, 0);
1371 notify_pointer_focus(&input->base, &input->output->base, x, y);
1375 if (location == THEME_LOCATION_CLIENT_AREA)
1376 notify_motion_absolute(&input->base, time, x, y);
1380 input_handle_button(void *data, struct wl_pointer *pointer,
1381 uint32_t serial, uint32_t time, uint32_t button,
1384 struct wayland_input *input = data;
1385 enum wl_pointer_button_state state = state_w;
1386 enum frame_button_state fstate;
1387 enum theme_location location;
1389 if (input->output->frame) {
1390 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1391 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1393 location = frame_pointer_button(input->output->frame, input,
1396 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1398 wl_shell_surface_move(input->output->parent.shell_surface,
1399 input->parent.seat, serial);
1400 frame_status_clear(input->output->frame,
1405 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1406 wl_display_terminate(input->compositor->base.wl_display);
1408 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1409 weston_output_schedule_repaint(&input->output->base);
1411 location = THEME_LOCATION_CLIENT_AREA;
1414 if (location == THEME_LOCATION_CLIENT_AREA)
1415 notify_button(&input->base, time, button, state);
1419 input_handle_axis(void *data, struct wl_pointer *pointer,
1420 uint32_t time, uint32_t axis, wl_fixed_t value)
1422 struct wayland_input *input = data;
1424 notify_axis(&input->base, time, axis, value);
1427 static const struct wl_pointer_listener pointer_listener = {
1428 input_handle_pointer_enter,
1429 input_handle_pointer_leave,
1430 input_handle_motion,
1431 input_handle_button,
1436 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1437 int fd, uint32_t size)
1439 struct wayland_input *input = data;
1440 struct xkb_keymap *keymap;
1448 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1449 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1450 if (map_str == MAP_FAILED) {
1451 weston_log("mmap failed: %m\n");
1455 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1457 XKB_KEYMAP_FORMAT_TEXT_V1,
1459 munmap(map_str, size);
1462 weston_log("failed to compile keymap\n");
1466 input->keyboard_state_update = STATE_UPDATE_NONE;
1467 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1468 weston_log("No keymap provided; falling back to defalt\n");
1470 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1472 weston_log("Invalid keymap\n");
1478 if (input->base.keyboard)
1479 weston_seat_update_keymap(&input->base, keymap);
1481 weston_seat_init_keyboard(&input->base, keymap);
1483 xkb_keymap_unref(keymap);
1488 wl_keyboard_release(input->parent.keyboard);
1493 input_handle_keyboard_enter(void *data,
1494 struct wl_keyboard *keyboard,
1496 struct wl_surface *surface,
1497 struct wl_array *keys)
1499 struct wayland_input *input = data;
1500 struct wayland_output *focus;
1502 focus = input->keyboard_focus;
1504 /* This shouldn't happen */
1505 focus->keyboard_count--;
1506 if (!focus->keyboard_count && focus->frame)
1507 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1508 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1509 weston_output_schedule_repaint(&focus->base);
1512 input->keyboard_focus = wl_surface_get_user_data(surface);
1513 input->keyboard_focus->keyboard_count++;
1515 focus = input->keyboard_focus;
1517 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1518 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1519 weston_output_schedule_repaint(&focus->base);
1523 /* XXX: If we get a modifier event immediately before the focus,
1524 * we should try to keep the same serial. */
1525 notify_keyboard_focus_in(&input->base, keys,
1526 STATE_UPDATE_AUTOMATIC);
1530 input_handle_keyboard_leave(void *data,
1531 struct wl_keyboard *keyboard,
1533 struct wl_surface *surface)
1535 struct wayland_input *input = data;
1536 struct wayland_output *focus;
1538 notify_keyboard_focus_out(&input->base);
1540 focus = input->keyboard_focus;
1542 return; /* This shouldn't happen */
1544 focus->keyboard_count--;
1545 if (!focus->keyboard_count && focus->frame) {
1546 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1547 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1548 weston_output_schedule_repaint(&focus->base);
1551 input->keyboard_focus = NULL;
1555 input_handle_key(void *data, struct wl_keyboard *keyboard,
1556 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1558 struct wayland_input *input = data;
1560 input->key_serial = serial;
1561 notify_key(&input->base, time, key,
1562 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
1563 WL_KEYBOARD_KEY_STATE_RELEASED,
1564 input->keyboard_state_update);
1568 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1569 uint32_t serial_in, uint32_t mods_depressed,
1570 uint32_t mods_latched, uint32_t mods_locked,
1573 struct wayland_input *input = data;
1574 struct wayland_compositor *c = input->compositor;
1575 uint32_t serial_out;
1577 /* If we get a key event followed by a modifier event with the
1578 * same serial number, then we try to preserve those semantics by
1579 * reusing the same serial number on the way out too. */
1580 if (serial_in == input->key_serial)
1581 serial_out = wl_display_get_serial(c->base.wl_display);
1583 serial_out = wl_display_next_serial(c->base.wl_display);
1585 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
1586 mods_depressed, mods_latched,
1587 mods_locked, 0, 0, group);
1588 notify_modifiers(&input->base, serial_out);
1592 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1593 int32_t rate, int32_t delay)
1595 struct wayland_input *input = data;
1596 struct wayland_compositor *c = input->compositor;
1598 c->base.kb_repeat_rate = rate;
1599 c->base.kb_repeat_delay = delay;
1602 static const struct wl_keyboard_listener keyboard_listener = {
1603 input_handle_keymap,
1604 input_handle_keyboard_enter,
1605 input_handle_keyboard_leave,
1607 input_handle_modifiers,
1608 input_handle_repeat_info,
1612 input_capabilities_updated(struct wayland_input *input)
1614 if ((input->caps & WL_SEAT_CAPABILITY_POINTER) &&
1615 !input->parent.pointer) {
1616 input->parent.pointer = wl_seat_get_pointer(input->parent.seat);
1617 wl_pointer_set_user_data(input->parent.pointer, input);
1618 wl_pointer_add_listener(input->parent.pointer,
1619 &pointer_listener, input);
1620 weston_seat_init_pointer(&input->base);
1621 } else if (!(input->caps & WL_SEAT_CAPABILITY_POINTER) &&
1622 input->parent.pointer) {
1623 wl_pointer_destroy(input->parent.pointer);
1624 input->parent.pointer = NULL;
1627 if ((input->caps & WL_SEAT_CAPABILITY_KEYBOARD) &&
1628 !input->parent.keyboard) {
1629 input->parent.keyboard = wl_seat_get_keyboard(
1630 input->parent.seat);
1631 wl_keyboard_set_user_data(input->parent.keyboard, input);
1632 wl_keyboard_add_listener(input->parent.keyboard,
1633 &keyboard_listener, input);
1634 } else if (!(input->caps & WL_SEAT_CAPABILITY_KEYBOARD) &&
1635 input->parent.keyboard) {
1636 wl_keyboard_destroy(input->parent.keyboard);
1637 input->parent.keyboard = NULL;
1639 input->caps_update_required = 0;
1643 input_handle_capabilities(void *data, struct wl_seat *seat,
1644 enum wl_seat_capability caps)
1646 struct wayland_input *input = data;
1649 weston_log ("input_handle_capabilities input with seatname %s"
1650 " and caps %d\n", input->base.seat_name, input->caps);
1651 if (input->base.seat_name || !input->compositor->seatfilter) {
1652 input_capabilities_updated (input);
1654 input->caps_update_required = 1;
1659 input_handle_name(void *data, struct wl_seat *seat,
1662 struct wayland_input *input = data;
1663 if (input->compositor->seatfilter &&
1664 (!name || !input->compositor->seatname ||
1665 strcmp(input->compositor->seatname, name) != 0)) {
1666 weston_log("seatname does not match %s:%s; remove "
1667 "input\n", name, input->compositor->seatname);
1668 wl_seat_destroy(input->parent.seat);
1669 wl_list_remove(&input->link);
1673 if (!input->initialized) {
1674 weston_log ("initialise input with seat name %s\n", name);
1675 weston_seat_init (&input->base, &input->compositor->base, name);
1676 input->parent.cursor.surface = wl_compositor_create_surface(
1677 input->compositor->parent.compositor);
1678 input->initialized = 1;
1680 if (input->caps_update_required)
1681 input_capabilities_updated (input);
1684 static const struct wl_seat_listener seat_listener = {
1685 input_handle_capabilities,
1690 display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
1692 struct wayland_input *input;
1694 input = zalloc(sizeof *input);
1698 weston_log ("create input with id %d\n", id);
1699 input->compositor = c;
1701 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1703 version > 2 ? version : 2);
1705 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1706 &wl_seat_interface, MIN(version, 4));
1707 input->base.seat_name = NULL;
1708 input->initialized = 0;
1710 wl_list_insert(c->input_list.prev, &input->link);
1712 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1713 wl_seat_set_user_data(input->parent.seat, input);
1715 if (!input->compositor->seatfilter) {
1716 weston_log ("initialise input with seat name %s\n",
1718 weston_seat_init (&input->base, &input->compositor->base,
1719 c->seatname ? c->seatname : "");
1720 input->parent.cursor.surface = wl_compositor_create_surface(
1721 c->parent.compositor);
1722 input->initialized = 1;
1727 wayland_parent_output_destroy(struct wayland_parent_output *output)
1729 struct weston_mode *mode, *next;
1732 wayland_output_destroy(&output->output->base);
1734 wl_output_destroy(output->global);
1735 free(output->physical.make);
1736 free(output->physical.model);
1737 free(output->seatname);
1739 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1740 wl_list_remove(&mode->link);
1746 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1747 int32_t x, int32_t y,
1748 int32_t physical_width, int32_t physical_height,
1749 int32_t subpixel, const char *make,
1750 const char *model, int32_t transform)
1752 struct wayland_parent_output *output = data;
1756 output->physical.width = physical_width;
1757 output->physical.height = physical_height;
1758 output->physical.subpixel = subpixel;
1760 free(output->physical.make);
1761 output->physical.make = strdup(make);
1762 free(output->physical.model);
1763 output->physical.model = strdup(model);
1765 output->transform = transform;
1768 static struct weston_mode *
1769 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1771 struct weston_mode *mode;
1773 wl_list_for_each(mode, list, link) {
1774 if (mode->width == width && mode->height == height &&
1775 mode->refresh == refresh)
1779 mode = zalloc(sizeof *mode);
1783 mode->width = width;
1784 mode->height = height;
1785 mode->refresh = refresh;
1786 wl_list_insert(list, &mode->link);
1792 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1793 uint32_t flags, int32_t width, int32_t height,
1796 struct wayland_parent_output *output = data;
1797 struct weston_mode *mode;
1799 if (output->output) {
1800 mode = find_mode(&output->output->base.mode_list,
1801 width, height, refresh);
1804 mode->flags = flags;
1805 /* Do a mode-switch on current mode change? */
1807 mode = find_mode(&output->mode_list, width, height, refresh);
1810 mode->flags = flags;
1811 if (flags & WL_OUTPUT_MODE_CURRENT)
1812 output->current_mode = mode;
1813 if (flags & WL_OUTPUT_MODE_PREFERRED)
1814 output->preferred_mode = mode;
1820 wayland_parent_output_done(void *data,
1821 struct wl_output *wl_output)
1823 struct wayland_parent_output *output = data;
1824 if (!output->seatname || !output->c || !output->c->seatname ||
1825 strcmp(output->c->seatname, output->seatname) != 0) {
1826 weston_log("output seatname (%s) doesnt match with"
1827 " compositor seatname. destroying output\n",
1829 wayland_parent_output_destroy (output);
1830 wl_list_remove(&output->link);
1833 if (output->c->sprawl_across_outputs) {
1834 wayland_output_create_for_parent_output(output->c, output);
1839 wayland_parent_output_scale(void *data,
1840 struct wl_output *wl_output,
1846 wayland_parent_output_name(void *data, struct wl_output *wl_output_proxy,
1849 weston_log("output name %s\n", name);
1853 wayland_parent_output_seatname(void *data, struct wl_output *wl_output_proxy,
1856 struct wayland_parent_output *poutput = data;
1857 weston_log("output seatname %s\n", name);
1858 poutput->seatname = strdup(name);
1862 static const struct wl_output_listener output_listener = {
1863 wayland_parent_output_geometry,
1864 wayland_parent_output_mode
1867 wayland_parent_output_done,
1868 wayland_parent_output_scale,
1869 wayland_parent_output_name,
1870 wayland_parent_output_seatname
1875 wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1877 struct wayland_parent_output *output;
1879 output = zalloc(sizeof *output);
1886 output->global = wl_registry_bind(c->parent.registry, id,
1887 &wl_output_interface, 3);
1889 output->global = wl_registry_bind(c->parent.registry, id,
1890 &wl_output_interface, 1);
1891 if (!output->global) {
1897 output->seatname = NULL;
1898 output->output = NULL;
1899 wl_output_add_listener(output->global, &output_listener, output);
1902 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1903 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1904 wl_list_init(&output->mode_list);
1905 wl_list_insert(&c->parent.output_list, &output->link);
1907 if (!c->seatfilter && output->c->sprawl_across_outputs) {
1908 wl_display_roundtrip(output->c->parent.wl_display);
1909 wayland_output_create_for_parent_output(output->c, output);
1914 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1915 const char *interface, uint32_t version)
1917 struct wayland_compositor *c = data;
1919 if (strcmp(interface, "wl_compositor") == 0) {
1920 c->parent.compositor =
1921 wl_registry_bind(registry, name,
1922 &wl_compositor_interface, 1);
1923 } else if (strcmp(interface, "wl_shell") == 0) {
1925 wl_registry_bind(registry, name,
1926 &wl_shell_interface, 1);
1927 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1929 wl_registry_bind(registry, name,
1930 &_wl_fullscreen_shell_interface, 1);
1931 } else if (strcmp(interface, "wl_seat") == 0) {
1932 display_add_seat(c, name, version);
1933 } else if (strcmp(interface, "wl_output") == 0) {
1934 wayland_compositor_register_output(c, name);
1935 } else if (strcmp(interface, "wl_shm") == 0) {
1937 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1942 registry_handle_global_remove(void *data, struct wl_registry *registry,
1945 struct wayland_compositor *c = data;
1946 struct wayland_parent_output *output;
1948 wl_list_for_each(output, &c->parent.output_list, link)
1949 if (output->id == name)
1950 wayland_parent_output_destroy(output);
1953 static const struct wl_registry_listener registry_listener = {
1954 registry_handle_global,
1955 registry_handle_global_remove
1959 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1961 struct wayland_compositor *c = data;
1964 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1965 wl_display_terminate(c->base.wl_display);
1969 if (mask & WL_EVENT_READABLE)
1970 count = wl_display_dispatch(c->parent.wl_display);
1971 if (mask & WL_EVENT_WRITABLE)
1972 wl_display_flush(c->parent.wl_display);
1975 count = wl_display_dispatch_pending(c->parent.wl_display);
1976 wl_display_flush(c->parent.wl_display);
1983 wayland_restore(struct weston_compositor *ec)
1988 wayland_destroy(struct weston_compositor *ec)
1990 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1992 weston_compositor_shutdown(ec);
1995 wl_shm_destroy(c->parent.shm);
2000 static const char *left_ptrs[] = {
2008 create_cursor(struct wayland_compositor *c, struct weston_config *config)
2010 struct weston_config_section *s;
2015 s = weston_config_get_section(config, "shell", NULL, NULL);
2016 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
2017 weston_config_section_get_int(s, "cursor-size", &size, 32);
2019 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
2020 if (!c->cursor_theme) {
2021 fprintf(stderr, "could not load cursor theme\n");
2028 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2029 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
2032 fprintf(stderr, "could not load left cursor\n");
2038 fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
2041 struct wayland_compositor *c = data;
2042 struct wayland_input *input = NULL;
2044 wl_list_for_each(input, &c->input_list, link)
2045 if (&input->base == seat_base)
2048 if (!input || !input->output)
2051 if (input->output->frame)
2052 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2054 wayland_output_set_windowed(input->output);
2056 weston_output_schedule_repaint(&input->output->base);
2059 static struct wayland_compositor *
2060 wayland_compositor_create(struct wl_display *display, int use_pixman,
2061 const char *display_name, int *argc, char *argv[],
2062 struct weston_config *config, const char* seatname,
2065 struct wayland_compositor *c;
2066 struct wl_event_loop *loop;
2069 c = zalloc(sizeof *c);
2072 c->seatname = seatname ? strdup(seatname) : NULL;
2073 c->seatfilter = seatfilter;
2075 if (weston_compositor_init(&c->base, display, argc, argv,
2079 c->parent.wl_display = wl_display_connect(display_name);
2081 if (c->parent.wl_display == NULL) {
2082 weston_log("failed to create display: %m\n");
2083 goto err_compositor;
2086 wl_list_init(&c->parent.output_list);
2087 wl_list_init(&c->input_list);
2088 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
2089 wl_registry_add_listener(c->parent.registry, ®istry_listener, c);
2090 wl_display_roundtrip(c->parent.wl_display);
2092 create_cursor(c, config);
2094 c->base.wl_display = display;
2096 c->use_pixman = use_pixman;
2098 if (!c->use_pixman) {
2099 gl_renderer = weston_load_module("gl-renderer.so",
2100 "gl_renderer_interface");
2105 if (!c->use_pixman) {
2106 if (gl_renderer->create(&c->base, c->parent.wl_display,
2107 gl_renderer->alpha_attribs,
2109 weston_log("Failed to initialize the GL renderer; "
2110 "falling back to pixman.\n");
2115 if (c->use_pixman) {
2116 if (pixman_renderer_init(&c->base) < 0) {
2117 weston_log("Failed to initialize pixman renderer\n");
2122 c->base.destroy = wayland_destroy;
2123 c->base.restore = wayland_restore;
2125 loop = wl_display_get_event_loop(c->base.wl_display);
2127 fd = wl_display_get_fd(c->parent.wl_display);
2128 c->parent.wl_source =
2129 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2130 wayland_compositor_handle_event, c);
2131 if (c->parent.wl_source == NULL)
2134 wl_event_source_check(c->parent.wl_source);
2138 c->base.renderer->destroy(&c->base);
2140 wl_display_disconnect(c->parent.wl_display);
2142 weston_compositor_shutdown(&c->base);
2150 wayland_compositor_destroy(struct wayland_compositor *c)
2152 struct weston_output *output, *next;
2154 wl_list_for_each_safe(output, next, &c->base.output_list, link)
2155 wayland_output_destroy(output);
2157 c->base.renderer->destroy(&c->base);
2158 wl_display_disconnect(c->parent.wl_display);
2161 theme_destroy(c->theme);
2162 if (c->frame_device)
2163 cairo_device_destroy(c->frame_device);
2164 wl_cursor_theme_destroy(c->cursor_theme);
2166 weston_compositor_shutdown(&c->base);
2171 WL_EXPORT struct weston_compositor *
2172 backend_init(struct wl_display *display, int *argc, char *argv[],
2173 struct weston_config *config)
2175 struct wayland_compositor *c;
2176 struct wayland_output *output;
2177 struct wayland_parent_output *poutput;
2178 struct weston_config_section *section;
2179 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
2185 const char *section_name, *display_name, *seatname;
2188 const struct weston_option wayland_options[] = {
2189 { WESTON_OPTION_INTEGER, "width", 0, &width },
2190 { WESTON_OPTION_INTEGER, "height", 0, &height },
2191 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
2192 { WESTON_OPTION_STRING, "display", 0, &display_name },
2193 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2194 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
2195 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
2196 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
2202 display_name = NULL;
2207 parse_options(wayland_options,
2208 ARRAY_LENGTH(wayland_options), argc, argv);
2210 seatname = getenv("XDG_SEAT");
2211 if (!seatname && seatfilter) {
2212 weston_log("unable to determine seat for the compositor from"
2213 "XDG_SEAT env. Disabling seat filter\n");
2217 weston_log("Multiseat enabled %d\n", seatfilter);
2218 c = wayland_compositor_create(display, use_pixman, display_name,
2219 argc, argv, config, seatname, seatfilter);
2223 if (sprawl || c->parent.fshell) {
2224 c->sprawl_across_outputs = 1;
2225 wl_display_roundtrip(c->parent.wl_display);
2226 if (wl_list_empty(&c->parent.output_list))
2229 wl_list_for_each(poutput, &c->parent.output_list, link)
2230 wayland_output_create_for_parent_output(c, poutput);
2236 output = wayland_output_create(c, 0, 0, width, height,
2241 wayland_output_set_fullscreen(output, 0, 0, NULL);
2247 while (weston_config_next_section(config, §ion, §ion_name)) {
2248 if (!section_name || strcmp(section_name, "output") != 0)
2250 weston_config_section_get_string(section, "name", &name, NULL);
2254 if (name[0] != 'W' || name[1] != 'L') {
2260 output = wayland_output_create_for_config(c, section, width,
2261 height, scale, x, 0);
2264 if (wayland_output_set_windowed(output))
2267 x += output->base.width;
2278 output = wayland_output_create(c, x, 0, width, height,
2282 if (wayland_output_set_windowed(output))
2289 weston_compositor_add_key_binding(&c->base, KEY_F,
2290 MODIFIER_CTRL | MODIFIER_ALT,
2291 fullscreen_binding, c);
2296 wayland_compositor_destroy(c);