blob: 775da252002bca483742ca784a9ef38e51342758 [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
Armin Krezović2e662522016-10-09 17:30:29 +0200632static void
633wayland_output_destroy_shm_buffers(struct wayland_output *output)
634{
635 struct wayland_shm_buffer *buffer, *next;
636
637 /* Throw away any remaining SHM buffers */
638 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
639 wayland_shm_buffer_destroy(buffer);
640 /* These will get thrown away when they get released */
641 wl_list_for_each(buffer, &output->shm.buffers, link)
642 buffer->output = NULL;
643}
644
Armin Krezović174448a2016-09-30 14:11:09 +0200645static int
646wayland_output_disable(struct weston_output *base)
647{
648 struct wayland_output *output = to_wayland_output(base);
649 struct wayland_backend *b = to_wayland_backend(base->compositor);
650
651 if (!output->base.enabled)
652 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700653
Giulio Camuffo954f1832014-10-11 18:27:30 +0300654 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200655 pixman_renderer_output_destroy(&output->base);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500656 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200657 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100658 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500659 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100660
Armin Krezović2e662522016-10-09 17:30:29 +0200661 wayland_output_destroy_shm_buffers(output);
662
Armin Krezovićf054d352016-10-09 17:30:27 +0200663 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500664
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600665 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500666 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600667
668 cairo_surface_destroy(output->gl.border.top);
669 cairo_surface_destroy(output->gl.border.left);
670 cairo_surface_destroy(output->gl.border.right);
671 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500672
Armin Krezović174448a2016-09-30 14:11:09 +0200673 return 0;
674}
Matt Roper361d2ad2011-08-29 13:52:23 -0700675
Armin Krezović174448a2016-09-30 14:11:09 +0200676static void
677wayland_output_destroy(struct weston_output *base)
678{
679 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +0200680
681 wayland_output_disable(&output->base);
682
Armin Krezović174448a2016-09-30 14:11:09 +0200683 weston_output_destroy(&output->base);
684
685 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700686}
687
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300688static const struct wl_shell_surface_listener shell_surface_listener;
689
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200690static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500691wayland_output_init_gl_renderer(struct wayland_output *output)
692{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500693 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500694
695 if (output->frame) {
696 fwidth = frame_width(output->frame);
697 fheight = frame_height(output->frame);
698 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500699 fwidth = output->base.current_mode->width;
700 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500701 }
702
703 output->gl.egl_window =
704 wl_egl_window_create(output->parent.surface,
705 fwidth, fheight);
706 if (!output->gl.egl_window) {
707 weston_log("failure to create wl_egl_window\n");
708 return -1;
709 }
710
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200711 if (gl_renderer->output_window_create(&output->base,
712 output->gl.egl_window,
713 output->gl.egl_window,
714 gl_renderer->alpha_attribs,
715 NULL,
716 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500717 goto cleanup_window;
718
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500719 return 0;
720
721cleanup_window:
722 wl_egl_window_destroy(output->gl.egl_window);
723 return -1;
724}
725
726static int
727wayland_output_init_pixman_renderer(struct wayland_output *output)
728{
729 return pixman_renderer_output_create(&output->base);
730}
731
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600732static void
733wayland_output_resize_surface(struct wayland_output *output)
734{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300735 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200736 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600737 int32_t ix, iy, iwidth, iheight;
738 int32_t width, height;
739 struct wl_region *region;
740
741 width = output->base.current_mode->width;
742 height = output->base.current_mode->height;
743
744 if (output->frame) {
745 frame_resize_inside(output->frame, width, height);
746
747 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300748 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600749 wl_region_add(region, ix, iy, iwidth, iheight);
750 wl_surface_set_input_region(output->parent.surface, region);
751 wl_region_destroy(region);
752
753 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300754 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600755 wl_region_add(region, ix, iy, iwidth, iheight);
756 wl_surface_set_opaque_region(output->parent.surface, region);
757 wl_region_destroy(region);
758
759 width = frame_width(output->frame);
760 height = frame_height(output->frame);
761 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300762 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600763 wl_region_add(region, 0, 0, width, height);
764 wl_surface_set_input_region(output->parent.surface, region);
765 wl_region_destroy(region);
766
Giulio Camuffo954f1832014-10-11 18:27:30 +0300767 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600768 wl_region_add(region, 0, 0, width, height);
769 wl_surface_set_opaque_region(output->parent.surface, region);
770 wl_region_destroy(region);
771 }
772
773 if (output->gl.egl_window) {
774 wl_egl_window_resize(output->gl.egl_window,
775 width, height, 0, 0);
776
777 /* These will need to be re-created due to the resize */
778 gl_renderer->output_set_border(&output->base,
779 GL_RENDERER_BORDER_TOP,
780 0, 0, 0, NULL);
781 cairo_surface_destroy(output->gl.border.top);
782 output->gl.border.top = NULL;
783 gl_renderer->output_set_border(&output->base,
784 GL_RENDERER_BORDER_LEFT,
785 0, 0, 0, NULL);
786 cairo_surface_destroy(output->gl.border.left);
787 output->gl.border.left = NULL;
788 gl_renderer->output_set_border(&output->base,
789 GL_RENDERER_BORDER_RIGHT,
790 0, 0, 0, NULL);
791 cairo_surface_destroy(output->gl.border.right);
792 output->gl.border.right = NULL;
793 gl_renderer->output_set_border(&output->base,
794 GL_RENDERER_BORDER_BOTTOM,
795 0, 0, 0, NULL);
796 cairo_surface_destroy(output->gl.border.bottom);
797 output->gl.border.bottom = NULL;
798 }
799
Armin Krezović2e662522016-10-09 17:30:29 +0200800 wayland_output_destroy_shm_buffers(output);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600801}
802
803static int
804wayland_output_set_windowed(struct wayland_output *output)
805{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300806 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200807 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600808 int tlen;
809 char *title;
810
811 if (output->frame)
812 return 0;
813
814 if (output->name) {
815 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
816 title = malloc(tlen + 1);
817 if (!title)
818 return -1;
819
820 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
821 } else {
822 title = strdup(WINDOW_TITLE);
823 }
824
Giulio Camuffo954f1832014-10-11 18:27:30 +0300825 if (!b->theme) {
826 b->theme = theme_create();
827 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600828 free(title);
829 return -1;
830 }
831 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300832 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600833 FRAME_BUTTON_CLOSE, title);
834 free(title);
835 if (!output->frame)
836 return -1;
837
838 if (output->keyboard_count)
839 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
840
841 wayland_output_resize_surface(output);
842
843 wl_shell_surface_set_toplevel(output->parent.shell_surface);
844
845 return 0;
846}
847
848static void
849wayland_output_set_fullscreen(struct wayland_output *output,
850 enum wl_shell_surface_fullscreen_method method,
851 uint32_t framerate, struct wl_output *target)
852{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300853 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200854 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500855
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600856 if (output->frame) {
857 frame_destroy(output->frame);
858 output->frame = NULL;
859 }
860
861 wayland_output_resize_surface(output);
862
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500863 if (output->parent.shell_surface) {
864 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
865 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300866 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800867 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
868 output->parent.surface,
869 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500870 }
871}
872
873static struct weston_mode *
874wayland_output_choose_mode(struct wayland_output *output,
875 struct weston_mode *ref_mode)
876{
877 struct weston_mode *mode;
878
879 /* First look for an exact match */
880 wl_list_for_each(mode, &output->base.mode_list, link)
881 if (mode->width == ref_mode->width &&
882 mode->height == ref_mode->height &&
883 mode->refresh == ref_mode->refresh)
884 return mode;
885
886 /* If we can't find an exact match, ignore refresh and try again */
887 wl_list_for_each(mode, &output->base.mode_list, link)
888 if (mode->width == ref_mode->width &&
889 mode->height == ref_mode->height)
890 return mode;
891
892 /* Yeah, we failed */
893 return NULL;
894}
895
896enum mode_status {
897 MODE_STATUS_UNKNOWN,
898 MODE_STATUS_SUCCESS,
899 MODE_STATUS_FAIL,
900 MODE_STATUS_CANCEL,
901};
902
903static void
904mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800905 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500906{
907 enum mode_status *value = data;
908
909 printf("Mode switch successful\n");
910
911 *value = MODE_STATUS_SUCCESS;
912}
913
914static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800915mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500916{
917 enum mode_status *value = data;
918
919 printf("Mode switch failed\n");
920
921 *value = MODE_STATUS_FAIL;
922}
923
924static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800925mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500926{
927 enum mode_status *value = data;
928
929 printf("Mode switch cancelled\n");
930
931 *value = MODE_STATUS_CANCEL;
932}
933
Jonas Ådahl496adb32015-11-17 16:00:27 +0800934struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500935 mode_feedback_successful,
936 mode_feedback_failed,
937 mode_feedback_cancelled,
938};
939
940static int
941wayland_output_switch_mode(struct weston_output *output_base,
942 struct weston_mode *mode)
943{
Armin Krezović938dc522016-08-01 19:17:57 +0200944 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300945 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500946 struct wl_surface *old_surface;
947 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800948 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500949 enum mode_status mode_status;
950 int ret = 0;
951
952 if (output_base == NULL) {
953 weston_log("output is NULL.\n");
954 return -1;
955 }
956
957 if (mode == NULL) {
958 weston_log("mode is NULL.\n");
959 return -1;
960 }
961
Armin Krezović938dc522016-08-01 19:17:57 +0200962 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500963
Giulio Camuffo954f1832014-10-11 18:27:30 +0300964 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500965 return -1;
966
967 mode = wayland_output_choose_mode(output, mode);
968 if (mode == NULL)
969 return -1;
970
971 if (output->base.current_mode == mode)
972 return 0;
973
974 old_mode = output->base.current_mode;
975 old_surface = output->parent.surface;
976 output->base.current_mode = mode;
977 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300978 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500979 wl_surface_set_user_data(output->parent.surface, output);
980
981 /* Blow the old buffers because we changed size/surfaces */
982 wayland_output_resize_surface(output);
983
984 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800985 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
986 output->parent.surface,
987 output->parent.output,
988 mode->refresh);
989 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
990 &mode_feedback_listener,
991 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500992
993 /* This should kick-start things again */
Armin Krezović2d321e32016-10-09 17:30:25 +0200994 output->parent.draw_initial_frame = true;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500995 wayland_output_start_repaint_loop(&output->base);
996
997 mode_status = MODE_STATUS_UNKNOWN;
998 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300999 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001000
Jonas Ådahl496adb32015-11-17 16:00:27 +08001001 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001002
1003 if (mode_status == MODE_STATUS_FAIL) {
1004 output->base.current_mode = old_mode;
1005 wl_surface_destroy(output->parent.surface);
1006 output->parent.surface = old_surface;
1007 wayland_output_resize_surface(output);
1008
1009 return -1;
1010 }
1011
1012 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1013 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1014
Giulio Camuffo954f1832014-10-11 18:27:30 +03001015 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001016 pixman_renderer_output_destroy(output_base);
1017 if (wayland_output_init_pixman_renderer(output) < 0)
1018 goto err_output;
1019 } else {
1020 gl_renderer->output_destroy(output_base);
1021 wl_egl_window_destroy(output->gl.egl_window);
1022 if (wayland_output_init_gl_renderer(output) < 0)
1023 goto err_output;
1024 }
1025 wl_surface_destroy(old_surface);
1026
1027 weston_output_schedule_repaint(&output->base);
1028
1029 return 0;
1030
1031err_output:
1032 /* XXX */
1033 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001034}
1035
Armin Krezović174448a2016-09-30 14:11:09 +02001036static int
1037wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001038{
Armin Krezović174448a2016-09-30 14:11:09 +02001039 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001040
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001041 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001042 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001043 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001044 return -1;
1045
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001046 wl_surface_set_user_data(output->parent.surface, output);
1047
Armin Krezović2d321e32016-10-09 17:30:25 +02001048 output->parent.draw_initial_frame = true;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001049
Giulio Camuffo954f1832014-10-11 18:27:30 +03001050 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001051 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001052 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001053 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001054 if (!output->parent.shell_surface) {
1055 wl_surface_destroy(output->parent.surface);
1056 return -1;
1057 }
1058
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001059 wl_shell_surface_add_listener(output->parent.shell_surface,
1060 &shell_surface_listener, output);
1061 }
1062
Armin Krezović174448a2016-09-30 14:11:09 +02001063 return 0;
1064}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001065
Armin Krezović174448a2016-09-30 14:11:09 +02001066static int
1067wayland_output_enable(struct weston_output *base)
1068{
1069 struct wayland_output *output = to_wayland_output(base);
1070 struct wayland_backend *b = to_wayland_backend(base->compositor);
1071 int ret = 0;
1072
Armin Krezović174448a2016-09-30 14:11:09 +02001073 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1074 output->base.current_mode->width,
1075 output->base.current_mode->height,
1076 output->base.x, output->base.y);
1077
Armin Krezovićf054d352016-10-09 17:30:27 +02001078 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001079 ret = wayland_backend_create_output_surface(output);
1080
1081 if (ret < 0)
1082 return -1;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001083
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001084 wl_list_init(&output->shm.buffers);
1085 wl_list_init(&output->shm.free_buffers);
1086
Giulio Camuffo954f1832014-10-11 18:27:30 +03001087 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001088 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001089 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001090
1091 output->base.repaint = wayland_output_repaint_pixman;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001092 } else {
1093 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001094 goto err_output;
Armin Krezovićf16de172016-10-09 17:30:26 +02001095
1096 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001097 }
1098
Armin Krezovićf16de172016-10-09 17:30:26 +02001099 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
1100 output->base.assign_planes = NULL;
1101 output->base.set_backlight = NULL;
1102 output->base.set_dpms = NULL;
1103 output->base.switch_mode = wayland_output_switch_mode;
1104
Armin Krezović174448a2016-09-30 14:11:09 +02001105 if (b->sprawl_across_outputs) {
1106 wayland_output_set_fullscreen(output,
1107 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1108 output->mode.refresh, output->parent.output);
1109
1110 if (output->parent.shell_surface) {
1111 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1112 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1113 output->mode.refresh, output->parent.output);
1114 } else if (b->parent.fshell) {
1115 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1116 output->parent.surface,
1117 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1118 output->parent.output);
1119 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1120 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1121 output->parent.surface,
1122 output->parent.output,
1123 output->mode.refresh));
1124 }
1125 } else if (b->fullscreen) {
1126 wayland_output_set_fullscreen(output, 0, 0, NULL);
1127 } else {
1128 wayland_output_set_windowed(output);
1129 }
1130
1131 return 0;
1132
1133err_output:
Armin Krezovićf054d352016-10-09 17:30:27 +02001134 wayland_backend_destroy_output_surface(output);
Armin Krezović174448a2016-09-30 14:11:09 +02001135
1136 return -1;
1137}
1138
1139static struct wayland_output *
1140wayland_output_create_common(void)
1141{
1142 struct wayland_output *output;
1143
1144 output = zalloc(sizeof *output);
1145 if (output == NULL) {
1146 perror("zalloc");
1147 return NULL;
1148 }
1149
Matt Roper361d2ad2011-08-29 13:52:23 -07001150 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001151 output->base.disable = wayland_output_disable;
1152 output->base.enable = wayland_output_enable;
1153
1154 return output;
1155}
1156
1157static int
1158wayland_output_create(struct weston_compositor *compositor, const char *name)
1159{
1160 struct wayland_output *output = wayland_output_create_common();
1161
1162 /* name can't be NULL. */
1163 assert(name);
1164
1165 output->base.name = strdup(name);
1166
Armin Krezović40087402016-09-30 14:11:12 +02001167 weston_output_init(&output->base, compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001168 weston_compositor_add_pending_output(&output->base, compositor);
1169
1170 return 0;
1171}
1172
1173static int
1174wayland_output_set_size(struct weston_output *base, int width, int height)
1175{
1176 struct wayland_output *output = to_wayland_output(base);
Armin Krezović174448a2016-09-30 14:11:09 +02001177 int output_width, output_height;
1178
1179 /* We can only be called once. */
1180 assert(!output->base.current_mode);
1181
1182 /* Make sure we have scale set. */
1183 assert(output->base.scale);
1184
1185 if (width < 1) {
1186 weston_log("Invalid width \"%d\" for output %s\n",
1187 width, output->base.name);
1188 return -1;
1189 }
1190
1191 if (height < 1) {
1192 weston_log("Invalid height \"%d\" for output %s\n",
1193 height, output->base.name);
1194 return -1;
1195 }
1196
1197 output_width = width * output->base.scale;
1198 output_height = height * output->base.scale;
1199
1200 output->mode.flags =
1201 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1202
1203 output->mode.width = output_width;
1204 output->mode.height = output_height;
1205 output->mode.refresh = 60000;
1206 output->scale = output->base.scale;
1207 wl_list_init(&output->base.mode_list);
1208 wl_list_insert(&output->base.mode_list, &output->mode.link);
1209
1210 output->base.current_mode = &output->mode;
1211 output->base.make = "wayland";
1212 output->base.model = "none";
1213
1214 /* XXX: Calculate proper size. */
1215 output->base.mm_width = width;
1216 output->base.mm_height = height;
1217
Armin Krezović174448a2016-09-30 14:11:09 +02001218 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001219}
1220
Armin Krezović174448a2016-09-30 14:11:09 +02001221static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001222wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001223 struct wayland_parent_output *poutput)
1224{
Armin Krezović174448a2016-09-30 14:11:09 +02001225 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001226 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001227
1228 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001229
1230 if (poutput->current_mode) {
1231 mode = poutput->current_mode;
1232 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001233 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001234 } else if (!wl_list_empty(&poutput->mode_list)) {
1235 mode = container_of(poutput->mode_list.next,
1236 struct weston_mode, link);
1237 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001238 weston_log("No valid modes found. Skipping output.\n");
1239 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001240 }
1241
Armin Krezović40087402016-09-30 14:11:12 +02001242 weston_output_init(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001243
Armin Krezović174448a2016-09-30 14:11:09 +02001244 output->base.scale = 1;
1245 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001246
Armin Krezović174448a2016-09-30 14:11:09 +02001247 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1248 goto out;
1249
1250 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001251 output->parent.output = poutput->global;
1252
1253 output->base.make = poutput->physical.make;
1254 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001255
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001256 wl_list_init(&output->base.mode_list);
1257 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1258 wl_list_init(&poutput->mode_list);
1259
Armin Krezović174448a2016-09-30 14:11:09 +02001260 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001261
Armin Krezović174448a2016-09-30 14:11:09 +02001262 return 0;
1263
1264out:
1265 free(output->name);
1266 free(output);
1267
1268 return -1;
1269}
1270
1271static int
1272wayland_output_create_fullscreen(struct wayland_backend *b)
1273{
1274 struct wayland_output *output = wayland_output_create_common();
1275 int width = 0, height = 0;
1276
1277 output->base.name = strdup("wayland-fullscreen");
1278
Armin Krezović40087402016-09-30 14:11:12 +02001279 weston_output_init(&output->base, b->compositor);
Armin Krezović174448a2016-09-30 14:11:09 +02001280
1281 output->base.scale = 1;
1282 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1283
1284 if (wayland_backend_create_output_surface(output) < 0)
1285 goto err_surface;
1286
1287 /* What should size be set if conditional is false? */
1288 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001289 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
Armin Krezović174448a2016-09-30 14:11:09 +02001290 0, 0, NULL);
1291 wl_display_roundtrip(b->parent.wl_display);
1292
1293 width = output->parent.configure_width;
1294 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001295 }
1296
Armin Krezović174448a2016-09-30 14:11:09 +02001297 if (wayland_output_set_size(&output->base, width, height) < 0)
1298 goto err_set_size;
1299
1300 weston_compositor_add_pending_output(&output->base, b->compositor);
1301
1302 return 0;
1303
1304err_set_size:
1305 wayland_backend_destroy_output_surface(output);
1306err_surface:
1307 free(output->name);
1308 free(output);
1309
1310 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001311}
1312
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001313static void
1314shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1315 uint32_t serial)
1316{
1317 wl_shell_surface_pong(shell_surface, serial);
1318}
1319
1320static void
1321shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1322 uint32_t edges, int32_t width, int32_t height)
1323{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001324 struct wayland_output *output = data;
1325
1326 output->parent.configure_width = width;
1327 output->parent.configure_height = height;
1328
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001329 /* FIXME: implement resizing */
1330}
1331
1332static void
1333shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1334{
1335}
1336
1337static const struct wl_shell_surface_listener shell_surface_listener = {
1338 shell_surface_ping,
1339 shell_surface_configure,
1340 shell_surface_popup_done
1341};
1342
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001343/* Events received from the wayland-server this compositor is client of: */
1344
Jason Ekstrand7744f712013-10-27 22:24:55 -05001345/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001346static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001347input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001348{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001349
Jason Ekstrand7744f712013-10-27 22:24:55 -05001350 struct wl_buffer *buffer;
1351 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001352
Giulio Camuffo954f1832014-10-11 18:27:30 +03001353 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001354 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001355
Giulio Camuffo954f1832014-10-11 18:27:30 +03001356 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001357 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001358 if (!buffer)
1359 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001360
1361 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1362 input->parent.cursor.surface,
1363 image->hotspot_x, image->hotspot_y);
1364
1365 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1366 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1367 image->width, image->height);
1368 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001369}
1370
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001371static void
Daniel Stone37816df2012-05-16 18:45:18 +01001372input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1373 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001374 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001375{
1376 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001377 int32_t fx, fy;
1378 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001379 double x, y;
1380
1381 x = wl_fixed_to_double(fixed_x);
1382 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001383
Daniel Stone50692802012-06-22 13:21:41 +01001384 /* XXX: If we get a modifier event immediately before the focus,
1385 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001386 input->enter_serial = serial;
1387 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001388
1389 if (input->output->frame) {
1390 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001391 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001392 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001393 x -= fx;
1394 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001395
1396 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1397 weston_output_schedule_repaint(&input->output->base);
1398 } else {
1399 location = THEME_LOCATION_CLIENT_AREA;
1400 }
1401
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001402 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001403
1404 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001405 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001406 notify_pointer_focus(&input->base, &input->output->base, x, y);
1407 wl_pointer_set_cursor(input->parent.pointer,
1408 input->enter_serial, NULL, 0, 0);
1409 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001410 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001411 notify_pointer_focus(&input->base, NULL, 0, 0);
1412 input_set_cursor(input);
1413 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001414}
1415
1416static void
Daniel Stone37816df2012-05-16 18:45:18 +01001417input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1418 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001419{
1420 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001421
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001422 if (!input->output)
1423 return;
1424
Jason Ekstrand7744f712013-10-27 22:24:55 -05001425 if (input->output->frame) {
1426 frame_pointer_leave(input->output->frame, input);
1427
1428 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1429 weston_output_schedule_repaint(&input->output->base);
1430 }
1431
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001432 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001433 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001434 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001435}
1436
1437static void
Daniel Stone37816df2012-05-16 18:45:18 +01001438input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001439 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001440{
1441 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001442 int32_t fx, fy;
1443 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001444 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001445 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001446
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001447 if (!input->output)
1448 return;
1449
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001450 x = wl_fixed_to_double(fixed_x);
1451 y = wl_fixed_to_double(fixed_y);
1452
Jason Ekstrand7744f712013-10-27 22:24:55 -05001453 if (input->output->frame) {
1454 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001455 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001456 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001457 x -= fx;
1458 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001459
1460 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1461 weston_output_schedule_repaint(&input->output->base);
1462 } else {
1463 location = THEME_LOCATION_CLIENT_AREA;
1464 }
1465
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001466 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001467
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001468 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001469 input_set_cursor(input);
1470 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001471 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001472 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001473 } else if (!input->has_focus &&
1474 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001475 wl_pointer_set_cursor(input->parent.pointer,
1476 input->enter_serial, NULL, 0, 0);
1477 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001478 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001479 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001480 }
1481
Peter Hutterer87743e92016-01-18 16:38:22 +10001482 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001483 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001484 want_frame = true;
1485 }
1486
1487 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1488 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001489}
1490
1491static void
1492input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001493 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001494 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001495{
1496 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001497 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001498
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001499 if (!input->output)
1500 return;
1501
Jason Ekstrand7744f712013-10-27 22:24:55 -05001502 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001503 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001504 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001505
1506 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1507
1508 wl_shell_surface_move(input->output->parent.shell_surface,
1509 input->parent.seat, serial);
1510 frame_status_clear(input->output->frame,
1511 FRAME_STATUS_MOVE);
1512 return;
1513 }
1514
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001515 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1516 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001517 input->output = NULL;
1518 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001519
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001521 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001522
1523 return;
1524 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001525
1526 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1527 weston_output_schedule_repaint(&input->output->base);
1528 } else {
1529 location = THEME_LOCATION_CLIENT_AREA;
1530 }
1531
Peter Hutterer87743e92016-01-18 16:38:22 +10001532 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001533 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001534 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1535 notify_pointer_frame(&input->base);
1536 }
Daniel Stone37816df2012-05-16 18:45:18 +01001537}
1538
1539static void
1540input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001541 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001542{
1543 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001544 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001545
Peter Hutterer89b6a492016-01-18 15:58:17 +10001546 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001547 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001548
Peter Hutterer87743e92016-01-18 16:38:22 +10001549 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1550 input->vert.has_discrete) {
1551 weston_event.has_discrete = true;
1552 weston_event.discrete = input->vert.discrete;
1553 input->vert.has_discrete = false;
1554 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1555 input->horiz.has_discrete) {
1556 weston_event.has_discrete = true;
1557 weston_event.discrete = input->horiz.discrete;
1558 input->horiz.has_discrete = false;
1559 }
1560
Peter Hutterer89b6a492016-01-18 15:58:17 +10001561 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001562
1563 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1564 notify_pointer_frame(&input->base);
1565}
1566
1567static void
1568input_handle_frame(void *data, struct wl_pointer *pointer)
1569{
1570 struct wayland_input *input = data;
1571
1572 notify_pointer_frame(&input->base);
1573}
1574
1575static void
1576input_handle_axis_source(void *data, struct wl_pointer *pointer,
1577 uint32_t source)
1578{
1579 struct wayland_input *input = data;
1580
1581 notify_axis_source(&input->base, source);
1582}
1583
1584static void
1585input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1586 uint32_t time, uint32_t axis)
1587{
1588 struct wayland_input *input = data;
1589 struct weston_pointer_axis_event weston_event;
1590
1591 weston_event.axis = axis;
1592 weston_event.value = 0;
1593
1594 notify_axis(&input->base, time, &weston_event);
1595}
1596
1597static void
1598input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1599 uint32_t axis, int32_t discrete)
1600{
1601 struct wayland_input *input = data;
1602
1603 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1604 input->vert.has_discrete = true;
1605 input->vert.discrete = discrete;
1606 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1607 input->horiz.has_discrete = true;
1608 input->horiz.discrete = discrete;
1609 }
Daniel Stone37816df2012-05-16 18:45:18 +01001610}
1611
1612static const struct wl_pointer_listener pointer_listener = {
1613 input_handle_pointer_enter,
1614 input_handle_pointer_leave,
1615 input_handle_motion,
1616 input_handle_button,
1617 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001618 input_handle_frame,
1619 input_handle_axis_source,
1620 input_handle_axis_stop,
1621 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001622};
1623
1624static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001625input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1626 int fd, uint32_t size)
1627{
1628 struct wayland_input *input = data;
1629 struct xkb_keymap *keymap;
1630 char *map_str;
1631
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001632 if (!data) {
1633 close(fd);
1634 return;
1635 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001636
1637 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1638 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1639 if (map_str == MAP_FAILED) {
1640 weston_log("mmap failed: %m\n");
1641 goto error;
1642 }
1643
Giulio Camuffo954f1832014-10-11 18:27:30 +03001644 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001645 map_str,
1646 XKB_KEYMAP_FORMAT_TEXT_V1,
1647 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001648 munmap(map_str, size);
1649
1650 if (!keymap) {
1651 weston_log("failed to compile keymap\n");
1652 goto error;
1653 }
1654
1655 input->keyboard_state_update = STATE_UPDATE_NONE;
1656 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1657 weston_log("No keymap provided; falling back to defalt\n");
1658 keymap = NULL;
1659 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1660 } else {
1661 weston_log("Invalid keymap\n");
1662 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001663 }
1664
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001665 close(fd);
1666
Derek Foreman1281a362015-07-31 16:55:32 -05001667 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001668 weston_seat_update_keymap(&input->base, keymap);
1669 else
1670 weston_seat_init_keyboard(&input->base, keymap);
1671
Ran Benitac9c74152014-08-19 23:59:52 +03001672 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001673
1674 return;
1675
1676error:
1677 wl_keyboard_release(input->parent.keyboard);
1678 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001679}
1680
1681static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001682input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001683 struct wl_keyboard *keyboard,
1684 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001685 struct wl_surface *surface,
1686 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001687{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001688 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001689 struct wayland_output *focus;
1690
1691 focus = input->keyboard_focus;
1692 if (focus) {
1693 /* This shouldn't happen */
1694 focus->keyboard_count--;
1695 if (!focus->keyboard_count && focus->frame)
1696 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1697 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1698 weston_output_schedule_repaint(&focus->base);
1699 }
1700
1701 input->keyboard_focus = wl_surface_get_user_data(surface);
1702 input->keyboard_focus->keyboard_count++;
1703
1704 focus = input->keyboard_focus;
1705 if (focus->frame) {
1706 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1707 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1708 weston_output_schedule_repaint(&focus->base);
1709 }
1710
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001711
Daniel Stone50692802012-06-22 13:21:41 +01001712 /* XXX: If we get a modifier event immediately before the focus,
1713 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001714 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001715 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001716}
1717
1718static void
1719input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001720 struct wl_keyboard *keyboard,
1721 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001722 struct wl_surface *surface)
1723{
1724 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001725 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001726
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001727 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001728
1729 focus = input->keyboard_focus;
1730 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001731 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001732
1733 focus->keyboard_count--;
1734 if (!focus->keyboard_count && focus->frame) {
1735 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1736 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1737 weston_output_schedule_repaint(&focus->base);
1738 }
1739
1740 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001741}
1742
Daniel Stone37816df2012-05-16 18:45:18 +01001743static void
1744input_handle_key(void *data, struct wl_keyboard *keyboard,
1745 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1746{
1747 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001748
Daniel Stone50692802012-06-22 13:21:41 +01001749 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001750 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001751 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001752 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001753 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001754}
1755
Daniel Stone351eb612012-05-31 15:27:47 -04001756static void
Derek Foreman1281a362015-07-31 16:55:32 -05001757input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001758 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001759 uint32_t mods_latched, uint32_t mods_locked,
1760 uint32_t group)
1761{
Derek Foreman1281a362015-07-31 16:55:32 -05001762 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001763 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001764 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001765 uint32_t serial_out;
1766
1767 /* If we get a key event followed by a modifier event with the
1768 * same serial number, then we try to preserve those semantics by
1769 * reusing the same serial number on the way out too. */
1770 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001771 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001772 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001773 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001774
Derek Foreman1281a362015-07-31 16:55:32 -05001775 keyboard = weston_seat_get_keyboard(&input->base);
1776 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001777 mods_depressed, mods_latched,
1778 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001779 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001780}
1781
Jonny Lamb497994a2014-08-12 14:58:26 +02001782static void
1783input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1784 int32_t rate, int32_t delay)
1785{
1786 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001787 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001788
Giulio Camuffo954f1832014-10-11 18:27:30 +03001789 b->compositor->kb_repeat_rate = rate;
1790 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001791}
1792
Daniel Stone37816df2012-05-16 18:45:18 +01001793static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001794 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001795 input_handle_keyboard_enter,
1796 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001797 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001798 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001799 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001800};
1801
1802static void
Derek Foreman748c6952015-11-06 15:56:10 -06001803input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1804 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001805 struct wl_surface *surface, int32_t id,
1806 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001807{
1808 struct wayland_input *input = data;
1809 struct wayland_output *output;
1810 enum theme_location location;
1811 bool first_touch;
1812 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001813 double x, y;
1814
1815 x = wl_fixed_to_double(fixed_x);
1816 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001817
1818 first_touch = (input->touch_points == 0);
1819 input->touch_points++;
1820
1821 input->touch_focus = wl_surface_get_user_data(surface);
1822 output = input->touch_focus;
1823 if (!first_touch && !input->touch_active)
1824 return;
1825
1826 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001827 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001828
1829 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001830 x -= fx;
1831 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001832
1833 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1834 weston_output_schedule_repaint(&output->base);
1835
1836 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1837 input->touch_points--;
1838 wl_shell_surface_move(output->parent.shell_surface,
1839 input->parent.seat, serial);
1840 frame_status_clear(output->frame,
1841 FRAME_STATUS_MOVE);
1842 return;
1843 }
1844
1845 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1846 return;
1847 }
1848
1849 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1850
1851 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1852 input->touch_active = true;
1853}
1854
1855static void
1856input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1857 uint32_t serial, uint32_t time, int32_t id)
1858{
1859 struct wayland_input *input = data;
1860 struct wayland_output *output = input->touch_focus;
1861 bool active = input->touch_active;
1862
1863 input->touch_points--;
1864 if (input->touch_points == 0) {
1865 input->touch_focus = NULL;
1866 input->touch_active = false;
1867 }
1868
1869 if (!output)
1870 return;
1871
1872 if (output->frame) {
1873 frame_touch_up(output->frame, input, id);
1874
1875 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1876 wayland_output_destroy(&output->base);
1877 input->touch_focus = NULL;
1878 input->keyboard_focus = NULL;
1879 if (wl_list_empty(&input->backend->compositor->output_list))
1880 weston_compositor_exit(input->backend->compositor);
1881
1882 return;
1883 }
1884 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1885 weston_output_schedule_repaint(&output->base);
1886 }
1887
1888 if (active)
1889 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1890}
1891
1892static void
1893input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001894 uint32_t time, int32_t id,
1895 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001896{
1897 struct wayland_input *input = data;
1898 struct wayland_output *output = input->touch_focus;
1899 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001900 double x, y;
1901
1902 x = wl_fixed_to_double(fixed_x);
1903 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001904
1905 if (!output || !input->touch_active)
1906 return;
1907
1908 if (output->frame) {
1909 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001910 x -= fx;
1911 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001912 }
1913
1914 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1915
1916 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1917}
1918
1919static void
1920input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1921{
1922 struct wayland_input *input = data;
1923
1924 if (!input->touch_focus || !input->touch_active)
1925 return;
1926
1927 notify_touch_frame(&input->base);
1928}
1929
1930static void
1931input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1932{
1933 struct wayland_input *input = data;
1934
1935 if (!input->touch_focus || !input->touch_active)
1936 return;
1937
1938 notify_touch_cancel(&input->base);
1939}
1940
1941static const struct wl_touch_listener touch_listener = {
1942 input_handle_touch_down,
1943 input_handle_touch_up,
1944 input_handle_touch_motion,
1945 input_handle_touch_frame,
1946 input_handle_touch_cancel,
1947};
1948
1949
1950static void
Daniel Stone37816df2012-05-16 18:45:18 +01001951input_handle_capabilities(void *data, struct wl_seat *seat,
1952 enum wl_seat_capability caps)
1953{
1954 struct wayland_input *input = data;
1955
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001956 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1957 input->parent.pointer = wl_seat_get_pointer(seat);
1958 wl_pointer_set_user_data(input->parent.pointer, input);
1959 wl_pointer_add_listener(input->parent.pointer,
1960 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001961 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001962 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001963 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1964 wl_pointer_release(input->parent.pointer);
1965 else
1966 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001967 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001968 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001969 }
1970
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001971 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1972 input->parent.keyboard = wl_seat_get_keyboard(seat);
1973 wl_keyboard_set_user_data(input->parent.keyboard, input);
1974 wl_keyboard_add_listener(input->parent.keyboard,
1975 &keyboard_listener, input);
1976 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001977 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1978 wl_keyboard_release(input->parent.keyboard);
1979 else
1980 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001981 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001982 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001983 }
Derek Foreman748c6952015-11-06 15:56:10 -06001984
1985 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1986 input->parent.touch = wl_seat_get_touch(seat);
1987 wl_touch_set_user_data(input->parent.touch, input);
1988 wl_touch_add_listener(input->parent.touch,
1989 &touch_listener, input);
1990 weston_seat_init_touch(&input->base);
1991 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1992 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1993 wl_touch_release(input->parent.touch);
1994 else
1995 wl_touch_destroy(input->parent.touch);
1996 input->parent.touch = NULL;
1997 weston_seat_release_touch(&input->base);
1998 }
Daniel Stone37816df2012-05-16 18:45:18 +01001999}
2000
Jonny Lamb497994a2014-08-12 14:58:26 +02002001static void
2002input_handle_name(void *data, struct wl_seat *seat,
2003 const char *name)
2004{
2005}
2006
Daniel Stone37816df2012-05-16 18:45:18 +01002007static const struct wl_seat_listener seat_listener = {
2008 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002009 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002010};
2011
2012static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002013display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002014{
2015 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002016 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002017
Peter Huttererf3d62272013-08-08 11:57:05 +10002018 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002019 if (input == NULL)
2020 return;
2021
Giulio Camuffo954f1832014-10-11 18:27:30 +03002022 weston_seat_init(&input->base, b->compositor, "default");
2023 input->backend = b;
2024 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002025 &wl_seat_interface, version);
2026 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002027 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002028
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002029 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2030 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002031
2032 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002033 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002034
2035 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2036 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002037}
2038
2039static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002040wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2041 int32_t x, int32_t y,
2042 int32_t physical_width, int32_t physical_height,
2043 int32_t subpixel, const char *make,
2044 const char *model, int32_t transform)
2045{
2046 struct wayland_parent_output *output = data;
2047
2048 output->x = x;
2049 output->y = y;
2050 output->physical.width = physical_width;
2051 output->physical.height = physical_height;
2052 output->physical.subpixel = subpixel;
2053
2054 free(output->physical.make);
2055 output->physical.make = strdup(make);
2056 free(output->physical.model);
2057 output->physical.model = strdup(model);
2058
2059 output->transform = transform;
2060}
2061
2062static struct weston_mode *
2063find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2064{
2065 struct weston_mode *mode;
2066
2067 wl_list_for_each(mode, list, link) {
2068 if (mode->width == width && mode->height == height &&
2069 mode->refresh == refresh)
2070 return mode;
2071 }
2072
2073 mode = zalloc(sizeof *mode);
2074 if (!mode)
2075 return NULL;
2076
2077 mode->width = width;
2078 mode->height = height;
2079 mode->refresh = refresh;
2080 wl_list_insert(list, &mode->link);
2081
2082 return mode;
2083}
2084
2085static void
2086wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2087 uint32_t flags, int32_t width, int32_t height,
2088 int32_t refresh)
2089{
2090 struct wayland_parent_output *output = data;
2091 struct weston_mode *mode;
2092
2093 if (output->output) {
2094 mode = find_mode(&output->output->base.mode_list,
2095 width, height, refresh);
2096 if (!mode)
2097 return;
2098 mode->flags = flags;
2099 /* Do a mode-switch on current mode change? */
2100 } else {
2101 mode = find_mode(&output->mode_list, width, height, refresh);
2102 if (!mode)
2103 return;
2104 mode->flags = flags;
2105 if (flags & WL_OUTPUT_MODE_CURRENT)
2106 output->current_mode = mode;
2107 if (flags & WL_OUTPUT_MODE_PREFERRED)
2108 output->preferred_mode = mode;
2109 }
2110}
2111
2112static const struct wl_output_listener output_listener = {
2113 wayland_parent_output_geometry,
2114 wayland_parent_output_mode
2115};
2116
2117static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002118wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002119{
2120 struct wayland_parent_output *output;
2121
2122 output = zalloc(sizeof *output);
2123 if (!output)
2124 return;
2125
2126 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002127 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002128 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002129 if (!output->global) {
2130 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002131 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002132 }
2133
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002134 wl_output_add_listener(output->global, &output_listener, output);
2135
2136 output->scale = 0;
2137 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2138 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2139 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002140 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002141
Giulio Camuffo954f1832014-10-11 18:27:30 +03002142 if (b->sprawl_across_outputs) {
2143 wl_display_roundtrip(b->parent.wl_display);
2144 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002145 }
2146}
2147
2148static void
2149wayland_parent_output_destroy(struct wayland_parent_output *output)
2150{
2151 struct weston_mode *mode, *next;
2152
2153 if (output->output)
2154 wayland_output_destroy(&output->output->base);
2155
2156 wl_output_destroy(output->global);
2157 free(output->physical.make);
2158 free(output->physical.model);
2159
2160 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2161 wl_list_remove(&mode->link);
2162 free(mode);
2163 }
2164}
2165
2166static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002167registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2168 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002169{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002170 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002171
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002172 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002173 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002174 wl_registry_bind(registry, name,
2175 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002176 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002177 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002178 wl_registry_bind(registry, name,
2179 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002180 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002181 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002182 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002183 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002184 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002185 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002186 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002187 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002188 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002189 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002190 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002191 }
2192}
2193
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002194static void
2195registry_handle_global_remove(void *data, struct wl_registry *registry,
2196 uint32_t name)
2197{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002198 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002199 struct wayland_parent_output *output;
2200
Giulio Camuffo954f1832014-10-11 18:27:30 +03002201 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002202 if (output->id == name)
2203 wayland_parent_output_destroy(output);
2204}
2205
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002206static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002207 registry_handle_global,
2208 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002209};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002210
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002211static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002212wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002213{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002214 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002215 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002216
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002217 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002218 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002219 return 0;
2220 }
2221
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002222 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002223 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002224 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002225 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002226
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002227 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002228 count = wl_display_dispatch_pending(b->parent.wl_display);
2229 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002230 }
2231
2232 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002233}
2234
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002235static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002236wayland_restore(struct weston_compositor *ec)
2237{
2238}
2239
2240static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002241wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002242{
Armin Krezović938dc522016-08-01 19:17:57 +02002243 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002244
Armin Krezović78895c52016-10-09 17:30:28 +02002245 wl_event_source_remove(b->parent.wl_source);
2246
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002247 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002248
Giulio Camuffo954f1832014-10-11 18:27:30 +03002249 if (b->parent.shm)
2250 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002251
Armin Krezović78895c52016-10-09 17:30:28 +02002252 if (b->parent.fshell)
2253 zwp_fullscreen_shell_v1_release(b->parent.fshell);
2254
2255 if (b->parent.compositor)
2256 wl_compositor_destroy(b->parent.compositor);
2257
2258 wl_registry_destroy(b->parent.registry);
2259 wl_display_flush(b->parent.wl_display);
2260 wl_display_disconnect(b->parent.wl_display);
2261
2262 if (b->theme)
2263 theme_destroy(b->theme);
2264
2265 if (b->frame_device)
2266 cairo_device_destroy(b->frame_device);
2267
2268 wl_cursor_theme_destroy(b->cursor_theme);
2269
Giulio Camuffo954f1832014-10-11 18:27:30 +03002270 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002271}
2272
Jason Ekstrand7744f712013-10-27 22:24:55 -05002273static const char *left_ptrs[] = {
2274 "left_ptr",
2275 "default",
2276 "top_left_arrow",
2277 "left-arrow"
2278};
2279
2280static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002281create_cursor(struct wayland_backend *b,
2282 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002283{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002284 unsigned int i;
2285
Benoit Gschwind244ff792016-04-28 20:33:11 +02002286 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2287 config->cursor_size,
2288 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002289 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002290 fprintf(stderr, "could not load cursor theme\n");
2291 return;
2292 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002293
Giulio Camuffo954f1832014-10-11 18:27:30 +03002294 b->cursor = NULL;
2295 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2296 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002297 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002298 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002299 fprintf(stderr, "could not load left cursor\n");
2300 return;
2301 }
2302}
2303
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002304static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002305fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2306 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002307{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002308 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002309 struct wayland_input *input = NULL;
2310
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002312 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002313 break;
2314
2315 if (!input || !input->output)
2316 return;
2317
2318 if (input->output->frame)
2319 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2320 else
2321 wayland_output_set_windowed(input->output);
2322
2323 weston_output_schedule_repaint(&input->output->base);
2324}
2325
Giulio Camuffo954f1832014-10-11 18:27:30 +03002326static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002327wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002328 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002329{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002330 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002331 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002332 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002333
Giulio Camuffo954f1832014-10-11 18:27:30 +03002334 b = zalloc(sizeof *b);
2335 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002336 return NULL;
2337
Giulio Camuffo954f1832014-10-11 18:27:30 +03002338 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002339 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002340 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002341
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002342 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002343 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002344 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002345 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002346 }
2347
Giulio Camuffo954f1832014-10-11 18:27:30 +03002348 wl_list_init(&b->parent.output_list);
2349 wl_list_init(&b->input_list);
2350 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2351 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2352 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002353
Benoit Gschwind244ff792016-04-28 20:33:11 +02002354 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002355
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002356 b->use_pixman = new_config->use_pixman;
Armin Krezović174448a2016-09-30 14:11:09 +02002357 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002358
Giulio Camuffo954f1832014-10-11 18:27:30 +03002359 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002360 gl_renderer = weston_load_module("gl-renderer.so",
2361 "gl_renderer_interface");
2362 if (!gl_renderer)
Armin Krezović7e71b872016-10-09 17:30:22 +02002363 b->use_pixman = true;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002364 }
2365
Giulio Camuffo954f1832014-10-11 18:27:30 +03002366 if (!b->use_pixman) {
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002367 if (gl_renderer->display_create(compositor,
2368 EGL_PLATFORM_WAYLAND_KHR,
2369 b->parent.wl_display,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002370 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002371 gl_renderer->alpha_attribs,
2372 NULL,
2373 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002374 weston_log("Failed to initialize the GL renderer; "
2375 "falling back to pixman.\n");
Armin Krezović7e71b872016-10-09 17:30:22 +02002376 b->use_pixman = true;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002377 }
2378 }
2379
Giulio Camuffo954f1832014-10-11 18:27:30 +03002380 if (b->use_pixman) {
2381 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002382 weston_log("Failed to initialize pixman renderer\n");
2383 goto err_display;
2384 }
2385 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002386
Giulio Camuffo954f1832014-10-11 18:27:30 +03002387 b->base.destroy = wayland_destroy;
2388 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002389
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002391
Giulio Camuffo954f1832014-10-11 18:27:30 +03002392 fd = wl_display_get_fd(b->parent.wl_display);
2393 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002394 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002395 wayland_backend_handle_event, b);
2396 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002397 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002398
Giulio Camuffo954f1832014-10-11 18:27:30 +03002399 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002400
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002401 if (compositor->renderer->import_dmabuf) {
2402 if (linux_dmabuf_setup(compositor) < 0)
2403 weston_log("Error: initializing dmabuf "
2404 "support failed.\n");
2405 }
2406
Giulio Camuffo954f1832014-10-11 18:27:30 +03002407 compositor->backend = &b->base;
2408 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002409err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002410 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002411err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002412 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002413 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002414 return NULL;
2415}
2416
2417static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002418wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002419{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002420 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002421
Giulio Camuffo954f1832014-10-11 18:27:30 +03002422 if (b->theme)
2423 theme_destroy(b->theme);
2424 if (b->frame_device)
2425 cairo_device_destroy(b->frame_device);
2426 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002427
Giulio Camuffo954f1832014-10-11 18:27:30 +03002428 weston_compositor_shutdown(b->compositor);
2429 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002430}
2431
Armin Krezović174448a2016-09-30 14:11:09 +02002432static const struct weston_windowed_output_api windowed_api = {
2433 wayland_output_set_size,
2434 wayland_output_create,
2435};
2436
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002437static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002438config_init_to_defaults(struct weston_wayland_backend_config *config)
2439{
2440}
2441
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002442WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002443backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002444 struct weston_backend_config *config_base)
2445{
2446 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002447 struct wayland_parent_output *poutput;
2448 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002449 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002450
Benoit Gschwinde091b452016-05-10 22:47:48 +02002451 if (config_base == NULL ||
2452 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2453 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2454 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002455 return -1;
2456 }
2457
2458 config_init_to_defaults(&new_config);
2459 memcpy(&new_config, config_base, config_base->struct_size);
2460
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002461 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002462
Giulio Camuffo954f1832014-10-11 18:27:30 +03002463 if (!b)
2464 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002465
Benoit Gschwind37a68072016-04-28 20:33:08 +02002466 if (new_config.sprawl || b->parent.fshell) {
Armin Krezović7f1c0b82016-10-09 17:30:23 +02002467 b->sprawl_across_outputs = true;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002468 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002469
Giulio Camuffo954f1832014-10-11 18:27:30 +03002470 wl_list_for_each(poutput, &b->parent.output_list, link)
2471 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002472
Giulio Camuffo954f1832014-10-11 18:27:30 +03002473 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002474 }
2475
Benoit Gschwind37a68072016-04-28 20:33:08 +02002476 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002477 if (wayland_output_create_fullscreen(b) < 0) {
2478 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002479 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002480 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002481
Giulio Camuffo954f1832014-10-11 18:27:30 +03002482 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002483 }
2484
Armin Krezović174448a2016-09-30 14:11:09 +02002485 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2486 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002487
Armin Krezović174448a2016-09-30 14:11:09 +02002488 if (ret < 0) {
2489 weston_log("Failed to register output API.\n");
2490 wayland_backend_destroy(b);
2491 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002492 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002493
Giulio Camuffo954f1832014-10-11 18:27:30 +03002494 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002495 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002496 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002497 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002498
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002499err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002500 wayland_backend_destroy(b);
2501 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002502}