blob: 5b802f5d997b9ea12fe13e8b6bdce1668336ad32 [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
29#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010035#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060036#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkebe014562011-02-18 17:04:24 +010038#include <wayland-client.h>
39#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050040#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010041
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042#include "compositor.h"
Benoit Gschwind639322a2016-04-28 20:33:15 +020043#include "compositor-wayland.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010044#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050045#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070046#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070047#include "shared/image-loader.h"
48#include "shared/os-compatibility.h"
49#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080050#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020051#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020052#include "linux-dmabuf.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053
Jason Ekstrand48ce4212013-10-27 22:25:02 -050054#define WINDOW_TITLE "Weston Compositor"
55
Giulio Camuffo954f1832014-10-11 18:27:30 +030056struct wayland_backend {
57 struct weston_backend base;
58 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059
60 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040061 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040062 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063 struct wl_compositor *compositor;
64 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080065 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020066 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050068 struct wl_list output_list;
69
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010070 struct wl_event_source *wl_source;
71 uint32_t event_mask;
72 } parent;
73
Jason Ekstrandff2fd462013-10-27 22:24:58 -050074 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050075 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050076
Jason Ekstrand7744f712013-10-27 22:24:55 -050077 struct theme *theme;
78 cairo_device_t *frame_device;
79 struct wl_cursor_theme *cursor_theme;
80 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050081
Jason Ekstrand06ced802013-11-07 20:13:29 -060082 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010083};
84
85struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050086 struct weston_output base;
87
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010088 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050089 int draw_initial_frame;
90 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050091
92 struct wl_output *output;
93 uint32_t global_id;
94
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050095 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060096 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010097 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050098
Jason Ekstrand7744f712013-10-27 22:24:55 -050099 int keyboard_count;
100
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600101 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500102 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500103
Jason Ekstrand7744f712013-10-27 22:24:55 -0500104 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500105 struct wl_egl_window *egl_window;
106 struct {
107 cairo_surface_t *top;
108 cairo_surface_t *left;
109 cairo_surface_t *right;
110 cairo_surface_t *bottom;
111 } border;
112 } gl;
113
114 struct {
115 struct wl_list buffers;
116 struct wl_list free_buffers;
117 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500118
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500119 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500120 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100121};
122
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500123struct wayland_parent_output {
124 struct wayland_output *output;
125 struct wl_list link;
126
127 struct wl_output *global;
128 uint32_t id;
129
130 struct {
131 char *make;
132 char *model;
133 int32_t width, height;
134 uint32_t subpixel;
135 } physical;
136
137 int32_t x, y;
138 uint32_t transform;
139 uint32_t scale;
140
141 struct wl_list mode_list;
142 struct weston_mode *preferred_mode;
143 struct weston_mode *current_mode;
144};
145
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500146struct wayland_shm_buffer {
147 struct wayland_output *output;
148 struct wl_list link;
149 struct wl_list free_link;
150
151 struct wl_buffer *buffer;
152 void *data;
153 size_t size;
154 pixman_region32_t damage;
155 int frame_damaged;
156
157 pixman_image_t *pm_image;
158 cairo_surface_t *c_surface;
159};
160
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100161struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400162 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300163 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100164 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500165
166 struct {
167 struct wl_seat *seat;
168 struct wl_pointer *pointer;
169 struct wl_keyboard *keyboard;
170 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500171
172 struct {
173 struct wl_surface *surface;
174 int32_t hx, hy;
175 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500176 } parent;
177
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500178 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100179 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400180 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600181 uint32_t touch_points;
182 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600183 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600184 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600185
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400186 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600187 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500188 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000189
190 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100191};
192
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300193struct gl_renderer_interface *gl_renderer;
194
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500195static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500196wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
197{
198 cairo_surface_destroy(buffer->c_surface);
199 pixman_image_unref(buffer->pm_image);
200
201 wl_buffer_destroy(buffer->buffer);
202 munmap(buffer->data, buffer->size);
203
204 pixman_region32_fini(&buffer->damage);
205
206 wl_list_remove(&buffer->link);
207 wl_list_remove(&buffer->free_link);
208 free(buffer);
209}
210
211static void
212buffer_release(void *data, struct wl_buffer *buffer)
213{
214 struct wayland_shm_buffer *sb = data;
215
216 if (sb->output) {
217 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
218 } else {
219 wayland_shm_buffer_destroy(sb);
220 }
221}
222
223static const struct wl_buffer_listener buffer_listener = {
224 buffer_release
225};
226
227static struct wayland_shm_buffer *
228wayland_output_get_shm_buffer(struct wayland_output *output)
229{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300230 struct wayland_backend *b =
231 (struct wayland_backend *) output->base.compositor->backend;
232 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500233 struct wayland_shm_buffer *sb;
234
235 struct wl_shm_pool *pool;
236 int width, height, stride;
237 int32_t fx, fy;
238 int fd;
239 unsigned char *data;
240
241 if (!wl_list_empty(&output->shm.free_buffers)) {
242 sb = container_of(output->shm.free_buffers.next,
243 struct wayland_shm_buffer, free_link);
244 wl_list_remove(&sb->free_link);
245 wl_list_init(&sb->free_link);
246
247 return sb;
248 }
249
250 if (output->frame) {
251 width = frame_width(output->frame);
252 height = frame_height(output->frame);
253 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500254 width = output->base.current_mode->width;
255 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500256 }
257
258 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
259
260 fd = os_create_anonymous_file(height * stride);
261 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000262 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500263 return NULL;
264 }
265
266 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
267 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000268 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500269 close(fd);
270 return NULL;
271 }
272
273 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000274 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200275 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000276 close(fd);
277 free(data);
278 return NULL;
279 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500280
281 sb->output = output;
282 wl_list_init(&sb->free_link);
283 wl_list_insert(&output->shm.buffers, &sb->link);
284
285 pixman_region32_init_rect(&sb->damage, 0, 0,
286 output->base.width, output->base.height);
287 sb->frame_damaged = 1;
288
289 sb->data = data;
290 sb->size = height * stride;
291
292 pool = wl_shm_create_pool(shm, fd, sb->size);
293
294 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
295 width, height,
296 stride,
297 WL_SHM_FORMAT_ARGB8888);
298 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
299 wl_shm_pool_destroy(pool);
300 close(fd);
301
302 memset(data, 0, sb->size);
303
304 sb->c_surface =
305 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
306 width, height, stride);
307
308 fx = 0;
309 fy = 0;
310 if (output->frame)
311 frame_interior(output->frame, &fx, &fy, 0, 0);
312 sb->pm_image =
313 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
314 (uint32_t *)(data + fy * stride) + fx,
315 stride);
316
317 return sb;
318}
319
320static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500321frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400322{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500323 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400324 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400325
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500326 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400327
328 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300329
330 /*
331 * This is the fallback case, where Presentation extension is not
332 * available from the parent compositor. We do not know the base for
333 * 'time', so we cannot feed it to finish_frame(). Do the only thing
334 * we can, and pretend finish_frame time is when we process this
335 * event.
336 */
337 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200338 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400339}
340
341static const struct wl_callback_listener frame_listener = {
342 frame_done
343};
344
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200346draw_initial_frame(struct wayland_output *output)
347{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500348 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200349
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500350 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200351
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500352 /* If we are rendering with GL, then orphan it so that it gets
353 * destroyed immediately */
354 if (output->gl.egl_window)
355 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500356
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500357 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500358 wl_surface_damage(output->parent.surface, 0, 0,
359 output->base.current_mode->width,
360 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200361}
362
363static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500364wayland_output_update_gl_border(struct wayland_output *output)
365{
366 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
367 cairo_t *cr;
368
369 if (!output->frame)
370 return;
371 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
372 return;
373
374 fwidth = frame_width(output->frame);
375 fheight = frame_height(output->frame);
376 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
377
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500378 if (!output->gl.border.top)
379 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500380 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
381 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500382 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500383 frame_repaint(output->frame, cr);
384 cairo_destroy(cr);
385 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
386 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500387 cairo_image_surface_get_stride(output->gl.border.top) / 4,
388 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389
390
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500391 if (!output->gl.border.left)
392 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500393 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
394 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 cairo_translate(cr, 0, -iy);
397 frame_repaint(output->frame, cr);
398 cairo_destroy(cr);
399 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
400 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500401 cairo_image_surface_get_stride(output->gl.border.left) / 4,
402 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500403
404
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500405 if (!output->gl.border.right)
406 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500407 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
408 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410 cairo_translate(cr, -(iwidth + ix), -iy);
411 frame_repaint(output->frame, cr);
412 cairo_destroy(cr);
413 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
414 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500415 cairo_image_surface_get_stride(output->gl.border.right) / 4,
416 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500417
418
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500419 if (!output->gl.border.bottom)
420 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500421 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
422 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500423 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500424 cairo_translate(cr, 0, -(iy + iheight));
425 frame_repaint(output->frame, cr);
426 cairo_destroy(cr);
427 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
428 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500429 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
430 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500431}
432
433static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200434wayland_output_start_repaint_loop(struct weston_output *output_base)
435{
436 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300437 struct wayland_backend *wb =
438 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200439 struct wl_callback *callback;
440
441 /* If this is the initial frame, we need to attach a buffer so that
442 * the compositor can map the surface and include it in its render
443 * loop. If the surface doesn't end up in the render loop, the frame
444 * callback won't be invoked. The buffer is transparent and of the
445 * same size as the future real output buffer. */
446 if (output->parent.draw_initial_frame) {
447 output->parent.draw_initial_frame = 0;
448
449 draw_initial_frame(output);
450 }
451
452 callback = wl_surface_frame(output->parent.surface);
453 wl_callback_add_listener(callback, &frame_listener, output);
454 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300455 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200456}
457
David Herrmann1edf44c2013-10-22 17:11:26 +0200458static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500459wayland_output_repaint_gl(struct weston_output *output_base,
460 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400461{
462 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400463 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400464 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600465
Kristian Høgsberg33418202011-08-16 23:01:28 -0400466 callback = wl_surface_frame(output->parent.surface);
467 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100468
Jason Ekstrand7744f712013-10-27 22:24:55 -0500469 wayland_output_update_gl_border(output);
470
Pekka Paalanenbc106382012-10-10 12:49:31 +0300471 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200472
473 pixman_region32_subtract(&ec->primary_plane.damage,
474 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200475 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100476}
477
Matt Roper361d2ad2011-08-29 13:52:23 -0700478static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500479wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
480{
481 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
482 cairo_t *cr;
483
484 if (!buffer->output->frame || !buffer->frame_damaged)
485 return;
486
487 cr = cairo_create(buffer->c_surface);
488
489 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
490 fwidth = frame_width(buffer->output->frame);
491 fheight = frame_height(buffer->output->frame);
492
493 /* Set the clip so we don't unnecisaraly damage the surface */
494 cairo_move_to(cr, ix, iy);
495 cairo_rel_line_to(cr, iwidth, 0);
496 cairo_rel_line_to(cr, 0, iheight);
497 cairo_rel_line_to(cr, -iwidth, 0);
498 cairo_line_to(cr, ix, iy);
499 cairo_line_to(cr, 0, iy);
500 cairo_line_to(cr, 0, fheight);
501 cairo_line_to(cr, fwidth, fheight);
502 cairo_line_to(cr, fwidth, 0);
503 cairo_line_to(cr, 0, 0);
504 cairo_line_to(cr, 0, iy);
505 cairo_close_path(cr);
506 cairo_clip(cr);
507
508 /* Draw using a pattern so that the final result gets clipped */
509 cairo_push_group(cr);
510 frame_repaint(buffer->output->frame, cr);
511 cairo_pop_group_to_source(cr);
512 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
513 cairo_paint(cr);
514
515 cairo_destroy(cr);
516}
517
518static void
519wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
520{
521 pixman_region32_t damage;
522 pixman_box32_t *rects;
523 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
524 int i, n;
525
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500526 pixman_region32_init(&damage);
527 weston_transformed_region(sb->output->base.width,
528 sb->output->base.height,
529 sb->output->base.transform,
530 sb->output->base.current_scale,
531 &sb->damage, &damage);
532
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500533 if (sb->output->frame) {
534 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
535 fwidth = frame_width(sb->output->frame);
536 fheight = frame_height(sb->output->frame);
537
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500538 pixman_region32_translate(&damage, ix, iy);
539
540 if (sb->frame_damaged) {
541 pixman_region32_union_rect(&damage, &damage,
542 0, 0, fwidth, iy);
543 pixman_region32_union_rect(&damage, &damage,
544 0, iy, ix, iheight);
545 pixman_region32_union_rect(&damage, &damage,
546 ix + iwidth, iy,
547 fwidth - (ix + iwidth), iheight);
548 pixman_region32_union_rect(&damage, &damage,
549 0, iy + iheight,
550 fwidth, fheight - (iy + iheight));
551 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500552 }
553
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500554 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500555 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
556 for (i = 0; i < n; ++i)
557 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
558 rects[i].y1, rects[i].x2 - rects[i].x1,
559 rects[i].y2 - rects[i].y1);
560
561 if (sb->output->frame)
562 pixman_region32_fini(&damage);
563}
564
565static int
566wayland_output_repaint_pixman(struct weston_output *output_base,
567 pixman_region32_t *damage)
568{
569 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300570 struct wayland_backend *b =
571 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500572 struct wl_callback *callback;
573 struct wayland_shm_buffer *sb;
574
575 if (output->frame) {
576 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
577 wl_list_for_each(sb, &output->shm.buffers, link)
578 sb->frame_damaged = 1;
579 }
580
581 wl_list_for_each(sb, &output->shm.buffers, link)
582 pixman_region32_union(&sb->damage, &sb->damage, damage);
583
584 sb = wayland_output_get_shm_buffer(output);
585
586 wayland_output_update_shm_border(sb);
587 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300588 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500589
590 wayland_shm_buffer_attach(sb);
591
592 callback = wl_surface_frame(output->parent.surface);
593 wl_callback_add_listener(callback, &frame_listener, output);
594 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300595 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500596
597 pixman_region32_fini(&sb->damage);
598 pixman_region32_init(&sb->damage);
599 sb->frame_damaged = 0;
600
Giulio Camuffo954f1832014-10-11 18:27:30 +0300601 pixman_region32_subtract(&b->compositor->primary_plane.damage,
602 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500603 return 0;
604}
605
606static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500607wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700608{
609 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300610 struct wayland_backend *b =
611 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700612
Giulio Camuffo954f1832014-10-11 18:27:30 +0300613 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500614 pixman_renderer_output_destroy(output_base);
615 } else {
616 gl_renderer->output_destroy(output_base);
617 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100618
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500619 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500620 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500621 if (output->parent.shell_surface)
622 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500623
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600624 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500625 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600626
627 cairo_surface_destroy(output->gl.border.top);
628 cairo_surface_destroy(output->gl.border.left);
629 cairo_surface_destroy(output->gl.border.right);
630 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500631
632 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700633 free(output);
634
635 return;
636}
637
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300638static const struct wl_shell_surface_listener shell_surface_listener;
639
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200640static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500641wayland_output_init_gl_renderer(struct wayland_output *output)
642{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500643 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500644
645 if (output->frame) {
646 fwidth = frame_width(output->frame);
647 fheight = frame_height(output->frame);
648 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500649 fwidth = output->base.current_mode->width;
650 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500651 }
652
653 output->gl.egl_window =
654 wl_egl_window_create(output->parent.surface,
655 fwidth, fheight);
656 if (!output->gl.egl_window) {
657 weston_log("failure to create wl_egl_window\n");
658 return -1;
659 }
660
661 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000662 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100663 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000664 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500665 NULL,
666 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500667 goto cleanup_window;
668
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500669 return 0;
670
671cleanup_window:
672 wl_egl_window_destroy(output->gl.egl_window);
673 return -1;
674}
675
676static int
677wayland_output_init_pixman_renderer(struct wayland_output *output)
678{
679 return pixman_renderer_output_create(&output->base);
680}
681
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600682static void
683wayland_output_resize_surface(struct wayland_output *output)
684{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300685 struct wayland_backend *b =
686 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600687 struct wayland_shm_buffer *buffer, *next;
688 int32_t ix, iy, iwidth, iheight;
689 int32_t width, height;
690 struct wl_region *region;
691
692 width = output->base.current_mode->width;
693 height = output->base.current_mode->height;
694
695 if (output->frame) {
696 frame_resize_inside(output->frame, width, height);
697
698 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300699 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600700 wl_region_add(region, ix, iy, iwidth, iheight);
701 wl_surface_set_input_region(output->parent.surface, region);
702 wl_region_destroy(region);
703
704 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300705 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600706 wl_region_add(region, ix, iy, iwidth, iheight);
707 wl_surface_set_opaque_region(output->parent.surface, region);
708 wl_region_destroy(region);
709
710 width = frame_width(output->frame);
711 height = frame_height(output->frame);
712 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300713 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600714 wl_region_add(region, 0, 0, width, height);
715 wl_surface_set_input_region(output->parent.surface, region);
716 wl_region_destroy(region);
717
Giulio Camuffo954f1832014-10-11 18:27:30 +0300718 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600719 wl_region_add(region, 0, 0, width, height);
720 wl_surface_set_opaque_region(output->parent.surface, region);
721 wl_region_destroy(region);
722 }
723
724 if (output->gl.egl_window) {
725 wl_egl_window_resize(output->gl.egl_window,
726 width, height, 0, 0);
727
728 /* These will need to be re-created due to the resize */
729 gl_renderer->output_set_border(&output->base,
730 GL_RENDERER_BORDER_TOP,
731 0, 0, 0, NULL);
732 cairo_surface_destroy(output->gl.border.top);
733 output->gl.border.top = NULL;
734 gl_renderer->output_set_border(&output->base,
735 GL_RENDERER_BORDER_LEFT,
736 0, 0, 0, NULL);
737 cairo_surface_destroy(output->gl.border.left);
738 output->gl.border.left = NULL;
739 gl_renderer->output_set_border(&output->base,
740 GL_RENDERER_BORDER_RIGHT,
741 0, 0, 0, NULL);
742 cairo_surface_destroy(output->gl.border.right);
743 output->gl.border.right = NULL;
744 gl_renderer->output_set_border(&output->base,
745 GL_RENDERER_BORDER_BOTTOM,
746 0, 0, 0, NULL);
747 cairo_surface_destroy(output->gl.border.bottom);
748 output->gl.border.bottom = NULL;
749 }
750
751 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500752 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600753 wayland_shm_buffer_destroy(buffer);
754 /* These will get thrown away when they get released */
755 wl_list_for_each(buffer, &output->shm.buffers, link)
756 buffer->output = NULL;
757}
758
759static int
760wayland_output_set_windowed(struct wayland_output *output)
761{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300762 struct wayland_backend *b =
763 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600764 int tlen;
765 char *title;
766
767 if (output->frame)
768 return 0;
769
770 if (output->name) {
771 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
772 title = malloc(tlen + 1);
773 if (!title)
774 return -1;
775
776 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
777 } else {
778 title = strdup(WINDOW_TITLE);
779 }
780
Giulio Camuffo954f1832014-10-11 18:27:30 +0300781 if (!b->theme) {
782 b->theme = theme_create();
783 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600784 free(title);
785 return -1;
786 }
787 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300788 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600789 FRAME_BUTTON_CLOSE, title);
790 free(title);
791 if (!output->frame)
792 return -1;
793
794 if (output->keyboard_count)
795 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
796
797 wayland_output_resize_surface(output);
798
799 wl_shell_surface_set_toplevel(output->parent.shell_surface);
800
801 return 0;
802}
803
804static void
805wayland_output_set_fullscreen(struct wayland_output *output,
806 enum wl_shell_surface_fullscreen_method method,
807 uint32_t framerate, struct wl_output *target)
808{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300809 struct wayland_backend *b =
810 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500811
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600812 if (output->frame) {
813 frame_destroy(output->frame);
814 output->frame = NULL;
815 }
816
817 wayland_output_resize_surface(output);
818
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500819 if (output->parent.shell_surface) {
820 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
821 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300822 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800823 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
824 output->parent.surface,
825 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500826 }
827}
828
829static struct weston_mode *
830wayland_output_choose_mode(struct wayland_output *output,
831 struct weston_mode *ref_mode)
832{
833 struct weston_mode *mode;
834
835 /* First look for an exact match */
836 wl_list_for_each(mode, &output->base.mode_list, link)
837 if (mode->width == ref_mode->width &&
838 mode->height == ref_mode->height &&
839 mode->refresh == ref_mode->refresh)
840 return mode;
841
842 /* If we can't find an exact match, ignore refresh and try again */
843 wl_list_for_each(mode, &output->base.mode_list, link)
844 if (mode->width == ref_mode->width &&
845 mode->height == ref_mode->height)
846 return mode;
847
848 /* Yeah, we failed */
849 return NULL;
850}
851
852enum mode_status {
853 MODE_STATUS_UNKNOWN,
854 MODE_STATUS_SUCCESS,
855 MODE_STATUS_FAIL,
856 MODE_STATUS_CANCEL,
857};
858
859static void
860mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800861 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500862{
863 enum mode_status *value = data;
864
865 printf("Mode switch successful\n");
866
867 *value = MODE_STATUS_SUCCESS;
868}
869
870static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800871mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500872{
873 enum mode_status *value = data;
874
875 printf("Mode switch failed\n");
876
877 *value = MODE_STATUS_FAIL;
878}
879
880static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800881mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500882{
883 enum mode_status *value = data;
884
885 printf("Mode switch cancelled\n");
886
887 *value = MODE_STATUS_CANCEL;
888}
889
Jonas Ådahl496adb32015-11-17 16:00:27 +0800890struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500891 mode_feedback_successful,
892 mode_feedback_failed,
893 mode_feedback_cancelled,
894};
895
896static int
897wayland_output_switch_mode(struct weston_output *output_base,
898 struct weston_mode *mode)
899{
900 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300901 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500902 struct wl_surface *old_surface;
903 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800904 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500905 enum mode_status mode_status;
906 int ret = 0;
907
908 if (output_base == NULL) {
909 weston_log("output is NULL.\n");
910 return -1;
911 }
912
913 if (mode == NULL) {
914 weston_log("mode is NULL.\n");
915 return -1;
916 }
917
Giulio Camuffo954f1832014-10-11 18:27:30 +0300918 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500919
Giulio Camuffo954f1832014-10-11 18:27:30 +0300920 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500921 return -1;
922
923 mode = wayland_output_choose_mode(output, mode);
924 if (mode == NULL)
925 return -1;
926
927 if (output->base.current_mode == mode)
928 return 0;
929
930 old_mode = output->base.current_mode;
931 old_surface = output->parent.surface;
932 output->base.current_mode = mode;
933 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300934 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500935 wl_surface_set_user_data(output->parent.surface, output);
936
937 /* Blow the old buffers because we changed size/surfaces */
938 wayland_output_resize_surface(output);
939
940 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800941 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
942 output->parent.surface,
943 output->parent.output,
944 mode->refresh);
945 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
946 &mode_feedback_listener,
947 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500948
949 /* This should kick-start things again */
950 output->parent.draw_initial_frame = 1;
951 wayland_output_start_repaint_loop(&output->base);
952
953 mode_status = MODE_STATUS_UNKNOWN;
954 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300955 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500956
Jonas Ådahl496adb32015-11-17 16:00:27 +0800957 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500958
959 if (mode_status == MODE_STATUS_FAIL) {
960 output->base.current_mode = old_mode;
961 wl_surface_destroy(output->parent.surface);
962 output->parent.surface = old_surface;
963 wayland_output_resize_surface(output);
964
965 return -1;
966 }
967
968 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
969 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
970
Giulio Camuffo954f1832014-10-11 18:27:30 +0300971 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500972 pixman_renderer_output_destroy(output_base);
973 if (wayland_output_init_pixman_renderer(output) < 0)
974 goto err_output;
975 } else {
976 gl_renderer->output_destroy(output_base);
977 wl_egl_window_destroy(output->gl.egl_window);
978 if (wayland_output_init_gl_renderer(output) < 0)
979 goto err_output;
980 }
981 wl_surface_destroy(old_surface);
982
983 weston_output_schedule_repaint(&output->base);
984
985 return 0;
986
987err_output:
988 /* XXX */
989 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600990}
991
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500992static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300993wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600994 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500995 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100996{
997 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500998 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600999
1000 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1001 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001002
Peter Huttererf3d62272013-08-08 11:57:05 +10001003 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001004 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001005 return NULL;
1006
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007 output->name = name ? strdup(name) : NULL;
1008 output->base.make = "waywayland";
1009 output->base.model = "none";
1010
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001011 output_width = width * scale;
1012 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001013
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001014 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001015 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001016 if (!output->parent.surface)
1017 goto err_name;
1018 wl_surface_set_user_data(output->parent.surface, output);
1019
1020 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001021
Giulio Camuffo954f1832014-10-11 18:27:30 +03001022 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001023 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001024 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001025 output->parent.surface);
1026 if (!output->parent.shell_surface)
1027 goto err_surface;
1028 wl_shell_surface_add_listener(output->parent.shell_surface,
1029 &shell_surface_listener, output);
1030 }
1031
Giulio Camuffo954f1832014-10-11 18:27:30 +03001032 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001033 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1034 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001035 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001036 if (!width)
1037 output_width = output->parent.configure_width;
1038 if (!height)
1039 output_height = output->parent.configure_height;
1040 }
1041
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001042 output->mode.flags =
1043 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001044 output->mode.width = output_width;
1045 output->mode.height = output_height;
1046 output->mode.refresh = 60000;
1047 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001048 wl_list_init(&output->base.mode_list);
1049 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001050 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001051
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001052 wl_list_init(&output->shm.buffers);
1053 wl_list_init(&output->shm.free_buffers);
1054
Giulio Camuffo954f1832014-10-11 18:27:30 +03001055 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001056 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001057
Giulio Camuffo954f1832014-10-11 18:27:30 +03001058 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001059 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001060 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001061 output->base.repaint = wayland_output_repaint_pixman;
1062 } else {
1063 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001064 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001065 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001066 }
1067
Jonas Ådahle5a12252013-04-05 23:07:11 +02001068 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001069 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001070 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001071 output->base.set_backlight = NULL;
1072 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001073 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001074
Giulio Camuffo954f1832014-10-11 18:27:30 +03001075 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001076
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001077 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001078
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001079err_output:
1080 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001081 if (output->parent.shell_surface)
1082 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001083err_surface:
1084 wl_surface_destroy(output->parent.surface);
1085err_name:
1086 free(output->name);
1087
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001088 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001089 free(output);
1090
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001091 return NULL;
1092}
1093
Benoit Gschwind830b7882016-04-28 20:33:12 +02001094static struct wayland_output *
1095wayland_output_create_for_config(struct wayland_backend *b,
1096 struct weston_wayland_backend_output_config *oc,
1097 int fullscreen, int32_t x, int32_t y)
1098{
1099 struct wayland_output *output;
1100
1101 output = wayland_output_create(b, x, y, oc->width, oc->height, oc->name,
1102 fullscreen, oc->transform, oc->scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001103
1104 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001105}
1106
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001107static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001108wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001109 struct wayland_parent_output *poutput)
1110{
1111 struct wayland_output *output;
1112 struct weston_mode *mode;
1113 int32_t x;
1114
1115 if (poutput->current_mode) {
1116 mode = poutput->current_mode;
1117 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001118 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001119 } else if (!wl_list_empty(&poutput->mode_list)) {
1120 mode = container_of(poutput->mode_list.next,
1121 struct weston_mode, link);
1122 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001123 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001124 return NULL;
1125 }
1126
Giulio Camuffo954f1832014-10-11 18:27:30 +03001127 if (!wl_list_empty(&b->compositor->output_list)) {
1128 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001129 struct wayland_output, base.link);
1130 x = output->base.x + output->base.current_mode->width;
1131 } else {
1132 x = 0;
1133 }
1134
Giulio Camuffo954f1832014-10-11 18:27:30 +03001135 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001136 NULL, 0,
1137 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1138 if (!output)
1139 return NULL;
1140
1141 output->parent.output = poutput->global;
1142
1143 output->base.make = poutput->physical.make;
1144 output->base.model = poutput->physical.model;
1145 wl_list_init(&output->base.mode_list);
1146 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1147 wl_list_init(&poutput->mode_list);
1148
1149 wayland_output_set_fullscreen(output,
1150 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1151 mode->refresh, poutput->global);
1152
1153 if (output->parent.shell_surface) {
1154 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1155 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1156 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001157 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001158 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1159 output->parent.surface,
1160 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1161 poutput->global);
1162 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1163 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1164 output->parent.surface,
1165 poutput->global,
1166 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001167 }
1168
1169 return output;
1170}
1171
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001172static void
1173shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1174 uint32_t serial)
1175{
1176 wl_shell_surface_pong(shell_surface, serial);
1177}
1178
1179static void
1180shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1181 uint32_t edges, int32_t width, int32_t height)
1182{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001183 struct wayland_output *output = data;
1184
1185 output->parent.configure_width = width;
1186 output->parent.configure_height = height;
1187
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001188 /* FIXME: implement resizing */
1189}
1190
1191static void
1192shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1193{
1194}
1195
1196static const struct wl_shell_surface_listener shell_surface_listener = {
1197 shell_surface_ping,
1198 shell_surface_configure,
1199 shell_surface_popup_done
1200};
1201
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001202/* Events received from the wayland-server this compositor is client of: */
1203
Jason Ekstrand7744f712013-10-27 22:24:55 -05001204/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001205static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001206input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001207{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001208
Jason Ekstrand7744f712013-10-27 22:24:55 -05001209 struct wl_buffer *buffer;
1210 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001211
Giulio Camuffo954f1832014-10-11 18:27:30 +03001212 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001213 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001214
Giulio Camuffo954f1832014-10-11 18:27:30 +03001215 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001216 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001217 if (!buffer)
1218 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001219
1220 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1221 input->parent.cursor.surface,
1222 image->hotspot_x, image->hotspot_y);
1223
1224 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1225 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1226 image->width, image->height);
1227 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001228}
1229
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001230static void
Daniel Stone37816df2012-05-16 18:45:18 +01001231input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1232 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001233 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001234{
1235 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001236 int32_t fx, fy;
1237 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001238 double x, y;
1239
1240 x = wl_fixed_to_double(fixed_x);
1241 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001242
Daniel Stone50692802012-06-22 13:21:41 +01001243 /* XXX: If we get a modifier event immediately before the focus,
1244 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001245 input->enter_serial = serial;
1246 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247
1248 if (input->output->frame) {
1249 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001250 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001251 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001252 x -= fx;
1253 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001254
1255 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1256 weston_output_schedule_repaint(&input->output->base);
1257 } else {
1258 location = THEME_LOCATION_CLIENT_AREA;
1259 }
1260
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001261 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001262
1263 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001264 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001265 notify_pointer_focus(&input->base, &input->output->base, x, y);
1266 wl_pointer_set_cursor(input->parent.pointer,
1267 input->enter_serial, NULL, 0, 0);
1268 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001269 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001270 notify_pointer_focus(&input->base, NULL, 0, 0);
1271 input_set_cursor(input);
1272 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001273}
1274
1275static void
Daniel Stone37816df2012-05-16 18:45:18 +01001276input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1277 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001278{
1279 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001280
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001281 if (!input->output)
1282 return;
1283
Jason Ekstrand7744f712013-10-27 22:24:55 -05001284 if (input->output->frame) {
1285 frame_pointer_leave(input->output->frame, input);
1286
1287 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1288 weston_output_schedule_repaint(&input->output->base);
1289 }
1290
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001291 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001292 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001293 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001294}
1295
1296static void
Daniel Stone37816df2012-05-16 18:45:18 +01001297input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001298 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001299{
1300 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001301 int32_t fx, fy;
1302 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001303 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001304 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001305
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001306 if (!input->output)
1307 return;
1308
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001309 x = wl_fixed_to_double(fixed_x);
1310 y = wl_fixed_to_double(fixed_y);
1311
Jason Ekstrand7744f712013-10-27 22:24:55 -05001312 if (input->output->frame) {
1313 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001314 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001315 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001316 x -= fx;
1317 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001318
1319 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1320 weston_output_schedule_repaint(&input->output->base);
1321 } else {
1322 location = THEME_LOCATION_CLIENT_AREA;
1323 }
1324
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001325 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001326
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001327 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001328 input_set_cursor(input);
1329 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001330 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001331 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001332 } else if (!input->has_focus &&
1333 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001334 wl_pointer_set_cursor(input->parent.pointer,
1335 input->enter_serial, NULL, 0, 0);
1336 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001337 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001338 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001339 }
1340
Peter Hutterer87743e92016-01-18 16:38:22 +10001341 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001342 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001343 want_frame = true;
1344 }
1345
1346 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1347 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001348}
1349
1350static void
1351input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001352 uint32_t serial, uint32_t time, uint32_t button,
1353 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001354{
1355 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001356 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001357 enum frame_button_state fstate;
1358 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001359
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001360 if (!input->output)
1361 return;
1362
Jason Ekstrand7744f712013-10-27 22:24:55 -05001363 if (input->output->frame) {
1364 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1365 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1366
1367 location = frame_pointer_button(input->output->frame, input,
1368 button, fstate);
1369
1370 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1371
1372 wl_shell_surface_move(input->output->parent.shell_surface,
1373 input->parent.seat, serial);
1374 frame_status_clear(input->output->frame,
1375 FRAME_STATUS_MOVE);
1376 return;
1377 }
1378
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001379 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1380 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001381 input->output = NULL;
1382 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001383
Giulio Camuffo954f1832014-10-11 18:27:30 +03001384 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001385 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001386
1387 return;
1388 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001389
1390 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1391 weston_output_schedule_repaint(&input->output->base);
1392 } else {
1393 location = THEME_LOCATION_CLIENT_AREA;
1394 }
1395
Peter Hutterer87743e92016-01-18 16:38:22 +10001396 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001397 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001398 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1399 notify_pointer_frame(&input->base);
1400 }
Daniel Stone37816df2012-05-16 18:45:18 +01001401}
1402
1403static void
1404input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001405 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001406{
1407 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001408 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001409
Peter Hutterer89b6a492016-01-18 15:58:17 +10001410 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001411 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001412
Peter Hutterer87743e92016-01-18 16:38:22 +10001413 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1414 input->vert.has_discrete) {
1415 weston_event.has_discrete = true;
1416 weston_event.discrete = input->vert.discrete;
1417 input->vert.has_discrete = false;
1418 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1419 input->horiz.has_discrete) {
1420 weston_event.has_discrete = true;
1421 weston_event.discrete = input->horiz.discrete;
1422 input->horiz.has_discrete = false;
1423 }
1424
Peter Hutterer89b6a492016-01-18 15:58:17 +10001425 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001426
1427 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1428 notify_pointer_frame(&input->base);
1429}
1430
1431static void
1432input_handle_frame(void *data, struct wl_pointer *pointer)
1433{
1434 struct wayland_input *input = data;
1435
1436 notify_pointer_frame(&input->base);
1437}
1438
1439static void
1440input_handle_axis_source(void *data, struct wl_pointer *pointer,
1441 uint32_t source)
1442{
1443 struct wayland_input *input = data;
1444
1445 notify_axis_source(&input->base, source);
1446}
1447
1448static void
1449input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1450 uint32_t time, uint32_t axis)
1451{
1452 struct wayland_input *input = data;
1453 struct weston_pointer_axis_event weston_event;
1454
1455 weston_event.axis = axis;
1456 weston_event.value = 0;
1457
1458 notify_axis(&input->base, time, &weston_event);
1459}
1460
1461static void
1462input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1463 uint32_t axis, int32_t discrete)
1464{
1465 struct wayland_input *input = data;
1466
1467 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1468 input->vert.has_discrete = true;
1469 input->vert.discrete = discrete;
1470 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1471 input->horiz.has_discrete = true;
1472 input->horiz.discrete = discrete;
1473 }
Daniel Stone37816df2012-05-16 18:45:18 +01001474}
1475
1476static const struct wl_pointer_listener pointer_listener = {
1477 input_handle_pointer_enter,
1478 input_handle_pointer_leave,
1479 input_handle_motion,
1480 input_handle_button,
1481 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001482 input_handle_frame,
1483 input_handle_axis_source,
1484 input_handle_axis_stop,
1485 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001486};
1487
1488static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001489input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1490 int fd, uint32_t size)
1491{
1492 struct wayland_input *input = data;
1493 struct xkb_keymap *keymap;
1494 char *map_str;
1495
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001496 if (!data) {
1497 close(fd);
1498 return;
1499 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001500
1501 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1502 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1503 if (map_str == MAP_FAILED) {
1504 weston_log("mmap failed: %m\n");
1505 goto error;
1506 }
1507
Giulio Camuffo954f1832014-10-11 18:27:30 +03001508 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001509 map_str,
1510 XKB_KEYMAP_FORMAT_TEXT_V1,
1511 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001512 munmap(map_str, size);
1513
1514 if (!keymap) {
1515 weston_log("failed to compile keymap\n");
1516 goto error;
1517 }
1518
1519 input->keyboard_state_update = STATE_UPDATE_NONE;
1520 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1521 weston_log("No keymap provided; falling back to defalt\n");
1522 keymap = NULL;
1523 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1524 } else {
1525 weston_log("Invalid keymap\n");
1526 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001527 }
1528
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001529 close(fd);
1530
Derek Foreman1281a362015-07-31 16:55:32 -05001531 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001532 weston_seat_update_keymap(&input->base, keymap);
1533 else
1534 weston_seat_init_keyboard(&input->base, keymap);
1535
Ran Benitac9c74152014-08-19 23:59:52 +03001536 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001537
1538 return;
1539
1540error:
1541 wl_keyboard_release(input->parent.keyboard);
1542 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001543}
1544
1545static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001546input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001547 struct wl_keyboard *keyboard,
1548 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001549 struct wl_surface *surface,
1550 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001551{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001552 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001553 struct wayland_output *focus;
1554
1555 focus = input->keyboard_focus;
1556 if (focus) {
1557 /* This shouldn't happen */
1558 focus->keyboard_count--;
1559 if (!focus->keyboard_count && focus->frame)
1560 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1561 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1562 weston_output_schedule_repaint(&focus->base);
1563 }
1564
1565 input->keyboard_focus = wl_surface_get_user_data(surface);
1566 input->keyboard_focus->keyboard_count++;
1567
1568 focus = input->keyboard_focus;
1569 if (focus->frame) {
1570 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1571 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1572 weston_output_schedule_repaint(&focus->base);
1573 }
1574
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001575
Daniel Stone50692802012-06-22 13:21:41 +01001576 /* XXX: If we get a modifier event immediately before the focus,
1577 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001578 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001579 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001580}
1581
1582static void
1583input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001584 struct wl_keyboard *keyboard,
1585 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001586 struct wl_surface *surface)
1587{
1588 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001589 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001590
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001591 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001592
1593 focus = input->keyboard_focus;
1594 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001595 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001596
1597 focus->keyboard_count--;
1598 if (!focus->keyboard_count && focus->frame) {
1599 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1600 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1601 weston_output_schedule_repaint(&focus->base);
1602 }
1603
1604 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001605}
1606
Daniel Stone37816df2012-05-16 18:45:18 +01001607static void
1608input_handle_key(void *data, struct wl_keyboard *keyboard,
1609 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1610{
1611 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001612
Daniel Stone50692802012-06-22 13:21:41 +01001613 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001614 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001615 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001616 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001617 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001618}
1619
Daniel Stone351eb612012-05-31 15:27:47 -04001620static void
Derek Foreman1281a362015-07-31 16:55:32 -05001621input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001622 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001623 uint32_t mods_latched, uint32_t mods_locked,
1624 uint32_t group)
1625{
Derek Foreman1281a362015-07-31 16:55:32 -05001626 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001627 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001629 uint32_t serial_out;
1630
1631 /* If we get a key event followed by a modifier event with the
1632 * same serial number, then we try to preserve those semantics by
1633 * reusing the same serial number on the way out too. */
1634 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001635 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001636 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001637 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001638
Derek Foreman1281a362015-07-31 16:55:32 -05001639 keyboard = weston_seat_get_keyboard(&input->base);
1640 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001641 mods_depressed, mods_latched,
1642 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001643 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001644}
1645
Jonny Lamb497994a2014-08-12 14:58:26 +02001646static void
1647input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1648 int32_t rate, int32_t delay)
1649{
1650 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001651 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001652
Giulio Camuffo954f1832014-10-11 18:27:30 +03001653 b->compositor->kb_repeat_rate = rate;
1654 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001655}
1656
Daniel Stone37816df2012-05-16 18:45:18 +01001657static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001658 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001659 input_handle_keyboard_enter,
1660 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001661 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001662 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001663 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001664};
1665
1666static void
Derek Foreman748c6952015-11-06 15:56:10 -06001667input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1668 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001669 struct wl_surface *surface, int32_t id,
1670 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001671{
1672 struct wayland_input *input = data;
1673 struct wayland_output *output;
1674 enum theme_location location;
1675 bool first_touch;
1676 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001677 double x, y;
1678
1679 x = wl_fixed_to_double(fixed_x);
1680 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001681
1682 first_touch = (input->touch_points == 0);
1683 input->touch_points++;
1684
1685 input->touch_focus = wl_surface_get_user_data(surface);
1686 output = input->touch_focus;
1687 if (!first_touch && !input->touch_active)
1688 return;
1689
1690 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001691 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001692
1693 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001694 x -= fx;
1695 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001696
1697 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1698 weston_output_schedule_repaint(&output->base);
1699
1700 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1701 input->touch_points--;
1702 wl_shell_surface_move(output->parent.shell_surface,
1703 input->parent.seat, serial);
1704 frame_status_clear(output->frame,
1705 FRAME_STATUS_MOVE);
1706 return;
1707 }
1708
1709 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1710 return;
1711 }
1712
1713 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1714
1715 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1716 input->touch_active = true;
1717}
1718
1719static void
1720input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1721 uint32_t serial, uint32_t time, int32_t id)
1722{
1723 struct wayland_input *input = data;
1724 struct wayland_output *output = input->touch_focus;
1725 bool active = input->touch_active;
1726
1727 input->touch_points--;
1728 if (input->touch_points == 0) {
1729 input->touch_focus = NULL;
1730 input->touch_active = false;
1731 }
1732
1733 if (!output)
1734 return;
1735
1736 if (output->frame) {
1737 frame_touch_up(output->frame, input, id);
1738
1739 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1740 wayland_output_destroy(&output->base);
1741 input->touch_focus = NULL;
1742 input->keyboard_focus = NULL;
1743 if (wl_list_empty(&input->backend->compositor->output_list))
1744 weston_compositor_exit(input->backend->compositor);
1745
1746 return;
1747 }
1748 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1749 weston_output_schedule_repaint(&output->base);
1750 }
1751
1752 if (active)
1753 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1754}
1755
1756static void
1757input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001758 uint32_t time, int32_t id,
1759 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001760{
1761 struct wayland_input *input = data;
1762 struct wayland_output *output = input->touch_focus;
1763 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001764 double x, y;
1765
1766 x = wl_fixed_to_double(fixed_x);
1767 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001768
1769 if (!output || !input->touch_active)
1770 return;
1771
1772 if (output->frame) {
1773 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001774 x -= fx;
1775 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001776 }
1777
1778 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1779
1780 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1781}
1782
1783static void
1784input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1785{
1786 struct wayland_input *input = data;
1787
1788 if (!input->touch_focus || !input->touch_active)
1789 return;
1790
1791 notify_touch_frame(&input->base);
1792}
1793
1794static void
1795input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1796{
1797 struct wayland_input *input = data;
1798
1799 if (!input->touch_focus || !input->touch_active)
1800 return;
1801
1802 notify_touch_cancel(&input->base);
1803}
1804
1805static const struct wl_touch_listener touch_listener = {
1806 input_handle_touch_down,
1807 input_handle_touch_up,
1808 input_handle_touch_motion,
1809 input_handle_touch_frame,
1810 input_handle_touch_cancel,
1811};
1812
1813
1814static void
Daniel Stone37816df2012-05-16 18:45:18 +01001815input_handle_capabilities(void *data, struct wl_seat *seat,
1816 enum wl_seat_capability caps)
1817{
1818 struct wayland_input *input = data;
1819
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001820 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1821 input->parent.pointer = wl_seat_get_pointer(seat);
1822 wl_pointer_set_user_data(input->parent.pointer, input);
1823 wl_pointer_add_listener(input->parent.pointer,
1824 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001825 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001826 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001827 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1828 wl_pointer_release(input->parent.pointer);
1829 else
1830 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001831 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001832 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001833 }
1834
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001835 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1836 input->parent.keyboard = wl_seat_get_keyboard(seat);
1837 wl_keyboard_set_user_data(input->parent.keyboard, input);
1838 wl_keyboard_add_listener(input->parent.keyboard,
1839 &keyboard_listener, input);
1840 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001841 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1842 wl_keyboard_release(input->parent.keyboard);
1843 else
1844 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001845 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001846 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001847 }
Derek Foreman748c6952015-11-06 15:56:10 -06001848
1849 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1850 input->parent.touch = wl_seat_get_touch(seat);
1851 wl_touch_set_user_data(input->parent.touch, input);
1852 wl_touch_add_listener(input->parent.touch,
1853 &touch_listener, input);
1854 weston_seat_init_touch(&input->base);
1855 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1856 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1857 wl_touch_release(input->parent.touch);
1858 else
1859 wl_touch_destroy(input->parent.touch);
1860 input->parent.touch = NULL;
1861 weston_seat_release_touch(&input->base);
1862 }
Daniel Stone37816df2012-05-16 18:45:18 +01001863}
1864
Jonny Lamb497994a2014-08-12 14:58:26 +02001865static void
1866input_handle_name(void *data, struct wl_seat *seat,
1867 const char *name)
1868{
1869}
1870
Daniel Stone37816df2012-05-16 18:45:18 +01001871static const struct wl_seat_listener seat_listener = {
1872 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001873 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001874};
1875
1876static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001877display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001878{
1879 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001880 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001881
Peter Huttererf3d62272013-08-08 11:57:05 +10001882 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001883 if (input == NULL)
1884 return;
1885
Giulio Camuffo954f1832014-10-11 18:27:30 +03001886 weston_seat_init(&input->base, b->compositor, "default");
1887 input->backend = b;
1888 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001889 &wl_seat_interface, version);
1890 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001891 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001892
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001893 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1894 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001895
1896 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001897 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001898
1899 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1900 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001901}
1902
1903static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001904wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1905 int32_t x, int32_t y,
1906 int32_t physical_width, int32_t physical_height,
1907 int32_t subpixel, const char *make,
1908 const char *model, int32_t transform)
1909{
1910 struct wayland_parent_output *output = data;
1911
1912 output->x = x;
1913 output->y = y;
1914 output->physical.width = physical_width;
1915 output->physical.height = physical_height;
1916 output->physical.subpixel = subpixel;
1917
1918 free(output->physical.make);
1919 output->physical.make = strdup(make);
1920 free(output->physical.model);
1921 output->physical.model = strdup(model);
1922
1923 output->transform = transform;
1924}
1925
1926static struct weston_mode *
1927find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1928{
1929 struct weston_mode *mode;
1930
1931 wl_list_for_each(mode, list, link) {
1932 if (mode->width == width && mode->height == height &&
1933 mode->refresh == refresh)
1934 return mode;
1935 }
1936
1937 mode = zalloc(sizeof *mode);
1938 if (!mode)
1939 return NULL;
1940
1941 mode->width = width;
1942 mode->height = height;
1943 mode->refresh = refresh;
1944 wl_list_insert(list, &mode->link);
1945
1946 return mode;
1947}
1948
1949static void
1950wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1951 uint32_t flags, int32_t width, int32_t height,
1952 int32_t refresh)
1953{
1954 struct wayland_parent_output *output = data;
1955 struct weston_mode *mode;
1956
1957 if (output->output) {
1958 mode = find_mode(&output->output->base.mode_list,
1959 width, height, refresh);
1960 if (!mode)
1961 return;
1962 mode->flags = flags;
1963 /* Do a mode-switch on current mode change? */
1964 } else {
1965 mode = find_mode(&output->mode_list, width, height, refresh);
1966 if (!mode)
1967 return;
1968 mode->flags = flags;
1969 if (flags & WL_OUTPUT_MODE_CURRENT)
1970 output->current_mode = mode;
1971 if (flags & WL_OUTPUT_MODE_PREFERRED)
1972 output->preferred_mode = mode;
1973 }
1974}
1975
1976static const struct wl_output_listener output_listener = {
1977 wayland_parent_output_geometry,
1978 wayland_parent_output_mode
1979};
1980
1981static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001982wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001983{
1984 struct wayland_parent_output *output;
1985
1986 output = zalloc(sizeof *output);
1987 if (!output)
1988 return;
1989
1990 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001991 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001992 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001993 if (!output->global) {
1994 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001995 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001996 }
1997
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001998 wl_output_add_listener(output->global, &output_listener, output);
1999
2000 output->scale = 0;
2001 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2002 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2003 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002004 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002005
Giulio Camuffo954f1832014-10-11 18:27:30 +03002006 if (b->sprawl_across_outputs) {
2007 wl_display_roundtrip(b->parent.wl_display);
2008 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002009 }
2010}
2011
2012static void
2013wayland_parent_output_destroy(struct wayland_parent_output *output)
2014{
2015 struct weston_mode *mode, *next;
2016
2017 if (output->output)
2018 wayland_output_destroy(&output->output->base);
2019
2020 wl_output_destroy(output->global);
2021 free(output->physical.make);
2022 free(output->physical.model);
2023
2024 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2025 wl_list_remove(&mode->link);
2026 free(mode);
2027 }
2028}
2029
2030static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002031registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2032 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002033{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002034 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002035
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002036 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002037 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002038 wl_registry_bind(registry, name,
2039 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002040 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002041 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002042 wl_registry_bind(registry, name,
2043 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002044 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002046 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002047 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002048 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002049 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002050 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002051 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002052 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002053 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002054 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002055 }
2056}
2057
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002058static void
2059registry_handle_global_remove(void *data, struct wl_registry *registry,
2060 uint32_t name)
2061{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002062 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002063 struct wayland_parent_output *output;
2064
Giulio Camuffo954f1832014-10-11 18:27:30 +03002065 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002066 if (output->id == name)
2067 wayland_parent_output_destroy(output);
2068}
2069
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002070static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002071 registry_handle_global,
2072 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002073};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002074
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002075static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002076wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002077{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002078 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002079 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002080
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002081 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002082 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002083 return 0;
2084 }
2085
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002086 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002087 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002088 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002089 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002090
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002091 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002092 count = wl_display_dispatch_pending(b->parent.wl_display);
2093 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002094 }
2095
2096 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002097}
2098
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002099static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002100wayland_restore(struct weston_compositor *ec)
2101{
2102}
2103
2104static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002105wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002106{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002107 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002108
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002109 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002110
Giulio Camuffo954f1832014-10-11 18:27:30 +03002111 if (b->parent.shm)
2112 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002113
Giulio Camuffo954f1832014-10-11 18:27:30 +03002114 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002115}
2116
Jason Ekstrand7744f712013-10-27 22:24:55 -05002117static const char *left_ptrs[] = {
2118 "left_ptr",
2119 "default",
2120 "top_left_arrow",
2121 "left-arrow"
2122};
2123
2124static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002125create_cursor(struct wayland_backend *b,
2126 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002127{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002128 unsigned int i;
2129
Benoit Gschwind244ff792016-04-28 20:33:11 +02002130 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2131 config->cursor_size,
2132 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002133 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002134 fprintf(stderr, "could not load cursor theme\n");
2135 return;
2136 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002137
Giulio Camuffo954f1832014-10-11 18:27:30 +03002138 b->cursor = NULL;
2139 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2140 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002141 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002142 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002143 fprintf(stderr, "could not load left cursor\n");
2144 return;
2145 }
2146}
2147
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002148static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002149fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2150 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002151{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002152 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002153 struct wayland_input *input = NULL;
2154
Giulio Camuffo954f1832014-10-11 18:27:30 +03002155 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002156 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002157 break;
2158
2159 if (!input || !input->output)
2160 return;
2161
2162 if (input->output->frame)
2163 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2164 else
2165 wayland_output_set_windowed(input->output);
2166
2167 weston_output_schedule_repaint(&input->output->base);
2168}
2169
Giulio Camuffo954f1832014-10-11 18:27:30 +03002170static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002171wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002172 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002173{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002174 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002175 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002176 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002177
Giulio Camuffo954f1832014-10-11 18:27:30 +03002178 b = zalloc(sizeof *b);
2179 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002180 return NULL;
2181
Giulio Camuffo954f1832014-10-11 18:27:30 +03002182 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002183 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002184 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002185
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002186 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002187 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002188 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002189 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002190 }
2191
Giulio Camuffo954f1832014-10-11 18:27:30 +03002192 wl_list_init(&b->parent.output_list);
2193 wl_list_init(&b->input_list);
2194 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2195 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2196 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002197
Benoit Gschwind244ff792016-04-28 20:33:11 +02002198 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002199
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002200 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002201
Giulio Camuffo954f1832014-10-11 18:27:30 +03002202 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002203 gl_renderer = weston_load_module("gl-renderer.so",
2204 "gl_renderer_interface");
2205 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002206 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002207 }
2208
Giulio Camuffo954f1832014-10-11 18:27:30 +03002209 if (!b->use_pixman) {
2210 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002211 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002212 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002213 gl_renderer->alpha_attribs,
2214 NULL,
2215 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002216 weston_log("Failed to initialize the GL renderer; "
2217 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002218 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002219 }
2220 }
2221
Giulio Camuffo954f1832014-10-11 18:27:30 +03002222 if (b->use_pixman) {
2223 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002224 weston_log("Failed to initialize pixman renderer\n");
2225 goto err_display;
2226 }
2227 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002228
Giulio Camuffo954f1832014-10-11 18:27:30 +03002229 b->base.destroy = wayland_destroy;
2230 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002231
Giulio Camuffo954f1832014-10-11 18:27:30 +03002232 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002233
Giulio Camuffo954f1832014-10-11 18:27:30 +03002234 fd = wl_display_get_fd(b->parent.wl_display);
2235 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002236 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237 wayland_backend_handle_event, b);
2238 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002239 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002240
Giulio Camuffo954f1832014-10-11 18:27:30 +03002241 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002242
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002243 if (compositor->renderer->import_dmabuf) {
2244 if (linux_dmabuf_setup(compositor) < 0)
2245 weston_log("Error: initializing dmabuf "
2246 "support failed.\n");
2247 }
2248
Giulio Camuffo954f1832014-10-11 18:27:30 +03002249 compositor->backend = &b->base;
2250 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002251err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002252 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002253err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002254 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002255 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002256 return NULL;
2257}
2258
2259static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002260wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002261{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002262 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002263
Giulio Camuffo954f1832014-10-11 18:27:30 +03002264 if (b->theme)
2265 theme_destroy(b->theme);
2266 if (b->frame_device)
2267 cairo_device_destroy(b->frame_device);
2268 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002269
Giulio Camuffo954f1832014-10-11 18:27:30 +03002270 weston_compositor_shutdown(b->compositor);
2271 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002272}
2273
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002274static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002275config_init_to_defaults(struct weston_wayland_backend_config *config)
2276{
2277}
2278
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002279WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002280backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002281 struct weston_backend_config *config_base)
2282{
2283 struct wayland_backend *b;
2284 struct wayland_output *output;
2285 struct wayland_parent_output *poutput;
2286 struct weston_wayland_backend_config new_config;
2287 int x, count;
2288
Benoit Gschwinde091b452016-05-10 22:47:48 +02002289 if (config_base == NULL ||
2290 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2291 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2292 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002293 return -1;
2294 }
2295
2296 config_init_to_defaults(&new_config);
2297 memcpy(&new_config, config_base, config_base->struct_size);
2298
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002299 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002300
Giulio Camuffo954f1832014-10-11 18:27:30 +03002301 if (!b)
2302 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002303
Benoit Gschwind37a68072016-04-28 20:33:08 +02002304 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002305 b->sprawl_across_outputs = 1;
2306 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002307
Giulio Camuffo954f1832014-10-11 18:27:30 +03002308 wl_list_for_each(poutput, &b->parent.output_list, link)
2309 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002310
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002312 }
2313
Benoit Gschwind37a68072016-04-28 20:33:08 +02002314 if (new_config.fullscreen) {
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002315 if (new_config.num_outputs != 1 || !new_config.outputs)
2316 goto err_outputs;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002317
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002318 output = wayland_output_create_for_config(b,
2319 &new_config.outputs[0],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002320 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002321 if (!output)
2322 goto err_outputs;
2323
Axel Davydd8b88d2013-11-17 21:34:16 +01002324 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002325 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002326 }
2327
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002328 x = 0;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002329 for (count = 0; count < new_config.num_outputs; ++count) {
2330 output = wayland_output_create_for_config(b, &new_config.outputs[count],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002331 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002332 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002333 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002334 if (wayland_output_set_windowed(output))
2335 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002336
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002337 x += output->base.width;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002338 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002339
Giulio Camuffo954f1832014-10-11 18:27:30 +03002340 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002341 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002343 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002344
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002345err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002346 wayland_backend_destroy(b);
2347 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002348}