blob: d96fc0c68190892ae313aeada8015da5c38b7f6b [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"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010043#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050044#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/image-loader.h"
47#include "shared/os-compatibility.h"
48#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080049#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020050#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020051#include "linux-dmabuf.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052
Jason Ekstrand48ce4212013-10-27 22:25:02 -050053#define WINDOW_TITLE "Weston Compositor"
54
Benoit Gschwind830b7882016-04-28 20:33:12 +020055struct weston_wayland_backend_output_config {
56 int width;
57 int height;
58 char *name;
59 uint32_t transform;
60 int32_t scale;
61};
62
Benoit Gschwind37a68072016-04-28 20:33:08 +020063struct weston_wayland_backend_config {
64 int use_pixman;
65 int sprawl;
66 char *display_name;
67 int fullscreen;
Benoit Gschwind244ff792016-04-28 20:33:11 +020068 char *cursor_theme;
69 int cursor_size;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +020070 int num_outputs;
71 struct weston_wayland_backend_output_config *outputs;
Benoit Gschwind37a68072016-04-28 20:33:08 +020072};
73
Giulio Camuffo954f1832014-10-11 18:27:30 +030074struct wayland_backend {
75 struct weston_backend base;
76 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010077
78 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040079 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040080 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081 struct wl_compositor *compositor;
82 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080083 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020084 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010085
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050086 struct wl_list output_list;
87
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010088 struct wl_event_source *wl_source;
89 uint32_t event_mask;
90 } parent;
91
Jason Ekstrandff2fd462013-10-27 22:24:58 -050092 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050093 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050094
Jason Ekstrand7744f712013-10-27 22:24:55 -050095 struct theme *theme;
96 cairo_device_t *frame_device;
97 struct wl_cursor_theme *cursor_theme;
98 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050099
Jason Ekstrand06ced802013-11-07 20:13:29 -0600100 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100101};
102
103struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500104 struct weston_output base;
105
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100106 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500107 int draw_initial_frame;
108 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500109
110 struct wl_output *output;
111 uint32_t global_id;
112
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500113 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600114 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100115 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500116
Jason Ekstrand7744f712013-10-27 22:24:55 -0500117 int keyboard_count;
118
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600119 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500120 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500121
Jason Ekstrand7744f712013-10-27 22:24:55 -0500122 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500123 struct wl_egl_window *egl_window;
124 struct {
125 cairo_surface_t *top;
126 cairo_surface_t *left;
127 cairo_surface_t *right;
128 cairo_surface_t *bottom;
129 } border;
130 } gl;
131
132 struct {
133 struct wl_list buffers;
134 struct wl_list free_buffers;
135 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500136
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500137 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500138 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100139};
140
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500141struct wayland_parent_output {
142 struct wayland_output *output;
143 struct wl_list link;
144
145 struct wl_output *global;
146 uint32_t id;
147
148 struct {
149 char *make;
150 char *model;
151 int32_t width, height;
152 uint32_t subpixel;
153 } physical;
154
155 int32_t x, y;
156 uint32_t transform;
157 uint32_t scale;
158
159 struct wl_list mode_list;
160 struct weston_mode *preferred_mode;
161 struct weston_mode *current_mode;
162};
163
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500164struct wayland_shm_buffer {
165 struct wayland_output *output;
166 struct wl_list link;
167 struct wl_list free_link;
168
169 struct wl_buffer *buffer;
170 void *data;
171 size_t size;
172 pixman_region32_t damage;
173 int frame_damaged;
174
175 pixman_image_t *pm_image;
176 cairo_surface_t *c_surface;
177};
178
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100179struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400180 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300181 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100182 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500183
184 struct {
185 struct wl_seat *seat;
186 struct wl_pointer *pointer;
187 struct wl_keyboard *keyboard;
188 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500189
190 struct {
191 struct wl_surface *surface;
192 int32_t hx, hy;
193 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500194 } parent;
195
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500196 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100197 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400198 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600199 uint32_t touch_points;
200 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600201 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600202 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600203
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400204 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600205 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500206 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000207
208 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100209};
210
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300211struct gl_renderer_interface *gl_renderer;
212
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500213static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500214wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
215{
216 cairo_surface_destroy(buffer->c_surface);
217 pixman_image_unref(buffer->pm_image);
218
219 wl_buffer_destroy(buffer->buffer);
220 munmap(buffer->data, buffer->size);
221
222 pixman_region32_fini(&buffer->damage);
223
224 wl_list_remove(&buffer->link);
225 wl_list_remove(&buffer->free_link);
226 free(buffer);
227}
228
229static void
230buffer_release(void *data, struct wl_buffer *buffer)
231{
232 struct wayland_shm_buffer *sb = data;
233
234 if (sb->output) {
235 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
236 } else {
237 wayland_shm_buffer_destroy(sb);
238 }
239}
240
241static const struct wl_buffer_listener buffer_listener = {
242 buffer_release
243};
244
245static struct wayland_shm_buffer *
246wayland_output_get_shm_buffer(struct wayland_output *output)
247{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300248 struct wayland_backend *b =
249 (struct wayland_backend *) output->base.compositor->backend;
250 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500251 struct wayland_shm_buffer *sb;
252
253 struct wl_shm_pool *pool;
254 int width, height, stride;
255 int32_t fx, fy;
256 int fd;
257 unsigned char *data;
258
259 if (!wl_list_empty(&output->shm.free_buffers)) {
260 sb = container_of(output->shm.free_buffers.next,
261 struct wayland_shm_buffer, free_link);
262 wl_list_remove(&sb->free_link);
263 wl_list_init(&sb->free_link);
264
265 return sb;
266 }
267
268 if (output->frame) {
269 width = frame_width(output->frame);
270 height = frame_height(output->frame);
271 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500272 width = output->base.current_mode->width;
273 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500274 }
275
276 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
277
278 fd = os_create_anonymous_file(height * stride);
279 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000280 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500281 return NULL;
282 }
283
284 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
285 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000286 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500287 close(fd);
288 return NULL;
289 }
290
291 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000292 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200293 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000294 close(fd);
295 free(data);
296 return NULL;
297 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500298
299 sb->output = output;
300 wl_list_init(&sb->free_link);
301 wl_list_insert(&output->shm.buffers, &sb->link);
302
303 pixman_region32_init_rect(&sb->damage, 0, 0,
304 output->base.width, output->base.height);
305 sb->frame_damaged = 1;
306
307 sb->data = data;
308 sb->size = height * stride;
309
310 pool = wl_shm_create_pool(shm, fd, sb->size);
311
312 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
313 width, height,
314 stride,
315 WL_SHM_FORMAT_ARGB8888);
316 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
317 wl_shm_pool_destroy(pool);
318 close(fd);
319
320 memset(data, 0, sb->size);
321
322 sb->c_surface =
323 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
324 width, height, stride);
325
326 fx = 0;
327 fy = 0;
328 if (output->frame)
329 frame_interior(output->frame, &fx, &fy, 0, 0);
330 sb->pm_image =
331 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
332 (uint32_t *)(data + fy * stride) + fx,
333 stride);
334
335 return sb;
336}
337
338static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500339frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400340{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500341 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400342 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400343
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500344 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400345
346 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300347
348 /*
349 * This is the fallback case, where Presentation extension is not
350 * available from the parent compositor. We do not know the base for
351 * 'time', so we cannot feed it to finish_frame(). Do the only thing
352 * we can, and pretend finish_frame time is when we process this
353 * event.
354 */
355 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200356 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400357}
358
359static const struct wl_callback_listener frame_listener = {
360 frame_done
361};
362
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500363static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200364draw_initial_frame(struct wayland_output *output)
365{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500366 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200367
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200369
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500370 /* If we are rendering with GL, then orphan it so that it gets
371 * destroyed immediately */
372 if (output->gl.egl_window)
373 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500374
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500375 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500376 wl_surface_damage(output->parent.surface, 0, 0,
377 output->base.current_mode->width,
378 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200379}
380
381static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382wayland_output_update_gl_border(struct wayland_output *output)
383{
384 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
385 cairo_t *cr;
386
387 if (!output->frame)
388 return;
389 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
390 return;
391
392 fwidth = frame_width(output->frame);
393 fheight = frame_height(output->frame);
394 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
395
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500396 if (!output->gl.border.top)
397 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500398 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
399 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500400 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500401 frame_repaint(output->frame, cr);
402 cairo_destroy(cr);
403 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
404 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500405 cairo_image_surface_get_stride(output->gl.border.top) / 4,
406 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500407
408
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 if (!output->gl.border.left)
410 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500411 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
412 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500413 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500414 cairo_translate(cr, 0, -iy);
415 frame_repaint(output->frame, cr);
416 cairo_destroy(cr);
417 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
418 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500419 cairo_image_surface_get_stride(output->gl.border.left) / 4,
420 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500421
422
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500423 if (!output->gl.border.right)
424 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500425 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
426 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500427 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500428 cairo_translate(cr, -(iwidth + ix), -iy);
429 frame_repaint(output->frame, cr);
430 cairo_destroy(cr);
431 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
432 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500433 cairo_image_surface_get_stride(output->gl.border.right) / 4,
434 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500435
436
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500437 if (!output->gl.border.bottom)
438 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500439 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
440 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500441 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500442 cairo_translate(cr, 0, -(iy + iheight));
443 frame_repaint(output->frame, cr);
444 cairo_destroy(cr);
445 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
446 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500447 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
448 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500449}
450
451static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200452wayland_output_start_repaint_loop(struct weston_output *output_base)
453{
454 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300455 struct wayland_backend *wb =
456 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200457 struct wl_callback *callback;
458
459 /* If this is the initial frame, we need to attach a buffer so that
460 * the compositor can map the surface and include it in its render
461 * loop. If the surface doesn't end up in the render loop, the frame
462 * callback won't be invoked. The buffer is transparent and of the
463 * same size as the future real output buffer. */
464 if (output->parent.draw_initial_frame) {
465 output->parent.draw_initial_frame = 0;
466
467 draw_initial_frame(output);
468 }
469
470 callback = wl_surface_frame(output->parent.surface);
471 wl_callback_add_listener(callback, &frame_listener, output);
472 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300473 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200474}
475
David Herrmann1edf44c2013-10-22 17:11:26 +0200476static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500477wayland_output_repaint_gl(struct weston_output *output_base,
478 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400479{
480 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400481 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400482 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600483
Kristian Høgsberg33418202011-08-16 23:01:28 -0400484 callback = wl_surface_frame(output->parent.surface);
485 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100486
Jason Ekstrand7744f712013-10-27 22:24:55 -0500487 wayland_output_update_gl_border(output);
488
Pekka Paalanenbc106382012-10-10 12:49:31 +0300489 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200490
491 pixman_region32_subtract(&ec->primary_plane.damage,
492 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200493 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100494}
495
Matt Roper361d2ad2011-08-29 13:52:23 -0700496static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500497wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
498{
499 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
500 cairo_t *cr;
501
502 if (!buffer->output->frame || !buffer->frame_damaged)
503 return;
504
505 cr = cairo_create(buffer->c_surface);
506
507 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
508 fwidth = frame_width(buffer->output->frame);
509 fheight = frame_height(buffer->output->frame);
510
511 /* Set the clip so we don't unnecisaraly damage the surface */
512 cairo_move_to(cr, ix, iy);
513 cairo_rel_line_to(cr, iwidth, 0);
514 cairo_rel_line_to(cr, 0, iheight);
515 cairo_rel_line_to(cr, -iwidth, 0);
516 cairo_line_to(cr, ix, iy);
517 cairo_line_to(cr, 0, iy);
518 cairo_line_to(cr, 0, fheight);
519 cairo_line_to(cr, fwidth, fheight);
520 cairo_line_to(cr, fwidth, 0);
521 cairo_line_to(cr, 0, 0);
522 cairo_line_to(cr, 0, iy);
523 cairo_close_path(cr);
524 cairo_clip(cr);
525
526 /* Draw using a pattern so that the final result gets clipped */
527 cairo_push_group(cr);
528 frame_repaint(buffer->output->frame, cr);
529 cairo_pop_group_to_source(cr);
530 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
531 cairo_paint(cr);
532
533 cairo_destroy(cr);
534}
535
536static void
537wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
538{
539 pixman_region32_t damage;
540 pixman_box32_t *rects;
541 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
542 int i, n;
543
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500544 pixman_region32_init(&damage);
545 weston_transformed_region(sb->output->base.width,
546 sb->output->base.height,
547 sb->output->base.transform,
548 sb->output->base.current_scale,
549 &sb->damage, &damage);
550
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500551 if (sb->output->frame) {
552 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
553 fwidth = frame_width(sb->output->frame);
554 fheight = frame_height(sb->output->frame);
555
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500556 pixman_region32_translate(&damage, ix, iy);
557
558 if (sb->frame_damaged) {
559 pixman_region32_union_rect(&damage, &damage,
560 0, 0, fwidth, iy);
561 pixman_region32_union_rect(&damage, &damage,
562 0, iy, ix, iheight);
563 pixman_region32_union_rect(&damage, &damage,
564 ix + iwidth, iy,
565 fwidth - (ix + iwidth), iheight);
566 pixman_region32_union_rect(&damage, &damage,
567 0, iy + iheight,
568 fwidth, fheight - (iy + iheight));
569 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500570 }
571
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500572 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500573 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
574 for (i = 0; i < n; ++i)
575 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
576 rects[i].y1, rects[i].x2 - rects[i].x1,
577 rects[i].y2 - rects[i].y1);
578
579 if (sb->output->frame)
580 pixman_region32_fini(&damage);
581}
582
583static int
584wayland_output_repaint_pixman(struct weston_output *output_base,
585 pixman_region32_t *damage)
586{
587 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300588 struct wayland_backend *b =
589 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500590 struct wl_callback *callback;
591 struct wayland_shm_buffer *sb;
592
593 if (output->frame) {
594 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
595 wl_list_for_each(sb, &output->shm.buffers, link)
596 sb->frame_damaged = 1;
597 }
598
599 wl_list_for_each(sb, &output->shm.buffers, link)
600 pixman_region32_union(&sb->damage, &sb->damage, damage);
601
602 sb = wayland_output_get_shm_buffer(output);
603
604 wayland_output_update_shm_border(sb);
605 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300606 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500607
608 wayland_shm_buffer_attach(sb);
609
610 callback = wl_surface_frame(output->parent.surface);
611 wl_callback_add_listener(callback, &frame_listener, output);
612 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300613 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500614
615 pixman_region32_fini(&sb->damage);
616 pixman_region32_init(&sb->damage);
617 sb->frame_damaged = 0;
618
Giulio Camuffo954f1832014-10-11 18:27:30 +0300619 pixman_region32_subtract(&b->compositor->primary_plane.damage,
620 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500621 return 0;
622}
623
624static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500625wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700626{
627 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300628 struct wayland_backend *b =
629 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700630
Giulio Camuffo954f1832014-10-11 18:27:30 +0300631 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500632 pixman_renderer_output_destroy(output_base);
633 } else {
634 gl_renderer->output_destroy(output_base);
635 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100636
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500637 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500638 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500639 if (output->parent.shell_surface)
640 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500641
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600642 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500643 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600644
645 cairo_surface_destroy(output->gl.border.top);
646 cairo_surface_destroy(output->gl.border.left);
647 cairo_surface_destroy(output->gl.border.right);
648 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500649
650 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700651 free(output);
652
653 return;
654}
655
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300656static const struct wl_shell_surface_listener shell_surface_listener;
657
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200658static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500659wayland_output_init_gl_renderer(struct wayland_output *output)
660{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500661 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500662
663 if (output->frame) {
664 fwidth = frame_width(output->frame);
665 fheight = frame_height(output->frame);
666 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500667 fwidth = output->base.current_mode->width;
668 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500669 }
670
671 output->gl.egl_window =
672 wl_egl_window_create(output->parent.surface,
673 fwidth, fheight);
674 if (!output->gl.egl_window) {
675 weston_log("failure to create wl_egl_window\n");
676 return -1;
677 }
678
679 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000680 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100681 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000682 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500683 NULL,
684 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500685 goto cleanup_window;
686
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500687 return 0;
688
689cleanup_window:
690 wl_egl_window_destroy(output->gl.egl_window);
691 return -1;
692}
693
694static int
695wayland_output_init_pixman_renderer(struct wayland_output *output)
696{
697 return pixman_renderer_output_create(&output->base);
698}
699
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600700static void
701wayland_output_resize_surface(struct wayland_output *output)
702{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300703 struct wayland_backend *b =
704 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600705 struct wayland_shm_buffer *buffer, *next;
706 int32_t ix, iy, iwidth, iheight;
707 int32_t width, height;
708 struct wl_region *region;
709
710 width = output->base.current_mode->width;
711 height = output->base.current_mode->height;
712
713 if (output->frame) {
714 frame_resize_inside(output->frame, width, height);
715
716 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300717 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600718 wl_region_add(region, ix, iy, iwidth, iheight);
719 wl_surface_set_input_region(output->parent.surface, region);
720 wl_region_destroy(region);
721
722 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300723 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600724 wl_region_add(region, ix, iy, iwidth, iheight);
725 wl_surface_set_opaque_region(output->parent.surface, region);
726 wl_region_destroy(region);
727
728 width = frame_width(output->frame);
729 height = frame_height(output->frame);
730 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300731 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600732 wl_region_add(region, 0, 0, width, height);
733 wl_surface_set_input_region(output->parent.surface, region);
734 wl_region_destroy(region);
735
Giulio Camuffo954f1832014-10-11 18:27:30 +0300736 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600737 wl_region_add(region, 0, 0, width, height);
738 wl_surface_set_opaque_region(output->parent.surface, region);
739 wl_region_destroy(region);
740 }
741
742 if (output->gl.egl_window) {
743 wl_egl_window_resize(output->gl.egl_window,
744 width, height, 0, 0);
745
746 /* These will need to be re-created due to the resize */
747 gl_renderer->output_set_border(&output->base,
748 GL_RENDERER_BORDER_TOP,
749 0, 0, 0, NULL);
750 cairo_surface_destroy(output->gl.border.top);
751 output->gl.border.top = NULL;
752 gl_renderer->output_set_border(&output->base,
753 GL_RENDERER_BORDER_LEFT,
754 0, 0, 0, NULL);
755 cairo_surface_destroy(output->gl.border.left);
756 output->gl.border.left = NULL;
757 gl_renderer->output_set_border(&output->base,
758 GL_RENDERER_BORDER_RIGHT,
759 0, 0, 0, NULL);
760 cairo_surface_destroy(output->gl.border.right);
761 output->gl.border.right = NULL;
762 gl_renderer->output_set_border(&output->base,
763 GL_RENDERER_BORDER_BOTTOM,
764 0, 0, 0, NULL);
765 cairo_surface_destroy(output->gl.border.bottom);
766 output->gl.border.bottom = NULL;
767 }
768
769 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500770 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600771 wayland_shm_buffer_destroy(buffer);
772 /* These will get thrown away when they get released */
773 wl_list_for_each(buffer, &output->shm.buffers, link)
774 buffer->output = NULL;
775}
776
777static int
778wayland_output_set_windowed(struct wayland_output *output)
779{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300780 struct wayland_backend *b =
781 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600782 int tlen;
783 char *title;
784
785 if (output->frame)
786 return 0;
787
788 if (output->name) {
789 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
790 title = malloc(tlen + 1);
791 if (!title)
792 return -1;
793
794 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
795 } else {
796 title = strdup(WINDOW_TITLE);
797 }
798
Giulio Camuffo954f1832014-10-11 18:27:30 +0300799 if (!b->theme) {
800 b->theme = theme_create();
801 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600802 free(title);
803 return -1;
804 }
805 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300806 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600807 FRAME_BUTTON_CLOSE, title);
808 free(title);
809 if (!output->frame)
810 return -1;
811
812 if (output->keyboard_count)
813 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
814
815 wayland_output_resize_surface(output);
816
817 wl_shell_surface_set_toplevel(output->parent.shell_surface);
818
819 return 0;
820}
821
822static void
823wayland_output_set_fullscreen(struct wayland_output *output,
824 enum wl_shell_surface_fullscreen_method method,
825 uint32_t framerate, struct wl_output *target)
826{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300827 struct wayland_backend *b =
828 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500829
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600830 if (output->frame) {
831 frame_destroy(output->frame);
832 output->frame = NULL;
833 }
834
835 wayland_output_resize_surface(output);
836
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500837 if (output->parent.shell_surface) {
838 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
839 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300840 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800841 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
842 output->parent.surface,
843 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500844 }
845}
846
847static struct weston_mode *
848wayland_output_choose_mode(struct wayland_output *output,
849 struct weston_mode *ref_mode)
850{
851 struct weston_mode *mode;
852
853 /* First look for an exact match */
854 wl_list_for_each(mode, &output->base.mode_list, link)
855 if (mode->width == ref_mode->width &&
856 mode->height == ref_mode->height &&
857 mode->refresh == ref_mode->refresh)
858 return mode;
859
860 /* If we can't find an exact match, ignore refresh and try again */
861 wl_list_for_each(mode, &output->base.mode_list, link)
862 if (mode->width == ref_mode->width &&
863 mode->height == ref_mode->height)
864 return mode;
865
866 /* Yeah, we failed */
867 return NULL;
868}
869
870enum mode_status {
871 MODE_STATUS_UNKNOWN,
872 MODE_STATUS_SUCCESS,
873 MODE_STATUS_FAIL,
874 MODE_STATUS_CANCEL,
875};
876
877static void
878mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800879 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500880{
881 enum mode_status *value = data;
882
883 printf("Mode switch successful\n");
884
885 *value = MODE_STATUS_SUCCESS;
886}
887
888static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800889mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500890{
891 enum mode_status *value = data;
892
893 printf("Mode switch failed\n");
894
895 *value = MODE_STATUS_FAIL;
896}
897
898static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800899mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500900{
901 enum mode_status *value = data;
902
903 printf("Mode switch cancelled\n");
904
905 *value = MODE_STATUS_CANCEL;
906}
907
Jonas Ådahl496adb32015-11-17 16:00:27 +0800908struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500909 mode_feedback_successful,
910 mode_feedback_failed,
911 mode_feedback_cancelled,
912};
913
914static int
915wayland_output_switch_mode(struct weston_output *output_base,
916 struct weston_mode *mode)
917{
918 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300919 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500920 struct wl_surface *old_surface;
921 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800922 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500923 enum mode_status mode_status;
924 int ret = 0;
925
926 if (output_base == NULL) {
927 weston_log("output is NULL.\n");
928 return -1;
929 }
930
931 if (mode == NULL) {
932 weston_log("mode is NULL.\n");
933 return -1;
934 }
935
Giulio Camuffo954f1832014-10-11 18:27:30 +0300936 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500937
Giulio Camuffo954f1832014-10-11 18:27:30 +0300938 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500939 return -1;
940
941 mode = wayland_output_choose_mode(output, mode);
942 if (mode == NULL)
943 return -1;
944
945 if (output->base.current_mode == mode)
946 return 0;
947
948 old_mode = output->base.current_mode;
949 old_surface = output->parent.surface;
950 output->base.current_mode = mode;
951 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300952 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500953 wl_surface_set_user_data(output->parent.surface, output);
954
955 /* Blow the old buffers because we changed size/surfaces */
956 wayland_output_resize_surface(output);
957
958 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800959 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
960 output->parent.surface,
961 output->parent.output,
962 mode->refresh);
963 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
964 &mode_feedback_listener,
965 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500966
967 /* This should kick-start things again */
968 output->parent.draw_initial_frame = 1;
969 wayland_output_start_repaint_loop(&output->base);
970
971 mode_status = MODE_STATUS_UNKNOWN;
972 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300973 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500974
Jonas Ådahl496adb32015-11-17 16:00:27 +0800975 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500976
977 if (mode_status == MODE_STATUS_FAIL) {
978 output->base.current_mode = old_mode;
979 wl_surface_destroy(output->parent.surface);
980 output->parent.surface = old_surface;
981 wayland_output_resize_surface(output);
982
983 return -1;
984 }
985
986 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
987 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
988
Giulio Camuffo954f1832014-10-11 18:27:30 +0300989 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500990 pixman_renderer_output_destroy(output_base);
991 if (wayland_output_init_pixman_renderer(output) < 0)
992 goto err_output;
993 } else {
994 gl_renderer->output_destroy(output_base);
995 wl_egl_window_destroy(output->gl.egl_window);
996 if (wayland_output_init_gl_renderer(output) < 0)
997 goto err_output;
998 }
999 wl_surface_destroy(old_surface);
1000
1001 weston_output_schedule_repaint(&output->base);
1002
1003 return 0;
1004
1005err_output:
1006 /* XXX */
1007 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001008}
1009
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001010static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001011wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001012 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001013 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001014{
1015 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001016 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001017
1018 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1019 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001020
Peter Huttererf3d62272013-08-08 11:57:05 +10001021 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001022 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001023 return NULL;
1024
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001025 output->name = name ? strdup(name) : NULL;
1026 output->base.make = "waywayland";
1027 output->base.model = "none";
1028
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001029 output_width = width * scale;
1030 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001031
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001032 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001033 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001034 if (!output->parent.surface)
1035 goto err_name;
1036 wl_surface_set_user_data(output->parent.surface, output);
1037
1038 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001039
Giulio Camuffo954f1832014-10-11 18:27:30 +03001040 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001041 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001042 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001043 output->parent.surface);
1044 if (!output->parent.shell_surface)
1045 goto err_surface;
1046 wl_shell_surface_add_listener(output->parent.shell_surface,
1047 &shell_surface_listener, output);
1048 }
1049
Giulio Camuffo954f1832014-10-11 18:27:30 +03001050 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001051 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1052 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001053 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001054 if (!width)
1055 output_width = output->parent.configure_width;
1056 if (!height)
1057 output_height = output->parent.configure_height;
1058 }
1059
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001060 output->mode.flags =
1061 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001062 output->mode.width = output_width;
1063 output->mode.height = output_height;
1064 output->mode.refresh = 60000;
1065 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001066 wl_list_init(&output->base.mode_list);
1067 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001068 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001069
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001070 wl_list_init(&output->shm.buffers);
1071 wl_list_init(&output->shm.free_buffers);
1072
Giulio Camuffo954f1832014-10-11 18:27:30 +03001073 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001074 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001075
Giulio Camuffo954f1832014-10-11 18:27:30 +03001076 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001077 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001078 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001079 output->base.repaint = wayland_output_repaint_pixman;
1080 } else {
1081 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001082 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001083 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001084 }
1085
Jonas Ådahle5a12252013-04-05 23:07:11 +02001086 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001087 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001088 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001089 output->base.set_backlight = NULL;
1090 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001091 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001092
Giulio Camuffo954f1832014-10-11 18:27:30 +03001093 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001094
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001095 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001096
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001097err_output:
1098 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001099 if (output->parent.shell_surface)
1100 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001101err_surface:
1102 wl_surface_destroy(output->parent.surface);
1103err_name:
1104 free(output->name);
1105
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001106 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001107 free(output);
1108
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001109 return NULL;
1110}
1111
Benoit Gschwind830b7882016-04-28 20:33:12 +02001112static void
1113wayland_output_init_from_config(struct weston_wayland_backend_output_config *output,
1114 struct weston_config_section *config_section,
1115 int option_width, int option_height,
1116 int option_scale)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001117{
Benoit Gschwind830b7882016-04-28 20:33:12 +02001118 char *mode, *t, *str;
Derek Foreman64a3df02014-10-23 12:24:18 -05001119 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001120
Benoit Gschwind830b7882016-04-28 20:33:12 +02001121 weston_config_section_get_string(config_section, "name", &output->name,
1122 NULL);
1123 if (output->name) {
1124 slen = strlen(output->name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001125 slen += strlen(WINDOW_TITLE " - ");
1126 str = malloc(slen + 1);
1127 if (str)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001128 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1129 output->name);
1130 free(output->name);
1131 output->name = str;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001132 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02001133 if (!output->name)
1134 output->name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001135
1136 weston_config_section_get_string(config_section,
1137 "mode", &mode, "1024x600");
Benoit Gschwind830b7882016-04-28 20:33:12 +02001138 if (sscanf(mode, "%dx%d", &output->width, &output->height) != 2) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001139 weston_log("Invalid mode \"%s\" for output %s\n",
Benoit Gschwind830b7882016-04-28 20:33:12 +02001140 mode, output->name);
1141 output->width = 1024;
1142 output->height = 640;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001143 }
1144 free(mode);
1145
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001146 if (option_width)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001147 output->width = option_width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001148 if (option_height)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001149 output->height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001150
Benoit Gschwind830b7882016-04-28 20:33:12 +02001151 weston_config_section_get_int(config_section, "scale", &output->scale, 1);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001152
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001153 if (option_scale)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001154 output->scale = option_scale;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001155
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001156 weston_config_section_get_string(config_section,
1157 "transform", &t, "normal");
Benoit Gschwind830b7882016-04-28 20:33:12 +02001158 if (weston_parse_transform(t, &output->transform) < 0)
Derek Foreman64a3df02014-10-23 12:24:18 -05001159 weston_log("Invalid transform \"%s\" for output %s\n",
Benoit Gschwind830b7882016-04-28 20:33:12 +02001160 t, output->name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001161 free(t);
1162
Benoit Gschwind830b7882016-04-28 20:33:12 +02001163}
1164
1165static struct wayland_output *
1166wayland_output_create_for_config(struct wayland_backend *b,
1167 struct weston_wayland_backend_output_config *oc,
1168 int fullscreen, int32_t x, int32_t y)
1169{
1170 struct wayland_output *output;
1171
1172 output = wayland_output_create(b, x, y, oc->width, oc->height, oc->name,
1173 fullscreen, oc->transform, oc->scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001174
1175 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001176}
1177
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001178static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001179wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001180 struct wayland_parent_output *poutput)
1181{
1182 struct wayland_output *output;
1183 struct weston_mode *mode;
1184 int32_t x;
1185
1186 if (poutput->current_mode) {
1187 mode = poutput->current_mode;
1188 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001189 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001190 } else if (!wl_list_empty(&poutput->mode_list)) {
1191 mode = container_of(poutput->mode_list.next,
1192 struct weston_mode, link);
1193 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001194 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001195 return NULL;
1196 }
1197
Giulio Camuffo954f1832014-10-11 18:27:30 +03001198 if (!wl_list_empty(&b->compositor->output_list)) {
1199 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001200 struct wayland_output, base.link);
1201 x = output->base.x + output->base.current_mode->width;
1202 } else {
1203 x = 0;
1204 }
1205
Giulio Camuffo954f1832014-10-11 18:27:30 +03001206 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001207 NULL, 0,
1208 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1209 if (!output)
1210 return NULL;
1211
1212 output->parent.output = poutput->global;
1213
1214 output->base.make = poutput->physical.make;
1215 output->base.model = poutput->physical.model;
1216 wl_list_init(&output->base.mode_list);
1217 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1218 wl_list_init(&poutput->mode_list);
1219
1220 wayland_output_set_fullscreen(output,
1221 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1222 mode->refresh, poutput->global);
1223
1224 if (output->parent.shell_surface) {
1225 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1226 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1227 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001228 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001229 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1230 output->parent.surface,
1231 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1232 poutput->global);
1233 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1234 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1235 output->parent.surface,
1236 poutput->global,
1237 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001238 }
1239
1240 return output;
1241}
1242
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001243static void
1244shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1245 uint32_t serial)
1246{
1247 wl_shell_surface_pong(shell_surface, serial);
1248}
1249
1250static void
1251shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1252 uint32_t edges, int32_t width, int32_t height)
1253{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001254 struct wayland_output *output = data;
1255
1256 output->parent.configure_width = width;
1257 output->parent.configure_height = height;
1258
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001259 /* FIXME: implement resizing */
1260}
1261
1262static void
1263shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1264{
1265}
1266
1267static const struct wl_shell_surface_listener shell_surface_listener = {
1268 shell_surface_ping,
1269 shell_surface_configure,
1270 shell_surface_popup_done
1271};
1272
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001273/* Events received from the wayland-server this compositor is client of: */
1274
Jason Ekstrand7744f712013-10-27 22:24:55 -05001275/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001276static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001277input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001278{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001279
Jason Ekstrand7744f712013-10-27 22:24:55 -05001280 struct wl_buffer *buffer;
1281 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001282
Giulio Camuffo954f1832014-10-11 18:27:30 +03001283 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001284 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001285
Giulio Camuffo954f1832014-10-11 18:27:30 +03001286 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001287 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001288 if (!buffer)
1289 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001290
1291 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1292 input->parent.cursor.surface,
1293 image->hotspot_x, image->hotspot_y);
1294
1295 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1296 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1297 image->width, image->height);
1298 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001299}
1300
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001301static void
Daniel Stone37816df2012-05-16 18:45:18 +01001302input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1303 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001304 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001305{
1306 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001307 int32_t fx, fy;
1308 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001309 double x, y;
1310
1311 x = wl_fixed_to_double(fixed_x);
1312 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001313
Daniel Stone50692802012-06-22 13:21:41 +01001314 /* XXX: If we get a modifier event immediately before the focus,
1315 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001316 input->enter_serial = serial;
1317 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001318
1319 if (input->output->frame) {
1320 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001321 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001322 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001323 x -= fx;
1324 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001325
1326 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1327 weston_output_schedule_repaint(&input->output->base);
1328 } else {
1329 location = THEME_LOCATION_CLIENT_AREA;
1330 }
1331
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001332 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001333
1334 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001335 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336 notify_pointer_focus(&input->base, &input->output->base, x, y);
1337 wl_pointer_set_cursor(input->parent.pointer,
1338 input->enter_serial, NULL, 0, 0);
1339 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001340 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001341 notify_pointer_focus(&input->base, NULL, 0, 0);
1342 input_set_cursor(input);
1343 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001344}
1345
1346static void
Daniel Stone37816df2012-05-16 18:45:18 +01001347input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1348 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001349{
1350 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001351
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001352 if (!input->output)
1353 return;
1354
Jason Ekstrand7744f712013-10-27 22:24:55 -05001355 if (input->output->frame) {
1356 frame_pointer_leave(input->output->frame, input);
1357
1358 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1359 weston_output_schedule_repaint(&input->output->base);
1360 }
1361
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001362 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001363 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001364 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001365}
1366
1367static void
Daniel Stone37816df2012-05-16 18:45:18 +01001368input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001369 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001370{
1371 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001372 int32_t fx, fy;
1373 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001374 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001375 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001376
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001377 if (!input->output)
1378 return;
1379
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001380 x = wl_fixed_to_double(fixed_x);
1381 y = wl_fixed_to_double(fixed_y);
1382
Jason Ekstrand7744f712013-10-27 22:24:55 -05001383 if (input->output->frame) {
1384 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001385 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001386 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001387 x -= fx;
1388 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001389
1390 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1391 weston_output_schedule_repaint(&input->output->base);
1392 } else {
1393 location = THEME_LOCATION_CLIENT_AREA;
1394 }
1395
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001396 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001397
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001398 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001399 input_set_cursor(input);
1400 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001401 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001402 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001403 } else if (!input->has_focus &&
1404 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001405 wl_pointer_set_cursor(input->parent.pointer,
1406 input->enter_serial, NULL, 0, 0);
1407 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001408 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001409 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001410 }
1411
Peter Hutterer87743e92016-01-18 16:38:22 +10001412 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001413 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001414 want_frame = true;
1415 }
1416
1417 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1418 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001419}
1420
1421static void
1422input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001423 uint32_t serial, uint32_t time, uint32_t button,
1424 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001425{
1426 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001427 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001428 enum frame_button_state fstate;
1429 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001430
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001431 if (!input->output)
1432 return;
1433
Jason Ekstrand7744f712013-10-27 22:24:55 -05001434 if (input->output->frame) {
1435 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1436 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1437
1438 location = frame_pointer_button(input->output->frame, input,
1439 button, fstate);
1440
1441 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1442
1443 wl_shell_surface_move(input->output->parent.shell_surface,
1444 input->parent.seat, serial);
1445 frame_status_clear(input->output->frame,
1446 FRAME_STATUS_MOVE);
1447 return;
1448 }
1449
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001450 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1451 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001452 input->output = NULL;
1453 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001454
Giulio Camuffo954f1832014-10-11 18:27:30 +03001455 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001456 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001457
1458 return;
1459 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001460
1461 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1462 weston_output_schedule_repaint(&input->output->base);
1463 } else {
1464 location = THEME_LOCATION_CLIENT_AREA;
1465 }
1466
Peter Hutterer87743e92016-01-18 16:38:22 +10001467 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001468 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001469 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1470 notify_pointer_frame(&input->base);
1471 }
Daniel Stone37816df2012-05-16 18:45:18 +01001472}
1473
1474static void
1475input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001476 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001477{
1478 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001479 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001480
Peter Hutterer89b6a492016-01-18 15:58:17 +10001481 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001482 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001483
Peter Hutterer87743e92016-01-18 16:38:22 +10001484 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1485 input->vert.has_discrete) {
1486 weston_event.has_discrete = true;
1487 weston_event.discrete = input->vert.discrete;
1488 input->vert.has_discrete = false;
1489 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1490 input->horiz.has_discrete) {
1491 weston_event.has_discrete = true;
1492 weston_event.discrete = input->horiz.discrete;
1493 input->horiz.has_discrete = false;
1494 }
1495
Peter Hutterer89b6a492016-01-18 15:58:17 +10001496 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001497
1498 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1499 notify_pointer_frame(&input->base);
1500}
1501
1502static void
1503input_handle_frame(void *data, struct wl_pointer *pointer)
1504{
1505 struct wayland_input *input = data;
1506
1507 notify_pointer_frame(&input->base);
1508}
1509
1510static void
1511input_handle_axis_source(void *data, struct wl_pointer *pointer,
1512 uint32_t source)
1513{
1514 struct wayland_input *input = data;
1515
1516 notify_axis_source(&input->base, source);
1517}
1518
1519static void
1520input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1521 uint32_t time, uint32_t axis)
1522{
1523 struct wayland_input *input = data;
1524 struct weston_pointer_axis_event weston_event;
1525
1526 weston_event.axis = axis;
1527 weston_event.value = 0;
1528
1529 notify_axis(&input->base, time, &weston_event);
1530}
1531
1532static void
1533input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1534 uint32_t axis, int32_t discrete)
1535{
1536 struct wayland_input *input = data;
1537
1538 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1539 input->vert.has_discrete = true;
1540 input->vert.discrete = discrete;
1541 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1542 input->horiz.has_discrete = true;
1543 input->horiz.discrete = discrete;
1544 }
Daniel Stone37816df2012-05-16 18:45:18 +01001545}
1546
1547static const struct wl_pointer_listener pointer_listener = {
1548 input_handle_pointer_enter,
1549 input_handle_pointer_leave,
1550 input_handle_motion,
1551 input_handle_button,
1552 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001553 input_handle_frame,
1554 input_handle_axis_source,
1555 input_handle_axis_stop,
1556 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001557};
1558
1559static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001560input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1561 int fd, uint32_t size)
1562{
1563 struct wayland_input *input = data;
1564 struct xkb_keymap *keymap;
1565 char *map_str;
1566
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001567 if (!data) {
1568 close(fd);
1569 return;
1570 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001571
1572 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1573 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1574 if (map_str == MAP_FAILED) {
1575 weston_log("mmap failed: %m\n");
1576 goto error;
1577 }
1578
Giulio Camuffo954f1832014-10-11 18:27:30 +03001579 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001580 map_str,
1581 XKB_KEYMAP_FORMAT_TEXT_V1,
1582 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001583 munmap(map_str, size);
1584
1585 if (!keymap) {
1586 weston_log("failed to compile keymap\n");
1587 goto error;
1588 }
1589
1590 input->keyboard_state_update = STATE_UPDATE_NONE;
1591 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1592 weston_log("No keymap provided; falling back to defalt\n");
1593 keymap = NULL;
1594 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1595 } else {
1596 weston_log("Invalid keymap\n");
1597 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001598 }
1599
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001600 close(fd);
1601
Derek Foreman1281a362015-07-31 16:55:32 -05001602 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001603 weston_seat_update_keymap(&input->base, keymap);
1604 else
1605 weston_seat_init_keyboard(&input->base, keymap);
1606
Ran Benitac9c74152014-08-19 23:59:52 +03001607 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001608
1609 return;
1610
1611error:
1612 wl_keyboard_release(input->parent.keyboard);
1613 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001614}
1615
1616static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001617input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001618 struct wl_keyboard *keyboard,
1619 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001620 struct wl_surface *surface,
1621 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001622{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001623 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001624 struct wayland_output *focus;
1625
1626 focus = input->keyboard_focus;
1627 if (focus) {
1628 /* This shouldn't happen */
1629 focus->keyboard_count--;
1630 if (!focus->keyboard_count && focus->frame)
1631 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1632 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1633 weston_output_schedule_repaint(&focus->base);
1634 }
1635
1636 input->keyboard_focus = wl_surface_get_user_data(surface);
1637 input->keyboard_focus->keyboard_count++;
1638
1639 focus = input->keyboard_focus;
1640 if (focus->frame) {
1641 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1642 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1643 weston_output_schedule_repaint(&focus->base);
1644 }
1645
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001646
Daniel Stone50692802012-06-22 13:21:41 +01001647 /* XXX: If we get a modifier event immediately before the focus,
1648 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001649 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001650 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001651}
1652
1653static void
1654input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001655 struct wl_keyboard *keyboard,
1656 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001657 struct wl_surface *surface)
1658{
1659 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001660 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001661
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001662 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001663
1664 focus = input->keyboard_focus;
1665 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001666 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001667
1668 focus->keyboard_count--;
1669 if (!focus->keyboard_count && focus->frame) {
1670 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1671 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1672 weston_output_schedule_repaint(&focus->base);
1673 }
1674
1675 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001676}
1677
Daniel Stone37816df2012-05-16 18:45:18 +01001678static void
1679input_handle_key(void *data, struct wl_keyboard *keyboard,
1680 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1681{
1682 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001683
Daniel Stone50692802012-06-22 13:21:41 +01001684 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001685 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001686 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001687 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001688 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001689}
1690
Daniel Stone351eb612012-05-31 15:27:47 -04001691static void
Derek Foreman1281a362015-07-31 16:55:32 -05001692input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001693 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001694 uint32_t mods_latched, uint32_t mods_locked,
1695 uint32_t group)
1696{
Derek Foreman1281a362015-07-31 16:55:32 -05001697 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001698 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001699 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001700 uint32_t serial_out;
1701
1702 /* If we get a key event followed by a modifier event with the
1703 * same serial number, then we try to preserve those semantics by
1704 * reusing the same serial number on the way out too. */
1705 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001706 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001707 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001708 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001709
Derek Foreman1281a362015-07-31 16:55:32 -05001710 keyboard = weston_seat_get_keyboard(&input->base);
1711 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001712 mods_depressed, mods_latched,
1713 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001714 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001715}
1716
Jonny Lamb497994a2014-08-12 14:58:26 +02001717static void
1718input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1719 int32_t rate, int32_t delay)
1720{
1721 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001722 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001723
Giulio Camuffo954f1832014-10-11 18:27:30 +03001724 b->compositor->kb_repeat_rate = rate;
1725 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001726}
1727
Daniel Stone37816df2012-05-16 18:45:18 +01001728static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001729 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001730 input_handle_keyboard_enter,
1731 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001732 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001733 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001734 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001735};
1736
1737static void
Derek Foreman748c6952015-11-06 15:56:10 -06001738input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1739 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001740 struct wl_surface *surface, int32_t id,
1741 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001742{
1743 struct wayland_input *input = data;
1744 struct wayland_output *output;
1745 enum theme_location location;
1746 bool first_touch;
1747 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001748 double x, y;
1749
1750 x = wl_fixed_to_double(fixed_x);
1751 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001752
1753 first_touch = (input->touch_points == 0);
1754 input->touch_points++;
1755
1756 input->touch_focus = wl_surface_get_user_data(surface);
1757 output = input->touch_focus;
1758 if (!first_touch && !input->touch_active)
1759 return;
1760
1761 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001762 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001763
1764 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001765 x -= fx;
1766 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001767
1768 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1769 weston_output_schedule_repaint(&output->base);
1770
1771 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1772 input->touch_points--;
1773 wl_shell_surface_move(output->parent.shell_surface,
1774 input->parent.seat, serial);
1775 frame_status_clear(output->frame,
1776 FRAME_STATUS_MOVE);
1777 return;
1778 }
1779
1780 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1781 return;
1782 }
1783
1784 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1785
1786 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1787 input->touch_active = true;
1788}
1789
1790static void
1791input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1792 uint32_t serial, uint32_t time, int32_t id)
1793{
1794 struct wayland_input *input = data;
1795 struct wayland_output *output = input->touch_focus;
1796 bool active = input->touch_active;
1797
1798 input->touch_points--;
1799 if (input->touch_points == 0) {
1800 input->touch_focus = NULL;
1801 input->touch_active = false;
1802 }
1803
1804 if (!output)
1805 return;
1806
1807 if (output->frame) {
1808 frame_touch_up(output->frame, input, id);
1809
1810 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1811 wayland_output_destroy(&output->base);
1812 input->touch_focus = NULL;
1813 input->keyboard_focus = NULL;
1814 if (wl_list_empty(&input->backend->compositor->output_list))
1815 weston_compositor_exit(input->backend->compositor);
1816
1817 return;
1818 }
1819 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1820 weston_output_schedule_repaint(&output->base);
1821 }
1822
1823 if (active)
1824 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1825}
1826
1827static void
1828input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001829 uint32_t time, int32_t id,
1830 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001831{
1832 struct wayland_input *input = data;
1833 struct wayland_output *output = input->touch_focus;
1834 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001835 double x, y;
1836
1837 x = wl_fixed_to_double(fixed_x);
1838 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001839
1840 if (!output || !input->touch_active)
1841 return;
1842
1843 if (output->frame) {
1844 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001845 x -= fx;
1846 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001847 }
1848
1849 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1850
1851 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1852}
1853
1854static void
1855input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1856{
1857 struct wayland_input *input = data;
1858
1859 if (!input->touch_focus || !input->touch_active)
1860 return;
1861
1862 notify_touch_frame(&input->base);
1863}
1864
1865static void
1866input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1867{
1868 struct wayland_input *input = data;
1869
1870 if (!input->touch_focus || !input->touch_active)
1871 return;
1872
1873 notify_touch_cancel(&input->base);
1874}
1875
1876static const struct wl_touch_listener touch_listener = {
1877 input_handle_touch_down,
1878 input_handle_touch_up,
1879 input_handle_touch_motion,
1880 input_handle_touch_frame,
1881 input_handle_touch_cancel,
1882};
1883
1884
1885static void
Daniel Stone37816df2012-05-16 18:45:18 +01001886input_handle_capabilities(void *data, struct wl_seat *seat,
1887 enum wl_seat_capability caps)
1888{
1889 struct wayland_input *input = data;
1890
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001891 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1892 input->parent.pointer = wl_seat_get_pointer(seat);
1893 wl_pointer_set_user_data(input->parent.pointer, input);
1894 wl_pointer_add_listener(input->parent.pointer,
1895 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001896 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001897 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001898 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1899 wl_pointer_release(input->parent.pointer);
1900 else
1901 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001902 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001903 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001904 }
1905
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001906 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1907 input->parent.keyboard = wl_seat_get_keyboard(seat);
1908 wl_keyboard_set_user_data(input->parent.keyboard, input);
1909 wl_keyboard_add_listener(input->parent.keyboard,
1910 &keyboard_listener, input);
1911 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001912 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1913 wl_keyboard_release(input->parent.keyboard);
1914 else
1915 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001916 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001917 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001918 }
Derek Foreman748c6952015-11-06 15:56:10 -06001919
1920 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1921 input->parent.touch = wl_seat_get_touch(seat);
1922 wl_touch_set_user_data(input->parent.touch, input);
1923 wl_touch_add_listener(input->parent.touch,
1924 &touch_listener, input);
1925 weston_seat_init_touch(&input->base);
1926 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1927 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1928 wl_touch_release(input->parent.touch);
1929 else
1930 wl_touch_destroy(input->parent.touch);
1931 input->parent.touch = NULL;
1932 weston_seat_release_touch(&input->base);
1933 }
Daniel Stone37816df2012-05-16 18:45:18 +01001934}
1935
Jonny Lamb497994a2014-08-12 14:58:26 +02001936static void
1937input_handle_name(void *data, struct wl_seat *seat,
1938 const char *name)
1939{
1940}
1941
Daniel Stone37816df2012-05-16 18:45:18 +01001942static const struct wl_seat_listener seat_listener = {
1943 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001944 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001945};
1946
1947static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001948display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001949{
1950 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001951 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001952
Peter Huttererf3d62272013-08-08 11:57:05 +10001953 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001954 if (input == NULL)
1955 return;
1956
Giulio Camuffo954f1832014-10-11 18:27:30 +03001957 weston_seat_init(&input->base, b->compositor, "default");
1958 input->backend = b;
1959 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001960 &wl_seat_interface, version);
1961 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001962 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001963
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001964 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1965 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001966
1967 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001968 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001969
1970 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1971 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001972}
1973
1974static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001975wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1976 int32_t x, int32_t y,
1977 int32_t physical_width, int32_t physical_height,
1978 int32_t subpixel, const char *make,
1979 const char *model, int32_t transform)
1980{
1981 struct wayland_parent_output *output = data;
1982
1983 output->x = x;
1984 output->y = y;
1985 output->physical.width = physical_width;
1986 output->physical.height = physical_height;
1987 output->physical.subpixel = subpixel;
1988
1989 free(output->physical.make);
1990 output->physical.make = strdup(make);
1991 free(output->physical.model);
1992 output->physical.model = strdup(model);
1993
1994 output->transform = transform;
1995}
1996
1997static struct weston_mode *
1998find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1999{
2000 struct weston_mode *mode;
2001
2002 wl_list_for_each(mode, list, link) {
2003 if (mode->width == width && mode->height == height &&
2004 mode->refresh == refresh)
2005 return mode;
2006 }
2007
2008 mode = zalloc(sizeof *mode);
2009 if (!mode)
2010 return NULL;
2011
2012 mode->width = width;
2013 mode->height = height;
2014 mode->refresh = refresh;
2015 wl_list_insert(list, &mode->link);
2016
2017 return mode;
2018}
2019
2020static void
2021wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2022 uint32_t flags, int32_t width, int32_t height,
2023 int32_t refresh)
2024{
2025 struct wayland_parent_output *output = data;
2026 struct weston_mode *mode;
2027
2028 if (output->output) {
2029 mode = find_mode(&output->output->base.mode_list,
2030 width, height, refresh);
2031 if (!mode)
2032 return;
2033 mode->flags = flags;
2034 /* Do a mode-switch on current mode change? */
2035 } else {
2036 mode = find_mode(&output->mode_list, width, height, refresh);
2037 if (!mode)
2038 return;
2039 mode->flags = flags;
2040 if (flags & WL_OUTPUT_MODE_CURRENT)
2041 output->current_mode = mode;
2042 if (flags & WL_OUTPUT_MODE_PREFERRED)
2043 output->preferred_mode = mode;
2044 }
2045}
2046
2047static const struct wl_output_listener output_listener = {
2048 wayland_parent_output_geometry,
2049 wayland_parent_output_mode
2050};
2051
2052static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002053wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002054{
2055 struct wayland_parent_output *output;
2056
2057 output = zalloc(sizeof *output);
2058 if (!output)
2059 return;
2060
2061 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002062 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002063 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002064 if (!output->global) {
2065 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002066 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002067 }
2068
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002069 wl_output_add_listener(output->global, &output_listener, output);
2070
2071 output->scale = 0;
2072 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2073 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2074 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002075 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002076
Giulio Camuffo954f1832014-10-11 18:27:30 +03002077 if (b->sprawl_across_outputs) {
2078 wl_display_roundtrip(b->parent.wl_display);
2079 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002080 }
2081}
2082
2083static void
2084wayland_parent_output_destroy(struct wayland_parent_output *output)
2085{
2086 struct weston_mode *mode, *next;
2087
2088 if (output->output)
2089 wayland_output_destroy(&output->output->base);
2090
2091 wl_output_destroy(output->global);
2092 free(output->physical.make);
2093 free(output->physical.model);
2094
2095 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2096 wl_list_remove(&mode->link);
2097 free(mode);
2098 }
2099}
2100
2101static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002102registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2103 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002104{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002105 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002106
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002107 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002108 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002109 wl_registry_bind(registry, name,
2110 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002111 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002112 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002113 wl_registry_bind(registry, name,
2114 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002115 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002116 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002117 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002118 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002119 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002120 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002121 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002122 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002123 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002124 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002125 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002126 }
2127}
2128
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002129static void
2130registry_handle_global_remove(void *data, struct wl_registry *registry,
2131 uint32_t name)
2132{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002133 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002134 struct wayland_parent_output *output;
2135
Giulio Camuffo954f1832014-10-11 18:27:30 +03002136 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002137 if (output->id == name)
2138 wayland_parent_output_destroy(output);
2139}
2140
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002141static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002142 registry_handle_global,
2143 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002144};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002145
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002146static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002147wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002148{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002149 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002150 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002151
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002152 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002153 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002154 return 0;
2155 }
2156
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002157 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002158 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002159 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002160 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002161
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002162 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163 count = wl_display_dispatch_pending(b->parent.wl_display);
2164 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002165 }
2166
2167 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002168}
2169
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002170static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002171wayland_restore(struct weston_compositor *ec)
2172{
2173}
2174
2175static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002176wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002177{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002178 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002179
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002180 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002181
Giulio Camuffo954f1832014-10-11 18:27:30 +03002182 if (b->parent.shm)
2183 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002184
Giulio Camuffo954f1832014-10-11 18:27:30 +03002185 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002186}
2187
Jason Ekstrand7744f712013-10-27 22:24:55 -05002188static const char *left_ptrs[] = {
2189 "left_ptr",
2190 "default",
2191 "top_left_arrow",
2192 "left-arrow"
2193};
2194
2195static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002196create_cursor(struct wayland_backend *b,
2197 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002198{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002199 unsigned int i;
2200
Benoit Gschwind244ff792016-04-28 20:33:11 +02002201 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2202 config->cursor_size,
2203 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002204 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002205 fprintf(stderr, "could not load cursor theme\n");
2206 return;
2207 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002208
Giulio Camuffo954f1832014-10-11 18:27:30 +03002209 b->cursor = NULL;
2210 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2211 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002212 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002213 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002214 fprintf(stderr, "could not load left cursor\n");
2215 return;
2216 }
2217}
2218
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002219static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002220fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2221 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002222{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002223 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002224 struct wayland_input *input = NULL;
2225
Giulio Camuffo954f1832014-10-11 18:27:30 +03002226 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002227 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002228 break;
2229
2230 if (!input || !input->output)
2231 return;
2232
2233 if (input->output->frame)
2234 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2235 else
2236 wayland_output_set_windowed(input->output);
2237
2238 weston_output_schedule_repaint(&input->output->base);
2239}
2240
Giulio Camuffo954f1832014-10-11 18:27:30 +03002241static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002242wayland_backend_create(struct weston_compositor *compositor,
2243 struct weston_wayland_backend_config *new_config,
2244 int *argc, char *argv[], struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002245{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002246 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002247 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002248 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002249
Giulio Camuffo954f1832014-10-11 18:27:30 +03002250 b = zalloc(sizeof *b);
2251 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002252 return NULL;
2253
Giulio Camuffo954f1832014-10-11 18:27:30 +03002254 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002255 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002256 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002257
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002258 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002259 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002260 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002261 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002262 }
2263
Giulio Camuffo954f1832014-10-11 18:27:30 +03002264 wl_list_init(&b->parent.output_list);
2265 wl_list_init(&b->input_list);
2266 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2267 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2268 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002269
Benoit Gschwind244ff792016-04-28 20:33:11 +02002270 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002271
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002272 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002273
Giulio Camuffo954f1832014-10-11 18:27:30 +03002274 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002275 gl_renderer = weston_load_module("gl-renderer.so",
2276 "gl_renderer_interface");
2277 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002278 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002279 }
2280
Giulio Camuffo954f1832014-10-11 18:27:30 +03002281 if (!b->use_pixman) {
2282 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002283 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002284 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002285 gl_renderer->alpha_attribs,
2286 NULL,
2287 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002288 weston_log("Failed to initialize the GL renderer; "
2289 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002290 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002291 }
2292 }
2293
Giulio Camuffo954f1832014-10-11 18:27:30 +03002294 if (b->use_pixman) {
2295 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002296 weston_log("Failed to initialize pixman renderer\n");
2297 goto err_display;
2298 }
2299 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002300
Giulio Camuffo954f1832014-10-11 18:27:30 +03002301 b->base.destroy = wayland_destroy;
2302 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002303
Giulio Camuffo954f1832014-10-11 18:27:30 +03002304 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002305
Giulio Camuffo954f1832014-10-11 18:27:30 +03002306 fd = wl_display_get_fd(b->parent.wl_display);
2307 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002308 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 wayland_backend_handle_event, b);
2310 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002311 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002312
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002314
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002315 if (compositor->renderer->import_dmabuf) {
2316 if (linux_dmabuf_setup(compositor) < 0)
2317 weston_log("Error: initializing dmabuf "
2318 "support failed.\n");
2319 }
2320
Giulio Camuffo954f1832014-10-11 18:27:30 +03002321 compositor->backend = &b->base;
2322 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002323err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002324 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002325err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002326 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002327 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002328 return NULL;
2329}
2330
2331static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002332wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002333{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002334 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002335
Giulio Camuffo954f1832014-10-11 18:27:30 +03002336 if (b->theme)
2337 theme_destroy(b->theme);
2338 if (b->frame_device)
2339 cairo_device_destroy(b->frame_device);
2340 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002341
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342 weston_compositor_shutdown(b->compositor);
2343 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002344}
2345
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002346static void
2347wayland_backend_config_release(struct weston_wayland_backend_config *new_config) {
2348 int i;
2349 for (i = 0; i < new_config->num_outputs; ++i) {
2350 free(new_config->outputs[i].name);
2351 }
2352 free(new_config->cursor_theme);
2353 free(new_config->display_name);
2354 free(new_config->outputs);
2355}
2356
2357/*
2358 * Append a new output struct at the end of new_config.outputs and return a
2359 * pointer to the newly allocated structure or NULL if fail. The allocated
2360 * structure is NOT cleared nor set to default values.
2361 */
2362static struct weston_wayland_backend_output_config *
2363wayland_backend_config_add_new_output(struct weston_wayland_backend_config *new_config) {
2364 struct weston_wayland_backend_output_config *outputs;
2365 outputs = realloc(new_config->outputs,
2366 (new_config->num_outputs+1)*sizeof(struct weston_wayland_backend_output_config));
2367 if (!outputs)
2368 return NULL;
2369 new_config->num_outputs += 1;
2370 new_config->outputs = outputs;
2371 return &(new_config->outputs[new_config->num_outputs-1]);
2372}
2373
2374static int
2375load_wayland_backend_config(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002376 struct weston_config *config,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002377 struct weston_wayland_backend_config *out_config)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002378{
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002379 struct weston_wayland_backend_config new_config = { 0, };
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002380 struct weston_config_section *section;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002381 struct weston_wayland_backend_output_config *oc;
2382 int count, width, height, scale;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002383 const char *section_name;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002384 char *name;
2385
2386 const struct weston_option wayland_options[] = {
2387 { WESTON_OPTION_INTEGER, "width", 0, &width },
2388 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002389 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002390 { WESTON_OPTION_STRING, "display", 0, &new_config.display_name },
2391 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &new_config.use_pixman },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002392 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002393 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &new_config.fullscreen },
2394 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &new_config.sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002395 };
2396
2397 width = 0;
2398 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002399 scale = 0;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002400 new_config.display_name = NULL;
2401 new_config.use_pixman = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002402 count = 1;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002403 new_config.fullscreen = 0;
2404 new_config.sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002405 parse_options(wayland_options,
2406 ARRAY_LENGTH(wayland_options), argc, argv);
2407
Benoit Gschwind244ff792016-04-28 20:33:11 +02002408 new_config.cursor_size = 32;
2409 new_config.cursor_theme = NULL;
2410
2411 section = weston_config_get_section(config, "shell", NULL, NULL);
2412 weston_config_section_get_string(section, "cursor-theme",
2413 &new_config.cursor_theme, NULL);
2414 weston_config_section_get_int(section, "cursor-size",
2415 &new_config.cursor_size, 32);
2416
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002417 if (new_config.sprawl) {
2418 /* do nothing, everything is already set */
2419 *out_config = new_config;
2420 return 0;
2421 }
2422
2423 if (new_config.fullscreen) {
2424 oc = wayland_backend_config_add_new_output(&new_config);
2425 if (!oc)
2426 goto err_outputs;
2427
2428 oc->width = width;
2429 oc->height = height;
2430 oc->name = NULL;
2431 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2432 oc->scale = 1;
2433
2434 *out_config = new_config;
2435 return 0;
2436 }
2437
2438 section = NULL;
2439 while (weston_config_next_section(config, &section, &section_name)) {
2440 if (!section_name || strcmp(section_name, "output") != 0)
2441 continue;
2442 weston_config_section_get_string(section, "name", &name, NULL);
2443 if (name == NULL)
2444 continue;
2445
2446 if (name[0] != 'W' || name[1] != 'L') {
2447 free(name);
2448 continue;
2449 }
2450 free(name);
2451
2452 oc = wayland_backend_config_add_new_output(&new_config);
2453
2454 if (!oc)
2455 goto err_outputs;
2456
2457 wayland_output_init_from_config(oc, section, width,
2458 height, scale);
2459 --count;
2460 }
2461
2462 if (!width)
2463 width = 1024;
2464 if (!height)
2465 height = 640;
2466 if (!scale)
2467 scale = 1;
2468 while (count > 0) {
2469
2470 oc = wayland_backend_config_add_new_output(&new_config);
2471
2472 if (!oc)
2473 goto err_outputs;
2474
2475 oc->width = width;
2476 oc->height = height;
2477 oc->name = NULL;
2478 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2479 oc->scale = scale;
2480
2481 --count;
2482 }
2483
2484 *out_config = new_config;
2485 return 0;
2486
2487err_outputs:
2488 wayland_backend_config_release(&new_config);
2489 return -1;
2490}
2491
2492WL_EXPORT int
2493backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
2494 struct weston_config *config,
2495 struct weston_backend_config *config_base)
2496{
2497 struct wayland_backend *b;
2498 struct wayland_output *output;
2499 struct wayland_parent_output *poutput;
2500 struct weston_wayland_backend_config new_config;
2501 int x, count;
2502
2503 if (load_wayland_backend_config(compositor, argc, argv, config,
2504 &new_config) < 0) {
2505 wayland_backend_config_release(&new_config);
2506 return -1;
2507 }
2508
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002509 b = wayland_backend_create(compositor, &new_config, argc, argv, config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002510
Giulio Camuffo954f1832014-10-11 18:27:30 +03002511 if (!b)
2512 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002513
Benoit Gschwind37a68072016-04-28 20:33:08 +02002514 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002515 b->sprawl_across_outputs = 1;
2516 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002517
Giulio Camuffo954f1832014-10-11 18:27:30 +03002518 wl_list_for_each(poutput, &b->parent.output_list, link)
2519 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002520
Giulio Camuffo954f1832014-10-11 18:27:30 +03002521 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002522 }
2523
Benoit Gschwind37a68072016-04-28 20:33:08 +02002524 if (new_config.fullscreen) {
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002525 if (new_config.num_outputs != 1 || !new_config.outputs)
2526 goto err_outputs;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002527
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002528 output = wayland_output_create_for_config(b,
2529 &new_config.outputs[0],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002530 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002531 if (!output)
2532 goto err_outputs;
2533
Axel Davydd8b88d2013-11-17 21:34:16 +01002534 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002535 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002536 }
2537
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002538 x = 0;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002539 for (count = 0; count < new_config.num_outputs; ++count) {
2540 output = wayland_output_create_for_config(b, &new_config.outputs[count],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002541 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002542 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002543 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002544 if (wayland_output_set_windowed(output))
2545 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002546
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002547 x += output->base.width;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002548 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002549
Giulio Camuffo954f1832014-10-11 18:27:30 +03002550 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002551 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002552 fullscreen_binding, b);
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002553 wayland_backend_config_release(&new_config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002554 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002555
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002556err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002557 wayland_backend_destroy(b);
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002558 wayland_backend_config_release(&new_config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002559 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002560}