blob: aaf205ba486ec10d94b032079cfcf894a7f7f4d1 [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 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010022 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025
26#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010032#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060033#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010034
Benjamin Franzkebe014562011-02-18 17:04:24 +010035#include <wayland-client.h>
36#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050037#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010040#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050041#include "pixman-renderer.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050042#include "../shared/image-loader.h"
Jonas Ådahle5a12252013-04-05 23:07:11 +020043#include "../shared/os-compatibility.h"
Jason Ekstrand7744f712013-10-27 22:24:55 -050044#include "../shared/cairo-util.h"
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050045#include "fullscreen-shell-client-protocol.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020046#include "presentation_timing-server-protocol.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047
Jason Ekstrand48ce4212013-10-27 22:25:02 -050048#define WINDOW_TITLE "Weston Compositor"
49
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010050struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050051 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052
53 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040054 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040055 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056 struct wl_compositor *compositor;
57 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050058 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020059 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010060
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050061 struct wl_list output_list;
62
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063 struct wl_event_source *wl_source;
64 uint32_t event_mask;
65 } parent;
66
Jason Ekstrandff2fd462013-10-27 22:24:58 -050067 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050068 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050069
Jason Ekstrand7744f712013-10-27 22:24:55 -050070 struct theme *theme;
71 cairo_device_t *frame_device;
72 struct wl_cursor_theme *cursor_theme;
73 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050074
Jason Ekstrand06ced802013-11-07 20:13:29 -060075 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076};
77
78struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050079 struct weston_output base;
80
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050082 int draw_initial_frame;
83 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050084
85 struct wl_output *output;
86 uint32_t global_id;
87
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050088 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060089 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010090 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050091
Jason Ekstrand7744f712013-10-27 22:24:55 -050092 int keyboard_count;
93
Jason Ekstrand5ea04802013-11-07 20:13:33 -060094 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050095 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050096
Jason Ekstrand7744f712013-10-27 22:24:55 -050097 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050098 struct wl_egl_window *egl_window;
99 struct {
100 cairo_surface_t *top;
101 cairo_surface_t *left;
102 cairo_surface_t *right;
103 cairo_surface_t *bottom;
104 } border;
105 } gl;
106
107 struct {
108 struct wl_list buffers;
109 struct wl_list free_buffers;
110 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500111
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500112 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500113 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100114};
115
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500116struct wayland_parent_output {
117 struct wayland_output *output;
118 struct wl_list link;
119
120 struct wl_output *global;
121 uint32_t id;
122
123 struct {
124 char *make;
125 char *model;
126 int32_t width, height;
127 uint32_t subpixel;
128 } physical;
129
130 int32_t x, y;
131 uint32_t transform;
132 uint32_t scale;
133
134 struct wl_list mode_list;
135 struct weston_mode *preferred_mode;
136 struct weston_mode *current_mode;
137};
138
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500139struct wayland_shm_buffer {
140 struct wayland_output *output;
141 struct wl_list link;
142 struct wl_list free_link;
143
144 struct wl_buffer *buffer;
145 void *data;
146 size_t size;
147 pixman_region32_t damage;
148 int frame_damaged;
149
150 pixman_image_t *pm_image;
151 cairo_surface_t *c_surface;
152};
153
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100154struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400155 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100156 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100157 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500158
159 struct {
160 struct wl_seat *seat;
161 struct wl_pointer *pointer;
162 struct wl_keyboard *keyboard;
163 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500164
165 struct {
166 struct wl_surface *surface;
167 int32_t hx, hy;
168 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500169 } parent;
170
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500171 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100172 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400173 uint32_t enter_serial;
174 int focus;
175 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500176 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100177};
178
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300179struct gl_renderer_interface *gl_renderer;
180
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500181static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500182wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
183{
184 cairo_surface_destroy(buffer->c_surface);
185 pixman_image_unref(buffer->pm_image);
186
187 wl_buffer_destroy(buffer->buffer);
188 munmap(buffer->data, buffer->size);
189
190 pixman_region32_fini(&buffer->damage);
191
192 wl_list_remove(&buffer->link);
193 wl_list_remove(&buffer->free_link);
194 free(buffer);
195}
196
197static void
198buffer_release(void *data, struct wl_buffer *buffer)
199{
200 struct wayland_shm_buffer *sb = data;
201
202 if (sb->output) {
203 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
204 } else {
205 wayland_shm_buffer_destroy(sb);
206 }
207}
208
209static const struct wl_buffer_listener buffer_listener = {
210 buffer_release
211};
212
213static struct wayland_shm_buffer *
214wayland_output_get_shm_buffer(struct wayland_output *output)
215{
216 struct wayland_compositor *c =
217 (struct wayland_compositor *) output->base.compositor;
218 struct wl_shm *shm = c->parent.shm;
219 struct wayland_shm_buffer *sb;
220
221 struct wl_shm_pool *pool;
222 int width, height, stride;
223 int32_t fx, fy;
224 int fd;
225 unsigned char *data;
226
227 if (!wl_list_empty(&output->shm.free_buffers)) {
228 sb = container_of(output->shm.free_buffers.next,
229 struct wayland_shm_buffer, free_link);
230 wl_list_remove(&sb->free_link);
231 wl_list_init(&sb->free_link);
232
233 return sb;
234 }
235
236 if (output->frame) {
237 width = frame_width(output->frame);
238 height = frame_height(output->frame);
239 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500240 width = output->base.current_mode->width;
241 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500242 }
243
244 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
245
246 fd = os_create_anonymous_file(height * stride);
247 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000248 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500249 return NULL;
250 }
251
252 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
253 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000254 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500255 close(fd);
256 return NULL;
257 }
258
259 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000260 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200261 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000262 close(fd);
263 free(data);
264 return NULL;
265 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500266
267 sb->output = output;
268 wl_list_init(&sb->free_link);
269 wl_list_insert(&output->shm.buffers, &sb->link);
270
271 pixman_region32_init_rect(&sb->damage, 0, 0,
272 output->base.width, output->base.height);
273 sb->frame_damaged = 1;
274
275 sb->data = data;
276 sb->size = height * stride;
277
278 pool = wl_shm_create_pool(shm, fd, sb->size);
279
280 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
281 width, height,
282 stride,
283 WL_SHM_FORMAT_ARGB8888);
284 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
285 wl_shm_pool_destroy(pool);
286 close(fd);
287
288 memset(data, 0, sb->size);
289
290 sb->c_surface =
291 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
292 width, height, stride);
293
294 fx = 0;
295 fy = 0;
296 if (output->frame)
297 frame_interior(output->frame, &fx, &fy, 0, 0);
298 sb->pm_image =
299 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
300 (uint32_t *)(data + fy * stride) + fx,
301 stride);
302
303 return sb;
304}
305
306static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500307frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400308{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500309 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400310 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400311
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500312 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400313
314 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300315
316 /*
317 * This is the fallback case, where Presentation extension is not
318 * available from the parent compositor. We do not know the base for
319 * 'time', so we cannot feed it to finish_frame(). Do the only thing
320 * we can, and pretend finish_frame time is when we process this
321 * event.
322 */
323 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200324 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400325}
326
327static const struct wl_callback_listener frame_listener = {
328 frame_done
329};
330
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500331static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200332draw_initial_frame(struct wayland_output *output)
333{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500334 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200335
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500336 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200337
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500338 /* If we are rendering with GL, then orphan it so that it gets
339 * destroyed immediately */
340 if (output->gl.egl_window)
341 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500342
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500343 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500344 wl_surface_damage(output->parent.surface, 0, 0,
345 output->base.current_mode->width,
346 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200347}
348
349static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500350wayland_output_update_gl_border(struct wayland_output *output)
351{
352 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
353 cairo_t *cr;
354
355 if (!output->frame)
356 return;
357 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
358 return;
359
360 fwidth = frame_width(output->frame);
361 fheight = frame_height(output->frame);
362 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
363
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500364 if (!output->gl.border.top)
365 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500366 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
367 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500369 frame_repaint(output->frame, cr);
370 cairo_destroy(cr);
371 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
372 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500373 cairo_image_surface_get_stride(output->gl.border.top) / 4,
374 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500375
376
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500377 if (!output->gl.border.left)
378 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500379 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
380 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500381 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382 cairo_translate(cr, 0, -iy);
383 frame_repaint(output->frame, cr);
384 cairo_destroy(cr);
385 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
386 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500387 cairo_image_surface_get_stride(output->gl.border.left) / 4,
388 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389
390
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500391 if (!output->gl.border.right)
392 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500393 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
394 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 cairo_translate(cr, -(iwidth + ix), -iy);
397 frame_repaint(output->frame, cr);
398 cairo_destroy(cr);
399 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
400 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500401 cairo_image_surface_get_stride(output->gl.border.right) / 4,
402 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500403
404
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500405 if (!output->gl.border.bottom)
406 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500407 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
408 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410 cairo_translate(cr, 0, -(iy + iheight));
411 frame_repaint(output->frame, cr);
412 cairo_destroy(cr);
413 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
414 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500415 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
416 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500417}
418
419static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200420wayland_output_start_repaint_loop(struct weston_output *output_base)
421{
422 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500423 struct wayland_compositor *wc =
424 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200425 struct wl_callback *callback;
426
427 /* If this is the initial frame, we need to attach a buffer so that
428 * the compositor can map the surface and include it in its render
429 * loop. If the surface doesn't end up in the render loop, the frame
430 * callback won't be invoked. The buffer is transparent and of the
431 * same size as the future real output buffer. */
432 if (output->parent.draw_initial_frame) {
433 output->parent.draw_initial_frame = 0;
434
435 draw_initial_frame(output);
436 }
437
438 callback = wl_surface_frame(output->parent.surface);
439 wl_callback_add_listener(callback, &frame_listener, output);
440 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500441 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200442}
443
David Herrmann1edf44c2013-10-22 17:11:26 +0200444static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445wayland_output_repaint_gl(struct weston_output *output_base,
446 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400447{
448 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400449 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400450 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600451
Kristian Høgsberg33418202011-08-16 23:01:28 -0400452 callback = wl_surface_frame(output->parent.surface);
453 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100454
Jason Ekstrand7744f712013-10-27 22:24:55 -0500455 wayland_output_update_gl_border(output);
456
Pekka Paalanenbc106382012-10-10 12:49:31 +0300457 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200458
459 pixman_region32_subtract(&ec->primary_plane.damage,
460 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200461 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100462}
463
Matt Roper361d2ad2011-08-29 13:52:23 -0700464static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500465wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
466{
467 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
468 cairo_t *cr;
469
470 if (!buffer->output->frame || !buffer->frame_damaged)
471 return;
472
473 cr = cairo_create(buffer->c_surface);
474
475 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
476 fwidth = frame_width(buffer->output->frame);
477 fheight = frame_height(buffer->output->frame);
478
479 /* Set the clip so we don't unnecisaraly damage the surface */
480 cairo_move_to(cr, ix, iy);
481 cairo_rel_line_to(cr, iwidth, 0);
482 cairo_rel_line_to(cr, 0, iheight);
483 cairo_rel_line_to(cr, -iwidth, 0);
484 cairo_line_to(cr, ix, iy);
485 cairo_line_to(cr, 0, iy);
486 cairo_line_to(cr, 0, fheight);
487 cairo_line_to(cr, fwidth, fheight);
488 cairo_line_to(cr, fwidth, 0);
489 cairo_line_to(cr, 0, 0);
490 cairo_line_to(cr, 0, iy);
491 cairo_close_path(cr);
492 cairo_clip(cr);
493
494 /* Draw using a pattern so that the final result gets clipped */
495 cairo_push_group(cr);
496 frame_repaint(buffer->output->frame, cr);
497 cairo_pop_group_to_source(cr);
498 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
499 cairo_paint(cr);
500
501 cairo_destroy(cr);
502}
503
504static void
505wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
506{
507 pixman_region32_t damage;
508 pixman_box32_t *rects;
509 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
510 int i, n;
511
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500512 pixman_region32_init(&damage);
513 weston_transformed_region(sb->output->base.width,
514 sb->output->base.height,
515 sb->output->base.transform,
516 sb->output->base.current_scale,
517 &sb->damage, &damage);
518
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500519 if (sb->output->frame) {
520 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
521 fwidth = frame_width(sb->output->frame);
522 fheight = frame_height(sb->output->frame);
523
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500524 pixman_region32_translate(&damage, ix, iy);
525
526 if (sb->frame_damaged) {
527 pixman_region32_union_rect(&damage, &damage,
528 0, 0, fwidth, iy);
529 pixman_region32_union_rect(&damage, &damage,
530 0, iy, ix, iheight);
531 pixman_region32_union_rect(&damage, &damage,
532 ix + iwidth, iy,
533 fwidth - (ix + iwidth), iheight);
534 pixman_region32_union_rect(&damage, &damage,
535 0, iy + iheight,
536 fwidth, fheight - (iy + iheight));
537 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500538 }
539
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500540 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500541 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
542 for (i = 0; i < n; ++i)
543 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
544 rects[i].y1, rects[i].x2 - rects[i].x1,
545 rects[i].y2 - rects[i].y1);
546
547 if (sb->output->frame)
548 pixman_region32_fini(&damage);
549}
550
551static int
552wayland_output_repaint_pixman(struct weston_output *output_base,
553 pixman_region32_t *damage)
554{
555 struct wayland_output *output = (struct wayland_output *) output_base;
556 struct wayland_compositor *c =
557 (struct wayland_compositor *)output->base.compositor;
558 struct wl_callback *callback;
559 struct wayland_shm_buffer *sb;
560
561 if (output->frame) {
562 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
563 wl_list_for_each(sb, &output->shm.buffers, link)
564 sb->frame_damaged = 1;
565 }
566
567 wl_list_for_each(sb, &output->shm.buffers, link)
568 pixman_region32_union(&sb->damage, &sb->damage, damage);
569
570 sb = wayland_output_get_shm_buffer(output);
571
572 wayland_output_update_shm_border(sb);
573 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
574 c->base.renderer->repaint_output(output_base, &sb->damage);
575
576 wayland_shm_buffer_attach(sb);
577
578 callback = wl_surface_frame(output->parent.surface);
579 wl_callback_add_listener(callback, &frame_listener, output);
580 wl_surface_commit(output->parent.surface);
581 wl_display_flush(c->parent.wl_display);
582
583 pixman_region32_fini(&sb->damage);
584 pixman_region32_init(&sb->damage);
585 sb->frame_damaged = 0;
586
587 pixman_region32_subtract(&c->base.primary_plane.damage,
588 &c->base.primary_plane.damage, damage);
589 return 0;
590}
591
592static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700594{
595 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500596 struct wayland_compositor *c =
597 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700598
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500599 if (c->use_pixman) {
600 pixman_renderer_output_destroy(output_base);
601 } else {
602 gl_renderer->output_destroy(output_base);
603 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100604
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500605 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500606 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500607 if (output->parent.shell_surface)
608 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500609
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600610 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500611 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600612
613 cairo_surface_destroy(output->gl.border.top);
614 cairo_surface_destroy(output->gl.border.left);
615 cairo_surface_destroy(output->gl.border.right);
616 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500617
618 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700619 free(output);
620
621 return;
622}
623
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300624static const struct wl_shell_surface_listener shell_surface_listener;
625
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200626static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500627wayland_output_init_gl_renderer(struct wayland_output *output)
628{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500629 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630
631 if (output->frame) {
632 fwidth = frame_width(output->frame);
633 fheight = frame_height(output->frame);
634 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500635 fwidth = output->base.current_mode->width;
636 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500637 }
638
639 output->gl.egl_window =
640 wl_egl_window_create(output->parent.surface,
641 fwidth, fheight);
642 if (!output->gl.egl_window) {
643 weston_log("failure to create wl_egl_window\n");
644 return -1;
645 }
646
647 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000648 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100649 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000650 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500651 NULL,
652 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500653 goto cleanup_window;
654
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500655 return 0;
656
657cleanup_window:
658 wl_egl_window_destroy(output->gl.egl_window);
659 return -1;
660}
661
662static int
663wayland_output_init_pixman_renderer(struct wayland_output *output)
664{
665 return pixman_renderer_output_create(&output->base);
666}
667
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600668static void
669wayland_output_resize_surface(struct wayland_output *output)
670{
671 struct wayland_compositor *c =
672 (struct wayland_compositor *)output->base.compositor;
673 struct wayland_shm_buffer *buffer, *next;
674 int32_t ix, iy, iwidth, iheight;
675 int32_t width, height;
676 struct wl_region *region;
677
678 width = output->base.current_mode->width;
679 height = output->base.current_mode->height;
680
681 if (output->frame) {
682 frame_resize_inside(output->frame, width, height);
683
684 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
685 region = wl_compositor_create_region(c->parent.compositor);
686 wl_region_add(region, ix, iy, iwidth, iheight);
687 wl_surface_set_input_region(output->parent.surface, region);
688 wl_region_destroy(region);
689
690 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
691 region = wl_compositor_create_region(c->parent.compositor);
692 wl_region_add(region, ix, iy, iwidth, iheight);
693 wl_surface_set_opaque_region(output->parent.surface, region);
694 wl_region_destroy(region);
695
696 width = frame_width(output->frame);
697 height = frame_height(output->frame);
698 } else {
699 region = wl_compositor_create_region(c->parent.compositor);
700 wl_region_add(region, 0, 0, width, height);
701 wl_surface_set_input_region(output->parent.surface, region);
702 wl_region_destroy(region);
703
704 region = wl_compositor_create_region(c->parent.compositor);
705 wl_region_add(region, 0, 0, width, height);
706 wl_surface_set_opaque_region(output->parent.surface, region);
707 wl_region_destroy(region);
708 }
709
710 if (output->gl.egl_window) {
711 wl_egl_window_resize(output->gl.egl_window,
712 width, height, 0, 0);
713
714 /* These will need to be re-created due to the resize */
715 gl_renderer->output_set_border(&output->base,
716 GL_RENDERER_BORDER_TOP,
717 0, 0, 0, NULL);
718 cairo_surface_destroy(output->gl.border.top);
719 output->gl.border.top = NULL;
720 gl_renderer->output_set_border(&output->base,
721 GL_RENDERER_BORDER_LEFT,
722 0, 0, 0, NULL);
723 cairo_surface_destroy(output->gl.border.left);
724 output->gl.border.left = NULL;
725 gl_renderer->output_set_border(&output->base,
726 GL_RENDERER_BORDER_RIGHT,
727 0, 0, 0, NULL);
728 cairo_surface_destroy(output->gl.border.right);
729 output->gl.border.right = NULL;
730 gl_renderer->output_set_border(&output->base,
731 GL_RENDERER_BORDER_BOTTOM,
732 0, 0, 0, NULL);
733 cairo_surface_destroy(output->gl.border.bottom);
734 output->gl.border.bottom = NULL;
735 }
736
737 /* Throw away any remaining SHM buffers */
738 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
739 wayland_shm_buffer_destroy(buffer);
740 /* These will get thrown away when they get released */
741 wl_list_for_each(buffer, &output->shm.buffers, link)
742 buffer->output = NULL;
743}
744
745static int
746wayland_output_set_windowed(struct wayland_output *output)
747{
748 struct wayland_compositor *c =
749 (struct wayland_compositor *)output->base.compositor;
750 int tlen;
751 char *title;
752
753 if (output->frame)
754 return 0;
755
756 if (output->name) {
757 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
758 title = malloc(tlen + 1);
759 if (!title)
760 return -1;
761
762 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
763 } else {
764 title = strdup(WINDOW_TITLE);
765 }
766
767 if (!c->theme) {
768 c->theme = theme_create();
769 if (!c->theme) {
770 free(title);
771 return -1;
772 }
773 }
774 output->frame = frame_create(c->theme, 100, 100,
775 FRAME_BUTTON_CLOSE, title);
776 free(title);
777 if (!output->frame)
778 return -1;
779
780 if (output->keyboard_count)
781 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
782
783 wayland_output_resize_surface(output);
784
785 wl_shell_surface_set_toplevel(output->parent.shell_surface);
786
787 return 0;
788}
789
790static void
791wayland_output_set_fullscreen(struct wayland_output *output,
792 enum wl_shell_surface_fullscreen_method method,
793 uint32_t framerate, struct wl_output *target)
794{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500795 struct wayland_compositor *c =
796 (struct wayland_compositor *)output->base.compositor;
797
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600798 if (output->frame) {
799 frame_destroy(output->frame);
800 output->frame = NULL;
801 }
802
803 wayland_output_resize_surface(output);
804
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500805 if (output->parent.shell_surface) {
806 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
807 method, framerate, target);
808 } else if (c->parent.fshell) {
809 _wl_fullscreen_shell_present_surface(c->parent.fshell,
810 output->parent.surface,
811 method, target);
812 }
813}
814
815static struct weston_mode *
816wayland_output_choose_mode(struct wayland_output *output,
817 struct weston_mode *ref_mode)
818{
819 struct weston_mode *mode;
820
821 /* First look for an exact match */
822 wl_list_for_each(mode, &output->base.mode_list, link)
823 if (mode->width == ref_mode->width &&
824 mode->height == ref_mode->height &&
825 mode->refresh == ref_mode->refresh)
826 return mode;
827
828 /* If we can't find an exact match, ignore refresh and try again */
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 return mode;
833
834 /* Yeah, we failed */
835 return NULL;
836}
837
838enum mode_status {
839 MODE_STATUS_UNKNOWN,
840 MODE_STATUS_SUCCESS,
841 MODE_STATUS_FAIL,
842 MODE_STATUS_CANCEL,
843};
844
845static void
846mode_feedback_successful(void *data,
847 struct _wl_fullscreen_shell_mode_feedback *fb)
848{
849 enum mode_status *value = data;
850
851 printf("Mode switch successful\n");
852
853 *value = MODE_STATUS_SUCCESS;
854}
855
856static void
857mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
858{
859 enum mode_status *value = data;
860
861 printf("Mode switch failed\n");
862
863 *value = MODE_STATUS_FAIL;
864}
865
866static void
867mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
868{
869 enum mode_status *value = data;
870
871 printf("Mode switch cancelled\n");
872
873 *value = MODE_STATUS_CANCEL;
874}
875
876struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
877 mode_feedback_successful,
878 mode_feedback_failed,
879 mode_feedback_cancelled,
880};
881
882static int
883wayland_output_switch_mode(struct weston_output *output_base,
884 struct weston_mode *mode)
885{
886 struct wayland_output *output = (struct wayland_output *) output_base;
887 struct wayland_compositor *c;
888 struct wl_surface *old_surface;
889 struct weston_mode *old_mode;
890 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
891 enum mode_status mode_status;
892 int ret = 0;
893
894 if (output_base == NULL) {
895 weston_log("output is NULL.\n");
896 return -1;
897 }
898
899 if (mode == NULL) {
900 weston_log("mode is NULL.\n");
901 return -1;
902 }
903
904 c = (struct wayland_compositor *)output_base->compositor;
905
906 if (output->parent.shell_surface || !c->parent.fshell)
907 return -1;
908
909 mode = wayland_output_choose_mode(output, mode);
910 if (mode == NULL)
911 return -1;
912
913 if (output->base.current_mode == mode)
914 return 0;
915
916 old_mode = output->base.current_mode;
917 old_surface = output->parent.surface;
918 output->base.current_mode = mode;
919 output->parent.surface =
920 wl_compositor_create_surface(c->parent.compositor);
921 wl_surface_set_user_data(output->parent.surface, output);
922
923 /* Blow the old buffers because we changed size/surfaces */
924 wayland_output_resize_surface(output);
925
926 mode_feedback =
927 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
928 output->parent.surface,
929 output->parent.output,
930 mode->refresh);
931 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
932 &mode_feedback_listener,
933 &mode_status);
934
935 /* This should kick-start things again */
936 output->parent.draw_initial_frame = 1;
937 wayland_output_start_repaint_loop(&output->base);
938
939 mode_status = MODE_STATUS_UNKNOWN;
940 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
941 ret = wl_display_dispatch(c->parent.wl_display);
942
943 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
944
945 if (mode_status == MODE_STATUS_FAIL) {
946 output->base.current_mode = old_mode;
947 wl_surface_destroy(output->parent.surface);
948 output->parent.surface = old_surface;
949 wayland_output_resize_surface(output);
950
951 return -1;
952 }
953
954 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
955 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
956
957 if (c->use_pixman) {
958 pixman_renderer_output_destroy(output_base);
959 if (wayland_output_init_pixman_renderer(output) < 0)
960 goto err_output;
961 } else {
962 gl_renderer->output_destroy(output_base);
963 wl_egl_window_destroy(output->gl.egl_window);
964 if (wayland_output_init_gl_renderer(output) < 0)
965 goto err_output;
966 }
967 wl_surface_destroy(old_surface);
968
969 weston_output_schedule_repaint(&output->base);
970
971 return 0;
972
973err_output:
974 /* XXX */
975 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600976}
977
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500978static struct wayland_output *
979wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600980 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500981 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100982{
983 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500984 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600985
986 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
987 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100988
Peter Huttererf3d62272013-08-08 11:57:05 +1000989 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100990 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500991 return NULL;
992
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600993 output->name = name ? strdup(name) : NULL;
994 output->base.make = "waywayland";
995 output->base.model = "none";
996
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500997 output_width = width * scale;
998 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100999
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001000 output->parent.surface =
1001 wl_compositor_create_surface(c->parent.compositor);
1002 if (!output->parent.surface)
1003 goto err_name;
1004 wl_surface_set_user_data(output->parent.surface, output);
1005
1006 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001008 if (c->parent.shell) {
1009 output->parent.shell_surface =
1010 wl_shell_get_shell_surface(c->parent.shell,
1011 output->parent.surface);
1012 if (!output->parent.shell_surface)
1013 goto err_surface;
1014 wl_shell_surface_add_listener(output->parent.shell_surface,
1015 &shell_surface_listener, output);
1016 }
1017
1018 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001019 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1020 0, 0, NULL);
1021 wl_display_roundtrip(c->parent.wl_display);
1022 if (!width)
1023 output_width = output->parent.configure_width;
1024 if (!height)
1025 output_height = output->parent.configure_height;
1026 }
1027
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001028 output->mode.flags =
1029 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001030 output->mode.width = output_width;
1031 output->mode.height = output_height;
1032 output->mode.refresh = 60000;
1033 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001034 wl_list_init(&output->base.mode_list);
1035 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001036 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001037
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001038 wl_list_init(&output->shm.buffers);
1039 wl_list_init(&output->shm.free_buffers);
1040
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001041 weston_output_init(&output->base, &c->base, x, y, width, height,
1042 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001043
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001044 if (c->use_pixman) {
1045 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001046 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001047 output->base.repaint = wayland_output_repaint_pixman;
1048 } else {
1049 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001050 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001051 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001052 }
1053
Jonas Ådahle5a12252013-04-05 23:07:11 +02001054 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001055 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001056 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001057 output->base.set_backlight = NULL;
1058 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001059 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001060
Giulio Camuffob1147152015-05-06 21:41:57 +03001061 weston_compositor_add_output(&c->base, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001062
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001063 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001064
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001065err_output:
1066 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001067 if (output->parent.shell_surface)
1068 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001069err_surface:
1070 wl_surface_destroy(output->parent.surface);
1071err_name:
1072 free(output->name);
1073
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001074 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001075 free(output);
1076
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001077 return NULL;
1078}
1079
1080static struct wayland_output *
1081wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001082 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001083 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001084 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001085{
1086 struct wayland_output *output;
1087 char *mode, *t, *name, *str;
1088 int width, height, scale;
1089 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001090 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001091
1092 weston_config_section_get_string(config_section, "name", &name, NULL);
1093 if (name) {
1094 slen = strlen(name);
1095 slen += strlen(WINDOW_TITLE " - ");
1096 str = malloc(slen + 1);
1097 if (str)
1098 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1099 free(name);
1100 name = str;
1101 }
1102 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001103 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001104
1105 weston_config_section_get_string(config_section,
1106 "mode", &mode, "1024x600");
1107 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1108 weston_log("Invalid mode \"%s\" for output %s\n",
1109 mode, name);
1110 width = 1024;
1111 height = 640;
1112 }
1113 free(mode);
1114
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001115 if (option_width)
1116 width = option_width;
1117 if (option_height)
1118 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001119
1120 weston_config_section_get_int(config_section, "scale", &scale, 1);
1121
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001122 if (option_scale)
1123 scale = option_scale;
1124
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001125 weston_config_section_get_string(config_section,
1126 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001127 if (weston_parse_transform(t, &transform) < 0)
1128 weston_log("Invalid transform \"%s\" for output %s\n",
1129 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001130 free(t);
1131
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001132 output = wayland_output_create(c, x, y, width, height, name, 0,
1133 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001134 free(name);
1135
1136 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001137}
1138
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001139static struct wayland_output *
1140wayland_output_create_for_parent_output(struct wayland_compositor *c,
1141 struct wayland_parent_output *poutput)
1142{
1143 struct wayland_output *output;
1144 struct weston_mode *mode;
1145 int32_t x;
1146
1147 if (poutput->current_mode) {
1148 mode = poutput->current_mode;
1149 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001150 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001151 } else if (!wl_list_empty(&poutput->mode_list)) {
1152 mode = container_of(poutput->mode_list.next,
1153 struct weston_mode, link);
1154 } else {
1155 weston_log("No valid modes found. Skipping output");
1156 return NULL;
1157 }
1158
1159 if (!wl_list_empty(&c->base.output_list)) {
1160 output = container_of(c->base.output_list.prev,
1161 struct wayland_output, base.link);
1162 x = output->base.x + output->base.current_mode->width;
1163 } else {
1164 x = 0;
1165 }
1166
1167 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1168 NULL, 0,
1169 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1170 if (!output)
1171 return NULL;
1172
1173 output->parent.output = poutput->global;
1174
1175 output->base.make = poutput->physical.make;
1176 output->base.model = poutput->physical.model;
1177 wl_list_init(&output->base.mode_list);
1178 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1179 wl_list_init(&poutput->mode_list);
1180
1181 wayland_output_set_fullscreen(output,
1182 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1183 mode->refresh, poutput->global);
1184
1185 if (output->parent.shell_surface) {
1186 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1187 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1188 mode->refresh, poutput->global);
1189 } else if (c->parent.fshell) {
1190 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1191 output->parent.surface,
1192 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1193 poutput->global);
1194 _wl_fullscreen_shell_mode_feedback_destroy(
1195 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1196 output->parent.surface,
1197 poutput->global,
1198 mode->refresh));
1199 }
1200
1201 return output;
1202}
1203
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001204static void
1205shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1206 uint32_t serial)
1207{
1208 wl_shell_surface_pong(shell_surface, serial);
1209}
1210
1211static void
1212shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1213 uint32_t edges, int32_t width, int32_t height)
1214{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001215 struct wayland_output *output = data;
1216
1217 output->parent.configure_width = width;
1218 output->parent.configure_height = height;
1219
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001220 /* FIXME: implement resizing */
1221}
1222
1223static void
1224shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1225{
1226}
1227
1228static const struct wl_shell_surface_listener shell_surface_listener = {
1229 shell_surface_ping,
1230 shell_surface_configure,
1231 shell_surface_popup_done
1232};
1233
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001234/* Events received from the wayland-server this compositor is client of: */
1235
Jason Ekstrand7744f712013-10-27 22:24:55 -05001236/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001237static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001238input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001239{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001240
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241 struct wl_buffer *buffer;
1242 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001243
Jason Ekstrand7744f712013-10-27 22:24:55 -05001244 if (!input->compositor->cursor)
1245 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001246
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247 image = input->compositor->cursor->images[0];
1248 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001249 if (!buffer)
1250 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001251
1252 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1253 input->parent.cursor.surface,
1254 image->hotspot_x, image->hotspot_y);
1255
1256 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1257 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1258 image->width, image->height);
1259 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001260}
1261
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001262static void
Daniel Stone37816df2012-05-16 18:45:18 +01001263input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1264 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001265 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001266{
1267 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001268 int32_t fx, fy;
1269 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001270
Daniel Stone50692802012-06-22 13:21:41 +01001271 /* XXX: If we get a modifier event immediately before the focus,
1272 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001273 input->enter_serial = serial;
1274 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001275
1276 if (input->output->frame) {
1277 location = frame_pointer_enter(input->output->frame, input,
1278 wl_fixed_to_int(x),
1279 wl_fixed_to_int(y));
1280 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1281 x -= wl_fixed_from_int(fx);
1282 y -= wl_fixed_from_int(fy);
1283
1284 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1285 weston_output_schedule_repaint(&input->output->base);
1286 } else {
1287 location = THEME_LOCATION_CLIENT_AREA;
1288 }
1289
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001290 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001291
1292 if (location == THEME_LOCATION_CLIENT_AREA) {
1293 input->focus = 1;
1294 notify_pointer_focus(&input->base, &input->output->base, x, y);
1295 wl_pointer_set_cursor(input->parent.pointer,
1296 input->enter_serial, NULL, 0, 0);
1297 } else {
1298 input->focus = 0;
1299 notify_pointer_focus(&input->base, NULL, 0, 0);
1300 input_set_cursor(input);
1301 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001302}
1303
1304static void
Daniel Stone37816df2012-05-16 18:45:18 +01001305input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1306 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001307{
1308 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001309
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001310 if (!input->output)
1311 return;
1312
Jason Ekstrand7744f712013-10-27 22:24:55 -05001313 if (input->output->frame) {
1314 frame_pointer_leave(input->output->frame, input);
1315
1316 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1317 weston_output_schedule_repaint(&input->output->base);
1318 }
1319
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001320 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001321 input->output = NULL;
1322 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001323}
1324
1325static void
Daniel Stone37816df2012-05-16 18:45:18 +01001326input_handle_motion(void *data, struct wl_pointer *pointer,
1327 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1328{
1329 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001330 int32_t fx, fy;
1331 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001332
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001333 if (!input->output)
1334 return;
1335
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336 if (input->output->frame) {
1337 location = frame_pointer_motion(input->output->frame, input,
1338 wl_fixed_to_int(x),
1339 wl_fixed_to_int(y));
1340 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1341 x -= wl_fixed_from_int(fx);
1342 y -= wl_fixed_from_int(fy);
1343
1344 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1345 weston_output_schedule_repaint(&input->output->base);
1346 } else {
1347 location = THEME_LOCATION_CLIENT_AREA;
1348 }
1349
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001350 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001351
1352 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1353 input_set_cursor(input);
1354 notify_pointer_focus(&input->base, NULL, 0, 0);
1355 input->focus = 0;
1356 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1357 wl_pointer_set_cursor(input->parent.pointer,
1358 input->enter_serial, NULL, 0, 0);
1359 notify_pointer_focus(&input->base, &input->output->base, x, y);
1360 input->focus = 1;
1361 }
1362
1363 if (location == THEME_LOCATION_CLIENT_AREA)
1364 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001365}
1366
1367static void
1368input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001369 uint32_t serial, uint32_t time, uint32_t button,
1370 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001371{
1372 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001373 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001374 enum frame_button_state fstate;
1375 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001376
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001377 if (!input->output)
1378 return;
1379
Jason Ekstrand7744f712013-10-27 22:24:55 -05001380 if (input->output->frame) {
1381 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1382 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1383
1384 location = frame_pointer_button(input->output->frame, input,
1385 button, fstate);
1386
1387 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1388
1389 wl_shell_surface_move(input->output->parent.shell_surface,
1390 input->parent.seat, serial);
1391 frame_status_clear(input->output->frame,
1392 FRAME_STATUS_MOVE);
1393 return;
1394 }
1395
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001396 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1397 wayland_output_destroy(&input->output->base);
1398 input->output = input->keyboard_focus = NULL;
1399
1400 if (wl_list_empty(&input->compositor->base.output_list))
1401 wl_display_terminate(input->compositor->base.wl_display);
1402
1403 return;
1404 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001405
1406 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1407 weston_output_schedule_repaint(&input->output->base);
1408 } else {
1409 location = THEME_LOCATION_CLIENT_AREA;
1410 }
1411
1412 if (location == THEME_LOCATION_CLIENT_AREA)
1413 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001414}
1415
1416static void
1417input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001418 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001419{
1420 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001421
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001422 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001423}
1424
1425static const struct wl_pointer_listener pointer_listener = {
1426 input_handle_pointer_enter,
1427 input_handle_pointer_leave,
1428 input_handle_motion,
1429 input_handle_button,
1430 input_handle_axis,
1431};
1432
1433static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001434input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1435 int fd, uint32_t size)
1436{
1437 struct wayland_input *input = data;
1438 struct xkb_keymap *keymap;
1439 char *map_str;
1440
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001441 if (!data) {
1442 close(fd);
1443 return;
1444 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001445
1446 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1447 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1448 if (map_str == MAP_FAILED) {
1449 weston_log("mmap failed: %m\n");
1450 goto error;
1451 }
1452
Ran Benita2e1968f2014-08-19 23:59:51 +03001453 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1454 map_str,
1455 XKB_KEYMAP_FORMAT_TEXT_V1,
1456 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001457 munmap(map_str, size);
1458
1459 if (!keymap) {
1460 weston_log("failed to compile keymap\n");
1461 goto error;
1462 }
1463
1464 input->keyboard_state_update = STATE_UPDATE_NONE;
1465 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1466 weston_log("No keymap provided; falling back to defalt\n");
1467 keymap = NULL;
1468 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1469 } else {
1470 weston_log("Invalid keymap\n");
1471 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001472 }
1473
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001474 close(fd);
1475
Rui Matos0c194ce2013-10-10 19:44:21 +02001476 if (input->base.keyboard)
1477 weston_seat_update_keymap(&input->base, keymap);
1478 else
1479 weston_seat_init_keyboard(&input->base, keymap);
1480
Ran Benitac9c74152014-08-19 23:59:52 +03001481 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001482
1483 return;
1484
1485error:
1486 wl_keyboard_release(input->parent.keyboard);
1487 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001488}
1489
1490static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001491input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001492 struct wl_keyboard *keyboard,
1493 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001494 struct wl_surface *surface,
1495 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001496{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001497 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001498 struct wayland_output *focus;
1499
1500 focus = input->keyboard_focus;
1501 if (focus) {
1502 /* This shouldn't happen */
1503 focus->keyboard_count--;
1504 if (!focus->keyboard_count && focus->frame)
1505 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1506 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1507 weston_output_schedule_repaint(&focus->base);
1508 }
1509
1510 input->keyboard_focus = wl_surface_get_user_data(surface);
1511 input->keyboard_focus->keyboard_count++;
1512
1513 focus = input->keyboard_focus;
1514 if (focus->frame) {
1515 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1516 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1517 weston_output_schedule_repaint(&focus->base);
1518 }
1519
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001520
Daniel Stone50692802012-06-22 13:21:41 +01001521 /* XXX: If we get a modifier event immediately before the focus,
1522 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001523 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001524 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001525}
1526
1527static void
1528input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001529 struct wl_keyboard *keyboard,
1530 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001531 struct wl_surface *surface)
1532{
1533 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001534 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001535
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001536 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001537
1538 focus = input->keyboard_focus;
1539 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001540 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001541
1542 focus->keyboard_count--;
1543 if (!focus->keyboard_count && focus->frame) {
1544 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1545 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1546 weston_output_schedule_repaint(&focus->base);
1547 }
1548
1549 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001550}
1551
Daniel Stone37816df2012-05-16 18:45:18 +01001552static void
1553input_handle_key(void *data, struct wl_keyboard *keyboard,
1554 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1555{
1556 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001557
Daniel Stone50692802012-06-22 13:21:41 +01001558 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001559 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001560 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001561 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001562 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001563}
1564
Daniel Stone351eb612012-05-31 15:27:47 -04001565static void
1566input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001567 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001568 uint32_t mods_latched, uint32_t mods_locked,
1569 uint32_t group)
1570{
Daniel Stone50692802012-06-22 13:21:41 +01001571 struct wayland_input *input = data;
1572 struct wayland_compositor *c = input->compositor;
1573 uint32_t serial_out;
1574
1575 /* If we get a key event followed by a modifier event with the
1576 * same serial number, then we try to preserve those semantics by
1577 * reusing the same serial number on the way out too. */
1578 if (serial_in == input->key_serial)
1579 serial_out = wl_display_get_serial(c->base.wl_display);
1580 else
1581 serial_out = wl_display_next_serial(c->base.wl_display);
1582
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001583 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001584 mods_depressed, mods_latched,
1585 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001587}
1588
Jonny Lamb497994a2014-08-12 14:58:26 +02001589static void
1590input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1591 int32_t rate, int32_t delay)
1592{
1593 struct wayland_input *input = data;
1594 struct wayland_compositor *c = input->compositor;
1595
1596 c->base.kb_repeat_rate = rate;
1597 c->base.kb_repeat_delay = delay;
1598}
1599
Daniel Stone37816df2012-05-16 18:45:18 +01001600static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001601 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001602 input_handle_keyboard_enter,
1603 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001604 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001605 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001606 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001607};
1608
1609static void
Daniel Stone37816df2012-05-16 18:45:18 +01001610input_handle_capabilities(void *data, struct wl_seat *seat,
1611 enum wl_seat_capability caps)
1612{
1613 struct wayland_input *input = data;
1614
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001615 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1616 input->parent.pointer = wl_seat_get_pointer(seat);
1617 wl_pointer_set_user_data(input->parent.pointer, input);
1618 wl_pointer_add_listener(input->parent.pointer,
1619 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001620 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001621 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1622 wl_pointer_destroy(input->parent.pointer);
1623 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001624 }
1625
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001626 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1627 input->parent.keyboard = wl_seat_get_keyboard(seat);
1628 wl_keyboard_set_user_data(input->parent.keyboard, input);
1629 wl_keyboard_add_listener(input->parent.keyboard,
1630 &keyboard_listener, input);
1631 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1632 wl_keyboard_destroy(input->parent.keyboard);
1633 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001634 }
1635}
1636
Jonny Lamb497994a2014-08-12 14:58:26 +02001637static void
1638input_handle_name(void *data, struct wl_seat *seat,
1639 const char *name)
1640{
1641}
1642
Daniel Stone37816df2012-05-16 18:45:18 +01001643static const struct wl_seat_listener seat_listener = {
1644 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001645 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001646};
1647
1648static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001649display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001650{
1651 struct wayland_input *input;
1652
Peter Huttererf3d62272013-08-08 11:57:05 +10001653 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001654 if (input == NULL)
1655 return;
1656
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001657 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001658 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001659 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001660 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001661 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001662
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001663 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1664 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001665
1666 input->parent.cursor.surface =
1667 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001668}
1669
1670static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001671wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1672 int32_t x, int32_t y,
1673 int32_t physical_width, int32_t physical_height,
1674 int32_t subpixel, const char *make,
1675 const char *model, int32_t transform)
1676{
1677 struct wayland_parent_output *output = data;
1678
1679 output->x = x;
1680 output->y = y;
1681 output->physical.width = physical_width;
1682 output->physical.height = physical_height;
1683 output->physical.subpixel = subpixel;
1684
1685 free(output->physical.make);
1686 output->physical.make = strdup(make);
1687 free(output->physical.model);
1688 output->physical.model = strdup(model);
1689
1690 output->transform = transform;
1691}
1692
1693static struct weston_mode *
1694find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1695{
1696 struct weston_mode *mode;
1697
1698 wl_list_for_each(mode, list, link) {
1699 if (mode->width == width && mode->height == height &&
1700 mode->refresh == refresh)
1701 return mode;
1702 }
1703
1704 mode = zalloc(sizeof *mode);
1705 if (!mode)
1706 return NULL;
1707
1708 mode->width = width;
1709 mode->height = height;
1710 mode->refresh = refresh;
1711 wl_list_insert(list, &mode->link);
1712
1713 return mode;
1714}
1715
1716static void
1717wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1718 uint32_t flags, int32_t width, int32_t height,
1719 int32_t refresh)
1720{
1721 struct wayland_parent_output *output = data;
1722 struct weston_mode *mode;
1723
1724 if (output->output) {
1725 mode = find_mode(&output->output->base.mode_list,
1726 width, height, refresh);
1727 if (!mode)
1728 return;
1729 mode->flags = flags;
1730 /* Do a mode-switch on current mode change? */
1731 } else {
1732 mode = find_mode(&output->mode_list, width, height, refresh);
1733 if (!mode)
1734 return;
1735 mode->flags = flags;
1736 if (flags & WL_OUTPUT_MODE_CURRENT)
1737 output->current_mode = mode;
1738 if (flags & WL_OUTPUT_MODE_PREFERRED)
1739 output->preferred_mode = mode;
1740 }
1741}
1742
1743static const struct wl_output_listener output_listener = {
1744 wayland_parent_output_geometry,
1745 wayland_parent_output_mode
1746};
1747
1748static void
1749wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1750{
1751 struct wayland_parent_output *output;
1752
1753 output = zalloc(sizeof *output);
1754 if (!output)
1755 return;
1756
1757 output->id = id;
1758 output->global = wl_registry_bind(c->parent.registry, id,
1759 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001760 if (!output->global) {
1761 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001762 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001763 }
1764
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001765 wl_output_add_listener(output->global, &output_listener, output);
1766
1767 output->scale = 0;
1768 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1769 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1770 wl_list_init(&output->mode_list);
1771 wl_list_insert(&c->parent.output_list, &output->link);
1772
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001773 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001774 wl_display_roundtrip(c->parent.wl_display);
1775 wayland_output_create_for_parent_output(c, output);
1776 }
1777}
1778
1779static void
1780wayland_parent_output_destroy(struct wayland_parent_output *output)
1781{
1782 struct weston_mode *mode, *next;
1783
1784 if (output->output)
1785 wayland_output_destroy(&output->output->base);
1786
1787 wl_output_destroy(output->global);
1788 free(output->physical.make);
1789 free(output->physical.model);
1790
1791 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1792 wl_list_remove(&mode->link);
1793 free(mode);
1794 }
1795}
1796
1797static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001798registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1799 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001800{
1801 struct wayland_compositor *c = data;
1802
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001803 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001804 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001805 wl_registry_bind(registry, name,
1806 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001807 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001808 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001809 wl_registry_bind(registry, name,
1810 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001811 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1812 c->parent.fshell =
1813 wl_registry_bind(registry, name,
1814 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001815 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001816 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001817 } else if (strcmp(interface, "wl_output") == 0) {
1818 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001819 } else if (strcmp(interface, "wl_shm") == 0) {
1820 c->parent.shm =
1821 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001822 }
1823}
1824
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001825static void
1826registry_handle_global_remove(void *data, struct wl_registry *registry,
1827 uint32_t name)
1828{
1829 struct wayland_compositor *c = data;
1830 struct wayland_parent_output *output;
1831
1832 wl_list_for_each(output, &c->parent.output_list, link)
1833 if (output->id == name)
1834 wayland_parent_output_destroy(output);
1835}
1836
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001837static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001838 registry_handle_global,
1839 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001840};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001841
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001842static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001843wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1844{
1845 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001846 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001847
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001848 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1849 wl_display_terminate(c->base.wl_display);
1850 return 0;
1851 }
1852
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001853 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001854 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001855 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001856 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001857
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001858 if (mask == 0) {
1859 count = wl_display_dispatch_pending(c->parent.wl_display);
1860 wl_display_flush(c->parent.wl_display);
1861 }
1862
1863 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001864}
1865
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001866static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001867wayland_restore(struct weston_compositor *ec)
1868{
1869}
1870
1871static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001872wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001873{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001874 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1875
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001876 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001877
Jonas Ådahle5a12252013-04-05 23:07:11 +02001878 if (c->parent.shm)
1879 wl_shm_destroy(c->parent.shm);
1880
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001881 free(ec);
1882}
1883
Jason Ekstrand7744f712013-10-27 22:24:55 -05001884static const char *left_ptrs[] = {
1885 "left_ptr",
1886 "default",
1887 "top_left_arrow",
1888 "left-arrow"
1889};
1890
1891static void
1892create_cursor(struct wayland_compositor *c, struct weston_config *config)
1893{
1894 struct weston_config_section *s;
1895 int size;
1896 char *theme = NULL;
1897 unsigned int i;
1898
1899 s = weston_config_get_section(config, "shell", NULL, NULL);
1900 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1901 weston_config_section_get_int(s, "cursor-size", &size, 32);
1902
1903 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001904 if (!c->cursor_theme) {
1905 fprintf(stderr, "could not load cursor theme\n");
1906 return;
1907 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001908
U. Artie Eoffff755002014-01-17 12:36:58 -08001909 free(theme);
1910
Jason Ekstrand7744f712013-10-27 22:24:55 -05001911 c->cursor = NULL;
1912 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1913 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1914 left_ptrs[i]);
1915 if (!c->cursor) {
1916 fprintf(stderr, "could not load left cursor\n");
1917 return;
1918 }
1919}
1920
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001921static void
1922fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1923 void *data)
1924{
1925 struct wayland_compositor *c = data;
1926 struct wayland_input *input = NULL;
1927
1928 wl_list_for_each(input, &c->input_list, link)
1929 if (&input->base == seat_base)
1930 break;
1931
1932 if (!input || !input->output)
1933 return;
1934
1935 if (input->output->frame)
1936 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1937 else
1938 wayland_output_set_windowed(input->output);
1939
1940 weston_output_schedule_repaint(&input->output->base);
1941}
1942
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001943static struct wayland_compositor *
1944wayland_compositor_create(struct wl_display *display, int use_pixman,
1945 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001946 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001947{
1948 struct wayland_compositor *c;
1949 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001950 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001951
Peter Huttererf3d62272013-08-08 11:57:05 +10001952 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001953 if (c == NULL)
1954 return NULL;
1955
Daniel Stone725c2c32012-06-22 14:04:36 +01001956 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001957 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001958 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001959
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001960 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1961 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001962
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001963 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001964 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001965 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001966 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001967 }
1968
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001969 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001970 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001971 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1972 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001973 wl_display_roundtrip(c->parent.wl_display);
1974
1975 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001976
1977 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001978
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001979 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001980
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001981 if (!c->use_pixman) {
1982 gl_renderer = weston_load_module("gl-renderer.so",
1983 "gl_renderer_interface");
1984 if (!gl_renderer)
1985 c->use_pixman = 1;
1986 }
1987
1988 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001989 if (gl_renderer->create(&c->base,
Derek Foremane76f1852015-05-15 12:12:39 -05001990 EGL_PLATFORM_WAYLAND_KHR,
1991 c->parent.wl_display,
1992 gl_renderer->alpha_attribs,
1993 NULL,
1994 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001995 weston_log("Failed to initialize the GL renderer; "
1996 "falling back to pixman.\n");
1997 c->use_pixman = 1;
1998 }
1999 }
2000
2001 if (c->use_pixman) {
2002 if (pixman_renderer_init(&c->base) < 0) {
2003 weston_log("Failed to initialize pixman renderer\n");
2004 goto err_display;
2005 }
2006 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002007
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002008 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002009 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002010
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002011 loop = wl_display_get_event_loop(c->base.wl_display);
2012
2013 fd = wl_display_get_fd(c->parent.wl_display);
2014 c->parent.wl_source =
2015 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2016 wayland_compositor_handle_event, c);
2017 if (c->parent.wl_source == NULL)
2018 goto err_renderer;
2019
2020 wl_event_source_check(c->parent.wl_source);
2021
2022 return c;
2023err_renderer:
2024 c->base.renderer->destroy(&c->base);
2025err_display:
2026 wl_display_disconnect(c->parent.wl_display);
2027err_compositor:
2028 weston_compositor_shutdown(&c->base);
2029err_free:
2030 free(c);
2031 return NULL;
2032}
2033
2034static void
2035wayland_compositor_destroy(struct wayland_compositor *c)
2036{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002037 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002038
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002039 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002040 wayland_output_destroy(output);
2041
2042 c->base.renderer->destroy(&c->base);
2043 wl_display_disconnect(c->parent.wl_display);
2044
2045 if (c->theme)
2046 theme_destroy(c->theme);
2047 if (c->frame_device)
2048 cairo_device_destroy(c->frame_device);
2049 wl_cursor_theme_destroy(c->cursor_theme);
2050
2051 weston_compositor_shutdown(&c->base);
2052 free(c);
2053}
2054
2055WL_EXPORT struct weston_compositor *
2056backend_init(struct wl_display *display, int *argc, char *argv[],
2057 struct weston_config *config)
2058{
2059 struct wayland_compositor *c;
2060 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002061 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002062 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002063 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002064 const char *section_name, *display_name;
2065 char *name;
2066
2067 const struct weston_option wayland_options[] = {
2068 { WESTON_OPTION_INTEGER, "width", 0, &width },
2069 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002070 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002071 { WESTON_OPTION_STRING, "display", 0, &display_name },
2072 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2073 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002074 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002075 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002076 };
2077
2078 width = 0;
2079 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002080 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002081 display_name = NULL;
2082 use_pixman = 0;
2083 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002084 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002085 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002086 parse_options(wayland_options,
2087 ARRAY_LENGTH(wayland_options), argc, argv);
2088
2089 c = wayland_compositor_create(display, use_pixman, display_name,
2090 argc, argv, config);
2091 if (!c)
2092 return NULL;
2093
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002094 if (sprawl || c->parent.fshell) {
2095 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002096 wl_display_roundtrip(c->parent.wl_display);
2097
2098 wl_list_for_each(poutput, &c->parent.output_list, link)
2099 wayland_output_create_for_parent_output(c, poutput);
2100
2101 return &c->base;
2102 }
2103
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002104 if (fullscreen) {
2105 output = wayland_output_create(c, 0, 0, width, height,
2106 NULL, 1, 0, 1);
2107 if (!output)
2108 goto err_outputs;
2109
Axel Davydd8b88d2013-11-17 21:34:16 +01002110 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002111 return &c->base;
2112 }
2113
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002114 section = NULL;
2115 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002116 while (weston_config_next_section(config, &section, &section_name)) {
2117 if (!section_name || strcmp(section_name, "output") != 0)
2118 continue;
2119 weston_config_section_get_string(section, "name", &name, NULL);
2120 if (name == NULL)
2121 continue;
2122
2123 if (name[0] != 'W' || name[1] != 'L') {
2124 free(name);
2125 continue;
2126 }
2127 free(name);
2128
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002129 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002130 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002131 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002132 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002133 if (wayland_output_set_windowed(output))
2134 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002135
2136 x += output->base.width;
2137 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002138 }
2139
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002140 if (!width)
2141 width = 1024;
2142 if (!height)
2143 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002144 if (!scale)
2145 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002146 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002147 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002148 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002149 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002150 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002151 if (wayland_output_set_windowed(output))
2152 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002153
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002154 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002155 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002156 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002157
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002158 weston_compositor_add_key_binding(&c->base, KEY_F,
2159 MODIFIER_CTRL | MODIFIER_ALT,
2160 fullscreen_binding, c);
2161
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002162 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002163
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002164err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002165 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002166 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002167}