blob: 0f3a1a7477cd50b38b56832122390d0753fce1cc [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;
Derek Foreman4bcc54d2015-11-06 15:56:06 -0600180 bool has_focus;
181
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400182 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500183 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100184};
185
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300186struct gl_renderer_interface *gl_renderer;
187
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500188static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500189wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
190{
191 cairo_surface_destroy(buffer->c_surface);
192 pixman_image_unref(buffer->pm_image);
193
194 wl_buffer_destroy(buffer->buffer);
195 munmap(buffer->data, buffer->size);
196
197 pixman_region32_fini(&buffer->damage);
198
199 wl_list_remove(&buffer->link);
200 wl_list_remove(&buffer->free_link);
201 free(buffer);
202}
203
204static void
205buffer_release(void *data, struct wl_buffer *buffer)
206{
207 struct wayland_shm_buffer *sb = data;
208
209 if (sb->output) {
210 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
211 } else {
212 wayland_shm_buffer_destroy(sb);
213 }
214}
215
216static const struct wl_buffer_listener buffer_listener = {
217 buffer_release
218};
219
220static struct wayland_shm_buffer *
221wayland_output_get_shm_buffer(struct wayland_output *output)
222{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300223 struct wayland_backend *b =
224 (struct wayland_backend *) output->base.compositor->backend;
225 struct wl_shm *shm = b->parent.shm;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500226 struct wayland_shm_buffer *sb;
227
228 struct wl_shm_pool *pool;
229 int width, height, stride;
230 int32_t fx, fy;
231 int fd;
232 unsigned char *data;
233
234 if (!wl_list_empty(&output->shm.free_buffers)) {
235 sb = container_of(output->shm.free_buffers.next,
236 struct wayland_shm_buffer, free_link);
237 wl_list_remove(&sb->free_link);
238 wl_list_init(&sb->free_link);
239
240 return sb;
241 }
242
243 if (output->frame) {
244 width = frame_width(output->frame);
245 height = frame_height(output->frame);
246 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500247 width = output->base.current_mode->width;
248 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500249 }
250
251 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
252
253 fd = os_create_anonymous_file(height * stride);
254 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000255 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500256 return NULL;
257 }
258
259 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
260 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000261 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500262 close(fd);
263 return NULL;
264 }
265
266 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000267 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200268 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000269 close(fd);
270 free(data);
271 return NULL;
272 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500273
274 sb->output = output;
275 wl_list_init(&sb->free_link);
276 wl_list_insert(&output->shm.buffers, &sb->link);
277
278 pixman_region32_init_rect(&sb->damage, 0, 0,
279 output->base.width, output->base.height);
280 sb->frame_damaged = 1;
281
282 sb->data = data;
283 sb->size = height * stride;
284
285 pool = wl_shm_create_pool(shm, fd, sb->size);
286
287 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
288 width, height,
289 stride,
290 WL_SHM_FORMAT_ARGB8888);
291 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
292 wl_shm_pool_destroy(pool);
293 close(fd);
294
295 memset(data, 0, sb->size);
296
297 sb->c_surface =
298 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
299 width, height, stride);
300
301 fx = 0;
302 fy = 0;
303 if (output->frame)
304 frame_interior(output->frame, &fx, &fy, 0, 0);
305 sb->pm_image =
306 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
307 (uint32_t *)(data + fy * stride) + fx,
308 stride);
309
310 return sb;
311}
312
313static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500314frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400317 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400318
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500319 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400320
321 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300322
323 /*
324 * This is the fallback case, where Presentation extension is not
325 * available from the parent compositor. We do not know the base for
326 * 'time', so we cannot feed it to finish_frame(). Do the only thing
327 * we can, and pretend finish_frame time is when we process this
328 * event.
329 */
330 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200331 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400332}
333
334static const struct wl_callback_listener frame_listener = {
335 frame_done
336};
337
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500338static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200339draw_initial_frame(struct wayland_output *output)
340{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500341 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200342
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500343 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200344
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500345 /* If we are rendering with GL, then orphan it so that it gets
346 * destroyed immediately */
347 if (output->gl.egl_window)
348 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500349
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500350 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500351 wl_surface_damage(output->parent.surface, 0, 0,
352 output->base.current_mode->width,
353 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200354}
355
356static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500357wayland_output_update_gl_border(struct wayland_output *output)
358{
359 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
360 cairo_t *cr;
361
362 if (!output->frame)
363 return;
364 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
365 return;
366
367 fwidth = frame_width(output->frame);
368 fheight = frame_height(output->frame);
369 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
370
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500371 if (!output->gl.border.top)
372 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500373 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
374 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500375 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500376 frame_repaint(output->frame, cr);
377 cairo_destroy(cr);
378 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
379 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500380 cairo_image_surface_get_stride(output->gl.border.top) / 4,
381 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382
383
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500384 if (!output->gl.border.left)
385 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
387 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389 cairo_translate(cr, 0, -iy);
390 frame_repaint(output->frame, cr);
391 cairo_destroy(cr);
392 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
393 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 cairo_image_surface_get_stride(output->gl.border.left) / 4,
395 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396
397
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398 if (!output->gl.border.right)
399 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500400 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
401 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500402 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500403 cairo_translate(cr, -(iwidth + ix), -iy);
404 frame_repaint(output->frame, cr);
405 cairo_destroy(cr);
406 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
407 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 cairo_image_surface_get_stride(output->gl.border.right) / 4,
409 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410
411
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500412 if (!output->gl.border.bottom)
413 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500414 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
415 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500416 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500417 cairo_translate(cr, 0, -(iy + iheight));
418 frame_repaint(output->frame, cr);
419 cairo_destroy(cr);
420 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
421 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500422 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
423 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500424}
425
426static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200427wayland_output_start_repaint_loop(struct weston_output *output_base)
428{
429 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300430 struct wayland_backend *wb =
431 (struct wayland_backend *)output->base.compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200432 struct wl_callback *callback;
433
434 /* If this is the initial frame, we need to attach a buffer so that
435 * the compositor can map the surface and include it in its render
436 * loop. If the surface doesn't end up in the render loop, the frame
437 * callback won't be invoked. The buffer is transparent and of the
438 * same size as the future real output buffer. */
439 if (output->parent.draw_initial_frame) {
440 output->parent.draw_initial_frame = 0;
441
442 draw_initial_frame(output);
443 }
444
445 callback = wl_surface_frame(output->parent.surface);
446 wl_callback_add_listener(callback, &frame_listener, output);
447 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300448 wl_display_flush(wb->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200449}
450
David Herrmann1edf44c2013-10-22 17:11:26 +0200451static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500452wayland_output_repaint_gl(struct weston_output *output_base,
453 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400454{
455 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400456 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400457 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600458
Kristian Høgsberg33418202011-08-16 23:01:28 -0400459 callback = wl_surface_frame(output->parent.surface);
460 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100461
Jason Ekstrand7744f712013-10-27 22:24:55 -0500462 wayland_output_update_gl_border(output);
463
Pekka Paalanenbc106382012-10-10 12:49:31 +0300464 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200465
466 pixman_region32_subtract(&ec->primary_plane.damage,
467 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200468 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100469}
470
Matt Roper361d2ad2011-08-29 13:52:23 -0700471static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500472wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
473{
474 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
475 cairo_t *cr;
476
477 if (!buffer->output->frame || !buffer->frame_damaged)
478 return;
479
480 cr = cairo_create(buffer->c_surface);
481
482 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
483 fwidth = frame_width(buffer->output->frame);
484 fheight = frame_height(buffer->output->frame);
485
486 /* Set the clip so we don't unnecisaraly damage the surface */
487 cairo_move_to(cr, ix, iy);
488 cairo_rel_line_to(cr, iwidth, 0);
489 cairo_rel_line_to(cr, 0, iheight);
490 cairo_rel_line_to(cr, -iwidth, 0);
491 cairo_line_to(cr, ix, iy);
492 cairo_line_to(cr, 0, iy);
493 cairo_line_to(cr, 0, fheight);
494 cairo_line_to(cr, fwidth, fheight);
495 cairo_line_to(cr, fwidth, 0);
496 cairo_line_to(cr, 0, 0);
497 cairo_line_to(cr, 0, iy);
498 cairo_close_path(cr);
499 cairo_clip(cr);
500
501 /* Draw using a pattern so that the final result gets clipped */
502 cairo_push_group(cr);
503 frame_repaint(buffer->output->frame, cr);
504 cairo_pop_group_to_source(cr);
505 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
506 cairo_paint(cr);
507
508 cairo_destroy(cr);
509}
510
511static void
512wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
513{
514 pixman_region32_t damage;
515 pixman_box32_t *rects;
516 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
517 int i, n;
518
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500519 pixman_region32_init(&damage);
520 weston_transformed_region(sb->output->base.width,
521 sb->output->base.height,
522 sb->output->base.transform,
523 sb->output->base.current_scale,
524 &sb->damage, &damage);
525
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500526 if (sb->output->frame) {
527 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
528 fwidth = frame_width(sb->output->frame);
529 fheight = frame_height(sb->output->frame);
530
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500531 pixman_region32_translate(&damage, ix, iy);
532
533 if (sb->frame_damaged) {
534 pixman_region32_union_rect(&damage, &damage,
535 0, 0, fwidth, iy);
536 pixman_region32_union_rect(&damage, &damage,
537 0, iy, ix, iheight);
538 pixman_region32_union_rect(&damage, &damage,
539 ix + iwidth, iy,
540 fwidth - (ix + iwidth), iheight);
541 pixman_region32_union_rect(&damage, &damage,
542 0, iy + iheight,
543 fwidth, fheight - (iy + iheight));
544 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500545 }
546
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500547 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500548 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
549 for (i = 0; i < n; ++i)
550 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
551 rects[i].y1, rects[i].x2 - rects[i].x1,
552 rects[i].y2 - rects[i].y1);
553
554 if (sb->output->frame)
555 pixman_region32_fini(&damage);
556}
557
558static int
559wayland_output_repaint_pixman(struct weston_output *output_base,
560 pixman_region32_t *damage)
561{
562 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300563 struct wayland_backend *b =
564 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500565 struct wl_callback *callback;
566 struct wayland_shm_buffer *sb;
567
568 if (output->frame) {
569 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
570 wl_list_for_each(sb, &output->shm.buffers, link)
571 sb->frame_damaged = 1;
572 }
573
574 wl_list_for_each(sb, &output->shm.buffers, link)
575 pixman_region32_union(&sb->damage, &sb->damage, damage);
576
577 sb = wayland_output_get_shm_buffer(output);
578
579 wayland_output_update_shm_border(sb);
580 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300581 b->compositor->renderer->repaint_output(output_base, &sb->damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500582
583 wayland_shm_buffer_attach(sb);
584
585 callback = wl_surface_frame(output->parent.surface);
586 wl_callback_add_listener(callback, &frame_listener, output);
587 wl_surface_commit(output->parent.surface);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300588 wl_display_flush(b->parent.wl_display);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500589
590 pixman_region32_fini(&sb->damage);
591 pixman_region32_init(&sb->damage);
592 sb->frame_damaged = 0;
593
Giulio Camuffo954f1832014-10-11 18:27:30 +0300594 pixman_region32_subtract(&b->compositor->primary_plane.damage,
595 &b->compositor->primary_plane.damage, damage);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500596 return 0;
597}
598
599static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500600wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700601{
602 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 struct wayland_backend *b =
604 (struct wayland_backend *) output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -0700605
Giulio Camuffo954f1832014-10-11 18:27:30 +0300606 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500607 pixman_renderer_output_destroy(output_base);
608 } else {
609 gl_renderer->output_destroy(output_base);
610 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100611
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500612 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500613 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500614 if (output->parent.shell_surface)
615 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500616
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600617 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500618 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600619
620 cairo_surface_destroy(output->gl.border.top);
621 cairo_surface_destroy(output->gl.border.left);
622 cairo_surface_destroy(output->gl.border.right);
623 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500624
625 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700626 free(output);
627
628 return;
629}
630
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300631static const struct wl_shell_surface_listener shell_surface_listener;
632
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200633static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500634wayland_output_init_gl_renderer(struct wayland_output *output)
635{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500636 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500637
638 if (output->frame) {
639 fwidth = frame_width(output->frame);
640 fheight = frame_height(output->frame);
641 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500642 fwidth = output->base.current_mode->width;
643 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500644 }
645
646 output->gl.egl_window =
647 wl_egl_window_create(output->parent.surface,
648 fwidth, fheight);
649 if (!output->gl.egl_window) {
650 weston_log("failure to create wl_egl_window\n");
651 return -1;
652 }
653
654 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000655 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100656 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000657 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500658 NULL,
659 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500660 goto cleanup_window;
661
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500662 return 0;
663
664cleanup_window:
665 wl_egl_window_destroy(output->gl.egl_window);
666 return -1;
667}
668
669static int
670wayland_output_init_pixman_renderer(struct wayland_output *output)
671{
672 return pixman_renderer_output_create(&output->base);
673}
674
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600675static void
676wayland_output_resize_surface(struct wayland_output *output)
677{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300678 struct wayland_backend *b =
679 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600680 struct wayland_shm_buffer *buffer, *next;
681 int32_t ix, iy, iwidth, iheight;
682 int32_t width, height;
683 struct wl_region *region;
684
685 width = output->base.current_mode->width;
686 height = output->base.current_mode->height;
687
688 if (output->frame) {
689 frame_resize_inside(output->frame, width, height);
690
691 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300692 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600693 wl_region_add(region, ix, iy, iwidth, iheight);
694 wl_surface_set_input_region(output->parent.surface, region);
695 wl_region_destroy(region);
696
697 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300698 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600699 wl_region_add(region, ix, iy, iwidth, iheight);
700 wl_surface_set_opaque_region(output->parent.surface, region);
701 wl_region_destroy(region);
702
703 width = frame_width(output->frame);
704 height = frame_height(output->frame);
705 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300706 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600707 wl_region_add(region, 0, 0, width, height);
708 wl_surface_set_input_region(output->parent.surface, region);
709 wl_region_destroy(region);
710
Giulio Camuffo954f1832014-10-11 18:27:30 +0300711 region = wl_compositor_create_region(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600712 wl_region_add(region, 0, 0, width, height);
713 wl_surface_set_opaque_region(output->parent.surface, region);
714 wl_region_destroy(region);
715 }
716
717 if (output->gl.egl_window) {
718 wl_egl_window_resize(output->gl.egl_window,
719 width, height, 0, 0);
720
721 /* These will need to be re-created due to the resize */
722 gl_renderer->output_set_border(&output->base,
723 GL_RENDERER_BORDER_TOP,
724 0, 0, 0, NULL);
725 cairo_surface_destroy(output->gl.border.top);
726 output->gl.border.top = NULL;
727 gl_renderer->output_set_border(&output->base,
728 GL_RENDERER_BORDER_LEFT,
729 0, 0, 0, NULL);
730 cairo_surface_destroy(output->gl.border.left);
731 output->gl.border.left = NULL;
732 gl_renderer->output_set_border(&output->base,
733 GL_RENDERER_BORDER_RIGHT,
734 0, 0, 0, NULL);
735 cairo_surface_destroy(output->gl.border.right);
736 output->gl.border.right = NULL;
737 gl_renderer->output_set_border(&output->base,
738 GL_RENDERER_BORDER_BOTTOM,
739 0, 0, 0, NULL);
740 cairo_surface_destroy(output->gl.border.bottom);
741 output->gl.border.bottom = NULL;
742 }
743
744 /* Throw away any remaining SHM buffers */
Derek Foremanf53beb82015-05-29 16:32:52 -0500745 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600746 wayland_shm_buffer_destroy(buffer);
747 /* These will get thrown away when they get released */
748 wl_list_for_each(buffer, &output->shm.buffers, link)
749 buffer->output = NULL;
750}
751
752static int
753wayland_output_set_windowed(struct wayland_output *output)
754{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300755 struct wayland_backend *b =
756 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600757 int tlen;
758 char *title;
759
760 if (output->frame)
761 return 0;
762
763 if (output->name) {
764 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
765 title = malloc(tlen + 1);
766 if (!title)
767 return -1;
768
769 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
770 } else {
771 title = strdup(WINDOW_TITLE);
772 }
773
Giulio Camuffo954f1832014-10-11 18:27:30 +0300774 if (!b->theme) {
775 b->theme = theme_create();
776 if (!b->theme) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600777 free(title);
778 return -1;
779 }
780 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300781 output->frame = frame_create(b->theme, 100, 100,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600782 FRAME_BUTTON_CLOSE, title);
783 free(title);
784 if (!output->frame)
785 return -1;
786
787 if (output->keyboard_count)
788 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
789
790 wayland_output_resize_surface(output);
791
792 wl_shell_surface_set_toplevel(output->parent.shell_surface);
793
794 return 0;
795}
796
797static void
798wayland_output_set_fullscreen(struct wayland_output *output,
799 enum wl_shell_surface_fullscreen_method method,
800 uint32_t framerate, struct wl_output *target)
801{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300802 struct wayland_backend *b =
803 (struct wayland_backend *)output->base.compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500804
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600805 if (output->frame) {
806 frame_destroy(output->frame);
807 output->frame = NULL;
808 }
809
810 wayland_output_resize_surface(output);
811
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500812 if (output->parent.shell_surface) {
813 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
814 method, framerate, target);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300815 } else if (b->parent.fshell) {
816 _wl_fullscreen_shell_present_surface(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500817 output->parent.surface,
818 method, target);
819 }
820}
821
822static struct weston_mode *
823wayland_output_choose_mode(struct wayland_output *output,
824 struct weston_mode *ref_mode)
825{
826 struct weston_mode *mode;
827
828 /* First look for an exact match */
829 wl_list_for_each(mode, &output->base.mode_list, link)
830 if (mode->width == ref_mode->width &&
831 mode->height == ref_mode->height &&
832 mode->refresh == ref_mode->refresh)
833 return mode;
834
835 /* If we can't find an exact match, ignore refresh and try again */
836 wl_list_for_each(mode, &output->base.mode_list, link)
837 if (mode->width == ref_mode->width &&
838 mode->height == ref_mode->height)
839 return mode;
840
841 /* Yeah, we failed */
842 return NULL;
843}
844
845enum mode_status {
846 MODE_STATUS_UNKNOWN,
847 MODE_STATUS_SUCCESS,
848 MODE_STATUS_FAIL,
849 MODE_STATUS_CANCEL,
850};
851
852static void
853mode_feedback_successful(void *data,
854 struct _wl_fullscreen_shell_mode_feedback *fb)
855{
856 enum mode_status *value = data;
857
858 printf("Mode switch successful\n");
859
860 *value = MODE_STATUS_SUCCESS;
861}
862
863static void
864mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
865{
866 enum mode_status *value = data;
867
868 printf("Mode switch failed\n");
869
870 *value = MODE_STATUS_FAIL;
871}
872
873static void
874mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
875{
876 enum mode_status *value = data;
877
878 printf("Mode switch cancelled\n");
879
880 *value = MODE_STATUS_CANCEL;
881}
882
883struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
884 mode_feedback_successful,
885 mode_feedback_failed,
886 mode_feedback_cancelled,
887};
888
889static int
890wayland_output_switch_mode(struct weston_output *output_base,
891 struct weston_mode *mode)
892{
893 struct wayland_output *output = (struct wayland_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300894 struct wayland_backend *b;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500895 struct wl_surface *old_surface;
896 struct weston_mode *old_mode;
897 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
898 enum mode_status mode_status;
899 int ret = 0;
900
901 if (output_base == NULL) {
902 weston_log("output is NULL.\n");
903 return -1;
904 }
905
906 if (mode == NULL) {
907 weston_log("mode is NULL.\n");
908 return -1;
909 }
910
Giulio Camuffo954f1832014-10-11 18:27:30 +0300911 b = (struct wayland_backend *)output_base->compositor->backend;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500912
Giulio Camuffo954f1832014-10-11 18:27:30 +0300913 if (output->parent.shell_surface || !b->parent.fshell)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500914 return -1;
915
916 mode = wayland_output_choose_mode(output, mode);
917 if (mode == NULL)
918 return -1;
919
920 if (output->base.current_mode == mode)
921 return 0;
922
923 old_mode = output->base.current_mode;
924 old_surface = output->parent.surface;
925 output->base.current_mode = mode;
926 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300927 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500928 wl_surface_set_user_data(output->parent.surface, output);
929
930 /* Blow the old buffers because we changed size/surfaces */
931 wayland_output_resize_surface(output);
932
933 mode_feedback =
Giulio Camuffo954f1832014-10-11 18:27:30 +0300934 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500935 output->parent.surface,
936 output->parent.output,
937 mode->refresh);
938 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
939 &mode_feedback_listener,
940 &mode_status);
941
942 /* This should kick-start things again */
943 output->parent.draw_initial_frame = 1;
944 wayland_output_start_repaint_loop(&output->base);
945
946 mode_status = MODE_STATUS_UNKNOWN;
947 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300948 ret = wl_display_dispatch(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500949
950 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
951
952 if (mode_status == MODE_STATUS_FAIL) {
953 output->base.current_mode = old_mode;
954 wl_surface_destroy(output->parent.surface);
955 output->parent.surface = old_surface;
956 wayland_output_resize_surface(output);
957
958 return -1;
959 }
960
961 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
962 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
963
Giulio Camuffo954f1832014-10-11 18:27:30 +0300964 if (b->use_pixman) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500965 pixman_renderer_output_destroy(output_base);
966 if (wayland_output_init_pixman_renderer(output) < 0)
967 goto err_output;
968 } else {
969 gl_renderer->output_destroy(output_base);
970 wl_egl_window_destroy(output->gl.egl_window);
971 if (wayland_output_init_gl_renderer(output) < 0)
972 goto err_output;
973 }
974 wl_surface_destroy(old_surface);
975
976 weston_output_schedule_repaint(&output->base);
977
978 return 0;
979
980err_output:
981 /* XXX */
982 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600983}
984
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500985static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300986wayland_output_create(struct wayland_backend *b, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600987 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500988 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100989{
990 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500991 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600992
993 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
994 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100995
Peter Huttererf3d62272013-08-08 11:57:05 +1000996 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100997 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500998 return NULL;
999
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001000 output->name = name ? strdup(name) : NULL;
1001 output->base.make = "waywayland";
1002 output->base.model = "none";
1003
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001004 output_width = width * scale;
1005 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001006
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007 output->parent.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001008 wl_compositor_create_surface(b->parent.compositor);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001009 if (!output->parent.surface)
1010 goto err_name;
1011 wl_surface_set_user_data(output->parent.surface, output);
1012
1013 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001014
Giulio Camuffo954f1832014-10-11 18:27:30 +03001015 if (b->parent.shell) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001016 output->parent.shell_surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001017 wl_shell_get_shell_surface(b->parent.shell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001018 output->parent.surface);
1019 if (!output->parent.shell_surface)
1020 goto err_surface;
1021 wl_shell_surface_add_listener(output->parent.shell_surface,
1022 &shell_surface_listener, output);
1023 }
1024
Giulio Camuffo954f1832014-10-11 18:27:30 +03001025 if (fullscreen && b->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001026 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1027 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001028 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001029 if (!width)
1030 output_width = output->parent.configure_width;
1031 if (!height)
1032 output_height = output->parent.configure_height;
1033 }
1034
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001035 output->mode.flags =
1036 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001037 output->mode.width = output_width;
1038 output->mode.height = output_height;
1039 output->mode.refresh = 60000;
1040 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001041 wl_list_init(&output->base.mode_list);
1042 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001043 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001044
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001045 wl_list_init(&output->shm.buffers);
1046 wl_list_init(&output->shm.free_buffers);
1047
Giulio Camuffo954f1832014-10-11 18:27:30 +03001048 weston_output_init(&output->base, b->compositor, x, y, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001049 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001050
Giulio Camuffo954f1832014-10-11 18:27:30 +03001051 if (b->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001052 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001053 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001054 output->base.repaint = wayland_output_repaint_pixman;
1055 } else {
1056 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001057 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001058 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001059 }
1060
Jonas Ådahle5a12252013-04-05 23:07:11 +02001061 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001062 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001063 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001064 output->base.set_backlight = NULL;
1065 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001066 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001067
Giulio Camuffo954f1832014-10-11 18:27:30 +03001068 weston_compositor_add_output(b->compositor, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001069
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001070 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001071
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001072err_output:
1073 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001074 if (output->parent.shell_surface)
1075 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001076err_surface:
1077 wl_surface_destroy(output->parent.surface);
1078err_name:
1079 free(output->name);
1080
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001081 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001082 free(output);
1083
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001084 return NULL;
1085}
1086
1087static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001088wayland_output_create_for_config(struct wayland_backend *b,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001089 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001090 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001091 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001092{
1093 struct wayland_output *output;
1094 char *mode, *t, *name, *str;
1095 int width, height, scale;
1096 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001097 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001098
1099 weston_config_section_get_string(config_section, "name", &name, NULL);
1100 if (name) {
1101 slen = strlen(name);
1102 slen += strlen(WINDOW_TITLE " - ");
1103 str = malloc(slen + 1);
1104 if (str)
1105 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1106 free(name);
1107 name = str;
1108 }
1109 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001110 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001111
1112 weston_config_section_get_string(config_section,
1113 "mode", &mode, "1024x600");
1114 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1115 weston_log("Invalid mode \"%s\" for output %s\n",
1116 mode, name);
1117 width = 1024;
1118 height = 640;
1119 }
1120 free(mode);
1121
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001122 if (option_width)
1123 width = option_width;
1124 if (option_height)
1125 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001126
1127 weston_config_section_get_int(config_section, "scale", &scale, 1);
1128
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001129 if (option_scale)
1130 scale = option_scale;
1131
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001132 weston_config_section_get_string(config_section,
1133 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001134 if (weston_parse_transform(t, &transform) < 0)
1135 weston_log("Invalid transform \"%s\" for output %s\n",
1136 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001137 free(t);
1138
Giulio Camuffo954f1832014-10-11 18:27:30 +03001139 output = wayland_output_create(b, x, y, width, height, name, 0,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001140 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001141 free(name);
1142
1143 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001144}
1145
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001146static struct wayland_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001147wayland_output_create_for_parent_output(struct wayland_backend *b,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001148 struct wayland_parent_output *poutput)
1149{
1150 struct wayland_output *output;
1151 struct weston_mode *mode;
1152 int32_t x;
1153
1154 if (poutput->current_mode) {
1155 mode = poutput->current_mode;
1156 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001157 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001158 } else if (!wl_list_empty(&poutput->mode_list)) {
1159 mode = container_of(poutput->mode_list.next,
1160 struct weston_mode, link);
1161 } else {
Chris Michael90eea272015-09-29 17:03:20 +03001162 weston_log("No valid modes found. Skipping output\n");
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001163 return NULL;
1164 }
1165
Giulio Camuffo954f1832014-10-11 18:27:30 +03001166 if (!wl_list_empty(&b->compositor->output_list)) {
1167 output = container_of(b->compositor->output_list.prev,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001168 struct wayland_output, base.link);
1169 x = output->base.x + output->base.current_mode->width;
1170 } else {
1171 x = 0;
1172 }
1173
Giulio Camuffo954f1832014-10-11 18:27:30 +03001174 output = wayland_output_create(b, x, 0, mode->width, mode->height,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001175 NULL, 0,
1176 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1177 if (!output)
1178 return NULL;
1179
1180 output->parent.output = poutput->global;
1181
1182 output->base.make = poutput->physical.make;
1183 output->base.model = poutput->physical.model;
1184 wl_list_init(&output->base.mode_list);
1185 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1186 wl_list_init(&poutput->mode_list);
1187
1188 wayland_output_set_fullscreen(output,
1189 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1190 mode->refresh, poutput->global);
1191
1192 if (output->parent.shell_surface) {
1193 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1194 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1195 mode->refresh, poutput->global);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001196 } else if (b->parent.fshell) {
1197 _wl_fullscreen_shell_present_surface(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001198 output->parent.surface,
1199 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1200 poutput->global);
1201 _wl_fullscreen_shell_mode_feedback_destroy(
Giulio Camuffo954f1832014-10-11 18:27:30 +03001202 _wl_fullscreen_shell_present_surface_for_mode(b->parent.fshell,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001203 output->parent.surface,
1204 poutput->global,
1205 mode->refresh));
1206 }
1207
1208 return output;
1209}
1210
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001211static void
1212shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1213 uint32_t serial)
1214{
1215 wl_shell_surface_pong(shell_surface, serial);
1216}
1217
1218static void
1219shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1220 uint32_t edges, int32_t width, int32_t height)
1221{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001222 struct wayland_output *output = data;
1223
1224 output->parent.configure_width = width;
1225 output->parent.configure_height = height;
1226
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001227 /* FIXME: implement resizing */
1228}
1229
1230static void
1231shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1232{
1233}
1234
1235static const struct wl_shell_surface_listener shell_surface_listener = {
1236 shell_surface_ping,
1237 shell_surface_configure,
1238 shell_surface_popup_done
1239};
1240
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001241/* Events received from the wayland-server this compositor is client of: */
1242
Jason Ekstrand7744f712013-10-27 22:24:55 -05001243/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001244static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001245input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001246{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001247
Jason Ekstrand7744f712013-10-27 22:24:55 -05001248 struct wl_buffer *buffer;
1249 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001250
Giulio Camuffo954f1832014-10-11 18:27:30 +03001251 if (!input->backend->cursor)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001252 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001253
Giulio Camuffo954f1832014-10-11 18:27:30 +03001254 image = input->backend->cursor->images[0];
Jason Ekstrand7744f712013-10-27 22:24:55 -05001255 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001256 if (!buffer)
1257 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001258
1259 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1260 input->parent.cursor.surface,
1261 image->hotspot_x, image->hotspot_y);
1262
1263 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1264 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1265 image->width, image->height);
1266 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001267}
1268
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001269static void
Daniel Stone37816df2012-05-16 18:45:18 +01001270input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1271 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001272 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001273{
1274 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001275 int32_t fx, fy;
1276 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001277
Daniel Stone50692802012-06-22 13:21:41 +01001278 /* XXX: If we get a modifier event immediately before the focus,
1279 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001280 input->enter_serial = serial;
1281 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001282
1283 if (input->output->frame) {
1284 location = frame_pointer_enter(input->output->frame, input,
1285 wl_fixed_to_int(x),
1286 wl_fixed_to_int(y));
1287 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1288 x -= wl_fixed_from_int(fx);
1289 y -= wl_fixed_from_int(fy);
1290
1291 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1292 weston_output_schedule_repaint(&input->output->base);
1293 } else {
1294 location = THEME_LOCATION_CLIENT_AREA;
1295 }
1296
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001297 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001298
1299 if (location == THEME_LOCATION_CLIENT_AREA) {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001300 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001301 notify_pointer_focus(&input->base, &input->output->base, x, y);
1302 wl_pointer_set_cursor(input->parent.pointer,
1303 input->enter_serial, NULL, 0, 0);
1304 } else {
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001305 input->has_focus = false;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001306 notify_pointer_focus(&input->base, NULL, 0, 0);
1307 input_set_cursor(input);
1308 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001309}
1310
1311static void
Daniel Stone37816df2012-05-16 18:45:18 +01001312input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1313 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001314{
1315 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001316
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001317 if (!input->output)
1318 return;
1319
Jason Ekstrand7744f712013-10-27 22:24:55 -05001320 if (input->output->frame) {
1321 frame_pointer_leave(input->output->frame, input);
1322
1323 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1324 weston_output_schedule_repaint(&input->output->base);
1325 }
1326
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001327 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001328 input->output = NULL;
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001329 input->has_focus = false;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001330}
1331
1332static void
Daniel Stone37816df2012-05-16 18:45:18 +01001333input_handle_motion(void *data, struct wl_pointer *pointer,
1334 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1335{
1336 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001337 int32_t fx, fy;
1338 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001339
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001340 if (!input->output)
1341 return;
1342
Jason Ekstrand7744f712013-10-27 22:24:55 -05001343 if (input->output->frame) {
1344 location = frame_pointer_motion(input->output->frame, input,
1345 wl_fixed_to_int(x),
1346 wl_fixed_to_int(y));
1347 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1348 x -= wl_fixed_from_int(fx);
1349 y -= wl_fixed_from_int(fy);
1350
1351 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1352 weston_output_schedule_repaint(&input->output->base);
1353 } else {
1354 location = THEME_LOCATION_CLIENT_AREA;
1355 }
1356
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001357 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001358
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001359 if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001360 input_set_cursor(input);
1361 notify_pointer_focus(&input->base, NULL, 0, 0);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001362 input->has_focus = false;
1363 } else if (!input->has_focus &&
1364 location == THEME_LOCATION_CLIENT_AREA) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001365 wl_pointer_set_cursor(input->parent.pointer,
1366 input->enter_serial, NULL, 0, 0);
1367 notify_pointer_focus(&input->base, &input->output->base, x, y);
Derek Foreman4bcc54d2015-11-06 15:56:06 -06001368 input->has_focus = true;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001369 }
1370
1371 if (location == THEME_LOCATION_CLIENT_AREA)
1372 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001373}
1374
1375static void
1376input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001377 uint32_t serial, uint32_t time, uint32_t button,
1378 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001379{
1380 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001381 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001382 enum frame_button_state fstate;
1383 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001384
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001385 if (!input->output)
1386 return;
1387
Jason Ekstrand7744f712013-10-27 22:24:55 -05001388 if (input->output->frame) {
1389 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1390 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1391
1392 location = frame_pointer_button(input->output->frame, input,
1393 button, fstate);
1394
1395 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1396
1397 wl_shell_surface_move(input->output->parent.shell_surface,
1398 input->parent.seat, serial);
1399 frame_status_clear(input->output->frame,
1400 FRAME_STATUS_MOVE);
1401 return;
1402 }
1403
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001404 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1405 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001406 input->output = NULL;
1407 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001408
Giulio Camuffo954f1832014-10-11 18:27:30 +03001409 if (wl_list_empty(&input->backend->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001410 weston_compositor_exit(input->backend->compositor);
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001411
1412 return;
1413 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001414
1415 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1416 weston_output_schedule_repaint(&input->output->base);
1417 } else {
1418 location = THEME_LOCATION_CLIENT_AREA;
1419 }
1420
1421 if (location == THEME_LOCATION_CLIENT_AREA)
1422 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001423}
1424
1425static void
1426input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001427 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001428{
1429 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001430
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001431 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001432}
1433
1434static const struct wl_pointer_listener pointer_listener = {
1435 input_handle_pointer_enter,
1436 input_handle_pointer_leave,
1437 input_handle_motion,
1438 input_handle_button,
1439 input_handle_axis,
1440};
1441
1442static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001443input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1444 int fd, uint32_t size)
1445{
1446 struct wayland_input *input = data;
1447 struct xkb_keymap *keymap;
1448 char *map_str;
1449
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001450 if (!data) {
1451 close(fd);
1452 return;
1453 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001454
1455 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1456 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1457 if (map_str == MAP_FAILED) {
1458 weston_log("mmap failed: %m\n");
1459 goto error;
1460 }
1461
Giulio Camuffo954f1832014-10-11 18:27:30 +03001462 keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context,
Ran Benita2e1968f2014-08-19 23:59:51 +03001463 map_str,
1464 XKB_KEYMAP_FORMAT_TEXT_V1,
1465 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001466 munmap(map_str, size);
1467
1468 if (!keymap) {
1469 weston_log("failed to compile keymap\n");
1470 goto error;
1471 }
1472
1473 input->keyboard_state_update = STATE_UPDATE_NONE;
1474 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1475 weston_log("No keymap provided; falling back to defalt\n");
1476 keymap = NULL;
1477 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1478 } else {
1479 weston_log("Invalid keymap\n");
1480 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001481 }
1482
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001483 close(fd);
1484
Derek Foreman1281a362015-07-31 16:55:32 -05001485 if (weston_seat_get_keyboard(&input->base))
Rui Matos0c194ce2013-10-10 19:44:21 +02001486 weston_seat_update_keymap(&input->base, keymap);
1487 else
1488 weston_seat_init_keyboard(&input->base, keymap);
1489
Ran Benitac9c74152014-08-19 23:59:52 +03001490 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001491
1492 return;
1493
1494error:
1495 wl_keyboard_release(input->parent.keyboard);
1496 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001497}
1498
1499static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001500input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001501 struct wl_keyboard *keyboard,
1502 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001503 struct wl_surface *surface,
1504 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001505{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001506 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001507 struct wayland_output *focus;
1508
1509 focus = input->keyboard_focus;
1510 if (focus) {
1511 /* This shouldn't happen */
1512 focus->keyboard_count--;
1513 if (!focus->keyboard_count && focus->frame)
1514 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1515 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1516 weston_output_schedule_repaint(&focus->base);
1517 }
1518
1519 input->keyboard_focus = wl_surface_get_user_data(surface);
1520 input->keyboard_focus->keyboard_count++;
1521
1522 focus = input->keyboard_focus;
1523 if (focus->frame) {
1524 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1525 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1526 weston_output_schedule_repaint(&focus->base);
1527 }
1528
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001529
Daniel Stone50692802012-06-22 13:21:41 +01001530 /* XXX: If we get a modifier event immediately before the focus,
1531 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001532 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001533 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001534}
1535
1536static void
1537input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001538 struct wl_keyboard *keyboard,
1539 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001540 struct wl_surface *surface)
1541{
1542 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001543 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001544
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001545 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001546
1547 focus = input->keyboard_focus;
1548 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001549 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001550
1551 focus->keyboard_count--;
1552 if (!focus->keyboard_count && focus->frame) {
1553 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1554 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1555 weston_output_schedule_repaint(&focus->base);
1556 }
1557
1558 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001559}
1560
Daniel Stone37816df2012-05-16 18:45:18 +01001561static void
1562input_handle_key(void *data, struct wl_keyboard *keyboard,
1563 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1564{
1565 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001566
Daniel Stone50692802012-06-22 13:21:41 +01001567 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001568 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001569 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001570 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001571 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001572}
1573
Daniel Stone351eb612012-05-31 15:27:47 -04001574static void
Derek Foreman1281a362015-07-31 16:55:32 -05001575input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001576 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001577 uint32_t mods_latched, uint32_t mods_locked,
1578 uint32_t group)
1579{
Derek Foreman1281a362015-07-31 16:55:32 -05001580 struct weston_keyboard *keyboard;
Daniel Stone50692802012-06-22 13:21:41 +01001581 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001582 struct wayland_backend *b = input->backend;
Daniel Stone50692802012-06-22 13:21:41 +01001583 uint32_t serial_out;
1584
1585 /* If we get a key event followed by a modifier event with the
1586 * same serial number, then we try to preserve those semantics by
1587 * reusing the same serial number on the way out too. */
1588 if (serial_in == input->key_serial)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001589 serial_out = wl_display_get_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001590 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591 serial_out = wl_display_next_serial(b->compositor->wl_display);
Daniel Stone50692802012-06-22 13:21:41 +01001592
Derek Foreman1281a362015-07-31 16:55:32 -05001593 keyboard = weston_seat_get_keyboard(&input->base);
1594 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001595 mods_depressed, mods_latched,
1596 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001597 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001598}
1599
Jonny Lamb497994a2014-08-12 14:58:26 +02001600static void
1601input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1602 int32_t rate, int32_t delay)
1603{
1604 struct wayland_input *input = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001605 struct wayland_backend *b = input->backend;
Jonny Lamb497994a2014-08-12 14:58:26 +02001606
Giulio Camuffo954f1832014-10-11 18:27:30 +03001607 b->compositor->kb_repeat_rate = rate;
1608 b->compositor->kb_repeat_delay = delay;
Jonny Lamb497994a2014-08-12 14:58:26 +02001609}
1610
Daniel Stone37816df2012-05-16 18:45:18 +01001611static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001612 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001613 input_handle_keyboard_enter,
1614 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001615 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001616 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001617 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001618};
1619
1620static void
Daniel Stone37816df2012-05-16 18:45:18 +01001621input_handle_capabilities(void *data, struct wl_seat *seat,
1622 enum wl_seat_capability caps)
1623{
1624 struct wayland_input *input = data;
1625
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001626 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1627 input->parent.pointer = wl_seat_get_pointer(seat);
1628 wl_pointer_set_user_data(input->parent.pointer, input);
1629 wl_pointer_add_listener(input->parent.pointer,
1630 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001631 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001632 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1633 wl_pointer_destroy(input->parent.pointer);
1634 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001635 }
1636
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001637 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1638 input->parent.keyboard = wl_seat_get_keyboard(seat);
1639 wl_keyboard_set_user_data(input->parent.keyboard, input);
1640 wl_keyboard_add_listener(input->parent.keyboard,
1641 &keyboard_listener, input);
1642 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1643 wl_keyboard_destroy(input->parent.keyboard);
1644 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001645 }
1646}
1647
Jonny Lamb497994a2014-08-12 14:58:26 +02001648static void
1649input_handle_name(void *data, struct wl_seat *seat,
1650 const char *name)
1651{
1652}
1653
Daniel Stone37816df2012-05-16 18:45:18 +01001654static const struct wl_seat_listener seat_listener = {
1655 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001656 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001657};
1658
1659static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001660display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001661{
1662 struct wayland_input *input;
1663
Peter Huttererf3d62272013-08-08 11:57:05 +10001664 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001665 if (input == NULL)
1666 return;
1667
Giulio Camuffo954f1832014-10-11 18:27:30 +03001668 weston_seat_init(&input->base, b->compositor, "default");
1669 input->backend = b;
1670 input->parent.seat = wl_registry_bind(b->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001671 &wl_seat_interface, MIN(version, 4));
Giulio Camuffo954f1832014-10-11 18:27:30 +03001672 wl_list_insert(b->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001673
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001674 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1675 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001676
1677 input->parent.cursor.surface =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001678 wl_compositor_create_surface(b->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001679}
1680
1681static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001682wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1683 int32_t x, int32_t y,
1684 int32_t physical_width, int32_t physical_height,
1685 int32_t subpixel, const char *make,
1686 const char *model, int32_t transform)
1687{
1688 struct wayland_parent_output *output = data;
1689
1690 output->x = x;
1691 output->y = y;
1692 output->physical.width = physical_width;
1693 output->physical.height = physical_height;
1694 output->physical.subpixel = subpixel;
1695
1696 free(output->physical.make);
1697 output->physical.make = strdup(make);
1698 free(output->physical.model);
1699 output->physical.model = strdup(model);
1700
1701 output->transform = transform;
1702}
1703
1704static struct weston_mode *
1705find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1706{
1707 struct weston_mode *mode;
1708
1709 wl_list_for_each(mode, list, link) {
1710 if (mode->width == width && mode->height == height &&
1711 mode->refresh == refresh)
1712 return mode;
1713 }
1714
1715 mode = zalloc(sizeof *mode);
1716 if (!mode)
1717 return NULL;
1718
1719 mode->width = width;
1720 mode->height = height;
1721 mode->refresh = refresh;
1722 wl_list_insert(list, &mode->link);
1723
1724 return mode;
1725}
1726
1727static void
1728wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1729 uint32_t flags, int32_t width, int32_t height,
1730 int32_t refresh)
1731{
1732 struct wayland_parent_output *output = data;
1733 struct weston_mode *mode;
1734
1735 if (output->output) {
1736 mode = find_mode(&output->output->base.mode_list,
1737 width, height, refresh);
1738 if (!mode)
1739 return;
1740 mode->flags = flags;
1741 /* Do a mode-switch on current mode change? */
1742 } else {
1743 mode = find_mode(&output->mode_list, width, height, refresh);
1744 if (!mode)
1745 return;
1746 mode->flags = flags;
1747 if (flags & WL_OUTPUT_MODE_CURRENT)
1748 output->current_mode = mode;
1749 if (flags & WL_OUTPUT_MODE_PREFERRED)
1750 output->preferred_mode = mode;
1751 }
1752}
1753
1754static const struct wl_output_listener output_listener = {
1755 wayland_parent_output_geometry,
1756 wayland_parent_output_mode
1757};
1758
1759static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001760wayland_backend_register_output(struct wayland_backend *b, uint32_t id)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001761{
1762 struct wayland_parent_output *output;
1763
1764 output = zalloc(sizeof *output);
1765 if (!output)
1766 return;
1767
1768 output->id = id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001769 output->global = wl_registry_bind(b->parent.registry, id,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001770 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001771 if (!output->global) {
1772 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001773 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001774 }
1775
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001776 wl_output_add_listener(output->global, &output_listener, output);
1777
1778 output->scale = 0;
1779 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1780 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1781 wl_list_init(&output->mode_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001782 wl_list_insert(&b->parent.output_list, &output->link);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001783
Giulio Camuffo954f1832014-10-11 18:27:30 +03001784 if (b->sprawl_across_outputs) {
1785 wl_display_roundtrip(b->parent.wl_display);
1786 wayland_output_create_for_parent_output(b, output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001787 }
1788}
1789
1790static void
1791wayland_parent_output_destroy(struct wayland_parent_output *output)
1792{
1793 struct weston_mode *mode, *next;
1794
1795 if (output->output)
1796 wayland_output_destroy(&output->output->base);
1797
1798 wl_output_destroy(output->global);
1799 free(output->physical.make);
1800 free(output->physical.model);
1801
1802 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1803 wl_list_remove(&mode->link);
1804 free(mode);
1805 }
1806}
1807
1808static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001809registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1810 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001811{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001812 struct wayland_backend *b = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001813
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001814 if (strcmp(interface, "wl_compositor") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001815 b->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001816 wl_registry_bind(registry, name,
1817 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001818 } else if (strcmp(interface, "wl_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001819 b->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001820 wl_registry_bind(registry, name,
1821 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001822 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001823 b->parent.fshell =
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001824 wl_registry_bind(registry, name,
1825 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001826 } else if (strcmp(interface, "wl_seat") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001827 display_add_seat(b, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001828 } else if (strcmp(interface, "wl_output") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001829 wayland_backend_register_output(b, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001830 } else if (strcmp(interface, "wl_shm") == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001831 b->parent.shm =
Jonas Ådahle5a12252013-04-05 23:07:11 +02001832 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001833 }
1834}
1835
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001836static void
1837registry_handle_global_remove(void *data, struct wl_registry *registry,
1838 uint32_t name)
1839{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001840 struct wayland_backend *b = data;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001841 struct wayland_parent_output *output;
1842
Giulio Camuffo954f1832014-10-11 18:27:30 +03001843 wl_list_for_each(output, &b->parent.output_list, link)
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001844 if (output->id == name)
1845 wayland_parent_output_destroy(output);
1846}
1847
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001848static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001849 registry_handle_global,
1850 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001851};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001852
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001853static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001854wayland_backend_handle_event(int fd, uint32_t mask, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001855{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001856 struct wayland_backend *b = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001857 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001858
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001859 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
Giulio Camuffo459137b2014-10-11 23:56:24 +03001860 weston_compositor_exit(b->compositor);
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001861 return 0;
1862 }
1863
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001864 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001865 count = wl_display_dispatch(b->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001866 if (mask & WL_EVENT_WRITABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001867 wl_display_flush(b->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001868
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001869 if (mask == 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001870 count = wl_display_dispatch_pending(b->parent.wl_display);
1871 wl_display_flush(b->parent.wl_display);
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001872 }
1873
1874 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001875}
1876
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001877static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001878wayland_restore(struct weston_compositor *ec)
1879{
1880}
1881
1882static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001883wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001884{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001885 struct wayland_backend *b = (struct wayland_backend *) ec->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001886
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001887 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001888
Giulio Camuffo954f1832014-10-11 18:27:30 +03001889 if (b->parent.shm)
1890 wl_shm_destroy(b->parent.shm);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001891
Giulio Camuffo954f1832014-10-11 18:27:30 +03001892 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001893}
1894
Jason Ekstrand7744f712013-10-27 22:24:55 -05001895static const char *left_ptrs[] = {
1896 "left_ptr",
1897 "default",
1898 "top_left_arrow",
1899 "left-arrow"
1900};
1901
1902static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001903create_cursor(struct wayland_backend *b, struct weston_config *config)
Jason Ekstrand7744f712013-10-27 22:24:55 -05001904{
1905 struct weston_config_section *s;
1906 int size;
1907 char *theme = NULL;
1908 unsigned int i;
1909
1910 s = weston_config_get_section(config, "shell", NULL, NULL);
1911 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1912 weston_config_section_get_int(s, "cursor-size", &size, 32);
1913
Giulio Camuffo954f1832014-10-11 18:27:30 +03001914 b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
1915 if (!b->cursor_theme) {
Hardening842a36a2014-03-18 14:12:50 +01001916 fprintf(stderr, "could not load cursor theme\n");
1917 return;
1918 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001919
U. Artie Eoffff755002014-01-17 12:36:58 -08001920 free(theme);
1921
Giulio Camuffo954f1832014-10-11 18:27:30 +03001922 b->cursor = NULL;
1923 for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1924 b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
Jason Ekstrand7744f712013-10-27 22:24:55 -05001925 left_ptrs[i]);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001926 if (!b->cursor) {
Jason Ekstrand7744f712013-10-27 22:24:55 -05001927 fprintf(stderr, "could not load left cursor\n");
1928 return;
1929 }
1930}
1931
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001932static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05001933fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
1934 uint32_t key, void *data)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001935{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001936 struct wayland_backend *b = data;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001937 struct wayland_input *input = NULL;
1938
Giulio Camuffo954f1832014-10-11 18:27:30 +03001939 wl_list_for_each(input, &b->input_list, link)
Derek Foreman8ae2db52015-07-15 13:00:36 -05001940 if (&input->base == keyboard->seat)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001941 break;
1942
1943 if (!input || !input->output)
1944 return;
1945
1946 if (input->output->frame)
1947 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1948 else
1949 wayland_output_set_windowed(input->output);
1950
1951 weston_output_schedule_repaint(&input->output->base);
1952}
1953
Giulio Camuffo954f1832014-10-11 18:27:30 +03001954static struct wayland_backend *
1955wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
1956 const char *display_name, int *argc, char *argv[],
1957 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001958{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001959 struct wayland_backend *b;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001960 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001961 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001962
Giulio Camuffo954f1832014-10-11 18:27:30 +03001963 b = zalloc(sizeof *b);
1964 if (b == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001965 return NULL;
1966
Giulio Camuffo954f1832014-10-11 18:27:30 +03001967 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001968 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001969 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001970
Giulio Camuffo954f1832014-10-11 18:27:30 +03001971 b->parent.wl_display = wl_display_connect(display_name);
1972 if (b->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001973 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001974 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001975 }
1976
Giulio Camuffo954f1832014-10-11 18:27:30 +03001977 wl_list_init(&b->parent.output_list);
1978 wl_list_init(&b->input_list);
1979 b->parent.registry = wl_display_get_registry(b->parent.wl_display);
1980 wl_registry_add_listener(b->parent.registry, &registry_listener, b);
1981 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001982
Giulio Camuffo954f1832014-10-11 18:27:30 +03001983 create_cursor(b, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001984
Giulio Camuffo954f1832014-10-11 18:27:30 +03001985 b->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001986
Giulio Camuffo954f1832014-10-11 18:27:30 +03001987 if (!b->use_pixman) {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001988 gl_renderer = weston_load_module("gl-renderer.so",
1989 "gl_renderer_interface");
1990 if (!gl_renderer)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001991 b->use_pixman = 1;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001992 }
1993
Giulio Camuffo954f1832014-10-11 18:27:30 +03001994 if (!b->use_pixman) {
1995 if (gl_renderer->create(compositor,
Derek Foremane76f1852015-05-15 12:12:39 -05001996 EGL_PLATFORM_WAYLAND_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001997 b->parent.wl_display,
Derek Foremane76f1852015-05-15 12:12:39 -05001998 gl_renderer->alpha_attribs,
1999 NULL,
2000 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002001 weston_log("Failed to initialize the GL renderer; "
2002 "falling back to pixman.\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002003 b->use_pixman = 1;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002004 }
2005 }
2006
Giulio Camuffo954f1832014-10-11 18:27:30 +03002007 if (b->use_pixman) {
2008 if (pixman_renderer_init(compositor) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002009 weston_log("Failed to initialize pixman renderer\n");
2010 goto err_display;
2011 }
2012 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002013
Giulio Camuffo954f1832014-10-11 18:27:30 +03002014 b->base.destroy = wayland_destroy;
2015 b->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002016
Giulio Camuffo954f1832014-10-11 18:27:30 +03002017 loop = wl_display_get_event_loop(compositor->wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002018
Giulio Camuffo954f1832014-10-11 18:27:30 +03002019 fd = wl_display_get_fd(b->parent.wl_display);
2020 b->parent.wl_source =
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002021 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002022 wayland_backend_handle_event, b);
2023 if (b->parent.wl_source == NULL)
Dawid Gajownik82d49252015-07-31 00:02:28 -03002024 goto err_display;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002025
Giulio Camuffo954f1832014-10-11 18:27:30 +03002026 wl_event_source_check(b->parent.wl_source);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002027
Emmanuel Gil Peyrotc59f18e2015-09-25 11:58:40 +02002028 if (compositor->renderer->import_dmabuf) {
2029 if (linux_dmabuf_setup(compositor) < 0)
2030 weston_log("Error: initializing dmabuf "
2031 "support failed.\n");
2032 }
2033
Giulio Camuffo954f1832014-10-11 18:27:30 +03002034 compositor->backend = &b->base;
2035 return b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002036err_display:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002037 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002038err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002039 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002040 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002041 return NULL;
2042}
2043
2044static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045wayland_backend_destroy(struct wayland_backend *b)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002046{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002047 wl_display_disconnect(b->parent.wl_display);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002048
Giulio Camuffo954f1832014-10-11 18:27:30 +03002049 if (b->theme)
2050 theme_destroy(b->theme);
2051 if (b->frame_device)
2052 cairo_device_destroy(b->frame_device);
2053 wl_cursor_theme_destroy(b->cursor_theme);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002054
Giulio Camuffo954f1832014-10-11 18:27:30 +03002055 weston_compositor_shutdown(b->compositor);
2056 free(b);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002057}
2058
Giulio Camuffo954f1832014-10-11 18:27:30 +03002059WL_EXPORT int
2060backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03002061 struct weston_config *config,
2062 struct weston_backend_config *config_base)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002063{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002064 struct wayland_backend *b;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002065 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002066 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002067 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002068 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002069 const char *section_name, *display_name;
2070 char *name;
2071
2072 const struct weston_option wayland_options[] = {
2073 { WESTON_OPTION_INTEGER, "width", 0, &width },
2074 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002075 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002076 { WESTON_OPTION_STRING, "display", 0, &display_name },
2077 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2078 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002079 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002080 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002081 };
2082
2083 width = 0;
2084 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002085 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002086 display_name = NULL;
2087 use_pixman = 0;
2088 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002089 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002090 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002091 parse_options(wayland_options,
2092 ARRAY_LENGTH(wayland_options), argc, argv);
2093
Giulio Camuffo954f1832014-10-11 18:27:30 +03002094 b = wayland_backend_create(compositor, use_pixman, display_name,
2095 argc, argv, config);
2096 if (!b)
2097 return -1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002098
Giulio Camuffo954f1832014-10-11 18:27:30 +03002099 if (sprawl || b->parent.fshell) {
2100 b->sprawl_across_outputs = 1;
2101 wl_display_roundtrip(b->parent.wl_display);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002102
Giulio Camuffo954f1832014-10-11 18:27:30 +03002103 wl_list_for_each(poutput, &b->parent.output_list, link)
2104 wayland_output_create_for_parent_output(b, poutput);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002105
Giulio Camuffo954f1832014-10-11 18:27:30 +03002106 return 0;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002107 }
2108
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002109 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002110 output = wayland_output_create(b, 0, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002111 NULL, 1, 0, 1);
2112 if (!output)
2113 goto err_outputs;
2114
Axel Davydd8b88d2013-11-17 21:34:16 +01002115 wayland_output_set_fullscreen(output, 0, 0, NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002116 return 0;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002117 }
2118
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002119 section = NULL;
2120 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002121 while (weston_config_next_section(config, &section, &section_name)) {
2122 if (!section_name || strcmp(section_name, "output") != 0)
2123 continue;
2124 weston_config_section_get_string(section, "name", &name, NULL);
2125 if (name == NULL)
2126 continue;
2127
2128 if (name[0] != 'W' || name[1] != 'L') {
2129 free(name);
2130 continue;
2131 }
2132 free(name);
2133
Giulio Camuffo954f1832014-10-11 18:27:30 +03002134 output = wayland_output_create_for_config(b, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002135 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002137 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002138 if (wayland_output_set_windowed(output))
2139 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002140
2141 x += output->base.width;
2142 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002143 }
2144
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002145 if (!width)
2146 width = 1024;
2147 if (!height)
2148 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002149 if (!scale)
2150 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002151 while (count > 0) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002152 output = wayland_output_create(b, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002153 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002154 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002155 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002156 if (wayland_output_set_windowed(output))
2157 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002158
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002159 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002160 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002161 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002162
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163 weston_compositor_add_key_binding(compositor, KEY_F,
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002164 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002165 fullscreen_binding, b);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002166
Giulio Camuffo954f1832014-10-11 18:27:30 +03002167 return 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002168
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002169err_outputs:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002170 wayland_backend_destroy(b);
2171 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002172}