blob: ebdbd13b95e529e1710c49136aa7fdc9b43e2c2a [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
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600114 char *name;
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;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500133 uint32_t scale;
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800134
135 struct wl_callback *frame_cb;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100136};
137
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500138struct wayland_parent_output {
139 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
156 struct wl_list mode_list;
157 struct weston_mode *preferred_mode;
158 struct weston_mode *current_mode;
159};
160
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500161struct wayland_shm_buffer {
162 struct wayland_output *output;
163 struct wl_list link;
164 struct wl_list free_link;
165
166 struct wl_buffer *buffer;
167 void *data;
168 size_t size;
169 pixman_region32_t damage;
170 int frame_damaged;
171
172 pixman_image_t *pm_image;
173 cairo_surface_t *c_surface;
174};
175
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100176struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400177 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300178 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100179 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500180
181 struct {
182 struct wl_seat *seat;
183 struct wl_pointer *pointer;
184 struct wl_keyboard *keyboard;
185 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500186
187 struct {
188 struct wl_surface *surface;
189 int32_t hx, hy;
190 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500191 } parent;
192
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500193 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100194 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400195 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600196 uint32_t touch_points;
197 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600198 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600199 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600200
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400201 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600202 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500203 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000204
205 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100206};
207
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300208struct gl_renderer_interface *gl_renderer;
209
Armin Krezović938dc522016-08-01 19:17:57 +0200210static inline struct wayland_output *
211to_wayland_output(struct weston_output *base)
212{
213 return container_of(base, struct wayland_output, base);
214}
215
216static inline struct wayland_backend *
217to_wayland_backend(struct weston_compositor *base)
218{
219 return container_of(base->backend, struct wayland_backend, base);
220}
221
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500222static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500223wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
224{
225 cairo_surface_destroy(buffer->c_surface);
226 pixman_image_unref(buffer->pm_image);
227
228 wl_buffer_destroy(buffer->buffer);
229 munmap(buffer->data, buffer->size);
230
231 pixman_region32_fini(&buffer->damage);
232
233 wl_list_remove(&buffer->link);
234 wl_list_remove(&buffer->free_link);
235 free(buffer);
236}
237
238static void
239buffer_release(void *data, struct wl_buffer *buffer)
240{
241 struct wayland_shm_buffer *sb = data;
242
243 if (sb->output) {
244 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
245 } else {
246 wayland_shm_buffer_destroy(sb);
247 }
248}
249
250static const struct wl_buffer_listener buffer_listener = {
251 buffer_release
252};
253
254static struct wayland_shm_buffer *
255wayland_output_get_shm_buffer(struct wayland_output *output)
256{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300257 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200258 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300259 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500260 struct wayland_shm_buffer *sb;
261
262 struct wl_shm_pool *pool;
263 int width, height, stride;
264 int32_t fx, fy;
265 int fd;
266 unsigned char *data;
267
268 if (!wl_list_empty(&output->shm.free_buffers)) {
269 sb = container_of(output->shm.free_buffers.next,
270 struct wayland_shm_buffer, free_link);
271 wl_list_remove(&sb->free_link);
272 wl_list_init(&sb->free_link);
273
274 return sb;
275 }
276
277 if (output->frame) {
278 width = frame_width(output->frame);
279 height = frame_height(output->frame);
280 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500281 width = output->base.current_mode->width;
282 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500283 }
284
285 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
286
287 fd = os_create_anonymous_file(height * stride);
288 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000289 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500290 return NULL;
291 }
292
293 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
294 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000295 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500296 close(fd);
297 return NULL;
298 }
299
300 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000301 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200302 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000303 close(fd);
304 free(data);
305 return NULL;
306 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500307
308 sb->output = output;
309 wl_list_init(&sb->free_link);
310 wl_list_insert(&output->shm.buffers, &sb->link);
311
312 pixman_region32_init_rect(&sb->damage, 0, 0,
313 output->base.width, output->base.height);
314 sb->frame_damaged = 1;
315
316 sb->data = data;
317 sb->size = height * stride;
318
319 pool = wl_shm_create_pool(shm, fd, sb->size);
320
321 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
322 width, height,
323 stride,
324 WL_SHM_FORMAT_ARGB8888);
325 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
326 wl_shm_pool_destroy(pool);
327 close(fd);
328
329 memset(data, 0, sb->size);
330
331 sb->c_surface =
332 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
333 width, height, stride);
334
335 fx = 0;
336 fy = 0;
337 if (output->frame)
338 frame_interior(output->frame, &fx, &fy, 0, 0);
339 sb->pm_image =
340 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
341 (uint32_t *)(data + fy * stride) + fx,
342 stride);
343
344 return sb;
345}
346
347static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500348frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400349{
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800350 struct wayland_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400351 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400352
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800353 assert(callback == output->frame_cb);
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500354 wl_callback_destroy(callback);
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800355 output->frame_cb = NULL;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400356
357 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300358
359 /*
360 * This is the fallback case, where Presentation extension is not
361 * available from the parent compositor. We do not know the base for
362 * 'time', so we cannot feed it to finish_frame(). Do the only thing
363 * we can, and pretend finish_frame time is when we process this
364 * event.
365 */
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800366 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
367 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400368}
369
370static const struct wl_callback_listener frame_listener = {
371 frame_done
372};
373
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500374static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200375draw_initial_frame(struct wayland_output *output)
376{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500377 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200378
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200380
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500381 /* If we are rendering with GL, then orphan it so that it gets
382 * destroyed immediately */
383 if (output->gl.egl_window)
384 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500385
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500386 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500387 wl_surface_damage(output->parent.surface, 0, 0,
388 output->base.current_mode->width,
389 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200390}
391
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100392#ifdef ENABLE_EGL
Jonas Ådahle5a12252013-04-05 23:07:11 +0200393static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500394wayland_output_update_gl_border(struct wayland_output *output)
395{
396 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
397 cairo_t *cr;
398
399 if (!output->frame)
400 return;
401 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
402 return;
403
404 fwidth = frame_width(output->frame);
405 fheight = frame_height(output->frame);
406 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
407
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 if (!output->gl.border.top)
409 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
411 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500412 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500413 frame_repaint(output->frame, cr);
414 cairo_destroy(cr);
415 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
416 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500417 cairo_image_surface_get_stride(output->gl.border.top) / 4,
418 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500419
420
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500421 if (!output->gl.border.left)
422 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500423 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
424 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500425 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500426 cairo_translate(cr, 0, -iy);
427 frame_repaint(output->frame, cr);
428 cairo_destroy(cr);
429 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
430 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500431 cairo_image_surface_get_stride(output->gl.border.left) / 4,
432 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500433
434
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500435 if (!output->gl.border.right)
436 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500437 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
438 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500439 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500440 cairo_translate(cr, -(iwidth + ix), -iy);
441 frame_repaint(output->frame, cr);
442 cairo_destroy(cr);
443 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
444 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445 cairo_image_surface_get_stride(output->gl.border.right) / 4,
446 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500447
448
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500449 if (!output->gl.border.bottom)
450 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500451 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
452 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500453 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500454 cairo_translate(cr, 0, -(iy + iheight));
455 frame_repaint(output->frame, cr);
456 cairo_destroy(cr);
457 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
458 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500459 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
460 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500461}
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100462#endif
Jason Ekstrand7744f712013-10-27 22:24:55 -0500463
464static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200465wayland_output_start_repaint_loop(struct weston_output *output_base)
466{
Armin Krezović938dc522016-08-01 19:17:57 +0200467 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300468 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200469 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200470
471 /* If this is the initial frame, we need to attach a buffer so that
472 * the compositor can map the surface and include it in its render
473 * loop. If the surface doesn't end up in the render loop, the frame
474 * callback won't be invoked. The buffer is transparent and of the
475 * same size as the future real output buffer. */
476 if (output->parent.draw_initial_frame) {
Armin Krezović2d321e32016-10-09 17:30:25 +0200477 output->parent.draw_initial_frame = false;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200478
479 draw_initial_frame(output);
480 }
481
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800482 output->frame_cb = wl_surface_frame(output->parent.surface);
483 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200484 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300485 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200486}
487
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100488#ifdef ENABLE_EGL
David Herrmann1edf44c2013-10-22 17:11:26 +0200489static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500490wayland_output_repaint_gl(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000491 pixman_region32_t *damage,
492 void *repaint_data)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400493{
Armin Krezović938dc522016-08-01 19:17:57 +0200494 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400495 struct weston_compositor *ec = output->base.compositor;
Scott Moreau062be7e2012-04-20 13:37:33 -0600496
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800497 output->frame_cb = wl_surface_frame(output->parent.surface);
498 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100499
Jason Ekstrand7744f712013-10-27 22:24:55 -0500500 wayland_output_update_gl_border(output);
501
Pekka Paalanenbc106382012-10-10 12:49:31 +0300502 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200503
504 pixman_region32_subtract(&ec->primary_plane.damage,
505 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200506 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100507}
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100508#endif
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509
Matt Roper361d2ad2011-08-29 13:52:23 -0700510static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500511wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
512{
513 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
514 cairo_t *cr;
515
516 if (!buffer->output->frame || !buffer->frame_damaged)
517 return;
518
519 cr = cairo_create(buffer->c_surface);
520
521 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
522 fwidth = frame_width(buffer->output->frame);
523 fheight = frame_height(buffer->output->frame);
524
525 /* Set the clip so we don't unnecisaraly damage the surface */
526 cairo_move_to(cr, ix, iy);
527 cairo_rel_line_to(cr, iwidth, 0);
528 cairo_rel_line_to(cr, 0, iheight);
529 cairo_rel_line_to(cr, -iwidth, 0);
530 cairo_line_to(cr, ix, iy);
531 cairo_line_to(cr, 0, iy);
532 cairo_line_to(cr, 0, fheight);
533 cairo_line_to(cr, fwidth, fheight);
534 cairo_line_to(cr, fwidth, 0);
535 cairo_line_to(cr, 0, 0);
536 cairo_line_to(cr, 0, iy);
537 cairo_close_path(cr);
538 cairo_clip(cr);
539
540 /* Draw using a pattern so that the final result gets clipped */
541 cairo_push_group(cr);
542 frame_repaint(buffer->output->frame, cr);
543 cairo_pop_group_to_source(cr);
544 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
545 cairo_paint(cr);
546
547 cairo_destroy(cr);
548}
549
550static void
551wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
552{
553 pixman_region32_t damage;
554 pixman_box32_t *rects;
555 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
556 int i, n;
557
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500558 pixman_region32_init(&damage);
559 weston_transformed_region(sb->output->base.width,
560 sb->output->base.height,
561 sb->output->base.transform,
562 sb->output->base.current_scale,
563 &sb->damage, &damage);
564
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500565 if (sb->output->frame) {
566 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
567 fwidth = frame_width(sb->output->frame);
568 fheight = frame_height(sb->output->frame);
569
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500570 pixman_region32_translate(&damage, ix, iy);
571
572 if (sb->frame_damaged) {
573 pixman_region32_union_rect(&damage, &damage,
574 0, 0, fwidth, iy);
575 pixman_region32_union_rect(&damage, &damage,
576 0, iy, ix, iheight);
577 pixman_region32_union_rect(&damage, &damage,
578 ix + iwidth, iy,
579 fwidth - (ix + iwidth), iheight);
580 pixman_region32_union_rect(&damage, &damage,
581 0, iy + iheight,
582 fwidth, fheight - (iy + iheight));
583 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500584 }
585
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500586 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500587 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
588 for (i = 0; i < n; ++i)
589 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
590 rects[i].y1, rects[i].x2 - rects[i].x1,
591 rects[i].y2 - rects[i].y1);
592
593 if (sb->output->frame)
594 pixman_region32_fini(&damage);
595}
596
597static int
598wayland_output_repaint_pixman(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000599 pixman_region32_t *damage,
600 void *repaint_data)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500601{
Armin Krezović938dc522016-08-01 19:17:57 +0200602 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200604 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500605 struct wayland_shm_buffer *sb;
606
607 if (output->frame) {
608 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
609 wl_list_for_each(sb, &output->shm.buffers, link)
610 sb->frame_damaged = 1;
611 }
612
613 wl_list_for_each(sb, &output->shm.buffers, link)
614 pixman_region32_union(&sb->damage, &sb->damage, damage);
615
616 sb = wayland_output_get_shm_buffer(output);
617
618 wayland_output_update_shm_border(sb);
619 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300620 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500621
622 wayland_shm_buffer_attach(sb);
623
Daniel Stone21fac602016-11-28 16:05:35 +0000624 output->frame_cb = wl_surface_frame(output->parent.surface);
625 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500626 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300627 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500628
629 pixman_region32_fini(&sb->damage);
630 pixman_region32_init(&sb->damage);
631 sb->frame_damaged = 0;
632
Giulio Camuffo954f1832014-10-11 18:27:30 +0300633 pixman_region32_subtract(&b->compositor->primary_plane.damage,
634 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500635 return 0;
636}
637
638static void
Armin Krezović174448a2016-09-30 14:11:09 +0200639wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700640{
Armin Krezović34476192016-11-21 18:42:42 +0100641 if (output->parent.xdg_toplevel)
642 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
643
644 if (output->parent.xdg_surface)
645 zxdg_surface_v6_destroy(output->parent.xdg_surface);
646
Armin Krezović174448a2016-09-30 14:11:09 +0200647 if (output->parent.shell_surface)
648 wl_shell_surface_destroy(output->parent.shell_surface);
649
650 wl_surface_destroy(output->parent.surface);
651}
652
Armin Krezović2e662522016-10-09 17:30:29 +0200653static void
654wayland_output_destroy_shm_buffers(struct wayland_output *output)
655{
656 struct wayland_shm_buffer *buffer, *next;
657
658 /* Throw away any remaining SHM buffers */
659 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
660 wayland_shm_buffer_destroy(buffer);
661 /* These will get thrown away when they get released */
662 wl_list_for_each(buffer, &output->shm.buffers, link)
663 buffer->output = NULL;
664}
665
Armin Krezović174448a2016-09-30 14:11:09 +0200666static int
667wayland_output_disable(struct weston_output *base)
668{
669 struct wayland_output *output = to_wayland_output(base);
670 struct wayland_backend *b = to_wayland_backend(base->compositor);
671
672 if (!output->base.enabled)
673 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700674
Giulio Camuffo954f1832014-10-11 18:27:30 +0300675 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200676 pixman_renderer_output_destroy(&output->base);
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100677#ifdef ENABLE_EGL
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500678 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200679 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100680 wl_egl_window_destroy(output->gl.egl_window);
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100681#endif
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500682 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100683
Armin Krezović2e662522016-10-09 17:30:29 +0200684 wayland_output_destroy_shm_buffers(output);
685
Armin Krezovićf054d352016-10-09 17:30:27 +0200686 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500687
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600688 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500689 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600690
691 cairo_surface_destroy(output->gl.border.top);
692 cairo_surface_destroy(output->gl.border.left);
693 cairo_surface_destroy(output->gl.border.right);
694 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500695
Armin Krezović174448a2016-09-30 14:11:09 +0200696 return 0;
697}
Matt Roper361d2ad2011-08-29 13:52:23 -0700698
Armin Krezović174448a2016-09-30 14:11:09 +0200699static void
700wayland_output_destroy(struct weston_output *base)
701{
702 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +0200703
704 wayland_output_disable(&output->base);
705
Armin Krezović174448a2016-09-30 14:11:09 +0200706 weston_output_destroy(&output->base);
707
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800708 if (output->frame_cb)
709 wl_callback_destroy(output->frame_cb);
710
Armin Krezović174448a2016-09-30 14:11:09 +0200711 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700712}
713
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300714static const struct wl_shell_surface_listener shell_surface_listener;
715
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100716#ifdef ENABLE_EGL
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200717static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500718wayland_output_init_gl_renderer(struct wayland_output *output)
719{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500720 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500721
722 if (output->frame) {
723 fwidth = frame_width(output->frame);
724 fheight = frame_height(output->frame);
725 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500726 fwidth = output->base.current_mode->width;
727 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500728 }
729
730 output->gl.egl_window =
731 wl_egl_window_create(output->parent.surface,
732 fwidth, fheight);
733 if (!output->gl.egl_window) {
734 weston_log("failure to create wl_egl_window\n");
735 return -1;
736 }
737
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200738 if (gl_renderer->output_window_create(&output->base,
739 output->gl.egl_window,
740 output->gl.egl_window,
741 gl_renderer->alpha_attribs,
742 NULL,
743 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500744 goto cleanup_window;
745
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500746 return 0;
747
748cleanup_window:
749 wl_egl_window_destroy(output->gl.egl_window);
750 return -1;
751}
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100752#endif
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500753
754static int
755wayland_output_init_pixman_renderer(struct wayland_output *output)
756{
757 return pixman_renderer_output_create(&output->base);
758}
759
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600760static void
761wayland_output_resize_surface(struct wayland_output *output)
762{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300763 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200764 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600765 int32_t ix, iy, iwidth, iheight;
766 int32_t width, height;
767 struct wl_region *region;
768
769 width = output->base.current_mode->width;
770 height = output->base.current_mode->height;
771
772 if (output->frame) {
773 frame_resize_inside(output->frame, width, height);
774
775 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300776 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600777 wl_region_add(region, ix, iy, iwidth, iheight);
778 wl_surface_set_input_region(output->parent.surface, region);
779 wl_region_destroy(region);
780
781 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300782 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600783 wl_region_add(region, ix, iy, iwidth, iheight);
784 wl_surface_set_opaque_region(output->parent.surface, region);
785 wl_region_destroy(region);
786
787 width = frame_width(output->frame);
788 height = frame_height(output->frame);
789 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300790 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600791 wl_region_add(region, 0, 0, width, height);
792 wl_surface_set_input_region(output->parent.surface, region);
793 wl_region_destroy(region);
794
Giulio Camuffo954f1832014-10-11 18:27:30 +0300795 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600796 wl_region_add(region, 0, 0, width, height);
797 wl_surface_set_opaque_region(output->parent.surface, region);
798 wl_region_destroy(region);
799 }
800
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100801#ifdef ENABLE_EGL
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600802 if (output->gl.egl_window) {
803 wl_egl_window_resize(output->gl.egl_window,
804 width, height, 0, 0);
805
806 /* These will need to be re-created due to the resize */
807 gl_renderer->output_set_border(&output->base,
808 GL_RENDERER_BORDER_TOP,
809 0, 0, 0, NULL);
810 cairo_surface_destroy(output->gl.border.top);
811 output->gl.border.top = NULL;
812 gl_renderer->output_set_border(&output->base,
813 GL_RENDERER_BORDER_LEFT,
814 0, 0, 0, NULL);
815 cairo_surface_destroy(output->gl.border.left);
816 output->gl.border.left = NULL;
817 gl_renderer->output_set_border(&output->base,
818 GL_RENDERER_BORDER_RIGHT,
819 0, 0, 0, NULL);
820 cairo_surface_destroy(output->gl.border.right);
821 output->gl.border.right = NULL;
822 gl_renderer->output_set_border(&output->base,
823 GL_RENDERER_BORDER_BOTTOM,
824 0, 0, 0, NULL);
825 cairo_surface_destroy(output->gl.border.bottom);
826 output->gl.border.bottom = NULL;
827 }
Armin Krezovićb08e1a52016-12-09 22:58:27 +0100828#endif
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600829
Armin Krezović2e662522016-10-09 17:30:29 +0200830 wayland_output_destroy_shm_buffers(output);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600831}
832
833static int
834wayland_output_set_windowed(struct wayland_output *output)
835{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300836 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200837 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600838 int tlen;
839 char *title;
840
841 if (output->frame)
842 return 0;
843
844 if (output->name) {
845 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
846 title = malloc(tlen + 1);
847 if (!title)
848 return -1;
849
850 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
851 } else {
852 title = strdup(WINDOW_TITLE);
853 }
854
Armin Krezović34476192016-11-21 18:42:42 +0100855 if (output->parent.xdg_toplevel)
856 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, title);
857
Giulio Camuffo954f1832014-10-11 18:27:30 +0300858 if (!b->theme) {
859 b->theme = theme_create();
860 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600861 free(title);
862 return -1;
863 }
864 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300865 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600866 FRAME_BUTTON_CLOSE, title);
867 free(title);
868 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
976static int
977wayland_output_switch_mode(struct weston_output *output_base,
978 struct weston_mode *mode)
979{
Armin Krezović938dc522016-08-01 19:17:57 +0200980 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300981 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500982 struct wl_surface *old_surface;
983 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800984 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500985 enum mode_status mode_status;
986 int ret = 0;
987
988 if (output_base == NULL) {
989 weston_log("output is NULL.\n");
990 return -1;
991 }
992
993 if (mode == NULL) {
994 weston_log("mode is NULL.\n");
995 return -1;
996 }
997
Armin Krezović938dc522016-08-01 19:17:57 +0200998 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500999
Armin Krezović34476192016-11-21 18:42:42 +01001000 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001001 return -1;
1002
1003 mode = wayland_output_choose_mode(output, mode);
1004 if (mode == NULL)
1005 return -1;
1006
1007 if (output->base.current_mode == mode)
1008 return 0;
1009
1010 old_mode = output->base.current_mode;
1011 old_surface = output->parent.surface;
1012 output->base.current_mode = mode;
1013 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001014 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001015 wl_surface_set_user_data(output->parent.surface, output);
1016
1017 /* Blow the old buffers because we changed size/surfaces */
1018 wayland_output_resize_surface(output);
1019
1020 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +08001021 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1022 output->parent.surface,
1023 output->parent.output,
1024 mode->refresh);
1025 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1026 &mode_feedback_listener,
1027 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001028
1029 /* This should kick-start things again */
Armin Krezović2d321e32016-10-09 17:30:25 +02001030 output->parent.draw_initial_frame = true;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001031 wayland_output_start_repaint_loop(&output->base);
1032
1033 mode_status = MODE_STATUS_UNKNOWN;
1034 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001035 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001036
Jonas Ådahl496adb32015-11-17 16:00:27 +08001037 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001038
1039 if (mode_status == MODE_STATUS_FAIL) {
1040 output->base.current_mode = old_mode;
1041 wl_surface_destroy(output->parent.surface);
1042 output->parent.surface = old_surface;
1043 wayland_output_resize_surface(output);
1044
1045 return -1;
1046 }
1047
1048 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1049 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1050
Giulio Camuffo954f1832014-10-11 18:27:30 +03001051 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001052 pixman_renderer_output_destroy(output_base);
1053 if (wayland_output_init_pixman_renderer(output) < 0)
1054 goto err_output;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001055#ifdef ENABLE_EGL
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001056 } else {
1057 gl_renderer->output_destroy(output_base);
1058 wl_egl_window_destroy(output->gl.egl_window);
1059 if (wayland_output_init_gl_renderer(output) < 0)
1060 goto err_output;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001061#endif
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001062 }
1063 wl_surface_destroy(old_surface);
1064
1065 weston_output_schedule_repaint(&output->base);
1066
1067 return 0;
1068
1069err_output:
1070 /* XXX */
1071 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001072}
1073
Armin Krezović34476192016-11-21 18:42:42 +01001074static void
1075handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1076 uint32_t serial)
1077{
1078 zxdg_surface_v6_ack_configure(surface, serial);
1079}
1080
1081static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1082 handle_xdg_surface_configure
1083};
1084
1085static void
1086handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1087 int32_t width, int32_t height,
1088 struct wl_array *states)
1089{
1090 struct wayland_output *output = data;
1091
1092 output->parent.configure_width = width;
1093 output->parent.configure_height = height;
1094
1095 output->parent.wait_for_configure = false;
1096 /* FIXME: implement resizing */
1097}
1098
1099static void
1100handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1101{
1102}
1103
1104static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1105 handle_xdg_toplevel_configure,
1106 handle_xdg_toplevel_close,
1107};
1108
Armin Krezović174448a2016-09-30 14:11:09 +02001109static int
1110wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001111{
Armin Krezović174448a2016-09-30 14:11:09 +02001112 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001113
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001114 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001115 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001116 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001117 return -1;
1118
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001119 wl_surface_set_user_data(output->parent.surface, output);
1120
Armin Krezović2d321e32016-10-09 17:30:25 +02001121 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001122
Armin Krezović34476192016-11-21 18:42:42 +01001123 if (b->parent.xdg_shell) {
1124 output->parent.xdg_surface =
1125 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1126 output->parent.surface);
1127 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1128 &xdg_surface_listener, output);
1129
1130 output->parent.xdg_toplevel =
1131 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1132 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1133 &xdg_toplevel_listener, output);
1134
1135 wl_surface_commit(output->parent.surface);
1136
1137 output->parent.wait_for_configure = true;
1138
1139 while (output->parent.wait_for_configure)
1140 wl_display_dispatch(b->parent.wl_display);
1141
1142 weston_log("wayland-backend: Using xdg_shell_v6\n");
1143 }
1144 else if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001145 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001146 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001147 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001148 if (!output->parent.shell_surface) {
1149 wl_surface_destroy(output->parent.surface);
1150 return -1;
1151 }
1152
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001153 wl_shell_surface_add_listener(output->parent.shell_surface,
1154 &shell_surface_listener, output);
Armin Krezović34476192016-11-21 18:42:42 +01001155
1156 weston_log("wayland-backend: Using wl_shell\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001157 }
1158
Armin Krezović174448a2016-09-30 14:11:09 +02001159 return 0;
1160}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001161
Armin Krezović174448a2016-09-30 14:11:09 +02001162static int
1163wayland_output_enable(struct weston_output *base)
1164{
1165 struct wayland_output *output = to_wayland_output(base);
1166 struct wayland_backend *b = to_wayland_backend(base->compositor);
1167 int ret = 0;
1168
Armin Krezović174448a2016-09-30 14:11:09 +02001169 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1170 output->base.current_mode->width,
1171 output->base.current_mode->height,
1172 output->base.x, output->base.y);
1173
Armin Krezovićf054d352016-10-09 17:30:27 +02001174 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001175 ret = wayland_backend_create_output_surface(output);
1176
1177 if (ret < 0)
1178 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001179
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001180 wl_list_init(&output->shm.buffers);
1181 wl_list_init(&output->shm.free_buffers);
1182
Giulio Camuffo954f1832014-10-11 18:27:30 +03001183 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001184 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001185 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001186
1187 output->base.repaint = wayland_output_repaint_pixman;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001188#ifdef ENABLE_EGL
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001189 } else {
1190 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001191 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001192
1193 output->base.repaint = wayland_output_repaint_gl;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01001194#endif
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001195 }
1196
Armin Krezovićf16de172016-10-09 17:30:26 +02001197 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1198 output->base.assign_planes = NULL;
1199 output->base.set_backlight = NULL;
1200 output->base.set_dpms = NULL;
1201 output->base.switch_mode = wayland_output_switch_mode;
1202
Armin Krezović174448a2016-09-30 14:11:09 +02001203 if (b->sprawl_across_outputs) {
1204 wayland_output_set_fullscreen(output,
1205 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1206 output->mode.refresh, output->parent.output);
1207
Armin Krezović34476192016-11-21 18:42:42 +01001208 if (output->parent.xdg_toplevel) {
1209 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1210 output->parent.output);
1211 }
1212 else if (output->parent.shell_surface) {
Armin Krezović174448a2016-09-30 14:11:09 +02001213 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1214 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1215 output->mode.refresh, output->parent.output);
1216 } else if (b->parent.fshell) {
1217 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1218 output->parent.surface,
1219 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1220 output->parent.output);
1221 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1222 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1223 output->parent.surface,
1224 output->parent.output,
1225 output->mode.refresh));
1226 }
1227 } else if (b->fullscreen) {
1228 wayland_output_set_fullscreen(output, 0, 0, NULL);
1229 } else {
1230 wayland_output_set_windowed(output);
1231 }
1232
1233 return 0;
1234
1235err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001236 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001237
1238 return -1;
1239}
1240
1241static struct wayland_output *
1242wayland_output_create_common(void)
1243{
1244 struct wayland_output *output;
1245
1246 output = zalloc(sizeof *output);
1247 if (output == NULL) {
1248 perror("zalloc");
1249 return NULL;
1250 }
1251
Matt Roper361d2ad2011-08-29 13:52:23 -07001252 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001253 output->base.disable = wayland_output_disable;
1254 output->base.enable = wayland_output_enable;
1255
1256 return output;
1257}
1258
1259static int
1260wayland_output_create(struct weston_compositor *compositor, const char *name)
1261{
1262 struct wayland_output *output = wayland_output_create_common();
1263
1264 /* name can't be NULL. */
1265 assert(name);
1266
1267 output->base.name = strdup(name);
1268
Armin Krezović40087402016-09-30 14:11:12 +02001269 weston_output_init(&output->base, compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001270 weston_compositor_add_pending_output(&output->base, compositor);
1271
1272 return 0;
1273}
1274
1275static int
1276wayland_output_set_size(struct weston_output *base, int width, int height)
1277{
1278 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001279 int output_width, output_height;
1280
1281 /* We can only be called once. */
1282 assert(!output->base.current_mode);
1283
1284 /* Make sure we have scale set. */
1285 assert(output->base.scale);
1286
1287 if (width < 1) {
1288 weston_log("Invalid width \"%d\" for output %s\n",
1289 width, output->base.name);
1290 return -1;
1291 }
1292
1293 if (height < 1) {
1294 weston_log("Invalid height \"%d\" for output %s\n",
1295 height, output->base.name);
1296 return -1;
1297 }
1298
1299 output_width = width * output->base.scale;
1300 output_height = height * output->base.scale;
1301
1302 output->mode.flags =
1303 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1304
1305 output->mode.width = output_width;
1306 output->mode.height = output_height;
1307 output->mode.refresh = 60000;
1308 output->scale = output->base.scale;
1309 wl_list_init(&output->base.mode_list);
1310 wl_list_insert(&output->base.mode_list, &output->mode.link);
1311
1312 output->base.current_mode = &output->mode;
1313 output->base.make = "wayland";
1314 output->base.model = "none";
1315
1316 /* XXX: Calculate proper size. */
1317 output->base.mm_width = width;
1318 output->base.mm_height = height;
1319
Armin Krezović174448a2016-09-30 14:11:09 +02001320 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001321}
1322
Armin Krezović174448a2016-09-30 14:11:09 +02001323static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001324wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001325 struct wayland_parent_output *poutput)
1326{
Armin Krezović174448a2016-09-30 14:11:09 +02001327 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001328 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001329
1330 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001331
1332 if (poutput->current_mode) {
1333 mode = poutput->current_mode;
1334 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001335 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001336 } else if (!wl_list_empty(&poutput->mode_list)) {
1337 mode = container_of(poutput->mode_list.next,
1338 struct weston_mode, link);
1339 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001340 weston_log("No valid modes found. Skipping output.\n");
1341 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001342 }
1343
Armin Krezović40087402016-09-30 14:11:12 +02001344 weston_output_init(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001345
Armin Krezović174448a2016-09-30 14:11:09 +02001346 output->base.scale = 1;
1347 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001348
Armin Krezović174448a2016-09-30 14:11:09 +02001349 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1350 goto out;
1351
1352 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001353 output->parent.output = poutput->global;
1354
1355 output->base.make = poutput->physical.make;
1356 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001357
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001358 wl_list_init(&output->base.mode_list);
1359 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1360 wl_list_init(&poutput->mode_list);
1361
Armin Krezović174448a2016-09-30 14:11:09 +02001362 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001363
Armin Krezović174448a2016-09-30 14:11:09 +02001364 return 0;
1365
1366out:
1367 free(output->name);
1368 free(output);
1369
1370 return -1;
1371}
1372
1373static int
1374wayland_output_create_fullscreen(struct wayland_backend *b)
1375{
1376 struct wayland_output *output = wayland_output_create_common();
1377 int width = 0, height = 0;
1378
1379 output->base.name = strdup("wayland-fullscreen");
1380
Armin Krezović40087402016-09-30 14:11:12 +02001381 weston_output_init(&output->base, b->compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001382
1383 output->base.scale = 1;
1384 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1385
1386 if (wayland_backend_create_output_surface(output) < 0)
1387 goto err_surface;
1388
1389 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001390 if (b->parent.xdg_shell || b->parent.shell) {
1391 if (output->parent.xdg_toplevel)
1392 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1393 output->parent.output);
1394 else if (output->parent.shell_surface)
1395 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1396 0, 0, NULL);
1397
Armin Krezović174448a2016-09-30 14:11:09 +02001398 wl_display_roundtrip(b->parent.wl_display);
1399
1400 width = output->parent.configure_width;
1401 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001402 }
1403
Armin Krezović174448a2016-09-30 14:11:09 +02001404 if (wayland_output_set_size(&output->base, width, height) < 0)
1405 goto err_set_size;
1406
1407 weston_compositor_add_pending_output(&output->base, b->compositor);
1408
1409 return 0;
1410
1411err_set_size:
1412 wayland_backend_destroy_output_surface(output);
1413err_surface:
1414 free(output->name);
1415 free(output);
1416
1417 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001418}
1419
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001420static void
1421shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1422 uint32_t serial)
1423{
1424 wl_shell_surface_pong(shell_surface, serial);
1425}
1426
1427static void
1428shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1429 uint32_t edges, int32_t width, int32_t height)
1430{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001431 struct wayland_output *output = data;
1432
1433 output->parent.configure_width = width;
1434 output->parent.configure_height = height;
1435
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001436 /* FIXME: implement resizing */
1437}
1438
1439static void
1440shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1441{
1442}
1443
1444static const struct wl_shell_surface_listener shell_surface_listener = {
1445 shell_surface_ping,
1446 shell_surface_configure,
1447 shell_surface_popup_done
1448};
1449
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001450/* Events received from the wayland-server this compositor is client of: */
1451
Jason Ekstrand7744f712013-10-27 22:24:55 -05001452/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001453static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001454input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001455{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001456
Jason Ekstrand7744f712013-10-27 22:24:55 -05001457 struct wl_buffer *buffer;
1458 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001459
Giulio Camuffo954f1832014-10-11 18:27:30 +03001460 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001461 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001462
Giulio Camuffo954f1832014-10-11 18:27:30 +03001463 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001464 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001465 if (!buffer)
1466 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001467
1468 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1469 input->parent.cursor.surface,
1470 image->hotspot_x, image->hotspot_y);
1471
1472 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1473 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1474 image->width, image->height);
1475 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001476}
1477
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001478static void
Daniel Stone37816df2012-05-16 18:45:18 +01001479input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1480 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001481 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001482{
1483 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001484 int32_t fx, fy;
1485 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001486 double x, y;
1487
1488 x = wl_fixed_to_double(fixed_x);
1489 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001490
Daniel Stone50692802012-06-22 13:21:41 +01001491 /* XXX: If we get a modifier event immediately before the focus,
1492 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001493 input->enter_serial = serial;
1494 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001495
1496 if (input->output->frame) {
1497 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001498 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001499 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001500 x -= fx;
1501 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001502
1503 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1504 weston_output_schedule_repaint(&input->output->base);
1505 } else {
1506 location = THEME_LOCATION_CLIENT_AREA;
1507 }
1508
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001509 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001510
1511 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001512 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001513 notify_pointer_focus(&input->base, &input->output->base, x, y);
1514 wl_pointer_set_cursor(input->parent.pointer,
1515 input->enter_serial, NULL, 0, 0);
1516 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001517 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001518 notify_pointer_focus(&input->base, NULL, 0, 0);
1519 input_set_cursor(input);
1520 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001521}
1522
1523static void
Daniel Stone37816df2012-05-16 18:45:18 +01001524input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1525 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001526{
1527 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001528
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001529 if (!input->output)
1530 return;
1531
Jason Ekstrand7744f712013-10-27 22:24:55 -05001532 if (input->output->frame) {
1533 frame_pointer_leave(input->output->frame, input);
1534
1535 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1536 weston_output_schedule_repaint(&input->output->base);
1537 }
1538
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001539 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001540 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001541 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001542}
1543
1544static void
Daniel Stone37816df2012-05-16 18:45:18 +01001545input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001546 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001547{
1548 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001549 int32_t fx, fy;
1550 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001551 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001552 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001553
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001554 if (!input->output)
1555 return;
1556
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001557 x = wl_fixed_to_double(fixed_x);
1558 y = wl_fixed_to_double(fixed_y);
1559
Jason Ekstrand7744f712013-10-27 22:24:55 -05001560 if (input->output->frame) {
1561 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001562 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001563 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001564 x -= fx;
1565 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001566
1567 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1568 weston_output_schedule_repaint(&input->output->base);
1569 } else {
1570 location = THEME_LOCATION_CLIENT_AREA;
1571 }
1572
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001573 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001574
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001575 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001576 input_set_cursor(input);
1577 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001578 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001579 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001580 } else if (!input->has_focus &&
1581 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001582 wl_pointer_set_cursor(input->parent.pointer,
1583 input->enter_serial, NULL, 0, 0);
1584 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001585 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001586 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001587 }
1588
Peter Hutterer87743e92016-01-18 16:38:22 +10001589 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001590 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001591 want_frame = true;
1592 }
1593
1594 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1595 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001596}
1597
1598static void
1599input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001600 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001601 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001602{
1603 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001604 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001605
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001606 if (!input->output)
1607 return;
1608
Jason Ekstrand7744f712013-10-27 22:24:55 -05001609 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001610 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001611 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001612
1613 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001614 if (input->output->parent.xdg_toplevel)
1615 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001616 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001617 else if (input->output->parent.shell_surface)
1618 wl_shell_surface_move(input->output->parent.shell_surface,
1619 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001620 frame_status_clear(input->output->frame,
1621 FRAME_STATUS_MOVE);
1622 return;
1623 }
1624
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001625 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1626 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001627 input->output = NULL;
1628 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001629
Giulio Camuffo954f1832014-10-11 18:27:30 +03001630 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001631 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001632
1633 return;
1634 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001635
1636 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1637 weston_output_schedule_repaint(&input->output->base);
1638 } else {
1639 location = THEME_LOCATION_CLIENT_AREA;
1640 }
1641
Peter Hutterer87743e92016-01-18 16:38:22 +10001642 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001643 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001644 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1645 notify_pointer_frame(&input->base);
1646 }
Daniel Stone37816df2012-05-16 18:45:18 +01001647}
1648
1649static void
1650input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001651 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001652{
1653 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001654 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001655
Peter Hutterer89b6a492016-01-18 15:58:17 +10001656 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001657 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001658
Peter Hutterer87743e92016-01-18 16:38:22 +10001659 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1660 input->vert.has_discrete) {
1661 weston_event.has_discrete = true;
1662 weston_event.discrete = input->vert.discrete;
1663 input->vert.has_discrete = false;
1664 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1665 input->horiz.has_discrete) {
1666 weston_event.has_discrete = true;
1667 weston_event.discrete = input->horiz.discrete;
1668 input->horiz.has_discrete = false;
1669 }
1670
Peter Hutterer89b6a492016-01-18 15:58:17 +10001671 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001672
1673 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1674 notify_pointer_frame(&input->base);
1675}
1676
1677static void
1678input_handle_frame(void *data, struct wl_pointer *pointer)
1679{
1680 struct wayland_input *input = data;
1681
1682 notify_pointer_frame(&input->base);
1683}
1684
1685static void
1686input_handle_axis_source(void *data, struct wl_pointer *pointer,
1687 uint32_t source)
1688{
1689 struct wayland_input *input = data;
1690
1691 notify_axis_source(&input->base, source);
1692}
1693
1694static void
1695input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1696 uint32_t time, uint32_t axis)
1697{
1698 struct wayland_input *input = data;
1699 struct weston_pointer_axis_event weston_event;
1700
1701 weston_event.axis = axis;
1702 weston_event.value = 0;
1703
1704 notify_axis(&input->base, time, &weston_event);
1705}
1706
1707static void
1708input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1709 uint32_t axis, int32_t discrete)
1710{
1711 struct wayland_input *input = data;
1712
1713 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1714 input->vert.has_discrete = true;
1715 input->vert.discrete = discrete;
1716 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1717 input->horiz.has_discrete = true;
1718 input->horiz.discrete = discrete;
1719 }
Daniel Stone37816df2012-05-16 18:45:18 +01001720}
1721
1722static const struct wl_pointer_listener pointer_listener = {
1723 input_handle_pointer_enter,
1724 input_handle_pointer_leave,
1725 input_handle_motion,
1726 input_handle_button,
1727 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001728 input_handle_frame,
1729 input_handle_axis_source,
1730 input_handle_axis_stop,
1731 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001732};
1733
1734static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001735input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1736 int fd, uint32_t size)
1737{
1738 struct wayland_input *input = data;
1739 struct xkb_keymap *keymap;
1740 char *map_str;
1741
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001742 if (!data) {
1743 close(fd);
1744 return;
1745 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001746
1747 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1748 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1749 if (map_str == MAP_FAILED) {
1750 weston_log("mmap failed: %m\n");
1751 goto error;
1752 }
1753
Giulio Camuffo954f1832014-10-11 18:27:30 +03001754 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001755 map_str,
1756 XKB_KEYMAP_FORMAT_TEXT_V1,
1757 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001758 munmap(map_str, size);
1759
1760 if (!keymap) {
1761 weston_log("failed to compile keymap\n");
1762 goto error;
1763 }
1764
1765 input->keyboard_state_update = STATE_UPDATE_NONE;
1766 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1767 weston_log("No keymap provided; falling back to defalt\n");
1768 keymap = NULL;
1769 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1770 } else {
1771 weston_log("Invalid keymap\n");
1772 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001773 }
1774
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001775 close(fd);
1776
Derek Foreman1281a362015-07-31 16:55:32 -05001777 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001778 weston_seat_update_keymap(&input->base, keymap);
1779 else
1780 weston_seat_init_keyboard(&input->base, keymap);
1781
Ran Benitac9c74152014-08-19 23:59:52 +03001782 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001783
1784 return;
1785
1786error:
1787 wl_keyboard_release(input->parent.keyboard);
1788 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001789}
1790
1791static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001792input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001793 struct wl_keyboard *keyboard,
1794 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001795 struct wl_surface *surface,
1796 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001797{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001798 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001799 struct wayland_output *focus;
1800
1801 focus = input->keyboard_focus;
1802 if (focus) {
1803 /* This shouldn't happen */
1804 focus->keyboard_count--;
1805 if (!focus->keyboard_count && focus->frame)
1806 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1807 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1808 weston_output_schedule_repaint(&focus->base);
1809 }
1810
1811 input->keyboard_focus = wl_surface_get_user_data(surface);
1812 input->keyboard_focus->keyboard_count++;
1813
1814 focus = input->keyboard_focus;
1815 if (focus->frame) {
1816 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1817 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1818 weston_output_schedule_repaint(&focus->base);
1819 }
1820
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001821
Daniel Stone50692802012-06-22 13:21:41 +01001822 /* XXX: If we get a modifier event immediately before the focus,
1823 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001824 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001825 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001826}
1827
1828static void
1829input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001830 struct wl_keyboard *keyboard,
1831 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001832 struct wl_surface *surface)
1833{
1834 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001835 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001836
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001837 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001838
1839 focus = input->keyboard_focus;
1840 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001841 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001842
1843 focus->keyboard_count--;
1844 if (!focus->keyboard_count && focus->frame) {
1845 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1846 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1847 weston_output_schedule_repaint(&focus->base);
1848 }
1849
1850 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001851}
1852
Daniel Stone37816df2012-05-16 18:45:18 +01001853static void
1854input_handle_key(void *data, struct wl_keyboard *keyboard,
1855 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1856{
1857 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001858
Daniel Stone50692802012-06-22 13:21:41 +01001859 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001860 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001861 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001862 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001863 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001864}
1865
Daniel Stone351eb612012-05-31 15:27:47 -04001866static void
Derek Foreman1281a362015-07-31 16:55:32 -05001867input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001868 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001869 uint32_t mods_latched, uint32_t mods_locked,
1870 uint32_t group)
1871{
Derek Foreman1281a362015-07-31 16:55:32 -05001872 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001873 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001874 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001875 uint32_t serial_out;
1876
1877 /* If we get a key event followed by a modifier event with the
1878 * same serial number, then we try to preserve those semantics by
1879 * reusing the same serial number on the way out too. */
1880 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001881 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001882 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001883 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001884
Derek Foreman1281a362015-07-31 16:55:32 -05001885 keyboard = weston_seat_get_keyboard(&input->base);
1886 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001887 mods_depressed, mods_latched,
1888 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001889 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001890}
1891
Jonny Lamb497994a2014-08-12 14:58:26 +02001892static void
1893input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1894 int32_t rate, int32_t delay)
1895{
1896 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001897 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001898
Giulio Camuffo954f1832014-10-11 18:27:30 +03001899 b->compositor->kb_repeat_rate = rate;
1900 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001901}
1902
Daniel Stone37816df2012-05-16 18:45:18 +01001903static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001904 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001905 input_handle_keyboard_enter,
1906 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001907 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001908 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001909 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001910};
1911
1912static void
Derek Foreman748c6952015-11-06 15:56:10 -06001913input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1914 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001915 struct wl_surface *surface, int32_t id,
1916 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001917{
1918 struct wayland_input *input = data;
1919 struct wayland_output *output;
1920 enum theme_location location;
1921 bool first_touch;
1922 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001923 double x, y;
1924
1925 x = wl_fixed_to_double(fixed_x);
1926 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001927
1928 first_touch = (input->touch_points == 0);
1929 input->touch_points++;
1930
1931 input->touch_focus = wl_surface_get_user_data(surface);
1932 output = input->touch_focus;
1933 if (!first_touch && !input->touch_active)
1934 return;
1935
1936 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001937 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001938
1939 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001940 x -= fx;
1941 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001942
1943 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1944 weston_output_schedule_repaint(&output->base);
1945
1946 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1947 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001948 if (output->parent.xdg_toplevel)
1949 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1950 input->parent.seat, serial);
1951 else if (output->parent.shell_surface)
1952 wl_shell_surface_move(output->parent.shell_surface,
1953 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001954 frame_status_clear(output->frame,
1955 FRAME_STATUS_MOVE);
1956 return;
1957 }
1958
1959 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1960 return;
1961 }
1962
1963 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1964
1965 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1966 input->touch_active = true;
1967}
1968
1969static void
1970input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1971 uint32_t serial, uint32_t time, int32_t id)
1972{
1973 struct wayland_input *input = data;
1974 struct wayland_output *output = input->touch_focus;
1975 bool active = input->touch_active;
1976
1977 input->touch_points--;
1978 if (input->touch_points == 0) {
1979 input->touch_focus = NULL;
1980 input->touch_active = false;
1981 }
1982
1983 if (!output)
1984 return;
1985
1986 if (output->frame) {
1987 frame_touch_up(output->frame, input, id);
1988
1989 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1990 wayland_output_destroy(&output->base);
1991 input->touch_focus = NULL;
1992 input->keyboard_focus = NULL;
1993 if (wl_list_empty(&input->backend->compositor->output_list))
1994 weston_compositor_exit(input->backend->compositor);
1995
1996 return;
1997 }
1998 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1999 weston_output_schedule_repaint(&output->base);
2000 }
2001
2002 if (active)
2003 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
2004}
2005
2006static void
2007input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002008 uint32_t time, int32_t id,
2009 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06002010{
2011 struct wayland_input *input = data;
2012 struct wayland_output *output = input->touch_focus;
2013 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002014 double x, y;
2015
2016 x = wl_fixed_to_double(fixed_x);
2017 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06002018
2019 if (!output || !input->touch_active)
2020 return;
2021
2022 if (output->frame) {
2023 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002024 x -= fx;
2025 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002026 }
2027
2028 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2029
2030 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2031}
2032
2033static void
2034input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2035{
2036 struct wayland_input *input = data;
2037
2038 if (!input->touch_focus || !input->touch_active)
2039 return;
2040
2041 notify_touch_frame(&input->base);
2042}
2043
2044static void
2045input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2046{
2047 struct wayland_input *input = data;
2048
2049 if (!input->touch_focus || !input->touch_active)
2050 return;
2051
2052 notify_touch_cancel(&input->base);
2053}
2054
2055static const struct wl_touch_listener touch_listener = {
2056 input_handle_touch_down,
2057 input_handle_touch_up,
2058 input_handle_touch_motion,
2059 input_handle_touch_frame,
2060 input_handle_touch_cancel,
2061};
2062
2063
2064static void
Daniel Stone37816df2012-05-16 18:45:18 +01002065input_handle_capabilities(void *data, struct wl_seat *seat,
2066 enum wl_seat_capability caps)
2067{
2068 struct wayland_input *input = data;
2069
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002070 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2071 input->parent.pointer = wl_seat_get_pointer(seat);
2072 wl_pointer_set_user_data(input->parent.pointer, input);
2073 wl_pointer_add_listener(input->parent.pointer,
2074 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002075 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002076 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002077 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2078 wl_pointer_release(input->parent.pointer);
2079 else
2080 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002081 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002082 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002083 }
2084
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002085 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2086 input->parent.keyboard = wl_seat_get_keyboard(seat);
2087 wl_keyboard_set_user_data(input->parent.keyboard, input);
2088 wl_keyboard_add_listener(input->parent.keyboard,
2089 &keyboard_listener, input);
2090 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002091 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2092 wl_keyboard_release(input->parent.keyboard);
2093 else
2094 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002095 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002096 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002097 }
Derek Foreman748c6952015-11-06 15:56:10 -06002098
2099 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2100 input->parent.touch = wl_seat_get_touch(seat);
2101 wl_touch_set_user_data(input->parent.touch, input);
2102 wl_touch_add_listener(input->parent.touch,
2103 &touch_listener, input);
2104 weston_seat_init_touch(&input->base);
2105 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2106 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2107 wl_touch_release(input->parent.touch);
2108 else
2109 wl_touch_destroy(input->parent.touch);
2110 input->parent.touch = NULL;
2111 weston_seat_release_touch(&input->base);
2112 }
Daniel Stone37816df2012-05-16 18:45:18 +01002113}
2114
Jonny Lamb497994a2014-08-12 14:58:26 +02002115static void
2116input_handle_name(void *data, struct wl_seat *seat,
2117 const char *name)
2118{
2119}
2120
Daniel Stone37816df2012-05-16 18:45:18 +01002121static const struct wl_seat_listener seat_listener = {
2122 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002123 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002124};
2125
2126static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002127display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002128{
2129 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002130 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002131
Peter Huttererf3d62272013-08-08 11:57:05 +10002132 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002133 if (input == NULL)
2134 return;
2135
Giulio Camuffo954f1832014-10-11 18:27:30 +03002136 weston_seat_init(&input->base, b->compositor, "default");
2137 input->backend = b;
2138 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002139 &wl_seat_interface, version);
2140 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002141 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002142
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002143 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2144 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002145
2146 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002147 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002148
2149 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2150 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002151}
2152
2153static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002154wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2155 int32_t x, int32_t y,
2156 int32_t physical_width, int32_t physical_height,
2157 int32_t subpixel, const char *make,
2158 const char *model, int32_t transform)
2159{
2160 struct wayland_parent_output *output = data;
2161
2162 output->x = x;
2163 output->y = y;
2164 output->physical.width = physical_width;
2165 output->physical.height = physical_height;
2166 output->physical.subpixel = subpixel;
2167
2168 free(output->physical.make);
2169 output->physical.make = strdup(make);
2170 free(output->physical.model);
2171 output->physical.model = strdup(model);
2172
2173 output->transform = transform;
2174}
2175
2176static struct weston_mode *
2177find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2178{
2179 struct weston_mode *mode;
2180
2181 wl_list_for_each(mode, list, link) {
2182 if (mode->width == width && mode->height == height &&
2183 mode->refresh == refresh)
2184 return mode;
2185 }
2186
2187 mode = zalloc(sizeof *mode);
2188 if (!mode)
2189 return NULL;
2190
2191 mode->width = width;
2192 mode->height = height;
2193 mode->refresh = refresh;
2194 wl_list_insert(list, &mode->link);
2195
2196 return mode;
2197}
2198
2199static void
2200wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2201 uint32_t flags, int32_t width, int32_t height,
2202 int32_t refresh)
2203{
2204 struct wayland_parent_output *output = data;
2205 struct weston_mode *mode;
2206
2207 if (output->output) {
2208 mode = find_mode(&output->output->base.mode_list,
2209 width, height, refresh);
2210 if (!mode)
2211 return;
2212 mode->flags = flags;
2213 /* Do a mode-switch on current mode change? */
2214 } else {
2215 mode = find_mode(&output->mode_list, width, height, refresh);
2216 if (!mode)
2217 return;
2218 mode->flags = flags;
2219 if (flags & WL_OUTPUT_MODE_CURRENT)
2220 output->current_mode = mode;
2221 if (flags & WL_OUTPUT_MODE_PREFERRED)
2222 output->preferred_mode = mode;
2223 }
2224}
2225
2226static const struct wl_output_listener output_listener = {
2227 wayland_parent_output_geometry,
2228 wayland_parent_output_mode
2229};
2230
2231static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002232wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002233{
2234 struct wayland_parent_output *output;
2235
2236 output = zalloc(sizeof *output);
2237 if (!output)
2238 return;
2239
2240 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002241 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002242 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002243 if (!output->global) {
2244 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002245 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002246 }
2247
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002248 wl_output_add_listener(output->global, &output_listener, output);
2249
2250 output->scale = 0;
2251 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2252 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2253 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002254 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002255
Giulio Camuffo954f1832014-10-11 18:27:30 +03002256 if (b->sprawl_across_outputs) {
2257 wl_display_roundtrip(b->parent.wl_display);
2258 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002259 }
2260}
2261
2262static void
2263wayland_parent_output_destroy(struct wayland_parent_output *output)
2264{
2265 struct weston_mode *mode, *next;
2266
2267 if (output->output)
2268 wayland_output_destroy(&output->output->base);
2269
2270 wl_output_destroy(output->global);
2271 free(output->physical.make);
2272 free(output->physical.model);
2273
2274 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2275 wl_list_remove(&mode->link);
2276 free(mode);
2277 }
2278}
2279
2280static void
Armin Krezović34476192016-11-21 18:42:42 +01002281xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2282{
2283 zxdg_shell_v6_pong(shell, serial);
2284}
2285
2286static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2287 xdg_shell_ping,
2288};
2289
2290static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002291registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2292 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002293{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002294 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002295
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002296 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002297 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002298 wl_registry_bind(registry, name,
2299 &wl_compositor_interface, 1);
Armin Krezović34476192016-11-21 18:42:42 +01002300 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2301 b->parent.xdg_shell =
2302 wl_registry_bind(registry, name,
2303 &zxdg_shell_v6_interface, 1);
2304 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2305 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002306 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002307 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002308 wl_registry_bind(registry, name,
2309 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002310 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002312 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002313 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002314 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002315 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002316 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002317 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002318 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002319 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002320 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002321 }
2322}
2323
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002324static void
2325registry_handle_global_remove(void *data, struct wl_registry *registry,
2326 uint32_t name)
2327{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002328 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002329 struct wayland_parent_output *output;
2330
Giulio Camuffo954f1832014-10-11 18:27:30 +03002331 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002332 if (output->id == name)
2333 wayland_parent_output_destroy(output);
2334}
2335
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002336static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002337 registry_handle_global,
2338 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002339};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002340
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002341static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002343{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002344 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002345 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002346
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002347 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002348 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002349 return 0;
2350 }
2351
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002352 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002353 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002354 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002355 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002356
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002357 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002358 count = wl_display_dispatch_pending(b->parent.wl_display);
2359 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002360 }
2361
2362 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002363}
2364
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002365static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002366wayland_restore(struct weston_compositor *ec)
2367{
2368}
2369
2370static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002371wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002372{
Armin Krezović938dc522016-08-01 19:17:57 +02002373 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002374
Armin Krezović78895c52016-10-09 17:30:28 +02002375 wl_event_source_remove(b->parent.wl_source);
2376
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002377 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002378
Giulio Camuffo954f1832014-10-11 18:27:30 +03002379 if (b->parent.shm)
2380 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002381
Armin Krezović34476192016-11-21 18:42:42 +01002382 if (b->parent.xdg_shell)
2383 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2384
2385 if (b->parent.shell)
2386 wl_shell_destroy(b->parent.shell);
2387
Armin Krezović78895c52016-10-09 17:30:28 +02002388 if (b->parent.fshell)
2389 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2390
2391 if (b->parent.compositor)
2392 wl_compositor_destroy(b->parent.compositor);
2393
Armin Krezović78895c52016-10-09 17:30:28 +02002394 if (b->theme)
2395 theme_destroy(b->theme);
2396
2397 if (b->frame_device)
2398 cairo_device_destroy(b->frame_device);
2399
2400 wl_cursor_theme_destroy(b->cursor_theme);
2401
Daniel Stone698f9bf2016-11-24 15:31:33 +00002402 wl_registry_destroy(b->parent.registry);
2403 wl_display_flush(b->parent.wl_display);
2404 wl_display_disconnect(b->parent.wl_display);
2405
Giulio Camuffo954f1832014-10-11 18:27:30 +03002406 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002407}
2408
Jason Ekstrand7744f712013-10-27 22:24:55 -05002409static const char *left_ptrs[] = {
2410 "left_ptr",
2411 "default",
2412 "top_left_arrow",
2413 "left-arrow"
2414};
2415
2416static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002417create_cursor(struct wayland_backend *b,
2418 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002419{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002420 unsigned int i;
2421
Benoit Gschwind244ff792016-04-28 20:33:11 +02002422 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2423 config->cursor_size,
2424 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002425 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002426 fprintf(stderr, "could not load cursor theme\n");
2427 return;
2428 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002429
Giulio Camuffo954f1832014-10-11 18:27:30 +03002430 b->cursor = NULL;
2431 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2432 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002433 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002434 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002435 fprintf(stderr, "could not load left cursor\n");
2436 return;
2437 }
2438}
2439
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002440static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002441fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2442 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002443{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002444 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002445 struct wayland_input *input = NULL;
2446
Giulio Camuffo954f1832014-10-11 18:27:30 +03002447 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002448 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002449 break;
2450
2451 if (!input || !input->output)
2452 return;
2453
2454 if (input->output->frame)
2455 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2456 else
2457 wayland_output_set_windowed(input->output);
2458
2459 weston_output_schedule_repaint(&input->output->base);
2460}
2461
Giulio Camuffo954f1832014-10-11 18:27:30 +03002462static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002463wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002464 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002465{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002466 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002467 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002468 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002469
Giulio Camuffo954f1832014-10-11 18:27:30 +03002470 b = zalloc(sizeof *b);
2471 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002472 return NULL;
2473
Giulio Camuffo954f1832014-10-11 18:27:30 +03002474 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002475 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002476 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002477
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002478 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002479 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002480 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002481 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002482 }
2483
Giulio Camuffo954f1832014-10-11 18:27:30 +03002484 wl_list_init(&b->parent.output_list);
2485 wl_list_init(&b->input_list);
2486 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2487 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2488 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002489
Benoit Gschwind244ff792016-04-28 20:33:11 +02002490 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002491
Armin Krezovićb08e1a52016-12-09 22:58:27 +01002492#ifdef ENABLE_EGL
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002493 b->use_pixman = new_config->use_pixman;
Armin Krezovićb08e1a52016-12-09 22:58:27 +01002494#else
2495 b->use_pixman = true;
2496#endif
Armin Krezović174448a2016-09-30 14:11:09 +02002497 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002498
Giulio Camuffo954f1832014-10-11 18:27:30 +03002499 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002500 gl_renderer = weston_load_module("gl-renderer.so",
2501 "gl_renderer_interface");
2502 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002503 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002504 }
2505
Giulio Camuffo954f1832014-10-11 18:27:30 +03002506 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002507 if (gl_renderer->display_create(compositor,
2508 EGL_PLATFORM_WAYLAND_KHR,
2509 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002510 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002511 gl_renderer->alpha_attribs,
2512 NULL,
2513 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002514 weston_log("Failed to initialize the GL renderer; "
2515 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002516 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002517 }
2518 }
2519
Giulio Camuffo954f1832014-10-11 18:27:30 +03002520 if (b->use_pixman) {
2521 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002522 weston_log("Failed to initialize pixman renderer\n");
2523 goto err_display;
2524 }
2525 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002526
Giulio Camuffo954f1832014-10-11 18:27:30 +03002527 b->base.destroy = wayland_destroy;
2528 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002529
Giulio Camuffo954f1832014-10-11 18:27:30 +03002530 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002531
Giulio Camuffo954f1832014-10-11 18:27:30 +03002532 fd = wl_display_get_fd(b->parent.wl_display);
2533 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002534 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002535 wayland_backend_handle_event, b);
2536 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002537 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002538
Giulio Camuffo954f1832014-10-11 18:27:30 +03002539 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002540
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002541 if (compositor->renderer->import_dmabuf) {
2542 if (linux_dmabuf_setup(compositor) < 0)
2543 weston_log("Error: initializing dmabuf "
2544 "support failed.\n");
2545 }
2546
Giulio Camuffo954f1832014-10-11 18:27:30 +03002547 compositor->backend = &b->base;
2548 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002549err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002550 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002551err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002552 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002553 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002554 return NULL;
2555}
2556
2557static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002558wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002559{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002560 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002561
Giulio Camuffo954f1832014-10-11 18:27:30 +03002562 if (b->theme)
2563 theme_destroy(b->theme);
2564 if (b->frame_device)
2565 cairo_device_destroy(b->frame_device);
2566 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002567
Giulio Camuffo954f1832014-10-11 18:27:30 +03002568 weston_compositor_shutdown(b->compositor);
2569 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002570}
2571
Armin Krezović174448a2016-09-30 14:11:09 +02002572static const struct weston_windowed_output_api windowed_api = {
2573 wayland_output_set_size,
2574 wayland_output_create,
2575};
2576
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002577static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002578config_init_to_defaults(struct weston_wayland_backend_config *config)
2579{
2580}
2581
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002582WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01002583weston_backend_init(struct weston_compositor *compositor,
2584 struct weston_backend_config *config_base)
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002585{
2586 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002587 struct wayland_parent_output *poutput;
2588 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002589 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002590
Benoit Gschwinde091b452016-05-10 22:47:48 +02002591 if (config_base == NULL ||
2592 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2593 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2594 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002595 return -1;
2596 }
2597
2598 config_init_to_defaults(&new_config);
2599 memcpy(&new_config, config_base, config_base->struct_size);
2600
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002601 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002602
Giulio Camuffo954f1832014-10-11 18:27:30 +03002603 if (!b)
2604 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002605
Benoit Gschwind37a68072016-04-28 20:33:08 +02002606 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002607 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002608 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002609
Giulio Camuffo954f1832014-10-11 18:27:30 +03002610 wl_list_for_each(poutput, &b->parent.output_list, link)
2611 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002612
Giulio Camuffo954f1832014-10-11 18:27:30 +03002613 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002614 }
2615
Benoit Gschwind37a68072016-04-28 20:33:08 +02002616 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002617 if (wayland_output_create_fullscreen(b) < 0) {
2618 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002619 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002620 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002621
Giulio Camuffo954f1832014-10-11 18:27:30 +03002622 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002623 }
2624
Armin Krezović174448a2016-09-30 14:11:09 +02002625 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2626 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002627
Armin Krezović174448a2016-09-30 14:11:09 +02002628 if (ret < 0) {
2629 weston_log("Failed to register output API.\n");
2630 wayland_backend_destroy(b);
2631 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002632 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002633
Giulio Camuffo954f1832014-10-11 18:27:30 +03002634 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002635 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002636 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002637 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002638
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002639err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002640 wayland_backend_destroy(b);
2641 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002642}