blob: 49f74cecccba40a3d3e4264b425f3b07b440ae8b [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"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010046
Jason Ekstrand48ce4212013-10-27 22:25:02 -050047#define WINDOW_TITLE "Weston Compositor"
48
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050050 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051
52 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040053 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040054 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 struct wl_compositor *compositor;
56 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050057 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020058 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050060 struct wl_list output_list;
61
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_event_source *wl_source;
63 uint32_t event_mask;
64 } parent;
65
Jason Ekstrandff2fd462013-10-27 22:24:58 -050066 int use_pixman;
67
Jason Ekstrand7744f712013-10-27 22:24:55 -050068 struct theme *theme;
69 cairo_device_t *frame_device;
70 struct wl_cursor_theme *cursor_theme;
71 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050072
Jason Ekstrand06ced802013-11-07 20:13:29 -060073 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010074};
75
76struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050077 struct weston_output base;
78
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010079 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050080 int draw_initial_frame;
81 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050082
83 struct wl_output *output;
84 uint32_t global_id;
85
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050086 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060087 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010088 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050089
Jason Ekstrand7744f712013-10-27 22:24:55 -050090 int keyboard_count;
91
Jason Ekstrand5ea04802013-11-07 20:13:33 -060092 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050093 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050094
Jason Ekstrand7744f712013-10-27 22:24:55 -050095 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050096 struct wl_egl_window *egl_window;
97 struct {
98 cairo_surface_t *top;
99 cairo_surface_t *left;
100 cairo_surface_t *right;
101 cairo_surface_t *bottom;
102 } border;
103 } gl;
104
105 struct {
106 struct wl_list buffers;
107 struct wl_list free_buffers;
108 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500109
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500110 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500111 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100112};
113
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500114struct wayland_parent_output {
115 struct wayland_output *output;
116 struct wl_list link;
117
118 struct wl_output *global;
119 uint32_t id;
120
121 struct {
122 char *make;
123 char *model;
124 int32_t width, height;
125 uint32_t subpixel;
126 } physical;
127
128 int32_t x, y;
129 uint32_t transform;
130 uint32_t scale;
131
132 struct wl_list mode_list;
133 struct weston_mode *preferred_mode;
134 struct weston_mode *current_mode;
135};
136
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500137struct wayland_shm_buffer {
138 struct wayland_output *output;
139 struct wl_list link;
140 struct wl_list free_link;
141
142 struct wl_buffer *buffer;
143 void *data;
144 size_t size;
145 pixman_region32_t damage;
146 int frame_damaged;
147
148 pixman_image_t *pm_image;
149 cairo_surface_t *c_surface;
150};
151
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100152struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400153 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100154 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100155 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500156
157 struct {
158 struct wl_seat *seat;
159 struct wl_pointer *pointer;
160 struct wl_keyboard *keyboard;
161 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500162
163 struct {
164 struct wl_surface *surface;
165 int32_t hx, hy;
166 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500167 } parent;
168
Daniel Stone50692802012-06-22 13:21:41 +0100169 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400170 uint32_t enter_serial;
171 int focus;
172 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500173 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100174};
175
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300176struct gl_renderer_interface *gl_renderer;
177
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500178static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500179wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
180{
181 cairo_surface_destroy(buffer->c_surface);
182 pixman_image_unref(buffer->pm_image);
183
184 wl_buffer_destroy(buffer->buffer);
185 munmap(buffer->data, buffer->size);
186
187 pixman_region32_fini(&buffer->damage);
188
189 wl_list_remove(&buffer->link);
190 wl_list_remove(&buffer->free_link);
191 free(buffer);
192}
193
194static void
195buffer_release(void *data, struct wl_buffer *buffer)
196{
197 struct wayland_shm_buffer *sb = data;
198
199 if (sb->output) {
200 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
201 } else {
202 wayland_shm_buffer_destroy(sb);
203 }
204}
205
206static const struct wl_buffer_listener buffer_listener = {
207 buffer_release
208};
209
210static struct wayland_shm_buffer *
211wayland_output_get_shm_buffer(struct wayland_output *output)
212{
213 struct wayland_compositor *c =
214 (struct wayland_compositor *) output->base.compositor;
215 struct wl_shm *shm = c->parent.shm;
216 struct wayland_shm_buffer *sb;
217
218 struct wl_shm_pool *pool;
219 int width, height, stride;
220 int32_t fx, fy;
221 int fd;
222 unsigned char *data;
223
224 if (!wl_list_empty(&output->shm.free_buffers)) {
225 sb = container_of(output->shm.free_buffers.next,
226 struct wayland_shm_buffer, free_link);
227 wl_list_remove(&sb->free_link);
228 wl_list_init(&sb->free_link);
229
230 return sb;
231 }
232
233 if (output->frame) {
234 width = frame_width(output->frame);
235 height = frame_height(output->frame);
236 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500237 width = output->base.current_mode->width;
238 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500239 }
240
241 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
242
243 fd = os_create_anonymous_file(height * stride);
244 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000245 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500246 return NULL;
247 }
248
249 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
250 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000251 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500252 close(fd);
253 return NULL;
254 }
255
256 sb = zalloc(sizeof *sb);
257
258 sb->output = output;
259 wl_list_init(&sb->free_link);
260 wl_list_insert(&output->shm.buffers, &sb->link);
261
262 pixman_region32_init_rect(&sb->damage, 0, 0,
263 output->base.width, output->base.height);
264 sb->frame_damaged = 1;
265
266 sb->data = data;
267 sb->size = height * stride;
268
269 pool = wl_shm_create_pool(shm, fd, sb->size);
270
271 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
272 width, height,
273 stride,
274 WL_SHM_FORMAT_ARGB8888);
275 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
276 wl_shm_pool_destroy(pool);
277 close(fd);
278
279 memset(data, 0, sb->size);
280
281 sb->c_surface =
282 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
283 width, height, stride);
284
285 fx = 0;
286 fy = 0;
287 if (output->frame)
288 frame_interior(output->frame, &fx, &fy, 0, 0);
289 sb->pm_image =
290 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
291 (uint32_t *)(data + fy * stride) + fx,
292 stride);
293
294 return sb;
295}
296
297static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500298frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400299{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400301
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500302 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500303 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400304}
305
306static const struct wl_callback_listener frame_listener = {
307 frame_done
308};
309
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500310static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200311draw_initial_frame(struct wayland_output *output)
312{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500313 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200314
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500315 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200316
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500317 /* If we are rendering with GL, then orphan it so that it gets
318 * destroyed immediately */
319 if (output->gl.egl_window)
320 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500321
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500322 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500323 wl_surface_damage(output->parent.surface, 0, 0,
324 output->base.current_mode->width,
325 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200326}
327
328static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500329wayland_output_update_gl_border(struct wayland_output *output)
330{
331 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
332 cairo_t *cr;
333
334 if (!output->frame)
335 return;
336 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
337 return;
338
339 fwidth = frame_width(output->frame);
340 fheight = frame_height(output->frame);
341 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
342
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500343 if (!output->gl.border.top)
344 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500345 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
346 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500347 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500348 frame_repaint(output->frame, cr);
349 cairo_destroy(cr);
350 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
351 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500352 cairo_image_surface_get_stride(output->gl.border.top) / 4,
353 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500354
355
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500356 if (!output->gl.border.left)
357 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500358 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
359 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500360 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500361 cairo_translate(cr, 0, -iy);
362 frame_repaint(output->frame, cr);
363 cairo_destroy(cr);
364 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
365 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500366 cairo_image_surface_get_stride(output->gl.border.left) / 4,
367 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500368
369
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500370 if (!output->gl.border.right)
371 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
373 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500374 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500375 cairo_translate(cr, -(iwidth + ix), -iy);
376 frame_repaint(output->frame, cr);
377 cairo_destroy(cr);
378 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
379 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500380 cairo_image_surface_get_stride(output->gl.border.right) / 4,
381 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382
383
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500384 if (!output->gl.border.bottom)
385 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
387 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389 cairo_translate(cr, 0, -(iy + iheight));
390 frame_repaint(output->frame, cr);
391 cairo_destroy(cr);
392 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
393 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
395 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396}
397
398static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200399wayland_output_start_repaint_loop(struct weston_output *output_base)
400{
401 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500402 struct wayland_compositor *wc =
403 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200404 struct wl_callback *callback;
405
406 /* If this is the initial frame, we need to attach a buffer so that
407 * the compositor can map the surface and include it in its render
408 * loop. If the surface doesn't end up in the render loop, the frame
409 * callback won't be invoked. The buffer is transparent and of the
410 * same size as the future real output buffer. */
411 if (output->parent.draw_initial_frame) {
412 output->parent.draw_initial_frame = 0;
413
414 draw_initial_frame(output);
415 }
416
417 callback = wl_surface_frame(output->parent.surface);
418 wl_callback_add_listener(callback, &frame_listener, output);
419 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500420 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200421}
422
David Herrmann1edf44c2013-10-22 17:11:26 +0200423static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500424wayland_output_repaint_gl(struct weston_output *output_base,
425 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400426{
427 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400428 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400429 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600430
Kristian Høgsberg33418202011-08-16 23:01:28 -0400431 callback = wl_surface_frame(output->parent.surface);
432 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100433
Jason Ekstrand7744f712013-10-27 22:24:55 -0500434 wayland_output_update_gl_border(output);
435
Pekka Paalanenbc106382012-10-10 12:49:31 +0300436 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200437
438 pixman_region32_subtract(&ec->primary_plane.damage,
439 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200440 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100441}
442
Matt Roper361d2ad2011-08-29 13:52:23 -0700443static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500444wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
445{
446 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
447 cairo_t *cr;
448
449 if (!buffer->output->frame || !buffer->frame_damaged)
450 return;
451
452 cr = cairo_create(buffer->c_surface);
453
454 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
455 fwidth = frame_width(buffer->output->frame);
456 fheight = frame_height(buffer->output->frame);
457
458 /* Set the clip so we don't unnecisaraly damage the surface */
459 cairo_move_to(cr, ix, iy);
460 cairo_rel_line_to(cr, iwidth, 0);
461 cairo_rel_line_to(cr, 0, iheight);
462 cairo_rel_line_to(cr, -iwidth, 0);
463 cairo_line_to(cr, ix, iy);
464 cairo_line_to(cr, 0, iy);
465 cairo_line_to(cr, 0, fheight);
466 cairo_line_to(cr, fwidth, fheight);
467 cairo_line_to(cr, fwidth, 0);
468 cairo_line_to(cr, 0, 0);
469 cairo_line_to(cr, 0, iy);
470 cairo_close_path(cr);
471 cairo_clip(cr);
472
473 /* Draw using a pattern so that the final result gets clipped */
474 cairo_push_group(cr);
475 frame_repaint(buffer->output->frame, cr);
476 cairo_pop_group_to_source(cr);
477 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
478 cairo_paint(cr);
479
480 cairo_destroy(cr);
481}
482
483static void
484wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
485{
486 pixman_region32_t damage;
487 pixman_box32_t *rects;
488 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
489 int i, n;
490
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500491 pixman_region32_init(&damage);
492 weston_transformed_region(sb->output->base.width,
493 sb->output->base.height,
494 sb->output->base.transform,
495 sb->output->base.current_scale,
496 &sb->damage, &damage);
497
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500498 if (sb->output->frame) {
499 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
500 fwidth = frame_width(sb->output->frame);
501 fheight = frame_height(sb->output->frame);
502
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500503 pixman_region32_translate(&damage, ix, iy);
504
505 if (sb->frame_damaged) {
506 pixman_region32_union_rect(&damage, &damage,
507 0, 0, fwidth, iy);
508 pixman_region32_union_rect(&damage, &damage,
509 0, iy, ix, iheight);
510 pixman_region32_union_rect(&damage, &damage,
511 ix + iwidth, iy,
512 fwidth - (ix + iwidth), iheight);
513 pixman_region32_union_rect(&damage, &damage,
514 0, iy + iheight,
515 fwidth, fheight - (iy + iheight));
516 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500517 }
518
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500519 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500520 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
521 for (i = 0; i < n; ++i)
522 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
523 rects[i].y1, rects[i].x2 - rects[i].x1,
524 rects[i].y2 - rects[i].y1);
525
526 if (sb->output->frame)
527 pixman_region32_fini(&damage);
528}
529
530static int
531wayland_output_repaint_pixman(struct weston_output *output_base,
532 pixman_region32_t *damage)
533{
534 struct wayland_output *output = (struct wayland_output *) output_base;
535 struct wayland_compositor *c =
536 (struct wayland_compositor *)output->base.compositor;
537 struct wl_callback *callback;
538 struct wayland_shm_buffer *sb;
539
540 if (output->frame) {
541 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
542 wl_list_for_each(sb, &output->shm.buffers, link)
543 sb->frame_damaged = 1;
544 }
545
546 wl_list_for_each(sb, &output->shm.buffers, link)
547 pixman_region32_union(&sb->damage, &sb->damage, damage);
548
549 sb = wayland_output_get_shm_buffer(output);
550
551 wayland_output_update_shm_border(sb);
552 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
553 c->base.renderer->repaint_output(output_base, &sb->damage);
554
555 wayland_shm_buffer_attach(sb);
556
557 callback = wl_surface_frame(output->parent.surface);
558 wl_callback_add_listener(callback, &frame_listener, output);
559 wl_surface_commit(output->parent.surface);
560 wl_display_flush(c->parent.wl_display);
561
562 pixman_region32_fini(&sb->damage);
563 pixman_region32_init(&sb->damage);
564 sb->frame_damaged = 0;
565
566 pixman_region32_subtract(&c->base.primary_plane.damage,
567 &c->base.primary_plane.damage, damage);
568 return 0;
569}
570
571static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500572wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700573{
574 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500575 struct wayland_compositor *c =
576 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700577
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500578 if (c->use_pixman) {
579 pixman_renderer_output_destroy(output_base);
580 } else {
581 gl_renderer->output_destroy(output_base);
582 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100583
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500584 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500585 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500586 if (output->parent.shell_surface)
587 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500588
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600589 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500590 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600591
592 cairo_surface_destroy(output->gl.border.top);
593 cairo_surface_destroy(output->gl.border.left);
594 cairo_surface_destroy(output->gl.border.right);
595 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500596
597 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700598 free(output);
599
600 return;
601}
602
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300603static const struct wl_shell_surface_listener shell_surface_listener;
604
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200605static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500606wayland_output_init_gl_renderer(struct wayland_output *output)
607{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500608 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500609
610 if (output->frame) {
611 fwidth = frame_width(output->frame);
612 fheight = frame_height(output->frame);
613 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500614 fwidth = output->base.current_mode->width;
615 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500616 }
617
618 output->gl.egl_window =
619 wl_egl_window_create(output->parent.surface,
620 fwidth, fheight);
621 if (!output->gl.egl_window) {
622 weston_log("failure to create wl_egl_window\n");
623 return -1;
624 }
625
626 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000627 output->gl.egl_window,
628 gl_renderer->alpha_attribs,
629 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630 goto cleanup_window;
631
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500632 return 0;
633
634cleanup_window:
635 wl_egl_window_destroy(output->gl.egl_window);
636 return -1;
637}
638
639static int
640wayland_output_init_pixman_renderer(struct wayland_output *output)
641{
642 return pixman_renderer_output_create(&output->base);
643}
644
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600645static void
646wayland_output_resize_surface(struct wayland_output *output)
647{
648 struct wayland_compositor *c =
649 (struct wayland_compositor *)output->base.compositor;
650 struct wayland_shm_buffer *buffer, *next;
651 int32_t ix, iy, iwidth, iheight;
652 int32_t width, height;
653 struct wl_region *region;
654
655 width = output->base.current_mode->width;
656 height = output->base.current_mode->height;
657
658 if (output->frame) {
659 frame_resize_inside(output->frame, width, height);
660
661 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
662 region = wl_compositor_create_region(c->parent.compositor);
663 wl_region_add(region, ix, iy, iwidth, iheight);
664 wl_surface_set_input_region(output->parent.surface, region);
665 wl_region_destroy(region);
666
667 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
668 region = wl_compositor_create_region(c->parent.compositor);
669 wl_region_add(region, ix, iy, iwidth, iheight);
670 wl_surface_set_opaque_region(output->parent.surface, region);
671 wl_region_destroy(region);
672
673 width = frame_width(output->frame);
674 height = frame_height(output->frame);
675 } else {
676 region = wl_compositor_create_region(c->parent.compositor);
677 wl_region_add(region, 0, 0, width, height);
678 wl_surface_set_input_region(output->parent.surface, region);
679 wl_region_destroy(region);
680
681 region = wl_compositor_create_region(c->parent.compositor);
682 wl_region_add(region, 0, 0, width, height);
683 wl_surface_set_opaque_region(output->parent.surface, region);
684 wl_region_destroy(region);
685 }
686
687 if (output->gl.egl_window) {
688 wl_egl_window_resize(output->gl.egl_window,
689 width, height, 0, 0);
690
691 /* These will need to be re-created due to the resize */
692 gl_renderer->output_set_border(&output->base,
693 GL_RENDERER_BORDER_TOP,
694 0, 0, 0, NULL);
695 cairo_surface_destroy(output->gl.border.top);
696 output->gl.border.top = NULL;
697 gl_renderer->output_set_border(&output->base,
698 GL_RENDERER_BORDER_LEFT,
699 0, 0, 0, NULL);
700 cairo_surface_destroy(output->gl.border.left);
701 output->gl.border.left = NULL;
702 gl_renderer->output_set_border(&output->base,
703 GL_RENDERER_BORDER_RIGHT,
704 0, 0, 0, NULL);
705 cairo_surface_destroy(output->gl.border.right);
706 output->gl.border.right = NULL;
707 gl_renderer->output_set_border(&output->base,
708 GL_RENDERER_BORDER_BOTTOM,
709 0, 0, 0, NULL);
710 cairo_surface_destroy(output->gl.border.bottom);
711 output->gl.border.bottom = NULL;
712 }
713
714 /* Throw away any remaining SHM buffers */
715 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
716 wayland_shm_buffer_destroy(buffer);
717 /* These will get thrown away when they get released */
718 wl_list_for_each(buffer, &output->shm.buffers, link)
719 buffer->output = NULL;
720}
721
722static int
723wayland_output_set_windowed(struct wayland_output *output)
724{
725 struct wayland_compositor *c =
726 (struct wayland_compositor *)output->base.compositor;
727 int tlen;
728 char *title;
729
730 if (output->frame)
731 return 0;
732
733 if (output->name) {
734 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
735 title = malloc(tlen + 1);
736 if (!title)
737 return -1;
738
739 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
740 } else {
741 title = strdup(WINDOW_TITLE);
742 }
743
744 if (!c->theme) {
745 c->theme = theme_create();
746 if (!c->theme) {
747 free(title);
748 return -1;
749 }
750 }
751 output->frame = frame_create(c->theme, 100, 100,
752 FRAME_BUTTON_CLOSE, title);
753 free(title);
754 if (!output->frame)
755 return -1;
756
757 if (output->keyboard_count)
758 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
759
760 wayland_output_resize_surface(output);
761
762 wl_shell_surface_set_toplevel(output->parent.shell_surface);
763
764 return 0;
765}
766
767static void
768wayland_output_set_fullscreen(struct wayland_output *output,
769 enum wl_shell_surface_fullscreen_method method,
770 uint32_t framerate, struct wl_output *target)
771{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500772 struct wayland_compositor *c =
773 (struct wayland_compositor *)output->base.compositor;
774
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600775 if (output->frame) {
776 frame_destroy(output->frame);
777 output->frame = NULL;
778 }
779
780 wayland_output_resize_surface(output);
781
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500782 if (output->parent.shell_surface) {
783 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
784 method, framerate, target);
785 } else if (c->parent.fshell) {
786 _wl_fullscreen_shell_present_surface(c->parent.fshell,
787 output->parent.surface,
788 method, target);
789 }
790}
791
792static struct weston_mode *
793wayland_output_choose_mode(struct wayland_output *output,
794 struct weston_mode *ref_mode)
795{
796 struct weston_mode *mode;
797
798 /* First look for an exact match */
799 wl_list_for_each(mode, &output->base.mode_list, link)
800 if (mode->width == ref_mode->width &&
801 mode->height == ref_mode->height &&
802 mode->refresh == ref_mode->refresh)
803 return mode;
804
805 /* If we can't find an exact match, ignore refresh and try again */
806 wl_list_for_each(mode, &output->base.mode_list, link)
807 if (mode->width == ref_mode->width &&
808 mode->height == ref_mode->height)
809 return mode;
810
811 /* Yeah, we failed */
812 return NULL;
813}
814
815enum mode_status {
816 MODE_STATUS_UNKNOWN,
817 MODE_STATUS_SUCCESS,
818 MODE_STATUS_FAIL,
819 MODE_STATUS_CANCEL,
820};
821
822static void
823mode_feedback_successful(void *data,
824 struct _wl_fullscreen_shell_mode_feedback *fb)
825{
826 enum mode_status *value = data;
827
828 printf("Mode switch successful\n");
829
830 *value = MODE_STATUS_SUCCESS;
831}
832
833static void
834mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
835{
836 enum mode_status *value = data;
837
838 printf("Mode switch failed\n");
839
840 *value = MODE_STATUS_FAIL;
841}
842
843static void
844mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
845{
846 enum mode_status *value = data;
847
848 printf("Mode switch cancelled\n");
849
850 *value = MODE_STATUS_CANCEL;
851}
852
853struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
854 mode_feedback_successful,
855 mode_feedback_failed,
856 mode_feedback_cancelled,
857};
858
859static int
860wayland_output_switch_mode(struct weston_output *output_base,
861 struct weston_mode *mode)
862{
863 struct wayland_output *output = (struct wayland_output *) output_base;
864 struct wayland_compositor *c;
865 struct wl_surface *old_surface;
866 struct weston_mode *old_mode;
867 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
868 enum mode_status mode_status;
869 int ret = 0;
870
871 if (output_base == NULL) {
872 weston_log("output is NULL.\n");
873 return -1;
874 }
875
876 if (mode == NULL) {
877 weston_log("mode is NULL.\n");
878 return -1;
879 }
880
881 c = (struct wayland_compositor *)output_base->compositor;
882
883 if (output->parent.shell_surface || !c->parent.fshell)
884 return -1;
885
886 mode = wayland_output_choose_mode(output, mode);
887 if (mode == NULL)
888 return -1;
889
890 if (output->base.current_mode == mode)
891 return 0;
892
893 old_mode = output->base.current_mode;
894 old_surface = output->parent.surface;
895 output->base.current_mode = mode;
896 output->parent.surface =
897 wl_compositor_create_surface(c->parent.compositor);
898 wl_surface_set_user_data(output->parent.surface, output);
899
900 /* Blow the old buffers because we changed size/surfaces */
901 wayland_output_resize_surface(output);
902
903 mode_feedback =
904 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
905 output->parent.surface,
906 output->parent.output,
907 mode->refresh);
908 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
909 &mode_feedback_listener,
910 &mode_status);
911
912 /* This should kick-start things again */
913 output->parent.draw_initial_frame = 1;
914 wayland_output_start_repaint_loop(&output->base);
915
916 mode_status = MODE_STATUS_UNKNOWN;
917 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
918 ret = wl_display_dispatch(c->parent.wl_display);
919
920 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
921
922 if (mode_status == MODE_STATUS_FAIL) {
923 output->base.current_mode = old_mode;
924 wl_surface_destroy(output->parent.surface);
925 output->parent.surface = old_surface;
926 wayland_output_resize_surface(output);
927
928 return -1;
929 }
930
931 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
932 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
933
934 if (c->use_pixman) {
935 pixman_renderer_output_destroy(output_base);
936 if (wayland_output_init_pixman_renderer(output) < 0)
937 goto err_output;
938 } else {
939 gl_renderer->output_destroy(output_base);
940 wl_egl_window_destroy(output->gl.egl_window);
941 if (wayland_output_init_gl_renderer(output) < 0)
942 goto err_output;
943 }
944 wl_surface_destroy(old_surface);
945
946 weston_output_schedule_repaint(&output->base);
947
948 return 0;
949
950err_output:
951 /* XXX */
952 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600953}
954
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500955static struct wayland_output *
956wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600957 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500958 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100959{
960 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500961 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600962
963 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
964 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100965
Peter Huttererf3d62272013-08-08 11:57:05 +1000966 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100967 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500968 return NULL;
969
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600970 output->name = name ? strdup(name) : NULL;
971 output->base.make = "waywayland";
972 output->base.model = "none";
973
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500974 output_width = width * scale;
975 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100976
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600977 output->parent.surface =
978 wl_compositor_create_surface(c->parent.compositor);
979 if (!output->parent.surface)
980 goto err_name;
981 wl_surface_set_user_data(output->parent.surface, output);
982
983 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600984
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500985 if (c->parent.shell) {
986 output->parent.shell_surface =
987 wl_shell_get_shell_surface(c->parent.shell,
988 output->parent.surface);
989 if (!output->parent.shell_surface)
990 goto err_surface;
991 wl_shell_surface_add_listener(output->parent.shell_surface,
992 &shell_surface_listener, output);
993 }
994
995 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600996 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
997 0, 0, NULL);
998 wl_display_roundtrip(c->parent.wl_display);
999 if (!width)
1000 output_width = output->parent.configure_width;
1001 if (!height)
1002 output_height = output->parent.configure_height;
1003 }
1004
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001005 output->mode.flags =
1006 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001007 output->mode.width = output_width;
1008 output->mode.height = output_height;
1009 output->mode.refresh = 60000;
1010 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001011 wl_list_init(&output->base.mode_list);
1012 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001013 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001014
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001015 wl_list_init(&output->shm.buffers);
1016 wl_list_init(&output->shm.free_buffers);
1017
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001018 weston_output_init(&output->base, &c->base, x, y, width, height,
1019 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001020
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001021 if (c->use_pixman) {
1022 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001023 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001024 output->base.repaint = wayland_output_repaint_pixman;
1025 } else {
1026 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001027 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001028 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001029 }
1030
Jonas Ådahle5a12252013-04-05 23:07:11 +02001031 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001032 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001033 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001034 output->base.set_backlight = NULL;
1035 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001036 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001037
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001038 wl_list_insert(c->base.output_list.prev, &output->base.link);
1039
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001040 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001041
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001042err_output:
1043 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001044 if (output->parent.shell_surface)
1045 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001046err_surface:
1047 wl_surface_destroy(output->parent.surface);
1048err_name:
1049 free(output->name);
1050
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001052 free(output);
1053
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001054 return NULL;
1055}
1056
1057static struct wayland_output *
1058wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001059 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001060 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001061 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001062{
1063 struct wayland_output *output;
1064 char *mode, *t, *name, *str;
1065 int width, height, scale;
1066 uint32_t transform;
1067 unsigned int i, slen;
1068
1069 static const struct { const char *name; uint32_t token; } transform_names[] = {
1070 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1071 { "90", WL_OUTPUT_TRANSFORM_90 },
1072 { "180", WL_OUTPUT_TRANSFORM_180 },
1073 { "270", WL_OUTPUT_TRANSFORM_270 },
1074 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1075 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1076 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1077 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1078 };
1079
1080 weston_config_section_get_string(config_section, "name", &name, NULL);
1081 if (name) {
1082 slen = strlen(name);
1083 slen += strlen(WINDOW_TITLE " - ");
1084 str = malloc(slen + 1);
1085 if (str)
1086 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1087 free(name);
1088 name = str;
1089 }
1090 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001091 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001092
1093 weston_config_section_get_string(config_section,
1094 "mode", &mode, "1024x600");
1095 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1096 weston_log("Invalid mode \"%s\" for output %s\n",
1097 mode, name);
1098 width = 1024;
1099 height = 640;
1100 }
1101 free(mode);
1102
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001103 if (option_width)
1104 width = option_width;
1105 if (option_height)
1106 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001107
1108 weston_config_section_get_int(config_section, "scale", &scale, 1);
1109
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001110 if (option_scale)
1111 scale = option_scale;
1112
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001113 weston_config_section_get_string(config_section,
1114 "transform", &t, "normal");
1115 transform = WL_OUTPUT_TRANSFORM_NORMAL;
1116 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
1117 if (strcmp(transform_names[i].name, t) == 0) {
1118 transform = transform_names[i].token;
1119 break;
1120 }
1121 }
1122 if (i >= ARRAY_LENGTH(transform_names))
1123 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
1124 free(t);
1125
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001126 output = wayland_output_create(c, x, y, width, height, name, 0,
1127 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001128 free(name);
1129
1130 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001131}
1132
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001133static struct wayland_output *
1134wayland_output_create_for_parent_output(struct wayland_compositor *c,
1135 struct wayland_parent_output *poutput)
1136{
1137 struct wayland_output *output;
1138 struct weston_mode *mode;
1139 int32_t x;
1140
1141 if (poutput->current_mode) {
1142 mode = poutput->current_mode;
1143 } else if (poutput->preferred_mode) {
1144 mode = poutput->current_mode;
1145 } else if (!wl_list_empty(&poutput->mode_list)) {
1146 mode = container_of(poutput->mode_list.next,
1147 struct weston_mode, link);
1148 } else {
1149 weston_log("No valid modes found. Skipping output");
1150 return NULL;
1151 }
1152
1153 if (!wl_list_empty(&c->base.output_list)) {
1154 output = container_of(c->base.output_list.prev,
1155 struct wayland_output, base.link);
1156 x = output->base.x + output->base.current_mode->width;
1157 } else {
1158 x = 0;
1159 }
1160
1161 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1162 NULL, 0,
1163 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1164 if (!output)
1165 return NULL;
1166
1167 output->parent.output = poutput->global;
1168
1169 output->base.make = poutput->physical.make;
1170 output->base.model = poutput->physical.model;
1171 wl_list_init(&output->base.mode_list);
1172 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1173 wl_list_init(&poutput->mode_list);
1174
1175 wayland_output_set_fullscreen(output,
1176 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1177 mode->refresh, poutput->global);
1178
1179 if (output->parent.shell_surface) {
1180 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1181 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1182 mode->refresh, poutput->global);
1183 } else if (c->parent.fshell) {
1184 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1185 output->parent.surface,
1186 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1187 poutput->global);
1188 _wl_fullscreen_shell_mode_feedback_destroy(
1189 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1190 output->parent.surface,
1191 poutput->global,
1192 mode->refresh));
1193 }
1194
1195 return output;
1196}
1197
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001198static void
1199shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1200 uint32_t serial)
1201{
1202 wl_shell_surface_pong(shell_surface, serial);
1203}
1204
1205static void
1206shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1207 uint32_t edges, int32_t width, int32_t height)
1208{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001209 struct wayland_output *output = data;
1210
1211 output->parent.configure_width = width;
1212 output->parent.configure_height = height;
1213
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001214 /* FIXME: implement resizing */
1215}
1216
1217static void
1218shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1219{
1220}
1221
1222static const struct wl_shell_surface_listener shell_surface_listener = {
1223 shell_surface_ping,
1224 shell_surface_configure,
1225 shell_surface_popup_done
1226};
1227
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001228/* Events received from the wayland-server this compositor is client of: */
1229
Jason Ekstrand7744f712013-10-27 22:24:55 -05001230/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001231static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001232input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001233{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001234
Jason Ekstrand7744f712013-10-27 22:24:55 -05001235 struct wl_buffer *buffer;
1236 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001237
Jason Ekstrand7744f712013-10-27 22:24:55 -05001238 if (!input->compositor->cursor)
1239 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001240
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241 image = input->compositor->cursor->images[0];
1242 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001243 if (!buffer)
1244 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001245
1246 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1247 input->parent.cursor.surface,
1248 image->hotspot_x, image->hotspot_y);
1249
1250 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1251 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1252 image->width, image->height);
1253 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001254}
1255
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001256static void
Daniel Stone37816df2012-05-16 18:45:18 +01001257input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1258 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001259 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001260{
1261 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001262 int32_t fx, fy;
1263 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001264
Daniel Stone50692802012-06-22 13:21:41 +01001265 /* XXX: If we get a modifier event immediately before the focus,
1266 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001267 input->enter_serial = serial;
1268 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001269
1270 if (input->output->frame) {
1271 location = frame_pointer_enter(input->output->frame, input,
1272 wl_fixed_to_int(x),
1273 wl_fixed_to_int(y));
1274 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1275 x -= wl_fixed_from_int(fx);
1276 y -= wl_fixed_from_int(fy);
1277
1278 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1279 weston_output_schedule_repaint(&input->output->base);
1280 } else {
1281 location = THEME_LOCATION_CLIENT_AREA;
1282 }
1283
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001284 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001285
1286 if (location == THEME_LOCATION_CLIENT_AREA) {
1287 input->focus = 1;
1288 notify_pointer_focus(&input->base, &input->output->base, x, y);
1289 wl_pointer_set_cursor(input->parent.pointer,
1290 input->enter_serial, NULL, 0, 0);
1291 } else {
1292 input->focus = 0;
1293 notify_pointer_focus(&input->base, NULL, 0, 0);
1294 input_set_cursor(input);
1295 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001296}
1297
1298static void
Daniel Stone37816df2012-05-16 18:45:18 +01001299input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1300 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001301{
1302 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001303
Jason Ekstrand7744f712013-10-27 22:24:55 -05001304 if (input->output->frame) {
1305 frame_pointer_leave(input->output->frame, input);
1306
1307 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1308 weston_output_schedule_repaint(&input->output->base);
1309 }
1310
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001311 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001312 input->output = NULL;
1313 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001314}
1315
1316static void
Daniel Stone37816df2012-05-16 18:45:18 +01001317input_handle_motion(void *data, struct wl_pointer *pointer,
1318 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1319{
1320 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001321 int32_t fx, fy;
1322 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001323
Jason Ekstrand7744f712013-10-27 22:24:55 -05001324 if (input->output->frame) {
1325 location = frame_pointer_motion(input->output->frame, input,
1326 wl_fixed_to_int(x),
1327 wl_fixed_to_int(y));
1328 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1329 x -= wl_fixed_from_int(fx);
1330 y -= wl_fixed_from_int(fy);
1331
1332 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1333 weston_output_schedule_repaint(&input->output->base);
1334 } else {
1335 location = THEME_LOCATION_CLIENT_AREA;
1336 }
1337
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001338 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001339
1340 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1341 input_set_cursor(input);
1342 notify_pointer_focus(&input->base, NULL, 0, 0);
1343 input->focus = 0;
1344 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1345 wl_pointer_set_cursor(input->parent.pointer,
1346 input->enter_serial, NULL, 0, 0);
1347 notify_pointer_focus(&input->base, &input->output->base, x, y);
1348 input->focus = 1;
1349 }
1350
1351 if (location == THEME_LOCATION_CLIENT_AREA)
1352 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001353}
1354
1355static void
1356input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001357 uint32_t serial, uint32_t time, uint32_t button,
1358 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001359{
1360 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001361 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001362 enum frame_button_state fstate;
1363 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001364
Jason Ekstrand7744f712013-10-27 22:24:55 -05001365 if (input->output->frame) {
1366 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1367 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1368
1369 location = frame_pointer_button(input->output->frame, input,
1370 button, fstate);
1371
1372 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1373
1374 wl_shell_surface_move(input->output->parent.shell_surface,
1375 input->parent.seat, serial);
1376 frame_status_clear(input->output->frame,
1377 FRAME_STATUS_MOVE);
1378 return;
1379 }
1380
1381 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1382 wl_display_terminate(input->compositor->base.wl_display);
1383
1384 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1385 weston_output_schedule_repaint(&input->output->base);
1386 } else {
1387 location = THEME_LOCATION_CLIENT_AREA;
1388 }
1389
1390 if (location == THEME_LOCATION_CLIENT_AREA)
1391 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001392}
1393
1394static void
1395input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001396 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001397{
1398 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001399
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001400 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001401}
1402
1403static const struct wl_pointer_listener pointer_listener = {
1404 input_handle_pointer_enter,
1405 input_handle_pointer_leave,
1406 input_handle_motion,
1407 input_handle_button,
1408 input_handle_axis,
1409};
1410
1411static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001412input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1413 int fd, uint32_t size)
1414{
1415 struct wayland_input *input = data;
1416 struct xkb_keymap *keymap;
1417 char *map_str;
1418
1419 if (!data) {
1420 close(fd);
1421 return;
1422 }
1423
1424 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1425 close(fd);
1426 return;
1427 }
1428
1429 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1430 if (map_str == MAP_FAILED) {
1431 close(fd);
1432 return;
1433 }
1434
1435 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1436 map_str,
1437 XKB_KEYMAP_FORMAT_TEXT_V1,
1438 0);
1439 munmap(map_str, size);
1440 close(fd);
1441
1442 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001443 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001444 return;
1445 }
1446
Rui Matos0c194ce2013-10-10 19:44:21 +02001447 if (input->base.keyboard)
1448 weston_seat_update_keymap(&input->base, keymap);
1449 else
1450 weston_seat_init_keyboard(&input->base, keymap);
1451
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001452 xkb_map_unref(keymap);
1453}
1454
1455static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001456input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001457 struct wl_keyboard *keyboard,
1458 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001459 struct wl_surface *surface,
1460 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001461{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001462 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001463 struct wayland_output *focus;
1464
1465 focus = input->keyboard_focus;
1466 if (focus) {
1467 /* This shouldn't happen */
1468 focus->keyboard_count--;
1469 if (!focus->keyboard_count && focus->frame)
1470 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1471 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1472 weston_output_schedule_repaint(&focus->base);
1473 }
1474
1475 input->keyboard_focus = wl_surface_get_user_data(surface);
1476 input->keyboard_focus->keyboard_count++;
1477
1478 focus = input->keyboard_focus;
1479 if (focus->frame) {
1480 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1481 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1482 weston_output_schedule_repaint(&focus->base);
1483 }
1484
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001485
Daniel Stone50692802012-06-22 13:21:41 +01001486 /* XXX: If we get a modifier event immediately before the focus,
1487 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001488 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001489 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001490}
1491
1492static void
1493input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001494 struct wl_keyboard *keyboard,
1495 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001496 struct wl_surface *surface)
1497{
1498 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001499 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001500
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001501 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001502
1503 focus = input->keyboard_focus;
1504 if (!focus)
1505 return; /* This shouldn't happen */
1506
1507 focus->keyboard_count--;
1508 if (!focus->keyboard_count && focus->frame) {
1509 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1510 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1511 weston_output_schedule_repaint(&focus->base);
1512 }
1513
1514 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001515}
1516
Daniel Stone37816df2012-05-16 18:45:18 +01001517static void
1518input_handle_key(void *data, struct wl_keyboard *keyboard,
1519 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1520{
1521 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001522
Daniel Stone50692802012-06-22 13:21:41 +01001523 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001524 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001525 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001526 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001527 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001528}
1529
Daniel Stone351eb612012-05-31 15:27:47 -04001530static void
1531input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001532 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001533 uint32_t mods_latched, uint32_t mods_locked,
1534 uint32_t group)
1535{
Daniel Stone50692802012-06-22 13:21:41 +01001536 struct wayland_input *input = data;
1537 struct wayland_compositor *c = input->compositor;
1538 uint32_t serial_out;
1539
1540 /* If we get a key event followed by a modifier event with the
1541 * same serial number, then we try to preserve those semantics by
1542 * reusing the same serial number on the way out too. */
1543 if (serial_in == input->key_serial)
1544 serial_out = wl_display_get_serial(c->base.wl_display);
1545 else
1546 serial_out = wl_display_next_serial(c->base.wl_display);
1547
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001548 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001549 mods_depressed, mods_latched,
1550 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001551 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001552}
1553
Daniel Stone37816df2012-05-16 18:45:18 +01001554static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001555 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001556 input_handle_keyboard_enter,
1557 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001558 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001559 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001560};
1561
1562static void
Daniel Stone37816df2012-05-16 18:45:18 +01001563input_handle_capabilities(void *data, struct wl_seat *seat,
1564 enum wl_seat_capability caps)
1565{
1566 struct wayland_input *input = data;
1567
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001568 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1569 input->parent.pointer = wl_seat_get_pointer(seat);
1570 wl_pointer_set_user_data(input->parent.pointer, input);
1571 wl_pointer_add_listener(input->parent.pointer,
1572 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001573 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001574 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1575 wl_pointer_destroy(input->parent.pointer);
1576 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001577 }
1578
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001579 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1580 input->parent.keyboard = wl_seat_get_keyboard(seat);
1581 wl_keyboard_set_user_data(input->parent.keyboard, input);
1582 wl_keyboard_add_listener(input->parent.keyboard,
1583 &keyboard_listener, input);
1584 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1585 wl_keyboard_destroy(input->parent.keyboard);
1586 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001587 }
1588}
1589
1590static const struct wl_seat_listener seat_listener = {
1591 input_handle_capabilities,
1592};
1593
1594static void
1595display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001596{
1597 struct wayland_input *input;
1598
Peter Huttererf3d62272013-08-08 11:57:05 +10001599 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001600 if (input == NULL)
1601 return;
1602
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001603 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001604 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001605 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1606 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001607 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001608
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001609 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1610 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001611
1612 input->parent.cursor.surface =
1613 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001614}
1615
1616static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001617wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1618 int32_t x, int32_t y,
1619 int32_t physical_width, int32_t physical_height,
1620 int32_t subpixel, const char *make,
1621 const char *model, int32_t transform)
1622{
1623 struct wayland_parent_output *output = data;
1624
1625 output->x = x;
1626 output->y = y;
1627 output->physical.width = physical_width;
1628 output->physical.height = physical_height;
1629 output->physical.subpixel = subpixel;
1630
1631 free(output->physical.make);
1632 output->physical.make = strdup(make);
1633 free(output->physical.model);
1634 output->physical.model = strdup(model);
1635
1636 output->transform = transform;
1637}
1638
1639static struct weston_mode *
1640find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1641{
1642 struct weston_mode *mode;
1643
1644 wl_list_for_each(mode, list, link) {
1645 if (mode->width == width && mode->height == height &&
1646 mode->refresh == refresh)
1647 return mode;
1648 }
1649
1650 mode = zalloc(sizeof *mode);
1651 if (!mode)
1652 return NULL;
1653
1654 mode->width = width;
1655 mode->height = height;
1656 mode->refresh = refresh;
1657 wl_list_insert(list, &mode->link);
1658
1659 return mode;
1660}
1661
1662static void
1663wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1664 uint32_t flags, int32_t width, int32_t height,
1665 int32_t refresh)
1666{
1667 struct wayland_parent_output *output = data;
1668 struct weston_mode *mode;
1669
1670 if (output->output) {
1671 mode = find_mode(&output->output->base.mode_list,
1672 width, height, refresh);
1673 if (!mode)
1674 return;
1675 mode->flags = flags;
1676 /* Do a mode-switch on current mode change? */
1677 } else {
1678 mode = find_mode(&output->mode_list, width, height, refresh);
1679 if (!mode)
1680 return;
1681 mode->flags = flags;
1682 if (flags & WL_OUTPUT_MODE_CURRENT)
1683 output->current_mode = mode;
1684 if (flags & WL_OUTPUT_MODE_PREFERRED)
1685 output->preferred_mode = mode;
1686 }
1687}
1688
1689static const struct wl_output_listener output_listener = {
1690 wayland_parent_output_geometry,
1691 wayland_parent_output_mode
1692};
1693
1694static void
1695wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1696{
1697 struct wayland_parent_output *output;
1698
1699 output = zalloc(sizeof *output);
1700 if (!output)
1701 return;
1702
1703 output->id = id;
1704 output->global = wl_registry_bind(c->parent.registry, id,
1705 &wl_output_interface, 1);
1706 if (!output->global)
1707 return;
1708 wl_output_add_listener(output->global, &output_listener, output);
1709
1710 output->scale = 0;
1711 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1712 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1713 wl_list_init(&output->mode_list);
1714 wl_list_insert(&c->parent.output_list, &output->link);
1715
1716 if (c->parent.fshell) {
1717 wl_display_roundtrip(c->parent.wl_display);
1718 wayland_output_create_for_parent_output(c, output);
1719 }
1720}
1721
1722static void
1723wayland_parent_output_destroy(struct wayland_parent_output *output)
1724{
1725 struct weston_mode *mode, *next;
1726
1727 if (output->output)
1728 wayland_output_destroy(&output->output->base);
1729
1730 wl_output_destroy(output->global);
1731 free(output->physical.make);
1732 free(output->physical.model);
1733
1734 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1735 wl_list_remove(&mode->link);
1736 free(mode);
1737 }
1738}
1739
1740static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001741registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1742 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001743{
1744 struct wayland_compositor *c = data;
1745
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001746 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001747 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001748 wl_registry_bind(registry, name,
1749 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001750 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001751 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001752 wl_registry_bind(registry, name,
1753 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001754 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1755 c->parent.fshell =
1756 wl_registry_bind(registry, name,
1757 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001758 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001759 display_add_seat(c, name);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001760 } else if (strcmp(interface, "wl_output") == 0) {
1761 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001762 } else if (strcmp(interface, "wl_shm") == 0) {
1763 c->parent.shm =
1764 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001765 }
1766}
1767
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001768static void
1769registry_handle_global_remove(void *data, struct wl_registry *registry,
1770 uint32_t name)
1771{
1772 struct wayland_compositor *c = data;
1773 struct wayland_parent_output *output;
1774
1775 wl_list_for_each(output, &c->parent.output_list, link)
1776 if (output->id == name)
1777 wayland_parent_output_destroy(output);
1778}
1779
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001780static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001781 registry_handle_global,
1782 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001783};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001784
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001785static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001786wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1787{
1788 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001789 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001790
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001791 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1792 wl_display_terminate(c->base.wl_display);
1793 return 0;
1794 }
1795
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001796 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001797 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001798 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001799 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001800
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001801 if (mask == 0) {
1802 count = wl_display_dispatch_pending(c->parent.wl_display);
1803 wl_display_flush(c->parent.wl_display);
1804 }
1805
1806 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001807}
1808
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001809static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001810wayland_restore(struct weston_compositor *ec)
1811{
1812}
1813
1814static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001815wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001816{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001817 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1818
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001819 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001820
Jonas Ådahle5a12252013-04-05 23:07:11 +02001821 if (c->parent.shm)
1822 wl_shm_destroy(c->parent.shm);
1823
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001824 free(ec);
1825}
1826
Jason Ekstrand7744f712013-10-27 22:24:55 -05001827static const char *left_ptrs[] = {
1828 "left_ptr",
1829 "default",
1830 "top_left_arrow",
1831 "left-arrow"
1832};
1833
1834static void
1835create_cursor(struct wayland_compositor *c, struct weston_config *config)
1836{
1837 struct weston_config_section *s;
1838 int size;
1839 char *theme = NULL;
1840 unsigned int i;
1841
1842 s = weston_config_get_section(config, "shell", NULL, NULL);
1843 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1844 weston_config_section_get_int(s, "cursor-size", &size, 32);
1845
1846 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001847 if (!c->cursor_theme) {
1848 fprintf(stderr, "could not load cursor theme\n");
1849 return;
1850 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001851
U. Artie Eoffff755002014-01-17 12:36:58 -08001852 free(theme);
1853
Jason Ekstrand7744f712013-10-27 22:24:55 -05001854 c->cursor = NULL;
1855 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1856 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1857 left_ptrs[i]);
1858 if (!c->cursor) {
1859 fprintf(stderr, "could not load left cursor\n");
1860 return;
1861 }
1862}
1863
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001864static void
1865fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1866 void *data)
1867{
1868 struct wayland_compositor *c = data;
1869 struct wayland_input *input = NULL;
1870
1871 wl_list_for_each(input, &c->input_list, link)
1872 if (&input->base == seat_base)
1873 break;
1874
1875 if (!input || !input->output)
1876 return;
1877
1878 if (input->output->frame)
1879 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1880 else
1881 wayland_output_set_windowed(input->output);
1882
1883 weston_output_schedule_repaint(&input->output->base);
1884}
1885
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001886static struct wayland_compositor *
1887wayland_compositor_create(struct wl_display *display, int use_pixman,
1888 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001889 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001890{
1891 struct wayland_compositor *c;
1892 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001893 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001894
Peter Huttererf3d62272013-08-08 11:57:05 +10001895 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001896 if (c == NULL)
1897 return NULL;
1898
Daniel Stone725c2c32012-06-22 14:04:36 +01001899 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001900 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001901 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001902
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001903 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001904
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001905 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001906 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001907 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001908 }
1909
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001910 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001911 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001912 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1913 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001914 wl_display_roundtrip(c->parent.wl_display);
1915
1916 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001917
1918 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001919
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001920 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001921
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001922 if (!c->use_pixman) {
1923 gl_renderer = weston_load_module("gl-renderer.so",
1924 "gl_renderer_interface");
1925 if (!gl_renderer)
1926 c->use_pixman = 1;
1927 }
1928
1929 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001930 if (gl_renderer->create(&c->base, c->parent.wl_display,
1931 gl_renderer->alpha_attribs,
1932 NULL) < 0) {
1933 weston_log("Failed to initialize the GL renderer; "
1934 "falling back to pixman.\n");
1935 c->use_pixman = 1;
1936 }
1937 }
1938
1939 if (c->use_pixman) {
1940 if (pixman_renderer_init(&c->base) < 0) {
1941 weston_log("Failed to initialize pixman renderer\n");
1942 goto err_display;
1943 }
1944 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001945
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001946 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001947 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001948
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001949 loop = wl_display_get_event_loop(c->base.wl_display);
1950
1951 fd = wl_display_get_fd(c->parent.wl_display);
1952 c->parent.wl_source =
1953 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1954 wayland_compositor_handle_event, c);
1955 if (c->parent.wl_source == NULL)
1956 goto err_renderer;
1957
1958 wl_event_source_check(c->parent.wl_source);
1959
1960 return c;
1961err_renderer:
1962 c->base.renderer->destroy(&c->base);
1963err_display:
1964 wl_display_disconnect(c->parent.wl_display);
1965err_compositor:
1966 weston_compositor_shutdown(&c->base);
1967err_free:
1968 free(c);
1969 return NULL;
1970}
1971
1972static void
1973wayland_compositor_destroy(struct wayland_compositor *c)
1974{
1975 struct weston_output *output;
1976
1977 wl_list_for_each(output, &c->base.output_list, link)
1978 wayland_output_destroy(output);
1979
1980 c->base.renderer->destroy(&c->base);
1981 wl_display_disconnect(c->parent.wl_display);
1982
1983 if (c->theme)
1984 theme_destroy(c->theme);
1985 if (c->frame_device)
1986 cairo_device_destroy(c->frame_device);
1987 wl_cursor_theme_destroy(c->cursor_theme);
1988
1989 weston_compositor_shutdown(&c->base);
1990 free(c);
1991}
1992
1993WL_EXPORT struct weston_compositor *
1994backend_init(struct wl_display *display, int *argc, char *argv[],
1995 struct weston_config *config)
1996{
1997 struct wayland_compositor *c;
1998 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001999 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002000 struct weston_config_section *section;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002001 int x, count, width, height, scale, use_pixman, fullscreen;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002002 const char *section_name, *display_name;
2003 char *name;
2004
2005 const struct weston_option wayland_options[] = {
2006 { WESTON_OPTION_INTEGER, "width", 0, &width },
2007 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002008 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002009 { WESTON_OPTION_STRING, "display", 0, &display_name },
2010 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2011 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002012 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002013 };
2014
2015 width = 0;
2016 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002017 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002018 display_name = NULL;
2019 use_pixman = 0;
2020 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002021 fullscreen = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002022 parse_options(wayland_options,
2023 ARRAY_LENGTH(wayland_options), argc, argv);
2024
2025 c = wayland_compositor_create(display, use_pixman, display_name,
2026 argc, argv, config);
2027 if (!c)
2028 return NULL;
2029
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002030 if (c->parent.fshell) {
2031 wl_display_roundtrip(c->parent.wl_display);
2032
2033 wl_list_for_each(poutput, &c->parent.output_list, link)
2034 wayland_output_create_for_parent_output(c, poutput);
2035
2036 return &c->base;
2037 }
2038
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002039 if (fullscreen) {
2040 output = wayland_output_create(c, 0, 0, width, height,
2041 NULL, 1, 0, 1);
2042 if (!output)
2043 goto err_outputs;
2044
Axel Davydd8b88d2013-11-17 21:34:16 +01002045 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002046 return &c->base;
2047 }
2048
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002049 section = NULL;
2050 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002051 while (weston_config_next_section(config, &section, &section_name)) {
2052 if (!section_name || strcmp(section_name, "output") != 0)
2053 continue;
2054 weston_config_section_get_string(section, "name", &name, NULL);
2055 if (name == NULL)
2056 continue;
2057
2058 if (name[0] != 'W' || name[1] != 'L') {
2059 free(name);
2060 continue;
2061 }
2062 free(name);
2063
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002064 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002065 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002066 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002067 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002068 if (wayland_output_set_windowed(output))
2069 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002070
2071 x += output->base.width;
2072 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002073 }
2074
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002075 if (!width)
2076 width = 1024;
2077 if (!height)
2078 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002079 if (!scale)
2080 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002081 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002082 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002083 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002084 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002085 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002086 if (wayland_output_set_windowed(output))
2087 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002088
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002089 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002090 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002091 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002092
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002093 weston_compositor_add_key_binding(&c->base, KEY_F,
2094 MODIFIER_CTRL | MODIFIER_ALT,
2095 fullscreen_binding, c);
2096
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002097 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002098
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002099err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002100 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002101 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002102}