blob: bc78cbdb573790a86dc3806d6711421bf527508b [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 Paalanen44195432017-10-18 12:31:40 +0300880 } else {
881 abort();
Pekka Paalanen7d2fc922017-10-18 12:18:39 +0300882 }
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600883
884 return 0;
885}
886
887static void
888wayland_output_set_fullscreen(struct wayland_output *output,
889 enum wl_shell_surface_fullscreen_method method,
890 uint32_t framerate, struct wl_output *target)
891{
892 if (output->frame) {
893 frame_destroy(output->frame);
894 output->frame = NULL;
895 }
896
897 wayland_output_resize_surface(output);
898
Armin Krezović34476192016-11-21 18:42:42 +0100899 if (output->parent.xdg_toplevel) {
900 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
901 } else if (output->parent.shell_surface) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500902 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
903 method, framerate, target);
Pekka Paalanen44195432017-10-18 12:31:40 +0300904 } else {
905 abort();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500906 }
907}
908
909static struct weston_mode *
910wayland_output_choose_mode(struct wayland_output *output,
911 struct weston_mode *ref_mode)
912{
913 struct weston_mode *mode;
914
915 /* First look for an exact match */
916 wl_list_for_each(mode, &output->base.mode_list, link)
917 if (mode->width == ref_mode->width &&
918 mode->height == ref_mode->height &&
919 mode->refresh == ref_mode->refresh)
920 return mode;
921
922 /* If we can't find an exact match, ignore refresh and try again */
923 wl_list_for_each(mode, &output->base.mode_list, link)
924 if (mode->width == ref_mode->width &&
925 mode->height == ref_mode->height)
926 return mode;
927
928 /* Yeah, we failed */
929 return NULL;
930}
931
932enum mode_status {
933 MODE_STATUS_UNKNOWN,
934 MODE_STATUS_SUCCESS,
935 MODE_STATUS_FAIL,
936 MODE_STATUS_CANCEL,
937};
938
939static void
940mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800941 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500942{
943 enum mode_status *value = data;
944
945 printf("Mode switch successful\n");
946
947 *value = MODE_STATUS_SUCCESS;
948}
949
950static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800951mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500952{
953 enum mode_status *value = data;
954
955 printf("Mode switch failed\n");
956
957 *value = MODE_STATUS_FAIL;
958}
959
960static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800961mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500962{
963 enum mode_status *value = data;
964
965 printf("Mode switch cancelled\n");
966
967 *value = MODE_STATUS_CANCEL;
968}
969
Jonas Ådahl496adb32015-11-17 16:00:27 +0800970struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500971 mode_feedback_successful,
972 mode_feedback_failed,
973 mode_feedback_cancelled,
974};
975
Armin Krezović71ebc052017-02-07 21:03:40 +0100976static enum mode_status
977wayland_output_fullscreen_shell_mode_feedback(struct wayland_output *output,
978 struct weston_mode *mode)
979{
980 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
981 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
982 enum mode_status mode_status;
983 int ret = 0;
984
985 mode_feedback =
986 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
987 output->parent.surface,
988 output->parent.output,
989 mode->refresh);
990
991 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
992 &mode_feedback_listener,
993 &mode_status);
994
995 output->parent.draw_initial_frame = false;
996 draw_initial_frame(output);
997 wl_surface_commit(output->parent.surface);
998
999 mode_status = MODE_STATUS_UNKNOWN;
1000 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
1001 ret = wl_display_dispatch(b->parent.wl_display);
1002
1003 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
1004
1005 return mode_status;
1006}
1007
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001008static int
1009wayland_output_switch_mode(struct weston_output *output_base,
1010 struct weston_mode *mode)
1011{
Armin Krezović938dc522016-08-01 19:17:57 +02001012 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001013 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001014 struct wl_surface *old_surface;
1015 struct weston_mode *old_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001016 enum mode_status mode_status;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001017
1018 if (output_base == NULL) {
1019 weston_log("output is NULL.\n");
1020 return -1;
1021 }
1022
1023 if (mode == NULL) {
1024 weston_log("mode is NULL.\n");
1025 return -1;
1026 }
1027
Armin Krezović938dc522016-08-01 19:17:57 +02001028 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001029
Armin Krezović34476192016-11-21 18:42:42 +01001030 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001031 return -1;
1032
1033 mode = wayland_output_choose_mode(output, mode);
1034 if (mode == NULL)
1035 return -1;
1036
1037 if (output->base.current_mode == mode)
1038 return 0;
1039
1040 old_mode = output->base.current_mode;
1041 old_surface = output->parent.surface;
1042 output->base.current_mode = mode;
1043 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001044 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001045 wl_surface_set_user_data(output->parent.surface, output);
1046
1047 /* Blow the old buffers because we changed size/surfaces */
1048 wayland_output_resize_surface(output);
1049
Armin Krezović71ebc052017-02-07 21:03:40 +01001050 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, mode);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001051
1052 /* This should kick-start things again */
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001053 wayland_output_start_repaint_loop(&output->base);
1054
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001055 if (mode_status == MODE_STATUS_FAIL) {
1056 output->base.current_mode = old_mode;
1057 wl_surface_destroy(output->parent.surface);
1058 output->parent.surface = old_surface;
1059 wayland_output_resize_surface(output);
1060
1061 return -1;
1062 }
1063
1064 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1065 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1066
Giulio Camuffo954f1832014-10-11 18:27:30 +03001067 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001068 pixman_renderer_output_destroy(output_base);
1069 if (wayland_output_init_pixman_renderer(output) < 0)
1070 goto err_output;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001071#ifdef ENABLE_EGL
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001072 } else {
1073 gl_renderer->output_destroy(output_base);
1074 wl_egl_window_destroy(output->gl.egl_window);
1075 if (wayland_output_init_gl_renderer(output) < 0)
1076 goto err_output;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001077#endif
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001078 }
1079 wl_surface_destroy(old_surface);
1080
1081 weston_output_schedule_repaint(&output->base);
1082
1083 return 0;
1084
1085err_output:
1086 /* XXX */
1087 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001088}
1089
Armin Krezović34476192016-11-21 18:42:42 +01001090static void
1091handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1092 uint32_t serial)
1093{
1094 zxdg_surface_v6_ack_configure(surface, serial);
1095}
1096
1097static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1098 handle_xdg_surface_configure
1099};
1100
1101static void
1102handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1103 int32_t width, int32_t height,
1104 struct wl_array *states)
1105{
1106 struct wayland_output *output = data;
1107
1108 output->parent.configure_width = width;
1109 output->parent.configure_height = height;
1110
1111 output->parent.wait_for_configure = false;
1112 /* FIXME: implement resizing */
1113}
1114
1115static void
1116handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1117{
Sergi Granelleaa73582017-03-25 17:19:36 +01001118 struct wayland_output *output = data;
Sergi Granellb4e239f2017-09-27 16:06:37 +02001119 struct weston_compositor *compositor = output->base.compositor;
Sergi Granelleaa73582017-03-25 17:19:36 +01001120
Sergi Granellb4e239f2017-09-27 16:06:37 +02001121 wayland_output_destroy(&output->base);
1122
1123 if (wl_list_empty(&compositor->output_list))
1124 weston_compositor_exit(compositor);
Armin Krezović34476192016-11-21 18:42:42 +01001125}
1126
1127static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1128 handle_xdg_toplevel_configure,
1129 handle_xdg_toplevel_close,
1130};
1131
Armin Krezović174448a2016-09-30 14:11:09 +02001132static int
1133wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001134{
Armin Krezović174448a2016-09-30 14:11:09 +02001135 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001136
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001137 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001138 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001139 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001140 return -1;
1141
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001142 wl_surface_set_user_data(output->parent.surface, output);
1143
Armin Krezović2d321e32016-10-09 17:30:25 +02001144 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001145
Armin Krezović34476192016-11-21 18:42:42 +01001146 if (b->parent.xdg_shell) {
1147 output->parent.xdg_surface =
1148 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1149 output->parent.surface);
1150 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1151 &xdg_surface_listener, output);
1152
1153 output->parent.xdg_toplevel =
1154 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1155 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1156 &xdg_toplevel_listener, output);
1157
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001158 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, output->title);
1159
Armin Krezović34476192016-11-21 18:42:42 +01001160 wl_surface_commit(output->parent.surface);
1161
1162 output->parent.wait_for_configure = true;
1163
1164 while (output->parent.wait_for_configure)
1165 wl_display_dispatch(b->parent.wl_display);
1166
1167 weston_log("wayland-backend: Using xdg_shell_v6\n");
1168 }
1169 else if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001170 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001171 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001172 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001173 if (!output->parent.shell_surface) {
1174 wl_surface_destroy(output->parent.surface);
1175 return -1;
1176 }
1177
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001178 wl_shell_surface_add_listener(output->parent.shell_surface,
1179 &shell_surface_listener, output);
Armin Krezović34476192016-11-21 18:42:42 +01001180
1181 weston_log("wayland-backend: Using wl_shell\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001182 }
1183
Armin Krezović174448a2016-09-30 14:11:09 +02001184 return 0;
1185}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001186
Armin Krezović174448a2016-09-30 14:11:09 +02001187static int
1188wayland_output_enable(struct weston_output *base)
1189{
1190 struct wayland_output *output = to_wayland_output(base);
1191 struct wayland_backend *b = to_wayland_backend(base->compositor);
Armin Krezović71ebc052017-02-07 21:03:40 +01001192 enum mode_status mode_status;
Armin Krezović174448a2016-09-30 14:11:09 +02001193 int ret = 0;
1194
Armin Krezović174448a2016-09-30 14:11:09 +02001195 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1196 output->base.current_mode->width,
1197 output->base.current_mode->height,
1198 output->base.x, output->base.y);
1199
Armin Krezovićf054d352016-10-09 17:30:27 +02001200 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001201 ret = wayland_backend_create_output_surface(output);
1202
1203 if (ret < 0)
1204 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001205
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001206 wl_list_init(&output->shm.buffers);
1207 wl_list_init(&output->shm.free_buffers);
1208
Giulio Camuffo954f1832014-10-11 18:27:30 +03001209 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001210 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001211 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001212
1213 output->base.repaint = wayland_output_repaint_pixman;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001214#ifdef ENABLE_EGL
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001215 } else {
1216 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001217 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001218
1219 output->base.repaint = wayland_output_repaint_gl;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001220#endif
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001221 }
1222
Armin Krezovićf16de172016-10-09 17:30:26 +02001223 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1224 output->base.assign_planes = NULL;
1225 output->base.set_backlight = NULL;
1226 output->base.set_dpms = NULL;
1227 output->base.switch_mode = wayland_output_switch_mode;
1228
Armin Krezović174448a2016-09-30 14:11:09 +02001229 if (b->sprawl_across_outputs) {
Armin Krezović71ebc052017-02-07 21:03:40 +01001230 if (b->parent.fshell) {
1231 wayland_output_resize_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001232
Armin Krezović71ebc052017-02-07 21:03:40 +01001233 mode_status = wayland_output_fullscreen_shell_mode_feedback(output, &output->mode);
1234
1235 if (mode_status == MODE_STATUS_FAIL) {
1236 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1237 output->parent.surface,
1238 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1239 output->parent.output);
1240
1241 output->parent.draw_initial_frame = true;
1242 }
1243 } else {
1244 wayland_output_set_fullscreen(output,
1245 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1246 output->mode.refresh, output->parent.output);
Armin Krezović174448a2016-09-30 14:11:09 +02001247 }
1248 } else if (b->fullscreen) {
1249 wayland_output_set_fullscreen(output, 0, 0, NULL);
1250 } else {
1251 wayland_output_set_windowed(output);
1252 }
1253
1254 return 0;
1255
1256err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001257 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001258
1259 return -1;
1260}
1261
1262static struct wayland_output *
Pekka Paalanen1580be62017-08-11 16:05:41 +03001263wayland_output_create_common(struct weston_compositor *compositor,
1264 const char *name)
Armin Krezović174448a2016-09-30 14:11:09 +02001265{
1266 struct wayland_output *output;
Pekka Paalanenffa42ff2017-08-11 15:55:32 +03001267 char *title;
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001268
1269 /* name can't be NULL. */
1270 assert(name);
Armin Krezović174448a2016-09-30 14:11:09 +02001271
1272 output = zalloc(sizeof *output);
1273 if (output == NULL) {
1274 perror("zalloc");
1275 return NULL;
1276 }
1277
Pekka Paalanenffa42ff2017-08-11 15:55:32 +03001278 if (asprintf(&title, "%s - %s", WINDOW_TITLE, name) < 0) {
1279 free(output);
1280 return NULL;
1281 }
1282 output->title = title;
1283
Pekka Paalanen1580be62017-08-11 16:05:41 +03001284 weston_output_init(&output->base, compositor, name);
1285
Matt Roper361d2ad2011-08-29 13:52:23 -07001286 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001287 output->base.disable = wayland_output_disable;
1288 output->base.enable = wayland_output_enable;
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001289
Armin Krezović174448a2016-09-30 14:11:09 +02001290 return output;
1291}
1292
1293static int
1294wayland_output_create(struct weston_compositor *compositor, const char *name)
1295{
Pekka Paalanen1580be62017-08-11 16:05:41 +03001296 struct wayland_output *output;
Armin Krezović174448a2016-09-30 14:11:09 +02001297
Pekka Paalanen1580be62017-08-11 16:05:41 +03001298 output = wayland_output_create_common(compositor, name);
Sergi Granell7fecb432017-03-24 20:48:02 +01001299 if (!output)
1300 return -1;
1301
Armin Krezović174448a2016-09-30 14:11:09 +02001302 weston_compositor_add_pending_output(&output->base, compositor);
1303
1304 return 0;
1305}
1306
1307static int
1308wayland_output_set_size(struct weston_output *base, int width, int height)
1309{
1310 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001311 int output_width, output_height;
1312
1313 /* We can only be called once. */
1314 assert(!output->base.current_mode);
1315
1316 /* Make sure we have scale set. */
1317 assert(output->base.scale);
1318
1319 if (width < 1) {
1320 weston_log("Invalid width \"%d\" for output %s\n",
1321 width, output->base.name);
1322 return -1;
1323 }
1324
1325 if (height < 1) {
1326 weston_log("Invalid height \"%d\" for output %s\n",
1327 height, output->base.name);
1328 return -1;
1329 }
1330
1331 output_width = width * output->base.scale;
1332 output_height = height * output->base.scale;
1333
1334 output->mode.flags =
1335 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1336
1337 output->mode.width = output_width;
1338 output->mode.height = output_height;
1339 output->mode.refresh = 60000;
Armin Krezović174448a2016-09-30 14:11:09 +02001340 wl_list_insert(&output->base.mode_list, &output->mode.link);
1341
1342 output->base.current_mode = &output->mode;
1343 output->base.make = "wayland";
1344 output->base.model = "none";
1345
1346 /* XXX: Calculate proper size. */
1347 output->base.mm_width = width;
1348 output->base.mm_height = height;
1349
Armin Krezović174448a2016-09-30 14:11:09 +02001350 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001351}
1352
Armin Krezović174448a2016-09-30 14:11:09 +02001353static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001354wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001355 struct wayland_parent_output *poutput)
1356{
Sergi Granell7fecb432017-03-24 20:48:02 +01001357 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001358 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001359
Pekka Paalanen1580be62017-08-11 16:05:41 +03001360 output = wayland_output_create_common(b->compositor, "wlparent");
Sergi Granell7fecb432017-03-24 20:48:02 +01001361 if (!output)
1362 return -1;
1363
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001364 if (poutput->current_mode) {
1365 mode = poutput->current_mode;
1366 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001367 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001368 } else if (!wl_list_empty(&poutput->mode_list)) {
1369 mode = container_of(poutput->mode_list.next,
1370 struct weston_mode, link);
1371 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001372 weston_log("No valid modes found. Skipping output.\n");
1373 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001374 }
1375
Armin Krezović174448a2016-09-30 14:11:09 +02001376 output->base.scale = 1;
1377 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001378
1379 output->parent.output = poutput->global;
1380
1381 output->base.make = poutput->physical.make;
1382 output->base.model = poutput->physical.model;
Pekka Paalanen4dab5832017-10-19 13:19:50 +03001383 output->base.mm_width = poutput->physical.width;
1384 output->base.mm_height = poutput->physical.height;
Armin Krezović174448a2016-09-30 14:11:09 +02001385
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001386 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1387 wl_list_init(&poutput->mode_list);
1388
Pekka Paalanen4dab5832017-10-19 13:19:50 +03001389 /* No other mode should have CURRENT already. */
1390 mode->flags |= WL_OUTPUT_MODE_CURRENT;
1391 output->base.current_mode = mode;
1392
1393 /* output->mode is unused in this path. */
1394
Armin Krezović174448a2016-09-30 14:11:09 +02001395 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001396
Armin Krezović174448a2016-09-30 14:11:09 +02001397 return 0;
1398
1399out:
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001400 weston_output_release(&output->base);
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001401 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +02001402 free(output);
1403
1404 return -1;
1405}
1406
1407static int
1408wayland_output_create_fullscreen(struct wayland_backend *b)
1409{
Sergi Granell7fecb432017-03-24 20:48:02 +01001410 struct wayland_output *output;
Armin Krezović174448a2016-09-30 14:11:09 +02001411 int width = 0, height = 0;
1412
Pekka Paalanen1580be62017-08-11 16:05:41 +03001413 output = wayland_output_create_common(b->compositor, "wayland-fullscreen");
Sergi Granell7fecb432017-03-24 20:48:02 +01001414 if (!output)
1415 return -1;
1416
Armin Krezović174448a2016-09-30 14:11:09 +02001417 output->base.scale = 1;
1418 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1419
1420 if (wayland_backend_create_output_surface(output) < 0)
1421 goto err_surface;
1422
1423 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001424 if (b->parent.xdg_shell || b->parent.shell) {
1425 if (output->parent.xdg_toplevel)
1426 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1427 output->parent.output);
1428 else if (output->parent.shell_surface)
1429 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1430 0, 0, NULL);
1431
Armin Krezović174448a2016-09-30 14:11:09 +02001432 wl_display_roundtrip(b->parent.wl_display);
1433
1434 width = output->parent.configure_width;
1435 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001436 }
1437
Armin Krezović174448a2016-09-30 14:11:09 +02001438 if (wayland_output_set_size(&output->base, width, height) < 0)
1439 goto err_set_size;
1440
1441 weston_compositor_add_pending_output(&output->base, b->compositor);
1442
1443 return 0;
1444
1445err_set_size:
1446 wayland_backend_destroy_output_surface(output);
1447err_surface:
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001448 weston_output_release(&output->base);
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001449 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +02001450 free(output);
1451
1452 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001453}
1454
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001455static void
1456shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1457 uint32_t serial)
1458{
1459 wl_shell_surface_pong(shell_surface, serial);
1460}
1461
1462static void
1463shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1464 uint32_t edges, int32_t width, int32_t height)
1465{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001466 struct wayland_output *output = data;
1467
1468 output->parent.configure_width = width;
1469 output->parent.configure_height = height;
1470
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001471 /* FIXME: implement resizing */
1472}
1473
1474static void
1475shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1476{
1477}
1478
1479static const struct wl_shell_surface_listener shell_surface_listener = {
1480 shell_surface_ping,
1481 shell_surface_configure,
1482 shell_surface_popup_done
1483};
1484
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001485/* Events received from the wayland-server this compositor is client of: */
1486
Jason Ekstrand7744f712013-10-27 22:24:55 -05001487/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001488static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001489input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001490{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001491
Jason Ekstrand7744f712013-10-27 22:24:55 -05001492 struct wl_buffer *buffer;
1493 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001494
Giulio Camuffo954f1832014-10-11 18:27:30 +03001495 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001496 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001497
Giulio Camuffo954f1832014-10-11 18:27:30 +03001498 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001499 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001500 if (!buffer)
1501 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001502
1503 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1504 input->parent.cursor.surface,
1505 image->hotspot_x, image->hotspot_y);
1506
1507 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1508 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1509 image->width, image->height);
1510 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001511}
1512
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001513static void
Daniel Stone37816df2012-05-16 18:45:18 +01001514input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1515 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001516 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001517{
1518 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001519 int32_t fx, fy;
1520 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001521 double x, y;
1522
1523 x = wl_fixed_to_double(fixed_x);
1524 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001525
Daniel Stone50692802012-06-22 13:21:41 +01001526 /* XXX: If we get a modifier event immediately before the focus,
1527 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001528 input->enter_serial = serial;
1529 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001530
1531 if (input->output->frame) {
1532 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001533 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001534 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001535 x -= fx;
1536 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001537
1538 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1539 weston_output_schedule_repaint(&input->output->base);
1540 } else {
1541 location = THEME_LOCATION_CLIENT_AREA;
1542 }
1543
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001544 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001545
1546 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001547 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001548 notify_pointer_focus(&input->base, &input->output->base, x, y);
1549 wl_pointer_set_cursor(input->parent.pointer,
1550 input->enter_serial, NULL, 0, 0);
1551 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001552 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001553 notify_pointer_focus(&input->base, NULL, 0, 0);
1554 input_set_cursor(input);
1555 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001556}
1557
1558static void
Daniel Stone37816df2012-05-16 18:45:18 +01001559input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1560 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001561{
1562 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001563
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001564 if (!input->output)
1565 return;
1566
Jason Ekstrand7744f712013-10-27 22:24:55 -05001567 if (input->output->frame) {
1568 frame_pointer_leave(input->output->frame, input);
1569
1570 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1571 weston_output_schedule_repaint(&input->output->base);
1572 }
1573
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001574 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001575 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001576 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001577}
1578
1579static void
Daniel Stone37816df2012-05-16 18:45:18 +01001580input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001581 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001582{
1583 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001584 int32_t fx, fy;
1585 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001586 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001587 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001588
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001589 if (!input->output)
1590 return;
1591
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001592 x = wl_fixed_to_double(fixed_x);
1593 y = wl_fixed_to_double(fixed_y);
1594
Jason Ekstrand7744f712013-10-27 22:24:55 -05001595 if (input->output->frame) {
1596 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001597 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001598 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001599 x -= fx;
1600 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001601
1602 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1603 weston_output_schedule_repaint(&input->output->base);
1604 } else {
1605 location = THEME_LOCATION_CLIENT_AREA;
1606 }
1607
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001608 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001609
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001610 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001611 input_set_cursor(input);
1612 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001613 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001614 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001615 } else if (!input->has_focus &&
1616 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001617 wl_pointer_set_cursor(input->parent.pointer,
1618 input->enter_serial, NULL, 0, 0);
1619 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001620 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001621 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001622 }
1623
Peter Hutterer87743e92016-01-18 16:38:22 +10001624 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001625 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001626 want_frame = true;
1627 }
1628
1629 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1630 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001631}
1632
1633static void
1634input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001635 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001636 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001637{
1638 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001639 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001640
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001641 if (!input->output)
1642 return;
1643
Jason Ekstrand7744f712013-10-27 22:24:55 -05001644 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001645 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001646 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001647
1648 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001649 if (input->output->parent.xdg_toplevel)
1650 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001651 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001652 else if (input->output->parent.shell_surface)
1653 wl_shell_surface_move(input->output->parent.shell_surface,
1654 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001655 frame_status_clear(input->output->frame,
1656 FRAME_STATUS_MOVE);
1657 return;
1658 }
1659
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001660 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1661 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001662 input->output = NULL;
1663 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001664
Giulio Camuffo954f1832014-10-11 18:27:30 +03001665 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001666 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001667
1668 return;
1669 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001670
1671 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1672 weston_output_schedule_repaint(&input->output->base);
1673 } else {
1674 location = THEME_LOCATION_CLIENT_AREA;
1675 }
1676
Peter Hutterer87743e92016-01-18 16:38:22 +10001677 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001678 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001679 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1680 notify_pointer_frame(&input->base);
1681 }
Daniel Stone37816df2012-05-16 18:45:18 +01001682}
1683
1684static void
1685input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001686 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001687{
1688 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001689 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001690
Peter Hutterer89b6a492016-01-18 15:58:17 +10001691 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001692 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001693
Peter Hutterer87743e92016-01-18 16:38:22 +10001694 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1695 input->vert.has_discrete) {
1696 weston_event.has_discrete = true;
1697 weston_event.discrete = input->vert.discrete;
1698 input->vert.has_discrete = false;
1699 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1700 input->horiz.has_discrete) {
1701 weston_event.has_discrete = true;
1702 weston_event.discrete = input->horiz.discrete;
1703 input->horiz.has_discrete = false;
1704 }
1705
Peter Hutterer89b6a492016-01-18 15:58:17 +10001706 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001707
1708 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1709 notify_pointer_frame(&input->base);
1710}
1711
1712static void
1713input_handle_frame(void *data, struct wl_pointer *pointer)
1714{
1715 struct wayland_input *input = data;
1716
1717 notify_pointer_frame(&input->base);
1718}
1719
1720static void
1721input_handle_axis_source(void *data, struct wl_pointer *pointer,
1722 uint32_t source)
1723{
1724 struct wayland_input *input = data;
1725
1726 notify_axis_source(&input->base, source);
1727}
1728
1729static void
1730input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1731 uint32_t time, uint32_t axis)
1732{
1733 struct wayland_input *input = data;
1734 struct weston_pointer_axis_event weston_event;
1735
1736 weston_event.axis = axis;
1737 weston_event.value = 0;
1738
1739 notify_axis(&input->base, time, &weston_event);
1740}
1741
1742static void
1743input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1744 uint32_t axis, int32_t discrete)
1745{
1746 struct wayland_input *input = data;
1747
1748 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1749 input->vert.has_discrete = true;
1750 input->vert.discrete = discrete;
1751 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1752 input->horiz.has_discrete = true;
1753 input->horiz.discrete = discrete;
1754 }
Daniel Stone37816df2012-05-16 18:45:18 +01001755}
1756
1757static const struct wl_pointer_listener pointer_listener = {
1758 input_handle_pointer_enter,
1759 input_handle_pointer_leave,
1760 input_handle_motion,
1761 input_handle_button,
1762 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001763 input_handle_frame,
1764 input_handle_axis_source,
1765 input_handle_axis_stop,
1766 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001767};
1768
1769static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001770input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1771 int fd, uint32_t size)
1772{
1773 struct wayland_input *input = data;
1774 struct xkb_keymap *keymap;
1775 char *map_str;
1776
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001777 if (!data) {
1778 close(fd);
1779 return;
1780 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001781
1782 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1783 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1784 if (map_str == MAP_FAILED) {
1785 weston_log("mmap failed: %m\n");
1786 goto error;
1787 }
1788
Giulio Camuffo954f1832014-10-11 18:27:30 +03001789 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001790 map_str,
1791 XKB_KEYMAP_FORMAT_TEXT_V1,
1792 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001793 munmap(map_str, size);
1794
1795 if (!keymap) {
1796 weston_log("failed to compile keymap\n");
1797 goto error;
1798 }
1799
1800 input->keyboard_state_update = STATE_UPDATE_NONE;
1801 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1802 weston_log("No keymap provided; falling back to defalt\n");
1803 keymap = NULL;
1804 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1805 } else {
1806 weston_log("Invalid keymap\n");
1807 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001808 }
1809
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001810 close(fd);
1811
Derek Foreman1281a362015-07-31 16:55:32 -05001812 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001813 weston_seat_update_keymap(&input->base, keymap);
1814 else
1815 weston_seat_init_keyboard(&input->base, keymap);
1816
Ran Benitac9c74152014-08-19 23:59:52 +03001817 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001818
1819 return;
1820
1821error:
1822 wl_keyboard_release(input->parent.keyboard);
1823 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001824}
1825
1826static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001827input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001828 struct wl_keyboard *keyboard,
1829 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001830 struct wl_surface *surface,
1831 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001832{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001833 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001834 struct wayland_output *focus;
1835
1836 focus = input->keyboard_focus;
1837 if (focus) {
1838 /* This shouldn't happen */
1839 focus->keyboard_count--;
1840 if (!focus->keyboard_count && focus->frame)
1841 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1842 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1843 weston_output_schedule_repaint(&focus->base);
1844 }
1845
1846 input->keyboard_focus = wl_surface_get_user_data(surface);
1847 input->keyboard_focus->keyboard_count++;
1848
1849 focus = input->keyboard_focus;
1850 if (focus->frame) {
1851 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1852 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1853 weston_output_schedule_repaint(&focus->base);
1854 }
1855
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001856
Daniel Stone50692802012-06-22 13:21:41 +01001857 /* XXX: If we get a modifier event immediately before the focus,
1858 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001859 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001860 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001861}
1862
1863static void
1864input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001865 struct wl_keyboard *keyboard,
1866 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001867 struct wl_surface *surface)
1868{
1869 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001870 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001871
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001872 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001873
1874 focus = input->keyboard_focus;
1875 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001876 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001877
1878 focus->keyboard_count--;
1879 if (!focus->keyboard_count && focus->frame) {
1880 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1881 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1882 weston_output_schedule_repaint(&focus->base);
1883 }
1884
1885 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001886}
1887
Daniel Stone37816df2012-05-16 18:45:18 +01001888static void
1889input_handle_key(void *data, struct wl_keyboard *keyboard,
1890 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1891{
1892 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001893
Daniel Stone50692802012-06-22 13:21:41 +01001894 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001895 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001896 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001897 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001898 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001899}
1900
Daniel Stone351eb612012-05-31 15:27:47 -04001901static void
Derek Foreman1281a362015-07-31 16:55:32 -05001902input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001903 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001904 uint32_t mods_latched, uint32_t mods_locked,
1905 uint32_t group)
1906{
Derek Foreman1281a362015-07-31 16:55:32 -05001907 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001908 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001909 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001910 uint32_t serial_out;
1911
1912 /* If we get a key event followed by a modifier event with the
1913 * same serial number, then we try to preserve those semantics by
1914 * reusing the same serial number on the way out too. */
1915 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001916 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001917 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001918 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001919
Derek Foreman1281a362015-07-31 16:55:32 -05001920 keyboard = weston_seat_get_keyboard(&input->base);
1921 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001922 mods_depressed, mods_latched,
1923 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001924 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001925}
1926
Jonny Lamb497994a2014-08-12 14:58:26 +02001927static void
1928input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1929 int32_t rate, int32_t delay)
1930{
1931 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001932 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001933
Giulio Camuffo954f1832014-10-11 18:27:30 +03001934 b->compositor->kb_repeat_rate = rate;
1935 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001936}
1937
Daniel Stone37816df2012-05-16 18:45:18 +01001938static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001939 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001940 input_handle_keyboard_enter,
1941 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001942 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001943 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001944 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001945};
1946
1947static void
Derek Foreman748c6952015-11-06 15:56:10 -06001948input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1949 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001950 struct wl_surface *surface, int32_t id,
1951 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001952{
1953 struct wayland_input *input = data;
1954 struct wayland_output *output;
1955 enum theme_location location;
1956 bool first_touch;
1957 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001958 double x, y;
1959
1960 x = wl_fixed_to_double(fixed_x);
1961 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001962
1963 first_touch = (input->touch_points == 0);
1964 input->touch_points++;
1965
1966 input->touch_focus = wl_surface_get_user_data(surface);
1967 output = input->touch_focus;
1968 if (!first_touch && !input->touch_active)
1969 return;
1970
1971 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001972 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001973
1974 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001975 x -= fx;
1976 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001977
1978 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1979 weston_output_schedule_repaint(&output->base);
1980
1981 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1982 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001983 if (output->parent.xdg_toplevel)
1984 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1985 input->parent.seat, serial);
1986 else if (output->parent.shell_surface)
1987 wl_shell_surface_move(output->parent.shell_surface,
1988 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001989 frame_status_clear(output->frame,
1990 FRAME_STATUS_MOVE);
1991 return;
1992 }
1993
1994 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1995 return;
1996 }
1997
1998 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1999
2000 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
2001 input->touch_active = true;
2002}
2003
2004static void
2005input_handle_touch_up(void *data, struct wl_touch *wl_touch,
2006 uint32_t serial, uint32_t time, int32_t id)
2007{
2008 struct wayland_input *input = data;
2009 struct wayland_output *output = input->touch_focus;
2010 bool active = input->touch_active;
2011
2012 input->touch_points--;
2013 if (input->touch_points == 0) {
2014 input->touch_focus = NULL;
2015 input->touch_active = false;
2016 }
2017
2018 if (!output)
2019 return;
2020
2021 if (output->frame) {
2022 frame_touch_up(output->frame, input, id);
2023
2024 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
2025 wayland_output_destroy(&output->base);
2026 input->touch_focus = NULL;
2027 input->keyboard_focus = NULL;
2028 if (wl_list_empty(&input->backend->compositor->output_list))
2029 weston_compositor_exit(input->backend->compositor);
2030
2031 return;
2032 }
2033 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2034 weston_output_schedule_repaint(&output->base);
2035 }
2036
2037 if (active)
2038 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
2039}
2040
2041static void
2042input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002043 uint32_t time, int32_t id,
2044 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06002045{
2046 struct wayland_input *input = data;
2047 struct wayland_output *output = input->touch_focus;
2048 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002049 double x, y;
2050
2051 x = wl_fixed_to_double(fixed_x);
2052 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06002053
2054 if (!output || !input->touch_active)
2055 return;
2056
2057 if (output->frame) {
2058 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002059 x -= fx;
2060 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002061 }
2062
2063 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2064
2065 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2066}
2067
2068static void
2069input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2070{
2071 struct wayland_input *input = data;
2072
2073 if (!input->touch_focus || !input->touch_active)
2074 return;
2075
2076 notify_touch_frame(&input->base);
2077}
2078
2079static void
2080input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2081{
2082 struct wayland_input *input = data;
2083
2084 if (!input->touch_focus || !input->touch_active)
2085 return;
2086
2087 notify_touch_cancel(&input->base);
2088}
2089
2090static const struct wl_touch_listener touch_listener = {
2091 input_handle_touch_down,
2092 input_handle_touch_up,
2093 input_handle_touch_motion,
2094 input_handle_touch_frame,
2095 input_handle_touch_cancel,
2096};
2097
2098
2099static void
Daniel Stone37816df2012-05-16 18:45:18 +01002100input_handle_capabilities(void *data, struct wl_seat *seat,
2101 enum wl_seat_capability caps)
2102{
2103 struct wayland_input *input = data;
2104
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002105 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2106 input->parent.pointer = wl_seat_get_pointer(seat);
2107 wl_pointer_set_user_data(input->parent.pointer, input);
2108 wl_pointer_add_listener(input->parent.pointer,
2109 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002110 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002111 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002112 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2113 wl_pointer_release(input->parent.pointer);
2114 else
2115 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002116 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002117 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002118 }
2119
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002120 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2121 input->parent.keyboard = wl_seat_get_keyboard(seat);
2122 wl_keyboard_set_user_data(input->parent.keyboard, input);
2123 wl_keyboard_add_listener(input->parent.keyboard,
2124 &keyboard_listener, input);
2125 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002126 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2127 wl_keyboard_release(input->parent.keyboard);
2128 else
2129 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002130 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002131 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002132 }
Derek Foreman748c6952015-11-06 15:56:10 -06002133
2134 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2135 input->parent.touch = wl_seat_get_touch(seat);
2136 wl_touch_set_user_data(input->parent.touch, input);
2137 wl_touch_add_listener(input->parent.touch,
2138 &touch_listener, input);
2139 weston_seat_init_touch(&input->base);
2140 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2141 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2142 wl_touch_release(input->parent.touch);
2143 else
2144 wl_touch_destroy(input->parent.touch);
2145 input->parent.touch = NULL;
2146 weston_seat_release_touch(&input->base);
2147 }
Daniel Stone37816df2012-05-16 18:45:18 +01002148}
2149
Jonny Lamb497994a2014-08-12 14:58:26 +02002150static void
2151input_handle_name(void *data, struct wl_seat *seat,
2152 const char *name)
2153{
2154}
2155
Daniel Stone37816df2012-05-16 18:45:18 +01002156static const struct wl_seat_listener seat_listener = {
2157 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002158 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002159};
2160
2161static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002162display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002163{
2164 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002165 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002166
Peter Huttererf3d62272013-08-08 11:57:05 +10002167 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002168 if (input == NULL)
2169 return;
2170
Giulio Camuffo954f1832014-10-11 18:27:30 +03002171 weston_seat_init(&input->base, b->compositor, "default");
2172 input->backend = b;
2173 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002174 &wl_seat_interface, version);
2175 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002176 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002177
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002178 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2179 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002180
2181 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002182 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002183
2184 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2185 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002186}
2187
2188static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002189wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2190 int32_t x, int32_t y,
2191 int32_t physical_width, int32_t physical_height,
2192 int32_t subpixel, const char *make,
2193 const char *model, int32_t transform)
2194{
2195 struct wayland_parent_output *output = data;
2196
2197 output->x = x;
2198 output->y = y;
2199 output->physical.width = physical_width;
2200 output->physical.height = physical_height;
2201 output->physical.subpixel = subpixel;
2202
2203 free(output->physical.make);
2204 output->physical.make = strdup(make);
2205 free(output->physical.model);
2206 output->physical.model = strdup(model);
2207
2208 output->transform = transform;
2209}
2210
2211static struct weston_mode *
2212find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2213{
2214 struct weston_mode *mode;
2215
2216 wl_list_for_each(mode, list, link) {
2217 if (mode->width == width && mode->height == height &&
2218 mode->refresh == refresh)
2219 return mode;
2220 }
2221
2222 mode = zalloc(sizeof *mode);
2223 if (!mode)
2224 return NULL;
2225
2226 mode->width = width;
2227 mode->height = height;
2228 mode->refresh = refresh;
2229 wl_list_insert(list, &mode->link);
2230
2231 return mode;
2232}
2233
2234static void
2235wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2236 uint32_t flags, int32_t width, int32_t height,
2237 int32_t refresh)
2238{
2239 struct wayland_parent_output *output = data;
2240 struct weston_mode *mode;
2241
2242 if (output->output) {
2243 mode = find_mode(&output->output->base.mode_list,
2244 width, height, refresh);
2245 if (!mode)
2246 return;
2247 mode->flags = flags;
2248 /* Do a mode-switch on current mode change? */
2249 } else {
2250 mode = find_mode(&output->mode_list, width, height, refresh);
2251 if (!mode)
2252 return;
2253 mode->flags = flags;
2254 if (flags & WL_OUTPUT_MODE_CURRENT)
2255 output->current_mode = mode;
2256 if (flags & WL_OUTPUT_MODE_PREFERRED)
2257 output->preferred_mode = mode;
2258 }
2259}
2260
2261static const struct wl_output_listener output_listener = {
2262 wayland_parent_output_geometry,
2263 wayland_parent_output_mode
2264};
2265
2266static void
Pekka Paalanenb07de932017-10-19 12:03:06 +03002267output_sync_callback(void *data, struct wl_callback *callback, uint32_t unused)
2268{
2269 struct wayland_parent_output *output = data;
2270
2271 assert(output->sync_cb == callback);
2272 wl_callback_destroy(callback);
2273 output->sync_cb = NULL;
2274
2275 assert(output->backend->sprawl_across_outputs);
2276
2277 wayland_output_create_for_parent_output(output->backend, output);
2278}
2279
2280static const struct wl_callback_listener output_sync_listener = {
2281 output_sync_callback
2282};
2283
2284static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002285wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002286{
2287 struct wayland_parent_output *output;
2288
2289 output = zalloc(sizeof *output);
2290 if (!output)
2291 return;
2292
Pekka Paalanenb07de932017-10-19 12:03:06 +03002293 output->backend = b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002294 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002295 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002296 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002297 if (!output->global) {
2298 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002299 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002300 }
2301
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002302 wl_output_add_listener(output->global, &output_listener, output);
2303
2304 output->scale = 0;
2305 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2306 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2307 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002308 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002309
Giulio Camuffo954f1832014-10-11 18:27:30 +03002310 if (b->sprawl_across_outputs) {
Pekka Paalanenb07de932017-10-19 12:03:06 +03002311 output->sync_cb = wl_display_sync(b->parent.wl_display);
2312 wl_callback_add_listener(output->sync_cb,
2313 &output_sync_listener, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002314 }
2315}
2316
2317static void
2318wayland_parent_output_destroy(struct wayland_parent_output *output)
2319{
2320 struct weston_mode *mode, *next;
2321
Pekka Paalanenb07de932017-10-19 12:03:06 +03002322 if (output->sync_cb)
2323 wl_callback_destroy(output->sync_cb);
2324
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002325 if (output->output)
2326 wayland_output_destroy(&output->output->base);
2327
2328 wl_output_destroy(output->global);
2329 free(output->physical.make);
2330 free(output->physical.model);
2331
2332 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2333 wl_list_remove(&mode->link);
2334 free(mode);
2335 }
Pekka Paalanenf1322792017-10-19 10:26:27 +03002336
2337 wl_list_remove(&output->link);
2338 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002339}
2340
2341static void
Armin Krezović34476192016-11-21 18:42:42 +01002342xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2343{
2344 zxdg_shell_v6_pong(shell, serial);
2345}
2346
2347static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2348 xdg_shell_ping,
2349};
2350
2351static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002352registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2353 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002354{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002355 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002356
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002357 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002358 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002359 wl_registry_bind(registry, name,
Derek Foremandbfd2482017-05-19 10:42:07 -05002360 &wl_compositor_interface,
2361 MIN(version, 4));
Armin Krezović34476192016-11-21 18:42:42 +01002362 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2363 b->parent.xdg_shell =
2364 wl_registry_bind(registry, name,
2365 &zxdg_shell_v6_interface, 1);
2366 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2367 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002368 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002369 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002370 wl_registry_bind(registry, name,
2371 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002372 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002373 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002374 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002375 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002376 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002377 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002378 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002379 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002380 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002381 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002382 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002383 }
2384}
2385
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002386static void
2387registry_handle_global_remove(void *data, struct wl_registry *registry,
2388 uint32_t name)
2389{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 struct wayland_backend *b = data;
Pekka Paalanenf1322792017-10-19 10:26:27 +03002391 struct wayland_parent_output *output, *next;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002392
Pekka Paalanenf1322792017-10-19 10:26:27 +03002393 wl_list_for_each_safe(output, next, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002394 if (output->id == name)
2395 wayland_parent_output_destroy(output);
2396}
2397
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002398static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002399 registry_handle_global,
2400 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002401};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002402
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002403static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002404wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002405{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002406 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002407 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002408
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002409 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002410 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002411 return 0;
2412 }
2413
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002414 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002415 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002416 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002417 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002418
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002419 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002420 count = wl_display_dispatch_pending(b->parent.wl_display);
2421 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002422 }
2423
2424 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002425}
2426
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002427static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002428wayland_restore(struct weston_compositor *ec)
2429{
2430}
2431
2432static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002433wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002434{
Armin Krezović938dc522016-08-01 19:17:57 +02002435 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002436
Armin Krezović78895c52016-10-09 17:30:28 +02002437 wl_event_source_remove(b->parent.wl_source);
2438
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002439 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002440
Giulio Camuffo954f1832014-10-11 18:27:30 +03002441 if (b->parent.shm)
2442 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002443
Armin Krezović34476192016-11-21 18:42:42 +01002444 if (b->parent.xdg_shell)
2445 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2446
2447 if (b->parent.shell)
2448 wl_shell_destroy(b->parent.shell);
2449
Armin Krezović78895c52016-10-09 17:30:28 +02002450 if (b->parent.fshell)
2451 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2452
2453 if (b->parent.compositor)
2454 wl_compositor_destroy(b->parent.compositor);
2455
Armin Krezović78895c52016-10-09 17:30:28 +02002456 if (b->theme)
2457 theme_destroy(b->theme);
2458
2459 if (b->frame_device)
2460 cairo_device_destroy(b->frame_device);
2461
2462 wl_cursor_theme_destroy(b->cursor_theme);
2463
Daniel Stone698f9bf2016-11-24 15:31:33 +00002464 wl_registry_destroy(b->parent.registry);
2465 wl_display_flush(b->parent.wl_display);
2466 wl_display_disconnect(b->parent.wl_display);
2467
Giulio Camuffo954f1832014-10-11 18:27:30 +03002468 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002469}
2470
Jason Ekstrand7744f712013-10-27 22:24:55 -05002471static const char *left_ptrs[] = {
2472 "left_ptr",
2473 "default",
2474 "top_left_arrow",
2475 "left-arrow"
2476};
2477
2478static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002479create_cursor(struct wayland_backend *b,
2480 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002481{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002482 unsigned int i;
2483
Benoit Gschwind244ff792016-04-28 20:33:11 +02002484 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2485 config->cursor_size,
2486 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002487 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002488 fprintf(stderr, "could not load cursor theme\n");
2489 return;
2490 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002491
Giulio Camuffo954f1832014-10-11 18:27:30 +03002492 b->cursor = NULL;
2493 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2494 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002495 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002496 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002497 fprintf(stderr, "could not load left cursor\n");
2498 return;
2499 }
2500}
2501
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002502static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002503fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2504 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002505{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002506 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002507 struct wayland_input *input = NULL;
2508
Giulio Camuffo954f1832014-10-11 18:27:30 +03002509 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002510 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002511 break;
2512
2513 if (!input || !input->output)
2514 return;
2515
2516 if (input->output->frame)
2517 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2518 else
2519 wayland_output_set_windowed(input->output);
2520
2521 weston_output_schedule_repaint(&input->output->base);
2522}
2523
Giulio Camuffo954f1832014-10-11 18:27:30 +03002524static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002525wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002526 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002527{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002528 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002529 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002530 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002531
Giulio Camuffo954f1832014-10-11 18:27:30 +03002532 b = zalloc(sizeof *b);
2533 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002534 return NULL;
2535
Giulio Camuffo954f1832014-10-11 18:27:30 +03002536 b->compositor = compositor;
Pekka Paalanen7da9a382017-08-30 11:29:49 +03002537 compositor->backend = &b->base;
2538
Giulio Camuffo954f1832014-10-11 18:27:30 +03002539 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002540 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002541
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002542 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002543 if (b->parent.wl_display == NULL) {
Pekka Paalanen99c92e72017-10-23 12:03:38 +03002544 weston_log("Error: Failed to connect to parent Wayland compositor: %m\n");
2545 weston_log_continue(STAMP_SPACE "display option: %s, WAYLAND_DISPLAY=%s\n",
2546 new_config->display_name ?: "(none)",
2547 getenv("WAYLAND_DISPLAY") ?: "(not set)");
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}