blob: a8e63e14148bda76e588a44f9e54524470f3846c [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"
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050049#include "fullscreen-shell-client-protocol.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020050#include "presentation_timing-server-protocol.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051
Jason Ekstrand48ce4212013-10-27 22:25:02 -050052#define WINDOW_TITLE "Weston Compositor"
53
Giulio Camuffo954f1832014-10-11 18:27:30 +030054struct wayland_backend {
55 struct weston_backend base;
56 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057
58 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040059 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040060 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010061 struct wl_compositor *compositor;
62 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050063 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020064 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010065
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050066 struct wl_list output_list;
67
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010068 struct wl_event_source *wl_source;
69 uint32_t event_mask;
70 } parent;
71
Jason Ekstrandff2fd462013-10-27 22:24:58 -050072 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050073 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050074
Jason Ekstrand7744f712013-10-27 22:24:55 -050075 struct theme *theme;
76 cairo_device_t *frame_device;
77 struct wl_cursor_theme *cursor_theme;
78 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050079
Jason Ekstrand06ced802013-11-07 20:13:29 -060080 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081};
82
83struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050084 struct weston_output base;
85
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010086 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050087 int draw_initial_frame;
88 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050089
90 struct wl_output *output;
91 uint32_t global_id;
92
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050093 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060094 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010095 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050096
Jason Ekstrand7744f712013-10-27 22:24:55 -050097 int keyboard_count;
98
Jason Ekstrand5ea04802013-11-07 20:13:33 -060099 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500100 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500101
Jason Ekstrand7744f712013-10-27 22:24:55 -0500102 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500103 struct wl_egl_window *egl_window;
104 struct {
105 cairo_surface_t *top;
106 cairo_surface_t *left;
107 cairo_surface_t *right;
108 cairo_surface_t *bottom;
109 } border;
110 } gl;
111
112 struct {
113 struct wl_list buffers;
114 struct wl_list free_buffers;
115 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500116
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500117 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500118 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100119};
120
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500121struct wayland_parent_output {
122 struct wayland_output *output;
123 struct wl_list link;
124
125 struct wl_output *global;
126 uint32_t id;
127
128 struct {
129 char *make;
130 char *model;
131 int32_t width, height;
132 uint32_t subpixel;
133 } physical;
134
135 int32_t x, y;
136 uint32_t transform;
137 uint32_t scale;
138
139 struct wl_list mode_list;
140 struct weston_mode *preferred_mode;
141 struct weston_mode *current_mode;
142};
143
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500144struct wayland_shm_buffer {
145 struct wayland_output *output;
146 struct wl_list link;
147 struct wl_list free_link;
148
149 struct wl_buffer *buffer;
150 void *data;
151 size_t size;
152 pixman_region32_t damage;
153 int frame_damaged;
154
155 pixman_image_t *pm_image;
156 cairo_surface_t *c_surface;
157};
158
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100159struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400160 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300161 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100162 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500163
164 struct {
165 struct wl_seat *seat;
166 struct wl_pointer *pointer;
167 struct wl_keyboard *keyboard;
168 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500169
170 struct {
171 struct wl_surface *surface;
172 int32_t hx, hy;
173 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500174 } parent;
175
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500176 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100177 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400178 uint32_t enter_serial;
179 int focus;
180 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500181 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100182};
183
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300184struct gl_renderer_interface *gl_renderer;
185
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500186static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500187wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
188{
189 cairo_surface_destroy(buffer->c_surface);
190 pixman_image_unref(buffer->pm_image);
191
192 wl_buffer_destroy(buffer->buffer);
193 munmap(buffer->data, buffer->size);
194
195 pixman_region32_fini(&buffer->damage);
196
197 wl_list_remove(&buffer->link);
198 wl_list_remove(&buffer->free_link);
199 free(buffer);
200}
201
202static void
203buffer_release(void *data, struct wl_buffer *buffer)
204{
205 struct wayland_shm_buffer *sb = data;
206
207 if (sb->output) {
208 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
209 } else {
210 wayland_shm_buffer_destroy(sb);
211 }
212}
213
214static const struct wl_buffer_listener buffer_listener = {
215 buffer_release
216};
217
218static struct wayland_shm_buffer *
219wayland_output_get_shm_buffer(struct wayland_output *output)
220{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300221 struct wayland_backend *b =
222 (struct wayland_backend *) output->base.compositor->backend;
223 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500224 struct wayland_shm_buffer *sb;
225
226 struct wl_shm_pool *pool;
227 int width, height, stride;
228 int32_t fx, fy;
229 int fd;
230 unsigned char *data;
231
232 if (!wl_list_empty(&output->shm.free_buffers)) {
233 sb = container_of(output->shm.free_buffers.next,
234 struct wayland_shm_buffer, free_link);
235 wl_list_remove(&sb->free_link);
236 wl_list_init(&sb->free_link);
237
238 return sb;
239 }
240
241 if (output->frame) {
242 width = frame_width(output->frame);
243 height = frame_height(output->frame);
244 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500245 width = output->base.current_mode->width;
246 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500247 }
248
249 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
250
251 fd = os_create_anonymous_file(height * stride);
252 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000253 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500254 return NULL;
255 }
256
257 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
258 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000259 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500260 close(fd);
261 return NULL;
262 }
263
264 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000265 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200266 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000267 close(fd);
268 free(data);
269 return NULL;
270 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500271
272 sb->output = output;
273 wl_list_init(&sb->free_link);
274 wl_list_insert(&output->shm.buffers, &sb->link);
275
276 pixman_region32_init_rect(&sb->damage, 0, 0,
277 output->base.width, output->base.height);
278 sb->frame_damaged = 1;
279
280 sb->data = data;
281 sb->size = height * stride;
282
283 pool = wl_shm_create_pool(shm, fd, sb->size);
284
285 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
286 width, height,
287 stride,
288 WL_SHM_FORMAT_ARGB8888);
289 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
290 wl_shm_pool_destroy(pool);
291 close(fd);
292
293 memset(data, 0, sb->size);
294
295 sb->c_surface =
296 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
297 width, height, stride);
298
299 fx = 0;
300 fy = 0;
301 if (output->frame)
302 frame_interior(output->frame, &fx, &fy, 0, 0);
303 sb->pm_image =
304 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
305 (uint32_t *)(data + fy * stride) + fx,
306 stride);
307
308 return sb;
309}
310
311static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500312frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400313{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500314 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400315 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400316
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500317 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400318
319 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300320
321 /*
322 * This is the fallback case, where Presentation extension is not
323 * available from the parent compositor. We do not know the base for
324 * 'time', so we cannot feed it to finish_frame(). Do the only thing
325 * we can, and pretend finish_frame time is when we process this
326 * event.
327 */
328 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200329 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400330}
331
332static const struct wl_callback_listener frame_listener = {
333 frame_done
334};
335
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500336static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200337draw_initial_frame(struct wayland_output *output)
338{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500339 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200340
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500341 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200342
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500343 /* If we are rendering with GL, then orphan it so that it gets
344 * destroyed immediately */
345 if (output->gl.egl_window)
346 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500347
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500348 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500349 wl_surface_damage(output->parent.surface, 0, 0,
350 output->base.current_mode->width,
351 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200352}
353
354static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500355wayland_output_update_gl_border(struct wayland_output *output)
356{
357 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
358 cairo_t *cr;
359
360 if (!output->frame)
361 return;
362 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
363 return;
364
365 fwidth = frame_width(output->frame);
366 fheight = frame_height(output->frame);
367 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
368
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500369 if (!output->gl.border.top)
370 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500371 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
372 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500373 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500374 frame_repaint(output->frame, cr);
375 cairo_destroy(cr);
376 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
377 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500378 cairo_image_surface_get_stride(output->gl.border.top) / 4,
379 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500380
381
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500382 if (!output->gl.border.left)
383 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500384 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
385 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500386 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500387 cairo_translate(cr, 0, -iy);
388 frame_repaint(output->frame, cr);
389 cairo_destroy(cr);
390 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
391 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500392 cairo_image_surface_get_stride(output->gl.border.left) / 4,
393 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500394
395
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500396 if (!output->gl.border.right)
397 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500398 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
399 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500400 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500401 cairo_translate(cr, -(iwidth + ix), -iy);
402 frame_repaint(output->frame, cr);
403 cairo_destroy(cr);
404 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
405 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500406 cairo_image_surface_get_stride(output->gl.border.right) / 4,
407 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500408
409
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500410 if (!output->gl.border.bottom)
411 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500412 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
413 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500414 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500415 cairo_translate(cr, 0, -(iy + iheight));
416 frame_repaint(output->frame, cr);
417 cairo_destroy(cr);
418 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
419 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500420 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
421 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500422}
423
424static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200425wayland_output_start_repaint_loop(struct weston_output *output_base)
426{
427 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300428 struct wayland_backend *wb =
429 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200430 struct wl_callback *callback;
431
432 /* If this is the initial frame, we need to attach a buffer so that
433 * the compositor can map the surface and include it in its render
434 * loop. If the surface doesn't end up in the render loop, the frame
435 * callback won't be invoked. The buffer is transparent and of the
436 * same size as the future real output buffer. */
437 if (output->parent.draw_initial_frame) {
438 output->parent.draw_initial_frame = 0;
439
440 draw_initial_frame(output);
441 }
442
443 callback = wl_surface_frame(output->parent.surface);
444 wl_callback_add_listener(callback, &frame_listener, output);
445 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300446 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200447}
448
David Herrmann1edf44c2013-10-22 17:11:26 +0200449static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500450wayland_output_repaint_gl(struct weston_output *output_base,
451 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400452{
453 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400454 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400455 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600456
Kristian Høgsberg33418202011-08-16 23:01:28 -0400457 callback = wl_surface_frame(output->parent.surface);
458 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100459
Jason Ekstrand7744f712013-10-27 22:24:55 -0500460 wayland_output_update_gl_border(output);
461
Pekka Paalanenbc106382012-10-10 12:49:31 +0300462 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200463
464 pixman_region32_subtract(&ec->primary_plane.damage,
465 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200466 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100467}
468
Matt Roper361d2ad2011-08-29 13:52:23 -0700469static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500470wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
471{
472 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
473 cairo_t *cr;
474
475 if (!buffer->output->frame || !buffer->frame_damaged)
476 return;
477
478 cr = cairo_create(buffer->c_surface);
479
480 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
481 fwidth = frame_width(buffer->output->frame);
482 fheight = frame_height(buffer->output->frame);
483
484 /* Set the clip so we don't unnecisaraly damage the surface */
485 cairo_move_to(cr, ix, iy);
486 cairo_rel_line_to(cr, iwidth, 0);
487 cairo_rel_line_to(cr, 0, iheight);
488 cairo_rel_line_to(cr, -iwidth, 0);
489 cairo_line_to(cr, ix, iy);
490 cairo_line_to(cr, 0, iy);
491 cairo_line_to(cr, 0, fheight);
492 cairo_line_to(cr, fwidth, fheight);
493 cairo_line_to(cr, fwidth, 0);
494 cairo_line_to(cr, 0, 0);
495 cairo_line_to(cr, 0, iy);
496 cairo_close_path(cr);
497 cairo_clip(cr);
498
499 /* Draw using a pattern so that the final result gets clipped */
500 cairo_push_group(cr);
501 frame_repaint(buffer->output->frame, cr);
502 cairo_pop_group_to_source(cr);
503 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
504 cairo_paint(cr);
505
506 cairo_destroy(cr);
507}
508
509static void
510wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
511{
512 pixman_region32_t damage;
513 pixman_box32_t *rects;
514 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
515 int i, n;
516
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500517 pixman_region32_init(&damage);
518 weston_transformed_region(sb->output->base.width,
519 sb->output->base.height,
520 sb->output->base.transform,
521 sb->output->base.current_scale,
522 &sb->damage, &damage);
523
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500524 if (sb->output->frame) {
525 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
526 fwidth = frame_width(sb->output->frame);
527 fheight = frame_height(sb->output->frame);
528
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500529 pixman_region32_translate(&damage, ix, iy);
530
531 if (sb->frame_damaged) {
532 pixman_region32_union_rect(&damage, &damage,
533 0, 0, fwidth, iy);
534 pixman_region32_union_rect(&damage, &damage,
535 0, iy, ix, iheight);
536 pixman_region32_union_rect(&damage, &damage,
537 ix + iwidth, iy,
538 fwidth - (ix + iwidth), iheight);
539 pixman_region32_union_rect(&damage, &damage,
540 0, iy + iheight,
541 fwidth, fheight - (iy + iheight));
542 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500543 }
544
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500545 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500546 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
547 for (i = 0; i < n; ++i)
548 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
549 rects[i].y1, rects[i].x2 - rects[i].x1,
550 rects[i].y2 - rects[i].y1);
551
552 if (sb->output->frame)
553 pixman_region32_fini(&damage);
554}
555
556static int
557wayland_output_repaint_pixman(struct weston_output *output_base,
558 pixman_region32_t *damage)
559{
560 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300561 struct wayland_backend *b =
562 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500563 struct wl_callback *callback;
564 struct wayland_shm_buffer *sb;
565
566 if (output->frame) {
567 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
568 wl_list_for_each(sb, &output->shm.buffers, link)
569 sb->frame_damaged = 1;
570 }
571
572 wl_list_for_each(sb, &output->shm.buffers, link)
573 pixman_region32_union(&sb->damage, &sb->damage, damage);
574
575 sb = wayland_output_get_shm_buffer(output);
576
577 wayland_output_update_shm_border(sb);
578 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300579 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500580
581 wayland_shm_buffer_attach(sb);
582
583 callback = wl_surface_frame(output->parent.surface);
584 wl_callback_add_listener(callback, &frame_listener, output);
585 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300586 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500587
588 pixman_region32_fini(&sb->damage);
589 pixman_region32_init(&sb->damage);
590 sb->frame_damaged = 0;
591
Giulio Camuffo954f1832014-10-11 18:27:30 +0300592 pixman_region32_subtract(&b->compositor->primary_plane.damage,
593 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500594 return 0;
595}
596
597static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500598wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700599{
600 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300601 struct wayland_backend *b =
602 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700603
Giulio Camuffo954f1832014-10-11 18:27:30 +0300604 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500605 pixman_renderer_output_destroy(output_base);
606 } else {
607 gl_renderer->output_destroy(output_base);
608 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100609
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500610 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500611 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500612 if (output->parent.shell_surface)
613 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500614
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600615 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500616 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600617
618 cairo_surface_destroy(output->gl.border.top);
619 cairo_surface_destroy(output->gl.border.left);
620 cairo_surface_destroy(output->gl.border.right);
621 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500622
623 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700624 free(output);
625
626 return;
627}
628
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300629static const struct wl_shell_surface_listener shell_surface_listener;
630
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200631static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500632wayland_output_init_gl_renderer(struct wayland_output *output)
633{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500634 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500635
636 if (output->frame) {
637 fwidth = frame_width(output->frame);
638 fheight = frame_height(output->frame);
639 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500640 fwidth = output->base.current_mode->width;
641 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500642 }
643
644 output->gl.egl_window =
645 wl_egl_window_create(output->parent.surface,
646 fwidth, fheight);
647 if (!output->gl.egl_window) {
648 weston_log("failure to create wl_egl_window\n");
649 return -1;
650 }
651
652 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000653 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100654 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000655 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500656 NULL,
657 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500658 goto cleanup_window;
659
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500660 return 0;
661
662cleanup_window:
663 wl_egl_window_destroy(output->gl.egl_window);
664 return -1;
665}
666
667static int
668wayland_output_init_pixman_renderer(struct wayland_output *output)
669{
670 return pixman_renderer_output_create(&output->base);
671}
672
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600673static void
674wayland_output_resize_surface(struct wayland_output *output)
675{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300676 struct wayland_backend *b =
677 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600678 struct wayland_shm_buffer *buffer, *next;
679 int32_t ix, iy, iwidth, iheight;
680 int32_t width, height;
681 struct wl_region *region;
682
683 width = output->base.current_mode->width;
684 height = output->base.current_mode->height;
685
686 if (output->frame) {
687 frame_resize_inside(output->frame, width, height);
688
689 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300690 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600691 wl_region_add(region, ix, iy, iwidth, iheight);
692 wl_surface_set_input_region(output->parent.surface, region);
693 wl_region_destroy(region);
694
695 frame_opaque_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_opaque_region(output->parent.surface, region);
699 wl_region_destroy(region);
700
701 width = frame_width(output->frame);
702 height = frame_height(output->frame);
703 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300704 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600705 wl_region_add(region, 0, 0, width, height);
706 wl_surface_set_input_region(output->parent.surface, region);
707 wl_region_destroy(region);
708
Giulio Camuffo954f1832014-10-11 18:27:30 +0300709 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600710 wl_region_add(region, 0, 0, width, height);
711 wl_surface_set_opaque_region(output->parent.surface, region);
712 wl_region_destroy(region);
713 }
714
715 if (output->gl.egl_window) {
716 wl_egl_window_resize(output->gl.egl_window,
717 width, height, 0, 0);
718
719 /* These will need to be re-created due to the resize */
720 gl_renderer->output_set_border(&output->base,
721 GL_RENDERER_BORDER_TOP,
722 0, 0, 0, NULL);
723 cairo_surface_destroy(output->gl.border.top);
724 output->gl.border.top = NULL;
725 gl_renderer->output_set_border(&output->base,
726 GL_RENDERER_BORDER_LEFT,
727 0, 0, 0, NULL);
728 cairo_surface_destroy(output->gl.border.left);
729 output->gl.border.left = NULL;
730 gl_renderer->output_set_border(&output->base,
731 GL_RENDERER_BORDER_RIGHT,
732 0, 0, 0, NULL);
733 cairo_surface_destroy(output->gl.border.right);
734 output->gl.border.right = NULL;
735 gl_renderer->output_set_border(&output->base,
736 GL_RENDERER_BORDER_BOTTOM,
737 0, 0, 0, NULL);
738 cairo_surface_destroy(output->gl.border.bottom);
739 output->gl.border.bottom = NULL;
740 }
741
742 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500743 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600744 wayland_shm_buffer_destroy(buffer);
745 /* These will get thrown away when they get released */
746 wl_list_for_each(buffer, &output->shm.buffers, link)
747 buffer->output = NULL;
748}
749
750static int
751wayland_output_set_windowed(struct wayland_output *output)
752{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300753 struct wayland_backend *b =
754 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600755 int tlen;
756 char *title;
757
758 if (output->frame)
759 return 0;
760
761 if (output->name) {
762 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
763 title = malloc(tlen + 1);
764 if (!title)
765 return -1;
766
767 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
768 } else {
769 title = strdup(WINDOW_TITLE);
770 }
771
Giulio Camuffo954f1832014-10-11 18:27:30 +0300772 if (!b->theme) {
773 b->theme = theme_create();
774 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600775 free(title);
776 return -1;
777 }
778 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300779 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600780 FRAME_BUTTON_CLOSE, title);
781 free(title);
782 if (!output->frame)
783 return -1;
784
785 if (output->keyboard_count)
786 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
787
788 wayland_output_resize_surface(output);
789
790 wl_shell_surface_set_toplevel(output->parent.shell_surface);
791
792 return 0;
793}
794
795static void
796wayland_output_set_fullscreen(struct wayland_output *output,
797 enum wl_shell_surface_fullscreen_method method,
798 uint32_t framerate, struct wl_output *target)
799{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300800 struct wayland_backend *b =
801 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500802
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600803 if (output->frame) {
804 frame_destroy(output->frame);
805 output->frame = NULL;
806 }
807
808 wayland_output_resize_surface(output);
809
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500810 if (output->parent.shell_surface) {
811 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
812 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300813 } else if (b->parent.fshell) {
814 _wl_fullscreen_shell_present_surface(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500815 output->parent.surface,
816 method, target);
817 }
818}
819
820static struct weston_mode *
821wayland_output_choose_mode(struct wayland_output *output,
822 struct weston_mode *ref_mode)
823{
824 struct weston_mode *mode;
825
826 /* First look for an exact match */
827 wl_list_for_each(mode, &output->base.mode_list, link)
828 if (mode->width == ref_mode->width &&
829 mode->height == ref_mode->height &&
830 mode->refresh == ref_mode->refresh)
831 return mode;
832
833 /* If we can't find an exact match, ignore refresh and try again */
834 wl_list_for_each(mode, &output->base.mode_list, link)
835 if (mode->width == ref_mode->width &&
836 mode->height == ref_mode->height)
837 return mode;
838
839 /* Yeah, we failed */
840 return NULL;
841}
842
843enum mode_status {
844 MODE_STATUS_UNKNOWN,
845 MODE_STATUS_SUCCESS,
846 MODE_STATUS_FAIL,
847 MODE_STATUS_CANCEL,
848};
849
850static void
851mode_feedback_successful(void *data,
852 struct _wl_fullscreen_shell_mode_feedback *fb)
853{
854 enum mode_status *value = data;
855
856 printf("Mode switch successful\n");
857
858 *value = MODE_STATUS_SUCCESS;
859}
860
861static void
862mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
863{
864 enum mode_status *value = data;
865
866 printf("Mode switch failed\n");
867
868 *value = MODE_STATUS_FAIL;
869}
870
871static void
872mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
873{
874 enum mode_status *value = data;
875
876 printf("Mode switch cancelled\n");
877
878 *value = MODE_STATUS_CANCEL;
879}
880
881struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
882 mode_feedback_successful,
883 mode_feedback_failed,
884 mode_feedback_cancelled,
885};
886
887static int
888wayland_output_switch_mode(struct weston_output *output_base,
889 struct weston_mode *mode)
890{
891 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300892 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500893 struct wl_surface *old_surface;
894 struct weston_mode *old_mode;
895 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
896 enum mode_status mode_status;
897 int ret = 0;
898
899 if (output_base == NULL) {
900 weston_log("output is NULL.\n");
901 return -1;
902 }
903
904 if (mode == NULL) {
905 weston_log("mode is NULL.\n");
906 return -1;
907 }
908
Giulio Camuffo954f1832014-10-11 18:27:30 +0300909 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500910
Giulio Camuffo954f1832014-10-11 18:27:30 +0300911 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500912 return -1;
913
914 mode = wayland_output_choose_mode(output, mode);
915 if (mode == NULL)
916 return -1;
917
918 if (output->base.current_mode == mode)
919 return 0;
920
921 old_mode = output->base.current_mode;
922 old_surface = output->parent.surface;
923 output->base.current_mode = mode;
924 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300925 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500926 wl_surface_set_user_data(output->parent.surface, output);
927
928 /* Blow the old buffers because we changed size/surfaces */
929 wayland_output_resize_surface(output);
930
931 mode_feedback =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300932 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500933 output->parent.surface,
934 output->parent.output,
935 mode->refresh);
936 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
937 &mode_feedback_listener,
938 &mode_status);
939
940 /* This should kick-start things again */
941 output->parent.draw_initial_frame = 1;
942 wayland_output_start_repaint_loop(&output->base);
943
944 mode_status = MODE_STATUS_UNKNOWN;
945 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300946 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500947
948 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
949
950 if (mode_status == MODE_STATUS_FAIL) {
951 output->base.current_mode = old_mode;
952 wl_surface_destroy(output->parent.surface);
953 output->parent.surface = old_surface;
954 wayland_output_resize_surface(output);
955
956 return -1;
957 }
958
959 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
960 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
961
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500963 pixman_renderer_output_destroy(output_base);
964 if (wayland_output_init_pixman_renderer(output) < 0)
965 goto err_output;
966 } else {
967 gl_renderer->output_destroy(output_base);
968 wl_egl_window_destroy(output->gl.egl_window);
969 if (wayland_output_init_gl_renderer(output) < 0)
970 goto err_output;
971 }
972 wl_surface_destroy(old_surface);
973
974 weston_output_schedule_repaint(&output->base);
975
976 return 0;
977
978err_output:
979 /* XXX */
980 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600981}
982
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500983static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300984wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600985 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500986 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100987{
988 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500989 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600990
991 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
992 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100993
Peter Huttererf3d62272013-08-08 11:57:05 +1000994 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100995 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500996 return NULL;
997
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600998 output->name = name ? strdup(name) : NULL;
999 output->base.make = "waywayland";
1000 output->base.model = "none";
1001
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001002 output_width = width * scale;
1003 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001004
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001005 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001006 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007 if (!output->parent.surface)
1008 goto err_name;
1009 wl_surface_set_user_data(output->parent.surface, output);
1010
1011 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001012
Giulio Camuffo954f1832014-10-11 18:27:30 +03001013 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001014 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001015 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001016 output->parent.surface);
1017 if (!output->parent.shell_surface)
1018 goto err_surface;
1019 wl_shell_surface_add_listener(output->parent.shell_surface,
1020 &shell_surface_listener, output);
1021 }
1022
Giulio Camuffo954f1832014-10-11 18:27:30 +03001023 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001024 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1025 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001026 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001027 if (!width)
1028 output_width = output->parent.configure_width;
1029 if (!height)
1030 output_height = output->parent.configure_height;
1031 }
1032
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001033 output->mode.flags =
1034 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001035 output->mode.width = output_width;
1036 output->mode.height = output_height;
1037 output->mode.refresh = 60000;
1038 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001039 wl_list_init(&output->base.mode_list);
1040 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001041 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001042
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001043 wl_list_init(&output->shm.buffers);
1044 wl_list_init(&output->shm.free_buffers);
1045
Giulio Camuffo954f1832014-10-11 18:27:30 +03001046 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001047 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001048
Giulio Camuffo954f1832014-10-11 18:27:30 +03001049 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001050 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001051 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001052 output->base.repaint = wayland_output_repaint_pixman;
1053 } else {
1054 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001055 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001056 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001057 }
1058
Jonas Ådahle5a12252013-04-05 23:07:11 +02001059 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001060 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001061 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001062 output->base.set_backlight = NULL;
1063 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001064 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001065
Giulio Camuffo954f1832014-10-11 18:27:30 +03001066 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001067
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001068 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001069
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001070err_output:
1071 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001072 if (output->parent.shell_surface)
1073 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001074err_surface:
1075 wl_surface_destroy(output->parent.surface);
1076err_name:
1077 free(output->name);
1078
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001079 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001080 free(output);
1081
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001082 return NULL;
1083}
1084
1085static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001086wayland_output_create_for_config(struct wayland_backend *b,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001087 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001088 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001089 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001090{
1091 struct wayland_output *output;
1092 char *mode, *t, *name, *str;
1093 int width, height, scale;
1094 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001095 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001096
1097 weston_config_section_get_string(config_section, "name", &name, NULL);
1098 if (name) {
1099 slen = strlen(name);
1100 slen += strlen(WINDOW_TITLE " - ");
1101 str = malloc(slen + 1);
1102 if (str)
1103 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1104 free(name);
1105 name = str;
1106 }
1107 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001108 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001109
1110 weston_config_section_get_string(config_section,
1111 "mode", &mode, "1024x600");
1112 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1113 weston_log("Invalid mode \"%s\" for output %s\n",
1114 mode, name);
1115 width = 1024;
1116 height = 640;
1117 }
1118 free(mode);
1119
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001120 if (option_width)
1121 width = option_width;
1122 if (option_height)
1123 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001124
1125 weston_config_section_get_int(config_section, "scale", &scale, 1);
1126
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001127 if (option_scale)
1128 scale = option_scale;
1129
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001130 weston_config_section_get_string(config_section,
1131 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001132 if (weston_parse_transform(t, &transform) < 0)
1133 weston_log("Invalid transform \"%s\" for output %s\n",
1134 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001135 free(t);
1136
Giulio Camuffo954f1832014-10-11 18:27:30 +03001137 output = wayland_output_create(b, x, y, width, height, name, 0,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001138 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001139 free(name);
1140
1141 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001142}
1143
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001144static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001145wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001146 struct wayland_parent_output *poutput)
1147{
1148 struct wayland_output *output;
1149 struct weston_mode *mode;
1150 int32_t x;
1151
1152 if (poutput->current_mode) {
1153 mode = poutput->current_mode;
1154 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001155 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001156 } else if (!wl_list_empty(&poutput->mode_list)) {
1157 mode = container_of(poutput->mode_list.next,
1158 struct weston_mode, link);
1159 } else {
1160 weston_log("No valid modes found. Skipping output");
1161 return NULL;
1162 }
1163
Giulio Camuffo954f1832014-10-11 18:27:30 +03001164 if (!wl_list_empty(&b->compositor->output_list)) {
1165 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001166 struct wayland_output, base.link);
1167 x = output->base.x + output->base.current_mode->width;
1168 } else {
1169 x = 0;
1170 }
1171
Giulio Camuffo954f1832014-10-11 18:27:30 +03001172 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001173 NULL, 0,
1174 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1175 if (!output)
1176 return NULL;
1177
1178 output->parent.output = poutput->global;
1179
1180 output->base.make = poutput->physical.make;
1181 output->base.model = poutput->physical.model;
1182 wl_list_init(&output->base.mode_list);
1183 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1184 wl_list_init(&poutput->mode_list);
1185
1186 wayland_output_set_fullscreen(output,
1187 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1188 mode->refresh, poutput->global);
1189
1190 if (output->parent.shell_surface) {
1191 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1192 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1193 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001194 } else if (b->parent.fshell) {
1195 _wl_fullscreen_shell_present_surface(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001196 output->parent.surface,
1197 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1198 poutput->global);
1199 _wl_fullscreen_shell_mode_feedback_destroy(
Giulio Camuffo954f1832014-10-11 18:27:30 +03001200 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001201 output->parent.surface,
1202 poutput->global,
1203 mode->refresh));
1204 }
1205
1206 return output;
1207}
1208
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001209static void
1210shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1211 uint32_t serial)
1212{
1213 wl_shell_surface_pong(shell_surface, serial);
1214}
1215
1216static void
1217shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1218 uint32_t edges, int32_t width, int32_t height)
1219{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001220 struct wayland_output *output = data;
1221
1222 output->parent.configure_width = width;
1223 output->parent.configure_height = height;
1224
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001225 /* FIXME: implement resizing */
1226}
1227
1228static void
1229shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1230{
1231}
1232
1233static const struct wl_shell_surface_listener shell_surface_listener = {
1234 shell_surface_ping,
1235 shell_surface_configure,
1236 shell_surface_popup_done
1237};
1238
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001239/* Events received from the wayland-server this compositor is client of: */
1240
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001242static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001243input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001244{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001245
Jason Ekstrand7744f712013-10-27 22:24:55 -05001246 struct wl_buffer *buffer;
1247 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001248
Giulio Camuffo954f1832014-10-11 18:27:30 +03001249 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001250 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001251
Giulio Camuffo954f1832014-10-11 18:27:30 +03001252 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001253 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001254 if (!buffer)
1255 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001256
1257 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1258 input->parent.cursor.surface,
1259 image->hotspot_x, image->hotspot_y);
1260
1261 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1262 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1263 image->width, image->height);
1264 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001265}
1266
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001267static void
Daniel Stone37816df2012-05-16 18:45:18 +01001268input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1269 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001270 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001271{
1272 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001273 int32_t fx, fy;
1274 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001275
Daniel Stone50692802012-06-22 13:21:41 +01001276 /* XXX: If we get a modifier event immediately before the focus,
1277 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001278 input->enter_serial = serial;
1279 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001280
1281 if (input->output->frame) {
1282 location = frame_pointer_enter(input->output->frame, input,
1283 wl_fixed_to_int(x),
1284 wl_fixed_to_int(y));
1285 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1286 x -= wl_fixed_from_int(fx);
1287 y -= wl_fixed_from_int(fy);
1288
1289 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1290 weston_output_schedule_repaint(&input->output->base);
1291 } else {
1292 location = THEME_LOCATION_CLIENT_AREA;
1293 }
1294
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001295 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001296
1297 if (location == THEME_LOCATION_CLIENT_AREA) {
1298 input->focus = 1;
1299 notify_pointer_focus(&input->base, &input->output->base, x, y);
1300 wl_pointer_set_cursor(input->parent.pointer,
1301 input->enter_serial, NULL, 0, 0);
1302 } else {
1303 input->focus = 0;
1304 notify_pointer_focus(&input->base, NULL, 0, 0);
1305 input_set_cursor(input);
1306 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001307}
1308
1309static void
Daniel Stone37816df2012-05-16 18:45:18 +01001310input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1311 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001312{
1313 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001314
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001315 if (!input->output)
1316 return;
1317
Jason Ekstrand7744f712013-10-27 22:24:55 -05001318 if (input->output->frame) {
1319 frame_pointer_leave(input->output->frame, input);
1320
1321 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1322 weston_output_schedule_repaint(&input->output->base);
1323 }
1324
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001325 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001326 input->output = NULL;
1327 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001328}
1329
1330static void
Daniel Stone37816df2012-05-16 18:45:18 +01001331input_handle_motion(void *data, struct wl_pointer *pointer,
1332 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1333{
1334 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001335 int32_t fx, fy;
1336 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001337
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001338 if (!input->output)
1339 return;
1340
Jason Ekstrand7744f712013-10-27 22:24:55 -05001341 if (input->output->frame) {
1342 location = frame_pointer_motion(input->output->frame, input,
1343 wl_fixed_to_int(x),
1344 wl_fixed_to_int(y));
1345 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1346 x -= wl_fixed_from_int(fx);
1347 y -= wl_fixed_from_int(fy);
1348
1349 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1350 weston_output_schedule_repaint(&input->output->base);
1351 } else {
1352 location = THEME_LOCATION_CLIENT_AREA;
1353 }
1354
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001355 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001356
1357 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1358 input_set_cursor(input);
1359 notify_pointer_focus(&input->base, NULL, 0, 0);
1360 input->focus = 0;
1361 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1362 wl_pointer_set_cursor(input->parent.pointer,
1363 input->enter_serial, NULL, 0, 0);
1364 notify_pointer_focus(&input->base, &input->output->base, x, y);
1365 input->focus = 1;
1366 }
1367
1368 if (location == THEME_LOCATION_CLIENT_AREA)
1369 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001370}
1371
1372static void
1373input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001374 uint32_t serial, uint32_t time, uint32_t button,
1375 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001376{
1377 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001378 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001379 enum frame_button_state fstate;
1380 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001381
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001382 if (!input->output)
1383 return;
1384
Jason Ekstrand7744f712013-10-27 22:24:55 -05001385 if (input->output->frame) {
1386 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1387 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1388
1389 location = frame_pointer_button(input->output->frame, input,
1390 button, fstate);
1391
1392 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1393
1394 wl_shell_surface_move(input->output->parent.shell_surface,
1395 input->parent.seat, serial);
1396 frame_status_clear(input->output->frame,
1397 FRAME_STATUS_MOVE);
1398 return;
1399 }
1400
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001401 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1402 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001403 input->output = NULL;
1404 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001405
Giulio Camuffo954f1832014-10-11 18:27:30 +03001406 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001407 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001408
1409 return;
1410 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001411
1412 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1413 weston_output_schedule_repaint(&input->output->base);
1414 } else {
1415 location = THEME_LOCATION_CLIENT_AREA;
1416 }
1417
1418 if (location == THEME_LOCATION_CLIENT_AREA)
1419 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001420}
1421
1422static void
1423input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001424 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001425{
1426 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001427
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001428 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001429}
1430
1431static const struct wl_pointer_listener pointer_listener = {
1432 input_handle_pointer_enter,
1433 input_handle_pointer_leave,
1434 input_handle_motion,
1435 input_handle_button,
1436 input_handle_axis,
1437};
1438
1439static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001440input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1441 int fd, uint32_t size)
1442{
1443 struct wayland_input *input = data;
1444 struct xkb_keymap *keymap;
1445 char *map_str;
1446
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001447 if (!data) {
1448 close(fd);
1449 return;
1450 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001451
1452 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1453 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1454 if (map_str == MAP_FAILED) {
1455 weston_log("mmap failed: %m\n");
1456 goto error;
1457 }
1458
Giulio Camuffo954f1832014-10-11 18:27:30 +03001459 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001460 map_str,
1461 XKB_KEYMAP_FORMAT_TEXT_V1,
1462 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001463 munmap(map_str, size);
1464
1465 if (!keymap) {
1466 weston_log("failed to compile keymap\n");
1467 goto error;
1468 }
1469
1470 input->keyboard_state_update = STATE_UPDATE_NONE;
1471 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1472 weston_log("No keymap provided; falling back to defalt\n");
1473 keymap = NULL;
1474 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1475 } else {
1476 weston_log("Invalid keymap\n");
1477 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001478 }
1479
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001480 close(fd);
1481
Rui Matos0c194ce2013-10-10 19:44:21 +02001482 if (input->base.keyboard)
1483 weston_seat_update_keymap(&input->base, keymap);
1484 else
1485 weston_seat_init_keyboard(&input->base, keymap);
1486
Ran Benitac9c74152014-08-19 23:59:52 +03001487 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001488
1489 return;
1490
1491error:
1492 wl_keyboard_release(input->parent.keyboard);
1493 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001494}
1495
1496static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001497input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001498 struct wl_keyboard *keyboard,
1499 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001500 struct wl_surface *surface,
1501 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001502{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001503 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001504 struct wayland_output *focus;
1505
1506 focus = input->keyboard_focus;
1507 if (focus) {
1508 /* This shouldn't happen */
1509 focus->keyboard_count--;
1510 if (!focus->keyboard_count && focus->frame)
1511 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1512 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1513 weston_output_schedule_repaint(&focus->base);
1514 }
1515
1516 input->keyboard_focus = wl_surface_get_user_data(surface);
1517 input->keyboard_focus->keyboard_count++;
1518
1519 focus = input->keyboard_focus;
1520 if (focus->frame) {
1521 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1522 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1523 weston_output_schedule_repaint(&focus->base);
1524 }
1525
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001526
Daniel Stone50692802012-06-22 13:21:41 +01001527 /* XXX: If we get a modifier event immediately before the focus,
1528 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001529 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001530 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001531}
1532
1533static void
1534input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001535 struct wl_keyboard *keyboard,
1536 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001537 struct wl_surface *surface)
1538{
1539 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001540 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001541
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001542 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001543
1544 focus = input->keyboard_focus;
1545 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001546 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001547
1548 focus->keyboard_count--;
1549 if (!focus->keyboard_count && focus->frame) {
1550 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1551 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1552 weston_output_schedule_repaint(&focus->base);
1553 }
1554
1555 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001556}
1557
Daniel Stone37816df2012-05-16 18:45:18 +01001558static void
1559input_handle_key(void *data, struct wl_keyboard *keyboard,
1560 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1561{
1562 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001563
Daniel Stone50692802012-06-22 13:21:41 +01001564 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001565 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001566 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001567 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001568 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001569}
1570
Daniel Stone351eb612012-05-31 15:27:47 -04001571static void
1572input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001573 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001574 uint32_t mods_latched, uint32_t mods_locked,
1575 uint32_t group)
1576{
Daniel Stone50692802012-06-22 13:21:41 +01001577 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001578 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001579 uint32_t serial_out;
1580
1581 /* If we get a key event followed by a modifier event with the
1582 * same serial number, then we try to preserve those semantics by
1583 * reusing the same serial number on the way out too. */
1584 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001585 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001586 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001587 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001588
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001589 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001590 mods_depressed, mods_latched,
1591 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001592 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001593}
1594
Jonny Lamb497994a2014-08-12 14:58:26 +02001595static void
1596input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1597 int32_t rate, int32_t delay)
1598{
1599 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001600 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001601
Giulio Camuffo954f1832014-10-11 18:27:30 +03001602 b->compositor->kb_repeat_rate = rate;
1603 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001604}
1605
Daniel Stone37816df2012-05-16 18:45:18 +01001606static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001607 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001608 input_handle_keyboard_enter,
1609 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001610 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001611 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001612 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001613};
1614
1615static void
Daniel Stone37816df2012-05-16 18:45:18 +01001616input_handle_capabilities(void *data, struct wl_seat *seat,
1617 enum wl_seat_capability caps)
1618{
1619 struct wayland_input *input = data;
1620
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001621 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1622 input->parent.pointer = wl_seat_get_pointer(seat);
1623 wl_pointer_set_user_data(input->parent.pointer, input);
1624 wl_pointer_add_listener(input->parent.pointer,
1625 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001626 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001627 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1628 wl_pointer_destroy(input->parent.pointer);
1629 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001630 }
1631
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001632 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1633 input->parent.keyboard = wl_seat_get_keyboard(seat);
1634 wl_keyboard_set_user_data(input->parent.keyboard, input);
1635 wl_keyboard_add_listener(input->parent.keyboard,
1636 &keyboard_listener, input);
1637 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1638 wl_keyboard_destroy(input->parent.keyboard);
1639 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001640 }
1641}
1642
Jonny Lamb497994a2014-08-12 14:58:26 +02001643static void
1644input_handle_name(void *data, struct wl_seat *seat,
1645 const char *name)
1646{
1647}
1648
Daniel Stone37816df2012-05-16 18:45:18 +01001649static const struct wl_seat_listener seat_listener = {
1650 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001651 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001652};
1653
1654static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001655display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001656{
1657 struct wayland_input *input;
1658
Peter Huttererf3d62272013-08-08 11:57:05 +10001659 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001660 if (input == NULL)
1661 return;
1662
Giulio Camuffo954f1832014-10-11 18:27:30 +03001663 weston_seat_init(&input->base, b->compositor, "default");
1664 input->backend = b;
1665 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001666 &wl_seat_interface, MIN(version, 4));
Giulio Camuffo954f1832014-10-11 18:27:30 +03001667 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001668
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001669 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1670 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001671
1672 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001673 wl_compositor_create_surface(b->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001674}
1675
1676static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001677wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1678 int32_t x, int32_t y,
1679 int32_t physical_width, int32_t physical_height,
1680 int32_t subpixel, const char *make,
1681 const char *model, int32_t transform)
1682{
1683 struct wayland_parent_output *output = data;
1684
1685 output->x = x;
1686 output->y = y;
1687 output->physical.width = physical_width;
1688 output->physical.height = physical_height;
1689 output->physical.subpixel = subpixel;
1690
1691 free(output->physical.make);
1692 output->physical.make = strdup(make);
1693 free(output->physical.model);
1694 output->physical.model = strdup(model);
1695
1696 output->transform = transform;
1697}
1698
1699static struct weston_mode *
1700find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1701{
1702 struct weston_mode *mode;
1703
1704 wl_list_for_each(mode, list, link) {
1705 if (mode->width == width && mode->height == height &&
1706 mode->refresh == refresh)
1707 return mode;
1708 }
1709
1710 mode = zalloc(sizeof *mode);
1711 if (!mode)
1712 return NULL;
1713
1714 mode->width = width;
1715 mode->height = height;
1716 mode->refresh = refresh;
1717 wl_list_insert(list, &mode->link);
1718
1719 return mode;
1720}
1721
1722static void
1723wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1724 uint32_t flags, int32_t width, int32_t height,
1725 int32_t refresh)
1726{
1727 struct wayland_parent_output *output = data;
1728 struct weston_mode *mode;
1729
1730 if (output->output) {
1731 mode = find_mode(&output->output->base.mode_list,
1732 width, height, refresh);
1733 if (!mode)
1734 return;
1735 mode->flags = flags;
1736 /* Do a mode-switch on current mode change? */
1737 } else {
1738 mode = find_mode(&output->mode_list, width, height, refresh);
1739 if (!mode)
1740 return;
1741 mode->flags = flags;
1742 if (flags & WL_OUTPUT_MODE_CURRENT)
1743 output->current_mode = mode;
1744 if (flags & WL_OUTPUT_MODE_PREFERRED)
1745 output->preferred_mode = mode;
1746 }
1747}
1748
1749static const struct wl_output_listener output_listener = {
1750 wayland_parent_output_geometry,
1751 wayland_parent_output_mode
1752};
1753
1754static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001755wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001756{
1757 struct wayland_parent_output *output;
1758
1759 output = zalloc(sizeof *output);
1760 if (!output)
1761 return;
1762
1763 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001764 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001765 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001766 if (!output->global) {
1767 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001768 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001769 }
1770
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001771 wl_output_add_listener(output->global, &output_listener, output);
1772
1773 output->scale = 0;
1774 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1775 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1776 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001777 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001778
Giulio Camuffo954f1832014-10-11 18:27:30 +03001779 if (b->sprawl_across_outputs) {
1780 wl_display_roundtrip(b->parent.wl_display);
1781 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001782 }
1783}
1784
1785static void
1786wayland_parent_output_destroy(struct wayland_parent_output *output)
1787{
1788 struct weston_mode *mode, *next;
1789
1790 if (output->output)
1791 wayland_output_destroy(&output->output->base);
1792
1793 wl_output_destroy(output->global);
1794 free(output->physical.make);
1795 free(output->physical.model);
1796
1797 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1798 wl_list_remove(&mode->link);
1799 free(mode);
1800 }
1801}
1802
1803static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001804registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1805 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001806{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001807 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001808
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001809 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001810 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001811 wl_registry_bind(registry, name,
1812 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001813 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001814 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001815 wl_registry_bind(registry, name,
1816 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001817 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001818 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001819 wl_registry_bind(registry, name,
1820 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001821 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001822 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001823 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001824 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001825 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001826 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02001827 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001828 }
1829}
1830
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001831static void
1832registry_handle_global_remove(void *data, struct wl_registry *registry,
1833 uint32_t name)
1834{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001835 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001836 struct wayland_parent_output *output;
1837
Giulio Camuffo954f1832014-10-11 18:27:30 +03001838 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001839 if (output->id == name)
1840 wayland_parent_output_destroy(output);
1841}
1842
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001843static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001844 registry_handle_global,
1845 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001846};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001847
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001848static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001849wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001850{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001851 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001852 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001853
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001854 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03001855 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001856 return 0;
1857 }
1858
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001859 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001860 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001861 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001862 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001863
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001864 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001865 count = wl_display_dispatch_pending(b->parent.wl_display);
1866 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001867 }
1868
1869 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001870}
1871
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001872static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001873wayland_restore(struct weston_compositor *ec)
1874{
1875}
1876
1877static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001878wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001879{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001880 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001881
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001882 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001883
Giulio Camuffo954f1832014-10-11 18:27:30 +03001884 if (b->parent.shm)
1885 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001886
Giulio Camuffo954f1832014-10-11 18:27:30 +03001887 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001888}
1889
Jason Ekstrand7744f712013-10-27 22:24:55 -05001890static const char *left_ptrs[] = {
1891 "left_ptr",
1892 "default",
1893 "top_left_arrow",
1894 "left-arrow"
1895};
1896
1897static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001898create_cursor(struct wayland_backend *b, struct weston_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001899{
1900 struct weston_config_section *s;
1901 int size;
1902 char *theme = NULL;
1903 unsigned int i;
1904
1905 s = weston_config_get_section(config, "shell", NULL, NULL);
1906 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1907 weston_config_section_get_int(s, "cursor-size", &size, 32);
1908
Giulio Camuffo954f1832014-10-11 18:27:30 +03001909 b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
1910 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01001911 fprintf(stderr, "could not load cursor theme\n");
1912 return;
1913 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001914
U. Artie Eoffff755002014-01-17 12:36:58 -08001915 free(theme);
1916
Giulio Camuffo954f1832014-10-11 18:27:30 +03001917 b->cursor = NULL;
1918 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1919 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001920 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001921 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001922 fprintf(stderr, "could not load left cursor\n");
1923 return;
1924 }
1925}
1926
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001927static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05001928fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
1929 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001930{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001931 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001932 struct wayland_input *input = NULL;
1933
Giulio Camuffo954f1832014-10-11 18:27:30 +03001934 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05001935 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001936 break;
1937
1938 if (!input || !input->output)
1939 return;
1940
1941 if (input->output->frame)
1942 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1943 else
1944 wayland_output_set_windowed(input->output);
1945
1946 weston_output_schedule_repaint(&input->output->base);
1947}
1948
Giulio Camuffo954f1832014-10-11 18:27:30 +03001949static struct wayland_backend *
1950wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
1951 const char *display_name, int *argc, char *argv[],
1952 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001953{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001954 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001955 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001956 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001957
Giulio Camuffo954f1832014-10-11 18:27:30 +03001958 b = zalloc(sizeof *b);
1959 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001960 return NULL;
1961
Giulio Camuffo954f1832014-10-11 18:27:30 +03001962 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001963 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001964 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001965
Giulio Camuffo954f1832014-10-11 18:27:30 +03001966 b->parent.wl_display = wl_display_connect(display_name);
1967 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001968 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001969 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001970 }
1971
Giulio Camuffo954f1832014-10-11 18:27:30 +03001972 wl_list_init(&b->parent.output_list);
1973 wl_list_init(&b->input_list);
1974 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
1975 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
1976 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001977
Giulio Camuffo954f1832014-10-11 18:27:30 +03001978 create_cursor(b, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001979
Giulio Camuffo954f1832014-10-11 18:27:30 +03001980 b->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001981
Giulio Camuffo954f1832014-10-11 18:27:30 +03001982 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001983 gl_renderer = weston_load_module("gl-renderer.so",
1984 "gl_renderer_interface");
1985 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001986 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001987 }
1988
Giulio Camuffo954f1832014-10-11 18:27:30 +03001989 if (!b->use_pixman) {
1990 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05001991 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001992 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05001993 gl_renderer->alpha_attribs,
1994 NULL,
1995 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001996 weston_log("Failed to initialize the GL renderer; "
1997 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001998 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001999 }
2000 }
2001
Giulio Camuffo954f1832014-10-11 18:27:30 +03002002 if (b->use_pixman) {
2003 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002004 weston_log("Failed to initialize pixman renderer\n");
2005 goto err_display;
2006 }
2007 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002008
Giulio Camuffo954f1832014-10-11 18:27:30 +03002009 b->base.destroy = wayland_destroy;
2010 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002011
Giulio Camuffo954f1832014-10-11 18:27:30 +03002012 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002013
Giulio Camuffo954f1832014-10-11 18:27:30 +03002014 fd = wl_display_get_fd(b->parent.wl_display);
2015 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002016 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002017 wayland_backend_handle_event, b);
2018 if (b->parent.wl_source == NULL)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002019 goto err_renderer;
2020
Giulio Camuffo954f1832014-10-11 18:27:30 +03002021 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002022
Giulio Camuffo954f1832014-10-11 18:27:30 +03002023 compositor->backend = &b->base;
2024 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002025err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002026 compositor->renderer->destroy(compositor);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002027err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002028 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002029err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002030 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002031 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002032 return NULL;
2033}
2034
2035static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002036wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002037{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002038 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002039
Giulio Camuffo954f1832014-10-11 18:27:30 +03002040 wl_list_for_each_safe(output, next, &b->compositor->output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002041 wayland_output_destroy(output);
2042
Giulio Camuffo954f1832014-10-11 18:27:30 +03002043 b->compositor->renderer->destroy(b->compositor);
2044 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002045
Giulio Camuffo954f1832014-10-11 18:27:30 +03002046 if (b->theme)
2047 theme_destroy(b->theme);
2048 if (b->frame_device)
2049 cairo_device_destroy(b->frame_device);
2050 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002051
Giulio Camuffo954f1832014-10-11 18:27:30 +03002052 weston_compositor_shutdown(b->compositor);
2053 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002054}
2055
Giulio Camuffo954f1832014-10-11 18:27:30 +03002056WL_EXPORT int
2057backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002058 struct weston_config *config)
2059{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002060 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002061 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002062 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002063 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002064 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002065 const char *section_name, *display_name;
2066 char *name;
2067
2068 const struct weston_option wayland_options[] = {
2069 { WESTON_OPTION_INTEGER, "width", 0, &width },
2070 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002071 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002072 { WESTON_OPTION_STRING, "display", 0, &display_name },
2073 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2074 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002075 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002076 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002077 };
2078
2079 width = 0;
2080 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002081 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002082 display_name = NULL;
2083 use_pixman = 0;
2084 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002085 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002086 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002087 parse_options(wayland_options,
2088 ARRAY_LENGTH(wayland_options), argc, argv);
2089
Giulio Camuffo954f1832014-10-11 18:27:30 +03002090 b = wayland_backend_create(compositor, use_pixman, display_name,
2091 argc, argv, config);
2092 if (!b)
2093 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002094
Giulio Camuffo954f1832014-10-11 18:27:30 +03002095 if (sprawl || b->parent.fshell) {
2096 b->sprawl_across_outputs = 1;
2097 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002098
Giulio Camuffo954f1832014-10-11 18:27:30 +03002099 wl_list_for_each(poutput, &b->parent.output_list, link)
2100 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002101
Giulio Camuffo954f1832014-10-11 18:27:30 +03002102 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002103 }
2104
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002105 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002106 output = wayland_output_create(b, 0, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002107 NULL, 1, 0, 1);
2108 if (!output)
2109 goto err_outputs;
2110
Axel Davydd8b88d2013-11-17 21:34:16 +01002111 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002112 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002113 }
2114
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002115 section = NULL;
2116 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002117 while (weston_config_next_section(config, &section, &section_name)) {
2118 if (!section_name || strcmp(section_name, "output") != 0)
2119 continue;
2120 weston_config_section_get_string(section, "name", &name, NULL);
2121 if (name == NULL)
2122 continue;
2123
2124 if (name[0] != 'W' || name[1] != 'L') {
2125 free(name);
2126 continue;
2127 }
2128 free(name);
2129
Giulio Camuffo954f1832014-10-11 18:27:30 +03002130 output = wayland_output_create_for_config(b, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002131 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002132 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002133 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002134 if (wayland_output_set_windowed(output))
2135 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136
2137 x += output->base.width;
2138 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002139 }
2140
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002141 if (!width)
2142 width = 1024;
2143 if (!height)
2144 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002145 if (!scale)
2146 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002147 while (count > 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002148 output = wayland_output_create(b, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002149 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002150 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002151 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002152 if (wayland_output_set_windowed(output))
2153 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002154
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002155 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002156 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002157 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002158
Giulio Camuffo954f1832014-10-11 18:27:30 +03002159 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002160 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002161 fullscreen_binding, b);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002162
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002164
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002165err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002166 wayland_backend_destroy(b);
2167 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002168}