blob: da0c4fe476c95970298a696a42271b73a5c32eda [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"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050047#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070048#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070049#include "shared/image-loader.h"
50#include "shared/os-compatibility.h"
51#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080052#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020053#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020054#include "linux-dmabuf.h"
Armin Krezović174448a2016-09-30 14:11:09 +020055#include "windowed-output-api.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056
Jason Ekstrand48ce4212013-10-27 22:25:02 -050057#define WINDOW_TITLE "Weston Compositor"
58
Giulio Camuffo954f1832014-10-11 18:27:30 +030059struct wayland_backend {
60 struct weston_backend base;
61 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062
63 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040064 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040065 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010066 struct wl_compositor *compositor;
67 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080068 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020069 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010070
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050071 struct wl_list output_list;
72
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073 struct wl_event_source *wl_source;
74 uint32_t event_mask;
75 } parent;
76
Jason Ekstrandff2fd462013-10-27 22:24:58 -050077 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050078 int sprawl_across_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +020079 int fullscreen;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050080
Jason Ekstrand7744f712013-10-27 22:24:55 -050081 struct theme *theme;
82 cairo_device_t *frame_device;
83 struct wl_cursor_theme *cursor_theme;
84 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050085
Jason Ekstrand06ced802013-11-07 20:13:29 -060086 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010087};
88
89struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050090 struct weston_output base;
91
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010092 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050093 int draw_initial_frame;
94 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050095
96 struct wl_output *output;
97 uint32_t global_id;
98
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050099 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600100 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100101 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500102
Jason Ekstrand7744f712013-10-27 22:24:55 -0500103 int keyboard_count;
104
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600105 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500106 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500107
Jason Ekstrand7744f712013-10-27 22:24:55 -0500108 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500109 struct wl_egl_window *egl_window;
110 struct {
111 cairo_surface_t *top;
112 cairo_surface_t *left;
113 cairo_surface_t *right;
114 cairo_surface_t *bottom;
115 } border;
116 } gl;
117
118 struct {
119 struct wl_list buffers;
120 struct wl_list free_buffers;
121 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500122
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500123 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500124 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100125};
126
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500127struct wayland_parent_output {
128 struct wayland_output *output;
129 struct wl_list link;
130
131 struct wl_output *global;
132 uint32_t id;
133
134 struct {
135 char *make;
136 char *model;
137 int32_t width, height;
138 uint32_t subpixel;
139 } physical;
140
141 int32_t x, y;
142 uint32_t transform;
143 uint32_t scale;
144
145 struct wl_list mode_list;
146 struct weston_mode *preferred_mode;
147 struct weston_mode *current_mode;
148};
149
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500150struct wayland_shm_buffer {
151 struct wayland_output *output;
152 struct wl_list link;
153 struct wl_list free_link;
154
155 struct wl_buffer *buffer;
156 void *data;
157 size_t size;
158 pixman_region32_t damage;
159 int frame_damaged;
160
161 pixman_image_t *pm_image;
162 cairo_surface_t *c_surface;
163};
164
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100165struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400166 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300167 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100168 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500169
170 struct {
171 struct wl_seat *seat;
172 struct wl_pointer *pointer;
173 struct wl_keyboard *keyboard;
174 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500175
176 struct {
177 struct wl_surface *surface;
178 int32_t hx, hy;
179 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500180 } parent;
181
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500182 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100183 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400184 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600185 uint32_t touch_points;
186 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600187 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600188 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600189
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400190 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600191 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500192 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000193
194 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100195};
196
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300197struct gl_renderer_interface *gl_renderer;
198
Armin Krezović938dc522016-08-01 19:17:57 +0200199static inline struct wayland_output *
200to_wayland_output(struct weston_output *base)
201{
202 return container_of(base, struct wayland_output, base);
203}
204
205static inline struct wayland_backend *
206to_wayland_backend(struct weston_compositor *base)
207{
208 return container_of(base->backend, struct wayland_backend, base);
209}
210
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500211static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500212wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
213{
214 cairo_surface_destroy(buffer->c_surface);
215 pixman_image_unref(buffer->pm_image);
216
217 wl_buffer_destroy(buffer->buffer);
218 munmap(buffer->data, buffer->size);
219
220 pixman_region32_fini(&buffer->damage);
221
222 wl_list_remove(&buffer->link);
223 wl_list_remove(&buffer->free_link);
224 free(buffer);
225}
226
227static void
228buffer_release(void *data, struct wl_buffer *buffer)
229{
230 struct wayland_shm_buffer *sb = data;
231
232 if (sb->output) {
233 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
234 } else {
235 wayland_shm_buffer_destroy(sb);
236 }
237}
238
239static const struct wl_buffer_listener buffer_listener = {
240 buffer_release
241};
242
243static struct wayland_shm_buffer *
244wayland_output_get_shm_buffer(struct wayland_output *output)
245{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300246 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200247 to_wayland_backend(output->base.compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300248 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500249 struct wayland_shm_buffer *sb;
250
251 struct wl_shm_pool *pool;
252 int width, height, stride;
253 int32_t fx, fy;
254 int fd;
255 unsigned char *data;
256
257 if (!wl_list_empty(&output->shm.free_buffers)) {
258 sb = container_of(output->shm.free_buffers.next,
259 struct wayland_shm_buffer, free_link);
260 wl_list_remove(&sb->free_link);
261 wl_list_init(&sb->free_link);
262
263 return sb;
264 }
265
266 if (output->frame) {
267 width = frame_width(output->frame);
268 height = frame_height(output->frame);
269 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500270 width = output->base.current_mode->width;
271 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500272 }
273
274 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
275
276 fd = os_create_anonymous_file(height * stride);
277 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000278 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500279 return NULL;
280 }
281
282 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
283 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000284 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500285 close(fd);
286 return NULL;
287 }
288
289 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000290 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200291 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000292 close(fd);
293 free(data);
294 return NULL;
295 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500296
297 sb->output = output;
298 wl_list_init(&sb->free_link);
299 wl_list_insert(&output->shm.buffers, &sb->link);
300
301 pixman_region32_init_rect(&sb->damage, 0, 0,
302 output->base.width, output->base.height);
303 sb->frame_damaged = 1;
304
305 sb->data = data;
306 sb->size = height * stride;
307
308 pool = wl_shm_create_pool(shm, fd, sb->size);
309
310 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
311 width, height,
312 stride,
313 WL_SHM_FORMAT_ARGB8888);
314 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
315 wl_shm_pool_destroy(pool);
316 close(fd);
317
318 memset(data, 0, sb->size);
319
320 sb->c_surface =
321 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
322 width, height, stride);
323
324 fx = 0;
325 fy = 0;
326 if (output->frame)
327 frame_interior(output->frame, &fx, &fy, 0, 0);
328 sb->pm_image =
329 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
330 (uint32_t *)(data + fy * stride) + fx,
331 stride);
332
333 return sb;
334}
335
336static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500337frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400338{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500339 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400340 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400341
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500342 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400343
344 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300345
346 /*
347 * This is the fallback case, where Presentation extension is not
348 * available from the parent compositor. We do not know the base for
349 * 'time', so we cannot feed it to finish_frame(). Do the only thing
350 * we can, and pretend finish_frame time is when we process this
351 * event.
352 */
353 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200354 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400355}
356
357static const struct wl_callback_listener frame_listener = {
358 frame_done
359};
360
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500361static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200362draw_initial_frame(struct wayland_output *output)
363{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500364 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200365
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500366 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200367
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 /* If we are rendering with GL, then orphan it so that it gets
369 * destroyed immediately */
370 if (output->gl.egl_window)
371 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500373 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500374 wl_surface_damage(output->parent.surface, 0, 0,
375 output->base.current_mode->width,
376 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200377}
378
379static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500380wayland_output_update_gl_border(struct wayland_output *output)
381{
382 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
383 cairo_t *cr;
384
385 if (!output->frame)
386 return;
387 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
388 return;
389
390 fwidth = frame_width(output->frame);
391 fheight = frame_height(output->frame);
392 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
393
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 if (!output->gl.border.top)
395 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
397 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 frame_repaint(output->frame, cr);
400 cairo_destroy(cr);
401 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
402 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500403 cairo_image_surface_get_stride(output->gl.border.top) / 4,
404 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500405
406
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500407 if (!output->gl.border.left)
408 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
410 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500411 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500412 cairo_translate(cr, 0, -iy);
413 frame_repaint(output->frame, cr);
414 cairo_destroy(cr);
415 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
416 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500417 cairo_image_surface_get_stride(output->gl.border.left) / 4,
418 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500419
420
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500421 if (!output->gl.border.right)
422 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500423 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
424 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500425 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500426 cairo_translate(cr, -(iwidth + ix), -iy);
427 frame_repaint(output->frame, cr);
428 cairo_destroy(cr);
429 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
430 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500431 cairo_image_surface_get_stride(output->gl.border.right) / 4,
432 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500433
434
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500435 if (!output->gl.border.bottom)
436 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500437 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
438 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500439 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500440 cairo_translate(cr, 0, -(iy + iheight));
441 frame_repaint(output->frame, cr);
442 cairo_destroy(cr);
443 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
444 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
446 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500447}
448
449static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200450wayland_output_start_repaint_loop(struct weston_output *output_base)
451{
Armin Krezović938dc522016-08-01 19:17:57 +0200452 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300453 struct wayland_backend *wb =
Armin Krezović938dc522016-08-01 19:17:57 +0200454 to_wayland_backend(output->base.compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200455 struct wl_callback *callback;
456
457 /* If this is the initial frame, we need to attach a buffer so that
458 * the compositor can map the surface and include it in its render
459 * loop. If the surface doesn't end up in the render loop, the frame
460 * callback won't be invoked. The buffer is transparent and of the
461 * same size as the future real output buffer. */
462 if (output->parent.draw_initial_frame) {
463 output->parent.draw_initial_frame = 0;
464
465 draw_initial_frame(output);
466 }
467
468 callback = wl_surface_frame(output->parent.surface);
469 wl_callback_add_listener(callback, &frame_listener, output);
470 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300471 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200472}
473
David Herrmann1edf44c2013-10-22 17:11:26 +0200474static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500475wayland_output_repaint_gl(struct weston_output *output_base,
476 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400477{
Armin Krezović938dc522016-08-01 19:17:57 +0200478 struct wayland_output *output = to_wayland_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400479 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400480 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600481
Kristian Høgsberg33418202011-08-16 23:01:28 -0400482 callback = wl_surface_frame(output->parent.surface);
483 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100484
Jason Ekstrand7744f712013-10-27 22:24:55 -0500485 wayland_output_update_gl_border(output);
486
Pekka Paalanenbc106382012-10-10 12:49:31 +0300487 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200488
489 pixman_region32_subtract(&ec->primary_plane.damage,
490 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200491 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100492}
493
Matt Roper361d2ad2011-08-29 13:52:23 -0700494static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500495wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
496{
497 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
498 cairo_t *cr;
499
500 if (!buffer->output->frame || !buffer->frame_damaged)
501 return;
502
503 cr = cairo_create(buffer->c_surface);
504
505 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
506 fwidth = frame_width(buffer->output->frame);
507 fheight = frame_height(buffer->output->frame);
508
509 /* Set the clip so we don't unnecisaraly damage the surface */
510 cairo_move_to(cr, ix, iy);
511 cairo_rel_line_to(cr, iwidth, 0);
512 cairo_rel_line_to(cr, 0, iheight);
513 cairo_rel_line_to(cr, -iwidth, 0);
514 cairo_line_to(cr, ix, iy);
515 cairo_line_to(cr, 0, iy);
516 cairo_line_to(cr, 0, fheight);
517 cairo_line_to(cr, fwidth, fheight);
518 cairo_line_to(cr, fwidth, 0);
519 cairo_line_to(cr, 0, 0);
520 cairo_line_to(cr, 0, iy);
521 cairo_close_path(cr);
522 cairo_clip(cr);
523
524 /* Draw using a pattern so that the final result gets clipped */
525 cairo_push_group(cr);
526 frame_repaint(buffer->output->frame, cr);
527 cairo_pop_group_to_source(cr);
528 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
529 cairo_paint(cr);
530
531 cairo_destroy(cr);
532}
533
534static void
535wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
536{
537 pixman_region32_t damage;
538 pixman_box32_t *rects;
539 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
540 int i, n;
541
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500542 pixman_region32_init(&damage);
543 weston_transformed_region(sb->output->base.width,
544 sb->output->base.height,
545 sb->output->base.transform,
546 sb->output->base.current_scale,
547 &sb->damage, &damage);
548
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500549 if (sb->output->frame) {
550 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
551 fwidth = frame_width(sb->output->frame);
552 fheight = frame_height(sb->output->frame);
553
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500554 pixman_region32_translate(&damage, ix, iy);
555
556 if (sb->frame_damaged) {
557 pixman_region32_union_rect(&damage, &damage,
558 0, 0, fwidth, iy);
559 pixman_region32_union_rect(&damage, &damage,
560 0, iy, ix, iheight);
561 pixman_region32_union_rect(&damage, &damage,
562 ix + iwidth, iy,
563 fwidth - (ix + iwidth), iheight);
564 pixman_region32_union_rect(&damage, &damage,
565 0, iy + iheight,
566 fwidth, fheight - (iy + iheight));
567 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500568 }
569
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500570 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500571 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
572 for (i = 0; i < n; ++i)
573 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
574 rects[i].y1, rects[i].x2 - rects[i].x1,
575 rects[i].y2 - rects[i].y1);
576
577 if (sb->output->frame)
578 pixman_region32_fini(&damage);
579}
580
581static int
582wayland_output_repaint_pixman(struct weston_output *output_base,
583 pixman_region32_t *damage)
584{
Armin Krezović938dc522016-08-01 19:17:57 +0200585 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300586 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200587 to_wayland_backend(output->base.compositor);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500588 struct wl_callback *callback;
589 struct wayland_shm_buffer *sb;
590
591 if (output->frame) {
592 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
593 wl_list_for_each(sb, &output->shm.buffers, link)
594 sb->frame_damaged = 1;
595 }
596
597 wl_list_for_each(sb, &output->shm.buffers, link)
598 pixman_region32_union(&sb->damage, &sb->damage, damage);
599
600 sb = wayland_output_get_shm_buffer(output);
601
602 wayland_output_update_shm_border(sb);
603 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300604 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500605
606 wayland_shm_buffer_attach(sb);
607
608 callback = wl_surface_frame(output->parent.surface);
609 wl_callback_add_listener(callback, &frame_listener, output);
610 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500612
613 pixman_region32_fini(&sb->damage);
614 pixman_region32_init(&sb->damage);
615 sb->frame_damaged = 0;
616
Giulio Camuffo954f1832014-10-11 18:27:30 +0300617 pixman_region32_subtract(&b->compositor->primary_plane.damage,
618 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500619 return 0;
620}
621
622static void
Armin Krezović174448a2016-09-30 14:11:09 +0200623wayland_backend_destroy_output_surface(struct wayland_output *output)
Matt Roper361d2ad2011-08-29 13:52:23 -0700624{
Armin Krezović174448a2016-09-30 14:11:09 +0200625 if (output->parent.shell_surface)
626 wl_shell_surface_destroy(output->parent.shell_surface);
627
628 wl_surface_destroy(output->parent.surface);
629}
630
631static int
632wayland_output_disable(struct weston_output *base)
633{
634 struct wayland_output *output = to_wayland_output(base);
635 struct wayland_backend *b = to_wayland_backend(base->compositor);
636
637 if (!output->base.enabled)
638 return 0;
Matt Roper361d2ad2011-08-29 13:52:23 -0700639
Giulio Camuffo954f1832014-10-11 18:27:30 +0300640 if (b->use_pixman) {
Armin Krezović174448a2016-09-30 14:11:09 +0200641 pixman_renderer_output_destroy(&output->base);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500642 } else {
Armin Krezović174448a2016-09-30 14:11:09 +0200643 gl_renderer->output_destroy(&output->base);
Emmanuel Gil Peyrot85571a32016-09-01 15:19:46 +0100644 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500645 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100646
Armin Krezović174448a2016-09-30 14:11:09 +0200647 /* Done on output->enable when not fullscreen, otherwise
648 * done in output_create, to get the proper mode */
649 if (!b->fullscreen)
650 wayland_backend_destroy_output_surface(output);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500651
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600652 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500653 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600654
655 cairo_surface_destroy(output->gl.border.top);
656 cairo_surface_destroy(output->gl.border.left);
657 cairo_surface_destroy(output->gl.border.right);
658 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500659
Armin Krezović174448a2016-09-30 14:11:09 +0200660 return 0;
661}
Matt Roper361d2ad2011-08-29 13:52:23 -0700662
Armin Krezović174448a2016-09-30 14:11:09 +0200663static void
664wayland_output_destroy(struct weston_output *base)
665{
666 struct wayland_output *output = to_wayland_output(base);
667 struct wayland_backend *b = to_wayland_backend(base->compositor);
668
669 wayland_output_disable(&output->base);
670
671 if (b->fullscreen)
672 wayland_backend_destroy_output_surface(output);
673
674 weston_output_destroy(&output->base);
675
676 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700677}
678
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300679static const struct wl_shell_surface_listener shell_surface_listener;
680
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200681static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500682wayland_output_init_gl_renderer(struct wayland_output *output)
683{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500684 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500685
686 if (output->frame) {
687 fwidth = frame_width(output->frame);
688 fheight = frame_height(output->frame);
689 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500690 fwidth = output->base.current_mode->width;
691 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500692 }
693
694 output->gl.egl_window =
695 wl_egl_window_create(output->parent.surface,
696 fwidth, fheight);
697 if (!output->gl.egl_window) {
698 weston_log("failure to create wl_egl_window\n");
699 return -1;
700 }
701
702 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000703 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100704 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000705 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500706 NULL,
707 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500708 goto cleanup_window;
709
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500710 return 0;
711
712cleanup_window:
713 wl_egl_window_destroy(output->gl.egl_window);
714 return -1;
715}
716
717static int
718wayland_output_init_pixman_renderer(struct wayland_output *output)
719{
720 return pixman_renderer_output_create(&output->base);
721}
722
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600723static void
724wayland_output_resize_surface(struct wayland_output *output)
725{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300726 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200727 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600728 struct wayland_shm_buffer *buffer, *next;
729 int32_t ix, iy, iwidth, iheight;
730 int32_t width, height;
731 struct wl_region *region;
732
733 width = output->base.current_mode->width;
734 height = output->base.current_mode->height;
735
736 if (output->frame) {
737 frame_resize_inside(output->frame, width, height);
738
739 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300740 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600741 wl_region_add(region, ix, iy, iwidth, iheight);
742 wl_surface_set_input_region(output->parent.surface, region);
743 wl_region_destroy(region);
744
745 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300746 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600747 wl_region_add(region, ix, iy, iwidth, iheight);
748 wl_surface_set_opaque_region(output->parent.surface, region);
749 wl_region_destroy(region);
750
751 width = frame_width(output->frame);
752 height = frame_height(output->frame);
753 } else {
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, 0, 0, width, height);
756 wl_surface_set_input_region(output->parent.surface, region);
757 wl_region_destroy(region);
758
Giulio Camuffo954f1832014-10-11 18:27:30 +0300759 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600760 wl_region_add(region, 0, 0, width, height);
761 wl_surface_set_opaque_region(output->parent.surface, region);
762 wl_region_destroy(region);
763 }
764
765 if (output->gl.egl_window) {
766 wl_egl_window_resize(output->gl.egl_window,
767 width, height, 0, 0);
768
769 /* These will need to be re-created due to the resize */
770 gl_renderer->output_set_border(&output->base,
771 GL_RENDERER_BORDER_TOP,
772 0, 0, 0, NULL);
773 cairo_surface_destroy(output->gl.border.top);
774 output->gl.border.top = NULL;
775 gl_renderer->output_set_border(&output->base,
776 GL_RENDERER_BORDER_LEFT,
777 0, 0, 0, NULL);
778 cairo_surface_destroy(output->gl.border.left);
779 output->gl.border.left = NULL;
780 gl_renderer->output_set_border(&output->base,
781 GL_RENDERER_BORDER_RIGHT,
782 0, 0, 0, NULL);
783 cairo_surface_destroy(output->gl.border.right);
784 output->gl.border.right = NULL;
785 gl_renderer->output_set_border(&output->base,
786 GL_RENDERER_BORDER_BOTTOM,
787 0, 0, 0, NULL);
788 cairo_surface_destroy(output->gl.border.bottom);
789 output->gl.border.bottom = NULL;
790 }
791
792 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500793 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600794 wayland_shm_buffer_destroy(buffer);
795 /* These will get thrown away when they get released */
796 wl_list_for_each(buffer, &output->shm.buffers, link)
797 buffer->output = NULL;
798}
799
800static int
801wayland_output_set_windowed(struct wayland_output *output)
802{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300803 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200804 to_wayland_backend(output->base.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600805 int tlen;
806 char *title;
807
808 if (output->frame)
809 return 0;
810
811 if (output->name) {
812 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
813 title = malloc(tlen + 1);
814 if (!title)
815 return -1;
816
817 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
818 } else {
819 title = strdup(WINDOW_TITLE);
820 }
821
Giulio Camuffo954f1832014-10-11 18:27:30 +0300822 if (!b->theme) {
823 b->theme = theme_create();
824 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600825 free(title);
826 return -1;
827 }
828 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300829 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600830 FRAME_BUTTON_CLOSE, title);
831 free(title);
832 if (!output->frame)
833 return -1;
834
835 if (output->keyboard_count)
836 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
837
838 wayland_output_resize_surface(output);
839
840 wl_shell_surface_set_toplevel(output->parent.shell_surface);
841
842 return 0;
843}
844
845static void
846wayland_output_set_fullscreen(struct wayland_output *output,
847 enum wl_shell_surface_fullscreen_method method,
848 uint32_t framerate, struct wl_output *target)
849{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300850 struct wayland_backend *b =
Armin Krezović938dc522016-08-01 19:17:57 +0200851 to_wayland_backend(output->base.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500852
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600853 if (output->frame) {
854 frame_destroy(output->frame);
855 output->frame = NULL;
856 }
857
858 wayland_output_resize_surface(output);
859
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500860 if (output->parent.shell_surface) {
861 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
862 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300863 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800864 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
865 output->parent.surface,
866 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500867 }
868}
869
870static struct weston_mode *
871wayland_output_choose_mode(struct wayland_output *output,
872 struct weston_mode *ref_mode)
873{
874 struct weston_mode *mode;
875
876 /* First look for an exact match */
877 wl_list_for_each(mode, &output->base.mode_list, link)
878 if (mode->width == ref_mode->width &&
879 mode->height == ref_mode->height &&
880 mode->refresh == ref_mode->refresh)
881 return mode;
882
883 /* If we can't find an exact match, ignore refresh and try again */
884 wl_list_for_each(mode, &output->base.mode_list, link)
885 if (mode->width == ref_mode->width &&
886 mode->height == ref_mode->height)
887 return mode;
888
889 /* Yeah, we failed */
890 return NULL;
891}
892
893enum mode_status {
894 MODE_STATUS_UNKNOWN,
895 MODE_STATUS_SUCCESS,
896 MODE_STATUS_FAIL,
897 MODE_STATUS_CANCEL,
898};
899
900static void
901mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800902 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500903{
904 enum mode_status *value = data;
905
906 printf("Mode switch successful\n");
907
908 *value = MODE_STATUS_SUCCESS;
909}
910
911static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800912mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500913{
914 enum mode_status *value = data;
915
916 printf("Mode switch failed\n");
917
918 *value = MODE_STATUS_FAIL;
919}
920
921static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800922mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500923{
924 enum mode_status *value = data;
925
926 printf("Mode switch cancelled\n");
927
928 *value = MODE_STATUS_CANCEL;
929}
930
Jonas Ådahl496adb32015-11-17 16:00:27 +0800931struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500932 mode_feedback_successful,
933 mode_feedback_failed,
934 mode_feedback_cancelled,
935};
936
937static int
938wayland_output_switch_mode(struct weston_output *output_base,
939 struct weston_mode *mode)
940{
Armin Krezović938dc522016-08-01 19:17:57 +0200941 struct wayland_output *output = to_wayland_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300942 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500943 struct wl_surface *old_surface;
944 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800945 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500946 enum mode_status mode_status;
947 int ret = 0;
948
949 if (output_base == NULL) {
950 weston_log("output is NULL.\n");
951 return -1;
952 }
953
954 if (mode == NULL) {
955 weston_log("mode is NULL.\n");
956 return -1;
957 }
958
Armin Krezović938dc522016-08-01 19:17:57 +0200959 b = to_wayland_backend(output_base->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500960
Giulio Camuffo954f1832014-10-11 18:27:30 +0300961 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500962 return -1;
963
964 mode = wayland_output_choose_mode(output, mode);
965 if (mode == NULL)
966 return -1;
967
968 if (output->base.current_mode == mode)
969 return 0;
970
971 old_mode = output->base.current_mode;
972 old_surface = output->parent.surface;
973 output->base.current_mode = mode;
974 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300975 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500976 wl_surface_set_user_data(output->parent.surface, output);
977
978 /* Blow the old buffers because we changed size/surfaces */
979 wayland_output_resize_surface(output);
980
981 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800982 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
983 output->parent.surface,
984 output->parent.output,
985 mode->refresh);
986 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
987 &mode_feedback_listener,
988 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500989
990 /* This should kick-start things again */
991 output->parent.draw_initial_frame = 1;
992 wayland_output_start_repaint_loop(&output->base);
993
994 mode_status = MODE_STATUS_UNKNOWN;
995 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300996 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500997
Jonas Ådahl496adb32015-11-17 16:00:27 +0800998 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500999
1000 if (mode_status == MODE_STATUS_FAIL) {
1001 output->base.current_mode = old_mode;
1002 wl_surface_destroy(output->parent.surface);
1003 output->parent.surface = old_surface;
1004 wayland_output_resize_surface(output);
1005
1006 return -1;
1007 }
1008
1009 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
1010 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1011
Giulio Camuffo954f1832014-10-11 18:27:30 +03001012 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001013 pixman_renderer_output_destroy(output_base);
1014 if (wayland_output_init_pixman_renderer(output) < 0)
1015 goto err_output;
1016 } else {
1017 gl_renderer->output_destroy(output_base);
1018 wl_egl_window_destroy(output->gl.egl_window);
1019 if (wayland_output_init_gl_renderer(output) < 0)
1020 goto err_output;
1021 }
1022 wl_surface_destroy(old_surface);
1023
1024 weston_output_schedule_repaint(&output->base);
1025
1026 return 0;
1027
1028err_output:
1029 /* XXX */
1030 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001031}
1032
Armin Krezović174448a2016-09-30 14:11:09 +02001033static int
1034wayland_backend_create_output_surface(struct wayland_output *output)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001035{
Armin Krezović174448a2016-09-30 14:11:09 +02001036 struct wayland_backend *b = to_wayland_backend(output->base.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001037
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001038 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001039 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001040 if (!output->parent.surface)
Armin Krezović174448a2016-09-30 14:11:09 +02001041 return -1;
1042
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001043 wl_surface_set_user_data(output->parent.surface, output);
1044
1045 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001046
Giulio Camuffo954f1832014-10-11 18:27:30 +03001047 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001048 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001049 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001050 output->parent.surface);
Armin Krezović174448a2016-09-30 14:11:09 +02001051 if (!output->parent.shell_surface) {
1052 wl_surface_destroy(output->parent.surface);
1053 return -1;
1054 }
1055
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001056 wl_shell_surface_add_listener(output->parent.shell_surface,
1057 &shell_surface_listener, output);
1058 }
1059
Armin Krezović174448a2016-09-30 14:11:09 +02001060 return 0;
1061}
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001062
Armin Krezović174448a2016-09-30 14:11:09 +02001063static int
1064wayland_output_enable(struct weston_output *base)
1065{
1066 struct wayland_output *output = to_wayland_output(base);
1067 struct wayland_backend *b = to_wayland_backend(base->compositor);
1068 int ret = 0;
1069
1070
1071 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1072 output->base.current_mode->width,
1073 output->base.current_mode->height,
1074 output->base.x, output->base.y);
1075
1076 /* If fullscreen was specified, this needs to be done before
1077 * enable to get the proper mode */
1078 if (!b->fullscreen)
1079 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;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001090 } else {
1091 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001092 goto err_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001093 }
1094
Armin Krezović174448a2016-09-30 14:11:09 +02001095 if (b->sprawl_across_outputs) {
1096 wayland_output_set_fullscreen(output,
1097 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1098 output->mode.refresh, output->parent.output);
1099
1100 if (output->parent.shell_surface) {
1101 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1102 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1103 output->mode.refresh, output->parent.output);
1104 } else if (b->parent.fshell) {
1105 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1106 output->parent.surface,
1107 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1108 output->parent.output);
1109 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1110 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1111 output->parent.surface,
1112 output->parent.output,
1113 output->mode.refresh));
1114 }
1115 } else if (b->fullscreen) {
1116 wayland_output_set_fullscreen(output, 0, 0, NULL);
1117 } else {
1118 wayland_output_set_windowed(output);
1119 }
1120
1121 return 0;
1122
1123err_output:
1124 if (!b->fullscreen)
1125 wayland_backend_destroy_output_surface(output);
1126
1127 return -1;
1128}
1129
1130static struct wayland_output *
1131wayland_output_create_common(void)
1132{
1133 struct wayland_output *output;
1134
1135 output = zalloc(sizeof *output);
1136 if (output == NULL) {
1137 perror("zalloc");
1138 return NULL;
1139 }
1140
Matt Roper361d2ad2011-08-29 13:52:23 -07001141 output->base.destroy = wayland_output_destroy;
Armin Krezović174448a2016-09-30 14:11:09 +02001142 output->base.disable = wayland_output_disable;
1143 output->base.enable = wayland_output_enable;
1144
1145 return output;
1146}
1147
1148static int
1149wayland_output_create(struct weston_compositor *compositor, const char *name)
1150{
1151 struct wayland_output *output = wayland_output_create_common();
1152
1153 /* name can't be NULL. */
1154 assert(name);
1155
1156 output->base.name = strdup(name);
1157
1158 weston_output_init_pending(&output->base, compositor);
1159 weston_compositor_add_pending_output(&output->base, compositor);
1160
1161 return 0;
1162}
1163
1164static int
1165wayland_output_set_size(struct weston_output *base, int width, int height)
1166{
1167 struct wayland_output *output = to_wayland_output(base);
1168 struct wayland_backend *b = to_wayland_backend(base->compositor);
1169 int output_width, output_height;
1170
1171 /* We can only be called once. */
1172 assert(!output->base.current_mode);
1173
1174 /* Make sure we have scale set. */
1175 assert(output->base.scale);
1176
1177 if (width < 1) {
1178 weston_log("Invalid width \"%d\" for output %s\n",
1179 width, output->base.name);
1180 return -1;
1181 }
1182
1183 if (height < 1) {
1184 weston_log("Invalid height \"%d\" for output %s\n",
1185 height, output->base.name);
1186 return -1;
1187 }
1188
1189 output_width = width * output->base.scale;
1190 output_height = height * output->base.scale;
1191
1192 output->mode.flags =
1193 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1194
1195 output->mode.width = output_width;
1196 output->mode.height = output_height;
1197 output->mode.refresh = 60000;
1198 output->scale = output->base.scale;
1199 wl_list_init(&output->base.mode_list);
1200 wl_list_insert(&output->base.mode_list, &output->mode.link);
1201
1202 output->base.current_mode = &output->mode;
1203 output->base.make = "wayland";
1204 output->base.model = "none";
1205
1206 /* XXX: Calculate proper size. */
1207 output->base.mm_width = width;
1208 output->base.mm_height = height;
1209
1210 if (b->use_pixman)
1211 output->base.repaint = wayland_output_repaint_pixman;
1212 else
1213 output->base.repaint = wayland_output_repaint_gl;
1214
1215 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001216 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001217 output->base.set_backlight = NULL;
1218 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001219 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001220
Armin Krezović174448a2016-09-30 14:11:09 +02001221 return 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001222}
1223
Armin Krezović174448a2016-09-30 14:11:09 +02001224static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001225wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001226 struct wayland_parent_output *poutput)
1227{
Armin Krezović174448a2016-09-30 14:11:09 +02001228 struct wayland_output *output = wayland_output_create_common();
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001229 struct weston_mode *mode;
Armin Krezović174448a2016-09-30 14:11:09 +02001230
1231 output->base.name = strdup("wlparent");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001232
1233 if (poutput->current_mode) {
1234 mode = poutput->current_mode;
1235 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001236 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001237 } else if (!wl_list_empty(&poutput->mode_list)) {
1238 mode = container_of(poutput->mode_list.next,
1239 struct weston_mode, link);
1240 } else {
Armin Krezović174448a2016-09-30 14:11:09 +02001241 weston_log("No valid modes found. Skipping output.\n");
1242 goto out;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001243 }
1244
Armin Krezović174448a2016-09-30 14:11:09 +02001245 weston_output_init_pending(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001246
Armin Krezović174448a2016-09-30 14:11:09 +02001247 output->base.scale = 1;
1248 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001249
Armin Krezović174448a2016-09-30 14:11:09 +02001250 if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0)
1251 goto out;
1252
1253 output->mode = *mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001254 output->parent.output = poutput->global;
1255
1256 output->base.make = poutput->physical.make;
1257 output->base.model = poutput->physical.model;
Armin Krezović174448a2016-09-30 14:11:09 +02001258
1259 /* XXX: Clear previously added values */
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001260 wl_list_init(&output->base.mode_list);
1261 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1262 wl_list_init(&poutput->mode_list);
1263
Armin Krezović174448a2016-09-30 14:11:09 +02001264 weston_compositor_add_pending_output(&output->base, b->compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001265
Armin Krezović174448a2016-09-30 14:11:09 +02001266 return 0;
1267
1268out:
1269 free(output->name);
1270 free(output);
1271
1272 return -1;
1273}
1274
1275static int
1276wayland_output_create_fullscreen(struct wayland_backend *b)
1277{
1278 struct wayland_output *output = wayland_output_create_common();
1279 int width = 0, height = 0;
1280
1281 output->base.name = strdup("wayland-fullscreen");
1282
1283 weston_output_init_pending(&output->base, b->compositor);
1284
1285 output->base.scale = 1;
1286 output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1287
1288 if (wayland_backend_create_output_surface(output) < 0)
1289 goto err_surface;
1290
1291 /* What should size be set if conditional is false? */
1292 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001293 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
Armin Krezović174448a2016-09-30 14:11:09 +02001294 0, 0, NULL);
1295 wl_display_roundtrip(b->parent.wl_display);
1296
1297 width = output->parent.configure_width;
1298 height = output->parent.configure_height;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001299 }
1300
Armin Krezović174448a2016-09-30 14:11:09 +02001301 if (wayland_output_set_size(&output->base, width, height) < 0)
1302 goto err_set_size;
1303
1304 weston_compositor_add_pending_output(&output->base, b->compositor);
1305
1306 return 0;
1307
1308err_set_size:
1309 wayland_backend_destroy_output_surface(output);
1310err_surface:
1311 free(output->name);
1312 free(output);
1313
1314 return -1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001315}
1316
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001317static void
1318shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1319 uint32_t serial)
1320{
1321 wl_shell_surface_pong(shell_surface, serial);
1322}
1323
1324static void
1325shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1326 uint32_t edges, int32_t width, int32_t height)
1327{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001328 struct wayland_output *output = data;
1329
1330 output->parent.configure_width = width;
1331 output->parent.configure_height = height;
1332
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001333 /* FIXME: implement resizing */
1334}
1335
1336static void
1337shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1338{
1339}
1340
1341static const struct wl_shell_surface_listener shell_surface_listener = {
1342 shell_surface_ping,
1343 shell_surface_configure,
1344 shell_surface_popup_done
1345};
1346
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001347/* Events received from the wayland-server this compositor is client of: */
1348
Jason Ekstrand7744f712013-10-27 22:24:55 -05001349/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001350static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001351input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001352{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001353
Jason Ekstrand7744f712013-10-27 22:24:55 -05001354 struct wl_buffer *buffer;
1355 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001356
Giulio Camuffo954f1832014-10-11 18:27:30 +03001357 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001358 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001359
Giulio Camuffo954f1832014-10-11 18:27:30 +03001360 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001361 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001362 if (!buffer)
1363 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001364
1365 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1366 input->parent.cursor.surface,
1367 image->hotspot_x, image->hotspot_y);
1368
1369 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1370 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1371 image->width, image->height);
1372 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001373}
1374
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001375static void
Daniel Stone37816df2012-05-16 18:45:18 +01001376input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1377 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001378 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001379{
1380 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001381 int32_t fx, fy;
1382 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001383 double x, y;
1384
1385 x = wl_fixed_to_double(fixed_x);
1386 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001387
Daniel Stone50692802012-06-22 13:21:41 +01001388 /* XXX: If we get a modifier event immediately before the focus,
1389 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001390 input->enter_serial = serial;
1391 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001392
1393 if (input->output->frame) {
1394 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001395 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001396 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001397 x -= fx;
1398 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001399
1400 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1401 weston_output_schedule_repaint(&input->output->base);
1402 } else {
1403 location = THEME_LOCATION_CLIENT_AREA;
1404 }
1405
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001406 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001407
1408 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001409 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001410 notify_pointer_focus(&input->base, &input->output->base, x, y);
1411 wl_pointer_set_cursor(input->parent.pointer,
1412 input->enter_serial, NULL, 0, 0);
1413 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001414 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001415 notify_pointer_focus(&input->base, NULL, 0, 0);
1416 input_set_cursor(input);
1417 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001418}
1419
1420static void
Daniel Stone37816df2012-05-16 18:45:18 +01001421input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1422 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001423{
1424 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001425
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001426 if (!input->output)
1427 return;
1428
Jason Ekstrand7744f712013-10-27 22:24:55 -05001429 if (input->output->frame) {
1430 frame_pointer_leave(input->output->frame, input);
1431
1432 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1433 weston_output_schedule_repaint(&input->output->base);
1434 }
1435
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001436 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001437 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001438 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001439}
1440
1441static void
Daniel Stone37816df2012-05-16 18:45:18 +01001442input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001443 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001444{
1445 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001446 int32_t fx, fy;
1447 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001448 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001449 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001450
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001451 if (!input->output)
1452 return;
1453
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001454 x = wl_fixed_to_double(fixed_x);
1455 y = wl_fixed_to_double(fixed_y);
1456
Jason Ekstrand7744f712013-10-27 22:24:55 -05001457 if (input->output->frame) {
1458 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001459 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001460 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001461 x -= fx;
1462 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001463
1464 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1465 weston_output_schedule_repaint(&input->output->base);
1466 } else {
1467 location = THEME_LOCATION_CLIENT_AREA;
1468 }
1469
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001470 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001471
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001472 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001473 input_set_cursor(input);
1474 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001475 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001476 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001477 } else if (!input->has_focus &&
1478 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001479 wl_pointer_set_cursor(input->parent.pointer,
1480 input->enter_serial, NULL, 0, 0);
1481 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001482 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001483 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001484 }
1485
Peter Hutterer87743e92016-01-18 16:38:22 +10001486 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001487 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001488 want_frame = true;
1489 }
1490
1491 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1492 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001493}
1494
1495static void
1496input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001497 uint32_t serial, uint32_t time, uint32_t button,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001498 enum wl_pointer_button_state state)
Daniel Stone37816df2012-05-16 18:45:18 +01001499{
1500 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001501 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001502
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001503 if (!input->output)
1504 return;
1505
Jason Ekstrand7744f712013-10-27 22:24:55 -05001506 if (input->output->frame) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001507 location = frame_pointer_button(input->output->frame, input,
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001508 button, state);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001509
1510 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1511
1512 wl_shell_surface_move(input->output->parent.shell_surface,
1513 input->parent.seat, serial);
1514 frame_status_clear(input->output->frame,
1515 FRAME_STATUS_MOVE);
1516 return;
1517 }
1518
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001519 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1520 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001521 input->output = NULL;
1522 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001523
Giulio Camuffo954f1832014-10-11 18:27:30 +03001524 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001525 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001526
1527 return;
1528 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001529
1530 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1531 weston_output_schedule_repaint(&input->output->base);
1532 } else {
1533 location = THEME_LOCATION_CLIENT_AREA;
1534 }
1535
Peter Hutterer87743e92016-01-18 16:38:22 +10001536 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001537 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001538 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1539 notify_pointer_frame(&input->base);
1540 }
Daniel Stone37816df2012-05-16 18:45:18 +01001541}
1542
1543static void
1544input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001545 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001546{
1547 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001548 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001549
Peter Hutterer89b6a492016-01-18 15:58:17 +10001550 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001551 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001552
Peter Hutterer87743e92016-01-18 16:38:22 +10001553 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1554 input->vert.has_discrete) {
1555 weston_event.has_discrete = true;
1556 weston_event.discrete = input->vert.discrete;
1557 input->vert.has_discrete = false;
1558 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1559 input->horiz.has_discrete) {
1560 weston_event.has_discrete = true;
1561 weston_event.discrete = input->horiz.discrete;
1562 input->horiz.has_discrete = false;
1563 }
1564
Peter Hutterer89b6a492016-01-18 15:58:17 +10001565 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001566
1567 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1568 notify_pointer_frame(&input->base);
1569}
1570
1571static void
1572input_handle_frame(void *data, struct wl_pointer *pointer)
1573{
1574 struct wayland_input *input = data;
1575
1576 notify_pointer_frame(&input->base);
1577}
1578
1579static void
1580input_handle_axis_source(void *data, struct wl_pointer *pointer,
1581 uint32_t source)
1582{
1583 struct wayland_input *input = data;
1584
1585 notify_axis_source(&input->base, source);
1586}
1587
1588static void
1589input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1590 uint32_t time, uint32_t axis)
1591{
1592 struct wayland_input *input = data;
1593 struct weston_pointer_axis_event weston_event;
1594
1595 weston_event.axis = axis;
1596 weston_event.value = 0;
1597
1598 notify_axis(&input->base, time, &weston_event);
1599}
1600
1601static void
1602input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1603 uint32_t axis, int32_t discrete)
1604{
1605 struct wayland_input *input = data;
1606
1607 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1608 input->vert.has_discrete = true;
1609 input->vert.discrete = discrete;
1610 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1611 input->horiz.has_discrete = true;
1612 input->horiz.discrete = discrete;
1613 }
Daniel Stone37816df2012-05-16 18:45:18 +01001614}
1615
1616static const struct wl_pointer_listener pointer_listener = {
1617 input_handle_pointer_enter,
1618 input_handle_pointer_leave,
1619 input_handle_motion,
1620 input_handle_button,
1621 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001622 input_handle_frame,
1623 input_handle_axis_source,
1624 input_handle_axis_stop,
1625 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001626};
1627
1628static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001629input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1630 int fd, uint32_t size)
1631{
1632 struct wayland_input *input = data;
1633 struct xkb_keymap *keymap;
1634 char *map_str;
1635
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001636 if (!data) {
1637 close(fd);
1638 return;
1639 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001640
1641 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1642 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1643 if (map_str == MAP_FAILED) {
1644 weston_log("mmap failed: %m\n");
1645 goto error;
1646 }
1647
Giulio Camuffo954f1832014-10-11 18:27:30 +03001648 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001649 map_str,
1650 XKB_KEYMAP_FORMAT_TEXT_V1,
1651 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001652 munmap(map_str, size);
1653
1654 if (!keymap) {
1655 weston_log("failed to compile keymap\n");
1656 goto error;
1657 }
1658
1659 input->keyboard_state_update = STATE_UPDATE_NONE;
1660 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1661 weston_log("No keymap provided; falling back to defalt\n");
1662 keymap = NULL;
1663 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1664 } else {
1665 weston_log("Invalid keymap\n");
1666 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001667 }
1668
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001669 close(fd);
1670
Derek Foreman1281a362015-07-31 16:55:32 -05001671 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001672 weston_seat_update_keymap(&input->base, keymap);
1673 else
1674 weston_seat_init_keyboard(&input->base, keymap);
1675
Ran Benitac9c74152014-08-19 23:59:52 +03001676 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001677
1678 return;
1679
1680error:
1681 wl_keyboard_release(input->parent.keyboard);
1682 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001683}
1684
1685static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001686input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001687 struct wl_keyboard *keyboard,
1688 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001689 struct wl_surface *surface,
1690 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001691{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001692 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001693 struct wayland_output *focus;
1694
1695 focus = input->keyboard_focus;
1696 if (focus) {
1697 /* This shouldn't happen */
1698 focus->keyboard_count--;
1699 if (!focus->keyboard_count && focus->frame)
1700 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1701 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1702 weston_output_schedule_repaint(&focus->base);
1703 }
1704
1705 input->keyboard_focus = wl_surface_get_user_data(surface);
1706 input->keyboard_focus->keyboard_count++;
1707
1708 focus = input->keyboard_focus;
1709 if (focus->frame) {
1710 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1711 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1712 weston_output_schedule_repaint(&focus->base);
1713 }
1714
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001715
Daniel Stone50692802012-06-22 13:21:41 +01001716 /* XXX: If we get a modifier event immediately before the focus,
1717 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001718 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001719 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001720}
1721
1722static void
1723input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001724 struct wl_keyboard *keyboard,
1725 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001726 struct wl_surface *surface)
1727{
1728 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001729 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001730
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001731 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001732
1733 focus = input->keyboard_focus;
1734 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001735 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001736
1737 focus->keyboard_count--;
1738 if (!focus->keyboard_count && focus->frame) {
1739 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1740 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1741 weston_output_schedule_repaint(&focus->base);
1742 }
1743
1744 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001745}
1746
Daniel Stone37816df2012-05-16 18:45:18 +01001747static void
1748input_handle_key(void *data, struct wl_keyboard *keyboard,
1749 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1750{
1751 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001752
Daniel Stone50692802012-06-22 13:21:41 +01001753 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001754 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001755 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001756 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001757 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001758}
1759
Daniel Stone351eb612012-05-31 15:27:47 -04001760static void
Derek Foreman1281a362015-07-31 16:55:32 -05001761input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001762 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001763 uint32_t mods_latched, uint32_t mods_locked,
1764 uint32_t group)
1765{
Derek Foreman1281a362015-07-31 16:55:32 -05001766 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001767 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001768 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001769 uint32_t serial_out;
1770
1771 /* If we get a key event followed by a modifier event with the
1772 * same serial number, then we try to preserve those semantics by
1773 * reusing the same serial number on the way out too. */
1774 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001775 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001776 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001777 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001778
Derek Foreman1281a362015-07-31 16:55:32 -05001779 keyboard = weston_seat_get_keyboard(&input->base);
1780 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001781 mods_depressed, mods_latched,
1782 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001783 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001784}
1785
Jonny Lamb497994a2014-08-12 14:58:26 +02001786static void
1787input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1788 int32_t rate, int32_t delay)
1789{
1790 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001791 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001792
Giulio Camuffo954f1832014-10-11 18:27:30 +03001793 b->compositor->kb_repeat_rate = rate;
1794 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001795}
1796
Daniel Stone37816df2012-05-16 18:45:18 +01001797static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001798 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001799 input_handle_keyboard_enter,
1800 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001801 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001802 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001803 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001804};
1805
1806static void
Derek Foreman748c6952015-11-06 15:56:10 -06001807input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1808 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001809 struct wl_surface *surface, int32_t id,
1810 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001811{
1812 struct wayland_input *input = data;
1813 struct wayland_output *output;
1814 enum theme_location location;
1815 bool first_touch;
1816 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001817 double x, y;
1818
1819 x = wl_fixed_to_double(fixed_x);
1820 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001821
1822 first_touch = (input->touch_points == 0);
1823 input->touch_points++;
1824
1825 input->touch_focus = wl_surface_get_user_data(surface);
1826 output = input->touch_focus;
1827 if (!first_touch && !input->touch_active)
1828 return;
1829
1830 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001831 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001832
1833 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001834 x -= fx;
1835 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001836
1837 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1838 weston_output_schedule_repaint(&output->base);
1839
1840 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1841 input->touch_points--;
1842 wl_shell_surface_move(output->parent.shell_surface,
1843 input->parent.seat, serial);
1844 frame_status_clear(output->frame,
1845 FRAME_STATUS_MOVE);
1846 return;
1847 }
1848
1849 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1850 return;
1851 }
1852
1853 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1854
1855 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1856 input->touch_active = true;
1857}
1858
1859static void
1860input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1861 uint32_t serial, uint32_t time, int32_t id)
1862{
1863 struct wayland_input *input = data;
1864 struct wayland_output *output = input->touch_focus;
1865 bool active = input->touch_active;
1866
1867 input->touch_points--;
1868 if (input->touch_points == 0) {
1869 input->touch_focus = NULL;
1870 input->touch_active = false;
1871 }
1872
1873 if (!output)
1874 return;
1875
1876 if (output->frame) {
1877 frame_touch_up(output->frame, input, id);
1878
1879 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1880 wayland_output_destroy(&output->base);
1881 input->touch_focus = NULL;
1882 input->keyboard_focus = NULL;
1883 if (wl_list_empty(&input->backend->compositor->output_list))
1884 weston_compositor_exit(input->backend->compositor);
1885
1886 return;
1887 }
1888 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1889 weston_output_schedule_repaint(&output->base);
1890 }
1891
1892 if (active)
1893 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1894}
1895
1896static void
1897input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001898 uint32_t time, int32_t id,
1899 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001900{
1901 struct wayland_input *input = data;
1902 struct wayland_output *output = input->touch_focus;
1903 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001904 double x, y;
1905
1906 x = wl_fixed_to_double(fixed_x);
1907 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001908
1909 if (!output || !input->touch_active)
1910 return;
1911
1912 if (output->frame) {
1913 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001914 x -= fx;
1915 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001916 }
1917
1918 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1919
1920 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1921}
1922
1923static void
1924input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1925{
1926 struct wayland_input *input = data;
1927
1928 if (!input->touch_focus || !input->touch_active)
1929 return;
1930
1931 notify_touch_frame(&input->base);
1932}
1933
1934static void
1935input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1936{
1937 struct wayland_input *input = data;
1938
1939 if (!input->touch_focus || !input->touch_active)
1940 return;
1941
1942 notify_touch_cancel(&input->base);
1943}
1944
1945static const struct wl_touch_listener touch_listener = {
1946 input_handle_touch_down,
1947 input_handle_touch_up,
1948 input_handle_touch_motion,
1949 input_handle_touch_frame,
1950 input_handle_touch_cancel,
1951};
1952
1953
1954static void
Daniel Stone37816df2012-05-16 18:45:18 +01001955input_handle_capabilities(void *data, struct wl_seat *seat,
1956 enum wl_seat_capability caps)
1957{
1958 struct wayland_input *input = data;
1959
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001960 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1961 input->parent.pointer = wl_seat_get_pointer(seat);
1962 wl_pointer_set_user_data(input->parent.pointer, input);
1963 wl_pointer_add_listener(input->parent.pointer,
1964 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001965 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001966 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001967 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1968 wl_pointer_release(input->parent.pointer);
1969 else
1970 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001971 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001972 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001973 }
1974
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001975 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1976 input->parent.keyboard = wl_seat_get_keyboard(seat);
1977 wl_keyboard_set_user_data(input->parent.keyboard, input);
1978 wl_keyboard_add_listener(input->parent.keyboard,
1979 &keyboard_listener, input);
1980 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001981 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1982 wl_keyboard_release(input->parent.keyboard);
1983 else
1984 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001985 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001986 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001987 }
Derek Foreman748c6952015-11-06 15:56:10 -06001988
1989 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1990 input->parent.touch = wl_seat_get_touch(seat);
1991 wl_touch_set_user_data(input->parent.touch, input);
1992 wl_touch_add_listener(input->parent.touch,
1993 &touch_listener, input);
1994 weston_seat_init_touch(&input->base);
1995 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1996 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1997 wl_touch_release(input->parent.touch);
1998 else
1999 wl_touch_destroy(input->parent.touch);
2000 input->parent.touch = NULL;
2001 weston_seat_release_touch(&input->base);
2002 }
Daniel Stone37816df2012-05-16 18:45:18 +01002003}
2004
Jonny Lamb497994a2014-08-12 14:58:26 +02002005static void
2006input_handle_name(void *data, struct wl_seat *seat,
2007 const char *name)
2008{
2009}
2010
Daniel Stone37816df2012-05-16 18:45:18 +01002011static const struct wl_seat_listener seat_listener = {
2012 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02002013 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01002014};
2015
2016static void
Derek Foremancfce7d02015-11-06 15:56:08 -06002017display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002018{
2019 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06002020 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002021
Peter Huttererf3d62272013-08-08 11:57:05 +10002022 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002023 if (input == NULL)
2024 return;
2025
Giulio Camuffo954f1832014-10-11 18:27:30 +03002026 weston_seat_init(&input->base, b->compositor, "default");
2027 input->backend = b;
2028 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06002029 &wl_seat_interface, version);
2030 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002031 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002032
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05002033 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
2034 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002035
2036 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002037 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10002038
2039 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
2040 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002041}
2042
2043static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002044wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
2045 int32_t x, int32_t y,
2046 int32_t physical_width, int32_t physical_height,
2047 int32_t subpixel, const char *make,
2048 const char *model, int32_t transform)
2049{
2050 struct wayland_parent_output *output = data;
2051
2052 output->x = x;
2053 output->y = y;
2054 output->physical.width = physical_width;
2055 output->physical.height = physical_height;
2056 output->physical.subpixel = subpixel;
2057
2058 free(output->physical.make);
2059 output->physical.make = strdup(make);
2060 free(output->physical.model);
2061 output->physical.model = strdup(model);
2062
2063 output->transform = transform;
2064}
2065
2066static struct weston_mode *
2067find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
2068{
2069 struct weston_mode *mode;
2070
2071 wl_list_for_each(mode, list, link) {
2072 if (mode->width == width && mode->height == height &&
2073 mode->refresh == refresh)
2074 return mode;
2075 }
2076
2077 mode = zalloc(sizeof *mode);
2078 if (!mode)
2079 return NULL;
2080
2081 mode->width = width;
2082 mode->height = height;
2083 mode->refresh = refresh;
2084 wl_list_insert(list, &mode->link);
2085
2086 return mode;
2087}
2088
2089static void
2090wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2091 uint32_t flags, int32_t width, int32_t height,
2092 int32_t refresh)
2093{
2094 struct wayland_parent_output *output = data;
2095 struct weston_mode *mode;
2096
2097 if (output->output) {
2098 mode = find_mode(&output->output->base.mode_list,
2099 width, height, refresh);
2100 if (!mode)
2101 return;
2102 mode->flags = flags;
2103 /* Do a mode-switch on current mode change? */
2104 } else {
2105 mode = find_mode(&output->mode_list, width, height, refresh);
2106 if (!mode)
2107 return;
2108 mode->flags = flags;
2109 if (flags & WL_OUTPUT_MODE_CURRENT)
2110 output->current_mode = mode;
2111 if (flags & WL_OUTPUT_MODE_PREFERRED)
2112 output->preferred_mode = mode;
2113 }
2114}
2115
2116static const struct wl_output_listener output_listener = {
2117 wayland_parent_output_geometry,
2118 wayland_parent_output_mode
2119};
2120
2121static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002122wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002123{
2124 struct wayland_parent_output *output;
2125
2126 output = zalloc(sizeof *output);
2127 if (!output)
2128 return;
2129
2130 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002131 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002132 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002133 if (!output->global) {
2134 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002135 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002136 }
2137
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002138 wl_output_add_listener(output->global, &output_listener, output);
2139
2140 output->scale = 0;
2141 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2142 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2143 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002144 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002145
Giulio Camuffo954f1832014-10-11 18:27:30 +03002146 if (b->sprawl_across_outputs) {
2147 wl_display_roundtrip(b->parent.wl_display);
2148 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002149 }
2150}
2151
2152static void
2153wayland_parent_output_destroy(struct wayland_parent_output *output)
2154{
2155 struct weston_mode *mode, *next;
2156
2157 if (output->output)
2158 wayland_output_destroy(&output->output->base);
2159
2160 wl_output_destroy(output->global);
2161 free(output->physical.make);
2162 free(output->physical.model);
2163
2164 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2165 wl_list_remove(&mode->link);
2166 free(mode);
2167 }
2168}
2169
2170static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002171registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2172 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002173{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002174 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002175
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002176 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002177 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002178 wl_registry_bind(registry, name,
2179 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002180 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002181 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002182 wl_registry_bind(registry, name,
2183 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002184 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002185 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002186 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002187 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002188 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002189 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002190 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002191 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002192 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002193 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002194 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002195 }
2196}
2197
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002198static void
2199registry_handle_global_remove(void *data, struct wl_registry *registry,
2200 uint32_t name)
2201{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002202 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002203 struct wayland_parent_output *output;
2204
Giulio Camuffo954f1832014-10-11 18:27:30 +03002205 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002206 if (output->id == name)
2207 wayland_parent_output_destroy(output);
2208}
2209
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002210static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002211 registry_handle_global,
2212 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002213};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002214
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002215static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002216wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002217{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002218 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002219 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002220
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002221 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002222 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002223 return 0;
2224 }
2225
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002226 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002227 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002228 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002229 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002230
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002231 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002232 count = wl_display_dispatch_pending(b->parent.wl_display);
2233 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002234 }
2235
2236 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002237}
2238
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002239static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002240wayland_restore(struct weston_compositor *ec)
2241{
2242}
2243
2244static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002245wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002246{
Armin Krezović938dc522016-08-01 19:17:57 +02002247 struct wayland_backend *b = to_wayland_backend(ec);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002248
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002249 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002250
Giulio Camuffo954f1832014-10-11 18:27:30 +03002251 if (b->parent.shm)
2252 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002253
Giulio Camuffo954f1832014-10-11 18:27:30 +03002254 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002255}
2256
Jason Ekstrand7744f712013-10-27 22:24:55 -05002257static const char *left_ptrs[] = {
2258 "left_ptr",
2259 "default",
2260 "top_left_arrow",
2261 "left-arrow"
2262};
2263
2264static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002265create_cursor(struct wayland_backend *b,
2266 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002267{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002268 unsigned int i;
2269
Benoit Gschwind244ff792016-04-28 20:33:11 +02002270 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2271 config->cursor_size,
2272 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002273 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002274 fprintf(stderr, "could not load cursor theme\n");
2275 return;
2276 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002277
Giulio Camuffo954f1832014-10-11 18:27:30 +03002278 b->cursor = NULL;
2279 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2280 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002281 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002282 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002283 fprintf(stderr, "could not load left cursor\n");
2284 return;
2285 }
2286}
2287
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002288static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002289fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2290 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002291{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002292 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002293 struct wayland_input *input = NULL;
2294
Giulio Camuffo954f1832014-10-11 18:27:30 +03002295 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002296 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002297 break;
2298
2299 if (!input || !input->output)
2300 return;
2301
2302 if (input->output->frame)
2303 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2304 else
2305 wayland_output_set_windowed(input->output);
2306
2307 weston_output_schedule_repaint(&input->output->base);
2308}
2309
Giulio Camuffo954f1832014-10-11 18:27:30 +03002310static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002311wayland_backend_create(struct weston_compositor *compositor,
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002312 struct weston_wayland_backend_config *new_config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002313{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002314 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002315 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002316 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002317
Giulio Camuffo954f1832014-10-11 18:27:30 +03002318 b = zalloc(sizeof *b);
2319 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002320 return NULL;
2321
Giulio Camuffo954f1832014-10-11 18:27:30 +03002322 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002323 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002324 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002325
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002326 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002327 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002328 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002329 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002330 }
2331
Giulio Camuffo954f1832014-10-11 18:27:30 +03002332 wl_list_init(&b->parent.output_list);
2333 wl_list_init(&b->input_list);
2334 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2335 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2336 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002337
Benoit Gschwind244ff792016-04-28 20:33:11 +02002338 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002339
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002340 b->use_pixman = new_config->use_pixman;
Armin Krezović174448a2016-09-30 14:11:09 +02002341 b->fullscreen = new_config->fullscreen;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002342
Giulio Camuffo954f1832014-10-11 18:27:30 +03002343 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002344 gl_renderer = weston_load_module("gl-renderer.so",
2345 "gl_renderer_interface");
2346 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002347 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002348 }
2349
Giulio Camuffo954f1832014-10-11 18:27:30 +03002350 if (!b->use_pixman) {
2351 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002352 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002353 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002354 gl_renderer->alpha_attribs,
2355 NULL,
2356 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002357 weston_log("Failed to initialize the GL renderer; "
2358 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002359 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002360 }
2361 }
2362
Giulio Camuffo954f1832014-10-11 18:27:30 +03002363 if (b->use_pixman) {
2364 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002365 weston_log("Failed to initialize pixman renderer\n");
2366 goto err_display;
2367 }
2368 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002369
Giulio Camuffo954f1832014-10-11 18:27:30 +03002370 b->base.destroy = wayland_destroy;
2371 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002372
Giulio Camuffo954f1832014-10-11 18:27:30 +03002373 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002374
Giulio Camuffo954f1832014-10-11 18:27:30 +03002375 fd = wl_display_get_fd(b->parent.wl_display);
2376 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002377 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002378 wayland_backend_handle_event, b);
2379 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002380 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002381
Giulio Camuffo954f1832014-10-11 18:27:30 +03002382 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002383
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002384 if (compositor->renderer->import_dmabuf) {
2385 if (linux_dmabuf_setup(compositor) < 0)
2386 weston_log("Error: initializing dmabuf "
2387 "support failed.\n");
2388 }
2389
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 compositor->backend = &b->base;
2391 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002392err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002393 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002394err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002395 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002396 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002397 return NULL;
2398}
2399
2400static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002401wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002402{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002403 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002404
Giulio Camuffo954f1832014-10-11 18:27:30 +03002405 if (b->theme)
2406 theme_destroy(b->theme);
2407 if (b->frame_device)
2408 cairo_device_destroy(b->frame_device);
2409 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002410
Giulio Camuffo954f1832014-10-11 18:27:30 +03002411 weston_compositor_shutdown(b->compositor);
2412 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002413}
2414
Armin Krezović174448a2016-09-30 14:11:09 +02002415static const struct weston_windowed_output_api windowed_api = {
2416 wayland_output_set_size,
2417 wayland_output_create,
2418};
2419
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002420static void
Benoit Gschwinde091b452016-05-10 22:47:48 +02002421config_init_to_defaults(struct weston_wayland_backend_config *config)
2422{
2423}
2424
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002425WL_EXPORT int
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002426backend_init(struct weston_compositor *compositor,
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002427 struct weston_backend_config *config_base)
2428{
2429 struct wayland_backend *b;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002430 struct wayland_parent_output *poutput;
2431 struct weston_wayland_backend_config new_config;
Armin Krezović174448a2016-09-30 14:11:09 +02002432 int ret;
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002433
Benoit Gschwinde091b452016-05-10 22:47:48 +02002434 if (config_base == NULL ||
2435 config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
2436 config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
2437 weston_log("wayland backend config structure is invalid\n");
Benoit Gschwinde091b452016-05-10 22:47:48 +02002438 return -1;
2439 }
2440
2441 config_init_to_defaults(&new_config);
2442 memcpy(&new_config, config_base, config_base->struct_size);
2443
Pekka Paalanena256c5e2016-06-03 14:56:18 +03002444 b = wayland_backend_create(compositor, &new_config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002445
Giulio Camuffo954f1832014-10-11 18:27:30 +03002446 if (!b)
2447 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002448
Benoit Gschwind37a68072016-04-28 20:33:08 +02002449 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002450 b->sprawl_across_outputs = 1;
2451 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002452
Giulio Camuffo954f1832014-10-11 18:27:30 +03002453 wl_list_for_each(poutput, &b->parent.output_list, link)
2454 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002455
Giulio Camuffo954f1832014-10-11 18:27:30 +03002456 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002457 }
2458
Benoit Gschwind37a68072016-04-28 20:33:08 +02002459 if (new_config.fullscreen) {
Armin Krezović174448a2016-09-30 14:11:09 +02002460 if (wayland_output_create_fullscreen(b) < 0) {
2461 weston_log("Unable to create a fullscreen output.\n");
Benoit Gschwindcd63b3e2016-04-28 20:33:14 +02002462 goto err_outputs;
Armin Krezović174448a2016-09-30 14:11:09 +02002463 }
Benoit Gschwind830b7882016-04-28 20:33:12 +02002464
Giulio Camuffo954f1832014-10-11 18:27:30 +03002465 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002466 }
2467
Armin Krezović174448a2016-09-30 14:11:09 +02002468 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
2469 &windowed_api, sizeof(windowed_api));
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002470
Armin Krezović174448a2016-09-30 14:11:09 +02002471 if (ret < 0) {
2472 weston_log("Failed to register output API.\n");
2473 wayland_backend_destroy(b);
2474 return -1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002475 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002476
Giulio Camuffo954f1832014-10-11 18:27:30 +03002477 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002478 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002479 fullscreen_binding, b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002480 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002481
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002482err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002483 wayland_backend_destroy(b);
2484 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002485}