blob: d1e387df67500c8cac817fefab66def4b74f8a28 [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;
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800131
132 struct wl_callback *frame_cb;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100133};
134
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500135struct wayland_parent_output {
136 struct wayland_output *output;
137 struct wl_list link;
138
139 struct wl_output *global;
140 uint32_t id;
141
142 struct {
143 char *make;
144 char *model;
145 int32_t width, height;
146 uint32_t subpixel;
147 } physical;
148
149 int32_t x, y;
150 uint32_t transform;
151 uint32_t scale;
152
153 struct wl_list mode_list;
154 struct weston_mode *preferred_mode;
155 struct weston_mode *current_mode;
156};
157
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500158struct wayland_shm_buffer {
159 struct wayland_output *output;
160 struct wl_list link;
161 struct wl_list free_link;
162
163 struct wl_buffer *buffer;
164 void *data;
165 size_t size;
166 pixman_region32_t damage;
167 int frame_damaged;
168
169 pixman_image_t *pm_image;
170 cairo_surface_t *c_surface;
171};
172
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100173struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400174 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300175 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100176 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500177
178 struct {
179 struct wl_seat *seat;
180 struct wl_pointer *pointer;
181 struct wl_keyboard *keyboard;
182 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500183
184 struct {
185 struct wl_surface *surface;
186 int32_t hx, hy;
187 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500188 } parent;
189
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500190 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100191 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400192 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600193 uint32_t touch_points;
194 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600195 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600196 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600197
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400198 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600199 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500200 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000201
202 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100203};
204
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300205struct gl_renderer_interface *gl_renderer;
206
Armin Krezović938dc522016-08-01 19:17:57 +0200207static inline struct wayland_output *
208to_wayland_output(struct weston_output *base)
209{
210 return container_of(base, struct wayland_output, base);
211}
212
213static inline struct wayland_backend *
214to_wayland_backend(struct weston_compositor *base)
215{
216 return container_of(base->backend, struct wayland_backend, base);
217}
218
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500219static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500220wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
221{
222 cairo_surface_destroy(buffer->c_surface);
223 pixman_image_unref(buffer->pm_image);
224
225 wl_buffer_destroy(buffer->buffer);
226 munmap(buffer->data, buffer->size);
227
228 pixman_region32_fini(&buffer->damage);
229
230 wl_list_remove(&buffer->link);
231 wl_list_remove(&buffer->free_link);
232 free(buffer);
233}
234
235static void
236buffer_release(void *data, struct wl_buffer *buffer)
237{
238 struct wayland_shm_buffer *sb = data;
239
240 if (sb->output) {
241 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
242 } else {
243 wayland_shm_buffer_destroy(sb);
244 }
245}
246
247static const struct wl_buffer_listener buffer_listener = {
248 buffer_release
249};
250
251static struct wayland_shm_buffer *
252wayland_output_get_shm_buffer(struct wayland_output *output)
253{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300254 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200255 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300256 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500257 struct wayland_shm_buffer *sb;
258
259 struct wl_shm_pool *pool;
260 int width, height, stride;
261 int32_t fx, fy;
262 int fd;
263 unsigned char *data;
264
265 if (!wl_list_empty(&output->shm.free_buffers)) {
266 sb = container_of(output->shm.free_buffers.next,
267 struct wayland_shm_buffer, free_link);
268 wl_list_remove(&sb->free_link);
269 wl_list_init(&sb->free_link);
270
271 return sb;
272 }
273
274 if (output->frame) {
275 width = frame_width(output->frame);
276 height = frame_height(output->frame);
277 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500278 width = output->base.current_mode->width;
279 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500280 }
281
282 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
283
284 fd = os_create_anonymous_file(height * stride);
285 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000286 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500287 return NULL;
288 }
289
290 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
291 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000292 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500293 close(fd);
294 return NULL;
295 }
296
297 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000298 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200299 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000300 close(fd);
301 free(data);
302 return NULL;
303 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500304
305 sb->output = output;
306 wl_list_init(&sb->free_link);
307 wl_list_insert(&output->shm.buffers, &sb->link);
308
309 pixman_region32_init_rect(&sb->damage, 0, 0,
310 output->base.width, output->base.height);
311 sb->frame_damaged = 1;
312
313 sb->data = data;
314 sb->size = height * stride;
315
316 pool = wl_shm_create_pool(shm, fd, sb->size);
317
318 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
319 width, height,
320 stride,
321 WL_SHM_FORMAT_ARGB8888);
322 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
323 wl_shm_pool_destroy(pool);
324 close(fd);
325
326 memset(data, 0, sb->size);
327
328 sb->c_surface =
329 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
330 width, height, stride);
331
332 fx = 0;
333 fy = 0;
334 if (output->frame)
335 frame_interior(output->frame, &fx, &fy, 0, 0);
336 sb->pm_image =
337 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
338 (uint32_t *)(data + fy * stride) + fx,
339 stride);
340
341 return sb;
342}
343
344static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500345frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400346{
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800347 struct wayland_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400348 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400349
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800350 assert(callback == output->frame_cb);
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500351 wl_callback_destroy(callback);
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800352 output->frame_cb = NULL;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400353
354 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300355
356 /*
357 * This is the fallback case, where Presentation extension is not
358 * available from the parent compositor. We do not know the base for
359 * 'time', so we cannot feed it to finish_frame(). Do the only thing
360 * we can, and pretend finish_frame time is when we process this
361 * event.
362 */
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800363 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
364 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400365}
366
367static const struct wl_callback_listener frame_listener = {
368 frame_done
369};
370
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500371static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200372draw_initial_frame(struct wayland_output *output)
373{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500374 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200375
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500376 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200377
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500378 /* If we are rendering with GL, then orphan it so that it gets
379 * destroyed immediately */
380 if (output->gl.egl_window)
381 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500383 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500384 wl_surface_damage(output->parent.surface, 0, 0,
385 output->base.current_mode->width,
386 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200387}
388
389static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500390wayland_output_update_gl_border(struct wayland_output *output)
391{
392 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
393 cairo_t *cr;
394
395 if (!output->frame)
396 return;
397 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
398 return;
399
400 fwidth = frame_width(output->frame);
401 fheight = frame_height(output->frame);
402 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
403
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500404 if (!output->gl.border.top)
405 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500406 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
407 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409 frame_repaint(output->frame, cr);
410 cairo_destroy(cr);
411 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
412 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500413 cairo_image_surface_get_stride(output->gl.border.top) / 4,
414 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500415
416
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500417 if (!output->gl.border.left)
418 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500419 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
420 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500421 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500422 cairo_translate(cr, 0, -iy);
423 frame_repaint(output->frame, cr);
424 cairo_destroy(cr);
425 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
426 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500427 cairo_image_surface_get_stride(output->gl.border.left) / 4,
428 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500429
430
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500431 if (!output->gl.border.right)
432 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500433 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
434 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500435 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500436 cairo_translate(cr, -(iwidth + ix), -iy);
437 frame_repaint(output->frame, cr);
438 cairo_destroy(cr);
439 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
440 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500441 cairo_image_surface_get_stride(output->gl.border.right) / 4,
442 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500443
444
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445 if (!output->gl.border.bottom)
446 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500447 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
448 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500449 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500450 cairo_translate(cr, 0, -(iy + iheight));
451 frame_repaint(output->frame, cr);
452 cairo_destroy(cr);
453 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
454 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500455 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
456 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500457}
458
459static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200460wayland_output_start_repaint_loop(struct weston_output *output_base)
461{
Armin Krezović938dc522016-08-01 19:17:57 +0200462 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300463 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200464 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200465
466 /* If this is the initial frame, we need to attach a buffer so that
467 * the compositor can map the surface and include it in its render
468 * loop. If the surface doesn't end up in the render loop, the frame
469 * callback won't be invoked. The buffer is transparent and of the
470 * same size as the future real output buffer. */
471 if (output->parent.draw_initial_frame) {
Armin Krezović2d321e32016-10-09 17:30:25 +0200472 output->parent.draw_initial_frame = false;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200473
474 draw_initial_frame(output);
475 }
476
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800477 output->frame_cb = wl_surface_frame(output->parent.surface);
478 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200479 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300480 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200481}
482
David Herrmann1edf44c2013-10-22 17:11:26 +0200483static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500484wayland_output_repaint_gl(struct weston_output *output_base,
485 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400486{
Armin Krezović938dc522016-08-01 19:17:57 +0200487 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400488 struct weston_compositor *ec = output->base.compositor;
Scott Moreau062be7e2012-04-20 13:37:33 -0600489
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800490 output->frame_cb = wl_surface_frame(output->parent.surface);
491 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100492
Jason Ekstrand7744f712013-10-27 22:24:55 -0500493 wayland_output_update_gl_border(output);
494
Pekka Paalanenbc106382012-10-10 12:49:31 +0300495 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200496
497 pixman_region32_subtract(&ec->primary_plane.damage,
498 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200499 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100500}
501
Matt Roper361d2ad2011-08-29 13:52:23 -0700502static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500503wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
504{
505 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
506 cairo_t *cr;
507
508 if (!buffer->output->frame || !buffer->frame_damaged)
509 return;
510
511 cr = cairo_create(buffer->c_surface);
512
513 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
514 fwidth = frame_width(buffer->output->frame);
515 fheight = frame_height(buffer->output->frame);
516
517 /* Set the clip so we don't unnecisaraly damage the surface */
518 cairo_move_to(cr, ix, iy);
519 cairo_rel_line_to(cr, iwidth, 0);
520 cairo_rel_line_to(cr, 0, iheight);
521 cairo_rel_line_to(cr, -iwidth, 0);
522 cairo_line_to(cr, ix, iy);
523 cairo_line_to(cr, 0, iy);
524 cairo_line_to(cr, 0, fheight);
525 cairo_line_to(cr, fwidth, fheight);
526 cairo_line_to(cr, fwidth, 0);
527 cairo_line_to(cr, 0, 0);
528 cairo_line_to(cr, 0, iy);
529 cairo_close_path(cr);
530 cairo_clip(cr);
531
532 /* Draw using a pattern so that the final result gets clipped */
533 cairo_push_group(cr);
534 frame_repaint(buffer->output->frame, cr);
535 cairo_pop_group_to_source(cr);
536 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
537 cairo_paint(cr);
538
539 cairo_destroy(cr);
540}
541
542static void
543wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
544{
545 pixman_region32_t damage;
546 pixman_box32_t *rects;
547 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
548 int i, n;
549
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500550 pixman_region32_init(&damage);
551 weston_transformed_region(sb->output->base.width,
552 sb->output->base.height,
553 sb->output->base.transform,
554 sb->output->base.current_scale,
555 &sb->damage, &damage);
556
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500557 if (sb->output->frame) {
558 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
559 fwidth = frame_width(sb->output->frame);
560 fheight = frame_height(sb->output->frame);
561
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500562 pixman_region32_translate(&damage, ix, iy);
563
564 if (sb->frame_damaged) {
565 pixman_region32_union_rect(&damage, &damage,
566 0, 0, fwidth, iy);
567 pixman_region32_union_rect(&damage, &damage,
568 0, iy, ix, iheight);
569 pixman_region32_union_rect(&damage, &damage,
570 ix + iwidth, iy,
571 fwidth - (ix + iwidth), iheight);
572 pixman_region32_union_rect(&damage, &damage,
573 0, iy + iheight,
574 fwidth, fheight - (iy + iheight));
575 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500576 }
577
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500578 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500579 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
580 for (i = 0; i < n; ++i)
581 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
582 rects[i].y1, rects[i].x2 - rects[i].x1,
583 rects[i].y2 - rects[i].y1);
584
585 if (sb->output->frame)
586 pixman_region32_fini(&damage);
587}
588
589static int
590wayland_output_repaint_pixman(struct weston_output *output_base,
591 pixman_region32_t *damage)
592{
Armin Krezović938dc522016-08-01 19:17:57 +0200593 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300594 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200595 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500596 struct wayland_shm_buffer *sb;
597
598 if (output->frame) {
599 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
600 wl_list_for_each(sb, &output->shm.buffers, link)
601 sb->frame_damaged = 1;
602 }
603
604 wl_list_for_each(sb, &output->shm.buffers, link)
605 pixman_region32_union(&sb->damage, &sb->damage, damage);
606
607 sb = wayland_output_get_shm_buffer(output);
608
609 wayland_output_update_shm_border(sb);
610 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500612
613 wayland_shm_buffer_attach(sb);
614
Daniel Stone21fac602016-11-28 16:05:35 +0000615 output->frame_cb = wl_surface_frame(output->parent.surface);
616 wl_callback_add_listener(output->frame_cb, &frame_listener, output);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500617 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300618 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500619
620 pixman_region32_fini(&sb->damage);
621 pixman_region32_init(&sb->damage);
622 sb->frame_damaged = 0;
623
Giulio Camuffo954f1832014-10-11 18:27:30 +0300624 pixman_region32_subtract(&b->compositor->primary_plane.damage,
625 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500626 return 0;
627}
628
629static void
Armin Krezović174448a2016-09-30 14:11:09 +0200630wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700631{
Armin Krezović34476192016-11-21 18:42:42 +0100632 if (output->parent.xdg_toplevel)
633 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
634
635 if (output->parent.xdg_surface)
636 zxdg_surface_v6_destroy(output->parent.xdg_surface);
637
Armin Krezović174448a2016-09-30 14:11:09 +0200638 if (output->parent.shell_surface)
639 wl_shell_surface_destroy(output->parent.shell_surface);
640
641 wl_surface_destroy(output->parent.surface);
642}
643
Armin Krezović2e662522016-10-09 17:30:29 +0200644static void
645wayland_output_destroy_shm_buffers(struct wayland_output *output)
646{
647 struct wayland_shm_buffer *buffer, *next;
648
649 /* Throw away any remaining SHM buffers */
650 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
651 wayland_shm_buffer_destroy(buffer);
652 /* These will get thrown away when they get released */
653 wl_list_for_each(buffer, &output->shm.buffers, link)
654 buffer->output = NULL;
655}
656
Armin Krezović174448a2016-09-30 14:11:09 +0200657static int
658wayland_output_disable(struct weston_output *base)
659{
660 struct wayland_output *output = to_wayland_output(base);
661 struct wayland_backend *b = to_wayland_backend(base->compositor);
662
663 if (!output->base.enabled)
664 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700665
Giulio Camuffo954f1832014-10-11 18:27:30 +0300666 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200667 pixman_renderer_output_destroy(&output->base);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500668 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200669 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100670 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500671 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100672
Armin Krezović2e662522016-10-09 17:30:29 +0200673 wayland_output_destroy_shm_buffers(output);
674
Armin Krezovićf054d352016-10-09 17:30:27 +0200675 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500676
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600677 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500678 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600679
680 cairo_surface_destroy(output->gl.border.top);
681 cairo_surface_destroy(output->gl.border.left);
682 cairo_surface_destroy(output->gl.border.right);
683 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500684
Armin Krezović174448a2016-09-30 14:11:09 +0200685 return 0;
686}
Matt Roper361d2ad2011-08-29 13:52:23 -0700687
Armin Krezović174448a2016-09-30 14:11:09 +0200688static void
689wayland_output_destroy(struct weston_output *base)
690{
691 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +0200692
693 wayland_output_disable(&output->base);
694
Armin Krezović174448a2016-09-30 14:11:09 +0200695 weston_output_destroy(&output->base);
696
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800697 if (output->frame_cb)
698 wl_callback_destroy(output->frame_cb);
699
Armin Krezović174448a2016-09-30 14:11:09 +0200700 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700701}
702
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300703static const struct wl_shell_surface_listener shell_surface_listener;
704
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200705static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500706wayland_output_init_gl_renderer(struct wayland_output *output)
707{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500708 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500709
710 if (output->frame) {
711 fwidth = frame_width(output->frame);
712 fheight = frame_height(output->frame);
713 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500714 fwidth = output->base.current_mode->width;
715 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500716 }
717
718 output->gl.egl_window =
719 wl_egl_window_create(output->parent.surface,
720 fwidth, fheight);
721 if (!output->gl.egl_window) {
722 weston_log("failure to create wl_egl_window\n");
723 return -1;
724 }
725
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200726 if (gl_renderer->output_window_create(&output->base,
727 output->gl.egl_window,
728 output->gl.egl_window,
729 gl_renderer->alpha_attribs,
730 NULL,
731 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500732 goto cleanup_window;
733
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500734 return 0;
735
736cleanup_window:
737 wl_egl_window_destroy(output->gl.egl_window);
738 return -1;
739}
740
741static int
742wayland_output_init_pixman_renderer(struct wayland_output *output)
743{
744 return pixman_renderer_output_create(&output->base);
745}
746
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600747static void
748wayland_output_resize_surface(struct wayland_output *output)
749{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300750 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200751 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600752 int32_t ix, iy, iwidth, iheight;
753 int32_t width, height;
754 struct wl_region *region;
755
756 width = output->base.current_mode->width;
757 height = output->base.current_mode->height;
758
759 if (output->frame) {
760 frame_resize_inside(output->frame, width, height);
761
762 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300763 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600764 wl_region_add(region, ix, iy, iwidth, iheight);
765 wl_surface_set_input_region(output->parent.surface, region);
766 wl_region_destroy(region);
767
768 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300769 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600770 wl_region_add(region, ix, iy, iwidth, iheight);
771 wl_surface_set_opaque_region(output->parent.surface, region);
772 wl_region_destroy(region);
773
774 width = frame_width(output->frame);
775 height = frame_height(output->frame);
776 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300777 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600778 wl_region_add(region, 0, 0, width, height);
779 wl_surface_set_input_region(output->parent.surface, region);
780 wl_region_destroy(region);
781
Giulio Camuffo954f1832014-10-11 18:27:30 +0300782 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600783 wl_region_add(region, 0, 0, width, height);
784 wl_surface_set_opaque_region(output->parent.surface, region);
785 wl_region_destroy(region);
786 }
787
788 if (output->gl.egl_window) {
789 wl_egl_window_resize(output->gl.egl_window,
790 width, height, 0, 0);
791
792 /* These will need to be re-created due to the resize */
793 gl_renderer->output_set_border(&output->base,
794 GL_RENDERER_BORDER_TOP,
795 0, 0, 0, NULL);
796 cairo_surface_destroy(output->gl.border.top);
797 output->gl.border.top = NULL;
798 gl_renderer->output_set_border(&output->base,
799 GL_RENDERER_BORDER_LEFT,
800 0, 0, 0, NULL);
801 cairo_surface_destroy(output->gl.border.left);
802 output->gl.border.left = NULL;
803 gl_renderer->output_set_border(&output->base,
804 GL_RENDERER_BORDER_RIGHT,
805 0, 0, 0, NULL);
806 cairo_surface_destroy(output->gl.border.right);
807 output->gl.border.right = NULL;
808 gl_renderer->output_set_border(&output->base,
809 GL_RENDERER_BORDER_BOTTOM,
810 0, 0, 0, NULL);
811 cairo_surface_destroy(output->gl.border.bottom);
812 output->gl.border.bottom = NULL;
813 }
814
Armin Krezović2e662522016-10-09 17:30:29 +0200815 wayland_output_destroy_shm_buffers(output);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600816}
817
818static int
819wayland_output_set_windowed(struct wayland_output *output)
820{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300821 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200822 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600823 int tlen;
824 char *title;
825
826 if (output->frame)
827 return 0;
828
829 if (output->name) {
830 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
831 title = malloc(tlen + 1);
832 if (!title)
833 return -1;
834
835 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
836 } else {
837 title = strdup(WINDOW_TITLE);
838 }
839
Armin Krezović34476192016-11-21 18:42:42 +0100840 if (output->parent.xdg_toplevel)
841 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, title);
842
Giulio Camuffo954f1832014-10-11 18:27:30 +0300843 if (!b->theme) {
844 b->theme = theme_create();
845 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600846 free(title);
847 return -1;
848 }
849 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300850 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600851 FRAME_BUTTON_CLOSE, title);
852 free(title);
853 if (!output->frame)
854 return -1;
855
856 if (output->keyboard_count)
857 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
858
859 wayland_output_resize_surface(output);
860
Armin Krezović34476192016-11-21 18:42:42 +0100861 if (output->parent.shell_surface)
862 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600863
864 return 0;
865}
866
867static void
868wayland_output_set_fullscreen(struct wayland_output *output,
869 enum wl_shell_surface_fullscreen_method method,
870 uint32_t framerate, struct wl_output *target)
871{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300872 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200873 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500874
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600875 if (output->frame) {
876 frame_destroy(output->frame);
877 output->frame = NULL;
878 }
879
880 wayland_output_resize_surface(output);
881
Armin Krezović34476192016-11-21 18:42:42 +0100882 if (output->parent.xdg_toplevel) {
883 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
884 } else if (output->parent.shell_surface) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500885 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
886 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300887 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800888 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
889 output->parent.surface,
890 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500891 }
892}
893
894static struct weston_mode *
895wayland_output_choose_mode(struct wayland_output *output,
896 struct weston_mode *ref_mode)
897{
898 struct weston_mode *mode;
899
900 /* First look for an exact match */
901 wl_list_for_each(mode, &output->base.mode_list, link)
902 if (mode->width == ref_mode->width &&
903 mode->height == ref_mode->height &&
904 mode->refresh == ref_mode->refresh)
905 return mode;
906
907 /* If we can't find an exact match, ignore refresh and try again */
908 wl_list_for_each(mode, &output->base.mode_list, link)
909 if (mode->width == ref_mode->width &&
910 mode->height == ref_mode->height)
911 return mode;
912
913 /* Yeah, we failed */
914 return NULL;
915}
916
917enum mode_status {
918 MODE_STATUS_UNKNOWN,
919 MODE_STATUS_SUCCESS,
920 MODE_STATUS_FAIL,
921 MODE_STATUS_CANCEL,
922};
923
924static void
925mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800926 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500927{
928 enum mode_status *value = data;
929
930 printf("Mode switch successful\n");
931
932 *value = MODE_STATUS_SUCCESS;
933}
934
935static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800936mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500937{
938 enum mode_status *value = data;
939
940 printf("Mode switch failed\n");
941
942 *value = MODE_STATUS_FAIL;
943}
944
945static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800946mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500947{
948 enum mode_status *value = data;
949
950 printf("Mode switch cancelled\n");
951
952 *value = MODE_STATUS_CANCEL;
953}
954
Jonas Ådahl496adb32015-11-17 16:00:27 +0800955struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500956 mode_feedback_successful,
957 mode_feedback_failed,
958 mode_feedback_cancelled,
959};
960
961static int
962wayland_output_switch_mode(struct weston_output *output_base,
963 struct weston_mode *mode)
964{
Armin Krezović938dc522016-08-01 19:17:57 +0200965 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300966 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500967 struct wl_surface *old_surface;
968 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800969 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500970 enum mode_status mode_status;
971 int ret = 0;
972
973 if (output_base == NULL) {
974 weston_log("output is NULL.\n");
975 return -1;
976 }
977
978 if (mode == NULL) {
979 weston_log("mode is NULL.\n");
980 return -1;
981 }
982
Armin Krezović938dc522016-08-01 19:17:57 +0200983 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500984
Armin Krezović34476192016-11-21 18:42:42 +0100985 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500986 return -1;
987
988 mode = wayland_output_choose_mode(output, mode);
989 if (mode == NULL)
990 return -1;
991
992 if (output->base.current_mode == mode)
993 return 0;
994
995 old_mode = output->base.current_mode;
996 old_surface = output->parent.surface;
997 output->base.current_mode = mode;
998 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300999 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001000 wl_surface_set_user_data(output->parent.surface, output);
1001
1002 /* Blow the old buffers because we changed size/surfaces */
1003 wayland_output_resize_surface(output);
1004
1005 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +08001006 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1007 output->parent.surface,
1008 output->parent.output,
1009 mode->refresh);
1010 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1011 &mode_feedback_listener,
1012 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001013
1014 /* This should kick-start things again */
Armin Krezović2d321e32016-10-09 17:30:25 +02001015 output->parent.draw_initial_frame = true;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001016 wayland_output_start_repaint_loop(&output->base);
1017
1018 mode_status = MODE_STATUS_UNKNOWN;
1019 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001020 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001021
Jonas Ådahl496adb32015-11-17 16:00:27 +08001022 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001023
1024 if (mode_status == MODE_STATUS_FAIL) {
1025 output->base.current_mode = old_mode;
1026 wl_surface_destroy(output->parent.surface);
1027 output->parent.surface = old_surface;
1028 wayland_output_resize_surface(output);
1029
1030 return -1;
1031 }
1032
1033 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1034 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1035
Giulio Camuffo954f1832014-10-11 18:27:30 +03001036 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001037 pixman_renderer_output_destroy(output_base);
1038 if (wayland_output_init_pixman_renderer(output) < 0)
1039 goto err_output;
1040 } else {
1041 gl_renderer->output_destroy(output_base);
1042 wl_egl_window_destroy(output->gl.egl_window);
1043 if (wayland_output_init_gl_renderer(output) < 0)
1044 goto err_output;
1045 }
1046 wl_surface_destroy(old_surface);
1047
1048 weston_output_schedule_repaint(&output->base);
1049
1050 return 0;
1051
1052err_output:
1053 /* XXX */
1054 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001055}
1056
Armin Krezović34476192016-11-21 18:42:42 +01001057static void
1058handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1059 uint32_t serial)
1060{
1061 zxdg_surface_v6_ack_configure(surface, serial);
1062}
1063
1064static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1065 handle_xdg_surface_configure
1066};
1067
1068static void
1069handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1070 int32_t width, int32_t height,
1071 struct wl_array *states)
1072{
1073 struct wayland_output *output = data;
1074
1075 output->parent.configure_width = width;
1076 output->parent.configure_height = height;
1077
1078 output->parent.wait_for_configure = false;
1079 /* FIXME: implement resizing */
1080}
1081
1082static void
1083handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1084{
1085}
1086
1087static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1088 handle_xdg_toplevel_configure,
1089 handle_xdg_toplevel_close,
1090};
1091
Armin Krezović174448a2016-09-30 14:11:09 +02001092static int
1093wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001094{
Armin Krezović174448a2016-09-30 14:11:09 +02001095 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001096
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001097 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001098 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001099 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001100 return -1;
1101
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001102 wl_surface_set_user_data(output->parent.surface, output);
1103
Armin Krezović2d321e32016-10-09 17:30:25 +02001104 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001105
Armin Krezović34476192016-11-21 18:42:42 +01001106 if (b->parent.xdg_shell) {
1107 output->parent.xdg_surface =
1108 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1109 output->parent.surface);
1110 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1111 &xdg_surface_listener, output);
1112
1113 output->parent.xdg_toplevel =
1114 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1115 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1116 &xdg_toplevel_listener, output);
1117
1118 wl_surface_commit(output->parent.surface);
1119
1120 output->parent.wait_for_configure = true;
1121
1122 while (output->parent.wait_for_configure)
1123 wl_display_dispatch(b->parent.wl_display);
1124
1125 weston_log("wayland-backend: Using xdg_shell_v6\n");
1126 }
1127 else if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001128 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001129 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001130 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001131 if (!output->parent.shell_surface) {
1132 wl_surface_destroy(output->parent.surface);
1133 return -1;
1134 }
1135
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001136 wl_shell_surface_add_listener(output->parent.shell_surface,
1137 &shell_surface_listener, output);
Armin Krezović34476192016-11-21 18:42:42 +01001138
1139 weston_log("wayland-backend: Using wl_shell\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001140 }
1141
Armin Krezović174448a2016-09-30 14:11:09 +02001142 return 0;
1143}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001144
Armin Krezović174448a2016-09-30 14:11:09 +02001145static int
1146wayland_output_enable(struct weston_output *base)
1147{
1148 struct wayland_output *output = to_wayland_output(base);
1149 struct wayland_backend *b = to_wayland_backend(base->compositor);
1150 int ret = 0;
1151
Armin Krezović174448a2016-09-30 14:11:09 +02001152 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1153 output->base.current_mode->width,
1154 output->base.current_mode->height,
1155 output->base.x, output->base.y);
1156
Armin Krezovićf054d352016-10-09 17:30:27 +02001157 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001158 ret = wayland_backend_create_output_surface(output);
1159
1160 if (ret < 0)
1161 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001162
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001163 wl_list_init(&output->shm.buffers);
1164 wl_list_init(&output->shm.free_buffers);
1165
Giulio Camuffo954f1832014-10-11 18:27:30 +03001166 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001167 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001168 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001169
1170 output->base.repaint = wayland_output_repaint_pixman;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001171 } else {
1172 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001173 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001174
1175 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001176 }
1177
Armin Krezovićf16de172016-10-09 17:30:26 +02001178 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1179 output->base.assign_planes = NULL;
1180 output->base.set_backlight = NULL;
1181 output->base.set_dpms = NULL;
1182 output->base.switch_mode = wayland_output_switch_mode;
1183
Armin Krezović174448a2016-09-30 14:11:09 +02001184 if (b->sprawl_across_outputs) {
1185 wayland_output_set_fullscreen(output,
1186 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1187 output->mode.refresh, output->parent.output);
1188
Armin Krezović34476192016-11-21 18:42:42 +01001189 if (output->parent.xdg_toplevel) {
1190 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1191 output->parent.output);
1192 }
1193 else if (output->parent.shell_surface) {
Armin Krezović174448a2016-09-30 14:11:09 +02001194 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1195 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1196 output->mode.refresh, output->parent.output);
1197 } else if (b->parent.fshell) {
1198 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1199 output->parent.surface,
1200 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1201 output->parent.output);
1202 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1203 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1204 output->parent.surface,
1205 output->parent.output,
1206 output->mode.refresh));
1207 }
1208 } else if (b->fullscreen) {
1209 wayland_output_set_fullscreen(output, 0, 0, NULL);
1210 } else {
1211 wayland_output_set_windowed(output);
1212 }
1213
1214 return 0;
1215
1216err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001217 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001218
1219 return -1;
1220}
1221
1222static struct wayland_output *
1223wayland_output_create_common(void)
1224{
1225 struct wayland_output *output;
1226
1227 output = zalloc(sizeof *output);
1228 if (output == NULL) {
1229 perror("zalloc");
1230 return NULL;
1231 }
1232
Matt Roper361d2ad2011-08-29 13:52:23 -07001233 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001234 output->base.disable = wayland_output_disable;
1235 output->base.enable = wayland_output_enable;
1236
1237 return output;
1238}
1239
1240static int
1241wayland_output_create(struct weston_compositor *compositor, const char *name)
1242{
1243 struct wayland_output *output = wayland_output_create_common();
1244
1245 /* name can't be NULL. */
1246 assert(name);
1247
1248 output->base.name = strdup(name);
1249
Armin Krezović40087402016-09-30 14:11:12 +02001250 weston_output_init(&output->base, compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001251 weston_compositor_add_pending_output(&output->base, compositor);
1252
1253 return 0;
1254}
1255
1256static int
1257wayland_output_set_size(struct weston_output *base, int width, int height)
1258{
1259 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001260 int output_width, output_height;
1261
1262 /* We can only be called once. */
1263 assert(!output->base.current_mode);
1264
1265 /* Make sure we have scale set. */
1266 assert(output->base.scale);
1267
1268 if (width < 1) {
1269 weston_log("Invalid width \"%d\" for output %s\n",
1270 width, output->base.name);
1271 return -1;
1272 }
1273
1274 if (height < 1) {
1275 weston_log("Invalid height \"%d\" for output %s\n",
1276 height, output->base.name);
1277 return -1;
1278 }
1279
1280 output_width = width * output->base.scale;
1281 output_height = height * output->base.scale;
1282
1283 output->mode.flags =
1284 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1285
1286 output->mode.width = output_width;
1287 output->mode.height = output_height;
1288 output->mode.refresh = 60000;
1289 output->scale = output->base.scale;
1290 wl_list_init(&output->base.mode_list);
1291 wl_list_insert(&output->base.mode_list, &output->mode.link);
1292
1293 output->base.current_mode = &output->mode;
1294 output->base.make = "wayland";
1295 output->base.model = "none";
1296
1297 /* XXX: Calculate proper size. */
1298 output->base.mm_width = width;
1299 output->base.mm_height = height;
1300
Armin Krezović174448a2016-09-30 14:11:09 +02001301 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001302}
1303
Armin Krezović174448a2016-09-30 14:11:09 +02001304static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001305wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001306 struct wayland_parent_output *poutput)
1307{
Armin Krezović174448a2016-09-30 14:11:09 +02001308 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001309 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001310
1311 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001312
1313 if (poutput->current_mode) {
1314 mode = poutput->current_mode;
1315 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001316 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001317 } else if (!wl_list_empty(&poutput->mode_list)) {
1318 mode = container_of(poutput->mode_list.next,
1319 struct weston_mode, link);
1320 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001321 weston_log("No valid modes found. Skipping output.\n");
1322 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001323 }
1324
Armin Krezović40087402016-09-30 14:11:12 +02001325 weston_output_init(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001326
Armin Krezović174448a2016-09-30 14:11:09 +02001327 output->base.scale = 1;
1328 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001329
Armin Krezović174448a2016-09-30 14:11:09 +02001330 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1331 goto out;
1332
1333 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001334 output->parent.output = poutput->global;
1335
1336 output->base.make = poutput->physical.make;
1337 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001338
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001339 wl_list_init(&output->base.mode_list);
1340 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1341 wl_list_init(&poutput->mode_list);
1342
Armin Krezović174448a2016-09-30 14:11:09 +02001343 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001344
Armin Krezović174448a2016-09-30 14:11:09 +02001345 return 0;
1346
1347out:
1348 free(output->name);
1349 free(output);
1350
1351 return -1;
1352}
1353
1354static int
1355wayland_output_create_fullscreen(struct wayland_backend *b)
1356{
1357 struct wayland_output *output = wayland_output_create_common();
1358 int width = 0, height = 0;
1359
1360 output->base.name = strdup("wayland-fullscreen");
1361
Armin Krezović40087402016-09-30 14:11:12 +02001362 weston_output_init(&output->base, b->compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001363
1364 output->base.scale = 1;
1365 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1366
1367 if (wayland_backend_create_output_surface(output) < 0)
1368 goto err_surface;
1369
1370 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001371 if (b->parent.xdg_shell || b->parent.shell) {
1372 if (output->parent.xdg_toplevel)
1373 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1374 output->parent.output);
1375 else if (output->parent.shell_surface)
1376 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1377 0, 0, NULL);
1378
Armin Krezović174448a2016-09-30 14:11:09 +02001379 wl_display_roundtrip(b->parent.wl_display);
1380
1381 width = output->parent.configure_width;
1382 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001383 }
1384
Armin Krezović174448a2016-09-30 14:11:09 +02001385 if (wayland_output_set_size(&output->base, width, height) < 0)
1386 goto err_set_size;
1387
1388 weston_compositor_add_pending_output(&output->base, b->compositor);
1389
1390 return 0;
1391
1392err_set_size:
1393 wayland_backend_destroy_output_surface(output);
1394err_surface:
1395 free(output->name);
1396 free(output);
1397
1398 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001399}
1400
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001401static void
1402shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1403 uint32_t serial)
1404{
1405 wl_shell_surface_pong(shell_surface, serial);
1406}
1407
1408static void
1409shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1410 uint32_t edges, int32_t width, int32_t height)
1411{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001412 struct wayland_output *output = data;
1413
1414 output->parent.configure_width = width;
1415 output->parent.configure_height = height;
1416
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001417 /* FIXME: implement resizing */
1418}
1419
1420static void
1421shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1422{
1423}
1424
1425static const struct wl_shell_surface_listener shell_surface_listener = {
1426 shell_surface_ping,
1427 shell_surface_configure,
1428 shell_surface_popup_done
1429};
1430
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001431/* Events received from the wayland-server this compositor is client of: */
1432
Jason Ekstrand7744f712013-10-27 22:24:55 -05001433/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001434static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001435input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001436{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001437
Jason Ekstrand7744f712013-10-27 22:24:55 -05001438 struct wl_buffer *buffer;
1439 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001440
Giulio Camuffo954f1832014-10-11 18:27:30 +03001441 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001442 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001443
Giulio Camuffo954f1832014-10-11 18:27:30 +03001444 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001445 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001446 if (!buffer)
1447 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001448
1449 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1450 input->parent.cursor.surface,
1451 image->hotspot_x, image->hotspot_y);
1452
1453 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1454 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1455 image->width, image->height);
1456 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001457}
1458
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001459static void
Daniel Stone37816df2012-05-16 18:45:18 +01001460input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1461 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001462 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001463{
1464 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001465 int32_t fx, fy;
1466 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001467 double x, y;
1468
1469 x = wl_fixed_to_double(fixed_x);
1470 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001471
Daniel Stone50692802012-06-22 13:21:41 +01001472 /* XXX: If we get a modifier event immediately before the focus,
1473 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001474 input->enter_serial = serial;
1475 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001476
1477 if (input->output->frame) {
1478 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001479 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001480 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001481 x -= fx;
1482 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001483
1484 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1485 weston_output_schedule_repaint(&input->output->base);
1486 } else {
1487 location = THEME_LOCATION_CLIENT_AREA;
1488 }
1489
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001490 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001491
1492 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001493 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001494 notify_pointer_focus(&input->base, &input->output->base, x, y);
1495 wl_pointer_set_cursor(input->parent.pointer,
1496 input->enter_serial, NULL, 0, 0);
1497 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001498 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001499 notify_pointer_focus(&input->base, NULL, 0, 0);
1500 input_set_cursor(input);
1501 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001502}
1503
1504static void
Daniel Stone37816df2012-05-16 18:45:18 +01001505input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1506 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001507{
1508 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001509
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001510 if (!input->output)
1511 return;
1512
Jason Ekstrand7744f712013-10-27 22:24:55 -05001513 if (input->output->frame) {
1514 frame_pointer_leave(input->output->frame, input);
1515
1516 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1517 weston_output_schedule_repaint(&input->output->base);
1518 }
1519
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001520 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001521 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001522 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001523}
1524
1525static void
Daniel Stone37816df2012-05-16 18:45:18 +01001526input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001527 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001528{
1529 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001530 int32_t fx, fy;
1531 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001532 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001533 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001534
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001535 if (!input->output)
1536 return;
1537
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001538 x = wl_fixed_to_double(fixed_x);
1539 y = wl_fixed_to_double(fixed_y);
1540
Jason Ekstrand7744f712013-10-27 22:24:55 -05001541 if (input->output->frame) {
1542 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001543 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001544 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001545 x -= fx;
1546 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001547
1548 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1549 weston_output_schedule_repaint(&input->output->base);
1550 } else {
1551 location = THEME_LOCATION_CLIENT_AREA;
1552 }
1553
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001554 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001555
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001556 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001557 input_set_cursor(input);
1558 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001559 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001560 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001561 } else if (!input->has_focus &&
1562 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001563 wl_pointer_set_cursor(input->parent.pointer,
1564 input->enter_serial, NULL, 0, 0);
1565 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001566 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001567 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001568 }
1569
Peter Hutterer87743e92016-01-18 16:38:22 +10001570 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001571 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001572 want_frame = true;
1573 }
1574
1575 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1576 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001577}
1578
1579static void
1580input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001581 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001582 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001583{
1584 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001585 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001586
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001587 if (!input->output)
1588 return;
1589
Jason Ekstrand7744f712013-10-27 22:24:55 -05001590 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001591 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001592 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001593
1594 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001595 if (input->output->parent.xdg_toplevel)
1596 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001597 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001598 else if (input->output->parent.shell_surface)
1599 wl_shell_surface_move(input->output->parent.shell_surface,
1600 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001601 frame_status_clear(input->output->frame,
1602 FRAME_STATUS_MOVE);
1603 return;
1604 }
1605
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001606 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1607 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001608 input->output = NULL;
1609 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001610
Giulio Camuffo954f1832014-10-11 18:27:30 +03001611 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001612 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001613
1614 return;
1615 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001616
1617 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1618 weston_output_schedule_repaint(&input->output->base);
1619 } else {
1620 location = THEME_LOCATION_CLIENT_AREA;
1621 }
1622
Peter Hutterer87743e92016-01-18 16:38:22 +10001623 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001624 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001625 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1626 notify_pointer_frame(&input->base);
1627 }
Daniel Stone37816df2012-05-16 18:45:18 +01001628}
1629
1630static void
1631input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001632 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001633{
1634 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001635 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001636
Peter Hutterer89b6a492016-01-18 15:58:17 +10001637 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001638 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001639
Peter Hutterer87743e92016-01-18 16:38:22 +10001640 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1641 input->vert.has_discrete) {
1642 weston_event.has_discrete = true;
1643 weston_event.discrete = input->vert.discrete;
1644 input->vert.has_discrete = false;
1645 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1646 input->horiz.has_discrete) {
1647 weston_event.has_discrete = true;
1648 weston_event.discrete = input->horiz.discrete;
1649 input->horiz.has_discrete = false;
1650 }
1651
Peter Hutterer89b6a492016-01-18 15:58:17 +10001652 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001653
1654 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1655 notify_pointer_frame(&input->base);
1656}
1657
1658static void
1659input_handle_frame(void *data, struct wl_pointer *pointer)
1660{
1661 struct wayland_input *input = data;
1662
1663 notify_pointer_frame(&input->base);
1664}
1665
1666static void
1667input_handle_axis_source(void *data, struct wl_pointer *pointer,
1668 uint32_t source)
1669{
1670 struct wayland_input *input = data;
1671
1672 notify_axis_source(&input->base, source);
1673}
1674
1675static void
1676input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1677 uint32_t time, uint32_t axis)
1678{
1679 struct wayland_input *input = data;
1680 struct weston_pointer_axis_event weston_event;
1681
1682 weston_event.axis = axis;
1683 weston_event.value = 0;
1684
1685 notify_axis(&input->base, time, &weston_event);
1686}
1687
1688static void
1689input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1690 uint32_t axis, int32_t discrete)
1691{
1692 struct wayland_input *input = data;
1693
1694 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1695 input->vert.has_discrete = true;
1696 input->vert.discrete = discrete;
1697 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1698 input->horiz.has_discrete = true;
1699 input->horiz.discrete = discrete;
1700 }
Daniel Stone37816df2012-05-16 18:45:18 +01001701}
1702
1703static const struct wl_pointer_listener pointer_listener = {
1704 input_handle_pointer_enter,
1705 input_handle_pointer_leave,
1706 input_handle_motion,
1707 input_handle_button,
1708 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001709 input_handle_frame,
1710 input_handle_axis_source,
1711 input_handle_axis_stop,
1712 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001713};
1714
1715static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001716input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1717 int fd, uint32_t size)
1718{
1719 struct wayland_input *input = data;
1720 struct xkb_keymap *keymap;
1721 char *map_str;
1722
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001723 if (!data) {
1724 close(fd);
1725 return;
1726 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001727
1728 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1729 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1730 if (map_str == MAP_FAILED) {
1731 weston_log("mmap failed: %m\n");
1732 goto error;
1733 }
1734
Giulio Camuffo954f1832014-10-11 18:27:30 +03001735 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001736 map_str,
1737 XKB_KEYMAP_FORMAT_TEXT_V1,
1738 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001739 munmap(map_str, size);
1740
1741 if (!keymap) {
1742 weston_log("failed to compile keymap\n");
1743 goto error;
1744 }
1745
1746 input->keyboard_state_update = STATE_UPDATE_NONE;
1747 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1748 weston_log("No keymap provided; falling back to defalt\n");
1749 keymap = NULL;
1750 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1751 } else {
1752 weston_log("Invalid keymap\n");
1753 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001754 }
1755
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001756 close(fd);
1757
Derek Foreman1281a362015-07-31 16:55:32 -05001758 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001759 weston_seat_update_keymap(&input->base, keymap);
1760 else
1761 weston_seat_init_keyboard(&input->base, keymap);
1762
Ran Benitac9c74152014-08-19 23:59:52 +03001763 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001764
1765 return;
1766
1767error:
1768 wl_keyboard_release(input->parent.keyboard);
1769 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001770}
1771
1772static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001773input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001774 struct wl_keyboard *keyboard,
1775 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001776 struct wl_surface *surface,
1777 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001778{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001779 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001780 struct wayland_output *focus;
1781
1782 focus = input->keyboard_focus;
1783 if (focus) {
1784 /* This shouldn't happen */
1785 focus->keyboard_count--;
1786 if (!focus->keyboard_count && focus->frame)
1787 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1788 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1789 weston_output_schedule_repaint(&focus->base);
1790 }
1791
1792 input->keyboard_focus = wl_surface_get_user_data(surface);
1793 input->keyboard_focus->keyboard_count++;
1794
1795 focus = input->keyboard_focus;
1796 if (focus->frame) {
1797 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1798 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1799 weston_output_schedule_repaint(&focus->base);
1800 }
1801
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001802
Daniel Stone50692802012-06-22 13:21:41 +01001803 /* XXX: If we get a modifier event immediately before the focus,
1804 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001805 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001806 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001807}
1808
1809static void
1810input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001811 struct wl_keyboard *keyboard,
1812 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001813 struct wl_surface *surface)
1814{
1815 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001816 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001817
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001818 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001819
1820 focus = input->keyboard_focus;
1821 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001822 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001823
1824 focus->keyboard_count--;
1825 if (!focus->keyboard_count && focus->frame) {
1826 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1827 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1828 weston_output_schedule_repaint(&focus->base);
1829 }
1830
1831 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001832}
1833
Daniel Stone37816df2012-05-16 18:45:18 +01001834static void
1835input_handle_key(void *data, struct wl_keyboard *keyboard,
1836 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1837{
1838 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001839
Daniel Stone50692802012-06-22 13:21:41 +01001840 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001841 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001842 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001843 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001844 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001845}
1846
Daniel Stone351eb612012-05-31 15:27:47 -04001847static void
Derek Foreman1281a362015-07-31 16:55:32 -05001848input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001849 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001850 uint32_t mods_latched, uint32_t mods_locked,
1851 uint32_t group)
1852{
Derek Foreman1281a362015-07-31 16:55:32 -05001853 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001854 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001855 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001856 uint32_t serial_out;
1857
1858 /* If we get a key event followed by a modifier event with the
1859 * same serial number, then we try to preserve those semantics by
1860 * reusing the same serial number on the way out too. */
1861 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001862 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001863 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001864 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001865
Derek Foreman1281a362015-07-31 16:55:32 -05001866 keyboard = weston_seat_get_keyboard(&input->base);
1867 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001868 mods_depressed, mods_latched,
1869 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001870 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001871}
1872
Jonny Lamb497994a2014-08-12 14:58:26 +02001873static void
1874input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1875 int32_t rate, int32_t delay)
1876{
1877 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001878 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001879
Giulio Camuffo954f1832014-10-11 18:27:30 +03001880 b->compositor->kb_repeat_rate = rate;
1881 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001882}
1883
Daniel Stone37816df2012-05-16 18:45:18 +01001884static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001885 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001886 input_handle_keyboard_enter,
1887 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001888 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001889 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001890 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001891};
1892
1893static void
Derek Foreman748c6952015-11-06 15:56:10 -06001894input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1895 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001896 struct wl_surface *surface, int32_t id,
1897 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001898{
1899 struct wayland_input *input = data;
1900 struct wayland_output *output;
1901 enum theme_location location;
1902 bool first_touch;
1903 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001904 double x, y;
1905
1906 x = wl_fixed_to_double(fixed_x);
1907 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001908
1909 first_touch = (input->touch_points == 0);
1910 input->touch_points++;
1911
1912 input->touch_focus = wl_surface_get_user_data(surface);
1913 output = input->touch_focus;
1914 if (!first_touch && !input->touch_active)
1915 return;
1916
1917 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001918 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001919
1920 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001921 x -= fx;
1922 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001923
1924 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1925 weston_output_schedule_repaint(&output->base);
1926
1927 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1928 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001929 if (output->parent.xdg_toplevel)
1930 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1931 input->parent.seat, serial);
1932 else if (output->parent.shell_surface)
1933 wl_shell_surface_move(output->parent.shell_surface,
1934 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001935 frame_status_clear(output->frame,
1936 FRAME_STATUS_MOVE);
1937 return;
1938 }
1939
1940 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1941 return;
1942 }
1943
1944 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1945
1946 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1947 input->touch_active = true;
1948}
1949
1950static void
1951input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1952 uint32_t serial, uint32_t time, int32_t id)
1953{
1954 struct wayland_input *input = data;
1955 struct wayland_output *output = input->touch_focus;
1956 bool active = input->touch_active;
1957
1958 input->touch_points--;
1959 if (input->touch_points == 0) {
1960 input->touch_focus = NULL;
1961 input->touch_active = false;
1962 }
1963
1964 if (!output)
1965 return;
1966
1967 if (output->frame) {
1968 frame_touch_up(output->frame, input, id);
1969
1970 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1971 wayland_output_destroy(&output->base);
1972 input->touch_focus = NULL;
1973 input->keyboard_focus = NULL;
1974 if (wl_list_empty(&input->backend->compositor->output_list))
1975 weston_compositor_exit(input->backend->compositor);
1976
1977 return;
1978 }
1979 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1980 weston_output_schedule_repaint(&output->base);
1981 }
1982
1983 if (active)
1984 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1985}
1986
1987static void
1988input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001989 uint32_t time, int32_t id,
1990 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001991{
1992 struct wayland_input *input = data;
1993 struct wayland_output *output = input->touch_focus;
1994 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001995 double x, y;
1996
1997 x = wl_fixed_to_double(fixed_x);
1998 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001999
2000 if (!output || !input->touch_active)
2001 return;
2002
2003 if (output->frame) {
2004 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002005 x -= fx;
2006 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002007 }
2008
2009 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2010
2011 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2012}
2013
2014static void
2015input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2016{
2017 struct wayland_input *input = data;
2018
2019 if (!input->touch_focus || !input->touch_active)
2020 return;
2021
2022 notify_touch_frame(&input->base);
2023}
2024
2025static void
2026input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2027{
2028 struct wayland_input *input = data;
2029
2030 if (!input->touch_focus || !input->touch_active)
2031 return;
2032
2033 notify_touch_cancel(&input->base);
2034}
2035
2036static const struct wl_touch_listener touch_listener = {
2037 input_handle_touch_down,
2038 input_handle_touch_up,
2039 input_handle_touch_motion,
2040 input_handle_touch_frame,
2041 input_handle_touch_cancel,
2042};
2043
2044
2045static void
Daniel Stone37816df2012-05-16 18:45:18 +01002046input_handle_capabilities(void *data, struct wl_seat *seat,
2047 enum wl_seat_capability caps)
2048{
2049 struct wayland_input *input = data;
2050
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002051 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2052 input->parent.pointer = wl_seat_get_pointer(seat);
2053 wl_pointer_set_user_data(input->parent.pointer, input);
2054 wl_pointer_add_listener(input->parent.pointer,
2055 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002056 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002057 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002058 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2059 wl_pointer_release(input->parent.pointer);
2060 else
2061 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002062 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002063 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002064 }
2065
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002066 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2067 input->parent.keyboard = wl_seat_get_keyboard(seat);
2068 wl_keyboard_set_user_data(input->parent.keyboard, input);
2069 wl_keyboard_add_listener(input->parent.keyboard,
2070 &keyboard_listener, input);
2071 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002072 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2073 wl_keyboard_release(input->parent.keyboard);
2074 else
2075 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002076 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002077 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002078 }
Derek Foreman748c6952015-11-06 15:56:10 -06002079
2080 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2081 input->parent.touch = wl_seat_get_touch(seat);
2082 wl_touch_set_user_data(input->parent.touch, input);
2083 wl_touch_add_listener(input->parent.touch,
2084 &touch_listener, input);
2085 weston_seat_init_touch(&input->base);
2086 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2087 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2088 wl_touch_release(input->parent.touch);
2089 else
2090 wl_touch_destroy(input->parent.touch);
2091 input->parent.touch = NULL;
2092 weston_seat_release_touch(&input->base);
2093 }
Daniel Stone37816df2012-05-16 18:45:18 +01002094}
2095
Jonny Lamb497994a2014-08-12 14:58:26 +02002096static void
2097input_handle_name(void *data, struct wl_seat *seat,
2098 const char *name)
2099{
2100}
2101
Daniel Stone37816df2012-05-16 18:45:18 +01002102static const struct wl_seat_listener seat_listener = {
2103 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002104 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002105};
2106
2107static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002108display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002109{
2110 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002111 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002112
Peter Huttererf3d62272013-08-08 11:57:05 +10002113 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002114 if (input == NULL)
2115 return;
2116
Giulio Camuffo954f1832014-10-11 18:27:30 +03002117 weston_seat_init(&input->base, b->compositor, "default");
2118 input->backend = b;
2119 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002120 &wl_seat_interface, version);
2121 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002122 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002123
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002124 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2125 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002126
2127 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002128 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002129
2130 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2131 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002132}
2133
2134static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002135wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2136 int32_t x, int32_t y,
2137 int32_t physical_width, int32_t physical_height,
2138 int32_t subpixel, const char *make,
2139 const char *model, int32_t transform)
2140{
2141 struct wayland_parent_output *output = data;
2142
2143 output->x = x;
2144 output->y = y;
2145 output->physical.width = physical_width;
2146 output->physical.height = physical_height;
2147 output->physical.subpixel = subpixel;
2148
2149 free(output->physical.make);
2150 output->physical.make = strdup(make);
2151 free(output->physical.model);
2152 output->physical.model = strdup(model);
2153
2154 output->transform = transform;
2155}
2156
2157static struct weston_mode *
2158find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2159{
2160 struct weston_mode *mode;
2161
2162 wl_list_for_each(mode, list, link) {
2163 if (mode->width == width && mode->height == height &&
2164 mode->refresh == refresh)
2165 return mode;
2166 }
2167
2168 mode = zalloc(sizeof *mode);
2169 if (!mode)
2170 return NULL;
2171
2172 mode->width = width;
2173 mode->height = height;
2174 mode->refresh = refresh;
2175 wl_list_insert(list, &mode->link);
2176
2177 return mode;
2178}
2179
2180static void
2181wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2182 uint32_t flags, int32_t width, int32_t height,
2183 int32_t refresh)
2184{
2185 struct wayland_parent_output *output = data;
2186 struct weston_mode *mode;
2187
2188 if (output->output) {
2189 mode = find_mode(&output->output->base.mode_list,
2190 width, height, refresh);
2191 if (!mode)
2192 return;
2193 mode->flags = flags;
2194 /* Do a mode-switch on current mode change? */
2195 } else {
2196 mode = find_mode(&output->mode_list, width, height, refresh);
2197 if (!mode)
2198 return;
2199 mode->flags = flags;
2200 if (flags & WL_OUTPUT_MODE_CURRENT)
2201 output->current_mode = mode;
2202 if (flags & WL_OUTPUT_MODE_PREFERRED)
2203 output->preferred_mode = mode;
2204 }
2205}
2206
2207static const struct wl_output_listener output_listener = {
2208 wayland_parent_output_geometry,
2209 wayland_parent_output_mode
2210};
2211
2212static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002213wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002214{
2215 struct wayland_parent_output *output;
2216
2217 output = zalloc(sizeof *output);
2218 if (!output)
2219 return;
2220
2221 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002222 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002223 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002224 if (!output->global) {
2225 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002226 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002227 }
2228
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002229 wl_output_add_listener(output->global, &output_listener, output);
2230
2231 output->scale = 0;
2232 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2233 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2234 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002235 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002236
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237 if (b->sprawl_across_outputs) {
2238 wl_display_roundtrip(b->parent.wl_display);
2239 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002240 }
2241}
2242
2243static void
2244wayland_parent_output_destroy(struct wayland_parent_output *output)
2245{
2246 struct weston_mode *mode, *next;
2247
2248 if (output->output)
2249 wayland_output_destroy(&output->output->base);
2250
2251 wl_output_destroy(output->global);
2252 free(output->physical.make);
2253 free(output->physical.model);
2254
2255 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2256 wl_list_remove(&mode->link);
2257 free(mode);
2258 }
2259}
2260
2261static void
Armin Krezović34476192016-11-21 18:42:42 +01002262xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2263{
2264 zxdg_shell_v6_pong(shell, serial);
2265}
2266
2267static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2268 xdg_shell_ping,
2269};
2270
2271static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002272registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2273 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002274{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002275 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002276
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002277 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002278 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002279 wl_registry_bind(registry, name,
2280 &wl_compositor_interface, 1);
Armin Krezović34476192016-11-21 18:42:42 +01002281 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2282 b->parent.xdg_shell =
2283 wl_registry_bind(registry, name,
2284 &zxdg_shell_v6_interface, 1);
2285 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2286 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002287 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002288 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002289 wl_registry_bind(registry, name,
2290 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002291 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002292 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002293 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002294 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002295 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002296 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002297 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002298 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002299 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002300 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002301 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002302 }
2303}
2304
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002305static void
2306registry_handle_global_remove(void *data, struct wl_registry *registry,
2307 uint32_t name)
2308{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002310 struct wayland_parent_output *output;
2311
Giulio Camuffo954f1832014-10-11 18:27:30 +03002312 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002313 if (output->id == name)
2314 wayland_parent_output_destroy(output);
2315}
2316
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002317static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002318 registry_handle_global,
2319 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002320};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002321
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002322static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002323wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002324{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002325 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002326 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002327
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002328 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002329 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002330 return 0;
2331 }
2332
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002333 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002334 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002335 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002336 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002337
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002338 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002339 count = wl_display_dispatch_pending(b->parent.wl_display);
2340 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002341 }
2342
2343 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002344}
2345
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002346static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002347wayland_restore(struct weston_compositor *ec)
2348{
2349}
2350
2351static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002352wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002353{
Armin Krezović938dc522016-08-01 19:17:57 +02002354 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002355
Armin Krezović78895c52016-10-09 17:30:28 +02002356 wl_event_source_remove(b->parent.wl_source);
2357
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002358 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002359
Giulio Camuffo954f1832014-10-11 18:27:30 +03002360 if (b->parent.shm)
2361 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002362
Armin Krezović34476192016-11-21 18:42:42 +01002363 if (b->parent.xdg_shell)
2364 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2365
2366 if (b->parent.shell)
2367 wl_shell_destroy(b->parent.shell);
2368
Armin Krezović78895c52016-10-09 17:30:28 +02002369 if (b->parent.fshell)
2370 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2371
2372 if (b->parent.compositor)
2373 wl_compositor_destroy(b->parent.compositor);
2374
Armin Krezović78895c52016-10-09 17:30:28 +02002375 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
Daniel Stone698f9bf2016-11-24 15:31:33 +00002383 wl_registry_destroy(b->parent.registry);
2384 wl_display_flush(b->parent.wl_display);
2385 wl_display_disconnect(b->parent.wl_display);
2386
Giulio Camuffo954f1832014-10-11 18:27:30 +03002387 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002388}
2389
Jason Ekstrand7744f712013-10-27 22:24:55 -05002390static const char *left_ptrs[] = {
2391 "left_ptr",
2392 "default",
2393 "top_left_arrow",
2394 "left-arrow"
2395};
2396
2397static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002398create_cursor(struct wayland_backend *b,
2399 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002400{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002401 unsigned int i;
2402
Benoit Gschwind244ff792016-04-28 20:33:11 +02002403 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2404 config->cursor_size,
2405 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002406 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002407 fprintf(stderr, "could not load cursor theme\n");
2408 return;
2409 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002410
Giulio Camuffo954f1832014-10-11 18:27:30 +03002411 b->cursor = NULL;
2412 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2413 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002414 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002415 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002416 fprintf(stderr, "could not load left cursor\n");
2417 return;
2418 }
2419}
2420
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002421static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002422fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2423 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002424{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002425 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002426 struct wayland_input *input = NULL;
2427
Giulio Camuffo954f1832014-10-11 18:27:30 +03002428 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002429 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002430 break;
2431
2432 if (!input || !input->output)
2433 return;
2434
2435 if (input->output->frame)
2436 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2437 else
2438 wayland_output_set_windowed(input->output);
2439
2440 weston_output_schedule_repaint(&input->output->base);
2441}
2442
Giulio Camuffo954f1832014-10-11 18:27:30 +03002443static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002444wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002445 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002446{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002447 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002448 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002449 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002450
Giulio Camuffo954f1832014-10-11 18:27:30 +03002451 b = zalloc(sizeof *b);
2452 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002453 return NULL;
2454
Giulio Camuffo954f1832014-10-11 18:27:30 +03002455 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002456 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002457 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002458
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002459 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002460 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002461 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002462 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002463 }
2464
Giulio Camuffo954f1832014-10-11 18:27:30 +03002465 wl_list_init(&b->parent.output_list);
2466 wl_list_init(&b->input_list);
2467 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2468 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2469 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002470
Benoit Gschwind244ff792016-04-28 20:33:11 +02002471 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002472
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002473 b->use_pixman = new_config->use_pixman;
Armin Krezović174448a2016-09-30 14:11:09 +02002474 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002475
Giulio Camuffo954f1832014-10-11 18:27:30 +03002476 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002477 gl_renderer = weston_load_module("gl-renderer.so",
2478 "gl_renderer_interface");
2479 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002480 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002481 }
2482
Giulio Camuffo954f1832014-10-11 18:27:30 +03002483 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002484 if (gl_renderer->display_create(compositor,
2485 EGL_PLATFORM_WAYLAND_KHR,
2486 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002487 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002488 gl_renderer->alpha_attribs,
2489 NULL,
2490 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002491 weston_log("Failed to initialize the GL renderer; "
2492 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002493 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002494 }
2495 }
2496
Giulio Camuffo954f1832014-10-11 18:27:30 +03002497 if (b->use_pixman) {
2498 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002499 weston_log("Failed to initialize pixman renderer\n");
2500 goto err_display;
2501 }
2502 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002503
Giulio Camuffo954f1832014-10-11 18:27:30 +03002504 b->base.destroy = wayland_destroy;
2505 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002506
Giulio Camuffo954f1832014-10-11 18:27:30 +03002507 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002508
Giulio Camuffo954f1832014-10-11 18:27:30 +03002509 fd = wl_display_get_fd(b->parent.wl_display);
2510 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002511 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002512 wayland_backend_handle_event, b);
2513 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002514 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002515
Giulio Camuffo954f1832014-10-11 18:27:30 +03002516 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002517
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002518 if (compositor->renderer->import_dmabuf) {
2519 if (linux_dmabuf_setup(compositor) < 0)
2520 weston_log("Error: initializing dmabuf "
2521 "support failed.\n");
2522 }
2523
Giulio Camuffo954f1832014-10-11 18:27:30 +03002524 compositor->backend = &b->base;
2525 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002526err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002527 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002528err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002529 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002530 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002531 return NULL;
2532}
2533
2534static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002535wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002536{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002537 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002538
Giulio Camuffo954f1832014-10-11 18:27:30 +03002539 if (b->theme)
2540 theme_destroy(b->theme);
2541 if (b->frame_device)
2542 cairo_device_destroy(b->frame_device);
2543 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002544
Giulio Camuffo954f1832014-10-11 18:27:30 +03002545 weston_compositor_shutdown(b->compositor);
2546 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002547}
2548
Armin Krezović174448a2016-09-30 14:11:09 +02002549static const struct weston_windowed_output_api windowed_api = {
2550 wayland_output_set_size,
2551 wayland_output_create,
2552};
2553
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002554static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002555config_init_to_defaults(struct weston_wayland_backend_config *config)
2556{
2557}
2558
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002559WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002560backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002561 struct weston_backend_config *config_base)
2562{
2563 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002564 struct wayland_parent_output *poutput;
2565 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002566 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002567
Benoit Gschwinde091b452016-05-10 22:47:48 +02002568 if (config_base == NULL ||
2569 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2570 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2571 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002572 return -1;
2573 }
2574
2575 config_init_to_defaults(&new_config);
2576 memcpy(&new_config, config_base, config_base->struct_size);
2577
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002578 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002579
Giulio Camuffo954f1832014-10-11 18:27:30 +03002580 if (!b)
2581 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002582
Benoit Gschwind37a68072016-04-28 20:33:08 +02002583 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002584 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002585 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002586
Giulio Camuffo954f1832014-10-11 18:27:30 +03002587 wl_list_for_each(poutput, &b->parent.output_list, link)
2588 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002589
Giulio Camuffo954f1832014-10-11 18:27:30 +03002590 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002591 }
2592
Benoit Gschwind37a68072016-04-28 20:33:08 +02002593 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002594 if (wayland_output_create_fullscreen(b) < 0) {
2595 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002596 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002597 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002598
Giulio Camuffo954f1832014-10-11 18:27:30 +03002599 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002600 }
2601
Armin Krezović174448a2016-09-30 14:11:09 +02002602 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2603 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002604
Armin Krezović174448a2016-09-30 14:11:09 +02002605 if (ret < 0) {
2606 weston_log("Failed to register output API.\n");
2607 wayland_backend_destroy(b);
2608 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002609 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002610
Giulio Camuffo954f1832014-10-11 18:27:30 +03002611 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002612 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002613 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002615
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002616err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002617 wayland_backend_destroy(b);
2618 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002619}