blob: d0d10823490ddd5be3d96b56198240dfbec08bdd [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 void
1095wayland_output_init_from_config(struct weston_wayland_backend_output_config *output,
1096 struct weston_config_section *config_section,
1097 int option_width, int option_height,
1098 int option_scale)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001099{
Benoit Gschwind830b7882016-04-28 20:33:12 +02001100 char *mode, *t, *str;
Derek Foreman64a3df02014-10-23 12:24:18 -05001101 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001102
Benoit Gschwind830b7882016-04-28 20:33:12 +02001103 weston_config_section_get_string(config_section, "name", &output->name,
1104 NULL);
1105 if (output->name) {
1106 slen = strlen(output->name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001107 slen += strlen(WINDOW_TITLE " - ");
1108 str = malloc(slen + 1);
1109 if (str)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001110 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1111 output->name);
1112 free(output->name);
1113 output->name = str;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001114 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02001115 if (!output->name)
1116 output->name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001117
1118 weston_config_section_get_string(config_section,
1119 "mode", &mode, "1024x600");
Benoit Gschwind830b7882016-04-28 20:33:12 +02001120 if (sscanf(mode, "%dx%d", &output->width, &output->height) != 2) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001121 weston_log("Invalid mode \"%s\" for output %s\n",
Benoit Gschwind830b7882016-04-28 20:33:12 +02001122 mode, output->name);
1123 output->width = 1024;
1124 output->height = 640;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001125 }
1126 free(mode);
1127
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001128 if (option_width)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001129 output->width = option_width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001130 if (option_height)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001131 output->height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001132
Benoit Gschwind830b7882016-04-28 20:33:12 +02001133 weston_config_section_get_int(config_section, "scale", &output->scale, 1);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001134
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001135 if (option_scale)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001136 output->scale = option_scale;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001137
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001138 weston_config_section_get_string(config_section,
1139 "transform", &t, "normal");
Benoit Gschwind830b7882016-04-28 20:33:12 +02001140 if (weston_parse_transform(t, &output->transform) < 0)
Derek Foreman64a3df02014-10-23 12:24:18 -05001141 weston_log("Invalid transform \"%s\" for output %s\n",
Benoit Gschwind830b7882016-04-28 20:33:12 +02001142 t, output->name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001143 free(t);
1144
Benoit Gschwind830b7882016-04-28 20:33:12 +02001145}
1146
1147static struct wayland_output *
1148wayland_output_create_for_config(struct wayland_backend *b,
1149 struct weston_wayland_backend_output_config *oc,
1150 int fullscreen, int32_t x, int32_t y)
1151{
1152 struct wayland_output *output;
1153
1154 output = wayland_output_create(b, x, y, oc->width, oc->height, oc->name,
1155 fullscreen, oc->transform, oc->scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001156
1157 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001158}
1159
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001160static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001161wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001162 struct wayland_parent_output *poutput)
1163{
1164 struct wayland_output *output;
1165 struct weston_mode *mode;
1166 int32_t x;
1167
1168 if (poutput->current_mode) {
1169 mode = poutput->current_mode;
1170 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001171 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001172 } else if (!wl_list_empty(&poutput->mode_list)) {
1173 mode = container_of(poutput->mode_list.next,
1174 struct weston_mode, link);
1175 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001176 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001177 return NULL;
1178 }
1179
Giulio Camuffo954f1832014-10-11 18:27:30 +03001180 if (!wl_list_empty(&b->compositor->output_list)) {
1181 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001182 struct wayland_output, base.link);
1183 x = output->base.x + output->base.current_mode->width;
1184 } else {
1185 x = 0;
1186 }
1187
Giulio Camuffo954f1832014-10-11 18:27:30 +03001188 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001189 NULL, 0,
1190 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1191 if (!output)
1192 return NULL;
1193
1194 output->parent.output = poutput->global;
1195
1196 output->base.make = poutput->physical.make;
1197 output->base.model = poutput->physical.model;
1198 wl_list_init(&output->base.mode_list);
1199 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1200 wl_list_init(&poutput->mode_list);
1201
1202 wayland_output_set_fullscreen(output,
1203 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1204 mode->refresh, poutput->global);
1205
1206 if (output->parent.shell_surface) {
1207 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1208 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1209 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001210 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001211 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1212 output->parent.surface,
1213 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1214 poutput->global);
1215 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1216 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1217 output->parent.surface,
1218 poutput->global,
1219 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001220 }
1221
1222 return output;
1223}
1224
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001225static void
1226shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1227 uint32_t serial)
1228{
1229 wl_shell_surface_pong(shell_surface, serial);
1230}
1231
1232static void
1233shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1234 uint32_t edges, int32_t width, int32_t height)
1235{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001236 struct wayland_output *output = data;
1237
1238 output->parent.configure_width = width;
1239 output->parent.configure_height = height;
1240
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001241 /* FIXME: implement resizing */
1242}
1243
1244static void
1245shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1246{
1247}
1248
1249static const struct wl_shell_surface_listener shell_surface_listener = {
1250 shell_surface_ping,
1251 shell_surface_configure,
1252 shell_surface_popup_done
1253};
1254
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001255/* Events received from the wayland-server this compositor is client of: */
1256
Jason Ekstrand7744f712013-10-27 22:24:55 -05001257/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001258static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001259input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001260{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001261
Jason Ekstrand7744f712013-10-27 22:24:55 -05001262 struct wl_buffer *buffer;
1263 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001264
Giulio Camuffo954f1832014-10-11 18:27:30 +03001265 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001266 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001267
Giulio Camuffo954f1832014-10-11 18:27:30 +03001268 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001269 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001270 if (!buffer)
1271 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001272
1273 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1274 input->parent.cursor.surface,
1275 image->hotspot_x, image->hotspot_y);
1276
1277 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1278 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1279 image->width, image->height);
1280 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001281}
1282
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001283static void
Daniel Stone37816df2012-05-16 18:45:18 +01001284input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1285 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001286 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001287{
1288 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001289 int32_t fx, fy;
1290 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001291 double x, y;
1292
1293 x = wl_fixed_to_double(fixed_x);
1294 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001295
Daniel Stone50692802012-06-22 13:21:41 +01001296 /* XXX: If we get a modifier event immediately before the focus,
1297 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001298 input->enter_serial = serial;
1299 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001300
1301 if (input->output->frame) {
1302 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001303 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001304 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001305 x -= fx;
1306 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001307
1308 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1309 weston_output_schedule_repaint(&input->output->base);
1310 } else {
1311 location = THEME_LOCATION_CLIENT_AREA;
1312 }
1313
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001314 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001315
1316 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001317 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001318 notify_pointer_focus(&input->base, &input->output->base, x, y);
1319 wl_pointer_set_cursor(input->parent.pointer,
1320 input->enter_serial, NULL, 0, 0);
1321 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001322 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001323 notify_pointer_focus(&input->base, NULL, 0, 0);
1324 input_set_cursor(input);
1325 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001326}
1327
1328static void
Daniel Stone37816df2012-05-16 18:45:18 +01001329input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1330 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001331{
1332 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001333
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001334 if (!input->output)
1335 return;
1336
Jason Ekstrand7744f712013-10-27 22:24:55 -05001337 if (input->output->frame) {
1338 frame_pointer_leave(input->output->frame, input);
1339
1340 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1341 weston_output_schedule_repaint(&input->output->base);
1342 }
1343
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001344 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001345 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001346 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001347}
1348
1349static void
Daniel Stone37816df2012-05-16 18:45:18 +01001350input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001351 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001352{
1353 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001354 int32_t fx, fy;
1355 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001356 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001357 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001358
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001359 if (!input->output)
1360 return;
1361
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001362 x = wl_fixed_to_double(fixed_x);
1363 y = wl_fixed_to_double(fixed_y);
1364
Jason Ekstrand7744f712013-10-27 22:24:55 -05001365 if (input->output->frame) {
1366 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001367 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001368 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001369 x -= fx;
1370 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001371
1372 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1373 weston_output_schedule_repaint(&input->output->base);
1374 } else {
1375 location = THEME_LOCATION_CLIENT_AREA;
1376 }
1377
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001378 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001379
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001380 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001381 input_set_cursor(input);
1382 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001383 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001384 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001385 } else if (!input->has_focus &&
1386 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001387 wl_pointer_set_cursor(input->parent.pointer,
1388 input->enter_serial, NULL, 0, 0);
1389 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001390 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001391 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001392 }
1393
Peter Hutterer87743e92016-01-18 16:38:22 +10001394 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001395 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001396 want_frame = true;
1397 }
1398
1399 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1400 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001401}
1402
1403static void
1404input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001405 uint32_t serial, uint32_t time, uint32_t button,
1406 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001407{
1408 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001409 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001410 enum frame_button_state fstate;
1411 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001412
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001413 if (!input->output)
1414 return;
1415
Jason Ekstrand7744f712013-10-27 22:24:55 -05001416 if (input->output->frame) {
1417 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1418 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1419
1420 location = frame_pointer_button(input->output->frame, input,
1421 button, fstate);
1422
1423 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1424
1425 wl_shell_surface_move(input->output->parent.shell_surface,
1426 input->parent.seat, serial);
1427 frame_status_clear(input->output->frame,
1428 FRAME_STATUS_MOVE);
1429 return;
1430 }
1431
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001432 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1433 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001434 input->output = NULL;
1435 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001436
Giulio Camuffo954f1832014-10-11 18:27:30 +03001437 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001438 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001439
1440 return;
1441 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001442
1443 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1444 weston_output_schedule_repaint(&input->output->base);
1445 } else {
1446 location = THEME_LOCATION_CLIENT_AREA;
1447 }
1448
Peter Hutterer87743e92016-01-18 16:38:22 +10001449 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001450 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001451 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1452 notify_pointer_frame(&input->base);
1453 }
Daniel Stone37816df2012-05-16 18:45:18 +01001454}
1455
1456static void
1457input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001458 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001459{
1460 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001461 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001462
Peter Hutterer89b6a492016-01-18 15:58:17 +10001463 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001464 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001465
Peter Hutterer87743e92016-01-18 16:38:22 +10001466 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1467 input->vert.has_discrete) {
1468 weston_event.has_discrete = true;
1469 weston_event.discrete = input->vert.discrete;
1470 input->vert.has_discrete = false;
1471 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1472 input->horiz.has_discrete) {
1473 weston_event.has_discrete = true;
1474 weston_event.discrete = input->horiz.discrete;
1475 input->horiz.has_discrete = false;
1476 }
1477
Peter Hutterer89b6a492016-01-18 15:58:17 +10001478 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001479
1480 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1481 notify_pointer_frame(&input->base);
1482}
1483
1484static void
1485input_handle_frame(void *data, struct wl_pointer *pointer)
1486{
1487 struct wayland_input *input = data;
1488
1489 notify_pointer_frame(&input->base);
1490}
1491
1492static void
1493input_handle_axis_source(void *data, struct wl_pointer *pointer,
1494 uint32_t source)
1495{
1496 struct wayland_input *input = data;
1497
1498 notify_axis_source(&input->base, source);
1499}
1500
1501static void
1502input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1503 uint32_t time, uint32_t axis)
1504{
1505 struct wayland_input *input = data;
1506 struct weston_pointer_axis_event weston_event;
1507
1508 weston_event.axis = axis;
1509 weston_event.value = 0;
1510
1511 notify_axis(&input->base, time, &weston_event);
1512}
1513
1514static void
1515input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1516 uint32_t axis, int32_t discrete)
1517{
1518 struct wayland_input *input = data;
1519
1520 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1521 input->vert.has_discrete = true;
1522 input->vert.discrete = discrete;
1523 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1524 input->horiz.has_discrete = true;
1525 input->horiz.discrete = discrete;
1526 }
Daniel Stone37816df2012-05-16 18:45:18 +01001527}
1528
1529static const struct wl_pointer_listener pointer_listener = {
1530 input_handle_pointer_enter,
1531 input_handle_pointer_leave,
1532 input_handle_motion,
1533 input_handle_button,
1534 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001535 input_handle_frame,
1536 input_handle_axis_source,
1537 input_handle_axis_stop,
1538 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001539};
1540
1541static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001542input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1543 int fd, uint32_t size)
1544{
1545 struct wayland_input *input = data;
1546 struct xkb_keymap *keymap;
1547 char *map_str;
1548
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001549 if (!data) {
1550 close(fd);
1551 return;
1552 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001553
1554 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1555 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1556 if (map_str == MAP_FAILED) {
1557 weston_log("mmap failed: %m\n");
1558 goto error;
1559 }
1560
Giulio Camuffo954f1832014-10-11 18:27:30 +03001561 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001562 map_str,
1563 XKB_KEYMAP_FORMAT_TEXT_V1,
1564 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001565 munmap(map_str, size);
1566
1567 if (!keymap) {
1568 weston_log("failed to compile keymap\n");
1569 goto error;
1570 }
1571
1572 input->keyboard_state_update = STATE_UPDATE_NONE;
1573 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1574 weston_log("No keymap provided; falling back to defalt\n");
1575 keymap = NULL;
1576 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1577 } else {
1578 weston_log("Invalid keymap\n");
1579 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001580 }
1581
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001582 close(fd);
1583
Derek Foreman1281a362015-07-31 16:55:32 -05001584 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001585 weston_seat_update_keymap(&input->base, keymap);
1586 else
1587 weston_seat_init_keyboard(&input->base, keymap);
1588
Ran Benitac9c74152014-08-19 23:59:52 +03001589 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001590
1591 return;
1592
1593error:
1594 wl_keyboard_release(input->parent.keyboard);
1595 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001596}
1597
1598static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001599input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001600 struct wl_keyboard *keyboard,
1601 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001602 struct wl_surface *surface,
1603 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001604{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001605 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001606 struct wayland_output *focus;
1607
1608 focus = input->keyboard_focus;
1609 if (focus) {
1610 /* This shouldn't happen */
1611 focus->keyboard_count--;
1612 if (!focus->keyboard_count && focus->frame)
1613 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1614 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1615 weston_output_schedule_repaint(&focus->base);
1616 }
1617
1618 input->keyboard_focus = wl_surface_get_user_data(surface);
1619 input->keyboard_focus->keyboard_count++;
1620
1621 focus = input->keyboard_focus;
1622 if (focus->frame) {
1623 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1624 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1625 weston_output_schedule_repaint(&focus->base);
1626 }
1627
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001628
Daniel Stone50692802012-06-22 13:21:41 +01001629 /* XXX: If we get a modifier event immediately before the focus,
1630 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001631 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001632 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001633}
1634
1635static void
1636input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001637 struct wl_keyboard *keyboard,
1638 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001639 struct wl_surface *surface)
1640{
1641 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001642 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001643
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001644 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001645
1646 focus = input->keyboard_focus;
1647 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001648 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001649
1650 focus->keyboard_count--;
1651 if (!focus->keyboard_count && focus->frame) {
1652 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1653 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1654 weston_output_schedule_repaint(&focus->base);
1655 }
1656
1657 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001658}
1659
Daniel Stone37816df2012-05-16 18:45:18 +01001660static void
1661input_handle_key(void *data, struct wl_keyboard *keyboard,
1662 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1663{
1664 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001665
Daniel Stone50692802012-06-22 13:21:41 +01001666 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001667 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001668 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001669 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001670 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001671}
1672
Daniel Stone351eb612012-05-31 15:27:47 -04001673static void
Derek Foreman1281a362015-07-31 16:55:32 -05001674input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001675 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001676 uint32_t mods_latched, uint32_t mods_locked,
1677 uint32_t group)
1678{
Derek Foreman1281a362015-07-31 16:55:32 -05001679 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001680 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001681 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001682 uint32_t serial_out;
1683
1684 /* If we get a key event followed by a modifier event with the
1685 * same serial number, then we try to preserve those semantics by
1686 * reusing the same serial number on the way out too. */
1687 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001688 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001689 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001690 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001691
Derek Foreman1281a362015-07-31 16:55:32 -05001692 keyboard = weston_seat_get_keyboard(&input->base);
1693 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001694 mods_depressed, mods_latched,
1695 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001696 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001697}
1698
Jonny Lamb497994a2014-08-12 14:58:26 +02001699static void
1700input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1701 int32_t rate, int32_t delay)
1702{
1703 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001704 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001705
Giulio Camuffo954f1832014-10-11 18:27:30 +03001706 b->compositor->kb_repeat_rate = rate;
1707 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001708}
1709
Daniel Stone37816df2012-05-16 18:45:18 +01001710static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001711 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001712 input_handle_keyboard_enter,
1713 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001714 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001715 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001716 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001717};
1718
1719static void
Derek Foreman748c6952015-11-06 15:56:10 -06001720input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1721 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001722 struct wl_surface *surface, int32_t id,
1723 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001724{
1725 struct wayland_input *input = data;
1726 struct wayland_output *output;
1727 enum theme_location location;
1728 bool first_touch;
1729 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001730 double x, y;
1731
1732 x = wl_fixed_to_double(fixed_x);
1733 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001734
1735 first_touch = (input->touch_points == 0);
1736 input->touch_points++;
1737
1738 input->touch_focus = wl_surface_get_user_data(surface);
1739 output = input->touch_focus;
1740 if (!first_touch && !input->touch_active)
1741 return;
1742
1743 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001744 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001745
1746 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001747 x -= fx;
1748 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001749
1750 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1751 weston_output_schedule_repaint(&output->base);
1752
1753 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1754 input->touch_points--;
1755 wl_shell_surface_move(output->parent.shell_surface,
1756 input->parent.seat, serial);
1757 frame_status_clear(output->frame,
1758 FRAME_STATUS_MOVE);
1759 return;
1760 }
1761
1762 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1763 return;
1764 }
1765
1766 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1767
1768 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1769 input->touch_active = true;
1770}
1771
1772static void
1773input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1774 uint32_t serial, uint32_t time, int32_t id)
1775{
1776 struct wayland_input *input = data;
1777 struct wayland_output *output = input->touch_focus;
1778 bool active = input->touch_active;
1779
1780 input->touch_points--;
1781 if (input->touch_points == 0) {
1782 input->touch_focus = NULL;
1783 input->touch_active = false;
1784 }
1785
1786 if (!output)
1787 return;
1788
1789 if (output->frame) {
1790 frame_touch_up(output->frame, input, id);
1791
1792 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1793 wayland_output_destroy(&output->base);
1794 input->touch_focus = NULL;
1795 input->keyboard_focus = NULL;
1796 if (wl_list_empty(&input->backend->compositor->output_list))
1797 weston_compositor_exit(input->backend->compositor);
1798
1799 return;
1800 }
1801 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1802 weston_output_schedule_repaint(&output->base);
1803 }
1804
1805 if (active)
1806 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1807}
1808
1809static void
1810input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001811 uint32_t time, int32_t id,
1812 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001813{
1814 struct wayland_input *input = data;
1815 struct wayland_output *output = input->touch_focus;
1816 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001817 double x, y;
1818
1819 x = wl_fixed_to_double(fixed_x);
1820 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001821
1822 if (!output || !input->touch_active)
1823 return;
1824
1825 if (output->frame) {
1826 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001827 x -= fx;
1828 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001829 }
1830
1831 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1832
1833 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1834}
1835
1836static void
1837input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1838{
1839 struct wayland_input *input = data;
1840
1841 if (!input->touch_focus || !input->touch_active)
1842 return;
1843
1844 notify_touch_frame(&input->base);
1845}
1846
1847static void
1848input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1849{
1850 struct wayland_input *input = data;
1851
1852 if (!input->touch_focus || !input->touch_active)
1853 return;
1854
1855 notify_touch_cancel(&input->base);
1856}
1857
1858static const struct wl_touch_listener touch_listener = {
1859 input_handle_touch_down,
1860 input_handle_touch_up,
1861 input_handle_touch_motion,
1862 input_handle_touch_frame,
1863 input_handle_touch_cancel,
1864};
1865
1866
1867static void
Daniel Stone37816df2012-05-16 18:45:18 +01001868input_handle_capabilities(void *data, struct wl_seat *seat,
1869 enum wl_seat_capability caps)
1870{
1871 struct wayland_input *input = data;
1872
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001873 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1874 input->parent.pointer = wl_seat_get_pointer(seat);
1875 wl_pointer_set_user_data(input->parent.pointer, input);
1876 wl_pointer_add_listener(input->parent.pointer,
1877 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001878 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001879 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001880 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1881 wl_pointer_release(input->parent.pointer);
1882 else
1883 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001884 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001885 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001886 }
1887
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001888 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1889 input->parent.keyboard = wl_seat_get_keyboard(seat);
1890 wl_keyboard_set_user_data(input->parent.keyboard, input);
1891 wl_keyboard_add_listener(input->parent.keyboard,
1892 &keyboard_listener, input);
1893 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001894 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1895 wl_keyboard_release(input->parent.keyboard);
1896 else
1897 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001898 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001899 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001900 }
Derek Foreman748c6952015-11-06 15:56:10 -06001901
1902 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1903 input->parent.touch = wl_seat_get_touch(seat);
1904 wl_touch_set_user_data(input->parent.touch, input);
1905 wl_touch_add_listener(input->parent.touch,
1906 &touch_listener, input);
1907 weston_seat_init_touch(&input->base);
1908 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1909 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1910 wl_touch_release(input->parent.touch);
1911 else
1912 wl_touch_destroy(input->parent.touch);
1913 input->parent.touch = NULL;
1914 weston_seat_release_touch(&input->base);
1915 }
Daniel Stone37816df2012-05-16 18:45:18 +01001916}
1917
Jonny Lamb497994a2014-08-12 14:58:26 +02001918static void
1919input_handle_name(void *data, struct wl_seat *seat,
1920 const char *name)
1921{
1922}
1923
Daniel Stone37816df2012-05-16 18:45:18 +01001924static const struct wl_seat_listener seat_listener = {
1925 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001926 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001927};
1928
1929static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001930display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001931{
1932 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001933 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001934
Peter Huttererf3d62272013-08-08 11:57:05 +10001935 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001936 if (input == NULL)
1937 return;
1938
Giulio Camuffo954f1832014-10-11 18:27:30 +03001939 weston_seat_init(&input->base, b->compositor, "default");
1940 input->backend = b;
1941 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001942 &wl_seat_interface, version);
1943 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001944 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001945
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001946 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1947 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001948
1949 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001950 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001951
1952 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1953 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001954}
1955
1956static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001957wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1958 int32_t x, int32_t y,
1959 int32_t physical_width, int32_t physical_height,
1960 int32_t subpixel, const char *make,
1961 const char *model, int32_t transform)
1962{
1963 struct wayland_parent_output *output = data;
1964
1965 output->x = x;
1966 output->y = y;
1967 output->physical.width = physical_width;
1968 output->physical.height = physical_height;
1969 output->physical.subpixel = subpixel;
1970
1971 free(output->physical.make);
1972 output->physical.make = strdup(make);
1973 free(output->physical.model);
1974 output->physical.model = strdup(model);
1975
1976 output->transform = transform;
1977}
1978
1979static struct weston_mode *
1980find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1981{
1982 struct weston_mode *mode;
1983
1984 wl_list_for_each(mode, list, link) {
1985 if (mode->width == width && mode->height == height &&
1986 mode->refresh == refresh)
1987 return mode;
1988 }
1989
1990 mode = zalloc(sizeof *mode);
1991 if (!mode)
1992 return NULL;
1993
1994 mode->width = width;
1995 mode->height = height;
1996 mode->refresh = refresh;
1997 wl_list_insert(list, &mode->link);
1998
1999 return mode;
2000}
2001
2002static void
2003wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2004 uint32_t flags, int32_t width, int32_t height,
2005 int32_t refresh)
2006{
2007 struct wayland_parent_output *output = data;
2008 struct weston_mode *mode;
2009
2010 if (output->output) {
2011 mode = find_mode(&output->output->base.mode_list,
2012 width, height, refresh);
2013 if (!mode)
2014 return;
2015 mode->flags = flags;
2016 /* Do a mode-switch on current mode change? */
2017 } else {
2018 mode = find_mode(&output->mode_list, width, height, refresh);
2019 if (!mode)
2020 return;
2021 mode->flags = flags;
2022 if (flags & WL_OUTPUT_MODE_CURRENT)
2023 output->current_mode = mode;
2024 if (flags & WL_OUTPUT_MODE_PREFERRED)
2025 output->preferred_mode = mode;
2026 }
2027}
2028
2029static const struct wl_output_listener output_listener = {
2030 wayland_parent_output_geometry,
2031 wayland_parent_output_mode
2032};
2033
2034static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002035wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002036{
2037 struct wayland_parent_output *output;
2038
2039 output = zalloc(sizeof *output);
2040 if (!output)
2041 return;
2042
2043 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002044 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002045 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002046 if (!output->global) {
2047 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002048 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002049 }
2050
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002051 wl_output_add_listener(output->global, &output_listener, output);
2052
2053 output->scale = 0;
2054 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2055 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2056 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002057 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002058
Giulio Camuffo954f1832014-10-11 18:27:30 +03002059 if (b->sprawl_across_outputs) {
2060 wl_display_roundtrip(b->parent.wl_display);
2061 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002062 }
2063}
2064
2065static void
2066wayland_parent_output_destroy(struct wayland_parent_output *output)
2067{
2068 struct weston_mode *mode, *next;
2069
2070 if (output->output)
2071 wayland_output_destroy(&output->output->base);
2072
2073 wl_output_destroy(output->global);
2074 free(output->physical.make);
2075 free(output->physical.model);
2076
2077 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2078 wl_list_remove(&mode->link);
2079 free(mode);
2080 }
2081}
2082
2083static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002084registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2085 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002086{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002087 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002088
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002089 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002090 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002091 wl_registry_bind(registry, name,
2092 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002093 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002094 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002095 wl_registry_bind(registry, name,
2096 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002097 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002098 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002099 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002100 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002101 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002102 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002103 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002104 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002105 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002106 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002107 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002108 }
2109}
2110
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002111static void
2112registry_handle_global_remove(void *data, struct wl_registry *registry,
2113 uint32_t name)
2114{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002115 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002116 struct wayland_parent_output *output;
2117
Giulio Camuffo954f1832014-10-11 18:27:30 +03002118 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002119 if (output->id == name)
2120 wayland_parent_output_destroy(output);
2121}
2122
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002123static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002124 registry_handle_global,
2125 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002126};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002127
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002128static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002129wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002130{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002131 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002132 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002133
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002134 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002135 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002136 return 0;
2137 }
2138
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002139 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002140 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002141 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002142 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002143
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002144 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002145 count = wl_display_dispatch_pending(b->parent.wl_display);
2146 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002147 }
2148
2149 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002150}
2151
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002152static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002153wayland_restore(struct weston_compositor *ec)
2154{
2155}
2156
2157static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002158wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002159{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002160 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002161
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002162 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002163
Giulio Camuffo954f1832014-10-11 18:27:30 +03002164 if (b->parent.shm)
2165 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002166
Giulio Camuffo954f1832014-10-11 18:27:30 +03002167 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002168}
2169
Jason Ekstrand7744f712013-10-27 22:24:55 -05002170static const char *left_ptrs[] = {
2171 "left_ptr",
2172 "default",
2173 "top_left_arrow",
2174 "left-arrow"
2175};
2176
2177static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002178create_cursor(struct wayland_backend *b,
2179 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002180{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002181 unsigned int i;
2182
Benoit Gschwind244ff792016-04-28 20:33:11 +02002183 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2184 config->cursor_size,
2185 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002186 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002187 fprintf(stderr, "could not load cursor theme\n");
2188 return;
2189 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002190
Giulio Camuffo954f1832014-10-11 18:27:30 +03002191 b->cursor = NULL;
2192 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2193 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002194 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002195 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002196 fprintf(stderr, "could not load left cursor\n");
2197 return;
2198 }
2199}
2200
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002201static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002202fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2203 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002204{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002205 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002206 struct wayland_input *input = NULL;
2207
Giulio Camuffo954f1832014-10-11 18:27:30 +03002208 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002209 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002210 break;
2211
2212 if (!input || !input->output)
2213 return;
2214
2215 if (input->output->frame)
2216 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2217 else
2218 wayland_output_set_windowed(input->output);
2219
2220 weston_output_schedule_repaint(&input->output->base);
2221}
2222
Giulio Camuffo954f1832014-10-11 18:27:30 +03002223static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002224wayland_backend_create(struct weston_compositor *compositor,
2225 struct weston_wayland_backend_config *new_config,
2226 int *argc, char *argv[], struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002227{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002228 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002229 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002230 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002231
Giulio Camuffo954f1832014-10-11 18:27:30 +03002232 b = zalloc(sizeof *b);
2233 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002234 return NULL;
2235
Giulio Camuffo954f1832014-10-11 18:27:30 +03002236 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002238 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002239
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002240 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002241 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002242 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002243 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002244 }
2245
Giulio Camuffo954f1832014-10-11 18:27:30 +03002246 wl_list_init(&b->parent.output_list);
2247 wl_list_init(&b->input_list);
2248 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2249 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2250 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002251
Benoit Gschwind244ff792016-04-28 20:33:11 +02002252 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002253
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002254 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002255
Giulio Camuffo954f1832014-10-11 18:27:30 +03002256 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002257 gl_renderer = weston_load_module("gl-renderer.so",
2258 "gl_renderer_interface");
2259 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002260 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002261 }
2262
Giulio Camuffo954f1832014-10-11 18:27:30 +03002263 if (!b->use_pixman) {
2264 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002265 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002266 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002267 gl_renderer->alpha_attribs,
2268 NULL,
2269 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002270 weston_log("Failed to initialize the GL renderer; "
2271 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002272 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002273 }
2274 }
2275
Giulio Camuffo954f1832014-10-11 18:27:30 +03002276 if (b->use_pixman) {
2277 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002278 weston_log("Failed to initialize pixman renderer\n");
2279 goto err_display;
2280 }
2281 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002282
Giulio Camuffo954f1832014-10-11 18:27:30 +03002283 b->base.destroy = wayland_destroy;
2284 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002285
Giulio Camuffo954f1832014-10-11 18:27:30 +03002286 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002287
Giulio Camuffo954f1832014-10-11 18:27:30 +03002288 fd = wl_display_get_fd(b->parent.wl_display);
2289 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002290 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002291 wayland_backend_handle_event, b);
2292 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002293 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002294
Giulio Camuffo954f1832014-10-11 18:27:30 +03002295 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002296
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002297 if (compositor->renderer->import_dmabuf) {
2298 if (linux_dmabuf_setup(compositor) < 0)
2299 weston_log("Error: initializing dmabuf "
2300 "support failed.\n");
2301 }
2302
Giulio Camuffo954f1832014-10-11 18:27:30 +03002303 compositor->backend = &b->base;
2304 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002305err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002306 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002307err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002308 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002310 return NULL;
2311}
2312
2313static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002314wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002315{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002316 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002317
Giulio Camuffo954f1832014-10-11 18:27:30 +03002318 if (b->theme)
2319 theme_destroy(b->theme);
2320 if (b->frame_device)
2321 cairo_device_destroy(b->frame_device);
2322 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002323
Giulio Camuffo954f1832014-10-11 18:27:30 +03002324 weston_compositor_shutdown(b->compositor);
2325 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002326}
2327
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002328static void
2329wayland_backend_config_release(struct weston_wayland_backend_config *new_config) {
2330 int i;
2331 for (i = 0; i < new_config->num_outputs; ++i) {
2332 free(new_config->outputs[i].name);
2333 }
2334 free(new_config->cursor_theme);
2335 free(new_config->display_name);
2336 free(new_config->outputs);
2337}
2338
2339/*
2340 * Append a new output struct at the end of new_config.outputs and return a
2341 * pointer to the newly allocated structure or NULL if fail. The allocated
2342 * structure is NOT cleared nor set to default values.
2343 */
2344static struct weston_wayland_backend_output_config *
2345wayland_backend_config_add_new_output(struct weston_wayland_backend_config *new_config) {
2346 struct weston_wayland_backend_output_config *outputs;
2347 outputs = realloc(new_config->outputs,
2348 (new_config->num_outputs+1)*sizeof(struct weston_wayland_backend_output_config));
2349 if (!outputs)
2350 return NULL;
2351 new_config->num_outputs += 1;
2352 new_config->outputs = outputs;
2353 return &(new_config->outputs[new_config->num_outputs-1]);
2354}
2355
2356static int
2357load_wayland_backend_config(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002358 struct weston_config *config,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002359 struct weston_wayland_backend_config *out_config)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002360{
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002361 struct weston_wayland_backend_config new_config = { 0, };
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002362 struct weston_config_section *section;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002363 struct weston_wayland_backend_output_config *oc;
2364 int count, width, height, scale;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002365 const char *section_name;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002366 char *name;
2367
2368 const struct weston_option wayland_options[] = {
2369 { WESTON_OPTION_INTEGER, "width", 0, &width },
2370 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002371 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002372 { WESTON_OPTION_STRING, "display", 0, &new_config.display_name },
2373 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &new_config.use_pixman },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002374 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002375 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &new_config.fullscreen },
2376 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &new_config.sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002377 };
2378
2379 width = 0;
2380 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002381 scale = 0;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002382 new_config.display_name = NULL;
2383 new_config.use_pixman = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002384 count = 1;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002385 new_config.fullscreen = 0;
2386 new_config.sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002387 parse_options(wayland_options,
2388 ARRAY_LENGTH(wayland_options), argc, argv);
2389
Benoit Gschwind244ff792016-04-28 20:33:11 +02002390 new_config.cursor_size = 32;
2391 new_config.cursor_theme = NULL;
2392
2393 section = weston_config_get_section(config, "shell", NULL, NULL);
2394 weston_config_section_get_string(section, "cursor-theme",
2395 &new_config.cursor_theme, NULL);
2396 weston_config_section_get_int(section, "cursor-size",
2397 &new_config.cursor_size, 32);
2398
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002399 if (new_config.sprawl) {
2400 /* do nothing, everything is already set */
2401 *out_config = new_config;
2402 return 0;
2403 }
2404
2405 if (new_config.fullscreen) {
2406 oc = wayland_backend_config_add_new_output(&new_config);
2407 if (!oc)
2408 goto err_outputs;
2409
2410 oc->width = width;
2411 oc->height = height;
2412 oc->name = NULL;
2413 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2414 oc->scale = 1;
2415
2416 *out_config = new_config;
2417 return 0;
2418 }
2419
2420 section = NULL;
2421 while (weston_config_next_section(config, &section, &section_name)) {
2422 if (!section_name || strcmp(section_name, "output") != 0)
2423 continue;
2424 weston_config_section_get_string(section, "name", &name, NULL);
2425 if (name == NULL)
2426 continue;
2427
2428 if (name[0] != 'W' || name[1] != 'L') {
2429 free(name);
2430 continue;
2431 }
2432 free(name);
2433
2434 oc = wayland_backend_config_add_new_output(&new_config);
2435
2436 if (!oc)
2437 goto err_outputs;
2438
2439 wayland_output_init_from_config(oc, section, width,
2440 height, scale);
2441 --count;
2442 }
2443
2444 if (!width)
2445 width = 1024;
2446 if (!height)
2447 height = 640;
2448 if (!scale)
2449 scale = 1;
2450 while (count > 0) {
2451
2452 oc = wayland_backend_config_add_new_output(&new_config);
2453
2454 if (!oc)
2455 goto err_outputs;
2456
2457 oc->width = width;
2458 oc->height = height;
2459 oc->name = NULL;
2460 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2461 oc->scale = scale;
2462
2463 --count;
2464 }
2465
2466 *out_config = new_config;
2467 return 0;
2468
2469err_outputs:
2470 wayland_backend_config_release(&new_config);
2471 return -1;
2472}
2473
2474WL_EXPORT int
2475backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
2476 struct weston_config *config,
2477 struct weston_backend_config *config_base)
2478{
2479 struct wayland_backend *b;
2480 struct wayland_output *output;
2481 struct wayland_parent_output *poutput;
2482 struct weston_wayland_backend_config new_config;
2483 int x, count;
2484
2485 if (load_wayland_backend_config(compositor, argc, argv, config,
2486 &new_config) < 0) {
2487 wayland_backend_config_release(&new_config);
2488 return -1;
2489 }
2490
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002491 b = wayland_backend_create(compositor, &new_config, argc, argv, config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002492
Giulio Camuffo954f1832014-10-11 18:27:30 +03002493 if (!b)
2494 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002495
Benoit Gschwind37a68072016-04-28 20:33:08 +02002496 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002497 b->sprawl_across_outputs = 1;
2498 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002499
Giulio Camuffo954f1832014-10-11 18:27:30 +03002500 wl_list_for_each(poutput, &b->parent.output_list, link)
2501 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002502
Giulio Camuffo954f1832014-10-11 18:27:30 +03002503 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002504 }
2505
Benoit Gschwind37a68072016-04-28 20:33:08 +02002506 if (new_config.fullscreen) {
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002507 if (new_config.num_outputs != 1 || !new_config.outputs)
2508 goto err_outputs;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002509
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002510 output = wayland_output_create_for_config(b,
2511 &new_config.outputs[0],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002512 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002513 if (!output)
2514 goto err_outputs;
2515
Axel Davydd8b88d2013-11-17 21:34:16 +01002516 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002518 }
2519
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002520 x = 0;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002521 for (count = 0; count < new_config.num_outputs; ++count) {
2522 output = wayland_output_create_for_config(b, &new_config.outputs[count],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002523 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002524 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002525 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002526 if (wayland_output_set_windowed(output))
2527 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002528
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002529 x += output->base.width;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002530 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002531
Giulio Camuffo954f1832014-10-11 18:27:30 +03002532 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002533 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002534 fullscreen_binding, b);
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002535 wayland_backend_config_release(&new_config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002536 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002537
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002538err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002539 wayland_backend_destroy(b);
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002540 wayland_backend_config_release(&new_config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002541 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002542}