2 * Copyright © 2010-2011 Benjamin Franzke
3 * Copyright © 2013 Jason Ekstrand
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #include <linux/input.h>
40 #include <wayland-client.h>
41 #include <wayland-cursor.h>
44 #include <wayland-egl.h>
47 #include "compositor.h"
48 #include "compositor-wayland.h"
49 #include "gl-renderer.h"
50 #include "weston-egl-ext.h"
51 #include "pixman-renderer.h"
52 #include "shared/helpers.h"
53 #include "shared/image-loader.h"
54 #include "shared/os-compatibility.h"
55 #include "shared/cairo-util.h"
56 #include "shared/timespec-util.h"
57 #include "fullscreen-shell-unstable-v1-client-protocol.h"
58 #include "xdg-shell-unstable-v6-client-protocol.h"
59 #include "presentation-time-server-protocol.h"
60 #include "linux-dmabuf.h"
61 #include "windowed-output-api.h"
63 #define WINDOW_TITLE "Weston Compositor"
65 struct wayland_backend {
66 struct weston_backend base;
67 struct weston_compositor *compositor;
70 struct wl_display *wl_display;
71 struct wl_registry *registry;
72 struct wl_compositor *compositor;
73 struct wl_shell *shell;
74 struct zxdg_shell_v6 *xdg_shell;
75 struct zwp_fullscreen_shell_v1 *fshell;
78 struct wl_list output_list;
80 struct wl_event_source *wl_source;
85 bool sprawl_across_outputs;
89 cairo_device_t *frame_device;
90 struct wl_cursor_theme *cursor_theme;
91 struct wl_cursor *cursor;
93 struct wl_list input_list;
96 struct wayland_output {
97 struct weston_output base;
100 bool draw_initial_frame;
101 struct wl_surface *surface;
103 struct wl_output *output;
106 struct wl_shell_surface *shell_surface;
107 struct zxdg_surface_v6 *xdg_surface;
108 struct zxdg_toplevel_v6 *xdg_toplevel;
109 int configure_width, configure_height;
110 bool wait_for_configure;
119 struct wl_egl_window *egl_window;
121 cairo_surface_t *top;
122 cairo_surface_t *left;
123 cairo_surface_t *right;
124 cairo_surface_t *bottom;
129 struct wl_list buffers;
130 struct wl_list free_buffers;
133 struct weston_mode mode;
135 struct wl_callback *frame_cb;
138 struct wayland_parent_output {
139 struct wayland_backend *backend; /**< convenience */
140 struct wayland_head *head;
143 struct wl_output *global;
149 int32_t width, height;
157 struct wl_callback *sync_cb; /**< wl_output < 2 done replacement */
159 struct wl_list mode_list;
160 struct weston_mode *preferred_mode;
161 struct weston_mode *current_mode;
164 struct wayland_head {
165 struct weston_head base;
166 struct wayland_parent_output *parent_output;
169 struct wayland_shm_buffer {
170 struct wayland_output *output;
172 struct wl_list free_link;
174 struct wl_buffer *buffer;
177 pixman_region32_t damage; /**< in global coords */
180 pixman_image_t *pm_image;
181 cairo_surface_t *c_surface;
184 struct wayland_input {
185 struct weston_seat base;
186 struct wayland_backend *backend;
190 struct wl_seat *seat;
191 struct wl_pointer *pointer;
192 struct wl_keyboard *keyboard;
193 struct wl_touch *touch;
196 struct wl_surface *surface;
201 enum weston_key_state_update keyboard_state_update;
203 uint32_t enter_serial;
204 uint32_t touch_points;
209 struct wayland_output *output;
210 struct wayland_output *touch_focus;
211 struct wayland_output *keyboard_focus;
213 struct weston_pointer_axis_event vert, horiz;
216 struct gl_renderer_interface *gl_renderer;
218 static inline struct wayland_head *
219 to_wayland_head(struct weston_head *base)
221 return container_of(base, struct wayland_head, base);
224 static inline struct wayland_output *
225 to_wayland_output(struct weston_output *base)
227 return container_of(base, struct wayland_output, base);
230 static inline struct wayland_backend *
231 to_wayland_backend(struct weston_compositor *base)
233 return container_of(base->backend, struct wayland_backend, base);
237 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
239 cairo_surface_destroy(buffer->c_surface);
240 pixman_image_unref(buffer->pm_image);
242 wl_buffer_destroy(buffer->buffer);
243 munmap(buffer->data, buffer->size);
245 pixman_region32_fini(&buffer->damage);
247 wl_list_remove(&buffer->link);
248 wl_list_remove(&buffer->free_link);
253 buffer_release(void *data, struct wl_buffer *buffer)
255 struct wayland_shm_buffer *sb = data;
258 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
260 wayland_shm_buffer_destroy(sb);
264 static const struct wl_buffer_listener buffer_listener = {
268 static struct wayland_shm_buffer *
269 wayland_output_get_shm_buffer(struct wayland_output *output)
271 struct wayland_backend *b =
272 to_wayland_backend(output->base.compositor);
273 struct wl_shm *shm = b->parent.shm;
274 struct wayland_shm_buffer *sb;
276 struct wl_shm_pool *pool;
277 int width, height, stride;
282 if (!wl_list_empty(&output->shm.free_buffers)) {
283 sb = container_of(output->shm.free_buffers.next,
284 struct wayland_shm_buffer, free_link);
285 wl_list_remove(&sb->free_link);
286 wl_list_init(&sb->free_link);
292 width = frame_width(output->frame);
293 height = frame_height(output->frame);
295 width = output->base.current_mode->width;
296 height = output->base.current_mode->height;
299 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
301 fd = os_create_anonymous_file(height * stride);
303 weston_log("could not create an anonymous file buffer: %m\n");
307 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
308 if (data == MAP_FAILED) {
309 weston_log("could not mmap %d memory for data: %m\n", height * stride);
314 sb = zalloc(sizeof *sb);
316 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
318 munmap(data, height * stride);
323 wl_list_init(&sb->free_link);
324 wl_list_insert(&output->shm.buffers, &sb->link);
326 pixman_region32_init(&sb->damage);
327 pixman_region32_copy(&sb->damage, &output->base.region);
328 sb->frame_damaged = 1;
331 sb->size = height * stride;
333 pool = wl_shm_create_pool(shm, fd, sb->size);
335 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
338 WL_SHM_FORMAT_ARGB8888);
339 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
340 wl_shm_pool_destroy(pool);
343 memset(data, 0, sb->size);
346 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
347 width, height, stride);
352 frame_interior(output->frame, &fx, &fy, 0, 0);
354 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
355 (uint32_t *)(data + fy * stride) + fx,
362 frame_done(void *data, struct wl_callback *callback, uint32_t time)
364 struct wayland_output *output = data;
367 assert(callback == output->frame_cb);
368 wl_callback_destroy(callback);
369 output->frame_cb = NULL;
371 /* XXX: use the presentation extension for proper timings */
374 * This is the fallback case, where Presentation extension is not
375 * available from the parent compositor. We do not know the base for
376 * 'time', so we cannot feed it to finish_frame(). Do the only thing
377 * we can, and pretend finish_frame time is when we process this
380 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
381 weston_output_finish_frame(&output->base, &ts, 0);
384 static const struct wl_callback_listener frame_listener = {
389 draw_initial_frame(struct wayland_output *output)
391 struct wayland_shm_buffer *sb;
393 sb = wayland_output_get_shm_buffer(output);
395 /* If we are rendering with GL, then orphan it so that it gets
396 * destroyed immediately */
397 if (output->gl.egl_window)
400 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
401 wl_surface_damage(output->parent.surface, 0, 0,
402 output->base.current_mode->width,
403 output->base.current_mode->height);
408 wayland_output_update_gl_border(struct wayland_output *output)
410 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
415 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
418 fwidth = frame_width(output->frame);
419 fheight = frame_height(output->frame);
420 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
422 if (!output->gl.border.top)
423 output->gl.border.top =
424 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
426 cr = cairo_create(output->gl.border.top);
427 frame_repaint(output->frame, cr);
429 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
431 cairo_image_surface_get_stride(output->gl.border.top) / 4,
432 cairo_image_surface_get_data(output->gl.border.top));
435 if (!output->gl.border.left)
436 output->gl.border.left =
437 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
439 cr = cairo_create(output->gl.border.left);
440 cairo_translate(cr, 0, -iy);
441 frame_repaint(output->frame, cr);
443 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
445 cairo_image_surface_get_stride(output->gl.border.left) / 4,
446 cairo_image_surface_get_data(output->gl.border.left));
449 if (!output->gl.border.right)
450 output->gl.border.right =
451 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
452 fwidth - (ix + iwidth), 1);
453 cr = cairo_create(output->gl.border.right);
454 cairo_translate(cr, -(iwidth + ix), -iy);
455 frame_repaint(output->frame, cr);
457 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
458 fwidth - (ix + iwidth), 1,
459 cairo_image_surface_get_stride(output->gl.border.right) / 4,
460 cairo_image_surface_get_data(output->gl.border.right));
463 if (!output->gl.border.bottom)
464 output->gl.border.bottom =
465 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
466 fwidth, fheight - (iy + iheight));
467 cr = cairo_create(output->gl.border.bottom);
468 cairo_translate(cr, 0, -(iy + iheight));
469 frame_repaint(output->frame, cr);
471 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
472 fwidth, fheight - (iy + iheight),
473 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
474 cairo_image_surface_get_data(output->gl.border.bottom));
479 wayland_output_start_repaint_loop(struct weston_output *output_base)
481 struct wayland_output *output = to_wayland_output(output_base);
482 struct wayland_backend *wb =
483 to_wayland_backend(output->base.compositor);
485 /* If this is the initial frame, we need to attach a buffer so that
486 * the compositor can map the surface and include it in its render
487 * loop. If the surface doesn't end up in the render loop, the frame
488 * callback won't be invoked. The buffer is transparent and of the
489 * same size as the future real output buffer. */
490 if (output->parent.draw_initial_frame) {
491 output->parent.draw_initial_frame = false;
493 draw_initial_frame(output);
496 output->frame_cb = wl_surface_frame(output->parent.surface);
497 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
498 wl_surface_commit(output->parent.surface);
499 wl_display_flush(wb->parent.wl_display);
504 wayland_output_repaint_gl(struct weston_output *output_base,
505 pixman_region32_t *damage,
508 struct wayland_output *output = to_wayland_output(output_base);
509 struct weston_compositor *ec = output->base.compositor;
511 output->frame_cb = wl_surface_frame(output->parent.surface);
512 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
514 wayland_output_update_gl_border(output);
516 ec->renderer->repaint_output(&output->base, damage);
518 pixman_region32_subtract(&ec->primary_plane.damage,
519 &ec->primary_plane.damage, damage);
525 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
527 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
530 if (!buffer->output->frame || !buffer->frame_damaged)
533 cr = cairo_create(buffer->c_surface);
535 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
536 fwidth = frame_width(buffer->output->frame);
537 fheight = frame_height(buffer->output->frame);
539 /* Set the clip so we don't unnecisaraly damage the surface */
540 cairo_move_to(cr, ix, iy);
541 cairo_rel_line_to(cr, iwidth, 0);
542 cairo_rel_line_to(cr, 0, iheight);
543 cairo_rel_line_to(cr, -iwidth, 0);
544 cairo_line_to(cr, ix, iy);
545 cairo_line_to(cr, 0, iy);
546 cairo_line_to(cr, 0, fheight);
547 cairo_line_to(cr, fwidth, fheight);
548 cairo_line_to(cr, fwidth, 0);
549 cairo_line_to(cr, 0, 0);
550 cairo_line_to(cr, 0, iy);
551 cairo_close_path(cr);
554 /* Draw using a pattern so that the final result gets clipped */
555 cairo_push_group(cr);
556 frame_repaint(buffer->output->frame, cr);
557 cairo_pop_group_to_source(cr);
558 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
565 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
567 pixman_region32_t damage;
568 pixman_box32_t *rects;
569 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
572 pixman_region32_init(&damage);
573 pixman_region32_copy(&damage, &sb->damage);
574 pixman_region32_translate(&damage, -sb->output->base.x,
575 -sb->output->base.y);
577 weston_transformed_region(sb->output->base.width,
578 sb->output->base.height,
579 sb->output->base.transform,
580 sb->output->base.current_scale,
583 if (sb->output->frame) {
584 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
585 fwidth = frame_width(sb->output->frame);
586 fheight = frame_height(sb->output->frame);
588 pixman_region32_translate(&damage, ix, iy);
590 if (sb->frame_damaged) {
591 pixman_region32_union_rect(&damage, &damage,
593 pixman_region32_union_rect(&damage, &damage,
595 pixman_region32_union_rect(&damage, &damage,
597 fwidth - (ix + iwidth), iheight);
598 pixman_region32_union_rect(&damage, &damage,
600 fwidth, fheight - (iy + iheight));
604 rects = pixman_region32_rectangles(&damage, &n);
605 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
606 for (i = 0; i < n; ++i)
607 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
608 rects[i].y1, rects[i].x2 - rects[i].x1,
609 rects[i].y2 - rects[i].y1);
611 if (sb->output->frame)
612 pixman_region32_fini(&damage);
616 wayland_output_repaint_pixman(struct weston_output *output_base,
617 pixman_region32_t *damage,
620 struct wayland_output *output = to_wayland_output(output_base);
621 struct wayland_backend *b =
622 to_wayland_backend(output->base.compositor);
623 struct wayland_shm_buffer *sb;
626 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
627 wl_list_for_each(sb, &output->shm.buffers, link)
628 sb->frame_damaged = 1;
631 wl_list_for_each(sb, &output->shm.buffers, link)
632 pixman_region32_union(&sb->damage, &sb->damage, damage);
634 sb = wayland_output_get_shm_buffer(output);
636 wayland_output_update_shm_border(sb);
637 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
638 b->compositor->renderer->repaint_output(output_base, &sb->damage);
640 wayland_shm_buffer_attach(sb);
642 output->frame_cb = wl_surface_frame(output->parent.surface);
643 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
644 wl_surface_commit(output->parent.surface);
645 wl_display_flush(b->parent.wl_display);
647 pixman_region32_fini(&sb->damage);
648 pixman_region32_init(&sb->damage);
649 sb->frame_damaged = 0;
651 pixman_region32_subtract(&b->compositor->primary_plane.damage,
652 &b->compositor->primary_plane.damage, damage);
657 wayland_backend_destroy_output_surface(struct wayland_output *output)
659 assert(output->parent.surface);
661 if (output->parent.xdg_toplevel) {
662 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
663 output->parent.xdg_toplevel = NULL;
666 if (output->parent.xdg_surface) {
667 zxdg_surface_v6_destroy(output->parent.xdg_surface);
668 output->parent.xdg_surface = NULL;
671 if (output->parent.shell_surface) {
672 wl_shell_surface_destroy(output->parent.shell_surface);
673 output->parent.shell_surface = NULL;
676 wl_surface_destroy(output->parent.surface);
677 output->parent.surface = NULL;
681 wayland_output_destroy_shm_buffers(struct wayland_output *output)
683 struct wayland_shm_buffer *buffer, *next;
685 /* Throw away any remaining SHM buffers */
686 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
687 wayland_shm_buffer_destroy(buffer);
688 /* These will get thrown away when they get released */
689 wl_list_for_each(buffer, &output->shm.buffers, link)
690 buffer->output = NULL;
694 wayland_output_disable(struct weston_output *base)
696 struct wayland_output *output = to_wayland_output(base);
697 struct wayland_backend *b = to_wayland_backend(base->compositor);
699 if (!output->base.enabled)
703 pixman_renderer_output_destroy(&output->base);
706 gl_renderer->output_destroy(&output->base);
707 wl_egl_window_destroy(output->gl.egl_window);
711 wayland_output_destroy_shm_buffers(output);
713 wayland_backend_destroy_output_surface(output);
716 frame_destroy(output->frame);
718 cairo_surface_destroy(output->gl.border.top);
719 cairo_surface_destroy(output->gl.border.left);
720 cairo_surface_destroy(output->gl.border.right);
721 cairo_surface_destroy(output->gl.border.bottom);
727 wayland_output_destroy(struct weston_output *base)
729 struct wayland_output *output = to_wayland_output(base);
731 wayland_output_disable(&output->base);
733 weston_output_release(&output->base);
735 if (output->frame_cb)
736 wl_callback_destroy(output->frame_cb);
742 static const struct wl_shell_surface_listener shell_surface_listener;
746 wayland_output_init_gl_renderer(struct wayland_output *output)
748 int32_t fwidth = 0, fheight = 0;
751 fwidth = frame_width(output->frame);
752 fheight = frame_height(output->frame);
754 fwidth = output->base.current_mode->width;
755 fheight = output->base.current_mode->height;
758 output->gl.egl_window =
759 wl_egl_window_create(output->parent.surface,
761 if (!output->gl.egl_window) {
762 weston_log("failure to create wl_egl_window\n");
766 if (gl_renderer->output_window_create(&output->base,
767 output->gl.egl_window,
768 output->gl.egl_window,
769 gl_renderer->alpha_attribs,
777 wl_egl_window_destroy(output->gl.egl_window);
783 wayland_output_init_pixman_renderer(struct wayland_output *output)
785 return pixman_renderer_output_create(&output->base,
786 PIXMAN_RENDERER_OUTPUT_USE_SHADOW);
790 wayland_output_resize_surface(struct wayland_output *output)
792 struct wayland_backend *b =
793 to_wayland_backend(output->base.compositor);
794 int32_t ix, iy, iwidth, iheight;
795 int32_t width, height;
796 struct wl_region *region;
798 width = output->base.current_mode->width;
799 height = output->base.current_mode->height;
802 frame_resize_inside(output->frame, width, height);
804 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
805 region = wl_compositor_create_region(b->parent.compositor);
806 wl_region_add(region, ix, iy, iwidth, iheight);
807 wl_surface_set_input_region(output->parent.surface, region);
808 wl_region_destroy(region);
810 if (output->parent.xdg_surface) {
811 zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
818 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
819 region = wl_compositor_create_region(b->parent.compositor);
820 wl_region_add(region, ix, iy, iwidth, iheight);
821 wl_surface_set_opaque_region(output->parent.surface, region);
822 wl_region_destroy(region);
824 width = frame_width(output->frame);
825 height = frame_height(output->frame);
827 region = wl_compositor_create_region(b->parent.compositor);
828 wl_region_add(region, 0, 0, width, height);
829 wl_surface_set_input_region(output->parent.surface, region);
830 wl_region_destroy(region);
832 region = wl_compositor_create_region(b->parent.compositor);
833 wl_region_add(region, 0, 0, width, height);
834 wl_surface_set_opaque_region(output->parent.surface, region);
835 wl_region_destroy(region);
837 if (output->parent.xdg_surface) {
838 zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
847 if (output->gl.egl_window) {
848 wl_egl_window_resize(output->gl.egl_window,
849 width, height, 0, 0);
851 /* These will need to be re-created due to the resize */
852 gl_renderer->output_set_border(&output->base,
853 GL_RENDERER_BORDER_TOP,
855 cairo_surface_destroy(output->gl.border.top);
856 output->gl.border.top = NULL;
857 gl_renderer->output_set_border(&output->base,
858 GL_RENDERER_BORDER_LEFT,
860 cairo_surface_destroy(output->gl.border.left);
861 output->gl.border.left = NULL;
862 gl_renderer->output_set_border(&output->base,
863 GL_RENDERER_BORDER_RIGHT,
865 cairo_surface_destroy(output->gl.border.right);
866 output->gl.border.right = NULL;
867 gl_renderer->output_set_border(&output->base,
868 GL_RENDERER_BORDER_BOTTOM,
870 cairo_surface_destroy(output->gl.border.bottom);
871 output->gl.border.bottom = NULL;
875 wayland_output_destroy_shm_buffers(output);
879 wayland_output_set_windowed(struct wayland_output *output)
881 struct wayland_backend *b =
882 to_wayland_backend(output->base.compositor);
888 b->theme = theme_create();
892 output->frame = frame_create(b->theme, 100, 100,
893 FRAME_BUTTON_CLOSE, output->title, NULL);
897 if (output->keyboard_count)
898 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
900 wayland_output_resize_surface(output);
902 if (output->parent.xdg_toplevel) {
903 zxdg_toplevel_v6_unset_fullscreen(output->parent.xdg_toplevel);
904 } else if (output->parent.shell_surface) {
905 wl_shell_surface_set_toplevel(output->parent.shell_surface);
914 wayland_output_set_fullscreen(struct wayland_output *output,
915 enum wl_shell_surface_fullscreen_method method,
916 uint32_t framerate, struct wl_output *target)
919 frame_destroy(output->frame);
920 output->frame = NULL;
923 wayland_output_resize_surface(output);
925 if (output->parent.xdg_toplevel) {
926 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
927 } else if (output->parent.shell_surface) {
928 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
929 method, framerate, target);
935 static struct weston_mode *
936 wayland_output_choose_mode(struct wayland_output *output,
937 struct weston_mode *ref_mode)
939 struct weston_mode *mode;
941 /* First look for an exact match */
942 wl_list_for_each(mode, &output->base.mode_list, link)
943 if (mode->width == ref_mode->width &&
944 mode->height == ref_mode->height &&
945 mode->refresh == ref_mode->refresh)
948 /* If we can't find an exact match, ignore refresh and try again */
949 wl_list_for_each(mode, &output->base.mode_list, link)
950 if (mode->width == ref_mode->width &&
951 mode->height == ref_mode->height)
954 /* Yeah, we failed */
966 mode_feedback_successful(void *data,
967 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
969 enum mode_status *value = data;
971 printf("Mode switch successful\n");
973 *value = MODE_STATUS_SUCCESS;
977 mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
979 enum mode_status *value = data;
981 printf("Mode switch failed\n");
983 *value = MODE_STATUS_FAIL;
987 mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
989 enum mode_status *value = data;
991 printf("Mode switch cancelled\n");
993 *value = MODE_STATUS_CANCEL;
996 struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
997 mode_feedback_successful,
998 mode_feedback_failed,
999 mode_feedback_cancelled,
1002 static enum mode_status
1003 wayland_output_fullscreen_shell_mode_feedback(struct wayland_output *output,
1004 struct weston_mode *mode)
1006 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1007 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
1008 enum mode_status mode_status;
1012 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1013 output->parent.surface,
1014 output->parent.output,
1017 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1018 &mode_feedback_listener,
1021 output->parent.draw_initial_frame = false;
1022 draw_initial_frame(output);
1023 wl_surface_commit(output->parent.surface);
1025 mode_status = MODE_STATUS_UNKNOWN;
1026 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
1027 ret = wl_display_dispatch(b->parent.wl_display);
1029 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
1035 wayland_output_switch_mode(struct weston_output *output_base,
1036 struct weston_mode *mode)
1038 struct wayland_output *output = to_wayland_output(output_base);
1039 struct wayland_backend *b;
1040 struct wl_surface *old_surface;
1041 struct weston_mode *old_mode;
1042 enum mode_status mode_status;
1044 if (output_base == NULL) {
1045 weston_log("output is NULL.\n");
1050 weston_log("mode is NULL.\n");
1054 b = to_wayland_backend(output_base->compositor);
1056 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
1059 mode = wayland_output_choose_mode(output, mode);
1063 if (output->base.current_mode == mode)
1066 old_mode = output->base.current_mode;
1067 old_surface = output->parent.surface;
1068 output->base.current_mode = mode;
1069 output->parent.surface =
1070 wl_compositor_create_surface(b->parent.compositor);
1071 wl_surface_set_user_data(output->parent.surface, output);
1073 /* Blow the old buffers because we changed size/surfaces */
1074 wayland_output_resize_surface(output);
1076 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, mode);
1078 /* This should kick-start things again */
1079 wayland_output_start_repaint_loop(&output->base);
1081 if (mode_status == MODE_STATUS_FAIL) {
1082 output->base.current_mode = old_mode;
1083 wl_surface_destroy(output->parent.surface);
1084 output->parent.surface = old_surface;
1085 wayland_output_resize_surface(output);
1090 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1091 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1093 if (b->use_pixman) {
1094 pixman_renderer_output_destroy(output_base);
1095 if (wayland_output_init_pixman_renderer(output) < 0)
1099 gl_renderer->output_destroy(output_base);
1100 wl_egl_window_destroy(output->gl.egl_window);
1101 if (wayland_output_init_gl_renderer(output) < 0)
1105 wl_surface_destroy(old_surface);
1107 weston_output_schedule_repaint(&output->base);
1117 handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1120 zxdg_surface_v6_ack_configure(surface, serial);
1123 static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1124 handle_xdg_surface_configure
1128 handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1129 int32_t width, int32_t height,
1130 struct wl_array *states)
1132 struct wayland_output *output = data;
1134 output->parent.configure_width = width;
1135 output->parent.configure_height = height;
1137 output->parent.wait_for_configure = false;
1138 /* FIXME: implement resizing */
1142 handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1144 struct wayland_output *output = data;
1145 struct weston_compositor *compositor = output->base.compositor;
1147 wayland_output_destroy(&output->base);
1149 if (wl_list_empty(&compositor->output_list))
1150 weston_compositor_exit(compositor);
1153 static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1154 handle_xdg_toplevel_configure,
1155 handle_xdg_toplevel_close,
1159 wayland_backend_create_output_surface(struct wayland_output *output)
1161 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1163 assert(!output->parent.surface);
1165 output->parent.surface =
1166 wl_compositor_create_surface(b->parent.compositor);
1167 if (!output->parent.surface)
1170 wl_surface_set_user_data(output->parent.surface, output);
1172 output->parent.draw_initial_frame = true;
1174 if (b->parent.xdg_shell) {
1175 output->parent.xdg_surface =
1176 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1177 output->parent.surface);
1178 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1179 &xdg_surface_listener, output);
1181 output->parent.xdg_toplevel =
1182 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1183 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1184 &xdg_toplevel_listener, output);
1186 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, output->title);
1188 wl_surface_commit(output->parent.surface);
1190 output->parent.wait_for_configure = true;
1192 while (output->parent.wait_for_configure)
1193 wl_display_dispatch(b->parent.wl_display);
1195 weston_log("wayland-backend: Using xdg_shell_v6\n");
1197 else if (b->parent.shell) {
1198 output->parent.shell_surface =
1199 wl_shell_get_shell_surface(b->parent.shell,
1200 output->parent.surface);
1201 if (!output->parent.shell_surface) {
1202 wl_surface_destroy(output->parent.surface);
1206 wl_shell_surface_add_listener(output->parent.shell_surface,
1207 &shell_surface_listener, output);
1209 weston_log("wayland-backend: Using wl_shell\n");
1216 wayland_output_enable(struct weston_output *base)
1218 struct wayland_output *output = to_wayland_output(base);
1219 struct wayland_backend *b = to_wayland_backend(base->compositor);
1220 enum mode_status mode_status;
1223 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1224 output->base.current_mode->width,
1225 output->base.current_mode->height,
1226 output->base.x, output->base.y);
1228 if (!output->parent.surface)
1229 ret = wayland_backend_create_output_surface(output);
1234 wl_list_init(&output->shm.buffers);
1235 wl_list_init(&output->shm.free_buffers);
1237 if (b->use_pixman) {
1238 if (wayland_output_init_pixman_renderer(output) < 0)
1241 output->base.repaint = wayland_output_repaint_pixman;
1244 if (wayland_output_init_gl_renderer(output) < 0)
1247 output->base.repaint = wayland_output_repaint_gl;
1251 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1252 output->base.assign_planes = NULL;
1253 output->base.set_backlight = NULL;
1254 output->base.set_dpms = NULL;
1255 output->base.switch_mode = wayland_output_switch_mode;
1257 if (b->sprawl_across_outputs) {
1258 if (b->parent.fshell) {
1259 wayland_output_resize_surface(output);
1261 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, &output->mode);
1263 if (mode_status == MODE_STATUS_FAIL) {
1264 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1265 output->parent.surface,
1266 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1267 output->parent.output);
1269 output->parent.draw_initial_frame = true;
1272 wayland_output_set_fullscreen(output,
1273 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1274 output->mode.refresh, output->parent.output);
1276 } else if (b->fullscreen) {
1277 wayland_output_set_fullscreen(output, 0, 0, NULL);
1279 wayland_output_set_windowed(output);
1285 wayland_backend_destroy_output_surface(output);
1291 wayland_output_setup_for_parent_output(struct wayland_output *output,
1292 struct wayland_parent_output *poutput);
1295 wayland_output_setup_fullscreen(struct wayland_output *output,
1296 struct wayland_head *head);
1299 wayland_output_attach_head(struct weston_output *output_base,
1300 struct weston_head *head_base)
1302 struct wayland_backend *b = to_wayland_backend(output_base->compositor);
1303 struct wayland_output *output = to_wayland_output(output_base);
1304 struct wayland_head *head = to_wayland_head(head_base);
1306 if (!wl_list_empty(&output->base.head_list))
1309 if (head->parent_output) {
1310 if (wayland_output_setup_for_parent_output(output,
1311 head->parent_output) < 0)
1313 } else if (b->fullscreen) {
1314 if (wayland_output_setup_fullscreen(output, head) < 0)
1317 /* A floating window, nothing to do. */
1324 wayland_output_detach_head(struct weston_output *output_base,
1325 struct weston_head *head)
1327 struct wayland_output *output = to_wayland_output(output_base);
1329 /* Rely on the disable hook if the output was enabled. We do not
1330 * support cloned heads, so detaching is guaranteed to disable the
1333 if (output->base.enabled)
1336 /* undo setup fullscreen */
1337 if (output->parent.surface)
1338 wayland_backend_destroy_output_surface(output);
1341 static struct weston_output *
1342 wayland_output_create(struct weston_compositor *compositor, const char *name)
1344 struct wayland_output *output;
1347 /* name can't be NULL. */
1350 output = zalloc(sizeof *output);
1351 if (output == NULL) {
1356 if (asprintf(&title, "%s - %s", WINDOW_TITLE, name) < 0) {
1360 output->title = title;
1362 weston_output_init(&output->base, compositor, name);
1364 output->base.destroy = wayland_output_destroy;
1365 output->base.disable = wayland_output_disable;
1366 output->base.enable = wayland_output_enable;
1367 output->base.attach_head = wayland_output_attach_head;
1368 output->base.detach_head = wayland_output_detach_head;
1370 weston_compositor_add_pending_output(&output->base, compositor);
1372 return &output->base;
1375 static struct wayland_head *
1376 wayland_head_create(struct weston_compositor *compositor, const char *name)
1378 struct wayland_head *head;
1382 head = zalloc(sizeof *head);
1386 weston_head_init(&head->base, name);
1387 weston_head_set_connection_status(&head->base, true);
1388 weston_compositor_add_head(compositor, &head->base);
1394 wayland_head_create_windowed(struct weston_compositor *compositor,
1397 if (!wayland_head_create(compositor, name))
1404 wayland_head_create_for_parent_output(struct weston_compositor *compositor,
1405 struct wayland_parent_output *poutput)
1407 struct wayland_head *head;
1411 ret = snprintf(name, sizeof(name), "wlparent-%d", poutput->id);
1412 if (ret < 1 || (unsigned)ret >= sizeof(name))
1415 head = wayland_head_create(compositor, name);
1419 assert(!poutput->head);
1420 head->parent_output = poutput;
1421 poutput->head = head;
1423 weston_head_set_monitor_strings(&head->base,
1424 poutput->physical.make,
1425 poutput->physical.model, NULL);
1426 weston_head_set_physical_size(&head->base,
1427 poutput->physical.width,
1428 poutput->physical.height);
1434 wayland_head_destroy(struct wayland_head *head)
1436 if (head->parent_output)
1437 head->parent_output->head = NULL;
1439 weston_head_release(&head->base);
1444 wayland_output_set_size(struct weston_output *base, int width, int height)
1446 struct wayland_output *output = to_wayland_output(base);
1447 struct weston_head *head;
1448 int output_width, output_height;
1450 /* We can only be called once. */
1451 assert(!output->base.current_mode);
1453 /* Make sure we have scale set. */
1454 assert(output->base.scale);
1457 weston_log("Invalid width \"%d\" for output %s\n",
1458 width, output->base.name);
1463 weston_log("Invalid height \"%d\" for output %s\n",
1464 height, output->base.name);
1468 wl_list_for_each(head, &output->base.head_list, output_link) {
1469 weston_head_set_monitor_strings(head, "wayland", "none", NULL);
1471 /* XXX: Calculate proper size. */
1472 weston_head_set_physical_size(head, width, height);
1475 output_width = width * output->base.scale;
1476 output_height = height * output->base.scale;
1478 output->mode.flags =
1479 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1481 output->mode.width = output_width;
1482 output->mode.height = output_height;
1483 output->mode.refresh = 60000;
1484 wl_list_insert(&output->base.mode_list, &output->mode.link);
1486 output->base.current_mode = &output->mode;
1492 wayland_output_setup_for_parent_output(struct wayland_output *output,
1493 struct wayland_parent_output *poutput)
1495 struct weston_mode *mode;
1497 if (poutput->current_mode) {
1498 mode = poutput->current_mode;
1499 } else if (poutput->preferred_mode) {
1500 mode = poutput->preferred_mode;
1501 } else if (!wl_list_empty(&poutput->mode_list)) {
1502 mode = container_of(poutput->mode_list.next,
1503 struct weston_mode, link);
1505 weston_log("No valid modes found. Skipping output.\n");
1509 output->base.scale = 1;
1510 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1512 output->parent.output = poutput->global;
1514 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1515 wl_list_init(&poutput->mode_list);
1517 /* No other mode should have CURRENT already. */
1518 mode->flags |= WL_OUTPUT_MODE_CURRENT;
1519 output->base.current_mode = mode;
1521 /* output->mode is unused in this path. */
1527 wayland_output_setup_fullscreen(struct wayland_output *output,
1528 struct wayland_head *head)
1530 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1531 int width = 0, height = 0;
1533 output->base.scale = 1;
1534 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1536 if (wayland_backend_create_output_surface(output) < 0)
1539 /* What should size be set if conditional is false? */
1540 if (b->parent.xdg_shell || b->parent.shell) {
1541 if (output->parent.xdg_toplevel)
1542 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1543 output->parent.output);
1544 else if (output->parent.shell_surface)
1545 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1548 wl_display_roundtrip(b->parent.wl_display);
1550 width = output->parent.configure_width;
1551 height = output->parent.configure_height;
1554 if (wayland_output_set_size(&output->base, width, height) < 0)
1557 /* The head is not attached yet, so set_size did not set these. */
1558 weston_head_set_monitor_strings(&head->base, "wayland", "none", NULL);
1559 /* XXX: Calculate proper size. */
1560 weston_head_set_physical_size(&head->base, width, height);
1565 wayland_backend_destroy_output_surface(output);
1571 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1574 wl_shell_surface_pong(shell_surface, serial);
1578 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1579 uint32_t edges, int32_t width, int32_t height)
1581 struct wayland_output *output = data;
1583 output->parent.configure_width = width;
1584 output->parent.configure_height = height;
1586 /* FIXME: implement resizing */
1590 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1594 static const struct wl_shell_surface_listener shell_surface_listener = {
1596 shell_surface_configure,
1597 shell_surface_popup_done
1600 /* Events received from the wayland-server this compositor is client of: */
1602 /* parent input interface */
1604 input_set_cursor(struct wayland_input *input)
1607 struct wl_buffer *buffer;
1608 struct wl_cursor_image *image;
1610 if (!input->backend->cursor)
1611 return; /* Couldn't load the cursor. Can't set it */
1613 image = input->backend->cursor->images[0];
1614 buffer = wl_cursor_image_get_buffer(image);
1618 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1619 input->parent.cursor.surface,
1620 image->hotspot_x, image->hotspot_y);
1622 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1623 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1624 image->width, image->height);
1625 wl_surface_commit(input->parent.cursor.surface);
1629 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1630 uint32_t serial, struct wl_surface *surface,
1631 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
1633 struct wayland_input *input = data;
1635 enum theme_location location;
1639 input->output = NULL;
1640 input->has_focus = false;
1641 notify_pointer_focus(&input->base, NULL, 0, 0);
1645 x = wl_fixed_to_double(fixed_x);
1646 y = wl_fixed_to_double(fixed_y);
1648 /* XXX: If we get a modifier event immediately before the focus,
1649 * we should try to keep the same serial. */
1650 input->enter_serial = serial;
1651 input->output = wl_surface_get_user_data(surface);
1653 if (input->output->frame) {
1654 location = frame_pointer_enter(input->output->frame, input,
1656 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1660 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1661 weston_output_schedule_repaint(&input->output->base);
1663 location = THEME_LOCATION_CLIENT_AREA;
1666 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1668 if (location == THEME_LOCATION_CLIENT_AREA) {
1669 input->has_focus = true;
1670 notify_pointer_focus(&input->base, &input->output->base, x, y);
1671 wl_pointer_set_cursor(input->parent.pointer,
1672 input->enter_serial, NULL, 0, 0);
1674 input->has_focus = false;
1675 notify_pointer_focus(&input->base, NULL, 0, 0);
1676 input_set_cursor(input);
1681 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1682 uint32_t serial, struct wl_surface *surface)
1684 struct wayland_input *input = data;
1689 if (input->output->frame) {
1690 frame_pointer_leave(input->output->frame, input);
1692 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1693 weston_output_schedule_repaint(&input->output->base);
1696 notify_pointer_focus(&input->base, NULL, 0, 0);
1697 input->output = NULL;
1698 input->has_focus = false;
1702 input_handle_motion(void *data, struct wl_pointer *pointer,
1703 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
1705 struct wayland_input *input = data;
1707 enum theme_location location;
1708 bool want_frame = false;
1715 x = wl_fixed_to_double(fixed_x);
1716 y = wl_fixed_to_double(fixed_y);
1718 if (input->output->frame) {
1719 location = frame_pointer_motion(input->output->frame, input,
1721 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1725 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1726 weston_output_schedule_repaint(&input->output->base);
1728 location = THEME_LOCATION_CLIENT_AREA;
1731 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1733 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
1734 input_set_cursor(input);
1735 notify_pointer_focus(&input->base, NULL, 0, 0);
1736 input->has_focus = false;
1738 } else if (!input->has_focus &&
1739 location == THEME_LOCATION_CLIENT_AREA) {
1740 wl_pointer_set_cursor(input->parent.pointer,
1741 input->enter_serial, NULL, 0, 0);
1742 notify_pointer_focus(&input->base, &input->output->base, x, y);
1743 input->has_focus = true;
1747 if (location == THEME_LOCATION_CLIENT_AREA) {
1748 timespec_from_msec(&ts, time);
1749 notify_motion_absolute(&input->base, &ts, x, y);
1753 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1754 notify_pointer_frame(&input->base);
1758 input_handle_button(void *data, struct wl_pointer *pointer,
1759 uint32_t serial, uint32_t time, uint32_t button,
1760 enum wl_pointer_button_state state)
1762 struct wayland_input *input = data;
1763 enum theme_location location;
1769 if (input->output->frame) {
1770 location = frame_pointer_button(input->output->frame, input,
1773 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1774 if (input->output->parent.xdg_toplevel)
1775 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
1776 input->parent.seat, serial);
1777 else if (input->output->parent.shell_surface)
1778 wl_shell_surface_move(input->output->parent.shell_surface,
1779 input->parent.seat, serial);
1780 frame_status_clear(input->output->frame,
1785 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1786 wayland_output_destroy(&input->output->base);
1787 input->output = NULL;
1788 input->keyboard_focus = NULL;
1790 if (wl_list_empty(&input->backend->compositor->output_list))
1791 weston_compositor_exit(input->backend->compositor);
1796 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1797 weston_output_schedule_repaint(&input->output->base);
1799 location = THEME_LOCATION_CLIENT_AREA;
1802 if (location == THEME_LOCATION_CLIENT_AREA) {
1803 timespec_from_msec(&ts, time);
1804 notify_button(&input->base, &ts, button, state);
1805 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1806 notify_pointer_frame(&input->base);
1811 input_handle_axis(void *data, struct wl_pointer *pointer,
1812 uint32_t time, uint32_t axis, wl_fixed_t value)
1814 struct wayland_input *input = data;
1815 struct weston_pointer_axis_event weston_event;
1818 weston_event.axis = axis;
1819 weston_event.value = wl_fixed_to_double(value);
1820 weston_event.has_discrete = false;
1822 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1823 input->vert.has_discrete) {
1824 weston_event.has_discrete = true;
1825 weston_event.discrete = input->vert.discrete;
1826 input->vert.has_discrete = false;
1827 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1828 input->horiz.has_discrete) {
1829 weston_event.has_discrete = true;
1830 weston_event.discrete = input->horiz.discrete;
1831 input->horiz.has_discrete = false;
1834 timespec_from_msec(&ts, time);
1836 notify_axis(&input->base, &ts, &weston_event);
1838 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1839 notify_pointer_frame(&input->base);
1843 input_handle_frame(void *data, struct wl_pointer *pointer)
1845 struct wayland_input *input = data;
1847 notify_pointer_frame(&input->base);
1851 input_handle_axis_source(void *data, struct wl_pointer *pointer,
1854 struct wayland_input *input = data;
1856 notify_axis_source(&input->base, source);
1860 input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1861 uint32_t time, uint32_t axis)
1863 struct wayland_input *input = data;
1864 struct weston_pointer_axis_event weston_event;
1867 weston_event.axis = axis;
1868 weston_event.value = 0;
1870 timespec_from_msec(&ts, time);
1872 notify_axis(&input->base, &ts, &weston_event);
1876 input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1877 uint32_t axis, int32_t discrete)
1879 struct wayland_input *input = data;
1881 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1882 input->vert.has_discrete = true;
1883 input->vert.discrete = discrete;
1884 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1885 input->horiz.has_discrete = true;
1886 input->horiz.discrete = discrete;
1890 static const struct wl_pointer_listener pointer_listener = {
1891 input_handle_pointer_enter,
1892 input_handle_pointer_leave,
1893 input_handle_motion,
1894 input_handle_button,
1897 input_handle_axis_source,
1898 input_handle_axis_stop,
1899 input_handle_axis_discrete,
1903 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1904 int fd, uint32_t size)
1906 struct wayland_input *input = data;
1907 struct xkb_keymap *keymap;
1915 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1916 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1917 if (map_str == MAP_FAILED) {
1918 weston_log("mmap failed: %m\n");
1922 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
1924 XKB_KEYMAP_FORMAT_TEXT_V1,
1926 munmap(map_str, size);
1929 weston_log("failed to compile keymap\n");
1933 input->keyboard_state_update = STATE_UPDATE_NONE;
1934 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1935 weston_log("No keymap provided; falling back to defalt\n");
1937 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1939 weston_log("Invalid keymap\n");
1945 if (weston_seat_get_keyboard(&input->base))
1946 weston_seat_update_keymap(&input->base, keymap);
1948 weston_seat_init_keyboard(&input->base, keymap);
1950 xkb_keymap_unref(keymap);
1955 wl_keyboard_release(input->parent.keyboard);
1960 input_handle_keyboard_enter(void *data,
1961 struct wl_keyboard *keyboard,
1963 struct wl_surface *surface,
1964 struct wl_array *keys)
1966 struct wayland_input *input = data;
1967 struct wayland_output *focus;
1969 focus = input->keyboard_focus;
1971 /* This shouldn't happen */
1972 focus->keyboard_count--;
1973 if (!focus->keyboard_count && focus->frame)
1974 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1975 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1976 weston_output_schedule_repaint(&focus->base);
1980 input->keyboard_focus = NULL;
1984 input->keyboard_focus = wl_surface_get_user_data(surface);
1985 input->keyboard_focus->keyboard_count++;
1987 focus = input->keyboard_focus;
1989 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1990 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1991 weston_output_schedule_repaint(&focus->base);
1995 /* XXX: If we get a modifier event immediately before the focus,
1996 * we should try to keep the same serial. */
1997 notify_keyboard_focus_in(&input->base, keys,
1998 STATE_UPDATE_AUTOMATIC);
2002 input_handle_keyboard_leave(void *data,
2003 struct wl_keyboard *keyboard,
2005 struct wl_surface *surface)
2007 struct wayland_input *input = data;
2008 struct wayland_output *focus;
2010 notify_keyboard_focus_out(&input->base);
2012 focus = input->keyboard_focus;
2016 focus->keyboard_count--;
2017 if (!focus->keyboard_count && focus->frame) {
2018 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
2019 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
2020 weston_output_schedule_repaint(&focus->base);
2023 input->keyboard_focus = NULL;
2027 input_handle_key(void *data, struct wl_keyboard *keyboard,
2028 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
2030 struct wayland_input *input = data;
2033 if (!input->keyboard_focus)
2036 timespec_from_msec(&ts, time);
2038 input->key_serial = serial;
2039 notify_key(&input->base, &ts, key,
2040 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
2041 WL_KEYBOARD_KEY_STATE_RELEASED,
2042 input->keyboard_state_update);
2046 input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
2047 uint32_t serial_in, uint32_t mods_depressed,
2048 uint32_t mods_latched, uint32_t mods_locked,
2051 struct weston_keyboard *keyboard;
2052 struct wayland_input *input = data;
2053 struct wayland_backend *b = input->backend;
2054 uint32_t serial_out;
2056 /* If we get a key event followed by a modifier event with the
2057 * same serial number, then we try to preserve those semantics by
2058 * reusing the same serial number on the way out too. */
2059 if (serial_in == input->key_serial)
2060 serial_out = wl_display_get_serial(b->compositor->wl_display);
2062 serial_out = wl_display_next_serial(b->compositor->wl_display);
2064 keyboard = weston_seat_get_keyboard(&input->base);
2065 xkb_state_update_mask(keyboard->xkb_state.state,
2066 mods_depressed, mods_latched,
2067 mods_locked, 0, 0, group);
2068 notify_modifiers(&input->base, serial_out);
2072 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2073 int32_t rate, int32_t delay)
2075 struct wayland_input *input = data;
2076 struct wayland_backend *b = input->backend;
2078 b->compositor->kb_repeat_rate = rate;
2079 b->compositor->kb_repeat_delay = delay;
2082 static const struct wl_keyboard_listener keyboard_listener = {
2083 input_handle_keymap,
2084 input_handle_keyboard_enter,
2085 input_handle_keyboard_leave,
2087 input_handle_modifiers,
2088 input_handle_repeat_info,
2092 input_handle_touch_down(void *data, struct wl_touch *wl_touch,
2093 uint32_t serial, uint32_t time,
2094 struct wl_surface *surface, int32_t id,
2095 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
2097 struct wayland_input *input = data;
2098 struct wayland_output *output;
2099 enum theme_location location;
2105 x = wl_fixed_to_double(fixed_x);
2106 y = wl_fixed_to_double(fixed_y);
2108 timespec_from_msec(&ts, time);
2110 first_touch = (input->touch_points == 0);
2111 input->touch_points++;
2113 input->touch_focus = wl_surface_get_user_data(surface);
2114 output = input->touch_focus;
2115 if (!first_touch && !input->touch_active)
2118 if (output->frame) {
2119 location = frame_touch_down(output->frame, input, id, x, y);
2121 frame_interior(output->frame, &fx, &fy, NULL, NULL);
2125 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2126 weston_output_schedule_repaint(&output->base);
2128 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
2129 input->touch_points--;
2130 if (output->parent.xdg_toplevel)
2131 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
2132 input->parent.seat, serial);
2133 else if (output->parent.shell_surface)
2134 wl_shell_surface_move(output->parent.shell_surface,
2135 input->parent.seat, serial);
2136 frame_status_clear(output->frame,
2141 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
2145 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2147 notify_touch(&input->base, &ts, id, x, y, WL_TOUCH_DOWN);
2148 input->touch_active = true;
2152 input_handle_touch_up(void *data, struct wl_touch *wl_touch,
2153 uint32_t serial, uint32_t time, int32_t id)
2155 struct wayland_input *input = data;
2156 struct wayland_output *output = input->touch_focus;
2157 bool active = input->touch_active;
2160 timespec_from_msec(&ts, time);
2162 input->touch_points--;
2163 if (input->touch_points == 0) {
2164 input->touch_focus = NULL;
2165 input->touch_active = false;
2171 if (output->frame) {
2172 frame_touch_up(output->frame, input, id);
2174 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
2175 wayland_output_destroy(&output->base);
2176 input->touch_focus = NULL;
2177 input->keyboard_focus = NULL;
2178 if (wl_list_empty(&input->backend->compositor->output_list))
2179 weston_compositor_exit(input->backend->compositor);
2183 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2184 weston_output_schedule_repaint(&output->base);
2188 notify_touch(&input->base, &ts, id, 0, 0, WL_TOUCH_UP);
2192 input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
2193 uint32_t time, int32_t id,
2194 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
2196 struct wayland_input *input = data;
2197 struct wayland_output *output = input->touch_focus;
2202 x = wl_fixed_to_double(fixed_x);
2203 y = wl_fixed_to_double(fixed_y);
2204 timespec_from_msec(&ts, time);
2206 if (!output || !input->touch_active)
2209 if (output->frame) {
2210 frame_interior(output->frame, &fx, &fy, NULL, NULL);
2215 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2217 notify_touch(&input->base, &ts, id, x, y, WL_TOUCH_MOTION);
2221 input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2223 struct wayland_input *input = data;
2225 if (!input->touch_focus || !input->touch_active)
2228 notify_touch_frame(&input->base);
2232 input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2234 struct wayland_input *input = data;
2236 if (!input->touch_focus || !input->touch_active)
2239 notify_touch_cancel(&input->base);
2242 static const struct wl_touch_listener touch_listener = {
2243 input_handle_touch_down,
2244 input_handle_touch_up,
2245 input_handle_touch_motion,
2246 input_handle_touch_frame,
2247 input_handle_touch_cancel,
2252 input_handle_capabilities(void *data, struct wl_seat *seat,
2253 enum wl_seat_capability caps)
2255 struct wayland_input *input = data;
2257 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2258 input->parent.pointer = wl_seat_get_pointer(seat);
2259 wl_pointer_set_user_data(input->parent.pointer, input);
2260 wl_pointer_add_listener(input->parent.pointer,
2261 &pointer_listener, input);
2262 weston_seat_init_pointer(&input->base);
2263 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
2264 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2265 wl_pointer_release(input->parent.pointer);
2267 wl_pointer_destroy(input->parent.pointer);
2268 input->parent.pointer = NULL;
2269 weston_seat_release_pointer(&input->base);
2272 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2273 input->parent.keyboard = wl_seat_get_keyboard(seat);
2274 wl_keyboard_set_user_data(input->parent.keyboard, input);
2275 wl_keyboard_add_listener(input->parent.keyboard,
2276 &keyboard_listener, input);
2277 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
2278 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2279 wl_keyboard_release(input->parent.keyboard);
2281 wl_keyboard_destroy(input->parent.keyboard);
2282 input->parent.keyboard = NULL;
2283 weston_seat_release_keyboard(&input->base);
2286 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2287 input->parent.touch = wl_seat_get_touch(seat);
2288 wl_touch_set_user_data(input->parent.touch, input);
2289 wl_touch_add_listener(input->parent.touch,
2290 &touch_listener, input);
2291 weston_seat_init_touch(&input->base);
2292 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2293 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2294 wl_touch_release(input->parent.touch);
2296 wl_touch_destroy(input->parent.touch);
2297 input->parent.touch = NULL;
2298 weston_seat_release_touch(&input->base);
2303 input_handle_name(void *data, struct wl_seat *seat,
2308 static const struct wl_seat_listener seat_listener = {
2309 input_handle_capabilities,
2314 display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
2316 struct wayland_input *input;
2317 uint32_t version = MIN(available_version, 4);
2319 input = zalloc(sizeof *input);
2323 weston_seat_init(&input->base, b->compositor, "default");
2325 input->parent.seat = wl_registry_bind(b->parent.registry, id,
2326 &wl_seat_interface, version);
2327 input->seat_version = version;
2328 wl_list_insert(b->input_list.prev, &input->link);
2330 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2331 wl_seat_set_user_data(input->parent.seat, input);
2333 input->parent.cursor.surface =
2334 wl_compositor_create_surface(b->parent.compositor);
2336 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2337 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
2341 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2342 int32_t x, int32_t y,
2343 int32_t physical_width, int32_t physical_height,
2344 int32_t subpixel, const char *make,
2345 const char *model, int32_t transform)
2347 struct wayland_parent_output *output = data;
2351 output->physical.width = physical_width;
2352 output->physical.height = physical_height;
2353 output->physical.subpixel = subpixel;
2355 free(output->physical.make);
2356 output->physical.make = strdup(make);
2357 free(output->physical.model);
2358 output->physical.model = strdup(model);
2360 output->transform = transform;
2363 static struct weston_mode *
2364 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2366 struct weston_mode *mode;
2368 wl_list_for_each(mode, list, link) {
2369 if (mode->width == width && mode->height == height &&
2370 mode->refresh == refresh)
2374 mode = zalloc(sizeof *mode);
2378 mode->width = width;
2379 mode->height = height;
2380 mode->refresh = refresh;
2381 wl_list_insert(list, &mode->link);
2386 static struct weston_output *
2387 wayland_parent_output_get_enabled_output(struct wayland_parent_output *poutput)
2389 struct wayland_head *head = poutput->head;
2394 if (!weston_head_is_enabled(&head->base))
2397 return weston_head_get_output(&head->base);
2401 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2402 uint32_t flags, int32_t width, int32_t height,
2405 struct wayland_parent_output *output = data;
2406 struct weston_output *enabled_output;
2407 struct weston_mode *mode;
2409 enabled_output = wayland_parent_output_get_enabled_output(output);
2410 if (enabled_output) {
2411 mode = find_mode(&enabled_output->mode_list,
2412 width, height, refresh);
2415 mode->flags = flags;
2416 /* Do a mode-switch on current mode change? */
2418 mode = find_mode(&output->mode_list, width, height, refresh);
2421 mode->flags = flags;
2422 if (flags & WL_OUTPUT_MODE_CURRENT)
2423 output->current_mode = mode;
2424 if (flags & WL_OUTPUT_MODE_PREFERRED)
2425 output->preferred_mode = mode;
2429 static const struct wl_output_listener output_listener = {
2430 wayland_parent_output_geometry,
2431 wayland_parent_output_mode
2435 output_sync_callback(void *data, struct wl_callback *callback, uint32_t unused)
2437 struct wayland_parent_output *output = data;
2439 assert(output->sync_cb == callback);
2440 wl_callback_destroy(callback);
2441 output->sync_cb = NULL;
2443 assert(output->backend->sprawl_across_outputs);
2445 wayland_head_create_for_parent_output(output->backend->compositor, output);
2448 static const struct wl_callback_listener output_sync_listener = {
2449 output_sync_callback
2453 wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
2455 struct wayland_parent_output *output;
2457 output = zalloc(sizeof *output);
2461 output->backend = b;
2463 output->global = wl_registry_bind(b->parent.registry, id,
2464 &wl_output_interface, 1);
2465 if (!output->global) {
2470 wl_output_add_listener(output->global, &output_listener, output);
2473 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2474 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2475 wl_list_init(&output->mode_list);
2476 wl_list_insert(&b->parent.output_list, &output->link);
2478 if (b->sprawl_across_outputs) {
2479 output->sync_cb = wl_display_sync(b->parent.wl_display);
2480 wl_callback_add_listener(output->sync_cb,
2481 &output_sync_listener, output);
2486 wayland_parent_output_destroy(struct wayland_parent_output *output)
2488 struct weston_mode *mode, *next;
2490 if (output->sync_cb)
2491 wl_callback_destroy(output->sync_cb);
2494 wayland_head_destroy(output->head);
2496 wl_output_destroy(output->global);
2497 free(output->physical.make);
2498 free(output->physical.model);
2500 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2501 wl_list_remove(&mode->link);
2505 wl_list_remove(&output->link);
2510 xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2512 zxdg_shell_v6_pong(shell, serial);
2515 static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2520 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2521 const char *interface, uint32_t version)
2523 struct wayland_backend *b = data;
2525 if (strcmp(interface, "wl_compositor") == 0) {
2526 b->parent.compositor =
2527 wl_registry_bind(registry, name,
2528 &wl_compositor_interface,
2530 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2531 b->parent.xdg_shell =
2532 wl_registry_bind(registry, name,
2533 &zxdg_shell_v6_interface, 1);
2534 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2535 &xdg_shell_listener, b);
2536 } else if (strcmp(interface, "wl_shell") == 0) {
2538 wl_registry_bind(registry, name,
2539 &wl_shell_interface, 1);
2540 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
2542 wl_registry_bind(registry, name,
2543 &zwp_fullscreen_shell_v1_interface, 1);
2544 } else if (strcmp(interface, "wl_seat") == 0) {
2545 display_add_seat(b, name, version);
2546 } else if (strcmp(interface, "wl_output") == 0) {
2547 wayland_backend_register_output(b, name);
2548 } else if (strcmp(interface, "wl_shm") == 0) {
2550 wl_registry_bind(registry, name, &wl_shm_interface, 1);
2555 registry_handle_global_remove(void *data, struct wl_registry *registry,
2558 struct wayland_backend *b = data;
2559 struct wayland_parent_output *output, *next;
2561 wl_list_for_each_safe(output, next, &b->parent.output_list, link)
2562 if (output->id == name)
2563 wayland_parent_output_destroy(output);
2566 static const struct wl_registry_listener registry_listener = {
2567 registry_handle_global,
2568 registry_handle_global_remove
2572 wayland_backend_handle_event(int fd, uint32_t mask, void *data)
2574 struct wayland_backend *b = data;
2577 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
2578 weston_compositor_exit(b->compositor);
2582 if (mask & WL_EVENT_READABLE)
2583 count = wl_display_dispatch(b->parent.wl_display);
2584 if (mask & WL_EVENT_WRITABLE)
2585 wl_display_flush(b->parent.wl_display);
2588 count = wl_display_dispatch_pending(b->parent.wl_display);
2589 wl_display_flush(b->parent.wl_display);
2596 wayland_destroy(struct weston_compositor *ec)
2598 struct wayland_backend *b = to_wayland_backend(ec);
2599 struct weston_head *base, *next;
2601 wl_event_source_remove(b->parent.wl_source);
2603 weston_compositor_shutdown(ec);
2605 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2606 wayland_head_destroy(to_wayland_head(base));
2609 wl_shm_destroy(b->parent.shm);
2611 if (b->parent.xdg_shell)
2612 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2614 if (b->parent.shell)
2615 wl_shell_destroy(b->parent.shell);
2617 if (b->parent.fshell)
2618 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2620 if (b->parent.compositor)
2621 wl_compositor_destroy(b->parent.compositor);
2624 theme_destroy(b->theme);
2626 if (b->frame_device)
2627 cairo_device_destroy(b->frame_device);
2629 wl_cursor_theme_destroy(b->cursor_theme);
2631 wl_registry_destroy(b->parent.registry);
2632 wl_display_flush(b->parent.wl_display);
2633 wl_display_disconnect(b->parent.wl_display);
2638 static const char *left_ptrs[] = {
2646 create_cursor(struct wayland_backend *b,
2647 struct weston_wayland_backend_config *config)
2651 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2652 config->cursor_size,
2654 if (!b->cursor_theme) {
2655 fprintf(stderr, "could not load cursor theme\n");
2660 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2661 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
2664 fprintf(stderr, "could not load left cursor\n");
2670 fullscreen_binding(struct weston_keyboard *keyboard,
2671 const struct timespec *time, uint32_t key, void *data)
2673 struct wayland_backend *b = data;
2674 struct wayland_input *input = NULL;
2676 wl_list_for_each(input, &b->input_list, link)
2677 if (&input->base == keyboard->seat)
2680 if (!input || !input->output)
2683 if (input->output->frame)
2684 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2686 wayland_output_set_windowed(input->output);
2688 weston_output_schedule_repaint(&input->output->base);
2691 static struct wayland_backend *
2692 wayland_backend_create(struct weston_compositor *compositor,
2693 struct weston_wayland_backend_config *new_config)
2695 struct wayland_backend *b;
2696 struct wl_event_loop *loop;
2699 b = zalloc(sizeof *b);
2703 b->compositor = compositor;
2704 compositor->backend = &b->base;
2706 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
2707 goto err_compositor;
2709 b->parent.wl_display = wl_display_connect(new_config->display_name);
2710 if (b->parent.wl_display == NULL) {
2711 weston_log("Error: Failed to connect to parent Wayland compositor: %m\n");
2712 weston_log_continue(STAMP_SPACE "display option: %s, WAYLAND_DISPLAY=%s\n",
2713 new_config->display_name ?: "(none)",
2714 getenv("WAYLAND_DISPLAY") ?: "(not set)");
2715 goto err_compositor;
2718 wl_list_init(&b->parent.output_list);
2719 wl_list_init(&b->input_list);
2720 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2721 wl_registry_add_listener(b->parent.registry, ®istry_listener, b);
2722 wl_display_roundtrip(b->parent.wl_display);
2724 create_cursor(b, new_config);
2727 b->use_pixman = new_config->use_pixman;
2729 b->use_pixman = true;
2731 b->fullscreen = new_config->fullscreen;
2733 if (!b->use_pixman) {
2734 gl_renderer = weston_load_module("gl-renderer.so",
2735 "gl_renderer_interface");
2737 b->use_pixman = true;
2740 if (!b->use_pixman) {
2741 if (gl_renderer->display_create(compositor,
2742 EGL_PLATFORM_WAYLAND_KHR,
2743 b->parent.wl_display,
2745 gl_renderer->alpha_attribs,
2748 weston_log("Failed to initialize the GL renderer; "
2749 "falling back to pixman.\n");
2750 b->use_pixman = true;
2754 if (b->use_pixman) {
2755 if (pixman_renderer_init(compositor) < 0) {
2756 weston_log("Failed to initialize pixman renderer\n");
2761 b->base.destroy = wayland_destroy;
2762 b->base.create_output = wayland_output_create;
2764 loop = wl_display_get_event_loop(compositor->wl_display);
2766 fd = wl_display_get_fd(b->parent.wl_display);
2767 b->parent.wl_source =
2768 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2769 wayland_backend_handle_event, b);
2770 if (b->parent.wl_source == NULL)
2773 wl_event_source_check(b->parent.wl_source);
2775 if (compositor->renderer->import_dmabuf) {
2776 if (linux_dmabuf_setup(compositor) < 0)
2777 weston_log("Error: initializing dmabuf "
2778 "support failed.\n");
2783 wl_display_disconnect(b->parent.wl_display);
2785 weston_compositor_shutdown(compositor);
2791 wayland_backend_destroy(struct wayland_backend *b)
2793 wl_display_disconnect(b->parent.wl_display);
2796 theme_destroy(b->theme);
2797 if (b->frame_device)
2798 cairo_device_destroy(b->frame_device);
2799 wl_cursor_theme_destroy(b->cursor_theme);
2801 weston_compositor_shutdown(b->compositor);
2805 static const struct weston_windowed_output_api windowed_api = {
2806 wayland_output_set_size,
2807 wayland_head_create_windowed,
2811 config_init_to_defaults(struct weston_wayland_backend_config *config)
2816 weston_backend_init(struct weston_compositor *compositor,
2817 struct weston_backend_config *config_base)
2819 struct wayland_backend *b;
2820 struct wayland_parent_output *poutput;
2821 struct weston_wayland_backend_config new_config;
2824 if (config_base == NULL ||
2825 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2826 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2827 weston_log("wayland backend config structure is invalid\n");
2831 config_init_to_defaults(&new_config);
2832 memcpy(&new_config, config_base, config_base->struct_size);
2834 b = wayland_backend_create(compositor, &new_config);
2839 if (new_config.sprawl || b->parent.fshell) {
2840 b->sprawl_across_outputs = true;
2841 wl_display_roundtrip(b->parent.wl_display);
2843 wl_list_for_each(poutput, &b->parent.output_list, link)
2844 wayland_head_create_for_parent_output(compositor, poutput);
2849 if (new_config.fullscreen) {
2850 if (!wayland_head_create(compositor, "wayland-fullscreen")) {
2851 weston_log("Unable to create a fullscreen head.\n");
2858 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2859 &windowed_api, sizeof(windowed_api));
2862 weston_log("Failed to register output API.\n");
2863 wayland_backend_destroy(b);
2867 weston_compositor_add_key_binding(compositor, KEY_F,
2868 MODIFIER_CTRL | MODIFIER_ALT,
2869 fullscreen_binding, b);
2873 wayland_backend_destroy(b);