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 struct weston_touch_device *touch_device;
203 enum weston_key_state_update keyboard_state_update;
205 uint32_t enter_serial;
206 uint32_t touch_points;
211 struct wayland_output *output;
212 struct wayland_output *touch_focus;
213 struct wayland_output *keyboard_focus;
215 struct weston_pointer_axis_event vert, horiz;
218 struct gl_renderer_interface *gl_renderer;
220 static inline struct wayland_head *
221 to_wayland_head(struct weston_head *base)
223 return container_of(base, struct wayland_head, base);
226 static inline struct wayland_output *
227 to_wayland_output(struct weston_output *base)
229 return container_of(base, struct wayland_output, base);
232 static inline struct wayland_backend *
233 to_wayland_backend(struct weston_compositor *base)
235 return container_of(base->backend, struct wayland_backend, base);
239 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
241 cairo_surface_destroy(buffer->c_surface);
242 pixman_image_unref(buffer->pm_image);
244 wl_buffer_destroy(buffer->buffer);
245 munmap(buffer->data, buffer->size);
247 pixman_region32_fini(&buffer->damage);
249 wl_list_remove(&buffer->link);
250 wl_list_remove(&buffer->free_link);
255 buffer_release(void *data, struct wl_buffer *buffer)
257 struct wayland_shm_buffer *sb = data;
260 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
262 wayland_shm_buffer_destroy(sb);
266 static const struct wl_buffer_listener buffer_listener = {
270 static struct wayland_shm_buffer *
271 wayland_output_get_shm_buffer(struct wayland_output *output)
273 struct wayland_backend *b =
274 to_wayland_backend(output->base.compositor);
275 struct wl_shm *shm = b->parent.shm;
276 struct wayland_shm_buffer *sb;
278 struct wl_shm_pool *pool;
279 int width, height, stride;
284 if (!wl_list_empty(&output->shm.free_buffers)) {
285 sb = container_of(output->shm.free_buffers.next,
286 struct wayland_shm_buffer, free_link);
287 wl_list_remove(&sb->free_link);
288 wl_list_init(&sb->free_link);
294 width = frame_width(output->frame);
295 height = frame_height(output->frame);
297 width = output->base.current_mode->width;
298 height = output->base.current_mode->height;
301 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
303 fd = os_create_anonymous_file(height * stride);
305 weston_log("could not create an anonymous file buffer: %m\n");
309 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
310 if (data == MAP_FAILED) {
311 weston_log("could not mmap %d memory for data: %m\n", height * stride);
316 sb = zalloc(sizeof *sb);
318 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
320 munmap(data, height * stride);
325 wl_list_init(&sb->free_link);
326 wl_list_insert(&output->shm.buffers, &sb->link);
328 pixman_region32_init(&sb->damage);
329 pixman_region32_copy(&sb->damage, &output->base.region);
330 sb->frame_damaged = 1;
333 sb->size = height * stride;
335 pool = wl_shm_create_pool(shm, fd, sb->size);
337 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
340 WL_SHM_FORMAT_ARGB8888);
341 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
342 wl_shm_pool_destroy(pool);
345 memset(data, 0, sb->size);
348 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
349 width, height, stride);
354 frame_interior(output->frame, &fx, &fy, 0, 0);
356 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
357 (uint32_t *)(data + fy * stride) + fx,
364 frame_done(void *data, struct wl_callback *callback, uint32_t time)
366 struct wayland_output *output = data;
369 assert(callback == output->frame_cb);
370 wl_callback_destroy(callback);
371 output->frame_cb = NULL;
373 /* XXX: use the presentation extension for proper timings */
376 * This is the fallback case, where Presentation extension is not
377 * available from the parent compositor. We do not know the base for
378 * 'time', so we cannot feed it to finish_frame(). Do the only thing
379 * we can, and pretend finish_frame time is when we process this
382 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
383 weston_output_finish_frame(&output->base, &ts, 0);
386 static const struct wl_callback_listener frame_listener = {
391 draw_initial_frame(struct wayland_output *output)
393 struct wayland_shm_buffer *sb;
395 sb = wayland_output_get_shm_buffer(output);
397 /* If we are rendering with GL, then orphan it so that it gets
398 * destroyed immediately */
399 if (output->gl.egl_window)
402 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
403 wl_surface_damage(output->parent.surface, 0, 0,
404 output->base.current_mode->width,
405 output->base.current_mode->height);
410 wayland_output_update_gl_border(struct wayland_output *output)
412 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
417 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
420 fwidth = frame_width(output->frame);
421 fheight = frame_height(output->frame);
422 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
424 if (!output->gl.border.top)
425 output->gl.border.top =
426 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
428 cr = cairo_create(output->gl.border.top);
429 frame_repaint(output->frame, cr);
431 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
433 cairo_image_surface_get_stride(output->gl.border.top) / 4,
434 cairo_image_surface_get_data(output->gl.border.top));
437 if (!output->gl.border.left)
438 output->gl.border.left =
439 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
441 cr = cairo_create(output->gl.border.left);
442 cairo_translate(cr, 0, -iy);
443 frame_repaint(output->frame, cr);
445 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
447 cairo_image_surface_get_stride(output->gl.border.left) / 4,
448 cairo_image_surface_get_data(output->gl.border.left));
451 if (!output->gl.border.right)
452 output->gl.border.right =
453 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
454 fwidth - (ix + iwidth), 1);
455 cr = cairo_create(output->gl.border.right);
456 cairo_translate(cr, -(iwidth + ix), -iy);
457 frame_repaint(output->frame, cr);
459 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
460 fwidth - (ix + iwidth), 1,
461 cairo_image_surface_get_stride(output->gl.border.right) / 4,
462 cairo_image_surface_get_data(output->gl.border.right));
465 if (!output->gl.border.bottom)
466 output->gl.border.bottom =
467 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
468 fwidth, fheight - (iy + iheight));
469 cr = cairo_create(output->gl.border.bottom);
470 cairo_translate(cr, 0, -(iy + iheight));
471 frame_repaint(output->frame, cr);
473 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
474 fwidth, fheight - (iy + iheight),
475 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
476 cairo_image_surface_get_data(output->gl.border.bottom));
481 wayland_output_start_repaint_loop(struct weston_output *output_base)
483 struct wayland_output *output = to_wayland_output(output_base);
484 struct wayland_backend *wb =
485 to_wayland_backend(output->base.compositor);
487 /* If this is the initial frame, we need to attach a buffer so that
488 * the compositor can map the surface and include it in its render
489 * loop. If the surface doesn't end up in the render loop, the frame
490 * callback won't be invoked. The buffer is transparent and of the
491 * same size as the future real output buffer. */
492 if (output->parent.draw_initial_frame) {
493 output->parent.draw_initial_frame = false;
495 draw_initial_frame(output);
498 output->frame_cb = wl_surface_frame(output->parent.surface);
499 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
500 wl_surface_commit(output->parent.surface);
501 wl_display_flush(wb->parent.wl_display);
506 wayland_output_repaint_gl(struct weston_output *output_base,
507 pixman_region32_t *damage,
510 struct wayland_output *output = to_wayland_output(output_base);
511 struct weston_compositor *ec = output->base.compositor;
513 output->frame_cb = wl_surface_frame(output->parent.surface);
514 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
516 wayland_output_update_gl_border(output);
518 ec->renderer->repaint_output(&output->base, damage);
520 pixman_region32_subtract(&ec->primary_plane.damage,
521 &ec->primary_plane.damage, damage);
527 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
529 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
532 if (!buffer->output->frame || !buffer->frame_damaged)
535 cr = cairo_create(buffer->c_surface);
537 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
538 fwidth = frame_width(buffer->output->frame);
539 fheight = frame_height(buffer->output->frame);
541 /* Set the clip so we don't unnecisaraly damage the surface */
542 cairo_move_to(cr, ix, iy);
543 cairo_rel_line_to(cr, iwidth, 0);
544 cairo_rel_line_to(cr, 0, iheight);
545 cairo_rel_line_to(cr, -iwidth, 0);
546 cairo_line_to(cr, ix, iy);
547 cairo_line_to(cr, 0, iy);
548 cairo_line_to(cr, 0, fheight);
549 cairo_line_to(cr, fwidth, fheight);
550 cairo_line_to(cr, fwidth, 0);
551 cairo_line_to(cr, 0, 0);
552 cairo_line_to(cr, 0, iy);
553 cairo_close_path(cr);
556 /* Draw using a pattern so that the final result gets clipped */
557 cairo_push_group(cr);
558 frame_repaint(buffer->output->frame, cr);
559 cairo_pop_group_to_source(cr);
560 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
567 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
569 pixman_region32_t damage;
570 pixman_box32_t *rects;
571 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
574 pixman_region32_init(&damage);
575 pixman_region32_copy(&damage, &sb->damage);
576 pixman_region32_translate(&damage, -sb->output->base.x,
577 -sb->output->base.y);
579 weston_transformed_region(sb->output->base.width,
580 sb->output->base.height,
581 sb->output->base.transform,
582 sb->output->base.current_scale,
585 if (sb->output->frame) {
586 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
587 fwidth = frame_width(sb->output->frame);
588 fheight = frame_height(sb->output->frame);
590 pixman_region32_translate(&damage, ix, iy);
592 if (sb->frame_damaged) {
593 pixman_region32_union_rect(&damage, &damage,
595 pixman_region32_union_rect(&damage, &damage,
597 pixman_region32_union_rect(&damage, &damage,
599 fwidth - (ix + iwidth), iheight);
600 pixman_region32_union_rect(&damage, &damage,
602 fwidth, fheight - (iy + iheight));
606 rects = pixman_region32_rectangles(&damage, &n);
607 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
608 for (i = 0; i < n; ++i)
609 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
610 rects[i].y1, rects[i].x2 - rects[i].x1,
611 rects[i].y2 - rects[i].y1);
613 if (sb->output->frame)
614 pixman_region32_fini(&damage);
618 wayland_output_repaint_pixman(struct weston_output *output_base,
619 pixman_region32_t *damage,
622 struct wayland_output *output = to_wayland_output(output_base);
623 struct wayland_backend *b =
624 to_wayland_backend(output->base.compositor);
625 struct wayland_shm_buffer *sb;
628 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
629 wl_list_for_each(sb, &output->shm.buffers, link)
630 sb->frame_damaged = 1;
633 wl_list_for_each(sb, &output->shm.buffers, link)
634 pixman_region32_union(&sb->damage, &sb->damage, damage);
636 sb = wayland_output_get_shm_buffer(output);
638 wayland_output_update_shm_border(sb);
639 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
640 b->compositor->renderer->repaint_output(output_base, &sb->damage);
642 wayland_shm_buffer_attach(sb);
644 output->frame_cb = wl_surface_frame(output->parent.surface);
645 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
646 wl_surface_commit(output->parent.surface);
647 wl_display_flush(b->parent.wl_display);
649 pixman_region32_fini(&sb->damage);
650 pixman_region32_init(&sb->damage);
651 sb->frame_damaged = 0;
653 pixman_region32_subtract(&b->compositor->primary_plane.damage,
654 &b->compositor->primary_plane.damage, damage);
659 wayland_backend_destroy_output_surface(struct wayland_output *output)
661 assert(output->parent.surface);
663 if (output->parent.xdg_toplevel) {
664 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
665 output->parent.xdg_toplevel = NULL;
668 if (output->parent.xdg_surface) {
669 zxdg_surface_v6_destroy(output->parent.xdg_surface);
670 output->parent.xdg_surface = NULL;
673 if (output->parent.shell_surface) {
674 wl_shell_surface_destroy(output->parent.shell_surface);
675 output->parent.shell_surface = NULL;
678 wl_surface_destroy(output->parent.surface);
679 output->parent.surface = NULL;
683 wayland_output_destroy_shm_buffers(struct wayland_output *output)
685 struct wayland_shm_buffer *buffer, *next;
687 /* Throw away any remaining SHM buffers */
688 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
689 wayland_shm_buffer_destroy(buffer);
690 /* These will get thrown away when they get released */
691 wl_list_for_each(buffer, &output->shm.buffers, link)
692 buffer->output = NULL;
696 wayland_output_disable(struct weston_output *base)
698 struct wayland_output *output = to_wayland_output(base);
699 struct wayland_backend *b = to_wayland_backend(base->compositor);
701 if (!output->base.enabled)
705 pixman_renderer_output_destroy(&output->base);
708 gl_renderer->output_destroy(&output->base);
709 wl_egl_window_destroy(output->gl.egl_window);
713 wayland_output_destroy_shm_buffers(output);
715 wayland_backend_destroy_output_surface(output);
718 frame_destroy(output->frame);
720 cairo_surface_destroy(output->gl.border.top);
721 cairo_surface_destroy(output->gl.border.left);
722 cairo_surface_destroy(output->gl.border.right);
723 cairo_surface_destroy(output->gl.border.bottom);
729 wayland_output_destroy(struct weston_output *base)
731 struct wayland_output *output = to_wayland_output(base);
733 wayland_output_disable(&output->base);
735 weston_output_release(&output->base);
737 if (output->frame_cb)
738 wl_callback_destroy(output->frame_cb);
744 static const struct wl_shell_surface_listener shell_surface_listener;
748 wayland_output_init_gl_renderer(struct wayland_output *output)
750 int32_t fwidth = 0, fheight = 0;
753 fwidth = frame_width(output->frame);
754 fheight = frame_height(output->frame);
756 fwidth = output->base.current_mode->width;
757 fheight = output->base.current_mode->height;
760 output->gl.egl_window =
761 wl_egl_window_create(output->parent.surface,
763 if (!output->gl.egl_window) {
764 weston_log("failure to create wl_egl_window\n");
768 if (gl_renderer->output_window_create(&output->base,
769 output->gl.egl_window,
770 output->gl.egl_window,
771 gl_renderer->alpha_attribs,
779 wl_egl_window_destroy(output->gl.egl_window);
785 wayland_output_init_pixman_renderer(struct wayland_output *output)
787 return pixman_renderer_output_create(&output->base,
788 PIXMAN_RENDERER_OUTPUT_USE_SHADOW);
792 wayland_output_resize_surface(struct wayland_output *output)
794 struct wayland_backend *b =
795 to_wayland_backend(output->base.compositor);
796 int32_t ix, iy, iwidth, iheight;
797 int32_t width, height;
798 struct wl_region *region;
800 width = output->base.current_mode->width;
801 height = output->base.current_mode->height;
804 frame_resize_inside(output->frame, width, height);
806 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
807 region = wl_compositor_create_region(b->parent.compositor);
808 wl_region_add(region, ix, iy, iwidth, iheight);
809 wl_surface_set_input_region(output->parent.surface, region);
810 wl_region_destroy(region);
812 if (output->parent.xdg_surface) {
813 zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
820 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
821 region = wl_compositor_create_region(b->parent.compositor);
822 wl_region_add(region, ix, iy, iwidth, iheight);
823 wl_surface_set_opaque_region(output->parent.surface, region);
824 wl_region_destroy(region);
826 width = frame_width(output->frame);
827 height = frame_height(output->frame);
829 region = wl_compositor_create_region(b->parent.compositor);
830 wl_region_add(region, 0, 0, width, height);
831 wl_surface_set_input_region(output->parent.surface, region);
832 wl_region_destroy(region);
834 region = wl_compositor_create_region(b->parent.compositor);
835 wl_region_add(region, 0, 0, width, height);
836 wl_surface_set_opaque_region(output->parent.surface, region);
837 wl_region_destroy(region);
839 if (output->parent.xdg_surface) {
840 zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
849 if (output->gl.egl_window) {
850 wl_egl_window_resize(output->gl.egl_window,
851 width, height, 0, 0);
853 /* These will need to be re-created due to the resize */
854 gl_renderer->output_set_border(&output->base,
855 GL_RENDERER_BORDER_TOP,
857 cairo_surface_destroy(output->gl.border.top);
858 output->gl.border.top = NULL;
859 gl_renderer->output_set_border(&output->base,
860 GL_RENDERER_BORDER_LEFT,
862 cairo_surface_destroy(output->gl.border.left);
863 output->gl.border.left = NULL;
864 gl_renderer->output_set_border(&output->base,
865 GL_RENDERER_BORDER_RIGHT,
867 cairo_surface_destroy(output->gl.border.right);
868 output->gl.border.right = NULL;
869 gl_renderer->output_set_border(&output->base,
870 GL_RENDERER_BORDER_BOTTOM,
872 cairo_surface_destroy(output->gl.border.bottom);
873 output->gl.border.bottom = NULL;
877 wayland_output_destroy_shm_buffers(output);
881 wayland_output_set_windowed(struct wayland_output *output)
883 struct wayland_backend *b =
884 to_wayland_backend(output->base.compositor);
890 b->theme = theme_create();
894 output->frame = frame_create(b->theme, 100, 100,
895 FRAME_BUTTON_CLOSE, output->title, NULL);
899 if (output->keyboard_count)
900 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
902 wayland_output_resize_surface(output);
904 if (output->parent.xdg_toplevel) {
905 zxdg_toplevel_v6_unset_fullscreen(output->parent.xdg_toplevel);
906 } else if (output->parent.shell_surface) {
907 wl_shell_surface_set_toplevel(output->parent.shell_surface);
916 wayland_output_set_fullscreen(struct wayland_output *output,
917 enum wl_shell_surface_fullscreen_method method,
918 uint32_t framerate, struct wl_output *target)
921 frame_destroy(output->frame);
922 output->frame = NULL;
925 wayland_output_resize_surface(output);
927 if (output->parent.xdg_toplevel) {
928 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
929 } else if (output->parent.shell_surface) {
930 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
931 method, framerate, target);
937 static struct weston_mode *
938 wayland_output_choose_mode(struct wayland_output *output,
939 struct weston_mode *ref_mode)
941 struct weston_mode *mode;
943 /* First look for an exact match */
944 wl_list_for_each(mode, &output->base.mode_list, link)
945 if (mode->width == ref_mode->width &&
946 mode->height == ref_mode->height &&
947 mode->refresh == ref_mode->refresh)
950 /* If we can't find an exact match, ignore refresh and try again */
951 wl_list_for_each(mode, &output->base.mode_list, link)
952 if (mode->width == ref_mode->width &&
953 mode->height == ref_mode->height)
956 /* Yeah, we failed */
968 mode_feedback_successful(void *data,
969 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
971 enum mode_status *value = data;
973 printf("Mode switch successful\n");
975 *value = MODE_STATUS_SUCCESS;
979 mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
981 enum mode_status *value = data;
983 printf("Mode switch failed\n");
985 *value = MODE_STATUS_FAIL;
989 mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
991 enum mode_status *value = data;
993 printf("Mode switch cancelled\n");
995 *value = MODE_STATUS_CANCEL;
998 struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
999 mode_feedback_successful,
1000 mode_feedback_failed,
1001 mode_feedback_cancelled,
1004 static enum mode_status
1005 wayland_output_fullscreen_shell_mode_feedback(struct wayland_output *output,
1006 struct weston_mode *mode)
1008 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1009 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
1010 enum mode_status mode_status;
1014 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1015 output->parent.surface,
1016 output->parent.output,
1019 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1020 &mode_feedback_listener,
1023 output->parent.draw_initial_frame = false;
1024 draw_initial_frame(output);
1025 wl_surface_commit(output->parent.surface);
1027 mode_status = MODE_STATUS_UNKNOWN;
1028 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
1029 ret = wl_display_dispatch(b->parent.wl_display);
1031 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
1037 wayland_output_switch_mode(struct weston_output *output_base,
1038 struct weston_mode *mode)
1040 struct wayland_output *output = to_wayland_output(output_base);
1041 struct wayland_backend *b;
1042 struct wl_surface *old_surface;
1043 struct weston_mode *old_mode;
1044 enum mode_status mode_status;
1046 if (output_base == NULL) {
1047 weston_log("output is NULL.\n");
1052 weston_log("mode is NULL.\n");
1056 b = to_wayland_backend(output_base->compositor);
1058 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
1061 mode = wayland_output_choose_mode(output, mode);
1065 if (output->base.current_mode == mode)
1068 old_mode = output->base.current_mode;
1069 old_surface = output->parent.surface;
1070 output->base.current_mode = mode;
1071 output->parent.surface =
1072 wl_compositor_create_surface(b->parent.compositor);
1073 wl_surface_set_user_data(output->parent.surface, output);
1075 /* Blow the old buffers because we changed size/surfaces */
1076 wayland_output_resize_surface(output);
1078 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, mode);
1080 /* This should kick-start things again */
1081 wayland_output_start_repaint_loop(&output->base);
1083 if (mode_status == MODE_STATUS_FAIL) {
1084 output->base.current_mode = old_mode;
1085 wl_surface_destroy(output->parent.surface);
1086 output->parent.surface = old_surface;
1087 wayland_output_resize_surface(output);
1092 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1093 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1095 if (b->use_pixman) {
1096 pixman_renderer_output_destroy(output_base);
1097 if (wayland_output_init_pixman_renderer(output) < 0)
1101 gl_renderer->output_destroy(output_base);
1102 wl_egl_window_destroy(output->gl.egl_window);
1103 if (wayland_output_init_gl_renderer(output) < 0)
1107 wl_surface_destroy(old_surface);
1109 weston_output_schedule_repaint(&output->base);
1119 handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1122 zxdg_surface_v6_ack_configure(surface, serial);
1125 static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1126 handle_xdg_surface_configure
1130 handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1131 int32_t width, int32_t height,
1132 struct wl_array *states)
1134 struct wayland_output *output = data;
1136 output->parent.configure_width = width;
1137 output->parent.configure_height = height;
1139 output->parent.wait_for_configure = false;
1140 /* FIXME: implement resizing */
1144 handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1146 struct wayland_output *output = data;
1147 struct weston_compositor *compositor = output->base.compositor;
1149 wayland_output_destroy(&output->base);
1151 if (wl_list_empty(&compositor->output_list))
1152 weston_compositor_exit(compositor);
1155 static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1156 handle_xdg_toplevel_configure,
1157 handle_xdg_toplevel_close,
1161 wayland_backend_create_output_surface(struct wayland_output *output)
1163 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1165 assert(!output->parent.surface);
1167 output->parent.surface =
1168 wl_compositor_create_surface(b->parent.compositor);
1169 if (!output->parent.surface)
1172 wl_surface_set_user_data(output->parent.surface, output);
1174 output->parent.draw_initial_frame = true;
1176 if (b->parent.xdg_shell) {
1177 output->parent.xdg_surface =
1178 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1179 output->parent.surface);
1180 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1181 &xdg_surface_listener, output);
1183 output->parent.xdg_toplevel =
1184 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1185 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1186 &xdg_toplevel_listener, output);
1188 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, output->title);
1190 wl_surface_commit(output->parent.surface);
1192 output->parent.wait_for_configure = true;
1194 while (output->parent.wait_for_configure)
1195 wl_display_dispatch(b->parent.wl_display);
1197 weston_log("wayland-backend: Using xdg_shell_v6\n");
1199 else if (b->parent.shell) {
1200 output->parent.shell_surface =
1201 wl_shell_get_shell_surface(b->parent.shell,
1202 output->parent.surface);
1203 if (!output->parent.shell_surface) {
1204 wl_surface_destroy(output->parent.surface);
1208 wl_shell_surface_add_listener(output->parent.shell_surface,
1209 &shell_surface_listener, output);
1211 weston_log("wayland-backend: Using wl_shell\n");
1218 wayland_output_enable(struct weston_output *base)
1220 struct wayland_output *output = to_wayland_output(base);
1221 struct wayland_backend *b = to_wayland_backend(base->compositor);
1222 enum mode_status mode_status;
1225 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1226 output->base.current_mode->width,
1227 output->base.current_mode->height,
1228 output->base.x, output->base.y);
1230 if (!output->parent.surface)
1231 ret = wayland_backend_create_output_surface(output);
1236 wl_list_init(&output->shm.buffers);
1237 wl_list_init(&output->shm.free_buffers);
1239 if (b->use_pixman) {
1240 if (wayland_output_init_pixman_renderer(output) < 0)
1243 output->base.repaint = wayland_output_repaint_pixman;
1246 if (wayland_output_init_gl_renderer(output) < 0)
1249 output->base.repaint = wayland_output_repaint_gl;
1253 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1254 output->base.assign_planes = NULL;
1255 output->base.set_backlight = NULL;
1256 output->base.set_dpms = NULL;
1257 output->base.switch_mode = wayland_output_switch_mode;
1259 if (b->sprawl_across_outputs) {
1260 if (b->parent.fshell) {
1261 wayland_output_resize_surface(output);
1263 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, &output->mode);
1265 if (mode_status == MODE_STATUS_FAIL) {
1266 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1267 output->parent.surface,
1268 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1269 output->parent.output);
1271 output->parent.draw_initial_frame = true;
1274 wayland_output_set_fullscreen(output,
1275 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1276 output->mode.refresh, output->parent.output);
1278 } else if (b->fullscreen) {
1279 wayland_output_set_fullscreen(output, 0, 0, NULL);
1281 wayland_output_set_windowed(output);
1287 wayland_backend_destroy_output_surface(output);
1293 wayland_output_setup_for_parent_output(struct wayland_output *output,
1294 struct wayland_parent_output *poutput);
1297 wayland_output_setup_fullscreen(struct wayland_output *output,
1298 struct wayland_head *head);
1301 wayland_output_attach_head(struct weston_output *output_base,
1302 struct weston_head *head_base)
1304 struct wayland_backend *b = to_wayland_backend(output_base->compositor);
1305 struct wayland_output *output = to_wayland_output(output_base);
1306 struct wayland_head *head = to_wayland_head(head_base);
1308 if (!wl_list_empty(&output->base.head_list))
1311 if (head->parent_output) {
1312 if (wayland_output_setup_for_parent_output(output,
1313 head->parent_output) < 0)
1315 } else if (b->fullscreen) {
1316 if (wayland_output_setup_fullscreen(output, head) < 0)
1319 /* A floating window, nothing to do. */
1326 wayland_output_detach_head(struct weston_output *output_base,
1327 struct weston_head *head)
1329 struct wayland_output *output = to_wayland_output(output_base);
1331 /* Rely on the disable hook if the output was enabled. We do not
1332 * support cloned heads, so detaching is guaranteed to disable the
1335 if (output->base.enabled)
1338 /* undo setup fullscreen */
1339 if (output->parent.surface)
1340 wayland_backend_destroy_output_surface(output);
1343 static struct weston_output *
1344 wayland_output_create(struct weston_compositor *compositor, const char *name)
1346 struct wayland_output *output;
1349 /* name can't be NULL. */
1352 output = zalloc(sizeof *output);
1353 if (output == NULL) {
1358 if (asprintf(&title, "%s - %s", WINDOW_TITLE, name) < 0) {
1362 output->title = title;
1364 weston_output_init(&output->base, compositor, name);
1366 output->base.destroy = wayland_output_destroy;
1367 output->base.disable = wayland_output_disable;
1368 output->base.enable = wayland_output_enable;
1369 output->base.attach_head = wayland_output_attach_head;
1370 output->base.detach_head = wayland_output_detach_head;
1372 weston_compositor_add_pending_output(&output->base, compositor);
1374 return &output->base;
1377 static struct wayland_head *
1378 wayland_head_create(struct weston_compositor *compositor, const char *name)
1380 struct wayland_head *head;
1384 head = zalloc(sizeof *head);
1388 weston_head_init(&head->base, name);
1389 weston_head_set_connection_status(&head->base, true);
1390 weston_compositor_add_head(compositor, &head->base);
1396 wayland_head_create_windowed(struct weston_compositor *compositor,
1399 if (!wayland_head_create(compositor, name))
1406 wayland_head_create_for_parent_output(struct weston_compositor *compositor,
1407 struct wayland_parent_output *poutput)
1409 struct wayland_head *head;
1413 ret = snprintf(name, sizeof(name), "wlparent-%d", poutput->id);
1414 if (ret < 1 || (unsigned)ret >= sizeof(name))
1417 head = wayland_head_create(compositor, name);
1421 assert(!poutput->head);
1422 head->parent_output = poutput;
1423 poutput->head = head;
1425 weston_head_set_monitor_strings(&head->base,
1426 poutput->physical.make,
1427 poutput->physical.model, NULL);
1428 weston_head_set_physical_size(&head->base,
1429 poutput->physical.width,
1430 poutput->physical.height);
1436 wayland_head_destroy(struct wayland_head *head)
1438 if (head->parent_output)
1439 head->parent_output->head = NULL;
1441 weston_head_release(&head->base);
1446 wayland_output_set_size(struct weston_output *base, int width, int height)
1448 struct wayland_output *output = to_wayland_output(base);
1449 struct weston_head *head;
1450 int output_width, output_height;
1452 /* We can only be called once. */
1453 assert(!output->base.current_mode);
1455 /* Make sure we have scale set. */
1456 assert(output->base.scale);
1459 weston_log("Invalid width \"%d\" for output %s\n",
1460 width, output->base.name);
1465 weston_log("Invalid height \"%d\" for output %s\n",
1466 height, output->base.name);
1470 wl_list_for_each(head, &output->base.head_list, output_link) {
1471 weston_head_set_monitor_strings(head, "wayland", "none", NULL);
1473 /* XXX: Calculate proper size. */
1474 weston_head_set_physical_size(head, width, height);
1477 output_width = width * output->base.scale;
1478 output_height = height * output->base.scale;
1480 output->mode.flags =
1481 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1483 output->mode.width = output_width;
1484 output->mode.height = output_height;
1485 output->mode.refresh = 60000;
1486 wl_list_insert(&output->base.mode_list, &output->mode.link);
1488 output->base.current_mode = &output->mode;
1494 wayland_output_setup_for_parent_output(struct wayland_output *output,
1495 struct wayland_parent_output *poutput)
1497 struct weston_mode *mode;
1499 if (poutput->current_mode) {
1500 mode = poutput->current_mode;
1501 } else if (poutput->preferred_mode) {
1502 mode = poutput->preferred_mode;
1503 } else if (!wl_list_empty(&poutput->mode_list)) {
1504 mode = container_of(poutput->mode_list.next,
1505 struct weston_mode, link);
1507 weston_log("No valid modes found. Skipping output.\n");
1511 output->base.scale = 1;
1512 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1514 output->parent.output = poutput->global;
1516 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1517 wl_list_init(&poutput->mode_list);
1519 /* No other mode should have CURRENT already. */
1520 mode->flags |= WL_OUTPUT_MODE_CURRENT;
1521 output->base.current_mode = mode;
1523 /* output->mode is unused in this path. */
1529 wayland_output_setup_fullscreen(struct wayland_output *output,
1530 struct wayland_head *head)
1532 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
1533 int width = 0, height = 0;
1535 output->base.scale = 1;
1536 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1538 if (wayland_backend_create_output_surface(output) < 0)
1541 /* What should size be set if conditional is false? */
1542 if (b->parent.xdg_shell || b->parent.shell) {
1543 if (output->parent.xdg_toplevel)
1544 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1545 output->parent.output);
1546 else if (output->parent.shell_surface)
1547 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1550 wl_display_roundtrip(b->parent.wl_display);
1552 width = output->parent.configure_width;
1553 height = output->parent.configure_height;
1556 if (wayland_output_set_size(&output->base, width, height) < 0)
1559 /* The head is not attached yet, so set_size did not set these. */
1560 weston_head_set_monitor_strings(&head->base, "wayland", "none", NULL);
1561 /* XXX: Calculate proper size. */
1562 weston_head_set_physical_size(&head->base, width, height);
1567 wayland_backend_destroy_output_surface(output);
1573 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1576 wl_shell_surface_pong(shell_surface, serial);
1580 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1581 uint32_t edges, int32_t width, int32_t height)
1583 struct wayland_output *output = data;
1585 output->parent.configure_width = width;
1586 output->parent.configure_height = height;
1588 /* FIXME: implement resizing */
1592 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1596 static const struct wl_shell_surface_listener shell_surface_listener = {
1598 shell_surface_configure,
1599 shell_surface_popup_done
1602 /* Events received from the wayland-server this compositor is client of: */
1604 /* parent input interface */
1606 input_set_cursor(struct wayland_input *input)
1609 struct wl_buffer *buffer;
1610 struct wl_cursor_image *image;
1612 if (!input->backend->cursor)
1613 return; /* Couldn't load the cursor. Can't set it */
1615 image = input->backend->cursor->images[0];
1616 buffer = wl_cursor_image_get_buffer(image);
1620 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1621 input->parent.cursor.surface,
1622 image->hotspot_x, image->hotspot_y);
1624 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1625 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1626 image->width, image->height);
1627 wl_surface_commit(input->parent.cursor.surface);
1631 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1632 uint32_t serial, struct wl_surface *surface,
1633 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
1635 struct wayland_input *input = data;
1637 enum theme_location location;
1641 input->output = NULL;
1642 input->has_focus = false;
1643 notify_pointer_focus(&input->base, NULL, 0, 0);
1647 x = wl_fixed_to_double(fixed_x);
1648 y = wl_fixed_to_double(fixed_y);
1650 /* XXX: If we get a modifier event immediately before the focus,
1651 * we should try to keep the same serial. */
1652 input->enter_serial = serial;
1653 input->output = wl_surface_get_user_data(surface);
1655 if (input->output->frame) {
1656 location = frame_pointer_enter(input->output->frame, input,
1658 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1662 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1663 weston_output_schedule_repaint(&input->output->base);
1665 location = THEME_LOCATION_CLIENT_AREA;
1668 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1670 if (location == THEME_LOCATION_CLIENT_AREA) {
1671 input->has_focus = true;
1672 notify_pointer_focus(&input->base, &input->output->base, x, y);
1673 wl_pointer_set_cursor(input->parent.pointer,
1674 input->enter_serial, NULL, 0, 0);
1676 input->has_focus = false;
1677 notify_pointer_focus(&input->base, NULL, 0, 0);
1678 input_set_cursor(input);
1683 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1684 uint32_t serial, struct wl_surface *surface)
1686 struct wayland_input *input = data;
1691 if (input->output->frame) {
1692 frame_pointer_leave(input->output->frame, input);
1694 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1695 weston_output_schedule_repaint(&input->output->base);
1698 notify_pointer_focus(&input->base, NULL, 0, 0);
1699 input->output = NULL;
1700 input->has_focus = false;
1704 input_handle_motion(void *data, struct wl_pointer *pointer,
1705 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
1707 struct wayland_input *input = data;
1709 enum theme_location location;
1710 bool want_frame = false;
1717 x = wl_fixed_to_double(fixed_x);
1718 y = wl_fixed_to_double(fixed_y);
1720 if (input->output->frame) {
1721 location = frame_pointer_motion(input->output->frame, input,
1723 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1727 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1728 weston_output_schedule_repaint(&input->output->base);
1730 location = THEME_LOCATION_CLIENT_AREA;
1733 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1735 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
1736 input_set_cursor(input);
1737 notify_pointer_focus(&input->base, NULL, 0, 0);
1738 input->has_focus = false;
1740 } else if (!input->has_focus &&
1741 location == THEME_LOCATION_CLIENT_AREA) {
1742 wl_pointer_set_cursor(input->parent.pointer,
1743 input->enter_serial, NULL, 0, 0);
1744 notify_pointer_focus(&input->base, &input->output->base, x, y);
1745 input->has_focus = true;
1749 if (location == THEME_LOCATION_CLIENT_AREA) {
1750 timespec_from_msec(&ts, time);
1751 notify_motion_absolute(&input->base, &ts, x, y);
1755 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1756 notify_pointer_frame(&input->base);
1760 input_handle_button(void *data, struct wl_pointer *pointer,
1761 uint32_t serial, uint32_t time, uint32_t button,
1762 enum wl_pointer_button_state state)
1764 struct wayland_input *input = data;
1765 enum theme_location location;
1771 if (input->output->frame) {
1772 location = frame_pointer_button(input->output->frame, input,
1775 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1776 if (input->output->parent.xdg_toplevel)
1777 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
1778 input->parent.seat, serial);
1779 else if (input->output->parent.shell_surface)
1780 wl_shell_surface_move(input->output->parent.shell_surface,
1781 input->parent.seat, serial);
1782 frame_status_clear(input->output->frame,
1787 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1788 wayland_output_destroy(&input->output->base);
1789 input->output = NULL;
1790 input->keyboard_focus = NULL;
1792 if (wl_list_empty(&input->backend->compositor->output_list))
1793 weston_compositor_exit(input->backend->compositor);
1798 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1799 weston_output_schedule_repaint(&input->output->base);
1801 location = THEME_LOCATION_CLIENT_AREA;
1804 if (location == THEME_LOCATION_CLIENT_AREA) {
1805 timespec_from_msec(&ts, time);
1806 notify_button(&input->base, &ts, button, state);
1807 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1808 notify_pointer_frame(&input->base);
1813 input_handle_axis(void *data, struct wl_pointer *pointer,
1814 uint32_t time, uint32_t axis, wl_fixed_t value)
1816 struct wayland_input *input = data;
1817 struct weston_pointer_axis_event weston_event;
1820 weston_event.axis = axis;
1821 weston_event.value = wl_fixed_to_double(value);
1822 weston_event.has_discrete = false;
1824 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1825 input->vert.has_discrete) {
1826 weston_event.has_discrete = true;
1827 weston_event.discrete = input->vert.discrete;
1828 input->vert.has_discrete = false;
1829 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1830 input->horiz.has_discrete) {
1831 weston_event.has_discrete = true;
1832 weston_event.discrete = input->horiz.discrete;
1833 input->horiz.has_discrete = false;
1836 timespec_from_msec(&ts, time);
1838 notify_axis(&input->base, &ts, &weston_event);
1840 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1841 notify_pointer_frame(&input->base);
1845 input_handle_frame(void *data, struct wl_pointer *pointer)
1847 struct wayland_input *input = data;
1849 notify_pointer_frame(&input->base);
1853 input_handle_axis_source(void *data, struct wl_pointer *pointer,
1856 struct wayland_input *input = data;
1858 notify_axis_source(&input->base, source);
1862 input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1863 uint32_t time, uint32_t axis)
1865 struct wayland_input *input = data;
1866 struct weston_pointer_axis_event weston_event;
1869 weston_event.axis = axis;
1870 weston_event.value = 0;
1872 timespec_from_msec(&ts, time);
1874 notify_axis(&input->base, &ts, &weston_event);
1878 input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1879 uint32_t axis, int32_t discrete)
1881 struct wayland_input *input = data;
1883 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1884 input->vert.has_discrete = true;
1885 input->vert.discrete = discrete;
1886 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1887 input->horiz.has_discrete = true;
1888 input->horiz.discrete = discrete;
1892 static const struct wl_pointer_listener pointer_listener = {
1893 input_handle_pointer_enter,
1894 input_handle_pointer_leave,
1895 input_handle_motion,
1896 input_handle_button,
1899 input_handle_axis_source,
1900 input_handle_axis_stop,
1901 input_handle_axis_discrete,
1905 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1906 int fd, uint32_t size)
1908 struct wayland_input *input = data;
1909 struct xkb_keymap *keymap;
1917 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1918 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1919 if (map_str == MAP_FAILED) {
1920 weston_log("mmap failed: %m\n");
1924 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
1926 XKB_KEYMAP_FORMAT_TEXT_V1,
1928 munmap(map_str, size);
1931 weston_log("failed to compile keymap\n");
1935 input->keyboard_state_update = STATE_UPDATE_NONE;
1936 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1937 weston_log("No keymap provided; falling back to defalt\n");
1939 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1941 weston_log("Invalid keymap\n");
1947 if (weston_seat_get_keyboard(&input->base))
1948 weston_seat_update_keymap(&input->base, keymap);
1950 weston_seat_init_keyboard(&input->base, keymap);
1952 xkb_keymap_unref(keymap);
1957 wl_keyboard_release(input->parent.keyboard);
1962 input_handle_keyboard_enter(void *data,
1963 struct wl_keyboard *keyboard,
1965 struct wl_surface *surface,
1966 struct wl_array *keys)
1968 struct wayland_input *input = data;
1969 struct wayland_output *focus;
1971 focus = input->keyboard_focus;
1973 /* This shouldn't happen */
1974 focus->keyboard_count--;
1975 if (!focus->keyboard_count && focus->frame)
1976 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1977 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1978 weston_output_schedule_repaint(&focus->base);
1982 input->keyboard_focus = NULL;
1986 input->keyboard_focus = wl_surface_get_user_data(surface);
1987 input->keyboard_focus->keyboard_count++;
1989 focus = input->keyboard_focus;
1991 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1992 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1993 weston_output_schedule_repaint(&focus->base);
1997 /* XXX: If we get a modifier event immediately before the focus,
1998 * we should try to keep the same serial. */
1999 notify_keyboard_focus_in(&input->base, keys,
2000 STATE_UPDATE_AUTOMATIC);
2004 input_handle_keyboard_leave(void *data,
2005 struct wl_keyboard *keyboard,
2007 struct wl_surface *surface)
2009 struct wayland_input *input = data;
2010 struct wayland_output *focus;
2012 notify_keyboard_focus_out(&input->base);
2014 focus = input->keyboard_focus;
2018 focus->keyboard_count--;
2019 if (!focus->keyboard_count && focus->frame) {
2020 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
2021 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
2022 weston_output_schedule_repaint(&focus->base);
2025 input->keyboard_focus = NULL;
2029 input_handle_key(void *data, struct wl_keyboard *keyboard,
2030 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
2032 struct wayland_input *input = data;
2035 if (!input->keyboard_focus)
2038 timespec_from_msec(&ts, time);
2040 input->key_serial = serial;
2041 notify_key(&input->base, &ts, key,
2042 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
2043 WL_KEYBOARD_KEY_STATE_RELEASED,
2044 input->keyboard_state_update);
2048 input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
2049 uint32_t serial_in, uint32_t mods_depressed,
2050 uint32_t mods_latched, uint32_t mods_locked,
2053 struct weston_keyboard *keyboard;
2054 struct wayland_input *input = data;
2055 struct wayland_backend *b = input->backend;
2056 uint32_t serial_out;
2058 /* If we get a key event followed by a modifier event with the
2059 * same serial number, then we try to preserve those semantics by
2060 * reusing the same serial number on the way out too. */
2061 if (serial_in == input->key_serial)
2062 serial_out = wl_display_get_serial(b->compositor->wl_display);
2064 serial_out = wl_display_next_serial(b->compositor->wl_display);
2066 keyboard = weston_seat_get_keyboard(&input->base);
2067 xkb_state_update_mask(keyboard->xkb_state.state,
2068 mods_depressed, mods_latched,
2069 mods_locked, 0, 0, group);
2070 notify_modifiers(&input->base, serial_out);
2074 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2075 int32_t rate, int32_t delay)
2077 struct wayland_input *input = data;
2078 struct wayland_backend *b = input->backend;
2080 b->compositor->kb_repeat_rate = rate;
2081 b->compositor->kb_repeat_delay = delay;
2084 static const struct wl_keyboard_listener keyboard_listener = {
2085 input_handle_keymap,
2086 input_handle_keyboard_enter,
2087 input_handle_keyboard_leave,
2089 input_handle_modifiers,
2090 input_handle_repeat_info,
2094 input_handle_touch_down(void *data, struct wl_touch *wl_touch,
2095 uint32_t serial, uint32_t time,
2096 struct wl_surface *surface, int32_t id,
2097 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
2099 struct wayland_input *input = data;
2100 struct wayland_output *output;
2101 enum theme_location location;
2107 x = wl_fixed_to_double(fixed_x);
2108 y = wl_fixed_to_double(fixed_y);
2110 timespec_from_msec(&ts, time);
2112 first_touch = (input->touch_points == 0);
2113 input->touch_points++;
2115 input->touch_focus = wl_surface_get_user_data(surface);
2116 output = input->touch_focus;
2117 if (!first_touch && !input->touch_active)
2120 if (output->frame) {
2121 location = frame_touch_down(output->frame, input, id, x, y);
2123 frame_interior(output->frame, &fx, &fy, NULL, NULL);
2127 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2128 weston_output_schedule_repaint(&output->base);
2130 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
2131 input->touch_points--;
2132 if (output->parent.xdg_toplevel)
2133 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
2134 input->parent.seat, serial);
2135 else if (output->parent.shell_surface)
2136 wl_shell_surface_move(output->parent.shell_surface,
2137 input->parent.seat, serial);
2138 frame_status_clear(output->frame,
2143 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
2147 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2149 notify_touch(&input->base, &ts, id, x, y, WL_TOUCH_DOWN);
2150 input->touch_active = true;
2154 input_handle_touch_up(void *data, struct wl_touch *wl_touch,
2155 uint32_t serial, uint32_t time, int32_t id)
2157 struct wayland_input *input = data;
2158 struct wayland_output *output = input->touch_focus;
2159 bool active = input->touch_active;
2162 timespec_from_msec(&ts, time);
2164 input->touch_points--;
2165 if (input->touch_points == 0) {
2166 input->touch_focus = NULL;
2167 input->touch_active = false;
2173 if (output->frame) {
2174 frame_touch_up(output->frame, input, id);
2176 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
2177 wayland_output_destroy(&output->base);
2178 input->touch_focus = NULL;
2179 input->keyboard_focus = NULL;
2180 if (wl_list_empty(&input->backend->compositor->output_list))
2181 weston_compositor_exit(input->backend->compositor);
2185 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2186 weston_output_schedule_repaint(&output->base);
2190 notify_touch(&input->base, &ts, id, 0, 0, WL_TOUCH_UP);
2194 input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
2195 uint32_t time, int32_t id,
2196 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
2198 struct wayland_input *input = data;
2199 struct wayland_output *output = input->touch_focus;
2204 x = wl_fixed_to_double(fixed_x);
2205 y = wl_fixed_to_double(fixed_y);
2206 timespec_from_msec(&ts, time);
2208 if (!output || !input->touch_active)
2211 if (output->frame) {
2212 frame_interior(output->frame, &fx, &fy, NULL, NULL);
2217 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2219 notify_touch(&input->base, &ts, id, x, y, WL_TOUCH_MOTION);
2223 input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2225 struct wayland_input *input = data;
2227 if (!input->touch_focus || !input->touch_active)
2230 notify_touch_frame(&input->base);
2234 input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2236 struct wayland_input *input = data;
2238 if (!input->touch_focus || !input->touch_active)
2241 notify_touch_cancel(&input->base);
2244 static const struct wl_touch_listener touch_listener = {
2245 input_handle_touch_down,
2246 input_handle_touch_up,
2247 input_handle_touch_motion,
2248 input_handle_touch_frame,
2249 input_handle_touch_cancel,
2253 static struct weston_touch_device *
2254 create_touch_device(struct wayland_input *input)
2256 struct weston_touch_device *touch_device;
2259 /* manufacture a unique'ish name */
2260 snprintf(str, sizeof str, "wayland-touch[%u]",
2261 wl_proxy_get_id((struct wl_proxy *)input->parent.seat));
2263 touch_device = weston_touch_create_touch_device(input->base.touch_state,
2266 return touch_device;
2270 input_handle_capabilities(void *data, struct wl_seat *seat,
2271 enum wl_seat_capability caps)
2273 struct wayland_input *input = data;
2275 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2276 input->parent.pointer = wl_seat_get_pointer(seat);
2277 wl_pointer_set_user_data(input->parent.pointer, input);
2278 wl_pointer_add_listener(input->parent.pointer,
2279 &pointer_listener, input);
2280 weston_seat_init_pointer(&input->base);
2281 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
2282 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2283 wl_pointer_release(input->parent.pointer);
2285 wl_pointer_destroy(input->parent.pointer);
2286 input->parent.pointer = NULL;
2287 weston_seat_release_pointer(&input->base);
2290 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2291 input->parent.keyboard = wl_seat_get_keyboard(seat);
2292 wl_keyboard_set_user_data(input->parent.keyboard, input);
2293 wl_keyboard_add_listener(input->parent.keyboard,
2294 &keyboard_listener, input);
2295 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
2296 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2297 wl_keyboard_release(input->parent.keyboard);
2299 wl_keyboard_destroy(input->parent.keyboard);
2300 input->parent.keyboard = NULL;
2301 weston_seat_release_keyboard(&input->base);
2304 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2305 input->parent.touch = wl_seat_get_touch(seat);
2306 wl_touch_set_user_data(input->parent.touch, input);
2307 wl_touch_add_listener(input->parent.touch,
2308 &touch_listener, input);
2309 weston_seat_init_touch(&input->base);
2310 input->touch_device = create_touch_device(input);
2311 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2312 weston_touch_device_destroy(input->touch_device);
2313 input->touch_device = NULL;
2314 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2315 wl_touch_release(input->parent.touch);
2317 wl_touch_destroy(input->parent.touch);
2318 input->parent.touch = NULL;
2319 weston_seat_release_touch(&input->base);
2324 input_handle_name(void *data, struct wl_seat *seat,
2329 static const struct wl_seat_listener seat_listener = {
2330 input_handle_capabilities,
2335 display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
2337 struct wayland_input *input;
2338 uint32_t version = MIN(available_version, 4);
2340 input = zalloc(sizeof *input);
2344 weston_seat_init(&input->base, b->compositor, "default");
2346 input->parent.seat = wl_registry_bind(b->parent.registry, id,
2347 &wl_seat_interface, version);
2348 input->seat_version = version;
2349 wl_list_insert(b->input_list.prev, &input->link);
2351 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2352 wl_seat_set_user_data(input->parent.seat, input);
2354 input->parent.cursor.surface =
2355 wl_compositor_create_surface(b->parent.compositor);
2357 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2358 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
2362 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2363 int32_t x, int32_t y,
2364 int32_t physical_width, int32_t physical_height,
2365 int32_t subpixel, const char *make,
2366 const char *model, int32_t transform)
2368 struct wayland_parent_output *output = data;
2372 output->physical.width = physical_width;
2373 output->physical.height = physical_height;
2374 output->physical.subpixel = subpixel;
2376 free(output->physical.make);
2377 output->physical.make = strdup(make);
2378 free(output->physical.model);
2379 output->physical.model = strdup(model);
2381 output->transform = transform;
2384 static struct weston_mode *
2385 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2387 struct weston_mode *mode;
2389 wl_list_for_each(mode, list, link) {
2390 if (mode->width == width && mode->height == height &&
2391 mode->refresh == refresh)
2395 mode = zalloc(sizeof *mode);
2399 mode->width = width;
2400 mode->height = height;
2401 mode->refresh = refresh;
2402 wl_list_insert(list, &mode->link);
2407 static struct weston_output *
2408 wayland_parent_output_get_enabled_output(struct wayland_parent_output *poutput)
2410 struct wayland_head *head = poutput->head;
2415 if (!weston_head_is_enabled(&head->base))
2418 return weston_head_get_output(&head->base);
2422 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2423 uint32_t flags, int32_t width, int32_t height,
2426 struct wayland_parent_output *output = data;
2427 struct weston_output *enabled_output;
2428 struct weston_mode *mode;
2430 enabled_output = wayland_parent_output_get_enabled_output(output);
2431 if (enabled_output) {
2432 mode = find_mode(&enabled_output->mode_list,
2433 width, height, refresh);
2436 mode->flags = flags;
2437 /* Do a mode-switch on current mode change? */
2439 mode = find_mode(&output->mode_list, width, height, refresh);
2442 mode->flags = flags;
2443 if (flags & WL_OUTPUT_MODE_CURRENT)
2444 output->current_mode = mode;
2445 if (flags & WL_OUTPUT_MODE_PREFERRED)
2446 output->preferred_mode = mode;
2450 static const struct wl_output_listener output_listener = {
2451 wayland_parent_output_geometry,
2452 wayland_parent_output_mode
2456 output_sync_callback(void *data, struct wl_callback *callback, uint32_t unused)
2458 struct wayland_parent_output *output = data;
2460 assert(output->sync_cb == callback);
2461 wl_callback_destroy(callback);
2462 output->sync_cb = NULL;
2464 assert(output->backend->sprawl_across_outputs);
2466 wayland_head_create_for_parent_output(output->backend->compositor, output);
2469 static const struct wl_callback_listener output_sync_listener = {
2470 output_sync_callback
2474 wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
2476 struct wayland_parent_output *output;
2478 output = zalloc(sizeof *output);
2482 output->backend = b;
2484 output->global = wl_registry_bind(b->parent.registry, id,
2485 &wl_output_interface, 1);
2486 if (!output->global) {
2491 wl_output_add_listener(output->global, &output_listener, output);
2494 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2495 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2496 wl_list_init(&output->mode_list);
2497 wl_list_insert(&b->parent.output_list, &output->link);
2499 if (b->sprawl_across_outputs) {
2500 output->sync_cb = wl_display_sync(b->parent.wl_display);
2501 wl_callback_add_listener(output->sync_cb,
2502 &output_sync_listener, output);
2507 wayland_parent_output_destroy(struct wayland_parent_output *output)
2509 struct weston_mode *mode, *next;
2511 if (output->sync_cb)
2512 wl_callback_destroy(output->sync_cb);
2515 wayland_head_destroy(output->head);
2517 wl_output_destroy(output->global);
2518 free(output->physical.make);
2519 free(output->physical.model);
2521 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2522 wl_list_remove(&mode->link);
2526 wl_list_remove(&output->link);
2531 xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2533 zxdg_shell_v6_pong(shell, serial);
2536 static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2541 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2542 const char *interface, uint32_t version)
2544 struct wayland_backend *b = data;
2546 if (strcmp(interface, "wl_compositor") == 0) {
2547 b->parent.compositor =
2548 wl_registry_bind(registry, name,
2549 &wl_compositor_interface,
2551 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2552 b->parent.xdg_shell =
2553 wl_registry_bind(registry, name,
2554 &zxdg_shell_v6_interface, 1);
2555 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2556 &xdg_shell_listener, b);
2557 } else if (strcmp(interface, "wl_shell") == 0) {
2559 wl_registry_bind(registry, name,
2560 &wl_shell_interface, 1);
2561 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
2563 wl_registry_bind(registry, name,
2564 &zwp_fullscreen_shell_v1_interface, 1);
2565 } else if (strcmp(interface, "wl_seat") == 0) {
2566 display_add_seat(b, name, version);
2567 } else if (strcmp(interface, "wl_output") == 0) {
2568 wayland_backend_register_output(b, name);
2569 } else if (strcmp(interface, "wl_shm") == 0) {
2571 wl_registry_bind(registry, name, &wl_shm_interface, 1);
2576 registry_handle_global_remove(void *data, struct wl_registry *registry,
2579 struct wayland_backend *b = data;
2580 struct wayland_parent_output *output, *next;
2582 wl_list_for_each_safe(output, next, &b->parent.output_list, link)
2583 if (output->id == name)
2584 wayland_parent_output_destroy(output);
2587 static const struct wl_registry_listener registry_listener = {
2588 registry_handle_global,
2589 registry_handle_global_remove
2593 wayland_backend_handle_event(int fd, uint32_t mask, void *data)
2595 struct wayland_backend *b = data;
2598 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
2599 weston_compositor_exit(b->compositor);
2603 if (mask & WL_EVENT_READABLE)
2604 count = wl_display_dispatch(b->parent.wl_display);
2605 if (mask & WL_EVENT_WRITABLE)
2606 wl_display_flush(b->parent.wl_display);
2609 count = wl_display_dispatch_pending(b->parent.wl_display);
2610 wl_display_flush(b->parent.wl_display);
2617 wayland_destroy(struct weston_compositor *ec)
2619 struct wayland_backend *b = to_wayland_backend(ec);
2620 struct weston_head *base, *next;
2622 wl_event_source_remove(b->parent.wl_source);
2624 weston_compositor_shutdown(ec);
2626 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2627 wayland_head_destroy(to_wayland_head(base));
2630 wl_shm_destroy(b->parent.shm);
2632 if (b->parent.xdg_shell)
2633 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2635 if (b->parent.shell)
2636 wl_shell_destroy(b->parent.shell);
2638 if (b->parent.fshell)
2639 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2641 if (b->parent.compositor)
2642 wl_compositor_destroy(b->parent.compositor);
2645 theme_destroy(b->theme);
2647 if (b->frame_device)
2648 cairo_device_destroy(b->frame_device);
2650 wl_cursor_theme_destroy(b->cursor_theme);
2652 wl_registry_destroy(b->parent.registry);
2653 wl_display_flush(b->parent.wl_display);
2654 wl_display_disconnect(b->parent.wl_display);
2659 static const char *left_ptrs[] = {
2667 create_cursor(struct wayland_backend *b,
2668 struct weston_wayland_backend_config *config)
2672 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2673 config->cursor_size,
2675 if (!b->cursor_theme) {
2676 fprintf(stderr, "could not load cursor theme\n");
2681 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2682 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
2685 fprintf(stderr, "could not load left cursor\n");
2691 fullscreen_binding(struct weston_keyboard *keyboard,
2692 const struct timespec *time, uint32_t key, void *data)
2694 struct wayland_backend *b = data;
2695 struct wayland_input *input = NULL;
2697 wl_list_for_each(input, &b->input_list, link)
2698 if (&input->base == keyboard->seat)
2701 if (!input || !input->output)
2704 if (input->output->frame)
2705 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2707 wayland_output_set_windowed(input->output);
2709 weston_output_schedule_repaint(&input->output->base);
2712 static struct wayland_backend *
2713 wayland_backend_create(struct weston_compositor *compositor,
2714 struct weston_wayland_backend_config *new_config)
2716 struct wayland_backend *b;
2717 struct wl_event_loop *loop;
2720 b = zalloc(sizeof *b);
2724 b->compositor = compositor;
2725 compositor->backend = &b->base;
2727 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
2728 goto err_compositor;
2730 b->parent.wl_display = wl_display_connect(new_config->display_name);
2731 if (b->parent.wl_display == NULL) {
2732 weston_log("Error: Failed to connect to parent Wayland compositor: %m\n");
2733 weston_log_continue(STAMP_SPACE "display option: %s, WAYLAND_DISPLAY=%s\n",
2734 new_config->display_name ?: "(none)",
2735 getenv("WAYLAND_DISPLAY") ?: "(not set)");
2736 goto err_compositor;
2739 wl_list_init(&b->parent.output_list);
2740 wl_list_init(&b->input_list);
2741 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2742 wl_registry_add_listener(b->parent.registry, ®istry_listener, b);
2743 wl_display_roundtrip(b->parent.wl_display);
2745 create_cursor(b, new_config);
2748 b->use_pixman = new_config->use_pixman;
2750 b->use_pixman = true;
2752 b->fullscreen = new_config->fullscreen;
2754 if (!b->use_pixman) {
2755 gl_renderer = weston_load_module("gl-renderer.so",
2756 "gl_renderer_interface");
2758 b->use_pixman = true;
2761 if (!b->use_pixman) {
2762 if (gl_renderer->display_create(compositor,
2763 EGL_PLATFORM_WAYLAND_KHR,
2764 b->parent.wl_display,
2766 gl_renderer->alpha_attribs,
2769 weston_log("Failed to initialize the GL renderer; "
2770 "falling back to pixman.\n");
2771 b->use_pixman = true;
2775 if (b->use_pixman) {
2776 if (pixman_renderer_init(compositor) < 0) {
2777 weston_log("Failed to initialize pixman renderer\n");
2782 b->base.destroy = wayland_destroy;
2783 b->base.create_output = wayland_output_create;
2785 loop = wl_display_get_event_loop(compositor->wl_display);
2787 fd = wl_display_get_fd(b->parent.wl_display);
2788 b->parent.wl_source =
2789 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2790 wayland_backend_handle_event, b);
2791 if (b->parent.wl_source == NULL)
2794 wl_event_source_check(b->parent.wl_source);
2796 if (compositor->renderer->import_dmabuf) {
2797 if (linux_dmabuf_setup(compositor) < 0)
2798 weston_log("Error: initializing dmabuf "
2799 "support failed.\n");
2804 wl_display_disconnect(b->parent.wl_display);
2806 weston_compositor_shutdown(compositor);
2812 wayland_backend_destroy(struct wayland_backend *b)
2814 wl_display_disconnect(b->parent.wl_display);
2817 theme_destroy(b->theme);
2818 if (b->frame_device)
2819 cairo_device_destroy(b->frame_device);
2820 wl_cursor_theme_destroy(b->cursor_theme);
2822 weston_compositor_shutdown(b->compositor);
2826 static const struct weston_windowed_output_api windowed_api = {
2827 wayland_output_set_size,
2828 wayland_head_create_windowed,
2832 config_init_to_defaults(struct weston_wayland_backend_config *config)
2837 weston_backend_init(struct weston_compositor *compositor,
2838 struct weston_backend_config *config_base)
2840 struct wayland_backend *b;
2841 struct wayland_parent_output *poutput;
2842 struct weston_wayland_backend_config new_config;
2845 if (config_base == NULL ||
2846 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2847 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2848 weston_log("wayland backend config structure is invalid\n");
2852 config_init_to_defaults(&new_config);
2853 memcpy(&new_config, config_base, config_base->struct_size);
2855 b = wayland_backend_create(compositor, &new_config);
2860 if (new_config.sprawl || b->parent.fshell) {
2861 b->sprawl_across_outputs = true;
2862 wl_display_roundtrip(b->parent.wl_display);
2864 wl_list_for_each(poutput, &b->parent.output_list, link)
2865 wayland_head_create_for_parent_output(compositor, poutput);
2870 if (new_config.fullscreen) {
2871 if (!wayland_head_create(compositor, "wayland-fullscreen")) {
2872 weston_log("Unable to create a fullscreen head.\n");
2879 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2880 &windowed_api, sizeof(windowed_api));
2883 weston_log("Failed to register output API.\n");
2884 wayland_backend_destroy(b);
2888 weston_compositor_add_key_binding(compositor, KEY_F,
2889 MODIFIER_CTRL | MODIFIER_ALT,
2890 fullscreen_binding, b);
2894 wayland_backend_destroy(b);