blob: 8f217af7742129a5b5fa1060bb9a3e1247f1841c [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 wl_callback *callback;
597 struct wayland_shm_buffer *sb;
598
599 if (output->frame) {
600 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
601 wl_list_for_each(sb, &output->shm.buffers, link)
602 sb->frame_damaged = 1;
603 }
604
605 wl_list_for_each(sb, &output->shm.buffers, link)
606 pixman_region32_union(&sb->damage, &sb->damage, damage);
607
608 sb = wayland_output_get_shm_buffer(output);
609
610 wayland_output_update_shm_border(sb);
611 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300612 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500613
614 wayland_shm_buffer_attach(sb);
615
616 callback = wl_surface_frame(output->parent.surface);
617 wl_callback_add_listener(callback, &frame_listener, output);
618 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300619 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500620
621 pixman_region32_fini(&sb->damage);
622 pixman_region32_init(&sb->damage);
623 sb->frame_damaged = 0;
624
Giulio Camuffo954f1832014-10-11 18:27:30 +0300625 pixman_region32_subtract(&b->compositor->primary_plane.damage,
626 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500627 return 0;
628}
629
630static void
Armin Krezović174448a2016-09-30 14:11:09 +0200631wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700632{
Armin Krezović34476192016-11-21 18:42:42 +0100633 if (output->parent.xdg_toplevel)
634 zxdg_toplevel_v6_destroy(output->parent.xdg_toplevel);
635
636 if (output->parent.xdg_surface)
637 zxdg_surface_v6_destroy(output->parent.xdg_surface);
638
Armin Krezović174448a2016-09-30 14:11:09 +0200639 if (output->parent.shell_surface)
640 wl_shell_surface_destroy(output->parent.shell_surface);
641
642 wl_surface_destroy(output->parent.surface);
643}
644
Armin Krezović2e662522016-10-09 17:30:29 +0200645static void
646wayland_output_destroy_shm_buffers(struct wayland_output *output)
647{
648 struct wayland_shm_buffer *buffer, *next;
649
650 /* Throw away any remaining SHM buffers */
651 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
652 wayland_shm_buffer_destroy(buffer);
653 /* These will get thrown away when they get released */
654 wl_list_for_each(buffer, &output->shm.buffers, link)
655 buffer->output = NULL;
656}
657
Armin Krezović174448a2016-09-30 14:11:09 +0200658static int
659wayland_output_disable(struct weston_output *base)
660{
661 struct wayland_output *output = to_wayland_output(base);
662 struct wayland_backend *b = to_wayland_backend(base->compositor);
663
664 if (!output->base.enabled)
665 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700666
Giulio Camuffo954f1832014-10-11 18:27:30 +0300667 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200668 pixman_renderer_output_destroy(&output->base);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500669 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200670 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100671 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500672 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100673
Armin Krezović2e662522016-10-09 17:30:29 +0200674 wayland_output_destroy_shm_buffers(output);
675
Armin Krezovićf054d352016-10-09 17:30:27 +0200676 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500677
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600678 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500679 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600680
681 cairo_surface_destroy(output->gl.border.top);
682 cairo_surface_destroy(output->gl.border.left);
683 cairo_surface_destroy(output->gl.border.right);
684 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500685
Armin Krezović174448a2016-09-30 14:11:09 +0200686 return 0;
687}
Matt Roper361d2ad2011-08-29 13:52:23 -0700688
Armin Krezović174448a2016-09-30 14:11:09 +0200689static void
690wayland_output_destroy(struct weston_output *base)
691{
692 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +0200693
694 wayland_output_disable(&output->base);
695
Armin Krezović174448a2016-09-30 14:11:09 +0200696 weston_output_destroy(&output->base);
697
Dima Ryazanov89c2f632016-11-24 05:13:12 -0800698 if (output->frame_cb)
699 wl_callback_destroy(output->frame_cb);
700
Armin Krezović174448a2016-09-30 14:11:09 +0200701 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700702}
703
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300704static const struct wl_shell_surface_listener shell_surface_listener;
705
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200706static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500707wayland_output_init_gl_renderer(struct wayland_output *output)
708{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500709 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500710
711 if (output->frame) {
712 fwidth = frame_width(output->frame);
713 fheight = frame_height(output->frame);
714 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500715 fwidth = output->base.current_mode->width;
716 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500717 }
718
719 output->gl.egl_window =
720 wl_egl_window_create(output->parent.surface,
721 fwidth, fheight);
722 if (!output->gl.egl_window) {
723 weston_log("failure to create wl_egl_window\n");
724 return -1;
725 }
726
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200727 if (gl_renderer->output_window_create(&output->base,
728 output->gl.egl_window,
729 output->gl.egl_window,
730 gl_renderer->alpha_attribs,
731 NULL,
732 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500733 goto cleanup_window;
734
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500735 return 0;
736
737cleanup_window:
738 wl_egl_window_destroy(output->gl.egl_window);
739 return -1;
740}
741
742static int
743wayland_output_init_pixman_renderer(struct wayland_output *output)
744{
745 return pixman_renderer_output_create(&output->base);
746}
747
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600748static void
749wayland_output_resize_surface(struct wayland_output *output)
750{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300751 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200752 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600753 int32_t ix, iy, iwidth, iheight;
754 int32_t width, height;
755 struct wl_region *region;
756
757 width = output->base.current_mode->width;
758 height = output->base.current_mode->height;
759
760 if (output->frame) {
761 frame_resize_inside(output->frame, width, height);
762
763 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300764 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600765 wl_region_add(region, ix, iy, iwidth, iheight);
766 wl_surface_set_input_region(output->parent.surface, region);
767 wl_region_destroy(region);
768
769 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300770 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600771 wl_region_add(region, ix, iy, iwidth, iheight);
772 wl_surface_set_opaque_region(output->parent.surface, region);
773 wl_region_destroy(region);
774
775 width = frame_width(output->frame);
776 height = frame_height(output->frame);
777 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300778 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600779 wl_region_add(region, 0, 0, width, height);
780 wl_surface_set_input_region(output->parent.surface, region);
781 wl_region_destroy(region);
782
Giulio Camuffo954f1832014-10-11 18:27:30 +0300783 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600784 wl_region_add(region, 0, 0, width, height);
785 wl_surface_set_opaque_region(output->parent.surface, region);
786 wl_region_destroy(region);
787 }
788
789 if (output->gl.egl_window) {
790 wl_egl_window_resize(output->gl.egl_window,
791 width, height, 0, 0);
792
793 /* These will need to be re-created due to the resize */
794 gl_renderer->output_set_border(&output->base,
795 GL_RENDERER_BORDER_TOP,
796 0, 0, 0, NULL);
797 cairo_surface_destroy(output->gl.border.top);
798 output->gl.border.top = NULL;
799 gl_renderer->output_set_border(&output->base,
800 GL_RENDERER_BORDER_LEFT,
801 0, 0, 0, NULL);
802 cairo_surface_destroy(output->gl.border.left);
803 output->gl.border.left = NULL;
804 gl_renderer->output_set_border(&output->base,
805 GL_RENDERER_BORDER_RIGHT,
806 0, 0, 0, NULL);
807 cairo_surface_destroy(output->gl.border.right);
808 output->gl.border.right = NULL;
809 gl_renderer->output_set_border(&output->base,
810 GL_RENDERER_BORDER_BOTTOM,
811 0, 0, 0, NULL);
812 cairo_surface_destroy(output->gl.border.bottom);
813 output->gl.border.bottom = NULL;
814 }
815
Armin Krezović2e662522016-10-09 17:30:29 +0200816 wayland_output_destroy_shm_buffers(output);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600817}
818
819static int
820wayland_output_set_windowed(struct wayland_output *output)
821{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300822 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200823 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600824 int tlen;
825 char *title;
826
827 if (output->frame)
828 return 0;
829
830 if (output->name) {
831 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
832 title = malloc(tlen + 1);
833 if (!title)
834 return -1;
835
836 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
837 } else {
838 title = strdup(WINDOW_TITLE);
839 }
840
Armin Krezović34476192016-11-21 18:42:42 +0100841 if (output->parent.xdg_toplevel)
842 zxdg_toplevel_v6_set_title(output->parent.xdg_toplevel, title);
843
Giulio Camuffo954f1832014-10-11 18:27:30 +0300844 if (!b->theme) {
845 b->theme = theme_create();
846 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600847 free(title);
848 return -1;
849 }
850 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300851 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600852 FRAME_BUTTON_CLOSE, title);
853 free(title);
854 if (!output->frame)
855 return -1;
856
857 if (output->keyboard_count)
858 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
859
860 wayland_output_resize_surface(output);
861
Armin Krezović34476192016-11-21 18:42:42 +0100862 if (output->parent.shell_surface)
863 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600864
865 return 0;
866}
867
868static void
869wayland_output_set_fullscreen(struct wayland_output *output,
870 enum wl_shell_surface_fullscreen_method method,
871 uint32_t framerate, struct wl_output *target)
872{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300873 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200874 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500875
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600876 if (output->frame) {
877 frame_destroy(output->frame);
878 output->frame = NULL;
879 }
880
881 wayland_output_resize_surface(output);
882
Armin Krezović34476192016-11-21 18:42:42 +0100883 if (output->parent.xdg_toplevel) {
884 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel, target);
885 } else if (output->parent.shell_surface) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500886 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
887 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300888 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800889 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
890 output->parent.surface,
891 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500892 }
893}
894
895static struct weston_mode *
896wayland_output_choose_mode(struct wayland_output *output,
897 struct weston_mode *ref_mode)
898{
899 struct weston_mode *mode;
900
901 /* First look for an exact match */
902 wl_list_for_each(mode, &output->base.mode_list, link)
903 if (mode->width == ref_mode->width &&
904 mode->height == ref_mode->height &&
905 mode->refresh == ref_mode->refresh)
906 return mode;
907
908 /* If we can't find an exact match, ignore refresh and try again */
909 wl_list_for_each(mode, &output->base.mode_list, link)
910 if (mode->width == ref_mode->width &&
911 mode->height == ref_mode->height)
912 return mode;
913
914 /* Yeah, we failed */
915 return NULL;
916}
917
918enum mode_status {
919 MODE_STATUS_UNKNOWN,
920 MODE_STATUS_SUCCESS,
921 MODE_STATUS_FAIL,
922 MODE_STATUS_CANCEL,
923};
924
925static void
926mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800927 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500928{
929 enum mode_status *value = data;
930
931 printf("Mode switch successful\n");
932
933 *value = MODE_STATUS_SUCCESS;
934}
935
936static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800937mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500938{
939 enum mode_status *value = data;
940
941 printf("Mode switch failed\n");
942
943 *value = MODE_STATUS_FAIL;
944}
945
946static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800947mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500948{
949 enum mode_status *value = data;
950
951 printf("Mode switch cancelled\n");
952
953 *value = MODE_STATUS_CANCEL;
954}
955
Jonas Ådahl496adb32015-11-17 16:00:27 +0800956struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500957 mode_feedback_successful,
958 mode_feedback_failed,
959 mode_feedback_cancelled,
960};
961
962static int
963wayland_output_switch_mode(struct weston_output *output_base,
964 struct weston_mode *mode)
965{
Armin Krezović938dc522016-08-01 19:17:57 +0200966 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300967 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500968 struct wl_surface *old_surface;
969 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800970 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500971 enum mode_status mode_status;
972 int ret = 0;
973
974 if (output_base == NULL) {
975 weston_log("output is NULL.\n");
976 return -1;
977 }
978
979 if (mode == NULL) {
980 weston_log("mode is NULL.\n");
981 return -1;
982 }
983
Armin Krezović938dc522016-08-01 19:17:57 +0200984 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500985
Armin Krezović34476192016-11-21 18:42:42 +0100986 if (output->parent.xdg_surface || output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500987 return -1;
988
989 mode = wayland_output_choose_mode(output, mode);
990 if (mode == NULL)
991 return -1;
992
993 if (output->base.current_mode == mode)
994 return 0;
995
996 old_mode = output->base.current_mode;
997 old_surface = output->parent.surface;
998 output->base.current_mode = mode;
999 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001000 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001001 wl_surface_set_user_data(output->parent.surface, output);
1002
1003 /* Blow the old buffers because we changed size/surfaces */
1004 wayland_output_resize_surface(output);
1005
1006 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +08001007 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1008 output->parent.surface,
1009 output->parent.output,
1010 mode->refresh);
1011 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
1012 &mode_feedback_listener,
1013 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001014
1015 /* This should kick-start things again */
Armin Krezović2d321e32016-10-09 17:30:25 +02001016 output->parent.draw_initial_frame = true;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001017 wayland_output_start_repaint_loop(&output->base);
1018
1019 mode_status = MODE_STATUS_UNKNOWN;
1020 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001021 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001022
Jonas Ådahl496adb32015-11-17 16:00:27 +08001023 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001024
1025 if (mode_status == MODE_STATUS_FAIL) {
1026 output->base.current_mode = old_mode;
1027 wl_surface_destroy(output->parent.surface);
1028 output->parent.surface = old_surface;
1029 wayland_output_resize_surface(output);
1030
1031 return -1;
1032 }
1033
1034 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1035 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1036
Giulio Camuffo954f1832014-10-11 18:27:30 +03001037 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001038 pixman_renderer_output_destroy(output_base);
1039 if (wayland_output_init_pixman_renderer(output) < 0)
1040 goto err_output;
1041 } else {
1042 gl_renderer->output_destroy(output_base);
1043 wl_egl_window_destroy(output->gl.egl_window);
1044 if (wayland_output_init_gl_renderer(output) < 0)
1045 goto err_output;
1046 }
1047 wl_surface_destroy(old_surface);
1048
1049 weston_output_schedule_repaint(&output->base);
1050
1051 return 0;
1052
1053err_output:
1054 /* XXX */
1055 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001056}
1057
Armin Krezović34476192016-11-21 18:42:42 +01001058static void
1059handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
1060 uint32_t serial)
1061{
1062 zxdg_surface_v6_ack_configure(surface, serial);
1063}
1064
1065static const struct zxdg_surface_v6_listener xdg_surface_listener = {
1066 handle_xdg_surface_configure
1067};
1068
1069static void
1070handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
1071 int32_t width, int32_t height,
1072 struct wl_array *states)
1073{
1074 struct wayland_output *output = data;
1075
1076 output->parent.configure_width = width;
1077 output->parent.configure_height = height;
1078
1079 output->parent.wait_for_configure = false;
1080 /* FIXME: implement resizing */
1081}
1082
1083static void
1084handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
1085{
1086}
1087
1088static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
1089 handle_xdg_toplevel_configure,
1090 handle_xdg_toplevel_close,
1091};
1092
Armin Krezović174448a2016-09-30 14:11:09 +02001093static int
1094wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001095{
Armin Krezović174448a2016-09-30 14:11:09 +02001096 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001097
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001098 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001099 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001100 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001101 return -1;
1102
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001103 wl_surface_set_user_data(output->parent.surface, output);
1104
Armin Krezović2d321e32016-10-09 17:30:25 +02001105 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001106
Armin Krezović34476192016-11-21 18:42:42 +01001107 if (b->parent.xdg_shell) {
1108 output->parent.xdg_surface =
1109 zxdg_shell_v6_get_xdg_surface(b->parent.xdg_shell,
1110 output->parent.surface);
1111 zxdg_surface_v6_add_listener(output->parent.xdg_surface,
1112 &xdg_surface_listener, output);
1113
1114 output->parent.xdg_toplevel =
1115 zxdg_surface_v6_get_toplevel(output->parent.xdg_surface);
1116 zxdg_toplevel_v6_add_listener(output->parent.xdg_toplevel,
1117 &xdg_toplevel_listener, output);
1118
1119 wl_surface_commit(output->parent.surface);
1120
1121 output->parent.wait_for_configure = true;
1122
1123 while (output->parent.wait_for_configure)
1124 wl_display_dispatch(b->parent.wl_display);
1125
1126 weston_log("wayland-backend: Using xdg_shell_v6\n");
1127 }
1128 else if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001129 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001130 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001131 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001132 if (!output->parent.shell_surface) {
1133 wl_surface_destroy(output->parent.surface);
1134 return -1;
1135 }
1136
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001137 wl_shell_surface_add_listener(output->parent.shell_surface,
1138 &shell_surface_listener, output);
Armin Krezović34476192016-11-21 18:42:42 +01001139
1140 weston_log("wayland-backend: Using wl_shell\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001141 }
1142
Armin Krezović174448a2016-09-30 14:11:09 +02001143 return 0;
1144}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001145
Armin Krezović174448a2016-09-30 14:11:09 +02001146static int
1147wayland_output_enable(struct weston_output *base)
1148{
1149 struct wayland_output *output = to_wayland_output(base);
1150 struct wayland_backend *b = to_wayland_backend(base->compositor);
1151 int ret = 0;
1152
Armin Krezović174448a2016-09-30 14:11:09 +02001153 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1154 output->base.current_mode->width,
1155 output->base.current_mode->height,
1156 output->base.x, output->base.y);
1157
Armin Krezovićf054d352016-10-09 17:30:27 +02001158 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001159 ret = wayland_backend_create_output_surface(output);
1160
1161 if (ret < 0)
1162 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001163
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001164 wl_list_init(&output->shm.buffers);
1165 wl_list_init(&output->shm.free_buffers);
1166
Giulio Camuffo954f1832014-10-11 18:27:30 +03001167 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001168 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001169 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001170
1171 output->base.repaint = wayland_output_repaint_pixman;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001172 } else {
1173 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001174 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001175
1176 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001177 }
1178
Armin Krezovićf16de172016-10-09 17:30:26 +02001179 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1180 output->base.assign_planes = NULL;
1181 output->base.set_backlight = NULL;
1182 output->base.set_dpms = NULL;
1183 output->base.switch_mode = wayland_output_switch_mode;
1184
Armin Krezović174448a2016-09-30 14:11:09 +02001185 if (b->sprawl_across_outputs) {
1186 wayland_output_set_fullscreen(output,
1187 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1188 output->mode.refresh, output->parent.output);
1189
Armin Krezović34476192016-11-21 18:42:42 +01001190 if (output->parent.xdg_toplevel) {
1191 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1192 output->parent.output);
1193 }
1194 else if (output->parent.shell_surface) {
Armin Krezović174448a2016-09-30 14:11:09 +02001195 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1196 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1197 output->mode.refresh, output->parent.output);
1198 } else if (b->parent.fshell) {
1199 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1200 output->parent.surface,
1201 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1202 output->parent.output);
1203 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1204 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1205 output->parent.surface,
1206 output->parent.output,
1207 output->mode.refresh));
1208 }
1209 } else if (b->fullscreen) {
1210 wayland_output_set_fullscreen(output, 0, 0, NULL);
1211 } else {
1212 wayland_output_set_windowed(output);
1213 }
1214
1215 return 0;
1216
1217err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001218 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001219
1220 return -1;
1221}
1222
1223static struct wayland_output *
1224wayland_output_create_common(void)
1225{
1226 struct wayland_output *output;
1227
1228 output = zalloc(sizeof *output);
1229 if (output == NULL) {
1230 perror("zalloc");
1231 return NULL;
1232 }
1233
Matt Roper361d2ad2011-08-29 13:52:23 -07001234 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001235 output->base.disable = wayland_output_disable;
1236 output->base.enable = wayland_output_enable;
1237
1238 return output;
1239}
1240
1241static int
1242wayland_output_create(struct weston_compositor *compositor, const char *name)
1243{
1244 struct wayland_output *output = wayland_output_create_common();
1245
1246 /* name can't be NULL. */
1247 assert(name);
1248
1249 output->base.name = strdup(name);
1250
Armin Krezović40087402016-09-30 14:11:12 +02001251 weston_output_init(&output->base, compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001252 weston_compositor_add_pending_output(&output->base, compositor);
1253
1254 return 0;
1255}
1256
1257static int
1258wayland_output_set_size(struct weston_output *base, int width, int height)
1259{
1260 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001261 int output_width, output_height;
1262
1263 /* We can only be called once. */
1264 assert(!output->base.current_mode);
1265
1266 /* Make sure we have scale set. */
1267 assert(output->base.scale);
1268
1269 if (width < 1) {
1270 weston_log("Invalid width \"%d\" for output %s\n",
1271 width, output->base.name);
1272 return -1;
1273 }
1274
1275 if (height < 1) {
1276 weston_log("Invalid height \"%d\" for output %s\n",
1277 height, output->base.name);
1278 return -1;
1279 }
1280
1281 output_width = width * output->base.scale;
1282 output_height = height * output->base.scale;
1283
1284 output->mode.flags =
1285 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1286
1287 output->mode.width = output_width;
1288 output->mode.height = output_height;
1289 output->mode.refresh = 60000;
1290 output->scale = output->base.scale;
1291 wl_list_init(&output->base.mode_list);
1292 wl_list_insert(&output->base.mode_list, &output->mode.link);
1293
1294 output->base.current_mode = &output->mode;
1295 output->base.make = "wayland";
1296 output->base.model = "none";
1297
1298 /* XXX: Calculate proper size. */
1299 output->base.mm_width = width;
1300 output->base.mm_height = height;
1301
Armin Krezović174448a2016-09-30 14:11:09 +02001302 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001303}
1304
Armin Krezović174448a2016-09-30 14:11:09 +02001305static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001306wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001307 struct wayland_parent_output *poutput)
1308{
Armin Krezović174448a2016-09-30 14:11:09 +02001309 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001310 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001311
1312 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001313
1314 if (poutput->current_mode) {
1315 mode = poutput->current_mode;
1316 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001317 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001318 } else if (!wl_list_empty(&poutput->mode_list)) {
1319 mode = container_of(poutput->mode_list.next,
1320 struct weston_mode, link);
1321 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001322 weston_log("No valid modes found. Skipping output.\n");
1323 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001324 }
1325
Armin Krezović40087402016-09-30 14:11:12 +02001326 weston_output_init(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001327
Armin Krezović174448a2016-09-30 14:11:09 +02001328 output->base.scale = 1;
1329 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001330
Armin Krezović174448a2016-09-30 14:11:09 +02001331 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1332 goto out;
1333
1334 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001335 output->parent.output = poutput->global;
1336
1337 output->base.make = poutput->physical.make;
1338 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001339
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001340 wl_list_init(&output->base.mode_list);
1341 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1342 wl_list_init(&poutput->mode_list);
1343
Armin Krezović174448a2016-09-30 14:11:09 +02001344 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001345
Armin Krezović174448a2016-09-30 14:11:09 +02001346 return 0;
1347
1348out:
1349 free(output->name);
1350 free(output);
1351
1352 return -1;
1353}
1354
1355static int
1356wayland_output_create_fullscreen(struct wayland_backend *b)
1357{
1358 struct wayland_output *output = wayland_output_create_common();
1359 int width = 0, height = 0;
1360
1361 output->base.name = strdup("wayland-fullscreen");
1362
Armin Krezović40087402016-09-30 14:11:12 +02001363 weston_output_init(&output->base, b->compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001364
1365 output->base.scale = 1;
1366 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1367
1368 if (wayland_backend_create_output_surface(output) < 0)
1369 goto err_surface;
1370
1371 /* What should size be set if conditional is false? */
Armin Krezović34476192016-11-21 18:42:42 +01001372 if (b->parent.xdg_shell || b->parent.shell) {
1373 if (output->parent.xdg_toplevel)
1374 zxdg_toplevel_v6_set_fullscreen(output->parent.xdg_toplevel,
1375 output->parent.output);
1376 else if (output->parent.shell_surface)
1377 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1378 0, 0, NULL);
1379
Armin Krezović174448a2016-09-30 14:11:09 +02001380 wl_display_roundtrip(b->parent.wl_display);
1381
1382 width = output->parent.configure_width;
1383 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001384 }
1385
Armin Krezović174448a2016-09-30 14:11:09 +02001386 if (wayland_output_set_size(&output->base, width, height) < 0)
1387 goto err_set_size;
1388
1389 weston_compositor_add_pending_output(&output->base, b->compositor);
1390
1391 return 0;
1392
1393err_set_size:
1394 wayland_backend_destroy_output_surface(output);
1395err_surface:
1396 free(output->name);
1397 free(output);
1398
1399 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001400}
1401
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001402static void
1403shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1404 uint32_t serial)
1405{
1406 wl_shell_surface_pong(shell_surface, serial);
1407}
1408
1409static void
1410shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1411 uint32_t edges, int32_t width, int32_t height)
1412{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001413 struct wayland_output *output = data;
1414
1415 output->parent.configure_width = width;
1416 output->parent.configure_height = height;
1417
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001418 /* FIXME: implement resizing */
1419}
1420
1421static void
1422shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1423{
1424}
1425
1426static const struct wl_shell_surface_listener shell_surface_listener = {
1427 shell_surface_ping,
1428 shell_surface_configure,
1429 shell_surface_popup_done
1430};
1431
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001432/* Events received from the wayland-server this compositor is client of: */
1433
Jason Ekstrand7744f712013-10-27 22:24:55 -05001434/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001435static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001436input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001437{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001438
Jason Ekstrand7744f712013-10-27 22:24:55 -05001439 struct wl_buffer *buffer;
1440 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001441
Giulio Camuffo954f1832014-10-11 18:27:30 +03001442 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001443 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001444
Giulio Camuffo954f1832014-10-11 18:27:30 +03001445 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001446 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001447 if (!buffer)
1448 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001449
1450 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1451 input->parent.cursor.surface,
1452 image->hotspot_x, image->hotspot_y);
1453
1454 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1455 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1456 image->width, image->height);
1457 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001458}
1459
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001460static void
Daniel Stone37816df2012-05-16 18:45:18 +01001461input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1462 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001463 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001464{
1465 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001466 int32_t fx, fy;
1467 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001468 double x, y;
1469
1470 x = wl_fixed_to_double(fixed_x);
1471 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001472
Daniel Stone50692802012-06-22 13:21:41 +01001473 /* XXX: If we get a modifier event immediately before the focus,
1474 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001475 input->enter_serial = serial;
1476 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001477
1478 if (input->output->frame) {
1479 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001480 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001481 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001482 x -= fx;
1483 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001484
1485 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1486 weston_output_schedule_repaint(&input->output->base);
1487 } else {
1488 location = THEME_LOCATION_CLIENT_AREA;
1489 }
1490
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001491 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001492
1493 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001494 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001495 notify_pointer_focus(&input->base, &input->output->base, x, y);
1496 wl_pointer_set_cursor(input->parent.pointer,
1497 input->enter_serial, NULL, 0, 0);
1498 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001499 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001500 notify_pointer_focus(&input->base, NULL, 0, 0);
1501 input_set_cursor(input);
1502 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001503}
1504
1505static void
Daniel Stone37816df2012-05-16 18:45:18 +01001506input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1507 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001508{
1509 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001510
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001511 if (!input->output)
1512 return;
1513
Jason Ekstrand7744f712013-10-27 22:24:55 -05001514 if (input->output->frame) {
1515 frame_pointer_leave(input->output->frame, input);
1516
1517 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1518 weston_output_schedule_repaint(&input->output->base);
1519 }
1520
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001521 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001522 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001523 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001524}
1525
1526static void
Daniel Stone37816df2012-05-16 18:45:18 +01001527input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001528 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001529{
1530 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001531 int32_t fx, fy;
1532 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001533 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001534 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001535
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001536 if (!input->output)
1537 return;
1538
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001539 x = wl_fixed_to_double(fixed_x);
1540 y = wl_fixed_to_double(fixed_y);
1541
Jason Ekstrand7744f712013-10-27 22:24:55 -05001542 if (input->output->frame) {
1543 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001544 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001545 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001546 x -= fx;
1547 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001548
1549 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1550 weston_output_schedule_repaint(&input->output->base);
1551 } else {
1552 location = THEME_LOCATION_CLIENT_AREA;
1553 }
1554
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001555 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001556
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001557 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001558 input_set_cursor(input);
1559 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001560 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001561 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001562 } else if (!input->has_focus &&
1563 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001564 wl_pointer_set_cursor(input->parent.pointer,
1565 input->enter_serial, NULL, 0, 0);
1566 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001567 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001568 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001569 }
1570
Peter Hutterer87743e92016-01-18 16:38:22 +10001571 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001572 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001573 want_frame = true;
1574 }
1575
1576 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1577 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001578}
1579
1580static void
1581input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001582 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001583 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001584{
1585 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001586 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001587
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001588 if (!input->output)
1589 return;
1590
Jason Ekstrand7744f712013-10-27 22:24:55 -05001591 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001592 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001593 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001594
1595 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
Armin Krezović34476192016-11-21 18:42:42 +01001596 if (input->output->parent.xdg_toplevel)
1597 zxdg_toplevel_v6_move(input->output->parent.xdg_toplevel,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001598 input->parent.seat, serial);
Armin Krezović34476192016-11-21 18:42:42 +01001599 else if (input->output->parent.shell_surface)
1600 wl_shell_surface_move(input->output->parent.shell_surface,
1601 input->parent.seat, serial);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001602 frame_status_clear(input->output->frame,
1603 FRAME_STATUS_MOVE);
1604 return;
1605 }
1606
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001607 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1608 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001609 input->output = NULL;
1610 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001611
Giulio Camuffo954f1832014-10-11 18:27:30 +03001612 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001613 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001614
1615 return;
1616 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001617
1618 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1619 weston_output_schedule_repaint(&input->output->base);
1620 } else {
1621 location = THEME_LOCATION_CLIENT_AREA;
1622 }
1623
Peter Hutterer87743e92016-01-18 16:38:22 +10001624 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001625 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001626 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1627 notify_pointer_frame(&input->base);
1628 }
Daniel Stone37816df2012-05-16 18:45:18 +01001629}
1630
1631static void
1632input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001633 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001634{
1635 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001636 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001637
Peter Hutterer89b6a492016-01-18 15:58:17 +10001638 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001639 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001640
Peter Hutterer87743e92016-01-18 16:38:22 +10001641 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1642 input->vert.has_discrete) {
1643 weston_event.has_discrete = true;
1644 weston_event.discrete = input->vert.discrete;
1645 input->vert.has_discrete = false;
1646 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1647 input->horiz.has_discrete) {
1648 weston_event.has_discrete = true;
1649 weston_event.discrete = input->horiz.discrete;
1650 input->horiz.has_discrete = false;
1651 }
1652
Peter Hutterer89b6a492016-01-18 15:58:17 +10001653 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001654
1655 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1656 notify_pointer_frame(&input->base);
1657}
1658
1659static void
1660input_handle_frame(void *data, struct wl_pointer *pointer)
1661{
1662 struct wayland_input *input = data;
1663
1664 notify_pointer_frame(&input->base);
1665}
1666
1667static void
1668input_handle_axis_source(void *data, struct wl_pointer *pointer,
1669 uint32_t source)
1670{
1671 struct wayland_input *input = data;
1672
1673 notify_axis_source(&input->base, source);
1674}
1675
1676static void
1677input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1678 uint32_t time, uint32_t axis)
1679{
1680 struct wayland_input *input = data;
1681 struct weston_pointer_axis_event weston_event;
1682
1683 weston_event.axis = axis;
1684 weston_event.value = 0;
1685
1686 notify_axis(&input->base, time, &weston_event);
1687}
1688
1689static void
1690input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1691 uint32_t axis, int32_t discrete)
1692{
1693 struct wayland_input *input = data;
1694
1695 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1696 input->vert.has_discrete = true;
1697 input->vert.discrete = discrete;
1698 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1699 input->horiz.has_discrete = true;
1700 input->horiz.discrete = discrete;
1701 }
Daniel Stone37816df2012-05-16 18:45:18 +01001702}
1703
1704static const struct wl_pointer_listener pointer_listener = {
1705 input_handle_pointer_enter,
1706 input_handle_pointer_leave,
1707 input_handle_motion,
1708 input_handle_button,
1709 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001710 input_handle_frame,
1711 input_handle_axis_source,
1712 input_handle_axis_stop,
1713 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001714};
1715
1716static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001717input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1718 int fd, uint32_t size)
1719{
1720 struct wayland_input *input = data;
1721 struct xkb_keymap *keymap;
1722 char *map_str;
1723
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001724 if (!data) {
1725 close(fd);
1726 return;
1727 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001728
1729 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1730 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1731 if (map_str == MAP_FAILED) {
1732 weston_log("mmap failed: %m\n");
1733 goto error;
1734 }
1735
Giulio Camuffo954f1832014-10-11 18:27:30 +03001736 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001737 map_str,
1738 XKB_KEYMAP_FORMAT_TEXT_V1,
1739 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001740 munmap(map_str, size);
1741
1742 if (!keymap) {
1743 weston_log("failed to compile keymap\n");
1744 goto error;
1745 }
1746
1747 input->keyboard_state_update = STATE_UPDATE_NONE;
1748 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1749 weston_log("No keymap provided; falling back to defalt\n");
1750 keymap = NULL;
1751 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1752 } else {
1753 weston_log("Invalid keymap\n");
1754 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001755 }
1756
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001757 close(fd);
1758
Derek Foreman1281a362015-07-31 16:55:32 -05001759 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001760 weston_seat_update_keymap(&input->base, keymap);
1761 else
1762 weston_seat_init_keyboard(&input->base, keymap);
1763
Ran Benitac9c74152014-08-19 23:59:52 +03001764 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001765
1766 return;
1767
1768error:
1769 wl_keyboard_release(input->parent.keyboard);
1770 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001771}
1772
1773static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001774input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001775 struct wl_keyboard *keyboard,
1776 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001777 struct wl_surface *surface,
1778 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001779{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001780 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001781 struct wayland_output *focus;
1782
1783 focus = input->keyboard_focus;
1784 if (focus) {
1785 /* This shouldn't happen */
1786 focus->keyboard_count--;
1787 if (!focus->keyboard_count && focus->frame)
1788 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1789 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1790 weston_output_schedule_repaint(&focus->base);
1791 }
1792
1793 input->keyboard_focus = wl_surface_get_user_data(surface);
1794 input->keyboard_focus->keyboard_count++;
1795
1796 focus = input->keyboard_focus;
1797 if (focus->frame) {
1798 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1799 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1800 weston_output_schedule_repaint(&focus->base);
1801 }
1802
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001803
Daniel Stone50692802012-06-22 13:21:41 +01001804 /* XXX: If we get a modifier event immediately before the focus,
1805 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001806 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001807 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001808}
1809
1810static void
1811input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001812 struct wl_keyboard *keyboard,
1813 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001814 struct wl_surface *surface)
1815{
1816 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001817 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001818
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001819 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001820
1821 focus = input->keyboard_focus;
1822 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001823 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001824
1825 focus->keyboard_count--;
1826 if (!focus->keyboard_count && focus->frame) {
1827 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1828 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1829 weston_output_schedule_repaint(&focus->base);
1830 }
1831
1832 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001833}
1834
Daniel Stone37816df2012-05-16 18:45:18 +01001835static void
1836input_handle_key(void *data, struct wl_keyboard *keyboard,
1837 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1838{
1839 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001840
Daniel Stone50692802012-06-22 13:21:41 +01001841 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001842 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001843 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001844 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001845 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001846}
1847
Daniel Stone351eb612012-05-31 15:27:47 -04001848static void
Derek Foreman1281a362015-07-31 16:55:32 -05001849input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001850 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001851 uint32_t mods_latched, uint32_t mods_locked,
1852 uint32_t group)
1853{
Derek Foreman1281a362015-07-31 16:55:32 -05001854 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001855 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001856 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001857 uint32_t serial_out;
1858
1859 /* If we get a key event followed by a modifier event with the
1860 * same serial number, then we try to preserve those semantics by
1861 * reusing the same serial number on the way out too. */
1862 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001863 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001864 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001865 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001866
Derek Foreman1281a362015-07-31 16:55:32 -05001867 keyboard = weston_seat_get_keyboard(&input->base);
1868 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001869 mods_depressed, mods_latched,
1870 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001871 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001872}
1873
Jonny Lamb497994a2014-08-12 14:58:26 +02001874static void
1875input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1876 int32_t rate, int32_t delay)
1877{
1878 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001879 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001880
Giulio Camuffo954f1832014-10-11 18:27:30 +03001881 b->compositor->kb_repeat_rate = rate;
1882 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001883}
1884
Daniel Stone37816df2012-05-16 18:45:18 +01001885static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001886 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001887 input_handle_keyboard_enter,
1888 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001889 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001890 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001891 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001892};
1893
1894static void
Derek Foreman748c6952015-11-06 15:56:10 -06001895input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1896 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001897 struct wl_surface *surface, int32_t id,
1898 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001899{
1900 struct wayland_input *input = data;
1901 struct wayland_output *output;
1902 enum theme_location location;
1903 bool first_touch;
1904 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001905 double x, y;
1906
1907 x = wl_fixed_to_double(fixed_x);
1908 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001909
1910 first_touch = (input->touch_points == 0);
1911 input->touch_points++;
1912
1913 input->touch_focus = wl_surface_get_user_data(surface);
1914 output = input->touch_focus;
1915 if (!first_touch && !input->touch_active)
1916 return;
1917
1918 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001919 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001920
1921 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001922 x -= fx;
1923 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001924
1925 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1926 weston_output_schedule_repaint(&output->base);
1927
1928 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1929 input->touch_points--;
Armin Krezović34476192016-11-21 18:42:42 +01001930 if (output->parent.xdg_toplevel)
1931 zxdg_toplevel_v6_move(output->parent.xdg_toplevel,
1932 input->parent.seat, serial);
1933 else if (output->parent.shell_surface)
1934 wl_shell_surface_move(output->parent.shell_surface,
1935 input->parent.seat, serial);
Derek Foreman748c6952015-11-06 15:56:10 -06001936 frame_status_clear(output->frame,
1937 FRAME_STATUS_MOVE);
1938 return;
1939 }
1940
1941 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1942 return;
1943 }
1944
1945 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1946
1947 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1948 input->touch_active = true;
1949}
1950
1951static void
1952input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1953 uint32_t serial, uint32_t time, int32_t id)
1954{
1955 struct wayland_input *input = data;
1956 struct wayland_output *output = input->touch_focus;
1957 bool active = input->touch_active;
1958
1959 input->touch_points--;
1960 if (input->touch_points == 0) {
1961 input->touch_focus = NULL;
1962 input->touch_active = false;
1963 }
1964
1965 if (!output)
1966 return;
1967
1968 if (output->frame) {
1969 frame_touch_up(output->frame, input, id);
1970
1971 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1972 wayland_output_destroy(&output->base);
1973 input->touch_focus = NULL;
1974 input->keyboard_focus = NULL;
1975 if (wl_list_empty(&input->backend->compositor->output_list))
1976 weston_compositor_exit(input->backend->compositor);
1977
1978 return;
1979 }
1980 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1981 weston_output_schedule_repaint(&output->base);
1982 }
1983
1984 if (active)
1985 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1986}
1987
1988static void
1989input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001990 uint32_t time, int32_t id,
1991 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001992{
1993 struct wayland_input *input = data;
1994 struct wayland_output *output = input->touch_focus;
1995 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001996 double x, y;
1997
1998 x = wl_fixed_to_double(fixed_x);
1999 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06002000
2001 if (!output || !input->touch_active)
2002 return;
2003
2004 if (output->frame) {
2005 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002006 x -= fx;
2007 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06002008 }
2009
2010 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
2011
2012 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
2013}
2014
2015static void
2016input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
2017{
2018 struct wayland_input *input = data;
2019
2020 if (!input->touch_focus || !input->touch_active)
2021 return;
2022
2023 notify_touch_frame(&input->base);
2024}
2025
2026static void
2027input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
2028{
2029 struct wayland_input *input = data;
2030
2031 if (!input->touch_focus || !input->touch_active)
2032 return;
2033
2034 notify_touch_cancel(&input->base);
2035}
2036
2037static const struct wl_touch_listener touch_listener = {
2038 input_handle_touch_down,
2039 input_handle_touch_up,
2040 input_handle_touch_motion,
2041 input_handle_touch_frame,
2042 input_handle_touch_cancel,
2043};
2044
2045
2046static void
Daniel Stone37816df2012-05-16 18:45:18 +01002047input_handle_capabilities(void *data, struct wl_seat *seat,
2048 enum wl_seat_capability caps)
2049{
2050 struct wayland_input *input = data;
2051
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002052 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
2053 input->parent.pointer = wl_seat_get_pointer(seat);
2054 wl_pointer_set_user_data(input->parent.pointer, input);
2055 wl_pointer_add_listener(input->parent.pointer,
2056 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04002057 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002058 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002059 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
2060 wl_pointer_release(input->parent.pointer);
2061 else
2062 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002063 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002064 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002065 }
2066
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002067 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
2068 input->parent.keyboard = wl_seat_get_keyboard(seat);
2069 wl_keyboard_set_user_data(input->parent.keyboard, input);
2070 wl_keyboard_add_listener(input->parent.keyboard,
2071 &keyboard_listener, input);
2072 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06002073 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
2074 wl_keyboard_release(input->parent.keyboard);
2075 else
2076 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002077 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06002078 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002079 }
Derek Foreman748c6952015-11-06 15:56:10 -06002080
2081 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
2082 input->parent.touch = wl_seat_get_touch(seat);
2083 wl_touch_set_user_data(input->parent.touch, input);
2084 wl_touch_add_listener(input->parent.touch,
2085 &touch_listener, input);
2086 weston_seat_init_touch(&input->base);
2087 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
2088 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
2089 wl_touch_release(input->parent.touch);
2090 else
2091 wl_touch_destroy(input->parent.touch);
2092 input->parent.touch = NULL;
2093 weston_seat_release_touch(&input->base);
2094 }
Daniel Stone37816df2012-05-16 18:45:18 +01002095}
2096
Jonny Lamb497994a2014-08-12 14:58:26 +02002097static void
2098input_handle_name(void *data, struct wl_seat *seat,
2099 const char *name)
2100{
2101}
2102
Daniel Stone37816df2012-05-16 18:45:18 +01002103static const struct wl_seat_listener seat_listener = {
2104 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002105 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002106};
2107
2108static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002109display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002110{
2111 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002112 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002113
Peter Huttererf3d62272013-08-08 11:57:05 +10002114 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002115 if (input == NULL)
2116 return;
2117
Giulio Camuffo954f1832014-10-11 18:27:30 +03002118 weston_seat_init(&input->base, b->compositor, "default");
2119 input->backend = b;
2120 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002121 &wl_seat_interface, version);
2122 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002123 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002124
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002125 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2126 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002127
2128 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002129 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002130
2131 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2132 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002133}
2134
2135static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002136wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2137 int32_t x, int32_t y,
2138 int32_t physical_width, int32_t physical_height,
2139 int32_t subpixel, const char *make,
2140 const char *model, int32_t transform)
2141{
2142 struct wayland_parent_output *output = data;
2143
2144 output->x = x;
2145 output->y = y;
2146 output->physical.width = physical_width;
2147 output->physical.height = physical_height;
2148 output->physical.subpixel = subpixel;
2149
2150 free(output->physical.make);
2151 output->physical.make = strdup(make);
2152 free(output->physical.model);
2153 output->physical.model = strdup(model);
2154
2155 output->transform = transform;
2156}
2157
2158static struct weston_mode *
2159find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2160{
2161 struct weston_mode *mode;
2162
2163 wl_list_for_each(mode, list, link) {
2164 if (mode->width == width && mode->height == height &&
2165 mode->refresh == refresh)
2166 return mode;
2167 }
2168
2169 mode = zalloc(sizeof *mode);
2170 if (!mode)
2171 return NULL;
2172
2173 mode->width = width;
2174 mode->height = height;
2175 mode->refresh = refresh;
2176 wl_list_insert(list, &mode->link);
2177
2178 return mode;
2179}
2180
2181static void
2182wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2183 uint32_t flags, int32_t width, int32_t height,
2184 int32_t refresh)
2185{
2186 struct wayland_parent_output *output = data;
2187 struct weston_mode *mode;
2188
2189 if (output->output) {
2190 mode = find_mode(&output->output->base.mode_list,
2191 width, height, refresh);
2192 if (!mode)
2193 return;
2194 mode->flags = flags;
2195 /* Do a mode-switch on current mode change? */
2196 } else {
2197 mode = find_mode(&output->mode_list, width, height, refresh);
2198 if (!mode)
2199 return;
2200 mode->flags = flags;
2201 if (flags & WL_OUTPUT_MODE_CURRENT)
2202 output->current_mode = mode;
2203 if (flags & WL_OUTPUT_MODE_PREFERRED)
2204 output->preferred_mode = mode;
2205 }
2206}
2207
2208static const struct wl_output_listener output_listener = {
2209 wayland_parent_output_geometry,
2210 wayland_parent_output_mode
2211};
2212
2213static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002214wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002215{
2216 struct wayland_parent_output *output;
2217
2218 output = zalloc(sizeof *output);
2219 if (!output)
2220 return;
2221
2222 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002223 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002224 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002225 if (!output->global) {
2226 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002227 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002228 }
2229
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002230 wl_output_add_listener(output->global, &output_listener, output);
2231
2232 output->scale = 0;
2233 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2234 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2235 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002236 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002237
Giulio Camuffo954f1832014-10-11 18:27:30 +03002238 if (b->sprawl_across_outputs) {
2239 wl_display_roundtrip(b->parent.wl_display);
2240 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002241 }
2242}
2243
2244static void
2245wayland_parent_output_destroy(struct wayland_parent_output *output)
2246{
2247 struct weston_mode *mode, *next;
2248
2249 if (output->output)
2250 wayland_output_destroy(&output->output->base);
2251
2252 wl_output_destroy(output->global);
2253 free(output->physical.make);
2254 free(output->physical.model);
2255
2256 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2257 wl_list_remove(&mode->link);
2258 free(mode);
2259 }
2260}
2261
2262static void
Armin Krezović34476192016-11-21 18:42:42 +01002263xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
2264{
2265 zxdg_shell_v6_pong(shell, serial);
2266}
2267
2268static const struct zxdg_shell_v6_listener xdg_shell_listener = {
2269 xdg_shell_ping,
2270};
2271
2272static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002273registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2274 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002275{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002276 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002277
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002278 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002279 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002280 wl_registry_bind(registry, name,
2281 &wl_compositor_interface, 1);
Armin Krezović34476192016-11-21 18:42:42 +01002282 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
2283 b->parent.xdg_shell =
2284 wl_registry_bind(registry, name,
2285 &zxdg_shell_v6_interface, 1);
2286 zxdg_shell_v6_add_listener(b->parent.xdg_shell,
2287 &xdg_shell_listener, b);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002288 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002289 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002290 wl_registry_bind(registry, name,
2291 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002292 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002293 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002294 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002295 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002296 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002297 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002298 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002299 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002300 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002301 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002302 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002303 }
2304}
2305
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002306static void
2307registry_handle_global_remove(void *data, struct wl_registry *registry,
2308 uint32_t name)
2309{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002310 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002311 struct wayland_parent_output *output;
2312
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002314 if (output->id == name)
2315 wayland_parent_output_destroy(output);
2316}
2317
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002318static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002319 registry_handle_global,
2320 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002321};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002322
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002323static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002324wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002325{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002326 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002327 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002328
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002329 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002330 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002331 return 0;
2332 }
2333
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002334 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002335 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002336 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002337 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002338
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002339 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002340 count = wl_display_dispatch_pending(b->parent.wl_display);
2341 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002342 }
2343
2344 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002345}
2346
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002347static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002348wayland_restore(struct weston_compositor *ec)
2349{
2350}
2351
2352static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002353wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002354{
Armin Krezović938dc522016-08-01 19:17:57 +02002355 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002356
Armin Krezović78895c52016-10-09 17:30:28 +02002357 wl_event_source_remove(b->parent.wl_source);
2358
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002359 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002360
Giulio Camuffo954f1832014-10-11 18:27:30 +03002361 if (b->parent.shm)
2362 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002363
Armin Krezović34476192016-11-21 18:42:42 +01002364 if (b->parent.xdg_shell)
2365 zxdg_shell_v6_destroy(b->parent.xdg_shell);
2366
2367 if (b->parent.shell)
2368 wl_shell_destroy(b->parent.shell);
2369
Armin Krezović78895c52016-10-09 17:30:28 +02002370 if (b->parent.fshell)
2371 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2372
2373 if (b->parent.compositor)
2374 wl_compositor_destroy(b->parent.compositor);
2375
2376 wl_registry_destroy(b->parent.registry);
2377 wl_display_flush(b->parent.wl_display);
2378 wl_display_disconnect(b->parent.wl_display);
2379
2380 if (b->theme)
2381 theme_destroy(b->theme);
2382
2383 if (b->frame_device)
2384 cairo_device_destroy(b->frame_device);
2385
2386 wl_cursor_theme_destroy(b->cursor_theme);
2387
Giulio Camuffo954f1832014-10-11 18:27:30 +03002388 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002389}
2390
Jason Ekstrand7744f712013-10-27 22:24:55 -05002391static const char *left_ptrs[] = {
2392 "left_ptr",
2393 "default",
2394 "top_left_arrow",
2395 "left-arrow"
2396};
2397
2398static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002399create_cursor(struct wayland_backend *b,
2400 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002401{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002402 unsigned int i;
2403
Benoit Gschwind244ff792016-04-28 20:33:11 +02002404 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2405 config->cursor_size,
2406 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002407 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002408 fprintf(stderr, "could not load cursor theme\n");
2409 return;
2410 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002411
Giulio Camuffo954f1832014-10-11 18:27:30 +03002412 b->cursor = NULL;
2413 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2414 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002415 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002416 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002417 fprintf(stderr, "could not load left cursor\n");
2418 return;
2419 }
2420}
2421
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002422static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002423fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2424 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002425{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002426 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002427 struct wayland_input *input = NULL;
2428
Giulio Camuffo954f1832014-10-11 18:27:30 +03002429 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002430 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002431 break;
2432
2433 if (!input || !input->output)
2434 return;
2435
2436 if (input->output->frame)
2437 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2438 else
2439 wayland_output_set_windowed(input->output);
2440
2441 weston_output_schedule_repaint(&input->output->base);
2442}
2443
Giulio Camuffo954f1832014-10-11 18:27:30 +03002444static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002445wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002446 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002447{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002448 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002449 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002450 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002451
Giulio Camuffo954f1832014-10-11 18:27:30 +03002452 b = zalloc(sizeof *b);
2453 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002454 return NULL;
2455
Giulio Camuffo954f1832014-10-11 18:27:30 +03002456 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002457 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002458 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002459
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002460 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002461 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002462 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002463 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002464 }
2465
Giulio Camuffo954f1832014-10-11 18:27:30 +03002466 wl_list_init(&b->parent.output_list);
2467 wl_list_init(&b->input_list);
2468 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2469 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2470 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002471
Benoit Gschwind244ff792016-04-28 20:33:11 +02002472 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002473
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002474 b->use_pixman = new_config->use_pixman;
Armin Krezović174448a2016-09-30 14:11:09 +02002475 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002476
Giulio Camuffo954f1832014-10-11 18:27:30 +03002477 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002478 gl_renderer = weston_load_module("gl-renderer.so",
2479 "gl_renderer_interface");
2480 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002481 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002482 }
2483
Giulio Camuffo954f1832014-10-11 18:27:30 +03002484 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002485 if (gl_renderer->display_create(compositor,
2486 EGL_PLATFORM_WAYLAND_KHR,
2487 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002488 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002489 gl_renderer->alpha_attribs,
2490 NULL,
2491 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002492 weston_log("Failed to initialize the GL renderer; "
2493 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002494 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002495 }
2496 }
2497
Giulio Camuffo954f1832014-10-11 18:27:30 +03002498 if (b->use_pixman) {
2499 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002500 weston_log("Failed to initialize pixman renderer\n");
2501 goto err_display;
2502 }
2503 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002504
Giulio Camuffo954f1832014-10-11 18:27:30 +03002505 b->base.destroy = wayland_destroy;
2506 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002507
Giulio Camuffo954f1832014-10-11 18:27:30 +03002508 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002509
Giulio Camuffo954f1832014-10-11 18:27:30 +03002510 fd = wl_display_get_fd(b->parent.wl_display);
2511 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002512 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002513 wayland_backend_handle_event, b);
2514 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002515 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002516
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002518
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002519 if (compositor->renderer->import_dmabuf) {
2520 if (linux_dmabuf_setup(compositor) < 0)
2521 weston_log("Error: initializing dmabuf "
2522 "support failed.\n");
2523 }
2524
Giulio Camuffo954f1832014-10-11 18:27:30 +03002525 compositor->backend = &b->base;
2526 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002527err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002528 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002529err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002530 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002531 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002532 return NULL;
2533}
2534
2535static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002536wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002537{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002538 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002539
Giulio Camuffo954f1832014-10-11 18:27:30 +03002540 if (b->theme)
2541 theme_destroy(b->theme);
2542 if (b->frame_device)
2543 cairo_device_destroy(b->frame_device);
2544 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002545
Giulio Camuffo954f1832014-10-11 18:27:30 +03002546 weston_compositor_shutdown(b->compositor);
2547 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002548}
2549
Armin Krezović174448a2016-09-30 14:11:09 +02002550static const struct weston_windowed_output_api windowed_api = {
2551 wayland_output_set_size,
2552 wayland_output_create,
2553};
2554
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002555static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002556config_init_to_defaults(struct weston_wayland_backend_config *config)
2557{
2558}
2559
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002560WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002561backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002562 struct weston_backend_config *config_base)
2563{
2564 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002565 struct wayland_parent_output *poutput;
2566 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002567 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002568
Benoit Gschwinde091b452016-05-10 22:47:48 +02002569 if (config_base == NULL ||
2570 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2571 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2572 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002573 return -1;
2574 }
2575
2576 config_init_to_defaults(&new_config);
2577 memcpy(&new_config, config_base, config_base->struct_size);
2578
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002579 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002580
Giulio Camuffo954f1832014-10-11 18:27:30 +03002581 if (!b)
2582 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002583
Benoit Gschwind37a68072016-04-28 20:33:08 +02002584 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002585 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002586 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002587
Giulio Camuffo954f1832014-10-11 18:27:30 +03002588 wl_list_for_each(poutput, &b->parent.output_list, link)
2589 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002590
Giulio Camuffo954f1832014-10-11 18:27:30 +03002591 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002592 }
2593
Benoit Gschwind37a68072016-04-28 20:33:08 +02002594 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002595 if (wayland_output_create_fullscreen(b) < 0) {
2596 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002597 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002598 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002599
Giulio Camuffo954f1832014-10-11 18:27:30 +03002600 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002601 }
2602
Armin Krezović174448a2016-09-30 14:11:09 +02002603 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2604 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002605
Armin Krezović174448a2016-09-30 14:11:09 +02002606 if (ret < 0) {
2607 weston_log("Failed to register output API.\n");
2608 wayland_backend_destroy(b);
2609 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002610 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002611
Giulio Camuffo954f1832014-10-11 18:27:30 +03002612 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002613 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002615 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002616
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002617err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618 wayland_backend_destroy(b);
2619 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002620}