blob: 175bd686bb0015cd670d38e8c04783d817513b7b [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;
Benoit Gschwind244ff792016-04-28 20:33:11 +020060 char *cursor_theme;
61 int cursor_size;
Benoit Gschwind37a68072016-04-28 20:33:08 +020062};
63
Giulio Camuffo954f1832014-10-11 18:27:30 +030064struct wayland_backend {
65 struct weston_backend base;
66 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067
68 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040069 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040070 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010071 struct wl_compositor *compositor;
72 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080073 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020074 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050076 struct wl_list output_list;
77
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078 struct wl_event_source *wl_source;
79 uint32_t event_mask;
80 } parent;
81
Jason Ekstrandff2fd462013-10-27 22:24:58 -050082 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050083 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050084
Jason Ekstrand7744f712013-10-27 22:24:55 -050085 struct theme *theme;
86 cairo_device_t *frame_device;
87 struct wl_cursor_theme *cursor_theme;
88 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050089
Jason Ekstrand06ced802013-11-07 20:13:29 -060090 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010091};
92
93struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050094 struct weston_output base;
95
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050097 int draw_initial_frame;
98 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050099
100 struct wl_output *output;
101 uint32_t global_id;
102
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500103 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600104 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100105 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500106
Jason Ekstrand7744f712013-10-27 22:24:55 -0500107 int keyboard_count;
108
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600109 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500110 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500111
Jason Ekstrand7744f712013-10-27 22:24:55 -0500112 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500113 struct wl_egl_window *egl_window;
114 struct {
115 cairo_surface_t *top;
116 cairo_surface_t *left;
117 cairo_surface_t *right;
118 cairo_surface_t *bottom;
119 } border;
120 } gl;
121
122 struct {
123 struct wl_list buffers;
124 struct wl_list free_buffers;
125 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500126
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500127 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500128 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100129};
130
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500131struct wayland_parent_output {
132 struct wayland_output *output;
133 struct wl_list link;
134
135 struct wl_output *global;
136 uint32_t id;
137
138 struct {
139 char *make;
140 char *model;
141 int32_t width, height;
142 uint32_t subpixel;
143 } physical;
144
145 int32_t x, y;
146 uint32_t transform;
147 uint32_t scale;
148
149 struct wl_list mode_list;
150 struct weston_mode *preferred_mode;
151 struct weston_mode *current_mode;
152};
153
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500154struct wayland_shm_buffer {
155 struct wayland_output *output;
156 struct wl_list link;
157 struct wl_list free_link;
158
159 struct wl_buffer *buffer;
160 void *data;
161 size_t size;
162 pixman_region32_t damage;
163 int frame_damaged;
164
165 pixman_image_t *pm_image;
166 cairo_surface_t *c_surface;
167};
168
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100169struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400170 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300171 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100172 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500173
174 struct {
175 struct wl_seat *seat;
176 struct wl_pointer *pointer;
177 struct wl_keyboard *keyboard;
178 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500179
180 struct {
181 struct wl_surface *surface;
182 int32_t hx, hy;
183 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500184 } parent;
185
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500186 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100187 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400188 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600189 uint32_t touch_points;
190 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600191 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600192 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600193
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400194 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600195 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500196 struct wayland_output *keyboard_focus;
Peter Hutterer87743e92016-01-18 16:38:22 +1000197
198 struct weston_pointer_axis_event vert, horiz;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100199};
200
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300201struct gl_renderer_interface *gl_renderer;
202
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500203static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500204wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
205{
206 cairo_surface_destroy(buffer->c_surface);
207 pixman_image_unref(buffer->pm_image);
208
209 wl_buffer_destroy(buffer->buffer);
210 munmap(buffer->data, buffer->size);
211
212 pixman_region32_fini(&buffer->damage);
213
214 wl_list_remove(&buffer->link);
215 wl_list_remove(&buffer->free_link);
216 free(buffer);
217}
218
219static void
220buffer_release(void *data, struct wl_buffer *buffer)
221{
222 struct wayland_shm_buffer *sb = data;
223
224 if (sb->output) {
225 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
226 } else {
227 wayland_shm_buffer_destroy(sb);
228 }
229}
230
231static const struct wl_buffer_listener buffer_listener = {
232 buffer_release
233};
234
235static struct wayland_shm_buffer *
236wayland_output_get_shm_buffer(struct wayland_output *output)
237{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300238 struct wayland_backend *b =
239 (struct wayland_backend *) output->base.compositor->backend;
240 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500241 struct wayland_shm_buffer *sb;
242
243 struct wl_shm_pool *pool;
244 int width, height, stride;
245 int32_t fx, fy;
246 int fd;
247 unsigned char *data;
248
249 if (!wl_list_empty(&output->shm.free_buffers)) {
250 sb = container_of(output->shm.free_buffers.next,
251 struct wayland_shm_buffer, free_link);
252 wl_list_remove(&sb->free_link);
253 wl_list_init(&sb->free_link);
254
255 return sb;
256 }
257
258 if (output->frame) {
259 width = frame_width(output->frame);
260 height = frame_height(output->frame);
261 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500262 width = output->base.current_mode->width;
263 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500264 }
265
266 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
267
268 fd = os_create_anonymous_file(height * stride);
269 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000270 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500271 return NULL;
272 }
273
274 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
275 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000276 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500277 close(fd);
278 return NULL;
279 }
280
281 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000282 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200283 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000284 close(fd);
285 free(data);
286 return NULL;
287 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500288
289 sb->output = output;
290 wl_list_init(&sb->free_link);
291 wl_list_insert(&output->shm.buffers, &sb->link);
292
293 pixman_region32_init_rect(&sb->damage, 0, 0,
294 output->base.width, output->base.height);
295 sb->frame_damaged = 1;
296
297 sb->data = data;
298 sb->size = height * stride;
299
300 pool = wl_shm_create_pool(shm, fd, sb->size);
301
302 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
303 width, height,
304 stride,
305 WL_SHM_FORMAT_ARGB8888);
306 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
307 wl_shm_pool_destroy(pool);
308 close(fd);
309
310 memset(data, 0, sb->size);
311
312 sb->c_surface =
313 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
314 width, height, stride);
315
316 fx = 0;
317 fy = 0;
318 if (output->frame)
319 frame_interior(output->frame, &fx, &fy, 0, 0);
320 sb->pm_image =
321 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
322 (uint32_t *)(data + fy * stride) + fx,
323 stride);
324
325 return sb;
326}
327
328static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500329frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400330{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500331 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400332 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400333
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500334 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400335
336 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300337
338 /*
339 * This is the fallback case, where Presentation extension is not
340 * available from the parent compositor. We do not know the base for
341 * 'time', so we cannot feed it to finish_frame(). Do the only thing
342 * we can, and pretend finish_frame time is when we process this
343 * event.
344 */
345 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200346 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400347}
348
349static const struct wl_callback_listener frame_listener = {
350 frame_done
351};
352
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500353static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200354draw_initial_frame(struct wayland_output *output)
355{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500356 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200357
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500358 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200359
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500360 /* If we are rendering with GL, then orphan it so that it gets
361 * destroyed immediately */
362 if (output->gl.egl_window)
363 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500364
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500365 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500366 wl_surface_damage(output->parent.surface, 0, 0,
367 output->base.current_mode->width,
368 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200369}
370
371static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372wayland_output_update_gl_border(struct wayland_output *output)
373{
374 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
375 cairo_t *cr;
376
377 if (!output->frame)
378 return;
379 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
380 return;
381
382 fwidth = frame_width(output->frame);
383 fheight = frame_height(output->frame);
384 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
385
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500386 if (!output->gl.border.top)
387 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500388 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
389 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500390 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500391 frame_repaint(output->frame, cr);
392 cairo_destroy(cr);
393 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
394 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 cairo_image_surface_get_stride(output->gl.border.top) / 4,
396 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500397
398
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500399 if (!output->gl.border.left)
400 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500401 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
402 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500403 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500404 cairo_translate(cr, 0, -iy);
405 frame_repaint(output->frame, cr);
406 cairo_destroy(cr);
407 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
408 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 cairo_image_surface_get_stride(output->gl.border.left) / 4,
410 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500411
412
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500413 if (!output->gl.border.right)
414 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500415 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
416 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500417 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500418 cairo_translate(cr, -(iwidth + ix), -iy);
419 frame_repaint(output->frame, cr);
420 cairo_destroy(cr);
421 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
422 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500423 cairo_image_surface_get_stride(output->gl.border.right) / 4,
424 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500425
426
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500427 if (!output->gl.border.bottom)
428 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500429 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
430 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500431 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500432 cairo_translate(cr, 0, -(iy + iheight));
433 frame_repaint(output->frame, cr);
434 cairo_destroy(cr);
435 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
436 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500437 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
438 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500439}
440
441static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200442wayland_output_start_repaint_loop(struct weston_output *output_base)
443{
444 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300445 struct wayland_backend *wb =
446 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200447 struct wl_callback *callback;
448
449 /* If this is the initial frame, we need to attach a buffer so that
450 * the compositor can map the surface and include it in its render
451 * loop. If the surface doesn't end up in the render loop, the frame
452 * callback won't be invoked. The buffer is transparent and of the
453 * same size as the future real output buffer. */
454 if (output->parent.draw_initial_frame) {
455 output->parent.draw_initial_frame = 0;
456
457 draw_initial_frame(output);
458 }
459
460 callback = wl_surface_frame(output->parent.surface);
461 wl_callback_add_listener(callback, &frame_listener, output);
462 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300463 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200464}
465
David Herrmann1edf44c2013-10-22 17:11:26 +0200466static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500467wayland_output_repaint_gl(struct weston_output *output_base,
468 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400469{
470 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400471 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400472 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600473
Kristian Høgsberg33418202011-08-16 23:01:28 -0400474 callback = wl_surface_frame(output->parent.surface);
475 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100476
Jason Ekstrand7744f712013-10-27 22:24:55 -0500477 wayland_output_update_gl_border(output);
478
Pekka Paalanenbc106382012-10-10 12:49:31 +0300479 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200480
481 pixman_region32_subtract(&ec->primary_plane.damage,
482 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200483 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100484}
485
Matt Roper361d2ad2011-08-29 13:52:23 -0700486static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500487wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
488{
489 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
490 cairo_t *cr;
491
492 if (!buffer->output->frame || !buffer->frame_damaged)
493 return;
494
495 cr = cairo_create(buffer->c_surface);
496
497 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
498 fwidth = frame_width(buffer->output->frame);
499 fheight = frame_height(buffer->output->frame);
500
501 /* Set the clip so we don't unnecisaraly damage the surface */
502 cairo_move_to(cr, ix, iy);
503 cairo_rel_line_to(cr, iwidth, 0);
504 cairo_rel_line_to(cr, 0, iheight);
505 cairo_rel_line_to(cr, -iwidth, 0);
506 cairo_line_to(cr, ix, iy);
507 cairo_line_to(cr, 0, iy);
508 cairo_line_to(cr, 0, fheight);
509 cairo_line_to(cr, fwidth, fheight);
510 cairo_line_to(cr, fwidth, 0);
511 cairo_line_to(cr, 0, 0);
512 cairo_line_to(cr, 0, iy);
513 cairo_close_path(cr);
514 cairo_clip(cr);
515
516 /* Draw using a pattern so that the final result gets clipped */
517 cairo_push_group(cr);
518 frame_repaint(buffer->output->frame, cr);
519 cairo_pop_group_to_source(cr);
520 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
521 cairo_paint(cr);
522
523 cairo_destroy(cr);
524}
525
526static void
527wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
528{
529 pixman_region32_t damage;
530 pixman_box32_t *rects;
531 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
532 int i, n;
533
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500534 pixman_region32_init(&damage);
535 weston_transformed_region(sb->output->base.width,
536 sb->output->base.height,
537 sb->output->base.transform,
538 sb->output->base.current_scale,
539 &sb->damage, &damage);
540
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500541 if (sb->output->frame) {
542 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
543 fwidth = frame_width(sb->output->frame);
544 fheight = frame_height(sb->output->frame);
545
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500546 pixman_region32_translate(&damage, ix, iy);
547
548 if (sb->frame_damaged) {
549 pixman_region32_union_rect(&damage, &damage,
550 0, 0, fwidth, iy);
551 pixman_region32_union_rect(&damage, &damage,
552 0, iy, ix, iheight);
553 pixman_region32_union_rect(&damage, &damage,
554 ix + iwidth, iy,
555 fwidth - (ix + iwidth), iheight);
556 pixman_region32_union_rect(&damage, &damage,
557 0, iy + iheight,
558 fwidth, fheight - (iy + iheight));
559 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500560 }
561
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500562 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500563 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
564 for (i = 0; i < n; ++i)
565 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
566 rects[i].y1, rects[i].x2 - rects[i].x1,
567 rects[i].y2 - rects[i].y1);
568
569 if (sb->output->frame)
570 pixman_region32_fini(&damage);
571}
572
573static int
574wayland_output_repaint_pixman(struct weston_output *output_base,
575 pixman_region32_t *damage)
576{
577 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300578 struct wayland_backend *b =
579 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500580 struct wl_callback *callback;
581 struct wayland_shm_buffer *sb;
582
583 if (output->frame) {
584 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
585 wl_list_for_each(sb, &output->shm.buffers, link)
586 sb->frame_damaged = 1;
587 }
588
589 wl_list_for_each(sb, &output->shm.buffers, link)
590 pixman_region32_union(&sb->damage, &sb->damage, damage);
591
592 sb = wayland_output_get_shm_buffer(output);
593
594 wayland_output_update_shm_border(sb);
595 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300596 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500597
598 wayland_shm_buffer_attach(sb);
599
600 callback = wl_surface_frame(output->parent.surface);
601 wl_callback_add_listener(callback, &frame_listener, output);
602 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500604
605 pixman_region32_fini(&sb->damage);
606 pixman_region32_init(&sb->damage);
607 sb->frame_damaged = 0;
608
Giulio Camuffo954f1832014-10-11 18:27:30 +0300609 pixman_region32_subtract(&b->compositor->primary_plane.damage,
610 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500611 return 0;
612}
613
614static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500615wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700616{
617 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300618 struct wayland_backend *b =
619 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700620
Giulio Camuffo954f1832014-10-11 18:27:30 +0300621 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500622 pixman_renderer_output_destroy(output_base);
623 } else {
624 gl_renderer->output_destroy(output_base);
625 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100626
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500627 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500628 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500629 if (output->parent.shell_surface)
630 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500631
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600632 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500633 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600634
635 cairo_surface_destroy(output->gl.border.top);
636 cairo_surface_destroy(output->gl.border.left);
637 cairo_surface_destroy(output->gl.border.right);
638 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500639
640 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700641 free(output);
642
643 return;
644}
645
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300646static const struct wl_shell_surface_listener shell_surface_listener;
647
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200648static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500649wayland_output_init_gl_renderer(struct wayland_output *output)
650{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500651 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500652
653 if (output->frame) {
654 fwidth = frame_width(output->frame);
655 fheight = frame_height(output->frame);
656 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500657 fwidth = output->base.current_mode->width;
658 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500659 }
660
661 output->gl.egl_window =
662 wl_egl_window_create(output->parent.surface,
663 fwidth, fheight);
664 if (!output->gl.egl_window) {
665 weston_log("failure to create wl_egl_window\n");
666 return -1;
667 }
668
669 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000670 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100671 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000672 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500673 NULL,
674 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500675 goto cleanup_window;
676
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500677 return 0;
678
679cleanup_window:
680 wl_egl_window_destroy(output->gl.egl_window);
681 return -1;
682}
683
684static int
685wayland_output_init_pixman_renderer(struct wayland_output *output)
686{
687 return pixman_renderer_output_create(&output->base);
688}
689
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600690static void
691wayland_output_resize_surface(struct wayland_output *output)
692{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300693 struct wayland_backend *b =
694 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600695 struct wayland_shm_buffer *buffer, *next;
696 int32_t ix, iy, iwidth, iheight;
697 int32_t width, height;
698 struct wl_region *region;
699
700 width = output->base.current_mode->width;
701 height = output->base.current_mode->height;
702
703 if (output->frame) {
704 frame_resize_inside(output->frame, width, height);
705
706 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300707 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600708 wl_region_add(region, ix, iy, iwidth, iheight);
709 wl_surface_set_input_region(output->parent.surface, region);
710 wl_region_destroy(region);
711
712 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300713 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600714 wl_region_add(region, ix, iy, iwidth, iheight);
715 wl_surface_set_opaque_region(output->parent.surface, region);
716 wl_region_destroy(region);
717
718 width = frame_width(output->frame);
719 height = frame_height(output->frame);
720 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300721 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600722 wl_region_add(region, 0, 0, width, height);
723 wl_surface_set_input_region(output->parent.surface, region);
724 wl_region_destroy(region);
725
Giulio Camuffo954f1832014-10-11 18:27:30 +0300726 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600727 wl_region_add(region, 0, 0, width, height);
728 wl_surface_set_opaque_region(output->parent.surface, region);
729 wl_region_destroy(region);
730 }
731
732 if (output->gl.egl_window) {
733 wl_egl_window_resize(output->gl.egl_window,
734 width, height, 0, 0);
735
736 /* These will need to be re-created due to the resize */
737 gl_renderer->output_set_border(&output->base,
738 GL_RENDERER_BORDER_TOP,
739 0, 0, 0, NULL);
740 cairo_surface_destroy(output->gl.border.top);
741 output->gl.border.top = NULL;
742 gl_renderer->output_set_border(&output->base,
743 GL_RENDERER_BORDER_LEFT,
744 0, 0, 0, NULL);
745 cairo_surface_destroy(output->gl.border.left);
746 output->gl.border.left = NULL;
747 gl_renderer->output_set_border(&output->base,
748 GL_RENDERER_BORDER_RIGHT,
749 0, 0, 0, NULL);
750 cairo_surface_destroy(output->gl.border.right);
751 output->gl.border.right = NULL;
752 gl_renderer->output_set_border(&output->base,
753 GL_RENDERER_BORDER_BOTTOM,
754 0, 0, 0, NULL);
755 cairo_surface_destroy(output->gl.border.bottom);
756 output->gl.border.bottom = NULL;
757 }
758
759 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500760 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600761 wayland_shm_buffer_destroy(buffer);
762 /* These will get thrown away when they get released */
763 wl_list_for_each(buffer, &output->shm.buffers, link)
764 buffer->output = NULL;
765}
766
767static int
768wayland_output_set_windowed(struct wayland_output *output)
769{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300770 struct wayland_backend *b =
771 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600772 int tlen;
773 char *title;
774
775 if (output->frame)
776 return 0;
777
778 if (output->name) {
779 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
780 title = malloc(tlen + 1);
781 if (!title)
782 return -1;
783
784 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
785 } else {
786 title = strdup(WINDOW_TITLE);
787 }
788
Giulio Camuffo954f1832014-10-11 18:27:30 +0300789 if (!b->theme) {
790 b->theme = theme_create();
791 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600792 free(title);
793 return -1;
794 }
795 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300796 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600797 FRAME_BUTTON_CLOSE, title);
798 free(title);
799 if (!output->frame)
800 return -1;
801
802 if (output->keyboard_count)
803 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
804
805 wayland_output_resize_surface(output);
806
807 wl_shell_surface_set_toplevel(output->parent.shell_surface);
808
809 return 0;
810}
811
812static void
813wayland_output_set_fullscreen(struct wayland_output *output,
814 enum wl_shell_surface_fullscreen_method method,
815 uint32_t framerate, struct wl_output *target)
816{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300817 struct wayland_backend *b =
818 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500819
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600820 if (output->frame) {
821 frame_destroy(output->frame);
822 output->frame = NULL;
823 }
824
825 wayland_output_resize_surface(output);
826
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500827 if (output->parent.shell_surface) {
828 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
829 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300830 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800831 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
832 output->parent.surface,
833 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500834 }
835}
836
837static struct weston_mode *
838wayland_output_choose_mode(struct wayland_output *output,
839 struct weston_mode *ref_mode)
840{
841 struct weston_mode *mode;
842
843 /* First look for an exact match */
844 wl_list_for_each(mode, &output->base.mode_list, link)
845 if (mode->width == ref_mode->width &&
846 mode->height == ref_mode->height &&
847 mode->refresh == ref_mode->refresh)
848 return mode;
849
850 /* If we can't find an exact match, ignore refresh and try again */
851 wl_list_for_each(mode, &output->base.mode_list, link)
852 if (mode->width == ref_mode->width &&
853 mode->height == ref_mode->height)
854 return mode;
855
856 /* Yeah, we failed */
857 return NULL;
858}
859
860enum mode_status {
861 MODE_STATUS_UNKNOWN,
862 MODE_STATUS_SUCCESS,
863 MODE_STATUS_FAIL,
864 MODE_STATUS_CANCEL,
865};
866
867static void
868mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800869 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500870{
871 enum mode_status *value = data;
872
873 printf("Mode switch successful\n");
874
875 *value = MODE_STATUS_SUCCESS;
876}
877
878static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800879mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500880{
881 enum mode_status *value = data;
882
883 printf("Mode switch failed\n");
884
885 *value = MODE_STATUS_FAIL;
886}
887
888static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800889mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500890{
891 enum mode_status *value = data;
892
893 printf("Mode switch cancelled\n");
894
895 *value = MODE_STATUS_CANCEL;
896}
897
Jonas Ådahl496adb32015-11-17 16:00:27 +0800898struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500899 mode_feedback_successful,
900 mode_feedback_failed,
901 mode_feedback_cancelled,
902};
903
904static int
905wayland_output_switch_mode(struct weston_output *output_base,
906 struct weston_mode *mode)
907{
908 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300909 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500910 struct wl_surface *old_surface;
911 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800912 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500913 enum mode_status mode_status;
914 int ret = 0;
915
916 if (output_base == NULL) {
917 weston_log("output is NULL.\n");
918 return -1;
919 }
920
921 if (mode == NULL) {
922 weston_log("mode is NULL.\n");
923 return -1;
924 }
925
Giulio Camuffo954f1832014-10-11 18:27:30 +0300926 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500927
Giulio Camuffo954f1832014-10-11 18:27:30 +0300928 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500929 return -1;
930
931 mode = wayland_output_choose_mode(output, mode);
932 if (mode == NULL)
933 return -1;
934
935 if (output->base.current_mode == mode)
936 return 0;
937
938 old_mode = output->base.current_mode;
939 old_surface = output->parent.surface;
940 output->base.current_mode = mode;
941 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300942 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500943 wl_surface_set_user_data(output->parent.surface, output);
944
945 /* Blow the old buffers because we changed size/surfaces */
946 wayland_output_resize_surface(output);
947
948 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800949 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
950 output->parent.surface,
951 output->parent.output,
952 mode->refresh);
953 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
954 &mode_feedback_listener,
955 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500956
957 /* This should kick-start things again */
958 output->parent.draw_initial_frame = 1;
959 wayland_output_start_repaint_loop(&output->base);
960
961 mode_status = MODE_STATUS_UNKNOWN;
962 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300963 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500964
Jonas Ådahl496adb32015-11-17 16:00:27 +0800965 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500966
967 if (mode_status == MODE_STATUS_FAIL) {
968 output->base.current_mode = old_mode;
969 wl_surface_destroy(output->parent.surface);
970 output->parent.surface = old_surface;
971 wayland_output_resize_surface(output);
972
973 return -1;
974 }
975
976 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
977 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
978
Giulio Camuffo954f1832014-10-11 18:27:30 +0300979 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500980 pixman_renderer_output_destroy(output_base);
981 if (wayland_output_init_pixman_renderer(output) < 0)
982 goto err_output;
983 } else {
984 gl_renderer->output_destroy(output_base);
985 wl_egl_window_destroy(output->gl.egl_window);
986 if (wayland_output_init_gl_renderer(output) < 0)
987 goto err_output;
988 }
989 wl_surface_destroy(old_surface);
990
991 weston_output_schedule_repaint(&output->base);
992
993 return 0;
994
995err_output:
996 /* XXX */
997 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600998}
999
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001000static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001001wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001002 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001003 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001004{
1005 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001006 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007
1008 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
1009 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001010
Peter Huttererf3d62272013-08-08 11:57:05 +10001011 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001012 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001013 return NULL;
1014
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001015 output->name = name ? strdup(name) : NULL;
1016 output->base.make = "waywayland";
1017 output->base.model = "none";
1018
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001019 output_width = width * scale;
1020 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001021
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001022 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001023 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001024 if (!output->parent.surface)
1025 goto err_name;
1026 wl_surface_set_user_data(output->parent.surface, output);
1027
1028 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001029
Giulio Camuffo954f1832014-10-11 18:27:30 +03001030 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001031 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001032 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001033 output->parent.surface);
1034 if (!output->parent.shell_surface)
1035 goto err_surface;
1036 wl_shell_surface_add_listener(output->parent.shell_surface,
1037 &shell_surface_listener, output);
1038 }
1039
Giulio Camuffo954f1832014-10-11 18:27:30 +03001040 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001041 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1042 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001043 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001044 if (!width)
1045 output_width = output->parent.configure_width;
1046 if (!height)
1047 output_height = output->parent.configure_height;
1048 }
1049
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001050 output->mode.flags =
1051 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001052 output->mode.width = output_width;
1053 output->mode.height = output_height;
1054 output->mode.refresh = 60000;
1055 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001056 wl_list_init(&output->base.mode_list);
1057 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001058 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001059
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001060 wl_list_init(&output->shm.buffers);
1061 wl_list_init(&output->shm.free_buffers);
1062
Giulio Camuffo954f1832014-10-11 18:27:30 +03001063 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001064 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001065
Giulio Camuffo954f1832014-10-11 18:27:30 +03001066 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001067 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001068 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001069 output->base.repaint = wayland_output_repaint_pixman;
1070 } else {
1071 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001072 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001073 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001074 }
1075
Jonas Ådahle5a12252013-04-05 23:07:11 +02001076 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001077 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001078 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001079 output->base.set_backlight = NULL;
1080 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001081 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001082
Giulio Camuffo954f1832014-10-11 18:27:30 +03001083 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001084
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001085 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001086
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001087err_output:
1088 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001089 if (output->parent.shell_surface)
1090 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001091err_surface:
1092 wl_surface_destroy(output->parent.surface);
1093err_name:
1094 free(output->name);
1095
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001096 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001097 free(output);
1098
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001099 return NULL;
1100}
1101
1102static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001103wayland_output_create_for_config(struct wayland_backend *b,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001104 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001105 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001106 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001107{
1108 struct wayland_output *output;
1109 char *mode, *t, *name, *str;
1110 int width, height, scale;
1111 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001112 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001113
1114 weston_config_section_get_string(config_section, "name", &name, NULL);
1115 if (name) {
1116 slen = strlen(name);
1117 slen += strlen(WINDOW_TITLE " - ");
1118 str = malloc(slen + 1);
1119 if (str)
1120 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1121 free(name);
1122 name = str;
1123 }
1124 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001125 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001126
1127 weston_config_section_get_string(config_section,
1128 "mode", &mode, "1024x600");
1129 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1130 weston_log("Invalid mode \"%s\" for output %s\n",
1131 mode, name);
1132 width = 1024;
1133 height = 640;
1134 }
1135 free(mode);
1136
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001137 if (option_width)
1138 width = option_width;
1139 if (option_height)
1140 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001141
1142 weston_config_section_get_int(config_section, "scale", &scale, 1);
1143
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001144 if (option_scale)
1145 scale = option_scale;
1146
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001147 weston_config_section_get_string(config_section,
1148 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001149 if (weston_parse_transform(t, &transform) < 0)
1150 weston_log("Invalid transform \"%s\" for output %s\n",
1151 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001152 free(t);
1153
Giulio Camuffo954f1832014-10-11 18:27:30 +03001154 output = wayland_output_create(b, x, y, width, height, name, 0,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001155 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001156 free(name);
1157
1158 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001159}
1160
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001161static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001162wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001163 struct wayland_parent_output *poutput)
1164{
1165 struct wayland_output *output;
1166 struct weston_mode *mode;
1167 int32_t x;
1168
1169 if (poutput->current_mode) {
1170 mode = poutput->current_mode;
1171 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001172 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001173 } else if (!wl_list_empty(&poutput->mode_list)) {
1174 mode = container_of(poutput->mode_list.next,
1175 struct weston_mode, link);
1176 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001177 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001178 return NULL;
1179 }
1180
Giulio Camuffo954f1832014-10-11 18:27:30 +03001181 if (!wl_list_empty(&b->compositor->output_list)) {
1182 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001183 struct wayland_output, base.link);
1184 x = output->base.x + output->base.current_mode->width;
1185 } else {
1186 x = 0;
1187 }
1188
Giulio Camuffo954f1832014-10-11 18:27:30 +03001189 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001190 NULL, 0,
1191 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1192 if (!output)
1193 return NULL;
1194
1195 output->parent.output = poutput->global;
1196
1197 output->base.make = poutput->physical.make;
1198 output->base.model = poutput->physical.model;
1199 wl_list_init(&output->base.mode_list);
1200 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1201 wl_list_init(&poutput->mode_list);
1202
1203 wayland_output_set_fullscreen(output,
1204 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1205 mode->refresh, poutput->global);
1206
1207 if (output->parent.shell_surface) {
1208 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1209 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1210 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001211 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001212 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1213 output->parent.surface,
1214 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1215 poutput->global);
1216 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1217 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1218 output->parent.surface,
1219 poutput->global,
1220 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001221 }
1222
1223 return output;
1224}
1225
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001226static void
1227shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1228 uint32_t serial)
1229{
1230 wl_shell_surface_pong(shell_surface, serial);
1231}
1232
1233static void
1234shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1235 uint32_t edges, int32_t width, int32_t height)
1236{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001237 struct wayland_output *output = data;
1238
1239 output->parent.configure_width = width;
1240 output->parent.configure_height = height;
1241
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001242 /* FIXME: implement resizing */
1243}
1244
1245static void
1246shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1247{
1248}
1249
1250static const struct wl_shell_surface_listener shell_surface_listener = {
1251 shell_surface_ping,
1252 shell_surface_configure,
1253 shell_surface_popup_done
1254};
1255
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001256/* Events received from the wayland-server this compositor is client of: */
1257
Jason Ekstrand7744f712013-10-27 22:24:55 -05001258/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001259static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001260input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001261{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001262
Jason Ekstrand7744f712013-10-27 22:24:55 -05001263 struct wl_buffer *buffer;
1264 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001265
Giulio Camuffo954f1832014-10-11 18:27:30 +03001266 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001267 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001268
Giulio Camuffo954f1832014-10-11 18:27:30 +03001269 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001270 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001271 if (!buffer)
1272 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001273
1274 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1275 input->parent.cursor.surface,
1276 image->hotspot_x, image->hotspot_y);
1277
1278 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1279 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1280 image->width, image->height);
1281 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001282}
1283
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001284static void
Daniel Stone37816df2012-05-16 18:45:18 +01001285input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1286 uint32_t serial, struct wl_surface *surface,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001287 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001288{
1289 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001290 int32_t fx, fy;
1291 enum theme_location location;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001292 double x, y;
1293
1294 x = wl_fixed_to_double(fixed_x);
1295 y = wl_fixed_to_double(fixed_y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001296
Daniel Stone50692802012-06-22 13:21:41 +01001297 /* XXX: If we get a modifier event immediately before the focus,
1298 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001299 input->enter_serial = serial;
1300 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001301
1302 if (input->output->frame) {
1303 location = frame_pointer_enter(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001304 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001305 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001306 x -= fx;
1307 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001308
1309 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1310 weston_output_schedule_repaint(&input->output->base);
1311 } else {
1312 location = THEME_LOCATION_CLIENT_AREA;
1313 }
1314
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001315 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001316
1317 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001318 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001319 notify_pointer_focus(&input->base, &input->output->base, x, y);
1320 wl_pointer_set_cursor(input->parent.pointer,
1321 input->enter_serial, NULL, 0, 0);
1322 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001323 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001324 notify_pointer_focus(&input->base, NULL, 0, 0);
1325 input_set_cursor(input);
1326 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001327}
1328
1329static void
Daniel Stone37816df2012-05-16 18:45:18 +01001330input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1331 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001332{
1333 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001334
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001335 if (!input->output)
1336 return;
1337
Jason Ekstrand7744f712013-10-27 22:24:55 -05001338 if (input->output->frame) {
1339 frame_pointer_leave(input->output->frame, input);
1340
1341 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1342 weston_output_schedule_repaint(&input->output->base);
1343 }
1344
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001345 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001346 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001347 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001348}
1349
1350static void
Daniel Stone37816df2012-05-16 18:45:18 +01001351input_handle_motion(void *data, struct wl_pointer *pointer,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001352 uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Daniel Stone37816df2012-05-16 18:45:18 +01001353{
1354 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001355 int32_t fx, fy;
1356 enum theme_location location;
Peter Hutterer87743e92016-01-18 16:38:22 +10001357 bool want_frame = false;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001358 double x, y;
Daniel Stone37816df2012-05-16 18:45:18 +01001359
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001360 if (!input->output)
1361 return;
1362
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001363 x = wl_fixed_to_double(fixed_x);
1364 y = wl_fixed_to_double(fixed_y);
1365
Jason Ekstrand7744f712013-10-27 22:24:55 -05001366 if (input->output->frame) {
1367 location = frame_pointer_motion(input->output->frame, input,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001368 x, y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001369 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001370 x -= fx;
1371 y -= fy;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001372
1373 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1374 weston_output_schedule_repaint(&input->output->base);
1375 } else {
1376 location = THEME_LOCATION_CLIENT_AREA;
1377 }
1378
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001379 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001380
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001381 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001382 input_set_cursor(input);
1383 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001384 input->has_focus = false;
Peter Hutterer87743e92016-01-18 16:38:22 +10001385 want_frame = true;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001386 } else if (!input->has_focus &&
1387 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001388 wl_pointer_set_cursor(input->parent.pointer,
1389 input->enter_serial, NULL, 0, 0);
1390 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001391 input->has_focus = true;
Peter Hutterer87743e92016-01-18 16:38:22 +10001392 want_frame = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001393 }
1394
Peter Hutterer87743e92016-01-18 16:38:22 +10001395 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001396 notify_motion_absolute(&input->base, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +10001397 want_frame = true;
1398 }
1399
1400 if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1401 notify_pointer_frame(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001402}
1403
1404static void
1405input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001406 uint32_t serial, uint32_t time, uint32_t button,
1407 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001408{
1409 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001410 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001411 enum frame_button_state fstate;
1412 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001413
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001414 if (!input->output)
1415 return;
1416
Jason Ekstrand7744f712013-10-27 22:24:55 -05001417 if (input->output->frame) {
1418 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1419 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1420
1421 location = frame_pointer_button(input->output->frame, input,
1422 button, fstate);
1423
1424 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1425
1426 wl_shell_surface_move(input->output->parent.shell_surface,
1427 input->parent.seat, serial);
1428 frame_status_clear(input->output->frame,
1429 FRAME_STATUS_MOVE);
1430 return;
1431 }
1432
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001433 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1434 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001435 input->output = NULL;
1436 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001437
Giulio Camuffo954f1832014-10-11 18:27:30 +03001438 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001439 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001440
1441 return;
1442 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001443
1444 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1445 weston_output_schedule_repaint(&input->output->base);
1446 } else {
1447 location = THEME_LOCATION_CLIENT_AREA;
1448 }
1449
Peter Hutterer87743e92016-01-18 16:38:22 +10001450 if (location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001451 notify_button(&input->base, time, button, state);
Peter Hutterer87743e92016-01-18 16:38:22 +10001452 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1453 notify_pointer_frame(&input->base);
1454 }
Daniel Stone37816df2012-05-16 18:45:18 +01001455}
1456
1457static void
1458input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001459 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001460{
1461 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001462 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001463
Peter Hutterer89b6a492016-01-18 15:58:17 +10001464 weston_event.axis = axis;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001465 weston_event.value = wl_fixed_to_double(value);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001466
Peter Hutterer87743e92016-01-18 16:38:22 +10001467 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL &&
1468 input->vert.has_discrete) {
1469 weston_event.has_discrete = true;
1470 weston_event.discrete = input->vert.discrete;
1471 input->vert.has_discrete = false;
1472 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL &&
1473 input->horiz.has_discrete) {
1474 weston_event.has_discrete = true;
1475 weston_event.discrete = input->horiz.discrete;
1476 input->horiz.has_discrete = false;
1477 }
1478
Peter Hutterer89b6a492016-01-18 15:58:17 +10001479 notify_axis(&input->base, time, &weston_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001480
1481 if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION)
1482 notify_pointer_frame(&input->base);
1483}
1484
1485static void
1486input_handle_frame(void *data, struct wl_pointer *pointer)
1487{
1488 struct wayland_input *input = data;
1489
1490 notify_pointer_frame(&input->base);
1491}
1492
1493static void
1494input_handle_axis_source(void *data, struct wl_pointer *pointer,
1495 uint32_t source)
1496{
1497 struct wayland_input *input = data;
1498
1499 notify_axis_source(&input->base, source);
1500}
1501
1502static void
1503input_handle_axis_stop(void *data, struct wl_pointer *pointer,
1504 uint32_t time, uint32_t axis)
1505{
1506 struct wayland_input *input = data;
1507 struct weston_pointer_axis_event weston_event;
1508
1509 weston_event.axis = axis;
1510 weston_event.value = 0;
1511
1512 notify_axis(&input->base, time, &weston_event);
1513}
1514
1515static void
1516input_handle_axis_discrete(void *data, struct wl_pointer *pointer,
1517 uint32_t axis, int32_t discrete)
1518{
1519 struct wayland_input *input = data;
1520
1521 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
1522 input->vert.has_discrete = true;
1523 input->vert.discrete = discrete;
1524 } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
1525 input->horiz.has_discrete = true;
1526 input->horiz.discrete = discrete;
1527 }
Daniel Stone37816df2012-05-16 18:45:18 +01001528}
1529
1530static const struct wl_pointer_listener pointer_listener = {
1531 input_handle_pointer_enter,
1532 input_handle_pointer_leave,
1533 input_handle_motion,
1534 input_handle_button,
1535 input_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001536 input_handle_frame,
1537 input_handle_axis_source,
1538 input_handle_axis_stop,
1539 input_handle_axis_discrete,
Daniel Stone37816df2012-05-16 18:45:18 +01001540};
1541
1542static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001543input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1544 int fd, uint32_t size)
1545{
1546 struct wayland_input *input = data;
1547 struct xkb_keymap *keymap;
1548 char *map_str;
1549
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001550 if (!data) {
1551 close(fd);
1552 return;
1553 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001554
1555 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1556 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1557 if (map_str == MAP_FAILED) {
1558 weston_log("mmap failed: %m\n");
1559 goto error;
1560 }
1561
Giulio Camuffo954f1832014-10-11 18:27:30 +03001562 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001563 map_str,
1564 XKB_KEYMAP_FORMAT_TEXT_V1,
1565 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001566 munmap(map_str, size);
1567
1568 if (!keymap) {
1569 weston_log("failed to compile keymap\n");
1570 goto error;
1571 }
1572
1573 input->keyboard_state_update = STATE_UPDATE_NONE;
1574 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1575 weston_log("No keymap provided; falling back to defalt\n");
1576 keymap = NULL;
1577 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1578 } else {
1579 weston_log("Invalid keymap\n");
1580 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001581 }
1582
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001583 close(fd);
1584
Derek Foreman1281a362015-07-31 16:55:32 -05001585 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001586 weston_seat_update_keymap(&input->base, keymap);
1587 else
1588 weston_seat_init_keyboard(&input->base, keymap);
1589
Ran Benitac9c74152014-08-19 23:59:52 +03001590 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001591
1592 return;
1593
1594error:
1595 wl_keyboard_release(input->parent.keyboard);
1596 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001597}
1598
1599static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001600input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001601 struct wl_keyboard *keyboard,
1602 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001603 struct wl_surface *surface,
1604 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001605{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001606 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001607 struct wayland_output *focus;
1608
1609 focus = input->keyboard_focus;
1610 if (focus) {
1611 /* This shouldn't happen */
1612 focus->keyboard_count--;
1613 if (!focus->keyboard_count && focus->frame)
1614 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1615 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1616 weston_output_schedule_repaint(&focus->base);
1617 }
1618
1619 input->keyboard_focus = wl_surface_get_user_data(surface);
1620 input->keyboard_focus->keyboard_count++;
1621
1622 focus = input->keyboard_focus;
1623 if (focus->frame) {
1624 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1625 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1626 weston_output_schedule_repaint(&focus->base);
1627 }
1628
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001629
Daniel Stone50692802012-06-22 13:21:41 +01001630 /* XXX: If we get a modifier event immediately before the focus,
1631 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001632 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001633 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001634}
1635
1636static void
1637input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001638 struct wl_keyboard *keyboard,
1639 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001640 struct wl_surface *surface)
1641{
1642 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001643 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001644
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001645 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001646
1647 focus = input->keyboard_focus;
1648 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001649 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001650
1651 focus->keyboard_count--;
1652 if (!focus->keyboard_count && focus->frame) {
1653 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1654 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1655 weston_output_schedule_repaint(&focus->base);
1656 }
1657
1658 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001659}
1660
Daniel Stone37816df2012-05-16 18:45:18 +01001661static void
1662input_handle_key(void *data, struct wl_keyboard *keyboard,
1663 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1664{
1665 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001666
Daniel Stone50692802012-06-22 13:21:41 +01001667 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001668 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001669 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001670 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001671 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001672}
1673
Daniel Stone351eb612012-05-31 15:27:47 -04001674static void
Derek Foreman1281a362015-07-31 16:55:32 -05001675input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001676 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001677 uint32_t mods_latched, uint32_t mods_locked,
1678 uint32_t group)
1679{
Derek Foreman1281a362015-07-31 16:55:32 -05001680 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001681 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001682 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001683 uint32_t serial_out;
1684
1685 /* If we get a key event followed by a modifier event with the
1686 * same serial number, then we try to preserve those semantics by
1687 * reusing the same serial number on the way out too. */
1688 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001689 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001690 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001691 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001692
Derek Foreman1281a362015-07-31 16:55:32 -05001693 keyboard = weston_seat_get_keyboard(&input->base);
1694 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001695 mods_depressed, mods_latched,
1696 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001697 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001698}
1699
Jonny Lamb497994a2014-08-12 14:58:26 +02001700static void
1701input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1702 int32_t rate, int32_t delay)
1703{
1704 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001705 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001706
Giulio Camuffo954f1832014-10-11 18:27:30 +03001707 b->compositor->kb_repeat_rate = rate;
1708 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001709}
1710
Daniel Stone37816df2012-05-16 18:45:18 +01001711static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001712 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001713 input_handle_keyboard_enter,
1714 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001715 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001716 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001717 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001718};
1719
1720static void
Derek Foreman748c6952015-11-06 15:56:10 -06001721input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1722 uint32_t serial, uint32_t time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001723 struct wl_surface *surface, int32_t id,
1724 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001725{
1726 struct wayland_input *input = data;
1727 struct wayland_output *output;
1728 enum theme_location location;
1729 bool first_touch;
1730 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001731 double x, y;
1732
1733 x = wl_fixed_to_double(fixed_x);
1734 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001735
1736 first_touch = (input->touch_points == 0);
1737 input->touch_points++;
1738
1739 input->touch_focus = wl_surface_get_user_data(surface);
1740 output = input->touch_focus;
1741 if (!first_touch && !input->touch_active)
1742 return;
1743
1744 if (output->frame) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001745 location = frame_touch_down(output->frame, input, id, x, y);
Derek Foreman748c6952015-11-06 15:56:10 -06001746
1747 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001748 x -= fx;
1749 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001750
1751 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1752 weston_output_schedule_repaint(&output->base);
1753
1754 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1755 input->touch_points--;
1756 wl_shell_surface_move(output->parent.shell_surface,
1757 input->parent.seat, serial);
1758 frame_status_clear(output->frame,
1759 FRAME_STATUS_MOVE);
1760 return;
1761 }
1762
1763 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1764 return;
1765 }
1766
1767 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1768
1769 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1770 input->touch_active = true;
1771}
1772
1773static void
1774input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1775 uint32_t serial, uint32_t time, int32_t id)
1776{
1777 struct wayland_input *input = data;
1778 struct wayland_output *output = input->touch_focus;
1779 bool active = input->touch_active;
1780
1781 input->touch_points--;
1782 if (input->touch_points == 0) {
1783 input->touch_focus = NULL;
1784 input->touch_active = false;
1785 }
1786
1787 if (!output)
1788 return;
1789
1790 if (output->frame) {
1791 frame_touch_up(output->frame, input, id);
1792
1793 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1794 wayland_output_destroy(&output->base);
1795 input->touch_focus = NULL;
1796 input->keyboard_focus = NULL;
1797 if (wl_list_empty(&input->backend->compositor->output_list))
1798 weston_compositor_exit(input->backend->compositor);
1799
1800 return;
1801 }
1802 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1803 weston_output_schedule_repaint(&output->base);
1804 }
1805
1806 if (active)
1807 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1808}
1809
1810static void
1811input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001812 uint32_t time, int32_t id,
1813 wl_fixed_t fixed_x, wl_fixed_t fixed_y)
Derek Foreman748c6952015-11-06 15:56:10 -06001814{
1815 struct wayland_input *input = data;
1816 struct wayland_output *output = input->touch_focus;
1817 int32_t fx, fy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001818 double x, y;
1819
1820 x = wl_fixed_to_double(fixed_x);
1821 y = wl_fixed_to_double(fixed_y);
Derek Foreman748c6952015-11-06 15:56:10 -06001822
1823 if (!output || !input->touch_active)
1824 return;
1825
1826 if (output->frame) {
1827 frame_interior(output->frame, &fx, &fy, NULL, NULL);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001828 x -= fx;
1829 y -= fy;
Derek Foreman748c6952015-11-06 15:56:10 -06001830 }
1831
1832 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1833
1834 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1835}
1836
1837static void
1838input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1839{
1840 struct wayland_input *input = data;
1841
1842 if (!input->touch_focus || !input->touch_active)
1843 return;
1844
1845 notify_touch_frame(&input->base);
1846}
1847
1848static void
1849input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1850{
1851 struct wayland_input *input = data;
1852
1853 if (!input->touch_focus || !input->touch_active)
1854 return;
1855
1856 notify_touch_cancel(&input->base);
1857}
1858
1859static const struct wl_touch_listener touch_listener = {
1860 input_handle_touch_down,
1861 input_handle_touch_up,
1862 input_handle_touch_motion,
1863 input_handle_touch_frame,
1864 input_handle_touch_cancel,
1865};
1866
1867
1868static void
Daniel Stone37816df2012-05-16 18:45:18 +01001869input_handle_capabilities(void *data, struct wl_seat *seat,
1870 enum wl_seat_capability caps)
1871{
1872 struct wayland_input *input = data;
1873
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001874 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1875 input->parent.pointer = wl_seat_get_pointer(seat);
1876 wl_pointer_set_user_data(input->parent.pointer, input);
1877 wl_pointer_add_listener(input->parent.pointer,
1878 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001879 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001880 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001881 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1882 wl_pointer_release(input->parent.pointer);
1883 else
1884 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001885 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001886 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001887 }
1888
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001889 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1890 input->parent.keyboard = wl_seat_get_keyboard(seat);
1891 wl_keyboard_set_user_data(input->parent.keyboard, input);
1892 wl_keyboard_add_listener(input->parent.keyboard,
1893 &keyboard_listener, input);
1894 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001895 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1896 wl_keyboard_release(input->parent.keyboard);
1897 else
1898 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001899 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001900 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001901 }
Derek Foreman748c6952015-11-06 15:56:10 -06001902
1903 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1904 input->parent.touch = wl_seat_get_touch(seat);
1905 wl_touch_set_user_data(input->parent.touch, input);
1906 wl_touch_add_listener(input->parent.touch,
1907 &touch_listener, input);
1908 weston_seat_init_touch(&input->base);
1909 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1910 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1911 wl_touch_release(input->parent.touch);
1912 else
1913 wl_touch_destroy(input->parent.touch);
1914 input->parent.touch = NULL;
1915 weston_seat_release_touch(&input->base);
1916 }
Daniel Stone37816df2012-05-16 18:45:18 +01001917}
1918
Jonny Lamb497994a2014-08-12 14:58:26 +02001919static void
1920input_handle_name(void *data, struct wl_seat *seat,
1921 const char *name)
1922{
1923}
1924
Daniel Stone37816df2012-05-16 18:45:18 +01001925static const struct wl_seat_listener seat_listener = {
1926 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001927 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001928};
1929
1930static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001931display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001932{
1933 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001934 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001935
Peter Huttererf3d62272013-08-08 11:57:05 +10001936 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001937 if (input == NULL)
1938 return;
1939
Giulio Camuffo954f1832014-10-11 18:27:30 +03001940 weston_seat_init(&input->base, b->compositor, "default");
1941 input->backend = b;
1942 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001943 &wl_seat_interface, version);
1944 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001945 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001946
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001947 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1948 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001949
1950 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001951 wl_compositor_create_surface(b->parent.compositor);
Peter Hutterer87743e92016-01-18 16:38:22 +10001952
1953 input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1954 input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001955}
1956
1957static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001958wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1959 int32_t x, int32_t y,
1960 int32_t physical_width, int32_t physical_height,
1961 int32_t subpixel, const char *make,
1962 const char *model, int32_t transform)
1963{
1964 struct wayland_parent_output *output = data;
1965
1966 output->x = x;
1967 output->y = y;
1968 output->physical.width = physical_width;
1969 output->physical.height = physical_height;
1970 output->physical.subpixel = subpixel;
1971
1972 free(output->physical.make);
1973 output->physical.make = strdup(make);
1974 free(output->physical.model);
1975 output->physical.model = strdup(model);
1976
1977 output->transform = transform;
1978}
1979
1980static struct weston_mode *
1981find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1982{
1983 struct weston_mode *mode;
1984
1985 wl_list_for_each(mode, list, link) {
1986 if (mode->width == width && mode->height == height &&
1987 mode->refresh == refresh)
1988 return mode;
1989 }
1990
1991 mode = zalloc(sizeof *mode);
1992 if (!mode)
1993 return NULL;
1994
1995 mode->width = width;
1996 mode->height = height;
1997 mode->refresh = refresh;
1998 wl_list_insert(list, &mode->link);
1999
2000 return mode;
2001}
2002
2003static void
2004wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
2005 uint32_t flags, int32_t width, int32_t height,
2006 int32_t refresh)
2007{
2008 struct wayland_parent_output *output = data;
2009 struct weston_mode *mode;
2010
2011 if (output->output) {
2012 mode = find_mode(&output->output->base.mode_list,
2013 width, height, refresh);
2014 if (!mode)
2015 return;
2016 mode->flags = flags;
2017 /* Do a mode-switch on current mode change? */
2018 } else {
2019 mode = find_mode(&output->mode_list, width, height, refresh);
2020 if (!mode)
2021 return;
2022 mode->flags = flags;
2023 if (flags & WL_OUTPUT_MODE_CURRENT)
2024 output->current_mode = mode;
2025 if (flags & WL_OUTPUT_MODE_PREFERRED)
2026 output->preferred_mode = mode;
2027 }
2028}
2029
2030static const struct wl_output_listener output_listener = {
2031 wayland_parent_output_geometry,
2032 wayland_parent_output_mode
2033};
2034
2035static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002036wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002037{
2038 struct wayland_parent_output *output;
2039
2040 output = zalloc(sizeof *output);
2041 if (!output)
2042 return;
2043
2044 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002046 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002047 if (!output->global) {
2048 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002049 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07002050 }
2051
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002052 wl_output_add_listener(output->global, &output_listener, output);
2053
2054 output->scale = 0;
2055 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2056 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
2057 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002058 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002059
Giulio Camuffo954f1832014-10-11 18:27:30 +03002060 if (b->sprawl_across_outputs) {
2061 wl_display_roundtrip(b->parent.wl_display);
2062 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002063 }
2064}
2065
2066static void
2067wayland_parent_output_destroy(struct wayland_parent_output *output)
2068{
2069 struct weston_mode *mode, *next;
2070
2071 if (output->output)
2072 wayland_output_destroy(&output->output->base);
2073
2074 wl_output_destroy(output->global);
2075 free(output->physical.make);
2076 free(output->physical.model);
2077
2078 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
2079 wl_list_remove(&mode->link);
2080 free(mode);
2081 }
2082}
2083
2084static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002085registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
2086 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002087{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002088 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002089
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002090 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002091 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002092 wl_registry_bind(registry, name,
2093 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02002094 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002095 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002096 wl_registry_bind(registry, name,
2097 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08002098 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002099 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002100 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002101 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002102 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002103 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002104 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002105 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002106 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002107 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002108 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002109 }
2110}
2111
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002112static void
2113registry_handle_global_remove(void *data, struct wl_registry *registry,
2114 uint32_t name)
2115{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002116 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002117 struct wayland_parent_output *output;
2118
Giulio Camuffo954f1832014-10-11 18:27:30 +03002119 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002120 if (output->id == name)
2121 wayland_parent_output_destroy(output);
2122}
2123
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002124static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002125 registry_handle_global,
2126 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002127};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002128
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002129static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002130wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002131{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002132 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002133 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002134
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002135 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002136 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002137 return 0;
2138 }
2139
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002140 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002141 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002142 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002143 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002144
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002145 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002146 count = wl_display_dispatch_pending(b->parent.wl_display);
2147 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002148 }
2149
2150 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002151}
2152
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002153static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002154wayland_restore(struct weston_compositor *ec)
2155{
2156}
2157
2158static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002159wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002160{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002161 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002162
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002163 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002164
Giulio Camuffo954f1832014-10-11 18:27:30 +03002165 if (b->parent.shm)
2166 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002167
Giulio Camuffo954f1832014-10-11 18:27:30 +03002168 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002169}
2170
Jason Ekstrand7744f712013-10-27 22:24:55 -05002171static const char *left_ptrs[] = {
2172 "left_ptr",
2173 "default",
2174 "top_left_arrow",
2175 "left-arrow"
2176};
2177
2178static void
Benoit Gschwind244ff792016-04-28 20:33:11 +02002179create_cursor(struct wayland_backend *b,
2180 struct weston_wayland_backend_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002181{
Jason Ekstrand7744f712013-10-27 22:24:55 -05002182 unsigned int i;
2183
Benoit Gschwind244ff792016-04-28 20:33:11 +02002184 b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
2185 config->cursor_size,
2186 b->parent.shm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002187 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002188 fprintf(stderr, "could not load cursor theme\n");
2189 return;
2190 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002191
Giulio Camuffo954f1832014-10-11 18:27:30 +03002192 b->cursor = NULL;
2193 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2194 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002195 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002196 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002197 fprintf(stderr, "could not load left cursor\n");
2198 return;
2199 }
2200}
2201
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002202static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002203fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2204 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002205{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002206 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002207 struct wayland_input *input = NULL;
2208
Giulio Camuffo954f1832014-10-11 18:27:30 +03002209 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002210 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002211 break;
2212
2213 if (!input || !input->output)
2214 return;
2215
2216 if (input->output->frame)
2217 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2218 else
2219 wayland_output_set_windowed(input->output);
2220
2221 weston_output_schedule_repaint(&input->output->base);
2222}
2223
Giulio Camuffo954f1832014-10-11 18:27:30 +03002224static struct wayland_backend *
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002225wayland_backend_create(struct weston_compositor *compositor,
2226 struct weston_wayland_backend_config *new_config,
2227 int *argc, char *argv[], struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002228{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002229 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002230 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002231 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002232
Giulio Camuffo954f1832014-10-11 18:27:30 +03002233 b = zalloc(sizeof *b);
2234 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002235 return NULL;
2236
Giulio Camuffo954f1832014-10-11 18:27:30 +03002237 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002238 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002239 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002240
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002241 b->parent.wl_display = wl_display_connect(new_config->display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002242 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002243 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002244 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002245 }
2246
Giulio Camuffo954f1832014-10-11 18:27:30 +03002247 wl_list_init(&b->parent.output_list);
2248 wl_list_init(&b->input_list);
2249 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2250 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2251 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002252
Benoit Gschwind244ff792016-04-28 20:33:11 +02002253 create_cursor(b, new_config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002254
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002255 b->use_pixman = new_config->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002256
Giulio Camuffo954f1832014-10-11 18:27:30 +03002257 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002258 gl_renderer = weston_load_module("gl-renderer.so",
2259 "gl_renderer_interface");
2260 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002261 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002262 }
2263
Giulio Camuffo954f1832014-10-11 18:27:30 +03002264 if (!b->use_pixman) {
2265 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002266 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002267 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002268 gl_renderer->alpha_attribs,
2269 NULL,
2270 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002271 weston_log("Failed to initialize the GL renderer; "
2272 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002273 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002274 }
2275 }
2276
Giulio Camuffo954f1832014-10-11 18:27:30 +03002277 if (b->use_pixman) {
2278 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002279 weston_log("Failed to initialize pixman renderer\n");
2280 goto err_display;
2281 }
2282 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002283
Giulio Camuffo954f1832014-10-11 18:27:30 +03002284 b->base.destroy = wayland_destroy;
2285 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002286
Giulio Camuffo954f1832014-10-11 18:27:30 +03002287 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002288
Giulio Camuffo954f1832014-10-11 18:27:30 +03002289 fd = wl_display_get_fd(b->parent.wl_display);
2290 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002291 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002292 wayland_backend_handle_event, b);
2293 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002294 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002295
Giulio Camuffo954f1832014-10-11 18:27:30 +03002296 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002297
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002298 if (compositor->renderer->import_dmabuf) {
2299 if (linux_dmabuf_setup(compositor) < 0)
2300 weston_log("Error: initializing dmabuf "
2301 "support failed.\n");
2302 }
2303
Giulio Camuffo954f1832014-10-11 18:27:30 +03002304 compositor->backend = &b->base;
2305 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002306err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002307 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002308err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002310 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002311 return NULL;
2312}
2313
2314static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002315wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002316{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002317 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002318
Giulio Camuffo954f1832014-10-11 18:27:30 +03002319 if (b->theme)
2320 theme_destroy(b->theme);
2321 if (b->frame_device)
2322 cairo_device_destroy(b->frame_device);
2323 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002324
Giulio Camuffo954f1832014-10-11 18:27:30 +03002325 weston_compositor_shutdown(b->compositor);
2326 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002327}
2328
Giulio Camuffo954f1832014-10-11 18:27:30 +03002329WL_EXPORT int
2330backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002331 struct weston_config *config,
2332 struct weston_backend_config *config_base)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002333{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002334 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002335 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002336 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002337 struct weston_config_section *section;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002338 struct weston_wayland_backend_config new_config;
2339 int x, count, width, height, scale;
2340 const char *section_name;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002341 char *name;
2342
2343 const struct weston_option wayland_options[] = {
2344 { WESTON_OPTION_INTEGER, "width", 0, &width },
2345 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002346 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002347 { WESTON_OPTION_STRING, "display", 0, &new_config.display_name },
2348 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &new_config.use_pixman },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002349 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind37a68072016-04-28 20:33:08 +02002350 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &new_config.fullscreen },
2351 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &new_config.sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002352 };
2353
2354 width = 0;
2355 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002356 scale = 0;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002357 new_config.display_name = NULL;
2358 new_config.use_pixman = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002359 count = 1;
Benoit Gschwind37a68072016-04-28 20:33:08 +02002360 new_config.fullscreen = 0;
2361 new_config.sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002362 parse_options(wayland_options,
2363 ARRAY_LENGTH(wayland_options), argc, argv);
2364
Benoit Gschwind244ff792016-04-28 20:33:11 +02002365 new_config.cursor_size = 32;
2366 new_config.cursor_theme = NULL;
2367
2368 section = weston_config_get_section(config, "shell", NULL, NULL);
2369 weston_config_section_get_string(section, "cursor-theme",
2370 &new_config.cursor_theme, NULL);
2371 weston_config_section_get_int(section, "cursor-size",
2372 &new_config.cursor_size, 32);
2373
Benoit Gschwind3a49b512016-04-28 20:33:10 +02002374 b = wayland_backend_create(compositor, &new_config, argc, argv, config);
Benoit Gschwind37a68072016-04-28 20:33:08 +02002375
Giulio Camuffo954f1832014-10-11 18:27:30 +03002376 if (!b)
2377 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002378
Benoit Gschwind37a68072016-04-28 20:33:08 +02002379 if (new_config.sprawl || b->parent.fshell) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002380 b->sprawl_across_outputs = 1;
2381 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002382
Giulio Camuffo954f1832014-10-11 18:27:30 +03002383 wl_list_for_each(poutput, &b->parent.output_list, link)
2384 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002385
Giulio Camuffo954f1832014-10-11 18:27:30 +03002386 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002387 }
2388
Benoit Gschwind37a68072016-04-28 20:33:08 +02002389 if (new_config.fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 output = wayland_output_create(b, 0, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002391 NULL, 1, 0, 1);
2392 if (!output)
2393 goto err_outputs;
2394
Axel Davydd8b88d2013-11-17 21:34:16 +01002395 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002396 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002397 }
2398
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002399 section = NULL;
2400 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002401 while (weston_config_next_section(config, &section, &section_name)) {
2402 if (!section_name || strcmp(section_name, "output") != 0)
2403 continue;
2404 weston_config_section_get_string(section, "name", &name, NULL);
2405 if (name == NULL)
2406 continue;
2407
2408 if (name[0] != 'W' || name[1] != 'L') {
2409 free(name);
2410 continue;
2411 }
2412 free(name);
2413
Giulio Camuffo954f1832014-10-11 18:27:30 +03002414 output = wayland_output_create_for_config(b, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002415 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002416 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002417 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002418 if (wayland_output_set_windowed(output))
2419 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002420
2421 x += output->base.width;
2422 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002423 }
2424
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002425 if (!width)
2426 width = 1024;
2427 if (!height)
2428 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002429 if (!scale)
2430 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002431 while (count > 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002432 output = wayland_output_create(b, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002433 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002434 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002435 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002436 if (wayland_output_set_windowed(output))
2437 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002438
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002439 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002440 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002441 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002442
Giulio Camuffo954f1832014-10-11 18:27:30 +03002443 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002444 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002445 fullscreen_binding, b);
Benoit Gschwind244ff792016-04-28 20:33:11 +02002446 free(new_config.cursor_theme);
Benoit Gschwind079b6262016-04-28 20:33:09 +02002447 free(new_config.display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002448 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002449
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002450err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002451 wayland_backend_destroy(b);
Benoit Gschwind244ff792016-04-28 20:33:11 +02002452 free(new_config.cursor_theme);
Benoit Gschwind079b6262016-04-28 20:33:09 +02002453 free(new_config.display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002454 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002455}