blob: e9e671341901fc64801832f41d8d3127f0bbcc9e [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;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050067 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050068
Jason Ekstrand7744f712013-10-27 22:24:55 -050069 struct theme *theme;
70 cairo_device_t *frame_device;
71 struct wl_cursor_theme *cursor_theme;
72 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050073
Jason Ekstrand06ced802013-11-07 20:13:29 -060074 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075};
76
77struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050078 struct weston_output base;
79
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010080 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050081 int draw_initial_frame;
82 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050083
84 struct wl_output *output;
85 uint32_t global_id;
86
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050087 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060088 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010089 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050090
Jason Ekstrand7744f712013-10-27 22:24:55 -050091 int keyboard_count;
92
Jason Ekstrand5ea04802013-11-07 20:13:33 -060093 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050094 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050095
Jason Ekstrand7744f712013-10-27 22:24:55 -050096 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050097 struct wl_egl_window *egl_window;
98 struct {
99 cairo_surface_t *top;
100 cairo_surface_t *left;
101 cairo_surface_t *right;
102 cairo_surface_t *bottom;
103 } border;
104 } gl;
105
106 struct {
107 struct wl_list buffers;
108 struct wl_list free_buffers;
109 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500110
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500111 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500112 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100113};
114
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500115struct wayland_parent_output {
116 struct wayland_output *output;
117 struct wl_list link;
118
119 struct wl_output *global;
120 uint32_t id;
121
122 struct {
123 char *make;
124 char *model;
125 int32_t width, height;
126 uint32_t subpixel;
127 } physical;
128
129 int32_t x, y;
130 uint32_t transform;
131 uint32_t scale;
132
133 struct wl_list mode_list;
134 struct weston_mode *preferred_mode;
135 struct weston_mode *current_mode;
136};
137
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500138struct wayland_shm_buffer {
139 struct wayland_output *output;
140 struct wl_list link;
141 struct wl_list free_link;
142
143 struct wl_buffer *buffer;
144 void *data;
145 size_t size;
146 pixman_region32_t damage;
147 int frame_damaged;
148
149 pixman_image_t *pm_image;
150 cairo_surface_t *c_surface;
151};
152
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100153struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400154 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100155 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100156 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500157
158 struct {
159 struct wl_seat *seat;
160 struct wl_pointer *pointer;
161 struct wl_keyboard *keyboard;
162 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500163
164 struct {
165 struct wl_surface *surface;
166 int32_t hx, hy;
167 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500168 } parent;
169
Daniel Stone50692802012-06-22 13:21:41 +0100170 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400171 uint32_t enter_serial;
172 int focus;
173 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500174 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100175};
176
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300177struct gl_renderer_interface *gl_renderer;
178
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500179static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500180wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
181{
182 cairo_surface_destroy(buffer->c_surface);
183 pixman_image_unref(buffer->pm_image);
184
185 wl_buffer_destroy(buffer->buffer);
186 munmap(buffer->data, buffer->size);
187
188 pixman_region32_fini(&buffer->damage);
189
190 wl_list_remove(&buffer->link);
191 wl_list_remove(&buffer->free_link);
192 free(buffer);
193}
194
195static void
196buffer_release(void *data, struct wl_buffer *buffer)
197{
198 struct wayland_shm_buffer *sb = data;
199
200 if (sb->output) {
201 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
202 } else {
203 wayland_shm_buffer_destroy(sb);
204 }
205}
206
207static const struct wl_buffer_listener buffer_listener = {
208 buffer_release
209};
210
211static struct wayland_shm_buffer *
212wayland_output_get_shm_buffer(struct wayland_output *output)
213{
214 struct wayland_compositor *c =
215 (struct wayland_compositor *) output->base.compositor;
216 struct wl_shm *shm = c->parent.shm;
217 struct wayland_shm_buffer *sb;
218
219 struct wl_shm_pool *pool;
220 int width, height, stride;
221 int32_t fx, fy;
222 int fd;
223 unsigned char *data;
224
225 if (!wl_list_empty(&output->shm.free_buffers)) {
226 sb = container_of(output->shm.free_buffers.next,
227 struct wayland_shm_buffer, free_link);
228 wl_list_remove(&sb->free_link);
229 wl_list_init(&sb->free_link);
230
231 return sb;
232 }
233
234 if (output->frame) {
235 width = frame_width(output->frame);
236 height = frame_height(output->frame);
237 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500238 width = output->base.current_mode->width;
239 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500240 }
241
242 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
243
244 fd = os_create_anonymous_file(height * stride);
245 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000246 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500247 return NULL;
248 }
249
250 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
251 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000252 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500253 close(fd);
254 return NULL;
255 }
256
257 sb = zalloc(sizeof *sb);
258
259 sb->output = output;
260 wl_list_init(&sb->free_link);
261 wl_list_insert(&output->shm.buffers, &sb->link);
262
263 pixman_region32_init_rect(&sb->damage, 0, 0,
264 output->base.width, output->base.height);
265 sb->frame_damaged = 1;
266
267 sb->data = data;
268 sb->size = height * stride;
269
270 pool = wl_shm_create_pool(shm, fd, sb->size);
271
272 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
273 width, height,
274 stride,
275 WL_SHM_FORMAT_ARGB8888);
276 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
277 wl_shm_pool_destroy(pool);
278 close(fd);
279
280 memset(data, 0, sb->size);
281
282 sb->c_surface =
283 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
284 width, height, stride);
285
286 fx = 0;
287 fy = 0;
288 if (output->frame)
289 frame_interior(output->frame, &fx, &fy, 0, 0);
290 sb->pm_image =
291 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
292 (uint32_t *)(data + fy * stride) + fx,
293 stride);
294
295 return sb;
296}
297
298static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500299frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400300{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500301 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400302
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500303 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500304 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400305}
306
307static const struct wl_callback_listener frame_listener = {
308 frame_done
309};
310
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500311static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200312draw_initial_frame(struct wayland_output *output)
313{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500314 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200315
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500316 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200317
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500318 /* If we are rendering with GL, then orphan it so that it gets
319 * destroyed immediately */
320 if (output->gl.egl_window)
321 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500322
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500323 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500324 wl_surface_damage(output->parent.surface, 0, 0,
325 output->base.current_mode->width,
326 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200327}
328
329static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500330wayland_output_update_gl_border(struct wayland_output *output)
331{
332 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
333 cairo_t *cr;
334
335 if (!output->frame)
336 return;
337 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
338 return;
339
340 fwidth = frame_width(output->frame);
341 fheight = frame_height(output->frame);
342 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
343
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500344 if (!output->gl.border.top)
345 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500346 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
347 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500348 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500349 frame_repaint(output->frame, cr);
350 cairo_destroy(cr);
351 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
352 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500353 cairo_image_surface_get_stride(output->gl.border.top) / 4,
354 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500355
356
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500357 if (!output->gl.border.left)
358 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500359 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
360 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500361 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500362 cairo_translate(cr, 0, -iy);
363 frame_repaint(output->frame, cr);
364 cairo_destroy(cr);
365 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
366 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500367 cairo_image_surface_get_stride(output->gl.border.left) / 4,
368 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500369
370
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500371 if (!output->gl.border.right)
372 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500373 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
374 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500375 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500376 cairo_translate(cr, -(iwidth + ix), -iy);
377 frame_repaint(output->frame, cr);
378 cairo_destroy(cr);
379 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
380 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500381 cairo_image_surface_get_stride(output->gl.border.right) / 4,
382 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500383
384
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500385 if (!output->gl.border.bottom)
386 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500387 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
388 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500389 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500390 cairo_translate(cr, 0, -(iy + iheight));
391 frame_repaint(output->frame, cr);
392 cairo_destroy(cr);
393 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
394 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
396 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500397}
398
399static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200400wayland_output_start_repaint_loop(struct weston_output *output_base)
401{
402 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500403 struct wayland_compositor *wc =
404 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200405 struct wl_callback *callback;
406
407 /* If this is the initial frame, we need to attach a buffer so that
408 * the compositor can map the surface and include it in its render
409 * loop. If the surface doesn't end up in the render loop, the frame
410 * callback won't be invoked. The buffer is transparent and of the
411 * same size as the future real output buffer. */
412 if (output->parent.draw_initial_frame) {
413 output->parent.draw_initial_frame = 0;
414
415 draw_initial_frame(output);
416 }
417
418 callback = wl_surface_frame(output->parent.surface);
419 wl_callback_add_listener(callback, &frame_listener, output);
420 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500421 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200422}
423
David Herrmann1edf44c2013-10-22 17:11:26 +0200424static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500425wayland_output_repaint_gl(struct weston_output *output_base,
426 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400427{
428 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400429 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400430 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600431
Kristian Høgsberg33418202011-08-16 23:01:28 -0400432 callback = wl_surface_frame(output->parent.surface);
433 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100434
Jason Ekstrand7744f712013-10-27 22:24:55 -0500435 wayland_output_update_gl_border(output);
436
Pekka Paalanenbc106382012-10-10 12:49:31 +0300437 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200438
439 pixman_region32_subtract(&ec->primary_plane.damage,
440 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200441 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100442}
443
Matt Roper361d2ad2011-08-29 13:52:23 -0700444static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
446{
447 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
448 cairo_t *cr;
449
450 if (!buffer->output->frame || !buffer->frame_damaged)
451 return;
452
453 cr = cairo_create(buffer->c_surface);
454
455 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
456 fwidth = frame_width(buffer->output->frame);
457 fheight = frame_height(buffer->output->frame);
458
459 /* Set the clip so we don't unnecisaraly damage the surface */
460 cairo_move_to(cr, ix, iy);
461 cairo_rel_line_to(cr, iwidth, 0);
462 cairo_rel_line_to(cr, 0, iheight);
463 cairo_rel_line_to(cr, -iwidth, 0);
464 cairo_line_to(cr, ix, iy);
465 cairo_line_to(cr, 0, iy);
466 cairo_line_to(cr, 0, fheight);
467 cairo_line_to(cr, fwidth, fheight);
468 cairo_line_to(cr, fwidth, 0);
469 cairo_line_to(cr, 0, 0);
470 cairo_line_to(cr, 0, iy);
471 cairo_close_path(cr);
472 cairo_clip(cr);
473
474 /* Draw using a pattern so that the final result gets clipped */
475 cairo_push_group(cr);
476 frame_repaint(buffer->output->frame, cr);
477 cairo_pop_group_to_source(cr);
478 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
479 cairo_paint(cr);
480
481 cairo_destroy(cr);
482}
483
484static void
485wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
486{
487 pixman_region32_t damage;
488 pixman_box32_t *rects;
489 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
490 int i, n;
491
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500492 pixman_region32_init(&damage);
493 weston_transformed_region(sb->output->base.width,
494 sb->output->base.height,
495 sb->output->base.transform,
496 sb->output->base.current_scale,
497 &sb->damage, &damage);
498
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500499 if (sb->output->frame) {
500 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
501 fwidth = frame_width(sb->output->frame);
502 fheight = frame_height(sb->output->frame);
503
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500504 pixman_region32_translate(&damage, ix, iy);
505
506 if (sb->frame_damaged) {
507 pixman_region32_union_rect(&damage, &damage,
508 0, 0, fwidth, iy);
509 pixman_region32_union_rect(&damage, &damage,
510 0, iy, ix, iheight);
511 pixman_region32_union_rect(&damage, &damage,
512 ix + iwidth, iy,
513 fwidth - (ix + iwidth), iheight);
514 pixman_region32_union_rect(&damage, &damage,
515 0, iy + iheight,
516 fwidth, fheight - (iy + iheight));
517 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500518 }
519
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500520 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500521 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
522 for (i = 0; i < n; ++i)
523 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
524 rects[i].y1, rects[i].x2 - rects[i].x1,
525 rects[i].y2 - rects[i].y1);
526
527 if (sb->output->frame)
528 pixman_region32_fini(&damage);
529}
530
531static int
532wayland_output_repaint_pixman(struct weston_output *output_base,
533 pixman_region32_t *damage)
534{
535 struct wayland_output *output = (struct wayland_output *) output_base;
536 struct wayland_compositor *c =
537 (struct wayland_compositor *)output->base.compositor;
538 struct wl_callback *callback;
539 struct wayland_shm_buffer *sb;
540
541 if (output->frame) {
542 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
543 wl_list_for_each(sb, &output->shm.buffers, link)
544 sb->frame_damaged = 1;
545 }
546
547 wl_list_for_each(sb, &output->shm.buffers, link)
548 pixman_region32_union(&sb->damage, &sb->damage, damage);
549
550 sb = wayland_output_get_shm_buffer(output);
551
552 wayland_output_update_shm_border(sb);
553 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
554 c->base.renderer->repaint_output(output_base, &sb->damage);
555
556 wayland_shm_buffer_attach(sb);
557
558 callback = wl_surface_frame(output->parent.surface);
559 wl_callback_add_listener(callback, &frame_listener, output);
560 wl_surface_commit(output->parent.surface);
561 wl_display_flush(c->parent.wl_display);
562
563 pixman_region32_fini(&sb->damage);
564 pixman_region32_init(&sb->damage);
565 sb->frame_damaged = 0;
566
567 pixman_region32_subtract(&c->base.primary_plane.damage,
568 &c->base.primary_plane.damage, damage);
569 return 0;
570}
571
572static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500573wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700574{
575 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500576 struct wayland_compositor *c =
577 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700578
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500579 if (c->use_pixman) {
580 pixman_renderer_output_destroy(output_base);
581 } else {
582 gl_renderer->output_destroy(output_base);
583 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100584
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500585 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500586 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500587 if (output->parent.shell_surface)
588 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500589
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600590 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500591 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600592
593 cairo_surface_destroy(output->gl.border.top);
594 cairo_surface_destroy(output->gl.border.left);
595 cairo_surface_destroy(output->gl.border.right);
596 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500597
598 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700599 free(output);
600
601 return;
602}
603
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300604static const struct wl_shell_surface_listener shell_surface_listener;
605
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200606static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500607wayland_output_init_gl_renderer(struct wayland_output *output)
608{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500609 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500610
611 if (output->frame) {
612 fwidth = frame_width(output->frame);
613 fheight = frame_height(output->frame);
614 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500615 fwidth = output->base.current_mode->width;
616 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500617 }
618
619 output->gl.egl_window =
620 wl_egl_window_create(output->parent.surface,
621 fwidth, fheight);
622 if (!output->gl.egl_window) {
623 weston_log("failure to create wl_egl_window\n");
624 return -1;
625 }
626
627 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000628 output->gl.egl_window,
629 gl_renderer->alpha_attribs,
630 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500631 goto cleanup_window;
632
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500633 return 0;
634
635cleanup_window:
636 wl_egl_window_destroy(output->gl.egl_window);
637 return -1;
638}
639
640static int
641wayland_output_init_pixman_renderer(struct wayland_output *output)
642{
643 return pixman_renderer_output_create(&output->base);
644}
645
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600646static void
647wayland_output_resize_surface(struct wayland_output *output)
648{
649 struct wayland_compositor *c =
650 (struct wayland_compositor *)output->base.compositor;
651 struct wayland_shm_buffer *buffer, *next;
652 int32_t ix, iy, iwidth, iheight;
653 int32_t width, height;
654 struct wl_region *region;
655
656 width = output->base.current_mode->width;
657 height = output->base.current_mode->height;
658
659 if (output->frame) {
660 frame_resize_inside(output->frame, width, height);
661
662 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
663 region = wl_compositor_create_region(c->parent.compositor);
664 wl_region_add(region, ix, iy, iwidth, iheight);
665 wl_surface_set_input_region(output->parent.surface, region);
666 wl_region_destroy(region);
667
668 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
669 region = wl_compositor_create_region(c->parent.compositor);
670 wl_region_add(region, ix, iy, iwidth, iheight);
671 wl_surface_set_opaque_region(output->parent.surface, region);
672 wl_region_destroy(region);
673
674 width = frame_width(output->frame);
675 height = frame_height(output->frame);
676 } else {
677 region = wl_compositor_create_region(c->parent.compositor);
678 wl_region_add(region, 0, 0, width, height);
679 wl_surface_set_input_region(output->parent.surface, region);
680 wl_region_destroy(region);
681
682 region = wl_compositor_create_region(c->parent.compositor);
683 wl_region_add(region, 0, 0, width, height);
684 wl_surface_set_opaque_region(output->parent.surface, region);
685 wl_region_destroy(region);
686 }
687
688 if (output->gl.egl_window) {
689 wl_egl_window_resize(output->gl.egl_window,
690 width, height, 0, 0);
691
692 /* These will need to be re-created due to the resize */
693 gl_renderer->output_set_border(&output->base,
694 GL_RENDERER_BORDER_TOP,
695 0, 0, 0, NULL);
696 cairo_surface_destroy(output->gl.border.top);
697 output->gl.border.top = NULL;
698 gl_renderer->output_set_border(&output->base,
699 GL_RENDERER_BORDER_LEFT,
700 0, 0, 0, NULL);
701 cairo_surface_destroy(output->gl.border.left);
702 output->gl.border.left = NULL;
703 gl_renderer->output_set_border(&output->base,
704 GL_RENDERER_BORDER_RIGHT,
705 0, 0, 0, NULL);
706 cairo_surface_destroy(output->gl.border.right);
707 output->gl.border.right = NULL;
708 gl_renderer->output_set_border(&output->base,
709 GL_RENDERER_BORDER_BOTTOM,
710 0, 0, 0, NULL);
711 cairo_surface_destroy(output->gl.border.bottom);
712 output->gl.border.bottom = NULL;
713 }
714
715 /* Throw away any remaining SHM buffers */
716 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
717 wayland_shm_buffer_destroy(buffer);
718 /* These will get thrown away when they get released */
719 wl_list_for_each(buffer, &output->shm.buffers, link)
720 buffer->output = NULL;
721}
722
723static int
724wayland_output_set_windowed(struct wayland_output *output)
725{
726 struct wayland_compositor *c =
727 (struct wayland_compositor *)output->base.compositor;
728 int tlen;
729 char *title;
730
731 if (output->frame)
732 return 0;
733
734 if (output->name) {
735 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
736 title = malloc(tlen + 1);
737 if (!title)
738 return -1;
739
740 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
741 } else {
742 title = strdup(WINDOW_TITLE);
743 }
744
745 if (!c->theme) {
746 c->theme = theme_create();
747 if (!c->theme) {
748 free(title);
749 return -1;
750 }
751 }
752 output->frame = frame_create(c->theme, 100, 100,
753 FRAME_BUTTON_CLOSE, title);
754 free(title);
755 if (!output->frame)
756 return -1;
757
758 if (output->keyboard_count)
759 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
760
761 wayland_output_resize_surface(output);
762
763 wl_shell_surface_set_toplevel(output->parent.shell_surface);
764
765 return 0;
766}
767
768static void
769wayland_output_set_fullscreen(struct wayland_output *output,
770 enum wl_shell_surface_fullscreen_method method,
771 uint32_t framerate, struct wl_output *target)
772{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500773 struct wayland_compositor *c =
774 (struct wayland_compositor *)output->base.compositor;
775
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600776 if (output->frame) {
777 frame_destroy(output->frame);
778 output->frame = NULL;
779 }
780
781 wayland_output_resize_surface(output);
782
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500783 if (output->parent.shell_surface) {
784 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
785 method, framerate, target);
786 } else if (c->parent.fshell) {
787 _wl_fullscreen_shell_present_surface(c->parent.fshell,
788 output->parent.surface,
789 method, target);
790 }
791}
792
793static struct weston_mode *
794wayland_output_choose_mode(struct wayland_output *output,
795 struct weston_mode *ref_mode)
796{
797 struct weston_mode *mode;
798
799 /* First look for an exact match */
800 wl_list_for_each(mode, &output->base.mode_list, link)
801 if (mode->width == ref_mode->width &&
802 mode->height == ref_mode->height &&
803 mode->refresh == ref_mode->refresh)
804 return mode;
805
806 /* If we can't find an exact match, ignore refresh and try again */
807 wl_list_for_each(mode, &output->base.mode_list, link)
808 if (mode->width == ref_mode->width &&
809 mode->height == ref_mode->height)
810 return mode;
811
812 /* Yeah, we failed */
813 return NULL;
814}
815
816enum mode_status {
817 MODE_STATUS_UNKNOWN,
818 MODE_STATUS_SUCCESS,
819 MODE_STATUS_FAIL,
820 MODE_STATUS_CANCEL,
821};
822
823static void
824mode_feedback_successful(void *data,
825 struct _wl_fullscreen_shell_mode_feedback *fb)
826{
827 enum mode_status *value = data;
828
829 printf("Mode switch successful\n");
830
831 *value = MODE_STATUS_SUCCESS;
832}
833
834static void
835mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
836{
837 enum mode_status *value = data;
838
839 printf("Mode switch failed\n");
840
841 *value = MODE_STATUS_FAIL;
842}
843
844static void
845mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
846{
847 enum mode_status *value = data;
848
849 printf("Mode switch cancelled\n");
850
851 *value = MODE_STATUS_CANCEL;
852}
853
854struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
855 mode_feedback_successful,
856 mode_feedback_failed,
857 mode_feedback_cancelled,
858};
859
860static int
861wayland_output_switch_mode(struct weston_output *output_base,
862 struct weston_mode *mode)
863{
864 struct wayland_output *output = (struct wayland_output *) output_base;
865 struct wayland_compositor *c;
866 struct wl_surface *old_surface;
867 struct weston_mode *old_mode;
868 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
869 enum mode_status mode_status;
870 int ret = 0;
871
872 if (output_base == NULL) {
873 weston_log("output is NULL.\n");
874 return -1;
875 }
876
877 if (mode == NULL) {
878 weston_log("mode is NULL.\n");
879 return -1;
880 }
881
882 c = (struct wayland_compositor *)output_base->compositor;
883
884 if (output->parent.shell_surface || !c->parent.fshell)
885 return -1;
886
887 mode = wayland_output_choose_mode(output, mode);
888 if (mode == NULL)
889 return -1;
890
891 if (output->base.current_mode == mode)
892 return 0;
893
894 old_mode = output->base.current_mode;
895 old_surface = output->parent.surface;
896 output->base.current_mode = mode;
897 output->parent.surface =
898 wl_compositor_create_surface(c->parent.compositor);
899 wl_surface_set_user_data(output->parent.surface, output);
900
901 /* Blow the old buffers because we changed size/surfaces */
902 wayland_output_resize_surface(output);
903
904 mode_feedback =
905 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
906 output->parent.surface,
907 output->parent.output,
908 mode->refresh);
909 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
910 &mode_feedback_listener,
911 &mode_status);
912
913 /* This should kick-start things again */
914 output->parent.draw_initial_frame = 1;
915 wayland_output_start_repaint_loop(&output->base);
916
917 mode_status = MODE_STATUS_UNKNOWN;
918 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
919 ret = wl_display_dispatch(c->parent.wl_display);
920
921 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
922
923 if (mode_status == MODE_STATUS_FAIL) {
924 output->base.current_mode = old_mode;
925 wl_surface_destroy(output->parent.surface);
926 output->parent.surface = old_surface;
927 wayland_output_resize_surface(output);
928
929 return -1;
930 }
931
932 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
933 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
934
935 if (c->use_pixman) {
936 pixman_renderer_output_destroy(output_base);
937 if (wayland_output_init_pixman_renderer(output) < 0)
938 goto err_output;
939 } else {
940 gl_renderer->output_destroy(output_base);
941 wl_egl_window_destroy(output->gl.egl_window);
942 if (wayland_output_init_gl_renderer(output) < 0)
943 goto err_output;
944 }
945 wl_surface_destroy(old_surface);
946
947 weston_output_schedule_repaint(&output->base);
948
949 return 0;
950
951err_output:
952 /* XXX */
953 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600954}
955
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500956static struct wayland_output *
957wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600958 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500959 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100960{
961 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500962 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600963
964 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
965 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100966
Peter Huttererf3d62272013-08-08 11:57:05 +1000967 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100968 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500969 return NULL;
970
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600971 output->name = name ? strdup(name) : NULL;
972 output->base.make = "waywayland";
973 output->base.model = "none";
974
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500975 output_width = width * scale;
976 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100977
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600978 output->parent.surface =
979 wl_compositor_create_surface(c->parent.compositor);
980 if (!output->parent.surface)
981 goto err_name;
982 wl_surface_set_user_data(output->parent.surface, output);
983
984 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600985
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500986 if (c->parent.shell) {
987 output->parent.shell_surface =
988 wl_shell_get_shell_surface(c->parent.shell,
989 output->parent.surface);
990 if (!output->parent.shell_surface)
991 goto err_surface;
992 wl_shell_surface_add_listener(output->parent.shell_surface,
993 &shell_surface_listener, output);
994 }
995
996 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600997 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
998 0, 0, NULL);
999 wl_display_roundtrip(c->parent.wl_display);
1000 if (!width)
1001 output_width = output->parent.configure_width;
1002 if (!height)
1003 output_height = output->parent.configure_height;
1004 }
1005
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001006 output->mode.flags =
1007 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001008 output->mode.width = output_width;
1009 output->mode.height = output_height;
1010 output->mode.refresh = 60000;
1011 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001012 wl_list_init(&output->base.mode_list);
1013 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001014 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001015
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001016 wl_list_init(&output->shm.buffers);
1017 wl_list_init(&output->shm.free_buffers);
1018
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001019 weston_output_init(&output->base, &c->base, x, y, width, height,
1020 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001021
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001022 if (c->use_pixman) {
1023 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001024 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001025 output->base.repaint = wayland_output_repaint_pixman;
1026 } else {
1027 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001028 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001029 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001030 }
1031
Jonas Ådahle5a12252013-04-05 23:07:11 +02001032 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001033 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001034 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001035 output->base.set_backlight = NULL;
1036 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001037 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001039 wl_list_insert(c->base.output_list.prev, &output->base.link);
1040
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001041 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001042
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001043err_output:
1044 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001045 if (output->parent.shell_surface)
1046 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001047err_surface:
1048 wl_surface_destroy(output->parent.surface);
1049err_name:
1050 free(output->name);
1051
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001052 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001053 free(output);
1054
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001055 return NULL;
1056}
1057
1058static struct wayland_output *
1059wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001060 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001061 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001062 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001063{
1064 struct wayland_output *output;
1065 char *mode, *t, *name, *str;
1066 int width, height, scale;
1067 uint32_t transform;
1068 unsigned int i, slen;
1069
1070 static const struct { const char *name; uint32_t token; } transform_names[] = {
1071 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1072 { "90", WL_OUTPUT_TRANSFORM_90 },
1073 { "180", WL_OUTPUT_TRANSFORM_180 },
1074 { "270", WL_OUTPUT_TRANSFORM_270 },
1075 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1076 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1077 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1078 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1079 };
1080
1081 weston_config_section_get_string(config_section, "name", &name, NULL);
1082 if (name) {
1083 slen = strlen(name);
1084 slen += strlen(WINDOW_TITLE " - ");
1085 str = malloc(slen + 1);
1086 if (str)
1087 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1088 free(name);
1089 name = str;
1090 }
1091 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001092 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001093
1094 weston_config_section_get_string(config_section,
1095 "mode", &mode, "1024x600");
1096 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1097 weston_log("Invalid mode \"%s\" for output %s\n",
1098 mode, name);
1099 width = 1024;
1100 height = 640;
1101 }
1102 free(mode);
1103
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001104 if (option_width)
1105 width = option_width;
1106 if (option_height)
1107 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001108
1109 weston_config_section_get_int(config_section, "scale", &scale, 1);
1110
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001111 if (option_scale)
1112 scale = option_scale;
1113
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001114 weston_config_section_get_string(config_section,
1115 "transform", &t, "normal");
1116 transform = WL_OUTPUT_TRANSFORM_NORMAL;
1117 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
1118 if (strcmp(transform_names[i].name, t) == 0) {
1119 transform = transform_names[i].token;
1120 break;
1121 }
1122 }
1123 if (i >= ARRAY_LENGTH(transform_names))
1124 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
1125 free(t);
1126
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001127 output = wayland_output_create(c, x, y, width, height, name, 0,
1128 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001129 free(name);
1130
1131 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001132}
1133
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001134static struct wayland_output *
1135wayland_output_create_for_parent_output(struct wayland_compositor *c,
1136 struct wayland_parent_output *poutput)
1137{
1138 struct wayland_output *output;
1139 struct weston_mode *mode;
1140 int32_t x;
1141
1142 if (poutput->current_mode) {
1143 mode = poutput->current_mode;
1144 } else if (poutput->preferred_mode) {
1145 mode = poutput->current_mode;
1146 } else if (!wl_list_empty(&poutput->mode_list)) {
1147 mode = container_of(poutput->mode_list.next,
1148 struct weston_mode, link);
1149 } else {
1150 weston_log("No valid modes found. Skipping output");
1151 return NULL;
1152 }
1153
1154 if (!wl_list_empty(&c->base.output_list)) {
1155 output = container_of(c->base.output_list.prev,
1156 struct wayland_output, base.link);
1157 x = output->base.x + output->base.current_mode->width;
1158 } else {
1159 x = 0;
1160 }
1161
1162 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1163 NULL, 0,
1164 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1165 if (!output)
1166 return NULL;
1167
1168 output->parent.output = poutput->global;
1169
1170 output->base.make = poutput->physical.make;
1171 output->base.model = poutput->physical.model;
1172 wl_list_init(&output->base.mode_list);
1173 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1174 wl_list_init(&poutput->mode_list);
1175
1176 wayland_output_set_fullscreen(output,
1177 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1178 mode->refresh, poutput->global);
1179
1180 if (output->parent.shell_surface) {
1181 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1182 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1183 mode->refresh, poutput->global);
1184 } else if (c->parent.fshell) {
1185 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1186 output->parent.surface,
1187 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1188 poutput->global);
1189 _wl_fullscreen_shell_mode_feedback_destroy(
1190 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1191 output->parent.surface,
1192 poutput->global,
1193 mode->refresh));
1194 }
1195
1196 return output;
1197}
1198
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001199static void
1200shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1201 uint32_t serial)
1202{
1203 wl_shell_surface_pong(shell_surface, serial);
1204}
1205
1206static void
1207shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1208 uint32_t edges, int32_t width, int32_t height)
1209{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001210 struct wayland_output *output = data;
1211
1212 output->parent.configure_width = width;
1213 output->parent.configure_height = height;
1214
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001215 /* FIXME: implement resizing */
1216}
1217
1218static void
1219shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1220{
1221}
1222
1223static const struct wl_shell_surface_listener shell_surface_listener = {
1224 shell_surface_ping,
1225 shell_surface_configure,
1226 shell_surface_popup_done
1227};
1228
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001229/* Events received from the wayland-server this compositor is client of: */
1230
Jason Ekstrand7744f712013-10-27 22:24:55 -05001231/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001232static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001233input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001234{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001235
Jason Ekstrand7744f712013-10-27 22:24:55 -05001236 struct wl_buffer *buffer;
1237 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001238
Jason Ekstrand7744f712013-10-27 22:24:55 -05001239 if (!input->compositor->cursor)
1240 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001241
Jason Ekstrand7744f712013-10-27 22:24:55 -05001242 image = input->compositor->cursor->images[0];
1243 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001244 if (!buffer)
1245 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001246
1247 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1248 input->parent.cursor.surface,
1249 image->hotspot_x, image->hotspot_y);
1250
1251 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1252 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1253 image->width, image->height);
1254 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001255}
1256
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001257static void
Daniel Stone37816df2012-05-16 18:45:18 +01001258input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1259 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001260 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001261{
1262 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001263 int32_t fx, fy;
1264 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001265
Daniel Stone50692802012-06-22 13:21:41 +01001266 /* XXX: If we get a modifier event immediately before the focus,
1267 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001268 input->enter_serial = serial;
1269 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001270
1271 if (input->output->frame) {
1272 location = frame_pointer_enter(input->output->frame, input,
1273 wl_fixed_to_int(x),
1274 wl_fixed_to_int(y));
1275 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1276 x -= wl_fixed_from_int(fx);
1277 y -= wl_fixed_from_int(fy);
1278
1279 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1280 weston_output_schedule_repaint(&input->output->base);
1281 } else {
1282 location = THEME_LOCATION_CLIENT_AREA;
1283 }
1284
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001285 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001286
1287 if (location == THEME_LOCATION_CLIENT_AREA) {
1288 input->focus = 1;
1289 notify_pointer_focus(&input->base, &input->output->base, x, y);
1290 wl_pointer_set_cursor(input->parent.pointer,
1291 input->enter_serial, NULL, 0, 0);
1292 } else {
1293 input->focus = 0;
1294 notify_pointer_focus(&input->base, NULL, 0, 0);
1295 input_set_cursor(input);
1296 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001297}
1298
1299static void
Daniel Stone37816df2012-05-16 18:45:18 +01001300input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1301 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001302{
1303 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001304
Jason Ekstrand7744f712013-10-27 22:24:55 -05001305 if (input->output->frame) {
1306 frame_pointer_leave(input->output->frame, input);
1307
1308 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1309 weston_output_schedule_repaint(&input->output->base);
1310 }
1311
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001312 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001313 input->output = NULL;
1314 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001315}
1316
1317static void
Daniel Stone37816df2012-05-16 18:45:18 +01001318input_handle_motion(void *data, struct wl_pointer *pointer,
1319 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1320{
1321 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001322 int32_t fx, fy;
1323 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001324
Jason Ekstrand7744f712013-10-27 22:24:55 -05001325 if (input->output->frame) {
1326 location = frame_pointer_motion(input->output->frame, input,
1327 wl_fixed_to_int(x),
1328 wl_fixed_to_int(y));
1329 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1330 x -= wl_fixed_from_int(fx);
1331 y -= wl_fixed_from_int(fy);
1332
1333 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1334 weston_output_schedule_repaint(&input->output->base);
1335 } else {
1336 location = THEME_LOCATION_CLIENT_AREA;
1337 }
1338
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001339 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001340
1341 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1342 input_set_cursor(input);
1343 notify_pointer_focus(&input->base, NULL, 0, 0);
1344 input->focus = 0;
1345 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1346 wl_pointer_set_cursor(input->parent.pointer,
1347 input->enter_serial, NULL, 0, 0);
1348 notify_pointer_focus(&input->base, &input->output->base, x, y);
1349 input->focus = 1;
1350 }
1351
1352 if (location == THEME_LOCATION_CLIENT_AREA)
1353 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001354}
1355
1356static void
1357input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001358 uint32_t serial, uint32_t time, uint32_t button,
1359 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001360{
1361 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001362 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001363 enum frame_button_state fstate;
1364 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001365
Jason Ekstrand7744f712013-10-27 22:24:55 -05001366 if (input->output->frame) {
1367 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1368 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1369
1370 location = frame_pointer_button(input->output->frame, input,
1371 button, fstate);
1372
1373 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1374
1375 wl_shell_surface_move(input->output->parent.shell_surface,
1376 input->parent.seat, serial);
1377 frame_status_clear(input->output->frame,
1378 FRAME_STATUS_MOVE);
1379 return;
1380 }
1381
1382 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1383 wl_display_terminate(input->compositor->base.wl_display);
1384
1385 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1386 weston_output_schedule_repaint(&input->output->base);
1387 } else {
1388 location = THEME_LOCATION_CLIENT_AREA;
1389 }
1390
1391 if (location == THEME_LOCATION_CLIENT_AREA)
1392 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001393}
1394
1395static void
1396input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001397 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001398{
1399 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001400
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001401 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001402}
1403
1404static const struct wl_pointer_listener pointer_listener = {
1405 input_handle_pointer_enter,
1406 input_handle_pointer_leave,
1407 input_handle_motion,
1408 input_handle_button,
1409 input_handle_axis,
1410};
1411
1412static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001413input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1414 int fd, uint32_t size)
1415{
1416 struct wayland_input *input = data;
1417 struct xkb_keymap *keymap;
1418 char *map_str;
1419
1420 if (!data) {
1421 close(fd);
1422 return;
1423 }
1424
1425 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1426 close(fd);
1427 return;
1428 }
1429
1430 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1431 if (map_str == MAP_FAILED) {
1432 close(fd);
1433 return;
1434 }
1435
1436 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1437 map_str,
1438 XKB_KEYMAP_FORMAT_TEXT_V1,
1439 0);
1440 munmap(map_str, size);
1441 close(fd);
1442
1443 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001444 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001445 return;
1446 }
1447
Rui Matos0c194ce2013-10-10 19:44:21 +02001448 if (input->base.keyboard)
1449 weston_seat_update_keymap(&input->base, keymap);
1450 else
1451 weston_seat_init_keyboard(&input->base, keymap);
1452
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001453 xkb_map_unref(keymap);
1454}
1455
1456static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001457input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001458 struct wl_keyboard *keyboard,
1459 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001460 struct wl_surface *surface,
1461 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001462{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001463 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001464 struct wayland_output *focus;
1465
1466 focus = input->keyboard_focus;
1467 if (focus) {
1468 /* This shouldn't happen */
1469 focus->keyboard_count--;
1470 if (!focus->keyboard_count && focus->frame)
1471 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1472 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1473 weston_output_schedule_repaint(&focus->base);
1474 }
1475
1476 input->keyboard_focus = wl_surface_get_user_data(surface);
1477 input->keyboard_focus->keyboard_count++;
1478
1479 focus = input->keyboard_focus;
1480 if (focus->frame) {
1481 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1482 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1483 weston_output_schedule_repaint(&focus->base);
1484 }
1485
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001486
Daniel Stone50692802012-06-22 13:21:41 +01001487 /* XXX: If we get a modifier event immediately before the focus,
1488 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001489 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001490 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001491}
1492
1493static void
1494input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001495 struct wl_keyboard *keyboard,
1496 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001497 struct wl_surface *surface)
1498{
1499 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001500 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001501
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001502 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001503
1504 focus = input->keyboard_focus;
1505 if (!focus)
1506 return; /* This shouldn't happen */
1507
1508 focus->keyboard_count--;
1509 if (!focus->keyboard_count && focus->frame) {
1510 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1511 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1512 weston_output_schedule_repaint(&focus->base);
1513 }
1514
1515 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001516}
1517
Daniel Stone37816df2012-05-16 18:45:18 +01001518static void
1519input_handle_key(void *data, struct wl_keyboard *keyboard,
1520 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1521{
1522 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001523
Daniel Stone50692802012-06-22 13:21:41 +01001524 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001525 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001526 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001527 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001528 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001529}
1530
Daniel Stone351eb612012-05-31 15:27:47 -04001531static void
1532input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001533 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001534 uint32_t mods_latched, uint32_t mods_locked,
1535 uint32_t group)
1536{
Daniel Stone50692802012-06-22 13:21:41 +01001537 struct wayland_input *input = data;
1538 struct wayland_compositor *c = input->compositor;
1539 uint32_t serial_out;
1540
1541 /* If we get a key event followed by a modifier event with the
1542 * same serial number, then we try to preserve those semantics by
1543 * reusing the same serial number on the way out too. */
1544 if (serial_in == input->key_serial)
1545 serial_out = wl_display_get_serial(c->base.wl_display);
1546 else
1547 serial_out = wl_display_next_serial(c->base.wl_display);
1548
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001549 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001550 mods_depressed, mods_latched,
1551 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001552 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001553}
1554
Daniel Stone37816df2012-05-16 18:45:18 +01001555static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001556 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001557 input_handle_keyboard_enter,
1558 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001559 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001560 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001561};
1562
1563static void
Daniel Stone37816df2012-05-16 18:45:18 +01001564input_handle_capabilities(void *data, struct wl_seat *seat,
1565 enum wl_seat_capability caps)
1566{
1567 struct wayland_input *input = data;
1568
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001569 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1570 input->parent.pointer = wl_seat_get_pointer(seat);
1571 wl_pointer_set_user_data(input->parent.pointer, input);
1572 wl_pointer_add_listener(input->parent.pointer,
1573 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001574 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001575 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1576 wl_pointer_destroy(input->parent.pointer);
1577 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001578 }
1579
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001580 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1581 input->parent.keyboard = wl_seat_get_keyboard(seat);
1582 wl_keyboard_set_user_data(input->parent.keyboard, input);
1583 wl_keyboard_add_listener(input->parent.keyboard,
1584 &keyboard_listener, input);
1585 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1586 wl_keyboard_destroy(input->parent.keyboard);
1587 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001588 }
1589}
1590
1591static const struct wl_seat_listener seat_listener = {
1592 input_handle_capabilities,
1593};
1594
1595static void
1596display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001597{
1598 struct wayland_input *input;
1599
Peter Huttererf3d62272013-08-08 11:57:05 +10001600 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001601 if (input == NULL)
1602 return;
1603
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001604 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001605 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001606 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1607 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001608 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001609
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001610 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1611 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001612
1613 input->parent.cursor.surface =
1614 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001615}
1616
1617static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001618wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1619 int32_t x, int32_t y,
1620 int32_t physical_width, int32_t physical_height,
1621 int32_t subpixel, const char *make,
1622 const char *model, int32_t transform)
1623{
1624 struct wayland_parent_output *output = data;
1625
1626 output->x = x;
1627 output->y = y;
1628 output->physical.width = physical_width;
1629 output->physical.height = physical_height;
1630 output->physical.subpixel = subpixel;
1631
1632 free(output->physical.make);
1633 output->physical.make = strdup(make);
1634 free(output->physical.model);
1635 output->physical.model = strdup(model);
1636
1637 output->transform = transform;
1638}
1639
1640static struct weston_mode *
1641find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1642{
1643 struct weston_mode *mode;
1644
1645 wl_list_for_each(mode, list, link) {
1646 if (mode->width == width && mode->height == height &&
1647 mode->refresh == refresh)
1648 return mode;
1649 }
1650
1651 mode = zalloc(sizeof *mode);
1652 if (!mode)
1653 return NULL;
1654
1655 mode->width = width;
1656 mode->height = height;
1657 mode->refresh = refresh;
1658 wl_list_insert(list, &mode->link);
1659
1660 return mode;
1661}
1662
1663static void
1664wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1665 uint32_t flags, int32_t width, int32_t height,
1666 int32_t refresh)
1667{
1668 struct wayland_parent_output *output = data;
1669 struct weston_mode *mode;
1670
1671 if (output->output) {
1672 mode = find_mode(&output->output->base.mode_list,
1673 width, height, refresh);
1674 if (!mode)
1675 return;
1676 mode->flags = flags;
1677 /* Do a mode-switch on current mode change? */
1678 } else {
1679 mode = find_mode(&output->mode_list, width, height, refresh);
1680 if (!mode)
1681 return;
1682 mode->flags = flags;
1683 if (flags & WL_OUTPUT_MODE_CURRENT)
1684 output->current_mode = mode;
1685 if (flags & WL_OUTPUT_MODE_PREFERRED)
1686 output->preferred_mode = mode;
1687 }
1688}
1689
1690static const struct wl_output_listener output_listener = {
1691 wayland_parent_output_geometry,
1692 wayland_parent_output_mode
1693};
1694
1695static void
1696wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1697{
1698 struct wayland_parent_output *output;
1699
1700 output = zalloc(sizeof *output);
1701 if (!output)
1702 return;
1703
1704 output->id = id;
1705 output->global = wl_registry_bind(c->parent.registry, id,
1706 &wl_output_interface, 1);
1707 if (!output->global)
1708 return;
1709 wl_output_add_listener(output->global, &output_listener, output);
1710
1711 output->scale = 0;
1712 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1713 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1714 wl_list_init(&output->mode_list);
1715 wl_list_insert(&c->parent.output_list, &output->link);
1716
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001717 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001718 wl_display_roundtrip(c->parent.wl_display);
1719 wayland_output_create_for_parent_output(c, output);
1720 }
1721}
1722
1723static void
1724wayland_parent_output_destroy(struct wayland_parent_output *output)
1725{
1726 struct weston_mode *mode, *next;
1727
1728 if (output->output)
1729 wayland_output_destroy(&output->output->base);
1730
1731 wl_output_destroy(output->global);
1732 free(output->physical.make);
1733 free(output->physical.model);
1734
1735 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1736 wl_list_remove(&mode->link);
1737 free(mode);
1738 }
1739}
1740
1741static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001742registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1743 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001744{
1745 struct wayland_compositor *c = data;
1746
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001747 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001748 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001749 wl_registry_bind(registry, name,
1750 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001751 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001752 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001753 wl_registry_bind(registry, name,
1754 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001755 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1756 c->parent.fshell =
1757 wl_registry_bind(registry, name,
1758 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001759 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001760 display_add_seat(c, name);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001761 } else if (strcmp(interface, "wl_output") == 0) {
1762 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001763 } else if (strcmp(interface, "wl_shm") == 0) {
1764 c->parent.shm =
1765 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001766 }
1767}
1768
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001769static void
1770registry_handle_global_remove(void *data, struct wl_registry *registry,
1771 uint32_t name)
1772{
1773 struct wayland_compositor *c = data;
1774 struct wayland_parent_output *output;
1775
1776 wl_list_for_each(output, &c->parent.output_list, link)
1777 if (output->id == name)
1778 wayland_parent_output_destroy(output);
1779}
1780
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001781static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001782 registry_handle_global,
1783 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001784};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001785
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001786static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001787wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1788{
1789 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001790 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001791
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001792 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1793 wl_display_terminate(c->base.wl_display);
1794 return 0;
1795 }
1796
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001797 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001798 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001799 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001800 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001801
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001802 if (mask == 0) {
1803 count = wl_display_dispatch_pending(c->parent.wl_display);
1804 wl_display_flush(c->parent.wl_display);
1805 }
1806
1807 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001808}
1809
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001810static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001811wayland_restore(struct weston_compositor *ec)
1812{
1813}
1814
1815static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001816wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001817{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001818 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1819
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001820 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001821
Jonas Ådahle5a12252013-04-05 23:07:11 +02001822 if (c->parent.shm)
1823 wl_shm_destroy(c->parent.shm);
1824
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001825 free(ec);
1826}
1827
Jason Ekstrand7744f712013-10-27 22:24:55 -05001828static const char *left_ptrs[] = {
1829 "left_ptr",
1830 "default",
1831 "top_left_arrow",
1832 "left-arrow"
1833};
1834
1835static void
1836create_cursor(struct wayland_compositor *c, struct weston_config *config)
1837{
1838 struct weston_config_section *s;
1839 int size;
1840 char *theme = NULL;
1841 unsigned int i;
1842
1843 s = weston_config_get_section(config, "shell", NULL, NULL);
1844 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1845 weston_config_section_get_int(s, "cursor-size", &size, 32);
1846
1847 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001848 if (!c->cursor_theme) {
1849 fprintf(stderr, "could not load cursor theme\n");
1850 return;
1851 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001852
U. Artie Eoffff755002014-01-17 12:36:58 -08001853 free(theme);
1854
Jason Ekstrand7744f712013-10-27 22:24:55 -05001855 c->cursor = NULL;
1856 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1857 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1858 left_ptrs[i]);
1859 if (!c->cursor) {
1860 fprintf(stderr, "could not load left cursor\n");
1861 return;
1862 }
1863}
1864
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001865static void
1866fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1867 void *data)
1868{
1869 struct wayland_compositor *c = data;
1870 struct wayland_input *input = NULL;
1871
1872 wl_list_for_each(input, &c->input_list, link)
1873 if (&input->base == seat_base)
1874 break;
1875
1876 if (!input || !input->output)
1877 return;
1878
1879 if (input->output->frame)
1880 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1881 else
1882 wayland_output_set_windowed(input->output);
1883
1884 weston_output_schedule_repaint(&input->output->base);
1885}
1886
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001887static struct wayland_compositor *
1888wayland_compositor_create(struct wl_display *display, int use_pixman,
1889 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001890 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001891{
1892 struct wayland_compositor *c;
1893 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001894 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001895
Peter Huttererf3d62272013-08-08 11:57:05 +10001896 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001897 if (c == NULL)
1898 return NULL;
1899
Daniel Stone725c2c32012-06-22 14:04:36 +01001900 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001901 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001902 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001903
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001904 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001905
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001906 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001907 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001908 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001909 }
1910
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001911 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001912 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001913 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1914 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001915 wl_display_roundtrip(c->parent.wl_display);
1916
1917 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001918
1919 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001920
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001921 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001922
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001923 if (!c->use_pixman) {
1924 gl_renderer = weston_load_module("gl-renderer.so",
1925 "gl_renderer_interface");
1926 if (!gl_renderer)
1927 c->use_pixman = 1;
1928 }
1929
1930 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001931 if (gl_renderer->create(&c->base, c->parent.wl_display,
1932 gl_renderer->alpha_attribs,
1933 NULL) < 0) {
1934 weston_log("Failed to initialize the GL renderer; "
1935 "falling back to pixman.\n");
1936 c->use_pixman = 1;
1937 }
1938 }
1939
1940 if (c->use_pixman) {
1941 if (pixman_renderer_init(&c->base) < 0) {
1942 weston_log("Failed to initialize pixman renderer\n");
1943 goto err_display;
1944 }
1945 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001946
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001947 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001948 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001949
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001950 loop = wl_display_get_event_loop(c->base.wl_display);
1951
1952 fd = wl_display_get_fd(c->parent.wl_display);
1953 c->parent.wl_source =
1954 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1955 wayland_compositor_handle_event, c);
1956 if (c->parent.wl_source == NULL)
1957 goto err_renderer;
1958
1959 wl_event_source_check(c->parent.wl_source);
1960
1961 return c;
1962err_renderer:
1963 c->base.renderer->destroy(&c->base);
1964err_display:
1965 wl_display_disconnect(c->parent.wl_display);
1966err_compositor:
1967 weston_compositor_shutdown(&c->base);
1968err_free:
1969 free(c);
1970 return NULL;
1971}
1972
1973static void
1974wayland_compositor_destroy(struct wayland_compositor *c)
1975{
1976 struct weston_output *output;
1977
1978 wl_list_for_each(output, &c->base.output_list, link)
1979 wayland_output_destroy(output);
1980
1981 c->base.renderer->destroy(&c->base);
1982 wl_display_disconnect(c->parent.wl_display);
1983
1984 if (c->theme)
1985 theme_destroy(c->theme);
1986 if (c->frame_device)
1987 cairo_device_destroy(c->frame_device);
1988 wl_cursor_theme_destroy(c->cursor_theme);
1989
1990 weston_compositor_shutdown(&c->base);
1991 free(c);
1992}
1993
1994WL_EXPORT struct weston_compositor *
1995backend_init(struct wl_display *display, int *argc, char *argv[],
1996 struct weston_config *config)
1997{
1998 struct wayland_compositor *c;
1999 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002000 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002001 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002002 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002003 const char *section_name, *display_name;
2004 char *name;
2005
2006 const struct weston_option wayland_options[] = {
2007 { WESTON_OPTION_INTEGER, "width", 0, &width },
2008 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002009 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002010 { WESTON_OPTION_STRING, "display", 0, &display_name },
2011 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2012 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002013 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002014 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002015 };
2016
2017 width = 0;
2018 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002019 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002020 display_name = NULL;
2021 use_pixman = 0;
2022 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002023 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002024 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002025 parse_options(wayland_options,
2026 ARRAY_LENGTH(wayland_options), argc, argv);
2027
2028 c = wayland_compositor_create(display, use_pixman, display_name,
2029 argc, argv, config);
2030 if (!c)
2031 return NULL;
2032
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002033 if (sprawl || c->parent.fshell) {
2034 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002035 wl_display_roundtrip(c->parent.wl_display);
2036
2037 wl_list_for_each(poutput, &c->parent.output_list, link)
2038 wayland_output_create_for_parent_output(c, poutput);
2039
2040 return &c->base;
2041 }
2042
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002043 if (fullscreen) {
2044 output = wayland_output_create(c, 0, 0, width, height,
2045 NULL, 1, 0, 1);
2046 if (!output)
2047 goto err_outputs;
2048
Axel Davydd8b88d2013-11-17 21:34:16 +01002049 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002050 return &c->base;
2051 }
2052
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002053 section = NULL;
2054 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002055 while (weston_config_next_section(config, &section, &section_name)) {
2056 if (!section_name || strcmp(section_name, "output") != 0)
2057 continue;
2058 weston_config_section_get_string(section, "name", &name, NULL);
2059 if (name == NULL)
2060 continue;
2061
2062 if (name[0] != 'W' || name[1] != 'L') {
2063 free(name);
2064 continue;
2065 }
2066 free(name);
2067
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002068 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002069 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002070 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002071 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002072 if (wayland_output_set_windowed(output))
2073 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002074
2075 x += output->base.width;
2076 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002077 }
2078
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002079 if (!width)
2080 width = 1024;
2081 if (!height)
2082 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002083 if (!scale)
2084 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002085 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002086 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002087 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002088 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002089 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002090 if (wayland_output_set_windowed(output))
2091 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002092
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002093 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002094 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002095 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002096
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002097 weston_compositor_add_key_binding(&c->base, KEY_F,
2098 MODIFIER_CTRL | MODIFIER_ALT,
2099 fullscreen_binding, c);
2100
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002101 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002102
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002103err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002104 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002105 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002106}