blob: ceb5050d45f63a6303d1e9ddef3bf6742fed415a [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
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500196static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500197wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
198{
199 cairo_surface_destroy(buffer->c_surface);
200 pixman_image_unref(buffer->pm_image);
201
202 wl_buffer_destroy(buffer->buffer);
203 munmap(buffer->data, buffer->size);
204
205 pixman_region32_fini(&buffer->damage);
206
207 wl_list_remove(&buffer->link);
208 wl_list_remove(&buffer->free_link);
209 free(buffer);
210}
211
212static void
213buffer_release(void *data, struct wl_buffer *buffer)
214{
215 struct wayland_shm_buffer *sb = data;
216
217 if (sb->output) {
218 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
219 } else {
220 wayland_shm_buffer_destroy(sb);
221 }
222}
223
224static const struct wl_buffer_listener buffer_listener = {
225 buffer_release
226};
227
228static struct wayland_shm_buffer *
229wayland_output_get_shm_buffer(struct wayland_output *output)
230{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300231 struct wayland_backend *b =
232 (struct wayland_backend *) output->base.compositor->backend;
233 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500234 struct wayland_shm_buffer *sb;
235
236 struct wl_shm_pool *pool;
237 int width, height, stride;
238 int32_t fx, fy;
239 int fd;
240 unsigned char *data;
241
242 if (!wl_list_empty(&output->shm.free_buffers)) {
243 sb = container_of(output->shm.free_buffers.next,
244 struct wayland_shm_buffer, free_link);
245 wl_list_remove(&sb->free_link);
246 wl_list_init(&sb->free_link);
247
248 return sb;
249 }
250
251 if (output->frame) {
252 width = frame_width(output->frame);
253 height = frame_height(output->frame);
254 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500255 width = output->base.current_mode->width;
256 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500257 }
258
259 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
260
261 fd = os_create_anonymous_file(height * stride);
262 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000263 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500264 return NULL;
265 }
266
267 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
268 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000269 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500270 close(fd);
271 return NULL;
272 }
273
274 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000275 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200276 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000277 close(fd);
278 free(data);
279 return NULL;
280 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500281
282 sb->output = output;
283 wl_list_init(&sb->free_link);
284 wl_list_insert(&output->shm.buffers, &sb->link);
285
286 pixman_region32_init_rect(&sb->damage, 0, 0,
287 output->base.width, output->base.height);
288 sb->frame_damaged = 1;
289
290 sb->data = data;
291 sb->size = height * stride;
292
293 pool = wl_shm_create_pool(shm, fd, sb->size);
294
295 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
296 width, height,
297 stride,
298 WL_SHM_FORMAT_ARGB8888);
299 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
300 wl_shm_pool_destroy(pool);
301 close(fd);
302
303 memset(data, 0, sb->size);
304
305 sb->c_surface =
306 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
307 width, height, stride);
308
309 fx = 0;
310 fy = 0;
311 if (output->frame)
312 frame_interior(output->frame, &fx, &fy, 0, 0);
313 sb->pm_image =
314 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
315 (uint32_t *)(data + fy * stride) + fx,
316 stride);
317
318 return sb;
319}
320
321static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500322frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400323{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500324 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400325 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400326
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500327 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400328
329 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300330
331 /*
332 * This is the fallback case, where Presentation extension is not
333 * available from the parent compositor. We do not know the base for
334 * 'time', so we cannot feed it to finish_frame(). Do the only thing
335 * we can, and pretend finish_frame time is when we process this
336 * event.
337 */
338 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200339 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400340}
341
342static const struct wl_callback_listener frame_listener = {
343 frame_done
344};
345
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500346static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200347draw_initial_frame(struct wayland_output *output)
348{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500349 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200350
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500351 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200352
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500353 /* If we are rendering with GL, then orphan it so that it gets
354 * destroyed immediately */
355 if (output->gl.egl_window)
356 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500357
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500358 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500359 wl_surface_damage(output->parent.surface, 0, 0,
360 output->base.current_mode->width,
361 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200362}
363
364static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500365wayland_output_update_gl_border(struct wayland_output *output)
366{
367 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
368 cairo_t *cr;
369
370 if (!output->frame)
371 return;
372 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
373 return;
374
375 fwidth = frame_width(output->frame);
376 fheight = frame_height(output->frame);
377 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
378
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 if (!output->gl.border.top)
380 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500381 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
382 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500383 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500384 frame_repaint(output->frame, cr);
385 cairo_destroy(cr);
386 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
387 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 cairo_image_surface_get_stride(output->gl.border.top) / 4,
389 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500390
391
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500392 if (!output->gl.border.left)
393 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500394 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
395 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500396 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500397 cairo_translate(cr, 0, -iy);
398 frame_repaint(output->frame, cr);
399 cairo_destroy(cr);
400 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
401 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500402 cairo_image_surface_get_stride(output->gl.border.left) / 4,
403 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500404
405
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500406 if (!output->gl.border.right)
407 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500408 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
409 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500410 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500411 cairo_translate(cr, -(iwidth + ix), -iy);
412 frame_repaint(output->frame, cr);
413 cairo_destroy(cr);
414 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
415 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500416 cairo_image_surface_get_stride(output->gl.border.right) / 4,
417 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500418
419
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500420 if (!output->gl.border.bottom)
421 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500422 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
423 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500424 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500425 cairo_translate(cr, 0, -(iy + iheight));
426 frame_repaint(output->frame, cr);
427 cairo_destroy(cr);
428 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
429 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500430 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
431 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500432}
433
434static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200435wayland_output_start_repaint_loop(struct weston_output *output_base)
436{
437 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300438 struct wayland_backend *wb =
439 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200440 struct wl_callback *callback;
441
442 /* If this is the initial frame, we need to attach a buffer so that
443 * the compositor can map the surface and include it in its render
444 * loop. If the surface doesn't end up in the render loop, the frame
445 * callback won't be invoked. The buffer is transparent and of the
446 * same size as the future real output buffer. */
447 if (output->parent.draw_initial_frame) {
448 output->parent.draw_initial_frame = 0;
449
450 draw_initial_frame(output);
451 }
452
453 callback = wl_surface_frame(output->parent.surface);
454 wl_callback_add_listener(callback, &frame_listener, output);
455 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300456 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200457}
458
David Herrmann1edf44c2013-10-22 17:11:26 +0200459static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500460wayland_output_repaint_gl(struct weston_output *output_base,
461 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400462{
463 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400464 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400465 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600466
Kristian Høgsberg33418202011-08-16 23:01:28 -0400467 callback = wl_surface_frame(output->parent.surface);
468 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100469
Jason Ekstrand7744f712013-10-27 22:24:55 -0500470 wayland_output_update_gl_border(output);
471
Pekka Paalanenbc106382012-10-10 12:49:31 +0300472 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200473
474 pixman_region32_subtract(&ec->primary_plane.damage,
475 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200476 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100477}
478
Matt Roper361d2ad2011-08-29 13:52:23 -0700479static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500480wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
481{
482 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
483 cairo_t *cr;
484
485 if (!buffer->output->frame || !buffer->frame_damaged)
486 return;
487
488 cr = cairo_create(buffer->c_surface);
489
490 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
491 fwidth = frame_width(buffer->output->frame);
492 fheight = frame_height(buffer->output->frame);
493
494 /* Set the clip so we don't unnecisaraly damage the surface */
495 cairo_move_to(cr, ix, iy);
496 cairo_rel_line_to(cr, iwidth, 0);
497 cairo_rel_line_to(cr, 0, iheight);
498 cairo_rel_line_to(cr, -iwidth, 0);
499 cairo_line_to(cr, ix, iy);
500 cairo_line_to(cr, 0, iy);
501 cairo_line_to(cr, 0, fheight);
502 cairo_line_to(cr, fwidth, fheight);
503 cairo_line_to(cr, fwidth, 0);
504 cairo_line_to(cr, 0, 0);
505 cairo_line_to(cr, 0, iy);
506 cairo_close_path(cr);
507 cairo_clip(cr);
508
509 /* Draw using a pattern so that the final result gets clipped */
510 cairo_push_group(cr);
511 frame_repaint(buffer->output->frame, cr);
512 cairo_pop_group_to_source(cr);
513 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
514 cairo_paint(cr);
515
516 cairo_destroy(cr);
517}
518
519static void
520wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
521{
522 pixman_region32_t damage;
523 pixman_box32_t *rects;
524 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
525 int i, n;
526
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500527 pixman_region32_init(&damage);
528 weston_transformed_region(sb->output->base.width,
529 sb->output->base.height,
530 sb->output->base.transform,
531 sb->output->base.current_scale,
532 &sb->damage, &damage);
533
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500534 if (sb->output->frame) {
535 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
536 fwidth = frame_width(sb->output->frame);
537 fheight = frame_height(sb->output->frame);
538
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500539 pixman_region32_translate(&damage, ix, iy);
540
541 if (sb->frame_damaged) {
542 pixman_region32_union_rect(&damage, &damage,
543 0, 0, fwidth, iy);
544 pixman_region32_union_rect(&damage, &damage,
545 0, iy, ix, iheight);
546 pixman_region32_union_rect(&damage, &damage,
547 ix + iwidth, iy,
548 fwidth - (ix + iwidth), iheight);
549 pixman_region32_union_rect(&damage, &damage,
550 0, iy + iheight,
551 fwidth, fheight - (iy + iheight));
552 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500553 }
554
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500555 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500556 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
557 for (i = 0; i < n; ++i)
558 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
559 rects[i].y1, rects[i].x2 - rects[i].x1,
560 rects[i].y2 - rects[i].y1);
561
562 if (sb->output->frame)
563 pixman_region32_fini(&damage);
564}
565
566static int
567wayland_output_repaint_pixman(struct weston_output *output_base,
568 pixman_region32_t *damage)
569{
570 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300571 struct wayland_backend *b =
572 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500573 struct wl_callback *callback;
574 struct wayland_shm_buffer *sb;
575
576 if (output->frame) {
577 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
578 wl_list_for_each(sb, &output->shm.buffers, link)
579 sb->frame_damaged = 1;
580 }
581
582 wl_list_for_each(sb, &output->shm.buffers, link)
583 pixman_region32_union(&sb->damage, &sb->damage, damage);
584
585 sb = wayland_output_get_shm_buffer(output);
586
587 wayland_output_update_shm_border(sb);
588 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300589 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500590
591 wayland_shm_buffer_attach(sb);
592
593 callback = wl_surface_frame(output->parent.surface);
594 wl_callback_add_listener(callback, &frame_listener, output);
595 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300596 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500597
598 pixman_region32_fini(&sb->damage);
599 pixman_region32_init(&sb->damage);
600 sb->frame_damaged = 0;
601
Giulio Camuffo954f1832014-10-11 18:27:30 +0300602 pixman_region32_subtract(&b->compositor->primary_plane.damage,
603 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500604 return 0;
605}
606
607static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500608wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700609{
610 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 struct wayland_backend *b =
612 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700613
Giulio Camuffo954f1832014-10-11 18:27:30 +0300614 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500615 pixman_renderer_output_destroy(output_base);
616 } else {
617 gl_renderer->output_destroy(output_base);
618 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100619
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500620 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500621 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500622 if (output->parent.shell_surface)
623 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500624
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600625 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500626 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600627
628 cairo_surface_destroy(output->gl.border.top);
629 cairo_surface_destroy(output->gl.border.left);
630 cairo_surface_destroy(output->gl.border.right);
631 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500632
633 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700634 free(output);
635
636 return;
637}
638
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300639static const struct wl_shell_surface_listener shell_surface_listener;
640
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200641static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500642wayland_output_init_gl_renderer(struct wayland_output *output)
643{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500644 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500645
646 if (output->frame) {
647 fwidth = frame_width(output->frame);
648 fheight = frame_height(output->frame);
649 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500650 fwidth = output->base.current_mode->width;
651 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500652 }
653
654 output->gl.egl_window =
655 wl_egl_window_create(output->parent.surface,
656 fwidth, fheight);
657 if (!output->gl.egl_window) {
658 weston_log("failure to create wl_egl_window\n");
659 return -1;
660 }
661
662 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000663 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100664 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000665 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500666 NULL,
667 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500668 goto cleanup_window;
669
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500670 return 0;
671
672cleanup_window:
673 wl_egl_window_destroy(output->gl.egl_window);
674 return -1;
675}
676
677static int
678wayland_output_init_pixman_renderer(struct wayland_output *output)
679{
680 return pixman_renderer_output_create(&output->base);
681}
682
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600683static void
684wayland_output_resize_surface(struct wayland_output *output)
685{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300686 struct wayland_backend *b =
687 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600688 struct wayland_shm_buffer *buffer, *next;
689 int32_t ix, iy, iwidth, iheight;
690 int32_t width, height;
691 struct wl_region *region;
692
693 width = output->base.current_mode->width;
694 height = output->base.current_mode->height;
695
696 if (output->frame) {
697 frame_resize_inside(output->frame, width, height);
698
699 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300700 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600701 wl_region_add(region, ix, iy, iwidth, iheight);
702 wl_surface_set_input_region(output->parent.surface, region);
703 wl_region_destroy(region);
704
705 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300706 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600707 wl_region_add(region, ix, iy, iwidth, iheight);
708 wl_surface_set_opaque_region(output->parent.surface, region);
709 wl_region_destroy(region);
710
711 width = frame_width(output->frame);
712 height = frame_height(output->frame);
713 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300714 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600715 wl_region_add(region, 0, 0, width, height);
716 wl_surface_set_input_region(output->parent.surface, region);
717 wl_region_destroy(region);
718
Giulio Camuffo954f1832014-10-11 18:27:30 +0300719 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600720 wl_region_add(region, 0, 0, width, height);
721 wl_surface_set_opaque_region(output->parent.surface, region);
722 wl_region_destroy(region);
723 }
724
725 if (output->gl.egl_window) {
726 wl_egl_window_resize(output->gl.egl_window,
727 width, height, 0, 0);
728
729 /* These will need to be re-created due to the resize */
730 gl_renderer->output_set_border(&output->base,
731 GL_RENDERER_BORDER_TOP,
732 0, 0, 0, NULL);
733 cairo_surface_destroy(output->gl.border.top);
734 output->gl.border.top = NULL;
735 gl_renderer->output_set_border(&output->base,
736 GL_RENDERER_BORDER_LEFT,
737 0, 0, 0, NULL);
738 cairo_surface_destroy(output->gl.border.left);
739 output->gl.border.left = NULL;
740 gl_renderer->output_set_border(&output->base,
741 GL_RENDERER_BORDER_RIGHT,
742 0, 0, 0, NULL);
743 cairo_surface_destroy(output->gl.border.right);
744 output->gl.border.right = NULL;
745 gl_renderer->output_set_border(&output->base,
746 GL_RENDERER_BORDER_BOTTOM,
747 0, 0, 0, NULL);
748 cairo_surface_destroy(output->gl.border.bottom);
749 output->gl.border.bottom = NULL;
750 }
751
752 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500753 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600754 wayland_shm_buffer_destroy(buffer);
755 /* These will get thrown away when they get released */
756 wl_list_for_each(buffer, &output->shm.buffers, link)
757 buffer->output = NULL;
758}
759
760static int
761wayland_output_set_windowed(struct wayland_output *output)
762{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300763 struct wayland_backend *b =
764 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600765 int tlen;
766 char *title;
767
768 if (output->frame)
769 return 0;
770
771 if (output->name) {
772 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
773 title = malloc(tlen + 1);
774 if (!title)
775 return -1;
776
777 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
778 } else {
779 title = strdup(WINDOW_TITLE);
780 }
781
Giulio Camuffo954f1832014-10-11 18:27:30 +0300782 if (!b->theme) {
783 b->theme = theme_create();
784 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600785 free(title);
786 return -1;
787 }
788 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300789 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600790 FRAME_BUTTON_CLOSE, title);
791 free(title);
792 if (!output->frame)
793 return -1;
794
795 if (output->keyboard_count)
796 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
797
798 wayland_output_resize_surface(output);
799
800 wl_shell_surface_set_toplevel(output->parent.shell_surface);
801
802 return 0;
803}
804
805static void
806wayland_output_set_fullscreen(struct wayland_output *output,
807 enum wl_shell_surface_fullscreen_method method,
808 uint32_t framerate, struct wl_output *target)
809{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300810 struct wayland_backend *b =
811 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500812
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600813 if (output->frame) {
814 frame_destroy(output->frame);
815 output->frame = NULL;
816 }
817
818 wayland_output_resize_surface(output);
819
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500820 if (output->parent.shell_surface) {
821 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
822 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300823 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800824 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
825 output->parent.surface,
826 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500827 }
828}
829
830static struct weston_mode *
831wayland_output_choose_mode(struct wayland_output *output,
832 struct weston_mode *ref_mode)
833{
834 struct weston_mode *mode;
835
836 /* First look for an exact match */
837 wl_list_for_each(mode, &output->base.mode_list, link)
838 if (mode->width == ref_mode->width &&
839 mode->height == ref_mode->height &&
840 mode->refresh == ref_mode->refresh)
841 return mode;
842
843 /* If we can't find an exact match, ignore refresh and try again */
844 wl_list_for_each(mode, &output->base.mode_list, link)
845 if (mode->width == ref_mode->width &&
846 mode->height == ref_mode->height)
847 return mode;
848
849 /* Yeah, we failed */
850 return NULL;
851}
852
853enum mode_status {
854 MODE_STATUS_UNKNOWN,
855 MODE_STATUS_SUCCESS,
856 MODE_STATUS_FAIL,
857 MODE_STATUS_CANCEL,
858};
859
860static void
861mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800862 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500863{
864 enum mode_status *value = data;
865
866 printf("Mode switch successful\n");
867
868 *value = MODE_STATUS_SUCCESS;
869}
870
871static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800872mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500873{
874 enum mode_status *value = data;
875
876 printf("Mode switch failed\n");
877
878 *value = MODE_STATUS_FAIL;
879}
880
881static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800882mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500883{
884 enum mode_status *value = data;
885
886 printf("Mode switch cancelled\n");
887
888 *value = MODE_STATUS_CANCEL;
889}
890
Jonas Ådahl496adb32015-11-17 16:00:27 +0800891struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500892 mode_feedback_successful,
893 mode_feedback_failed,
894 mode_feedback_cancelled,
895};
896
897static int
898wayland_output_switch_mode(struct weston_output *output_base,
899 struct weston_mode *mode)
900{
901 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300902 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500903 struct wl_surface *old_surface;
904 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800905 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500906 enum mode_status mode_status;
907 int ret = 0;
908
909 if (output_base == NULL) {
910 weston_log("output is NULL.\n");
911 return -1;
912 }
913
914 if (mode == NULL) {
915 weston_log("mode is NULL.\n");
916 return -1;
917 }
918
Giulio Camuffo954f1832014-10-11 18:27:30 +0300919 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500920
Giulio Camuffo954f1832014-10-11 18:27:30 +0300921 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500922 return -1;
923
924 mode = wayland_output_choose_mode(output, mode);
925 if (mode == NULL)
926 return -1;
927
928 if (output->base.current_mode == mode)
929 return 0;
930
931 old_mode = output->base.current_mode;
932 old_surface = output->parent.surface;
933 output->base.current_mode = mode;
934 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300935 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500936 wl_surface_set_user_data(output->parent.surface, output);
937
938 /* Blow the old buffers because we changed size/surfaces */
939 wayland_output_resize_surface(output);
940
941 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800942 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
943 output->parent.surface,
944 output->parent.output,
945 mode->refresh);
946 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
947 &mode_feedback_listener,
948 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500949
950 /* This should kick-start things again */
951 output->parent.draw_initial_frame = 1;
952 wayland_output_start_repaint_loop(&output->base);
953
954 mode_status = MODE_STATUS_UNKNOWN;
955 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300956 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500957
Jonas Ådahl496adb32015-11-17 16:00:27 +0800958 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500959
960 if (mode_status == MODE_STATUS_FAIL) {
961 output->base.current_mode = old_mode;
962 wl_surface_destroy(output->parent.surface);
963 output->parent.surface = old_surface;
964 wayland_output_resize_surface(output);
965
966 return -1;
967 }
968
969 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
970 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
971
Giulio Camuffo954f1832014-10-11 18:27:30 +0300972 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500973 pixman_renderer_output_destroy(output_base);
974 if (wayland_output_init_pixman_renderer(output) < 0)
975 goto err_output;
976 } else {
977 gl_renderer->output_destroy(output_base);
978 wl_egl_window_destroy(output->gl.egl_window);
979 if (wayland_output_init_gl_renderer(output) < 0)
980 goto err_output;
981 }
982 wl_surface_destroy(old_surface);
983
984 weston_output_schedule_repaint(&output->base);
985
986 return 0;
987
988err_output:
989 /* XXX */
990 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600991}
992
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500993static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300994wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600995 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500996 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100997{
998 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500999 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001000
1001 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1002 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001003
Peter Huttererf3d62272013-08-08 11:57:05 +10001004 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001005 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001006 return NULL;
1007
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001008 output->name = name ? strdup(name) : NULL;
Yong Bakose51647f2016-06-05 11:01:17 -05001009 output->base.make = "wayland";
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001010 output->base.model = "none";
1011
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001012 output_width = width * scale;
1013 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001014
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001015 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001016 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001017 if (!output->parent.surface)
1018 goto err_name;
1019 wl_surface_set_user_data(output->parent.surface, output);
1020
1021 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001022
Giulio Camuffo954f1832014-10-11 18:27:30 +03001023 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001024 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001025 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001026 output->parent.surface);
1027 if (!output->parent.shell_surface)
1028 goto err_surface;
1029 wl_shell_surface_add_listener(output->parent.shell_surface,
1030 &shell_surface_listener, output);
1031 }
1032
Giulio Camuffo954f1832014-10-11 18:27:30 +03001033 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001034 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1035 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001036 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001037 if (!width)
1038 output_width = output->parent.configure_width;
1039 if (!height)
1040 output_height = output->parent.configure_height;
1041 }
1042
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001043 output->mode.flags =
1044 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001045 output->mode.width = output_width;
1046 output->mode.height = output_height;
1047 output->mode.refresh = 60000;
1048 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001049 wl_list_init(&output->base.mode_list);
1050 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001051 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001052
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001053 wl_list_init(&output->shm.buffers);
1054 wl_list_init(&output->shm.free_buffers);
1055
Giulio Camuffo954f1832014-10-11 18:27:30 +03001056 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001057 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001058
Giulio Camuffo954f1832014-10-11 18:27:30 +03001059 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001060 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001061 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001062 output->base.repaint = wayland_output_repaint_pixman;
1063 } else {
1064 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001065 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001066 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001067 }
1068
Jonas Ådahle5a12252013-04-05 23:07:11 +02001069 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001070 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001071 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001072 output->base.set_backlight = NULL;
1073 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001074 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001075
Giulio Camuffo954f1832014-10-11 18:27:30 +03001076 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001077
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001078 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001079
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001080err_output:
1081 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001082 if (output->parent.shell_surface)
1083 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001084err_surface:
1085 wl_surface_destroy(output->parent.surface);
1086err_name:
1087 free(output->name);
1088
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001089 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001090 free(output);
1091
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001092 return NULL;
1093}
1094
Benoit Gschwind830b7882016-04-28 20:33:12 +02001095static struct wayland_output *
1096wayland_output_create_for_config(struct wayland_backend *b,
1097 struct weston_wayland_backend_output_config *oc,
1098 int fullscreen, int32_t x, int32_t y)
1099{
1100 struct wayland_output *output;
1101
1102 output = wayland_output_create(b, x, y, oc->width, oc->height, oc->name,
1103 fullscreen, oc->transform, oc->scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001104
1105 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001106}
1107
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001108static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001109wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001110 struct wayland_parent_output *poutput)
1111{
1112 struct wayland_output *output;
1113 struct weston_mode *mode;
1114 int32_t x;
1115
1116 if (poutput->current_mode) {
1117 mode = poutput->current_mode;
1118 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001119 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001120 } else if (!wl_list_empty(&poutput->mode_list)) {
1121 mode = container_of(poutput->mode_list.next,
1122 struct weston_mode, link);
1123 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001124 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001125 return NULL;
1126 }
1127
Giulio Camuffo954f1832014-10-11 18:27:30 +03001128 if (!wl_list_empty(&b->compositor->output_list)) {
1129 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001130 struct wayland_output, base.link);
1131 x = output->base.x + output->base.current_mode->width;
1132 } else {
1133 x = 0;
1134 }
1135
Giulio Camuffo954f1832014-10-11 18:27:30 +03001136 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001137 NULL, 0,
1138 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1139 if (!output)
1140 return NULL;
1141
1142 output->parent.output = poutput->global;
1143
1144 output->base.make = poutput->physical.make;
1145 output->base.model = poutput->physical.model;
1146 wl_list_init(&output->base.mode_list);
1147 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1148 wl_list_init(&poutput->mode_list);
1149
1150 wayland_output_set_fullscreen(output,
1151 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1152 mode->refresh, poutput->global);
1153
1154 if (output->parent.shell_surface) {
1155 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1156 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1157 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001158 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001159 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1160 output->parent.surface,
1161 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1162 poutput->global);
1163 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1164 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1165 output->parent.surface,
1166 poutput->global,
1167 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001168 }
1169
1170 return output;
1171}
1172
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001173static void
1174shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1175 uint32_t serial)
1176{
1177 wl_shell_surface_pong(shell_surface, serial);
1178}
1179
1180static void
1181shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1182 uint32_t edges, int32_t width, int32_t height)
1183{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001184 struct wayland_output *output = data;
1185
1186 output->parent.configure_width = width;
1187 output->parent.configure_height = height;
1188
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001189 /* FIXME: implement resizing */
1190}
1191
1192static void
1193shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1194{
1195}
1196
1197static const struct wl_shell_surface_listener shell_surface_listener = {
1198 shell_surface_ping,
1199 shell_surface_configure,
1200 shell_surface_popup_done
1201};
1202
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001203/* Events received from the wayland-server this compositor is client of: */
1204
Jason Ekstrand7744f712013-10-27 22:24:55 -05001205/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001206static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001207input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001208{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001209
Jason Ekstrand7744f712013-10-27 22:24:55 -05001210 struct wl_buffer *buffer;
1211 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001212
Giulio Camuffo954f1832014-10-11 18:27:30 +03001213 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001214 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001215
Giulio Camuffo954f1832014-10-11 18:27:30 +03001216 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001217 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001218 if (!buffer)
1219 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001220
1221 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1222 input->parent.cursor.surface,
1223 image->hotspot_x, image->hotspot_y);
1224
1225 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1226 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1227 image->width, image->height);
1228 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001229}
1230
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001231static void
Daniel Stone37816df2012-05-16 18:45:18 +01001232input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1233 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001234 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001235{
1236 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001237 int32_t fx, fy;
1238 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001239 double x, y;
1240
1241 x = wl_fixed_to_double(fixed_x);
1242 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001243
Daniel Stone50692802012-06-22 13:21:41 +01001244 /* XXX: If we get a modifier event immediately before the focus,
1245 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001246 input->enter_serial = serial;
1247 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001248
1249 if (input->output->frame) {
1250 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001251 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001252 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001253 x -= fx;
1254 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001255
1256 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1257 weston_output_schedule_repaint(&input->output->base);
1258 } else {
1259 location = THEME_LOCATION_CLIENT_AREA;
1260 }
1261
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001262 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001263
1264 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001265 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001266 notify_pointer_focus(&input->base, &input->output->base, x, y);
1267 wl_pointer_set_cursor(input->parent.pointer,
1268 input->enter_serial, NULL, 0, 0);
1269 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001270 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001271 notify_pointer_focus(&input->base, NULL, 0, 0);
1272 input_set_cursor(input);
1273 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001274}
1275
1276static void
Daniel Stone37816df2012-05-16 18:45:18 +01001277input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1278 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001279{
1280 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001281
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001282 if (!input->output)
1283 return;
1284
Jason Ekstrand7744f712013-10-27 22:24:55 -05001285 if (input->output->frame) {
1286 frame_pointer_leave(input->output->frame, input);
1287
1288 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1289 weston_output_schedule_repaint(&input->output->base);
1290 }
1291
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001292 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001293 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001294 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001295}
1296
1297static void
Daniel Stone37816df2012-05-16 18:45:18 +01001298input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001299 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001300{
1301 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001302 int32_t fx, fy;
1303 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001304 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001305 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001306
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001307 if (!input->output)
1308 return;
1309
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001310 x = wl_fixed_to_double(fixed_x);
1311 y = wl_fixed_to_double(fixed_y);
1312
Jason Ekstrand7744f712013-10-27 22:24:55 -05001313 if (input->output->frame) {
1314 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001315 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001316 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001317 x -= fx;
1318 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001319
1320 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1321 weston_output_schedule_repaint(&input->output->base);
1322 } else {
1323 location = THEME_LOCATION_CLIENT_AREA;
1324 }
1325
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001326 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001327
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001328 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001329 input_set_cursor(input);
1330 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001331 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001332 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001333 } else if (!input->has_focus &&
1334 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001335 wl_pointer_set_cursor(input->parent.pointer,
1336 input->enter_serial, NULL, 0, 0);
1337 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001338 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001339 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001340 }
1341
Peter Hutterer87743e92016-01-18 16:38:22 +10001342 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001343 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001344 want_frame = true;
1345 }
1346
1347 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1348 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001349}
1350
1351static void
1352input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001353 uint32_t serial, uint32_t time, uint32_t button,
1354 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001355{
1356 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001357 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001358 enum frame_button_state fstate;
1359 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001360
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001361 if (!input->output)
1362 return;
1363
Jason Ekstrand7744f712013-10-27 22:24:55 -05001364 if (input->output->frame) {
1365 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1366 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1367
1368 location = frame_pointer_button(input->output->frame, input,
1369 button, fstate);
1370
1371 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1372
1373 wl_shell_surface_move(input->output->parent.shell_surface,
1374 input->parent.seat, serial);
1375 frame_status_clear(input->output->frame,
1376 FRAME_STATUS_MOVE);
1377 return;
1378 }
1379
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001380 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1381 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001382 input->output = NULL;
1383 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001384
Giulio Camuffo954f1832014-10-11 18:27:30 +03001385 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001386 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001387
1388 return;
1389 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001390
1391 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1392 weston_output_schedule_repaint(&input->output->base);
1393 } else {
1394 location = THEME_LOCATION_CLIENT_AREA;
1395 }
1396
Peter Hutterer87743e92016-01-18 16:38:22 +10001397 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001398 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001399 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1400 notify_pointer_frame(&input->base);
1401 }
Daniel Stone37816df2012-05-16 18:45:18 +01001402}
1403
1404static void
1405input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001406 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001407{
1408 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001409 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001410
Peter Hutterer89b6a492016-01-18 15:58:17 +10001411 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001412 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001413
Peter Hutterer87743e92016-01-18 16:38:22 +10001414 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1415 input->vert.has_discrete) {
1416 weston_event.has_discrete = true;
1417 weston_event.discrete = input->vert.discrete;
1418 input->vert.has_discrete = false;
1419 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1420 input->horiz.has_discrete) {
1421 weston_event.has_discrete = true;
1422 weston_event.discrete = input->horiz.discrete;
1423 input->horiz.has_discrete = false;
1424 }
1425
Peter Hutterer89b6a492016-01-18 15:58:17 +10001426 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001427
1428 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1429 notify_pointer_frame(&input->base);
1430}
1431
1432static void
1433input_handle_frame(void *data, struct wl_pointer *pointer)
1434{
1435 struct wayland_input *input = data;
1436
1437 notify_pointer_frame(&input->base);
1438}
1439
1440static void
1441input_handle_axis_source(void *data, struct wl_pointer *pointer,
1442 uint32_t source)
1443{
1444 struct wayland_input *input = data;
1445
1446 notify_axis_source(&input->base, source);
1447}
1448
1449static void
1450input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1451 uint32_t time, uint32_t axis)
1452{
1453 struct wayland_input *input = data;
1454 struct weston_pointer_axis_event weston_event;
1455
1456 weston_event.axis = axis;
1457 weston_event.value = 0;
1458
1459 notify_axis(&input->base, time, &weston_event);
1460}
1461
1462static void
1463input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1464 uint32_t axis, int32_t discrete)
1465{
1466 struct wayland_input *input = data;
1467
1468 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1469 input->vert.has_discrete = true;
1470 input->vert.discrete = discrete;
1471 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1472 input->horiz.has_discrete = true;
1473 input->horiz.discrete = discrete;
1474 }
Daniel Stone37816df2012-05-16 18:45:18 +01001475}
1476
1477static const struct wl_pointer_listener pointer_listener = {
1478 input_handle_pointer_enter,
1479 input_handle_pointer_leave,
1480 input_handle_motion,
1481 input_handle_button,
1482 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001483 input_handle_frame,
1484 input_handle_axis_source,
1485 input_handle_axis_stop,
1486 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001487};
1488
1489static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001490input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1491 int fd, uint32_t size)
1492{
1493 struct wayland_input *input = data;
1494 struct xkb_keymap *keymap;
1495 char *map_str;
1496
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001497 if (!data) {
1498 close(fd);
1499 return;
1500 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001501
1502 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1503 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1504 if (map_str == MAP_FAILED) {
1505 weston_log("mmap failed: %m\n");
1506 goto error;
1507 }
1508
Giulio Camuffo954f1832014-10-11 18:27:30 +03001509 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001510 map_str,
1511 XKB_KEYMAP_FORMAT_TEXT_V1,
1512 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001513 munmap(map_str, size);
1514
1515 if (!keymap) {
1516 weston_log("failed to compile keymap\n");
1517 goto error;
1518 }
1519
1520 input->keyboard_state_update = STATE_UPDATE_NONE;
1521 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1522 weston_log("No keymap provided; falling back to defalt\n");
1523 keymap = NULL;
1524 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1525 } else {
1526 weston_log("Invalid keymap\n");
1527 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001528 }
1529
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001530 close(fd);
1531
Derek Foreman1281a362015-07-31 16:55:32 -05001532 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001533 weston_seat_update_keymap(&input->base, keymap);
1534 else
1535 weston_seat_init_keyboard(&input->base, keymap);
1536
Ran Benitac9c74152014-08-19 23:59:52 +03001537 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001538
1539 return;
1540
1541error:
1542 wl_keyboard_release(input->parent.keyboard);
1543 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001544}
1545
1546static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001547input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001548 struct wl_keyboard *keyboard,
1549 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001550 struct wl_surface *surface,
1551 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001552{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001553 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001554 struct wayland_output *focus;
1555
1556 focus = input->keyboard_focus;
1557 if (focus) {
1558 /* This shouldn't happen */
1559 focus->keyboard_count--;
1560 if (!focus->keyboard_count && focus->frame)
1561 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1562 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1563 weston_output_schedule_repaint(&focus->base);
1564 }
1565
1566 input->keyboard_focus = wl_surface_get_user_data(surface);
1567 input->keyboard_focus->keyboard_count++;
1568
1569 focus = input->keyboard_focus;
1570 if (focus->frame) {
1571 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1572 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1573 weston_output_schedule_repaint(&focus->base);
1574 }
1575
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001576
Daniel Stone50692802012-06-22 13:21:41 +01001577 /* XXX: If we get a modifier event immediately before the focus,
1578 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001579 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001580 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001581}
1582
1583static void
1584input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001585 struct wl_keyboard *keyboard,
1586 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001587 struct wl_surface *surface)
1588{
1589 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001590 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001591
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001592 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001593
1594 focus = input->keyboard_focus;
1595 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001596 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001597
1598 focus->keyboard_count--;
1599 if (!focus->keyboard_count && focus->frame) {
1600 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1601 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1602 weston_output_schedule_repaint(&focus->base);
1603 }
1604
1605 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001606}
1607
Daniel Stone37816df2012-05-16 18:45:18 +01001608static void
1609input_handle_key(void *data, struct wl_keyboard *keyboard,
1610 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1611{
1612 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001613
Daniel Stone50692802012-06-22 13:21:41 +01001614 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001615 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001616 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001617 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001618 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001619}
1620
Daniel Stone351eb612012-05-31 15:27:47 -04001621static void
Derek Foreman1281a362015-07-31 16:55:32 -05001622input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001623 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001624 uint32_t mods_latched, uint32_t mods_locked,
1625 uint32_t group)
1626{
Derek Foreman1281a362015-07-31 16:55:32 -05001627 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001628 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001629 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001630 uint32_t serial_out;
1631
1632 /* If we get a key event followed by a modifier event with the
1633 * same serial number, then we try to preserve those semantics by
1634 * reusing the same serial number on the way out too. */
1635 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001636 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001637 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001638 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001639
Derek Foreman1281a362015-07-31 16:55:32 -05001640 keyboard = weston_seat_get_keyboard(&input->base);
1641 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001642 mods_depressed, mods_latched,
1643 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001644 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001645}
1646
Jonny Lamb497994a2014-08-12 14:58:26 +02001647static void
1648input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1649 int32_t rate, int32_t delay)
1650{
1651 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001652 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001653
Giulio Camuffo954f1832014-10-11 18:27:30 +03001654 b->compositor->kb_repeat_rate = rate;
1655 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001656}
1657
Daniel Stone37816df2012-05-16 18:45:18 +01001658static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001659 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001660 input_handle_keyboard_enter,
1661 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001662 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001663 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001664 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001665};
1666
1667static void
Derek Foreman748c6952015-11-06 15:56:10 -06001668input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1669 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001670 struct wl_surface *surface, int32_t id,
1671 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001672{
1673 struct wayland_input *input = data;
1674 struct wayland_output *output;
1675 enum theme_location location;
1676 bool first_touch;
1677 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001678 double x, y;
1679
1680 x = wl_fixed_to_double(fixed_x);
1681 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001682
1683 first_touch = (input->touch_points == 0);
1684 input->touch_points++;
1685
1686 input->touch_focus = wl_surface_get_user_data(surface);
1687 output = input->touch_focus;
1688 if (!first_touch && !input->touch_active)
1689 return;
1690
1691 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001692 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001693
1694 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001695 x -= fx;
1696 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001697
1698 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1699 weston_output_schedule_repaint(&output->base);
1700
1701 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1702 input->touch_points--;
1703 wl_shell_surface_move(output->parent.shell_surface,
1704 input->parent.seat, serial);
1705 frame_status_clear(output->frame,
1706 FRAME_STATUS_MOVE);
1707 return;
1708 }
1709
1710 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1711 return;
1712 }
1713
1714 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1715
1716 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1717 input->touch_active = true;
1718}
1719
1720static void
1721input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1722 uint32_t serial, uint32_t time, int32_t id)
1723{
1724 struct wayland_input *input = data;
1725 struct wayland_output *output = input->touch_focus;
1726 bool active = input->touch_active;
1727
1728 input->touch_points--;
1729 if (input->touch_points == 0) {
1730 input->touch_focus = NULL;
1731 input->touch_active = false;
1732 }
1733
1734 if (!output)
1735 return;
1736
1737 if (output->frame) {
1738 frame_touch_up(output->frame, input, id);
1739
1740 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1741 wayland_output_destroy(&output->base);
1742 input->touch_focus = NULL;
1743 input->keyboard_focus = NULL;
1744 if (wl_list_empty(&input->backend->compositor->output_list))
1745 weston_compositor_exit(input->backend->compositor);
1746
1747 return;
1748 }
1749 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1750 weston_output_schedule_repaint(&output->base);
1751 }
1752
1753 if (active)
1754 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1755}
1756
1757static void
1758input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001759 uint32_t time, int32_t id,
1760 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001761{
1762 struct wayland_input *input = data;
1763 struct wayland_output *output = input->touch_focus;
1764 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001765 double x, y;
1766
1767 x = wl_fixed_to_double(fixed_x);
1768 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001769
1770 if (!output || !input->touch_active)
1771 return;
1772
1773 if (output->frame) {
1774 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001775 x -= fx;
1776 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001777 }
1778
1779 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1780
1781 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1782}
1783
1784static void
1785input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1786{
1787 struct wayland_input *input = data;
1788
1789 if (!input->touch_focus || !input->touch_active)
1790 return;
1791
1792 notify_touch_frame(&input->base);
1793}
1794
1795static void
1796input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1797{
1798 struct wayland_input *input = data;
1799
1800 if (!input->touch_focus || !input->touch_active)
1801 return;
1802
1803 notify_touch_cancel(&input->base);
1804}
1805
1806static const struct wl_touch_listener touch_listener = {
1807 input_handle_touch_down,
1808 input_handle_touch_up,
1809 input_handle_touch_motion,
1810 input_handle_touch_frame,
1811 input_handle_touch_cancel,
1812};
1813
1814
1815static void
Daniel Stone37816df2012-05-16 18:45:18 +01001816input_handle_capabilities(void *data, struct wl_seat *seat,
1817 enum wl_seat_capability caps)
1818{
1819 struct wayland_input *input = data;
1820
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001821 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1822 input->parent.pointer = wl_seat_get_pointer(seat);
1823 wl_pointer_set_user_data(input->parent.pointer, input);
1824 wl_pointer_add_listener(input->parent.pointer,
1825 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001826 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001827 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001828 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1829 wl_pointer_release(input->parent.pointer);
1830 else
1831 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001832 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001833 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001834 }
1835
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001836 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1837 input->parent.keyboard = wl_seat_get_keyboard(seat);
1838 wl_keyboard_set_user_data(input->parent.keyboard, input);
1839 wl_keyboard_add_listener(input->parent.keyboard,
1840 &keyboard_listener, input);
1841 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001842 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1843 wl_keyboard_release(input->parent.keyboard);
1844 else
1845 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001846 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001847 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001848 }
Derek Foreman748c6952015-11-06 15:56:10 -06001849
1850 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1851 input->parent.touch = wl_seat_get_touch(seat);
1852 wl_touch_set_user_data(input->parent.touch, input);
1853 wl_touch_add_listener(input->parent.touch,
1854 &touch_listener, input);
1855 weston_seat_init_touch(&input->base);
1856 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1857 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1858 wl_touch_release(input->parent.touch);
1859 else
1860 wl_touch_destroy(input->parent.touch);
1861 input->parent.touch = NULL;
1862 weston_seat_release_touch(&input->base);
1863 }
Daniel Stone37816df2012-05-16 18:45:18 +01001864}
1865
Jonny Lamb497994a2014-08-12 14:58:26 +02001866static void
1867input_handle_name(void *data, struct wl_seat *seat,
1868 const char *name)
1869{
1870}
1871
Daniel Stone37816df2012-05-16 18:45:18 +01001872static const struct wl_seat_listener seat_listener = {
1873 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001874 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001875};
1876
1877static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001878display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001879{
1880 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001881 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001882
Peter Huttererf3d62272013-08-08 11:57:05 +10001883 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001884 if (input == NULL)
1885 return;
1886
Giulio Camuffo954f1832014-10-11 18:27:30 +03001887 weston_seat_init(&input->base, b->compositor, "default");
1888 input->backend = b;
1889 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001890 &wl_seat_interface, version);
1891 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001892 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001893
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001894 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1895 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001896
1897 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001898 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001899
1900 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1901 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001902}
1903
1904static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001905wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1906 int32_t x, int32_t y,
1907 int32_t physical_width, int32_t physical_height,
1908 int32_t subpixel, const char *make,
1909 const char *model, int32_t transform)
1910{
1911 struct wayland_parent_output *output = data;
1912
1913 output->x = x;
1914 output->y = y;
1915 output->physical.width = physical_width;
1916 output->physical.height = physical_height;
1917 output->physical.subpixel = subpixel;
1918
1919 free(output->physical.make);
1920 output->physical.make = strdup(make);
1921 free(output->physical.model);
1922 output->physical.model = strdup(model);
1923
1924 output->transform = transform;
1925}
1926
1927static struct weston_mode *
1928find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1929{
1930 struct weston_mode *mode;
1931
1932 wl_list_for_each(mode, list, link) {
1933 if (mode->width == width && mode->height == height &&
1934 mode->refresh == refresh)
1935 return mode;
1936 }
1937
1938 mode = zalloc(sizeof *mode);
1939 if (!mode)
1940 return NULL;
1941
1942 mode->width = width;
1943 mode->height = height;
1944 mode->refresh = refresh;
1945 wl_list_insert(list, &mode->link);
1946
1947 return mode;
1948}
1949
1950static void
1951wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1952 uint32_t flags, int32_t width, int32_t height,
1953 int32_t refresh)
1954{
1955 struct wayland_parent_output *output = data;
1956 struct weston_mode *mode;
1957
1958 if (output->output) {
1959 mode = find_mode(&output->output->base.mode_list,
1960 width, height, refresh);
1961 if (!mode)
1962 return;
1963 mode->flags = flags;
1964 /* Do a mode-switch on current mode change? */
1965 } else {
1966 mode = find_mode(&output->mode_list, width, height, refresh);
1967 if (!mode)
1968 return;
1969 mode->flags = flags;
1970 if (flags & WL_OUTPUT_MODE_CURRENT)
1971 output->current_mode = mode;
1972 if (flags & WL_OUTPUT_MODE_PREFERRED)
1973 output->preferred_mode = mode;
1974 }
1975}
1976
1977static const struct wl_output_listener output_listener = {
1978 wayland_parent_output_geometry,
1979 wayland_parent_output_mode
1980};
1981
1982static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001983wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001984{
1985 struct wayland_parent_output *output;
1986
1987 output = zalloc(sizeof *output);
1988 if (!output)
1989 return;
1990
1991 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001992 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001993 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001994 if (!output->global) {
1995 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001996 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001997 }
1998
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001999 wl_output_add_listener(output->global, &output_listener, output);
2000
2001 output->scale = 0;
2002 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2003 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2004 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002005 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002006
Giulio Camuffo954f1832014-10-11 18:27:30 +03002007 if (b->sprawl_across_outputs) {
2008 wl_display_roundtrip(b->parent.wl_display);
2009 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002010 }
2011}
2012
2013static void
2014wayland_parent_output_destroy(struct wayland_parent_output *output)
2015{
2016 struct weston_mode *mode, *next;
2017
2018 if (output->output)
2019 wayland_output_destroy(&output->output->base);
2020
2021 wl_output_destroy(output->global);
2022 free(output->physical.make);
2023 free(output->physical.model);
2024
2025 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2026 wl_list_remove(&mode->link);
2027 free(mode);
2028 }
2029}
2030
2031static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002032registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2033 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002034{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002035 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002036
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002037 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002038 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002039 wl_registry_bind(registry, name,
2040 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002041 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002042 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002043 wl_registry_bind(registry, name,
2044 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002045 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002046 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002047 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002048 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002049 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002050 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002051 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002052 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002053 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002054 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002055 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002056 }
2057}
2058
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002059static void
2060registry_handle_global_remove(void *data, struct wl_registry *registry,
2061 uint32_t name)
2062{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002063 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002064 struct wayland_parent_output *output;
2065
Giulio Camuffo954f1832014-10-11 18:27:30 +03002066 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002067 if (output->id == name)
2068 wayland_parent_output_destroy(output);
2069}
2070
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002071static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002072 registry_handle_global,
2073 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002074};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002075
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002076static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002077wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002078{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002079 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002080 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002081
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002082 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002083 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002084 return 0;
2085 }
2086
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002087 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002088 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002089 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002090 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002091
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002092 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002093 count = wl_display_dispatch_pending(b->parent.wl_display);
2094 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002095 }
2096
2097 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002098}
2099
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002100static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002101wayland_restore(struct weston_compositor *ec)
2102{
2103}
2104
2105static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002106wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002107{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002108 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002109
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002110 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002111
Giulio Camuffo954f1832014-10-11 18:27:30 +03002112 if (b->parent.shm)
2113 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002114
Giulio Camuffo954f1832014-10-11 18:27:30 +03002115 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002116}
2117
Jason Ekstrand7744f712013-10-27 22:24:55 -05002118static const char *left_ptrs[] = {
2119 "left_ptr",
2120 "default",
2121 "top_left_arrow",
2122 "left-arrow"
2123};
2124
2125static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002126create_cursor(struct wayland_backend *b,
2127 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002128{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002129 unsigned int i;
2130
Benoit Gschwind244ff792016-04-28 20:33:11 +02002131 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2132 config->cursor_size,
2133 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002134 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002135 fprintf(stderr, "could not load cursor theme\n");
2136 return;
2137 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002138
Giulio Camuffo954f1832014-10-11 18:27:30 +03002139 b->cursor = NULL;
2140 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2141 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002142 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002143 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002144 fprintf(stderr, "could not load left cursor\n");
2145 return;
2146 }
2147}
2148
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002149static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002150fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2151 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002152{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002153 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002154 struct wayland_input *input = NULL;
2155
Giulio Camuffo954f1832014-10-11 18:27:30 +03002156 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002157 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002158 break;
2159
2160 if (!input || !input->output)
2161 return;
2162
2163 if (input->output->frame)
2164 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2165 else
2166 wayland_output_set_windowed(input->output);
2167
2168 weston_output_schedule_repaint(&input->output->base);
2169}
2170
Giulio Camuffo954f1832014-10-11 18:27:30 +03002171static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002172wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002173 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002174{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002175 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002176 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002177 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002178
Giulio Camuffo954f1832014-10-11 18:27:30 +03002179 b = zalloc(sizeof *b);
2180 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002181 return NULL;
2182
Giulio Camuffo954f1832014-10-11 18:27:30 +03002183 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002184 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002185 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002186
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002187 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002188 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002189 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002190 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002191 }
2192
Giulio Camuffo954f1832014-10-11 18:27:30 +03002193 wl_list_init(&b->parent.output_list);
2194 wl_list_init(&b->input_list);
2195 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2196 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2197 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002198
Benoit Gschwind244ff792016-04-28 20:33:11 +02002199 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002200
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002201 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002202
Giulio Camuffo954f1832014-10-11 18:27:30 +03002203 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002204 gl_renderer = weston_load_module("gl-renderer.so",
2205 "gl_renderer_interface");
2206 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002207 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002208 }
2209
Giulio Camuffo954f1832014-10-11 18:27:30 +03002210 if (!b->use_pixman) {
2211 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002212 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002213 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002214 gl_renderer->alpha_attribs,
2215 NULL,
2216 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002217 weston_log("Failed to initialize the GL renderer; "
2218 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002219 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002220 }
2221 }
2222
Giulio Camuffo954f1832014-10-11 18:27:30 +03002223 if (b->use_pixman) {
2224 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002225 weston_log("Failed to initialize pixman renderer\n");
2226 goto err_display;
2227 }
2228 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002229
Giulio Camuffo954f1832014-10-11 18:27:30 +03002230 b->base.destroy = wayland_destroy;
2231 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002232
Giulio Camuffo954f1832014-10-11 18:27:30 +03002233 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002234
Giulio Camuffo954f1832014-10-11 18:27:30 +03002235 fd = wl_display_get_fd(b->parent.wl_display);
2236 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002237 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002238 wayland_backend_handle_event, b);
2239 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002240 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002241
Giulio Camuffo954f1832014-10-11 18:27:30 +03002242 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002243
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002244 if (compositor->renderer->import_dmabuf) {
2245 if (linux_dmabuf_setup(compositor) < 0)
2246 weston_log("Error: initializing dmabuf "
2247 "support failed.\n");
2248 }
2249
Giulio Camuffo954f1832014-10-11 18:27:30 +03002250 compositor->backend = &b->base;
2251 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002252err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002253 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002254err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002255 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002256 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002257 return NULL;
2258}
2259
2260static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002261wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002262{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002263 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002264
Giulio Camuffo954f1832014-10-11 18:27:30 +03002265 if (b->theme)
2266 theme_destroy(b->theme);
2267 if (b->frame_device)
2268 cairo_device_destroy(b->frame_device);
2269 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002270
Giulio Camuffo954f1832014-10-11 18:27:30 +03002271 weston_compositor_shutdown(b->compositor);
2272 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002273}
2274
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002275static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002276config_init_to_defaults(struct weston_wayland_backend_config *config)
2277{
2278}
2279
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002280WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002281backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002282 struct weston_backend_config *config_base)
2283{
2284 struct wayland_backend *b;
2285 struct wayland_output *output;
2286 struct wayland_parent_output *poutput;
2287 struct weston_wayland_backend_config new_config;
2288 int x, count;
2289
Benoit Gschwinde091b452016-05-10 22:47:48 +02002290 if (config_base == NULL ||
2291 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2292 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2293 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002294 return -1;
2295 }
2296
2297 config_init_to_defaults(&new_config);
2298 memcpy(&new_config, config_base, config_base->struct_size);
2299
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002300 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002301
Giulio Camuffo954f1832014-10-11 18:27:30 +03002302 if (!b)
2303 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002304
Benoit Gschwind37a68072016-04-28 20:33:08 +02002305 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002306 b->sprawl_across_outputs = 1;
2307 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002308
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 wl_list_for_each(poutput, &b->parent.output_list, link)
2310 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002311
Giulio Camuffo954f1832014-10-11 18:27:30 +03002312 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002313 }
2314
Benoit Gschwind37a68072016-04-28 20:33:08 +02002315 if (new_config.fullscreen) {
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002316 if (new_config.num_outputs != 1 || !new_config.outputs)
2317 goto err_outputs;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002318
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002319 output = wayland_output_create_for_config(b,
2320 &new_config.outputs[0],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002321 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002322 if (!output)
2323 goto err_outputs;
2324
Axel Davydd8b88d2013-11-17 21:34:16 +01002325 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002326 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002327 }
2328
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002329 x = 0;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002330 for (count = 0; count < new_config.num_outputs; ++count) {
2331 output = wayland_output_create_for_config(b, &new_config.outputs[count],
Benoit Gschwind830b7882016-04-28 20:33:12 +02002332 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002333 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002334 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002335 if (wayland_output_set_windowed(output))
2336 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002337
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002338 x += output->base.width;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002339 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002340
Giulio Camuffo954f1832014-10-11 18:27:30 +03002341 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002342 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002343 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002344 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002345
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002346err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002347 wayland_backend_destroy(b);
2348 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002349}