2 * Copyright © 2010-2011 Benjamin Franzke
3 * Copyright © 2013 Jason Ekstrand
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 #include <wayland-client.h>
35 #include <wayland-egl.h>
36 #include <wayland-cursor.h>
38 #include "compositor.h"
39 #include "gl-renderer.h"
40 #include "pixman-renderer.h"
41 #include "../shared/image-loader.h"
42 #include "../shared/os-compatibility.h"
43 #include "../shared/cairo-util.h"
45 #define WINDOW_TITLE "Weston Compositor"
47 struct wayland_compositor {
48 struct weston_compositor base;
51 struct wl_display *wl_display;
52 struct wl_registry *registry;
53 struct wl_compositor *compositor;
54 struct wl_shell *shell;
57 struct wl_event_source *wl_source;
64 cairo_device_t *frame_device;
65 struct wl_cursor_theme *cursor_theme;
66 struct wl_cursor *cursor;
68 struct wl_list input_list;
71 struct wayland_output {
72 struct weston_output base;
75 int draw_initial_frame;
76 struct wl_surface *surface;
77 struct wl_shell_surface *shell_surface;
85 struct wl_egl_window *egl_window;
88 cairo_surface_t *left;
89 cairo_surface_t *right;
90 cairo_surface_t *bottom;
95 struct wl_list buffers;
96 struct wl_list free_buffers;
99 struct weston_mode mode;
103 struct wayland_shm_buffer {
104 struct wayland_output *output;
106 struct wl_list free_link;
108 struct wl_buffer *buffer;
111 pixman_region32_t damage;
114 pixman_image_t *pm_image;
115 cairo_surface_t *c_surface;
118 struct wayland_input {
119 struct weston_seat base;
120 struct wayland_compositor *compositor;
124 struct wl_seat *seat;
125 struct wl_pointer *pointer;
126 struct wl_keyboard *keyboard;
127 struct wl_touch *touch;
130 struct wl_surface *surface;
136 uint32_t enter_serial;
138 struct wayland_output *output;
139 struct wayland_output *keyboard_focus;
142 struct gl_renderer_interface *gl_renderer;
145 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
147 cairo_surface_destroy(buffer->c_surface);
148 pixman_image_unref(buffer->pm_image);
150 wl_buffer_destroy(buffer->buffer);
151 munmap(buffer->data, buffer->size);
153 pixman_region32_fini(&buffer->damage);
155 wl_list_remove(&buffer->link);
156 wl_list_remove(&buffer->free_link);
161 buffer_release(void *data, struct wl_buffer *buffer)
163 struct wayland_shm_buffer *sb = data;
166 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
168 wayland_shm_buffer_destroy(sb);
172 static const struct wl_buffer_listener buffer_listener = {
176 static struct wayland_shm_buffer *
177 wayland_output_get_shm_buffer(struct wayland_output *output)
179 struct wayland_compositor *c =
180 (struct wayland_compositor *) output->base.compositor;
181 struct wl_shm *shm = c->parent.shm;
182 struct wayland_shm_buffer *sb;
184 struct wl_shm_pool *pool;
185 int width, height, stride;
190 if (!wl_list_empty(&output->shm.free_buffers)) {
191 sb = container_of(output->shm.free_buffers.next,
192 struct wayland_shm_buffer, free_link);
193 wl_list_remove(&sb->free_link);
194 wl_list_init(&sb->free_link);
200 width = frame_width(output->frame);
201 height = frame_height(output->frame);
203 width = output->base.current_mode->width;
204 height = output->base.current_mode->height;
207 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
209 fd = os_create_anonymous_file(height * stride);
211 perror("os_create_anonymous_file");
215 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
216 if (data == MAP_FAILED) {
222 sb = zalloc(sizeof *sb);
225 wl_list_init(&sb->free_link);
226 wl_list_insert(&output->shm.buffers, &sb->link);
228 pixman_region32_init_rect(&sb->damage, 0, 0,
229 output->base.width, output->base.height);
230 sb->frame_damaged = 1;
233 sb->size = height * stride;
235 pool = wl_shm_create_pool(shm, fd, sb->size);
237 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
240 WL_SHM_FORMAT_ARGB8888);
241 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
242 wl_shm_pool_destroy(pool);
245 memset(data, 0, sb->size);
248 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
249 width, height, stride);
254 frame_interior(output->frame, &fx, &fy, 0, 0);
256 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
257 (uint32_t *)(data + fy * stride) + fx,
264 frame_done(void *data, struct wl_callback *callback, uint32_t time)
266 struct weston_output *output = data;
268 wl_callback_destroy(callback);
269 weston_output_finish_frame(output, time);
272 static const struct wl_callback_listener frame_listener = {
277 draw_initial_frame(struct wayland_output *output)
279 struct wayland_shm_buffer *sb;
281 sb = wayland_output_get_shm_buffer(output);
283 /* If we are rendering with GL, then orphan it so that it gets
284 * destroyed immediately */
285 if (output->gl.egl_window)
288 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
290 /* We only need to damage some part, as its only transparant
292 wl_surface_damage(output->parent.surface, 0, 0, 1, 1);
296 wayland_output_update_gl_border(struct wayland_output *output)
298 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
303 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
306 fwidth = frame_width(output->frame);
307 fheight = frame_height(output->frame);
308 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
310 if (!output->gl.border.top)
311 output->gl.border.top =
312 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
314 cr = cairo_create(output->gl.border.top);
315 frame_repaint(output->frame, cr);
317 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
319 cairo_image_surface_get_stride(output->gl.border.top) / 4,
320 cairo_image_surface_get_data(output->gl.border.top));
323 if (!output->gl.border.left)
324 output->gl.border.left =
325 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
327 cr = cairo_create(output->gl.border.left);
328 cairo_translate(cr, 0, -iy);
329 frame_repaint(output->frame, cr);
331 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
333 cairo_image_surface_get_stride(output->gl.border.left) / 4,
334 cairo_image_surface_get_data(output->gl.border.left));
337 if (!output->gl.border.right)
338 output->gl.border.right =
339 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
340 fwidth - (ix + iwidth), 1);
341 cr = cairo_create(output->gl.border.right);
342 cairo_translate(cr, -(iwidth + ix), -iy);
343 frame_repaint(output->frame, cr);
345 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
346 fwidth - (ix + iwidth), 1,
347 cairo_image_surface_get_stride(output->gl.border.right) / 4,
348 cairo_image_surface_get_data(output->gl.border.right));
351 if (!output->gl.border.bottom)
352 output->gl.border.bottom =
353 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
354 fwidth, fheight - (iy + iheight));
355 cr = cairo_create(output->gl.border.bottom);
356 cairo_translate(cr, 0, -(iy + iheight));
357 frame_repaint(output->frame, cr);
359 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
360 fwidth, fheight - (iy + iheight),
361 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
362 cairo_image_surface_get_data(output->gl.border.bottom));
366 wayland_output_start_repaint_loop(struct weston_output *output_base)
368 struct wayland_output *output = (struct wayland_output *) output_base;
369 struct wayland_compositor *wc =
370 (struct wayland_compositor *)output->base.compositor;
371 struct wl_callback *callback;
373 /* If this is the initial frame, we need to attach a buffer so that
374 * the compositor can map the surface and include it in its render
375 * loop. If the surface doesn't end up in the render loop, the frame
376 * callback won't be invoked. The buffer is transparent and of the
377 * same size as the future real output buffer. */
378 if (output->parent.draw_initial_frame) {
379 output->parent.draw_initial_frame = 0;
381 draw_initial_frame(output);
384 callback = wl_surface_frame(output->parent.surface);
385 wl_callback_add_listener(callback, &frame_listener, output);
386 wl_surface_commit(output->parent.surface);
387 wl_display_flush(wc->parent.wl_display);
391 wayland_output_repaint_gl(struct weston_output *output_base,
392 pixman_region32_t *damage)
394 struct wayland_output *output = (struct wayland_output *) output_base;
395 struct weston_compositor *ec = output->base.compositor;
396 struct wl_callback *callback;
398 callback = wl_surface_frame(output->parent.surface);
399 wl_callback_add_listener(callback, &frame_listener, output);
401 wayland_output_update_gl_border(output);
403 ec->renderer->repaint_output(&output->base, damage);
405 pixman_region32_subtract(&ec->primary_plane.damage,
406 &ec->primary_plane.damage, damage);
411 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
413 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
416 if (!buffer->output->frame || !buffer->frame_damaged)
419 cr = cairo_create(buffer->c_surface);
421 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
422 fwidth = frame_width(buffer->output->frame);
423 fheight = frame_height(buffer->output->frame);
425 /* Set the clip so we don't unnecisaraly damage the surface */
426 cairo_move_to(cr, ix, iy);
427 cairo_rel_line_to(cr, iwidth, 0);
428 cairo_rel_line_to(cr, 0, iheight);
429 cairo_rel_line_to(cr, -iwidth, 0);
430 cairo_line_to(cr, ix, iy);
431 cairo_line_to(cr, 0, iy);
432 cairo_line_to(cr, 0, fheight);
433 cairo_line_to(cr, fwidth, fheight);
434 cairo_line_to(cr, fwidth, 0);
435 cairo_line_to(cr, 0, 0);
436 cairo_line_to(cr, 0, iy);
437 cairo_close_path(cr);
440 /* Draw using a pattern so that the final result gets clipped */
441 cairo_push_group(cr);
442 frame_repaint(buffer->output->frame, cr);
443 cairo_pop_group_to_source(cr);
444 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
451 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
453 pixman_region32_t damage;
454 pixman_box32_t *rects;
455 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
458 pixman_region32_init(&damage);
459 weston_transformed_region(sb->output->base.width,
460 sb->output->base.height,
461 sb->output->base.transform,
462 sb->output->base.current_scale,
463 &sb->damage, &damage);
465 if (sb->output->frame) {
466 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
467 fwidth = frame_width(sb->output->frame);
468 fheight = frame_height(sb->output->frame);
470 pixman_region32_translate(&damage, ix, iy);
472 if (sb->frame_damaged) {
473 pixman_region32_union_rect(&damage, &damage,
475 pixman_region32_union_rect(&damage, &damage,
477 pixman_region32_union_rect(&damage, &damage,
479 fwidth - (ix + iwidth), iheight);
480 pixman_region32_union_rect(&damage, &damage,
482 fwidth, fheight - (iy + iheight));
486 rects = pixman_region32_rectangles(&damage, &n);
487 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
488 for (i = 0; i < n; ++i)
489 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
490 rects[i].y1, rects[i].x2 - rects[i].x1,
491 rects[i].y2 - rects[i].y1);
493 if (sb->output->frame)
494 pixman_region32_fini(&damage);
498 wayland_output_repaint_pixman(struct weston_output *output_base,
499 pixman_region32_t *damage)
501 struct wayland_output *output = (struct wayland_output *) output_base;
502 struct wayland_compositor *c =
503 (struct wayland_compositor *)output->base.compositor;
504 struct wl_callback *callback;
505 struct wayland_shm_buffer *sb;
508 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
509 wl_list_for_each(sb, &output->shm.buffers, link)
510 sb->frame_damaged = 1;
513 wl_list_for_each(sb, &output->shm.buffers, link)
514 pixman_region32_union(&sb->damage, &sb->damage, damage);
516 sb = wayland_output_get_shm_buffer(output);
518 wayland_output_update_shm_border(sb);
519 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
520 c->base.renderer->repaint_output(output_base, &sb->damage);
522 wayland_shm_buffer_attach(sb);
524 callback = wl_surface_frame(output->parent.surface);
525 wl_callback_add_listener(callback, &frame_listener, output);
526 wl_surface_commit(output->parent.surface);
527 wl_display_flush(c->parent.wl_display);
529 pixman_region32_fini(&sb->damage);
530 pixman_region32_init(&sb->damage);
531 sb->frame_damaged = 0;
533 pixman_region32_subtract(&c->base.primary_plane.damage,
534 &c->base.primary_plane.damage, damage);
539 wayland_output_destroy(struct weston_output *output_base)
541 struct wayland_output *output = (struct wayland_output *) output_base;
542 struct wayland_compositor *c =
543 (struct wayland_compositor *) output->base.compositor;
546 pixman_renderer_output_destroy(output_base);
548 gl_renderer->output_destroy(output_base);
551 wl_egl_window_destroy(output->gl.egl_window);
552 wl_surface_destroy(output->parent.surface);
553 wl_shell_surface_destroy(output->parent.shell_surface);
556 frame_destroy(output->frame);
557 cairo_surface_destroy(output->gl.border.top);
558 cairo_surface_destroy(output->gl.border.left);
559 cairo_surface_destroy(output->gl.border.right);
560 cairo_surface_destroy(output->gl.border.bottom);
563 weston_output_destroy(&output->base);
569 static const struct wl_shell_surface_listener shell_surface_listener;
572 wayland_output_init_gl_renderer(struct wayland_output *output)
574 int32_t fwidth = 0, fheight = 0;
577 fwidth = frame_width(output->frame);
578 fheight = frame_height(output->frame);
580 fwidth = output->base.current_mode->width;
581 fheight = output->base.current_mode->height;
584 output->gl.egl_window =
585 wl_egl_window_create(output->parent.surface,
587 if (!output->gl.egl_window) {
588 weston_log("failure to create wl_egl_window\n");
592 if (gl_renderer->output_create(&output->base,
593 output->gl.egl_window) < 0)
599 wl_egl_window_destroy(output->gl.egl_window);
604 wayland_output_init_pixman_renderer(struct wayland_output *output)
606 return pixman_renderer_output_create(&output->base);
609 static struct wayland_output *
610 wayland_output_create(struct wayland_compositor *c, int x, int y,
611 int width, int height, const char *name,
612 uint32_t transform, int32_t scale)
614 struct wayland_output *output;
615 int32_t fx, fy, fwidth, fheight;
616 int output_width, output_height;
617 struct wl_region *region;
619 output = zalloc(sizeof *output);
623 output_width = width * scale;
624 output_height = height * scale;
627 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
628 output->mode.width = output_width;
629 output->mode.height = output_height;
630 output->mode.refresh = 60000;
631 output->scale = scale;
632 wl_list_init(&output->base.mode_list);
633 wl_list_insert(&output->base.mode_list, &output->mode.link);
635 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
636 width, height, x, y);
638 output->base.current_mode = &output->mode;
639 weston_output_init(&output->base, &c->base, x, y, width, height,
642 output->base.make = "waywayland";
643 output->base.model = "none";
645 wl_list_init(&output->shm.buffers);
646 wl_list_init(&output->shm.free_buffers);
649 c->theme = theme_create();
650 output->frame = frame_create(c->theme, width, height,
651 FRAME_BUTTON_CLOSE, name);
652 frame_resize_inside(output->frame, output_width, output_height);
654 output->parent.surface =
655 wl_compositor_create_surface(c->parent.compositor);
656 wl_surface_set_user_data(output->parent.surface, output);
659 if (wayland_output_init_pixman_renderer(output) < 0)
661 output->base.repaint = wayland_output_repaint_pixman;
663 if (wayland_output_init_gl_renderer(output) < 0)
665 output->base.repaint = wayland_output_repaint_gl;
668 frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight);
669 region = wl_compositor_create_region(c->parent.compositor);
670 wl_region_add(region, fx, fy, fwidth, fheight);
671 wl_surface_set_input_region(output->parent.surface, region);
672 wl_region_destroy(region);
674 frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight);
675 region = wl_compositor_create_region(c->parent.compositor);
676 wl_region_add(region, fx, fy, fwidth, fheight);
677 wl_surface_set_opaque_region(output->parent.surface, region);
678 wl_region_destroy(region);
680 output->parent.draw_initial_frame = 1;
681 output->parent.shell_surface =
682 wl_shell_get_shell_surface(c->parent.shell,
683 output->parent.surface);
684 wl_shell_surface_add_listener(output->parent.shell_surface,
685 &shell_surface_listener, output);
686 wl_shell_surface_set_toplevel(output->parent.shell_surface);
688 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
689 output->base.destroy = wayland_output_destroy;
690 output->base.assign_planes = NULL;
691 output->base.set_backlight = NULL;
692 output->base.set_dpms = NULL;
693 output->base.switch_mode = NULL;
695 wl_list_insert(c->base.output_list.prev, &output->base.link);
700 /* FIXME: cleanup weston_output */
706 static struct wayland_output *
707 wayland_output_create_for_config(struct wayland_compositor *c,
708 struct weston_config_section *config_section,
709 int option_width, int option_height,
710 int32_t x, int32_t y)
712 struct wayland_output *output;
713 char *mode, *t, *name, *str;
714 int width, height, scale;
716 unsigned int i, slen;
718 static const struct { const char *name; uint32_t token; } transform_names[] = {
719 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
720 { "90", WL_OUTPUT_TRANSFORM_90 },
721 { "180", WL_OUTPUT_TRANSFORM_180 },
722 { "270", WL_OUTPUT_TRANSFORM_270 },
723 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
724 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
725 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
726 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
729 weston_config_section_get_string(config_section, "name", &name, NULL);
732 slen += strlen(WINDOW_TITLE " - ");
733 str = malloc(slen + 1);
735 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
742 weston_config_section_get_string(config_section,
743 "mode", &mode, "1024x600");
744 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
745 weston_log("Invalid mode \"%s\" for output %s\n",
753 width = option_width;
755 height = option_height;
757 weston_config_section_get_int(config_section, "scale", &scale, 1);
759 weston_config_section_get_string(config_section,
760 "transform", &t, "normal");
761 transform = WL_OUTPUT_TRANSFORM_NORMAL;
762 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
763 if (strcmp(transform_names[i].name, t) == 0) {
764 transform = transform_names[i].token;
768 if (i >= ARRAY_LENGTH(transform_names))
769 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
772 output = wayland_output_create(c, x, y, width, height, name, transform, scale);
779 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
782 wl_shell_surface_pong(shell_surface, serial);
786 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
787 uint32_t edges, int32_t width, int32_t height)
789 /* FIXME: implement resizing */
793 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
797 static const struct wl_shell_surface_listener shell_surface_listener = {
799 shell_surface_configure,
800 shell_surface_popup_done
803 /* Events received from the wayland-server this compositor is client of: */
805 /* parent input interface */
807 input_set_cursor(struct wayland_input *input)
810 struct wl_buffer *buffer;
811 struct wl_cursor_image *image;
813 if (!input->compositor->cursor)
814 return; /* Couldn't load the cursor. Can't set it */
816 image = input->compositor->cursor->images[0];
817 buffer = wl_cursor_image_get_buffer(image);
820 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
821 input->parent.cursor.surface,
822 image->hotspot_x, image->hotspot_y);
824 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
825 wl_surface_damage(input->parent.cursor.surface, 0, 0,
826 image->width, image->height);
827 wl_surface_commit(input->parent.cursor.surface);
831 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
832 uint32_t serial, struct wl_surface *surface,
833 wl_fixed_t x, wl_fixed_t y)
835 struct wayland_input *input = data;
837 enum theme_location location;
839 /* XXX: If we get a modifier event immediately before the focus,
840 * we should try to keep the same serial. */
841 input->enter_serial = serial;
842 input->output = wl_surface_get_user_data(surface);
844 if (input->output->frame) {
845 location = frame_pointer_enter(input->output->frame, input,
848 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
849 x -= wl_fixed_from_int(fx);
850 y -= wl_fixed_from_int(fy);
852 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
853 weston_output_schedule_repaint(&input->output->base);
855 location = THEME_LOCATION_CLIENT_AREA;
858 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
860 if (location == THEME_LOCATION_CLIENT_AREA) {
862 notify_pointer_focus(&input->base, &input->output->base, x, y);
863 wl_pointer_set_cursor(input->parent.pointer,
864 input->enter_serial, NULL, 0, 0);
867 notify_pointer_focus(&input->base, NULL, 0, 0);
868 input_set_cursor(input);
873 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
874 uint32_t serial, struct wl_surface *surface)
876 struct wayland_input *input = data;
878 if (input->output->frame) {
879 frame_pointer_leave(input->output->frame, input);
881 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
882 weston_output_schedule_repaint(&input->output->base);
885 notify_pointer_focus(&input->base, NULL, 0, 0);
886 input->output = NULL;
891 input_handle_motion(void *data, struct wl_pointer *pointer,
892 uint32_t time, wl_fixed_t x, wl_fixed_t y)
894 struct wayland_input *input = data;
896 enum theme_location location;
898 if (input->output->frame) {
899 location = frame_pointer_motion(input->output->frame, input,
902 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
903 x -= wl_fixed_from_int(fx);
904 y -= wl_fixed_from_int(fy);
906 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
907 weston_output_schedule_repaint(&input->output->base);
909 location = THEME_LOCATION_CLIENT_AREA;
912 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
914 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
915 input_set_cursor(input);
916 notify_pointer_focus(&input->base, NULL, 0, 0);
918 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
919 wl_pointer_set_cursor(input->parent.pointer,
920 input->enter_serial, NULL, 0, 0);
921 notify_pointer_focus(&input->base, &input->output->base, x, y);
925 if (location == THEME_LOCATION_CLIENT_AREA)
926 notify_motion_absolute(&input->base, time, x, y);
930 input_handle_button(void *data, struct wl_pointer *pointer,
931 uint32_t serial, uint32_t time, uint32_t button,
934 struct wayland_input *input = data;
935 enum wl_pointer_button_state state = state_w;
936 enum frame_button_state fstate;
937 enum theme_location location;
939 if (input->output->frame) {
940 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
941 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
943 location = frame_pointer_button(input->output->frame, input,
946 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
948 wl_shell_surface_move(input->output->parent.shell_surface,
949 input->parent.seat, serial);
950 frame_status_clear(input->output->frame,
955 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
956 wl_display_terminate(input->compositor->base.wl_display);
958 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
959 weston_output_schedule_repaint(&input->output->base);
961 location = THEME_LOCATION_CLIENT_AREA;
964 if (location == THEME_LOCATION_CLIENT_AREA)
965 notify_button(&input->base, time, button, state);
969 input_handle_axis(void *data, struct wl_pointer *pointer,
970 uint32_t time, uint32_t axis, wl_fixed_t value)
972 struct wayland_input *input = data;
974 notify_axis(&input->base, time, axis, value);
977 static const struct wl_pointer_listener pointer_listener = {
978 input_handle_pointer_enter,
979 input_handle_pointer_leave,
986 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
987 int fd, uint32_t size)
989 struct wayland_input *input = data;
990 struct xkb_keymap *keymap;
998 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1003 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1004 if (map_str == MAP_FAILED) {
1009 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1011 XKB_KEYMAP_FORMAT_TEXT_V1,
1013 munmap(map_str, size);
1017 weston_log("failed to compile keymap\n");
1021 if (input->base.keyboard)
1022 weston_seat_update_keymap(&input->base, keymap);
1024 weston_seat_init_keyboard(&input->base, keymap);
1026 xkb_map_unref(keymap);
1030 input_handle_keyboard_enter(void *data,
1031 struct wl_keyboard *keyboard,
1033 struct wl_surface *surface,
1034 struct wl_array *keys)
1036 struct wayland_input *input = data;
1037 struct wayland_output *focus;
1039 focus = input->keyboard_focus;
1041 /* This shouldn't happen */
1042 focus->keyboard_count--;
1043 if (!focus->keyboard_count && focus->frame)
1044 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1045 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1046 weston_output_schedule_repaint(&focus->base);
1049 input->keyboard_focus = wl_surface_get_user_data(surface);
1050 input->keyboard_focus->keyboard_count++;
1052 focus = input->keyboard_focus;
1054 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1055 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1056 weston_output_schedule_repaint(&focus->base);
1060 /* XXX: If we get a modifier event immediately before the focus,
1061 * we should try to keep the same serial. */
1062 notify_keyboard_focus_in(&input->base, keys,
1063 STATE_UPDATE_AUTOMATIC);
1067 input_handle_keyboard_leave(void *data,
1068 struct wl_keyboard *keyboard,
1070 struct wl_surface *surface)
1072 struct wayland_input *input = data;
1073 struct wayland_output *focus;
1075 notify_keyboard_focus_out(&input->base);
1077 focus = input->keyboard_focus;
1079 return; /* This shouldn't happen */
1081 focus->keyboard_count--;
1082 if (!focus->keyboard_count && focus->frame) {
1083 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1084 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1085 weston_output_schedule_repaint(&focus->base);
1088 input->keyboard_focus = NULL;
1092 input_handle_key(void *data, struct wl_keyboard *keyboard,
1093 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1095 struct wayland_input *input = data;
1097 input->key_serial = serial;
1098 notify_key(&input->base, time, key,
1099 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
1100 WL_KEYBOARD_KEY_STATE_RELEASED,
1105 input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1106 uint32_t serial_in, uint32_t mods_depressed,
1107 uint32_t mods_latched, uint32_t mods_locked,
1110 struct wayland_input *input = data;
1111 struct wayland_compositor *c = input->compositor;
1112 uint32_t serial_out;
1114 /* If we get a key event followed by a modifier event with the
1115 * same serial number, then we try to preserve those semantics by
1116 * reusing the same serial number on the way out too. */
1117 if (serial_in == input->key_serial)
1118 serial_out = wl_display_get_serial(c->base.wl_display);
1120 serial_out = wl_display_next_serial(c->base.wl_display);
1122 xkb_state_update_mask(input->base.xkb_state.state,
1123 mods_depressed, mods_latched,
1124 mods_locked, 0, 0, group);
1125 notify_modifiers(&input->base, serial_out);
1128 static const struct wl_keyboard_listener keyboard_listener = {
1129 input_handle_keymap,
1130 input_handle_keyboard_enter,
1131 input_handle_keyboard_leave,
1133 input_handle_modifiers,
1137 input_handle_capabilities(void *data, struct wl_seat *seat,
1138 enum wl_seat_capability caps)
1140 struct wayland_input *input = data;
1142 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1143 input->parent.pointer = wl_seat_get_pointer(seat);
1144 wl_pointer_set_user_data(input->parent.pointer, input);
1145 wl_pointer_add_listener(input->parent.pointer,
1146 &pointer_listener, input);
1147 weston_seat_init_pointer(&input->base);
1148 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1149 wl_pointer_destroy(input->parent.pointer);
1150 input->parent.pointer = NULL;
1153 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1154 input->parent.keyboard = wl_seat_get_keyboard(seat);
1155 wl_keyboard_set_user_data(input->parent.keyboard, input);
1156 wl_keyboard_add_listener(input->parent.keyboard,
1157 &keyboard_listener, input);
1158 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1159 wl_keyboard_destroy(input->parent.keyboard);
1160 input->parent.keyboard = NULL;
1164 static const struct wl_seat_listener seat_listener = {
1165 input_handle_capabilities,
1169 display_add_seat(struct wayland_compositor *c, uint32_t id)
1171 struct wayland_input *input;
1173 input = zalloc(sizeof *input);
1177 weston_seat_init(&input->base, &c->base, "default");
1178 input->compositor = c;
1179 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1180 &wl_seat_interface, 1);
1181 wl_list_insert(c->input_list.prev, &input->link);
1183 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1184 wl_seat_set_user_data(input->parent.seat, input);
1186 input->parent.cursor.surface =
1187 wl_compositor_create_surface(c->parent.compositor);
1191 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1192 const char *interface, uint32_t version)
1194 struct wayland_compositor *c = data;
1196 if (strcmp(interface, "wl_compositor") == 0) {
1197 c->parent.compositor =
1198 wl_registry_bind(registry, name,
1199 &wl_compositor_interface, 1);
1200 } else if (strcmp(interface, "wl_shell") == 0) {
1202 wl_registry_bind(registry, name,
1203 &wl_shell_interface, 1);
1204 } else if (strcmp(interface, "wl_seat") == 0) {
1205 display_add_seat(c, name);
1206 } else if (strcmp(interface, "wl_shm") == 0) {
1208 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1212 static const struct wl_registry_listener registry_listener = {
1213 registry_handle_global
1217 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1219 struct wayland_compositor *c = data;
1222 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1223 wl_display_terminate(c->base.wl_display);
1227 if (mask & WL_EVENT_READABLE)
1228 count = wl_display_dispatch(c->parent.wl_display);
1229 if (mask & WL_EVENT_WRITABLE)
1230 wl_display_flush(c->parent.wl_display);
1233 count = wl_display_dispatch_pending(c->parent.wl_display);
1234 wl_display_flush(c->parent.wl_display);
1241 wayland_restore(struct weston_compositor *ec)
1246 wayland_destroy(struct weston_compositor *ec)
1248 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1250 ec->renderer->destroy(ec);
1252 weston_compositor_shutdown(ec);
1255 wl_shm_destroy(c->parent.shm);
1260 static const char *left_ptrs[] = {
1268 create_cursor(struct wayland_compositor *c, struct weston_config *config)
1270 struct weston_config_section *s;
1275 s = weston_config_get_section(config, "shell", NULL, NULL);
1276 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1277 weston_config_section_get_int(s, "cursor-size", &size, 32);
1279 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1282 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1283 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1286 fprintf(stderr, "could not load left cursor\n");
1291 static struct wayland_compositor *
1292 wayland_compositor_create(struct wl_display *display, int use_pixman,
1293 const char *display_name, int *argc, char *argv[],
1294 struct weston_config *config)
1296 struct wayland_compositor *c;
1297 struct wl_event_loop *loop;
1300 c = zalloc(sizeof *c);
1304 if (weston_compositor_init(&c->base, display, argc, argv,
1308 c->parent.wl_display = wl_display_connect(display_name);
1310 if (c->parent.wl_display == NULL) {
1311 weston_log("failed to create display: %m\n");
1312 goto err_compositor;
1315 wl_list_init(&c->input_list);
1316 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1317 wl_registry_add_listener(c->parent.registry, ®istry_listener, c);
1318 wl_display_roundtrip(c->parent.wl_display);
1320 create_cursor(c, config);
1322 c->base.wl_display = display;
1324 c->use_pixman = use_pixman;
1326 if (!c->use_pixman) {
1327 gl_renderer = weston_load_module("gl-renderer.so",
1328 "gl_renderer_interface");
1333 if (!c->use_pixman) {
1334 if (gl_renderer->create(&c->base, c->parent.wl_display,
1335 gl_renderer->alpha_attribs,
1337 weston_log("Failed to initialize the GL renderer; "
1338 "falling back to pixman.\n");
1343 if (c->use_pixman) {
1344 if (pixman_renderer_init(&c->base) < 0) {
1345 weston_log("Failed to initialize pixman renderer\n");
1350 c->base.destroy = wayland_destroy;
1351 c->base.restore = wayland_restore;
1353 loop = wl_display_get_event_loop(c->base.wl_display);
1355 fd = wl_display_get_fd(c->parent.wl_display);
1356 c->parent.wl_source =
1357 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1358 wayland_compositor_handle_event, c);
1359 if (c->parent.wl_source == NULL)
1362 wl_event_source_check(c->parent.wl_source);
1366 c->base.renderer->destroy(&c->base);
1368 wl_display_disconnect(c->parent.wl_display);
1370 weston_compositor_shutdown(&c->base);
1377 wayland_compositor_destroy(struct wayland_compositor *c)
1379 struct weston_output *output;
1381 wl_list_for_each(output, &c->base.output_list, link)
1382 wayland_output_destroy(output);
1384 c->base.renderer->destroy(&c->base);
1385 wl_display_disconnect(c->parent.wl_display);
1388 theme_destroy(c->theme);
1389 if (c->frame_device)
1390 cairo_device_destroy(c->frame_device);
1391 wl_cursor_theme_destroy(c->cursor_theme);
1393 weston_compositor_shutdown(&c->base);
1397 WL_EXPORT struct weston_compositor *
1398 backend_init(struct wl_display *display, int *argc, char *argv[],
1399 struct weston_config *config)
1401 struct wayland_compositor *c;
1402 struct wayland_output *output;
1403 struct weston_config_section *section;
1404 int x, count, width, height, use_pixman;
1405 const char *section_name, *display_name;
1408 const struct weston_option wayland_options[] = {
1409 { WESTON_OPTION_INTEGER, "width", 0, &width },
1410 { WESTON_OPTION_INTEGER, "height", 0, &height },
1411 { WESTON_OPTION_STRING, "display", 0, &display_name },
1412 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1413 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
1418 display_name = NULL;
1422 parse_options(wayland_options,
1423 ARRAY_LENGTH(wayland_options), argc, argv);
1425 c = wayland_compositor_create(display, use_pixman, display_name,
1426 argc, argv, config);
1432 while (weston_config_next_section(config, §ion, §ion_name)) {
1433 if (!section_name || strcmp(section_name, "output") != 0)
1435 weston_config_section_get_string(section, "name", &name, NULL);
1439 if (name[0] != 'W' || name[1] != 'L') {
1445 output = wayland_output_create_for_config(c, section, width,
1450 x += output->base.width;
1459 output = wayland_output_create(c, x, 0, width, height, NULL, 0, 1);
1470 wayland_compositor_destroy(c);