1 /*
2 * Copyright © 2010-2011 Benjamin Franzke
3 * Copyright © 2013 Jason Ekstrand
4 *
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:
12 *
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.
16 *
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
24 * SOFTWARE.
25 */
26
27 #include "config.h"
28
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <sys/mman.h>
36 #include <linux/input.h>
37
38 #include <wayland-client.h>
39 #include <wayland-egl.h>
40 #include <wayland-cursor.h>
41
42 #include "compositor.h"
43 #include "gl-renderer.h"
44 #include "pixman-renderer.h"
45 #include "shared/helpers.h"
46 #include "shared/image-loader.h"
47 #include "shared/os-compatibility.h"
48 #include "shared/cairo-util.h"
49 #include "fullscreen-shell-client-protocol.h"
50 #include "presentation_timing-server-protocol.h"
51
52 #define WINDOW_TITLE "Weston Compositor"
53
54 struct wayland_backend {
55 struct weston_backend base;
56 struct weston_compositor *compositor;
57
58 struct {
59 struct wl_display *wl_display;
60 struct wl_registry *registry;
61 struct wl_compositor *compositor;
62 struct wl_shell *shell;
63 struct _wl_fullscreen_shell *fshell;
64 struct wl_shm *shm;
65
66 struct wl_list output_list;
67
68 struct wl_event_source *wl_source;
69 uint32_t event_mask;
70 } parent;
71
72 int use_pixman;
73 int sprawl_across_outputs;
74
75 struct theme *theme;
76 cairo_device_t *frame_device;
77 struct wl_cursor_theme *cursor_theme;
78 struct wl_cursor *cursor;
79
80 struct wl_list input_list;
81 };
82
83 struct wayland_output {
84 struct weston_output base;
85
86 struct {
87 int draw_initial_frame;
88 struct wl_surface *surface;
89
90 struct wl_output *output;
91 uint32_t global_id;
92
93 struct wl_shell_surface *shell_surface;
94 int configure_width, configure_height;
95 } parent;
96
97 int keyboard_count;
98
99 char *name;
100 struct frame *frame;
101
102 struct {
103 struct wl_egl_window *egl_window;
104 struct {
105 cairo_surface_t *top;
106 cairo_surface_t *left;
107 cairo_surface_t *right;
108 cairo_surface_t *bottom;
109 } border;
110 } gl;
111
112 struct {
113 struct wl_list buffers;
114 struct wl_list free_buffers;
115 } shm;
116
117 struct weston_mode mode;
118 uint32_t scale;
119 };
120
121 struct wayland_parent_output {
122 struct wayland_output *output;
123 struct wl_list link;
124
125 struct wl_output *global;
126 uint32_t id;
127
128 struct {
129 char *make;
130 char *model;
131 int32_t width, height;
132 uint32_t subpixel;
133 } physical;
134
135 int32_t x, y;
136 uint32_t transform;
137 uint32_t scale;
138
139 struct wl_list mode_list;
140 struct weston_mode *preferred_mode;
141 struct weston_mode *current_mode;
142 };
143
144 struct wayland_shm_buffer {
145 struct wayland_output *output;
146 struct wl_list link;
147 struct wl_list free_link;
148
149 struct wl_buffer *buffer;
150 void *data;
151 size_t size;
152 pixman_region32_t damage;
153 int frame_damaged;
154
155 pixman_image_t *pm_image;
156 cairo_surface_t *c_surface;
157 };
158
159 struct wayland_input {
160 struct weston_seat base;
161 struct wayland_backend *backend;
162 struct wl_list link;
163
164 struct {
165 struct wl_seat *seat;
166 struct wl_pointer *pointer;
167 struct wl_keyboard *keyboard;
168 struct wl_touch *touch;
169
170 struct {
171 struct wl_surface *surface;
172 int32_t hx, hy;
173 } cursor;
174 } parent;
175
176 enum weston_key_state_update keyboard_state_update;
177 uint32_t key_serial;
178 uint32_t enter_serial;
179 int focus;
180 struct wayland_output *output;
181 struct wayland_output *keyboard_focus;
182 };
183
184 struct gl_renderer_interface *gl_renderer;
185
186 static void
wayland_shm_buffer_destroy(struct wayland_shm_buffer * buffer)187 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
188 {
189 cairo_surface_destroy(buffer->c_surface);
190 pixman_image_unref(buffer->pm_image);
191
192 wl_buffer_destroy(buffer->buffer);
193 munmap(buffer->data, buffer->size);
194
195 pixman_region32_fini(&buffer->damage);
196
197 wl_list_remove(&buffer->link);
198 wl_list_remove(&buffer->free_link);
199 free(buffer);
200 }
201
202 static void
buffer_release(void * data,struct wl_buffer * buffer)203 buffer_release(void *data, struct wl_buffer *buffer)
204 {
205 struct wayland_shm_buffer *sb = data;
206
207 if (sb->output) {
208 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
209 } else {
210 wayland_shm_buffer_destroy(sb);
211 }
212 }
213
214 static const struct wl_buffer_listener buffer_listener = {
215 buffer_release
216 };
217
218 static struct wayland_shm_buffer *
wayland_output_get_shm_buffer(struct wayland_output * output)219 wayland_output_get_shm_buffer(struct wayland_output *output)
220 {
221 struct wayland_backend *b =
222 (struct wayland_backend *) output->base.compositor->backend;
223 struct wl_shm *shm = b->parent.shm;
224 struct wayland_shm_buffer *sb;
225
226 struct wl_shm_pool *pool;
227 int width, height, stride;
228 int32_t fx, fy;
229 int fd;
230 unsigned char *data;
231
232 if (!wl_list_empty(&output->shm.free_buffers)) {
233 sb = container_of(output->shm.free_buffers.next,
234 struct wayland_shm_buffer, free_link);
235 wl_list_remove(&sb->free_link);
236 wl_list_init(&sb->free_link);
237
238 return sb;
239 }
240
241 if (output->frame) {
242 width = frame_width(output->frame);
243 height = frame_height(output->frame);
244 } else {
245 width = output->base.current_mode->width;
246 height = output->base.current_mode->height;
247 }
248
249 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
250
251 fd = os_create_anonymous_file(height * stride);
252 if (fd < 0) {
253 weston_log("could not create an anonymous file buffer: %m\n");
254 return NULL;
255 }
256
257 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
258 if (data == MAP_FAILED) {
259 weston_log("could not mmap %d memory for data: %m\n", height * stride);
260 close(fd);
261 return NULL;
262 }
263
264 sb = zalloc(sizeof *sb);
265 if (sb == NULL) {
266 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
267 close(fd);
268 free(data);
269 return NULL;
270 }
271
272 sb->output = output;
273 wl_list_init(&sb->free_link);
274 wl_list_insert(&output->shm.buffers, &sb->link);
275
276 pixman_region32_init_rect(&sb->damage, 0, 0,
277 output->base.width, output->base.height);
278 sb->frame_damaged = 1;
279
280 sb->data = data;
281 sb->size = height * stride;
282
283 pool = wl_shm_create_pool(shm, fd, sb->size);
284
285 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
286 width, height,
287 stride,
288 WL_SHM_FORMAT_ARGB8888);
289 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
290 wl_shm_pool_destroy(pool);
291 close(fd);
292
293 memset(data, 0, sb->size);
294
295 sb->c_surface =
296 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
297 width, height, stride);
298
299 fx = 0;
300 fy = 0;
301 if (output->frame)
302 frame_interior(output->frame, &fx, &fy, 0, 0);
303 sb->pm_image =
304 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
305 (uint32_t *)(data + fy * stride) + fx,
306 stride);
307
308 return sb;
309 }
310
311 static void
frame_done(void * data,struct wl_callback * callback,uint32_t time)312 frame_done(void *data, struct wl_callback *callback, uint32_t time)
313 {
314 struct weston_output *output = data;
315 struct timespec ts;
316
317 wl_callback_destroy(callback);
318
319 /* XXX: use the presentation extension for proper timings */
320
321 /*
322 * This is the fallback case, where Presentation extension is not
323 * available from the parent compositor. We do not know the base for
324 * 'time', so we cannot feed it to finish_frame(). Do the only thing
325 * we can, and pretend finish_frame time is when we process this
326 * event.
327 */
328 weston_compositor_read_presentation_clock(output->compositor, &ts);
329 weston_output_finish_frame(output, &ts, 0);
330 }
331
332 static const struct wl_callback_listener frame_listener = {
333 frame_done
334 };
335
336 static void
draw_initial_frame(struct wayland_output * output)337 draw_initial_frame(struct wayland_output *output)
338 {
339 struct wayland_shm_buffer *sb;
340
341 sb = wayland_output_get_shm_buffer(output);
342
343 /* If we are rendering with GL, then orphan it so that it gets
344 * destroyed immediately */
345 if (output->gl.egl_window)
346 sb->output = NULL;
347
348 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
349 wl_surface_damage(output->parent.surface, 0, 0,
350 output->base.current_mode->width,
351 output->base.current_mode->height);
352 }
353
354 static void
wayland_output_update_gl_border(struct wayland_output * output)355 wayland_output_update_gl_border(struct wayland_output *output)
356 {
357 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
358 cairo_t *cr;
359
360 if (!output->frame)
361 return;
362 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
363 return;
364
365 fwidth = frame_width(output->frame);
366 fheight = frame_height(output->frame);
367 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
368
369 if (!output->gl.border.top)
370 output->gl.border.top =
371 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
372 fwidth, iy);
373 cr = cairo_create(output->gl.border.top);
374 frame_repaint(output->frame, cr);
375 cairo_destroy(cr);
376 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
377 fwidth, iy,
378 cairo_image_surface_get_stride(output->gl.border.top) / 4,
379 cairo_image_surface_get_data(output->gl.border.top));
380
381
382 if (!output->gl.border.left)
383 output->gl.border.left =
384 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
385 ix, 1);
386 cr = cairo_create(output->gl.border.left);
387 cairo_translate(cr, 0, -iy);
388 frame_repaint(output->frame, cr);
389 cairo_destroy(cr);
390 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
391 ix, 1,
392 cairo_image_surface_get_stride(output->gl.border.left) / 4,
393 cairo_image_surface_get_data(output->gl.border.left));
394
395
396 if (!output->gl.border.right)
397 output->gl.border.right =
398 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
399 fwidth - (ix + iwidth), 1);
400 cr = cairo_create(output->gl.border.right);
401 cairo_translate(cr, -(iwidth + ix), -iy);
402 frame_repaint(output->frame, cr);
403 cairo_destroy(cr);
404 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
405 fwidth - (ix + iwidth), 1,
406 cairo_image_surface_get_stride(output->gl.border.right) / 4,
407 cairo_image_surface_get_data(output->gl.border.right));
408
409
410 if (!output->gl.border.bottom)
411 output->gl.border.bottom =
412 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
413 fwidth, fheight - (iy + iheight));
414 cr = cairo_create(output->gl.border.bottom);
415 cairo_translate(cr, 0, -(iy + iheight));
416 frame_repaint(output->frame, cr);
417 cairo_destroy(cr);
418 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
419 fwidth, fheight - (iy + iheight),
420 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
421 cairo_image_surface_get_data(output->gl.border.bottom));
422 }
423
424 static void
wayland_output_start_repaint_loop(struct weston_output * output_base)425 wayland_output_start_repaint_loop(struct weston_output *output_base)
426 {
427 struct wayland_output *output = (struct wayland_output *) output_base;
428 struct wayland_backend *wb =
429 (struct wayland_backend *)output->base.compositor->backend;
430 struct wl_callback *callback;
431
432 /* If this is the initial frame, we need to attach a buffer so that
433 * the compositor can map the surface and include it in its render
434 * loop. If the surface doesn't end up in the render loop, the frame
435 * callback won't be invoked. The buffer is transparent and of the
436 * same size as the future real output buffer. */
437 if (output->parent.draw_initial_frame) {
438 output->parent.draw_initial_frame = 0;
439
440 draw_initial_frame(output);
441 }
442
443 callback = wl_surface_frame(output->parent.surface);
444 wl_callback_add_listener(callback, &frame_listener, output);
445 wl_surface_commit(output->parent.surface);
446 wl_display_flush(wb->parent.wl_display);
447 }
448
449 static int
wayland_output_repaint_gl(struct weston_output * output_base,pixman_region32_t * damage)450 wayland_output_repaint_gl(struct weston_output *output_base,
451 pixman_region32_t *damage)
452 {
453 struct wayland_output *output = (struct wayland_output *) output_base;
454 struct weston_compositor *ec = output->base.compositor;
455 struct wl_callback *callback;
456
457 callback = wl_surface_frame(output->parent.surface);
458 wl_callback_add_listener(callback, &frame_listener, output);
459
460 wayland_output_update_gl_border(output);
461
462 ec->renderer->repaint_output(&output->base, damage);
463
464 pixman_region32_subtract(&ec->primary_plane.damage,
465 &ec->primary_plane.damage, damage);
466 return 0;
467 }
468
469 static void
wayland_output_update_shm_border(struct wayland_shm_buffer * buffer)470 wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
471 {
472 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
473 cairo_t *cr;
474
475 if (!buffer->output->frame || !buffer->frame_damaged)
476 return;
477
478 cr = cairo_create(buffer->c_surface);
479
480 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
481 fwidth = frame_width(buffer->output->frame);
482 fheight = frame_height(buffer->output->frame);
483
484 /* Set the clip so we don't unnecisaraly damage the surface */
485 cairo_move_to(cr, ix, iy);
486 cairo_rel_line_to(cr, iwidth, 0);
487 cairo_rel_line_to(cr, 0, iheight);
488 cairo_rel_line_to(cr, -iwidth, 0);
489 cairo_line_to(cr, ix, iy);
490 cairo_line_to(cr, 0, iy);
491 cairo_line_to(cr, 0, fheight);
492 cairo_line_to(cr, fwidth, fheight);
493 cairo_line_to(cr, fwidth, 0);
494 cairo_line_to(cr, 0, 0);
495 cairo_line_to(cr, 0, iy);
496 cairo_close_path(cr);
497 cairo_clip(cr);
498
499 /* Draw using a pattern so that the final result gets clipped */
500 cairo_push_group(cr);
501 frame_repaint(buffer->output->frame, cr);
502 cairo_pop_group_to_source(cr);
503 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
504 cairo_paint(cr);
505
506 cairo_destroy(cr);
507 }
508
509 static void
wayland_shm_buffer_attach(struct wayland_shm_buffer * sb)510 wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
511 {
512 pixman_region32_t damage;
513 pixman_box32_t *rects;
514 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
515 int i, n;
516
517 pixman_region32_init(&damage);
518 weston_transformed_region(sb->output->base.width,
519 sb->output->base.height,
520 sb->output->base.transform,
521 sb->output->base.current_scale,
522 &sb->damage, &damage);
523
524 if (sb->output->frame) {
525 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
526 fwidth = frame_width(sb->output->frame);
527 fheight = frame_height(sb->output->frame);
528
529 pixman_region32_translate(&damage, ix, iy);
530
531 if (sb->frame_damaged) {
532 pixman_region32_union_rect(&damage, &damage,
533 0, 0, fwidth, iy);
534 pixman_region32_union_rect(&damage, &damage,
535 0, iy, ix, iheight);
536 pixman_region32_union_rect(&damage, &damage,
537 ix + iwidth, iy,
538 fwidth - (ix + iwidth), iheight);
539 pixman_region32_union_rect(&damage, &damage,
540 0, iy + iheight,
541 fwidth, fheight - (iy + iheight));
542 }
543 }
544
545 rects = pixman_region32_rectangles(&damage, &n);
546 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
547 for (i = 0; i < n; ++i)
548 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
549 rects[i].y1, rects[i].x2 - rects[i].x1,
550 rects[i].y2 - rects[i].y1);
551
552 if (sb->output->frame)
553 pixman_region32_fini(&damage);
554 }
555
556 static int
wayland_output_repaint_pixman(struct weston_output * output_base,pixman_region32_t * damage)557 wayland_output_repaint_pixman(struct weston_output *output_base,
558 pixman_region32_t *damage)
559 {
560 struct wayland_output *output = (struct wayland_output *) output_base;
561 struct wayland_backend *b =
562 (struct wayland_backend *)output->base.compositor->backend;
563 struct wl_callback *callback;
564 struct wayland_shm_buffer *sb;
565
566 if (output->frame) {
567 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
568 wl_list_for_each(sb, &output->shm.buffers, link)
569 sb->frame_damaged = 1;
570 }
571
572 wl_list_for_each(sb, &output->shm.buffers, link)
573 pixman_region32_union(&sb->damage, &sb->damage, damage);
574
575 sb = wayland_output_get_shm_buffer(output);
576
577 wayland_output_update_shm_border(sb);
578 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
579 b->compositor->renderer->repaint_output(output_base, &sb->damage);
580
581 wayland_shm_buffer_attach(sb);
582
583 callback = wl_surface_frame(output->parent.surface);
584 wl_callback_add_listener(callback, &frame_listener, output);
585 wl_surface_commit(output->parent.surface);
586 wl_display_flush(b->parent.wl_display);
587
588 pixman_region32_fini(&sb->damage);
589 pixman_region32_init(&sb->damage);
590 sb->frame_damaged = 0;
591
592 pixman_region32_subtract(&b->compositor->primary_plane.damage,
593 &b->compositor->primary_plane.damage, damage);
594 return 0;
595 }
596
597 static void
wayland_output_destroy(struct weston_output * output_base)598 wayland_output_destroy(struct weston_output *output_base)
599 {
600 struct wayland_output *output = (struct wayland_output *) output_base;
601 struct wayland_backend *b =
602 (struct wayland_backend *) output->base.compositor->backend;
603
604 if (b->use_pixman) {
605 pixman_renderer_output_destroy(output_base);
606 } else {
607 gl_renderer->output_destroy(output_base);
608 }
609
610 wl_egl_window_destroy(output->gl.egl_window);
611 wl_surface_destroy(output->parent.surface);
612 if (output->parent.shell_surface)
613 wl_shell_surface_destroy(output->parent.shell_surface);
614
615 if (output->frame)
616 frame_destroy(output->frame);
617
618 cairo_surface_destroy(output->gl.border.top);
619 cairo_surface_destroy(output->gl.border.left);
620 cairo_surface_destroy(output->gl.border.right);
621 cairo_surface_destroy(output->gl.border.bottom);
622
623 weston_output_destroy(&output->base);
624 free(output);
625
626 return;
627 }
628
629 static const struct wl_shell_surface_listener shell_surface_listener;
630
631 static int
wayland_output_init_gl_renderer(struct wayland_output * output)632 wayland_output_init_gl_renderer(struct wayland_output *output)
633 {
634 int32_t fwidth = 0, fheight = 0;
635
636 if (output->frame) {
637 fwidth = frame_width(output->frame);
638 fheight = frame_height(output->frame);
639 } else {
640 fwidth = output->base.current_mode->width;
641 fheight = output->base.current_mode->height;
642 }
643
644 output->gl.egl_window =
645 wl_egl_window_create(output->parent.surface,
646 fwidth, fheight);
647 if (!output->gl.egl_window) {
648 weston_log("failure to create wl_egl_window\n");
649 return -1;
650 }
651
652 if (gl_renderer->output_create(&output->base,
653 output->gl.egl_window,
654 output->gl.egl_window,
655 gl_renderer->alpha_attribs,
656 NULL,
657 0) < 0)
658 goto cleanup_window;
659
660 return 0;
661
662 cleanup_window:
663 wl_egl_window_destroy(output->gl.egl_window);
664 return -1;
665 }
666
667 static int
wayland_output_init_pixman_renderer(struct wayland_output * output)668 wayland_output_init_pixman_renderer(struct wayland_output *output)
669 {
670 return pixman_renderer_output_create(&output->base);
671 }
672
673 static void
wayland_output_resize_surface(struct wayland_output * output)674 wayland_output_resize_surface(struct wayland_output *output)
675 {
676 struct wayland_backend *b =
677 (struct wayland_backend *)output->base.compositor->backend;
678 struct wayland_shm_buffer *buffer, *next;
679 int32_t ix, iy, iwidth, iheight;
680 int32_t width, height;
681 struct wl_region *region;
682
683 width = output->base.current_mode->width;
684 height = output->base.current_mode->height;
685
686 if (output->frame) {
687 frame_resize_inside(output->frame, width, height);
688
689 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
690 region = wl_compositor_create_region(b->parent.compositor);
691 wl_region_add(region, ix, iy, iwidth, iheight);
692 wl_surface_set_input_region(output->parent.surface, region);
693 wl_region_destroy(region);
694
695 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
696 region = wl_compositor_create_region(b->parent.compositor);
697 wl_region_add(region, ix, iy, iwidth, iheight);
698 wl_surface_set_opaque_region(output->parent.surface, region);
699 wl_region_destroy(region);
700
701 width = frame_width(output->frame);
702 height = frame_height(output->frame);
703 } else {
704 region = wl_compositor_create_region(b->parent.compositor);
705 wl_region_add(region, 0, 0, width, height);
706 wl_surface_set_input_region(output->parent.surface, region);
707 wl_region_destroy(region);
708
709 region = wl_compositor_create_region(b->parent.compositor);
710 wl_region_add(region, 0, 0, width, height);
711 wl_surface_set_opaque_region(output->parent.surface, region);
712 wl_region_destroy(region);
713 }
714
715 if (output->gl.egl_window) {
716 wl_egl_window_resize(output->gl.egl_window,
717 width, height, 0, 0);
718
719 /* These will need to be re-created due to the resize */
720 gl_renderer->output_set_border(&output->base,
721 GL_RENDERER_BORDER_TOP,
722 0, 0, 0, NULL);
723 cairo_surface_destroy(output->gl.border.top);
724 output->gl.border.top = NULL;
725 gl_renderer->output_set_border(&output->base,
726 GL_RENDERER_BORDER_LEFT,
727 0, 0, 0, NULL);
728 cairo_surface_destroy(output->gl.border.left);
729 output->gl.border.left = NULL;
730 gl_renderer->output_set_border(&output->base,
731 GL_RENDERER_BORDER_RIGHT,
732 0, 0, 0, NULL);
733 cairo_surface_destroy(output->gl.border.right);
734 output->gl.border.right = NULL;
735 gl_renderer->output_set_border(&output->base,
736 GL_RENDERER_BORDER_BOTTOM,
737 0, 0, 0, NULL);
738 cairo_surface_destroy(output->gl.border.bottom);
739 output->gl.border.bottom = NULL;
740 }
741
742 /* Throw away any remaining SHM buffers */
743 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
744 wayland_shm_buffer_destroy(buffer);
745 /* These will get thrown away when they get released */
746 wl_list_for_each(buffer, &output->shm.buffers, link)
747 buffer->output = NULL;
748 }
749
750 static int
wayland_output_set_windowed(struct wayland_output * output)751 wayland_output_set_windowed(struct wayland_output *output)
752 {
753 struct wayland_backend *b =
754 (struct wayland_backend *)output->base.compositor->backend;
755 int tlen;
756 char *title;
757
758 if (output->frame)
759 return 0;
760
761 if (output->name) {
762 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
763 title = malloc(tlen + 1);
764 if (!title)
765 return -1;
766
767 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
768 } else {
769 title = strdup(WINDOW_TITLE);
770 }
771
772 if (!b->theme) {
773 b->theme = theme_create();
774 if (!b->theme) {
775 free(title);
776 return -1;
777 }
778 }
779 output->frame = frame_create(b->theme, 100, 100,
780 FRAME_BUTTON_CLOSE, title);
781 free(title);
782 if (!output->frame)
783 return -1;
784
785 if (output->keyboard_count)
786 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
787
788 wayland_output_resize_surface(output);
789
790 wl_shell_surface_set_toplevel(output->parent.shell_surface);
791
792 return 0;
793 }
794
795 static void
wayland_output_set_fullscreen(struct wayland_output * output,enum wl_shell_surface_fullscreen_method method,uint32_t framerate,struct wl_output * target)796 wayland_output_set_fullscreen(struct wayland_output *output,
797 enum wl_shell_surface_fullscreen_method method,
798 uint32_t framerate, struct wl_output *target)
799 {
800 struct wayland_backend *b =
801 (struct wayland_backend *)output->base.compositor->backend;
802
803 if (output->frame) {
804 frame_destroy(output->frame);
805 output->frame = NULL;
806 }
807
808 wayland_output_resize_surface(output);
809
810 if (output->parent.shell_surface) {
811 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
812 method, framerate, target);
813 } else if (b->parent.fshell) {
814 _wl_fullscreen_shell_present_surface(b->parent.fshell,
815 output->parent.surface,
816 method, target);
817 }
818 }
819
820 static struct weston_mode *
wayland_output_choose_mode(struct wayland_output * output,struct weston_mode * ref_mode)821 wayland_output_choose_mode(struct wayland_output *output,
822 struct weston_mode *ref_mode)
823 {
824 struct weston_mode *mode;
825
826 /* First look for an exact match */
827 wl_list_for_each(mode, &output->base.mode_list, link)
828 if (mode->width == ref_mode->width &&
829 mode->height == ref_mode->height &&
830 mode->refresh == ref_mode->refresh)
831 return mode;
832
833 /* If we can't find an exact match, ignore refresh and try again */
834 wl_list_for_each(mode, &output->base.mode_list, link)
835 if (mode->width == ref_mode->width &&
836 mode->height == ref_mode->height)
837 return mode;
838
839 /* Yeah, we failed */
840 return NULL;
841 }
842
843 enum mode_status {
844 MODE_STATUS_UNKNOWN,
845 MODE_STATUS_SUCCESS,
846 MODE_STATUS_FAIL,
847 MODE_STATUS_CANCEL,
848 };
849
850 static void
mode_feedback_successful(void * data,struct _wl_fullscreen_shell_mode_feedback * fb)851 mode_feedback_successful(void *data,
852 struct _wl_fullscreen_shell_mode_feedback *fb)
853 {
854 enum mode_status *value = data;
855
856 printf("Mode switch successful\n");
857
858 *value = MODE_STATUS_SUCCESS;
859 }
860
861 static void
mode_feedback_failed(void * data,struct _wl_fullscreen_shell_mode_feedback * fb)862 mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
863 {
864 enum mode_status *value = data;
865
866 printf("Mode switch failed\n");
867
868 *value = MODE_STATUS_FAIL;
869 }
870
871 static void
mode_feedback_cancelled(void * data,struct _wl_fullscreen_shell_mode_feedback * fb)872 mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
873 {
874 enum mode_status *value = data;
875
876 printf("Mode switch cancelled\n");
877
878 *value = MODE_STATUS_CANCEL;
879 }
880
881 struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
882 mode_feedback_successful,
883 mode_feedback_failed,
884 mode_feedback_cancelled,
885 };
886
887 static int
wayland_output_switch_mode(struct weston_output * output_base,struct weston_mode * mode)888 wayland_output_switch_mode(struct weston_output *output_base,
889 struct weston_mode *mode)
890 {
891 struct wayland_output *output = (struct wayland_output *) output_base;
892 struct wayland_backend *b;
893 struct wl_surface *old_surface;
894 struct weston_mode *old_mode;
895 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
896 enum mode_status mode_status;
897 int ret = 0;
898
899 if (output_base == NULL) {
900 weston_log("output is NULL.\n");
901 return -1;
902 }
903
904 if (mode == NULL) {
905 weston_log("mode is NULL.\n");
906 return -1;
907 }
908
909 b = (struct wayland_backend *)output_base->compositor->backend;
910
911 if (output->parent.shell_surface || !b->parent.fshell)
912 return -1;
913
914 mode = wayland_output_choose_mode(output, mode);
915 if (mode == NULL)
916 return -1;
917
918 if (output->base.current_mode == mode)
919 return 0;
920
921 old_mode = output->base.current_mode;
922 old_surface = output->parent.surface;
923 output->base.current_mode = mode;
924 output->parent.surface =
925 wl_compositor_create_surface(b->parent.compositor);
926 wl_surface_set_user_data(output->parent.surface, output);
927
928 /* Blow the old buffers because we changed size/surfaces */
929 wayland_output_resize_surface(output);
930
931 mode_feedback =
932 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
933 output->parent.surface,
934 output->parent.output,
935 mode->refresh);
936 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
937 &mode_feedback_listener,
938 &mode_status);
939
940 /* This should kick-start things again */
941 output->parent.draw_initial_frame = 1;
942 wayland_output_start_repaint_loop(&output->base);
943
944 mode_status = MODE_STATUS_UNKNOWN;
945 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
946 ret = wl_display_dispatch(b->parent.wl_display);
947
948 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
949
950 if (mode_status == MODE_STATUS_FAIL) {
951 output->base.current_mode = old_mode;
952 wl_surface_destroy(output->parent.surface);
953 output->parent.surface = old_surface;
954 wayland_output_resize_surface(output);
955
956 return -1;
957 }
958
959 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
960 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
961
962 if (b->use_pixman) {
963 pixman_renderer_output_destroy(output_base);
964 if (wayland_output_init_pixman_renderer(output) < 0)
965 goto err_output;
966 } else {
967 gl_renderer->output_destroy(output_base);
968 wl_egl_window_destroy(output->gl.egl_window);
969 if (wayland_output_init_gl_renderer(output) < 0)
970 goto err_output;
971 }
972 wl_surface_destroy(old_surface);
973
974 weston_output_schedule_repaint(&output->base);
975
976 return 0;
977
978 err_output:
979 /* XXX */
980 return -1;
981 }
982
983 static struct wayland_output *
wayland_output_create(struct wayland_backend * b,int x,int y,int width,int height,const char * name,int fullscreen,uint32_t transform,int32_t scale)984 wayland_output_create(struct wayland_backend *b, int x, int y,
985 int width, int height, const char *name, int fullscreen,
986 uint32_t transform, int32_t scale)
987 {
988 struct wayland_output *output;
989 int output_width, output_height;
990
991 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
992 width, height, x, y);
993
994 output = zalloc(sizeof *output);
995 if (output == NULL)
996 return NULL;
997
998 output->name = name ? strdup(name) : NULL;
999 output->base.make = "waywayland";
1000 output->base.model = "none";
1001
1002 output_width = width * scale;
1003 output_height = height * scale;
1004
1005 output->parent.surface =
1006 wl_compositor_create_surface(b->parent.compositor);
1007 if (!output->parent.surface)
1008 goto err_name;
1009 wl_surface_set_user_data(output->parent.surface, output);
1010
1011 output->parent.draw_initial_frame = 1;
1012
1013 if (b->parent.shell) {
1014 output->parent.shell_surface =
1015 wl_shell_get_shell_surface(b->parent.shell,
1016 output->parent.surface);
1017 if (!output->parent.shell_surface)
1018 goto err_surface;
1019 wl_shell_surface_add_listener(output->parent.shell_surface,
1020 &shell_surface_listener, output);
1021 }
1022
1023 if (fullscreen && b->parent.shell) {
1024 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1025 0, 0, NULL);
1026 wl_display_roundtrip(b->parent.wl_display);
1027 if (!width)
1028 output_width = output->parent.configure_width;
1029 if (!height)
1030 output_height = output->parent.configure_height;
1031 }
1032
1033 output->mode.flags =
1034 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1035 output->mode.width = output_width;
1036 output->mode.height = output_height;
1037 output->mode.refresh = 60000;
1038 output->scale = scale;
1039 wl_list_init(&output->base.mode_list);
1040 wl_list_insert(&output->base.mode_list, &output->mode.link);
1041 output->base.current_mode = &output->mode;
1042
1043 wl_list_init(&output->shm.buffers);
1044 wl_list_init(&output->shm.free_buffers);
1045
1046 weston_output_init(&output->base, b->compositor, x, y, width, height,
1047 transform, scale);
1048
1049 if (b->use_pixman) {
1050 if (wayland_output_init_pixman_renderer(output) < 0)
1051 goto err_output;
1052 output->base.repaint = wayland_output_repaint_pixman;
1053 } else {
1054 if (wayland_output_init_gl_renderer(output) < 0)
1055 goto err_output;
1056 output->base.repaint = wayland_output_repaint_gl;
1057 }
1058
1059 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1060 output->base.destroy = wayland_output_destroy;
1061 output->base.assign_planes = NULL;
1062 output->base.set_backlight = NULL;
1063 output->base.set_dpms = NULL;
1064 output->base.switch_mode = wayland_output_switch_mode;
1065
1066 weston_compositor_add_output(b->compositor, &output->base);
1067
1068 return output;
1069
1070 err_output:
1071 weston_output_destroy(&output->base);
1072 if (output->parent.shell_surface)
1073 wl_shell_surface_destroy(output->parent.shell_surface);
1074 err_surface:
1075 wl_surface_destroy(output->parent.surface);
1076 err_name:
1077 free(output->name);
1078
1079 /* FIXME: cleanup weston_output */
1080 free(output);
1081
1082 return NULL;
1083 }
1084
1085 static struct wayland_output *
wayland_output_create_for_config(struct wayland_backend * b,struct weston_config_section * config_section,int option_width,int option_height,int option_scale,int32_t x,int32_t y)1086 wayland_output_create_for_config(struct wayland_backend *b,
1087 struct weston_config_section *config_section,
1088 int option_width, int option_height,
1089 int option_scale, int32_t x, int32_t y)
1090 {
1091 struct wayland_output *output;
1092 char *mode, *t, *name, *str;
1093 int width, height, scale;
1094 uint32_t transform;
1095 unsigned int slen;
1096
1097 weston_config_section_get_string(config_section, "name", &name, NULL);
1098 if (name) {
1099 slen = strlen(name);
1100 slen += strlen(WINDOW_TITLE " - ");
1101 str = malloc(slen + 1);
1102 if (str)
1103 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1104 free(name);
1105 name = str;
1106 }
1107 if (!name)
1108 name = strdup(WINDOW_TITLE);
1109
1110 weston_config_section_get_string(config_section,
1111 "mode", &mode, "1024x600");
1112 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1113 weston_log("Invalid mode \"%s\" for output %s\n",
1114 mode, name);
1115 width = 1024;
1116 height = 640;
1117 }
1118 free(mode);
1119
1120 if (option_width)
1121 width = option_width;
1122 if (option_height)
1123 height = option_height;
1124
1125 weston_config_section_get_int(config_section, "scale", &scale, 1);
1126
1127 if (option_scale)
1128 scale = option_scale;
1129
1130 weston_config_section_get_string(config_section,
1131 "transform", &t, "normal");
1132 if (weston_parse_transform(t, &transform) < 0)
1133 weston_log("Invalid transform \"%s\" for output %s\n",
1134 t, name);
1135 free(t);
1136
1137 output = wayland_output_create(b, x, y, width, height, name, 0,
1138 transform, scale);
1139 free(name);
1140
1141 return output;
1142 }
1143
1144 static struct wayland_output *
wayland_output_create_for_parent_output(struct wayland_backend * b,struct wayland_parent_output * poutput)1145 wayland_output_create_for_parent_output(struct wayland_backend *b,
1146 struct wayland_parent_output *poutput)
1147 {
1148 struct wayland_output *output;
1149 struct weston_mode *mode;
1150 int32_t x;
1151
1152 if (poutput->current_mode) {
1153 mode = poutput->current_mode;
1154 } else if (poutput->preferred_mode) {
1155 mode = poutput->preferred_mode;
1156 } else if (!wl_list_empty(&poutput->mode_list)) {
1157 mode = container_of(poutput->mode_list.next,
1158 struct weston_mode, link);
1159 } else {
1160 weston_log("No valid modes found. Skipping output");
1161 return NULL;
1162 }
1163
1164 if (!wl_list_empty(&b->compositor->output_list)) {
1165 output = container_of(b->compositor->output_list.prev,
1166 struct wayland_output, base.link);
1167 x = output->base.x + output->base.current_mode->width;
1168 } else {
1169 x = 0;
1170 }
1171
1172 output = wayland_output_create(b, x, 0, mode->width, mode->height,
1173 NULL, 0,
1174 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1175 if (!output)
1176 return NULL;
1177
1178 output->parent.output = poutput->global;
1179
1180 output->base.make = poutput->physical.make;
1181 output->base.model = poutput->physical.model;
1182 wl_list_init(&output->base.mode_list);
1183 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1184 wl_list_init(&poutput->mode_list);
1185
1186 wayland_output_set_fullscreen(output,
1187 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1188 mode->refresh, poutput->global);
1189
1190 if (output->parent.shell_surface) {
1191 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1192 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1193 mode->refresh, poutput->global);
1194 } else if (b->parent.fshell) {
1195 _wl_fullscreen_shell_present_surface(b->parent.fshell,
1196 output->parent.surface,
1197 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1198 poutput->global);
1199 _wl_fullscreen_shell_mode_feedback_destroy(
1200 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
1201 output->parent.surface,
1202 poutput->global,
1203 mode->refresh));
1204 }
1205
1206 return output;
1207 }
1208
1209 static void
shell_surface_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)1210 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1211 uint32_t serial)
1212 {
1213 wl_shell_surface_pong(shell_surface, serial);
1214 }
1215
1216 static void
shell_surface_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)1217 shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1218 uint32_t edges, int32_t width, int32_t height)
1219 {
1220 struct wayland_output *output = data;
1221
1222 output->parent.configure_width = width;
1223 output->parent.configure_height = height;
1224
1225 /* FIXME: implement resizing */
1226 }
1227
1228 static void
shell_surface_popup_done(void * data,struct wl_shell_surface * shell_surface)1229 shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1230 {
1231 }
1232
1233 static const struct wl_shell_surface_listener shell_surface_listener = {
1234 shell_surface_ping,
1235 shell_surface_configure,
1236 shell_surface_popup_done
1237 };
1238
1239 /* Events received from the wayland-server this compositor is client of: */
1240
1241 /* parent input interface */
1242 static void
input_set_cursor(struct wayland_input * input)1243 input_set_cursor(struct wayland_input *input)
1244 {
1245
1246 struct wl_buffer *buffer;
1247 struct wl_cursor_image *image;
1248
1249 if (!input->backend->cursor)
1250 return; /* Couldn't load the cursor. Can't set it */
1251
1252 image = input->backend->cursor->images[0];
1253 buffer = wl_cursor_image_get_buffer(image);
1254 if (!buffer)
1255 return;
1256
1257 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1258 input->parent.cursor.surface,
1259 image->hotspot_x, image->hotspot_y);
1260
1261 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1262 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1263 image->width, image->height);
1264 wl_surface_commit(input->parent.cursor.surface);
1265 }
1266
1267 static void
input_handle_pointer_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t x,wl_fixed_t y)1268 input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1269 uint32_t serial, struct wl_surface *surface,
1270 wl_fixed_t x, wl_fixed_t y)
1271 {
1272 struct wayland_input *input = data;
1273 int32_t fx, fy;
1274 enum theme_location location;
1275
1276 /* XXX: If we get a modifier event immediately before the focus,
1277 * we should try to keep the same serial. */
1278 input->enter_serial = serial;
1279 input->output = wl_surface_get_user_data(surface);
1280
1281 if (input->output->frame) {
1282 location = frame_pointer_enter(input->output->frame, input,
1283 wl_fixed_to_int(x),
1284 wl_fixed_to_int(y));
1285 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1286 x -= wl_fixed_from_int(fx);
1287 y -= wl_fixed_from_int(fy);
1288
1289 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1290 weston_output_schedule_repaint(&input->output->base);
1291 } else {
1292 location = THEME_LOCATION_CLIENT_AREA;
1293 }
1294
1295 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1296
1297 if (location == THEME_LOCATION_CLIENT_AREA) {
1298 input->focus = 1;
1299 notify_pointer_focus(&input->base, &input->output->base, x, y);
1300 wl_pointer_set_cursor(input->parent.pointer,
1301 input->enter_serial, NULL, 0, 0);
1302 } else {
1303 input->focus = 0;
1304 notify_pointer_focus(&input->base, NULL, 0, 0);
1305 input_set_cursor(input);
1306 }
1307 }
1308
1309 static void
input_handle_pointer_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)1310 input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1311 uint32_t serial, struct wl_surface *surface)
1312 {
1313 struct wayland_input *input = data;
1314
1315 if (!input->output)
1316 return;
1317
1318 if (input->output->frame) {
1319 frame_pointer_leave(input->output->frame, input);
1320
1321 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1322 weston_output_schedule_repaint(&input->output->base);
1323 }
1324
1325 notify_pointer_focus(&input->base, NULL, 0, 0);
1326 input->output = NULL;
1327 input->focus = 0;
1328 }
1329
1330 static void
input_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t x,wl_fixed_t y)1331 input_handle_motion(void *data, struct wl_pointer *pointer,
1332 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1333 {
1334 struct wayland_input *input = data;
1335 int32_t fx, fy;
1336 enum theme_location location;
1337
1338 if (!input->output)
1339 return;
1340
1341 if (input->output->frame) {
1342 location = frame_pointer_motion(input->output->frame, input,
1343 wl_fixed_to_int(x),
1344 wl_fixed_to_int(y));
1345 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1346 x -= wl_fixed_from_int(fx);
1347 y -= wl_fixed_from_int(fy);
1348
1349 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1350 weston_output_schedule_repaint(&input->output->base);
1351 } else {
1352 location = THEME_LOCATION_CLIENT_AREA;
1353 }
1354
1355 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
1356
1357 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1358 input_set_cursor(input);
1359 notify_pointer_focus(&input->base, NULL, 0, 0);
1360 input->focus = 0;
1361 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1362 wl_pointer_set_cursor(input->parent.pointer,
1363 input->enter_serial, NULL, 0, 0);
1364 notify_pointer_focus(&input->base, &input->output->base, x, y);
1365 input->focus = 1;
1366 }
1367
1368 if (location == THEME_LOCATION_CLIENT_AREA)
1369 notify_motion_absolute(&input->base, time, x, y);
1370 }
1371
1372 static void
input_handle_button(void * data,struct wl_pointer * pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state_w)1373 input_handle_button(void *data, struct wl_pointer *pointer,
1374 uint32_t serial, uint32_t time, uint32_t button,
1375 uint32_t state_w)
1376 {
1377 struct wayland_input *input = data;
1378 enum wl_pointer_button_state state = state_w;
1379 enum frame_button_state fstate;
1380 enum theme_location location;
1381
1382 if (!input->output)
1383 return;
1384
1385 if (input->output->frame) {
1386 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1387 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1388
1389 location = frame_pointer_button(input->output->frame, input,
1390 button, fstate);
1391
1392 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1393
1394 wl_shell_surface_move(input->output->parent.shell_surface,
1395 input->parent.seat, serial);
1396 frame_status_clear(input->output->frame,
1397 FRAME_STATUS_MOVE);
1398 return;
1399 }
1400
1401 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1402 wayland_output_destroy(&input->output->base);
1403 input->output = NULL;
1404 input->keyboard_focus = NULL;
1405
1406 if (wl_list_empty(&input->backend->compositor->output_list))
1407 weston_compositor_exit(input->backend->compositor);
1408
1409 return;
1410 }
1411
1412 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1413 weston_output_schedule_repaint(&input->output->base);
1414 } else {
1415 location = THEME_LOCATION_CLIENT_AREA;
1416 }
1417
1418 if (location == THEME_LOCATION_CLIENT_AREA)
1419 notify_button(&input->base, time, button, state);
1420 }
1421
1422 static void
input_handle_axis(void * data,struct wl_pointer * pointer,uint32_t time,uint32_t axis,wl_fixed_t value)1423 input_handle_axis(void *data, struct wl_pointer *pointer,
1424 uint32_t time, uint32_t axis, wl_fixed_t value)
1425 {
1426 struct wayland_input *input = data;
1427
1428 notify_axis(&input->base, time, axis, value);
1429 }
1430
1431 static const struct wl_pointer_listener pointer_listener = {
1432 input_handle_pointer_enter,
1433 input_handle_pointer_leave,
1434 input_handle_motion,
1435 input_handle_button,
1436 input_handle_axis,
1437 };
1438
1439 static void
input_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)1440 input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1441 int fd, uint32_t size)
1442 {
1443 struct wayland_input *input = data;
1444 struct xkb_keymap *keymap;
1445 char *map_str;
1446
1447 if (!data) {
1448 close(fd);
1449 return;
1450 }
1451
1452 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1453 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1454 if (map_str == MAP_FAILED) {
1455 weston_log("mmap failed: %m\n");
1456 goto error;
1457 }
1458
1459 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
1460 map_str,
1461 XKB_KEYMAP_FORMAT_TEXT_V1,
1462 0);
1463 munmap(map_str, size);
1464
1465 if (!keymap) {
1466 weston_log("failed to compile keymap\n");
1467 goto error;
1468 }
1469
1470 input->keyboard_state_update = STATE_UPDATE_NONE;
1471 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1472 weston_log("No keymap provided; falling back to defalt\n");
1473 keymap = NULL;
1474 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1475 } else {
1476 weston_log("Invalid keymap\n");
1477 goto error;
1478 }
1479
1480 close(fd);
1481
1482 if (weston_seat_get_keyboard(&input->base))
1483 weston_seat_update_keymap(&input->base, keymap);
1484 else
1485 weston_seat_init_keyboard(&input->base, keymap);
1486
1487 xkb_keymap_unref(keymap);
1488
1489 return;
1490
1491 error:
1492 wl_keyboard_release(input->parent.keyboard);
1493 close(fd);
1494 }
1495
1496 static void
input_handle_keyboard_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)1497 input_handle_keyboard_enter(void *data,
1498 struct wl_keyboard *keyboard,
1499 uint32_t serial,
1500 struct wl_surface *surface,
1501 struct wl_array *keys)
1502 {
1503 struct wayland_input *input = data;
1504 struct wayland_output *focus;
1505
1506 focus = input->keyboard_focus;
1507 if (focus) {
1508 /* This shouldn't happen */
1509 focus->keyboard_count--;
1510 if (!focus->keyboard_count && focus->frame)
1511 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1512 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1513 weston_output_schedule_repaint(&focus->base);
1514 }
1515
1516 input->keyboard_focus = wl_surface_get_user_data(surface);
1517 input->keyboard_focus->keyboard_count++;
1518
1519 focus = input->keyboard_focus;
1520 if (focus->frame) {
1521 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1522 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1523 weston_output_schedule_repaint(&focus->base);
1524 }
1525
1526
1527 /* XXX: If we get a modifier event immediately before the focus,
1528 * we should try to keep the same serial. */
1529 notify_keyboard_focus_in(&input->base, keys,
1530 STATE_UPDATE_AUTOMATIC);
1531 }
1532
1533 static void
input_handle_keyboard_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)1534 input_handle_keyboard_leave(void *data,
1535 struct wl_keyboard *keyboard,
1536 uint32_t serial,
1537 struct wl_surface *surface)
1538 {
1539 struct wayland_input *input = data;
1540 struct wayland_output *focus;
1541
1542 notify_keyboard_focus_out(&input->base);
1543
1544 focus = input->keyboard_focus;
1545 if (!focus)
1546 return;
1547
1548 focus->keyboard_count--;
1549 if (!focus->keyboard_count && focus->frame) {
1550 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1551 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1552 weston_output_schedule_repaint(&focus->base);
1553 }
1554
1555 input->keyboard_focus = NULL;
1556 }
1557
1558 static void
input_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state)1559 input_handle_key(void *data, struct wl_keyboard *keyboard,
1560 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1561 {
1562 struct wayland_input *input = data;
1563
1564 input->key_serial = serial;
1565 notify_key(&input->base, time, key,
1566 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
1567 WL_KEYBOARD_KEY_STATE_RELEASED,
1568 input->keyboard_state_update);
1569 }
1570
1571 static void
input_handle_modifiers(void * data,struct wl_keyboard * wl_keyboard,uint32_t serial_in,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)1572 input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
1573 uint32_t serial_in, uint32_t mods_depressed,
1574 uint32_t mods_latched, uint32_t mods_locked,
1575 uint32_t group)
1576 {
1577 struct weston_keyboard *keyboard;
1578 struct wayland_input *input = data;
1579 struct wayland_backend *b = input->backend;
1580 uint32_t serial_out;
1581
1582 /* If we get a key event followed by a modifier event with the
1583 * same serial number, then we try to preserve those semantics by
1584 * reusing the same serial number on the way out too. */
1585 if (serial_in == input->key_serial)
1586 serial_out = wl_display_get_serial(b->compositor->wl_display);
1587 else
1588 serial_out = wl_display_next_serial(b->compositor->wl_display);
1589
1590 keyboard = weston_seat_get_keyboard(&input->base);
1591 xkb_state_update_mask(keyboard->xkb_state.state,
1592 mods_depressed, mods_latched,
1593 mods_locked, 0, 0, group);
1594 notify_modifiers(&input->base, serial_out);
1595 }
1596
1597 static void
input_handle_repeat_info(void * data,struct wl_keyboard * keyboard,int32_t rate,int32_t delay)1598 input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1599 int32_t rate, int32_t delay)
1600 {
1601 struct wayland_input *input = data;
1602 struct wayland_backend *b = input->backend;
1603
1604 b->compositor->kb_repeat_rate = rate;
1605 b->compositor->kb_repeat_delay = delay;
1606 }
1607
1608 static const struct wl_keyboard_listener keyboard_listener = {
1609 input_handle_keymap,
1610 input_handle_keyboard_enter,
1611 input_handle_keyboard_leave,
1612 input_handle_key,
1613 input_handle_modifiers,
1614 input_handle_repeat_info,
1615 };
1616
1617 static void
input_handle_capabilities(void * data,struct wl_seat * seat,enum wl_seat_capability caps)1618 input_handle_capabilities(void *data, struct wl_seat *seat,
1619 enum wl_seat_capability caps)
1620 {
1621 struct wayland_input *input = data;
1622
1623 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1624 input->parent.pointer = wl_seat_get_pointer(seat);
1625 wl_pointer_set_user_data(input->parent.pointer, input);
1626 wl_pointer_add_listener(input->parent.pointer,
1627 &pointer_listener, input);
1628 weston_seat_init_pointer(&input->base);
1629 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1630 wl_pointer_destroy(input->parent.pointer);
1631 input->parent.pointer = NULL;
1632 }
1633
1634 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1635 input->parent.keyboard = wl_seat_get_keyboard(seat);
1636 wl_keyboard_set_user_data(input->parent.keyboard, input);
1637 wl_keyboard_add_listener(input->parent.keyboard,
1638 &keyboard_listener, input);
1639 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1640 wl_keyboard_destroy(input->parent.keyboard);
1641 input->parent.keyboard = NULL;
1642 }
1643 }
1644
1645 static void
input_handle_name(void * data,struct wl_seat * seat,const char * name)1646 input_handle_name(void *data, struct wl_seat *seat,
1647 const char *name)
1648 {
1649 }
1650
1651 static const struct wl_seat_listener seat_listener = {
1652 input_handle_capabilities,
1653 input_handle_name,
1654 };
1655
1656 static void
display_add_seat(struct wayland_backend * b,uint32_t id,uint32_t version)1657 display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t version)
1658 {
1659 struct wayland_input *input;
1660
1661 input = zalloc(sizeof *input);
1662 if (input == NULL)
1663 return;
1664
1665 weston_seat_init(&input->base, b->compositor, "default");
1666 input->backend = b;
1667 input->parent.seat = wl_registry_bind(b->parent.registry, id,
1668 &wl_seat_interface, MIN(version, 4));
1669 wl_list_insert(b->input_list.prev, &input->link);
1670
1671 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1672 wl_seat_set_user_data(input->parent.seat, input);
1673
1674 input->parent.cursor.surface =
1675 wl_compositor_create_surface(b->parent.compositor);
1676 }
1677
1678 static void
wayland_parent_output_geometry(void * data,struct wl_output * output_proxy,int32_t x,int32_t y,int32_t physical_width,int32_t physical_height,int32_t subpixel,const char * make,const char * model,int32_t transform)1679 wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1680 int32_t x, int32_t y,
1681 int32_t physical_width, int32_t physical_height,
1682 int32_t subpixel, const char *make,
1683 const char *model, int32_t transform)
1684 {
1685 struct wayland_parent_output *output = data;
1686
1687 output->x = x;
1688 output->y = y;
1689 output->physical.width = physical_width;
1690 output->physical.height = physical_height;
1691 output->physical.subpixel = subpixel;
1692
1693 free(output->physical.make);
1694 output->physical.make = strdup(make);
1695 free(output->physical.model);
1696 output->physical.model = strdup(model);
1697
1698 output->transform = transform;
1699 }
1700
1701 static struct weston_mode *
find_mode(struct wl_list * list,int32_t width,int32_t height,uint32_t refresh)1702 find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1703 {
1704 struct weston_mode *mode;
1705
1706 wl_list_for_each(mode, list, link) {
1707 if (mode->width == width && mode->height == height &&
1708 mode->refresh == refresh)
1709 return mode;
1710 }
1711
1712 mode = zalloc(sizeof *mode);
1713 if (!mode)
1714 return NULL;
1715
1716 mode->width = width;
1717 mode->height = height;
1718 mode->refresh = refresh;
1719 wl_list_insert(list, &mode->link);
1720
1721 return mode;
1722 }
1723
1724 static void
wayland_parent_output_mode(void * data,struct wl_output * wl_output_proxy,uint32_t flags,int32_t width,int32_t height,int32_t refresh)1725 wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1726 uint32_t flags, int32_t width, int32_t height,
1727 int32_t refresh)
1728 {
1729 struct wayland_parent_output *output = data;
1730 struct weston_mode *mode;
1731
1732 if (output->output) {
1733 mode = find_mode(&output->output->base.mode_list,
1734 width, height, refresh);
1735 if (!mode)
1736 return;
1737 mode->flags = flags;
1738 /* Do a mode-switch on current mode change? */
1739 } else {
1740 mode = find_mode(&output->mode_list, width, height, refresh);
1741 if (!mode)
1742 return;
1743 mode->flags = flags;
1744 if (flags & WL_OUTPUT_MODE_CURRENT)
1745 output->current_mode = mode;
1746 if (flags & WL_OUTPUT_MODE_PREFERRED)
1747 output->preferred_mode = mode;
1748 }
1749 }
1750
1751 static const struct wl_output_listener output_listener = {
1752 wayland_parent_output_geometry,
1753 wayland_parent_output_mode
1754 };
1755
1756 static void
wayland_backend_register_output(struct wayland_backend * b,uint32_t id)1757 wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
1758 {
1759 struct wayland_parent_output *output;
1760
1761 output = zalloc(sizeof *output);
1762 if (!output)
1763 return;
1764
1765 output->id = id;
1766 output->global = wl_registry_bind(b->parent.registry, id,
1767 &wl_output_interface, 1);
1768 if (!output->global) {
1769 free(output);
1770 return;
1771 }
1772
1773 wl_output_add_listener(output->global, &output_listener, output);
1774
1775 output->scale = 0;
1776 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1777 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1778 wl_list_init(&output->mode_list);
1779 wl_list_insert(&b->parent.output_list, &output->link);
1780
1781 if (b->sprawl_across_outputs) {
1782 wl_display_roundtrip(b->parent.wl_display);
1783 wayland_output_create_for_parent_output(b, output);
1784 }
1785 }
1786
1787 static void
wayland_parent_output_destroy(struct wayland_parent_output * output)1788 wayland_parent_output_destroy(struct wayland_parent_output *output)
1789 {
1790 struct weston_mode *mode, *next;
1791
1792 if (output->output)
1793 wayland_output_destroy(&output->output->base);
1794
1795 wl_output_destroy(output->global);
1796 free(output->physical.make);
1797 free(output->physical.model);
1798
1799 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1800 wl_list_remove(&mode->link);
1801 free(mode);
1802 }
1803 }
1804
1805 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)1806 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1807 const char *interface, uint32_t version)
1808 {
1809 struct wayland_backend *b = data;
1810
1811 if (strcmp(interface, "wl_compositor") == 0) {
1812 b->parent.compositor =
1813 wl_registry_bind(registry, name,
1814 &wl_compositor_interface, 1);
1815 } else if (strcmp(interface, "wl_shell") == 0) {
1816 b->parent.shell =
1817 wl_registry_bind(registry, name,
1818 &wl_shell_interface, 1);
1819 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1820 b->parent.fshell =
1821 wl_registry_bind(registry, name,
1822 &_wl_fullscreen_shell_interface, 1);
1823 } else if (strcmp(interface, "wl_seat") == 0) {
1824 display_add_seat(b, name, version);
1825 } else if (strcmp(interface, "wl_output") == 0) {
1826 wayland_backend_register_output(b, name);
1827 } else if (strcmp(interface, "wl_shm") == 0) {
1828 b->parent.shm =
1829 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1830 }
1831 }
1832
1833 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)1834 registry_handle_global_remove(void *data, struct wl_registry *registry,
1835 uint32_t name)
1836 {
1837 struct wayland_backend *b = data;
1838 struct wayland_parent_output *output;
1839
1840 wl_list_for_each(output, &b->parent.output_list, link)
1841 if (output->id == name)
1842 wayland_parent_output_destroy(output);
1843 }
1844
1845 static const struct wl_registry_listener registry_listener = {
1846 registry_handle_global,
1847 registry_handle_global_remove
1848 };
1849
1850 static int
wayland_backend_handle_event(int fd,uint32_t mask,void * data)1851 wayland_backend_handle_event(int fd, uint32_t mask, void *data)
1852 {
1853 struct wayland_backend *b = data;
1854 int count = 0;
1855
1856 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1857 weston_compositor_exit(b->compositor);
1858 return 0;
1859 }
1860
1861 if (mask & WL_EVENT_READABLE)
1862 count = wl_display_dispatch(b->parent.wl_display);
1863 if (mask & WL_EVENT_WRITABLE)
1864 wl_display_flush(b->parent.wl_display);
1865
1866 if (mask == 0) {
1867 count = wl_display_dispatch_pending(b->parent.wl_display);
1868 wl_display_flush(b->parent.wl_display);
1869 }
1870
1871 return count;
1872 }
1873
1874 static void
wayland_restore(struct weston_compositor * ec)1875 wayland_restore(struct weston_compositor *ec)
1876 {
1877 }
1878
1879 static void
wayland_destroy(struct weston_compositor * ec)1880 wayland_destroy(struct weston_compositor *ec)
1881 {
1882 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
1883
1884 weston_compositor_shutdown(ec);
1885
1886 if (b->parent.shm)
1887 wl_shm_destroy(b->parent.shm);
1888
1889 free(b);
1890 }
1891
1892 static const char *left_ptrs[] = {
1893 "left_ptr",
1894 "default",
1895 "top_left_arrow",
1896 "left-arrow"
1897 };
1898
1899 static void
create_cursor(struct wayland_backend * b,struct weston_config * config)1900 create_cursor(struct wayland_backend *b, struct weston_config *config)
1901 {
1902 struct weston_config_section *s;
1903 int size;
1904 char *theme = NULL;
1905 unsigned int i;
1906
1907 s = weston_config_get_section(config, "shell", NULL, NULL);
1908 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1909 weston_config_section_get_int(s, "cursor-size", &size, 32);
1910
1911 b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
1912 if (!b->cursor_theme) {
1913 fprintf(stderr, "could not load cursor theme\n");
1914 return;
1915 }
1916
1917 free(theme);
1918
1919 b->cursor = NULL;
1920 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1921 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
1922 left_ptrs[i]);
1923 if (!b->cursor) {
1924 fprintf(stderr, "could not load left cursor\n");
1925 return;
1926 }
1927 }
1928
1929 static void
fullscreen_binding(struct weston_keyboard * keyboard,uint32_t time,uint32_t key,void * data)1930 fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
1931 uint32_t key, void *data)
1932 {
1933 struct wayland_backend *b = data;
1934 struct wayland_input *input = NULL;
1935
1936 wl_list_for_each(input, &b->input_list, link)
1937 if (&input->base == keyboard->seat)
1938 break;
1939
1940 if (!input || !input->output)
1941 return;
1942
1943 if (input->output->frame)
1944 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1945 else
1946 wayland_output_set_windowed(input->output);
1947
1948 weston_output_schedule_repaint(&input->output->base);
1949 }
1950
1951 static struct wayland_backend *
wayland_backend_create(struct weston_compositor * compositor,int use_pixman,const char * display_name,int * argc,char * argv[],struct weston_config * config)1952 wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
1953 const char *display_name, int *argc, char *argv[],
1954 struct weston_config *config)
1955 {
1956 struct wayland_backend *b;
1957 struct wl_event_loop *loop;
1958 int fd;
1959
1960 b = zalloc(sizeof *b);
1961 if (b == NULL)
1962 return NULL;
1963
1964 b->compositor = compositor;
1965 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
1966 goto err_compositor;
1967
1968 b->parent.wl_display = wl_display_connect(display_name);
1969 if (b->parent.wl_display == NULL) {
1970 weston_log("failed to create display: %m\n");
1971 goto err_compositor;
1972 }
1973
1974 wl_list_init(&b->parent.output_list);
1975 wl_list_init(&b->input_list);
1976 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
1977 wl_registry_add_listener(b->parent.registry, ®istry_listener, b);
1978 wl_display_roundtrip(b->parent.wl_display);
1979
1980 create_cursor(b, config);
1981
1982 b->use_pixman = use_pixman;
1983
1984 if (!b->use_pixman) {
1985 gl_renderer = weston_load_module("gl-renderer.so",
1986 "gl_renderer_interface");
1987 if (!gl_renderer)
1988 b->use_pixman = 1;
1989 }
1990
1991 if (!b->use_pixman) {
1992 if (gl_renderer->create(compositor,
1993 EGL_PLATFORM_WAYLAND_KHR,
1994 b->parent.wl_display,
1995 gl_renderer->alpha_attribs,
1996 NULL,
1997 0) < 0) {
1998 weston_log("Failed to initialize the GL renderer; "
1999 "falling back to pixman.\n");
2000 b->use_pixman = 1;
2001 }
2002 }
2003
2004 if (b->use_pixman) {
2005 if (pixman_renderer_init(compositor) < 0) {
2006 weston_log("Failed to initialize pixman renderer\n");
2007 goto err_display;
2008 }
2009 }
2010
2011 b->base.destroy = wayland_destroy;
2012 b->base.restore = wayland_restore;
2013
2014 loop = wl_display_get_event_loop(compositor->wl_display);
2015
2016 fd = wl_display_get_fd(b->parent.wl_display);
2017 b->parent.wl_source =
2018 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2019 wayland_backend_handle_event, b);
2020 if (b->parent.wl_source == NULL)
2021 goto err_display;
2022
2023 wl_event_source_check(b->parent.wl_source);
2024
2025 compositor->backend = &b->base;
2026 return b;
2027 err_display:
2028 wl_display_disconnect(b->parent.wl_display);
2029 err_compositor:
2030 weston_compositor_shutdown(compositor);
2031 free(b);
2032 return NULL;
2033 }
2034
2035 static void
wayland_backend_destroy(struct wayland_backend * b)2036 wayland_backend_destroy(struct wayland_backend *b)
2037 {
2038 wl_display_disconnect(b->parent.wl_display);
2039
2040 if (b->theme)
2041 theme_destroy(b->theme);
2042 if (b->frame_device)
2043 cairo_device_destroy(b->frame_device);
2044 wl_cursor_theme_destroy(b->cursor_theme);
2045
2046 weston_compositor_shutdown(b->compositor);
2047 free(b);
2048 }
2049
2050 WL_EXPORT int
backend_init(struct weston_compositor * compositor,int * argc,char * argv[],struct weston_config * config)2051 backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
2052 struct weston_config *config)
2053 {
2054 struct wayland_backend *b;
2055 struct wayland_output *output;
2056 struct wayland_parent_output *poutput;
2057 struct weston_config_section *section;
2058 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
2059 const char *section_name, *display_name;
2060 char *name;
2061
2062 const struct weston_option wayland_options[] = {
2063 { WESTON_OPTION_INTEGER, "width", 0, &width },
2064 { WESTON_OPTION_INTEGER, "height", 0, &height },
2065 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
2066 { WESTON_OPTION_STRING, "display", 0, &display_name },
2067 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2068 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
2069 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
2070 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
2071 };
2072
2073 width = 0;
2074 height = 0;
2075 scale = 0;
2076 display_name = NULL;
2077 use_pixman = 0;
2078 count = 1;
2079 fullscreen = 0;
2080 sprawl = 0;
2081 parse_options(wayland_options,
2082 ARRAY_LENGTH(wayland_options), argc, argv);
2083
2084 b = wayland_backend_create(compositor, use_pixman, display_name,
2085 argc, argv, config);
2086 if (!b)
2087 return -1;
2088
2089 if (sprawl || b->parent.fshell) {
2090 b->sprawl_across_outputs = 1;
2091 wl_display_roundtrip(b->parent.wl_display);
2092
2093 wl_list_for_each(poutput, &b->parent.output_list, link)
2094 wayland_output_create_for_parent_output(b, poutput);
2095
2096 return 0;
2097 }
2098
2099 if (fullscreen) {
2100 output = wayland_output_create(b, 0, 0, width, height,
2101 NULL, 1, 0, 1);
2102 if (!output)
2103 goto err_outputs;
2104
2105 wayland_output_set_fullscreen(output, 0, 0, NULL);
2106 return 0;
2107 }
2108
2109 section = NULL;
2110 x = 0;
2111 while (weston_config_next_section(config, §ion, §ion_name)) {
2112 if (!section_name || strcmp(section_name, "output") != 0)
2113 continue;
2114 weston_config_section_get_string(section, "name", &name, NULL);
2115 if (name == NULL)
2116 continue;
2117
2118 if (name[0] != 'W' || name[1] != 'L') {
2119 free(name);
2120 continue;
2121 }
2122 free(name);
2123
2124 output = wayland_output_create_for_config(b, section, width,
2125 height, scale, x, 0);
2126 if (!output)
2127 goto err_outputs;
2128 if (wayland_output_set_windowed(output))
2129 goto err_outputs;
2130
2131 x += output->base.width;
2132 --count;
2133 }
2134
2135 if (!width)
2136 width = 1024;
2137 if (!height)
2138 height = 640;
2139 if (!scale)
2140 scale = 1;
2141 while (count > 0) {
2142 output = wayland_output_create(b, x, 0, width, height,
2143 NULL, 0, 0, scale);
2144 if (!output)
2145 goto err_outputs;
2146 if (wayland_output_set_windowed(output))
2147 goto err_outputs;
2148
2149 x += width;
2150 --count;
2151 }
2152
2153 weston_compositor_add_key_binding(compositor, KEY_F,
2154 MODIFIER_CTRL | MODIFIER_ALT,
2155 fullscreen_binding, b);
2156
2157 return 0;
2158
2159 err_outputs:
2160 wayland_backend_destroy(b);
2161 return -1;
2162 }
2163