blob: f35db9c310c71f96fd91ed8a85a3f0dd1a8180c3 [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
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500170 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100171 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400172 uint32_t enter_serial;
173 int focus;
174 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500175 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100176};
177
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300178struct gl_renderer_interface *gl_renderer;
179
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500180static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500181wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
182{
183 cairo_surface_destroy(buffer->c_surface);
184 pixman_image_unref(buffer->pm_image);
185
186 wl_buffer_destroy(buffer->buffer);
187 munmap(buffer->data, buffer->size);
188
189 pixman_region32_fini(&buffer->damage);
190
191 wl_list_remove(&buffer->link);
192 wl_list_remove(&buffer->free_link);
193 free(buffer);
194}
195
196static void
197buffer_release(void *data, struct wl_buffer *buffer)
198{
199 struct wayland_shm_buffer *sb = data;
200
201 if (sb->output) {
202 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
203 } else {
204 wayland_shm_buffer_destroy(sb);
205 }
206}
207
208static const struct wl_buffer_listener buffer_listener = {
209 buffer_release
210};
211
212static struct wayland_shm_buffer *
213wayland_output_get_shm_buffer(struct wayland_output *output)
214{
215 struct wayland_compositor *c =
216 (struct wayland_compositor *) output->base.compositor;
217 struct wl_shm *shm = c->parent.shm;
218 struct wayland_shm_buffer *sb;
219
220 struct wl_shm_pool *pool;
221 int width, height, stride;
222 int32_t fx, fy;
223 int fd;
224 unsigned char *data;
225
226 if (!wl_list_empty(&output->shm.free_buffers)) {
227 sb = container_of(output->shm.free_buffers.next,
228 struct wayland_shm_buffer, free_link);
229 wl_list_remove(&sb->free_link);
230 wl_list_init(&sb->free_link);
231
232 return sb;
233 }
234
235 if (output->frame) {
236 width = frame_width(output->frame);
237 height = frame_height(output->frame);
238 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500239 width = output->base.current_mode->width;
240 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500241 }
242
243 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
244
245 fd = os_create_anonymous_file(height * stride);
246 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000247 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500248 return NULL;
249 }
250
251 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
252 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000253 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500254 close(fd);
255 return NULL;
256 }
257
258 sb = zalloc(sizeof *sb);
259
260 sb->output = output;
261 wl_list_init(&sb->free_link);
262 wl_list_insert(&output->shm.buffers, &sb->link);
263
264 pixman_region32_init_rect(&sb->damage, 0, 0,
265 output->base.width, output->base.height);
266 sb->frame_damaged = 1;
267
268 sb->data = data;
269 sb->size = height * stride;
270
271 pool = wl_shm_create_pool(shm, fd, sb->size);
272
273 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
274 width, height,
275 stride,
276 WL_SHM_FORMAT_ARGB8888);
277 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
278 wl_shm_pool_destroy(pool);
279 close(fd);
280
281 memset(data, 0, sb->size);
282
283 sb->c_surface =
284 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
285 width, height, stride);
286
287 fx = 0;
288 fy = 0;
289 if (output->frame)
290 frame_interior(output->frame, &fx, &fy, 0, 0);
291 sb->pm_image =
292 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
293 (uint32_t *)(data + fy * stride) + fx,
294 stride);
295
296 return sb;
297}
298
299static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500300frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400301{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500302 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400303
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500304 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500305 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400306}
307
308static const struct wl_callback_listener frame_listener = {
309 frame_done
310};
311
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500312static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200313draw_initial_frame(struct wayland_output *output)
314{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500315 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200316
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500317 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200318
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500319 /* If we are rendering with GL, then orphan it so that it gets
320 * destroyed immediately */
321 if (output->gl.egl_window)
322 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500323
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500324 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500325 wl_surface_damage(output->parent.surface, 0, 0,
326 output->base.current_mode->width,
327 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200328}
329
330static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500331wayland_output_update_gl_border(struct wayland_output *output)
332{
333 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
334 cairo_t *cr;
335
336 if (!output->frame)
337 return;
338 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
339 return;
340
341 fwidth = frame_width(output->frame);
342 fheight = frame_height(output->frame);
343 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
344
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500345 if (!output->gl.border.top)
346 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500347 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
348 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500349 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500350 frame_repaint(output->frame, cr);
351 cairo_destroy(cr);
352 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
353 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500354 cairo_image_surface_get_stride(output->gl.border.top) / 4,
355 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500356
357
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500358 if (!output->gl.border.left)
359 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500360 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
361 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500362 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500363 cairo_translate(cr, 0, -iy);
364 frame_repaint(output->frame, cr);
365 cairo_destroy(cr);
366 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
367 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 cairo_image_surface_get_stride(output->gl.border.left) / 4,
369 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500370
371
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500372 if (!output->gl.border.right)
373 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500374 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
375 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500376 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500377 cairo_translate(cr, -(iwidth + ix), -iy);
378 frame_repaint(output->frame, cr);
379 cairo_destroy(cr);
380 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
381 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500382 cairo_image_surface_get_stride(output->gl.border.right) / 4,
383 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500384
385
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500386 if (!output->gl.border.bottom)
387 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500388 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
389 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500390 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500391 cairo_translate(cr, 0, -(iy + iheight));
392 frame_repaint(output->frame, cr);
393 cairo_destroy(cr);
394 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
395 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500396 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
397 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500398}
399
400static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200401wayland_output_start_repaint_loop(struct weston_output *output_base)
402{
403 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500404 struct wayland_compositor *wc =
405 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200406 struct wl_callback *callback;
407
408 /* If this is the initial frame, we need to attach a buffer so that
409 * the compositor can map the surface and include it in its render
410 * loop. If the surface doesn't end up in the render loop, the frame
411 * callback won't be invoked. The buffer is transparent and of the
412 * same size as the future real output buffer. */
413 if (output->parent.draw_initial_frame) {
414 output->parent.draw_initial_frame = 0;
415
416 draw_initial_frame(output);
417 }
418
419 callback = wl_surface_frame(output->parent.surface);
420 wl_callback_add_listener(callback, &frame_listener, output);
421 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500422 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200423}
424
David Herrmann1edf44c2013-10-22 17:11:26 +0200425static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500426wayland_output_repaint_gl(struct weston_output *output_base,
427 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400428{
429 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400430 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400431 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600432
Kristian Høgsberg33418202011-08-16 23:01:28 -0400433 callback = wl_surface_frame(output->parent.surface);
434 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100435
Jason Ekstrand7744f712013-10-27 22:24:55 -0500436 wayland_output_update_gl_border(output);
437
Pekka Paalanenbc106382012-10-10 12:49:31 +0300438 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200439
440 pixman_region32_subtract(&ec->primary_plane.damage,
441 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200442 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100443}
444
Matt Roper361d2ad2011-08-29 13:52:23 -0700445static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500446wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
447{
448 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
449 cairo_t *cr;
450
451 if (!buffer->output->frame || !buffer->frame_damaged)
452 return;
453
454 cr = cairo_create(buffer->c_surface);
455
456 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
457 fwidth = frame_width(buffer->output->frame);
458 fheight = frame_height(buffer->output->frame);
459
460 /* Set the clip so we don't unnecisaraly damage the surface */
461 cairo_move_to(cr, ix, iy);
462 cairo_rel_line_to(cr, iwidth, 0);
463 cairo_rel_line_to(cr, 0, iheight);
464 cairo_rel_line_to(cr, -iwidth, 0);
465 cairo_line_to(cr, ix, iy);
466 cairo_line_to(cr, 0, iy);
467 cairo_line_to(cr, 0, fheight);
468 cairo_line_to(cr, fwidth, fheight);
469 cairo_line_to(cr, fwidth, 0);
470 cairo_line_to(cr, 0, 0);
471 cairo_line_to(cr, 0, iy);
472 cairo_close_path(cr);
473 cairo_clip(cr);
474
475 /* Draw using a pattern so that the final result gets clipped */
476 cairo_push_group(cr);
477 frame_repaint(buffer->output->frame, cr);
478 cairo_pop_group_to_source(cr);
479 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
480 cairo_paint(cr);
481
482 cairo_destroy(cr);
483}
484
485static void
486wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
487{
488 pixman_region32_t damage;
489 pixman_box32_t *rects;
490 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
491 int i, n;
492
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500493 pixman_region32_init(&damage);
494 weston_transformed_region(sb->output->base.width,
495 sb->output->base.height,
496 sb->output->base.transform,
497 sb->output->base.current_scale,
498 &sb->damage, &damage);
499
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500500 if (sb->output->frame) {
501 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
502 fwidth = frame_width(sb->output->frame);
503 fheight = frame_height(sb->output->frame);
504
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500505 pixman_region32_translate(&damage, ix, iy);
506
507 if (sb->frame_damaged) {
508 pixman_region32_union_rect(&damage, &damage,
509 0, 0, fwidth, iy);
510 pixman_region32_union_rect(&damage, &damage,
511 0, iy, ix, iheight);
512 pixman_region32_union_rect(&damage, &damage,
513 ix + iwidth, iy,
514 fwidth - (ix + iwidth), iheight);
515 pixman_region32_union_rect(&damage, &damage,
516 0, iy + iheight,
517 fwidth, fheight - (iy + iheight));
518 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500519 }
520
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500521 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500522 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
523 for (i = 0; i < n; ++i)
524 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
525 rects[i].y1, rects[i].x2 - rects[i].x1,
526 rects[i].y2 - rects[i].y1);
527
528 if (sb->output->frame)
529 pixman_region32_fini(&damage);
530}
531
532static int
533wayland_output_repaint_pixman(struct weston_output *output_base,
534 pixman_region32_t *damage)
535{
536 struct wayland_output *output = (struct wayland_output *) output_base;
537 struct wayland_compositor *c =
538 (struct wayland_compositor *)output->base.compositor;
539 struct wl_callback *callback;
540 struct wayland_shm_buffer *sb;
541
542 if (output->frame) {
543 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
544 wl_list_for_each(sb, &output->shm.buffers, link)
545 sb->frame_damaged = 1;
546 }
547
548 wl_list_for_each(sb, &output->shm.buffers, link)
549 pixman_region32_union(&sb->damage, &sb->damage, damage);
550
551 sb = wayland_output_get_shm_buffer(output);
552
553 wayland_output_update_shm_border(sb);
554 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
555 c->base.renderer->repaint_output(output_base, &sb->damage);
556
557 wayland_shm_buffer_attach(sb);
558
559 callback = wl_surface_frame(output->parent.surface);
560 wl_callback_add_listener(callback, &frame_listener, output);
561 wl_surface_commit(output->parent.surface);
562 wl_display_flush(c->parent.wl_display);
563
564 pixman_region32_fini(&sb->damage);
565 pixman_region32_init(&sb->damage);
566 sb->frame_damaged = 0;
567
568 pixman_region32_subtract(&c->base.primary_plane.damage,
569 &c->base.primary_plane.damage, damage);
570 return 0;
571}
572
573static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500574wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700575{
576 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500577 struct wayland_compositor *c =
578 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700579
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500580 if (c->use_pixman) {
581 pixman_renderer_output_destroy(output_base);
582 } else {
583 gl_renderer->output_destroy(output_base);
584 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100585
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500586 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500587 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500588 if (output->parent.shell_surface)
589 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500590
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600591 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500592 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600593
594 cairo_surface_destroy(output->gl.border.top);
595 cairo_surface_destroy(output->gl.border.left);
596 cairo_surface_destroy(output->gl.border.right);
597 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500598
599 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700600 free(output);
601
602 return;
603}
604
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300605static const struct wl_shell_surface_listener shell_surface_listener;
606
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200607static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500608wayland_output_init_gl_renderer(struct wayland_output *output)
609{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500610 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500611
612 if (output->frame) {
613 fwidth = frame_width(output->frame);
614 fheight = frame_height(output->frame);
615 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500616 fwidth = output->base.current_mode->width;
617 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500618 }
619
620 output->gl.egl_window =
621 wl_egl_window_create(output->parent.surface,
622 fwidth, fheight);
623 if (!output->gl.egl_window) {
624 weston_log("failure to create wl_egl_window\n");
625 return -1;
626 }
627
628 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000629 output->gl.egl_window,
630 gl_renderer->alpha_attribs,
631 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500632 goto cleanup_window;
633
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500634 return 0;
635
636cleanup_window:
637 wl_egl_window_destroy(output->gl.egl_window);
638 return -1;
639}
640
641static int
642wayland_output_init_pixman_renderer(struct wayland_output *output)
643{
644 return pixman_renderer_output_create(&output->base);
645}
646
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600647static void
648wayland_output_resize_surface(struct wayland_output *output)
649{
650 struct wayland_compositor *c =
651 (struct wayland_compositor *)output->base.compositor;
652 struct wayland_shm_buffer *buffer, *next;
653 int32_t ix, iy, iwidth, iheight;
654 int32_t width, height;
655 struct wl_region *region;
656
657 width = output->base.current_mode->width;
658 height = output->base.current_mode->height;
659
660 if (output->frame) {
661 frame_resize_inside(output->frame, width, height);
662
663 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
664 region = wl_compositor_create_region(c->parent.compositor);
665 wl_region_add(region, ix, iy, iwidth, iheight);
666 wl_surface_set_input_region(output->parent.surface, region);
667 wl_region_destroy(region);
668
669 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
670 region = wl_compositor_create_region(c->parent.compositor);
671 wl_region_add(region, ix, iy, iwidth, iheight);
672 wl_surface_set_opaque_region(output->parent.surface, region);
673 wl_region_destroy(region);
674
675 width = frame_width(output->frame);
676 height = frame_height(output->frame);
677 } else {
678 region = wl_compositor_create_region(c->parent.compositor);
679 wl_region_add(region, 0, 0, width, height);
680 wl_surface_set_input_region(output->parent.surface, region);
681 wl_region_destroy(region);
682
683 region = wl_compositor_create_region(c->parent.compositor);
684 wl_region_add(region, 0, 0, width, height);
685 wl_surface_set_opaque_region(output->parent.surface, region);
686 wl_region_destroy(region);
687 }
688
689 if (output->gl.egl_window) {
690 wl_egl_window_resize(output->gl.egl_window,
691 width, height, 0, 0);
692
693 /* These will need to be re-created due to the resize */
694 gl_renderer->output_set_border(&output->base,
695 GL_RENDERER_BORDER_TOP,
696 0, 0, 0, NULL);
697 cairo_surface_destroy(output->gl.border.top);
698 output->gl.border.top = NULL;
699 gl_renderer->output_set_border(&output->base,
700 GL_RENDERER_BORDER_LEFT,
701 0, 0, 0, NULL);
702 cairo_surface_destroy(output->gl.border.left);
703 output->gl.border.left = NULL;
704 gl_renderer->output_set_border(&output->base,
705 GL_RENDERER_BORDER_RIGHT,
706 0, 0, 0, NULL);
707 cairo_surface_destroy(output->gl.border.right);
708 output->gl.border.right = NULL;
709 gl_renderer->output_set_border(&output->base,
710 GL_RENDERER_BORDER_BOTTOM,
711 0, 0, 0, NULL);
712 cairo_surface_destroy(output->gl.border.bottom);
713 output->gl.border.bottom = NULL;
714 }
715
716 /* Throw away any remaining SHM buffers */
717 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
718 wayland_shm_buffer_destroy(buffer);
719 /* These will get thrown away when they get released */
720 wl_list_for_each(buffer, &output->shm.buffers, link)
721 buffer->output = NULL;
722}
723
724static int
725wayland_output_set_windowed(struct wayland_output *output)
726{
727 struct wayland_compositor *c =
728 (struct wayland_compositor *)output->base.compositor;
729 int tlen;
730 char *title;
731
732 if (output->frame)
733 return 0;
734
735 if (output->name) {
736 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
737 title = malloc(tlen + 1);
738 if (!title)
739 return -1;
740
741 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
742 } else {
743 title = strdup(WINDOW_TITLE);
744 }
745
746 if (!c->theme) {
747 c->theme = theme_create();
748 if (!c->theme) {
749 free(title);
750 return -1;
751 }
752 }
753 output->frame = frame_create(c->theme, 100, 100,
754 FRAME_BUTTON_CLOSE, title);
755 free(title);
756 if (!output->frame)
757 return -1;
758
759 if (output->keyboard_count)
760 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
761
762 wayland_output_resize_surface(output);
763
764 wl_shell_surface_set_toplevel(output->parent.shell_surface);
765
766 return 0;
767}
768
769static void
770wayland_output_set_fullscreen(struct wayland_output *output,
771 enum wl_shell_surface_fullscreen_method method,
772 uint32_t framerate, struct wl_output *target)
773{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500774 struct wayland_compositor *c =
775 (struct wayland_compositor *)output->base.compositor;
776
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600777 if (output->frame) {
778 frame_destroy(output->frame);
779 output->frame = NULL;
780 }
781
782 wayland_output_resize_surface(output);
783
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500784 if (output->parent.shell_surface) {
785 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
786 method, framerate, target);
787 } else if (c->parent.fshell) {
788 _wl_fullscreen_shell_present_surface(c->parent.fshell,
789 output->parent.surface,
790 method, target);
791 }
792}
793
794static struct weston_mode *
795wayland_output_choose_mode(struct wayland_output *output,
796 struct weston_mode *ref_mode)
797{
798 struct weston_mode *mode;
799
800 /* First look for an exact match */
801 wl_list_for_each(mode, &output->base.mode_list, link)
802 if (mode->width == ref_mode->width &&
803 mode->height == ref_mode->height &&
804 mode->refresh == ref_mode->refresh)
805 return mode;
806
807 /* If we can't find an exact match, ignore refresh and try again */
808 wl_list_for_each(mode, &output->base.mode_list, link)
809 if (mode->width == ref_mode->width &&
810 mode->height == ref_mode->height)
811 return mode;
812
813 /* Yeah, we failed */
814 return NULL;
815}
816
817enum mode_status {
818 MODE_STATUS_UNKNOWN,
819 MODE_STATUS_SUCCESS,
820 MODE_STATUS_FAIL,
821 MODE_STATUS_CANCEL,
822};
823
824static void
825mode_feedback_successful(void *data,
826 struct _wl_fullscreen_shell_mode_feedback *fb)
827{
828 enum mode_status *value = data;
829
830 printf("Mode switch successful\n");
831
832 *value = MODE_STATUS_SUCCESS;
833}
834
835static void
836mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
837{
838 enum mode_status *value = data;
839
840 printf("Mode switch failed\n");
841
842 *value = MODE_STATUS_FAIL;
843}
844
845static void
846mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
847{
848 enum mode_status *value = data;
849
850 printf("Mode switch cancelled\n");
851
852 *value = MODE_STATUS_CANCEL;
853}
854
855struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
856 mode_feedback_successful,
857 mode_feedback_failed,
858 mode_feedback_cancelled,
859};
860
861static int
862wayland_output_switch_mode(struct weston_output *output_base,
863 struct weston_mode *mode)
864{
865 struct wayland_output *output = (struct wayland_output *) output_base;
866 struct wayland_compositor *c;
867 struct wl_surface *old_surface;
868 struct weston_mode *old_mode;
869 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
870 enum mode_status mode_status;
871 int ret = 0;
872
873 if (output_base == NULL) {
874 weston_log("output is NULL.\n");
875 return -1;
876 }
877
878 if (mode == NULL) {
879 weston_log("mode is NULL.\n");
880 return -1;
881 }
882
883 c = (struct wayland_compositor *)output_base->compositor;
884
885 if (output->parent.shell_surface || !c->parent.fshell)
886 return -1;
887
888 mode = wayland_output_choose_mode(output, mode);
889 if (mode == NULL)
890 return -1;
891
892 if (output->base.current_mode == mode)
893 return 0;
894
895 old_mode = output->base.current_mode;
896 old_surface = output->parent.surface;
897 output->base.current_mode = mode;
898 output->parent.surface =
899 wl_compositor_create_surface(c->parent.compositor);
900 wl_surface_set_user_data(output->parent.surface, output);
901
902 /* Blow the old buffers because we changed size/surfaces */
903 wayland_output_resize_surface(output);
904
905 mode_feedback =
906 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
907 output->parent.surface,
908 output->parent.output,
909 mode->refresh);
910 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
911 &mode_feedback_listener,
912 &mode_status);
913
914 /* This should kick-start things again */
915 output->parent.draw_initial_frame = 1;
916 wayland_output_start_repaint_loop(&output->base);
917
918 mode_status = MODE_STATUS_UNKNOWN;
919 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
920 ret = wl_display_dispatch(c->parent.wl_display);
921
922 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
923
924 if (mode_status == MODE_STATUS_FAIL) {
925 output->base.current_mode = old_mode;
926 wl_surface_destroy(output->parent.surface);
927 output->parent.surface = old_surface;
928 wayland_output_resize_surface(output);
929
930 return -1;
931 }
932
933 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
934 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
935
936 if (c->use_pixman) {
937 pixman_renderer_output_destroy(output_base);
938 if (wayland_output_init_pixman_renderer(output) < 0)
939 goto err_output;
940 } else {
941 gl_renderer->output_destroy(output_base);
942 wl_egl_window_destroy(output->gl.egl_window);
943 if (wayland_output_init_gl_renderer(output) < 0)
944 goto err_output;
945 }
946 wl_surface_destroy(old_surface);
947
948 weston_output_schedule_repaint(&output->base);
949
950 return 0;
951
952err_output:
953 /* XXX */
954 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600955}
956
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500957static struct wayland_output *
958wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600959 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500960 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100961{
962 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500963 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600964
965 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
966 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100967
Peter Huttererf3d62272013-08-08 11:57:05 +1000968 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100969 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500970 return NULL;
971
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600972 output->name = name ? strdup(name) : NULL;
973 output->base.make = "waywayland";
974 output->base.model = "none";
975
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500976 output_width = width * scale;
977 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100978
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600979 output->parent.surface =
980 wl_compositor_create_surface(c->parent.compositor);
981 if (!output->parent.surface)
982 goto err_name;
983 wl_surface_set_user_data(output->parent.surface, output);
984
985 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600986
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500987 if (c->parent.shell) {
988 output->parent.shell_surface =
989 wl_shell_get_shell_surface(c->parent.shell,
990 output->parent.surface);
991 if (!output->parent.shell_surface)
992 goto err_surface;
993 wl_shell_surface_add_listener(output->parent.shell_surface,
994 &shell_surface_listener, output);
995 }
996
997 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600998 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
999 0, 0, NULL);
1000 wl_display_roundtrip(c->parent.wl_display);
1001 if (!width)
1002 output_width = output->parent.configure_width;
1003 if (!height)
1004 output_height = output->parent.configure_height;
1005 }
1006
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001007 output->mode.flags =
1008 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001009 output->mode.width = output_width;
1010 output->mode.height = output_height;
1011 output->mode.refresh = 60000;
1012 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001013 wl_list_init(&output->base.mode_list);
1014 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001015 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001016
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001017 wl_list_init(&output->shm.buffers);
1018 wl_list_init(&output->shm.free_buffers);
1019
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001020 weston_output_init(&output->base, &c->base, x, y, width, height,
1021 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001022
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001023 if (c->use_pixman) {
1024 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001025 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001026 output->base.repaint = wayland_output_repaint_pixman;
1027 } else {
1028 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001029 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001030 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001031 }
1032
Jonas Ådahle5a12252013-04-05 23:07:11 +02001033 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001034 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001035 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001036 output->base.set_backlight = NULL;
1037 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001038 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001039
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001040 wl_list_insert(c->base.output_list.prev, &output->base.link);
1041
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001042 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001043
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001044err_output:
1045 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001046 if (output->parent.shell_surface)
1047 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001048err_surface:
1049 wl_surface_destroy(output->parent.surface);
1050err_name:
1051 free(output->name);
1052
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001054 free(output);
1055
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001056 return NULL;
1057}
1058
1059static struct wayland_output *
1060wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001061 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001062 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001063 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001064{
1065 struct wayland_output *output;
1066 char *mode, *t, *name, *str;
1067 int width, height, scale;
1068 uint32_t transform;
1069 unsigned int i, slen;
1070
1071 static const struct { const char *name; uint32_t token; } transform_names[] = {
1072 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1073 { "90", WL_OUTPUT_TRANSFORM_90 },
1074 { "180", WL_OUTPUT_TRANSFORM_180 },
1075 { "270", WL_OUTPUT_TRANSFORM_270 },
1076 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1077 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1078 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1079 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1080 };
1081
1082 weston_config_section_get_string(config_section, "name", &name, NULL);
1083 if (name) {
1084 slen = strlen(name);
1085 slen += strlen(WINDOW_TITLE " - ");
1086 str = malloc(slen + 1);
1087 if (str)
1088 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1089 free(name);
1090 name = str;
1091 }
1092 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001093 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001094
1095 weston_config_section_get_string(config_section,
1096 "mode", &mode, "1024x600");
1097 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1098 weston_log("Invalid mode \"%s\" for output %s\n",
1099 mode, name);
1100 width = 1024;
1101 height = 640;
1102 }
1103 free(mode);
1104
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001105 if (option_width)
1106 width = option_width;
1107 if (option_height)
1108 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001109
1110 weston_config_section_get_int(config_section, "scale", &scale, 1);
1111
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001112 if (option_scale)
1113 scale = option_scale;
1114
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001115 weston_config_section_get_string(config_section,
1116 "transform", &t, "normal");
1117 transform = WL_OUTPUT_TRANSFORM_NORMAL;
1118 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
1119 if (strcmp(transform_names[i].name, t) == 0) {
1120 transform = transform_names[i].token;
1121 break;
1122 }
1123 }
1124 if (i >= ARRAY_LENGTH(transform_names))
1125 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
1126 free(t);
1127
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001128 output = wayland_output_create(c, x, y, width, height, name, 0,
1129 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001130 free(name);
1131
1132 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001133}
1134
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001135static struct wayland_output *
1136wayland_output_create_for_parent_output(struct wayland_compositor *c,
1137 struct wayland_parent_output *poutput)
1138{
1139 struct wayland_output *output;
1140 struct weston_mode *mode;
1141 int32_t x;
1142
1143 if (poutput->current_mode) {
1144 mode = poutput->current_mode;
1145 } else if (poutput->preferred_mode) {
1146 mode = poutput->current_mode;
1147 } else if (!wl_list_empty(&poutput->mode_list)) {
1148 mode = container_of(poutput->mode_list.next,
1149 struct weston_mode, link);
1150 } else {
1151 weston_log("No valid modes found. Skipping output");
1152 return NULL;
1153 }
1154
1155 if (!wl_list_empty(&c->base.output_list)) {
1156 output = container_of(c->base.output_list.prev,
1157 struct wayland_output, base.link);
1158 x = output->base.x + output->base.current_mode->width;
1159 } else {
1160 x = 0;
1161 }
1162
1163 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1164 NULL, 0,
1165 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1166 if (!output)
1167 return NULL;
1168
1169 output->parent.output = poutput->global;
1170
1171 output->base.make = poutput->physical.make;
1172 output->base.model = poutput->physical.model;
1173 wl_list_init(&output->base.mode_list);
1174 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1175 wl_list_init(&poutput->mode_list);
1176
1177 wayland_output_set_fullscreen(output,
1178 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1179 mode->refresh, poutput->global);
1180
1181 if (output->parent.shell_surface) {
1182 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1183 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1184 mode->refresh, poutput->global);
1185 } else if (c->parent.fshell) {
1186 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1187 output->parent.surface,
1188 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1189 poutput->global);
1190 _wl_fullscreen_shell_mode_feedback_destroy(
1191 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1192 output->parent.surface,
1193 poutput->global,
1194 mode->refresh));
1195 }
1196
1197 return output;
1198}
1199
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001200static void
1201shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1202 uint32_t serial)
1203{
1204 wl_shell_surface_pong(shell_surface, serial);
1205}
1206
1207static void
1208shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1209 uint32_t edges, int32_t width, int32_t height)
1210{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001211 struct wayland_output *output = data;
1212
1213 output->parent.configure_width = width;
1214 output->parent.configure_height = height;
1215
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001216 /* FIXME: implement resizing */
1217}
1218
1219static void
1220shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1221{
1222}
1223
1224static const struct wl_shell_surface_listener shell_surface_listener = {
1225 shell_surface_ping,
1226 shell_surface_configure,
1227 shell_surface_popup_done
1228};
1229
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001230/* Events received from the wayland-server this compositor is client of: */
1231
Jason Ekstrand7744f712013-10-27 22:24:55 -05001232/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001233static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001234input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001235{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001236
Jason Ekstrand7744f712013-10-27 22:24:55 -05001237 struct wl_buffer *buffer;
1238 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001239
Jason Ekstrand7744f712013-10-27 22:24:55 -05001240 if (!input->compositor->cursor)
1241 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001242
Jason Ekstrand7744f712013-10-27 22:24:55 -05001243 image = input->compositor->cursor->images[0];
1244 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001245 if (!buffer)
1246 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247
1248 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1249 input->parent.cursor.surface,
1250 image->hotspot_x, image->hotspot_y);
1251
1252 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1253 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1254 image->width, image->height);
1255 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001256}
1257
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001258static void
Daniel Stone37816df2012-05-16 18:45:18 +01001259input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1260 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001261 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001262{
1263 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001264 int32_t fx, fy;
1265 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001266
Daniel Stone50692802012-06-22 13:21:41 +01001267 /* XXX: If we get a modifier event immediately before the focus,
1268 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001269 input->enter_serial = serial;
1270 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001271
1272 if (input->output->frame) {
1273 location = frame_pointer_enter(input->output->frame, input,
1274 wl_fixed_to_int(x),
1275 wl_fixed_to_int(y));
1276 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1277 x -= wl_fixed_from_int(fx);
1278 y -= wl_fixed_from_int(fy);
1279
1280 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1281 weston_output_schedule_repaint(&input->output->base);
1282 } else {
1283 location = THEME_LOCATION_CLIENT_AREA;
1284 }
1285
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001286 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001287
1288 if (location == THEME_LOCATION_CLIENT_AREA) {
1289 input->focus = 1;
1290 notify_pointer_focus(&input->base, &input->output->base, x, y);
1291 wl_pointer_set_cursor(input->parent.pointer,
1292 input->enter_serial, NULL, 0, 0);
1293 } else {
1294 input->focus = 0;
1295 notify_pointer_focus(&input->base, NULL, 0, 0);
1296 input_set_cursor(input);
1297 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001298}
1299
1300static void
Daniel Stone37816df2012-05-16 18:45:18 +01001301input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1302 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001303{
1304 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001305
Jason Ekstrand7744f712013-10-27 22:24:55 -05001306 if (input->output->frame) {
1307 frame_pointer_leave(input->output->frame, input);
1308
1309 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1310 weston_output_schedule_repaint(&input->output->base);
1311 }
1312
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001313 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001314 input->output = NULL;
1315 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001316}
1317
1318static void
Daniel Stone37816df2012-05-16 18:45:18 +01001319input_handle_motion(void *data, struct wl_pointer *pointer,
1320 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1321{
1322 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001323 int32_t fx, fy;
1324 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001325
Jason Ekstrand7744f712013-10-27 22:24:55 -05001326 if (input->output->frame) {
1327 location = frame_pointer_motion(input->output->frame, input,
1328 wl_fixed_to_int(x),
1329 wl_fixed_to_int(y));
1330 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1331 x -= wl_fixed_from_int(fx);
1332 y -= wl_fixed_from_int(fy);
1333
1334 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1335 weston_output_schedule_repaint(&input->output->base);
1336 } else {
1337 location = THEME_LOCATION_CLIENT_AREA;
1338 }
1339
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001340 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001341
1342 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1343 input_set_cursor(input);
1344 notify_pointer_focus(&input->base, NULL, 0, 0);
1345 input->focus = 0;
1346 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1347 wl_pointer_set_cursor(input->parent.pointer,
1348 input->enter_serial, NULL, 0, 0);
1349 notify_pointer_focus(&input->base, &input->output->base, x, y);
1350 input->focus = 1;
1351 }
1352
1353 if (location == THEME_LOCATION_CLIENT_AREA)
1354 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001355}
1356
1357static void
1358input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001359 uint32_t serial, uint32_t time, uint32_t button,
1360 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001361{
1362 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001363 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001364 enum frame_button_state fstate;
1365 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001366
Jason Ekstrand7744f712013-10-27 22:24:55 -05001367 if (input->output->frame) {
1368 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1369 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1370
1371 location = frame_pointer_button(input->output->frame, input,
1372 button, fstate);
1373
1374 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1375
1376 wl_shell_surface_move(input->output->parent.shell_surface,
1377 input->parent.seat, serial);
1378 frame_status_clear(input->output->frame,
1379 FRAME_STATUS_MOVE);
1380 return;
1381 }
1382
1383 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1384 wl_display_terminate(input->compositor->base.wl_display);
1385
1386 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1387 weston_output_schedule_repaint(&input->output->base);
1388 } else {
1389 location = THEME_LOCATION_CLIENT_AREA;
1390 }
1391
1392 if (location == THEME_LOCATION_CLIENT_AREA)
1393 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001394}
1395
1396static void
1397input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001398 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001399{
1400 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001401
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001402 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001403}
1404
1405static const struct wl_pointer_listener pointer_listener = {
1406 input_handle_pointer_enter,
1407 input_handle_pointer_leave,
1408 input_handle_motion,
1409 input_handle_button,
1410 input_handle_axis,
1411};
1412
1413static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001414input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1415 int fd, uint32_t size)
1416{
1417 struct wayland_input *input = data;
1418 struct xkb_keymap *keymap;
1419 char *map_str;
1420
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001421 if (!data)
1422 goto error;
1423
1424 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1425 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1426 if (map_str == MAP_FAILED) {
1427 weston_log("mmap failed: %m\n");
1428 goto error;
1429 }
1430
1431 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1432 map_str,
1433 XKB_KEYMAP_FORMAT_TEXT_V1,
1434 0);
1435 munmap(map_str, size);
1436
1437 if (!keymap) {
1438 weston_log("failed to compile keymap\n");
1439 goto error;
1440 }
1441
1442 input->keyboard_state_update = STATE_UPDATE_NONE;
1443 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1444 weston_log("No keymap provided; falling back to defalt\n");
1445 keymap = NULL;
1446 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1447 } else {
1448 weston_log("Invalid keymap\n");
1449 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001450 }
1451
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001452 close(fd);
1453
Rui Matos0c194ce2013-10-10 19:44:21 +02001454 if (input->base.keyboard)
1455 weston_seat_update_keymap(&input->base, keymap);
1456 else
1457 weston_seat_init_keyboard(&input->base, keymap);
1458
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001459 if (keymap)
1460 xkb_map_unref(keymap);
1461
1462 return;
1463
1464error:
1465 wl_keyboard_release(input->parent.keyboard);
1466 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001467}
1468
1469static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001470input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001471 struct wl_keyboard *keyboard,
1472 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001473 struct wl_surface *surface,
1474 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001475{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001476 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001477 struct wayland_output *focus;
1478
1479 focus = input->keyboard_focus;
1480 if (focus) {
1481 /* This shouldn't happen */
1482 focus->keyboard_count--;
1483 if (!focus->keyboard_count && focus->frame)
1484 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1485 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1486 weston_output_schedule_repaint(&focus->base);
1487 }
1488
1489 input->keyboard_focus = wl_surface_get_user_data(surface);
1490 input->keyboard_focus->keyboard_count++;
1491
1492 focus = input->keyboard_focus;
1493 if (focus->frame) {
1494 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1495 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1496 weston_output_schedule_repaint(&focus->base);
1497 }
1498
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001499
Daniel Stone50692802012-06-22 13:21:41 +01001500 /* XXX: If we get a modifier event immediately before the focus,
1501 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001502 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001503 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001504}
1505
1506static void
1507input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001508 struct wl_keyboard *keyboard,
1509 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001510 struct wl_surface *surface)
1511{
1512 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001513 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001514
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001515 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001516
1517 focus = input->keyboard_focus;
1518 if (!focus)
1519 return; /* This shouldn't happen */
1520
1521 focus->keyboard_count--;
1522 if (!focus->keyboard_count && focus->frame) {
1523 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1524 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1525 weston_output_schedule_repaint(&focus->base);
1526 }
1527
1528 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001529}
1530
Daniel Stone37816df2012-05-16 18:45:18 +01001531static void
1532input_handle_key(void *data, struct wl_keyboard *keyboard,
1533 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1534{
1535 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001536
Daniel Stone50692802012-06-22 13:21:41 +01001537 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001538 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001539 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001540 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001541 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001542}
1543
Daniel Stone351eb612012-05-31 15:27:47 -04001544static void
1545input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001546 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001547 uint32_t mods_latched, uint32_t mods_locked,
1548 uint32_t group)
1549{
Daniel Stone50692802012-06-22 13:21:41 +01001550 struct wayland_input *input = data;
1551 struct wayland_compositor *c = input->compositor;
1552 uint32_t serial_out;
1553
1554 /* If we get a key event followed by a modifier event with the
1555 * same serial number, then we try to preserve those semantics by
1556 * reusing the same serial number on the way out too. */
1557 if (serial_in == input->key_serial)
1558 serial_out = wl_display_get_serial(c->base.wl_display);
1559 else
1560 serial_out = wl_display_next_serial(c->base.wl_display);
1561
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001562 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001563 mods_depressed, mods_latched,
1564 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001565 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001566}
1567
Daniel Stone37816df2012-05-16 18:45:18 +01001568static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001569 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001570 input_handle_keyboard_enter,
1571 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001572 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001573 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001574};
1575
1576static void
Daniel Stone37816df2012-05-16 18:45:18 +01001577input_handle_capabilities(void *data, struct wl_seat *seat,
1578 enum wl_seat_capability caps)
1579{
1580 struct wayland_input *input = data;
1581
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001582 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1583 input->parent.pointer = wl_seat_get_pointer(seat);
1584 wl_pointer_set_user_data(input->parent.pointer, input);
1585 wl_pointer_add_listener(input->parent.pointer,
1586 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001587 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001588 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1589 wl_pointer_destroy(input->parent.pointer);
1590 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001591 }
1592
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001593 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1594 input->parent.keyboard = wl_seat_get_keyboard(seat);
1595 wl_keyboard_set_user_data(input->parent.keyboard, input);
1596 wl_keyboard_add_listener(input->parent.keyboard,
1597 &keyboard_listener, input);
1598 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1599 wl_keyboard_destroy(input->parent.keyboard);
1600 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001601 }
1602}
1603
1604static const struct wl_seat_listener seat_listener = {
1605 input_handle_capabilities,
1606};
1607
1608static void
1609display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001610{
1611 struct wayland_input *input;
1612
Peter Huttererf3d62272013-08-08 11:57:05 +10001613 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001614 if (input == NULL)
1615 return;
1616
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001617 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001618 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001619 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1620 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001621 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001622
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001623 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1624 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001625
1626 input->parent.cursor.surface =
1627 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001628}
1629
1630static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001631wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1632 int32_t x, int32_t y,
1633 int32_t physical_width, int32_t physical_height,
1634 int32_t subpixel, const char *make,
1635 const char *model, int32_t transform)
1636{
1637 struct wayland_parent_output *output = data;
1638
1639 output->x = x;
1640 output->y = y;
1641 output->physical.width = physical_width;
1642 output->physical.height = physical_height;
1643 output->physical.subpixel = subpixel;
1644
1645 free(output->physical.make);
1646 output->physical.make = strdup(make);
1647 free(output->physical.model);
1648 output->physical.model = strdup(model);
1649
1650 output->transform = transform;
1651}
1652
1653static struct weston_mode *
1654find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1655{
1656 struct weston_mode *mode;
1657
1658 wl_list_for_each(mode, list, link) {
1659 if (mode->width == width && mode->height == height &&
1660 mode->refresh == refresh)
1661 return mode;
1662 }
1663
1664 mode = zalloc(sizeof *mode);
1665 if (!mode)
1666 return NULL;
1667
1668 mode->width = width;
1669 mode->height = height;
1670 mode->refresh = refresh;
1671 wl_list_insert(list, &mode->link);
1672
1673 return mode;
1674}
1675
1676static void
1677wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1678 uint32_t flags, int32_t width, int32_t height,
1679 int32_t refresh)
1680{
1681 struct wayland_parent_output *output = data;
1682 struct weston_mode *mode;
1683
1684 if (output->output) {
1685 mode = find_mode(&output->output->base.mode_list,
1686 width, height, refresh);
1687 if (!mode)
1688 return;
1689 mode->flags = flags;
1690 /* Do a mode-switch on current mode change? */
1691 } else {
1692 mode = find_mode(&output->mode_list, width, height, refresh);
1693 if (!mode)
1694 return;
1695 mode->flags = flags;
1696 if (flags & WL_OUTPUT_MODE_CURRENT)
1697 output->current_mode = mode;
1698 if (flags & WL_OUTPUT_MODE_PREFERRED)
1699 output->preferred_mode = mode;
1700 }
1701}
1702
1703static const struct wl_output_listener output_listener = {
1704 wayland_parent_output_geometry,
1705 wayland_parent_output_mode
1706};
1707
1708static void
1709wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1710{
1711 struct wayland_parent_output *output;
1712
1713 output = zalloc(sizeof *output);
1714 if (!output)
1715 return;
1716
1717 output->id = id;
1718 output->global = wl_registry_bind(c->parent.registry, id,
1719 &wl_output_interface, 1);
1720 if (!output->global)
1721 return;
1722 wl_output_add_listener(output->global, &output_listener, output);
1723
1724 output->scale = 0;
1725 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1726 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1727 wl_list_init(&output->mode_list);
1728 wl_list_insert(&c->parent.output_list, &output->link);
1729
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001730 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001731 wl_display_roundtrip(c->parent.wl_display);
1732 wayland_output_create_for_parent_output(c, output);
1733 }
1734}
1735
1736static void
1737wayland_parent_output_destroy(struct wayland_parent_output *output)
1738{
1739 struct weston_mode *mode, *next;
1740
1741 if (output->output)
1742 wayland_output_destroy(&output->output->base);
1743
1744 wl_output_destroy(output->global);
1745 free(output->physical.make);
1746 free(output->physical.model);
1747
1748 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1749 wl_list_remove(&mode->link);
1750 free(mode);
1751 }
1752}
1753
1754static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001755registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1756 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001757{
1758 struct wayland_compositor *c = data;
1759
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001760 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001761 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001762 wl_registry_bind(registry, name,
1763 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001764 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001765 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001766 wl_registry_bind(registry, name,
1767 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001768 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1769 c->parent.fshell =
1770 wl_registry_bind(registry, name,
1771 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001772 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001773 display_add_seat(c, name);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001774 } else if (strcmp(interface, "wl_output") == 0) {
1775 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001776 } else if (strcmp(interface, "wl_shm") == 0) {
1777 c->parent.shm =
1778 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001779 }
1780}
1781
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001782static void
1783registry_handle_global_remove(void *data, struct wl_registry *registry,
1784 uint32_t name)
1785{
1786 struct wayland_compositor *c = data;
1787 struct wayland_parent_output *output;
1788
1789 wl_list_for_each(output, &c->parent.output_list, link)
1790 if (output->id == name)
1791 wayland_parent_output_destroy(output);
1792}
1793
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001794static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001795 registry_handle_global,
1796 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001797};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001798
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001799static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001800wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1801{
1802 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001803 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001804
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001805 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1806 wl_display_terminate(c->base.wl_display);
1807 return 0;
1808 }
1809
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001810 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001811 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001812 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001813 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001814
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001815 if (mask == 0) {
1816 count = wl_display_dispatch_pending(c->parent.wl_display);
1817 wl_display_flush(c->parent.wl_display);
1818 }
1819
1820 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001821}
1822
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001823static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001824wayland_restore(struct weston_compositor *ec)
1825{
1826}
1827
1828static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001829wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001830{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001831 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1832
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001834
Jonas Ådahle5a12252013-04-05 23:07:11 +02001835 if (c->parent.shm)
1836 wl_shm_destroy(c->parent.shm);
1837
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001838 free(ec);
1839}
1840
Jason Ekstrand7744f712013-10-27 22:24:55 -05001841static const char *left_ptrs[] = {
1842 "left_ptr",
1843 "default",
1844 "top_left_arrow",
1845 "left-arrow"
1846};
1847
1848static void
1849create_cursor(struct wayland_compositor *c, struct weston_config *config)
1850{
1851 struct weston_config_section *s;
1852 int size;
1853 char *theme = NULL;
1854 unsigned int i;
1855
1856 s = weston_config_get_section(config, "shell", NULL, NULL);
1857 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1858 weston_config_section_get_int(s, "cursor-size", &size, 32);
1859
1860 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001861 if (!c->cursor_theme) {
1862 fprintf(stderr, "could not load cursor theme\n");
1863 return;
1864 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001865
U. Artie Eoffff755002014-01-17 12:36:58 -08001866 free(theme);
1867
Jason Ekstrand7744f712013-10-27 22:24:55 -05001868 c->cursor = NULL;
1869 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1870 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1871 left_ptrs[i]);
1872 if (!c->cursor) {
1873 fprintf(stderr, "could not load left cursor\n");
1874 return;
1875 }
1876}
1877
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001878static void
1879fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1880 void *data)
1881{
1882 struct wayland_compositor *c = data;
1883 struct wayland_input *input = NULL;
1884
1885 wl_list_for_each(input, &c->input_list, link)
1886 if (&input->base == seat_base)
1887 break;
1888
1889 if (!input || !input->output)
1890 return;
1891
1892 if (input->output->frame)
1893 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1894 else
1895 wayland_output_set_windowed(input->output);
1896
1897 weston_output_schedule_repaint(&input->output->base);
1898}
1899
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001900static struct wayland_compositor *
1901wayland_compositor_create(struct wl_display *display, int use_pixman,
1902 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001903 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001904{
1905 struct wayland_compositor *c;
1906 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001907 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001908
Peter Huttererf3d62272013-08-08 11:57:05 +10001909 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001910 if (c == NULL)
1911 return NULL;
1912
Daniel Stone725c2c32012-06-22 14:04:36 +01001913 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001914 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001915 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001916
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001917 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001918
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001919 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001920 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001921 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001922 }
1923
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001924 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001925 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001926 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1927 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001928 wl_display_roundtrip(c->parent.wl_display);
1929
1930 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001931
1932 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001933
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001934 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001935
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001936 if (!c->use_pixman) {
1937 gl_renderer = weston_load_module("gl-renderer.so",
1938 "gl_renderer_interface");
1939 if (!gl_renderer)
1940 c->use_pixman = 1;
1941 }
1942
1943 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001944 if (gl_renderer->create(&c->base, c->parent.wl_display,
1945 gl_renderer->alpha_attribs,
1946 NULL) < 0) {
1947 weston_log("Failed to initialize the GL renderer; "
1948 "falling back to pixman.\n");
1949 c->use_pixman = 1;
1950 }
1951 }
1952
1953 if (c->use_pixman) {
1954 if (pixman_renderer_init(&c->base) < 0) {
1955 weston_log("Failed to initialize pixman renderer\n");
1956 goto err_display;
1957 }
1958 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001959
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001960 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001961 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001962
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001963 loop = wl_display_get_event_loop(c->base.wl_display);
1964
1965 fd = wl_display_get_fd(c->parent.wl_display);
1966 c->parent.wl_source =
1967 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1968 wayland_compositor_handle_event, c);
1969 if (c->parent.wl_source == NULL)
1970 goto err_renderer;
1971
1972 wl_event_source_check(c->parent.wl_source);
1973
1974 return c;
1975err_renderer:
1976 c->base.renderer->destroy(&c->base);
1977err_display:
1978 wl_display_disconnect(c->parent.wl_display);
1979err_compositor:
1980 weston_compositor_shutdown(&c->base);
1981err_free:
1982 free(c);
1983 return NULL;
1984}
1985
1986static void
1987wayland_compositor_destroy(struct wayland_compositor *c)
1988{
1989 struct weston_output *output;
1990
1991 wl_list_for_each(output, &c->base.output_list, link)
1992 wayland_output_destroy(output);
1993
1994 c->base.renderer->destroy(&c->base);
1995 wl_display_disconnect(c->parent.wl_display);
1996
1997 if (c->theme)
1998 theme_destroy(c->theme);
1999 if (c->frame_device)
2000 cairo_device_destroy(c->frame_device);
2001 wl_cursor_theme_destroy(c->cursor_theme);
2002
2003 weston_compositor_shutdown(&c->base);
2004 free(c);
2005}
2006
2007WL_EXPORT struct weston_compositor *
2008backend_init(struct wl_display *display, int *argc, char *argv[],
2009 struct weston_config *config)
2010{
2011 struct wayland_compositor *c;
2012 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002013 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002014 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002015 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002016 const char *section_name, *display_name;
2017 char *name;
2018
2019 const struct weston_option wayland_options[] = {
2020 { WESTON_OPTION_INTEGER, "width", 0, &width },
2021 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002022 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002023 { WESTON_OPTION_STRING, "display", 0, &display_name },
2024 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2025 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002026 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002027 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002028 };
2029
2030 width = 0;
2031 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002032 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002033 display_name = NULL;
2034 use_pixman = 0;
2035 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002036 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002037 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002038 parse_options(wayland_options,
2039 ARRAY_LENGTH(wayland_options), argc, argv);
2040
2041 c = wayland_compositor_create(display, use_pixman, display_name,
2042 argc, argv, config);
2043 if (!c)
2044 return NULL;
2045
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002046 if (sprawl || c->parent.fshell) {
2047 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002048 wl_display_roundtrip(c->parent.wl_display);
2049
2050 wl_list_for_each(poutput, &c->parent.output_list, link)
2051 wayland_output_create_for_parent_output(c, poutput);
2052
2053 return &c->base;
2054 }
2055
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002056 if (fullscreen) {
2057 output = wayland_output_create(c, 0, 0, width, height,
2058 NULL, 1, 0, 1);
2059 if (!output)
2060 goto err_outputs;
2061
Axel Davydd8b88d2013-11-17 21:34:16 +01002062 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002063 return &c->base;
2064 }
2065
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002066 section = NULL;
2067 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002068 while (weston_config_next_section(config, &section, &section_name)) {
2069 if (!section_name || strcmp(section_name, "output") != 0)
2070 continue;
2071 weston_config_section_get_string(section, "name", &name, NULL);
2072 if (name == NULL)
2073 continue;
2074
2075 if (name[0] != 'W' || name[1] != 'L') {
2076 free(name);
2077 continue;
2078 }
2079 free(name);
2080
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002081 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002082 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002083 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002084 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002085 if (wayland_output_set_windowed(output))
2086 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002087
2088 x += output->base.width;
2089 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002090 }
2091
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002092 if (!width)
2093 width = 1024;
2094 if (!height)
2095 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002096 if (!scale)
2097 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002098 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002099 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002100 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002101 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002102 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002103 if (wayland_output_set_windowed(output))
2104 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002105
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002106 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002107 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002108 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002109
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002110 weston_compositor_add_key_binding(&c->base, KEY_F,
2111 MODIFIER_CTRL | MODIFIER_ALT,
2112 fullscreen_binding, c);
2113
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002114 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002115
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002116err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002117 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002118 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002119}