blob: aa7e4266fa65f20cd4c9405314d175735db10657 [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
Armin Krezović174448a2016-09-30 14:11:09 +020029#include <assert.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030#include <stddef.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030031#include <stdint.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010032#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <fcntl.h>
36#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010037#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060038#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039
Benjamin Franzkebe014562011-02-18 17:04:24 +010040#include <wayland-client.h>
41#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050042#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010043
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010044#include "compositor.h"
Benoit Gschwind639322a2016-04-28 20:33:15 +020045#include "compositor-wayland.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010046#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020047#include "weston-egl-ext.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050048#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070050#include "shared/image-loader.h"
51#include "shared/os-compatibility.h"
52#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080053#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020054#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020055#include "linux-dmabuf.h"
Armin Krezović174448a2016-09-30 14:11:09 +020056#include "windowed-output-api.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057
Jason Ekstrand48ce4212013-10-27 22:25:02 -050058#define WINDOW_TITLE "Weston Compositor"
59
Giulio Camuffo954f1832014-10-11 18:27:30 +030060struct wayland_backend {
61 struct weston_backend base;
62 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063
64 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040065 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040066 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067 struct wl_compositor *compositor;
68 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080069 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020070 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010071
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050072 struct wl_list output_list;
73
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010074 struct wl_event_source *wl_source;
75 uint32_t event_mask;
76 } parent;
77
Armin Krezović7e71b872016-10-09 17:30:22 +020078 bool use_pixman;
Armin Krezović7f1c0b82016-10-09 17:30:23 +020079 bool sprawl_across_outputs;
Armin Krezović20450162016-10-13 12:01:43 +020080 bool fullscreen;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050081
Jason Ekstrand7744f712013-10-27 22:24:55 -050082 struct theme *theme;
83 cairo_device_t *frame_device;
84 struct wl_cursor_theme *cursor_theme;
85 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050086
Jason Ekstrand06ced802013-11-07 20:13:29 -060087 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010088};
89
90struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050091 struct weston_output base;
92
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010093 struct {
Armin Krezović2d321e32016-10-09 17:30:25 +020094 bool draw_initial_frame;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050095 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050096
97 struct wl_output *output;
98 uint32_t global_id;
99
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500100 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600101 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100102 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500103
Jason Ekstrand7744f712013-10-27 22:24:55 -0500104 int keyboard_count;
105
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600106 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500107 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500108
Jason Ekstrand7744f712013-10-27 22:24:55 -0500109 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500110 struct wl_egl_window *egl_window;
111 struct {
112 cairo_surface_t *top;
113 cairo_surface_t *left;
114 cairo_surface_t *right;
115 cairo_surface_t *bottom;
116 } border;
117 } gl;
118
119 struct {
120 struct wl_list buffers;
121 struct wl_list free_buffers;
122 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500123
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500124 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500125 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100126};
127
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500128struct wayland_parent_output {
129 struct wayland_output *output;
130 struct wl_list link;
131
132 struct wl_output *global;
133 uint32_t id;
134
135 struct {
136 char *make;
137 char *model;
138 int32_t width, height;
139 uint32_t subpixel;
140 } physical;
141
142 int32_t x, y;
143 uint32_t transform;
144 uint32_t scale;
145
146 struct wl_list mode_list;
147 struct weston_mode *preferred_mode;
148 struct weston_mode *current_mode;
149};
150
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500151struct wayland_shm_buffer {
152 struct wayland_output *output;
153 struct wl_list link;
154 struct wl_list free_link;
155
156 struct wl_buffer *buffer;
157 void *data;
158 size_t size;
159 pixman_region32_t damage;
160 int frame_damaged;
161
162 pixman_image_t *pm_image;
163 cairo_surface_t *c_surface;
164};
165
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100166struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400167 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300168 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100169 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500170
171 struct {
172 struct wl_seat *seat;
173 struct wl_pointer *pointer;
174 struct wl_keyboard *keyboard;
175 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500176
177 struct {
178 struct wl_surface *surface;
179 int32_t hx, hy;
180 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500181 } parent;
182
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500183 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100184 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400185 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600186 uint32_t touch_points;
187 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600188 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600189 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600190
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400191 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600192 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500193 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000194
195 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100196};
197
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300198struct gl_renderer_interface *gl_renderer;
199
Armin Krezović938dc522016-08-01 19:17:57 +0200200static inline struct wayland_output *
201to_wayland_output(struct weston_output *base)
202{
203 return container_of(base, struct wayland_output, base);
204}
205
206static inline struct wayland_backend *
207to_wayland_backend(struct weston_compositor *base)
208{
209 return container_of(base->backend, struct wayland_backend, base);
210}
211
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500212static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500213wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
214{
215 cairo_surface_destroy(buffer->c_surface);
216 pixman_image_unref(buffer->pm_image);
217
218 wl_buffer_destroy(buffer->buffer);
219 munmap(buffer->data, buffer->size);
220
221 pixman_region32_fini(&buffer->damage);
222
223 wl_list_remove(&buffer->link);
224 wl_list_remove(&buffer->free_link);
225 free(buffer);
226}
227
228static void
229buffer_release(void *data, struct wl_buffer *buffer)
230{
231 struct wayland_shm_buffer *sb = data;
232
233 if (sb->output) {
234 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
235 } else {
236 wayland_shm_buffer_destroy(sb);
237 }
238}
239
240static const struct wl_buffer_listener buffer_listener = {
241 buffer_release
242};
243
244static struct wayland_shm_buffer *
245wayland_output_get_shm_buffer(struct wayland_output *output)
246{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300247 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200248 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300249 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500250 struct wayland_shm_buffer *sb;
251
252 struct wl_shm_pool *pool;
253 int width, height, stride;
254 int32_t fx, fy;
255 int fd;
256 unsigned char *data;
257
258 if (!wl_list_empty(&output->shm.free_buffers)) {
259 sb = container_of(output->shm.free_buffers.next,
260 struct wayland_shm_buffer, free_link);
261 wl_list_remove(&sb->free_link);
262 wl_list_init(&sb->free_link);
263
264 return sb;
265 }
266
267 if (output->frame) {
268 width = frame_width(output->frame);
269 height = frame_height(output->frame);
270 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500271 width = output->base.current_mode->width;
272 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500273 }
274
275 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
276
277 fd = os_create_anonymous_file(height * stride);
278 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000279 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500280 return NULL;
281 }
282
283 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
284 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000285 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500286 close(fd);
287 return NULL;
288 }
289
290 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000291 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200292 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000293 close(fd);
294 free(data);
295 return NULL;
296 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500297
298 sb->output = output;
299 wl_list_init(&sb->free_link);
300 wl_list_insert(&output->shm.buffers, &sb->link);
301
302 pixman_region32_init_rect(&sb->damage, 0, 0,
303 output->base.width, output->base.height);
304 sb->frame_damaged = 1;
305
306 sb->data = data;
307 sb->size = height * stride;
308
309 pool = wl_shm_create_pool(shm, fd, sb->size);
310
311 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
312 width, height,
313 stride,
314 WL_SHM_FORMAT_ARGB8888);
315 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
316 wl_shm_pool_destroy(pool);
317 close(fd);
318
319 memset(data, 0, sb->size);
320
321 sb->c_surface =
322 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
323 width, height, stride);
324
325 fx = 0;
326 fy = 0;
327 if (output->frame)
328 frame_interior(output->frame, &fx, &fy, 0, 0);
329 sb->pm_image =
330 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
331 (uint32_t *)(data + fy * stride) + fx,
332 stride);
333
334 return sb;
335}
336
337static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500338frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400339{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500340 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400341 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400342
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500343 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400344
345 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300346
347 /*
348 * This is the fallback case, where Presentation extension is not
349 * available from the parent compositor. We do not know the base for
350 * 'time', so we cannot feed it to finish_frame(). Do the only thing
351 * we can, and pretend finish_frame time is when we process this
352 * event.
353 */
354 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200355 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400356}
357
358static const struct wl_callback_listener frame_listener = {
359 frame_done
360};
361
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500362static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200363draw_initial_frame(struct wayland_output *output)
364{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500365 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200366
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500367 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200368
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500369 /* If we are rendering with GL, then orphan it so that it gets
370 * destroyed immediately */
371 if (output->gl.egl_window)
372 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500373
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500374 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500375 wl_surface_damage(output->parent.surface, 0, 0,
376 output->base.current_mode->width,
377 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200378}
379
380static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500381wayland_output_update_gl_border(struct wayland_output *output)
382{
383 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
384 cairo_t *cr;
385
386 if (!output->frame)
387 return;
388 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
389 return;
390
391 fwidth = frame_width(output->frame);
392 fheight = frame_height(output->frame);
393 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
394
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 if (!output->gl.border.top)
396 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500397 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
398 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500399 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500400 frame_repaint(output->frame, cr);
401 cairo_destroy(cr);
402 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
403 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500404 cairo_image_surface_get_stride(output->gl.border.top) / 4,
405 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500406
407
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 if (!output->gl.border.left)
409 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
411 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500412 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500413 cairo_translate(cr, 0, -iy);
414 frame_repaint(output->frame, cr);
415 cairo_destroy(cr);
416 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
417 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500418 cairo_image_surface_get_stride(output->gl.border.left) / 4,
419 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500420
421
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500422 if (!output->gl.border.right)
423 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500424 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
425 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500426 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500427 cairo_translate(cr, -(iwidth + ix), -iy);
428 frame_repaint(output->frame, cr);
429 cairo_destroy(cr);
430 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
431 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500432 cairo_image_surface_get_stride(output->gl.border.right) / 4,
433 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500434
435
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500436 if (!output->gl.border.bottom)
437 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500438 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
439 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500440 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500441 cairo_translate(cr, 0, -(iy + iheight));
442 frame_repaint(output->frame, cr);
443 cairo_destroy(cr);
444 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
445 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500446 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
447 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500448}
449
450static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200451wayland_output_start_repaint_loop(struct weston_output *output_base)
452{
Armin Krezović938dc522016-08-01 19:17:57 +0200453 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300454 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200455 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200456 struct wl_callback *callback;
457
458 /* If this is the initial frame, we need to attach a buffer so that
459 * the compositor can map the surface and include it in its render
460 * loop. If the surface doesn't end up in the render loop, the frame
461 * callback won't be invoked. The buffer is transparent and of the
462 * same size as the future real output buffer. */
463 if (output->parent.draw_initial_frame) {
Armin Krezović2d321e32016-10-09 17:30:25 +0200464 output->parent.draw_initial_frame = false;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200465
466 draw_initial_frame(output);
467 }
468
469 callback = wl_surface_frame(output->parent.surface);
470 wl_callback_add_listener(callback, &frame_listener, output);
471 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300472 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200473}
474
David Herrmann1edf44c2013-10-22 17:11:26 +0200475static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500476wayland_output_repaint_gl(struct weston_output *output_base,
477 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400478{
Armin Krezović938dc522016-08-01 19:17:57 +0200479 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400480 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400481 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600482
Kristian Høgsberg33418202011-08-16 23:01:28 -0400483 callback = wl_surface_frame(output->parent.surface);
484 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100485
Jason Ekstrand7744f712013-10-27 22:24:55 -0500486 wayland_output_update_gl_border(output);
487
Pekka Paalanenbc106382012-10-10 12:49:31 +0300488 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200489
490 pixman_region32_subtract(&ec->primary_plane.damage,
491 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200492 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100493}
494
Matt Roper361d2ad2011-08-29 13:52:23 -0700495static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500496wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
497{
498 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
499 cairo_t *cr;
500
501 if (!buffer->output->frame || !buffer->frame_damaged)
502 return;
503
504 cr = cairo_create(buffer->c_surface);
505
506 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
507 fwidth = frame_width(buffer->output->frame);
508 fheight = frame_height(buffer->output->frame);
509
510 /* Set the clip so we don't unnecisaraly damage the surface */
511 cairo_move_to(cr, ix, iy);
512 cairo_rel_line_to(cr, iwidth, 0);
513 cairo_rel_line_to(cr, 0, iheight);
514 cairo_rel_line_to(cr, -iwidth, 0);
515 cairo_line_to(cr, ix, iy);
516 cairo_line_to(cr, 0, iy);
517 cairo_line_to(cr, 0, fheight);
518 cairo_line_to(cr, fwidth, fheight);
519 cairo_line_to(cr, fwidth, 0);
520 cairo_line_to(cr, 0, 0);
521 cairo_line_to(cr, 0, iy);
522 cairo_close_path(cr);
523 cairo_clip(cr);
524
525 /* Draw using a pattern so that the final result gets clipped */
526 cairo_push_group(cr);
527 frame_repaint(buffer->output->frame, cr);
528 cairo_pop_group_to_source(cr);
529 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
530 cairo_paint(cr);
531
532 cairo_destroy(cr);
533}
534
535static void
536wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
537{
538 pixman_region32_t damage;
539 pixman_box32_t *rects;
540 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
541 int i, n;
542
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500543 pixman_region32_init(&damage);
544 weston_transformed_region(sb->output->base.width,
545 sb->output->base.height,
546 sb->output->base.transform,
547 sb->output->base.current_scale,
548 &sb->damage, &damage);
549
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500550 if (sb->output->frame) {
551 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
552 fwidth = frame_width(sb->output->frame);
553 fheight = frame_height(sb->output->frame);
554
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500555 pixman_region32_translate(&damage, ix, iy);
556
557 if (sb->frame_damaged) {
558 pixman_region32_union_rect(&damage, &damage,
559 0, 0, fwidth, iy);
560 pixman_region32_union_rect(&damage, &damage,
561 0, iy, ix, iheight);
562 pixman_region32_union_rect(&damage, &damage,
563 ix + iwidth, iy,
564 fwidth - (ix + iwidth), iheight);
565 pixman_region32_union_rect(&damage, &damage,
566 0, iy + iheight,
567 fwidth, fheight - (iy + iheight));
568 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500569 }
570
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500571 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500572 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
573 for (i = 0; i < n; ++i)
574 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
575 rects[i].y1, rects[i].x2 - rects[i].x1,
576 rects[i].y2 - rects[i].y1);
577
578 if (sb->output->frame)
579 pixman_region32_fini(&damage);
580}
581
582static int
583wayland_output_repaint_pixman(struct weston_output *output_base,
584 pixman_region32_t *damage)
585{
Armin Krezović938dc522016-08-01 19:17:57 +0200586 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300587 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200588 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500589 struct wl_callback *callback;
590 struct wayland_shm_buffer *sb;
591
592 if (output->frame) {
593 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
594 wl_list_for_each(sb, &output->shm.buffers, link)
595 sb->frame_damaged = 1;
596 }
597
598 wl_list_for_each(sb, &output->shm.buffers, link)
599 pixman_region32_union(&sb->damage, &sb->damage, damage);
600
601 sb = wayland_output_get_shm_buffer(output);
602
603 wayland_output_update_shm_border(sb);
604 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300605 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500606
607 wayland_shm_buffer_attach(sb);
608
609 callback = wl_surface_frame(output->parent.surface);
610 wl_callback_add_listener(callback, &frame_listener, output);
611 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300612 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500613
614 pixman_region32_fini(&sb->damage);
615 pixman_region32_init(&sb->damage);
616 sb->frame_damaged = 0;
617
Giulio Camuffo954f1832014-10-11 18:27:30 +0300618 pixman_region32_subtract(&b->compositor->primary_plane.damage,
619 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500620 return 0;
621}
622
623static void
Armin Krezović174448a2016-09-30 14:11:09 +0200624wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700625{
Armin Krezović174448a2016-09-30 14:11:09 +0200626 if (output->parent.shell_surface)
627 wl_shell_surface_destroy(output->parent.shell_surface);
628
629 wl_surface_destroy(output->parent.surface);
630}
631
632static int
633wayland_output_disable(struct weston_output *base)
634{
635 struct wayland_output *output = to_wayland_output(base);
636 struct wayland_backend *b = to_wayland_backend(base->compositor);
637
638 if (!output->base.enabled)
639 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700640
Giulio Camuffo954f1832014-10-11 18:27:30 +0300641 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200642 pixman_renderer_output_destroy(&output->base);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500643 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200644 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100645 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500646 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100647
Armin Krezović174448a2016-09-30 14:11:09 +0200648 /* Done on output->enable when not fullscreen, otherwise
649 * done in output_create, to get the proper mode */
650 if (!b->fullscreen)
651 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500652
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600653 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500654 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600655
656 cairo_surface_destroy(output->gl.border.top);
657 cairo_surface_destroy(output->gl.border.left);
658 cairo_surface_destroy(output->gl.border.right);
659 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500660
Armin Krezović174448a2016-09-30 14:11:09 +0200661 return 0;
662}
Matt Roper361d2ad2011-08-29 13:52:23 -0700663
Armin Krezović174448a2016-09-30 14:11:09 +0200664static void
665wayland_output_destroy(struct weston_output *base)
666{
667 struct wayland_output *output = to_wayland_output(base);
668 struct wayland_backend *b = to_wayland_backend(base->compositor);
669
670 wayland_output_disable(&output->base);
671
672 if (b->fullscreen)
673 wayland_backend_destroy_output_surface(output);
674
675 weston_output_destroy(&output->base);
676
677 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700678}
679
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300680static const struct wl_shell_surface_listener shell_surface_listener;
681
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200682static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500683wayland_output_init_gl_renderer(struct wayland_output *output)
684{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500685 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500686
687 if (output->frame) {
688 fwidth = frame_width(output->frame);
689 fheight = frame_height(output->frame);
690 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500691 fwidth = output->base.current_mode->width;
692 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500693 }
694
695 output->gl.egl_window =
696 wl_egl_window_create(output->parent.surface,
697 fwidth, fheight);
698 if (!output->gl.egl_window) {
699 weston_log("failure to create wl_egl_window\n");
700 return -1;
701 }
702
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200703 if (gl_renderer->output_window_create(&output->base,
704 output->gl.egl_window,
705 output->gl.egl_window,
706 gl_renderer->alpha_attribs,
707 NULL,
708 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500709 goto cleanup_window;
710
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500711 return 0;
712
713cleanup_window:
714 wl_egl_window_destroy(output->gl.egl_window);
715 return -1;
716}
717
718static int
719wayland_output_init_pixman_renderer(struct wayland_output *output)
720{
721 return pixman_renderer_output_create(&output->base);
722}
723
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600724static void
725wayland_output_resize_surface(struct wayland_output *output)
726{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300727 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200728 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600729 struct wayland_shm_buffer *buffer, *next;
730 int32_t ix, iy, iwidth, iheight;
731 int32_t width, height;
732 struct wl_region *region;
733
734 width = output->base.current_mode->width;
735 height = output->base.current_mode->height;
736
737 if (output->frame) {
738 frame_resize_inside(output->frame, width, height);
739
740 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300741 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600742 wl_region_add(region, ix, iy, iwidth, iheight);
743 wl_surface_set_input_region(output->parent.surface, region);
744 wl_region_destroy(region);
745
746 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300747 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600748 wl_region_add(region, ix, iy, iwidth, iheight);
749 wl_surface_set_opaque_region(output->parent.surface, region);
750 wl_region_destroy(region);
751
752 width = frame_width(output->frame);
753 height = frame_height(output->frame);
754 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300755 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600756 wl_region_add(region, 0, 0, width, height);
757 wl_surface_set_input_region(output->parent.surface, region);
758 wl_region_destroy(region);
759
Giulio Camuffo954f1832014-10-11 18:27:30 +0300760 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600761 wl_region_add(region, 0, 0, width, height);
762 wl_surface_set_opaque_region(output->parent.surface, region);
763 wl_region_destroy(region);
764 }
765
766 if (output->gl.egl_window) {
767 wl_egl_window_resize(output->gl.egl_window,
768 width, height, 0, 0);
769
770 /* These will need to be re-created due to the resize */
771 gl_renderer->output_set_border(&output->base,
772 GL_RENDERER_BORDER_TOP,
773 0, 0, 0, NULL);
774 cairo_surface_destroy(output->gl.border.top);
775 output->gl.border.top = NULL;
776 gl_renderer->output_set_border(&output->base,
777 GL_RENDERER_BORDER_LEFT,
778 0, 0, 0, NULL);
779 cairo_surface_destroy(output->gl.border.left);
780 output->gl.border.left = NULL;
781 gl_renderer->output_set_border(&output->base,
782 GL_RENDERER_BORDER_RIGHT,
783 0, 0, 0, NULL);
784 cairo_surface_destroy(output->gl.border.right);
785 output->gl.border.right = NULL;
786 gl_renderer->output_set_border(&output->base,
787 GL_RENDERER_BORDER_BOTTOM,
788 0, 0, 0, NULL);
789 cairo_surface_destroy(output->gl.border.bottom);
790 output->gl.border.bottom = NULL;
791 }
792
793 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500794 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600795 wayland_shm_buffer_destroy(buffer);
796 /* These will get thrown away when they get released */
797 wl_list_for_each(buffer, &output->shm.buffers, link)
798 buffer->output = NULL;
799}
800
801static int
802wayland_output_set_windowed(struct wayland_output *output)
803{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300804 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200805 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600806 int tlen;
807 char *title;
808
809 if (output->frame)
810 return 0;
811
812 if (output->name) {
813 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
814 title = malloc(tlen + 1);
815 if (!title)
816 return -1;
817
818 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
819 } else {
820 title = strdup(WINDOW_TITLE);
821 }
822
Giulio Camuffo954f1832014-10-11 18:27:30 +0300823 if (!b->theme) {
824 b->theme = theme_create();
825 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600826 free(title);
827 return -1;
828 }
829 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300830 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600831 FRAME_BUTTON_CLOSE, title);
832 free(title);
833 if (!output->frame)
834 return -1;
835
836 if (output->keyboard_count)
837 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
838
839 wayland_output_resize_surface(output);
840
841 wl_shell_surface_set_toplevel(output->parent.shell_surface);
842
843 return 0;
844}
845
846static void
847wayland_output_set_fullscreen(struct wayland_output *output,
848 enum wl_shell_surface_fullscreen_method method,
849 uint32_t framerate, struct wl_output *target)
850{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300851 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200852 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500853
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600854 if (output->frame) {
855 frame_destroy(output->frame);
856 output->frame = NULL;
857 }
858
859 wayland_output_resize_surface(output);
860
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500861 if (output->parent.shell_surface) {
862 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
863 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300864 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800865 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
866 output->parent.surface,
867 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500868 }
869}
870
871static struct weston_mode *
872wayland_output_choose_mode(struct wayland_output *output,
873 struct weston_mode *ref_mode)
874{
875 struct weston_mode *mode;
876
877 /* First look for an exact match */
878 wl_list_for_each(mode, &output->base.mode_list, link)
879 if (mode->width == ref_mode->width &&
880 mode->height == ref_mode->height &&
881 mode->refresh == ref_mode->refresh)
882 return mode;
883
884 /* If we can't find an exact match, ignore refresh and try again */
885 wl_list_for_each(mode, &output->base.mode_list, link)
886 if (mode->width == ref_mode->width &&
887 mode->height == ref_mode->height)
888 return mode;
889
890 /* Yeah, we failed */
891 return NULL;
892}
893
894enum mode_status {
895 MODE_STATUS_UNKNOWN,
896 MODE_STATUS_SUCCESS,
897 MODE_STATUS_FAIL,
898 MODE_STATUS_CANCEL,
899};
900
901static void
902mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800903 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500904{
905 enum mode_status *value = data;
906
907 printf("Mode switch successful\n");
908
909 *value = MODE_STATUS_SUCCESS;
910}
911
912static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800913mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500914{
915 enum mode_status *value = data;
916
917 printf("Mode switch failed\n");
918
919 *value = MODE_STATUS_FAIL;
920}
921
922static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800923mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500924{
925 enum mode_status *value = data;
926
927 printf("Mode switch cancelled\n");
928
929 *value = MODE_STATUS_CANCEL;
930}
931
Jonas Ådahl496adb32015-11-17 16:00:27 +0800932struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500933 mode_feedback_successful,
934 mode_feedback_failed,
935 mode_feedback_cancelled,
936};
937
938static int
939wayland_output_switch_mode(struct weston_output *output_base,
940 struct weston_mode *mode)
941{
Armin Krezović938dc522016-08-01 19:17:57 +0200942 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300943 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500944 struct wl_surface *old_surface;
945 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800946 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500947 enum mode_status mode_status;
948 int ret = 0;
949
950 if (output_base == NULL) {
951 weston_log("output is NULL.\n");
952 return -1;
953 }
954
955 if (mode == NULL) {
956 weston_log("mode is NULL.\n");
957 return -1;
958 }
959
Armin Krezović938dc522016-08-01 19:17:57 +0200960 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500961
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500963 return -1;
964
965 mode = wayland_output_choose_mode(output, mode);
966 if (mode == NULL)
967 return -1;
968
969 if (output->base.current_mode == mode)
970 return 0;
971
972 old_mode = output->base.current_mode;
973 old_surface = output->parent.surface;
974 output->base.current_mode = mode;
975 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300976 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500977 wl_surface_set_user_data(output->parent.surface, output);
978
979 /* Blow the old buffers because we changed size/surfaces */
980 wayland_output_resize_surface(output);
981
982 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800983 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
984 output->parent.surface,
985 output->parent.output,
986 mode->refresh);
987 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
988 &mode_feedback_listener,
989 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500990
991 /* This should kick-start things again */
Armin Krezović2d321e32016-10-09 17:30:25 +0200992 output->parent.draw_initial_frame = true;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500993 wayland_output_start_repaint_loop(&output->base);
994
995 mode_status = MODE_STATUS_UNKNOWN;
996 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300997 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500998
Jonas Ådahl496adb32015-11-17 16:00:27 +0800999 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001000
1001 if (mode_status == MODE_STATUS_FAIL) {
1002 output->base.current_mode = old_mode;
1003 wl_surface_destroy(output->parent.surface);
1004 output->parent.surface = old_surface;
1005 wayland_output_resize_surface(output);
1006
1007 return -1;
1008 }
1009
1010 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1011 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1012
Giulio Camuffo954f1832014-10-11 18:27:30 +03001013 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001014 pixman_renderer_output_destroy(output_base);
1015 if (wayland_output_init_pixman_renderer(output) < 0)
1016 goto err_output;
1017 } else {
1018 gl_renderer->output_destroy(output_base);
1019 wl_egl_window_destroy(output->gl.egl_window);
1020 if (wayland_output_init_gl_renderer(output) < 0)
1021 goto err_output;
1022 }
1023 wl_surface_destroy(old_surface);
1024
1025 weston_output_schedule_repaint(&output->base);
1026
1027 return 0;
1028
1029err_output:
1030 /* XXX */
1031 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001032}
1033
Armin Krezović174448a2016-09-30 14:11:09 +02001034static int
1035wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001036{
Armin Krezović174448a2016-09-30 14:11:09 +02001037 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001038
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001039 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001040 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001041 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001042 return -1;
1043
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001044 wl_surface_set_user_data(output->parent.surface, output);
1045
Armin Krezović2d321e32016-10-09 17:30:25 +02001046 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001047
Giulio Camuffo954f1832014-10-11 18:27:30 +03001048 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001049 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001050 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001051 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001052 if (!output->parent.shell_surface) {
1053 wl_surface_destroy(output->parent.surface);
1054 return -1;
1055 }
1056
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001057 wl_shell_surface_add_listener(output->parent.shell_surface,
1058 &shell_surface_listener, output);
1059 }
1060
Armin Krezović174448a2016-09-30 14:11:09 +02001061 return 0;
1062}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001063
Armin Krezović174448a2016-09-30 14:11:09 +02001064static int
1065wayland_output_enable(struct weston_output *base)
1066{
1067 struct wayland_output *output = to_wayland_output(base);
1068 struct wayland_backend *b = to_wayland_backend(base->compositor);
1069 int ret = 0;
1070
1071
1072 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1073 output->base.current_mode->width,
1074 output->base.current_mode->height,
1075 output->base.x, output->base.y);
1076
1077 /* If fullscreen was specified, this needs to be done before
1078 * enable to get the proper mode */
1079 if (!b->fullscreen)
1080 ret = wayland_backend_create_output_surface(output);
1081
1082 if (ret < 0)
1083 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001084
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001085 wl_list_init(&output->shm.buffers);
1086 wl_list_init(&output->shm.free_buffers);
1087
Giulio Camuffo954f1832014-10-11 18:27:30 +03001088 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001089 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001090 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001091
1092 output->base.repaint = wayland_output_repaint_pixman;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001093 } else {
1094 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001095 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001096
1097 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001098 }
1099
Armin Krezovićf16de172016-10-09 17:30:26 +02001100 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1101 output->base.assign_planes = NULL;
1102 output->base.set_backlight = NULL;
1103 output->base.set_dpms = NULL;
1104 output->base.switch_mode = wayland_output_switch_mode;
1105
Armin Krezović174448a2016-09-30 14:11:09 +02001106 if (b->sprawl_across_outputs) {
1107 wayland_output_set_fullscreen(output,
1108 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1109 output->mode.refresh, output->parent.output);
1110
1111 if (output->parent.shell_surface) {
1112 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1113 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1114 output->mode.refresh, output->parent.output);
1115 } else if (b->parent.fshell) {
1116 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1117 output->parent.surface,
1118 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1119 output->parent.output);
1120 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1121 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1122 output->parent.surface,
1123 output->parent.output,
1124 output->mode.refresh));
1125 }
1126 } else if (b->fullscreen) {
1127 wayland_output_set_fullscreen(output, 0, 0, NULL);
1128 } else {
1129 wayland_output_set_windowed(output);
1130 }
1131
1132 return 0;
1133
1134err_output:
1135 if (!b->fullscreen)
1136 wayland_backend_destroy_output_surface(output);
1137
1138 return -1;
1139}
1140
1141static struct wayland_output *
1142wayland_output_create_common(void)
1143{
1144 struct wayland_output *output;
1145
1146 output = zalloc(sizeof *output);
1147 if (output == NULL) {
1148 perror("zalloc");
1149 return NULL;
1150 }
1151
Matt Roper361d2ad2011-08-29 13:52:23 -07001152 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001153 output->base.disable = wayland_output_disable;
1154 output->base.enable = wayland_output_enable;
1155
1156 return output;
1157}
1158
1159static int
1160wayland_output_create(struct weston_compositor *compositor, const char *name)
1161{
1162 struct wayland_output *output = wayland_output_create_common();
1163
1164 /* name can't be NULL. */
1165 assert(name);
1166
1167 output->base.name = strdup(name);
1168
Armin Krezović40087402016-09-30 14:11:12 +02001169 weston_output_init(&output->base, compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001170 weston_compositor_add_pending_output(&output->base, compositor);
1171
1172 return 0;
1173}
1174
1175static int
1176wayland_output_set_size(struct weston_output *base, int width, int height)
1177{
1178 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001179 int output_width, output_height;
1180
1181 /* We can only be called once. */
1182 assert(!output->base.current_mode);
1183
1184 /* Make sure we have scale set. */
1185 assert(output->base.scale);
1186
1187 if (width < 1) {
1188 weston_log("Invalid width \"%d\" for output %s\n",
1189 width, output->base.name);
1190 return -1;
1191 }
1192
1193 if (height < 1) {
1194 weston_log("Invalid height \"%d\" for output %s\n",
1195 height, output->base.name);
1196 return -1;
1197 }
1198
1199 output_width = width * output->base.scale;
1200 output_height = height * output->base.scale;
1201
1202 output->mode.flags =
1203 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1204
1205 output->mode.width = output_width;
1206 output->mode.height = output_height;
1207 output->mode.refresh = 60000;
1208 output->scale = output->base.scale;
1209 wl_list_init(&output->base.mode_list);
1210 wl_list_insert(&output->base.mode_list, &output->mode.link);
1211
1212 output->base.current_mode = &output->mode;
1213 output->base.make = "wayland";
1214 output->base.model = "none";
1215
1216 /* XXX: Calculate proper size. */
1217 output->base.mm_width = width;
1218 output->base.mm_height = height;
1219
Armin Krezović174448a2016-09-30 14:11:09 +02001220 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001221}
1222
Armin Krezović174448a2016-09-30 14:11:09 +02001223static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001224wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001225 struct wayland_parent_output *poutput)
1226{
Armin Krezović174448a2016-09-30 14:11:09 +02001227 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001228 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001229
1230 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001231
1232 if (poutput->current_mode) {
1233 mode = poutput->current_mode;
1234 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001235 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001236 } else if (!wl_list_empty(&poutput->mode_list)) {
1237 mode = container_of(poutput->mode_list.next,
1238 struct weston_mode, link);
1239 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001240 weston_log("No valid modes found. Skipping output.\n");
1241 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001242 }
1243
Armin Krezović40087402016-09-30 14:11:12 +02001244 weston_output_init(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001245
Armin Krezović174448a2016-09-30 14:11:09 +02001246 output->base.scale = 1;
1247 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001248
Armin Krezović174448a2016-09-30 14:11:09 +02001249 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1250 goto out;
1251
1252 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001253 output->parent.output = poutput->global;
1254
1255 output->base.make = poutput->physical.make;
1256 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001257
1258 /* XXX: Clear previously added values */
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001259 wl_list_init(&output->base.mode_list);
1260 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1261 wl_list_init(&poutput->mode_list);
1262
Armin Krezović174448a2016-09-30 14:11:09 +02001263 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001264
Armin Krezović174448a2016-09-30 14:11:09 +02001265 return 0;
1266
1267out:
1268 free(output->name);
1269 free(output);
1270
1271 return -1;
1272}
1273
1274static int
1275wayland_output_create_fullscreen(struct wayland_backend *b)
1276{
1277 struct wayland_output *output = wayland_output_create_common();
1278 int width = 0, height = 0;
1279
1280 output->base.name = strdup("wayland-fullscreen");
1281
Armin Krezović40087402016-09-30 14:11:12 +02001282 weston_output_init(&output->base, b->compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001283
1284 output->base.scale = 1;
1285 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1286
1287 if (wayland_backend_create_output_surface(output) < 0)
1288 goto err_surface;
1289
1290 /* What should size be set if conditional is false? */
1291 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001292 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
Armin Krezović174448a2016-09-30 14:11:09 +02001293 0, 0, NULL);
1294 wl_display_roundtrip(b->parent.wl_display);
1295
1296 width = output->parent.configure_width;
1297 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001298 }
1299
Armin Krezović174448a2016-09-30 14:11:09 +02001300 if (wayland_output_set_size(&output->base, width, height) < 0)
1301 goto err_set_size;
1302
1303 weston_compositor_add_pending_output(&output->base, b->compositor);
1304
1305 return 0;
1306
1307err_set_size:
1308 wayland_backend_destroy_output_surface(output);
1309err_surface:
1310 free(output->name);
1311 free(output);
1312
1313 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001314}
1315
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001316static void
1317shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1318 uint32_t serial)
1319{
1320 wl_shell_surface_pong(shell_surface, serial);
1321}
1322
1323static void
1324shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1325 uint32_t edges, int32_t width, int32_t height)
1326{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001327 struct wayland_output *output = data;
1328
1329 output->parent.configure_width = width;
1330 output->parent.configure_height = height;
1331
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001332 /* FIXME: implement resizing */
1333}
1334
1335static void
1336shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1337{
1338}
1339
1340static const struct wl_shell_surface_listener shell_surface_listener = {
1341 shell_surface_ping,
1342 shell_surface_configure,
1343 shell_surface_popup_done
1344};
1345
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001346/* Events received from the wayland-server this compositor is client of: */
1347
Jason Ekstrand7744f712013-10-27 22:24:55 -05001348/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001349static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001350input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001351{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001352
Jason Ekstrand7744f712013-10-27 22:24:55 -05001353 struct wl_buffer *buffer;
1354 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001355
Giulio Camuffo954f1832014-10-11 18:27:30 +03001356 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001357 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001358
Giulio Camuffo954f1832014-10-11 18:27:30 +03001359 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001360 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001361 if (!buffer)
1362 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001363
1364 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1365 input->parent.cursor.surface,
1366 image->hotspot_x, image->hotspot_y);
1367
1368 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1369 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1370 image->width, image->height);
1371 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001372}
1373
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001374static void
Daniel Stone37816df2012-05-16 18:45:18 +01001375input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1376 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001377 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001378{
1379 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001380 int32_t fx, fy;
1381 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001382 double x, y;
1383
1384 x = wl_fixed_to_double(fixed_x);
1385 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001386
Daniel Stone50692802012-06-22 13:21:41 +01001387 /* XXX: If we get a modifier event immediately before the focus,
1388 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001389 input->enter_serial = serial;
1390 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001391
1392 if (input->output->frame) {
1393 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001394 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001395 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001396 x -= fx;
1397 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001398
1399 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1400 weston_output_schedule_repaint(&input->output->base);
1401 } else {
1402 location = THEME_LOCATION_CLIENT_AREA;
1403 }
1404
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001405 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001406
1407 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001408 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001409 notify_pointer_focus(&input->base, &input->output->base, x, y);
1410 wl_pointer_set_cursor(input->parent.pointer,
1411 input->enter_serial, NULL, 0, 0);
1412 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001413 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001414 notify_pointer_focus(&input->base, NULL, 0, 0);
1415 input_set_cursor(input);
1416 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001417}
1418
1419static void
Daniel Stone37816df2012-05-16 18:45:18 +01001420input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1421 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001422{
1423 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001424
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001425 if (!input->output)
1426 return;
1427
Jason Ekstrand7744f712013-10-27 22:24:55 -05001428 if (input->output->frame) {
1429 frame_pointer_leave(input->output->frame, input);
1430
1431 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1432 weston_output_schedule_repaint(&input->output->base);
1433 }
1434
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001435 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001436 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001437 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001438}
1439
1440static void
Daniel Stone37816df2012-05-16 18:45:18 +01001441input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001442 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001443{
1444 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001445 int32_t fx, fy;
1446 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001447 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001448 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001449
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001450 if (!input->output)
1451 return;
1452
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001453 x = wl_fixed_to_double(fixed_x);
1454 y = wl_fixed_to_double(fixed_y);
1455
Jason Ekstrand7744f712013-10-27 22:24:55 -05001456 if (input->output->frame) {
1457 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001458 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001459 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001460 x -= fx;
1461 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001462
1463 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1464 weston_output_schedule_repaint(&input->output->base);
1465 } else {
1466 location = THEME_LOCATION_CLIENT_AREA;
1467 }
1468
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001469 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001470
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001471 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001472 input_set_cursor(input);
1473 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001474 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001475 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001476 } else if (!input->has_focus &&
1477 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001478 wl_pointer_set_cursor(input->parent.pointer,
1479 input->enter_serial, NULL, 0, 0);
1480 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001481 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001482 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001483 }
1484
Peter Hutterer87743e92016-01-18 16:38:22 +10001485 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001486 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001487 want_frame = true;
1488 }
1489
1490 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1491 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001492}
1493
1494static void
1495input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001496 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001497 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001498{
1499 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001500 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001501
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001502 if (!input->output)
1503 return;
1504
Jason Ekstrand7744f712013-10-27 22:24:55 -05001505 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001506 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001507 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001508
1509 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1510
1511 wl_shell_surface_move(input->output->parent.shell_surface,
1512 input->parent.seat, serial);
1513 frame_status_clear(input->output->frame,
1514 FRAME_STATUS_MOVE);
1515 return;
1516 }
1517
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001518 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1519 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001520 input->output = NULL;
1521 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001522
Giulio Camuffo954f1832014-10-11 18:27:30 +03001523 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001524 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001525
1526 return;
1527 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001528
1529 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1530 weston_output_schedule_repaint(&input->output->base);
1531 } else {
1532 location = THEME_LOCATION_CLIENT_AREA;
1533 }
1534
Peter Hutterer87743e92016-01-18 16:38:22 +10001535 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001536 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001537 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1538 notify_pointer_frame(&input->base);
1539 }
Daniel Stone37816df2012-05-16 18:45:18 +01001540}
1541
1542static void
1543input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001544 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001545{
1546 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001547 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001548
Peter Hutterer89b6a492016-01-18 15:58:17 +10001549 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001550 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001551
Peter Hutterer87743e92016-01-18 16:38:22 +10001552 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1553 input->vert.has_discrete) {
1554 weston_event.has_discrete = true;
1555 weston_event.discrete = input->vert.discrete;
1556 input->vert.has_discrete = false;
1557 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1558 input->horiz.has_discrete) {
1559 weston_event.has_discrete = true;
1560 weston_event.discrete = input->horiz.discrete;
1561 input->horiz.has_discrete = false;
1562 }
1563
Peter Hutterer89b6a492016-01-18 15:58:17 +10001564 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001565
1566 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1567 notify_pointer_frame(&input->base);
1568}
1569
1570static void
1571input_handle_frame(void *data, struct wl_pointer *pointer)
1572{
1573 struct wayland_input *input = data;
1574
1575 notify_pointer_frame(&input->base);
1576}
1577
1578static void
1579input_handle_axis_source(void *data, struct wl_pointer *pointer,
1580 uint32_t source)
1581{
1582 struct wayland_input *input = data;
1583
1584 notify_axis_source(&input->base, source);
1585}
1586
1587static void
1588input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1589 uint32_t time, uint32_t axis)
1590{
1591 struct wayland_input *input = data;
1592 struct weston_pointer_axis_event weston_event;
1593
1594 weston_event.axis = axis;
1595 weston_event.value = 0;
1596
1597 notify_axis(&input->base, time, &weston_event);
1598}
1599
1600static void
1601input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1602 uint32_t axis, int32_t discrete)
1603{
1604 struct wayland_input *input = data;
1605
1606 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1607 input->vert.has_discrete = true;
1608 input->vert.discrete = discrete;
1609 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1610 input->horiz.has_discrete = true;
1611 input->horiz.discrete = discrete;
1612 }
Daniel Stone37816df2012-05-16 18:45:18 +01001613}
1614
1615static const struct wl_pointer_listener pointer_listener = {
1616 input_handle_pointer_enter,
1617 input_handle_pointer_leave,
1618 input_handle_motion,
1619 input_handle_button,
1620 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001621 input_handle_frame,
1622 input_handle_axis_source,
1623 input_handle_axis_stop,
1624 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001625};
1626
1627static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001628input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1629 int fd, uint32_t size)
1630{
1631 struct wayland_input *input = data;
1632 struct xkb_keymap *keymap;
1633 char *map_str;
1634
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001635 if (!data) {
1636 close(fd);
1637 return;
1638 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001639
1640 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1641 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1642 if (map_str == MAP_FAILED) {
1643 weston_log("mmap failed: %m\n");
1644 goto error;
1645 }
1646
Giulio Camuffo954f1832014-10-11 18:27:30 +03001647 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001648 map_str,
1649 XKB_KEYMAP_FORMAT_TEXT_V1,
1650 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001651 munmap(map_str, size);
1652
1653 if (!keymap) {
1654 weston_log("failed to compile keymap\n");
1655 goto error;
1656 }
1657
1658 input->keyboard_state_update = STATE_UPDATE_NONE;
1659 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1660 weston_log("No keymap provided; falling back to defalt\n");
1661 keymap = NULL;
1662 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1663 } else {
1664 weston_log("Invalid keymap\n");
1665 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001666 }
1667
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001668 close(fd);
1669
Derek Foreman1281a362015-07-31 16:55:32 -05001670 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001671 weston_seat_update_keymap(&input->base, keymap);
1672 else
1673 weston_seat_init_keyboard(&input->base, keymap);
1674
Ran Benitac9c74152014-08-19 23:59:52 +03001675 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001676
1677 return;
1678
1679error:
1680 wl_keyboard_release(input->parent.keyboard);
1681 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001682}
1683
1684static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001685input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001686 struct wl_keyboard *keyboard,
1687 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001688 struct wl_surface *surface,
1689 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001690{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001691 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001692 struct wayland_output *focus;
1693
1694 focus = input->keyboard_focus;
1695 if (focus) {
1696 /* This shouldn't happen */
1697 focus->keyboard_count--;
1698 if (!focus->keyboard_count && focus->frame)
1699 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1700 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1701 weston_output_schedule_repaint(&focus->base);
1702 }
1703
1704 input->keyboard_focus = wl_surface_get_user_data(surface);
1705 input->keyboard_focus->keyboard_count++;
1706
1707 focus = input->keyboard_focus;
1708 if (focus->frame) {
1709 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1710 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1711 weston_output_schedule_repaint(&focus->base);
1712 }
1713
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001714
Daniel Stone50692802012-06-22 13:21:41 +01001715 /* XXX: If we get a modifier event immediately before the focus,
1716 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001717 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001718 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001719}
1720
1721static void
1722input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001723 struct wl_keyboard *keyboard,
1724 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001725 struct wl_surface *surface)
1726{
1727 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001728 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001729
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001730 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001731
1732 focus = input->keyboard_focus;
1733 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001734 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001735
1736 focus->keyboard_count--;
1737 if (!focus->keyboard_count && focus->frame) {
1738 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1739 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1740 weston_output_schedule_repaint(&focus->base);
1741 }
1742
1743 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001744}
1745
Daniel Stone37816df2012-05-16 18:45:18 +01001746static void
1747input_handle_key(void *data, struct wl_keyboard *keyboard,
1748 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1749{
1750 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001751
Daniel Stone50692802012-06-22 13:21:41 +01001752 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001753 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001754 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001755 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001756 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001757}
1758
Daniel Stone351eb612012-05-31 15:27:47 -04001759static void
Derek Foreman1281a362015-07-31 16:55:32 -05001760input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001761 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001762 uint32_t mods_latched, uint32_t mods_locked,
1763 uint32_t group)
1764{
Derek Foreman1281a362015-07-31 16:55:32 -05001765 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001766 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001767 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001768 uint32_t serial_out;
1769
1770 /* If we get a key event followed by a modifier event with the
1771 * same serial number, then we try to preserve those semantics by
1772 * reusing the same serial number on the way out too. */
1773 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001774 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001775 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001776 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001777
Derek Foreman1281a362015-07-31 16:55:32 -05001778 keyboard = weston_seat_get_keyboard(&input->base);
1779 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001780 mods_depressed, mods_latched,
1781 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001782 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001783}
1784
Jonny Lamb497994a2014-08-12 14:58:26 +02001785static void
1786input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1787 int32_t rate, int32_t delay)
1788{
1789 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001790 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001791
Giulio Camuffo954f1832014-10-11 18:27:30 +03001792 b->compositor->kb_repeat_rate = rate;
1793 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001794}
1795
Daniel Stone37816df2012-05-16 18:45:18 +01001796static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001797 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001798 input_handle_keyboard_enter,
1799 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001800 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001801 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001802 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001803};
1804
1805static void
Derek Foreman748c6952015-11-06 15:56:10 -06001806input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1807 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001808 struct wl_surface *surface, int32_t id,
1809 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001810{
1811 struct wayland_input *input = data;
1812 struct wayland_output *output;
1813 enum theme_location location;
1814 bool first_touch;
1815 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001816 double x, y;
1817
1818 x = wl_fixed_to_double(fixed_x);
1819 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001820
1821 first_touch = (input->touch_points == 0);
1822 input->touch_points++;
1823
1824 input->touch_focus = wl_surface_get_user_data(surface);
1825 output = input->touch_focus;
1826 if (!first_touch && !input->touch_active)
1827 return;
1828
1829 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001830 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001831
1832 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001833 x -= fx;
1834 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001835
1836 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1837 weston_output_schedule_repaint(&output->base);
1838
1839 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1840 input->touch_points--;
1841 wl_shell_surface_move(output->parent.shell_surface,
1842 input->parent.seat, serial);
1843 frame_status_clear(output->frame,
1844 FRAME_STATUS_MOVE);
1845 return;
1846 }
1847
1848 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1849 return;
1850 }
1851
1852 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1853
1854 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1855 input->touch_active = true;
1856}
1857
1858static void
1859input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1860 uint32_t serial, uint32_t time, int32_t id)
1861{
1862 struct wayland_input *input = data;
1863 struct wayland_output *output = input->touch_focus;
1864 bool active = input->touch_active;
1865
1866 input->touch_points--;
1867 if (input->touch_points == 0) {
1868 input->touch_focus = NULL;
1869 input->touch_active = false;
1870 }
1871
1872 if (!output)
1873 return;
1874
1875 if (output->frame) {
1876 frame_touch_up(output->frame, input, id);
1877
1878 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1879 wayland_output_destroy(&output->base);
1880 input->touch_focus = NULL;
1881 input->keyboard_focus = NULL;
1882 if (wl_list_empty(&input->backend->compositor->output_list))
1883 weston_compositor_exit(input->backend->compositor);
1884
1885 return;
1886 }
1887 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1888 weston_output_schedule_repaint(&output->base);
1889 }
1890
1891 if (active)
1892 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1893}
1894
1895static void
1896input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001897 uint32_t time, int32_t id,
1898 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001899{
1900 struct wayland_input *input = data;
1901 struct wayland_output *output = input->touch_focus;
1902 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001903 double x, y;
1904
1905 x = wl_fixed_to_double(fixed_x);
1906 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001907
1908 if (!output || !input->touch_active)
1909 return;
1910
1911 if (output->frame) {
1912 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001913 x -= fx;
1914 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001915 }
1916
1917 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1918
1919 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1920}
1921
1922static void
1923input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1924{
1925 struct wayland_input *input = data;
1926
1927 if (!input->touch_focus || !input->touch_active)
1928 return;
1929
1930 notify_touch_frame(&input->base);
1931}
1932
1933static void
1934input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1935{
1936 struct wayland_input *input = data;
1937
1938 if (!input->touch_focus || !input->touch_active)
1939 return;
1940
1941 notify_touch_cancel(&input->base);
1942}
1943
1944static const struct wl_touch_listener touch_listener = {
1945 input_handle_touch_down,
1946 input_handle_touch_up,
1947 input_handle_touch_motion,
1948 input_handle_touch_frame,
1949 input_handle_touch_cancel,
1950};
1951
1952
1953static void
Daniel Stone37816df2012-05-16 18:45:18 +01001954input_handle_capabilities(void *data, struct wl_seat *seat,
1955 enum wl_seat_capability caps)
1956{
1957 struct wayland_input *input = data;
1958
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001959 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1960 input->parent.pointer = wl_seat_get_pointer(seat);
1961 wl_pointer_set_user_data(input->parent.pointer, input);
1962 wl_pointer_add_listener(input->parent.pointer,
1963 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001964 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001965 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001966 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1967 wl_pointer_release(input->parent.pointer);
1968 else
1969 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001970 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001971 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001972 }
1973
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001974 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1975 input->parent.keyboard = wl_seat_get_keyboard(seat);
1976 wl_keyboard_set_user_data(input->parent.keyboard, input);
1977 wl_keyboard_add_listener(input->parent.keyboard,
1978 &keyboard_listener, input);
1979 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001980 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1981 wl_keyboard_release(input->parent.keyboard);
1982 else
1983 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001984 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001985 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001986 }
Derek Foreman748c6952015-11-06 15:56:10 -06001987
1988 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1989 input->parent.touch = wl_seat_get_touch(seat);
1990 wl_touch_set_user_data(input->parent.touch, input);
1991 wl_touch_add_listener(input->parent.touch,
1992 &touch_listener, input);
1993 weston_seat_init_touch(&input->base);
1994 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1995 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1996 wl_touch_release(input->parent.touch);
1997 else
1998 wl_touch_destroy(input->parent.touch);
1999 input->parent.touch = NULL;
2000 weston_seat_release_touch(&input->base);
2001 }
Daniel Stone37816df2012-05-16 18:45:18 +01002002}
2003
Jonny Lamb497994a2014-08-12 14:58:26 +02002004static void
2005input_handle_name(void *data, struct wl_seat *seat,
2006 const char *name)
2007{
2008}
2009
Daniel Stone37816df2012-05-16 18:45:18 +01002010static const struct wl_seat_listener seat_listener = {
2011 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002012 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002013};
2014
2015static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002016display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002017{
2018 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002019 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002020
Peter Huttererf3d62272013-08-08 11:57:05 +10002021 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002022 if (input == NULL)
2023 return;
2024
Giulio Camuffo954f1832014-10-11 18:27:30 +03002025 weston_seat_init(&input->base, b->compositor, "default");
2026 input->backend = b;
2027 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002028 &wl_seat_interface, version);
2029 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002030 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002031
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002032 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2033 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002034
2035 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002036 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002037
2038 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2039 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002040}
2041
2042static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002043wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2044 int32_t x, int32_t y,
2045 int32_t physical_width, int32_t physical_height,
2046 int32_t subpixel, const char *make,
2047 const char *model, int32_t transform)
2048{
2049 struct wayland_parent_output *output = data;
2050
2051 output->x = x;
2052 output->y = y;
2053 output->physical.width = physical_width;
2054 output->physical.height = physical_height;
2055 output->physical.subpixel = subpixel;
2056
2057 free(output->physical.make);
2058 output->physical.make = strdup(make);
2059 free(output->physical.model);
2060 output->physical.model = strdup(model);
2061
2062 output->transform = transform;
2063}
2064
2065static struct weston_mode *
2066find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2067{
2068 struct weston_mode *mode;
2069
2070 wl_list_for_each(mode, list, link) {
2071 if (mode->width == width && mode->height == height &&
2072 mode->refresh == refresh)
2073 return mode;
2074 }
2075
2076 mode = zalloc(sizeof *mode);
2077 if (!mode)
2078 return NULL;
2079
2080 mode->width = width;
2081 mode->height = height;
2082 mode->refresh = refresh;
2083 wl_list_insert(list, &mode->link);
2084
2085 return mode;
2086}
2087
2088static void
2089wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2090 uint32_t flags, int32_t width, int32_t height,
2091 int32_t refresh)
2092{
2093 struct wayland_parent_output *output = data;
2094 struct weston_mode *mode;
2095
2096 if (output->output) {
2097 mode = find_mode(&output->output->base.mode_list,
2098 width, height, refresh);
2099 if (!mode)
2100 return;
2101 mode->flags = flags;
2102 /* Do a mode-switch on current mode change? */
2103 } else {
2104 mode = find_mode(&output->mode_list, width, height, refresh);
2105 if (!mode)
2106 return;
2107 mode->flags = flags;
2108 if (flags & WL_OUTPUT_MODE_CURRENT)
2109 output->current_mode = mode;
2110 if (flags & WL_OUTPUT_MODE_PREFERRED)
2111 output->preferred_mode = mode;
2112 }
2113}
2114
2115static const struct wl_output_listener output_listener = {
2116 wayland_parent_output_geometry,
2117 wayland_parent_output_mode
2118};
2119
2120static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002121wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002122{
2123 struct wayland_parent_output *output;
2124
2125 output = zalloc(sizeof *output);
2126 if (!output)
2127 return;
2128
2129 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002130 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002131 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002132 if (!output->global) {
2133 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002134 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002135 }
2136
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002137 wl_output_add_listener(output->global, &output_listener, output);
2138
2139 output->scale = 0;
2140 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2141 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2142 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002143 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002144
Giulio Camuffo954f1832014-10-11 18:27:30 +03002145 if (b->sprawl_across_outputs) {
2146 wl_display_roundtrip(b->parent.wl_display);
2147 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002148 }
2149}
2150
2151static void
2152wayland_parent_output_destroy(struct wayland_parent_output *output)
2153{
2154 struct weston_mode *mode, *next;
2155
2156 if (output->output)
2157 wayland_output_destroy(&output->output->base);
2158
2159 wl_output_destroy(output->global);
2160 free(output->physical.make);
2161 free(output->physical.model);
2162
2163 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2164 wl_list_remove(&mode->link);
2165 free(mode);
2166 }
2167}
2168
2169static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002170registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2171 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002172{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002173 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002174
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002175 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002176 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002177 wl_registry_bind(registry, name,
2178 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002179 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002180 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002181 wl_registry_bind(registry, name,
2182 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002183 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002184 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002185 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002186 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002187 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002188 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002189 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002190 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002191 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002192 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002193 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002194 }
2195}
2196
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002197static void
2198registry_handle_global_remove(void *data, struct wl_registry *registry,
2199 uint32_t name)
2200{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002201 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002202 struct wayland_parent_output *output;
2203
Giulio Camuffo954f1832014-10-11 18:27:30 +03002204 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002205 if (output->id == name)
2206 wayland_parent_output_destroy(output);
2207}
2208
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002209static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002210 registry_handle_global,
2211 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002212};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002213
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002214static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002215wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002216{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002217 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002218 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002219
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002220 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002221 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002222 return 0;
2223 }
2224
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002225 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002226 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002227 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002228 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002229
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002230 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002231 count = wl_display_dispatch_pending(b->parent.wl_display);
2232 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002233 }
2234
2235 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002236}
2237
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002238static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002239wayland_restore(struct weston_compositor *ec)
2240{
2241}
2242
2243static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002244wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002245{
Armin Krezović938dc522016-08-01 19:17:57 +02002246 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002247
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002248 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002249
Giulio Camuffo954f1832014-10-11 18:27:30 +03002250 if (b->parent.shm)
2251 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002252
Giulio Camuffo954f1832014-10-11 18:27:30 +03002253 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002254}
2255
Jason Ekstrand7744f712013-10-27 22:24:55 -05002256static const char *left_ptrs[] = {
2257 "left_ptr",
2258 "default",
2259 "top_left_arrow",
2260 "left-arrow"
2261};
2262
2263static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002264create_cursor(struct wayland_backend *b,
2265 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002266{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002267 unsigned int i;
2268
Benoit Gschwind244ff792016-04-28 20:33:11 +02002269 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2270 config->cursor_size,
2271 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002272 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002273 fprintf(stderr, "could not load cursor theme\n");
2274 return;
2275 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002276
Giulio Camuffo954f1832014-10-11 18:27:30 +03002277 b->cursor = NULL;
2278 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2279 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002280 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002281 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002282 fprintf(stderr, "could not load left cursor\n");
2283 return;
2284 }
2285}
2286
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002287static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002288fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2289 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002290{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002291 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002292 struct wayland_input *input = NULL;
2293
Giulio Camuffo954f1832014-10-11 18:27:30 +03002294 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002295 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002296 break;
2297
2298 if (!input || !input->output)
2299 return;
2300
2301 if (input->output->frame)
2302 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2303 else
2304 wayland_output_set_windowed(input->output);
2305
2306 weston_output_schedule_repaint(&input->output->base);
2307}
2308
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002310wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002311 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002312{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002314 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002315 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002316
Giulio Camuffo954f1832014-10-11 18:27:30 +03002317 b = zalloc(sizeof *b);
2318 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002319 return NULL;
2320
Giulio Camuffo954f1832014-10-11 18:27:30 +03002321 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002322 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002323 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002324
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002325 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002326 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002327 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002328 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002329 }
2330
Giulio Camuffo954f1832014-10-11 18:27:30 +03002331 wl_list_init(&b->parent.output_list);
2332 wl_list_init(&b->input_list);
2333 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2334 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2335 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002336
Benoit Gschwind244ff792016-04-28 20:33:11 +02002337 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002338
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002339 b->use_pixman = new_config->use_pixman;
Armin Krezović174448a2016-09-30 14:11:09 +02002340 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002341
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002343 gl_renderer = weston_load_module("gl-renderer.so",
2344 "gl_renderer_interface");
2345 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002346 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002347 }
2348
Giulio Camuffo954f1832014-10-11 18:27:30 +03002349 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002350 if (gl_renderer->display_create(compositor,
2351 EGL_PLATFORM_WAYLAND_KHR,
2352 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002353 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002354 gl_renderer->alpha_attribs,
2355 NULL,
2356 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002357 weston_log("Failed to initialize the GL renderer; "
2358 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002359 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002360 }
2361 }
2362
Giulio Camuffo954f1832014-10-11 18:27:30 +03002363 if (b->use_pixman) {
2364 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002365 weston_log("Failed to initialize pixman renderer\n");
2366 goto err_display;
2367 }
2368 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002369
Giulio Camuffo954f1832014-10-11 18:27:30 +03002370 b->base.destroy = wayland_destroy;
2371 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002372
Giulio Camuffo954f1832014-10-11 18:27:30 +03002373 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002374
Giulio Camuffo954f1832014-10-11 18:27:30 +03002375 fd = wl_display_get_fd(b->parent.wl_display);
2376 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002377 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002378 wayland_backend_handle_event, b);
2379 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002380 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002381
Giulio Camuffo954f1832014-10-11 18:27:30 +03002382 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002383
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002384 if (compositor->renderer->import_dmabuf) {
2385 if (linux_dmabuf_setup(compositor) < 0)
2386 weston_log("Error: initializing dmabuf "
2387 "support failed.\n");
2388 }
2389
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 compositor->backend = &b->base;
2391 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002392err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002393 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002394err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002395 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002396 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002397 return NULL;
2398}
2399
2400static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002401wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002402{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002403 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002404
Giulio Camuffo954f1832014-10-11 18:27:30 +03002405 if (b->theme)
2406 theme_destroy(b->theme);
2407 if (b->frame_device)
2408 cairo_device_destroy(b->frame_device);
2409 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002410
Giulio Camuffo954f1832014-10-11 18:27:30 +03002411 weston_compositor_shutdown(b->compositor);
2412 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002413}
2414
Armin Krezović174448a2016-09-30 14:11:09 +02002415static const struct weston_windowed_output_api windowed_api = {
2416 wayland_output_set_size,
2417 wayland_output_create,
2418};
2419
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002420static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002421config_init_to_defaults(struct weston_wayland_backend_config *config)
2422{
2423}
2424
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002425WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002426backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002427 struct weston_backend_config *config_base)
2428{
2429 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002430 struct wayland_parent_output *poutput;
2431 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002432 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002433
Benoit Gschwinde091b452016-05-10 22:47:48 +02002434 if (config_base == NULL ||
2435 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2436 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2437 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002438 return -1;
2439 }
2440
2441 config_init_to_defaults(&new_config);
2442 memcpy(&new_config, config_base, config_base->struct_size);
2443
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002444 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002445
Giulio Camuffo954f1832014-10-11 18:27:30 +03002446 if (!b)
2447 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002448
Benoit Gschwind37a68072016-04-28 20:33:08 +02002449 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002450 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002451 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002452
Giulio Camuffo954f1832014-10-11 18:27:30 +03002453 wl_list_for_each(poutput, &b->parent.output_list, link)
2454 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002455
Giulio Camuffo954f1832014-10-11 18:27:30 +03002456 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002457 }
2458
Benoit Gschwind37a68072016-04-28 20:33:08 +02002459 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002460 if (wayland_output_create_fullscreen(b) < 0) {
2461 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002462 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002463 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002464
Giulio Camuffo954f1832014-10-11 18:27:30 +03002465 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002466 }
2467
Armin Krezović174448a2016-09-30 14:11:09 +02002468 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2469 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002470
Armin Krezović174448a2016-09-30 14:11:09 +02002471 if (ret < 0) {
2472 weston_log("Failed to register output API.\n");
2473 wayland_backend_destroy(b);
2474 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002475 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002476
Giulio Camuffo954f1832014-10-11 18:27:30 +03002477 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002478 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002479 fullscreen_binding, b);
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);
2484 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002485}