blob: 48636fe843b563b57a5b7d5ece1f53cb3709763c [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 Paalanen363aa7b2014-12-17 16:20:40 +020050#include "presentation_timing-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
Giulio Camuffo954f1832014-10-11 18:27:30 +030055struct wayland_backend {
56 struct weston_backend base;
57 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058
59 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040060 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040061 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_compositor *compositor;
63 struct wl_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080064 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020065 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010066
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050067 struct wl_list output_list;
68
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069 struct wl_event_source *wl_source;
70 uint32_t event_mask;
71 } parent;
72
Jason Ekstrandff2fd462013-10-27 22:24:58 -050073 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050074 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050075
Jason Ekstrand7744f712013-10-27 22:24:55 -050076 struct theme *theme;
77 cairo_device_t *frame_device;
78 struct wl_cursor_theme *cursor_theme;
79 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050080
Jason Ekstrand06ced802013-11-07 20:13:29 -060081 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010082};
83
84struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050085 struct weston_output base;
86
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010087 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050088 int draw_initial_frame;
89 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050090
91 struct wl_output *output;
92 uint32_t global_id;
93
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050094 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060095 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050097
Jason Ekstrand7744f712013-10-27 22:24:55 -050098 int keyboard_count;
99
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600100 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500101 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500102
Jason Ekstrand7744f712013-10-27 22:24:55 -0500103 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500104 struct wl_egl_window *egl_window;
105 struct {
106 cairo_surface_t *top;
107 cairo_surface_t *left;
108 cairo_surface_t *right;
109 cairo_surface_t *bottom;
110 } border;
111 } gl;
112
113 struct {
114 struct wl_list buffers;
115 struct wl_list free_buffers;
116 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500117
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500118 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500119 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100120};
121
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500122struct wayland_parent_output {
123 struct wayland_output *output;
124 struct wl_list link;
125
126 struct wl_output *global;
127 uint32_t id;
128
129 struct {
130 char *make;
131 char *model;
132 int32_t width, height;
133 uint32_t subpixel;
134 } physical;
135
136 int32_t x, y;
137 uint32_t transform;
138 uint32_t scale;
139
140 struct wl_list mode_list;
141 struct weston_mode *preferred_mode;
142 struct weston_mode *current_mode;
143};
144
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500145struct wayland_shm_buffer {
146 struct wayland_output *output;
147 struct wl_list link;
148 struct wl_list free_link;
149
150 struct wl_buffer *buffer;
151 void *data;
152 size_t size;
153 pixman_region32_t damage;
154 int frame_damaged;
155
156 pixman_image_t *pm_image;
157 cairo_surface_t *c_surface;
158};
159
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100160struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400161 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300162 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100163 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500164
165 struct {
166 struct wl_seat *seat;
167 struct wl_pointer *pointer;
168 struct wl_keyboard *keyboard;
169 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500170
171 struct {
172 struct wl_surface *surface;
173 int32_t hx, hy;
174 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500175 } parent;
176
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500177 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100178 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400179 uint32_t enter_serial;
Derek Foreman748c6952015-11-06 15:56:10 -0600180 uint32_t touch_points;
181 bool touch_active;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600182 bool has_focus;
Derek Foremancfce7d02015-11-06 15:56:08 -0600183 int seat_version;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600184
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400185 struct wayland_output *output;
Derek Foreman748c6952015-11-06 15:56:10 -0600186 struct wayland_output *touch_focus;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500187 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100188};
189
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300190struct gl_renderer_interface *gl_renderer;
191
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500192static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500193wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
194{
195 cairo_surface_destroy(buffer->c_surface);
196 pixman_image_unref(buffer->pm_image);
197
198 wl_buffer_destroy(buffer->buffer);
199 munmap(buffer->data, buffer->size);
200
201 pixman_region32_fini(&buffer->damage);
202
203 wl_list_remove(&buffer->link);
204 wl_list_remove(&buffer->free_link);
205 free(buffer);
206}
207
208static void
209buffer_release(void *data, struct wl_buffer *buffer)
210{
211 struct wayland_shm_buffer *sb = data;
212
213 if (sb->output) {
214 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
215 } else {
216 wayland_shm_buffer_destroy(sb);
217 }
218}
219
220static const struct wl_buffer_listener buffer_listener = {
221 buffer_release
222};
223
224static struct wayland_shm_buffer *
225wayland_output_get_shm_buffer(struct wayland_output *output)
226{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300227 struct wayland_backend *b =
228 (struct wayland_backend *) output->base.compositor->backend;
229 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500230 struct wayland_shm_buffer *sb;
231
232 struct wl_shm_pool *pool;
233 int width, height, stride;
234 int32_t fx, fy;
235 int fd;
236 unsigned char *data;
237
238 if (!wl_list_empty(&output->shm.free_buffers)) {
239 sb = container_of(output->shm.free_buffers.next,
240 struct wayland_shm_buffer, free_link);
241 wl_list_remove(&sb->free_link);
242 wl_list_init(&sb->free_link);
243
244 return sb;
245 }
246
247 if (output->frame) {
248 width = frame_width(output->frame);
249 height = frame_height(output->frame);
250 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500251 width = output->base.current_mode->width;
252 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500253 }
254
255 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
256
257 fd = os_create_anonymous_file(height * stride);
258 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000259 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500260 return NULL;
261 }
262
263 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
264 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000265 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500266 close(fd);
267 return NULL;
268 }
269
270 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000271 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200272 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000273 close(fd);
274 free(data);
275 return NULL;
276 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500277
278 sb->output = output;
279 wl_list_init(&sb->free_link);
280 wl_list_insert(&output->shm.buffers, &sb->link);
281
282 pixman_region32_init_rect(&sb->damage, 0, 0,
283 output->base.width, output->base.height);
284 sb->frame_damaged = 1;
285
286 sb->data = data;
287 sb->size = height * stride;
288
289 pool = wl_shm_create_pool(shm, fd, sb->size);
290
291 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
292 width, height,
293 stride,
294 WL_SHM_FORMAT_ARGB8888);
295 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
296 wl_shm_pool_destroy(pool);
297 close(fd);
298
299 memset(data, 0, sb->size);
300
301 sb->c_surface =
302 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
303 width, height, stride);
304
305 fx = 0;
306 fy = 0;
307 if (output->frame)
308 frame_interior(output->frame, &fx, &fy, 0, 0);
309 sb->pm_image =
310 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
311 (uint32_t *)(data + fy * stride) + fx,
312 stride);
313
314 return sb;
315}
316
317static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500318frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400319{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400321 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400322
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500323 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400324
325 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300326
327 /*
328 * This is the fallback case, where Presentation extension is not
329 * available from the parent compositor. We do not know the base for
330 * 'time', so we cannot feed it to finish_frame(). Do the only thing
331 * we can, and pretend finish_frame time is when we process this
332 * event.
333 */
334 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200335 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400336}
337
338static const struct wl_callback_listener frame_listener = {
339 frame_done
340};
341
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500342static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200343draw_initial_frame(struct wayland_output *output)
344{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500345 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200346
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500347 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200348
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500349 /* If we are rendering with GL, then orphan it so that it gets
350 * destroyed immediately */
351 if (output->gl.egl_window)
352 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500353
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500354 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500355 wl_surface_damage(output->parent.surface, 0, 0,
356 output->base.current_mode->width,
357 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200358}
359
360static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500361wayland_output_update_gl_border(struct wayland_output *output)
362{
363 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
364 cairo_t *cr;
365
366 if (!output->frame)
367 return;
368 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
369 return;
370
371 fwidth = frame_width(output->frame);
372 fheight = frame_height(output->frame);
373 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
374
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500375 if (!output->gl.border.top)
376 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500377 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
378 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500380 frame_repaint(output->frame, cr);
381 cairo_destroy(cr);
382 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
383 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500384 cairo_image_surface_get_stride(output->gl.border.top) / 4,
385 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386
387
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 if (!output->gl.border.left)
389 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500390 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
391 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500392 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500393 cairo_translate(cr, 0, -iy);
394 frame_repaint(output->frame, cr);
395 cairo_destroy(cr);
396 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
397 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398 cairo_image_surface_get_stride(output->gl.border.left) / 4,
399 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500400
401
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500402 if (!output->gl.border.right)
403 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500404 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
405 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500406 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500407 cairo_translate(cr, -(iwidth + ix), -iy);
408 frame_repaint(output->frame, cr);
409 cairo_destroy(cr);
410 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
411 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500412 cairo_image_surface_get_stride(output->gl.border.right) / 4,
413 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500414
415
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500416 if (!output->gl.border.bottom)
417 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500418 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
419 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500420 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500421 cairo_translate(cr, 0, -(iy + iheight));
422 frame_repaint(output->frame, cr);
423 cairo_destroy(cr);
424 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
425 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500426 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
427 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500428}
429
430static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200431wayland_output_start_repaint_loop(struct weston_output *output_base)
432{
433 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300434 struct wayland_backend *wb =
435 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200436 struct wl_callback *callback;
437
438 /* If this is the initial frame, we need to attach a buffer so that
439 * the compositor can map the surface and include it in its render
440 * loop. If the surface doesn't end up in the render loop, the frame
441 * callback won't be invoked. The buffer is transparent and of the
442 * same size as the future real output buffer. */
443 if (output->parent.draw_initial_frame) {
444 output->parent.draw_initial_frame = 0;
445
446 draw_initial_frame(output);
447 }
448
449 callback = wl_surface_frame(output->parent.surface);
450 wl_callback_add_listener(callback, &frame_listener, output);
451 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300452 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200453}
454
David Herrmann1edf44c2013-10-22 17:11:26 +0200455static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500456wayland_output_repaint_gl(struct weston_output *output_base,
457 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400458{
459 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400460 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400461 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600462
Kristian Høgsberg33418202011-08-16 23:01:28 -0400463 callback = wl_surface_frame(output->parent.surface);
464 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100465
Jason Ekstrand7744f712013-10-27 22:24:55 -0500466 wayland_output_update_gl_border(output);
467
Pekka Paalanenbc106382012-10-10 12:49:31 +0300468 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200469
470 pixman_region32_subtract(&ec->primary_plane.damage,
471 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200472 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100473}
474
Matt Roper361d2ad2011-08-29 13:52:23 -0700475static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500476wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
477{
478 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
479 cairo_t *cr;
480
481 if (!buffer->output->frame || !buffer->frame_damaged)
482 return;
483
484 cr = cairo_create(buffer->c_surface);
485
486 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
487 fwidth = frame_width(buffer->output->frame);
488 fheight = frame_height(buffer->output->frame);
489
490 /* Set the clip so we don't unnecisaraly damage the surface */
491 cairo_move_to(cr, ix, iy);
492 cairo_rel_line_to(cr, iwidth, 0);
493 cairo_rel_line_to(cr, 0, iheight);
494 cairo_rel_line_to(cr, -iwidth, 0);
495 cairo_line_to(cr, ix, iy);
496 cairo_line_to(cr, 0, iy);
497 cairo_line_to(cr, 0, fheight);
498 cairo_line_to(cr, fwidth, fheight);
499 cairo_line_to(cr, fwidth, 0);
500 cairo_line_to(cr, 0, 0);
501 cairo_line_to(cr, 0, iy);
502 cairo_close_path(cr);
503 cairo_clip(cr);
504
505 /* Draw using a pattern so that the final result gets clipped */
506 cairo_push_group(cr);
507 frame_repaint(buffer->output->frame, cr);
508 cairo_pop_group_to_source(cr);
509 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
510 cairo_paint(cr);
511
512 cairo_destroy(cr);
513}
514
515static void
516wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
517{
518 pixman_region32_t damage;
519 pixman_box32_t *rects;
520 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
521 int i, n;
522
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500523 pixman_region32_init(&damage);
524 weston_transformed_region(sb->output->base.width,
525 sb->output->base.height,
526 sb->output->base.transform,
527 sb->output->base.current_scale,
528 &sb->damage, &damage);
529
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500530 if (sb->output->frame) {
531 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
532 fwidth = frame_width(sb->output->frame);
533 fheight = frame_height(sb->output->frame);
534
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500535 pixman_region32_translate(&damage, ix, iy);
536
537 if (sb->frame_damaged) {
538 pixman_region32_union_rect(&damage, &damage,
539 0, 0, fwidth, iy);
540 pixman_region32_union_rect(&damage, &damage,
541 0, iy, ix, iheight);
542 pixman_region32_union_rect(&damage, &damage,
543 ix + iwidth, iy,
544 fwidth - (ix + iwidth), iheight);
545 pixman_region32_union_rect(&damage, &damage,
546 0, iy + iheight,
547 fwidth, fheight - (iy + iheight));
548 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500549 }
550
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500551 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500552 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
553 for (i = 0; i < n; ++i)
554 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
555 rects[i].y1, rects[i].x2 - rects[i].x1,
556 rects[i].y2 - rects[i].y1);
557
558 if (sb->output->frame)
559 pixman_region32_fini(&damage);
560}
561
562static int
563wayland_output_repaint_pixman(struct weston_output *output_base,
564 pixman_region32_t *damage)
565{
566 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300567 struct wayland_backend *b =
568 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500569 struct wl_callback *callback;
570 struct wayland_shm_buffer *sb;
571
572 if (output->frame) {
573 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
574 wl_list_for_each(sb, &output->shm.buffers, link)
575 sb->frame_damaged = 1;
576 }
577
578 wl_list_for_each(sb, &output->shm.buffers, link)
579 pixman_region32_union(&sb->damage, &sb->damage, damage);
580
581 sb = wayland_output_get_shm_buffer(output);
582
583 wayland_output_update_shm_border(sb);
584 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300585 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500586
587 wayland_shm_buffer_attach(sb);
588
589 callback = wl_surface_frame(output->parent.surface);
590 wl_callback_add_listener(callback, &frame_listener, output);
591 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300592 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500593
594 pixman_region32_fini(&sb->damage);
595 pixman_region32_init(&sb->damage);
596 sb->frame_damaged = 0;
597
Giulio Camuffo954f1832014-10-11 18:27:30 +0300598 pixman_region32_subtract(&b->compositor->primary_plane.damage,
599 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500600 return 0;
601}
602
603static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500604wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700605{
606 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300607 struct wayland_backend *b =
608 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700609
Giulio Camuffo954f1832014-10-11 18:27:30 +0300610 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500611 pixman_renderer_output_destroy(output_base);
612 } else {
613 gl_renderer->output_destroy(output_base);
614 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100615
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500616 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500617 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500618 if (output->parent.shell_surface)
619 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500620
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600621 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500622 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600623
624 cairo_surface_destroy(output->gl.border.top);
625 cairo_surface_destroy(output->gl.border.left);
626 cairo_surface_destroy(output->gl.border.right);
627 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500628
629 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700630 free(output);
631
632 return;
633}
634
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300635static const struct wl_shell_surface_listener shell_surface_listener;
636
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200637static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500638wayland_output_init_gl_renderer(struct wayland_output *output)
639{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500640 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500641
642 if (output->frame) {
643 fwidth = frame_width(output->frame);
644 fheight = frame_height(output->frame);
645 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500646 fwidth = output->base.current_mode->width;
647 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500648 }
649
650 output->gl.egl_window =
651 wl_egl_window_create(output->parent.surface,
652 fwidth, fheight);
653 if (!output->gl.egl_window) {
654 weston_log("failure to create wl_egl_window\n");
655 return -1;
656 }
657
658 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000659 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100660 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000661 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500662 NULL,
663 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500664 goto cleanup_window;
665
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500666 return 0;
667
668cleanup_window:
669 wl_egl_window_destroy(output->gl.egl_window);
670 return -1;
671}
672
673static int
674wayland_output_init_pixman_renderer(struct wayland_output *output)
675{
676 return pixman_renderer_output_create(&output->base);
677}
678
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600679static void
680wayland_output_resize_surface(struct wayland_output *output)
681{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300682 struct wayland_backend *b =
683 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600684 struct wayland_shm_buffer *buffer, *next;
685 int32_t ix, iy, iwidth, iheight;
686 int32_t width, height;
687 struct wl_region *region;
688
689 width = output->base.current_mode->width;
690 height = output->base.current_mode->height;
691
692 if (output->frame) {
693 frame_resize_inside(output->frame, width, height);
694
695 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300696 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600697 wl_region_add(region, ix, iy, iwidth, iheight);
698 wl_surface_set_input_region(output->parent.surface, region);
699 wl_region_destroy(region);
700
701 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300702 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600703 wl_region_add(region, ix, iy, iwidth, iheight);
704 wl_surface_set_opaque_region(output->parent.surface, region);
705 wl_region_destroy(region);
706
707 width = frame_width(output->frame);
708 height = frame_height(output->frame);
709 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300710 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600711 wl_region_add(region, 0, 0, width, height);
712 wl_surface_set_input_region(output->parent.surface, region);
713 wl_region_destroy(region);
714
Giulio Camuffo954f1832014-10-11 18:27:30 +0300715 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600716 wl_region_add(region, 0, 0, width, height);
717 wl_surface_set_opaque_region(output->parent.surface, region);
718 wl_region_destroy(region);
719 }
720
721 if (output->gl.egl_window) {
722 wl_egl_window_resize(output->gl.egl_window,
723 width, height, 0, 0);
724
725 /* These will need to be re-created due to the resize */
726 gl_renderer->output_set_border(&output->base,
727 GL_RENDERER_BORDER_TOP,
728 0, 0, 0, NULL);
729 cairo_surface_destroy(output->gl.border.top);
730 output->gl.border.top = NULL;
731 gl_renderer->output_set_border(&output->base,
732 GL_RENDERER_BORDER_LEFT,
733 0, 0, 0, NULL);
734 cairo_surface_destroy(output->gl.border.left);
735 output->gl.border.left = NULL;
736 gl_renderer->output_set_border(&output->base,
737 GL_RENDERER_BORDER_RIGHT,
738 0, 0, 0, NULL);
739 cairo_surface_destroy(output->gl.border.right);
740 output->gl.border.right = NULL;
741 gl_renderer->output_set_border(&output->base,
742 GL_RENDERER_BORDER_BOTTOM,
743 0, 0, 0, NULL);
744 cairo_surface_destroy(output->gl.border.bottom);
745 output->gl.border.bottom = NULL;
746 }
747
748 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500749 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600750 wayland_shm_buffer_destroy(buffer);
751 /* These will get thrown away when they get released */
752 wl_list_for_each(buffer, &output->shm.buffers, link)
753 buffer->output = NULL;
754}
755
756static int
757wayland_output_set_windowed(struct wayland_output *output)
758{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300759 struct wayland_backend *b =
760 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600761 int tlen;
762 char *title;
763
764 if (output->frame)
765 return 0;
766
767 if (output->name) {
768 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
769 title = malloc(tlen + 1);
770 if (!title)
771 return -1;
772
773 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
774 } else {
775 title = strdup(WINDOW_TITLE);
776 }
777
Giulio Camuffo954f1832014-10-11 18:27:30 +0300778 if (!b->theme) {
779 b->theme = theme_create();
780 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600781 free(title);
782 return -1;
783 }
784 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300785 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600786 FRAME_BUTTON_CLOSE, title);
787 free(title);
788 if (!output->frame)
789 return -1;
790
791 if (output->keyboard_count)
792 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
793
794 wayland_output_resize_surface(output);
795
796 wl_shell_surface_set_toplevel(output->parent.shell_surface);
797
798 return 0;
799}
800
801static void
802wayland_output_set_fullscreen(struct wayland_output *output,
803 enum wl_shell_surface_fullscreen_method method,
804 uint32_t framerate, struct wl_output *target)
805{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300806 struct wayland_backend *b =
807 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500808
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600809 if (output->frame) {
810 frame_destroy(output->frame);
811 output->frame = NULL;
812 }
813
814 wayland_output_resize_surface(output);
815
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500816 if (output->parent.shell_surface) {
817 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
818 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300819 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800820 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
821 output->parent.surface,
822 method, target);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500823 }
824}
825
826static struct weston_mode *
827wayland_output_choose_mode(struct wayland_output *output,
828 struct weston_mode *ref_mode)
829{
830 struct weston_mode *mode;
831
832 /* First look for an exact match */
833 wl_list_for_each(mode, &output->base.mode_list, link)
834 if (mode->width == ref_mode->width &&
835 mode->height == ref_mode->height &&
836 mode->refresh == ref_mode->refresh)
837 return mode;
838
839 /* If we can't find an exact match, ignore refresh and try again */
840 wl_list_for_each(mode, &output->base.mode_list, link)
841 if (mode->width == ref_mode->width &&
842 mode->height == ref_mode->height)
843 return mode;
844
845 /* Yeah, we failed */
846 return NULL;
847}
848
849enum mode_status {
850 MODE_STATUS_UNKNOWN,
851 MODE_STATUS_SUCCESS,
852 MODE_STATUS_FAIL,
853 MODE_STATUS_CANCEL,
854};
855
856static void
857mode_feedback_successful(void *data,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800858 struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500859{
860 enum mode_status *value = data;
861
862 printf("Mode switch successful\n");
863
864 *value = MODE_STATUS_SUCCESS;
865}
866
867static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800868mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500869{
870 enum mode_status *value = data;
871
872 printf("Mode switch failed\n");
873
874 *value = MODE_STATUS_FAIL;
875}
876
877static void
Jonas Ådahl496adb32015-11-17 16:00:27 +0800878mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500879{
880 enum mode_status *value = data;
881
882 printf("Mode switch cancelled\n");
883
884 *value = MODE_STATUS_CANCEL;
885}
886
Jonas Ådahl496adb32015-11-17 16:00:27 +0800887struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500888 mode_feedback_successful,
889 mode_feedback_failed,
890 mode_feedback_cancelled,
891};
892
893static int
894wayland_output_switch_mode(struct weston_output *output_base,
895 struct weston_mode *mode)
896{
897 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300898 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500899 struct wl_surface *old_surface;
900 struct weston_mode *old_mode;
Jonas Ådahl496adb32015-11-17 16:00:27 +0800901 struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500902 enum mode_status mode_status;
903 int ret = 0;
904
905 if (output_base == NULL) {
906 weston_log("output is NULL.\n");
907 return -1;
908 }
909
910 if (mode == NULL) {
911 weston_log("mode is NULL.\n");
912 return -1;
913 }
914
Giulio Camuffo954f1832014-10-11 18:27:30 +0300915 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500916
Giulio Camuffo954f1832014-10-11 18:27:30 +0300917 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500918 return -1;
919
920 mode = wayland_output_choose_mode(output, mode);
921 if (mode == NULL)
922 return -1;
923
924 if (output->base.current_mode == mode)
925 return 0;
926
927 old_mode = output->base.current_mode;
928 old_surface = output->parent.surface;
929 output->base.current_mode = mode;
930 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300931 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500932 wl_surface_set_user_data(output->parent.surface, output);
933
934 /* Blow the old buffers because we changed size/surfaces */
935 wayland_output_resize_surface(output);
936
937 mode_feedback =
Jonas Ådahl496adb32015-11-17 16:00:27 +0800938 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
939 output->parent.surface,
940 output->parent.output,
941 mode->refresh);
942 zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback,
943 &mode_feedback_listener,
944 &mode_status);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500945
946 /* This should kick-start things again */
947 output->parent.draw_initial_frame = 1;
948 wayland_output_start_repaint_loop(&output->base);
949
950 mode_status = MODE_STATUS_UNKNOWN;
951 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300952 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500953
Jonas Ådahl496adb32015-11-17 16:00:27 +0800954 zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500955
956 if (mode_status == MODE_STATUS_FAIL) {
957 output->base.current_mode = old_mode;
958 wl_surface_destroy(output->parent.surface);
959 output->parent.surface = old_surface;
960 wayland_output_resize_surface(output);
961
962 return -1;
963 }
964
965 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
966 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
967
Giulio Camuffo954f1832014-10-11 18:27:30 +0300968 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500969 pixman_renderer_output_destroy(output_base);
970 if (wayland_output_init_pixman_renderer(output) < 0)
971 goto err_output;
972 } else {
973 gl_renderer->output_destroy(output_base);
974 wl_egl_window_destroy(output->gl.egl_window);
975 if (wayland_output_init_gl_renderer(output) < 0)
976 goto err_output;
977 }
978 wl_surface_destroy(old_surface);
979
980 weston_output_schedule_repaint(&output->base);
981
982 return 0;
983
984err_output:
985 /* XXX */
986 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600987}
988
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500989static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300990wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600991 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500992 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100993{
994 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500995 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600996
997 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
998 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100999
Peter Huttererf3d62272013-08-08 11:57:05 +10001000 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001001 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001002 return NULL;
1003
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001004 output->name = name ? strdup(name) : NULL;
1005 output->base.make = "waywayland";
1006 output->base.model = "none";
1007
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001008 output_width = width * scale;
1009 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001010
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001011 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001012 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001013 if (!output->parent.surface)
1014 goto err_name;
1015 wl_surface_set_user_data(output->parent.surface, output);
1016
1017 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001018
Giulio Camuffo954f1832014-10-11 18:27:30 +03001019 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001020 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001021 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001022 output->parent.surface);
1023 if (!output->parent.shell_surface)
1024 goto err_surface;
1025 wl_shell_surface_add_listener(output->parent.shell_surface,
1026 &shell_surface_listener, output);
1027 }
1028
Giulio Camuffo954f1832014-10-11 18:27:30 +03001029 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001030 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1031 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001032 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001033 if (!width)
1034 output_width = output->parent.configure_width;
1035 if (!height)
1036 output_height = output->parent.configure_height;
1037 }
1038
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001039 output->mode.flags =
1040 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001041 output->mode.width = output_width;
1042 output->mode.height = output_height;
1043 output->mode.refresh = 60000;
1044 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001045 wl_list_init(&output->base.mode_list);
1046 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001047 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001048
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001049 wl_list_init(&output->shm.buffers);
1050 wl_list_init(&output->shm.free_buffers);
1051
Giulio Camuffo954f1832014-10-11 18:27:30 +03001052 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001053 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001054
Giulio Camuffo954f1832014-10-11 18:27:30 +03001055 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001056 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001057 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001058 output->base.repaint = wayland_output_repaint_pixman;
1059 } else {
1060 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001061 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001062 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001063 }
1064
Jonas Ådahle5a12252013-04-05 23:07:11 +02001065 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001066 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001067 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001068 output->base.set_backlight = NULL;
1069 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001070 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001071
Giulio Camuffo954f1832014-10-11 18:27:30 +03001072 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001073
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001074 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001075
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001076err_output:
1077 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001078 if (output->parent.shell_surface)
1079 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001080err_surface:
1081 wl_surface_destroy(output->parent.surface);
1082err_name:
1083 free(output->name);
1084
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001085 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001086 free(output);
1087
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001088 return NULL;
1089}
1090
1091static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001092wayland_output_create_for_config(struct wayland_backend *b,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001093 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001094 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001095 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001096{
1097 struct wayland_output *output;
1098 char *mode, *t, *name, *str;
1099 int width, height, scale;
1100 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001101 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001102
1103 weston_config_section_get_string(config_section, "name", &name, NULL);
1104 if (name) {
1105 slen = strlen(name);
1106 slen += strlen(WINDOW_TITLE " - ");
1107 str = malloc(slen + 1);
1108 if (str)
1109 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1110 free(name);
1111 name = str;
1112 }
1113 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001114 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001115
1116 weston_config_section_get_string(config_section,
1117 "mode", &mode, "1024x600");
1118 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1119 weston_log("Invalid mode \"%s\" for output %s\n",
1120 mode, name);
1121 width = 1024;
1122 height = 640;
1123 }
1124 free(mode);
1125
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001126 if (option_width)
1127 width = option_width;
1128 if (option_height)
1129 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001130
1131 weston_config_section_get_int(config_section, "scale", &scale, 1);
1132
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001133 if (option_scale)
1134 scale = option_scale;
1135
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001136 weston_config_section_get_string(config_section,
1137 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001138 if (weston_parse_transform(t, &transform) < 0)
1139 weston_log("Invalid transform \"%s\" for output %s\n",
1140 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001141 free(t);
1142
Giulio Camuffo954f1832014-10-11 18:27:30 +03001143 output = wayland_output_create(b, x, y, width, height, name, 0,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001144 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001145 free(name);
1146
1147 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001148}
1149
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001150static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001151wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001152 struct wayland_parent_output *poutput)
1153{
1154 struct wayland_output *output;
1155 struct weston_mode *mode;
1156 int32_t x;
1157
1158 if (poutput->current_mode) {
1159 mode = poutput->current_mode;
1160 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001161 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001162 } else if (!wl_list_empty(&poutput->mode_list)) {
1163 mode = container_of(poutput->mode_list.next,
1164 struct weston_mode, link);
1165 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001166 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001167 return NULL;
1168 }
1169
Giulio Camuffo954f1832014-10-11 18:27:30 +03001170 if (!wl_list_empty(&b->compositor->output_list)) {
1171 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001172 struct wayland_output, base.link);
1173 x = output->base.x + output->base.current_mode->width;
1174 } else {
1175 x = 0;
1176 }
1177
Giulio Camuffo954f1832014-10-11 18:27:30 +03001178 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001179 NULL, 0,
1180 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1181 if (!output)
1182 return NULL;
1183
1184 output->parent.output = poutput->global;
1185
1186 output->base.make = poutput->physical.make;
1187 output->base.model = poutput->physical.model;
1188 wl_list_init(&output->base.mode_list);
1189 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1190 wl_list_init(&poutput->mode_list);
1191
1192 wayland_output_set_fullscreen(output,
1193 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1194 mode->refresh, poutput->global);
1195
1196 if (output->parent.shell_surface) {
1197 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1198 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1199 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001200 } else if (b->parent.fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +08001201 zwp_fullscreen_shell_v1_present_surface(b->parent.fshell,
1202 output->parent.surface,
1203 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER,
1204 poutput->global);
1205 zwp_fullscreen_shell_mode_feedback_v1_destroy(
1206 zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell,
1207 output->parent.surface,
1208 poutput->global,
1209 mode->refresh));
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001210 }
1211
1212 return output;
1213}
1214
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001215static void
1216shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1217 uint32_t serial)
1218{
1219 wl_shell_surface_pong(shell_surface, serial);
1220}
1221
1222static void
1223shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1224 uint32_t edges, int32_t width, int32_t height)
1225{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001226 struct wayland_output *output = data;
1227
1228 output->parent.configure_width = width;
1229 output->parent.configure_height = height;
1230
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001231 /* FIXME: implement resizing */
1232}
1233
1234static void
1235shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1236{
1237}
1238
1239static const struct wl_shell_surface_listener shell_surface_listener = {
1240 shell_surface_ping,
1241 shell_surface_configure,
1242 shell_surface_popup_done
1243};
1244
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001245/* Events received from the wayland-server this compositor is client of: */
1246
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001248static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001249input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001250{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001251
Jason Ekstrand7744f712013-10-27 22:24:55 -05001252 struct wl_buffer *buffer;
1253 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001254
Giulio Camuffo954f1832014-10-11 18:27:30 +03001255 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001256 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001257
Giulio Camuffo954f1832014-10-11 18:27:30 +03001258 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001259 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001260 if (!buffer)
1261 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001262
1263 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1264 input->parent.cursor.surface,
1265 image->hotspot_x, image->hotspot_y);
1266
1267 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1268 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1269 image->width, image->height);
1270 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001271}
1272
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001273static void
Daniel Stone37816df2012-05-16 18:45:18 +01001274input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1275 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001276 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001277{
1278 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001279 int32_t fx, fy;
1280 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001281
Daniel Stone50692802012-06-22 13:21:41 +01001282 /* XXX: If we get a modifier event immediately before the focus,
1283 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001284 input->enter_serial = serial;
1285 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001286
1287 if (input->output->frame) {
1288 location = frame_pointer_enter(input->output->frame, input,
1289 wl_fixed_to_int(x),
1290 wl_fixed_to_int(y));
1291 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1292 x -= wl_fixed_from_int(fx);
1293 y -= wl_fixed_from_int(fy);
1294
1295 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1296 weston_output_schedule_repaint(&input->output->base);
1297 } else {
1298 location = THEME_LOCATION_CLIENT_AREA;
1299 }
1300
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001301 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001302
1303 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001304 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001305 notify_pointer_focus(&input->base, &input->output->base, x, y);
1306 wl_pointer_set_cursor(input->parent.pointer,
1307 input->enter_serial, NULL, 0, 0);
1308 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001309 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001310 notify_pointer_focus(&input->base, NULL, 0, 0);
1311 input_set_cursor(input);
1312 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001313}
1314
1315static void
Daniel Stone37816df2012-05-16 18:45:18 +01001316input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1317 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001318{
1319 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001320
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001321 if (!input->output)
1322 return;
1323
Jason Ekstrand7744f712013-10-27 22:24:55 -05001324 if (input->output->frame) {
1325 frame_pointer_leave(input->output->frame, input);
1326
1327 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1328 weston_output_schedule_repaint(&input->output->base);
1329 }
1330
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001331 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001332 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001333 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001334}
1335
1336static void
Daniel Stone37816df2012-05-16 18:45:18 +01001337input_handle_motion(void *data, struct wl_pointer *pointer,
1338 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1339{
1340 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001341 int32_t fx, fy;
1342 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001343
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001344 if (!input->output)
1345 return;
1346
Jason Ekstrand7744f712013-10-27 22:24:55 -05001347 if (input->output->frame) {
1348 location = frame_pointer_motion(input->output->frame, input,
1349 wl_fixed_to_int(x),
1350 wl_fixed_to_int(y));
1351 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1352 x -= wl_fixed_from_int(fx);
1353 y -= wl_fixed_from_int(fy);
1354
1355 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1356 weston_output_schedule_repaint(&input->output->base);
1357 } else {
1358 location = THEME_LOCATION_CLIENT_AREA;
1359 }
1360
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001361 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001362
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001363 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001364 input_set_cursor(input);
1365 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001366 input->has_focus = false;
1367 } else if (!input->has_focus &&
1368 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001369 wl_pointer_set_cursor(input->parent.pointer,
1370 input->enter_serial, NULL, 0, 0);
1371 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001372 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001373 }
1374
1375 if (location == THEME_LOCATION_CLIENT_AREA)
1376 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001377}
1378
1379static void
1380input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001381 uint32_t serial, uint32_t time, uint32_t button,
1382 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001383{
1384 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001385 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001386 enum frame_button_state fstate;
1387 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001388
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001389 if (!input->output)
1390 return;
1391
Jason Ekstrand7744f712013-10-27 22:24:55 -05001392 if (input->output->frame) {
1393 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1394 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1395
1396 location = frame_pointer_button(input->output->frame, input,
1397 button, fstate);
1398
1399 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1400
1401 wl_shell_surface_move(input->output->parent.shell_surface,
1402 input->parent.seat, serial);
1403 frame_status_clear(input->output->frame,
1404 FRAME_STATUS_MOVE);
1405 return;
1406 }
1407
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001408 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1409 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001410 input->output = NULL;
1411 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001412
Giulio Camuffo954f1832014-10-11 18:27:30 +03001413 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001414 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001415
1416 return;
1417 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001418
1419 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1420 weston_output_schedule_repaint(&input->output->base);
1421 } else {
1422 location = THEME_LOCATION_CLIENT_AREA;
1423 }
1424
1425 if (location == THEME_LOCATION_CLIENT_AREA)
1426 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001427}
1428
1429static void
1430input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001431 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001432{
1433 struct wayland_input *input = data;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001434 struct weston_pointer_axis_event weston_event;
Daniel Stone37816df2012-05-16 18:45:18 +01001435
Peter Hutterer89b6a492016-01-18 15:58:17 +10001436 weston_event.axis = axis;
1437 weston_event.value = value;
1438
1439 notify_axis(&input->base, time, &weston_event);
Daniel Stone37816df2012-05-16 18:45:18 +01001440}
1441
1442static const struct wl_pointer_listener pointer_listener = {
1443 input_handle_pointer_enter,
1444 input_handle_pointer_leave,
1445 input_handle_motion,
1446 input_handle_button,
1447 input_handle_axis,
1448};
1449
1450static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001451input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1452 int fd, uint32_t size)
1453{
1454 struct wayland_input *input = data;
1455 struct xkb_keymap *keymap;
1456 char *map_str;
1457
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001458 if (!data) {
1459 close(fd);
1460 return;
1461 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001462
1463 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1464 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1465 if (map_str == MAP_FAILED) {
1466 weston_log("mmap failed: %m\n");
1467 goto error;
1468 }
1469
Giulio Camuffo954f1832014-10-11 18:27:30 +03001470 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001471 map_str,
1472 XKB_KEYMAP_FORMAT_TEXT_V1,
1473 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001474 munmap(map_str, size);
1475
1476 if (!keymap) {
1477 weston_log("failed to compile keymap\n");
1478 goto error;
1479 }
1480
1481 input->keyboard_state_update = STATE_UPDATE_NONE;
1482 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1483 weston_log("No keymap provided; falling back to defalt\n");
1484 keymap = NULL;
1485 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1486 } else {
1487 weston_log("Invalid keymap\n");
1488 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001489 }
1490
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001491 close(fd);
1492
Derek Foreman1281a362015-07-31 16:55:32 -05001493 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001494 weston_seat_update_keymap(&input->base, keymap);
1495 else
1496 weston_seat_init_keyboard(&input->base, keymap);
1497
Ran Benitac9c74152014-08-19 23:59:52 +03001498 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001499
1500 return;
1501
1502error:
1503 wl_keyboard_release(input->parent.keyboard);
1504 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001505}
1506
1507static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001508input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001509 struct wl_keyboard *keyboard,
1510 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001511 struct wl_surface *surface,
1512 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001513{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001514 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001515 struct wayland_output *focus;
1516
1517 focus = input->keyboard_focus;
1518 if (focus) {
1519 /* This shouldn't happen */
1520 focus->keyboard_count--;
1521 if (!focus->keyboard_count && focus->frame)
1522 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1523 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1524 weston_output_schedule_repaint(&focus->base);
1525 }
1526
1527 input->keyboard_focus = wl_surface_get_user_data(surface);
1528 input->keyboard_focus->keyboard_count++;
1529
1530 focus = input->keyboard_focus;
1531 if (focus->frame) {
1532 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1533 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1534 weston_output_schedule_repaint(&focus->base);
1535 }
1536
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001537
Daniel Stone50692802012-06-22 13:21:41 +01001538 /* XXX: If we get a modifier event immediately before the focus,
1539 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001540 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001541 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001542}
1543
1544static void
1545input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001546 struct wl_keyboard *keyboard,
1547 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001548 struct wl_surface *surface)
1549{
1550 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001551 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001552
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001553 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001554
1555 focus = input->keyboard_focus;
1556 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001557 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001558
1559 focus->keyboard_count--;
1560 if (!focus->keyboard_count && focus->frame) {
1561 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1562 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1563 weston_output_schedule_repaint(&focus->base);
1564 }
1565
1566 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001567}
1568
Daniel Stone37816df2012-05-16 18:45:18 +01001569static void
1570input_handle_key(void *data, struct wl_keyboard *keyboard,
1571 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1572{
1573 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001574
Daniel Stone50692802012-06-22 13:21:41 +01001575 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001576 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001577 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001578 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001579 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001580}
1581
Daniel Stone351eb612012-05-31 15:27:47 -04001582static void
Derek Foreman1281a362015-07-31 16:55:32 -05001583input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001584 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001585 uint32_t mods_latched, uint32_t mods_locked,
1586 uint32_t group)
1587{
Derek Foreman1281a362015-07-31 16:55:32 -05001588 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001589 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001590 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001591 uint32_t serial_out;
1592
1593 /* If we get a key event followed by a modifier event with the
1594 * same serial number, then we try to preserve those semantics by
1595 * reusing the same serial number on the way out too. */
1596 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001597 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001598 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001599 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001600
Derek Foreman1281a362015-07-31 16:55:32 -05001601 keyboard = weston_seat_get_keyboard(&input->base);
1602 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001603 mods_depressed, mods_latched,
1604 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001605 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001606}
1607
Jonny Lamb497994a2014-08-12 14:58:26 +02001608static void
1609input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1610 int32_t rate, int32_t delay)
1611{
1612 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001613 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001614
Giulio Camuffo954f1832014-10-11 18:27:30 +03001615 b->compositor->kb_repeat_rate = rate;
1616 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001617}
1618
Daniel Stone37816df2012-05-16 18:45:18 +01001619static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001620 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001621 input_handle_keyboard_enter,
1622 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001623 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001624 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001625 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001626};
1627
1628static void
Derek Foreman748c6952015-11-06 15:56:10 -06001629input_handle_touch_down(void *data, struct wl_touch *wl_touch,
1630 uint32_t serial, uint32_t time,
1631 struct wl_surface *surface, int32_t id, wl_fixed_t x,
1632 wl_fixed_t y)
1633{
1634 struct wayland_input *input = data;
1635 struct wayland_output *output;
1636 enum theme_location location;
1637 bool first_touch;
1638 int32_t fx, fy;
1639
1640 first_touch = (input->touch_points == 0);
1641 input->touch_points++;
1642
1643 input->touch_focus = wl_surface_get_user_data(surface);
1644 output = input->touch_focus;
1645 if (!first_touch && !input->touch_active)
1646 return;
1647
1648 if (output->frame) {
1649 location = frame_touch_down(output->frame, input, id,
1650 wl_fixed_to_int(x),
1651 wl_fixed_to_int(y));
1652
1653 frame_interior(output->frame, &fx, &fy, NULL, NULL);
1654 x -= wl_fixed_from_int(fx);
1655 y -= wl_fixed_from_int(fy);
1656
1657 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1658 weston_output_schedule_repaint(&output->base);
1659
1660 if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) {
1661 input->touch_points--;
1662 wl_shell_surface_move(output->parent.shell_surface,
1663 input->parent.seat, serial);
1664 frame_status_clear(output->frame,
1665 FRAME_STATUS_MOVE);
1666 return;
1667 }
1668
1669 if (first_touch && location != THEME_LOCATION_CLIENT_AREA)
1670 return;
1671 }
1672
1673 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1674
1675 notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN);
1676 input->touch_active = true;
1677}
1678
1679static void
1680input_handle_touch_up(void *data, struct wl_touch *wl_touch,
1681 uint32_t serial, uint32_t time, int32_t id)
1682{
1683 struct wayland_input *input = data;
1684 struct wayland_output *output = input->touch_focus;
1685 bool active = input->touch_active;
1686
1687 input->touch_points--;
1688 if (input->touch_points == 0) {
1689 input->touch_focus = NULL;
1690 input->touch_active = false;
1691 }
1692
1693 if (!output)
1694 return;
1695
1696 if (output->frame) {
1697 frame_touch_up(output->frame, input, id);
1698
1699 if (frame_status(output->frame) & FRAME_STATUS_CLOSE) {
1700 wayland_output_destroy(&output->base);
1701 input->touch_focus = NULL;
1702 input->keyboard_focus = NULL;
1703 if (wl_list_empty(&input->backend->compositor->output_list))
1704 weston_compositor_exit(input->backend->compositor);
1705
1706 return;
1707 }
1708 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
1709 weston_output_schedule_repaint(&output->base);
1710 }
1711
1712 if (active)
1713 notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP);
1714}
1715
1716static void
1717input_handle_touch_motion(void *data, struct wl_touch *wl_touch,
1718 uint32_t time, int32_t id, wl_fixed_t x,
1719 wl_fixed_t y)
1720{
1721 struct wayland_input *input = data;
1722 struct wayland_output *output = input->touch_focus;
1723 int32_t fx, fy;
1724
1725 if (!output || !input->touch_active)
1726 return;
1727
1728 if (output->frame) {
1729 frame_interior(output->frame, &fx, &fy, NULL, NULL);
1730 x -= wl_fixed_from_int(fx);
1731 y -= wl_fixed_from_int(fy);
1732 }
1733
1734 weston_output_transform_coordinate(&output->base, x, y, &x, &y);
1735
1736 notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION);
1737}
1738
1739static void
1740input_handle_touch_frame(void *data, struct wl_touch *wl_touch)
1741{
1742 struct wayland_input *input = data;
1743
1744 if (!input->touch_focus || !input->touch_active)
1745 return;
1746
1747 notify_touch_frame(&input->base);
1748}
1749
1750static void
1751input_handle_touch_cancel(void *data, struct wl_touch *wl_touch)
1752{
1753 struct wayland_input *input = data;
1754
1755 if (!input->touch_focus || !input->touch_active)
1756 return;
1757
1758 notify_touch_cancel(&input->base);
1759}
1760
1761static const struct wl_touch_listener touch_listener = {
1762 input_handle_touch_down,
1763 input_handle_touch_up,
1764 input_handle_touch_motion,
1765 input_handle_touch_frame,
1766 input_handle_touch_cancel,
1767};
1768
1769
1770static void
Daniel Stone37816df2012-05-16 18:45:18 +01001771input_handle_capabilities(void *data, struct wl_seat *seat,
1772 enum wl_seat_capability caps)
1773{
1774 struct wayland_input *input = data;
1775
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001776 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1777 input->parent.pointer = wl_seat_get_pointer(seat);
1778 wl_pointer_set_user_data(input->parent.pointer, input);
1779 wl_pointer_add_listener(input->parent.pointer,
1780 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001781 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001782 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001783 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
1784 wl_pointer_release(input->parent.pointer);
1785 else
1786 wl_pointer_destroy(input->parent.pointer);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001787 input->parent.pointer = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001788 weston_seat_release_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001789 }
1790
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001791 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1792 input->parent.keyboard = wl_seat_get_keyboard(seat);
1793 wl_keyboard_set_user_data(input->parent.keyboard, input);
1794 wl_keyboard_add_listener(input->parent.keyboard,
1795 &keyboard_listener, input);
1796 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
Derek Foremancfce7d02015-11-06 15:56:08 -06001797 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
1798 wl_keyboard_release(input->parent.keyboard);
1799 else
1800 wl_keyboard_destroy(input->parent.keyboard);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001801 input->parent.keyboard = NULL;
Derek Foremancfce7d02015-11-06 15:56:08 -06001802 weston_seat_release_keyboard(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001803 }
Derek Foreman748c6952015-11-06 15:56:10 -06001804
1805 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) {
1806 input->parent.touch = wl_seat_get_touch(seat);
1807 wl_touch_set_user_data(input->parent.touch, input);
1808 wl_touch_add_listener(input->parent.touch,
1809 &touch_listener, input);
1810 weston_seat_init_touch(&input->base);
1811 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) {
1812 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
1813 wl_touch_release(input->parent.touch);
1814 else
1815 wl_touch_destroy(input->parent.touch);
1816 input->parent.touch = NULL;
1817 weston_seat_release_touch(&input->base);
1818 }
Daniel Stone37816df2012-05-16 18:45:18 +01001819}
1820
Jonny Lamb497994a2014-08-12 14:58:26 +02001821static void
1822input_handle_name(void *data, struct wl_seat *seat,
1823 const char *name)
1824{
1825}
1826
Daniel Stone37816df2012-05-16 18:45:18 +01001827static const struct wl_seat_listener seat_listener = {
1828 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001829 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001830};
1831
1832static void
Derek Foremancfce7d02015-11-06 15:56:08 -06001833display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001834{
1835 struct wayland_input *input;
Derek Foremancfce7d02015-11-06 15:56:08 -06001836 uint32_t version = MIN(available_version, 4);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001837
Peter Huttererf3d62272013-08-08 11:57:05 +10001838 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001839 if (input == NULL)
1840 return;
1841
Giulio Camuffo954f1832014-10-11 18:27:30 +03001842 weston_seat_init(&input->base, b->compositor, "default");
1843 input->backend = b;
1844 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Derek Foremancfce7d02015-11-06 15:56:08 -06001845 &wl_seat_interface, version);
1846 input->seat_version = version;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001847 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001848
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001849 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1850 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001851
1852 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001853 wl_compositor_create_surface(b->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001854}
1855
1856static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001857wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1858 int32_t x, int32_t y,
1859 int32_t physical_width, int32_t physical_height,
1860 int32_t subpixel, const char *make,
1861 const char *model, int32_t transform)
1862{
1863 struct wayland_parent_output *output = data;
1864
1865 output->x = x;
1866 output->y = y;
1867 output->physical.width = physical_width;
1868 output->physical.height = physical_height;
1869 output->physical.subpixel = subpixel;
1870
1871 free(output->physical.make);
1872 output->physical.make = strdup(make);
1873 free(output->physical.model);
1874 output->physical.model = strdup(model);
1875
1876 output->transform = transform;
1877}
1878
1879static struct weston_mode *
1880find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1881{
1882 struct weston_mode *mode;
1883
1884 wl_list_for_each(mode, list, link) {
1885 if (mode->width == width && mode->height == height &&
1886 mode->refresh == refresh)
1887 return mode;
1888 }
1889
1890 mode = zalloc(sizeof *mode);
1891 if (!mode)
1892 return NULL;
1893
1894 mode->width = width;
1895 mode->height = height;
1896 mode->refresh = refresh;
1897 wl_list_insert(list, &mode->link);
1898
1899 return mode;
1900}
1901
1902static void
1903wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1904 uint32_t flags, int32_t width, int32_t height,
1905 int32_t refresh)
1906{
1907 struct wayland_parent_output *output = data;
1908 struct weston_mode *mode;
1909
1910 if (output->output) {
1911 mode = find_mode(&output->output->base.mode_list,
1912 width, height, refresh);
1913 if (!mode)
1914 return;
1915 mode->flags = flags;
1916 /* Do a mode-switch on current mode change? */
1917 } else {
1918 mode = find_mode(&output->mode_list, width, height, refresh);
1919 if (!mode)
1920 return;
1921 mode->flags = flags;
1922 if (flags & WL_OUTPUT_MODE_CURRENT)
1923 output->current_mode = mode;
1924 if (flags & WL_OUTPUT_MODE_PREFERRED)
1925 output->preferred_mode = mode;
1926 }
1927}
1928
1929static const struct wl_output_listener output_listener = {
1930 wayland_parent_output_geometry,
1931 wayland_parent_output_mode
1932};
1933
1934static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001935wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001936{
1937 struct wayland_parent_output *output;
1938
1939 output = zalloc(sizeof *output);
1940 if (!output)
1941 return;
1942
1943 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001944 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001945 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001946 if (!output->global) {
1947 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001948 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001949 }
1950
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001951 wl_output_add_listener(output->global, &output_listener, output);
1952
1953 output->scale = 0;
1954 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1955 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1956 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001957 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001958
Giulio Camuffo954f1832014-10-11 18:27:30 +03001959 if (b->sprawl_across_outputs) {
1960 wl_display_roundtrip(b->parent.wl_display);
1961 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001962 }
1963}
1964
1965static void
1966wayland_parent_output_destroy(struct wayland_parent_output *output)
1967{
1968 struct weston_mode *mode, *next;
1969
1970 if (output->output)
1971 wayland_output_destroy(&output->output->base);
1972
1973 wl_output_destroy(output->global);
1974 free(output->physical.make);
1975 free(output->physical.model);
1976
1977 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1978 wl_list_remove(&mode->link);
1979 free(mode);
1980 }
1981}
1982
1983static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001984registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1985 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001986{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001987 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001988
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001989 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001990 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001991 wl_registry_bind(registry, name,
1992 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001993 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001994 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001995 wl_registry_bind(registry, name,
1996 &wl_shell_interface, 1);
Jonas Ådahl496adb32015-11-17 16:00:27 +08001997 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001998 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001999 wl_registry_bind(registry, name,
Jonas Ådahl496adb32015-11-17 16:00:27 +08002000 &zwp_fullscreen_shell_v1_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01002001 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002002 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002003 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002004 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002005 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002006 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02002007 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002008 }
2009}
2010
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002011static void
2012registry_handle_global_remove(void *data, struct wl_registry *registry,
2013 uint32_t name)
2014{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002015 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002016 struct wayland_parent_output *output;
2017
Giulio Camuffo954f1832014-10-11 18:27:30 +03002018 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002019 if (output->id == name)
2020 wayland_parent_output_destroy(output);
2021}
2022
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002023static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002024 registry_handle_global,
2025 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04002026};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002027
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002028static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002029wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002030{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002031 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002032 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002033
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002034 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03002035 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07002036 return 0;
2037 }
2038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002039 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002040 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05002041 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002042 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04002043
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002044 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045 count = wl_display_dispatch_pending(b->parent.wl_display);
2046 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04002047 }
2048
2049 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002050}
2051
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002052static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002053wayland_restore(struct weston_compositor *ec)
2054{
2055}
2056
2057static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002058wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002059{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002060 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002061
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07002063
Giulio Camuffo954f1832014-10-11 18:27:30 +03002064 if (b->parent.shm)
2065 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02002066
Giulio Camuffo954f1832014-10-11 18:27:30 +03002067 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002068}
2069
Jason Ekstrand7744f712013-10-27 22:24:55 -05002070static const char *left_ptrs[] = {
2071 "left_ptr",
2072 "default",
2073 "top_left_arrow",
2074 "left-arrow"
2075};
2076
2077static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002078create_cursor(struct wayland_backend *b, struct weston_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05002079{
2080 struct weston_config_section *s;
2081 int size;
2082 char *theme = NULL;
2083 unsigned int i;
2084
2085 s = weston_config_get_section(config, "shell", NULL, NULL);
2086 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
2087 weston_config_section_get_int(s, "cursor-size", &size, 32);
2088
Giulio Camuffo954f1832014-10-11 18:27:30 +03002089 b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
2090 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01002091 fprintf(stderr, "could not load cursor theme\n");
2092 return;
2093 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05002094
U. Artie Eoffff755002014-01-17 12:36:58 -08002095 free(theme);
2096
Giulio Camuffo954f1832014-10-11 18:27:30 +03002097 b->cursor = NULL;
2098 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
2099 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05002100 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002101 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05002102 fprintf(stderr, "could not load left cursor\n");
2103 return;
2104 }
2105}
2106
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002107static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002108fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
2109 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002110{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002111 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002112 struct wayland_input *input = NULL;
2113
Giulio Camuffo954f1832014-10-11 18:27:30 +03002114 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05002115 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002116 break;
2117
2118 if (!input || !input->output)
2119 return;
2120
2121 if (input->output->frame)
2122 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
2123 else
2124 wayland_output_set_windowed(input->output);
2125
2126 weston_output_schedule_repaint(&input->output->base);
2127}
2128
Giulio Camuffo954f1832014-10-11 18:27:30 +03002129static struct wayland_backend *
2130wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
2131 const char *display_name, int *argc, char *argv[],
2132 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002133{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002134 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002135 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002137
Giulio Camuffo954f1832014-10-11 18:27:30 +03002138 b = zalloc(sizeof *b);
2139 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002140 return NULL;
2141
Giulio Camuffo954f1832014-10-11 18:27:30 +03002142 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002143 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002144 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002145
Giulio Camuffo954f1832014-10-11 18:27:30 +03002146 b->parent.wl_display = wl_display_connect(display_name);
2147 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002148 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02002149 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002150 }
2151
Giulio Camuffo954f1832014-10-11 18:27:30 +03002152 wl_list_init(&b->parent.output_list);
2153 wl_list_init(&b->input_list);
2154 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
2155 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
2156 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05002157
Giulio Camuffo954f1832014-10-11 18:27:30 +03002158 create_cursor(b, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002159
Giulio Camuffo954f1832014-10-11 18:27:30 +03002160 b->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002161
Giulio Camuffo954f1832014-10-11 18:27:30 +03002162 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002163 gl_renderer = weston_load_module("gl-renderer.so",
2164 "gl_renderer_interface");
2165 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002166 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002167 }
2168
Giulio Camuffo954f1832014-10-11 18:27:30 +03002169 if (!b->use_pixman) {
2170 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05002171 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002172 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05002173 gl_renderer->alpha_attribs,
2174 NULL,
2175 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002176 weston_log("Failed to initialize the GL renderer; "
2177 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002178 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002179 }
2180 }
2181
Giulio Camuffo954f1832014-10-11 18:27:30 +03002182 if (b->use_pixman) {
2183 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002184 weston_log("Failed to initialize pixman renderer\n");
2185 goto err_display;
2186 }
2187 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002188
Giulio Camuffo954f1832014-10-11 18:27:30 +03002189 b->base.destroy = wayland_destroy;
2190 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002191
Giulio Camuffo954f1832014-10-11 18:27:30 +03002192 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002193
Giulio Camuffo954f1832014-10-11 18:27:30 +03002194 fd = wl_display_get_fd(b->parent.wl_display);
2195 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002196 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002197 wayland_backend_handle_event, b);
2198 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002199 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002200
Giulio Camuffo954f1832014-10-11 18:27:30 +03002201 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002202
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002203 if (compositor->renderer->import_dmabuf) {
2204 if (linux_dmabuf_setup(compositor) < 0)
2205 weston_log("Error: initializing dmabuf "
2206 "support failed.\n");
2207 }
2208
Giulio Camuffo954f1832014-10-11 18:27:30 +03002209 compositor->backend = &b->base;
2210 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002211err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002212 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002213err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002214 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002215 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002216 return NULL;
2217}
2218
2219static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002220wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002221{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002222 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002223
Giulio Camuffo954f1832014-10-11 18:27:30 +03002224 if (b->theme)
2225 theme_destroy(b->theme);
2226 if (b->frame_device)
2227 cairo_device_destroy(b->frame_device);
2228 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002229
Giulio Camuffo954f1832014-10-11 18:27:30 +03002230 weston_compositor_shutdown(b->compositor);
2231 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002232}
2233
Giulio Camuffo954f1832014-10-11 18:27:30 +03002234WL_EXPORT int
2235backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002236 struct weston_config *config,
2237 struct weston_backend_config *config_base)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002238{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002239 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002240 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002241 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002242 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002243 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002244 const char *section_name, *display_name;
2245 char *name;
2246
2247 const struct weston_option wayland_options[] = {
2248 { WESTON_OPTION_INTEGER, "width", 0, &width },
2249 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002250 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002251 { WESTON_OPTION_STRING, "display", 0, &display_name },
2252 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2253 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002254 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002255 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002256 };
2257
2258 width = 0;
2259 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002260 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002261 display_name = NULL;
2262 use_pixman = 0;
2263 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002264 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002265 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002266 parse_options(wayland_options,
2267 ARRAY_LENGTH(wayland_options), argc, argv);
2268
Giulio Camuffo954f1832014-10-11 18:27:30 +03002269 b = wayland_backend_create(compositor, use_pixman, display_name,
2270 argc, argv, config);
2271 if (!b)
2272 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002273
Giulio Camuffo954f1832014-10-11 18:27:30 +03002274 if (sprawl || b->parent.fshell) {
2275 b->sprawl_across_outputs = 1;
2276 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002277
Giulio Camuffo954f1832014-10-11 18:27:30 +03002278 wl_list_for_each(poutput, &b->parent.output_list, link)
2279 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002280
Giulio Camuffo954f1832014-10-11 18:27:30 +03002281 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002282 }
2283
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002284 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002285 output = wayland_output_create(b, 0, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002286 NULL, 1, 0, 1);
2287 if (!output)
2288 goto err_outputs;
2289
Axel Davydd8b88d2013-11-17 21:34:16 +01002290 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002291 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002292 }
2293
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002294 section = NULL;
2295 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002296 while (weston_config_next_section(config, &section, &section_name)) {
2297 if (!section_name || strcmp(section_name, "output") != 0)
2298 continue;
2299 weston_config_section_get_string(section, "name", &name, NULL);
2300 if (name == NULL)
2301 continue;
2302
2303 if (name[0] != 'W' || name[1] != 'L') {
2304 free(name);
2305 continue;
2306 }
2307 free(name);
2308
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 output = wayland_output_create_for_config(b, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002310 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002311 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002312 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002313 if (wayland_output_set_windowed(output))
2314 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002315
2316 x += output->base.width;
2317 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002318 }
2319
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002320 if (!width)
2321 width = 1024;
2322 if (!height)
2323 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002324 if (!scale)
2325 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002326 while (count > 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002327 output = wayland_output_create(b, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002328 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002329 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002330 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002331 if (wayland_output_set_windowed(output))
2332 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002333
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002334 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002335 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002336 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002337
Giulio Camuffo954f1832014-10-11 18:27:30 +03002338 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002339 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002340 fullscreen_binding, b);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002341
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002343
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002344err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002345 wayland_backend_destroy(b);
2346 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002347}