blob: c223baa3db26557681369b3fd651c81d4154ee91 [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,
1366 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001367{
1368 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001369 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001370 enum frame_button_state fstate;
1371 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001372
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001373 if (!input->output)
1374 return;
1375
Jason Ekstrand7744f712013-10-27 22:24:55 -05001376 if (input->output->frame) {
1377 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1378 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1379
1380 location = frame_pointer_button(input->output->frame, input,
1381 button, fstate);
1382
1383 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1384
1385 wl_shell_surface_move(input->output->parent.shell_surface,
1386 input->parent.seat, serial);
1387 frame_status_clear(input->output->frame,
1388 FRAME_STATUS_MOVE);
1389 return;
1390 }
1391
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001392 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1393 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001394 input->output = NULL;
1395 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001396
Giulio Camuffo954f1832014-10-11 18:27:30 +03001397 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001398 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001399
1400 return;
1401 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001402
1403 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1404 weston_output_schedule_repaint(&input->output->base);
1405 } else {
1406 location = THEME_LOCATION_CLIENT_AREA;
1407 }
1408
Peter Hutterer87743e92016-01-18 16:38:22 +10001409 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001410 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001411 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1412 notify_pointer_frame(&input->base);
1413 }
Daniel Stone37816df2012-05-16 18:45:18 +01001414}
1415
1416static void
1417input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001418 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001419{
1420 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001421 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001422
Peter Hutterer89b6a492016-01-18 15:58:17 +10001423 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001424 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001425
Peter Hutterer87743e92016-01-18 16:38:22 +10001426 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1427 input->vert.has_discrete) {
1428 weston_event.has_discrete = true;
1429 weston_event.discrete = input->vert.discrete;
1430 input->vert.has_discrete = false;
1431 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1432 input->horiz.has_discrete) {
1433 weston_event.has_discrete = true;
1434 weston_event.discrete = input->horiz.discrete;
1435 input->horiz.has_discrete = false;
1436 }
1437
Peter Hutterer89b6a492016-01-18 15:58:17 +10001438 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001439
1440 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1441 notify_pointer_frame(&input->base);
1442}
1443
1444static void
1445input_handle_frame(void *data, struct wl_pointer *pointer)
1446{
1447 struct wayland_input *input = data;
1448
1449 notify_pointer_frame(&input->base);
1450}
1451
1452static void
1453input_handle_axis_source(void *data, struct wl_pointer *pointer,
1454 uint32_t source)
1455{
1456 struct wayland_input *input = data;
1457
1458 notify_axis_source(&input->base, source);
1459}
1460
1461static void
1462input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1463 uint32_t time, uint32_t axis)
1464{
1465 struct wayland_input *input = data;
1466 struct weston_pointer_axis_event weston_event;
1467
1468 weston_event.axis = axis;
1469 weston_event.value = 0;
1470
1471 notify_axis(&input->base, time, &weston_event);
1472}
1473
1474static void
1475input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1476 uint32_t axis, int32_t discrete)
1477{
1478 struct wayland_input *input = data;
1479
1480 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1481 input->vert.has_discrete = true;
1482 input->vert.discrete = discrete;
1483 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1484 input->horiz.has_discrete = true;
1485 input->horiz.discrete = discrete;
1486 }
Daniel Stone37816df2012-05-16 18:45:18 +01001487}
1488
1489static const struct wl_pointer_listener pointer_listener = {
1490 input_handle_pointer_enter,
1491 input_handle_pointer_leave,
1492 input_handle_motion,
1493 input_handle_button,
1494 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001495 input_handle_frame,
1496 input_handle_axis_source,
1497 input_handle_axis_stop,
1498 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001499};
1500
1501static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001502input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1503 int fd, uint32_t size)
1504{
1505 struct wayland_input *input = data;
1506 struct xkb_keymap *keymap;
1507 char *map_str;
1508
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001509 if (!data) {
1510 close(fd);
1511 return;
1512 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001513
1514 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1515 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1516 if (map_str == MAP_FAILED) {
1517 weston_log("mmap failed: %m\n");
1518 goto error;
1519 }
1520
Giulio Camuffo954f1832014-10-11 18:27:30 +03001521 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001522 map_str,
1523 XKB_KEYMAP_FORMAT_TEXT_V1,
1524 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001525 munmap(map_str, size);
1526
1527 if (!keymap) {
1528 weston_log("failed to compile keymap\n");
1529 goto error;
1530 }
1531
1532 input->keyboard_state_update = STATE_UPDATE_NONE;
1533 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1534 weston_log("No keymap provided; falling back to defalt\n");
1535 keymap = NULL;
1536 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1537 } else {
1538 weston_log("Invalid keymap\n");
1539 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001540 }
1541
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001542 close(fd);
1543
Derek Foreman1281a362015-07-31 16:55:32 -05001544 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001545 weston_seat_update_keymap(&input->base, keymap);
1546 else
1547 weston_seat_init_keyboard(&input->base, keymap);
1548
Ran Benitac9c74152014-08-19 23:59:52 +03001549 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001550
1551 return;
1552
1553error:
1554 wl_keyboard_release(input->parent.keyboard);
1555 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001556}
1557
1558static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001559input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001560 struct wl_keyboard *keyboard,
1561 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001562 struct wl_surface *surface,
1563 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001564{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001565 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001566 struct wayland_output *focus;
1567
1568 focus = input->keyboard_focus;
1569 if (focus) {
1570 /* This shouldn't happen */
1571 focus->keyboard_count--;
1572 if (!focus->keyboard_count && focus->frame)
1573 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1574 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1575 weston_output_schedule_repaint(&focus->base);
1576 }
1577
1578 input->keyboard_focus = wl_surface_get_user_data(surface);
1579 input->keyboard_focus->keyboard_count++;
1580
1581 focus = input->keyboard_focus;
1582 if (focus->frame) {
1583 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1584 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1585 weston_output_schedule_repaint(&focus->base);
1586 }
1587
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001588
Daniel Stone50692802012-06-22 13:21:41 +01001589 /* XXX: If we get a modifier event immediately before the focus,
1590 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001591 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001592 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001593}
1594
1595static void
1596input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001597 struct wl_keyboard *keyboard,
1598 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001599 struct wl_surface *surface)
1600{
1601 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001602 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001603
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001604 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001605
1606 focus = input->keyboard_focus;
1607 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001608 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001609
1610 focus->keyboard_count--;
1611 if (!focus->keyboard_count && focus->frame) {
1612 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1613 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1614 weston_output_schedule_repaint(&focus->base);
1615 }
1616
1617 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001618}
1619
Daniel Stone37816df2012-05-16 18:45:18 +01001620static void
1621input_handle_key(void *data, struct wl_keyboard *keyboard,
1622 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1623{
1624 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001625
Daniel Stone50692802012-06-22 13:21:41 +01001626 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001627 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001628 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001629 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001630 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001631}
1632
Daniel Stone351eb612012-05-31 15:27:47 -04001633static void
Derek Foreman1281a362015-07-31 16:55:32 -05001634input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001635 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001636 uint32_t mods_latched, uint32_t mods_locked,
1637 uint32_t group)
1638{
Derek Foreman1281a362015-07-31 16:55:32 -05001639 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001640 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001641 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001642 uint32_t serial_out;
1643
1644 /* If we get a key event followed by a modifier event with the
1645 * same serial number, then we try to preserve those semantics by
1646 * reusing the same serial number on the way out too. */
1647 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001648 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001649 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001650 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001651
Derek Foreman1281a362015-07-31 16:55:32 -05001652 keyboard = weston_seat_get_keyboard(&input->base);
1653 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001654 mods_depressed, mods_latched,
1655 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001656 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001657}
1658
Jonny Lamb497994a2014-08-12 14:58:26 +02001659static void
1660input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1661 int32_t rate, int32_t delay)
1662{
1663 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001664 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001665
Giulio Camuffo954f1832014-10-11 18:27:30 +03001666 b->compositor->kb_repeat_rate = rate;
1667 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001668}
1669
Daniel Stone37816df2012-05-16 18:45:18 +01001670static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001671 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001672 input_handle_keyboard_enter,
1673 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001674 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001675 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001676 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001677};
1678
1679static void
Derek Foreman748c6952015-11-06 15:56:10 -06001680input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1681 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001682 struct wl_surface *surface, int32_t id,
1683 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001684{
1685 struct wayland_input *input = data;
1686 struct wayland_output *output;
1687 enum theme_location location;
1688 bool first_touch;
1689 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001690 double x, y;
1691
1692 x = wl_fixed_to_double(fixed_x);
1693 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001694
1695 first_touch = (input->touch_points == 0);
1696 input->touch_points++;
1697
1698 input->touch_focus = wl_surface_get_user_data(surface);
1699 output = input->touch_focus;
1700 if (!first_touch && !input->touch_active)
1701 return;
1702
1703 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001704 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001705
1706 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001707 x -= fx;
1708 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001709
1710 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1711 weston_output_schedule_repaint(&output->base);
1712
1713 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1714 input->touch_points--;
1715 wl_shell_surface_move(output->parent.shell_surface,
1716 input->parent.seat, serial);
1717 frame_status_clear(output->frame,
1718 FRAME_STATUS_MOVE);
1719 return;
1720 }
1721
1722 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1723 return;
1724 }
1725
1726 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1727
1728 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1729 input->touch_active = true;
1730}
1731
1732static void
1733input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1734 uint32_t serial, uint32_t time, int32_t id)
1735{
1736 struct wayland_input *input = data;
1737 struct wayland_output *output = input->touch_focus;
1738 bool active = input->touch_active;
1739
1740 input->touch_points--;
1741 if (input->touch_points == 0) {
1742 input->touch_focus = NULL;
1743 input->touch_active = false;
1744 }
1745
1746 if (!output)
1747 return;
1748
1749 if (output->frame) {
1750 frame_touch_up(output->frame, input, id);
1751
1752 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1753 wayland_output_destroy(&output->base);
1754 input->touch_focus = NULL;
1755 input->keyboard_focus = NULL;
1756 if (wl_list_empty(&input->backend->compositor->output_list))
1757 weston_compositor_exit(input->backend->compositor);
1758
1759 return;
1760 }
1761 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1762 weston_output_schedule_repaint(&output->base);
1763 }
1764
1765 if (active)
1766 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1767}
1768
1769static void
1770input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001771 uint32_t time, int32_t id,
1772 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001773{
1774 struct wayland_input *input = data;
1775 struct wayland_output *output = input->touch_focus;
1776 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001777 double x, y;
1778
1779 x = wl_fixed_to_double(fixed_x);
1780 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001781
1782 if (!output || !input->touch_active)
1783 return;
1784
1785 if (output->frame) {
1786 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001787 x -= fx;
1788 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001789 }
1790
1791 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1792
1793 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1794}
1795
1796static void
1797input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1798{
1799 struct wayland_input *input = data;
1800
1801 if (!input->touch_focus || !input->touch_active)
1802 return;
1803
1804 notify_touch_frame(&input->base);
1805}
1806
1807static void
1808input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1809{
1810 struct wayland_input *input = data;
1811
1812 if (!input->touch_focus || !input->touch_active)
1813 return;
1814
1815 notify_touch_cancel(&input->base);
1816}
1817
1818static const struct wl_touch_listener touch_listener = {
1819 input_handle_touch_down,
1820 input_handle_touch_up,
1821 input_handle_touch_motion,
1822 input_handle_touch_frame,
1823 input_handle_touch_cancel,
1824};
1825
1826
1827static void
Daniel Stone37816df2012-05-16 18:45:18 +01001828input_handle_capabilities(void *data, struct wl_seat *seat,
1829 enum wl_seat_capability caps)
1830{
1831 struct wayland_input *input = data;
1832
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001833 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1834 input->parent.pointer = wl_seat_get_pointer(seat);
1835 wl_pointer_set_user_data(input->parent.pointer, input);
1836 wl_pointer_add_listener(input->parent.pointer,
1837 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001838 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001839 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001840 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1841 wl_pointer_release(input->parent.pointer);
1842 else
1843 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001844 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001845 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001846 }
1847
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001848 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1849 input->parent.keyboard = wl_seat_get_keyboard(seat);
1850 wl_keyboard_set_user_data(input->parent.keyboard, input);
1851 wl_keyboard_add_listener(input->parent.keyboard,
1852 &keyboard_listener, input);
1853 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001854 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1855 wl_keyboard_release(input->parent.keyboard);
1856 else
1857 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001858 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001859 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001860 }
Derek Foreman748c6952015-11-06 15:56:10 -06001861
1862 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1863 input->parent.touch = wl_seat_get_touch(seat);
1864 wl_touch_set_user_data(input->parent.touch, input);
1865 wl_touch_add_listener(input->parent.touch,
1866 &touch_listener, input);
1867 weston_seat_init_touch(&input->base);
1868 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1869 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1870 wl_touch_release(input->parent.touch);
1871 else
1872 wl_touch_destroy(input->parent.touch);
1873 input->parent.touch = NULL;
1874 weston_seat_release_touch(&input->base);
1875 }
Daniel Stone37816df2012-05-16 18:45:18 +01001876}
1877
Jonny Lamb497994a2014-08-12 14:58:26 +02001878static void
1879input_handle_name(void *data, struct wl_seat *seat,
1880 const char *name)
1881{
1882}
1883
Daniel Stone37816df2012-05-16 18:45:18 +01001884static const struct wl_seat_listener seat_listener = {
1885 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001886 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001887};
1888
1889static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001890display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001891{
1892 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001893 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001894
Peter Huttererf3d62272013-08-08 11:57:05 +10001895 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001896 if (input == NULL)
1897 return;
1898
Giulio Camuffo954f1832014-10-11 18:27:30 +03001899 weston_seat_init(&input->base, b->compositor, "default");
1900 input->backend = b;
1901 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001902 &wl_seat_interface, version);
1903 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001904 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001905
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001906 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1907 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001908
1909 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001910 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001911
1912 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1913 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001914}
1915
1916static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001917wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1918 int32_t x, int32_t y,
1919 int32_t physical_width, int32_t physical_height,
1920 int32_t subpixel, const char *make,
1921 const char *model, int32_t transform)
1922{
1923 struct wayland_parent_output *output = data;
1924
1925 output->x = x;
1926 output->y = y;
1927 output->physical.width = physical_width;
1928 output->physical.height = physical_height;
1929 output->physical.subpixel = subpixel;
1930
1931 free(output->physical.make);
1932 output->physical.make = strdup(make);
1933 free(output->physical.model);
1934 output->physical.model = strdup(model);
1935
1936 output->transform = transform;
1937}
1938
1939static struct weston_mode *
1940find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1941{
1942 struct weston_mode *mode;
1943
1944 wl_list_for_each(mode, list, link) {
1945 if (mode->width == width && mode->height == height &&
1946 mode->refresh == refresh)
1947 return mode;
1948 }
1949
1950 mode = zalloc(sizeof *mode);
1951 if (!mode)
1952 return NULL;
1953
1954 mode->width = width;
1955 mode->height = height;
1956 mode->refresh = refresh;
1957 wl_list_insert(list, &mode->link);
1958
1959 return mode;
1960}
1961
1962static void
1963wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1964 uint32_t flags, int32_t width, int32_t height,
1965 int32_t refresh)
1966{
1967 struct wayland_parent_output *output = data;
1968 struct weston_mode *mode;
1969
1970 if (output->output) {
1971 mode = find_mode(&output->output->base.mode_list,
1972 width, height, refresh);
1973 if (!mode)
1974 return;
1975 mode->flags = flags;
1976 /* Do a mode-switch on current mode change? */
1977 } else {
1978 mode = find_mode(&output->mode_list, width, height, refresh);
1979 if (!mode)
1980 return;
1981 mode->flags = flags;
1982 if (flags & WL_OUTPUT_MODE_CURRENT)
1983 output->current_mode = mode;
1984 if (flags & WL_OUTPUT_MODE_PREFERRED)
1985 output->preferred_mode = mode;
1986 }
1987}
1988
1989static const struct wl_output_listener output_listener = {
1990 wayland_parent_output_geometry,
1991 wayland_parent_output_mode
1992};
1993
1994static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001995wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001996{
1997 struct wayland_parent_output *output;
1998
1999 output = zalloc(sizeof *output);
2000 if (!output)
2001 return;
2002
2003 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002004 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002005 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002006 if (!output->global) {
2007 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002008 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002009 }
2010
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002011 wl_output_add_listener(output->global, &output_listener, output);
2012
2013 output->scale = 0;
2014 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2015 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2016 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002017 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002018
Giulio Camuffo954f1832014-10-11 18:27:30 +03002019 if (b->sprawl_across_outputs) {
2020 wl_display_roundtrip(b->parent.wl_display);
2021 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002022 }
2023}
2024
2025static void
2026wayland_parent_output_destroy(struct wayland_parent_output *output)
2027{
2028 struct weston_mode *mode, *next;
2029
2030 if (output->output)
2031 wayland_output_destroy(&output->output->base);
2032
2033 wl_output_destroy(output->global);
2034 free(output->physical.make);
2035 free(output->physical.model);
2036
2037 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2038 wl_list_remove(&mode->link);
2039 free(mode);
2040 }
2041}
2042
2043static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002044registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2045 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002046{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002047 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002048
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002049 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002050 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002051 wl_registry_bind(registry, name,
2052 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002053 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002054 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002055 wl_registry_bind(registry, name,
2056 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002057 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002058 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002059 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002060 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002061 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002062 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002063 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002064 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002065 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002066 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002067 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002068 }
2069}
2070
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002071static void
2072registry_handle_global_remove(void *data, struct wl_registry *registry,
2073 uint32_t name)
2074{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002075 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002076 struct wayland_parent_output *output;
2077
Giulio Camuffo954f1832014-10-11 18:27:30 +03002078 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002079 if (output->id == name)
2080 wayland_parent_output_destroy(output);
2081}
2082
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002083static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002084 registry_handle_global,
2085 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002086};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002087
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002088static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002089wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002090{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002091 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002092 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002093
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002094 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002095 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002096 return 0;
2097 }
2098
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002099 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002100 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002101 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002102 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002103
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002104 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002105 count = wl_display_dispatch_pending(b->parent.wl_display);
2106 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002107 }
2108
2109 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002110}
2111
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002112static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002113wayland_restore(struct weston_compositor *ec)
2114{
2115}
2116
2117static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002118wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002119{
Armin Krezović938dc522016-08-01 19:17:57 +02002120 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002121
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002122 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002123
Giulio Camuffo954f1832014-10-11 18:27:30 +03002124 if (b->parent.shm)
2125 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002126
Giulio Camuffo954f1832014-10-11 18:27:30 +03002127 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002128}
2129
Jason Ekstrand7744f712013-10-27 22:24:55 -05002130static const char *left_ptrs[] = {
2131 "left_ptr",
2132 "default",
2133 "top_left_arrow",
2134 "left-arrow"
2135};
2136
2137static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002138create_cursor(struct wayland_backend *b,
2139 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002140{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002141 unsigned int i;
2142
Benoit Gschwind244ff792016-04-28 20:33:11 +02002143 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2144 config->cursor_size,
2145 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002146 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002147 fprintf(stderr, "could not load cursor theme\n");
2148 return;
2149 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002150
Giulio Camuffo954f1832014-10-11 18:27:30 +03002151 b->cursor = NULL;
2152 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2153 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002154 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002155 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002156 fprintf(stderr, "could not load left cursor\n");
2157 return;
2158 }
2159}
2160
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002161static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002162fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2163 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002164{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002165 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002166 struct wayland_input *input = NULL;
2167
Giulio Camuffo954f1832014-10-11 18:27:30 +03002168 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002169 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002170 break;
2171
2172 if (!input || !input->output)
2173 return;
2174
2175 if (input->output->frame)
2176 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2177 else
2178 wayland_output_set_windowed(input->output);
2179
2180 weston_output_schedule_repaint(&input->output->base);
2181}
2182
Giulio Camuffo954f1832014-10-11 18:27:30 +03002183static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002184wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002185 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002186{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002187 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002188 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002189 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002190
Giulio Camuffo954f1832014-10-11 18:27:30 +03002191 b = zalloc(sizeof *b);
2192 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002193 return NULL;
2194
Giulio Camuffo954f1832014-10-11 18:27:30 +03002195 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002196 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002197 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002198
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002199 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002200 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002201 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002202 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002203 }
2204
Giulio Camuffo954f1832014-10-11 18:27:30 +03002205 wl_list_init(&b->parent.output_list);
2206 wl_list_init(&b->input_list);
2207 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2208 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2209 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002210
Benoit Gschwind244ff792016-04-28 20:33:11 +02002211 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002212
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002213 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002214
Giulio Camuffo954f1832014-10-11 18:27:30 +03002215 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002216 gl_renderer = weston_load_module("gl-renderer.so",
2217 "gl_renderer_interface");
2218 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002219 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002220 }
2221
Giulio Camuffo954f1832014-10-11 18:27:30 +03002222 if (!b->use_pixman) {
2223 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002224 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002225 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002226 gl_renderer->alpha_attribs,
2227 NULL,
2228 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002229 weston_log("Failed to initialize the GL renderer; "
2230 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002231 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002232 }
2233 }
2234
Giulio Camuffo954f1832014-10-11 18:27:30 +03002235 if (b->use_pixman) {
2236 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002237 weston_log("Failed to initialize pixman renderer\n");
2238 goto err_display;
2239 }
2240 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002241
Giulio Camuffo954f1832014-10-11 18:27:30 +03002242 b->base.destroy = wayland_destroy;
2243 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002244
Giulio Camuffo954f1832014-10-11 18:27:30 +03002245 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002246
Giulio Camuffo954f1832014-10-11 18:27:30 +03002247 fd = wl_display_get_fd(b->parent.wl_display);
2248 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002249 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002250 wayland_backend_handle_event, b);
2251 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002252 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002253
Giulio Camuffo954f1832014-10-11 18:27:30 +03002254 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002255
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002256 if (compositor->renderer->import_dmabuf) {
2257 if (linux_dmabuf_setup(compositor) < 0)
2258 weston_log("Error: initializing dmabuf "
2259 "support failed.\n");
2260 }
2261
Giulio Camuffo954f1832014-10-11 18:27:30 +03002262 compositor->backend = &b->base;
2263 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002264err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002265 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002266err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002267 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002268 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002269 return NULL;
2270}
2271
2272static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002273wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002274{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002275 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002276
Giulio Camuffo954f1832014-10-11 18:27:30 +03002277 if (b->theme)
2278 theme_destroy(b->theme);
2279 if (b->frame_device)
2280 cairo_device_destroy(b->frame_device);
2281 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002282
Giulio Camuffo954f1832014-10-11 18:27:30 +03002283 weston_compositor_shutdown(b->compositor);
2284 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002285}
2286
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002287static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002288config_init_to_defaults(struct weston_wayland_backend_config *config)
2289{
2290}
2291
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002292WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002293backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002294 struct weston_backend_config *config_base)
2295{
2296 struct wayland_backend *b;
2297 struct wayland_output *output;
2298 struct wayland_parent_output *poutput;
2299 struct weston_wayland_backend_config new_config;
2300 int x, count;
2301
Benoit Gschwinde091b452016-05-10 22:47:48 +02002302 if (config_base == NULL ||
2303 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2304 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2305 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002306 return -1;
2307 }
2308
2309 config_init_to_defaults(&new_config);
2310 memcpy(&new_config, config_base, config_base->struct_size);
2311
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002312 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002313
Giulio Camuffo954f1832014-10-11 18:27:30 +03002314 if (!b)
2315 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002316
Benoit Gschwind37a68072016-04-28 20:33:08 +02002317 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002318 b->sprawl_across_outputs = 1;
2319 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002320
Giulio Camuffo954f1832014-10-11 18:27:30 +03002321 wl_list_for_each(poutput, &b->parent.output_list, link)
2322 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002323
Giulio Camuffo954f1832014-10-11 18:27:30 +03002324 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002325 }
2326
Benoit Gschwind37a68072016-04-28 20:33:08 +02002327 if (new_config.fullscreen) {
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002328 if (new_config.num_outputs != 1 || !new_config.outputs)
2329 goto err_outputs;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002330
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002331 output = wayland_output_create_for_config(b,
2332 &new_config.outputs[0],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002333 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002334 if (!output)
2335 goto err_outputs;
2336
Axel Davydd8b88d2013-11-17 21:34:16 +01002337 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002338 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002339 }
2340
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002341 x = 0;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002342 for (count = 0; count < new_config.num_outputs; ++count) {
2343 output = wayland_output_create_for_config(b, &new_config.outputs[count],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002344 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002345 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002346 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002347 if (wayland_output_set_windowed(output))
2348 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002349
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002350 x += output->base.width;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002351 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002352
Giulio Camuffo954f1832014-10-11 18:27:30 +03002353 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002354 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002355 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002356 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002357
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002358err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002359 wayland_backend_destroy(b);
2360 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002361}