blob: d9cbde5db80b882fc84e805728da807e17349bf9 [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>
41#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050042#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010043
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010044#include "compositor.h"
Benoit Gschwind639322a2016-04-28 20:33:15 +020045#include "compositor-wayland.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010046#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020047#include "weston-egl-ext.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050048#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070050#include "shared/image-loader.h"
51#include "shared/os-compatibility.h"
52#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080053#include "fullscreen-shell-unstable-v1-client-protocol.h"
Armin Krezović34476192016-11-21 18:42:42 +010054#include "xdg-shell-unstable-v6-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020055#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020056#include "linux-dmabuf.h"
Armin Krezović174448a2016-09-30 14:11:09 +020057#include "windowed-output-api.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058
Jason Ekstrand48ce4212013-10-27 22:25:02 -050059#define WINDOW_TITLE "Weston Compositor"
60
Giulio Camuffo954f1832014-10-11 18:27:30 +030061struct wayland_backend {
62 struct weston_backend base;
63 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010064
65 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040066 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040067 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010068 struct wl_compositor *compositor;
69 struct wl_shell *shell;
Armin Krezović34476192016-11-21 18:42:42 +010070 struct zxdg_shell_v6 *xdg_shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080071 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020072 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050074 struct wl_list output_list;
75
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076 struct wl_event_source *wl_source;
77 uint32_t event_mask;
78 } parent;
79
Armin Krezović7e71b872016-10-09 17:30:22 +020080 bool use_pixman;
Armin Krezović7f1c0b82016-10-09 17:30:23 +020081 bool sprawl_across_outputs;
Armin Krezović20450162016-10-13 12:01:43 +020082 bool fullscreen;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050083
Jason Ekstrand7744f712013-10-27 22:24:55 -050084 struct theme *theme;
85 cairo_device_t *frame_device;
86 struct wl_cursor_theme *cursor_theme;
87 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050088
Jason Ekstrand06ced802013-11-07 20:13:29 -060089 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010090};
91
92struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050093 struct weston_output base;
94
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010095 struct {
Armin Krezović2d321e32016-10-09 17:30:25 +020096 bool draw_initial_frame;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050097 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050098
99 struct wl_output *output;
100 uint32_t global_id;
101
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500102 struct wl_shell_surface *shell_surface;
Armin Krezović34476192016-11-21 18:42:42 +0100103 struct zxdg_surface_v6 *xdg_surface;
104 struct zxdg_toplevel_v6 *xdg_toplevel;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600105 int configure_width, configure_height;
Armin Krezović34476192016-11-21 18:42:42 +0100106 bool wait_for_configure;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100107 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500108
Jason Ekstrand7744f712013-10-27 22:24:55 -0500109 int keyboard_count;
110
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600111 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500112 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500113
Jason Ekstrand7744f712013-10-27 22:24:55 -0500114 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500115 struct wl_egl_window *egl_window;
116 struct {
117 cairo_surface_t *top;
118 cairo_surface_t *left;
119 cairo_surface_t *right;
120 cairo_surface_t *bottom;
121 } border;
122 } gl;
123
124 struct {
125 struct wl_list buffers;
126 struct wl_list free_buffers;
127 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500128
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500129 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500130 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100131};
132
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500133struct wayland_parent_output {
134 struct wayland_output *output;
135 struct wl_list link;
136
137 struct wl_output *global;
138 uint32_t id;
139
140 struct {
141 char *make;
142 char *model;
143 int32_t width, height;
144 uint32_t subpixel;
145 } physical;
146
147 int32_t x, y;
148 uint32_t transform;
149 uint32_t scale;
150
151 struct wl_list mode_list;
152 struct weston_mode *preferred_mode;
153 struct weston_mode *current_mode;
154};
155
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500156struct wayland_shm_buffer {
157 struct wayland_output *output;
158 struct wl_list link;
159 struct wl_list free_link;
160
161 struct wl_buffer *buffer;
162 void *data;
163 size_t size;
164 pixman_region32_t damage;
165 int frame_damaged;
166
167 pixman_image_t *pm_image;
168 cairo_surface_t *c_surface;
169};
170
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100171struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400172 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300173 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100174 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500175
176 struct {
177 struct wl_seat *seat;
178 struct wl_pointer *pointer;
179 struct wl_keyboard *keyboard;
180 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500181
182 struct {
183 struct wl_surface *surface;
184 int32_t hx, hy;
185 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500186 } parent;
187
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500188 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100189 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400190 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600191 uint32_t touch_points;
192 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600193 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600194 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600195
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400196 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600197 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500198 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000199
200 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100201};
202
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300203struct gl_renderer_interface *gl_renderer;
204
Armin Krezović938dc522016-08-01 19:17:57 +0200205static inline struct wayland_output *
206to_wayland_output(struct weston_output *base)
207{
208 return container_of(base, struct wayland_output, base);
209}
210
211static inline struct wayland_backend *
212to_wayland_backend(struct weston_compositor *base)
213{
214 return container_of(base->backend, struct wayland_backend, base);
215}
216
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500217static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500218wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
219{
220 cairo_surface_destroy(buffer->c_surface);
221 pixman_image_unref(buffer->pm_image);
222
223 wl_buffer_destroy(buffer->buffer);
224 munmap(buffer->data, buffer->size);
225
226 pixman_region32_fini(&buffer->damage);
227
228 wl_list_remove(&buffer->link);
229 wl_list_remove(&buffer->free_link);
230 free(buffer);
231}
232
233static void
234buffer_release(void *data, struct wl_buffer *buffer)
235{
236 struct wayland_shm_buffer *sb = data;
237
238 if (sb->output) {
239 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
240 } else {
241 wayland_shm_buffer_destroy(sb);
242 }
243}
244
245static const struct wl_buffer_listener buffer_listener = {
246 buffer_release
247};
248
249static struct wayland_shm_buffer *
250wayland_output_get_shm_buffer(struct wayland_output *output)
251{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300252 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200253 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300254 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500255 struct wayland_shm_buffer *sb;
256
257 struct wl_shm_pool *pool;
258 int width, height, stride;
259 int32_t fx, fy;
260 int fd;
261 unsigned char *data;
262
263 if (!wl_list_empty(&output->shm.free_buffers)) {
264 sb = container_of(output->shm.free_buffers.next,
265 struct wayland_shm_buffer, free_link);
266 wl_list_remove(&sb->free_link);
267 wl_list_init(&sb->free_link);
268
269 return sb;
270 }
271
272 if (output->frame) {
273 width = frame_width(output->frame);
274 height = frame_height(output->frame);
275 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500276 width = output->base.current_mode->width;
277 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500278 }
279
280 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
281
282 fd = os_create_anonymous_file(height * stride);
283 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000284 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500285 return NULL;
286 }
287
288 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
289 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000290 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500291 close(fd);
292 return NULL;
293 }
294
295 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000296 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200297 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000298 close(fd);
299 free(data);
300 return NULL;
301 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500302
303 sb->output = output;
304 wl_list_init(&sb->free_link);
305 wl_list_insert(&output->shm.buffers, &sb->link);
306
307 pixman_region32_init_rect(&sb->damage, 0, 0,
308 output->base.width, output->base.height);
309 sb->frame_damaged = 1;
310
311 sb->data = data;
312 sb->size = height * stride;
313
314 pool = wl_shm_create_pool(shm, fd, sb->size);
315
316 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
317 width, height,
318 stride,
319 WL_SHM_FORMAT_ARGB8888);
320 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
321 wl_shm_pool_destroy(pool);
322 close(fd);
323
324 memset(data, 0, sb->size);
325
326 sb->c_surface =
327 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
328 width, height, stride);
329
330 fx = 0;
331 fy = 0;
332 if (output->frame)
333 frame_interior(output->frame, &fx, &fy, 0, 0);
334 sb->pm_image =
335 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
336 (uint32_t *)(data + fy * stride) + fx,
337 stride);
338
339 return sb;
340}
341
342static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500343frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400344{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500345 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400346 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400347
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500348 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400349
350 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300351
352 /*
353 * This is the fallback case, where Presentation extension is not
354 * available from the parent compositor. We do not know the base for
355 * 'time', so we cannot feed it to finish_frame(). Do the only thing
356 * we can, and pretend finish_frame time is when we process this
357 * event.
358 */
359 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200360 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400361}
362
363static const struct wl_callback_listener frame_listener = {
364 frame_done
365};
366
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500367static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200368draw_initial_frame(struct wayland_output *output)
369{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500370 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200371
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500372 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200373
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500374 /* If we are rendering with GL, then orphan it so that it gets
375 * destroyed immediately */
376 if (output->gl.egl_window)
377 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500378
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500380 wl_surface_damage(output->parent.surface, 0, 0,
381 output->base.current_mode->width,
382 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200383}
384
385static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386wayland_output_update_gl_border(struct wayland_output *output)
387{
388 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
389 cairo_t *cr;
390
391 if (!output->frame)
392 return;
393 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
394 return;
395
396 fwidth = frame_width(output->frame);
397 fheight = frame_height(output->frame);
398 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
399
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500400 if (!output->gl.border.top)
401 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500402 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
403 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500404 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500405 frame_repaint(output->frame, cr);
406 cairo_destroy(cr);
407 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
408 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 cairo_image_surface_get_stride(output->gl.border.top) / 4,
410 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500411
412
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500413 if (!output->gl.border.left)
414 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500415 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
416 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500417 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500418 cairo_translate(cr, 0, -iy);
419 frame_repaint(output->frame, cr);
420 cairo_destroy(cr);
421 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
422 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500423 cairo_image_surface_get_stride(output->gl.border.left) / 4,
424 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500425
426
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500427 if (!output->gl.border.right)
428 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500429 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
430 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500431 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500432 cairo_translate(cr, -(iwidth + ix), -iy);
433 frame_repaint(output->frame, cr);
434 cairo_destroy(cr);
435 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
436 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500437 cairo_image_surface_get_stride(output->gl.border.right) / 4,
438 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500439
440
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500441 if (!output->gl.border.bottom)
442 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500443 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
444 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500446 cairo_translate(cr, 0, -(iy + iheight));
447 frame_repaint(output->frame, cr);
448 cairo_destroy(cr);
449 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
450 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500451 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
452 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500453}
454
455static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200456wayland_output_start_repaint_loop(struct weston_output *output_base)
457{
Armin Krezović938dc522016-08-01 19:17:57 +0200458 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300459 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200460 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200461 struct wl_callback *callback;
462
463 /* If this is the initial frame, we need to attach a buffer so that
464 * the compositor can map the surface and include it in its render
465 * loop. If the surface doesn't end up in the render loop, the frame
466 * callback won't be invoked. The buffer is transparent and of the
467 * same size as the future real output buffer. */
468 if (output->parent.draw_initial_frame) {
Armin Krezović2d321e32016-10-09 17:30:25 +0200469 output->parent.draw_initial_frame = false;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200470
471 draw_initial_frame(output);
472 }
473
474 callback = wl_surface_frame(output->parent.surface);
475 wl_callback_add_listener(callback, &frame_listener, output);
476 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300477 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200478}
479
David Herrmann1edf44c2013-10-22 17:11:26 +0200480static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500481wayland_output_repaint_gl(struct weston_output *output_base,
482 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400483{
Armin Krezović938dc522016-08-01 19:17:57 +0200484 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400485 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400486 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600487
Kristian Høgsberg33418202011-08-16 23:01:28 -0400488 callback = wl_surface_frame(output->parent.surface);
489 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100490
Jason Ekstrand7744f712013-10-27 22:24:55 -0500491 wayland_output_update_gl_border(output);
492
Pekka Paalanenbc106382012-10-10 12:49:31 +0300493 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200494
495 pixman_region32_subtract(&ec->primary_plane.damage,
496 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200497 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100498}
499
Matt Roper361d2ad2011-08-29 13:52:23 -0700500static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500501wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
502{
503 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
504 cairo_t *cr;
505
506 if (!buffer->output->frame || !buffer->frame_damaged)
507 return;
508
509 cr = cairo_create(buffer->c_surface);
510
511 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
512 fwidth = frame_width(buffer->output->frame);
513 fheight = frame_height(buffer->output->frame);
514
515 /* Set the clip so we don't unnecisaraly damage the surface */
516 cairo_move_to(cr, ix, iy);
517 cairo_rel_line_to(cr, iwidth, 0);
518 cairo_rel_line_to(cr, 0, iheight);
519 cairo_rel_line_to(cr, -iwidth, 0);
520 cairo_line_to(cr, ix, iy);
521 cairo_line_to(cr, 0, iy);
522 cairo_line_to(cr, 0, fheight);
523 cairo_line_to(cr, fwidth, fheight);
524 cairo_line_to(cr, fwidth, 0);
525 cairo_line_to(cr, 0, 0);
526 cairo_line_to(cr, 0, iy);
527 cairo_close_path(cr);
528 cairo_clip(cr);
529
530 /* Draw using a pattern so that the final result gets clipped */
531 cairo_push_group(cr);
532 frame_repaint(buffer->output->frame, cr);
533 cairo_pop_group_to_source(cr);
534 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
535 cairo_paint(cr);
536
537 cairo_destroy(cr);
538}
539
540static void
541wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
542{
543 pixman_region32_t damage;
544 pixman_box32_t *rects;
545 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
546 int i, n;
547
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500548 pixman_region32_init(&damage);
549 weston_transformed_region(sb->output->base.width,
550 sb->output->base.height,
551 sb->output->base.transform,
552 sb->output->base.current_scale,
553 &sb->damage, &damage);
554
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500555 if (sb->output->frame) {
556 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
557 fwidth = frame_width(sb->output->frame);
558 fheight = frame_height(sb->output->frame);
559
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500560 pixman_region32_translate(&damage, ix, iy);
561
562 if (sb->frame_damaged) {
563 pixman_region32_union_rect(&damage, &damage,
564 0, 0, fwidth, iy);
565 pixman_region32_union_rect(&damage, &damage,
566 0, iy, ix, iheight);
567 pixman_region32_union_rect(&damage, &damage,
568 ix + iwidth, iy,
569 fwidth - (ix + iwidth), iheight);
570 pixman_region32_union_rect(&damage, &damage,
571 0, iy + iheight,
572 fwidth, fheight - (iy + iheight));
573 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500574 }
575
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500576 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500577 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
578 for (i = 0; i < n; ++i)
579 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
580 rects[i].y1, rects[i].x2 - rects[i].x1,
581 rects[i].y2 - rects[i].y1);
582
583 if (sb->output->frame)
584 pixman_region32_fini(&damage);
585}
586
587static int
588wayland_output_repaint_pixman(struct weston_output *output_base,
589 pixman_region32_t *damage)
590{
Armin Krezović938dc522016-08-01 19:17:57 +0200591 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300592 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200593 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500594 struct wl_callback *callback;
595 struct wayland_shm_buffer *sb;
596
597 if (output->frame) {
598 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
599 wl_list_for_each(sb, &output->shm.buffers, link)
600 sb->frame_damaged = 1;
601 }
602
603 wl_list_for_each(sb, &output->shm.buffers, link)
604 pixman_region32_union(&sb->damage, &sb->damage, damage);
605
606 sb = wayland_output_get_shm_buffer(output);
607
608 wayland_output_update_shm_border(sb);
609 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300610 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500611
612 wayland_shm_buffer_attach(sb);
613
614 callback = wl_surface_frame(output->parent.surface);
615 wl_callback_add_listener(callback, &frame_listener, output);
616 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300617 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500618
619 pixman_region32_fini(&sb->damage);
620 pixman_region32_init(&sb->damage);
621 sb->frame_damaged = 0;
622
Giulio Camuffo954f1832014-10-11 18:27:30 +0300623 pixman_region32_subtract(&b->compositor->primary_plane.damage,
624 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500625 return 0;
626}
627
628static void
Armin Krezović174448a2016-09-30 14:11:09 +0200629wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700630{
Armin Krezović34476192016-11-21 18:42:42 +0100631 if (output->parent.xdg_toplevel)
632 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
633
634 if (output->parent.xdg_surface)
635 zxdg_surface_v6_destroy(output->parent.xdg_surface);
636
Armin Krezović174448a2016-09-30 14:11:09 +0200637 if (output->parent.shell_surface)
638 wl_shell_surface_destroy(output->parent.shell_surface);
639
640 wl_surface_destroy(output->parent.surface);
641}
642
Armin Krezović2e662522016-10-09 17:30:29 +0200643static void
644wayland_output_destroy_shm_buffers(struct wayland_output *output)
645{
646 struct wayland_shm_buffer *buffer, *next;
647
648 /* Throw away any remaining SHM buffers */
649 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
650 wayland_shm_buffer_destroy(buffer);
651 /* These will get thrown away when they get released */
652 wl_list_for_each(buffer, &output->shm.buffers, link)
653 buffer->output = NULL;
654}
655
Armin Krezović174448a2016-09-30 14:11:09 +0200656static int
657wayland_output_disable(struct weston_output *base)
658{
659 struct wayland_output *output = to_wayland_output(base);
660 struct wayland_backend *b = to_wayland_backend(base->compositor);
661
662 if (!output->base.enabled)
663 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700664
Giulio Camuffo954f1832014-10-11 18:27:30 +0300665 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200666 pixman_renderer_output_destroy(&output->base);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500667 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200668 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100669 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500670 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100671
Armin Krezović2e662522016-10-09 17:30:29 +0200672 wayland_output_destroy_shm_buffers(output);
673
Armin Krezovićf054d352016-10-09 17:30:27 +0200674 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500675
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600676 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500677 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600678
679 cairo_surface_destroy(output->gl.border.top);
680 cairo_surface_destroy(output->gl.border.left);
681 cairo_surface_destroy(output->gl.border.right);
682 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500683
Armin Krezović174448a2016-09-30 14:11:09 +0200684 return 0;
685}
Matt Roper361d2ad2011-08-29 13:52:23 -0700686
Armin Krezović174448a2016-09-30 14:11:09 +0200687static void
688wayland_output_destroy(struct weston_output *base)
689{
690 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +0200691
692 wayland_output_disable(&output->base);
693
Armin Krezović174448a2016-09-30 14:11:09 +0200694 weston_output_destroy(&output->base);
695
696 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700697}
698
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300699static const struct wl_shell_surface_listener shell_surface_listener;
700
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200701static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500702wayland_output_init_gl_renderer(struct wayland_output *output)
703{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500704 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500705
706 if (output->frame) {
707 fwidth = frame_width(output->frame);
708 fheight = frame_height(output->frame);
709 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500710 fwidth = output->base.current_mode->width;
711 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500712 }
713
714 output->gl.egl_window =
715 wl_egl_window_create(output->parent.surface,
716 fwidth, fheight);
717 if (!output->gl.egl_window) {
718 weston_log("failure to create wl_egl_window\n");
719 return -1;
720 }
721
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200722 if (gl_renderer->output_window_create(&output->base,
723 output->gl.egl_window,
724 output->gl.egl_window,
725 gl_renderer->alpha_attribs,
726 NULL,
727 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500728 goto cleanup_window;
729
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500730 return 0;
731
732cleanup_window:
733 wl_egl_window_destroy(output->gl.egl_window);
734 return -1;
735}
736
737static int
738wayland_output_init_pixman_renderer(struct wayland_output *output)
739{
740 return pixman_renderer_output_create(&output->base);
741}
742
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600743static void
744wayland_output_resize_surface(struct wayland_output *output)
745{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300746 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200747 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600748 int32_t ix, iy, iwidth, iheight;
749 int32_t width, height;
750 struct wl_region *region;
751
752 width = output->base.current_mode->width;
753 height = output->base.current_mode->height;
754
755 if (output->frame) {
756 frame_resize_inside(output->frame, width, height);
757
758 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300759 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600760 wl_region_add(region, ix, iy, iwidth, iheight);
761 wl_surface_set_input_region(output->parent.surface, region);
762 wl_region_destroy(region);
763
764 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300765 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600766 wl_region_add(region, ix, iy, iwidth, iheight);
767 wl_surface_set_opaque_region(output->parent.surface, region);
768 wl_region_destroy(region);
769
770 width = frame_width(output->frame);
771 height = frame_height(output->frame);
772 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300773 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600774 wl_region_add(region, 0, 0, width, height);
775 wl_surface_set_input_region(output->parent.surface, region);
776 wl_region_destroy(region);
777
Giulio Camuffo954f1832014-10-11 18:27:30 +0300778 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600779 wl_region_add(region, 0, 0, width, height);
780 wl_surface_set_opaque_region(output->parent.surface, region);
781 wl_region_destroy(region);
782 }
783
784 if (output->gl.egl_window) {
785 wl_egl_window_resize(output->gl.egl_window,
786 width, height, 0, 0);
787
788 /* These will need to be re-created due to the resize */
789 gl_renderer->output_set_border(&output->base,
790 GL_RENDERER_BORDER_TOP,
791 0, 0, 0, NULL);
792 cairo_surface_destroy(output->gl.border.top);
793 output->gl.border.top = NULL;
794 gl_renderer->output_set_border(&output->base,
795 GL_RENDERER_BORDER_LEFT,
796 0, 0, 0, NULL);
797 cairo_surface_destroy(output->gl.border.left);
798 output->gl.border.left = NULL;
799 gl_renderer->output_set_border(&output->base,
800 GL_RENDERER_BORDER_RIGHT,
801 0, 0, 0, NULL);
802 cairo_surface_destroy(output->gl.border.right);
803 output->gl.border.right = NULL;
804 gl_renderer->output_set_border(&output->base,
805 GL_RENDERER_BORDER_BOTTOM,
806 0, 0, 0, NULL);
807 cairo_surface_destroy(output->gl.border.bottom);
808 output->gl.border.bottom = NULL;
809 }
810
Armin Krezović2e662522016-10-09 17:30:29 +0200811 wayland_output_destroy_shm_buffers(output);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600812}
813
814static int
815wayland_output_set_windowed(struct wayland_output *output)
816{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300817 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200818 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600819 int tlen;
820 char *title;
821
822 if (output->frame)
823 return 0;
824
825 if (output->name) {
826 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
827 title = malloc(tlen + 1);
828 if (!title)
829 return -1;
830
831 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
832 } else {
833 title = strdup(WINDOW_TITLE);
834 }
835
Armin Krezović34476192016-11-21 18:42:42 +0100836 if (output->parent.xdg_toplevel)
837 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, title);
838
Giulio Camuffo954f1832014-10-11 18:27:30 +0300839 if (!b->theme) {
840 b->theme = theme_create();
841 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600842 free(title);
843 return -1;
844 }
845 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300846 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600847 FRAME_BUTTON_CLOSE, title);
848 free(title);
849 if (!output->frame)
850 return -1;
851
852 if (output->keyboard_count)
853 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
854
855 wayland_output_resize_surface(output);
856
Armin Krezović34476192016-11-21 18:42:42 +0100857 if (output->parent.shell_surface)
858 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600859
860 return 0;
861}
862
863static void
864wayland_output_set_fullscreen(struct wayland_output *output,
865 enum wl_shell_surface_fullscreen_method method,
866 uint32_t framerate, struct wl_output *target)
867{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300868 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200869 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500870
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600871 if (output->frame) {
872 frame_destroy(output->frame);
873 output->frame = NULL;
874 }
875
876 wayland_output_resize_surface(output);
877
Armin Krezović34476192016-11-21 18:42:42 +0100878 if (output->parent.xdg_toplevel) {
879 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
880 } else if (output->parent.shell_surface) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500881 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
882 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300883 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800884 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
885 output->parent.surface,
886 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500887 }
888}
889
890static struct weston_mode *
891wayland_output_choose_mode(struct wayland_output *output,
892 struct weston_mode *ref_mode)
893{
894 struct weston_mode *mode;
895
896 /* First look for an exact match */
897 wl_list_for_each(mode, &output->base.mode_list, link)
898 if (mode->width == ref_mode->width &&
899 mode->height == ref_mode->height &&
900 mode->refresh == ref_mode->refresh)
901 return mode;
902
903 /* If we can't find an exact match, ignore refresh and try again */
904 wl_list_for_each(mode, &output->base.mode_list, link)
905 if (mode->width == ref_mode->width &&
906 mode->height == ref_mode->height)
907 return mode;
908
909 /* Yeah, we failed */
910 return NULL;
911}
912
913enum mode_status {
914 MODE_STATUS_UNKNOWN,
915 MODE_STATUS_SUCCESS,
916 MODE_STATUS_FAIL,
917 MODE_STATUS_CANCEL,
918};
919
920static void
921mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800922 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500923{
924 enum mode_status *value = data;
925
926 printf("Mode switch successful\n");
927
928 *value = MODE_STATUS_SUCCESS;
929}
930
931static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800932mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500933{
934 enum mode_status *value = data;
935
936 printf("Mode switch failed\n");
937
938 *value = MODE_STATUS_FAIL;
939}
940
941static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800942mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500943{
944 enum mode_status *value = data;
945
946 printf("Mode switch cancelled\n");
947
948 *value = MODE_STATUS_CANCEL;
949}
950
Jonas Ådahl496adb32015-11-17 16:00:27 +0800951struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500952 mode_feedback_successful,
953 mode_feedback_failed,
954 mode_feedback_cancelled,
955};
956
957static int
958wayland_output_switch_mode(struct weston_output *output_base,
959 struct weston_mode *mode)
960{
Armin Krezović938dc522016-08-01 19:17:57 +0200961 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500963 struct wl_surface *old_surface;
964 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800965 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500966 enum mode_status mode_status;
967 int ret = 0;
968
969 if (output_base == NULL) {
970 weston_log("output is NULL.\n");
971 return -1;
972 }
973
974 if (mode == NULL) {
975 weston_log("mode is NULL.\n");
976 return -1;
977 }
978
Armin Krezović938dc522016-08-01 19:17:57 +0200979 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500980
Armin Krezović34476192016-11-21 18:42:42 +0100981 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500982 return -1;
983
984 mode = wayland_output_choose_mode(output, mode);
985 if (mode == NULL)
986 return -1;
987
988 if (output->base.current_mode == mode)
989 return 0;
990
991 old_mode = output->base.current_mode;
992 old_surface = output->parent.surface;
993 output->base.current_mode = mode;
994 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300995 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500996 wl_surface_set_user_data(output->parent.surface, output);
997
998 /* Blow the old buffers because we changed size/surfaces */
999 wayland_output_resize_surface(output);
1000
1001 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +08001002 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1003 output->parent.surface,
1004 output->parent.output,
1005 mode->refresh);
1006 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1007 &mode_feedback_listener,
1008 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001009
1010 /* This should kick-start things again */
Armin Krezović2d321e32016-10-09 17:30:25 +02001011 output->parent.draw_initial_frame = true;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001012 wayland_output_start_repaint_loop(&output->base);
1013
1014 mode_status = MODE_STATUS_UNKNOWN;
1015 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001016 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001017
Jonas Ådahl496adb32015-11-17 16:00:27 +08001018 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001019
1020 if (mode_status == MODE_STATUS_FAIL) {
1021 output->base.current_mode = old_mode;
1022 wl_surface_destroy(output->parent.surface);
1023 output->parent.surface = old_surface;
1024 wayland_output_resize_surface(output);
1025
1026 return -1;
1027 }
1028
1029 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1030 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1031
Giulio Camuffo954f1832014-10-11 18:27:30 +03001032 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001033 pixman_renderer_output_destroy(output_base);
1034 if (wayland_output_init_pixman_renderer(output) < 0)
1035 goto err_output;
1036 } else {
1037 gl_renderer->output_destroy(output_base);
1038 wl_egl_window_destroy(output->gl.egl_window);
1039 if (wayland_output_init_gl_renderer(output) < 0)
1040 goto err_output;
1041 }
1042 wl_surface_destroy(old_surface);
1043
1044 weston_output_schedule_repaint(&output->base);
1045
1046 return 0;
1047
1048err_output:
1049 /* XXX */
1050 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001051}
1052
Armin Krezović34476192016-11-21 18:42:42 +01001053static void
1054handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1055 uint32_t serial)
1056{
1057 zxdg_surface_v6_ack_configure(surface, serial);
1058}
1059
1060static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1061 handle_xdg_surface_configure
1062};
1063
1064static void
1065handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1066 int32_t width, int32_t height,
1067 struct wl_array *states)
1068{
1069 struct wayland_output *output = data;
1070
1071 output->parent.configure_width = width;
1072 output->parent.configure_height = height;
1073
1074 output->parent.wait_for_configure = false;
1075 /* FIXME: implement resizing */
1076}
1077
1078static void
1079handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1080{
1081}
1082
1083static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1084 handle_xdg_toplevel_configure,
1085 handle_xdg_toplevel_close,
1086};
1087
Armin Krezović174448a2016-09-30 14:11:09 +02001088static int
1089wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001090{
Armin Krezović174448a2016-09-30 14:11:09 +02001091 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001092
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001093 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001094 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001095 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001096 return -1;
1097
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001098 wl_surface_set_user_data(output->parent.surface, output);
1099
Armin Krezović2d321e32016-10-09 17:30:25 +02001100 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001101
Armin Krezović34476192016-11-21 18:42:42 +01001102 if (b->parent.xdg_shell) {
1103 output->parent.xdg_surface =
1104 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1105 output->parent.surface);
1106 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1107 &xdg_surface_listener, output);
1108
1109 output->parent.xdg_toplevel =
1110 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1111 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1112 &xdg_toplevel_listener, output);
1113
1114 wl_surface_commit(output->parent.surface);
1115
1116 output->parent.wait_for_configure = true;
1117
1118 while (output->parent.wait_for_configure)
1119 wl_display_dispatch(b->parent.wl_display);
1120
1121 weston_log("wayland-backend: Using xdg_shell_v6\n");
1122 }
1123 else if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001124 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001125 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001126 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001127 if (!output->parent.shell_surface) {
1128 wl_surface_destroy(output->parent.surface);
1129 return -1;
1130 }
1131
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001132 wl_shell_surface_add_listener(output->parent.shell_surface,
1133 &shell_surface_listener, output);
Armin Krezović34476192016-11-21 18:42:42 +01001134
1135 weston_log("wayland-backend: Using wl_shell\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001136 }
1137
Armin Krezović174448a2016-09-30 14:11:09 +02001138 return 0;
1139}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001140
Armin Krezović174448a2016-09-30 14:11:09 +02001141static int
1142wayland_output_enable(struct weston_output *base)
1143{
1144 struct wayland_output *output = to_wayland_output(base);
1145 struct wayland_backend *b = to_wayland_backend(base->compositor);
1146 int ret = 0;
1147
Armin Krezović174448a2016-09-30 14:11:09 +02001148 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1149 output->base.current_mode->width,
1150 output->base.current_mode->height,
1151 output->base.x, output->base.y);
1152
Armin Krezovićf054d352016-10-09 17:30:27 +02001153 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001154 ret = wayland_backend_create_output_surface(output);
1155
1156 if (ret < 0)
1157 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001158
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001159 wl_list_init(&output->shm.buffers);
1160 wl_list_init(&output->shm.free_buffers);
1161
Giulio Camuffo954f1832014-10-11 18:27:30 +03001162 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001163 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001164 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001165
1166 output->base.repaint = wayland_output_repaint_pixman;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001167 } else {
1168 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001169 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001170
1171 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001172 }
1173
Armin Krezovićf16de172016-10-09 17:30:26 +02001174 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1175 output->base.assign_planes = NULL;
1176 output->base.set_backlight = NULL;
1177 output->base.set_dpms = NULL;
1178 output->base.switch_mode = wayland_output_switch_mode;
1179
Armin Krezović174448a2016-09-30 14:11:09 +02001180 if (b->sprawl_across_outputs) {
1181 wayland_output_set_fullscreen(output,
1182 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1183 output->mode.refresh, output->parent.output);
1184
Armin Krezović34476192016-11-21 18:42:42 +01001185 if (output->parent.xdg_toplevel) {
1186 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1187 output->parent.output);
1188 }
1189 else if (output->parent.shell_surface) {
Armin Krezović174448a2016-09-30 14:11:09 +02001190 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1191 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1192 output->mode.refresh, output->parent.output);
1193 } else if (b->parent.fshell) {
1194 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1195 output->parent.surface,
1196 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1197 output->parent.output);
1198 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1199 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1200 output->parent.surface,
1201 output->parent.output,
1202 output->mode.refresh));
1203 }
1204 } else if (b->fullscreen) {
1205 wayland_output_set_fullscreen(output, 0, 0, NULL);
1206 } else {
1207 wayland_output_set_windowed(output);
1208 }
1209
1210 return 0;
1211
1212err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001213 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001214
1215 return -1;
1216}
1217
1218static struct wayland_output *
1219wayland_output_create_common(void)
1220{
1221 struct wayland_output *output;
1222
1223 output = zalloc(sizeof *output);
1224 if (output == NULL) {
1225 perror("zalloc");
1226 return NULL;
1227 }
1228
Matt Roper361d2ad2011-08-29 13:52:23 -07001229 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001230 output->base.disable = wayland_output_disable;
1231 output->base.enable = wayland_output_enable;
1232
1233 return output;
1234}
1235
1236static int
1237wayland_output_create(struct weston_compositor *compositor, const char *name)
1238{
1239 struct wayland_output *output = wayland_output_create_common();
1240
1241 /* name can't be NULL. */
1242 assert(name);
1243
1244 output->base.name = strdup(name);
1245
Armin Krezović40087402016-09-30 14:11:12 +02001246 weston_output_init(&output->base, compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001247 weston_compositor_add_pending_output(&output->base, compositor);
1248
1249 return 0;
1250}
1251
1252static int
1253wayland_output_set_size(struct weston_output *base, int width, int height)
1254{
1255 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001256 int output_width, output_height;
1257
1258 /* We can only be called once. */
1259 assert(!output->base.current_mode);
1260
1261 /* Make sure we have scale set. */
1262 assert(output->base.scale);
1263
1264 if (width < 1) {
1265 weston_log("Invalid width \"%d\" for output %s\n",
1266 width, output->base.name);
1267 return -1;
1268 }
1269
1270 if (height < 1) {
1271 weston_log("Invalid height \"%d\" for output %s\n",
1272 height, output->base.name);
1273 return -1;
1274 }
1275
1276 output_width = width * output->base.scale;
1277 output_height = height * output->base.scale;
1278
1279 output->mode.flags =
1280 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1281
1282 output->mode.width = output_width;
1283 output->mode.height = output_height;
1284 output->mode.refresh = 60000;
1285 output->scale = output->base.scale;
1286 wl_list_init(&output->base.mode_list);
1287 wl_list_insert(&output->base.mode_list, &output->mode.link);
1288
1289 output->base.current_mode = &output->mode;
1290 output->base.make = "wayland";
1291 output->base.model = "none";
1292
1293 /* XXX: Calculate proper size. */
1294 output->base.mm_width = width;
1295 output->base.mm_height = height;
1296
Armin Krezović174448a2016-09-30 14:11:09 +02001297 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001298}
1299
Armin Krezović174448a2016-09-30 14:11:09 +02001300static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001301wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001302 struct wayland_parent_output *poutput)
1303{
Armin Krezović174448a2016-09-30 14:11:09 +02001304 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001305 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001306
1307 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001308
1309 if (poutput->current_mode) {
1310 mode = poutput->current_mode;
1311 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001312 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001313 } else if (!wl_list_empty(&poutput->mode_list)) {
1314 mode = container_of(poutput->mode_list.next,
1315 struct weston_mode, link);
1316 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001317 weston_log("No valid modes found. Skipping output.\n");
1318 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001319 }
1320
Armin Krezović40087402016-09-30 14:11:12 +02001321 weston_output_init(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001322
Armin Krezović174448a2016-09-30 14:11:09 +02001323 output->base.scale = 1;
1324 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001325
Armin Krezović174448a2016-09-30 14:11:09 +02001326 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1327 goto out;
1328
1329 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001330 output->parent.output = poutput->global;
1331
1332 output->base.make = poutput->physical.make;
1333 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001334
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001335 wl_list_init(&output->base.mode_list);
1336 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1337 wl_list_init(&poutput->mode_list);
1338
Armin Krezović174448a2016-09-30 14:11:09 +02001339 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001340
Armin Krezović174448a2016-09-30 14:11:09 +02001341 return 0;
1342
1343out:
1344 free(output->name);
1345 free(output);
1346
1347 return -1;
1348}
1349
1350static int
1351wayland_output_create_fullscreen(struct wayland_backend *b)
1352{
1353 struct wayland_output *output = wayland_output_create_common();
1354 int width = 0, height = 0;
1355
1356 output->base.name = strdup("wayland-fullscreen");
1357
Armin Krezović40087402016-09-30 14:11:12 +02001358 weston_output_init(&output->base, b->compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001359
1360 output->base.scale = 1;
1361 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1362
1363 if (wayland_backend_create_output_surface(output) < 0)
1364 goto err_surface;
1365
1366 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001367 if (b->parent.xdg_shell || b->parent.shell) {
1368 if (output->parent.xdg_toplevel)
1369 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1370 output->parent.output);
1371 else if (output->parent.shell_surface)
1372 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1373 0, 0, NULL);
1374
Armin Krezović174448a2016-09-30 14:11:09 +02001375 wl_display_roundtrip(b->parent.wl_display);
1376
1377 width = output->parent.configure_width;
1378 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001379 }
1380
Armin Krezović174448a2016-09-30 14:11:09 +02001381 if (wayland_output_set_size(&output->base, width, height) < 0)
1382 goto err_set_size;
1383
1384 weston_compositor_add_pending_output(&output->base, b->compositor);
1385
1386 return 0;
1387
1388err_set_size:
1389 wayland_backend_destroy_output_surface(output);
1390err_surface:
1391 free(output->name);
1392 free(output);
1393
1394 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001395}
1396
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001397static void
1398shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1399 uint32_t serial)
1400{
1401 wl_shell_surface_pong(shell_surface, serial);
1402}
1403
1404static void
1405shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1406 uint32_t edges, int32_t width, int32_t height)
1407{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001408 struct wayland_output *output = data;
1409
1410 output->parent.configure_width = width;
1411 output->parent.configure_height = height;
1412
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001413 /* FIXME: implement resizing */
1414}
1415
1416static void
1417shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1418{
1419}
1420
1421static const struct wl_shell_surface_listener shell_surface_listener = {
1422 shell_surface_ping,
1423 shell_surface_configure,
1424 shell_surface_popup_done
1425};
1426
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001427/* Events received from the wayland-server this compositor is client of: */
1428
Jason Ekstrand7744f712013-10-27 22:24:55 -05001429/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001430static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001431input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001432{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001433
Jason Ekstrand7744f712013-10-27 22:24:55 -05001434 struct wl_buffer *buffer;
1435 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001436
Giulio Camuffo954f1832014-10-11 18:27:30 +03001437 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001438 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001439
Giulio Camuffo954f1832014-10-11 18:27:30 +03001440 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001441 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001442 if (!buffer)
1443 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001444
1445 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1446 input->parent.cursor.surface,
1447 image->hotspot_x, image->hotspot_y);
1448
1449 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1450 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1451 image->width, image->height);
1452 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001453}
1454
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001455static void
Daniel Stone37816df2012-05-16 18:45:18 +01001456input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1457 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001458 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001459{
1460 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001461 int32_t fx, fy;
1462 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001463 double x, y;
1464
1465 x = wl_fixed_to_double(fixed_x);
1466 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001467
Daniel Stone50692802012-06-22 13:21:41 +01001468 /* XXX: If we get a modifier event immediately before the focus,
1469 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001470 input->enter_serial = serial;
1471 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001472
1473 if (input->output->frame) {
1474 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001475 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001476 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001477 x -= fx;
1478 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001479
1480 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1481 weston_output_schedule_repaint(&input->output->base);
1482 } else {
1483 location = THEME_LOCATION_CLIENT_AREA;
1484 }
1485
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001486 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001487
1488 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001489 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001490 notify_pointer_focus(&input->base, &input->output->base, x, y);
1491 wl_pointer_set_cursor(input->parent.pointer,
1492 input->enter_serial, NULL, 0, 0);
1493 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001494 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001495 notify_pointer_focus(&input->base, NULL, 0, 0);
1496 input_set_cursor(input);
1497 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001498}
1499
1500static void
Daniel Stone37816df2012-05-16 18:45:18 +01001501input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1502 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001503{
1504 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001505
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001506 if (!input->output)
1507 return;
1508
Jason Ekstrand7744f712013-10-27 22:24:55 -05001509 if (input->output->frame) {
1510 frame_pointer_leave(input->output->frame, input);
1511
1512 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1513 weston_output_schedule_repaint(&input->output->base);
1514 }
1515
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001516 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001517 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001518 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001519}
1520
1521static void
Daniel Stone37816df2012-05-16 18:45:18 +01001522input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001523 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001524{
1525 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001526 int32_t fx, fy;
1527 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001528 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001529 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001530
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001531 if (!input->output)
1532 return;
1533
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001534 x = wl_fixed_to_double(fixed_x);
1535 y = wl_fixed_to_double(fixed_y);
1536
Jason Ekstrand7744f712013-10-27 22:24:55 -05001537 if (input->output->frame) {
1538 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001539 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001540 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001541 x -= fx;
1542 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001543
1544 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1545 weston_output_schedule_repaint(&input->output->base);
1546 } else {
1547 location = THEME_LOCATION_CLIENT_AREA;
1548 }
1549
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001550 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001551
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001552 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001553 input_set_cursor(input);
1554 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001555 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001556 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001557 } else if (!input->has_focus &&
1558 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001559 wl_pointer_set_cursor(input->parent.pointer,
1560 input->enter_serial, NULL, 0, 0);
1561 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001562 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001563 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001564 }
1565
Peter Hutterer87743e92016-01-18 16:38:22 +10001566 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001567 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001568 want_frame = true;
1569 }
1570
1571 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1572 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001573}
1574
1575static void
1576input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001577 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001578 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001579{
1580 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001581 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001582
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001583 if (!input->output)
1584 return;
1585
Jason Ekstrand7744f712013-10-27 22:24:55 -05001586 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001587 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001588 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001589
1590 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001591 if (input->output->parent.xdg_toplevel)
1592 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001593 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001594 else if (input->output->parent.shell_surface)
1595 wl_shell_surface_move(input->output->parent.shell_surface,
1596 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001597 frame_status_clear(input->output->frame,
1598 FRAME_STATUS_MOVE);
1599 return;
1600 }
1601
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001602 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1603 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001604 input->output = NULL;
1605 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001606
Giulio Camuffo954f1832014-10-11 18:27:30 +03001607 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001608 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001609
1610 return;
1611 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001612
1613 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1614 weston_output_schedule_repaint(&input->output->base);
1615 } else {
1616 location = THEME_LOCATION_CLIENT_AREA;
1617 }
1618
Peter Hutterer87743e92016-01-18 16:38:22 +10001619 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001620 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001621 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1622 notify_pointer_frame(&input->base);
1623 }
Daniel Stone37816df2012-05-16 18:45:18 +01001624}
1625
1626static void
1627input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001628 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001629{
1630 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001631 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001632
Peter Hutterer89b6a492016-01-18 15:58:17 +10001633 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001634 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001635
Peter Hutterer87743e92016-01-18 16:38:22 +10001636 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1637 input->vert.has_discrete) {
1638 weston_event.has_discrete = true;
1639 weston_event.discrete = input->vert.discrete;
1640 input->vert.has_discrete = false;
1641 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1642 input->horiz.has_discrete) {
1643 weston_event.has_discrete = true;
1644 weston_event.discrete = input->horiz.discrete;
1645 input->horiz.has_discrete = false;
1646 }
1647
Peter Hutterer89b6a492016-01-18 15:58:17 +10001648 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001649
1650 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1651 notify_pointer_frame(&input->base);
1652}
1653
1654static void
1655input_handle_frame(void *data, struct wl_pointer *pointer)
1656{
1657 struct wayland_input *input = data;
1658
1659 notify_pointer_frame(&input->base);
1660}
1661
1662static void
1663input_handle_axis_source(void *data, struct wl_pointer *pointer,
1664 uint32_t source)
1665{
1666 struct wayland_input *input = data;
1667
1668 notify_axis_source(&input->base, source);
1669}
1670
1671static void
1672input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1673 uint32_t time, uint32_t axis)
1674{
1675 struct wayland_input *input = data;
1676 struct weston_pointer_axis_event weston_event;
1677
1678 weston_event.axis = axis;
1679 weston_event.value = 0;
1680
1681 notify_axis(&input->base, time, &weston_event);
1682}
1683
1684static void
1685input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1686 uint32_t axis, int32_t discrete)
1687{
1688 struct wayland_input *input = data;
1689
1690 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1691 input->vert.has_discrete = true;
1692 input->vert.discrete = discrete;
1693 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1694 input->horiz.has_discrete = true;
1695 input->horiz.discrete = discrete;
1696 }
Daniel Stone37816df2012-05-16 18:45:18 +01001697}
1698
1699static const struct wl_pointer_listener pointer_listener = {
1700 input_handle_pointer_enter,
1701 input_handle_pointer_leave,
1702 input_handle_motion,
1703 input_handle_button,
1704 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001705 input_handle_frame,
1706 input_handle_axis_source,
1707 input_handle_axis_stop,
1708 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001709};
1710
1711static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001712input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1713 int fd, uint32_t size)
1714{
1715 struct wayland_input *input = data;
1716 struct xkb_keymap *keymap;
1717 char *map_str;
1718
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001719 if (!data) {
1720 close(fd);
1721 return;
1722 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001723
1724 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1725 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1726 if (map_str == MAP_FAILED) {
1727 weston_log("mmap failed: %m\n");
1728 goto error;
1729 }
1730
Giulio Camuffo954f1832014-10-11 18:27:30 +03001731 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001732 map_str,
1733 XKB_KEYMAP_FORMAT_TEXT_V1,
1734 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001735 munmap(map_str, size);
1736
1737 if (!keymap) {
1738 weston_log("failed to compile keymap\n");
1739 goto error;
1740 }
1741
1742 input->keyboard_state_update = STATE_UPDATE_NONE;
1743 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1744 weston_log("No keymap provided; falling back to defalt\n");
1745 keymap = NULL;
1746 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1747 } else {
1748 weston_log("Invalid keymap\n");
1749 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001750 }
1751
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001752 close(fd);
1753
Derek Foreman1281a362015-07-31 16:55:32 -05001754 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001755 weston_seat_update_keymap(&input->base, keymap);
1756 else
1757 weston_seat_init_keyboard(&input->base, keymap);
1758
Ran Benitac9c74152014-08-19 23:59:52 +03001759 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001760
1761 return;
1762
1763error:
1764 wl_keyboard_release(input->parent.keyboard);
1765 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001766}
1767
1768static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001769input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001770 struct wl_keyboard *keyboard,
1771 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001772 struct wl_surface *surface,
1773 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001774{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001775 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001776 struct wayland_output *focus;
1777
1778 focus = input->keyboard_focus;
1779 if (focus) {
1780 /* This shouldn't happen */
1781 focus->keyboard_count--;
1782 if (!focus->keyboard_count && focus->frame)
1783 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1784 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1785 weston_output_schedule_repaint(&focus->base);
1786 }
1787
1788 input->keyboard_focus = wl_surface_get_user_data(surface);
1789 input->keyboard_focus->keyboard_count++;
1790
1791 focus = input->keyboard_focus;
1792 if (focus->frame) {
1793 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1794 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1795 weston_output_schedule_repaint(&focus->base);
1796 }
1797
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001798
Daniel Stone50692802012-06-22 13:21:41 +01001799 /* XXX: If we get a modifier event immediately before the focus,
1800 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001801 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001802 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001803}
1804
1805static void
1806input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001807 struct wl_keyboard *keyboard,
1808 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001809 struct wl_surface *surface)
1810{
1811 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001812 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001813
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001814 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001815
1816 focus = input->keyboard_focus;
1817 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001818 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001819
1820 focus->keyboard_count--;
1821 if (!focus->keyboard_count && focus->frame) {
1822 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1823 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1824 weston_output_schedule_repaint(&focus->base);
1825 }
1826
1827 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001828}
1829
Daniel Stone37816df2012-05-16 18:45:18 +01001830static void
1831input_handle_key(void *data, struct wl_keyboard *keyboard,
1832 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1833{
1834 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001835
Daniel Stone50692802012-06-22 13:21:41 +01001836 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001837 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001838 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001839 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001840 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001841}
1842
Daniel Stone351eb612012-05-31 15:27:47 -04001843static void
Derek Foreman1281a362015-07-31 16:55:32 -05001844input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001845 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001846 uint32_t mods_latched, uint32_t mods_locked,
1847 uint32_t group)
1848{
Derek Foreman1281a362015-07-31 16:55:32 -05001849 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001850 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001851 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001852 uint32_t serial_out;
1853
1854 /* If we get a key event followed by a modifier event with the
1855 * same serial number, then we try to preserve those semantics by
1856 * reusing the same serial number on the way out too. */
1857 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001858 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001859 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001860 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001861
Derek Foreman1281a362015-07-31 16:55:32 -05001862 keyboard = weston_seat_get_keyboard(&input->base);
1863 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001864 mods_depressed, mods_latched,
1865 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001866 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001867}
1868
Jonny Lamb497994a2014-08-12 14:58:26 +02001869static void
1870input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1871 int32_t rate, int32_t delay)
1872{
1873 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001874 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001875
Giulio Camuffo954f1832014-10-11 18:27:30 +03001876 b->compositor->kb_repeat_rate = rate;
1877 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001878}
1879
Daniel Stone37816df2012-05-16 18:45:18 +01001880static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001881 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001882 input_handle_keyboard_enter,
1883 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001884 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001885 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001886 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001887};
1888
1889static void
Derek Foreman748c6952015-11-06 15:56:10 -06001890input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1891 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001892 struct wl_surface *surface, int32_t id,
1893 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001894{
1895 struct wayland_input *input = data;
1896 struct wayland_output *output;
1897 enum theme_location location;
1898 bool first_touch;
1899 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001900 double x, y;
1901
1902 x = wl_fixed_to_double(fixed_x);
1903 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001904
1905 first_touch = (input->touch_points == 0);
1906 input->touch_points++;
1907
1908 input->touch_focus = wl_surface_get_user_data(surface);
1909 output = input->touch_focus;
1910 if (!first_touch && !input->touch_active)
1911 return;
1912
1913 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001914 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001915
1916 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001917 x -= fx;
1918 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001919
1920 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1921 weston_output_schedule_repaint(&output->base);
1922
1923 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1924 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001925 if (output->parent.xdg_toplevel)
1926 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1927 input->parent.seat, serial);
1928 else if (output->parent.shell_surface)
1929 wl_shell_surface_move(output->parent.shell_surface,
1930 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001931 frame_status_clear(output->frame,
1932 FRAME_STATUS_MOVE);
1933 return;
1934 }
1935
1936 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1937 return;
1938 }
1939
1940 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1941
1942 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1943 input->touch_active = true;
1944}
1945
1946static void
1947input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1948 uint32_t serial, uint32_t time, int32_t id)
1949{
1950 struct wayland_input *input = data;
1951 struct wayland_output *output = input->touch_focus;
1952 bool active = input->touch_active;
1953
1954 input->touch_points--;
1955 if (input->touch_points == 0) {
1956 input->touch_focus = NULL;
1957 input->touch_active = false;
1958 }
1959
1960 if (!output)
1961 return;
1962
1963 if (output->frame) {
1964 frame_touch_up(output->frame, input, id);
1965
1966 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1967 wayland_output_destroy(&output->base);
1968 input->touch_focus = NULL;
1969 input->keyboard_focus = NULL;
1970 if (wl_list_empty(&input->backend->compositor->output_list))
1971 weston_compositor_exit(input->backend->compositor);
1972
1973 return;
1974 }
1975 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1976 weston_output_schedule_repaint(&output->base);
1977 }
1978
1979 if (active)
1980 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1981}
1982
1983static void
1984input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001985 uint32_t time, int32_t id,
1986 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001987{
1988 struct wayland_input *input = data;
1989 struct wayland_output *output = input->touch_focus;
1990 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001991 double x, y;
1992
1993 x = wl_fixed_to_double(fixed_x);
1994 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001995
1996 if (!output || !input->touch_active)
1997 return;
1998
1999 if (output->frame) {
2000 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002001 x -= fx;
2002 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002003 }
2004
2005 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2006
2007 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2008}
2009
2010static void
2011input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2012{
2013 struct wayland_input *input = data;
2014
2015 if (!input->touch_focus || !input->touch_active)
2016 return;
2017
2018 notify_touch_frame(&input->base);
2019}
2020
2021static void
2022input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2023{
2024 struct wayland_input *input = data;
2025
2026 if (!input->touch_focus || !input->touch_active)
2027 return;
2028
2029 notify_touch_cancel(&input->base);
2030}
2031
2032static const struct wl_touch_listener touch_listener = {
2033 input_handle_touch_down,
2034 input_handle_touch_up,
2035 input_handle_touch_motion,
2036 input_handle_touch_frame,
2037 input_handle_touch_cancel,
2038};
2039
2040
2041static void
Daniel Stone37816df2012-05-16 18:45:18 +01002042input_handle_capabilities(void *data, struct wl_seat *seat,
2043 enum wl_seat_capability caps)
2044{
2045 struct wayland_input *input = data;
2046
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002047 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2048 input->parent.pointer = wl_seat_get_pointer(seat);
2049 wl_pointer_set_user_data(input->parent.pointer, input);
2050 wl_pointer_add_listener(input->parent.pointer,
2051 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002052 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002053 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002054 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2055 wl_pointer_release(input->parent.pointer);
2056 else
2057 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002058 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002059 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002060 }
2061
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002062 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2063 input->parent.keyboard = wl_seat_get_keyboard(seat);
2064 wl_keyboard_set_user_data(input->parent.keyboard, input);
2065 wl_keyboard_add_listener(input->parent.keyboard,
2066 &keyboard_listener, input);
2067 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002068 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2069 wl_keyboard_release(input->parent.keyboard);
2070 else
2071 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002072 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002073 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002074 }
Derek Foreman748c6952015-11-06 15:56:10 -06002075
2076 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2077 input->parent.touch = wl_seat_get_touch(seat);
2078 wl_touch_set_user_data(input->parent.touch, input);
2079 wl_touch_add_listener(input->parent.touch,
2080 &touch_listener, input);
2081 weston_seat_init_touch(&input->base);
2082 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2083 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2084 wl_touch_release(input->parent.touch);
2085 else
2086 wl_touch_destroy(input->parent.touch);
2087 input->parent.touch = NULL;
2088 weston_seat_release_touch(&input->base);
2089 }
Daniel Stone37816df2012-05-16 18:45:18 +01002090}
2091
Jonny Lamb497994a2014-08-12 14:58:26 +02002092static void
2093input_handle_name(void *data, struct wl_seat *seat,
2094 const char *name)
2095{
2096}
2097
Daniel Stone37816df2012-05-16 18:45:18 +01002098static const struct wl_seat_listener seat_listener = {
2099 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002100 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002101};
2102
2103static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002104display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002105{
2106 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002107 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002108
Peter Huttererf3d62272013-08-08 11:57:05 +10002109 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002110 if (input == NULL)
2111 return;
2112
Giulio Camuffo954f1832014-10-11 18:27:30 +03002113 weston_seat_init(&input->base, b->compositor, "default");
2114 input->backend = b;
2115 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002116 &wl_seat_interface, version);
2117 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002118 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002119
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002120 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2121 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002122
2123 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002124 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002125
2126 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2127 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002128}
2129
2130static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002131wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2132 int32_t x, int32_t y,
2133 int32_t physical_width, int32_t physical_height,
2134 int32_t subpixel, const char *make,
2135 const char *model, int32_t transform)
2136{
2137 struct wayland_parent_output *output = data;
2138
2139 output->x = x;
2140 output->y = y;
2141 output->physical.width = physical_width;
2142 output->physical.height = physical_height;
2143 output->physical.subpixel = subpixel;
2144
2145 free(output->physical.make);
2146 output->physical.make = strdup(make);
2147 free(output->physical.model);
2148 output->physical.model = strdup(model);
2149
2150 output->transform = transform;
2151}
2152
2153static struct weston_mode *
2154find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2155{
2156 struct weston_mode *mode;
2157
2158 wl_list_for_each(mode, list, link) {
2159 if (mode->width == width && mode->height == height &&
2160 mode->refresh == refresh)
2161 return mode;
2162 }
2163
2164 mode = zalloc(sizeof *mode);
2165 if (!mode)
2166 return NULL;
2167
2168 mode->width = width;
2169 mode->height = height;
2170 mode->refresh = refresh;
2171 wl_list_insert(list, &mode->link);
2172
2173 return mode;
2174}
2175
2176static void
2177wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2178 uint32_t flags, int32_t width, int32_t height,
2179 int32_t refresh)
2180{
2181 struct wayland_parent_output *output = data;
2182 struct weston_mode *mode;
2183
2184 if (output->output) {
2185 mode = find_mode(&output->output->base.mode_list,
2186 width, height, refresh);
2187 if (!mode)
2188 return;
2189 mode->flags = flags;
2190 /* Do a mode-switch on current mode change? */
2191 } else {
2192 mode = find_mode(&output->mode_list, width, height, refresh);
2193 if (!mode)
2194 return;
2195 mode->flags = flags;
2196 if (flags & WL_OUTPUT_MODE_CURRENT)
2197 output->current_mode = mode;
2198 if (flags & WL_OUTPUT_MODE_PREFERRED)
2199 output->preferred_mode = mode;
2200 }
2201}
2202
2203static const struct wl_output_listener output_listener = {
2204 wayland_parent_output_geometry,
2205 wayland_parent_output_mode
2206};
2207
2208static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002209wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002210{
2211 struct wayland_parent_output *output;
2212
2213 output = zalloc(sizeof *output);
2214 if (!output)
2215 return;
2216
2217 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002218 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002219 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002220 if (!output->global) {
2221 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002222 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002223 }
2224
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002225 wl_output_add_listener(output->global, &output_listener, output);
2226
2227 output->scale = 0;
2228 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2229 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2230 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002231 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002232
Giulio Camuffo954f1832014-10-11 18:27:30 +03002233 if (b->sprawl_across_outputs) {
2234 wl_display_roundtrip(b->parent.wl_display);
2235 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002236 }
2237}
2238
2239static void
2240wayland_parent_output_destroy(struct wayland_parent_output *output)
2241{
2242 struct weston_mode *mode, *next;
2243
2244 if (output->output)
2245 wayland_output_destroy(&output->output->base);
2246
2247 wl_output_destroy(output->global);
2248 free(output->physical.make);
2249 free(output->physical.model);
2250
2251 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2252 wl_list_remove(&mode->link);
2253 free(mode);
2254 }
2255}
2256
2257static void
Armin Krezović34476192016-11-21 18:42:42 +01002258xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2259{
2260 zxdg_shell_v6_pong(shell, serial);
2261}
2262
2263static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2264 xdg_shell_ping,
2265};
2266
2267static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002268registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2269 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002270{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002271 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002272
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002273 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002274 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002275 wl_registry_bind(registry, name,
2276 &wl_compositor_interface, 1);
Armin Krezović34476192016-11-21 18:42:42 +01002277 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2278 b->parent.xdg_shell =
2279 wl_registry_bind(registry, name,
2280 &zxdg_shell_v6_interface, 1);
2281 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2282 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002283 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002284 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002285 wl_registry_bind(registry, name,
2286 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002287 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002288 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002289 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002290 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002291 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002292 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002293 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002294 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002295 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002296 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002297 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002298 }
2299}
2300
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002301static void
2302registry_handle_global_remove(void *data, struct wl_registry *registry,
2303 uint32_t name)
2304{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002305 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002306 struct wayland_parent_output *output;
2307
Giulio Camuffo954f1832014-10-11 18:27:30 +03002308 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002309 if (output->id == name)
2310 wayland_parent_output_destroy(output);
2311}
2312
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002313static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002314 registry_handle_global,
2315 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002316};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002317
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002318static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002319wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002320{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002321 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002322 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002323
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002324 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002325 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002326 return 0;
2327 }
2328
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002329 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002330 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002331 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002332 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002333
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002334 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002335 count = wl_display_dispatch_pending(b->parent.wl_display);
2336 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002337 }
2338
2339 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002340}
2341
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002342static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002343wayland_restore(struct weston_compositor *ec)
2344{
2345}
2346
2347static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002348wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002349{
Armin Krezović938dc522016-08-01 19:17:57 +02002350 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002351
Armin Krezović78895c52016-10-09 17:30:28 +02002352 wl_event_source_remove(b->parent.wl_source);
2353
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002354 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002355
Giulio Camuffo954f1832014-10-11 18:27:30 +03002356 if (b->parent.shm)
2357 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002358
Armin Krezović34476192016-11-21 18:42:42 +01002359 if (b->parent.xdg_shell)
2360 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2361
2362 if (b->parent.shell)
2363 wl_shell_destroy(b->parent.shell);
2364
Armin Krezović78895c52016-10-09 17:30:28 +02002365 if (b->parent.fshell)
2366 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2367
2368 if (b->parent.compositor)
2369 wl_compositor_destroy(b->parent.compositor);
2370
2371 wl_registry_destroy(b->parent.registry);
2372 wl_display_flush(b->parent.wl_display);
2373 wl_display_disconnect(b->parent.wl_display);
2374
2375 if (b->theme)
2376 theme_destroy(b->theme);
2377
2378 if (b->frame_device)
2379 cairo_device_destroy(b->frame_device);
2380
2381 wl_cursor_theme_destroy(b->cursor_theme);
2382
Giulio Camuffo954f1832014-10-11 18:27:30 +03002383 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002384}
2385
Jason Ekstrand7744f712013-10-27 22:24:55 -05002386static const char *left_ptrs[] = {
2387 "left_ptr",
2388 "default",
2389 "top_left_arrow",
2390 "left-arrow"
2391};
2392
2393static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002394create_cursor(struct wayland_backend *b,
2395 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002396{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002397 unsigned int i;
2398
Benoit Gschwind244ff792016-04-28 20:33:11 +02002399 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2400 config->cursor_size,
2401 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002402 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002403 fprintf(stderr, "could not load cursor theme\n");
2404 return;
2405 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002406
Giulio Camuffo954f1832014-10-11 18:27:30 +03002407 b->cursor = NULL;
2408 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2409 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002410 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002411 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002412 fprintf(stderr, "could not load left cursor\n");
2413 return;
2414 }
2415}
2416
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002417static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002418fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2419 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002420{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002421 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002422 struct wayland_input *input = NULL;
2423
Giulio Camuffo954f1832014-10-11 18:27:30 +03002424 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002425 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002426 break;
2427
2428 if (!input || !input->output)
2429 return;
2430
2431 if (input->output->frame)
2432 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2433 else
2434 wayland_output_set_windowed(input->output);
2435
2436 weston_output_schedule_repaint(&input->output->base);
2437}
2438
Giulio Camuffo954f1832014-10-11 18:27:30 +03002439static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002440wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002441 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002442{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002443 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002444 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002445 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002446
Giulio Camuffo954f1832014-10-11 18:27:30 +03002447 b = zalloc(sizeof *b);
2448 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002449 return NULL;
2450
Giulio Camuffo954f1832014-10-11 18:27:30 +03002451 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002452 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002453 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002454
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002455 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002456 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002457 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002458 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002459 }
2460
Giulio Camuffo954f1832014-10-11 18:27:30 +03002461 wl_list_init(&b->parent.output_list);
2462 wl_list_init(&b->input_list);
2463 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2464 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2465 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002466
Benoit Gschwind244ff792016-04-28 20:33:11 +02002467 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002468
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002469 b->use_pixman = new_config->use_pixman;
Armin Krezović174448a2016-09-30 14:11:09 +02002470 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002471
Giulio Camuffo954f1832014-10-11 18:27:30 +03002472 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002473 gl_renderer = weston_load_module("gl-renderer.so",
2474 "gl_renderer_interface");
2475 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002476 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002477 }
2478
Giulio Camuffo954f1832014-10-11 18:27:30 +03002479 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002480 if (gl_renderer->display_create(compositor,
2481 EGL_PLATFORM_WAYLAND_KHR,
2482 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002483 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002484 gl_renderer->alpha_attribs,
2485 NULL,
2486 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002487 weston_log("Failed to initialize the GL renderer; "
2488 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002489 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002490 }
2491 }
2492
Giulio Camuffo954f1832014-10-11 18:27:30 +03002493 if (b->use_pixman) {
2494 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002495 weston_log("Failed to initialize pixman renderer\n");
2496 goto err_display;
2497 }
2498 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002499
Giulio Camuffo954f1832014-10-11 18:27:30 +03002500 b->base.destroy = wayland_destroy;
2501 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002502
Giulio Camuffo954f1832014-10-11 18:27:30 +03002503 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002504
Giulio Camuffo954f1832014-10-11 18:27:30 +03002505 fd = wl_display_get_fd(b->parent.wl_display);
2506 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002507 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002508 wayland_backend_handle_event, b);
2509 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002510 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002511
Giulio Camuffo954f1832014-10-11 18:27:30 +03002512 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002513
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002514 if (compositor->renderer->import_dmabuf) {
2515 if (linux_dmabuf_setup(compositor) < 0)
2516 weston_log("Error: initializing dmabuf "
2517 "support failed.\n");
2518 }
2519
Giulio Camuffo954f1832014-10-11 18:27:30 +03002520 compositor->backend = &b->base;
2521 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002522err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002523 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002524err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002525 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002526 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002527 return NULL;
2528}
2529
2530static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002531wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002532{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002533 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002534
Giulio Camuffo954f1832014-10-11 18:27:30 +03002535 if (b->theme)
2536 theme_destroy(b->theme);
2537 if (b->frame_device)
2538 cairo_device_destroy(b->frame_device);
2539 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002540
Giulio Camuffo954f1832014-10-11 18:27:30 +03002541 weston_compositor_shutdown(b->compositor);
2542 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002543}
2544
Armin Krezović174448a2016-09-30 14:11:09 +02002545static const struct weston_windowed_output_api windowed_api = {
2546 wayland_output_set_size,
2547 wayland_output_create,
2548};
2549
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002550static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002551config_init_to_defaults(struct weston_wayland_backend_config *config)
2552{
2553}
2554
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002555WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002556backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002557 struct weston_backend_config *config_base)
2558{
2559 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002560 struct wayland_parent_output *poutput;
2561 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002562 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002563
Benoit Gschwinde091b452016-05-10 22:47:48 +02002564 if (config_base == NULL ||
2565 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2566 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2567 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002568 return -1;
2569 }
2570
2571 config_init_to_defaults(&new_config);
2572 memcpy(&new_config, config_base, config_base->struct_size);
2573
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002574 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002575
Giulio Camuffo954f1832014-10-11 18:27:30 +03002576 if (!b)
2577 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002578
Benoit Gschwind37a68072016-04-28 20:33:08 +02002579 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002580 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002581 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002582
Giulio Camuffo954f1832014-10-11 18:27:30 +03002583 wl_list_for_each(poutput, &b->parent.output_list, link)
2584 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002585
Giulio Camuffo954f1832014-10-11 18:27:30 +03002586 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002587 }
2588
Benoit Gschwind37a68072016-04-28 20:33:08 +02002589 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002590 if (wayland_output_create_fullscreen(b) < 0) {
2591 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002592 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002593 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002594
Giulio Camuffo954f1832014-10-11 18:27:30 +03002595 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002596 }
2597
Armin Krezović174448a2016-09-30 14:11:09 +02002598 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2599 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002600
Armin Krezović174448a2016-09-30 14:11:09 +02002601 if (ret < 0) {
2602 weston_log("Failed to register output API.\n");
2603 wayland_backend_destroy(b);
2604 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002605 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002606
Giulio Camuffo954f1832014-10-11 18:27:30 +03002607 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002608 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002609 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002610 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002611
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002612err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002613 wayland_backend_destroy(b);
2614 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002615}