blob: 8577a74e90feee3710c8ab9e42931d548ef603c2 [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003 * Copyright © 2013 Jason Ekstrand
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010028
29#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010035#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060036#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkebe014562011-02-18 17:04:24 +010038#include <wayland-client.h>
39#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050040#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010041
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010043#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050044#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/image-loader.h"
47#include "shared/os-compatibility.h"
48#include "shared/cairo-util.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080049#include "fullscreen-shell-unstable-v1-client-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020050#include "presentation-time-server-protocol.h"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020051#include "linux-dmabuf.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052
Jason Ekstrand48ce4212013-10-27 22:25:02 -050053#define WINDOW_TITLE "Weston Compositor"
54
Benoit Gschwind37a68072016-04-28 20:33:08 +020055struct weston_wayland_backend_config {
56 int use_pixman;
57 int sprawl;
58 char *display_name;
59 int fullscreen;
60};
61
Giulio Camuffo954f1832014-10-11 18:27:30 +030062struct wayland_backend {
63 struct weston_backend base;
64 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010065
66 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040067 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040068 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069 struct wl_compositor *compositor;
70 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080071 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020072 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050074 struct wl_list output_list;
75
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076 struct wl_event_source *wl_source;
77 uint32_t event_mask;
78 } parent;
79
Jason Ekstrandff2fd462013-10-27 22:24:58 -050080 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050081 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050082
Jason Ekstrand7744f712013-10-27 22:24:55 -050083 struct theme *theme;
84 cairo_device_t *frame_device;
85 struct wl_cursor_theme *cursor_theme;
86 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050087
Jason Ekstrand06ced802013-11-07 20:13:29 -060088 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010089};
90
91struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050092 struct weston_output base;
93
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010094 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050095 int draw_initial_frame;
96 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050097
98 struct wl_output *output;
99 uint32_t global_id;
100
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500101 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600102 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100103 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500104
Jason Ekstrand7744f712013-10-27 22:24:55 -0500105 int keyboard_count;
106
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600107 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500108 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500109
Jason Ekstrand7744f712013-10-27 22:24:55 -0500110 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500111 struct wl_egl_window *egl_window;
112 struct {
113 cairo_surface_t *top;
114 cairo_surface_t *left;
115 cairo_surface_t *right;
116 cairo_surface_t *bottom;
117 } border;
118 } gl;
119
120 struct {
121 struct wl_list buffers;
122 struct wl_list free_buffers;
123 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500124
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500125 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500126 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100127};
128
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500129struct wayland_parent_output {
130 struct wayland_output *output;
131 struct wl_list link;
132
133 struct wl_output *global;
134 uint32_t id;
135
136 struct {
137 char *make;
138 char *model;
139 int32_t width, height;
140 uint32_t subpixel;
141 } physical;
142
143 int32_t x, y;
144 uint32_t transform;
145 uint32_t scale;
146
147 struct wl_list mode_list;
148 struct weston_mode *preferred_mode;
149 struct weston_mode *current_mode;
150};
151
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500152struct wayland_shm_buffer {
153 struct wayland_output *output;
154 struct wl_list link;
155 struct wl_list free_link;
156
157 struct wl_buffer *buffer;
158 void *data;
159 size_t size;
160 pixman_region32_t damage;
161 int frame_damaged;
162
163 pixman_image_t *pm_image;
164 cairo_surface_t *c_surface;
165};
166
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100167struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400168 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300169 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100170 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500171
172 struct {
173 struct wl_seat *seat;
174 struct wl_pointer *pointer;
175 struct wl_keyboard *keyboard;
176 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500177
178 struct {
179 struct wl_surface *surface;
180 int32_t hx, hy;
181 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500182 } parent;
183
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500184 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100185 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400186 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600187 uint32_t touch_points;
188 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600189 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600190 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600191
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400192 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600193 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500194 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000195
196 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100197};
198
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300199struct gl_renderer_interface *gl_renderer;
200
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500201static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500202wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
203{
204 cairo_surface_destroy(buffer->c_surface);
205 pixman_image_unref(buffer->pm_image);
206
207 wl_buffer_destroy(buffer->buffer);
208 munmap(buffer->data, buffer->size);
209
210 pixman_region32_fini(&buffer->damage);
211
212 wl_list_remove(&buffer->link);
213 wl_list_remove(&buffer->free_link);
214 free(buffer);
215}
216
217static void
218buffer_release(void *data, struct wl_buffer *buffer)
219{
220 struct wayland_shm_buffer *sb = data;
221
222 if (sb->output) {
223 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
224 } else {
225 wayland_shm_buffer_destroy(sb);
226 }
227}
228
229static const struct wl_buffer_listener buffer_listener = {
230 buffer_release
231};
232
233static struct wayland_shm_buffer *
234wayland_output_get_shm_buffer(struct wayland_output *output)
235{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300236 struct wayland_backend *b =
237 (struct wayland_backend *) output->base.compositor->backend;
238 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500239 struct wayland_shm_buffer *sb;
240
241 struct wl_shm_pool *pool;
242 int width, height, stride;
243 int32_t fx, fy;
244 int fd;
245 unsigned char *data;
246
247 if (!wl_list_empty(&output->shm.free_buffers)) {
248 sb = container_of(output->shm.free_buffers.next,
249 struct wayland_shm_buffer, free_link);
250 wl_list_remove(&sb->free_link);
251 wl_list_init(&sb->free_link);
252
253 return sb;
254 }
255
256 if (output->frame) {
257 width = frame_width(output->frame);
258 height = frame_height(output->frame);
259 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500260 width = output->base.current_mode->width;
261 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500262 }
263
264 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
265
266 fd = os_create_anonymous_file(height * stride);
267 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000268 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500269 return NULL;
270 }
271
272 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
273 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000274 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500275 close(fd);
276 return NULL;
277 }
278
279 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000280 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200281 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000282 close(fd);
283 free(data);
284 return NULL;
285 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500286
287 sb->output = output;
288 wl_list_init(&sb->free_link);
289 wl_list_insert(&output->shm.buffers, &sb->link);
290
291 pixman_region32_init_rect(&sb->damage, 0, 0,
292 output->base.width, output->base.height);
293 sb->frame_damaged = 1;
294
295 sb->data = data;
296 sb->size = height * stride;
297
298 pool = wl_shm_create_pool(shm, fd, sb->size);
299
300 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
301 width, height,
302 stride,
303 WL_SHM_FORMAT_ARGB8888);
304 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
305 wl_shm_pool_destroy(pool);
306 close(fd);
307
308 memset(data, 0, sb->size);
309
310 sb->c_surface =
311 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
312 width, height, stride);
313
314 fx = 0;
315 fy = 0;
316 if (output->frame)
317 frame_interior(output->frame, &fx, &fy, 0, 0);
318 sb->pm_image =
319 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
320 (uint32_t *)(data + fy * stride) + fx,
321 stride);
322
323 return sb;
324}
325
326static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500327frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400328{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500329 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400330 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400331
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500332 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400333
334 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300335
336 /*
337 * This is the fallback case, where Presentation extension is not
338 * available from the parent compositor. We do not know the base for
339 * 'time', so we cannot feed it to finish_frame(). Do the only thing
340 * we can, and pretend finish_frame time is when we process this
341 * event.
342 */
343 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200344 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400345}
346
347static const struct wl_callback_listener frame_listener = {
348 frame_done
349};
350
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500351static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200352draw_initial_frame(struct wayland_output *output)
353{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500354 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200355
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500356 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200357
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500358 /* If we are rendering with GL, then orphan it so that it gets
359 * destroyed immediately */
360 if (output->gl.egl_window)
361 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500362
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500363 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500364 wl_surface_damage(output->parent.surface, 0, 0,
365 output->base.current_mode->width,
366 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200367}
368
369static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500370wayland_output_update_gl_border(struct wayland_output *output)
371{
372 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
373 cairo_t *cr;
374
375 if (!output->frame)
376 return;
377 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
378 return;
379
380 fwidth = frame_width(output->frame);
381 fheight = frame_height(output->frame);
382 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
383
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500384 if (!output->gl.border.top)
385 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
387 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389 frame_repaint(output->frame, cr);
390 cairo_destroy(cr);
391 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
392 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500393 cairo_image_surface_get_stride(output->gl.border.top) / 4,
394 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500395
396
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500397 if (!output->gl.border.left)
398 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
400 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500401 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500402 cairo_translate(cr, 0, -iy);
403 frame_repaint(output->frame, cr);
404 cairo_destroy(cr);
405 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
406 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500407 cairo_image_surface_get_stride(output->gl.border.left) / 4,
408 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409
410
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500411 if (!output->gl.border.right)
412 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500413 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
414 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500415 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500416 cairo_translate(cr, -(iwidth + ix), -iy);
417 frame_repaint(output->frame, cr);
418 cairo_destroy(cr);
419 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
420 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500421 cairo_image_surface_get_stride(output->gl.border.right) / 4,
422 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500423
424
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500425 if (!output->gl.border.bottom)
426 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500427 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
428 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500429 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500430 cairo_translate(cr, 0, -(iy + iheight));
431 frame_repaint(output->frame, cr);
432 cairo_destroy(cr);
433 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
434 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500435 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
436 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500437}
438
439static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200440wayland_output_start_repaint_loop(struct weston_output *output_base)
441{
442 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300443 struct wayland_backend *wb =
444 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200445 struct wl_callback *callback;
446
447 /* If this is the initial frame, we need to attach a buffer so that
448 * the compositor can map the surface and include it in its render
449 * loop. If the surface doesn't end up in the render loop, the frame
450 * callback won't be invoked. The buffer is transparent and of the
451 * same size as the future real output buffer. */
452 if (output->parent.draw_initial_frame) {
453 output->parent.draw_initial_frame = 0;
454
455 draw_initial_frame(output);
456 }
457
458 callback = wl_surface_frame(output->parent.surface);
459 wl_callback_add_listener(callback, &frame_listener, output);
460 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300461 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200462}
463
David Herrmann1edf44c2013-10-22 17:11:26 +0200464static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500465wayland_output_repaint_gl(struct weston_output *output_base,
466 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400467{
468 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400469 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400470 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600471
Kristian Høgsberg33418202011-08-16 23:01:28 -0400472 callback = wl_surface_frame(output->parent.surface);
473 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100474
Jason Ekstrand7744f712013-10-27 22:24:55 -0500475 wayland_output_update_gl_border(output);
476
Pekka Paalanenbc106382012-10-10 12:49:31 +0300477 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200478
479 pixman_region32_subtract(&ec->primary_plane.damage,
480 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200481 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100482}
483
Matt Roper361d2ad2011-08-29 13:52:23 -0700484static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500485wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
486{
487 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
488 cairo_t *cr;
489
490 if (!buffer->output->frame || !buffer->frame_damaged)
491 return;
492
493 cr = cairo_create(buffer->c_surface);
494
495 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
496 fwidth = frame_width(buffer->output->frame);
497 fheight = frame_height(buffer->output->frame);
498
499 /* Set the clip so we don't unnecisaraly damage the surface */
500 cairo_move_to(cr, ix, iy);
501 cairo_rel_line_to(cr, iwidth, 0);
502 cairo_rel_line_to(cr, 0, iheight);
503 cairo_rel_line_to(cr, -iwidth, 0);
504 cairo_line_to(cr, ix, iy);
505 cairo_line_to(cr, 0, iy);
506 cairo_line_to(cr, 0, fheight);
507 cairo_line_to(cr, fwidth, fheight);
508 cairo_line_to(cr, fwidth, 0);
509 cairo_line_to(cr, 0, 0);
510 cairo_line_to(cr, 0, iy);
511 cairo_close_path(cr);
512 cairo_clip(cr);
513
514 /* Draw using a pattern so that the final result gets clipped */
515 cairo_push_group(cr);
516 frame_repaint(buffer->output->frame, cr);
517 cairo_pop_group_to_source(cr);
518 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
519 cairo_paint(cr);
520
521 cairo_destroy(cr);
522}
523
524static void
525wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
526{
527 pixman_region32_t damage;
528 pixman_box32_t *rects;
529 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
530 int i, n;
531
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500532 pixman_region32_init(&damage);
533 weston_transformed_region(sb->output->base.width,
534 sb->output->base.height,
535 sb->output->base.transform,
536 sb->output->base.current_scale,
537 &sb->damage, &damage);
538
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500539 if (sb->output->frame) {
540 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
541 fwidth = frame_width(sb->output->frame);
542 fheight = frame_height(sb->output->frame);
543
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500544 pixman_region32_translate(&damage, ix, iy);
545
546 if (sb->frame_damaged) {
547 pixman_region32_union_rect(&damage, &damage,
548 0, 0, fwidth, iy);
549 pixman_region32_union_rect(&damage, &damage,
550 0, iy, ix, iheight);
551 pixman_region32_union_rect(&damage, &damage,
552 ix + iwidth, iy,
553 fwidth - (ix + iwidth), iheight);
554 pixman_region32_union_rect(&damage, &damage,
555 0, iy + iheight,
556 fwidth, fheight - (iy + iheight));
557 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500558 }
559
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500560 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500561 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
562 for (i = 0; i < n; ++i)
563 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
564 rects[i].y1, rects[i].x2 - rects[i].x1,
565 rects[i].y2 - rects[i].y1);
566
567 if (sb->output->frame)
568 pixman_region32_fini(&damage);
569}
570
571static int
572wayland_output_repaint_pixman(struct weston_output *output_base,
573 pixman_region32_t *damage)
574{
575 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300576 struct wayland_backend *b =
577 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500578 struct wl_callback *callback;
579 struct wayland_shm_buffer *sb;
580
581 if (output->frame) {
582 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
583 wl_list_for_each(sb, &output->shm.buffers, link)
584 sb->frame_damaged = 1;
585 }
586
587 wl_list_for_each(sb, &output->shm.buffers, link)
588 pixman_region32_union(&sb->damage, &sb->damage, damage);
589
590 sb = wayland_output_get_shm_buffer(output);
591
592 wayland_output_update_shm_border(sb);
593 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300594 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500595
596 wayland_shm_buffer_attach(sb);
597
598 callback = wl_surface_frame(output->parent.surface);
599 wl_callback_add_listener(callback, &frame_listener, output);
600 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300601 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500602
603 pixman_region32_fini(&sb->damage);
604 pixman_region32_init(&sb->damage);
605 sb->frame_damaged = 0;
606
Giulio Camuffo954f1832014-10-11 18:27:30 +0300607 pixman_region32_subtract(&b->compositor->primary_plane.damage,
608 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500609 return 0;
610}
611
612static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500613wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700614{
615 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300616 struct wayland_backend *b =
617 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700618
Giulio Camuffo954f1832014-10-11 18:27:30 +0300619 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500620 pixman_renderer_output_destroy(output_base);
621 } else {
622 gl_renderer->output_destroy(output_base);
623 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100624
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500625 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500626 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500627 if (output->parent.shell_surface)
628 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500629
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600630 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500631 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600632
633 cairo_surface_destroy(output->gl.border.top);
634 cairo_surface_destroy(output->gl.border.left);
635 cairo_surface_destroy(output->gl.border.right);
636 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500637
638 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700639 free(output);
640
641 return;
642}
643
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300644static const struct wl_shell_surface_listener shell_surface_listener;
645
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200646static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500647wayland_output_init_gl_renderer(struct wayland_output *output)
648{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500649 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500650
651 if (output->frame) {
652 fwidth = frame_width(output->frame);
653 fheight = frame_height(output->frame);
654 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500655 fwidth = output->base.current_mode->width;
656 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500657 }
658
659 output->gl.egl_window =
660 wl_egl_window_create(output->parent.surface,
661 fwidth, fheight);
662 if (!output->gl.egl_window) {
663 weston_log("failure to create wl_egl_window\n");
664 return -1;
665 }
666
667 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000668 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100669 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000670 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500671 NULL,
672 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500673 goto cleanup_window;
674
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500675 return 0;
676
677cleanup_window:
678 wl_egl_window_destroy(output->gl.egl_window);
679 return -1;
680}
681
682static int
683wayland_output_init_pixman_renderer(struct wayland_output *output)
684{
685 return pixman_renderer_output_create(&output->base);
686}
687
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600688static void
689wayland_output_resize_surface(struct wayland_output *output)
690{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300691 struct wayland_backend *b =
692 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600693 struct wayland_shm_buffer *buffer, *next;
694 int32_t ix, iy, iwidth, iheight;
695 int32_t width, height;
696 struct wl_region *region;
697
698 width = output->base.current_mode->width;
699 height = output->base.current_mode->height;
700
701 if (output->frame) {
702 frame_resize_inside(output->frame, width, height);
703
704 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300705 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600706 wl_region_add(region, ix, iy, iwidth, iheight);
707 wl_surface_set_input_region(output->parent.surface, region);
708 wl_region_destroy(region);
709
710 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300711 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600712 wl_region_add(region, ix, iy, iwidth, iheight);
713 wl_surface_set_opaque_region(output->parent.surface, region);
714 wl_region_destroy(region);
715
716 width = frame_width(output->frame);
717 height = frame_height(output->frame);
718 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300719 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600720 wl_region_add(region, 0, 0, width, height);
721 wl_surface_set_input_region(output->parent.surface, region);
722 wl_region_destroy(region);
723
Giulio Camuffo954f1832014-10-11 18:27:30 +0300724 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600725 wl_region_add(region, 0, 0, width, height);
726 wl_surface_set_opaque_region(output->parent.surface, region);
727 wl_region_destroy(region);
728 }
729
730 if (output->gl.egl_window) {
731 wl_egl_window_resize(output->gl.egl_window,
732 width, height, 0, 0);
733
734 /* These will need to be re-created due to the resize */
735 gl_renderer->output_set_border(&output->base,
736 GL_RENDERER_BORDER_TOP,
737 0, 0, 0, NULL);
738 cairo_surface_destroy(output->gl.border.top);
739 output->gl.border.top = NULL;
740 gl_renderer->output_set_border(&output->base,
741 GL_RENDERER_BORDER_LEFT,
742 0, 0, 0, NULL);
743 cairo_surface_destroy(output->gl.border.left);
744 output->gl.border.left = NULL;
745 gl_renderer->output_set_border(&output->base,
746 GL_RENDERER_BORDER_RIGHT,
747 0, 0, 0, NULL);
748 cairo_surface_destroy(output->gl.border.right);
749 output->gl.border.right = NULL;
750 gl_renderer->output_set_border(&output->base,
751 GL_RENDERER_BORDER_BOTTOM,
752 0, 0, 0, NULL);
753 cairo_surface_destroy(output->gl.border.bottom);
754 output->gl.border.bottom = NULL;
755 }
756
757 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500758 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600759 wayland_shm_buffer_destroy(buffer);
760 /* These will get thrown away when they get released */
761 wl_list_for_each(buffer, &output->shm.buffers, link)
762 buffer->output = NULL;
763}
764
765static int
766wayland_output_set_windowed(struct wayland_output *output)
767{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300768 struct wayland_backend *b =
769 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600770 int tlen;
771 char *title;
772
773 if (output->frame)
774 return 0;
775
776 if (output->name) {
777 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
778 title = malloc(tlen + 1);
779 if (!title)
780 return -1;
781
782 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
783 } else {
784 title = strdup(WINDOW_TITLE);
785 }
786
Giulio Camuffo954f1832014-10-11 18:27:30 +0300787 if (!b->theme) {
788 b->theme = theme_create();
789 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600790 free(title);
791 return -1;
792 }
793 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300794 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600795 FRAME_BUTTON_CLOSE, title);
796 free(title);
797 if (!output->frame)
798 return -1;
799
800 if (output->keyboard_count)
801 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
802
803 wayland_output_resize_surface(output);
804
805 wl_shell_surface_set_toplevel(output->parent.shell_surface);
806
807 return 0;
808}
809
810static void
811wayland_output_set_fullscreen(struct wayland_output *output,
812 enum wl_shell_surface_fullscreen_method method,
813 uint32_t framerate, struct wl_output *target)
814{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300815 struct wayland_backend *b =
816 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500817
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600818 if (output->frame) {
819 frame_destroy(output->frame);
820 output->frame = NULL;
821 }
822
823 wayland_output_resize_surface(output);
824
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500825 if (output->parent.shell_surface) {
826 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
827 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300828 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800829 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
830 output->parent.surface,
831 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500832 }
833}
834
835static struct weston_mode *
836wayland_output_choose_mode(struct wayland_output *output,
837 struct weston_mode *ref_mode)
838{
839 struct weston_mode *mode;
840
841 /* First look for an exact match */
842 wl_list_for_each(mode, &output->base.mode_list, link)
843 if (mode->width == ref_mode->width &&
844 mode->height == ref_mode->height &&
845 mode->refresh == ref_mode->refresh)
846 return mode;
847
848 /* If we can't find an exact match, ignore refresh and try again */
849 wl_list_for_each(mode, &output->base.mode_list, link)
850 if (mode->width == ref_mode->width &&
851 mode->height == ref_mode->height)
852 return mode;
853
854 /* Yeah, we failed */
855 return NULL;
856}
857
858enum mode_status {
859 MODE_STATUS_UNKNOWN,
860 MODE_STATUS_SUCCESS,
861 MODE_STATUS_FAIL,
862 MODE_STATUS_CANCEL,
863};
864
865static void
866mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800867 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500868{
869 enum mode_status *value = data;
870
871 printf("Mode switch successful\n");
872
873 *value = MODE_STATUS_SUCCESS;
874}
875
876static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800877mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500878{
879 enum mode_status *value = data;
880
881 printf("Mode switch failed\n");
882
883 *value = MODE_STATUS_FAIL;
884}
885
886static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800887mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500888{
889 enum mode_status *value = data;
890
891 printf("Mode switch cancelled\n");
892
893 *value = MODE_STATUS_CANCEL;
894}
895
Jonas Ådahl496adb32015-11-17 16:00:27 +0800896struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500897 mode_feedback_successful,
898 mode_feedback_failed,
899 mode_feedback_cancelled,
900};
901
902static int
903wayland_output_switch_mode(struct weston_output *output_base,
904 struct weston_mode *mode)
905{
906 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300907 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500908 struct wl_surface *old_surface;
909 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800910 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500911 enum mode_status mode_status;
912 int ret = 0;
913
914 if (output_base == NULL) {
915 weston_log("output is NULL.\n");
916 return -1;
917 }
918
919 if (mode == NULL) {
920 weston_log("mode is NULL.\n");
921 return -1;
922 }
923
Giulio Camuffo954f1832014-10-11 18:27:30 +0300924 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500925
Giulio Camuffo954f1832014-10-11 18:27:30 +0300926 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500927 return -1;
928
929 mode = wayland_output_choose_mode(output, mode);
930 if (mode == NULL)
931 return -1;
932
933 if (output->base.current_mode == mode)
934 return 0;
935
936 old_mode = output->base.current_mode;
937 old_surface = output->parent.surface;
938 output->base.current_mode = mode;
939 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300940 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500941 wl_surface_set_user_data(output->parent.surface, output);
942
943 /* Blow the old buffers because we changed size/surfaces */
944 wayland_output_resize_surface(output);
945
946 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800947 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
948 output->parent.surface,
949 output->parent.output,
950 mode->refresh);
951 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
952 &mode_feedback_listener,
953 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500954
955 /* This should kick-start things again */
956 output->parent.draw_initial_frame = 1;
957 wayland_output_start_repaint_loop(&output->base);
958
959 mode_status = MODE_STATUS_UNKNOWN;
960 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300961 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500962
Jonas Ådahl496adb32015-11-17 16:00:27 +0800963 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500964
965 if (mode_status == MODE_STATUS_FAIL) {
966 output->base.current_mode = old_mode;
967 wl_surface_destroy(output->parent.surface);
968 output->parent.surface = old_surface;
969 wayland_output_resize_surface(output);
970
971 return -1;
972 }
973
974 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
975 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
976
Giulio Camuffo954f1832014-10-11 18:27:30 +0300977 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500978 pixman_renderer_output_destroy(output_base);
979 if (wayland_output_init_pixman_renderer(output) < 0)
980 goto err_output;
981 } else {
982 gl_renderer->output_destroy(output_base);
983 wl_egl_window_destroy(output->gl.egl_window);
984 if (wayland_output_init_gl_renderer(output) < 0)
985 goto err_output;
986 }
987 wl_surface_destroy(old_surface);
988
989 weston_output_schedule_repaint(&output->base);
990
991 return 0;
992
993err_output:
994 /* XXX */
995 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600996}
997
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500998static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300999wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001000 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001001 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001002{
1003 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001004 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001005
1006 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1007 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001008
Peter Huttererf3d62272013-08-08 11:57:05 +10001009 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001010 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001011 return NULL;
1012
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001013 output->name = name ? strdup(name) : NULL;
1014 output->base.make = "waywayland";
1015 output->base.model = "none";
1016
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001017 output_width = width * scale;
1018 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001019
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001020 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001021 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001022 if (!output->parent.surface)
1023 goto err_name;
1024 wl_surface_set_user_data(output->parent.surface, output);
1025
1026 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001027
Giulio Camuffo954f1832014-10-11 18:27:30 +03001028 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001029 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001030 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001031 output->parent.surface);
1032 if (!output->parent.shell_surface)
1033 goto err_surface;
1034 wl_shell_surface_add_listener(output->parent.shell_surface,
1035 &shell_surface_listener, output);
1036 }
1037
Giulio Camuffo954f1832014-10-11 18:27:30 +03001038 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001039 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1040 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001041 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001042 if (!width)
1043 output_width = output->parent.configure_width;
1044 if (!height)
1045 output_height = output->parent.configure_height;
1046 }
1047
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001048 output->mode.flags =
1049 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001050 output->mode.width = output_width;
1051 output->mode.height = output_height;
1052 output->mode.refresh = 60000;
1053 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001054 wl_list_init(&output->base.mode_list);
1055 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001056 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001057
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001058 wl_list_init(&output->shm.buffers);
1059 wl_list_init(&output->shm.free_buffers);
1060
Giulio Camuffo954f1832014-10-11 18:27:30 +03001061 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001062 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001063
Giulio Camuffo954f1832014-10-11 18:27:30 +03001064 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001065 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001066 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001067 output->base.repaint = wayland_output_repaint_pixman;
1068 } else {
1069 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001070 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001071 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001072 }
1073
Jonas Ådahle5a12252013-04-05 23:07:11 +02001074 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001075 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001076 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001077 output->base.set_backlight = NULL;
1078 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001079 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001080
Giulio Camuffo954f1832014-10-11 18:27:30 +03001081 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001082
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001083 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001084
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001085err_output:
1086 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001087 if (output->parent.shell_surface)
1088 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001089err_surface:
1090 wl_surface_destroy(output->parent.surface);
1091err_name:
1092 free(output->name);
1093
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001094 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001095 free(output);
1096
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001097 return NULL;
1098}
1099
1100static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001101wayland_output_create_for_config(struct wayland_backend *b,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001102 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001103 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001104 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001105{
1106 struct wayland_output *output;
1107 char *mode, *t, *name, *str;
1108 int width, height, scale;
1109 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001110 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001111
1112 weston_config_section_get_string(config_section, "name", &name, NULL);
1113 if (name) {
1114 slen = strlen(name);
1115 slen += strlen(WINDOW_TITLE " - ");
1116 str = malloc(slen + 1);
1117 if (str)
1118 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1119 free(name);
1120 name = str;
1121 }
1122 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001123 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001124
1125 weston_config_section_get_string(config_section,
1126 "mode", &mode, "1024x600");
1127 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1128 weston_log("Invalid mode \"%s\" for output %s\n",
1129 mode, name);
1130 width = 1024;
1131 height = 640;
1132 }
1133 free(mode);
1134
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001135 if (option_width)
1136 width = option_width;
1137 if (option_height)
1138 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001139
1140 weston_config_section_get_int(config_section, "scale", &scale, 1);
1141
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001142 if (option_scale)
1143 scale = option_scale;
1144
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001145 weston_config_section_get_string(config_section,
1146 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001147 if (weston_parse_transform(t, &transform) < 0)
1148 weston_log("Invalid transform \"%s\" for output %s\n",
1149 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001150 free(t);
1151
Giulio Camuffo954f1832014-10-11 18:27:30 +03001152 output = wayland_output_create(b, x, y, width, height, name, 0,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001153 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001154 free(name);
1155
1156 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001157}
1158
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001159static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001160wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001161 struct wayland_parent_output *poutput)
1162{
1163 struct wayland_output *output;
1164 struct weston_mode *mode;
1165 int32_t x;
1166
1167 if (poutput->current_mode) {
1168 mode = poutput->current_mode;
1169 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001170 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001171 } else if (!wl_list_empty(&poutput->mode_list)) {
1172 mode = container_of(poutput->mode_list.next,
1173 struct weston_mode, link);
1174 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001175 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001176 return NULL;
1177 }
1178
Giulio Camuffo954f1832014-10-11 18:27:30 +03001179 if (!wl_list_empty(&b->compositor->output_list)) {
1180 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001181 struct wayland_output, base.link);
1182 x = output->base.x + output->base.current_mode->width;
1183 } else {
1184 x = 0;
1185 }
1186
Giulio Camuffo954f1832014-10-11 18:27:30 +03001187 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001188 NULL, 0,
1189 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1190 if (!output)
1191 return NULL;
1192
1193 output->parent.output = poutput->global;
1194
1195 output->base.make = poutput->physical.make;
1196 output->base.model = poutput->physical.model;
1197 wl_list_init(&output->base.mode_list);
1198 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1199 wl_list_init(&poutput->mode_list);
1200
1201 wayland_output_set_fullscreen(output,
1202 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1203 mode->refresh, poutput->global);
1204
1205 if (output->parent.shell_surface) {
1206 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1207 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1208 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001209 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001210 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1211 output->parent.surface,
1212 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1213 poutput->global);
1214 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1215 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1216 output->parent.surface,
1217 poutput->global,
1218 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001219 }
1220
1221 return output;
1222}
1223
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001224static void
1225shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1226 uint32_t serial)
1227{
1228 wl_shell_surface_pong(shell_surface, serial);
1229}
1230
1231static void
1232shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1233 uint32_t edges, int32_t width, int32_t height)
1234{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001235 struct wayland_output *output = data;
1236
1237 output->parent.configure_width = width;
1238 output->parent.configure_height = height;
1239
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001240 /* FIXME: implement resizing */
1241}
1242
1243static void
1244shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1245{
1246}
1247
1248static const struct wl_shell_surface_listener shell_surface_listener = {
1249 shell_surface_ping,
1250 shell_surface_configure,
1251 shell_surface_popup_done
1252};
1253
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001254/* Events received from the wayland-server this compositor is client of: */
1255
Jason Ekstrand7744f712013-10-27 22:24:55 -05001256/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001257static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001258input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001259{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001260
Jason Ekstrand7744f712013-10-27 22:24:55 -05001261 struct wl_buffer *buffer;
1262 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001263
Giulio Camuffo954f1832014-10-11 18:27:30 +03001264 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001265 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001266
Giulio Camuffo954f1832014-10-11 18:27:30 +03001267 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001268 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001269 if (!buffer)
1270 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001271
1272 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1273 input->parent.cursor.surface,
1274 image->hotspot_x, image->hotspot_y);
1275
1276 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1277 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1278 image->width, image->height);
1279 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001280}
1281
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001282static void
Daniel Stone37816df2012-05-16 18:45:18 +01001283input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1284 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001285 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001286{
1287 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001288 int32_t fx, fy;
1289 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001290 double x, y;
1291
1292 x = wl_fixed_to_double(fixed_x);
1293 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001294
Daniel Stone50692802012-06-22 13:21:41 +01001295 /* XXX: If we get a modifier event immediately before the focus,
1296 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001297 input->enter_serial = serial;
1298 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001299
1300 if (input->output->frame) {
1301 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001302 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001303 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001304 x -= fx;
1305 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001306
1307 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1308 weston_output_schedule_repaint(&input->output->base);
1309 } else {
1310 location = THEME_LOCATION_CLIENT_AREA;
1311 }
1312
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001313 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001314
1315 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001316 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001317 notify_pointer_focus(&input->base, &input->output->base, x, y);
1318 wl_pointer_set_cursor(input->parent.pointer,
1319 input->enter_serial, NULL, 0, 0);
1320 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001321 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001322 notify_pointer_focus(&input->base, NULL, 0, 0);
1323 input_set_cursor(input);
1324 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001325}
1326
1327static void
Daniel Stone37816df2012-05-16 18:45:18 +01001328input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1329 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001330{
1331 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001332
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001333 if (!input->output)
1334 return;
1335
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336 if (input->output->frame) {
1337 frame_pointer_leave(input->output->frame, input);
1338
1339 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1340 weston_output_schedule_repaint(&input->output->base);
1341 }
1342
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001343 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001344 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001345 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001346}
1347
1348static void
Daniel Stone37816df2012-05-16 18:45:18 +01001349input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001350 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001351{
1352 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001353 int32_t fx, fy;
1354 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001355 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001356 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001357
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001358 if (!input->output)
1359 return;
1360
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001361 x = wl_fixed_to_double(fixed_x);
1362 y = wl_fixed_to_double(fixed_y);
1363
Jason Ekstrand7744f712013-10-27 22:24:55 -05001364 if (input->output->frame) {
1365 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001366 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001367 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001368 x -= fx;
1369 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001370
1371 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1372 weston_output_schedule_repaint(&input->output->base);
1373 } else {
1374 location = THEME_LOCATION_CLIENT_AREA;
1375 }
1376
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001377 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001378
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001379 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001380 input_set_cursor(input);
1381 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001382 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001383 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001384 } else if (!input->has_focus &&
1385 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001386 wl_pointer_set_cursor(input->parent.pointer,
1387 input->enter_serial, NULL, 0, 0);
1388 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001389 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001390 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001391 }
1392
Peter Hutterer87743e92016-01-18 16:38:22 +10001393 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001394 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001395 want_frame = true;
1396 }
1397
1398 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1399 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001400}
1401
1402static void
1403input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001404 uint32_t serial, uint32_t time, uint32_t button,
1405 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001406{
1407 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001408 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001409 enum frame_button_state fstate;
1410 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001411
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001412 if (!input->output)
1413 return;
1414
Jason Ekstrand7744f712013-10-27 22:24:55 -05001415 if (input->output->frame) {
1416 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1417 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1418
1419 location = frame_pointer_button(input->output->frame, input,
1420 button, fstate);
1421
1422 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1423
1424 wl_shell_surface_move(input->output->parent.shell_surface,
1425 input->parent.seat, serial);
1426 frame_status_clear(input->output->frame,
1427 FRAME_STATUS_MOVE);
1428 return;
1429 }
1430
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001431 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1432 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001433 input->output = NULL;
1434 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001435
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001437 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001438
1439 return;
1440 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001441
1442 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1443 weston_output_schedule_repaint(&input->output->base);
1444 } else {
1445 location = THEME_LOCATION_CLIENT_AREA;
1446 }
1447
Peter Hutterer87743e92016-01-18 16:38:22 +10001448 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001449 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001450 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1451 notify_pointer_frame(&input->base);
1452 }
Daniel Stone37816df2012-05-16 18:45:18 +01001453}
1454
1455static void
1456input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001457 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001458{
1459 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001460 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001461
Peter Hutterer89b6a492016-01-18 15:58:17 +10001462 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001463 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001464
Peter Hutterer87743e92016-01-18 16:38:22 +10001465 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1466 input->vert.has_discrete) {
1467 weston_event.has_discrete = true;
1468 weston_event.discrete = input->vert.discrete;
1469 input->vert.has_discrete = false;
1470 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1471 input->horiz.has_discrete) {
1472 weston_event.has_discrete = true;
1473 weston_event.discrete = input->horiz.discrete;
1474 input->horiz.has_discrete = false;
1475 }
1476
Peter Hutterer89b6a492016-01-18 15:58:17 +10001477 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001478
1479 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1480 notify_pointer_frame(&input->base);
1481}
1482
1483static void
1484input_handle_frame(void *data, struct wl_pointer *pointer)
1485{
1486 struct wayland_input *input = data;
1487
1488 notify_pointer_frame(&input->base);
1489}
1490
1491static void
1492input_handle_axis_source(void *data, struct wl_pointer *pointer,
1493 uint32_t source)
1494{
1495 struct wayland_input *input = data;
1496
1497 notify_axis_source(&input->base, source);
1498}
1499
1500static void
1501input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1502 uint32_t time, uint32_t axis)
1503{
1504 struct wayland_input *input = data;
1505 struct weston_pointer_axis_event weston_event;
1506
1507 weston_event.axis = axis;
1508 weston_event.value = 0;
1509
1510 notify_axis(&input->base, time, &weston_event);
1511}
1512
1513static void
1514input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1515 uint32_t axis, int32_t discrete)
1516{
1517 struct wayland_input *input = data;
1518
1519 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1520 input->vert.has_discrete = true;
1521 input->vert.discrete = discrete;
1522 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1523 input->horiz.has_discrete = true;
1524 input->horiz.discrete = discrete;
1525 }
Daniel Stone37816df2012-05-16 18:45:18 +01001526}
1527
1528static const struct wl_pointer_listener pointer_listener = {
1529 input_handle_pointer_enter,
1530 input_handle_pointer_leave,
1531 input_handle_motion,
1532 input_handle_button,
1533 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001534 input_handle_frame,
1535 input_handle_axis_source,
1536 input_handle_axis_stop,
1537 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001538};
1539
1540static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001541input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1542 int fd, uint32_t size)
1543{
1544 struct wayland_input *input = data;
1545 struct xkb_keymap *keymap;
1546 char *map_str;
1547
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001548 if (!data) {
1549 close(fd);
1550 return;
1551 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001552
1553 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1554 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1555 if (map_str == MAP_FAILED) {
1556 weston_log("mmap failed: %m\n");
1557 goto error;
1558 }
1559
Giulio Camuffo954f1832014-10-11 18:27:30 +03001560 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001561 map_str,
1562 XKB_KEYMAP_FORMAT_TEXT_V1,
1563 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001564 munmap(map_str, size);
1565
1566 if (!keymap) {
1567 weston_log("failed to compile keymap\n");
1568 goto error;
1569 }
1570
1571 input->keyboard_state_update = STATE_UPDATE_NONE;
1572 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1573 weston_log("No keymap provided; falling back to defalt\n");
1574 keymap = NULL;
1575 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1576 } else {
1577 weston_log("Invalid keymap\n");
1578 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001579 }
1580
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001581 close(fd);
1582
Derek Foreman1281a362015-07-31 16:55:32 -05001583 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001584 weston_seat_update_keymap(&input->base, keymap);
1585 else
1586 weston_seat_init_keyboard(&input->base, keymap);
1587
Ran Benitac9c74152014-08-19 23:59:52 +03001588 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001589
1590 return;
1591
1592error:
1593 wl_keyboard_release(input->parent.keyboard);
1594 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001595}
1596
1597static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001598input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001599 struct wl_keyboard *keyboard,
1600 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001601 struct wl_surface *surface,
1602 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001603{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001604 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001605 struct wayland_output *focus;
1606
1607 focus = input->keyboard_focus;
1608 if (focus) {
1609 /* This shouldn't happen */
1610 focus->keyboard_count--;
1611 if (!focus->keyboard_count && focus->frame)
1612 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1613 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1614 weston_output_schedule_repaint(&focus->base);
1615 }
1616
1617 input->keyboard_focus = wl_surface_get_user_data(surface);
1618 input->keyboard_focus->keyboard_count++;
1619
1620 focus = input->keyboard_focus;
1621 if (focus->frame) {
1622 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1623 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1624 weston_output_schedule_repaint(&focus->base);
1625 }
1626
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001627
Daniel Stone50692802012-06-22 13:21:41 +01001628 /* XXX: If we get a modifier event immediately before the focus,
1629 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001630 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001631 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001632}
1633
1634static void
1635input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001636 struct wl_keyboard *keyboard,
1637 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001638 struct wl_surface *surface)
1639{
1640 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001641 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001642
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001643 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001644
1645 focus = input->keyboard_focus;
1646 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001647 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001648
1649 focus->keyboard_count--;
1650 if (!focus->keyboard_count && focus->frame) {
1651 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1652 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1653 weston_output_schedule_repaint(&focus->base);
1654 }
1655
1656 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001657}
1658
Daniel Stone37816df2012-05-16 18:45:18 +01001659static void
1660input_handle_key(void *data, struct wl_keyboard *keyboard,
1661 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1662{
1663 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001664
Daniel Stone50692802012-06-22 13:21:41 +01001665 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001666 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001667 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001668 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001669 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001670}
1671
Daniel Stone351eb612012-05-31 15:27:47 -04001672static void
Derek Foreman1281a362015-07-31 16:55:32 -05001673input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001674 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001675 uint32_t mods_latched, uint32_t mods_locked,
1676 uint32_t group)
1677{
Derek Foreman1281a362015-07-31 16:55:32 -05001678 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001679 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001680 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001681 uint32_t serial_out;
1682
1683 /* If we get a key event followed by a modifier event with the
1684 * same serial number, then we try to preserve those semantics by
1685 * reusing the same serial number on the way out too. */
1686 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001687 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001688 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001689 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001690
Derek Foreman1281a362015-07-31 16:55:32 -05001691 keyboard = weston_seat_get_keyboard(&input->base);
1692 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001693 mods_depressed, mods_latched,
1694 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001695 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001696}
1697
Jonny Lamb497994a2014-08-12 14:58:26 +02001698static void
1699input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1700 int32_t rate, int32_t delay)
1701{
1702 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001703 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001704
Giulio Camuffo954f1832014-10-11 18:27:30 +03001705 b->compositor->kb_repeat_rate = rate;
1706 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001707}
1708
Daniel Stone37816df2012-05-16 18:45:18 +01001709static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001710 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001711 input_handle_keyboard_enter,
1712 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001713 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001714 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001715 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001716};
1717
1718static void
Derek Foreman748c6952015-11-06 15:56:10 -06001719input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1720 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001721 struct wl_surface *surface, int32_t id,
1722 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001723{
1724 struct wayland_input *input = data;
1725 struct wayland_output *output;
1726 enum theme_location location;
1727 bool first_touch;
1728 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001729 double x, y;
1730
1731 x = wl_fixed_to_double(fixed_x);
1732 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001733
1734 first_touch = (input->touch_points == 0);
1735 input->touch_points++;
1736
1737 input->touch_focus = wl_surface_get_user_data(surface);
1738 output = input->touch_focus;
1739 if (!first_touch && !input->touch_active)
1740 return;
1741
1742 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001743 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001744
1745 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001746 x -= fx;
1747 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001748
1749 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1750 weston_output_schedule_repaint(&output->base);
1751
1752 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1753 input->touch_points--;
1754 wl_shell_surface_move(output->parent.shell_surface,
1755 input->parent.seat, serial);
1756 frame_status_clear(output->frame,
1757 FRAME_STATUS_MOVE);
1758 return;
1759 }
1760
1761 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1762 return;
1763 }
1764
1765 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1766
1767 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1768 input->touch_active = true;
1769}
1770
1771static void
1772input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1773 uint32_t serial, uint32_t time, int32_t id)
1774{
1775 struct wayland_input *input = data;
1776 struct wayland_output *output = input->touch_focus;
1777 bool active = input->touch_active;
1778
1779 input->touch_points--;
1780 if (input->touch_points == 0) {
1781 input->touch_focus = NULL;
1782 input->touch_active = false;
1783 }
1784
1785 if (!output)
1786 return;
1787
1788 if (output->frame) {
1789 frame_touch_up(output->frame, input, id);
1790
1791 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1792 wayland_output_destroy(&output->base);
1793 input->touch_focus = NULL;
1794 input->keyboard_focus = NULL;
1795 if (wl_list_empty(&input->backend->compositor->output_list))
1796 weston_compositor_exit(input->backend->compositor);
1797
1798 return;
1799 }
1800 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1801 weston_output_schedule_repaint(&output->base);
1802 }
1803
1804 if (active)
1805 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1806}
1807
1808static void
1809input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001810 uint32_t time, int32_t id,
1811 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001812{
1813 struct wayland_input *input = data;
1814 struct wayland_output *output = input->touch_focus;
1815 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001816 double x, y;
1817
1818 x = wl_fixed_to_double(fixed_x);
1819 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001820
1821 if (!output || !input->touch_active)
1822 return;
1823
1824 if (output->frame) {
1825 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001826 x -= fx;
1827 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001828 }
1829
1830 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1831
1832 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1833}
1834
1835static void
1836input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1837{
1838 struct wayland_input *input = data;
1839
1840 if (!input->touch_focus || !input->touch_active)
1841 return;
1842
1843 notify_touch_frame(&input->base);
1844}
1845
1846static void
1847input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1848{
1849 struct wayland_input *input = data;
1850
1851 if (!input->touch_focus || !input->touch_active)
1852 return;
1853
1854 notify_touch_cancel(&input->base);
1855}
1856
1857static const struct wl_touch_listener touch_listener = {
1858 input_handle_touch_down,
1859 input_handle_touch_up,
1860 input_handle_touch_motion,
1861 input_handle_touch_frame,
1862 input_handle_touch_cancel,
1863};
1864
1865
1866static void
Daniel Stone37816df2012-05-16 18:45:18 +01001867input_handle_capabilities(void *data, struct wl_seat *seat,
1868 enum wl_seat_capability caps)
1869{
1870 struct wayland_input *input = data;
1871
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001872 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1873 input->parent.pointer = wl_seat_get_pointer(seat);
1874 wl_pointer_set_user_data(input->parent.pointer, input);
1875 wl_pointer_add_listener(input->parent.pointer,
1876 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001877 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001878 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001879 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1880 wl_pointer_release(input->parent.pointer);
1881 else
1882 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001883 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001884 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001885 }
1886
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001887 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1888 input->parent.keyboard = wl_seat_get_keyboard(seat);
1889 wl_keyboard_set_user_data(input->parent.keyboard, input);
1890 wl_keyboard_add_listener(input->parent.keyboard,
1891 &keyboard_listener, input);
1892 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001893 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1894 wl_keyboard_release(input->parent.keyboard);
1895 else
1896 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001897 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001898 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001899 }
Derek Foreman748c6952015-11-06 15:56:10 -06001900
1901 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1902 input->parent.touch = wl_seat_get_touch(seat);
1903 wl_touch_set_user_data(input->parent.touch, input);
1904 wl_touch_add_listener(input->parent.touch,
1905 &touch_listener, input);
1906 weston_seat_init_touch(&input->base);
1907 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1908 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1909 wl_touch_release(input->parent.touch);
1910 else
1911 wl_touch_destroy(input->parent.touch);
1912 input->parent.touch = NULL;
1913 weston_seat_release_touch(&input->base);
1914 }
Daniel Stone37816df2012-05-16 18:45:18 +01001915}
1916
Jonny Lamb497994a2014-08-12 14:58:26 +02001917static void
1918input_handle_name(void *data, struct wl_seat *seat,
1919 const char *name)
1920{
1921}
1922
Daniel Stone37816df2012-05-16 18:45:18 +01001923static const struct wl_seat_listener seat_listener = {
1924 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001925 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001926};
1927
1928static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001929display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001930{
1931 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001932 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001933
Peter Huttererf3d62272013-08-08 11:57:05 +10001934 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001935 if (input == NULL)
1936 return;
1937
Giulio Camuffo954f1832014-10-11 18:27:30 +03001938 weston_seat_init(&input->base, b->compositor, "default");
1939 input->backend = b;
1940 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001941 &wl_seat_interface, version);
1942 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001943 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001944
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001945 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1946 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001947
1948 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001949 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001950
1951 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1952 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001953}
1954
1955static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001956wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1957 int32_t x, int32_t y,
1958 int32_t physical_width, int32_t physical_height,
1959 int32_t subpixel, const char *make,
1960 const char *model, int32_t transform)
1961{
1962 struct wayland_parent_output *output = data;
1963
1964 output->x = x;
1965 output->y = y;
1966 output->physical.width = physical_width;
1967 output->physical.height = physical_height;
1968 output->physical.subpixel = subpixel;
1969
1970 free(output->physical.make);
1971 output->physical.make = strdup(make);
1972 free(output->physical.model);
1973 output->physical.model = strdup(model);
1974
1975 output->transform = transform;
1976}
1977
1978static struct weston_mode *
1979find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1980{
1981 struct weston_mode *mode;
1982
1983 wl_list_for_each(mode, list, link) {
1984 if (mode->width == width && mode->height == height &&
1985 mode->refresh == refresh)
1986 return mode;
1987 }
1988
1989 mode = zalloc(sizeof *mode);
1990 if (!mode)
1991 return NULL;
1992
1993 mode->width = width;
1994 mode->height = height;
1995 mode->refresh = refresh;
1996 wl_list_insert(list, &mode->link);
1997
1998 return mode;
1999}
2000
2001static void
2002wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2003 uint32_t flags, int32_t width, int32_t height,
2004 int32_t refresh)
2005{
2006 struct wayland_parent_output *output = data;
2007 struct weston_mode *mode;
2008
2009 if (output->output) {
2010 mode = find_mode(&output->output->base.mode_list,
2011 width, height, refresh);
2012 if (!mode)
2013 return;
2014 mode->flags = flags;
2015 /* Do a mode-switch on current mode change? */
2016 } else {
2017 mode = find_mode(&output->mode_list, width, height, refresh);
2018 if (!mode)
2019 return;
2020 mode->flags = flags;
2021 if (flags & WL_OUTPUT_MODE_CURRENT)
2022 output->current_mode = mode;
2023 if (flags & WL_OUTPUT_MODE_PREFERRED)
2024 output->preferred_mode = mode;
2025 }
2026}
2027
2028static const struct wl_output_listener output_listener = {
2029 wayland_parent_output_geometry,
2030 wayland_parent_output_mode
2031};
2032
2033static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002034wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002035{
2036 struct wayland_parent_output *output;
2037
2038 output = zalloc(sizeof *output);
2039 if (!output)
2040 return;
2041
2042 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002043 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002044 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002045 if (!output->global) {
2046 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002047 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002048 }
2049
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002050 wl_output_add_listener(output->global, &output_listener, output);
2051
2052 output->scale = 0;
2053 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2054 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2055 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002056 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002057
Giulio Camuffo954f1832014-10-11 18:27:30 +03002058 if (b->sprawl_across_outputs) {
2059 wl_display_roundtrip(b->parent.wl_display);
2060 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002061 }
2062}
2063
2064static void
2065wayland_parent_output_destroy(struct wayland_parent_output *output)
2066{
2067 struct weston_mode *mode, *next;
2068
2069 if (output->output)
2070 wayland_output_destroy(&output->output->base);
2071
2072 wl_output_destroy(output->global);
2073 free(output->physical.make);
2074 free(output->physical.model);
2075
2076 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2077 wl_list_remove(&mode->link);
2078 free(mode);
2079 }
2080}
2081
2082static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002083registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2084 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002085{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002086 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002087
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002088 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002089 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002090 wl_registry_bind(registry, name,
2091 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002092 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002093 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002094 wl_registry_bind(registry, name,
2095 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002096 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002097 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002098 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002099 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002100 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002101 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002102 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002103 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002104 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002105 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002106 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002107 }
2108}
2109
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002110static void
2111registry_handle_global_remove(void *data, struct wl_registry *registry,
2112 uint32_t name)
2113{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002114 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002115 struct wayland_parent_output *output;
2116
Giulio Camuffo954f1832014-10-11 18:27:30 +03002117 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002118 if (output->id == name)
2119 wayland_parent_output_destroy(output);
2120}
2121
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002122static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002123 registry_handle_global,
2124 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002125};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002126
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002127static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002128wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002129{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002130 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002131 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002132
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002133 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002134 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002135 return 0;
2136 }
2137
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002138 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002139 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002140 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002141 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002142
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002143 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002144 count = wl_display_dispatch_pending(b->parent.wl_display);
2145 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002146 }
2147
2148 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002149}
2150
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002151static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002152wayland_restore(struct weston_compositor *ec)
2153{
2154}
2155
2156static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002157wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002158{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002159 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002160
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002161 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002162
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163 if (b->parent.shm)
2164 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002165
Giulio Camuffo954f1832014-10-11 18:27:30 +03002166 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002167}
2168
Jason Ekstrand7744f712013-10-27 22:24:55 -05002169static const char *left_ptrs[] = {
2170 "left_ptr",
2171 "default",
2172 "top_left_arrow",
2173 "left-arrow"
2174};
2175
2176static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002177create_cursor(struct wayland_backend *b, struct weston_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002178{
2179 struct weston_config_section *s;
2180 int size;
2181 char *theme = NULL;
2182 unsigned int i;
2183
2184 s = weston_config_get_section(config, "shell", NULL, NULL);
2185 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
2186 weston_config_section_get_int(s, "cursor-size", &size, 32);
2187
Giulio Camuffo954f1832014-10-11 18:27:30 +03002188 b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
2189 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002190 fprintf(stderr, "could not load cursor theme\n");
2191 return;
2192 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002193
U. Artie Eoffff755002014-01-17 12:36:58 -08002194 free(theme);
2195
Giulio Camuffo954f1832014-10-11 18:27:30 +03002196 b->cursor = NULL;
2197 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2198 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002199 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002200 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002201 fprintf(stderr, "could not load left cursor\n");
2202 return;
2203 }
2204}
2205
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002206static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002207fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2208 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002209{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002210 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002211 struct wayland_input *input = NULL;
2212
Giulio Camuffo954f1832014-10-11 18:27:30 +03002213 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002214 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002215 break;
2216
2217 if (!input || !input->output)
2218 return;
2219
2220 if (input->output->frame)
2221 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2222 else
2223 wayland_output_set_windowed(input->output);
2224
2225 weston_output_schedule_repaint(&input->output->base);
2226}
2227
Giulio Camuffo954f1832014-10-11 18:27:30 +03002228static struct wayland_backend *
2229wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
2230 const char *display_name, int *argc, char *argv[],
2231 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002232{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002233 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002234 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002235 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002236
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237 b = zalloc(sizeof *b);
2238 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002239 return NULL;
2240
Giulio Camuffo954f1832014-10-11 18:27:30 +03002241 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002242 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002243 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002244
Giulio Camuffo954f1832014-10-11 18:27:30 +03002245 b->parent.wl_display = wl_display_connect(display_name);
2246 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002247 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002248 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002249 }
2250
Giulio Camuffo954f1832014-10-11 18:27:30 +03002251 wl_list_init(&b->parent.output_list);
2252 wl_list_init(&b->input_list);
2253 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2254 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2255 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002256
Giulio Camuffo954f1832014-10-11 18:27:30 +03002257 create_cursor(b, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002258
Giulio Camuffo954f1832014-10-11 18:27:30 +03002259 b->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002260
Giulio Camuffo954f1832014-10-11 18:27:30 +03002261 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002262 gl_renderer = weston_load_module("gl-renderer.so",
2263 "gl_renderer_interface");
2264 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002265 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002266 }
2267
Giulio Camuffo954f1832014-10-11 18:27:30 +03002268 if (!b->use_pixman) {
2269 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002270 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002271 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002272 gl_renderer->alpha_attribs,
2273 NULL,
2274 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002275 weston_log("Failed to initialize the GL renderer; "
2276 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002277 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002278 }
2279 }
2280
Giulio Camuffo954f1832014-10-11 18:27:30 +03002281 if (b->use_pixman) {
2282 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002283 weston_log("Failed to initialize pixman renderer\n");
2284 goto err_display;
2285 }
2286 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002287
Giulio Camuffo954f1832014-10-11 18:27:30 +03002288 b->base.destroy = wayland_destroy;
2289 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002290
Giulio Camuffo954f1832014-10-11 18:27:30 +03002291 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002292
Giulio Camuffo954f1832014-10-11 18:27:30 +03002293 fd = wl_display_get_fd(b->parent.wl_display);
2294 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002295 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002296 wayland_backend_handle_event, b);
2297 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002298 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002299
Giulio Camuffo954f1832014-10-11 18:27:30 +03002300 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002301
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002302 if (compositor->renderer->import_dmabuf) {
2303 if (linux_dmabuf_setup(compositor) < 0)
2304 weston_log("Error: initializing dmabuf "
2305 "support failed.\n");
2306 }
2307
Giulio Camuffo954f1832014-10-11 18:27:30 +03002308 compositor->backend = &b->base;
2309 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002310err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002312err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002314 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002315 return NULL;
2316}
2317
2318static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002319wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002320{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002321 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002322
Giulio Camuffo954f1832014-10-11 18:27:30 +03002323 if (b->theme)
2324 theme_destroy(b->theme);
2325 if (b->frame_device)
2326 cairo_device_destroy(b->frame_device);
2327 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002328
Giulio Camuffo954f1832014-10-11 18:27:30 +03002329 weston_compositor_shutdown(b->compositor);
2330 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002331}
2332
Giulio Camuffo954f1832014-10-11 18:27:30 +03002333WL_EXPORT int
2334backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002335 struct weston_config *config,
2336 struct weston_backend_config *config_base)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002337{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002338 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002339 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002340 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002341 struct weston_config_section *section;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002342 struct weston_wayland_backend_config new_config;
2343 int x, count, width, height, scale;
2344 const char *section_name;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002345 char *name;
2346
2347 const struct weston_option wayland_options[] = {
2348 { WESTON_OPTION_INTEGER, "width", 0, &width },
2349 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002350 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002351 { WESTON_OPTION_STRING, "display", 0, &new_config.display_name },
2352 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &new_config.use_pixman },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002353 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002354 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &new_config.fullscreen },
2355 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &new_config.sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002356 };
2357
2358 width = 0;
2359 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002360 scale = 0;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002361 new_config.display_name = NULL;
2362 new_config.use_pixman = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002363 count = 1;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002364 new_config.fullscreen = 0;
2365 new_config.sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002366 parse_options(wayland_options,
2367 ARRAY_LENGTH(wayland_options), argc, argv);
2368
Benoit Gschwind37a68072016-04-28 20:33:08 +02002369 b = wayland_backend_create(compositor, new_config.use_pixman,
2370 new_config.display_name, argc, argv, config);
2371
Giulio Camuffo954f1832014-10-11 18:27:30 +03002372 if (!b)
2373 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002374
Benoit Gschwind37a68072016-04-28 20:33:08 +02002375 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002376 b->sprawl_across_outputs = 1;
2377 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002378
Giulio Camuffo954f1832014-10-11 18:27:30 +03002379 wl_list_for_each(poutput, &b->parent.output_list, link)
2380 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002381
Giulio Camuffo954f1832014-10-11 18:27:30 +03002382 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002383 }
2384
Benoit Gschwind37a68072016-04-28 20:33:08 +02002385 if (new_config.fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002386 output = wayland_output_create(b, 0, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002387 NULL, 1, 0, 1);
2388 if (!output)
2389 goto err_outputs;
2390
Axel Davydd8b88d2013-11-17 21:34:16 +01002391 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002392 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002393 }
2394
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002395 section = NULL;
2396 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002397 while (weston_config_next_section(config, &section, &section_name)) {
2398 if (!section_name || strcmp(section_name, "output") != 0)
2399 continue;
2400 weston_config_section_get_string(section, "name", &name, NULL);
2401 if (name == NULL)
2402 continue;
2403
2404 if (name[0] != 'W' || name[1] != 'L') {
2405 free(name);
2406 continue;
2407 }
2408 free(name);
2409
Giulio Camuffo954f1832014-10-11 18:27:30 +03002410 output = wayland_output_create_for_config(b, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002411 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002412 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002413 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002414 if (wayland_output_set_windowed(output))
2415 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002416
2417 x += output->base.width;
2418 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002419 }
2420
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002421 if (!width)
2422 width = 1024;
2423 if (!height)
2424 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002425 if (!scale)
2426 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002427 while (count > 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002428 output = wayland_output_create(b, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002429 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002430 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002431 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002432 if (wayland_output_set_windowed(output))
2433 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002434
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002435 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002436 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002437 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002438
Giulio Camuffo954f1832014-10-11 18:27:30 +03002439 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002440 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002441 fullscreen_binding, b);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002442
Giulio Camuffo954f1832014-10-11 18:27:30 +03002443 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002444
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002445err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002446 wayland_backend_destroy(b);
2447 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002448}