blob: d61f08c1e47ab3b8fff785a43a6ef3e9fde68f21 [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
Armin Krezović34476192016-11-21 18:42:42 +0100876 if (output->parent.shell_surface)
877 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600878
879 return 0;
880}
881
882static void
883wayland_output_set_fullscreen(struct wayland_output *output,
884 enum wl_shell_surface_fullscreen_method method,
885 uint32_t framerate, struct wl_output *target)
886{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300887 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200888 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500889
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600890 if (output->frame) {
891 frame_destroy(output->frame);
892 output->frame = NULL;
893 }
894
895 wayland_output_resize_surface(output);
896
Armin Krezović34476192016-11-21 18:42:42 +0100897 if (output->parent.xdg_toplevel) {
898 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
899 } else if (output->parent.shell_surface) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500900 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
901 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300902 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800903 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
904 output->parent.surface,
905 method, target);
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
Armin Krezović174448a2016-09-30 14:11:09 +02001379 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1380 goto out;
1381
1382 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001383 output->parent.output = poutput->global;
1384
1385 output->base.make = poutput->physical.make;
1386 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001387
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001388 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1389 wl_list_init(&poutput->mode_list);
1390
Armin Krezović174448a2016-09-30 14:11:09 +02001391 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001392
Armin Krezović174448a2016-09-30 14:11:09 +02001393 return 0;
1394
1395out:
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001396 weston_output_release(&output->base);
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001397 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +02001398 free(output);
1399
1400 return -1;
1401}
1402
1403static int
1404wayland_output_create_fullscreen(struct wayland_backend *b)
1405{
Sergi Granell7fecb432017-03-24 20:48:02 +01001406 struct wayland_output *output;
Armin Krezović174448a2016-09-30 14:11:09 +02001407 int width = 0, height = 0;
1408
Pekka Paalanen1580be62017-08-11 16:05:41 +03001409 output = wayland_output_create_common(b->compositor, "wayland-fullscreen");
Sergi Granell7fecb432017-03-24 20:48:02 +01001410 if (!output)
1411 return -1;
1412
Armin Krezović174448a2016-09-30 14:11:09 +02001413 output->base.scale = 1;
1414 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1415
1416 if (wayland_backend_create_output_surface(output) < 0)
1417 goto err_surface;
1418
1419 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001420 if (b->parent.xdg_shell || b->parent.shell) {
1421 if (output->parent.xdg_toplevel)
1422 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1423 output->parent.output);
1424 else if (output->parent.shell_surface)
1425 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1426 0, 0, NULL);
1427
Armin Krezović174448a2016-09-30 14:11:09 +02001428 wl_display_roundtrip(b->parent.wl_display);
1429
1430 width = output->parent.configure_width;
1431 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001432 }
1433
Armin Krezović174448a2016-09-30 14:11:09 +02001434 if (wayland_output_set_size(&output->base, width, height) < 0)
1435 goto err_set_size;
1436
1437 weston_compositor_add_pending_output(&output->base, b->compositor);
1438
1439 return 0;
1440
1441err_set_size:
1442 wayland_backend_destroy_output_surface(output);
1443err_surface:
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03001444 weston_output_release(&output->base);
Sergi Granell2dcbb8d2017-03-24 20:48:01 +01001445 free(output->title);
Armin Krezović174448a2016-09-30 14:11:09 +02001446 free(output);
1447
1448 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001449}
1450
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001451static void
1452shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1453 uint32_t serial)
1454{
1455 wl_shell_surface_pong(shell_surface, serial);
1456}
1457
1458static void
1459shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1460 uint32_t edges, int32_t width, int32_t height)
1461{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001462 struct wayland_output *output = data;
1463
1464 output->parent.configure_width = width;
1465 output->parent.configure_height = height;
1466
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001467 /* FIXME: implement resizing */
1468}
1469
1470static void
1471shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1472{
1473}
1474
1475static const struct wl_shell_surface_listener shell_surface_listener = {
1476 shell_surface_ping,
1477 shell_surface_configure,
1478 shell_surface_popup_done
1479};
1480
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001481/* Events received from the wayland-server this compositor is client of: */
1482
Jason Ekstrand7744f712013-10-27 22:24:55 -05001483/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001484static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001485input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001486{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001487
Jason Ekstrand7744f712013-10-27 22:24:55 -05001488 struct wl_buffer *buffer;
1489 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001490
Giulio Camuffo954f1832014-10-11 18:27:30 +03001491 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001492 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001493
Giulio Camuffo954f1832014-10-11 18:27:30 +03001494 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001495 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001496 if (!buffer)
1497 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001498
1499 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1500 input->parent.cursor.surface,
1501 image->hotspot_x, image->hotspot_y);
1502
1503 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1504 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1505 image->width, image->height);
1506 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001507}
1508
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001509static void
Daniel Stone37816df2012-05-16 18:45:18 +01001510input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1511 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001512 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001513{
1514 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001515 int32_t fx, fy;
1516 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001517 double x, y;
1518
1519 x = wl_fixed_to_double(fixed_x);
1520 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001521
Daniel Stone50692802012-06-22 13:21:41 +01001522 /* XXX: If we get a modifier event immediately before the focus,
1523 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001524 input->enter_serial = serial;
1525 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001526
1527 if (input->output->frame) {
1528 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001529 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001530 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001531 x -= fx;
1532 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001533
1534 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1535 weston_output_schedule_repaint(&input->output->base);
1536 } else {
1537 location = THEME_LOCATION_CLIENT_AREA;
1538 }
1539
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001540 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001541
1542 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001543 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001544 notify_pointer_focus(&input->base, &input->output->base, x, y);
1545 wl_pointer_set_cursor(input->parent.pointer,
1546 input->enter_serial, NULL, 0, 0);
1547 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001548 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001549 notify_pointer_focus(&input->base, NULL, 0, 0);
1550 input_set_cursor(input);
1551 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001552}
1553
1554static void
Daniel Stone37816df2012-05-16 18:45:18 +01001555input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1556 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001557{
1558 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001559
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001560 if (!input->output)
1561 return;
1562
Jason Ekstrand7744f712013-10-27 22:24:55 -05001563 if (input->output->frame) {
1564 frame_pointer_leave(input->output->frame, input);
1565
1566 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1567 weston_output_schedule_repaint(&input->output->base);
1568 }
1569
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001570 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001571 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001572 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001573}
1574
1575static void
Daniel Stone37816df2012-05-16 18:45:18 +01001576input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001577 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001578{
1579 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001580 int32_t fx, fy;
1581 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001582 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001583 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001584
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001585 if (!input->output)
1586 return;
1587
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001588 x = wl_fixed_to_double(fixed_x);
1589 y = wl_fixed_to_double(fixed_y);
1590
Jason Ekstrand7744f712013-10-27 22:24:55 -05001591 if (input->output->frame) {
1592 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001593 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001594 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001595 x -= fx;
1596 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001597
1598 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1599 weston_output_schedule_repaint(&input->output->base);
1600 } else {
1601 location = THEME_LOCATION_CLIENT_AREA;
1602 }
1603
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001604 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001605
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001606 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001607 input_set_cursor(input);
1608 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001609 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001610 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001611 } else if (!input->has_focus &&
1612 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001613 wl_pointer_set_cursor(input->parent.pointer,
1614 input->enter_serial, NULL, 0, 0);
1615 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001616 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001617 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001618 }
1619
Peter Hutterer87743e92016-01-18 16:38:22 +10001620 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001621 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001622 want_frame = true;
1623 }
1624
1625 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1626 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001627}
1628
1629static void
1630input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001631 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001632 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001633{
1634 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001635 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001636
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001637 if (!input->output)
1638 return;
1639
Jason Ekstrand7744f712013-10-27 22:24:55 -05001640 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001641 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001642 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001643
1644 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001645 if (input->output->parent.xdg_toplevel)
1646 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001647 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001648 else if (input->output->parent.shell_surface)
1649 wl_shell_surface_move(input->output->parent.shell_surface,
1650 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001651 frame_status_clear(input->output->frame,
1652 FRAME_STATUS_MOVE);
1653 return;
1654 }
1655
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001656 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1657 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001658 input->output = NULL;
1659 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001660
Giulio Camuffo954f1832014-10-11 18:27:30 +03001661 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001662 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001663
1664 return;
1665 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001666
1667 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1668 weston_output_schedule_repaint(&input->output->base);
1669 } else {
1670 location = THEME_LOCATION_CLIENT_AREA;
1671 }
1672
Peter Hutterer87743e92016-01-18 16:38:22 +10001673 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001674 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001675 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1676 notify_pointer_frame(&input->base);
1677 }
Daniel Stone37816df2012-05-16 18:45:18 +01001678}
1679
1680static void
1681input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001682 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001683{
1684 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001685 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001686
Peter Hutterer89b6a492016-01-18 15:58:17 +10001687 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001688 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001689
Peter Hutterer87743e92016-01-18 16:38:22 +10001690 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1691 input->vert.has_discrete) {
1692 weston_event.has_discrete = true;
1693 weston_event.discrete = input->vert.discrete;
1694 input->vert.has_discrete = false;
1695 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1696 input->horiz.has_discrete) {
1697 weston_event.has_discrete = true;
1698 weston_event.discrete = input->horiz.discrete;
1699 input->horiz.has_discrete = false;
1700 }
1701
Peter Hutterer89b6a492016-01-18 15:58:17 +10001702 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001703
1704 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1705 notify_pointer_frame(&input->base);
1706}
1707
1708static void
1709input_handle_frame(void *data, struct wl_pointer *pointer)
1710{
1711 struct wayland_input *input = data;
1712
1713 notify_pointer_frame(&input->base);
1714}
1715
1716static void
1717input_handle_axis_source(void *data, struct wl_pointer *pointer,
1718 uint32_t source)
1719{
1720 struct wayland_input *input = data;
1721
1722 notify_axis_source(&input->base, source);
1723}
1724
1725static void
1726input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1727 uint32_t time, uint32_t axis)
1728{
1729 struct wayland_input *input = data;
1730 struct weston_pointer_axis_event weston_event;
1731
1732 weston_event.axis = axis;
1733 weston_event.value = 0;
1734
1735 notify_axis(&input->base, time, &weston_event);
1736}
1737
1738static void
1739input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1740 uint32_t axis, int32_t discrete)
1741{
1742 struct wayland_input *input = data;
1743
1744 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1745 input->vert.has_discrete = true;
1746 input->vert.discrete = discrete;
1747 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1748 input->horiz.has_discrete = true;
1749 input->horiz.discrete = discrete;
1750 }
Daniel Stone37816df2012-05-16 18:45:18 +01001751}
1752
1753static const struct wl_pointer_listener pointer_listener = {
1754 input_handle_pointer_enter,
1755 input_handle_pointer_leave,
1756 input_handle_motion,
1757 input_handle_button,
1758 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001759 input_handle_frame,
1760 input_handle_axis_source,
1761 input_handle_axis_stop,
1762 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001763};
1764
1765static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001766input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1767 int fd, uint32_t size)
1768{
1769 struct wayland_input *input = data;
1770 struct xkb_keymap *keymap;
1771 char *map_str;
1772
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001773 if (!data) {
1774 close(fd);
1775 return;
1776 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001777
1778 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1779 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1780 if (map_str == MAP_FAILED) {
1781 weston_log("mmap failed: %m\n");
1782 goto error;
1783 }
1784
Giulio Camuffo954f1832014-10-11 18:27:30 +03001785 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001786 map_str,
1787 XKB_KEYMAP_FORMAT_TEXT_V1,
1788 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001789 munmap(map_str, size);
1790
1791 if (!keymap) {
1792 weston_log("failed to compile keymap\n");
1793 goto error;
1794 }
1795
1796 input->keyboard_state_update = STATE_UPDATE_NONE;
1797 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1798 weston_log("No keymap provided; falling back to defalt\n");
1799 keymap = NULL;
1800 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1801 } else {
1802 weston_log("Invalid keymap\n");
1803 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001804 }
1805
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001806 close(fd);
1807
Derek Foreman1281a362015-07-31 16:55:32 -05001808 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001809 weston_seat_update_keymap(&input->base, keymap);
1810 else
1811 weston_seat_init_keyboard(&input->base, keymap);
1812
Ran Benitac9c74152014-08-19 23:59:52 +03001813 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001814
1815 return;
1816
1817error:
1818 wl_keyboard_release(input->parent.keyboard);
1819 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001820}
1821
1822static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001823input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001824 struct wl_keyboard *keyboard,
1825 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001826 struct wl_surface *surface,
1827 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001828{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001829 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001830 struct wayland_output *focus;
1831
1832 focus = input->keyboard_focus;
1833 if (focus) {
1834 /* This shouldn't happen */
1835 focus->keyboard_count--;
1836 if (!focus->keyboard_count && focus->frame)
1837 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1838 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1839 weston_output_schedule_repaint(&focus->base);
1840 }
1841
1842 input->keyboard_focus = wl_surface_get_user_data(surface);
1843 input->keyboard_focus->keyboard_count++;
1844
1845 focus = input->keyboard_focus;
1846 if (focus->frame) {
1847 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1848 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1849 weston_output_schedule_repaint(&focus->base);
1850 }
1851
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001852
Daniel Stone50692802012-06-22 13:21:41 +01001853 /* XXX: If we get a modifier event immediately before the focus,
1854 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001855 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001856 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001857}
1858
1859static void
1860input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001861 struct wl_keyboard *keyboard,
1862 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001863 struct wl_surface *surface)
1864{
1865 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001866 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001867
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001868 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001869
1870 focus = input->keyboard_focus;
1871 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001872 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001873
1874 focus->keyboard_count--;
1875 if (!focus->keyboard_count && focus->frame) {
1876 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1877 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1878 weston_output_schedule_repaint(&focus->base);
1879 }
1880
1881 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001882}
1883
Daniel Stone37816df2012-05-16 18:45:18 +01001884static void
1885input_handle_key(void *data, struct wl_keyboard *keyboard,
1886 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1887{
1888 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001889
Daniel Stone50692802012-06-22 13:21:41 +01001890 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001891 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001892 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001893 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001894 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001895}
1896
Daniel Stone351eb612012-05-31 15:27:47 -04001897static void
Derek Foreman1281a362015-07-31 16:55:32 -05001898input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001899 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001900 uint32_t mods_latched, uint32_t mods_locked,
1901 uint32_t group)
1902{
Derek Foreman1281a362015-07-31 16:55:32 -05001903 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001904 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001905 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001906 uint32_t serial_out;
1907
1908 /* If we get a key event followed by a modifier event with the
1909 * same serial number, then we try to preserve those semantics by
1910 * reusing the same serial number on the way out too. */
1911 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001912 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001913 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001914 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001915
Derek Foreman1281a362015-07-31 16:55:32 -05001916 keyboard = weston_seat_get_keyboard(&input->base);
1917 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001918 mods_depressed, mods_latched,
1919 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001920 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001921}
1922
Jonny Lamb497994a2014-08-12 14:58:26 +02001923static void
1924input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1925 int32_t rate, int32_t delay)
1926{
1927 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001928 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001929
Giulio Camuffo954f1832014-10-11 18:27:30 +03001930 b->compositor->kb_repeat_rate = rate;
1931 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001932}
1933
Daniel Stone37816df2012-05-16 18:45:18 +01001934static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001935 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001936 input_handle_keyboard_enter,
1937 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001938 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001939 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001940 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001941};
1942
1943static void
Derek Foreman748c6952015-11-06 15:56:10 -06001944input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1945 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001946 struct wl_surface *surface, int32_t id,
1947 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001948{
1949 struct wayland_input *input = data;
1950 struct wayland_output *output;
1951 enum theme_location location;
1952 bool first_touch;
1953 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001954 double x, y;
1955
1956 x = wl_fixed_to_double(fixed_x);
1957 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001958
1959 first_touch = (input->touch_points == 0);
1960 input->touch_points++;
1961
1962 input->touch_focus = wl_surface_get_user_data(surface);
1963 output = input->touch_focus;
1964 if (!first_touch && !input->touch_active)
1965 return;
1966
1967 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001968 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001969
1970 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001971 x -= fx;
1972 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001973
1974 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1975 weston_output_schedule_repaint(&output->base);
1976
1977 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1978 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001979 if (output->parent.xdg_toplevel)
1980 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1981 input->parent.seat, serial);
1982 else if (output->parent.shell_surface)
1983 wl_shell_surface_move(output->parent.shell_surface,
1984 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001985 frame_status_clear(output->frame,
1986 FRAME_STATUS_MOVE);
1987 return;
1988 }
1989
1990 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1991 return;
1992 }
1993
1994 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1995
1996 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1997 input->touch_active = true;
1998}
1999
2000static void
2001input_handle_touch_up(void *data, struct wl_touch *wl_touch,
2002 uint32_t serial, uint32_t time, int32_t id)
2003{
2004 struct wayland_input *input = data;
2005 struct wayland_output *output = input->touch_focus;
2006 bool active = input->touch_active;
2007
2008 input->touch_points--;
2009 if (input->touch_points == 0) {
2010 input->touch_focus = NULL;
2011 input->touch_active = false;
2012 }
2013
2014 if (!output)
2015 return;
2016
2017 if (output->frame) {
2018 frame_touch_up(output->frame, input, id);
2019
2020 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
2021 wayland_output_destroy(&output->base);
2022 input->touch_focus = NULL;
2023 input->keyboard_focus = NULL;
2024 if (wl_list_empty(&input->backend->compositor->output_list))
2025 weston_compositor_exit(input->backend->compositor);
2026
2027 return;
2028 }
2029 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
2030 weston_output_schedule_repaint(&output->base);
2031 }
2032
2033 if (active)
2034 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
2035}
2036
2037static void
2038input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002039 uint32_t time, int32_t id,
2040 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06002041{
2042 struct wayland_input *input = data;
2043 struct wayland_output *output = input->touch_focus;
2044 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002045 double x, y;
2046
2047 x = wl_fixed_to_double(fixed_x);
2048 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06002049
2050 if (!output || !input->touch_active)
2051 return;
2052
2053 if (output->frame) {
2054 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002055 x -= fx;
2056 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002057 }
2058
2059 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2060
2061 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2062}
2063
2064static void
2065input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2066{
2067 struct wayland_input *input = data;
2068
2069 if (!input->touch_focus || !input->touch_active)
2070 return;
2071
2072 notify_touch_frame(&input->base);
2073}
2074
2075static void
2076input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2077{
2078 struct wayland_input *input = data;
2079
2080 if (!input->touch_focus || !input->touch_active)
2081 return;
2082
2083 notify_touch_cancel(&input->base);
2084}
2085
2086static const struct wl_touch_listener touch_listener = {
2087 input_handle_touch_down,
2088 input_handle_touch_up,
2089 input_handle_touch_motion,
2090 input_handle_touch_frame,
2091 input_handle_touch_cancel,
2092};
2093
2094
2095static void
Daniel Stone37816df2012-05-16 18:45:18 +01002096input_handle_capabilities(void *data, struct wl_seat *seat,
2097 enum wl_seat_capability caps)
2098{
2099 struct wayland_input *input = data;
2100
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002101 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2102 input->parent.pointer = wl_seat_get_pointer(seat);
2103 wl_pointer_set_user_data(input->parent.pointer, input);
2104 wl_pointer_add_listener(input->parent.pointer,
2105 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002106 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002107 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002108 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2109 wl_pointer_release(input->parent.pointer);
2110 else
2111 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002112 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002113 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002114 }
2115
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002116 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2117 input->parent.keyboard = wl_seat_get_keyboard(seat);
2118 wl_keyboard_set_user_data(input->parent.keyboard, input);
2119 wl_keyboard_add_listener(input->parent.keyboard,
2120 &keyboard_listener, input);
2121 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002122 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2123 wl_keyboard_release(input->parent.keyboard);
2124 else
2125 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002126 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002127 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002128 }
Derek Foreman748c6952015-11-06 15:56:10 -06002129
2130 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2131 input->parent.touch = wl_seat_get_touch(seat);
2132 wl_touch_set_user_data(input->parent.touch, input);
2133 wl_touch_add_listener(input->parent.touch,
2134 &touch_listener, input);
2135 weston_seat_init_touch(&input->base);
2136 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2137 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2138 wl_touch_release(input->parent.touch);
2139 else
2140 wl_touch_destroy(input->parent.touch);
2141 input->parent.touch = NULL;
2142 weston_seat_release_touch(&input->base);
2143 }
Daniel Stone37816df2012-05-16 18:45:18 +01002144}
2145
Jonny Lamb497994a2014-08-12 14:58:26 +02002146static void
2147input_handle_name(void *data, struct wl_seat *seat,
2148 const char *name)
2149{
2150}
2151
Daniel Stone37816df2012-05-16 18:45:18 +01002152static const struct wl_seat_listener seat_listener = {
2153 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002154 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002155};
2156
2157static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002158display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002159{
2160 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002161 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002162
Peter Huttererf3d62272013-08-08 11:57:05 +10002163 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002164 if (input == NULL)
2165 return;
2166
Giulio Camuffo954f1832014-10-11 18:27:30 +03002167 weston_seat_init(&input->base, b->compositor, "default");
2168 input->backend = b;
2169 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002170 &wl_seat_interface, version);
2171 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002172 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002173
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002174 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2175 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002176
2177 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002178 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002179
2180 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2181 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002182}
2183
2184static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002185wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2186 int32_t x, int32_t y,
2187 int32_t physical_width, int32_t physical_height,
2188 int32_t subpixel, const char *make,
2189 const char *model, int32_t transform)
2190{
2191 struct wayland_parent_output *output = data;
2192
2193 output->x = x;
2194 output->y = y;
2195 output->physical.width = physical_width;
2196 output->physical.height = physical_height;
2197 output->physical.subpixel = subpixel;
2198
2199 free(output->physical.make);
2200 output->physical.make = strdup(make);
2201 free(output->physical.model);
2202 output->physical.model = strdup(model);
2203
2204 output->transform = transform;
2205}
2206
2207static struct weston_mode *
2208find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2209{
2210 struct weston_mode *mode;
2211
2212 wl_list_for_each(mode, list, link) {
2213 if (mode->width == width && mode->height == height &&
2214 mode->refresh == refresh)
2215 return mode;
2216 }
2217
2218 mode = zalloc(sizeof *mode);
2219 if (!mode)
2220 return NULL;
2221
2222 mode->width = width;
2223 mode->height = height;
2224 mode->refresh = refresh;
2225 wl_list_insert(list, &mode->link);
2226
2227 return mode;
2228}
2229
2230static void
2231wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2232 uint32_t flags, int32_t width, int32_t height,
2233 int32_t refresh)
2234{
2235 struct wayland_parent_output *output = data;
2236 struct weston_mode *mode;
2237
2238 if (output->output) {
2239 mode = find_mode(&output->output->base.mode_list,
2240 width, height, refresh);
2241 if (!mode)
2242 return;
2243 mode->flags = flags;
2244 /* Do a mode-switch on current mode change? */
2245 } else {
2246 mode = find_mode(&output->mode_list, width, height, refresh);
2247 if (!mode)
2248 return;
2249 mode->flags = flags;
2250 if (flags & WL_OUTPUT_MODE_CURRENT)
2251 output->current_mode = mode;
2252 if (flags & WL_OUTPUT_MODE_PREFERRED)
2253 output->preferred_mode = mode;
2254 }
2255}
2256
2257static const struct wl_output_listener output_listener = {
2258 wayland_parent_output_geometry,
2259 wayland_parent_output_mode
2260};
2261
2262static void
Pekka Paalanenb07de932017-10-19 12:03:06 +03002263output_sync_callback(void *data, struct wl_callback *callback, uint32_t unused)
2264{
2265 struct wayland_parent_output *output = data;
2266
2267 assert(output->sync_cb == callback);
2268 wl_callback_destroy(callback);
2269 output->sync_cb = NULL;
2270
2271 assert(output->backend->sprawl_across_outputs);
2272
2273 wayland_output_create_for_parent_output(output->backend, output);
2274}
2275
2276static const struct wl_callback_listener output_sync_listener = {
2277 output_sync_callback
2278};
2279
2280static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002281wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002282{
2283 struct wayland_parent_output *output;
2284
2285 output = zalloc(sizeof *output);
2286 if (!output)
2287 return;
2288
Pekka Paalanenb07de932017-10-19 12:03:06 +03002289 output->backend = b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002290 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002291 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002292 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002293 if (!output->global) {
2294 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002295 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002296 }
2297
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002298 wl_output_add_listener(output->global, &output_listener, output);
2299
2300 output->scale = 0;
2301 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2302 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2303 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002304 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002305
Giulio Camuffo954f1832014-10-11 18:27:30 +03002306 if (b->sprawl_across_outputs) {
Pekka Paalanenb07de932017-10-19 12:03:06 +03002307 output->sync_cb = wl_display_sync(b->parent.wl_display);
2308 wl_callback_add_listener(output->sync_cb,
2309 &output_sync_listener, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002310 }
2311}
2312
2313static void
2314wayland_parent_output_destroy(struct wayland_parent_output *output)
2315{
2316 struct weston_mode *mode, *next;
2317
Pekka Paalanenb07de932017-10-19 12:03:06 +03002318 if (output->sync_cb)
2319 wl_callback_destroy(output->sync_cb);
2320
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002321 if (output->output)
2322 wayland_output_destroy(&output->output->base);
2323
2324 wl_output_destroy(output->global);
2325 free(output->physical.make);
2326 free(output->physical.model);
2327
2328 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2329 wl_list_remove(&mode->link);
2330 free(mode);
2331 }
2332}
2333
2334static void
Armin Krezović34476192016-11-21 18:42:42 +01002335xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2336{
2337 zxdg_shell_v6_pong(shell, serial);
2338}
2339
2340static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2341 xdg_shell_ping,
2342};
2343
2344static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002345registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2346 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002347{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002348 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002349
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002350 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002351 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002352 wl_registry_bind(registry, name,
Derek Foremandbfd2482017-05-19 10:42:07 -05002353 &wl_compositor_interface,
2354 MIN(version, 4));
Armin Krezović34476192016-11-21 18:42:42 +01002355 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2356 b->parent.xdg_shell =
2357 wl_registry_bind(registry, name,
2358 &zxdg_shell_v6_interface, 1);
2359 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2360 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002361 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002362 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002363 wl_registry_bind(registry, name,
2364 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002365 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002366 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002367 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002368 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002369 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002370 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002371 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002372 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002373 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002374 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002375 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002376 }
2377}
2378
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002379static void
2380registry_handle_global_remove(void *data, struct wl_registry *registry,
2381 uint32_t name)
2382{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002383 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002384 struct wayland_parent_output *output;
2385
Giulio Camuffo954f1832014-10-11 18:27:30 +03002386 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002387 if (output->id == name)
2388 wayland_parent_output_destroy(output);
2389}
2390
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002391static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002392 registry_handle_global,
2393 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002394};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002395
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002396static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002397wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002398{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002399 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002400 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002401
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002402 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002403 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002404 return 0;
2405 }
2406
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002407 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002408 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002409 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002410 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002411
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002412 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002413 count = wl_display_dispatch_pending(b->parent.wl_display);
2414 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002415 }
2416
2417 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002418}
2419
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002420static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002421wayland_restore(struct weston_compositor *ec)
2422{
2423}
2424
2425static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002426wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002427{
Armin Krezović938dc522016-08-01 19:17:57 +02002428 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002429
Armin Krezović78895c52016-10-09 17:30:28 +02002430 wl_event_source_remove(b->parent.wl_source);
2431
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002432 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002433
Giulio Camuffo954f1832014-10-11 18:27:30 +03002434 if (b->parent.shm)
2435 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002436
Armin Krezović34476192016-11-21 18:42:42 +01002437 if (b->parent.xdg_shell)
2438 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2439
2440 if (b->parent.shell)
2441 wl_shell_destroy(b->parent.shell);
2442
Armin Krezović78895c52016-10-09 17:30:28 +02002443 if (b->parent.fshell)
2444 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2445
2446 if (b->parent.compositor)
2447 wl_compositor_destroy(b->parent.compositor);
2448
Armin Krezović78895c52016-10-09 17:30:28 +02002449 if (b->theme)
2450 theme_destroy(b->theme);
2451
2452 if (b->frame_device)
2453 cairo_device_destroy(b->frame_device);
2454
2455 wl_cursor_theme_destroy(b->cursor_theme);
2456
Daniel Stone698f9bf2016-11-24 15:31:33 +00002457 wl_registry_destroy(b->parent.registry);
2458 wl_display_flush(b->parent.wl_display);
2459 wl_display_disconnect(b->parent.wl_display);
2460
Giulio Camuffo954f1832014-10-11 18:27:30 +03002461 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002462}
2463
Jason Ekstrand7744f712013-10-27 22:24:55 -05002464static const char *left_ptrs[] = {
2465 "left_ptr",
2466 "default",
2467 "top_left_arrow",
2468 "left-arrow"
2469};
2470
2471static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002472create_cursor(struct wayland_backend *b,
2473 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002474{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002475 unsigned int i;
2476
Benoit Gschwind244ff792016-04-28 20:33:11 +02002477 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2478 config->cursor_size,
2479 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002480 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002481 fprintf(stderr, "could not load cursor theme\n");
2482 return;
2483 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002484
Giulio Camuffo954f1832014-10-11 18:27:30 +03002485 b->cursor = NULL;
2486 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2487 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002488 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002489 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002490 fprintf(stderr, "could not load left cursor\n");
2491 return;
2492 }
2493}
2494
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002495static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002496fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2497 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002498{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002499 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002500 struct wayland_input *input = NULL;
2501
Giulio Camuffo954f1832014-10-11 18:27:30 +03002502 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002503 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002504 break;
2505
2506 if (!input || !input->output)
2507 return;
2508
2509 if (input->output->frame)
2510 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2511 else
2512 wayland_output_set_windowed(input->output);
2513
2514 weston_output_schedule_repaint(&input->output->base);
2515}
2516
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002518wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002519 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002520{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002521 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002522 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002523 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002524
Giulio Camuffo954f1832014-10-11 18:27:30 +03002525 b = zalloc(sizeof *b);
2526 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002527 return NULL;
2528
Giulio Camuffo954f1832014-10-11 18:27:30 +03002529 b->compositor = compositor;
Pekka Paalanen7da9a382017-08-30 11:29:49 +03002530 compositor->backend = &b->base;
2531
Giulio Camuffo954f1832014-10-11 18:27:30 +03002532 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002533 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002534
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002535 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002536 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002537 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002538 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002539 }
2540
Giulio Camuffo954f1832014-10-11 18:27:30 +03002541 wl_list_init(&b->parent.output_list);
2542 wl_list_init(&b->input_list);
2543 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2544 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2545 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002546
Benoit Gschwind244ff792016-04-28 20:33:11 +02002547 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002548
Armin Krezovićb08e1a52016-12-09 22:58:27 +01002549#ifdef ENABLE_EGL
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002550 b->use_pixman = new_config->use_pixman;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01002551#else
2552 b->use_pixman = true;
2553#endif
Armin Krezović174448a2016-09-30 14:11:09 +02002554 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002555
Giulio Camuffo954f1832014-10-11 18:27:30 +03002556 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002557 gl_renderer = weston_load_module("gl-renderer.so",
2558 "gl_renderer_interface");
2559 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002560 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002561 }
2562
Giulio Camuffo954f1832014-10-11 18:27:30 +03002563 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002564 if (gl_renderer->display_create(compositor,
2565 EGL_PLATFORM_WAYLAND_KHR,
2566 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002567 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002568 gl_renderer->alpha_attribs,
2569 NULL,
2570 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002571 weston_log("Failed to initialize the GL renderer; "
2572 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002573 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002574 }
2575 }
2576
Giulio Camuffo954f1832014-10-11 18:27:30 +03002577 if (b->use_pixman) {
2578 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002579 weston_log("Failed to initialize pixman renderer\n");
2580 goto err_display;
2581 }
2582 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002583
Giulio Camuffo954f1832014-10-11 18:27:30 +03002584 b->base.destroy = wayland_destroy;
2585 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002586
Giulio Camuffo954f1832014-10-11 18:27:30 +03002587 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002588
Giulio Camuffo954f1832014-10-11 18:27:30 +03002589 fd = wl_display_get_fd(b->parent.wl_display);
2590 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002591 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002592 wayland_backend_handle_event, b);
2593 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002594 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002595
Giulio Camuffo954f1832014-10-11 18:27:30 +03002596 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002597
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002598 if (compositor->renderer->import_dmabuf) {
2599 if (linux_dmabuf_setup(compositor) < 0)
2600 weston_log("Error: initializing dmabuf "
2601 "support failed.\n");
2602 }
2603
Giulio Camuffo954f1832014-10-11 18:27:30 +03002604 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002605err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002606 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002607err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002608 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002609 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002610 return NULL;
2611}
2612
2613static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002615{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002616 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002617
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618 if (b->theme)
2619 theme_destroy(b->theme);
2620 if (b->frame_device)
2621 cairo_device_destroy(b->frame_device);
2622 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002623
Giulio Camuffo954f1832014-10-11 18:27:30 +03002624 weston_compositor_shutdown(b->compositor);
2625 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002626}
2627
Armin Krezović174448a2016-09-30 14:11:09 +02002628static const struct weston_windowed_output_api windowed_api = {
2629 wayland_output_set_size,
2630 wayland_output_create,
2631};
2632
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002633static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002634config_init_to_defaults(struct weston_wayland_backend_config *config)
2635{
2636}
2637
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002638WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01002639weston_backend_init(struct weston_compositor *compositor,
2640 struct weston_backend_config *config_base)
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002641{
2642 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002643 struct wayland_parent_output *poutput;
2644 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002645 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002646
Benoit Gschwinde091b452016-05-10 22:47:48 +02002647 if (config_base == NULL ||
2648 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2649 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2650 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002651 return -1;
2652 }
2653
2654 config_init_to_defaults(&new_config);
2655 memcpy(&new_config, config_base, config_base->struct_size);
2656
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002657 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002658
Giulio Camuffo954f1832014-10-11 18:27:30 +03002659 if (!b)
2660 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002661
Benoit Gschwind37a68072016-04-28 20:33:08 +02002662 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002663 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002664 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002665
Giulio Camuffo954f1832014-10-11 18:27:30 +03002666 wl_list_for_each(poutput, &b->parent.output_list, link)
2667 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002668
Giulio Camuffo954f1832014-10-11 18:27:30 +03002669 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002670 }
2671
Benoit Gschwind37a68072016-04-28 20:33:08 +02002672 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002673 if (wayland_output_create_fullscreen(b) < 0) {
2674 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002675 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002676 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002677
Giulio Camuffo954f1832014-10-11 18:27:30 +03002678 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002679 }
2680
Armin Krezović174448a2016-09-30 14:11:09 +02002681 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2682 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002683
Armin Krezović174448a2016-09-30 14:11:09 +02002684 if (ret < 0) {
2685 weston_log("Failed to register output API.\n");
2686 wayland_backend_destroy(b);
2687 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002688 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002689
Giulio Camuffo954f1832014-10-11 18:27:30 +03002690 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002691 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002692 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002693 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002694
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002695err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002696 wayland_backend_destroy(b);
2697 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002698}