blob: 2f6db05ca0ad8cd13ecbc92cae20c502d49aaecd [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003 * Copyright © 2013 Jason Ekstrand
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010028
29#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010035#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060036#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkebe014562011-02-18 17:04:24 +010038#include <wayland-client.h>
39#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050040#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010041
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010043#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050044#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/image-loader.h"
47#include "shared/os-compatibility.h"
48#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080049#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020050#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020051#include "linux-dmabuf.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052
Jason Ekstrand48ce4212013-10-27 22:25:02 -050053#define WINDOW_TITLE "Weston Compositor"
54
Benoit Gschwind830b7882016-04-28 20:33:12 +020055struct weston_wayland_backend_output_config {
56 int width;
57 int height;
58 char *name;
59 uint32_t transform;
60 int32_t scale;
61};
62
Benoit Gschwind37a68072016-04-28 20:33:08 +020063struct weston_wayland_backend_config {
64 int use_pixman;
65 int sprawl;
66 char *display_name;
67 int fullscreen;
Benoit Gschwind244ff792016-04-28 20:33:11 +020068 char *cursor_theme;
69 int cursor_size;
Benoit Gschwind37a68072016-04-28 20:33:08 +020070};
71
Giulio Camuffo954f1832014-10-11 18:27:30 +030072struct wayland_backend {
73 struct weston_backend base;
74 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075
76 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040077 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010079 struct wl_compositor *compositor;
80 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080081 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020082 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010083
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050084 struct wl_list output_list;
85
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010086 struct wl_event_source *wl_source;
87 uint32_t event_mask;
88 } parent;
89
Jason Ekstrandff2fd462013-10-27 22:24:58 -050090 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050091 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050092
Jason Ekstrand7744f712013-10-27 22:24:55 -050093 struct theme *theme;
94 cairo_device_t *frame_device;
95 struct wl_cursor_theme *cursor_theme;
96 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050097
Jason Ekstrand06ced802013-11-07 20:13:29 -060098 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010099};
100
101struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500102 struct weston_output base;
103
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100104 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500105 int draw_initial_frame;
106 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500107
108 struct wl_output *output;
109 uint32_t global_id;
110
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500111 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600112 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100113 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500114
Jason Ekstrand7744f712013-10-27 22:24:55 -0500115 int keyboard_count;
116
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600117 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500118 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500119
Jason Ekstrand7744f712013-10-27 22:24:55 -0500120 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500121 struct wl_egl_window *egl_window;
122 struct {
123 cairo_surface_t *top;
124 cairo_surface_t *left;
125 cairo_surface_t *right;
126 cairo_surface_t *bottom;
127 } border;
128 } gl;
129
130 struct {
131 struct wl_list buffers;
132 struct wl_list free_buffers;
133 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500134
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500135 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500136 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100137};
138
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500139struct wayland_parent_output {
140 struct wayland_output *output;
141 struct wl_list link;
142
143 struct wl_output *global;
144 uint32_t id;
145
146 struct {
147 char *make;
148 char *model;
149 int32_t width, height;
150 uint32_t subpixel;
151 } physical;
152
153 int32_t x, y;
154 uint32_t transform;
155 uint32_t scale;
156
157 struct wl_list mode_list;
158 struct weston_mode *preferred_mode;
159 struct weston_mode *current_mode;
160};
161
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500162struct wayland_shm_buffer {
163 struct wayland_output *output;
164 struct wl_list link;
165 struct wl_list free_link;
166
167 struct wl_buffer *buffer;
168 void *data;
169 size_t size;
170 pixman_region32_t damage;
171 int frame_damaged;
172
173 pixman_image_t *pm_image;
174 cairo_surface_t *c_surface;
175};
176
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100177struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400178 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300179 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100180 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500181
182 struct {
183 struct wl_seat *seat;
184 struct wl_pointer *pointer;
185 struct wl_keyboard *keyboard;
186 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500187
188 struct {
189 struct wl_surface *surface;
190 int32_t hx, hy;
191 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500192 } parent;
193
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500194 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100195 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400196 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600197 uint32_t touch_points;
198 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600199 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600200 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600201
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400202 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600203 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500204 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000205
206 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100207};
208
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300209struct gl_renderer_interface *gl_renderer;
210
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500211static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500212wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
213{
214 cairo_surface_destroy(buffer->c_surface);
215 pixman_image_unref(buffer->pm_image);
216
217 wl_buffer_destroy(buffer->buffer);
218 munmap(buffer->data, buffer->size);
219
220 pixman_region32_fini(&buffer->damage);
221
222 wl_list_remove(&buffer->link);
223 wl_list_remove(&buffer->free_link);
224 free(buffer);
225}
226
227static void
228buffer_release(void *data, struct wl_buffer *buffer)
229{
230 struct wayland_shm_buffer *sb = data;
231
232 if (sb->output) {
233 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
234 } else {
235 wayland_shm_buffer_destroy(sb);
236 }
237}
238
239static const struct wl_buffer_listener buffer_listener = {
240 buffer_release
241};
242
243static struct wayland_shm_buffer *
244wayland_output_get_shm_buffer(struct wayland_output *output)
245{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300246 struct wayland_backend *b =
247 (struct wayland_backend *) output->base.compositor->backend;
248 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500249 struct wayland_shm_buffer *sb;
250
251 struct wl_shm_pool *pool;
252 int width, height, stride;
253 int32_t fx, fy;
254 int fd;
255 unsigned char *data;
256
257 if (!wl_list_empty(&output->shm.free_buffers)) {
258 sb = container_of(output->shm.free_buffers.next,
259 struct wayland_shm_buffer, free_link);
260 wl_list_remove(&sb->free_link);
261 wl_list_init(&sb->free_link);
262
263 return sb;
264 }
265
266 if (output->frame) {
267 width = frame_width(output->frame);
268 height = frame_height(output->frame);
269 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500270 width = output->base.current_mode->width;
271 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500272 }
273
274 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
275
276 fd = os_create_anonymous_file(height * stride);
277 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000278 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500279 return NULL;
280 }
281
282 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
283 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000284 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500285 close(fd);
286 return NULL;
287 }
288
289 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000290 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200291 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000292 close(fd);
293 free(data);
294 return NULL;
295 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500296
297 sb->output = output;
298 wl_list_init(&sb->free_link);
299 wl_list_insert(&output->shm.buffers, &sb->link);
300
301 pixman_region32_init_rect(&sb->damage, 0, 0,
302 output->base.width, output->base.height);
303 sb->frame_damaged = 1;
304
305 sb->data = data;
306 sb->size = height * stride;
307
308 pool = wl_shm_create_pool(shm, fd, sb->size);
309
310 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
311 width, height,
312 stride,
313 WL_SHM_FORMAT_ARGB8888);
314 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
315 wl_shm_pool_destroy(pool);
316 close(fd);
317
318 memset(data, 0, sb->size);
319
320 sb->c_surface =
321 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
322 width, height, stride);
323
324 fx = 0;
325 fy = 0;
326 if (output->frame)
327 frame_interior(output->frame, &fx, &fy, 0, 0);
328 sb->pm_image =
329 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
330 (uint32_t *)(data + fy * stride) + fx,
331 stride);
332
333 return sb;
334}
335
336static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500337frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400338{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500339 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400340 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400341
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500342 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400343
344 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300345
346 /*
347 * This is the fallback case, where Presentation extension is not
348 * available from the parent compositor. We do not know the base for
349 * 'time', so we cannot feed it to finish_frame(). Do the only thing
350 * we can, and pretend finish_frame time is when we process this
351 * event.
352 */
353 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200354 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400355}
356
357static const struct wl_callback_listener frame_listener = {
358 frame_done
359};
360
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500361static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200362draw_initial_frame(struct wayland_output *output)
363{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500364 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200365
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500366 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200367
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 /* If we are rendering with GL, then orphan it so that it gets
369 * destroyed immediately */
370 if (output->gl.egl_window)
371 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500373 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500374 wl_surface_damage(output->parent.surface, 0, 0,
375 output->base.current_mode->width,
376 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200377}
378
379static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500380wayland_output_update_gl_border(struct wayland_output *output)
381{
382 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
383 cairo_t *cr;
384
385 if (!output->frame)
386 return;
387 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
388 return;
389
390 fwidth = frame_width(output->frame);
391 fheight = frame_height(output->frame);
392 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
393
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 if (!output->gl.border.top)
395 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
397 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 frame_repaint(output->frame, cr);
400 cairo_destroy(cr);
401 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
402 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500403 cairo_image_surface_get_stride(output->gl.border.top) / 4,
404 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500405
406
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500407 if (!output->gl.border.left)
408 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
410 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500411 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500412 cairo_translate(cr, 0, -iy);
413 frame_repaint(output->frame, cr);
414 cairo_destroy(cr);
415 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
416 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500417 cairo_image_surface_get_stride(output->gl.border.left) / 4,
418 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500419
420
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500421 if (!output->gl.border.right)
422 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500423 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
424 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500425 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500426 cairo_translate(cr, -(iwidth + ix), -iy);
427 frame_repaint(output->frame, cr);
428 cairo_destroy(cr);
429 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
430 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500431 cairo_image_surface_get_stride(output->gl.border.right) / 4,
432 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500433
434
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500435 if (!output->gl.border.bottom)
436 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500437 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
438 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500439 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500440 cairo_translate(cr, 0, -(iy + iheight));
441 frame_repaint(output->frame, cr);
442 cairo_destroy(cr);
443 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
444 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
446 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500447}
448
449static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200450wayland_output_start_repaint_loop(struct weston_output *output_base)
451{
452 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300453 struct wayland_backend *wb =
454 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200455 struct wl_callback *callback;
456
457 /* If this is the initial frame, we need to attach a buffer so that
458 * the compositor can map the surface and include it in its render
459 * loop. If the surface doesn't end up in the render loop, the frame
460 * callback won't be invoked. The buffer is transparent and of the
461 * same size as the future real output buffer. */
462 if (output->parent.draw_initial_frame) {
463 output->parent.draw_initial_frame = 0;
464
465 draw_initial_frame(output);
466 }
467
468 callback = wl_surface_frame(output->parent.surface);
469 wl_callback_add_listener(callback, &frame_listener, output);
470 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300471 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200472}
473
David Herrmann1edf44c2013-10-22 17:11:26 +0200474static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500475wayland_output_repaint_gl(struct weston_output *output_base,
476 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400477{
478 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400479 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400480 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600481
Kristian Høgsberg33418202011-08-16 23:01:28 -0400482 callback = wl_surface_frame(output->parent.surface);
483 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100484
Jason Ekstrand7744f712013-10-27 22:24:55 -0500485 wayland_output_update_gl_border(output);
486
Pekka Paalanenbc106382012-10-10 12:49:31 +0300487 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200488
489 pixman_region32_subtract(&ec->primary_plane.damage,
490 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200491 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100492}
493
Matt Roper361d2ad2011-08-29 13:52:23 -0700494static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500495wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
496{
497 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
498 cairo_t *cr;
499
500 if (!buffer->output->frame || !buffer->frame_damaged)
501 return;
502
503 cr = cairo_create(buffer->c_surface);
504
505 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
506 fwidth = frame_width(buffer->output->frame);
507 fheight = frame_height(buffer->output->frame);
508
509 /* Set the clip so we don't unnecisaraly damage the surface */
510 cairo_move_to(cr, ix, iy);
511 cairo_rel_line_to(cr, iwidth, 0);
512 cairo_rel_line_to(cr, 0, iheight);
513 cairo_rel_line_to(cr, -iwidth, 0);
514 cairo_line_to(cr, ix, iy);
515 cairo_line_to(cr, 0, iy);
516 cairo_line_to(cr, 0, fheight);
517 cairo_line_to(cr, fwidth, fheight);
518 cairo_line_to(cr, fwidth, 0);
519 cairo_line_to(cr, 0, 0);
520 cairo_line_to(cr, 0, iy);
521 cairo_close_path(cr);
522 cairo_clip(cr);
523
524 /* Draw using a pattern so that the final result gets clipped */
525 cairo_push_group(cr);
526 frame_repaint(buffer->output->frame, cr);
527 cairo_pop_group_to_source(cr);
528 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
529 cairo_paint(cr);
530
531 cairo_destroy(cr);
532}
533
534static void
535wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
536{
537 pixman_region32_t damage;
538 pixman_box32_t *rects;
539 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
540 int i, n;
541
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500542 pixman_region32_init(&damage);
543 weston_transformed_region(sb->output->base.width,
544 sb->output->base.height,
545 sb->output->base.transform,
546 sb->output->base.current_scale,
547 &sb->damage, &damage);
548
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500549 if (sb->output->frame) {
550 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
551 fwidth = frame_width(sb->output->frame);
552 fheight = frame_height(sb->output->frame);
553
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500554 pixman_region32_translate(&damage, ix, iy);
555
556 if (sb->frame_damaged) {
557 pixman_region32_union_rect(&damage, &damage,
558 0, 0, fwidth, iy);
559 pixman_region32_union_rect(&damage, &damage,
560 0, iy, ix, iheight);
561 pixman_region32_union_rect(&damage, &damage,
562 ix + iwidth, iy,
563 fwidth - (ix + iwidth), iheight);
564 pixman_region32_union_rect(&damage, &damage,
565 0, iy + iheight,
566 fwidth, fheight - (iy + iheight));
567 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500568 }
569
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500570 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500571 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
572 for (i = 0; i < n; ++i)
573 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
574 rects[i].y1, rects[i].x2 - rects[i].x1,
575 rects[i].y2 - rects[i].y1);
576
577 if (sb->output->frame)
578 pixman_region32_fini(&damage);
579}
580
581static int
582wayland_output_repaint_pixman(struct weston_output *output_base,
583 pixman_region32_t *damage)
584{
585 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300586 struct wayland_backend *b =
587 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500588 struct wl_callback *callback;
589 struct wayland_shm_buffer *sb;
590
591 if (output->frame) {
592 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
593 wl_list_for_each(sb, &output->shm.buffers, link)
594 sb->frame_damaged = 1;
595 }
596
597 wl_list_for_each(sb, &output->shm.buffers, link)
598 pixman_region32_union(&sb->damage, &sb->damage, damage);
599
600 sb = wayland_output_get_shm_buffer(output);
601
602 wayland_output_update_shm_border(sb);
603 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300604 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500605
606 wayland_shm_buffer_attach(sb);
607
608 callback = wl_surface_frame(output->parent.surface);
609 wl_callback_add_listener(callback, &frame_listener, output);
610 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500612
613 pixman_region32_fini(&sb->damage);
614 pixman_region32_init(&sb->damage);
615 sb->frame_damaged = 0;
616
Giulio Camuffo954f1832014-10-11 18:27:30 +0300617 pixman_region32_subtract(&b->compositor->primary_plane.damage,
618 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500619 return 0;
620}
621
622static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500623wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700624{
625 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300626 struct wayland_backend *b =
627 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700628
Giulio Camuffo954f1832014-10-11 18:27:30 +0300629 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630 pixman_renderer_output_destroy(output_base);
631 } else {
632 gl_renderer->output_destroy(output_base);
633 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100634
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500635 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500636 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500637 if (output->parent.shell_surface)
638 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500639
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600640 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500641 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600642
643 cairo_surface_destroy(output->gl.border.top);
644 cairo_surface_destroy(output->gl.border.left);
645 cairo_surface_destroy(output->gl.border.right);
646 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500647
648 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700649 free(output);
650
651 return;
652}
653
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300654static const struct wl_shell_surface_listener shell_surface_listener;
655
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200656static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500657wayland_output_init_gl_renderer(struct wayland_output *output)
658{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500659 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500660
661 if (output->frame) {
662 fwidth = frame_width(output->frame);
663 fheight = frame_height(output->frame);
664 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500665 fwidth = output->base.current_mode->width;
666 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500667 }
668
669 output->gl.egl_window =
670 wl_egl_window_create(output->parent.surface,
671 fwidth, fheight);
672 if (!output->gl.egl_window) {
673 weston_log("failure to create wl_egl_window\n");
674 return -1;
675 }
676
677 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000678 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100679 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000680 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500681 NULL,
682 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500683 goto cleanup_window;
684
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500685 return 0;
686
687cleanup_window:
688 wl_egl_window_destroy(output->gl.egl_window);
689 return -1;
690}
691
692static int
693wayland_output_init_pixman_renderer(struct wayland_output *output)
694{
695 return pixman_renderer_output_create(&output->base);
696}
697
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600698static void
699wayland_output_resize_surface(struct wayland_output *output)
700{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300701 struct wayland_backend *b =
702 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600703 struct wayland_shm_buffer *buffer, *next;
704 int32_t ix, iy, iwidth, iheight;
705 int32_t width, height;
706 struct wl_region *region;
707
708 width = output->base.current_mode->width;
709 height = output->base.current_mode->height;
710
711 if (output->frame) {
712 frame_resize_inside(output->frame, width, height);
713
714 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300715 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600716 wl_region_add(region, ix, iy, iwidth, iheight);
717 wl_surface_set_input_region(output->parent.surface, region);
718 wl_region_destroy(region);
719
720 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300721 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600722 wl_region_add(region, ix, iy, iwidth, iheight);
723 wl_surface_set_opaque_region(output->parent.surface, region);
724 wl_region_destroy(region);
725
726 width = frame_width(output->frame);
727 height = frame_height(output->frame);
728 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300729 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600730 wl_region_add(region, 0, 0, width, height);
731 wl_surface_set_input_region(output->parent.surface, region);
732 wl_region_destroy(region);
733
Giulio Camuffo954f1832014-10-11 18:27:30 +0300734 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600735 wl_region_add(region, 0, 0, width, height);
736 wl_surface_set_opaque_region(output->parent.surface, region);
737 wl_region_destroy(region);
738 }
739
740 if (output->gl.egl_window) {
741 wl_egl_window_resize(output->gl.egl_window,
742 width, height, 0, 0);
743
744 /* These will need to be re-created due to the resize */
745 gl_renderer->output_set_border(&output->base,
746 GL_RENDERER_BORDER_TOP,
747 0, 0, 0, NULL);
748 cairo_surface_destroy(output->gl.border.top);
749 output->gl.border.top = NULL;
750 gl_renderer->output_set_border(&output->base,
751 GL_RENDERER_BORDER_LEFT,
752 0, 0, 0, NULL);
753 cairo_surface_destroy(output->gl.border.left);
754 output->gl.border.left = NULL;
755 gl_renderer->output_set_border(&output->base,
756 GL_RENDERER_BORDER_RIGHT,
757 0, 0, 0, NULL);
758 cairo_surface_destroy(output->gl.border.right);
759 output->gl.border.right = NULL;
760 gl_renderer->output_set_border(&output->base,
761 GL_RENDERER_BORDER_BOTTOM,
762 0, 0, 0, NULL);
763 cairo_surface_destroy(output->gl.border.bottom);
764 output->gl.border.bottom = NULL;
765 }
766
767 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500768 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600769 wayland_shm_buffer_destroy(buffer);
770 /* These will get thrown away when they get released */
771 wl_list_for_each(buffer, &output->shm.buffers, link)
772 buffer->output = NULL;
773}
774
775static int
776wayland_output_set_windowed(struct wayland_output *output)
777{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300778 struct wayland_backend *b =
779 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600780 int tlen;
781 char *title;
782
783 if (output->frame)
784 return 0;
785
786 if (output->name) {
787 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
788 title = malloc(tlen + 1);
789 if (!title)
790 return -1;
791
792 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
793 } else {
794 title = strdup(WINDOW_TITLE);
795 }
796
Giulio Camuffo954f1832014-10-11 18:27:30 +0300797 if (!b->theme) {
798 b->theme = theme_create();
799 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600800 free(title);
801 return -1;
802 }
803 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300804 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600805 FRAME_BUTTON_CLOSE, title);
806 free(title);
807 if (!output->frame)
808 return -1;
809
810 if (output->keyboard_count)
811 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
812
813 wayland_output_resize_surface(output);
814
815 wl_shell_surface_set_toplevel(output->parent.shell_surface);
816
817 return 0;
818}
819
820static void
821wayland_output_set_fullscreen(struct wayland_output *output,
822 enum wl_shell_surface_fullscreen_method method,
823 uint32_t framerate, struct wl_output *target)
824{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300825 struct wayland_backend *b =
826 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500827
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600828 if (output->frame) {
829 frame_destroy(output->frame);
830 output->frame = NULL;
831 }
832
833 wayland_output_resize_surface(output);
834
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500835 if (output->parent.shell_surface) {
836 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
837 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300838 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800839 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
840 output->parent.surface,
841 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500842 }
843}
844
845static struct weston_mode *
846wayland_output_choose_mode(struct wayland_output *output,
847 struct weston_mode *ref_mode)
848{
849 struct weston_mode *mode;
850
851 /* First look for an exact match */
852 wl_list_for_each(mode, &output->base.mode_list, link)
853 if (mode->width == ref_mode->width &&
854 mode->height == ref_mode->height &&
855 mode->refresh == ref_mode->refresh)
856 return mode;
857
858 /* If we can't find an exact match, ignore refresh and try again */
859 wl_list_for_each(mode, &output->base.mode_list, link)
860 if (mode->width == ref_mode->width &&
861 mode->height == ref_mode->height)
862 return mode;
863
864 /* Yeah, we failed */
865 return NULL;
866}
867
868enum mode_status {
869 MODE_STATUS_UNKNOWN,
870 MODE_STATUS_SUCCESS,
871 MODE_STATUS_FAIL,
872 MODE_STATUS_CANCEL,
873};
874
875static void
876mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800877 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500878{
879 enum mode_status *value = data;
880
881 printf("Mode switch successful\n");
882
883 *value = MODE_STATUS_SUCCESS;
884}
885
886static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800887mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500888{
889 enum mode_status *value = data;
890
891 printf("Mode switch failed\n");
892
893 *value = MODE_STATUS_FAIL;
894}
895
896static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800897mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500898{
899 enum mode_status *value = data;
900
901 printf("Mode switch cancelled\n");
902
903 *value = MODE_STATUS_CANCEL;
904}
905
Jonas Ådahl496adb32015-11-17 16:00:27 +0800906struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500907 mode_feedback_successful,
908 mode_feedback_failed,
909 mode_feedback_cancelled,
910};
911
912static int
913wayland_output_switch_mode(struct weston_output *output_base,
914 struct weston_mode *mode)
915{
916 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300917 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500918 struct wl_surface *old_surface;
919 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800920 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500921 enum mode_status mode_status;
922 int ret = 0;
923
924 if (output_base == NULL) {
925 weston_log("output is NULL.\n");
926 return -1;
927 }
928
929 if (mode == NULL) {
930 weston_log("mode is NULL.\n");
931 return -1;
932 }
933
Giulio Camuffo954f1832014-10-11 18:27:30 +0300934 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500935
Giulio Camuffo954f1832014-10-11 18:27:30 +0300936 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500937 return -1;
938
939 mode = wayland_output_choose_mode(output, mode);
940 if (mode == NULL)
941 return -1;
942
943 if (output->base.current_mode == mode)
944 return 0;
945
946 old_mode = output->base.current_mode;
947 old_surface = output->parent.surface;
948 output->base.current_mode = mode;
949 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300950 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500951 wl_surface_set_user_data(output->parent.surface, output);
952
953 /* Blow the old buffers because we changed size/surfaces */
954 wayland_output_resize_surface(output);
955
956 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800957 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
958 output->parent.surface,
959 output->parent.output,
960 mode->refresh);
961 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
962 &mode_feedback_listener,
963 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500964
965 /* This should kick-start things again */
966 output->parent.draw_initial_frame = 1;
967 wayland_output_start_repaint_loop(&output->base);
968
969 mode_status = MODE_STATUS_UNKNOWN;
970 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300971 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500972
Jonas Ådahl496adb32015-11-17 16:00:27 +0800973 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500974
975 if (mode_status == MODE_STATUS_FAIL) {
976 output->base.current_mode = old_mode;
977 wl_surface_destroy(output->parent.surface);
978 output->parent.surface = old_surface;
979 wayland_output_resize_surface(output);
980
981 return -1;
982 }
983
984 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
985 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
986
Giulio Camuffo954f1832014-10-11 18:27:30 +0300987 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500988 pixman_renderer_output_destroy(output_base);
989 if (wayland_output_init_pixman_renderer(output) < 0)
990 goto err_output;
991 } else {
992 gl_renderer->output_destroy(output_base);
993 wl_egl_window_destroy(output->gl.egl_window);
994 if (wayland_output_init_gl_renderer(output) < 0)
995 goto err_output;
996 }
997 wl_surface_destroy(old_surface);
998
999 weston_output_schedule_repaint(&output->base);
1000
1001 return 0;
1002
1003err_output:
1004 /* XXX */
1005 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001006}
1007
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001008static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001009wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001010 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001011 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001012{
1013 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001014 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001015
1016 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1017 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001018
Peter Huttererf3d62272013-08-08 11:57:05 +10001019 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001020 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001021 return NULL;
1022
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001023 output->name = name ? strdup(name) : NULL;
1024 output->base.make = "waywayland";
1025 output->base.model = "none";
1026
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001027 output_width = width * scale;
1028 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001029
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001030 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001031 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001032 if (!output->parent.surface)
1033 goto err_name;
1034 wl_surface_set_user_data(output->parent.surface, output);
1035
1036 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001037
Giulio Camuffo954f1832014-10-11 18:27:30 +03001038 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001039 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001040 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001041 output->parent.surface);
1042 if (!output->parent.shell_surface)
1043 goto err_surface;
1044 wl_shell_surface_add_listener(output->parent.shell_surface,
1045 &shell_surface_listener, output);
1046 }
1047
Giulio Camuffo954f1832014-10-11 18:27:30 +03001048 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001049 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1050 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001051 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001052 if (!width)
1053 output_width = output->parent.configure_width;
1054 if (!height)
1055 output_height = output->parent.configure_height;
1056 }
1057
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001058 output->mode.flags =
1059 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001060 output->mode.width = output_width;
1061 output->mode.height = output_height;
1062 output->mode.refresh = 60000;
1063 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001064 wl_list_init(&output->base.mode_list);
1065 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001066 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001067
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001068 wl_list_init(&output->shm.buffers);
1069 wl_list_init(&output->shm.free_buffers);
1070
Giulio Camuffo954f1832014-10-11 18:27:30 +03001071 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001072 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001073
Giulio Camuffo954f1832014-10-11 18:27:30 +03001074 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001075 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001076 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001077 output->base.repaint = wayland_output_repaint_pixman;
1078 } else {
1079 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001080 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001081 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001082 }
1083
Jonas Ådahle5a12252013-04-05 23:07:11 +02001084 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001085 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001086 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001087 output->base.set_backlight = NULL;
1088 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001089 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001090
Giulio Camuffo954f1832014-10-11 18:27:30 +03001091 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001092
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001093 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001094
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001095err_output:
1096 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001097 if (output->parent.shell_surface)
1098 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001099err_surface:
1100 wl_surface_destroy(output->parent.surface);
1101err_name:
1102 free(output->name);
1103
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001104 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001105 free(output);
1106
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001107 return NULL;
1108}
1109
Benoit Gschwind830b7882016-04-28 20:33:12 +02001110static void
1111wayland_output_init_from_config(struct weston_wayland_backend_output_config *output,
1112 struct weston_config_section *config_section,
1113 int option_width, int option_height,
1114 int option_scale)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001115{
Benoit Gschwind830b7882016-04-28 20:33:12 +02001116 char *mode, *t, *str;
Derek Foreman64a3df02014-10-23 12:24:18 -05001117 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001118
Benoit Gschwind830b7882016-04-28 20:33:12 +02001119 weston_config_section_get_string(config_section, "name", &output->name,
1120 NULL);
1121 if (output->name) {
1122 slen = strlen(output->name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001123 slen += strlen(WINDOW_TITLE " - ");
1124 str = malloc(slen + 1);
1125 if (str)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001126 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1127 output->name);
1128 free(output->name);
1129 output->name = str;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001130 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02001131 if (!output->name)
1132 output->name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001133
1134 weston_config_section_get_string(config_section,
1135 "mode", &mode, "1024x600");
Benoit Gschwind830b7882016-04-28 20:33:12 +02001136 if (sscanf(mode, "%dx%d", &output->width, &output->height) != 2) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001137 weston_log("Invalid mode \"%s\" for output %s\n",
Benoit Gschwind830b7882016-04-28 20:33:12 +02001138 mode, output->name);
1139 output->width = 1024;
1140 output->height = 640;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001141 }
1142 free(mode);
1143
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001144 if (option_width)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001145 output->width = option_width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001146 if (option_height)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001147 output->height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001148
Benoit Gschwind830b7882016-04-28 20:33:12 +02001149 weston_config_section_get_int(config_section, "scale", &output->scale, 1);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001150
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001151 if (option_scale)
Benoit Gschwind830b7882016-04-28 20:33:12 +02001152 output->scale = option_scale;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001153
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001154 weston_config_section_get_string(config_section,
1155 "transform", &t, "normal");
Benoit Gschwind830b7882016-04-28 20:33:12 +02001156 if (weston_parse_transform(t, &output->transform) < 0)
Derek Foreman64a3df02014-10-23 12:24:18 -05001157 weston_log("Invalid transform \"%s\" for output %s\n",
Benoit Gschwind830b7882016-04-28 20:33:12 +02001158 t, output->name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001159 free(t);
1160
Benoit Gschwind830b7882016-04-28 20:33:12 +02001161}
1162
1163static struct wayland_output *
1164wayland_output_create_for_config(struct wayland_backend *b,
1165 struct weston_wayland_backend_output_config *oc,
1166 int fullscreen, int32_t x, int32_t y)
1167{
1168 struct wayland_output *output;
1169
1170 output = wayland_output_create(b, x, y, oc->width, oc->height, oc->name,
1171 fullscreen, oc->transform, oc->scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001172
1173 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001174}
1175
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001176static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001177wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001178 struct wayland_parent_output *poutput)
1179{
1180 struct wayland_output *output;
1181 struct weston_mode *mode;
1182 int32_t x;
1183
1184 if (poutput->current_mode) {
1185 mode = poutput->current_mode;
1186 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001187 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001188 } else if (!wl_list_empty(&poutput->mode_list)) {
1189 mode = container_of(poutput->mode_list.next,
1190 struct weston_mode, link);
1191 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001192 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001193 return NULL;
1194 }
1195
Giulio Camuffo954f1832014-10-11 18:27:30 +03001196 if (!wl_list_empty(&b->compositor->output_list)) {
1197 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001198 struct wayland_output, base.link);
1199 x = output->base.x + output->base.current_mode->width;
1200 } else {
1201 x = 0;
1202 }
1203
Giulio Camuffo954f1832014-10-11 18:27:30 +03001204 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001205 NULL, 0,
1206 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1207 if (!output)
1208 return NULL;
1209
1210 output->parent.output = poutput->global;
1211
1212 output->base.make = poutput->physical.make;
1213 output->base.model = poutput->physical.model;
1214 wl_list_init(&output->base.mode_list);
1215 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1216 wl_list_init(&poutput->mode_list);
1217
1218 wayland_output_set_fullscreen(output,
1219 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1220 mode->refresh, poutput->global);
1221
1222 if (output->parent.shell_surface) {
1223 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1224 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1225 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001226 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001227 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1228 output->parent.surface,
1229 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1230 poutput->global);
1231 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1232 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1233 output->parent.surface,
1234 poutput->global,
1235 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001236 }
1237
1238 return output;
1239}
1240
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001241static void
1242shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1243 uint32_t serial)
1244{
1245 wl_shell_surface_pong(shell_surface, serial);
1246}
1247
1248static void
1249shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1250 uint32_t edges, int32_t width, int32_t height)
1251{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001252 struct wayland_output *output = data;
1253
1254 output->parent.configure_width = width;
1255 output->parent.configure_height = height;
1256
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001257 /* FIXME: implement resizing */
1258}
1259
1260static void
1261shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1262{
1263}
1264
1265static const struct wl_shell_surface_listener shell_surface_listener = {
1266 shell_surface_ping,
1267 shell_surface_configure,
1268 shell_surface_popup_done
1269};
1270
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001271/* Events received from the wayland-server this compositor is client of: */
1272
Jason Ekstrand7744f712013-10-27 22:24:55 -05001273/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001274static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001275input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001276{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001277
Jason Ekstrand7744f712013-10-27 22:24:55 -05001278 struct wl_buffer *buffer;
1279 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001280
Giulio Camuffo954f1832014-10-11 18:27:30 +03001281 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001282 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001283
Giulio Camuffo954f1832014-10-11 18:27:30 +03001284 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001285 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001286 if (!buffer)
1287 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001288
1289 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1290 input->parent.cursor.surface,
1291 image->hotspot_x, image->hotspot_y);
1292
1293 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1294 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1295 image->width, image->height);
1296 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001297}
1298
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001299static void
Daniel Stone37816df2012-05-16 18:45:18 +01001300input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1301 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001302 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001303{
1304 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001305 int32_t fx, fy;
1306 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001307 double x, y;
1308
1309 x = wl_fixed_to_double(fixed_x);
1310 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001311
Daniel Stone50692802012-06-22 13:21:41 +01001312 /* XXX: If we get a modifier event immediately before the focus,
1313 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001314 input->enter_serial = serial;
1315 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001316
1317 if (input->output->frame) {
1318 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001319 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001320 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001321 x -= fx;
1322 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001323
1324 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1325 weston_output_schedule_repaint(&input->output->base);
1326 } else {
1327 location = THEME_LOCATION_CLIENT_AREA;
1328 }
1329
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001330 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001331
1332 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001333 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001334 notify_pointer_focus(&input->base, &input->output->base, x, y);
1335 wl_pointer_set_cursor(input->parent.pointer,
1336 input->enter_serial, NULL, 0, 0);
1337 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001338 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001339 notify_pointer_focus(&input->base, NULL, 0, 0);
1340 input_set_cursor(input);
1341 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001342}
1343
1344static void
Daniel Stone37816df2012-05-16 18:45:18 +01001345input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1346 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001347{
1348 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001349
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001350 if (!input->output)
1351 return;
1352
Jason Ekstrand7744f712013-10-27 22:24:55 -05001353 if (input->output->frame) {
1354 frame_pointer_leave(input->output->frame, input);
1355
1356 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1357 weston_output_schedule_repaint(&input->output->base);
1358 }
1359
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001360 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001361 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001362 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001363}
1364
1365static void
Daniel Stone37816df2012-05-16 18:45:18 +01001366input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001367 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001368{
1369 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001370 int32_t fx, fy;
1371 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001372 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001373 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001374
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001375 if (!input->output)
1376 return;
1377
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001378 x = wl_fixed_to_double(fixed_x);
1379 y = wl_fixed_to_double(fixed_y);
1380
Jason Ekstrand7744f712013-10-27 22:24:55 -05001381 if (input->output->frame) {
1382 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001383 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001384 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001385 x -= fx;
1386 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001387
1388 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1389 weston_output_schedule_repaint(&input->output->base);
1390 } else {
1391 location = THEME_LOCATION_CLIENT_AREA;
1392 }
1393
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001394 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001395
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001396 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001397 input_set_cursor(input);
1398 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001399 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001400 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001401 } else if (!input->has_focus &&
1402 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001403 wl_pointer_set_cursor(input->parent.pointer,
1404 input->enter_serial, NULL, 0, 0);
1405 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001406 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001407 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001408 }
1409
Peter Hutterer87743e92016-01-18 16:38:22 +10001410 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001411 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001412 want_frame = true;
1413 }
1414
1415 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1416 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001417}
1418
1419static void
1420input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001421 uint32_t serial, uint32_t time, uint32_t button,
1422 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001423{
1424 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001425 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001426 enum frame_button_state fstate;
1427 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001428
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001429 if (!input->output)
1430 return;
1431
Jason Ekstrand7744f712013-10-27 22:24:55 -05001432 if (input->output->frame) {
1433 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1434 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1435
1436 location = frame_pointer_button(input->output->frame, input,
1437 button, fstate);
1438
1439 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1440
1441 wl_shell_surface_move(input->output->parent.shell_surface,
1442 input->parent.seat, serial);
1443 frame_status_clear(input->output->frame,
1444 FRAME_STATUS_MOVE);
1445 return;
1446 }
1447
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001448 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1449 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001450 input->output = NULL;
1451 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001452
Giulio Camuffo954f1832014-10-11 18:27:30 +03001453 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001454 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001455
1456 return;
1457 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001458
1459 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1460 weston_output_schedule_repaint(&input->output->base);
1461 } else {
1462 location = THEME_LOCATION_CLIENT_AREA;
1463 }
1464
Peter Hutterer87743e92016-01-18 16:38:22 +10001465 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001466 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001467 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1468 notify_pointer_frame(&input->base);
1469 }
Daniel Stone37816df2012-05-16 18:45:18 +01001470}
1471
1472static void
1473input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001474 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001475{
1476 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001477 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001478
Peter Hutterer89b6a492016-01-18 15:58:17 +10001479 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001480 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001481
Peter Hutterer87743e92016-01-18 16:38:22 +10001482 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1483 input->vert.has_discrete) {
1484 weston_event.has_discrete = true;
1485 weston_event.discrete = input->vert.discrete;
1486 input->vert.has_discrete = false;
1487 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1488 input->horiz.has_discrete) {
1489 weston_event.has_discrete = true;
1490 weston_event.discrete = input->horiz.discrete;
1491 input->horiz.has_discrete = false;
1492 }
1493
Peter Hutterer89b6a492016-01-18 15:58:17 +10001494 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001495
1496 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1497 notify_pointer_frame(&input->base);
1498}
1499
1500static void
1501input_handle_frame(void *data, struct wl_pointer *pointer)
1502{
1503 struct wayland_input *input = data;
1504
1505 notify_pointer_frame(&input->base);
1506}
1507
1508static void
1509input_handle_axis_source(void *data, struct wl_pointer *pointer,
1510 uint32_t source)
1511{
1512 struct wayland_input *input = data;
1513
1514 notify_axis_source(&input->base, source);
1515}
1516
1517static void
1518input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1519 uint32_t time, uint32_t axis)
1520{
1521 struct wayland_input *input = data;
1522 struct weston_pointer_axis_event weston_event;
1523
1524 weston_event.axis = axis;
1525 weston_event.value = 0;
1526
1527 notify_axis(&input->base, time, &weston_event);
1528}
1529
1530static void
1531input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1532 uint32_t axis, int32_t discrete)
1533{
1534 struct wayland_input *input = data;
1535
1536 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1537 input->vert.has_discrete = true;
1538 input->vert.discrete = discrete;
1539 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1540 input->horiz.has_discrete = true;
1541 input->horiz.discrete = discrete;
1542 }
Daniel Stone37816df2012-05-16 18:45:18 +01001543}
1544
1545static const struct wl_pointer_listener pointer_listener = {
1546 input_handle_pointer_enter,
1547 input_handle_pointer_leave,
1548 input_handle_motion,
1549 input_handle_button,
1550 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001551 input_handle_frame,
1552 input_handle_axis_source,
1553 input_handle_axis_stop,
1554 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001555};
1556
1557static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001558input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1559 int fd, uint32_t size)
1560{
1561 struct wayland_input *input = data;
1562 struct xkb_keymap *keymap;
1563 char *map_str;
1564
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001565 if (!data) {
1566 close(fd);
1567 return;
1568 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001569
1570 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1571 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1572 if (map_str == MAP_FAILED) {
1573 weston_log("mmap failed: %m\n");
1574 goto error;
1575 }
1576
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001578 map_str,
1579 XKB_KEYMAP_FORMAT_TEXT_V1,
1580 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001581 munmap(map_str, size);
1582
1583 if (!keymap) {
1584 weston_log("failed to compile keymap\n");
1585 goto error;
1586 }
1587
1588 input->keyboard_state_update = STATE_UPDATE_NONE;
1589 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1590 weston_log("No keymap provided; falling back to defalt\n");
1591 keymap = NULL;
1592 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1593 } else {
1594 weston_log("Invalid keymap\n");
1595 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001596 }
1597
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001598 close(fd);
1599
Derek Foreman1281a362015-07-31 16:55:32 -05001600 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001601 weston_seat_update_keymap(&input->base, keymap);
1602 else
1603 weston_seat_init_keyboard(&input->base, keymap);
1604
Ran Benitac9c74152014-08-19 23:59:52 +03001605 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001606
1607 return;
1608
1609error:
1610 wl_keyboard_release(input->parent.keyboard);
1611 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001612}
1613
1614static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001615input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001616 struct wl_keyboard *keyboard,
1617 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001618 struct wl_surface *surface,
1619 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001620{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001621 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001622 struct wayland_output *focus;
1623
1624 focus = input->keyboard_focus;
1625 if (focus) {
1626 /* This shouldn't happen */
1627 focus->keyboard_count--;
1628 if (!focus->keyboard_count && focus->frame)
1629 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1630 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1631 weston_output_schedule_repaint(&focus->base);
1632 }
1633
1634 input->keyboard_focus = wl_surface_get_user_data(surface);
1635 input->keyboard_focus->keyboard_count++;
1636
1637 focus = input->keyboard_focus;
1638 if (focus->frame) {
1639 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1640 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1641 weston_output_schedule_repaint(&focus->base);
1642 }
1643
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001644
Daniel Stone50692802012-06-22 13:21:41 +01001645 /* XXX: If we get a modifier event immediately before the focus,
1646 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001647 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001648 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001649}
1650
1651static void
1652input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001653 struct wl_keyboard *keyboard,
1654 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001655 struct wl_surface *surface)
1656{
1657 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001658 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001659
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001660 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001661
1662 focus = input->keyboard_focus;
1663 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001664 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001665
1666 focus->keyboard_count--;
1667 if (!focus->keyboard_count && focus->frame) {
1668 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1669 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1670 weston_output_schedule_repaint(&focus->base);
1671 }
1672
1673 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001674}
1675
Daniel Stone37816df2012-05-16 18:45:18 +01001676static void
1677input_handle_key(void *data, struct wl_keyboard *keyboard,
1678 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1679{
1680 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001681
Daniel Stone50692802012-06-22 13:21:41 +01001682 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001683 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001684 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001685 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001686 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001687}
1688
Daniel Stone351eb612012-05-31 15:27:47 -04001689static void
Derek Foreman1281a362015-07-31 16:55:32 -05001690input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001691 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001692 uint32_t mods_latched, uint32_t mods_locked,
1693 uint32_t group)
1694{
Derek Foreman1281a362015-07-31 16:55:32 -05001695 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001696 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001697 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001698 uint32_t serial_out;
1699
1700 /* If we get a key event followed by a modifier event with the
1701 * same serial number, then we try to preserve those semantics by
1702 * reusing the same serial number on the way out too. */
1703 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001704 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001705 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001706 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001707
Derek Foreman1281a362015-07-31 16:55:32 -05001708 keyboard = weston_seat_get_keyboard(&input->base);
1709 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001710 mods_depressed, mods_latched,
1711 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001712 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001713}
1714
Jonny Lamb497994a2014-08-12 14:58:26 +02001715static void
1716input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1717 int32_t rate, int32_t delay)
1718{
1719 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001720 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001721
Giulio Camuffo954f1832014-10-11 18:27:30 +03001722 b->compositor->kb_repeat_rate = rate;
1723 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001724}
1725
Daniel Stone37816df2012-05-16 18:45:18 +01001726static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001727 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001728 input_handle_keyboard_enter,
1729 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001730 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001731 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001732 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001733};
1734
1735static void
Derek Foreman748c6952015-11-06 15:56:10 -06001736input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1737 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001738 struct wl_surface *surface, int32_t id,
1739 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001740{
1741 struct wayland_input *input = data;
1742 struct wayland_output *output;
1743 enum theme_location location;
1744 bool first_touch;
1745 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001746 double x, y;
1747
1748 x = wl_fixed_to_double(fixed_x);
1749 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001750
1751 first_touch = (input->touch_points == 0);
1752 input->touch_points++;
1753
1754 input->touch_focus = wl_surface_get_user_data(surface);
1755 output = input->touch_focus;
1756 if (!first_touch && !input->touch_active)
1757 return;
1758
1759 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001760 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001761
1762 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001763 x -= fx;
1764 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001765
1766 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1767 weston_output_schedule_repaint(&output->base);
1768
1769 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1770 input->touch_points--;
1771 wl_shell_surface_move(output->parent.shell_surface,
1772 input->parent.seat, serial);
1773 frame_status_clear(output->frame,
1774 FRAME_STATUS_MOVE);
1775 return;
1776 }
1777
1778 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1779 return;
1780 }
1781
1782 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1783
1784 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1785 input->touch_active = true;
1786}
1787
1788static void
1789input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1790 uint32_t serial, uint32_t time, int32_t id)
1791{
1792 struct wayland_input *input = data;
1793 struct wayland_output *output = input->touch_focus;
1794 bool active = input->touch_active;
1795
1796 input->touch_points--;
1797 if (input->touch_points == 0) {
1798 input->touch_focus = NULL;
1799 input->touch_active = false;
1800 }
1801
1802 if (!output)
1803 return;
1804
1805 if (output->frame) {
1806 frame_touch_up(output->frame, input, id);
1807
1808 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1809 wayland_output_destroy(&output->base);
1810 input->touch_focus = NULL;
1811 input->keyboard_focus = NULL;
1812 if (wl_list_empty(&input->backend->compositor->output_list))
1813 weston_compositor_exit(input->backend->compositor);
1814
1815 return;
1816 }
1817 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1818 weston_output_schedule_repaint(&output->base);
1819 }
1820
1821 if (active)
1822 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1823}
1824
1825static void
1826input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001827 uint32_t time, int32_t id,
1828 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001829{
1830 struct wayland_input *input = data;
1831 struct wayland_output *output = input->touch_focus;
1832 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001833 double x, y;
1834
1835 x = wl_fixed_to_double(fixed_x);
1836 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001837
1838 if (!output || !input->touch_active)
1839 return;
1840
1841 if (output->frame) {
1842 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001843 x -= fx;
1844 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001845 }
1846
1847 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1848
1849 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1850}
1851
1852static void
1853input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1854{
1855 struct wayland_input *input = data;
1856
1857 if (!input->touch_focus || !input->touch_active)
1858 return;
1859
1860 notify_touch_frame(&input->base);
1861}
1862
1863static void
1864input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1865{
1866 struct wayland_input *input = data;
1867
1868 if (!input->touch_focus || !input->touch_active)
1869 return;
1870
1871 notify_touch_cancel(&input->base);
1872}
1873
1874static const struct wl_touch_listener touch_listener = {
1875 input_handle_touch_down,
1876 input_handle_touch_up,
1877 input_handle_touch_motion,
1878 input_handle_touch_frame,
1879 input_handle_touch_cancel,
1880};
1881
1882
1883static void
Daniel Stone37816df2012-05-16 18:45:18 +01001884input_handle_capabilities(void *data, struct wl_seat *seat,
1885 enum wl_seat_capability caps)
1886{
1887 struct wayland_input *input = data;
1888
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001889 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1890 input->parent.pointer = wl_seat_get_pointer(seat);
1891 wl_pointer_set_user_data(input->parent.pointer, input);
1892 wl_pointer_add_listener(input->parent.pointer,
1893 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001894 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001895 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001896 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1897 wl_pointer_release(input->parent.pointer);
1898 else
1899 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001900 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001901 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001902 }
1903
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001904 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1905 input->parent.keyboard = wl_seat_get_keyboard(seat);
1906 wl_keyboard_set_user_data(input->parent.keyboard, input);
1907 wl_keyboard_add_listener(input->parent.keyboard,
1908 &keyboard_listener, input);
1909 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001910 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1911 wl_keyboard_release(input->parent.keyboard);
1912 else
1913 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001914 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001915 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001916 }
Derek Foreman748c6952015-11-06 15:56:10 -06001917
1918 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1919 input->parent.touch = wl_seat_get_touch(seat);
1920 wl_touch_set_user_data(input->parent.touch, input);
1921 wl_touch_add_listener(input->parent.touch,
1922 &touch_listener, input);
1923 weston_seat_init_touch(&input->base);
1924 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1925 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1926 wl_touch_release(input->parent.touch);
1927 else
1928 wl_touch_destroy(input->parent.touch);
1929 input->parent.touch = NULL;
1930 weston_seat_release_touch(&input->base);
1931 }
Daniel Stone37816df2012-05-16 18:45:18 +01001932}
1933
Jonny Lamb497994a2014-08-12 14:58:26 +02001934static void
1935input_handle_name(void *data, struct wl_seat *seat,
1936 const char *name)
1937{
1938}
1939
Daniel Stone37816df2012-05-16 18:45:18 +01001940static const struct wl_seat_listener seat_listener = {
1941 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001942 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001943};
1944
1945static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001946display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001947{
1948 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001949 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001950
Peter Huttererf3d62272013-08-08 11:57:05 +10001951 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001952 if (input == NULL)
1953 return;
1954
Giulio Camuffo954f1832014-10-11 18:27:30 +03001955 weston_seat_init(&input->base, b->compositor, "default");
1956 input->backend = b;
1957 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001958 &wl_seat_interface, version);
1959 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001960 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001961
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001962 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1963 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001964
1965 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001966 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001967
1968 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1969 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001970}
1971
1972static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001973wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1974 int32_t x, int32_t y,
1975 int32_t physical_width, int32_t physical_height,
1976 int32_t subpixel, const char *make,
1977 const char *model, int32_t transform)
1978{
1979 struct wayland_parent_output *output = data;
1980
1981 output->x = x;
1982 output->y = y;
1983 output->physical.width = physical_width;
1984 output->physical.height = physical_height;
1985 output->physical.subpixel = subpixel;
1986
1987 free(output->physical.make);
1988 output->physical.make = strdup(make);
1989 free(output->physical.model);
1990 output->physical.model = strdup(model);
1991
1992 output->transform = transform;
1993}
1994
1995static struct weston_mode *
1996find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1997{
1998 struct weston_mode *mode;
1999
2000 wl_list_for_each(mode, list, link) {
2001 if (mode->width == width && mode->height == height &&
2002 mode->refresh == refresh)
2003 return mode;
2004 }
2005
2006 mode = zalloc(sizeof *mode);
2007 if (!mode)
2008 return NULL;
2009
2010 mode->width = width;
2011 mode->height = height;
2012 mode->refresh = refresh;
2013 wl_list_insert(list, &mode->link);
2014
2015 return mode;
2016}
2017
2018static void
2019wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2020 uint32_t flags, int32_t width, int32_t height,
2021 int32_t refresh)
2022{
2023 struct wayland_parent_output *output = data;
2024 struct weston_mode *mode;
2025
2026 if (output->output) {
2027 mode = find_mode(&output->output->base.mode_list,
2028 width, height, refresh);
2029 if (!mode)
2030 return;
2031 mode->flags = flags;
2032 /* Do a mode-switch on current mode change? */
2033 } else {
2034 mode = find_mode(&output->mode_list, width, height, refresh);
2035 if (!mode)
2036 return;
2037 mode->flags = flags;
2038 if (flags & WL_OUTPUT_MODE_CURRENT)
2039 output->current_mode = mode;
2040 if (flags & WL_OUTPUT_MODE_PREFERRED)
2041 output->preferred_mode = mode;
2042 }
2043}
2044
2045static const struct wl_output_listener output_listener = {
2046 wayland_parent_output_geometry,
2047 wayland_parent_output_mode
2048};
2049
2050static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002051wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002052{
2053 struct wayland_parent_output *output;
2054
2055 output = zalloc(sizeof *output);
2056 if (!output)
2057 return;
2058
2059 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002060 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002061 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002062 if (!output->global) {
2063 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002064 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002065 }
2066
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002067 wl_output_add_listener(output->global, &output_listener, output);
2068
2069 output->scale = 0;
2070 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2071 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2072 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002073 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002074
Giulio Camuffo954f1832014-10-11 18:27:30 +03002075 if (b->sprawl_across_outputs) {
2076 wl_display_roundtrip(b->parent.wl_display);
2077 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002078 }
2079}
2080
2081static void
2082wayland_parent_output_destroy(struct wayland_parent_output *output)
2083{
2084 struct weston_mode *mode, *next;
2085
2086 if (output->output)
2087 wayland_output_destroy(&output->output->base);
2088
2089 wl_output_destroy(output->global);
2090 free(output->physical.make);
2091 free(output->physical.model);
2092
2093 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2094 wl_list_remove(&mode->link);
2095 free(mode);
2096 }
2097}
2098
2099static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002100registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2101 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002102{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002103 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002104
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002105 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002106 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002107 wl_registry_bind(registry, name,
2108 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002109 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002110 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002111 wl_registry_bind(registry, name,
2112 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002113 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002114 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002115 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002116 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002117 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002118 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002119 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002120 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002121 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002122 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002123 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002124 }
2125}
2126
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002127static void
2128registry_handle_global_remove(void *data, struct wl_registry *registry,
2129 uint32_t name)
2130{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002131 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002132 struct wayland_parent_output *output;
2133
Giulio Camuffo954f1832014-10-11 18:27:30 +03002134 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002135 if (output->id == name)
2136 wayland_parent_output_destroy(output);
2137}
2138
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002139static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002140 registry_handle_global,
2141 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002142};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002143
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002144static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002145wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002146{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002147 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002148 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002149
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002150 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002151 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002152 return 0;
2153 }
2154
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002155 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002156 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002157 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002158 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002159
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002160 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002161 count = wl_display_dispatch_pending(b->parent.wl_display);
2162 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002163 }
2164
2165 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002166}
2167
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002168static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002169wayland_restore(struct weston_compositor *ec)
2170{
2171}
2172
2173static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002174wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002175{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002176 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002177
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002178 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002179
Giulio Camuffo954f1832014-10-11 18:27:30 +03002180 if (b->parent.shm)
2181 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002182
Giulio Camuffo954f1832014-10-11 18:27:30 +03002183 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002184}
2185
Jason Ekstrand7744f712013-10-27 22:24:55 -05002186static const char *left_ptrs[] = {
2187 "left_ptr",
2188 "default",
2189 "top_left_arrow",
2190 "left-arrow"
2191};
2192
2193static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002194create_cursor(struct wayland_backend *b,
2195 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002196{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002197 unsigned int i;
2198
Benoit Gschwind244ff792016-04-28 20:33:11 +02002199 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2200 config->cursor_size,
2201 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002202 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002203 fprintf(stderr, "could not load cursor theme\n");
2204 return;
2205 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002206
Giulio Camuffo954f1832014-10-11 18:27:30 +03002207 b->cursor = NULL;
2208 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2209 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002210 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002211 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002212 fprintf(stderr, "could not load left cursor\n");
2213 return;
2214 }
2215}
2216
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002217static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002218fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2219 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002220{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002221 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002222 struct wayland_input *input = NULL;
2223
Giulio Camuffo954f1832014-10-11 18:27:30 +03002224 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002225 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002226 break;
2227
2228 if (!input || !input->output)
2229 return;
2230
2231 if (input->output->frame)
2232 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2233 else
2234 wayland_output_set_windowed(input->output);
2235
2236 weston_output_schedule_repaint(&input->output->base);
2237}
2238
Giulio Camuffo954f1832014-10-11 18:27:30 +03002239static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002240wayland_backend_create(struct weston_compositor *compositor,
2241 struct weston_wayland_backend_config *new_config,
2242 int *argc, char *argv[], struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002243{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002244 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002245 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002246 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002247
Giulio Camuffo954f1832014-10-11 18:27:30 +03002248 b = zalloc(sizeof *b);
2249 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002250 return NULL;
2251
Giulio Camuffo954f1832014-10-11 18:27:30 +03002252 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002253 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002254 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002255
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002256 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002257 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002258 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002259 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002260 }
2261
Giulio Camuffo954f1832014-10-11 18:27:30 +03002262 wl_list_init(&b->parent.output_list);
2263 wl_list_init(&b->input_list);
2264 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2265 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2266 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002267
Benoit Gschwind244ff792016-04-28 20:33:11 +02002268 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002269
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002270 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002271
Giulio Camuffo954f1832014-10-11 18:27:30 +03002272 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002273 gl_renderer = weston_load_module("gl-renderer.so",
2274 "gl_renderer_interface");
2275 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002276 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002277 }
2278
Giulio Camuffo954f1832014-10-11 18:27:30 +03002279 if (!b->use_pixman) {
2280 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002281 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002282 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002283 gl_renderer->alpha_attribs,
2284 NULL,
2285 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002286 weston_log("Failed to initialize the GL renderer; "
2287 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002288 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002289 }
2290 }
2291
Giulio Camuffo954f1832014-10-11 18:27:30 +03002292 if (b->use_pixman) {
2293 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002294 weston_log("Failed to initialize pixman renderer\n");
2295 goto err_display;
2296 }
2297 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002298
Giulio Camuffo954f1832014-10-11 18:27:30 +03002299 b->base.destroy = wayland_destroy;
2300 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002301
Giulio Camuffo954f1832014-10-11 18:27:30 +03002302 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002303
Giulio Camuffo954f1832014-10-11 18:27:30 +03002304 fd = wl_display_get_fd(b->parent.wl_display);
2305 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002306 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002307 wayland_backend_handle_event, b);
2308 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002309 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002310
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002312
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002313 if (compositor->renderer->import_dmabuf) {
2314 if (linux_dmabuf_setup(compositor) < 0)
2315 weston_log("Error: initializing dmabuf "
2316 "support failed.\n");
2317 }
2318
Giulio Camuffo954f1832014-10-11 18:27:30 +03002319 compositor->backend = &b->base;
2320 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002321err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002322 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002323err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002324 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002325 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002326 return NULL;
2327}
2328
2329static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002330wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002331{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002332 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002333
Giulio Camuffo954f1832014-10-11 18:27:30 +03002334 if (b->theme)
2335 theme_destroy(b->theme);
2336 if (b->frame_device)
2337 cairo_device_destroy(b->frame_device);
2338 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002339
Giulio Camuffo954f1832014-10-11 18:27:30 +03002340 weston_compositor_shutdown(b->compositor);
2341 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002342}
2343
Giulio Camuffo954f1832014-10-11 18:27:30 +03002344WL_EXPORT int
2345backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002346 struct weston_config *config,
2347 struct weston_backend_config *config_base)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002348{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002349 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002350 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002351 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002352 struct weston_config_section *section;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002353 struct weston_wayland_backend_config new_config;
Benoit Gschwind830b7882016-04-28 20:33:12 +02002354 struct weston_wayland_backend_output_config output_config;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002355 int x, count, width, height, scale;
2356 const char *section_name;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002357 char *name;
2358
2359 const struct weston_option wayland_options[] = {
2360 { WESTON_OPTION_INTEGER, "width", 0, &width },
2361 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002362 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002363 { WESTON_OPTION_STRING, "display", 0, &new_config.display_name },
2364 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &new_config.use_pixman },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002365 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002366 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &new_config.fullscreen },
2367 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &new_config.sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002368 };
2369
2370 width = 0;
2371 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002372 scale = 0;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002373 new_config.display_name = NULL;
2374 new_config.use_pixman = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002375 count = 1;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002376 new_config.fullscreen = 0;
2377 new_config.sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002378 parse_options(wayland_options,
2379 ARRAY_LENGTH(wayland_options), argc, argv);
2380
Benoit Gschwind244ff792016-04-28 20:33:11 +02002381 new_config.cursor_size = 32;
2382 new_config.cursor_theme = NULL;
2383
2384 section = weston_config_get_section(config, "shell", NULL, NULL);
2385 weston_config_section_get_string(section, "cursor-theme",
2386 &new_config.cursor_theme, NULL);
2387 weston_config_section_get_int(section, "cursor-size",
2388 &new_config.cursor_size, 32);
2389
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002390 b = wayland_backend_create(compositor, &new_config, argc, argv, config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002391
Giulio Camuffo954f1832014-10-11 18:27:30 +03002392 if (!b)
2393 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002394
Benoit Gschwind37a68072016-04-28 20:33:08 +02002395 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002396 b->sprawl_across_outputs = 1;
2397 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002398
Giulio Camuffo954f1832014-10-11 18:27:30 +03002399 wl_list_for_each(poutput, &b->parent.output_list, link)
2400 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002401
Giulio Camuffo954f1832014-10-11 18:27:30 +03002402 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002403 }
2404
Benoit Gschwind37a68072016-04-28 20:33:08 +02002405 if (new_config.fullscreen) {
Benoit Gschwind830b7882016-04-28 20:33:12 +02002406 output_config.width = width;
2407 output_config.height = height;
2408 output_config.name = NULL;
2409 output_config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
2410 output_config.scale = 1;
2411
2412 output = wayland_output_create_for_config(b, &output_config,
2413 1, 0, 0);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002414 if (!output)
2415 goto err_outputs;
2416
Axel Davydd8b88d2013-11-17 21:34:16 +01002417 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002418 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002419 }
2420
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002421 section = NULL;
2422 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002423 while (weston_config_next_section(config, &section, &section_name)) {
2424 if (!section_name || strcmp(section_name, "output") != 0)
2425 continue;
2426 weston_config_section_get_string(section, "name", &name, NULL);
2427 if (name == NULL)
2428 continue;
2429
2430 if (name[0] != 'W' || name[1] != 'L') {
2431 free(name);
2432 continue;
2433 }
2434 free(name);
2435
Benoit Gschwind830b7882016-04-28 20:33:12 +02002436 wayland_output_init_from_config(&output_config, section, width,
2437 height, scale);
2438 output = wayland_output_create_for_config(b, &output_config, 0,
2439 x, 0);
2440 free(output_config.name);
2441
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002442 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002443 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002444 if (wayland_output_set_windowed(output))
2445 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002446
2447 x += output->base.width;
2448 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002449 }
2450
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002451 if (!width)
2452 width = 1024;
2453 if (!height)
2454 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002455 if (!scale)
2456 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002457 while (count > 0) {
Benoit Gschwind830b7882016-04-28 20:33:12 +02002458 output_config.width = width;
2459 output_config.height = height;
2460 output_config.name = NULL;
2461 output_config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
2462 output_config.scale = scale;
2463
2464 output = wayland_output_create_for_config(b, &output_config,
2465 0, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002466 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002467 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002468 if (wayland_output_set_windowed(output))
2469 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002470
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002471 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002472 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002473 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002474
Giulio Camuffo954f1832014-10-11 18:27:30 +03002475 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002476 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002477 fullscreen_binding, b);
Benoit Gschwind244ff792016-04-28 20:33:11 +02002478 free(new_config.cursor_theme);
Benoit Gschwind079b6262016-04-28 20:33:09 +02002479 free(new_config.display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002480 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002481
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002482err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002483 wayland_backend_destroy(b);
Benoit Gschwind244ff792016-04-28 20:33:11 +02002484 free(new_config.cursor_theme);
Benoit Gschwind079b6262016-04-28 20:33:09 +02002485 free(new_config.display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002486 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002487}