blob: 0375a11c19c4c41000dffedc9d656786df80e700 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060037#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkebe014562011-02-18 17:04:24 +010039#include <wayland-client.h>
40#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050041#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010043#include "compositor.h"
Benoit Gschwind639322a2016-04-28 20:33:15 +020044#include "compositor-wayland.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010045#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050046#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070048#include "shared/image-loader.h"
49#include "shared/os-compatibility.h"
50#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080051#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020052#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020053#include "linux-dmabuf.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054
Jason Ekstrand48ce4212013-10-27 22:25:02 -050055#define WINDOW_TITLE "Weston Compositor"
56
Giulio Camuffo954f1832014-10-11 18:27:30 +030057struct wayland_backend {
58 struct weston_backend base;
59 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010060
61 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040062 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040063 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010064 struct wl_compositor *compositor;
65 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080066 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020067 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010068
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050069 struct wl_list output_list;
70
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010071 struct wl_event_source *wl_source;
72 uint32_t event_mask;
73 } parent;
74
Jason Ekstrandff2fd462013-10-27 22:24:58 -050075 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050076 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050077
Jason Ekstrand7744f712013-10-27 22:24:55 -050078 struct theme *theme;
79 cairo_device_t *frame_device;
80 struct wl_cursor_theme *cursor_theme;
81 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050082
Jason Ekstrand06ced802013-11-07 20:13:29 -060083 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010084};
85
86struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050087 struct weston_output base;
88
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010089 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050090 int draw_initial_frame;
91 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050092
93 struct wl_output *output;
94 uint32_t global_id;
95
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050096 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060097 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010098 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050099
Jason Ekstrand7744f712013-10-27 22:24:55 -0500100 int keyboard_count;
101
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600102 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500103 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500104
Jason Ekstrand7744f712013-10-27 22:24:55 -0500105 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500106 struct wl_egl_window *egl_window;
107 struct {
108 cairo_surface_t *top;
109 cairo_surface_t *left;
110 cairo_surface_t *right;
111 cairo_surface_t *bottom;
112 } border;
113 } gl;
114
115 struct {
116 struct wl_list buffers;
117 struct wl_list free_buffers;
118 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500119
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500120 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500121 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100122};
123
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500124struct wayland_parent_output {
125 struct wayland_output *output;
126 struct wl_list link;
127
128 struct wl_output *global;
129 uint32_t id;
130
131 struct {
132 char *make;
133 char *model;
134 int32_t width, height;
135 uint32_t subpixel;
136 } physical;
137
138 int32_t x, y;
139 uint32_t transform;
140 uint32_t scale;
141
142 struct wl_list mode_list;
143 struct weston_mode *preferred_mode;
144 struct weston_mode *current_mode;
145};
146
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500147struct wayland_shm_buffer {
148 struct wayland_output *output;
149 struct wl_list link;
150 struct wl_list free_link;
151
152 struct wl_buffer *buffer;
153 void *data;
154 size_t size;
155 pixman_region32_t damage;
156 int frame_damaged;
157
158 pixman_image_t *pm_image;
159 cairo_surface_t *c_surface;
160};
161
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100162struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400163 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300164 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100165 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500166
167 struct {
168 struct wl_seat *seat;
169 struct wl_pointer *pointer;
170 struct wl_keyboard *keyboard;
171 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500172
173 struct {
174 struct wl_surface *surface;
175 int32_t hx, hy;
176 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500177 } parent;
178
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500179 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100180 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400181 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600182 uint32_t touch_points;
183 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600184 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600185 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600186
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400187 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600188 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500189 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000190
191 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100192};
193
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300194struct gl_renderer_interface *gl_renderer;
195
Armin Krezović938dc522016-08-01 19:17:57 +0200196static inline struct wayland_output *
197to_wayland_output(struct weston_output *base)
198{
199 return container_of(base, struct wayland_output, base);
200}
201
202static inline struct wayland_backend *
203to_wayland_backend(struct weston_compositor *base)
204{
205 return container_of(base->backend, struct wayland_backend, base);
206}
207
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500208static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500209wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
210{
211 cairo_surface_destroy(buffer->c_surface);
212 pixman_image_unref(buffer->pm_image);
213
214 wl_buffer_destroy(buffer->buffer);
215 munmap(buffer->data, buffer->size);
216
217 pixman_region32_fini(&buffer->damage);
218
219 wl_list_remove(&buffer->link);
220 wl_list_remove(&buffer->free_link);
221 free(buffer);
222}
223
224static void
225buffer_release(void *data, struct wl_buffer *buffer)
226{
227 struct wayland_shm_buffer *sb = data;
228
229 if (sb->output) {
230 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
231 } else {
232 wayland_shm_buffer_destroy(sb);
233 }
234}
235
236static const struct wl_buffer_listener buffer_listener = {
237 buffer_release
238};
239
240static struct wayland_shm_buffer *
241wayland_output_get_shm_buffer(struct wayland_output *output)
242{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300243 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200244 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300245 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500246 struct wayland_shm_buffer *sb;
247
248 struct wl_shm_pool *pool;
249 int width, height, stride;
250 int32_t fx, fy;
251 int fd;
252 unsigned char *data;
253
254 if (!wl_list_empty(&output->shm.free_buffers)) {
255 sb = container_of(output->shm.free_buffers.next,
256 struct wayland_shm_buffer, free_link);
257 wl_list_remove(&sb->free_link);
258 wl_list_init(&sb->free_link);
259
260 return sb;
261 }
262
263 if (output->frame) {
264 width = frame_width(output->frame);
265 height = frame_height(output->frame);
266 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500267 width = output->base.current_mode->width;
268 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500269 }
270
271 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
272
273 fd = os_create_anonymous_file(height * stride);
274 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000275 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500276 return NULL;
277 }
278
279 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
280 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000281 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500282 close(fd);
283 return NULL;
284 }
285
286 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000287 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200288 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000289 close(fd);
290 free(data);
291 return NULL;
292 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500293
294 sb->output = output;
295 wl_list_init(&sb->free_link);
296 wl_list_insert(&output->shm.buffers, &sb->link);
297
298 pixman_region32_init_rect(&sb->damage, 0, 0,
299 output->base.width, output->base.height);
300 sb->frame_damaged = 1;
301
302 sb->data = data;
303 sb->size = height * stride;
304
305 pool = wl_shm_create_pool(shm, fd, sb->size);
306
307 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
308 width, height,
309 stride,
310 WL_SHM_FORMAT_ARGB8888);
311 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
312 wl_shm_pool_destroy(pool);
313 close(fd);
314
315 memset(data, 0, sb->size);
316
317 sb->c_surface =
318 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
319 width, height, stride);
320
321 fx = 0;
322 fy = 0;
323 if (output->frame)
324 frame_interior(output->frame, &fx, &fy, 0, 0);
325 sb->pm_image =
326 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
327 (uint32_t *)(data + fy * stride) + fx,
328 stride);
329
330 return sb;
331}
332
333static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500334frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400335{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500336 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400337 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400338
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500339 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400340
341 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300342
343 /*
344 * This is the fallback case, where Presentation extension is not
345 * available from the parent compositor. We do not know the base for
346 * 'time', so we cannot feed it to finish_frame(). Do the only thing
347 * we can, and pretend finish_frame time is when we process this
348 * event.
349 */
350 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200351 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400352}
353
354static const struct wl_callback_listener frame_listener = {
355 frame_done
356};
357
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500358static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200359draw_initial_frame(struct wayland_output *output)
360{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500361 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200362
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500363 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200364
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500365 /* If we are rendering with GL, then orphan it so that it gets
366 * destroyed immediately */
367 if (output->gl.egl_window)
368 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500369
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500370 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500371 wl_surface_damage(output->parent.surface, 0, 0,
372 output->base.current_mode->width,
373 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200374}
375
376static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500377wayland_output_update_gl_border(struct wayland_output *output)
378{
379 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
380 cairo_t *cr;
381
382 if (!output->frame)
383 return;
384 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
385 return;
386
387 fwidth = frame_width(output->frame);
388 fheight = frame_height(output->frame);
389 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
390
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500391 if (!output->gl.border.top)
392 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500393 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
394 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 frame_repaint(output->frame, cr);
397 cairo_destroy(cr);
398 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
399 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500400 cairo_image_surface_get_stride(output->gl.border.top) / 4,
401 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500402
403
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500404 if (!output->gl.border.left)
405 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500406 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
407 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409 cairo_translate(cr, 0, -iy);
410 frame_repaint(output->frame, cr);
411 cairo_destroy(cr);
412 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
413 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500414 cairo_image_surface_get_stride(output->gl.border.left) / 4,
415 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500416
417
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500418 if (!output->gl.border.right)
419 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500420 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
421 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500422 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500423 cairo_translate(cr, -(iwidth + ix), -iy);
424 frame_repaint(output->frame, cr);
425 cairo_destroy(cr);
426 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
427 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500428 cairo_image_surface_get_stride(output->gl.border.right) / 4,
429 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500430
431
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500432 if (!output->gl.border.bottom)
433 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500434 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
435 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500436 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500437 cairo_translate(cr, 0, -(iy + iheight));
438 frame_repaint(output->frame, cr);
439 cairo_destroy(cr);
440 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
441 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500442 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
443 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500444}
445
446static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200447wayland_output_start_repaint_loop(struct weston_output *output_base)
448{
Armin Krezović938dc522016-08-01 19:17:57 +0200449 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300450 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200451 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200452 struct wl_callback *callback;
453
454 /* If this is the initial frame, we need to attach a buffer so that
455 * the compositor can map the surface and include it in its render
456 * loop. If the surface doesn't end up in the render loop, the frame
457 * callback won't be invoked. The buffer is transparent and of the
458 * same size as the future real output buffer. */
459 if (output->parent.draw_initial_frame) {
460 output->parent.draw_initial_frame = 0;
461
462 draw_initial_frame(output);
463 }
464
465 callback = wl_surface_frame(output->parent.surface);
466 wl_callback_add_listener(callback, &frame_listener, output);
467 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300468 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200469}
470
David Herrmann1edf44c2013-10-22 17:11:26 +0200471static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500472wayland_output_repaint_gl(struct weston_output *output_base,
473 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400474{
Armin Krezović938dc522016-08-01 19:17:57 +0200475 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400476 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400477 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600478
Kristian Høgsberg33418202011-08-16 23:01:28 -0400479 callback = wl_surface_frame(output->parent.surface);
480 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100481
Jason Ekstrand7744f712013-10-27 22:24:55 -0500482 wayland_output_update_gl_border(output);
483
Pekka Paalanenbc106382012-10-10 12:49:31 +0300484 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200485
486 pixman_region32_subtract(&ec->primary_plane.damage,
487 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200488 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100489}
490
Matt Roper361d2ad2011-08-29 13:52:23 -0700491static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500492wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
493{
494 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
495 cairo_t *cr;
496
497 if (!buffer->output->frame || !buffer->frame_damaged)
498 return;
499
500 cr = cairo_create(buffer->c_surface);
501
502 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
503 fwidth = frame_width(buffer->output->frame);
504 fheight = frame_height(buffer->output->frame);
505
506 /* Set the clip so we don't unnecisaraly damage the surface */
507 cairo_move_to(cr, ix, iy);
508 cairo_rel_line_to(cr, iwidth, 0);
509 cairo_rel_line_to(cr, 0, iheight);
510 cairo_rel_line_to(cr, -iwidth, 0);
511 cairo_line_to(cr, ix, iy);
512 cairo_line_to(cr, 0, iy);
513 cairo_line_to(cr, 0, fheight);
514 cairo_line_to(cr, fwidth, fheight);
515 cairo_line_to(cr, fwidth, 0);
516 cairo_line_to(cr, 0, 0);
517 cairo_line_to(cr, 0, iy);
518 cairo_close_path(cr);
519 cairo_clip(cr);
520
521 /* Draw using a pattern so that the final result gets clipped */
522 cairo_push_group(cr);
523 frame_repaint(buffer->output->frame, cr);
524 cairo_pop_group_to_source(cr);
525 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
526 cairo_paint(cr);
527
528 cairo_destroy(cr);
529}
530
531static void
532wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
533{
534 pixman_region32_t damage;
535 pixman_box32_t *rects;
536 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
537 int i, n;
538
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500539 pixman_region32_init(&damage);
540 weston_transformed_region(sb->output->base.width,
541 sb->output->base.height,
542 sb->output->base.transform,
543 sb->output->base.current_scale,
544 &sb->damage, &damage);
545
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500546 if (sb->output->frame) {
547 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
548 fwidth = frame_width(sb->output->frame);
549 fheight = frame_height(sb->output->frame);
550
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500551 pixman_region32_translate(&damage, ix, iy);
552
553 if (sb->frame_damaged) {
554 pixman_region32_union_rect(&damage, &damage,
555 0, 0, fwidth, iy);
556 pixman_region32_union_rect(&damage, &damage,
557 0, iy, ix, iheight);
558 pixman_region32_union_rect(&damage, &damage,
559 ix + iwidth, iy,
560 fwidth - (ix + iwidth), iheight);
561 pixman_region32_union_rect(&damage, &damage,
562 0, iy + iheight,
563 fwidth, fheight - (iy + iheight));
564 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500565 }
566
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500567 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500568 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
569 for (i = 0; i < n; ++i)
570 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
571 rects[i].y1, rects[i].x2 - rects[i].x1,
572 rects[i].y2 - rects[i].y1);
573
574 if (sb->output->frame)
575 pixman_region32_fini(&damage);
576}
577
578static int
579wayland_output_repaint_pixman(struct weston_output *output_base,
580 pixman_region32_t *damage)
581{
Armin Krezović938dc522016-08-01 19:17:57 +0200582 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300583 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200584 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500585 struct wl_callback *callback;
586 struct wayland_shm_buffer *sb;
587
588 if (output->frame) {
589 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
590 wl_list_for_each(sb, &output->shm.buffers, link)
591 sb->frame_damaged = 1;
592 }
593
594 wl_list_for_each(sb, &output->shm.buffers, link)
595 pixman_region32_union(&sb->damage, &sb->damage, damage);
596
597 sb = wayland_output_get_shm_buffer(output);
598
599 wayland_output_update_shm_border(sb);
600 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300601 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500602
603 wayland_shm_buffer_attach(sb);
604
605 callback = wl_surface_frame(output->parent.surface);
606 wl_callback_add_listener(callback, &frame_listener, output);
607 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300608 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500609
610 pixman_region32_fini(&sb->damage);
611 pixman_region32_init(&sb->damage);
612 sb->frame_damaged = 0;
613
Giulio Camuffo954f1832014-10-11 18:27:30 +0300614 pixman_region32_subtract(&b->compositor->primary_plane.damage,
615 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500616 return 0;
617}
618
619static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500620wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700621{
Armin Krezović938dc522016-08-01 19:17:57 +0200622 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300623 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200624 to_wayland_backend(output->base.compositor);
Matt Roper361d2ad2011-08-29 13:52:23 -0700625
Giulio Camuffo954f1832014-10-11 18:27:30 +0300626 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500627 pixman_renderer_output_destroy(output_base);
628 } else {
629 gl_renderer->output_destroy(output_base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100630 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500631 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100632
Jason Ekstrand7744f712013-10-27 22:24:55 -0500633 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500634 if (output->parent.shell_surface)
635 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500636
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600637 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500638 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600639
640 cairo_surface_destroy(output->gl.border.top);
641 cairo_surface_destroy(output->gl.border.left);
642 cairo_surface_destroy(output->gl.border.right);
643 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500644
645 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700646 free(output);
647
648 return;
649}
650
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300651static const struct wl_shell_surface_listener shell_surface_listener;
652
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200653static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500654wayland_output_init_gl_renderer(struct wayland_output *output)
655{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500656 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500657
658 if (output->frame) {
659 fwidth = frame_width(output->frame);
660 fheight = frame_height(output->frame);
661 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500662 fwidth = output->base.current_mode->width;
663 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500664 }
665
666 output->gl.egl_window =
667 wl_egl_window_create(output->parent.surface,
668 fwidth, fheight);
669 if (!output->gl.egl_window) {
670 weston_log("failure to create wl_egl_window\n");
671 return -1;
672 }
673
674 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000675 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100676 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000677 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500678 NULL,
679 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500680 goto cleanup_window;
681
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500682 return 0;
683
684cleanup_window:
685 wl_egl_window_destroy(output->gl.egl_window);
686 return -1;
687}
688
689static int
690wayland_output_init_pixman_renderer(struct wayland_output *output)
691{
692 return pixman_renderer_output_create(&output->base);
693}
694
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600695static void
696wayland_output_resize_surface(struct wayland_output *output)
697{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300698 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200699 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600700 struct wayland_shm_buffer *buffer, *next;
701 int32_t ix, iy, iwidth, iheight;
702 int32_t width, height;
703 struct wl_region *region;
704
705 width = output->base.current_mode->width;
706 height = output->base.current_mode->height;
707
708 if (output->frame) {
709 frame_resize_inside(output->frame, width, height);
710
711 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300712 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600713 wl_region_add(region, ix, iy, iwidth, iheight);
714 wl_surface_set_input_region(output->parent.surface, region);
715 wl_region_destroy(region);
716
717 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300718 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600719 wl_region_add(region, ix, iy, iwidth, iheight);
720 wl_surface_set_opaque_region(output->parent.surface, region);
721 wl_region_destroy(region);
722
723 width = frame_width(output->frame);
724 height = frame_height(output->frame);
725 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300726 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600727 wl_region_add(region, 0, 0, width, height);
728 wl_surface_set_input_region(output->parent.surface, region);
729 wl_region_destroy(region);
730
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_opaque_region(output->parent.surface, region);
734 wl_region_destroy(region);
735 }
736
737 if (output->gl.egl_window) {
738 wl_egl_window_resize(output->gl.egl_window,
739 width, height, 0, 0);
740
741 /* These will need to be re-created due to the resize */
742 gl_renderer->output_set_border(&output->base,
743 GL_RENDERER_BORDER_TOP,
744 0, 0, 0, NULL);
745 cairo_surface_destroy(output->gl.border.top);
746 output->gl.border.top = NULL;
747 gl_renderer->output_set_border(&output->base,
748 GL_RENDERER_BORDER_LEFT,
749 0, 0, 0, NULL);
750 cairo_surface_destroy(output->gl.border.left);
751 output->gl.border.left = NULL;
752 gl_renderer->output_set_border(&output->base,
753 GL_RENDERER_BORDER_RIGHT,
754 0, 0, 0, NULL);
755 cairo_surface_destroy(output->gl.border.right);
756 output->gl.border.right = NULL;
757 gl_renderer->output_set_border(&output->base,
758 GL_RENDERER_BORDER_BOTTOM,
759 0, 0, 0, NULL);
760 cairo_surface_destroy(output->gl.border.bottom);
761 output->gl.border.bottom = NULL;
762 }
763
764 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500765 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600766 wayland_shm_buffer_destroy(buffer);
767 /* These will get thrown away when they get released */
768 wl_list_for_each(buffer, &output->shm.buffers, link)
769 buffer->output = NULL;
770}
771
772static int
773wayland_output_set_windowed(struct wayland_output *output)
774{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300775 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200776 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600777 int tlen;
778 char *title;
779
780 if (output->frame)
781 return 0;
782
783 if (output->name) {
784 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
785 title = malloc(tlen + 1);
786 if (!title)
787 return -1;
788
789 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
790 } else {
791 title = strdup(WINDOW_TITLE);
792 }
793
Giulio Camuffo954f1832014-10-11 18:27:30 +0300794 if (!b->theme) {
795 b->theme = theme_create();
796 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600797 free(title);
798 return -1;
799 }
800 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300801 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600802 FRAME_BUTTON_CLOSE, title);
803 free(title);
804 if (!output->frame)
805 return -1;
806
807 if (output->keyboard_count)
808 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
809
810 wayland_output_resize_surface(output);
811
812 wl_shell_surface_set_toplevel(output->parent.shell_surface);
813
814 return 0;
815}
816
817static void
818wayland_output_set_fullscreen(struct wayland_output *output,
819 enum wl_shell_surface_fullscreen_method method,
820 uint32_t framerate, struct wl_output *target)
821{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300822 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200823 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500824
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600825 if (output->frame) {
826 frame_destroy(output->frame);
827 output->frame = NULL;
828 }
829
830 wayland_output_resize_surface(output);
831
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500832 if (output->parent.shell_surface) {
833 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
834 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300835 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800836 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
837 output->parent.surface,
838 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500839 }
840}
841
842static struct weston_mode *
843wayland_output_choose_mode(struct wayland_output *output,
844 struct weston_mode *ref_mode)
845{
846 struct weston_mode *mode;
847
848 /* First look for an exact match */
849 wl_list_for_each(mode, &output->base.mode_list, link)
850 if (mode->width == ref_mode->width &&
851 mode->height == ref_mode->height &&
852 mode->refresh == ref_mode->refresh)
853 return mode;
854
855 /* If we can't find an exact match, ignore refresh and try again */
856 wl_list_for_each(mode, &output->base.mode_list, link)
857 if (mode->width == ref_mode->width &&
858 mode->height == ref_mode->height)
859 return mode;
860
861 /* Yeah, we failed */
862 return NULL;
863}
864
865enum mode_status {
866 MODE_STATUS_UNKNOWN,
867 MODE_STATUS_SUCCESS,
868 MODE_STATUS_FAIL,
869 MODE_STATUS_CANCEL,
870};
871
872static void
873mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800874 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500875{
876 enum mode_status *value = data;
877
878 printf("Mode switch successful\n");
879
880 *value = MODE_STATUS_SUCCESS;
881}
882
883static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800884mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500885{
886 enum mode_status *value = data;
887
888 printf("Mode switch failed\n");
889
890 *value = MODE_STATUS_FAIL;
891}
892
893static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800894mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500895{
896 enum mode_status *value = data;
897
898 printf("Mode switch cancelled\n");
899
900 *value = MODE_STATUS_CANCEL;
901}
902
Jonas Ådahl496adb32015-11-17 16:00:27 +0800903struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500904 mode_feedback_successful,
905 mode_feedback_failed,
906 mode_feedback_cancelled,
907};
908
909static int
910wayland_output_switch_mode(struct weston_output *output_base,
911 struct weston_mode *mode)
912{
Armin Krezović938dc522016-08-01 19:17:57 +0200913 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300914 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500915 struct wl_surface *old_surface;
916 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800917 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500918 enum mode_status mode_status;
919 int ret = 0;
920
921 if (output_base == NULL) {
922 weston_log("output is NULL.\n");
923 return -1;
924 }
925
926 if (mode == NULL) {
927 weston_log("mode is NULL.\n");
928 return -1;
929 }
930
Armin Krezović938dc522016-08-01 19:17:57 +0200931 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500932
Giulio Camuffo954f1832014-10-11 18:27:30 +0300933 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500934 return -1;
935
936 mode = wayland_output_choose_mode(output, mode);
937 if (mode == NULL)
938 return -1;
939
940 if (output->base.current_mode == mode)
941 return 0;
942
943 old_mode = output->base.current_mode;
944 old_surface = output->parent.surface;
945 output->base.current_mode = mode;
946 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300947 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500948 wl_surface_set_user_data(output->parent.surface, output);
949
950 /* Blow the old buffers because we changed size/surfaces */
951 wayland_output_resize_surface(output);
952
953 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800954 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
955 output->parent.surface,
956 output->parent.output,
957 mode->refresh);
958 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
959 &mode_feedback_listener,
960 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500961
962 /* This should kick-start things again */
963 output->parent.draw_initial_frame = 1;
964 wayland_output_start_repaint_loop(&output->base);
965
966 mode_status = MODE_STATUS_UNKNOWN;
967 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300968 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500969
Jonas Ådahl496adb32015-11-17 16:00:27 +0800970 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500971
972 if (mode_status == MODE_STATUS_FAIL) {
973 output->base.current_mode = old_mode;
974 wl_surface_destroy(output->parent.surface);
975 output->parent.surface = old_surface;
976 wayland_output_resize_surface(output);
977
978 return -1;
979 }
980
981 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
982 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
983
Giulio Camuffo954f1832014-10-11 18:27:30 +0300984 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500985 pixman_renderer_output_destroy(output_base);
986 if (wayland_output_init_pixman_renderer(output) < 0)
987 goto err_output;
988 } else {
989 gl_renderer->output_destroy(output_base);
990 wl_egl_window_destroy(output->gl.egl_window);
991 if (wayland_output_init_gl_renderer(output) < 0)
992 goto err_output;
993 }
994 wl_surface_destroy(old_surface);
995
996 weston_output_schedule_repaint(&output->base);
997
998 return 0;
999
1000err_output:
1001 /* XXX */
1002 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001003}
1004
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001005static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001006wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001008 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001009{
1010 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001011 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001012
1013 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1014 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001015
Peter Huttererf3d62272013-08-08 11:57:05 +10001016 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001017 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001018 return NULL;
1019
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001020 output->name = name ? strdup(name) : NULL;
Yong Bakose51647f2016-06-05 11:01:17 -05001021 output->base.make = "wayland";
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001022 output->base.model = "none";
1023
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001024 output_width = width * scale;
1025 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001026
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001027 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001028 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001029 if (!output->parent.surface)
1030 goto err_name;
1031 wl_surface_set_user_data(output->parent.surface, output);
1032
1033 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001034
Giulio Camuffo954f1832014-10-11 18:27:30 +03001035 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001036 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001037 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001038 output->parent.surface);
1039 if (!output->parent.shell_surface)
1040 goto err_surface;
1041 wl_shell_surface_add_listener(output->parent.shell_surface,
1042 &shell_surface_listener, output);
1043 }
1044
Giulio Camuffo954f1832014-10-11 18:27:30 +03001045 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001046 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1047 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001048 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001049 if (!width)
1050 output_width = output->parent.configure_width;
1051 if (!height)
1052 output_height = output->parent.configure_height;
1053 }
1054
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001055 output->mode.flags =
1056 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001057 output->mode.width = output_width;
1058 output->mode.height = output_height;
1059 output->mode.refresh = 60000;
1060 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001061 wl_list_init(&output->base.mode_list);
1062 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001063 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001064
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001065 wl_list_init(&output->shm.buffers);
1066 wl_list_init(&output->shm.free_buffers);
1067
Giulio Camuffo954f1832014-10-11 18:27:30 +03001068 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001069 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001070
Giulio Camuffo954f1832014-10-11 18:27:30 +03001071 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001072 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001073 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001074 output->base.repaint = wayland_output_repaint_pixman;
1075 } else {
1076 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001077 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001078 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001079 }
1080
Jonas Ådahle5a12252013-04-05 23:07:11 +02001081 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001082 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001083 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001084 output->base.set_backlight = NULL;
1085 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001086 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001087
Giulio Camuffo954f1832014-10-11 18:27:30 +03001088 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001089
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001090 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001091
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001092err_output:
1093 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001094 if (output->parent.shell_surface)
1095 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001096err_surface:
1097 wl_surface_destroy(output->parent.surface);
1098err_name:
1099 free(output->name);
1100
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001101 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001102 free(output);
1103
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001104 return NULL;
1105}
1106
Benoit Gschwind830b7882016-04-28 20:33:12 +02001107static struct wayland_output *
1108wayland_output_create_for_config(struct wayland_backend *b,
1109 struct weston_wayland_backend_output_config *oc,
1110 int fullscreen, int32_t x, int32_t y)
1111{
1112 struct wayland_output *output;
1113
1114 output = wayland_output_create(b, x, y, oc->width, oc->height, oc->name,
1115 fullscreen, oc->transform, oc->scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001116
1117 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001118}
1119
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001120static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001121wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001122 struct wayland_parent_output *poutput)
1123{
1124 struct wayland_output *output;
1125 struct weston_mode *mode;
1126 int32_t x;
1127
1128 if (poutput->current_mode) {
1129 mode = poutput->current_mode;
1130 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001131 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001132 } else if (!wl_list_empty(&poutput->mode_list)) {
1133 mode = container_of(poutput->mode_list.next,
1134 struct weston_mode, link);
1135 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001136 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001137 return NULL;
1138 }
1139
Giulio Camuffo954f1832014-10-11 18:27:30 +03001140 if (!wl_list_empty(&b->compositor->output_list)) {
1141 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001142 struct wayland_output, base.link);
1143 x = output->base.x + output->base.current_mode->width;
1144 } else {
1145 x = 0;
1146 }
1147
Giulio Camuffo954f1832014-10-11 18:27:30 +03001148 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001149 NULL, 0,
1150 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1151 if (!output)
1152 return NULL;
1153
1154 output->parent.output = poutput->global;
1155
1156 output->base.make = poutput->physical.make;
1157 output->base.model = poutput->physical.model;
1158 wl_list_init(&output->base.mode_list);
1159 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1160 wl_list_init(&poutput->mode_list);
1161
1162 wayland_output_set_fullscreen(output,
1163 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1164 mode->refresh, poutput->global);
1165
1166 if (output->parent.shell_surface) {
1167 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1168 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1169 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001170 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001171 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1172 output->parent.surface,
1173 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1174 poutput->global);
1175 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1176 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1177 output->parent.surface,
1178 poutput->global,
1179 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001180 }
1181
1182 return output;
1183}
1184
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001185static void
1186shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1187 uint32_t serial)
1188{
1189 wl_shell_surface_pong(shell_surface, serial);
1190}
1191
1192static void
1193shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1194 uint32_t edges, int32_t width, int32_t height)
1195{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001196 struct wayland_output *output = data;
1197
1198 output->parent.configure_width = width;
1199 output->parent.configure_height = height;
1200
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001201 /* FIXME: implement resizing */
1202}
1203
1204static void
1205shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1206{
1207}
1208
1209static const struct wl_shell_surface_listener shell_surface_listener = {
1210 shell_surface_ping,
1211 shell_surface_configure,
1212 shell_surface_popup_done
1213};
1214
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001215/* Events received from the wayland-server this compositor is client of: */
1216
Jason Ekstrand7744f712013-10-27 22:24:55 -05001217/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001218static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001219input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001220{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001221
Jason Ekstrand7744f712013-10-27 22:24:55 -05001222 struct wl_buffer *buffer;
1223 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001224
Giulio Camuffo954f1832014-10-11 18:27:30 +03001225 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001226 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001227
Giulio Camuffo954f1832014-10-11 18:27:30 +03001228 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001229 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001230 if (!buffer)
1231 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001232
1233 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1234 input->parent.cursor.surface,
1235 image->hotspot_x, image->hotspot_y);
1236
1237 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1238 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1239 image->width, image->height);
1240 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001241}
1242
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001243static void
Daniel Stone37816df2012-05-16 18:45:18 +01001244input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1245 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001246 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001247{
1248 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001249 int32_t fx, fy;
1250 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001251 double x, y;
1252
1253 x = wl_fixed_to_double(fixed_x);
1254 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001255
Daniel Stone50692802012-06-22 13:21:41 +01001256 /* XXX: If we get a modifier event immediately before the focus,
1257 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001258 input->enter_serial = serial;
1259 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001260
1261 if (input->output->frame) {
1262 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001263 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001264 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001265 x -= fx;
1266 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001267
1268 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1269 weston_output_schedule_repaint(&input->output->base);
1270 } else {
1271 location = THEME_LOCATION_CLIENT_AREA;
1272 }
1273
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001274 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001275
1276 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001277 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001278 notify_pointer_focus(&input->base, &input->output->base, x, y);
1279 wl_pointer_set_cursor(input->parent.pointer,
1280 input->enter_serial, NULL, 0, 0);
1281 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001282 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001283 notify_pointer_focus(&input->base, NULL, 0, 0);
1284 input_set_cursor(input);
1285 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001286}
1287
1288static void
Daniel Stone37816df2012-05-16 18:45:18 +01001289input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1290 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001291{
1292 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001293
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001294 if (!input->output)
1295 return;
1296
Jason Ekstrand7744f712013-10-27 22:24:55 -05001297 if (input->output->frame) {
1298 frame_pointer_leave(input->output->frame, input);
1299
1300 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1301 weston_output_schedule_repaint(&input->output->base);
1302 }
1303
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001304 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001305 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001306 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001307}
1308
1309static void
Daniel Stone37816df2012-05-16 18:45:18 +01001310input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001311 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001312{
1313 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001314 int32_t fx, fy;
1315 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001316 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001317 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001318
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001319 if (!input->output)
1320 return;
1321
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001322 x = wl_fixed_to_double(fixed_x);
1323 y = wl_fixed_to_double(fixed_y);
1324
Jason Ekstrand7744f712013-10-27 22:24:55 -05001325 if (input->output->frame) {
1326 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001327 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001328 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001329 x -= fx;
1330 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001331
1332 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1333 weston_output_schedule_repaint(&input->output->base);
1334 } else {
1335 location = THEME_LOCATION_CLIENT_AREA;
1336 }
1337
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001338 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001339
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001340 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001341 input_set_cursor(input);
1342 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001343 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001344 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001345 } else if (!input->has_focus &&
1346 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001347 wl_pointer_set_cursor(input->parent.pointer,
1348 input->enter_serial, NULL, 0, 0);
1349 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001350 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001351 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001352 }
1353
Peter Hutterer87743e92016-01-18 16:38:22 +10001354 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001355 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001356 want_frame = true;
1357 }
1358
1359 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1360 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001361}
1362
1363static void
1364input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001365 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001366 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001367{
1368 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001369 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001370
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001371 if (!input->output)
1372 return;
1373
Jason Ekstrand7744f712013-10-27 22:24:55 -05001374 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001375 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001376 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001377
1378 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1379
1380 wl_shell_surface_move(input->output->parent.shell_surface,
1381 input->parent.seat, serial);
1382 frame_status_clear(input->output->frame,
1383 FRAME_STATUS_MOVE);
1384 return;
1385 }
1386
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001387 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1388 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001389 input->output = NULL;
1390 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001391
Giulio Camuffo954f1832014-10-11 18:27:30 +03001392 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001393 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001394
1395 return;
1396 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001397
1398 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1399 weston_output_schedule_repaint(&input->output->base);
1400 } else {
1401 location = THEME_LOCATION_CLIENT_AREA;
1402 }
1403
Peter Hutterer87743e92016-01-18 16:38:22 +10001404 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001405 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001406 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1407 notify_pointer_frame(&input->base);
1408 }
Daniel Stone37816df2012-05-16 18:45:18 +01001409}
1410
1411static void
1412input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001413 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001414{
1415 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001416 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001417
Peter Hutterer89b6a492016-01-18 15:58:17 +10001418 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001419 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001420
Peter Hutterer87743e92016-01-18 16:38:22 +10001421 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1422 input->vert.has_discrete) {
1423 weston_event.has_discrete = true;
1424 weston_event.discrete = input->vert.discrete;
1425 input->vert.has_discrete = false;
1426 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1427 input->horiz.has_discrete) {
1428 weston_event.has_discrete = true;
1429 weston_event.discrete = input->horiz.discrete;
1430 input->horiz.has_discrete = false;
1431 }
1432
Peter Hutterer89b6a492016-01-18 15:58:17 +10001433 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001434
1435 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1436 notify_pointer_frame(&input->base);
1437}
1438
1439static void
1440input_handle_frame(void *data, struct wl_pointer *pointer)
1441{
1442 struct wayland_input *input = data;
1443
1444 notify_pointer_frame(&input->base);
1445}
1446
1447static void
1448input_handle_axis_source(void *data, struct wl_pointer *pointer,
1449 uint32_t source)
1450{
1451 struct wayland_input *input = data;
1452
1453 notify_axis_source(&input->base, source);
1454}
1455
1456static void
1457input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1458 uint32_t time, uint32_t axis)
1459{
1460 struct wayland_input *input = data;
1461 struct weston_pointer_axis_event weston_event;
1462
1463 weston_event.axis = axis;
1464 weston_event.value = 0;
1465
1466 notify_axis(&input->base, time, &weston_event);
1467}
1468
1469static void
1470input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1471 uint32_t axis, int32_t discrete)
1472{
1473 struct wayland_input *input = data;
1474
1475 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1476 input->vert.has_discrete = true;
1477 input->vert.discrete = discrete;
1478 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1479 input->horiz.has_discrete = true;
1480 input->horiz.discrete = discrete;
1481 }
Daniel Stone37816df2012-05-16 18:45:18 +01001482}
1483
1484static const struct wl_pointer_listener pointer_listener = {
1485 input_handle_pointer_enter,
1486 input_handle_pointer_leave,
1487 input_handle_motion,
1488 input_handle_button,
1489 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001490 input_handle_frame,
1491 input_handle_axis_source,
1492 input_handle_axis_stop,
1493 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001494};
1495
1496static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001497input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1498 int fd, uint32_t size)
1499{
1500 struct wayland_input *input = data;
1501 struct xkb_keymap *keymap;
1502 char *map_str;
1503
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001504 if (!data) {
1505 close(fd);
1506 return;
1507 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001508
1509 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1510 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1511 if (map_str == MAP_FAILED) {
1512 weston_log("mmap failed: %m\n");
1513 goto error;
1514 }
1515
Giulio Camuffo954f1832014-10-11 18:27:30 +03001516 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001517 map_str,
1518 XKB_KEYMAP_FORMAT_TEXT_V1,
1519 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001520 munmap(map_str, size);
1521
1522 if (!keymap) {
1523 weston_log("failed to compile keymap\n");
1524 goto error;
1525 }
1526
1527 input->keyboard_state_update = STATE_UPDATE_NONE;
1528 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1529 weston_log("No keymap provided; falling back to defalt\n");
1530 keymap = NULL;
1531 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1532 } else {
1533 weston_log("Invalid keymap\n");
1534 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001535 }
1536
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001537 close(fd);
1538
Derek Foreman1281a362015-07-31 16:55:32 -05001539 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001540 weston_seat_update_keymap(&input->base, keymap);
1541 else
1542 weston_seat_init_keyboard(&input->base, keymap);
1543
Ran Benitac9c74152014-08-19 23:59:52 +03001544 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001545
1546 return;
1547
1548error:
1549 wl_keyboard_release(input->parent.keyboard);
1550 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001551}
1552
1553static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001554input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001555 struct wl_keyboard *keyboard,
1556 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001557 struct wl_surface *surface,
1558 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001559{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001560 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001561 struct wayland_output *focus;
1562
1563 focus = input->keyboard_focus;
1564 if (focus) {
1565 /* This shouldn't happen */
1566 focus->keyboard_count--;
1567 if (!focus->keyboard_count && focus->frame)
1568 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1569 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1570 weston_output_schedule_repaint(&focus->base);
1571 }
1572
1573 input->keyboard_focus = wl_surface_get_user_data(surface);
1574 input->keyboard_focus->keyboard_count++;
1575
1576 focus = input->keyboard_focus;
1577 if (focus->frame) {
1578 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1579 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1580 weston_output_schedule_repaint(&focus->base);
1581 }
1582
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001583
Daniel Stone50692802012-06-22 13:21:41 +01001584 /* XXX: If we get a modifier event immediately before the focus,
1585 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001587 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001588}
1589
1590static void
1591input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001592 struct wl_keyboard *keyboard,
1593 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001594 struct wl_surface *surface)
1595{
1596 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001597 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001598
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001599 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001600
1601 focus = input->keyboard_focus;
1602 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001603 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001604
1605 focus->keyboard_count--;
1606 if (!focus->keyboard_count && focus->frame) {
1607 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1608 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1609 weston_output_schedule_repaint(&focus->base);
1610 }
1611
1612 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001613}
1614
Daniel Stone37816df2012-05-16 18:45:18 +01001615static void
1616input_handle_key(void *data, struct wl_keyboard *keyboard,
1617 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1618{
1619 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001620
Daniel Stone50692802012-06-22 13:21:41 +01001621 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001622 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001623 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001624 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001625 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001626}
1627
Daniel Stone351eb612012-05-31 15:27:47 -04001628static void
Derek Foreman1281a362015-07-31 16:55:32 -05001629input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001630 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001631 uint32_t mods_latched, uint32_t mods_locked,
1632 uint32_t group)
1633{
Derek Foreman1281a362015-07-31 16:55:32 -05001634 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001635 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001636 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001637 uint32_t serial_out;
1638
1639 /* If we get a key event followed by a modifier event with the
1640 * same serial number, then we try to preserve those semantics by
1641 * reusing the same serial number on the way out too. */
1642 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001643 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001644 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001645 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001646
Derek Foreman1281a362015-07-31 16:55:32 -05001647 keyboard = weston_seat_get_keyboard(&input->base);
1648 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001649 mods_depressed, mods_latched,
1650 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001651 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001652}
1653
Jonny Lamb497994a2014-08-12 14:58:26 +02001654static void
1655input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1656 int32_t rate, int32_t delay)
1657{
1658 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001659 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001660
Giulio Camuffo954f1832014-10-11 18:27:30 +03001661 b->compositor->kb_repeat_rate = rate;
1662 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001663}
1664
Daniel Stone37816df2012-05-16 18:45:18 +01001665static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001666 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001667 input_handle_keyboard_enter,
1668 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001669 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001670 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001671 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001672};
1673
1674static void
Derek Foreman748c6952015-11-06 15:56:10 -06001675input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1676 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001677 struct wl_surface *surface, int32_t id,
1678 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001679{
1680 struct wayland_input *input = data;
1681 struct wayland_output *output;
1682 enum theme_location location;
1683 bool first_touch;
1684 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001685 double x, y;
1686
1687 x = wl_fixed_to_double(fixed_x);
1688 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001689
1690 first_touch = (input->touch_points == 0);
1691 input->touch_points++;
1692
1693 input->touch_focus = wl_surface_get_user_data(surface);
1694 output = input->touch_focus;
1695 if (!first_touch && !input->touch_active)
1696 return;
1697
1698 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001699 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001700
1701 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001702 x -= fx;
1703 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001704
1705 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1706 weston_output_schedule_repaint(&output->base);
1707
1708 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1709 input->touch_points--;
1710 wl_shell_surface_move(output->parent.shell_surface,
1711 input->parent.seat, serial);
1712 frame_status_clear(output->frame,
1713 FRAME_STATUS_MOVE);
1714 return;
1715 }
1716
1717 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1718 return;
1719 }
1720
1721 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1722
1723 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1724 input->touch_active = true;
1725}
1726
1727static void
1728input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1729 uint32_t serial, uint32_t time, int32_t id)
1730{
1731 struct wayland_input *input = data;
1732 struct wayland_output *output = input->touch_focus;
1733 bool active = input->touch_active;
1734
1735 input->touch_points--;
1736 if (input->touch_points == 0) {
1737 input->touch_focus = NULL;
1738 input->touch_active = false;
1739 }
1740
1741 if (!output)
1742 return;
1743
1744 if (output->frame) {
1745 frame_touch_up(output->frame, input, id);
1746
1747 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1748 wayland_output_destroy(&output->base);
1749 input->touch_focus = NULL;
1750 input->keyboard_focus = NULL;
1751 if (wl_list_empty(&input->backend->compositor->output_list))
1752 weston_compositor_exit(input->backend->compositor);
1753
1754 return;
1755 }
1756 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1757 weston_output_schedule_repaint(&output->base);
1758 }
1759
1760 if (active)
1761 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1762}
1763
1764static void
1765input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001766 uint32_t time, int32_t id,
1767 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001768{
1769 struct wayland_input *input = data;
1770 struct wayland_output *output = input->touch_focus;
1771 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001772 double x, y;
1773
1774 x = wl_fixed_to_double(fixed_x);
1775 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001776
1777 if (!output || !input->touch_active)
1778 return;
1779
1780 if (output->frame) {
1781 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001782 x -= fx;
1783 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001784 }
1785
1786 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1787
1788 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1789}
1790
1791static void
1792input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1793{
1794 struct wayland_input *input = data;
1795
1796 if (!input->touch_focus || !input->touch_active)
1797 return;
1798
1799 notify_touch_frame(&input->base);
1800}
1801
1802static void
1803input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1804{
1805 struct wayland_input *input = data;
1806
1807 if (!input->touch_focus || !input->touch_active)
1808 return;
1809
1810 notify_touch_cancel(&input->base);
1811}
1812
1813static const struct wl_touch_listener touch_listener = {
1814 input_handle_touch_down,
1815 input_handle_touch_up,
1816 input_handle_touch_motion,
1817 input_handle_touch_frame,
1818 input_handle_touch_cancel,
1819};
1820
1821
1822static void
Daniel Stone37816df2012-05-16 18:45:18 +01001823input_handle_capabilities(void *data, struct wl_seat *seat,
1824 enum wl_seat_capability caps)
1825{
1826 struct wayland_input *input = data;
1827
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001828 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1829 input->parent.pointer = wl_seat_get_pointer(seat);
1830 wl_pointer_set_user_data(input->parent.pointer, input);
1831 wl_pointer_add_listener(input->parent.pointer,
1832 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001833 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001834 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001835 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1836 wl_pointer_release(input->parent.pointer);
1837 else
1838 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001839 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001840 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001841 }
1842
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001843 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1844 input->parent.keyboard = wl_seat_get_keyboard(seat);
1845 wl_keyboard_set_user_data(input->parent.keyboard, input);
1846 wl_keyboard_add_listener(input->parent.keyboard,
1847 &keyboard_listener, input);
1848 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001849 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1850 wl_keyboard_release(input->parent.keyboard);
1851 else
1852 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001853 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001854 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001855 }
Derek Foreman748c6952015-11-06 15:56:10 -06001856
1857 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1858 input->parent.touch = wl_seat_get_touch(seat);
1859 wl_touch_set_user_data(input->parent.touch, input);
1860 wl_touch_add_listener(input->parent.touch,
1861 &touch_listener, input);
1862 weston_seat_init_touch(&input->base);
1863 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1864 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1865 wl_touch_release(input->parent.touch);
1866 else
1867 wl_touch_destroy(input->parent.touch);
1868 input->parent.touch = NULL;
1869 weston_seat_release_touch(&input->base);
1870 }
Daniel Stone37816df2012-05-16 18:45:18 +01001871}
1872
Jonny Lamb497994a2014-08-12 14:58:26 +02001873static void
1874input_handle_name(void *data, struct wl_seat *seat,
1875 const char *name)
1876{
1877}
1878
Daniel Stone37816df2012-05-16 18:45:18 +01001879static const struct wl_seat_listener seat_listener = {
1880 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001881 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001882};
1883
1884static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001885display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001886{
1887 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001888 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001889
Peter Huttererf3d62272013-08-08 11:57:05 +10001890 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001891 if (input == NULL)
1892 return;
1893
Giulio Camuffo954f1832014-10-11 18:27:30 +03001894 weston_seat_init(&input->base, b->compositor, "default");
1895 input->backend = b;
1896 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001897 &wl_seat_interface, version);
1898 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001899 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001900
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001901 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1902 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001903
1904 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001905 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001906
1907 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1908 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001909}
1910
1911static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001912wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1913 int32_t x, int32_t y,
1914 int32_t physical_width, int32_t physical_height,
1915 int32_t subpixel, const char *make,
1916 const char *model, int32_t transform)
1917{
1918 struct wayland_parent_output *output = data;
1919
1920 output->x = x;
1921 output->y = y;
1922 output->physical.width = physical_width;
1923 output->physical.height = physical_height;
1924 output->physical.subpixel = subpixel;
1925
1926 free(output->physical.make);
1927 output->physical.make = strdup(make);
1928 free(output->physical.model);
1929 output->physical.model = strdup(model);
1930
1931 output->transform = transform;
1932}
1933
1934static struct weston_mode *
1935find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1936{
1937 struct weston_mode *mode;
1938
1939 wl_list_for_each(mode, list, link) {
1940 if (mode->width == width && mode->height == height &&
1941 mode->refresh == refresh)
1942 return mode;
1943 }
1944
1945 mode = zalloc(sizeof *mode);
1946 if (!mode)
1947 return NULL;
1948
1949 mode->width = width;
1950 mode->height = height;
1951 mode->refresh = refresh;
1952 wl_list_insert(list, &mode->link);
1953
1954 return mode;
1955}
1956
1957static void
1958wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1959 uint32_t flags, int32_t width, int32_t height,
1960 int32_t refresh)
1961{
1962 struct wayland_parent_output *output = data;
1963 struct weston_mode *mode;
1964
1965 if (output->output) {
1966 mode = find_mode(&output->output->base.mode_list,
1967 width, height, refresh);
1968 if (!mode)
1969 return;
1970 mode->flags = flags;
1971 /* Do a mode-switch on current mode change? */
1972 } else {
1973 mode = find_mode(&output->mode_list, width, height, refresh);
1974 if (!mode)
1975 return;
1976 mode->flags = flags;
1977 if (flags & WL_OUTPUT_MODE_CURRENT)
1978 output->current_mode = mode;
1979 if (flags & WL_OUTPUT_MODE_PREFERRED)
1980 output->preferred_mode = mode;
1981 }
1982}
1983
1984static const struct wl_output_listener output_listener = {
1985 wayland_parent_output_geometry,
1986 wayland_parent_output_mode
1987};
1988
1989static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001990wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001991{
1992 struct wayland_parent_output *output;
1993
1994 output = zalloc(sizeof *output);
1995 if (!output)
1996 return;
1997
1998 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001999 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002000 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002001 if (!output->global) {
2002 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002003 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002004 }
2005
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002006 wl_output_add_listener(output->global, &output_listener, output);
2007
2008 output->scale = 0;
2009 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2010 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2011 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002012 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002013
Giulio Camuffo954f1832014-10-11 18:27:30 +03002014 if (b->sprawl_across_outputs) {
2015 wl_display_roundtrip(b->parent.wl_display);
2016 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002017 }
2018}
2019
2020static void
2021wayland_parent_output_destroy(struct wayland_parent_output *output)
2022{
2023 struct weston_mode *mode, *next;
2024
2025 if (output->output)
2026 wayland_output_destroy(&output->output->base);
2027
2028 wl_output_destroy(output->global);
2029 free(output->physical.make);
2030 free(output->physical.model);
2031
2032 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2033 wl_list_remove(&mode->link);
2034 free(mode);
2035 }
2036}
2037
2038static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002039registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2040 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002041{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002042 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002043
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002044 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002046 wl_registry_bind(registry, name,
2047 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002048 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002049 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002050 wl_registry_bind(registry, name,
2051 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002052 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002053 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002054 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002055 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002056 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002057 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002058 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002059 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002060 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002061 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002062 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002063 }
2064}
2065
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002066static void
2067registry_handle_global_remove(void *data, struct wl_registry *registry,
2068 uint32_t name)
2069{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002070 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002071 struct wayland_parent_output *output;
2072
Giulio Camuffo954f1832014-10-11 18:27:30 +03002073 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002074 if (output->id == name)
2075 wayland_parent_output_destroy(output);
2076}
2077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002078static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002079 registry_handle_global,
2080 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002081};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002082
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002083static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002084wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002085{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002086 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002087 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002088
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002089 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002090 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002091 return 0;
2092 }
2093
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002094 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002095 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002096 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002097 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002098
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002099 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002100 count = wl_display_dispatch_pending(b->parent.wl_display);
2101 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002102 }
2103
2104 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002105}
2106
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002107static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002108wayland_restore(struct weston_compositor *ec)
2109{
2110}
2111
2112static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002113wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002114{
Armin Krezović938dc522016-08-01 19:17:57 +02002115 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002116
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002117 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002118
Giulio Camuffo954f1832014-10-11 18:27:30 +03002119 if (b->parent.shm)
2120 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002121
Giulio Camuffo954f1832014-10-11 18:27:30 +03002122 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002123}
2124
Jason Ekstrand7744f712013-10-27 22:24:55 -05002125static const char *left_ptrs[] = {
2126 "left_ptr",
2127 "default",
2128 "top_left_arrow",
2129 "left-arrow"
2130};
2131
2132static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002133create_cursor(struct wayland_backend *b,
2134 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002135{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002136 unsigned int i;
2137
Benoit Gschwind244ff792016-04-28 20:33:11 +02002138 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2139 config->cursor_size,
2140 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002141 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002142 fprintf(stderr, "could not load cursor theme\n");
2143 return;
2144 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002145
Giulio Camuffo954f1832014-10-11 18:27:30 +03002146 b->cursor = NULL;
2147 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2148 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002149 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002150 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002151 fprintf(stderr, "could not load left cursor\n");
2152 return;
2153 }
2154}
2155
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002156static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002157fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2158 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002159{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002160 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002161 struct wayland_input *input = NULL;
2162
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002164 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002165 break;
2166
2167 if (!input || !input->output)
2168 return;
2169
2170 if (input->output->frame)
2171 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2172 else
2173 wayland_output_set_windowed(input->output);
2174
2175 weston_output_schedule_repaint(&input->output->base);
2176}
2177
Giulio Camuffo954f1832014-10-11 18:27:30 +03002178static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002179wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002180 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002181{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002182 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002183 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002184 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002185
Giulio Camuffo954f1832014-10-11 18:27:30 +03002186 b = zalloc(sizeof *b);
2187 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002188 return NULL;
2189
Giulio Camuffo954f1832014-10-11 18:27:30 +03002190 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002191 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002192 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002193
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002194 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002195 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002196 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002197 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002198 }
2199
Giulio Camuffo954f1832014-10-11 18:27:30 +03002200 wl_list_init(&b->parent.output_list);
2201 wl_list_init(&b->input_list);
2202 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2203 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2204 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002205
Benoit Gschwind244ff792016-04-28 20:33:11 +02002206 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002207
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002208 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002209
Giulio Camuffo954f1832014-10-11 18:27:30 +03002210 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002211 gl_renderer = weston_load_module("gl-renderer.so",
2212 "gl_renderer_interface");
2213 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002214 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002215 }
2216
Giulio Camuffo954f1832014-10-11 18:27:30 +03002217 if (!b->use_pixman) {
2218 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002219 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002220 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002221 gl_renderer->alpha_attribs,
2222 NULL,
2223 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002224 weston_log("Failed to initialize the GL renderer; "
2225 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002226 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002227 }
2228 }
2229
Giulio Camuffo954f1832014-10-11 18:27:30 +03002230 if (b->use_pixman) {
2231 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002232 weston_log("Failed to initialize pixman renderer\n");
2233 goto err_display;
2234 }
2235 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002236
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237 b->base.destroy = wayland_destroy;
2238 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002239
Giulio Camuffo954f1832014-10-11 18:27:30 +03002240 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002241
Giulio Camuffo954f1832014-10-11 18:27:30 +03002242 fd = wl_display_get_fd(b->parent.wl_display);
2243 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002244 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002245 wayland_backend_handle_event, b);
2246 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002247 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002248
Giulio Camuffo954f1832014-10-11 18:27:30 +03002249 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002250
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002251 if (compositor->renderer->import_dmabuf) {
2252 if (linux_dmabuf_setup(compositor) < 0)
2253 weston_log("Error: initializing dmabuf "
2254 "support failed.\n");
2255 }
2256
Giulio Camuffo954f1832014-10-11 18:27:30 +03002257 compositor->backend = &b->base;
2258 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002259err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002260 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002261err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002262 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002263 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002264 return NULL;
2265}
2266
2267static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002268wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002269{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002270 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002271
Giulio Camuffo954f1832014-10-11 18:27:30 +03002272 if (b->theme)
2273 theme_destroy(b->theme);
2274 if (b->frame_device)
2275 cairo_device_destroy(b->frame_device);
2276 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002277
Giulio Camuffo954f1832014-10-11 18:27:30 +03002278 weston_compositor_shutdown(b->compositor);
2279 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002280}
2281
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002282static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002283config_init_to_defaults(struct weston_wayland_backend_config *config)
2284{
2285}
2286
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002287WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002288backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002289 struct weston_backend_config *config_base)
2290{
2291 struct wayland_backend *b;
2292 struct wayland_output *output;
2293 struct wayland_parent_output *poutput;
2294 struct weston_wayland_backend_config new_config;
2295 int x, count;
2296
Benoit Gschwinde091b452016-05-10 22:47:48 +02002297 if (config_base == NULL ||
2298 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2299 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2300 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002301 return -1;
2302 }
2303
2304 config_init_to_defaults(&new_config);
2305 memcpy(&new_config, config_base, config_base->struct_size);
2306
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002307 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002308
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 if (!b)
2310 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002311
Benoit Gschwind37a68072016-04-28 20:33:08 +02002312 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 b->sprawl_across_outputs = 1;
2314 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002315
Giulio Camuffo954f1832014-10-11 18:27:30 +03002316 wl_list_for_each(poutput, &b->parent.output_list, link)
2317 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002318
Giulio Camuffo954f1832014-10-11 18:27:30 +03002319 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002320 }
2321
Benoit Gschwind37a68072016-04-28 20:33:08 +02002322 if (new_config.fullscreen) {
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002323 if (new_config.num_outputs != 1 || !new_config.outputs)
2324 goto err_outputs;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002325
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002326 output = wayland_output_create_for_config(b,
2327 &new_config.outputs[0],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002328 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002329 if (!output)
2330 goto err_outputs;
2331
Axel Davydd8b88d2013-11-17 21:34:16 +01002332 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002333 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002334 }
2335
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002336 x = 0;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002337 for (count = 0; count < new_config.num_outputs; ++count) {
2338 output = wayland_output_create_for_config(b, &new_config.outputs[count],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002339 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002340 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002341 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002342 if (wayland_output_set_windowed(output))
2343 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002344
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002345 x += output->base.width;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002346 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002347
Giulio Camuffo954f1832014-10-11 18:27:30 +03002348 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002349 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002350 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002351 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002352
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002353err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002354 wayland_backend_destroy(b);
2355 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002356}