blob: a819867da133ce1b351de2b52edf7b0042e0f730 [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"
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +020051#include "linux-dmabuf.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052
Jason Ekstrand48ce4212013-10-27 22:25:02 -050053#define WINDOW_TITLE "Weston Compositor"
54
Giulio Camuffo954f1832014-10-11 18:27:30 +030055struct wayland_backend {
56 struct weston_backend base;
57 struct weston_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058
59 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040060 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040061 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_compositor *compositor;
63 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050064 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020065 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010066
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050067 struct wl_list output_list;
68
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069 struct wl_event_source *wl_source;
70 uint32_t event_mask;
71 } parent;
72
Jason Ekstrandff2fd462013-10-27 22:24:58 -050073 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050074 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050075
Jason Ekstrand7744f712013-10-27 22:24:55 -050076 struct theme *theme;
77 cairo_device_t *frame_device;
78 struct wl_cursor_theme *cursor_theme;
79 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050080
Jason Ekstrand06ced802013-11-07 20:13:29 -060081 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010082};
83
84struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050085 struct weston_output base;
86
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010087 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050088 int draw_initial_frame;
89 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050090
91 struct wl_output *output;
92 uint32_t global_id;
93
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050094 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060095 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050097
Jason Ekstrand7744f712013-10-27 22:24:55 -050098 int keyboard_count;
99
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600100 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500101 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500102
Jason Ekstrand7744f712013-10-27 22:24:55 -0500103 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500104 struct wl_egl_window *egl_window;
105 struct {
106 cairo_surface_t *top;
107 cairo_surface_t *left;
108 cairo_surface_t *right;
109 cairo_surface_t *bottom;
110 } border;
111 } gl;
112
113 struct {
114 struct wl_list buffers;
115 struct wl_list free_buffers;
116 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500117
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500118 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500119 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100120};
121
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500122struct wayland_parent_output {
123 struct wayland_output *output;
124 struct wl_list link;
125
126 struct wl_output *global;
127 uint32_t id;
128
129 struct {
130 char *make;
131 char *model;
132 int32_t width, height;
133 uint32_t subpixel;
134 } physical;
135
136 int32_t x, y;
137 uint32_t transform;
138 uint32_t scale;
139
140 struct wl_list mode_list;
141 struct weston_mode *preferred_mode;
142 struct weston_mode *current_mode;
143};
144
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500145struct wayland_shm_buffer {
146 struct wayland_output *output;
147 struct wl_list link;
148 struct wl_list free_link;
149
150 struct wl_buffer *buffer;
151 void *data;
152 size_t size;
153 pixman_region32_t damage;
154 int frame_damaged;
155
156 pixman_image_t *pm_image;
157 cairo_surface_t *c_surface;
158};
159
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100160struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400161 struct weston_seat base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300162 struct wayland_backend *backend;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100163 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500164
165 struct {
166 struct wl_seat *seat;
167 struct wl_pointer *pointer;
168 struct wl_keyboard *keyboard;
169 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500170
171 struct {
172 struct wl_surface *surface;
173 int32_t hx, hy;
174 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500175 } parent;
176
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500177 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100178 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400179 uint32_t enter_serial;
180 int focus;
181 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500182 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100183};
184
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300185struct gl_renderer_interface *gl_renderer;
186
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500187static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500188wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
189{
190 cairo_surface_destroy(buffer->c_surface);
191 pixman_image_unref(buffer->pm_image);
192
193 wl_buffer_destroy(buffer->buffer);
194 munmap(buffer->data, buffer->size);
195
196 pixman_region32_fini(&buffer->damage);
197
198 wl_list_remove(&buffer->link);
199 wl_list_remove(&buffer->free_link);
200 free(buffer);
201}
202
203static void
204buffer_release(void *data, struct wl_buffer *buffer)
205{
206 struct wayland_shm_buffer *sb = data;
207
208 if (sb->output) {
209 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
210 } else {
211 wayland_shm_buffer_destroy(sb);
212 }
213}
214
215static const struct wl_buffer_listener buffer_listener = {
216 buffer_release
217};
218
219static struct wayland_shm_buffer *
220wayland_output_get_shm_buffer(struct wayland_output *output)
221{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300222 struct wayland_backend *b =
223 (struct wayland_backend *) output->base.compositor->backend;
224 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500225 struct wayland_shm_buffer *sb;
226
227 struct wl_shm_pool *pool;
228 int width, height, stride;
229 int32_t fx, fy;
230 int fd;
231 unsigned char *data;
232
233 if (!wl_list_empty(&output->shm.free_buffers)) {
234 sb = container_of(output->shm.free_buffers.next,
235 struct wayland_shm_buffer, free_link);
236 wl_list_remove(&sb->free_link);
237 wl_list_init(&sb->free_link);
238
239 return sb;
240 }
241
242 if (output->frame) {
243 width = frame_width(output->frame);
244 height = frame_height(output->frame);
245 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500246 width = output->base.current_mode->width;
247 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500248 }
249
250 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
251
252 fd = os_create_anonymous_file(height * stride);
253 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000254 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500255 return NULL;
256 }
257
258 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
259 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000260 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500261 close(fd);
262 return NULL;
263 }
264
265 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000266 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200267 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000268 close(fd);
269 free(data);
270 return NULL;
271 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500272
273 sb->output = output;
274 wl_list_init(&sb->free_link);
275 wl_list_insert(&output->shm.buffers, &sb->link);
276
277 pixman_region32_init_rect(&sb->damage, 0, 0,
278 output->base.width, output->base.height);
279 sb->frame_damaged = 1;
280
281 sb->data = data;
282 sb->size = height * stride;
283
284 pool = wl_shm_create_pool(shm, fd, sb->size);
285
286 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
287 width, height,
288 stride,
289 WL_SHM_FORMAT_ARGB8888);
290 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
291 wl_shm_pool_destroy(pool);
292 close(fd);
293
294 memset(data, 0, sb->size);
295
296 sb->c_surface =
297 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
298 width, height, stride);
299
300 fx = 0;
301 fy = 0;
302 if (output->frame)
303 frame_interior(output->frame, &fx, &fy, 0, 0);
304 sb->pm_image =
305 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
306 (uint32_t *)(data + fy * stride) + fx,
307 stride);
308
309 return sb;
310}
311
312static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500313frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500315 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400316 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400317
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500318 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400319
320 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300321
322 /*
323 * This is the fallback case, where Presentation extension is not
324 * available from the parent compositor. We do not know the base for
325 * 'time', so we cannot feed it to finish_frame(). Do the only thing
326 * we can, and pretend finish_frame time is when we process this
327 * event.
328 */
329 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200330 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400331}
332
333static const struct wl_callback_listener frame_listener = {
334 frame_done
335};
336
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500337static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200338draw_initial_frame(struct wayland_output *output)
339{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500340 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200341
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500342 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200343
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500344 /* If we are rendering with GL, then orphan it so that it gets
345 * destroyed immediately */
346 if (output->gl.egl_window)
347 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500348
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500349 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500350 wl_surface_damage(output->parent.surface, 0, 0,
351 output->base.current_mode->width,
352 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200353}
354
355static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500356wayland_output_update_gl_border(struct wayland_output *output)
357{
358 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
359 cairo_t *cr;
360
361 if (!output->frame)
362 return;
363 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
364 return;
365
366 fwidth = frame_width(output->frame);
367 fheight = frame_height(output->frame);
368 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
369
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500370 if (!output->gl.border.top)
371 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
373 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500374 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500375 frame_repaint(output->frame, cr);
376 cairo_destroy(cr);
377 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
378 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 cairo_image_surface_get_stride(output->gl.border.top) / 4,
380 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500381
382
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500383 if (!output->gl.border.left)
384 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500385 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
386 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500387 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500388 cairo_translate(cr, 0, -iy);
389 frame_repaint(output->frame, cr);
390 cairo_destroy(cr);
391 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
392 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500393 cairo_image_surface_get_stride(output->gl.border.left) / 4,
394 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500395
396
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500397 if (!output->gl.border.right)
398 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
400 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500401 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500402 cairo_translate(cr, -(iwidth + ix), -iy);
403 frame_repaint(output->frame, cr);
404 cairo_destroy(cr);
405 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
406 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500407 cairo_image_surface_get_stride(output->gl.border.right) / 4,
408 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409
410
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500411 if (!output->gl.border.bottom)
412 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500413 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
414 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500415 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500416 cairo_translate(cr, 0, -(iy + iheight));
417 frame_repaint(output->frame, cr);
418 cairo_destroy(cr);
419 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
420 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500421 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
422 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500423}
424
425static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200426wayland_output_start_repaint_loop(struct weston_output *output_base)
427{
428 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300429 struct wayland_backend *wb =
430 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200431 struct wl_callback *callback;
432
433 /* If this is the initial frame, we need to attach a buffer so that
434 * the compositor can map the surface and include it in its render
435 * loop. If the surface doesn't end up in the render loop, the frame
436 * callback won't be invoked. The buffer is transparent and of the
437 * same size as the future real output buffer. */
438 if (output->parent.draw_initial_frame) {
439 output->parent.draw_initial_frame = 0;
440
441 draw_initial_frame(output);
442 }
443
444 callback = wl_surface_frame(output->parent.surface);
445 wl_callback_add_listener(callback, &frame_listener, output);
446 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300447 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200448}
449
David Herrmann1edf44c2013-10-22 17:11:26 +0200450static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500451wayland_output_repaint_gl(struct weston_output *output_base,
452 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400453{
454 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400455 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400456 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600457
Kristian Høgsberg33418202011-08-16 23:01:28 -0400458 callback = wl_surface_frame(output->parent.surface);
459 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100460
Jason Ekstrand7744f712013-10-27 22:24:55 -0500461 wayland_output_update_gl_border(output);
462
Pekka Paalanenbc106382012-10-10 12:49:31 +0300463 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200464
465 pixman_region32_subtract(&ec->primary_plane.damage,
466 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200467 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100468}
469
Matt Roper361d2ad2011-08-29 13:52:23 -0700470static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500471wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
472{
473 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
474 cairo_t *cr;
475
476 if (!buffer->output->frame || !buffer->frame_damaged)
477 return;
478
479 cr = cairo_create(buffer->c_surface);
480
481 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
482 fwidth = frame_width(buffer->output->frame);
483 fheight = frame_height(buffer->output->frame);
484
485 /* Set the clip so we don't unnecisaraly damage the surface */
486 cairo_move_to(cr, ix, iy);
487 cairo_rel_line_to(cr, iwidth, 0);
488 cairo_rel_line_to(cr, 0, iheight);
489 cairo_rel_line_to(cr, -iwidth, 0);
490 cairo_line_to(cr, ix, iy);
491 cairo_line_to(cr, 0, iy);
492 cairo_line_to(cr, 0, fheight);
493 cairo_line_to(cr, fwidth, fheight);
494 cairo_line_to(cr, fwidth, 0);
495 cairo_line_to(cr, 0, 0);
496 cairo_line_to(cr, 0, iy);
497 cairo_close_path(cr);
498 cairo_clip(cr);
499
500 /* Draw using a pattern so that the final result gets clipped */
501 cairo_push_group(cr);
502 frame_repaint(buffer->output->frame, cr);
503 cairo_pop_group_to_source(cr);
504 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
505 cairo_paint(cr);
506
507 cairo_destroy(cr);
508}
509
510static void
511wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
512{
513 pixman_region32_t damage;
514 pixman_box32_t *rects;
515 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
516 int i, n;
517
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500518 pixman_region32_init(&damage);
519 weston_transformed_region(sb->output->base.width,
520 sb->output->base.height,
521 sb->output->base.transform,
522 sb->output->base.current_scale,
523 &sb->damage, &damage);
524
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500525 if (sb->output->frame) {
526 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
527 fwidth = frame_width(sb->output->frame);
528 fheight = frame_height(sb->output->frame);
529
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500530 pixman_region32_translate(&damage, ix, iy);
531
532 if (sb->frame_damaged) {
533 pixman_region32_union_rect(&damage, &damage,
534 0, 0, fwidth, iy);
535 pixman_region32_union_rect(&damage, &damage,
536 0, iy, ix, iheight);
537 pixman_region32_union_rect(&damage, &damage,
538 ix + iwidth, iy,
539 fwidth - (ix + iwidth), iheight);
540 pixman_region32_union_rect(&damage, &damage,
541 0, iy + iheight,
542 fwidth, fheight - (iy + iheight));
543 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500544 }
545
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500546 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500547 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
548 for (i = 0; i < n; ++i)
549 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
550 rects[i].y1, rects[i].x2 - rects[i].x1,
551 rects[i].y2 - rects[i].y1);
552
553 if (sb->output->frame)
554 pixman_region32_fini(&damage);
555}
556
557static int
558wayland_output_repaint_pixman(struct weston_output *output_base,
559 pixman_region32_t *damage)
560{
561 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300562 struct wayland_backend *b =
563 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500564 struct wl_callback *callback;
565 struct wayland_shm_buffer *sb;
566
567 if (output->frame) {
568 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
569 wl_list_for_each(sb, &output->shm.buffers, link)
570 sb->frame_damaged = 1;
571 }
572
573 wl_list_for_each(sb, &output->shm.buffers, link)
574 pixman_region32_union(&sb->damage, &sb->damage, damage);
575
576 sb = wayland_output_get_shm_buffer(output);
577
578 wayland_output_update_shm_border(sb);
579 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300580 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500581
582 wayland_shm_buffer_attach(sb);
583
584 callback = wl_surface_frame(output->parent.surface);
585 wl_callback_add_listener(callback, &frame_listener, output);
586 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300587 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500588
589 pixman_region32_fini(&sb->damage);
590 pixman_region32_init(&sb->damage);
591 sb->frame_damaged = 0;
592
Giulio Camuffo954f1832014-10-11 18:27:30 +0300593 pixman_region32_subtract(&b->compositor->primary_plane.damage,
594 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500595 return 0;
596}
597
598static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500599wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700600{
601 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300602 struct wayland_backend *b =
603 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700604
Giulio Camuffo954f1832014-10-11 18:27:30 +0300605 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500606 pixman_renderer_output_destroy(output_base);
607 } else {
608 gl_renderer->output_destroy(output_base);
609 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100610
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500611 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500612 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500613 if (output->parent.shell_surface)
614 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500615
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600616 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500617 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600618
619 cairo_surface_destroy(output->gl.border.top);
620 cairo_surface_destroy(output->gl.border.left);
621 cairo_surface_destroy(output->gl.border.right);
622 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500623
624 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700625 free(output);
626
627 return;
628}
629
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300630static const struct wl_shell_surface_listener shell_surface_listener;
631
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200632static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500633wayland_output_init_gl_renderer(struct wayland_output *output)
634{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500635 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500636
637 if (output->frame) {
638 fwidth = frame_width(output->frame);
639 fheight = frame_height(output->frame);
640 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500641 fwidth = output->base.current_mode->width;
642 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500643 }
644
645 output->gl.egl_window =
646 wl_egl_window_create(output->parent.surface,
647 fwidth, fheight);
648 if (!output->gl.egl_window) {
649 weston_log("failure to create wl_egl_window\n");
650 return -1;
651 }
652
653 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000654 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100655 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000656 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500657 NULL,
658 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500659 goto cleanup_window;
660
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500661 return 0;
662
663cleanup_window:
664 wl_egl_window_destroy(output->gl.egl_window);
665 return -1;
666}
667
668static int
669wayland_output_init_pixman_renderer(struct wayland_output *output)
670{
671 return pixman_renderer_output_create(&output->base);
672}
673
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600674static void
675wayland_output_resize_surface(struct wayland_output *output)
676{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300677 struct wayland_backend *b =
678 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600679 struct wayland_shm_buffer *buffer, *next;
680 int32_t ix, iy, iwidth, iheight;
681 int32_t width, height;
682 struct wl_region *region;
683
684 width = output->base.current_mode->width;
685 height = output->base.current_mode->height;
686
687 if (output->frame) {
688 frame_resize_inside(output->frame, width, height);
689
690 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300691 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600692 wl_region_add(region, ix, iy, iwidth, iheight);
693 wl_surface_set_input_region(output->parent.surface, region);
694 wl_region_destroy(region);
695
696 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300697 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600698 wl_region_add(region, ix, iy, iwidth, iheight);
699 wl_surface_set_opaque_region(output->parent.surface, region);
700 wl_region_destroy(region);
701
702 width = frame_width(output->frame);
703 height = frame_height(output->frame);
704 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300705 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600706 wl_region_add(region, 0, 0, width, height);
707 wl_surface_set_input_region(output->parent.surface, region);
708 wl_region_destroy(region);
709
Giulio Camuffo954f1832014-10-11 18:27:30 +0300710 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600711 wl_region_add(region, 0, 0, width, height);
712 wl_surface_set_opaque_region(output->parent.surface, region);
713 wl_region_destroy(region);
714 }
715
716 if (output->gl.egl_window) {
717 wl_egl_window_resize(output->gl.egl_window,
718 width, height, 0, 0);
719
720 /* These will need to be re-created due to the resize */
721 gl_renderer->output_set_border(&output->base,
722 GL_RENDERER_BORDER_TOP,
723 0, 0, 0, NULL);
724 cairo_surface_destroy(output->gl.border.top);
725 output->gl.border.top = NULL;
726 gl_renderer->output_set_border(&output->base,
727 GL_RENDERER_BORDER_LEFT,
728 0, 0, 0, NULL);
729 cairo_surface_destroy(output->gl.border.left);
730 output->gl.border.left = NULL;
731 gl_renderer->output_set_border(&output->base,
732 GL_RENDERER_BORDER_RIGHT,
733 0, 0, 0, NULL);
734 cairo_surface_destroy(output->gl.border.right);
735 output->gl.border.right = NULL;
736 gl_renderer->output_set_border(&output->base,
737 GL_RENDERER_BORDER_BOTTOM,
738 0, 0, 0, NULL);
739 cairo_surface_destroy(output->gl.border.bottom);
740 output->gl.border.bottom = NULL;
741 }
742
743 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500744 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600745 wayland_shm_buffer_destroy(buffer);
746 /* These will get thrown away when they get released */
747 wl_list_for_each(buffer, &output->shm.buffers, link)
748 buffer->output = NULL;
749}
750
751static int
752wayland_output_set_windowed(struct wayland_output *output)
753{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300754 struct wayland_backend *b =
755 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600756 int tlen;
757 char *title;
758
759 if (output->frame)
760 return 0;
761
762 if (output->name) {
763 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
764 title = malloc(tlen + 1);
765 if (!title)
766 return -1;
767
768 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
769 } else {
770 title = strdup(WINDOW_TITLE);
771 }
772
Giulio Camuffo954f1832014-10-11 18:27:30 +0300773 if (!b->theme) {
774 b->theme = theme_create();
775 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600776 free(title);
777 return -1;
778 }
779 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300780 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600781 FRAME_BUTTON_CLOSE, title);
782 free(title);
783 if (!output->frame)
784 return -1;
785
786 if (output->keyboard_count)
787 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
788
789 wayland_output_resize_surface(output);
790
791 wl_shell_surface_set_toplevel(output->parent.shell_surface);
792
793 return 0;
794}
795
796static void
797wayland_output_set_fullscreen(struct wayland_output *output,
798 enum wl_shell_surface_fullscreen_method method,
799 uint32_t framerate, struct wl_output *target)
800{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300801 struct wayland_backend *b =
802 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500803
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600804 if (output->frame) {
805 frame_destroy(output->frame);
806 output->frame = NULL;
807 }
808
809 wayland_output_resize_surface(output);
810
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500811 if (output->parent.shell_surface) {
812 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
813 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300814 } else if (b->parent.fshell) {
815 _wl_fullscreen_shell_present_surface(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500816 output->parent.surface,
817 method, target);
818 }
819}
820
821static struct weston_mode *
822wayland_output_choose_mode(struct wayland_output *output,
823 struct weston_mode *ref_mode)
824{
825 struct weston_mode *mode;
826
827 /* First look for an exact match */
828 wl_list_for_each(mode, &output->base.mode_list, link)
829 if (mode->width == ref_mode->width &&
830 mode->height == ref_mode->height &&
831 mode->refresh == ref_mode->refresh)
832 return mode;
833
834 /* If we can't find an exact match, ignore refresh and try again */
835 wl_list_for_each(mode, &output->base.mode_list, link)
836 if (mode->width == ref_mode->width &&
837 mode->height == ref_mode->height)
838 return mode;
839
840 /* Yeah, we failed */
841 return NULL;
842}
843
844enum mode_status {
845 MODE_STATUS_UNKNOWN,
846 MODE_STATUS_SUCCESS,
847 MODE_STATUS_FAIL,
848 MODE_STATUS_CANCEL,
849};
850
851static void
852mode_feedback_successful(void *data,
853 struct _wl_fullscreen_shell_mode_feedback *fb)
854{
855 enum mode_status *value = data;
856
857 printf("Mode switch successful\n");
858
859 *value = MODE_STATUS_SUCCESS;
860}
861
862static void
863mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
864{
865 enum mode_status *value = data;
866
867 printf("Mode switch failed\n");
868
869 *value = MODE_STATUS_FAIL;
870}
871
872static void
873mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
874{
875 enum mode_status *value = data;
876
877 printf("Mode switch cancelled\n");
878
879 *value = MODE_STATUS_CANCEL;
880}
881
882struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
883 mode_feedback_successful,
884 mode_feedback_failed,
885 mode_feedback_cancelled,
886};
887
888static int
889wayland_output_switch_mode(struct weston_output *output_base,
890 struct weston_mode *mode)
891{
892 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300893 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500894 struct wl_surface *old_surface;
895 struct weston_mode *old_mode;
896 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
897 enum mode_status mode_status;
898 int ret = 0;
899
900 if (output_base == NULL) {
901 weston_log("output is NULL.\n");
902 return -1;
903 }
904
905 if (mode == NULL) {
906 weston_log("mode is NULL.\n");
907 return -1;
908 }
909
Giulio Camuffo954f1832014-10-11 18:27:30 +0300910 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500911
Giulio Camuffo954f1832014-10-11 18:27:30 +0300912 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500913 return -1;
914
915 mode = wayland_output_choose_mode(output, mode);
916 if (mode == NULL)
917 return -1;
918
919 if (output->base.current_mode == mode)
920 return 0;
921
922 old_mode = output->base.current_mode;
923 old_surface = output->parent.surface;
924 output->base.current_mode = mode;
925 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300926 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500927 wl_surface_set_user_data(output->parent.surface, output);
928
929 /* Blow the old buffers because we changed size/surfaces */
930 wayland_output_resize_surface(output);
931
932 mode_feedback =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300933 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500934 output->parent.surface,
935 output->parent.output,
936 mode->refresh);
937 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
938 &mode_feedback_listener,
939 &mode_status);
940
941 /* This should kick-start things again */
942 output->parent.draw_initial_frame = 1;
943 wayland_output_start_repaint_loop(&output->base);
944
945 mode_status = MODE_STATUS_UNKNOWN;
946 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300947 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500948
949 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
950
951 if (mode_status == MODE_STATUS_FAIL) {
952 output->base.current_mode = old_mode;
953 wl_surface_destroy(output->parent.surface);
954 output->parent.surface = old_surface;
955 wayland_output_resize_surface(output);
956
957 return -1;
958 }
959
960 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
961 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
962
Giulio Camuffo954f1832014-10-11 18:27:30 +0300963 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500964 pixman_renderer_output_destroy(output_base);
965 if (wayland_output_init_pixman_renderer(output) < 0)
966 goto err_output;
967 } else {
968 gl_renderer->output_destroy(output_base);
969 wl_egl_window_destroy(output->gl.egl_window);
970 if (wayland_output_init_gl_renderer(output) < 0)
971 goto err_output;
972 }
973 wl_surface_destroy(old_surface);
974
975 weston_output_schedule_repaint(&output->base);
976
977 return 0;
978
979err_output:
980 /* XXX */
981 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600982}
983
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500984static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300985wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600986 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500987 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100988{
989 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500990 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600991
992 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
993 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100994
Peter Huttererf3d62272013-08-08 11:57:05 +1000995 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100996 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500997 return NULL;
998
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600999 output->name = name ? strdup(name) : NULL;
1000 output->base.make = "waywayland";
1001 output->base.model = "none";
1002
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001003 output_width = width * scale;
1004 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001005
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001006 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001007 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001008 if (!output->parent.surface)
1009 goto err_name;
1010 wl_surface_set_user_data(output->parent.surface, output);
1011
1012 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001013
Giulio Camuffo954f1832014-10-11 18:27:30 +03001014 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001015 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001016 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001017 output->parent.surface);
1018 if (!output->parent.shell_surface)
1019 goto err_surface;
1020 wl_shell_surface_add_listener(output->parent.shell_surface,
1021 &shell_surface_listener, output);
1022 }
1023
Giulio Camuffo954f1832014-10-11 18:27:30 +03001024 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001025 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1026 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001027 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001028 if (!width)
1029 output_width = output->parent.configure_width;
1030 if (!height)
1031 output_height = output->parent.configure_height;
1032 }
1033
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001034 output->mode.flags =
1035 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001036 output->mode.width = output_width;
1037 output->mode.height = output_height;
1038 output->mode.refresh = 60000;
1039 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001040 wl_list_init(&output->base.mode_list);
1041 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001042 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001043
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001044 wl_list_init(&output->shm.buffers);
1045 wl_list_init(&output->shm.free_buffers);
1046
Giulio Camuffo954f1832014-10-11 18:27:30 +03001047 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001048 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001049
Giulio Camuffo954f1832014-10-11 18:27:30 +03001050 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001051 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001052 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001053 output->base.repaint = wayland_output_repaint_pixman;
1054 } else {
1055 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001056 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001057 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001058 }
1059
Jonas Ådahle5a12252013-04-05 23:07:11 +02001060 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001061 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001062 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001063 output->base.set_backlight = NULL;
1064 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001065 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001066
Giulio Camuffo954f1832014-10-11 18:27:30 +03001067 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001068
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001069 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001070
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001071err_output:
1072 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001073 if (output->parent.shell_surface)
1074 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001075err_surface:
1076 wl_surface_destroy(output->parent.surface);
1077err_name:
1078 free(output->name);
1079
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001080 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001081 free(output);
1082
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001083 return NULL;
1084}
1085
1086static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001087wayland_output_create_for_config(struct wayland_backend *b,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001088 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001089 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001090 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001091{
1092 struct wayland_output *output;
1093 char *mode, *t, *name, *str;
1094 int width, height, scale;
1095 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001096 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001097
1098 weston_config_section_get_string(config_section, "name", &name, NULL);
1099 if (name) {
1100 slen = strlen(name);
1101 slen += strlen(WINDOW_TITLE " - ");
1102 str = malloc(slen + 1);
1103 if (str)
1104 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1105 free(name);
1106 name = str;
1107 }
1108 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001109 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001110
1111 weston_config_section_get_string(config_section,
1112 "mode", &mode, "1024x600");
1113 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1114 weston_log("Invalid mode \"%s\" for output %s\n",
1115 mode, name);
1116 width = 1024;
1117 height = 640;
1118 }
1119 free(mode);
1120
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001121 if (option_width)
1122 width = option_width;
1123 if (option_height)
1124 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001125
1126 weston_config_section_get_int(config_section, "scale", &scale, 1);
1127
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001128 if (option_scale)
1129 scale = option_scale;
1130
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001131 weston_config_section_get_string(config_section,
1132 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001133 if (weston_parse_transform(t, &transform) < 0)
1134 weston_log("Invalid transform \"%s\" for output %s\n",
1135 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001136 free(t);
1137
Giulio Camuffo954f1832014-10-11 18:27:30 +03001138 output = wayland_output_create(b, x, y, width, height, name, 0,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001139 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001140 free(name);
1141
1142 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001143}
1144
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001145static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001146wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001147 struct wayland_parent_output *poutput)
1148{
1149 struct wayland_output *output;
1150 struct weston_mode *mode;
1151 int32_t x;
1152
1153 if (poutput->current_mode) {
1154 mode = poutput->current_mode;
1155 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001156 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001157 } else if (!wl_list_empty(&poutput->mode_list)) {
1158 mode = container_of(poutput->mode_list.next,
1159 struct weston_mode, link);
1160 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001161 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001162 return NULL;
1163 }
1164
Giulio Camuffo954f1832014-10-11 18:27:30 +03001165 if (!wl_list_empty(&b->compositor->output_list)) {
1166 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001167 struct wayland_output, base.link);
1168 x = output->base.x + output->base.current_mode->width;
1169 } else {
1170 x = 0;
1171 }
1172
Giulio Camuffo954f1832014-10-11 18:27:30 +03001173 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001174 NULL, 0,
1175 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1176 if (!output)
1177 return NULL;
1178
1179 output->parent.output = poutput->global;
1180
1181 output->base.make = poutput->physical.make;
1182 output->base.model = poutput->physical.model;
1183 wl_list_init(&output->base.mode_list);
1184 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1185 wl_list_init(&poutput->mode_list);
1186
1187 wayland_output_set_fullscreen(output,
1188 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1189 mode->refresh, poutput->global);
1190
1191 if (output->parent.shell_surface) {
1192 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1193 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1194 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001195 } else if (b->parent.fshell) {
1196 _wl_fullscreen_shell_present_surface(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001197 output->parent.surface,
1198 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1199 poutput->global);
1200 _wl_fullscreen_shell_mode_feedback_destroy(
Giulio Camuffo954f1832014-10-11 18:27:30 +03001201 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001202 output->parent.surface,
1203 poutput->global,
1204 mode->refresh));
1205 }
1206
1207 return output;
1208}
1209
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001210static void
1211shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1212 uint32_t serial)
1213{
1214 wl_shell_surface_pong(shell_surface, serial);
1215}
1216
1217static void
1218shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1219 uint32_t edges, int32_t width, int32_t height)
1220{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001221 struct wayland_output *output = data;
1222
1223 output->parent.configure_width = width;
1224 output->parent.configure_height = height;
1225
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001226 /* FIXME: implement resizing */
1227}
1228
1229static void
1230shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1231{
1232}
1233
1234static const struct wl_shell_surface_listener shell_surface_listener = {
1235 shell_surface_ping,
1236 shell_surface_configure,
1237 shell_surface_popup_done
1238};
1239
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001240/* Events received from the wayland-server this compositor is client of: */
1241
Jason Ekstrand7744f712013-10-27 22:24:55 -05001242/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001243static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001244input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001245{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001246
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247 struct wl_buffer *buffer;
1248 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001249
Giulio Camuffo954f1832014-10-11 18:27:30 +03001250 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001251 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001252
Giulio Camuffo954f1832014-10-11 18:27:30 +03001253 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001254 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001255 if (!buffer)
1256 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001257
1258 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1259 input->parent.cursor.surface,
1260 image->hotspot_x, image->hotspot_y);
1261
1262 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1263 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1264 image->width, image->height);
1265 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001266}
1267
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001268static void
Daniel Stone37816df2012-05-16 18:45:18 +01001269input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1270 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001271 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001272{
1273 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001274 int32_t fx, fy;
1275 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001276
Daniel Stone50692802012-06-22 13:21:41 +01001277 /* XXX: If we get a modifier event immediately before the focus,
1278 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001279 input->enter_serial = serial;
1280 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001281
1282 if (input->output->frame) {
1283 location = frame_pointer_enter(input->output->frame, input,
1284 wl_fixed_to_int(x),
1285 wl_fixed_to_int(y));
1286 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1287 x -= wl_fixed_from_int(fx);
1288 y -= wl_fixed_from_int(fy);
1289
1290 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1291 weston_output_schedule_repaint(&input->output->base);
1292 } else {
1293 location = THEME_LOCATION_CLIENT_AREA;
1294 }
1295
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001296 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001297
1298 if (location == THEME_LOCATION_CLIENT_AREA) {
1299 input->focus = 1;
1300 notify_pointer_focus(&input->base, &input->output->base, x, y);
1301 wl_pointer_set_cursor(input->parent.pointer,
1302 input->enter_serial, NULL, 0, 0);
1303 } else {
1304 input->focus = 0;
1305 notify_pointer_focus(&input->base, NULL, 0, 0);
1306 input_set_cursor(input);
1307 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001308}
1309
1310static void
Daniel Stone37816df2012-05-16 18:45:18 +01001311input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1312 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001313{
1314 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001315
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001316 if (!input->output)
1317 return;
1318
Jason Ekstrand7744f712013-10-27 22:24:55 -05001319 if (input->output->frame) {
1320 frame_pointer_leave(input->output->frame, input);
1321
1322 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1323 weston_output_schedule_repaint(&input->output->base);
1324 }
1325
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001326 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001327 input->output = NULL;
1328 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001329}
1330
1331static void
Daniel Stone37816df2012-05-16 18:45:18 +01001332input_handle_motion(void *data, struct wl_pointer *pointer,
1333 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1334{
1335 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336 int32_t fx, fy;
1337 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001338
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001339 if (!input->output)
1340 return;
1341
Jason Ekstrand7744f712013-10-27 22:24:55 -05001342 if (input->output->frame) {
1343 location = frame_pointer_motion(input->output->frame, input,
1344 wl_fixed_to_int(x),
1345 wl_fixed_to_int(y));
1346 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1347 x -= wl_fixed_from_int(fx);
1348 y -= wl_fixed_from_int(fy);
1349
1350 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1351 weston_output_schedule_repaint(&input->output->base);
1352 } else {
1353 location = THEME_LOCATION_CLIENT_AREA;
1354 }
1355
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001356 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001357
1358 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1359 input_set_cursor(input);
1360 notify_pointer_focus(&input->base, NULL, 0, 0);
1361 input->focus = 0;
1362 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1363 wl_pointer_set_cursor(input->parent.pointer,
1364 input->enter_serial, NULL, 0, 0);
1365 notify_pointer_focus(&input->base, &input->output->base, x, y);
1366 input->focus = 1;
1367 }
1368
1369 if (location == THEME_LOCATION_CLIENT_AREA)
1370 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001371}
1372
1373static void
1374input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001375 uint32_t serial, uint32_t time, uint32_t button,
1376 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001377{
1378 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001379 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001380 enum frame_button_state fstate;
1381 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001382
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001383 if (!input->output)
1384 return;
1385
Jason Ekstrand7744f712013-10-27 22:24:55 -05001386 if (input->output->frame) {
1387 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1388 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1389
1390 location = frame_pointer_button(input->output->frame, input,
1391 button, fstate);
1392
1393 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1394
1395 wl_shell_surface_move(input->output->parent.shell_surface,
1396 input->parent.seat, serial);
1397 frame_status_clear(input->output->frame,
1398 FRAME_STATUS_MOVE);
1399 return;
1400 }
1401
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001402 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1403 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001404 input->output = NULL;
1405 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001406
Giulio Camuffo954f1832014-10-11 18:27:30 +03001407 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001408 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001409
1410 return;
1411 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001412
1413 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1414 weston_output_schedule_repaint(&input->output->base);
1415 } else {
1416 location = THEME_LOCATION_CLIENT_AREA;
1417 }
1418
1419 if (location == THEME_LOCATION_CLIENT_AREA)
1420 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001421}
1422
1423static void
1424input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001425 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001426{
1427 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001428
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001429 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001430}
1431
1432static const struct wl_pointer_listener pointer_listener = {
1433 input_handle_pointer_enter,
1434 input_handle_pointer_leave,
1435 input_handle_motion,
1436 input_handle_button,
1437 input_handle_axis,
1438};
1439
1440static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001441input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1442 int fd, uint32_t size)
1443{
1444 struct wayland_input *input = data;
1445 struct xkb_keymap *keymap;
1446 char *map_str;
1447
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001448 if (!data) {
1449 close(fd);
1450 return;
1451 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001452
1453 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1454 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1455 if (map_str == MAP_FAILED) {
1456 weston_log("mmap failed: %m\n");
1457 goto error;
1458 }
1459
Giulio Camuffo954f1832014-10-11 18:27:30 +03001460 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001461 map_str,
1462 XKB_KEYMAP_FORMAT_TEXT_V1,
1463 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001464 munmap(map_str, size);
1465
1466 if (!keymap) {
1467 weston_log("failed to compile keymap\n");
1468 goto error;
1469 }
1470
1471 input->keyboard_state_update = STATE_UPDATE_NONE;
1472 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1473 weston_log("No keymap provided; falling back to defalt\n");
1474 keymap = NULL;
1475 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1476 } else {
1477 weston_log("Invalid keymap\n");
1478 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001479 }
1480
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001481 close(fd);
1482
Derek Foreman1281a362015-07-31 16:55:32 -05001483 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001484 weston_seat_update_keymap(&input->base, keymap);
1485 else
1486 weston_seat_init_keyboard(&input->base, keymap);
1487
Ran Benitac9c74152014-08-19 23:59:52 +03001488 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001489
1490 return;
1491
1492error:
1493 wl_keyboard_release(input->parent.keyboard);
1494 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001495}
1496
1497static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001498input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001499 struct wl_keyboard *keyboard,
1500 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001501 struct wl_surface *surface,
1502 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001503{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001504 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001505 struct wayland_output *focus;
1506
1507 focus = input->keyboard_focus;
1508 if (focus) {
1509 /* This shouldn't happen */
1510 focus->keyboard_count--;
1511 if (!focus->keyboard_count && focus->frame)
1512 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1513 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1514 weston_output_schedule_repaint(&focus->base);
1515 }
1516
1517 input->keyboard_focus = wl_surface_get_user_data(surface);
1518 input->keyboard_focus->keyboard_count++;
1519
1520 focus = input->keyboard_focus;
1521 if (focus->frame) {
1522 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1523 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1524 weston_output_schedule_repaint(&focus->base);
1525 }
1526
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001527
Daniel Stone50692802012-06-22 13:21:41 +01001528 /* XXX: If we get a modifier event immediately before the focus,
1529 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001530 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001531 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001532}
1533
1534static void
1535input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001536 struct wl_keyboard *keyboard,
1537 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001538 struct wl_surface *surface)
1539{
1540 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001541 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001542
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001543 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001544
1545 focus = input->keyboard_focus;
1546 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001547 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001548
1549 focus->keyboard_count--;
1550 if (!focus->keyboard_count && focus->frame) {
1551 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1552 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1553 weston_output_schedule_repaint(&focus->base);
1554 }
1555
1556 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001557}
1558
Daniel Stone37816df2012-05-16 18:45:18 +01001559static void
1560input_handle_key(void *data, struct wl_keyboard *keyboard,
1561 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1562{
1563 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001564
Daniel Stone50692802012-06-22 13:21:41 +01001565 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001566 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001567 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001568 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001569 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001570}
1571
Daniel Stone351eb612012-05-31 15:27:47 -04001572static void
Derek Foreman1281a362015-07-31 16:55:32 -05001573input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001574 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001575 uint32_t mods_latched, uint32_t mods_locked,
1576 uint32_t group)
1577{
Derek Foreman1281a362015-07-31 16:55:32 -05001578 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001579 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001580 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001581 uint32_t serial_out;
1582
1583 /* If we get a key event followed by a modifier event with the
1584 * same serial number, then we try to preserve those semantics by
1585 * reusing the same serial number on the way out too. */
1586 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001587 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001588 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001589 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001590
Derek Foreman1281a362015-07-31 16:55:32 -05001591 keyboard = weston_seat_get_keyboard(&input->base);
1592 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001593 mods_depressed, mods_latched,
1594 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001595 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001596}
1597
Jonny Lamb497994a2014-08-12 14:58:26 +02001598static void
1599input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1600 int32_t rate, int32_t delay)
1601{
1602 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001603 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001604
Giulio Camuffo954f1832014-10-11 18:27:30 +03001605 b->compositor->kb_repeat_rate = rate;
1606 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001607}
1608
Daniel Stone37816df2012-05-16 18:45:18 +01001609static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001610 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001611 input_handle_keyboard_enter,
1612 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001613 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001614 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001615 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001616};
1617
1618static void
Daniel Stone37816df2012-05-16 18:45:18 +01001619input_handle_capabilities(void *data, struct wl_seat *seat,
1620 enum wl_seat_capability caps)
1621{
1622 struct wayland_input *input = data;
1623
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001624 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1625 input->parent.pointer = wl_seat_get_pointer(seat);
1626 wl_pointer_set_user_data(input->parent.pointer, input);
1627 wl_pointer_add_listener(input->parent.pointer,
1628 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001629 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001630 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1631 wl_pointer_destroy(input->parent.pointer);
1632 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001633 }
1634
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001635 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1636 input->parent.keyboard = wl_seat_get_keyboard(seat);
1637 wl_keyboard_set_user_data(input->parent.keyboard, input);
1638 wl_keyboard_add_listener(input->parent.keyboard,
1639 &keyboard_listener, input);
1640 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1641 wl_keyboard_destroy(input->parent.keyboard);
1642 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001643 }
1644}
1645
Jonny Lamb497994a2014-08-12 14:58:26 +02001646static void
1647input_handle_name(void *data, struct wl_seat *seat,
1648 const char *name)
1649{
1650}
1651
Daniel Stone37816df2012-05-16 18:45:18 +01001652static const struct wl_seat_listener seat_listener = {
1653 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001654 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001655};
1656
1657static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001658display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001659{
1660 struct wayland_input *input;
1661
Peter Huttererf3d62272013-08-08 11:57:05 +10001662 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001663 if (input == NULL)
1664 return;
1665
Giulio Camuffo954f1832014-10-11 18:27:30 +03001666 weston_seat_init(&input->base, b->compositor, "default");
1667 input->backend = b;
1668 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001669 &wl_seat_interface, MIN(version, 4));
Giulio Camuffo954f1832014-10-11 18:27:30 +03001670 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001671
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001672 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1673 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001674
1675 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001676 wl_compositor_create_surface(b->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001677}
1678
1679static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001680wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1681 int32_t x, int32_t y,
1682 int32_t physical_width, int32_t physical_height,
1683 int32_t subpixel, const char *make,
1684 const char *model, int32_t transform)
1685{
1686 struct wayland_parent_output *output = data;
1687
1688 output->x = x;
1689 output->y = y;
1690 output->physical.width = physical_width;
1691 output->physical.height = physical_height;
1692 output->physical.subpixel = subpixel;
1693
1694 free(output->physical.make);
1695 output->physical.make = strdup(make);
1696 free(output->physical.model);
1697 output->physical.model = strdup(model);
1698
1699 output->transform = transform;
1700}
1701
1702static struct weston_mode *
1703find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1704{
1705 struct weston_mode *mode;
1706
1707 wl_list_for_each(mode, list, link) {
1708 if (mode->width == width && mode->height == height &&
1709 mode->refresh == refresh)
1710 return mode;
1711 }
1712
1713 mode = zalloc(sizeof *mode);
1714 if (!mode)
1715 return NULL;
1716
1717 mode->width = width;
1718 mode->height = height;
1719 mode->refresh = refresh;
1720 wl_list_insert(list, &mode->link);
1721
1722 return mode;
1723}
1724
1725static void
1726wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1727 uint32_t flags, int32_t width, int32_t height,
1728 int32_t refresh)
1729{
1730 struct wayland_parent_output *output = data;
1731 struct weston_mode *mode;
1732
1733 if (output->output) {
1734 mode = find_mode(&output->output->base.mode_list,
1735 width, height, refresh);
1736 if (!mode)
1737 return;
1738 mode->flags = flags;
1739 /* Do a mode-switch on current mode change? */
1740 } else {
1741 mode = find_mode(&output->mode_list, width, height, refresh);
1742 if (!mode)
1743 return;
1744 mode->flags = flags;
1745 if (flags & WL_OUTPUT_MODE_CURRENT)
1746 output->current_mode = mode;
1747 if (flags & WL_OUTPUT_MODE_PREFERRED)
1748 output->preferred_mode = mode;
1749 }
1750}
1751
1752static const struct wl_output_listener output_listener = {
1753 wayland_parent_output_geometry,
1754 wayland_parent_output_mode
1755};
1756
1757static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001758wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001759{
1760 struct wayland_parent_output *output;
1761
1762 output = zalloc(sizeof *output);
1763 if (!output)
1764 return;
1765
1766 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001767 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001768 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001769 if (!output->global) {
1770 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001771 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001772 }
1773
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001774 wl_output_add_listener(output->global, &output_listener, output);
1775
1776 output->scale = 0;
1777 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1778 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1779 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001780 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001781
Giulio Camuffo954f1832014-10-11 18:27:30 +03001782 if (b->sprawl_across_outputs) {
1783 wl_display_roundtrip(b->parent.wl_display);
1784 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001785 }
1786}
1787
1788static void
1789wayland_parent_output_destroy(struct wayland_parent_output *output)
1790{
1791 struct weston_mode *mode, *next;
1792
1793 if (output->output)
1794 wayland_output_destroy(&output->output->base);
1795
1796 wl_output_destroy(output->global);
1797 free(output->physical.make);
1798 free(output->physical.model);
1799
1800 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1801 wl_list_remove(&mode->link);
1802 free(mode);
1803 }
1804}
1805
1806static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001807registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1808 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001809{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001810 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001811
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001812 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001813 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001814 wl_registry_bind(registry, name,
1815 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001816 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001817 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001818 wl_registry_bind(registry, name,
1819 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001820 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001821 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001822 wl_registry_bind(registry, name,
1823 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001824 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001825 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001826 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001827 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001828 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001829 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02001830 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001831 }
1832}
1833
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001834static void
1835registry_handle_global_remove(void *data, struct wl_registry *registry,
1836 uint32_t name)
1837{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001838 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001839 struct wayland_parent_output *output;
1840
Giulio Camuffo954f1832014-10-11 18:27:30 +03001841 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001842 if (output->id == name)
1843 wayland_parent_output_destroy(output);
1844}
1845
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001846static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001847 registry_handle_global,
1848 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001849};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001850
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001851static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001852wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001853{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001854 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001855 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001856
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001857 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03001858 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001859 return 0;
1860 }
1861
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001862 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001863 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001864 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001865 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001866
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001867 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001868 count = wl_display_dispatch_pending(b->parent.wl_display);
1869 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001870 }
1871
1872 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001873}
1874
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001875static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001876wayland_restore(struct weston_compositor *ec)
1877{
1878}
1879
1880static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001881wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001882{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001883 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001884
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001885 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001886
Giulio Camuffo954f1832014-10-11 18:27:30 +03001887 if (b->parent.shm)
1888 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001889
Giulio Camuffo954f1832014-10-11 18:27:30 +03001890 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001891}
1892
Jason Ekstrand7744f712013-10-27 22:24:55 -05001893static const char *left_ptrs[] = {
1894 "left_ptr",
1895 "default",
1896 "top_left_arrow",
1897 "left-arrow"
1898};
1899
1900static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001901create_cursor(struct wayland_backend *b, struct weston_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001902{
1903 struct weston_config_section *s;
1904 int size;
1905 char *theme = NULL;
1906 unsigned int i;
1907
1908 s = weston_config_get_section(config, "shell", NULL, NULL);
1909 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1910 weston_config_section_get_int(s, "cursor-size", &size, 32);
1911
Giulio Camuffo954f1832014-10-11 18:27:30 +03001912 b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
1913 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01001914 fprintf(stderr, "could not load cursor theme\n");
1915 return;
1916 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001917
U. Artie Eoffff755002014-01-17 12:36:58 -08001918 free(theme);
1919
Giulio Camuffo954f1832014-10-11 18:27:30 +03001920 b->cursor = NULL;
1921 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1922 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001923 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001924 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001925 fprintf(stderr, "could not load left cursor\n");
1926 return;
1927 }
1928}
1929
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001930static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05001931fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
1932 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001933{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001934 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001935 struct wayland_input *input = NULL;
1936
Giulio Camuffo954f1832014-10-11 18:27:30 +03001937 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05001938 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001939 break;
1940
1941 if (!input || !input->output)
1942 return;
1943
1944 if (input->output->frame)
1945 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1946 else
1947 wayland_output_set_windowed(input->output);
1948
1949 weston_output_schedule_repaint(&input->output->base);
1950}
1951
Giulio Camuffo954f1832014-10-11 18:27:30 +03001952static struct wayland_backend *
1953wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
1954 const char *display_name, int *argc, char *argv[],
1955 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001956{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001957 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001958 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001959 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001960
Giulio Camuffo954f1832014-10-11 18:27:30 +03001961 b = zalloc(sizeof *b);
1962 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001963 return NULL;
1964
Giulio Camuffo954f1832014-10-11 18:27:30 +03001965 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001966 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001967 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001968
Giulio Camuffo954f1832014-10-11 18:27:30 +03001969 b->parent.wl_display = wl_display_connect(display_name);
1970 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001971 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001972 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001973 }
1974
Giulio Camuffo954f1832014-10-11 18:27:30 +03001975 wl_list_init(&b->parent.output_list);
1976 wl_list_init(&b->input_list);
1977 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
1978 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
1979 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001980
Giulio Camuffo954f1832014-10-11 18:27:30 +03001981 create_cursor(b, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001982
Giulio Camuffo954f1832014-10-11 18:27:30 +03001983 b->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001984
Giulio Camuffo954f1832014-10-11 18:27:30 +03001985 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001986 gl_renderer = weston_load_module("gl-renderer.so",
1987 "gl_renderer_interface");
1988 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001989 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001990 }
1991
Giulio Camuffo954f1832014-10-11 18:27:30 +03001992 if (!b->use_pixman) {
1993 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05001994 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001995 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05001996 gl_renderer->alpha_attribs,
1997 NULL,
1998 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001999 weston_log("Failed to initialize the GL renderer; "
2000 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002001 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002002 }
2003 }
2004
Giulio Camuffo954f1832014-10-11 18:27:30 +03002005 if (b->use_pixman) {
2006 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002007 weston_log("Failed to initialize pixman renderer\n");
2008 goto err_display;
2009 }
2010 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002011
Giulio Camuffo954f1832014-10-11 18:27:30 +03002012 b->base.destroy = wayland_destroy;
2013 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002014
Giulio Camuffo954f1832014-10-11 18:27:30 +03002015 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002016
Giulio Camuffo954f1832014-10-11 18:27:30 +03002017 fd = wl_display_get_fd(b->parent.wl_display);
2018 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002019 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002020 wayland_backend_handle_event, b);
2021 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002022 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002023
Giulio Camuffo954f1832014-10-11 18:27:30 +03002024 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002025
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002026 if (compositor->renderer->import_dmabuf) {
2027 if (linux_dmabuf_setup(compositor) < 0)
2028 weston_log("Error: initializing dmabuf "
2029 "support failed.\n");
2030 }
2031
Giulio Camuffo954f1832014-10-11 18:27:30 +03002032 compositor->backend = &b->base;
2033 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002034err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002035 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002036err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002037 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002038 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002039 return NULL;
2040}
2041
2042static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002043wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002044{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002046
Giulio Camuffo954f1832014-10-11 18:27:30 +03002047 if (b->theme)
2048 theme_destroy(b->theme);
2049 if (b->frame_device)
2050 cairo_device_destroy(b->frame_device);
2051 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002052
Giulio Camuffo954f1832014-10-11 18:27:30 +03002053 weston_compositor_shutdown(b->compositor);
2054 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002055}
2056
Giulio Camuffo954f1832014-10-11 18:27:30 +03002057WL_EXPORT int
2058backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002059 struct weston_config *config,
2060 struct weston_backend_config *config_base)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002061{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002062 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002063 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002064 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002065 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002066 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002067 const char *section_name, *display_name;
2068 char *name;
2069
2070 const struct weston_option wayland_options[] = {
2071 { WESTON_OPTION_INTEGER, "width", 0, &width },
2072 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002073 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002074 { WESTON_OPTION_STRING, "display", 0, &display_name },
2075 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2076 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002077 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002078 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002079 };
2080
2081 width = 0;
2082 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002083 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002084 display_name = NULL;
2085 use_pixman = 0;
2086 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002087 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002088 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002089 parse_options(wayland_options,
2090 ARRAY_LENGTH(wayland_options), argc, argv);
2091
Giulio Camuffo954f1832014-10-11 18:27:30 +03002092 b = wayland_backend_create(compositor, use_pixman, display_name,
2093 argc, argv, config);
2094 if (!b)
2095 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002096
Giulio Camuffo954f1832014-10-11 18:27:30 +03002097 if (sprawl || b->parent.fshell) {
2098 b->sprawl_across_outputs = 1;
2099 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002100
Giulio Camuffo954f1832014-10-11 18:27:30 +03002101 wl_list_for_each(poutput, &b->parent.output_list, link)
2102 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002103
Giulio Camuffo954f1832014-10-11 18:27:30 +03002104 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002105 }
2106
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002107 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002108 output = wayland_output_create(b, 0, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002109 NULL, 1, 0, 1);
2110 if (!output)
2111 goto err_outputs;
2112
Axel Davydd8b88d2013-11-17 21:34:16 +01002113 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002114 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002115 }
2116
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002117 section = NULL;
2118 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002119 while (weston_config_next_section(config, &section, &section_name)) {
2120 if (!section_name || strcmp(section_name, "output") != 0)
2121 continue;
2122 weston_config_section_get_string(section, "name", &name, NULL);
2123 if (name == NULL)
2124 continue;
2125
2126 if (name[0] != 'W' || name[1] != 'L') {
2127 free(name);
2128 continue;
2129 }
2130 free(name);
2131
Giulio Camuffo954f1832014-10-11 18:27:30 +03002132 output = wayland_output_create_for_config(b, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002133 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002134 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002135 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002136 if (wayland_output_set_windowed(output))
2137 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002138
2139 x += output->base.width;
2140 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002141 }
2142
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002143 if (!width)
2144 width = 1024;
2145 if (!height)
2146 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002147 if (!scale)
2148 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002149 while (count > 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002150 output = wayland_output_create(b, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002151 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002152 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002153 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002154 if (wayland_output_set_windowed(output))
2155 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002156
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002157 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002158 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002159 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002160
Giulio Camuffo954f1832014-10-11 18:27:30 +03002161 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002162 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163 fullscreen_binding, b);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002164
Giulio Camuffo954f1832014-10-11 18:27:30 +03002165 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002166
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002167err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002168 wayland_backend_destroy(b);
2169 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002170}