blob: 4266bb64faf2c6d7b36f58454f699afeaca1c720 [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003 * Copyright © 2013 Jason Ekstrand
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * 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:
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * 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.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010028
Armin Krezović174448a2016-09-30 14:11:09 +020029#include <assert.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030#include <stddef.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030031#include <stdint.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010032#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <fcntl.h>
36#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010037#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060038#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039
Benjamin Franzkebe014562011-02-18 17:04:24 +010040#include <wayland-client.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050041#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042
Armin Krezovićb08e1a52016-12-09 22:58:27 +010043#ifdef ENABLE_EGL
44#include <wayland-egl.h>
45#endif
46
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047#include "compositor.h"
Benoit Gschwind639322a2016-04-28 20:33:15 +020048#include "compositor-wayland.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010049#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020050#include "weston-egl-ext.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050051#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070053#include "shared/image-loader.h"
54#include "shared/os-compatibility.h"
55#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080056#include "fullscreen-shell-unstable-v1-client-protocol.h"
Armin Krezović34476192016-11-21 18:42:42 +010057#include "xdg-shell-unstable-v6-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020059#include "linux-dmabuf.h"
Armin Krezović174448a2016-09-30 14:11:09 +020060#include "windowed-output-api.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010061
Jason Ekstrand48ce4212013-10-27 22:25:02 -050062#define WINDOW_TITLE "Weston Compositor"
63
Giulio Camuffo954f1832014-10-11 18:27:30 +030064struct wayland_backend {
65 struct weston_backend base;
66 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067
68 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040069 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040070 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010071 struct wl_compositor *compositor;
72 struct wl_shell *shell;
Armin Krezović34476192016-11-21 18:42:42 +010073 struct zxdg_shell_v6 *xdg_shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080074 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020075 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050077 struct wl_list output_list;
78
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010079 struct wl_event_source *wl_source;
80 uint32_t event_mask;
81 } parent;
82
Armin Krezović7e71b872016-10-09 17:30:22 +020083 bool use_pixman;
Armin Krezović7f1c0b82016-10-09 17:30:23 +020084 bool sprawl_across_outputs;
Armin Krezović20450162016-10-13 12:01:43 +020085 bool fullscreen;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050086
Jason Ekstrand7744f712013-10-27 22:24:55 -050087 struct theme *theme;
88 cairo_device_t *frame_device;
89 struct wl_cursor_theme *cursor_theme;
90 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050091
Jason Ekstrand06ced802013-11-07 20:13:29 -060092 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010093};
94
95struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050096 struct weston_output base;
97
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010098 struct {
Armin Krezović2d321e32016-10-09 17:30:25 +020099 bool draw_initial_frame;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500100 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500101
102 struct wl_output *output;
103 uint32_t global_id;
104
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500105 struct wl_shell_surface *shell_surface;
Armin Krezović34476192016-11-21 18:42:42 +0100106 struct zxdg_surface_v6 *xdg_surface;
107 struct zxdg_toplevel_v6 *xdg_toplevel;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600108 int configure_width, configure_height;
Armin Krezović34476192016-11-21 18:42:42 +0100109 bool wait_for_configure;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100110 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500111
Jason Ekstrand7744f712013-10-27 22:24:55 -0500112 int keyboard_count;
113
Sergi Granell2dcbb8d2017-03-24 20:48:01 +0100114 char *title;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500115 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500116
Jason Ekstrand7744f712013-10-27 22:24:55 -0500117 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500118 struct wl_egl_window *egl_window;
119 struct {
120 cairo_surface_t *top;
121 cairo_surface_t *left;
122 cairo_surface_t *right;
123 cairo_surface_t *bottom;
124 } border;
125 } gl;
126
127 struct {
128 struct wl_list buffers;
129 struct wl_list free_buffers;
130 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500131
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500132 struct weston_mode mode;
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800133
134 struct wl_callback *frame_cb;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100135};
136
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500137struct wayland_parent_output {
Pekka Paalanenb07de932017-10-19 12:03:06 +0300138 struct wayland_backend *backend; /**< convenience */
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500139 struct wayland_output *output;
140 struct wl_list link;
141
142 struct wl_output *global;
143 uint32_t id;
144
145 struct {
146 char *make;
147 char *model;
148 int32_t width, height;
149 uint32_t subpixel;
150 } physical;
151
152 int32_t x, y;
153 uint32_t transform;
154 uint32_t scale;
155
Pekka Paalanenb07de932017-10-19 12:03:06 +0300156 struct wl_callback *sync_cb; /**< wl_output < 2 done replacement */
157
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500158 struct wl_list mode_list;
159 struct weston_mode *preferred_mode;
160 struct weston_mode *current_mode;
161};
162
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500163struct wayland_shm_buffer {
164 struct wayland_output *output;
165 struct wl_list link;
166 struct wl_list free_link;
167
168 struct wl_buffer *buffer;
169 void *data;
170 size_t size;
171 pixman_region32_t damage;
172 int frame_damaged;
173
174 pixman_image_t *pm_image;
175 cairo_surface_t *c_surface;
176};
177
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100178struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400179 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300180 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100181 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500182
183 struct {
184 struct wl_seat *seat;
185 struct wl_pointer *pointer;
186 struct wl_keyboard *keyboard;
187 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500188
189 struct {
190 struct wl_surface *surface;
191 int32_t hx, hy;
192 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500193 } parent;
194
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500195 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100196 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400197 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600198 uint32_t touch_points;
199 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600200 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600201 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600202
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400203 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600204 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500205 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000206
207 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100208};
209
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300210struct gl_renderer_interface *gl_renderer;
211
Armin Krezović938dc522016-08-01 19:17:57 +0200212static inline struct wayland_output *
213to_wayland_output(struct weston_output *base)
214{
215 return container_of(base, struct wayland_output, base);
216}
217
218static inline struct wayland_backend *
219to_wayland_backend(struct weston_compositor *base)
220{
221 return container_of(base->backend, struct wayland_backend, base);
222}
223
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500224static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500225wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
226{
227 cairo_surface_destroy(buffer->c_surface);
228 pixman_image_unref(buffer->pm_image);
229
230 wl_buffer_destroy(buffer->buffer);
231 munmap(buffer->data, buffer->size);
232
233 pixman_region32_fini(&buffer->damage);
234
235 wl_list_remove(&buffer->link);
236 wl_list_remove(&buffer->free_link);
237 free(buffer);
238}
239
240static void
241buffer_release(void *data, struct wl_buffer *buffer)
242{
243 struct wayland_shm_buffer *sb = data;
244
245 if (sb->output) {
246 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
247 } else {
248 wayland_shm_buffer_destroy(sb);
249 }
250}
251
252static const struct wl_buffer_listener buffer_listener = {
253 buffer_release
254};
255
256static struct wayland_shm_buffer *
257wayland_output_get_shm_buffer(struct wayland_output *output)
258{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300259 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200260 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300261 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500262 struct wayland_shm_buffer *sb;
263
264 struct wl_shm_pool *pool;
265 int width, height, stride;
266 int32_t fx, fy;
267 int fd;
268 unsigned char *data;
269
270 if (!wl_list_empty(&output->shm.free_buffers)) {
271 sb = container_of(output->shm.free_buffers.next,
272 struct wayland_shm_buffer, free_link);
273 wl_list_remove(&sb->free_link);
274 wl_list_init(&sb->free_link);
275
276 return sb;
277 }
278
279 if (output->frame) {
280 width = frame_width(output->frame);
281 height = frame_height(output->frame);
282 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500283 width = output->base.current_mode->width;
284 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500285 }
286
287 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
288
289 fd = os_create_anonymous_file(height * stride);
290 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000291 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500292 return NULL;
293 }
294
295 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
296 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000297 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500298 close(fd);
299 return NULL;
300 }
301
302 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000303 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200304 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000305 close(fd);
Raúl Peñacobafec723e2017-03-29 22:23:07 +0200306 munmap(data, height * stride);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000307 return NULL;
308 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500309
310 sb->output = output;
311 wl_list_init(&sb->free_link);
312 wl_list_insert(&output->shm.buffers, &sb->link);
313
314 pixman_region32_init_rect(&sb->damage, 0, 0,
315 output->base.width, output->base.height);
316 sb->frame_damaged = 1;
317
318 sb->data = data;
319 sb->size = height * stride;
320
321 pool = wl_shm_create_pool(shm, fd, sb->size);
322
323 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
324 width, height,
325 stride,
326 WL_SHM_FORMAT_ARGB8888);
327 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
328 wl_shm_pool_destroy(pool);
329 close(fd);
330
331 memset(data, 0, sb->size);
332
333 sb->c_surface =
334 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
335 width, height, stride);
336
337 fx = 0;
338 fy = 0;
339 if (output->frame)
340 frame_interior(output->frame, &fx, &fy, 0, 0);
341 sb->pm_image =
342 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
343 (uint32_t *)(data + fy * stride) + fx,
344 stride);
345
346 return sb;
347}
348
349static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500350frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400351{
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800352 struct wayland_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400353 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400354
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800355 assert(callback == output->frame_cb);
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500356 wl_callback_destroy(callback);
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800357 output->frame_cb = NULL;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400358
359 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300360
361 /*
362 * This is the fallback case, where Presentation extension is not
363 * available from the parent compositor. We do not know the base for
364 * 'time', so we cannot feed it to finish_frame(). Do the only thing
365 * we can, and pretend finish_frame time is when we process this
366 * event.
367 */
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800368 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
369 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400370}
371
372static const struct wl_callback_listener frame_listener = {
373 frame_done
374};
375
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500376static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200377draw_initial_frame(struct wayland_output *output)
378{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200380
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500381 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200382
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500383 /* If we are rendering with GL, then orphan it so that it gets
384 * destroyed immediately */
385 if (output->gl.egl_window)
386 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500387
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500389 wl_surface_damage(output->parent.surface, 0, 0,
390 output->base.current_mode->width,
391 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200392}
393
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100394#ifdef ENABLE_EGL
Jonas Ådahle5a12252013-04-05 23:07:11 +0200395static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396wayland_output_update_gl_border(struct wayland_output *output)
397{
398 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
399 cairo_t *cr;
400
401 if (!output->frame)
402 return;
403 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
404 return;
405
406 fwidth = frame_width(output->frame);
407 fheight = frame_height(output->frame);
408 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
409
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500410 if (!output->gl.border.top)
411 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500412 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
413 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500414 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500415 frame_repaint(output->frame, cr);
416 cairo_destroy(cr);
417 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
418 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500419 cairo_image_surface_get_stride(output->gl.border.top) / 4,
420 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500421
422
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500423 if (!output->gl.border.left)
424 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500425 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
426 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500427 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500428 cairo_translate(cr, 0, -iy);
429 frame_repaint(output->frame, cr);
430 cairo_destroy(cr);
431 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
432 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500433 cairo_image_surface_get_stride(output->gl.border.left) / 4,
434 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500435
436
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500437 if (!output->gl.border.right)
438 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500439 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
440 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500441 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500442 cairo_translate(cr, -(iwidth + ix), -iy);
443 frame_repaint(output->frame, cr);
444 cairo_destroy(cr);
445 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
446 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500447 cairo_image_surface_get_stride(output->gl.border.right) / 4,
448 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500449
450
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500451 if (!output->gl.border.bottom)
452 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500453 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
454 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500455 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500456 cairo_translate(cr, 0, -(iy + iheight));
457 frame_repaint(output->frame, cr);
458 cairo_destroy(cr);
459 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
460 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500461 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
462 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500463}
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100464#endif
Jason Ekstrand7744f712013-10-27 22:24:55 -0500465
466static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200467wayland_output_start_repaint_loop(struct weston_output *output_base)
468{
Armin Krezović938dc522016-08-01 19:17:57 +0200469 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300470 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200471 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200472
473 /* If this is the initial frame, we need to attach a buffer so that
474 * the compositor can map the surface and include it in its render
475 * loop. If the surface doesn't end up in the render loop, the frame
476 * callback won't be invoked. The buffer is transparent and of the
477 * same size as the future real output buffer. */
478 if (output->parent.draw_initial_frame) {
Armin Krezović2d321e32016-10-09 17:30:25 +0200479 output->parent.draw_initial_frame = false;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200480
481 draw_initial_frame(output);
482 }
483
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800484 output->frame_cb = wl_surface_frame(output->parent.surface);
485 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200486 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300487 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200488}
489
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100490#ifdef ENABLE_EGL
David Herrmann1edf44c2013-10-22 17:11:26 +0200491static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500492wayland_output_repaint_gl(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000493 pixman_region32_t *damage,
494 void *repaint_data)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400495{
Armin Krezović938dc522016-08-01 19:17:57 +0200496 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400497 struct weston_compositor *ec = output->base.compositor;
Scott Moreau062be7e2012-04-20 13:37:33 -0600498
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800499 output->frame_cb = wl_surface_frame(output->parent.surface);
500 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100501
Jason Ekstrand7744f712013-10-27 22:24:55 -0500502 wayland_output_update_gl_border(output);
503
Pekka Paalanenbc106382012-10-10 12:49:31 +0300504 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200505
506 pixman_region32_subtract(&ec->primary_plane.damage,
507 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200508 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509}
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100510#endif
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100511
Matt Roper361d2ad2011-08-29 13:52:23 -0700512static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500513wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
514{
515 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
516 cairo_t *cr;
517
518 if (!buffer->output->frame || !buffer->frame_damaged)
519 return;
520
521 cr = cairo_create(buffer->c_surface);
522
523 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
524 fwidth = frame_width(buffer->output->frame);
525 fheight = frame_height(buffer->output->frame);
526
527 /* Set the clip so we don't unnecisaraly damage the surface */
528 cairo_move_to(cr, ix, iy);
529 cairo_rel_line_to(cr, iwidth, 0);
530 cairo_rel_line_to(cr, 0, iheight);
531 cairo_rel_line_to(cr, -iwidth, 0);
532 cairo_line_to(cr, ix, iy);
533 cairo_line_to(cr, 0, iy);
534 cairo_line_to(cr, 0, fheight);
535 cairo_line_to(cr, fwidth, fheight);
536 cairo_line_to(cr, fwidth, 0);
537 cairo_line_to(cr, 0, 0);
538 cairo_line_to(cr, 0, iy);
539 cairo_close_path(cr);
540 cairo_clip(cr);
541
542 /* Draw using a pattern so that the final result gets clipped */
543 cairo_push_group(cr);
544 frame_repaint(buffer->output->frame, cr);
545 cairo_pop_group_to_source(cr);
546 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
547 cairo_paint(cr);
548
549 cairo_destroy(cr);
550}
551
552static void
553wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
554{
555 pixman_region32_t damage;
556 pixman_box32_t *rects;
557 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
558 int i, n;
559
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500560 pixman_region32_init(&damage);
561 weston_transformed_region(sb->output->base.width,
562 sb->output->base.height,
563 sb->output->base.transform,
564 sb->output->base.current_scale,
565 &sb->damage, &damage);
566
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500567 if (sb->output->frame) {
568 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
569 fwidth = frame_width(sb->output->frame);
570 fheight = frame_height(sb->output->frame);
571
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500572 pixman_region32_translate(&damage, ix, iy);
573
574 if (sb->frame_damaged) {
575 pixman_region32_union_rect(&damage, &damage,
576 0, 0, fwidth, iy);
577 pixman_region32_union_rect(&damage, &damage,
578 0, iy, ix, iheight);
579 pixman_region32_union_rect(&damage, &damage,
580 ix + iwidth, iy,
581 fwidth - (ix + iwidth), iheight);
582 pixman_region32_union_rect(&damage, &damage,
583 0, iy + iheight,
584 fwidth, fheight - (iy + iheight));
585 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500586 }
587
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500588 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500589 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
590 for (i = 0; i < n; ++i)
591 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
592 rects[i].y1, rects[i].x2 - rects[i].x1,
593 rects[i].y2 - rects[i].y1);
594
595 if (sb->output->frame)
596 pixman_region32_fini(&damage);
597}
598
599static int
600wayland_output_repaint_pixman(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000601 pixman_region32_t *damage,
602 void *repaint_data)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500603{
Armin Krezović938dc522016-08-01 19:17:57 +0200604 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300605 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200606 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500607 struct wayland_shm_buffer *sb;
608
609 if (output->frame) {
610 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
611 wl_list_for_each(sb, &output->shm.buffers, link)
612 sb->frame_damaged = 1;
613 }
614
615 wl_list_for_each(sb, &output->shm.buffers, link)
616 pixman_region32_union(&sb->damage, &sb->damage, damage);
617
618 sb = wayland_output_get_shm_buffer(output);
619
620 wayland_output_update_shm_border(sb);
621 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300622 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500623
624 wayland_shm_buffer_attach(sb);
625
Daniel Stone21fac602016-11-28 16:05:35 +0000626 output->frame_cb = wl_surface_frame(output->parent.surface);
627 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500628 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300629 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630
631 pixman_region32_fini(&sb->damage);
632 pixman_region32_init(&sb->damage);
633 sb->frame_damaged = 0;
634
Giulio Camuffo954f1832014-10-11 18:27:30 +0300635 pixman_region32_subtract(&b->compositor->primary_plane.damage,
636 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500637 return 0;
638}
639
640static void
Armin Krezović174448a2016-09-30 14:11:09 +0200641wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700642{
Armin Krezović34476192016-11-21 18:42:42 +0100643 if (output->parent.xdg_toplevel)
644 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
645
646 if (output->parent.xdg_surface)
647 zxdg_surface_v6_destroy(output->parent.xdg_surface);
648
Armin Krezović174448a2016-09-30 14:11:09 +0200649 if (output->parent.shell_surface)
650 wl_shell_surface_destroy(output->parent.shell_surface);
651
652 wl_surface_destroy(output->parent.surface);
653}
654
Armin Krezović2e662522016-10-09 17:30:29 +0200655static void
656wayland_output_destroy_shm_buffers(struct wayland_output *output)
657{
658 struct wayland_shm_buffer *buffer, *next;
659
660 /* Throw away any remaining SHM buffers */
661 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
662 wayland_shm_buffer_destroy(buffer);
663 /* These will get thrown away when they get released */
664 wl_list_for_each(buffer, &output->shm.buffers, link)
665 buffer->output = NULL;
666}
667
Armin Krezović174448a2016-09-30 14:11:09 +0200668static int
669wayland_output_disable(struct weston_output *base)
670{
671 struct wayland_output *output = to_wayland_output(base);
672 struct wayland_backend *b = to_wayland_backend(base->compositor);
673
674 if (!output->base.enabled)
675 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700676
Giulio Camuffo954f1832014-10-11 18:27:30 +0300677 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200678 pixman_renderer_output_destroy(&output->base);
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100679#ifdef ENABLE_EGL
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500680 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200681 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100682 wl_egl_window_destroy(output->gl.egl_window);
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100683#endif
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500684 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100685
Armin Krezović2e662522016-10-09 17:30:29 +0200686 wayland_output_destroy_shm_buffers(output);
687
Armin Krezovićf054d352016-10-09 17:30:27 +0200688 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500689
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600690 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500691 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600692
693 cairo_surface_destroy(output->gl.border.top);
694 cairo_surface_destroy(output->gl.border.left);
695 cairo_surface_destroy(output->gl.border.right);
696 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500697
Armin Krezović174448a2016-09-30 14:11:09 +0200698 return 0;
699}
Matt Roper361d2ad2011-08-29 13:52:23 -0700700
Armin Krezović174448a2016-09-30 14:11:09 +0200701static void
702wayland_output_destroy(struct weston_output *base)
703{
704 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +0200705
706 wayland_output_disable(&output->base);
707
Pekka Paalanenae6d35d2017-08-16 12:07:14 +0300708 weston_output_release(&output->base);
Armin Krezović174448a2016-09-30 14:11:09 +0200709
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800710 if (output->frame_cb)
711 wl_callback_destroy(output->frame_cb);
712
Sergi Granell2dcbb8d2017-03-24 20:48:01 +0100713 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +0200714 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700715}
716
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300717static const struct wl_shell_surface_listener shell_surface_listener;
718
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100719#ifdef ENABLE_EGL
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200720static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500721wayland_output_init_gl_renderer(struct wayland_output *output)
722{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500723 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500724
725 if (output->frame) {
726 fwidth = frame_width(output->frame);
727 fheight = frame_height(output->frame);
728 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500729 fwidth = output->base.current_mode->width;
730 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500731 }
732
733 output->gl.egl_window =
734 wl_egl_window_create(output->parent.surface,
735 fwidth, fheight);
736 if (!output->gl.egl_window) {
737 weston_log("failure to create wl_egl_window\n");
738 return -1;
739 }
740
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200741 if (gl_renderer->output_window_create(&output->base,
742 output->gl.egl_window,
743 output->gl.egl_window,
744 gl_renderer->alpha_attribs,
745 NULL,
746 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500747 goto cleanup_window;
748
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500749 return 0;
750
751cleanup_window:
752 wl_egl_window_destroy(output->gl.egl_window);
753 return -1;
754}
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100755#endif
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500756
757static int
758wayland_output_init_pixman_renderer(struct wayland_output *output)
759{
760 return pixman_renderer_output_create(&output->base);
761}
762
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600763static void
764wayland_output_resize_surface(struct wayland_output *output)
765{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300766 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200767 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600768 int32_t ix, iy, iwidth, iheight;
769 int32_t width, height;
770 struct wl_region *region;
771
772 width = output->base.current_mode->width;
773 height = output->base.current_mode->height;
774
775 if (output->frame) {
776 frame_resize_inside(output->frame, width, height);
777
778 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300779 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600780 wl_region_add(region, ix, iy, iwidth, iheight);
781 wl_surface_set_input_region(output->parent.surface, region);
782 wl_region_destroy(region);
783
Sergi Granelled016bf2017-03-24 23:45:13 +0100784 if (output->parent.xdg_surface) {
785 zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
786 ix,
787 iy,
788 iwidth,
789 iheight);
790 }
791
Sergi Granellcafc6522017-09-25 11:57:37 +0200792 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
793 region = wl_compositor_create_region(b->parent.compositor);
794 wl_region_add(region, ix, iy, iwidth, iheight);
795 wl_surface_set_opaque_region(output->parent.surface, region);
796 wl_region_destroy(region);
797
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600798 width = frame_width(output->frame);
799 height = frame_height(output->frame);
800 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300801 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600802 wl_region_add(region, 0, 0, width, height);
803 wl_surface_set_input_region(output->parent.surface, region);
804 wl_region_destroy(region);
805
Giulio Camuffo954f1832014-10-11 18:27:30 +0300806 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600807 wl_region_add(region, 0, 0, width, height);
808 wl_surface_set_opaque_region(output->parent.surface, region);
809 wl_region_destroy(region);
Sergi Granelled016bf2017-03-24 23:45:13 +0100810
811 if (output->parent.xdg_surface) {
812 zxdg_surface_v6_set_window_geometry(output->parent.xdg_surface,
813 0,
814 0,
815 width,
816 height);
817 }
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600818 }
819
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100820#ifdef ENABLE_EGL
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600821 if (output->gl.egl_window) {
822 wl_egl_window_resize(output->gl.egl_window,
823 width, height, 0, 0);
824
825 /* These will need to be re-created due to the resize */
826 gl_renderer->output_set_border(&output->base,
827 GL_RENDERER_BORDER_TOP,
828 0, 0, 0, NULL);
829 cairo_surface_destroy(output->gl.border.top);
830 output->gl.border.top = NULL;
831 gl_renderer->output_set_border(&output->base,
832 GL_RENDERER_BORDER_LEFT,
833 0, 0, 0, NULL);
834 cairo_surface_destroy(output->gl.border.left);
835 output->gl.border.left = NULL;
836 gl_renderer->output_set_border(&output->base,
837 GL_RENDERER_BORDER_RIGHT,
838 0, 0, 0, NULL);
839 cairo_surface_destroy(output->gl.border.right);
840 output->gl.border.right = NULL;
841 gl_renderer->output_set_border(&output->base,
842 GL_RENDERER_BORDER_BOTTOM,
843 0, 0, 0, NULL);
844 cairo_surface_destroy(output->gl.border.bottom);
845 output->gl.border.bottom = NULL;
846 }
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100847#endif
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600848
Armin Krezović2e662522016-10-09 17:30:29 +0200849 wayland_output_destroy_shm_buffers(output);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600850}
851
852static int
853wayland_output_set_windowed(struct wayland_output *output)
854{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300855 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200856 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600857
858 if (output->frame)
859 return 0;
860
Giulio Camuffo954f1832014-10-11 18:27:30 +0300861 if (!b->theme) {
862 b->theme = theme_create();
Sergi Granell2dcbb8d2017-03-24 20:48:01 +0100863 if (!b->theme)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600864 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600865 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300866 output->frame = frame_create(b->theme, 100, 100,
Sergi Granell2dcbb8d2017-03-24 20:48:01 +0100867 FRAME_BUTTON_CLOSE, output->title);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600868 if (!output->frame)
869 return -1;
870
871 if (output->keyboard_count)
872 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
873
874 wayland_output_resize_surface(output);
875
Pekka Paalanen7d2fc922017-10-18 12:18:39 +0300876 if (output->parent.xdg_toplevel) {
877 zxdg_toplevel_v6_unset_fullscreen(output->parent.xdg_toplevel);
878 } else if (output->parent.shell_surface) {
Armin Krezović34476192016-11-21 18:42:42 +0100879 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Pekka Paalanen7d2fc922017-10-18 12:18:39 +0300880 }
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600881
882 return 0;
883}
884
885static void
886wayland_output_set_fullscreen(struct wayland_output *output,
887 enum wl_shell_surface_fullscreen_method method,
888 uint32_t framerate, struct wl_output *target)
889{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300890 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200891 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500892
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600893 if (output->frame) {
894 frame_destroy(output->frame);
895 output->frame = NULL;
896 }
897
898 wayland_output_resize_surface(output);
899
Armin Krezović34476192016-11-21 18:42:42 +0100900 if (output->parent.xdg_toplevel) {
901 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
902 } else if (output->parent.shell_surface) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500903 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
904 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300905 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800906 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
907 output->parent.surface,
908 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500909 }
910}
911
912static struct weston_mode *
913wayland_output_choose_mode(struct wayland_output *output,
914 struct weston_mode *ref_mode)
915{
916 struct weston_mode *mode;
917
918 /* First look for an exact match */
919 wl_list_for_each(mode, &output->base.mode_list, link)
920 if (mode->width == ref_mode->width &&
921 mode->height == ref_mode->height &&
922 mode->refresh == ref_mode->refresh)
923 return mode;
924
925 /* If we can't find an exact match, ignore refresh and try again */
926 wl_list_for_each(mode, &output->base.mode_list, link)
927 if (mode->width == ref_mode->width &&
928 mode->height == ref_mode->height)
929 return mode;
930
931 /* Yeah, we failed */
932 return NULL;
933}
934
935enum mode_status {
936 MODE_STATUS_UNKNOWN,
937 MODE_STATUS_SUCCESS,
938 MODE_STATUS_FAIL,
939 MODE_STATUS_CANCEL,
940};
941
942static void
943mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800944 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500945{
946 enum mode_status *value = data;
947
948 printf("Mode switch successful\n");
949
950 *value = MODE_STATUS_SUCCESS;
951}
952
953static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800954mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500955{
956 enum mode_status *value = data;
957
958 printf("Mode switch failed\n");
959
960 *value = MODE_STATUS_FAIL;
961}
962
963static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800964mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500965{
966 enum mode_status *value = data;
967
968 printf("Mode switch cancelled\n");
969
970 *value = MODE_STATUS_CANCEL;
971}
972
Jonas Ådahl496adb32015-11-17 16:00:27 +0800973struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500974 mode_feedback_successful,
975 mode_feedback_failed,
976 mode_feedback_cancelled,
977};
978
Armin Krezović71ebc052017-02-07 21:03:40 +0100979static enum mode_status
980wayland_output_fullscreen_shell_mode_feedback(struct wayland_output *output,
981 struct weston_mode *mode)
982{
983 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
984 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
985 enum mode_status mode_status;
986 int ret = 0;
987
988 mode_feedback =
989 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
990 output->parent.surface,
991 output->parent.output,
992 mode->refresh);
993
994 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
995 &mode_feedback_listener,
996 &mode_status);
997
998 output->parent.draw_initial_frame = false;
999 draw_initial_frame(output);
1000 wl_surface_commit(output->parent.surface);
1001
1002 mode_status = MODE_STATUS_UNKNOWN;
1003 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
1004 ret = wl_display_dispatch(b->parent.wl_display);
1005
1006 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
1007
1008 return mode_status;
1009}
1010
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001011static int
1012wayland_output_switch_mode(struct weston_output *output_base,
1013 struct weston_mode *mode)
1014{
Armin Krezović938dc522016-08-01 19:17:57 +02001015 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001016 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001017 struct wl_surface *old_surface;
1018 struct weston_mode *old_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001019 enum mode_status mode_status;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001020
1021 if (output_base == NULL) {
1022 weston_log("output is NULL.\n");
1023 return -1;
1024 }
1025
1026 if (mode == NULL) {
1027 weston_log("mode is NULL.\n");
1028 return -1;
1029 }
1030
Armin Krezović938dc522016-08-01 19:17:57 +02001031 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001032
Armin Krezović34476192016-11-21 18:42:42 +01001033 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001034 return -1;
1035
1036 mode = wayland_output_choose_mode(output, mode);
1037 if (mode == NULL)
1038 return -1;
1039
1040 if (output->base.current_mode == mode)
1041 return 0;
1042
1043 old_mode = output->base.current_mode;
1044 old_surface = output->parent.surface;
1045 output->base.current_mode = mode;
1046 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001047 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001048 wl_surface_set_user_data(output->parent.surface, output);
1049
1050 /* Blow the old buffers because we changed size/surfaces */
1051 wayland_output_resize_surface(output);
1052
Armin Krezović71ebc052017-02-07 21:03:40 +01001053 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, mode);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001054
1055 /* This should kick-start things again */
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001056 wayland_output_start_repaint_loop(&output->base);
1057
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001058 if (mode_status == MODE_STATUS_FAIL) {
1059 output->base.current_mode = old_mode;
1060 wl_surface_destroy(output->parent.surface);
1061 output->parent.surface = old_surface;
1062 wayland_output_resize_surface(output);
1063
1064 return -1;
1065 }
1066
1067 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1068 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1069
Giulio Camuffo954f1832014-10-11 18:27:30 +03001070 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001071 pixman_renderer_output_destroy(output_base);
1072 if (wayland_output_init_pixman_renderer(output) < 0)
1073 goto err_output;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001074#ifdef ENABLE_EGL
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001075 } else {
1076 gl_renderer->output_destroy(output_base);
1077 wl_egl_window_destroy(output->gl.egl_window);
1078 if (wayland_output_init_gl_renderer(output) < 0)
1079 goto err_output;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001080#endif
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001081 }
1082 wl_surface_destroy(old_surface);
1083
1084 weston_output_schedule_repaint(&output->base);
1085
1086 return 0;
1087
1088err_output:
1089 /* XXX */
1090 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001091}
1092
Armin Krezović34476192016-11-21 18:42:42 +01001093static void
1094handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1095 uint32_t serial)
1096{
1097 zxdg_surface_v6_ack_configure(surface, serial);
1098}
1099
1100static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1101 handle_xdg_surface_configure
1102};
1103
1104static void
1105handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1106 int32_t width, int32_t height,
1107 struct wl_array *states)
1108{
1109 struct wayland_output *output = data;
1110
1111 output->parent.configure_width = width;
1112 output->parent.configure_height = height;
1113
1114 output->parent.wait_for_configure = false;
1115 /* FIXME: implement resizing */
1116}
1117
1118static void
1119handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1120{
Sergi Granelleaa73582017-03-25 17:19:36 +01001121 struct wayland_output *output = data;
Sergi Granellb4e239f2017-09-27 16:06:37 +02001122 struct weston_compositor *compositor = output->base.compositor;
Sergi Granelleaa73582017-03-25 17:19:36 +01001123
Sergi Granellb4e239f2017-09-27 16:06:37 +02001124 wayland_output_destroy(&output->base);
1125
1126 if (wl_list_empty(&compositor->output_list))
1127 weston_compositor_exit(compositor);
Armin Krezović34476192016-11-21 18:42:42 +01001128}
1129
1130static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1131 handle_xdg_toplevel_configure,
1132 handle_xdg_toplevel_close,
1133};
1134
Armin Krezović174448a2016-09-30 14:11:09 +02001135static int
1136wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001137{
Armin Krezović174448a2016-09-30 14:11:09 +02001138 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001139
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001140 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001141 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001142 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001143 return -1;
1144
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001145 wl_surface_set_user_data(output->parent.surface, output);
1146
Armin Krezović2d321e32016-10-09 17:30:25 +02001147 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001148
Armin Krezović34476192016-11-21 18:42:42 +01001149 if (b->parent.xdg_shell) {
1150 output->parent.xdg_surface =
1151 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1152 output->parent.surface);
1153 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1154 &xdg_surface_listener, output);
1155
1156 output->parent.xdg_toplevel =
1157 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1158 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1159 &xdg_toplevel_listener, output);
1160
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001161 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, output->title);
1162
Armin Krezović34476192016-11-21 18:42:42 +01001163 wl_surface_commit(output->parent.surface);
1164
1165 output->parent.wait_for_configure = true;
1166
1167 while (output->parent.wait_for_configure)
1168 wl_display_dispatch(b->parent.wl_display);
1169
1170 weston_log("wayland-backend: Using xdg_shell_v6\n");
1171 }
1172 else if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001173 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001174 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001175 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001176 if (!output->parent.shell_surface) {
1177 wl_surface_destroy(output->parent.surface);
1178 return -1;
1179 }
1180
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001181 wl_shell_surface_add_listener(output->parent.shell_surface,
1182 &shell_surface_listener, output);
Armin Krezović34476192016-11-21 18:42:42 +01001183
1184 weston_log("wayland-backend: Using wl_shell\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001185 }
1186
Armin Krezović174448a2016-09-30 14:11:09 +02001187 return 0;
1188}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001189
Armin Krezović174448a2016-09-30 14:11:09 +02001190static int
1191wayland_output_enable(struct weston_output *base)
1192{
1193 struct wayland_output *output = to_wayland_output(base);
1194 struct wayland_backend *b = to_wayland_backend(base->compositor);
Armin Krezović71ebc052017-02-07 21:03:40 +01001195 enum mode_status mode_status;
Armin Krezović174448a2016-09-30 14:11:09 +02001196 int ret = 0;
1197
Armin Krezović174448a2016-09-30 14:11:09 +02001198 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1199 output->base.current_mode->width,
1200 output->base.current_mode->height,
1201 output->base.x, output->base.y);
1202
Armin Krezovićf054d352016-10-09 17:30:27 +02001203 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001204 ret = wayland_backend_create_output_surface(output);
1205
1206 if (ret < 0)
1207 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001208
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001209 wl_list_init(&output->shm.buffers);
1210 wl_list_init(&output->shm.free_buffers);
1211
Giulio Camuffo954f1832014-10-11 18:27:30 +03001212 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001213 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001214 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001215
1216 output->base.repaint = wayland_output_repaint_pixman;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001217#ifdef ENABLE_EGL
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001218 } else {
1219 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001220 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001221
1222 output->base.repaint = wayland_output_repaint_gl;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001223#endif
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001224 }
1225
Armin Krezovićf16de172016-10-09 17:30:26 +02001226 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1227 output->base.assign_planes = NULL;
1228 output->base.set_backlight = NULL;
1229 output->base.set_dpms = NULL;
1230 output->base.switch_mode = wayland_output_switch_mode;
1231
Armin Krezović174448a2016-09-30 14:11:09 +02001232 if (b->sprawl_across_outputs) {
Armin Krezović71ebc052017-02-07 21:03:40 +01001233 if (b->parent.fshell) {
1234 wayland_output_resize_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001235
Armin Krezović71ebc052017-02-07 21:03:40 +01001236 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, &output->mode);
1237
1238 if (mode_status == MODE_STATUS_FAIL) {
1239 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1240 output->parent.surface,
1241 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1242 output->parent.output);
1243
1244 output->parent.draw_initial_frame = true;
1245 }
1246 } else {
1247 wayland_output_set_fullscreen(output,
1248 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1249 output->mode.refresh, output->parent.output);
Armin Krezović174448a2016-09-30 14:11:09 +02001250 }
1251 } else if (b->fullscreen) {
1252 wayland_output_set_fullscreen(output, 0, 0, NULL);
1253 } else {
1254 wayland_output_set_windowed(output);
1255 }
1256
1257 return 0;
1258
1259err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001260 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001261
1262 return -1;
1263}
1264
1265static struct wayland_output *
Pekka Paalanen1580be62017-08-11 16:05:41 +03001266wayland_output_create_common(struct weston_compositor *compositor,
1267 const char *name)
Armin Krezović174448a2016-09-30 14:11:09 +02001268{
1269 struct wayland_output *output;
Pekka Paalanenffa42ff2017-08-11 15:55:32 +03001270 char *title;
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001271
1272 /* name can't be NULL. */
1273 assert(name);
Armin Krezović174448a2016-09-30 14:11:09 +02001274
1275 output = zalloc(sizeof *output);
1276 if (output == NULL) {
1277 perror("zalloc");
1278 return NULL;
1279 }
1280
Pekka Paalanenffa42ff2017-08-11 15:55:32 +03001281 if (asprintf(&title, "%s - %s", WINDOW_TITLE, name) < 0) {
1282 free(output);
1283 return NULL;
1284 }
1285 output->title = title;
1286
Pekka Paalanen1580be62017-08-11 16:05:41 +03001287 weston_output_init(&output->base, compositor, name);
1288
Matt Roper361d2ad2011-08-29 13:52:23 -07001289 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001290 output->base.disable = wayland_output_disable;
1291 output->base.enable = wayland_output_enable;
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001292
Armin Krezović174448a2016-09-30 14:11:09 +02001293 return output;
1294}
1295
1296static int
1297wayland_output_create(struct weston_compositor *compositor, const char *name)
1298{
Pekka Paalanen1580be62017-08-11 16:05:41 +03001299 struct wayland_output *output;
Armin Krezović174448a2016-09-30 14:11:09 +02001300
Pekka Paalanen1580be62017-08-11 16:05:41 +03001301 output = wayland_output_create_common(compositor, name);
Sergi Granell7fecb432017-03-24 20:48:02 +01001302 if (!output)
1303 return -1;
1304
Armin Krezović174448a2016-09-30 14:11:09 +02001305 weston_compositor_add_pending_output(&output->base, compositor);
1306
1307 return 0;
1308}
1309
1310static int
1311wayland_output_set_size(struct weston_output *base, int width, int height)
1312{
1313 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001314 int output_width, output_height;
1315
1316 /* We can only be called once. */
1317 assert(!output->base.current_mode);
1318
1319 /* Make sure we have scale set. */
1320 assert(output->base.scale);
1321
1322 if (width < 1) {
1323 weston_log("Invalid width \"%d\" for output %s\n",
1324 width, output->base.name);
1325 return -1;
1326 }
1327
1328 if (height < 1) {
1329 weston_log("Invalid height \"%d\" for output %s\n",
1330 height, output->base.name);
1331 return -1;
1332 }
1333
1334 output_width = width * output->base.scale;
1335 output_height = height * output->base.scale;
1336
1337 output->mode.flags =
1338 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1339
1340 output->mode.width = output_width;
1341 output->mode.height = output_height;
1342 output->mode.refresh = 60000;
Armin Krezović174448a2016-09-30 14:11:09 +02001343 wl_list_insert(&output->base.mode_list, &output->mode.link);
1344
1345 output->base.current_mode = &output->mode;
1346 output->base.make = "wayland";
1347 output->base.model = "none";
1348
1349 /* XXX: Calculate proper size. */
1350 output->base.mm_width = width;
1351 output->base.mm_height = height;
1352
Armin Krezović174448a2016-09-30 14:11:09 +02001353 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001354}
1355
Armin Krezović174448a2016-09-30 14:11:09 +02001356static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001357wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001358 struct wayland_parent_output *poutput)
1359{
Sergi Granell7fecb432017-03-24 20:48:02 +01001360 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001361 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001362
Pekka Paalanen1580be62017-08-11 16:05:41 +03001363 output = wayland_output_create_common(b->compositor, "wlparent");
Sergi Granell7fecb432017-03-24 20:48:02 +01001364 if (!output)
1365 return -1;
1366
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001367 if (poutput->current_mode) {
1368 mode = poutput->current_mode;
1369 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001370 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001371 } else if (!wl_list_empty(&poutput->mode_list)) {
1372 mode = container_of(poutput->mode_list.next,
1373 struct weston_mode, link);
1374 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001375 weston_log("No valid modes found. Skipping output.\n");
1376 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001377 }
1378
Armin Krezović174448a2016-09-30 14:11:09 +02001379 output->base.scale = 1;
1380 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001381
1382 output->parent.output = poutput->global;
1383
1384 output->base.make = poutput->physical.make;
1385 output->base.model = poutput->physical.model;
Pekka Paalanen4dab5832017-10-19 13:19:50 +03001386 output->base.mm_width = poutput->physical.width;
1387 output->base.mm_height = poutput->physical.height;
Armin Krezović174448a2016-09-30 14:11:09 +02001388
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001389 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1390 wl_list_init(&poutput->mode_list);
1391
Pekka Paalanen4dab5832017-10-19 13:19:50 +03001392 /* No other mode should have CURRENT already. */
1393 mode->flags |= WL_OUTPUT_MODE_CURRENT;
1394 output->base.current_mode = mode;
1395
1396 /* output->mode is unused in this path. */
1397
Armin Krezović174448a2016-09-30 14:11:09 +02001398 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001399
Armin Krezović174448a2016-09-30 14:11:09 +02001400 return 0;
1401
1402out:
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001403 weston_output_release(&output->base);
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001404 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +02001405 free(output);
1406
1407 return -1;
1408}
1409
1410static int
1411wayland_output_create_fullscreen(struct wayland_backend *b)
1412{
Sergi Granell7fecb432017-03-24 20:48:02 +01001413 struct wayland_output *output;
Armin Krezović174448a2016-09-30 14:11:09 +02001414 int width = 0, height = 0;
1415
Pekka Paalanen1580be62017-08-11 16:05:41 +03001416 output = wayland_output_create_common(b->compositor, "wayland-fullscreen");
Sergi Granell7fecb432017-03-24 20:48:02 +01001417 if (!output)
1418 return -1;
1419
Armin Krezović174448a2016-09-30 14:11:09 +02001420 output->base.scale = 1;
1421 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1422
1423 if (wayland_backend_create_output_surface(output) < 0)
1424 goto err_surface;
1425
1426 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001427 if (b->parent.xdg_shell || b->parent.shell) {
1428 if (output->parent.xdg_toplevel)
1429 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1430 output->parent.output);
1431 else if (output->parent.shell_surface)
1432 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1433 0, 0, NULL);
1434
Armin Krezović174448a2016-09-30 14:11:09 +02001435 wl_display_roundtrip(b->parent.wl_display);
1436
1437 width = output->parent.configure_width;
1438 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001439 }
1440
Armin Krezović174448a2016-09-30 14:11:09 +02001441 if (wayland_output_set_size(&output->base, width, height) < 0)
1442 goto err_set_size;
1443
1444 weston_compositor_add_pending_output(&output->base, b->compositor);
1445
1446 return 0;
1447
1448err_set_size:
1449 wayland_backend_destroy_output_surface(output);
1450err_surface:
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001451 weston_output_release(&output->base);
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001452 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +02001453 free(output);
1454
1455 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001456}
1457
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001458static void
1459shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1460 uint32_t serial)
1461{
1462 wl_shell_surface_pong(shell_surface, serial);
1463}
1464
1465static void
1466shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1467 uint32_t edges, int32_t width, int32_t height)
1468{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001469 struct wayland_output *output = data;
1470
1471 output->parent.configure_width = width;
1472 output->parent.configure_height = height;
1473
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001474 /* FIXME: implement resizing */
1475}
1476
1477static void
1478shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1479{
1480}
1481
1482static const struct wl_shell_surface_listener shell_surface_listener = {
1483 shell_surface_ping,
1484 shell_surface_configure,
1485 shell_surface_popup_done
1486};
1487
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001488/* Events received from the wayland-server this compositor is client of: */
1489
Jason Ekstrand7744f712013-10-27 22:24:55 -05001490/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001491static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001492input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001493{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001494
Jason Ekstrand7744f712013-10-27 22:24:55 -05001495 struct wl_buffer *buffer;
1496 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001497
Giulio Camuffo954f1832014-10-11 18:27:30 +03001498 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001499 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001500
Giulio Camuffo954f1832014-10-11 18:27:30 +03001501 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001502 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001503 if (!buffer)
1504 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001505
1506 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1507 input->parent.cursor.surface,
1508 image->hotspot_x, image->hotspot_y);
1509
1510 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1511 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1512 image->width, image->height);
1513 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001514}
1515
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001516static void
Daniel Stone37816df2012-05-16 18:45:18 +01001517input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1518 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001519 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001520{
1521 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001522 int32_t fx, fy;
1523 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001524 double x, y;
1525
1526 x = wl_fixed_to_double(fixed_x);
1527 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001528
Daniel Stone50692802012-06-22 13:21:41 +01001529 /* XXX: If we get a modifier event immediately before the focus,
1530 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001531 input->enter_serial = serial;
1532 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001533
1534 if (input->output->frame) {
1535 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001536 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001537 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001538 x -= fx;
1539 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001540
1541 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1542 weston_output_schedule_repaint(&input->output->base);
1543 } else {
1544 location = THEME_LOCATION_CLIENT_AREA;
1545 }
1546
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001547 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001548
1549 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001550 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001551 notify_pointer_focus(&input->base, &input->output->base, x, y);
1552 wl_pointer_set_cursor(input->parent.pointer,
1553 input->enter_serial, NULL, 0, 0);
1554 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001555 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001556 notify_pointer_focus(&input->base, NULL, 0, 0);
1557 input_set_cursor(input);
1558 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001559}
1560
1561static void
Daniel Stone37816df2012-05-16 18:45:18 +01001562input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1563 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001564{
1565 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001566
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001567 if (!input->output)
1568 return;
1569
Jason Ekstrand7744f712013-10-27 22:24:55 -05001570 if (input->output->frame) {
1571 frame_pointer_leave(input->output->frame, input);
1572
1573 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1574 weston_output_schedule_repaint(&input->output->base);
1575 }
1576
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001577 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001578 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001579 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001580}
1581
1582static void
Daniel Stone37816df2012-05-16 18:45:18 +01001583input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001584 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001585{
1586 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001587 int32_t fx, fy;
1588 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001589 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001590 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001591
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001592 if (!input->output)
1593 return;
1594
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001595 x = wl_fixed_to_double(fixed_x);
1596 y = wl_fixed_to_double(fixed_y);
1597
Jason Ekstrand7744f712013-10-27 22:24:55 -05001598 if (input->output->frame) {
1599 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001600 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001601 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001602 x -= fx;
1603 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001604
1605 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1606 weston_output_schedule_repaint(&input->output->base);
1607 } else {
1608 location = THEME_LOCATION_CLIENT_AREA;
1609 }
1610
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001611 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001612
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001613 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001614 input_set_cursor(input);
1615 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001616 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001617 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001618 } else if (!input->has_focus &&
1619 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001620 wl_pointer_set_cursor(input->parent.pointer,
1621 input->enter_serial, NULL, 0, 0);
1622 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001623 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001624 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001625 }
1626
Peter Hutterer87743e92016-01-18 16:38:22 +10001627 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001628 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001629 want_frame = true;
1630 }
1631
1632 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1633 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001634}
1635
1636static void
1637input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001638 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001639 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001640{
1641 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001642 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001643
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001644 if (!input->output)
1645 return;
1646
Jason Ekstrand7744f712013-10-27 22:24:55 -05001647 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001648 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001649 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001650
1651 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001652 if (input->output->parent.xdg_toplevel)
1653 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001654 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001655 else if (input->output->parent.shell_surface)
1656 wl_shell_surface_move(input->output->parent.shell_surface,
1657 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001658 frame_status_clear(input->output->frame,
1659 FRAME_STATUS_MOVE);
1660 return;
1661 }
1662
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001663 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1664 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001665 input->output = NULL;
1666 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001667
Giulio Camuffo954f1832014-10-11 18:27:30 +03001668 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001669 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001670
1671 return;
1672 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001673
1674 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1675 weston_output_schedule_repaint(&input->output->base);
1676 } else {
1677 location = THEME_LOCATION_CLIENT_AREA;
1678 }
1679
Peter Hutterer87743e92016-01-18 16:38:22 +10001680 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001681 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001682 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1683 notify_pointer_frame(&input->base);
1684 }
Daniel Stone37816df2012-05-16 18:45:18 +01001685}
1686
1687static void
1688input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001689 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001690{
1691 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001692 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001693
Peter Hutterer89b6a492016-01-18 15:58:17 +10001694 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001695 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001696
Peter Hutterer87743e92016-01-18 16:38:22 +10001697 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1698 input->vert.has_discrete) {
1699 weston_event.has_discrete = true;
1700 weston_event.discrete = input->vert.discrete;
1701 input->vert.has_discrete = false;
1702 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1703 input->horiz.has_discrete) {
1704 weston_event.has_discrete = true;
1705 weston_event.discrete = input->horiz.discrete;
1706 input->horiz.has_discrete = false;
1707 }
1708
Peter Hutterer89b6a492016-01-18 15:58:17 +10001709 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001710
1711 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1712 notify_pointer_frame(&input->base);
1713}
1714
1715static void
1716input_handle_frame(void *data, struct wl_pointer *pointer)
1717{
1718 struct wayland_input *input = data;
1719
1720 notify_pointer_frame(&input->base);
1721}
1722
1723static void
1724input_handle_axis_source(void *data, struct wl_pointer *pointer,
1725 uint32_t source)
1726{
1727 struct wayland_input *input = data;
1728
1729 notify_axis_source(&input->base, source);
1730}
1731
1732static void
1733input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1734 uint32_t time, uint32_t axis)
1735{
1736 struct wayland_input *input = data;
1737 struct weston_pointer_axis_event weston_event;
1738
1739 weston_event.axis = axis;
1740 weston_event.value = 0;
1741
1742 notify_axis(&input->base, time, &weston_event);
1743}
1744
1745static void
1746input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1747 uint32_t axis, int32_t discrete)
1748{
1749 struct wayland_input *input = data;
1750
1751 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1752 input->vert.has_discrete = true;
1753 input->vert.discrete = discrete;
1754 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1755 input->horiz.has_discrete = true;
1756 input->horiz.discrete = discrete;
1757 }
Daniel Stone37816df2012-05-16 18:45:18 +01001758}
1759
1760static const struct wl_pointer_listener pointer_listener = {
1761 input_handle_pointer_enter,
1762 input_handle_pointer_leave,
1763 input_handle_motion,
1764 input_handle_button,
1765 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001766 input_handle_frame,
1767 input_handle_axis_source,
1768 input_handle_axis_stop,
1769 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001770};
1771
1772static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001773input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1774 int fd, uint32_t size)
1775{
1776 struct wayland_input *input = data;
1777 struct xkb_keymap *keymap;
1778 char *map_str;
1779
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001780 if (!data) {
1781 close(fd);
1782 return;
1783 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001784
1785 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1786 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1787 if (map_str == MAP_FAILED) {
1788 weston_log("mmap failed: %m\n");
1789 goto error;
1790 }
1791
Giulio Camuffo954f1832014-10-11 18:27:30 +03001792 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001793 map_str,
1794 XKB_KEYMAP_FORMAT_TEXT_V1,
1795 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001796 munmap(map_str, size);
1797
1798 if (!keymap) {
1799 weston_log("failed to compile keymap\n");
1800 goto error;
1801 }
1802
1803 input->keyboard_state_update = STATE_UPDATE_NONE;
1804 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1805 weston_log("No keymap provided; falling back to defalt\n");
1806 keymap = NULL;
1807 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1808 } else {
1809 weston_log("Invalid keymap\n");
1810 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001811 }
1812
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001813 close(fd);
1814
Derek Foreman1281a362015-07-31 16:55:32 -05001815 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001816 weston_seat_update_keymap(&input->base, keymap);
1817 else
1818 weston_seat_init_keyboard(&input->base, keymap);
1819
Ran Benitac9c74152014-08-19 23:59:52 +03001820 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001821
1822 return;
1823
1824error:
1825 wl_keyboard_release(input->parent.keyboard);
1826 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001827}
1828
1829static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001830input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001831 struct wl_keyboard *keyboard,
1832 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001833 struct wl_surface *surface,
1834 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001835{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001836 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001837 struct wayland_output *focus;
1838
1839 focus = input->keyboard_focus;
1840 if (focus) {
1841 /* This shouldn't happen */
1842 focus->keyboard_count--;
1843 if (!focus->keyboard_count && focus->frame)
1844 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1845 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1846 weston_output_schedule_repaint(&focus->base);
1847 }
1848
1849 input->keyboard_focus = wl_surface_get_user_data(surface);
1850 input->keyboard_focus->keyboard_count++;
1851
1852 focus = input->keyboard_focus;
1853 if (focus->frame) {
1854 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1855 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1856 weston_output_schedule_repaint(&focus->base);
1857 }
1858
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001859
Daniel Stone50692802012-06-22 13:21:41 +01001860 /* XXX: If we get a modifier event immediately before the focus,
1861 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001862 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001863 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001864}
1865
1866static void
1867input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001868 struct wl_keyboard *keyboard,
1869 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001870 struct wl_surface *surface)
1871{
1872 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001873 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001874
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001875 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001876
1877 focus = input->keyboard_focus;
1878 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001879 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001880
1881 focus->keyboard_count--;
1882 if (!focus->keyboard_count && focus->frame) {
1883 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1884 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1885 weston_output_schedule_repaint(&focus->base);
1886 }
1887
1888 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001889}
1890
Daniel Stone37816df2012-05-16 18:45:18 +01001891static void
1892input_handle_key(void *data, struct wl_keyboard *keyboard,
1893 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1894{
1895 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001896
Daniel Stone50692802012-06-22 13:21:41 +01001897 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001898 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001899 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001900 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001901 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001902}
1903
Daniel Stone351eb612012-05-31 15:27:47 -04001904static void
Derek Foreman1281a362015-07-31 16:55:32 -05001905input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001906 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001907 uint32_t mods_latched, uint32_t mods_locked,
1908 uint32_t group)
1909{
Derek Foreman1281a362015-07-31 16:55:32 -05001910 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001911 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001912 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001913 uint32_t serial_out;
1914
1915 /* If we get a key event followed by a modifier event with the
1916 * same serial number, then we try to preserve those semantics by
1917 * reusing the same serial number on the way out too. */
1918 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001919 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001920 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001921 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001922
Derek Foreman1281a362015-07-31 16:55:32 -05001923 keyboard = weston_seat_get_keyboard(&input->base);
1924 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001925 mods_depressed, mods_latched,
1926 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001927 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001928}
1929
Jonny Lamb497994a2014-08-12 14:58:26 +02001930static void
1931input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1932 int32_t rate, int32_t delay)
1933{
1934 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001935 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001936
Giulio Camuffo954f1832014-10-11 18:27:30 +03001937 b->compositor->kb_repeat_rate = rate;
1938 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001939}
1940
Daniel Stone37816df2012-05-16 18:45:18 +01001941static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001942 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001943 input_handle_keyboard_enter,
1944 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001945 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001946 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001947 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001948};
1949
1950static void
Derek Foreman748c6952015-11-06 15:56:10 -06001951input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1952 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001953 struct wl_surface *surface, int32_t id,
1954 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001955{
1956 struct wayland_input *input = data;
1957 struct wayland_output *output;
1958 enum theme_location location;
1959 bool first_touch;
1960 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001961 double x, y;
1962
1963 x = wl_fixed_to_double(fixed_x);
1964 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001965
1966 first_touch = (input->touch_points == 0);
1967 input->touch_points++;
1968
1969 input->touch_focus = wl_surface_get_user_data(surface);
1970 output = input->touch_focus;
1971 if (!first_touch && !input->touch_active)
1972 return;
1973
1974 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001975 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001976
1977 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001978 x -= fx;
1979 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001980
1981 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1982 weston_output_schedule_repaint(&output->base);
1983
1984 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1985 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001986 if (output->parent.xdg_toplevel)
1987 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1988 input->parent.seat, serial);
1989 else if (output->parent.shell_surface)
1990 wl_shell_surface_move(output->parent.shell_surface,
1991 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001992 frame_status_clear(output->frame,
1993 FRAME_STATUS_MOVE);
1994 return;
1995 }
1996
1997 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1998 return;
1999 }
2000
2001 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2002
2003 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
2004 input->touch_active = true;
2005}
2006
2007static void
2008input_handle_touch_up(void *data, struct wl_touch *wl_touch,
2009 uint32_t serial, uint32_t time, int32_t id)
2010{
2011 struct wayland_input *input = data;
2012 struct wayland_output *output = input->touch_focus;
2013 bool active = input->touch_active;
2014
2015 input->touch_points--;
2016 if (input->touch_points == 0) {
2017 input->touch_focus = NULL;
2018 input->touch_active = false;
2019 }
2020
2021 if (!output)
2022 return;
2023
2024 if (output->frame) {
2025 frame_touch_up(output->frame, input, id);
2026
2027 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
2028 wayland_output_destroy(&output->base);
2029 input->touch_focus = NULL;
2030 input->keyboard_focus = NULL;
2031 if (wl_list_empty(&input->backend->compositor->output_list))
2032 weston_compositor_exit(input->backend->compositor);
2033
2034 return;
2035 }
2036 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2037 weston_output_schedule_repaint(&output->base);
2038 }
2039
2040 if (active)
2041 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
2042}
2043
2044static void
2045input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002046 uint32_t time, int32_t id,
2047 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06002048{
2049 struct wayland_input *input = data;
2050 struct wayland_output *output = input->touch_focus;
2051 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002052 double x, y;
2053
2054 x = wl_fixed_to_double(fixed_x);
2055 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06002056
2057 if (!output || !input->touch_active)
2058 return;
2059
2060 if (output->frame) {
2061 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002062 x -= fx;
2063 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002064 }
2065
2066 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2067
2068 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2069}
2070
2071static void
2072input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2073{
2074 struct wayland_input *input = data;
2075
2076 if (!input->touch_focus || !input->touch_active)
2077 return;
2078
2079 notify_touch_frame(&input->base);
2080}
2081
2082static void
2083input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2084{
2085 struct wayland_input *input = data;
2086
2087 if (!input->touch_focus || !input->touch_active)
2088 return;
2089
2090 notify_touch_cancel(&input->base);
2091}
2092
2093static const struct wl_touch_listener touch_listener = {
2094 input_handle_touch_down,
2095 input_handle_touch_up,
2096 input_handle_touch_motion,
2097 input_handle_touch_frame,
2098 input_handle_touch_cancel,
2099};
2100
2101
2102static void
Daniel Stone37816df2012-05-16 18:45:18 +01002103input_handle_capabilities(void *data, struct wl_seat *seat,
2104 enum wl_seat_capability caps)
2105{
2106 struct wayland_input *input = data;
2107
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002108 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2109 input->parent.pointer = wl_seat_get_pointer(seat);
2110 wl_pointer_set_user_data(input->parent.pointer, input);
2111 wl_pointer_add_listener(input->parent.pointer,
2112 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002113 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002114 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002115 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2116 wl_pointer_release(input->parent.pointer);
2117 else
2118 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002119 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002120 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002121 }
2122
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002123 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2124 input->parent.keyboard = wl_seat_get_keyboard(seat);
2125 wl_keyboard_set_user_data(input->parent.keyboard, input);
2126 wl_keyboard_add_listener(input->parent.keyboard,
2127 &keyboard_listener, input);
2128 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002129 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2130 wl_keyboard_release(input->parent.keyboard);
2131 else
2132 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002133 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002134 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002135 }
Derek Foreman748c6952015-11-06 15:56:10 -06002136
2137 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2138 input->parent.touch = wl_seat_get_touch(seat);
2139 wl_touch_set_user_data(input->parent.touch, input);
2140 wl_touch_add_listener(input->parent.touch,
2141 &touch_listener, input);
2142 weston_seat_init_touch(&input->base);
2143 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2144 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2145 wl_touch_release(input->parent.touch);
2146 else
2147 wl_touch_destroy(input->parent.touch);
2148 input->parent.touch = NULL;
2149 weston_seat_release_touch(&input->base);
2150 }
Daniel Stone37816df2012-05-16 18:45:18 +01002151}
2152
Jonny Lamb497994a2014-08-12 14:58:26 +02002153static void
2154input_handle_name(void *data, struct wl_seat *seat,
2155 const char *name)
2156{
2157}
2158
Daniel Stone37816df2012-05-16 18:45:18 +01002159static const struct wl_seat_listener seat_listener = {
2160 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002161 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002162};
2163
2164static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002165display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002166{
2167 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002168 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002169
Peter Huttererf3d62272013-08-08 11:57:05 +10002170 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002171 if (input == NULL)
2172 return;
2173
Giulio Camuffo954f1832014-10-11 18:27:30 +03002174 weston_seat_init(&input->base, b->compositor, "default");
2175 input->backend = b;
2176 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002177 &wl_seat_interface, version);
2178 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002179 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002180
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002181 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2182 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002183
2184 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002185 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002186
2187 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2188 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002189}
2190
2191static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002192wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2193 int32_t x, int32_t y,
2194 int32_t physical_width, int32_t physical_height,
2195 int32_t subpixel, const char *make,
2196 const char *model, int32_t transform)
2197{
2198 struct wayland_parent_output *output = data;
2199
2200 output->x = x;
2201 output->y = y;
2202 output->physical.width = physical_width;
2203 output->physical.height = physical_height;
2204 output->physical.subpixel = subpixel;
2205
2206 free(output->physical.make);
2207 output->physical.make = strdup(make);
2208 free(output->physical.model);
2209 output->physical.model = strdup(model);
2210
2211 output->transform = transform;
2212}
2213
2214static struct weston_mode *
2215find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2216{
2217 struct weston_mode *mode;
2218
2219 wl_list_for_each(mode, list, link) {
2220 if (mode->width == width && mode->height == height &&
2221 mode->refresh == refresh)
2222 return mode;
2223 }
2224
2225 mode = zalloc(sizeof *mode);
2226 if (!mode)
2227 return NULL;
2228
2229 mode->width = width;
2230 mode->height = height;
2231 mode->refresh = refresh;
2232 wl_list_insert(list, &mode->link);
2233
2234 return mode;
2235}
2236
2237static void
2238wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2239 uint32_t flags, int32_t width, int32_t height,
2240 int32_t refresh)
2241{
2242 struct wayland_parent_output *output = data;
2243 struct weston_mode *mode;
2244
2245 if (output->output) {
2246 mode = find_mode(&output->output->base.mode_list,
2247 width, height, refresh);
2248 if (!mode)
2249 return;
2250 mode->flags = flags;
2251 /* Do a mode-switch on current mode change? */
2252 } else {
2253 mode = find_mode(&output->mode_list, width, height, refresh);
2254 if (!mode)
2255 return;
2256 mode->flags = flags;
2257 if (flags & WL_OUTPUT_MODE_CURRENT)
2258 output->current_mode = mode;
2259 if (flags & WL_OUTPUT_MODE_PREFERRED)
2260 output->preferred_mode = mode;
2261 }
2262}
2263
2264static const struct wl_output_listener output_listener = {
2265 wayland_parent_output_geometry,
2266 wayland_parent_output_mode
2267};
2268
2269static void
Pekka Paalanenb07de932017-10-19 12:03:06 +03002270output_sync_callback(void *data, struct wl_callback *callback, uint32_t unused)
2271{
2272 struct wayland_parent_output *output = data;
2273
2274 assert(output->sync_cb == callback);
2275 wl_callback_destroy(callback);
2276 output->sync_cb = NULL;
2277
2278 assert(output->backend->sprawl_across_outputs);
2279
2280 wayland_output_create_for_parent_output(output->backend, output);
2281}
2282
2283static const struct wl_callback_listener output_sync_listener = {
2284 output_sync_callback
2285};
2286
2287static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002288wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002289{
2290 struct wayland_parent_output *output;
2291
2292 output = zalloc(sizeof *output);
2293 if (!output)
2294 return;
2295
Pekka Paalanenb07de932017-10-19 12:03:06 +03002296 output->backend = b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002297 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002298 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002299 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002300 if (!output->global) {
2301 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002302 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002303 }
2304
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002305 wl_output_add_listener(output->global, &output_listener, output);
2306
2307 output->scale = 0;
2308 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2309 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2310 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002312
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 if (b->sprawl_across_outputs) {
Pekka Paalanenb07de932017-10-19 12:03:06 +03002314 output->sync_cb = wl_display_sync(b->parent.wl_display);
2315 wl_callback_add_listener(output->sync_cb,
2316 &output_sync_listener, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002317 }
2318}
2319
2320static void
2321wayland_parent_output_destroy(struct wayland_parent_output *output)
2322{
2323 struct weston_mode *mode, *next;
2324
Pekka Paalanenb07de932017-10-19 12:03:06 +03002325 if (output->sync_cb)
2326 wl_callback_destroy(output->sync_cb);
2327
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002328 if (output->output)
2329 wayland_output_destroy(&output->output->base);
2330
2331 wl_output_destroy(output->global);
2332 free(output->physical.make);
2333 free(output->physical.model);
2334
2335 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2336 wl_list_remove(&mode->link);
2337 free(mode);
2338 }
Pekka Paalanenf1322792017-10-19 10:26:27 +03002339
2340 wl_list_remove(&output->link);
2341 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002342}
2343
2344static void
Armin Krezović34476192016-11-21 18:42:42 +01002345xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2346{
2347 zxdg_shell_v6_pong(shell, serial);
2348}
2349
2350static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2351 xdg_shell_ping,
2352};
2353
2354static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002355registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2356 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002357{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002358 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002359
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002360 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002361 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002362 wl_registry_bind(registry, name,
Derek Foremandbfd2482017-05-19 10:42:07 -05002363 &wl_compositor_interface,
2364 MIN(version, 4));
Armin Krezović34476192016-11-21 18:42:42 +01002365 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2366 b->parent.xdg_shell =
2367 wl_registry_bind(registry, name,
2368 &zxdg_shell_v6_interface, 1);
2369 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2370 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002371 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002372 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002373 wl_registry_bind(registry, name,
2374 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002375 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002376 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002377 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002378 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002379 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002380 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002381 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002382 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002383 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002384 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002385 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002386 }
2387}
2388
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002389static void
2390registry_handle_global_remove(void *data, struct wl_registry *registry,
2391 uint32_t name)
2392{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002393 struct wayland_backend *b = data;
Pekka Paalanenf1322792017-10-19 10:26:27 +03002394 struct wayland_parent_output *output, *next;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002395
Pekka Paalanenf1322792017-10-19 10:26:27 +03002396 wl_list_for_each_safe(output, next, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002397 if (output->id == name)
2398 wayland_parent_output_destroy(output);
2399}
2400
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002401static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002402 registry_handle_global,
2403 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002404};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002405
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002406static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002407wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002408{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002409 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002410 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002411
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002412 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002413 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002414 return 0;
2415 }
2416
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002417 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002418 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002419 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002420 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002421
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002422 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002423 count = wl_display_dispatch_pending(b->parent.wl_display);
2424 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002425 }
2426
2427 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002428}
2429
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002430static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002431wayland_restore(struct weston_compositor *ec)
2432{
2433}
2434
2435static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002436wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002437{
Armin Krezović938dc522016-08-01 19:17:57 +02002438 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002439
Armin Krezović78895c52016-10-09 17:30:28 +02002440 wl_event_source_remove(b->parent.wl_source);
2441
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002442 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002443
Giulio Camuffo954f1832014-10-11 18:27:30 +03002444 if (b->parent.shm)
2445 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002446
Armin Krezović34476192016-11-21 18:42:42 +01002447 if (b->parent.xdg_shell)
2448 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2449
2450 if (b->parent.shell)
2451 wl_shell_destroy(b->parent.shell);
2452
Armin Krezović78895c52016-10-09 17:30:28 +02002453 if (b->parent.fshell)
2454 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2455
2456 if (b->parent.compositor)
2457 wl_compositor_destroy(b->parent.compositor);
2458
Armin Krezović78895c52016-10-09 17:30:28 +02002459 if (b->theme)
2460 theme_destroy(b->theme);
2461
2462 if (b->frame_device)
2463 cairo_device_destroy(b->frame_device);
2464
2465 wl_cursor_theme_destroy(b->cursor_theme);
2466
Daniel Stone698f9bf2016-11-24 15:31:33 +00002467 wl_registry_destroy(b->parent.registry);
2468 wl_display_flush(b->parent.wl_display);
2469 wl_display_disconnect(b->parent.wl_display);
2470
Giulio Camuffo954f1832014-10-11 18:27:30 +03002471 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002472}
2473
Jason Ekstrand7744f712013-10-27 22:24:55 -05002474static const char *left_ptrs[] = {
2475 "left_ptr",
2476 "default",
2477 "top_left_arrow",
2478 "left-arrow"
2479};
2480
2481static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002482create_cursor(struct wayland_backend *b,
2483 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002484{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002485 unsigned int i;
2486
Benoit Gschwind244ff792016-04-28 20:33:11 +02002487 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2488 config->cursor_size,
2489 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002490 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002491 fprintf(stderr, "could not load cursor theme\n");
2492 return;
2493 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002494
Giulio Camuffo954f1832014-10-11 18:27:30 +03002495 b->cursor = NULL;
2496 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2497 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002498 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002499 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002500 fprintf(stderr, "could not load left cursor\n");
2501 return;
2502 }
2503}
2504
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002505static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002506fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2507 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002508{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002509 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002510 struct wayland_input *input = NULL;
2511
Giulio Camuffo954f1832014-10-11 18:27:30 +03002512 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002513 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002514 break;
2515
2516 if (!input || !input->output)
2517 return;
2518
2519 if (input->output->frame)
2520 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2521 else
2522 wayland_output_set_windowed(input->output);
2523
2524 weston_output_schedule_repaint(&input->output->base);
2525}
2526
Giulio Camuffo954f1832014-10-11 18:27:30 +03002527static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002528wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002529 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002530{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002531 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002532 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002533 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002534
Giulio Camuffo954f1832014-10-11 18:27:30 +03002535 b = zalloc(sizeof *b);
2536 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002537 return NULL;
2538
Giulio Camuffo954f1832014-10-11 18:27:30 +03002539 b->compositor = compositor;
Pekka Paalanen7da9a382017-08-30 11:29:49 +03002540 compositor->backend = &b->base;
2541
Giulio Camuffo954f1832014-10-11 18:27:30 +03002542 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002543 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002544
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002545 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002546 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002547 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002548 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002549 }
2550
Giulio Camuffo954f1832014-10-11 18:27:30 +03002551 wl_list_init(&b->parent.output_list);
2552 wl_list_init(&b->input_list);
2553 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2554 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2555 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002556
Benoit Gschwind244ff792016-04-28 20:33:11 +02002557 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002558
Armin Krezovićb08e1a52016-12-09 22:58:27 +01002559#ifdef ENABLE_EGL
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002560 b->use_pixman = new_config->use_pixman;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01002561#else
2562 b->use_pixman = true;
2563#endif
Armin Krezović174448a2016-09-30 14:11:09 +02002564 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002565
Giulio Camuffo954f1832014-10-11 18:27:30 +03002566 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002567 gl_renderer = weston_load_module("gl-renderer.so",
2568 "gl_renderer_interface");
2569 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002570 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002571 }
2572
Giulio Camuffo954f1832014-10-11 18:27:30 +03002573 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002574 if (gl_renderer->display_create(compositor,
2575 EGL_PLATFORM_WAYLAND_KHR,
2576 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002577 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002578 gl_renderer->alpha_attribs,
2579 NULL,
2580 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002581 weston_log("Failed to initialize the GL renderer; "
2582 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002583 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002584 }
2585 }
2586
Giulio Camuffo954f1832014-10-11 18:27:30 +03002587 if (b->use_pixman) {
2588 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002589 weston_log("Failed to initialize pixman renderer\n");
2590 goto err_display;
2591 }
2592 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002593
Giulio Camuffo954f1832014-10-11 18:27:30 +03002594 b->base.destroy = wayland_destroy;
2595 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002596
Giulio Camuffo954f1832014-10-11 18:27:30 +03002597 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002598
Giulio Camuffo954f1832014-10-11 18:27:30 +03002599 fd = wl_display_get_fd(b->parent.wl_display);
2600 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002601 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002602 wayland_backend_handle_event, b);
2603 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002604 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002605
Giulio Camuffo954f1832014-10-11 18:27:30 +03002606 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002607
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002608 if (compositor->renderer->import_dmabuf) {
2609 if (linux_dmabuf_setup(compositor) < 0)
2610 weston_log("Error: initializing dmabuf "
2611 "support failed.\n");
2612 }
2613
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002615err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002616 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002617err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002619 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002620 return NULL;
2621}
2622
2623static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002624wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002625{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002626 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002627
Giulio Camuffo954f1832014-10-11 18:27:30 +03002628 if (b->theme)
2629 theme_destroy(b->theme);
2630 if (b->frame_device)
2631 cairo_device_destroy(b->frame_device);
2632 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002633
Giulio Camuffo954f1832014-10-11 18:27:30 +03002634 weston_compositor_shutdown(b->compositor);
2635 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002636}
2637
Armin Krezović174448a2016-09-30 14:11:09 +02002638static const struct weston_windowed_output_api windowed_api = {
2639 wayland_output_set_size,
2640 wayland_output_create,
2641};
2642
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002643static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002644config_init_to_defaults(struct weston_wayland_backend_config *config)
2645{
2646}
2647
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002648WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01002649weston_backend_init(struct weston_compositor *compositor,
2650 struct weston_backend_config *config_base)
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002651{
2652 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002653 struct wayland_parent_output *poutput;
2654 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002655 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002656
Benoit Gschwinde091b452016-05-10 22:47:48 +02002657 if (config_base == NULL ||
2658 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2659 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2660 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002661 return -1;
2662 }
2663
2664 config_init_to_defaults(&new_config);
2665 memcpy(&new_config, config_base, config_base->struct_size);
2666
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002667 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002668
Giulio Camuffo954f1832014-10-11 18:27:30 +03002669 if (!b)
2670 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002671
Benoit Gschwind37a68072016-04-28 20:33:08 +02002672 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002673 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002674 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002675
Giulio Camuffo954f1832014-10-11 18:27:30 +03002676 wl_list_for_each(poutput, &b->parent.output_list, link)
2677 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002678
Giulio Camuffo954f1832014-10-11 18:27:30 +03002679 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002680 }
2681
Benoit Gschwind37a68072016-04-28 20:33:08 +02002682 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002683 if (wayland_output_create_fullscreen(b) < 0) {
2684 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002685 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002686 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002687
Giulio Camuffo954f1832014-10-11 18:27:30 +03002688 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002689 }
2690
Armin Krezović174448a2016-09-30 14:11:09 +02002691 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2692 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002693
Armin Krezović174448a2016-09-30 14:11:09 +02002694 if (ret < 0) {
2695 weston_log("Failed to register output API.\n");
2696 wayland_backend_destroy(b);
2697 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002698 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002699
Giulio Camuffo954f1832014-10-11 18:27:30 +03002700 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002701 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002702 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002703 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002704
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002705err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 wayland_backend_destroy(b);
2707 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002708}