blob: 04a2331eb891d48891f432ccb38f022e5c8aef8f [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003 * Copyright © 2013 Jason Ekstrand
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010022 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025
26#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010032#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060033#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010034
Benjamin Franzkebe014562011-02-18 17:04:24 +010035#include <wayland-client.h>
36#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050037#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010040#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050041#include "pixman-renderer.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050042#include "../shared/image-loader.h"
Jonas Ådahle5a12252013-04-05 23:07:11 +020043#include "../shared/os-compatibility.h"
Jason Ekstrand7744f712013-10-27 22:24:55 -050044#include "../shared/cairo-util.h"
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050045#include "fullscreen-shell-client-protocol.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020046#include "presentation_timing-server-protocol.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047
Jason Ekstrand48ce4212013-10-27 22:25:02 -050048#define WINDOW_TITLE "Weston Compositor"
49
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010050struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050051 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052
53 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040054 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040055 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056 struct wl_compositor *compositor;
57 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050058 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020059 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010060
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050061 struct wl_list output_list;
62
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063 struct wl_event_source *wl_source;
64 uint32_t event_mask;
65 } parent;
66
Jason Ekstrandff2fd462013-10-27 22:24:58 -050067 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050068 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050069
Jason Ekstrand7744f712013-10-27 22:24:55 -050070 struct theme *theme;
71 cairo_device_t *frame_device;
72 struct wl_cursor_theme *cursor_theme;
73 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050074
Jason Ekstrand06ced802013-11-07 20:13:29 -060075 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076};
77
78struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050079 struct weston_output base;
80
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050082 int draw_initial_frame;
83 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050084
85 struct wl_output *output;
86 uint32_t global_id;
87
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050088 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060089 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010090 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050091
Jason Ekstrand7744f712013-10-27 22:24:55 -050092 int keyboard_count;
93
Jason Ekstrand5ea04802013-11-07 20:13:33 -060094 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050095 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050096
Jason Ekstrand7744f712013-10-27 22:24:55 -050097 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050098 struct wl_egl_window *egl_window;
99 struct {
100 cairo_surface_t *top;
101 cairo_surface_t *left;
102 cairo_surface_t *right;
103 cairo_surface_t *bottom;
104 } border;
105 } gl;
106
107 struct {
108 struct wl_list buffers;
109 struct wl_list free_buffers;
110 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500111
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500112 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500113 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100114};
115
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500116struct wayland_parent_output {
117 struct wayland_output *output;
118 struct wl_list link;
119
120 struct wl_output *global;
121 uint32_t id;
122
123 struct {
124 char *make;
125 char *model;
126 int32_t width, height;
127 uint32_t subpixel;
128 } physical;
129
130 int32_t x, y;
131 uint32_t transform;
132 uint32_t scale;
133
134 struct wl_list mode_list;
135 struct weston_mode *preferred_mode;
136 struct weston_mode *current_mode;
137};
138
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500139struct wayland_shm_buffer {
140 struct wayland_output *output;
141 struct wl_list link;
142 struct wl_list free_link;
143
144 struct wl_buffer *buffer;
145 void *data;
146 size_t size;
147 pixman_region32_t damage;
148 int frame_damaged;
149
150 pixman_image_t *pm_image;
151 cairo_surface_t *c_surface;
152};
153
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100154struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400155 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100156 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100157 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500158
159 struct {
160 struct wl_seat *seat;
161 struct wl_pointer *pointer;
162 struct wl_keyboard *keyboard;
163 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500164
165 struct {
166 struct wl_surface *surface;
167 int32_t hx, hy;
168 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500169 } parent;
170
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500171 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100172 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400173 uint32_t enter_serial;
174 int focus;
175 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500176 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100177};
178
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300179struct gl_renderer_interface *gl_renderer;
180
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500181static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500182wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
183{
184 cairo_surface_destroy(buffer->c_surface);
185 pixman_image_unref(buffer->pm_image);
186
187 wl_buffer_destroy(buffer->buffer);
188 munmap(buffer->data, buffer->size);
189
190 pixman_region32_fini(&buffer->damage);
191
192 wl_list_remove(&buffer->link);
193 wl_list_remove(&buffer->free_link);
194 free(buffer);
195}
196
197static void
198buffer_release(void *data, struct wl_buffer *buffer)
199{
200 struct wayland_shm_buffer *sb = data;
201
202 if (sb->output) {
203 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
204 } else {
205 wayland_shm_buffer_destroy(sb);
206 }
207}
208
209static const struct wl_buffer_listener buffer_listener = {
210 buffer_release
211};
212
213static struct wayland_shm_buffer *
214wayland_output_get_shm_buffer(struct wayland_output *output)
215{
216 struct wayland_compositor *c =
217 (struct wayland_compositor *) output->base.compositor;
218 struct wl_shm *shm = c->parent.shm;
219 struct wayland_shm_buffer *sb;
220
221 struct wl_shm_pool *pool;
222 int width, height, stride;
223 int32_t fx, fy;
224 int fd;
225 unsigned char *data;
226
227 if (!wl_list_empty(&output->shm.free_buffers)) {
228 sb = container_of(output->shm.free_buffers.next,
229 struct wayland_shm_buffer, free_link);
230 wl_list_remove(&sb->free_link);
231 wl_list_init(&sb->free_link);
232
233 return sb;
234 }
235
236 if (output->frame) {
237 width = frame_width(output->frame);
238 height = frame_height(output->frame);
239 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500240 width = output->base.current_mode->width;
241 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500242 }
243
244 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
245
246 fd = os_create_anonymous_file(height * stride);
247 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000248 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500249 return NULL;
250 }
251
252 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
253 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000254 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500255 close(fd);
256 return NULL;
257 }
258
259 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000260 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200261 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000262 close(fd);
263 free(data);
264 return NULL;
265 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500266
267 sb->output = output;
268 wl_list_init(&sb->free_link);
269 wl_list_insert(&output->shm.buffers, &sb->link);
270
271 pixman_region32_init_rect(&sb->damage, 0, 0,
272 output->base.width, output->base.height);
273 sb->frame_damaged = 1;
274
275 sb->data = data;
276 sb->size = height * stride;
277
278 pool = wl_shm_create_pool(shm, fd, sb->size);
279
280 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
281 width, height,
282 stride,
283 WL_SHM_FORMAT_ARGB8888);
284 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
285 wl_shm_pool_destroy(pool);
286 close(fd);
287
288 memset(data, 0, sb->size);
289
290 sb->c_surface =
291 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
292 width, height, stride);
293
294 fx = 0;
295 fy = 0;
296 if (output->frame)
297 frame_interior(output->frame, &fx, &fy, 0, 0);
298 sb->pm_image =
299 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
300 (uint32_t *)(data + fy * stride) + fx,
301 stride);
302
303 return sb;
304}
305
306static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500307frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400308{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500309 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400310 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400311
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500312 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400313
314 /* XXX: use the presentation extension for proper timings */
315 ts.tv_sec = time / 1000;
316 ts.tv_nsec = (time % 1000) * 1000000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200317 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400318}
319
320static const struct wl_callback_listener frame_listener = {
321 frame_done
322};
323
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500324static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200325draw_initial_frame(struct wayland_output *output)
326{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500327 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200328
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500329 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200330
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500331 /* If we are rendering with GL, then orphan it so that it gets
332 * destroyed immediately */
333 if (output->gl.egl_window)
334 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500335
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500336 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500337 wl_surface_damage(output->parent.surface, 0, 0,
338 output->base.current_mode->width,
339 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200340}
341
342static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500343wayland_output_update_gl_border(struct wayland_output *output)
344{
345 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
346 cairo_t *cr;
347
348 if (!output->frame)
349 return;
350 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
351 return;
352
353 fwidth = frame_width(output->frame);
354 fheight = frame_height(output->frame);
355 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
356
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500357 if (!output->gl.border.top)
358 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500359 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
360 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500361 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500362 frame_repaint(output->frame, cr);
363 cairo_destroy(cr);
364 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
365 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500366 cairo_image_surface_get_stride(output->gl.border.top) / 4,
367 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500368
369
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500370 if (!output->gl.border.left)
371 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
373 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500374 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500375 cairo_translate(cr, 0, -iy);
376 frame_repaint(output->frame, cr);
377 cairo_destroy(cr);
378 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
379 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500380 cairo_image_surface_get_stride(output->gl.border.left) / 4,
381 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382
383
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500384 if (!output->gl.border.right)
385 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
387 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500388 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389 cairo_translate(cr, -(iwidth + ix), -iy);
390 frame_repaint(output->frame, cr);
391 cairo_destroy(cr);
392 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
393 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 cairo_image_surface_get_stride(output->gl.border.right) / 4,
395 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396
397
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398 if (!output->gl.border.bottom)
399 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500400 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
401 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500402 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500403 cairo_translate(cr, 0, -(iy + iheight));
404 frame_repaint(output->frame, cr);
405 cairo_destroy(cr);
406 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
407 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
409 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410}
411
412static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200413wayland_output_start_repaint_loop(struct weston_output *output_base)
414{
415 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500416 struct wayland_compositor *wc =
417 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200418 struct wl_callback *callback;
419
420 /* If this is the initial frame, we need to attach a buffer so that
421 * the compositor can map the surface and include it in its render
422 * loop. If the surface doesn't end up in the render loop, the frame
423 * callback won't be invoked. The buffer is transparent and of the
424 * same size as the future real output buffer. */
425 if (output->parent.draw_initial_frame) {
426 output->parent.draw_initial_frame = 0;
427
428 draw_initial_frame(output);
429 }
430
431 callback = wl_surface_frame(output->parent.surface);
432 wl_callback_add_listener(callback, &frame_listener, output);
433 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500434 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200435}
436
David Herrmann1edf44c2013-10-22 17:11:26 +0200437static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500438wayland_output_repaint_gl(struct weston_output *output_base,
439 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400440{
441 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400442 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400443 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600444
Kristian Høgsberg33418202011-08-16 23:01:28 -0400445 callback = wl_surface_frame(output->parent.surface);
446 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100447
Jason Ekstrand7744f712013-10-27 22:24:55 -0500448 wayland_output_update_gl_border(output);
449
Pekka Paalanenbc106382012-10-10 12:49:31 +0300450 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200451
452 pixman_region32_subtract(&ec->primary_plane.damage,
453 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200454 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100455}
456
Matt Roper361d2ad2011-08-29 13:52:23 -0700457static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500458wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
459{
460 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
461 cairo_t *cr;
462
463 if (!buffer->output->frame || !buffer->frame_damaged)
464 return;
465
466 cr = cairo_create(buffer->c_surface);
467
468 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
469 fwidth = frame_width(buffer->output->frame);
470 fheight = frame_height(buffer->output->frame);
471
472 /* Set the clip so we don't unnecisaraly damage the surface */
473 cairo_move_to(cr, ix, iy);
474 cairo_rel_line_to(cr, iwidth, 0);
475 cairo_rel_line_to(cr, 0, iheight);
476 cairo_rel_line_to(cr, -iwidth, 0);
477 cairo_line_to(cr, ix, iy);
478 cairo_line_to(cr, 0, iy);
479 cairo_line_to(cr, 0, fheight);
480 cairo_line_to(cr, fwidth, fheight);
481 cairo_line_to(cr, fwidth, 0);
482 cairo_line_to(cr, 0, 0);
483 cairo_line_to(cr, 0, iy);
484 cairo_close_path(cr);
485 cairo_clip(cr);
486
487 /* Draw using a pattern so that the final result gets clipped */
488 cairo_push_group(cr);
489 frame_repaint(buffer->output->frame, cr);
490 cairo_pop_group_to_source(cr);
491 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
492 cairo_paint(cr);
493
494 cairo_destroy(cr);
495}
496
497static void
498wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
499{
500 pixman_region32_t damage;
501 pixman_box32_t *rects;
502 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
503 int i, n;
504
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500505 pixman_region32_init(&damage);
506 weston_transformed_region(sb->output->base.width,
507 sb->output->base.height,
508 sb->output->base.transform,
509 sb->output->base.current_scale,
510 &sb->damage, &damage);
511
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500512 if (sb->output->frame) {
513 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
514 fwidth = frame_width(sb->output->frame);
515 fheight = frame_height(sb->output->frame);
516
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500517 pixman_region32_translate(&damage, ix, iy);
518
519 if (sb->frame_damaged) {
520 pixman_region32_union_rect(&damage, &damage,
521 0, 0, fwidth, iy);
522 pixman_region32_union_rect(&damage, &damage,
523 0, iy, ix, iheight);
524 pixman_region32_union_rect(&damage, &damage,
525 ix + iwidth, iy,
526 fwidth - (ix + iwidth), iheight);
527 pixman_region32_union_rect(&damage, &damage,
528 0, iy + iheight,
529 fwidth, fheight - (iy + iheight));
530 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500531 }
532
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500533 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500534 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
535 for (i = 0; i < n; ++i)
536 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
537 rects[i].y1, rects[i].x2 - rects[i].x1,
538 rects[i].y2 - rects[i].y1);
539
540 if (sb->output->frame)
541 pixman_region32_fini(&damage);
542}
543
544static int
545wayland_output_repaint_pixman(struct weston_output *output_base,
546 pixman_region32_t *damage)
547{
548 struct wayland_output *output = (struct wayland_output *) output_base;
549 struct wayland_compositor *c =
550 (struct wayland_compositor *)output->base.compositor;
551 struct wl_callback *callback;
552 struct wayland_shm_buffer *sb;
553
554 if (output->frame) {
555 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
556 wl_list_for_each(sb, &output->shm.buffers, link)
557 sb->frame_damaged = 1;
558 }
559
560 wl_list_for_each(sb, &output->shm.buffers, link)
561 pixman_region32_union(&sb->damage, &sb->damage, damage);
562
563 sb = wayland_output_get_shm_buffer(output);
564
565 wayland_output_update_shm_border(sb);
566 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
567 c->base.renderer->repaint_output(output_base, &sb->damage);
568
569 wayland_shm_buffer_attach(sb);
570
571 callback = wl_surface_frame(output->parent.surface);
572 wl_callback_add_listener(callback, &frame_listener, output);
573 wl_surface_commit(output->parent.surface);
574 wl_display_flush(c->parent.wl_display);
575
576 pixman_region32_fini(&sb->damage);
577 pixman_region32_init(&sb->damage);
578 sb->frame_damaged = 0;
579
580 pixman_region32_subtract(&c->base.primary_plane.damage,
581 &c->base.primary_plane.damage, damage);
582 return 0;
583}
584
585static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500586wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700587{
588 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500589 struct wayland_compositor *c =
590 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700591
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500592 if (c->use_pixman) {
593 pixman_renderer_output_destroy(output_base);
594 } else {
595 gl_renderer->output_destroy(output_base);
596 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100597
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500598 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500599 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500600 if (output->parent.shell_surface)
601 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500602
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600603 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500604 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600605
606 cairo_surface_destroy(output->gl.border.top);
607 cairo_surface_destroy(output->gl.border.left);
608 cairo_surface_destroy(output->gl.border.right);
609 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500610
611 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700612 free(output);
613
614 return;
615}
616
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300617static const struct wl_shell_surface_listener shell_surface_listener;
618
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200619static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500620wayland_output_init_gl_renderer(struct wayland_output *output)
621{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500622 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500623
624 if (output->frame) {
625 fwidth = frame_width(output->frame);
626 fheight = frame_height(output->frame);
627 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500628 fwidth = output->base.current_mode->width;
629 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630 }
631
632 output->gl.egl_window =
633 wl_egl_window_create(output->parent.surface,
634 fwidth, fheight);
635 if (!output->gl.egl_window) {
636 weston_log("failure to create wl_egl_window\n");
637 return -1;
638 }
639
640 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000641 output->gl.egl_window,
642 gl_renderer->alpha_attribs,
643 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500644 goto cleanup_window;
645
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500646 return 0;
647
648cleanup_window:
649 wl_egl_window_destroy(output->gl.egl_window);
650 return -1;
651}
652
653static int
654wayland_output_init_pixman_renderer(struct wayland_output *output)
655{
656 return pixman_renderer_output_create(&output->base);
657}
658
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600659static void
660wayland_output_resize_surface(struct wayland_output *output)
661{
662 struct wayland_compositor *c =
663 (struct wayland_compositor *)output->base.compositor;
664 struct wayland_shm_buffer *buffer, *next;
665 int32_t ix, iy, iwidth, iheight;
666 int32_t width, height;
667 struct wl_region *region;
668
669 width = output->base.current_mode->width;
670 height = output->base.current_mode->height;
671
672 if (output->frame) {
673 frame_resize_inside(output->frame, width, height);
674
675 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
676 region = wl_compositor_create_region(c->parent.compositor);
677 wl_region_add(region, ix, iy, iwidth, iheight);
678 wl_surface_set_input_region(output->parent.surface, region);
679 wl_region_destroy(region);
680
681 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
682 region = wl_compositor_create_region(c->parent.compositor);
683 wl_region_add(region, ix, iy, iwidth, iheight);
684 wl_surface_set_opaque_region(output->parent.surface, region);
685 wl_region_destroy(region);
686
687 width = frame_width(output->frame);
688 height = frame_height(output->frame);
689 } else {
690 region = wl_compositor_create_region(c->parent.compositor);
691 wl_region_add(region, 0, 0, width, height);
692 wl_surface_set_input_region(output->parent.surface, region);
693 wl_region_destroy(region);
694
695 region = wl_compositor_create_region(c->parent.compositor);
696 wl_region_add(region, 0, 0, width, height);
697 wl_surface_set_opaque_region(output->parent.surface, region);
698 wl_region_destroy(region);
699 }
700
701 if (output->gl.egl_window) {
702 wl_egl_window_resize(output->gl.egl_window,
703 width, height, 0, 0);
704
705 /* These will need to be re-created due to the resize */
706 gl_renderer->output_set_border(&output->base,
707 GL_RENDERER_BORDER_TOP,
708 0, 0, 0, NULL);
709 cairo_surface_destroy(output->gl.border.top);
710 output->gl.border.top = NULL;
711 gl_renderer->output_set_border(&output->base,
712 GL_RENDERER_BORDER_LEFT,
713 0, 0, 0, NULL);
714 cairo_surface_destroy(output->gl.border.left);
715 output->gl.border.left = NULL;
716 gl_renderer->output_set_border(&output->base,
717 GL_RENDERER_BORDER_RIGHT,
718 0, 0, 0, NULL);
719 cairo_surface_destroy(output->gl.border.right);
720 output->gl.border.right = NULL;
721 gl_renderer->output_set_border(&output->base,
722 GL_RENDERER_BORDER_BOTTOM,
723 0, 0, 0, NULL);
724 cairo_surface_destroy(output->gl.border.bottom);
725 output->gl.border.bottom = NULL;
726 }
727
728 /* Throw away any remaining SHM buffers */
729 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
730 wayland_shm_buffer_destroy(buffer);
731 /* These will get thrown away when they get released */
732 wl_list_for_each(buffer, &output->shm.buffers, link)
733 buffer->output = NULL;
734}
735
736static int
737wayland_output_set_windowed(struct wayland_output *output)
738{
739 struct wayland_compositor *c =
740 (struct wayland_compositor *)output->base.compositor;
741 int tlen;
742 char *title;
743
744 if (output->frame)
745 return 0;
746
747 if (output->name) {
748 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
749 title = malloc(tlen + 1);
750 if (!title)
751 return -1;
752
753 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
754 } else {
755 title = strdup(WINDOW_TITLE);
756 }
757
758 if (!c->theme) {
759 c->theme = theme_create();
760 if (!c->theme) {
761 free(title);
762 return -1;
763 }
764 }
765 output->frame = frame_create(c->theme, 100, 100,
766 FRAME_BUTTON_CLOSE, title);
767 free(title);
768 if (!output->frame)
769 return -1;
770
771 if (output->keyboard_count)
772 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
773
774 wayland_output_resize_surface(output);
775
776 wl_shell_surface_set_toplevel(output->parent.shell_surface);
777
778 return 0;
779}
780
781static void
782wayland_output_set_fullscreen(struct wayland_output *output,
783 enum wl_shell_surface_fullscreen_method method,
784 uint32_t framerate, struct wl_output *target)
785{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500786 struct wayland_compositor *c =
787 (struct wayland_compositor *)output->base.compositor;
788
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600789 if (output->frame) {
790 frame_destroy(output->frame);
791 output->frame = NULL;
792 }
793
794 wayland_output_resize_surface(output);
795
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500796 if (output->parent.shell_surface) {
797 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
798 method, framerate, target);
799 } else if (c->parent.fshell) {
800 _wl_fullscreen_shell_present_surface(c->parent.fshell,
801 output->parent.surface,
802 method, target);
803 }
804}
805
806static struct weston_mode *
807wayland_output_choose_mode(struct wayland_output *output,
808 struct weston_mode *ref_mode)
809{
810 struct weston_mode *mode;
811
812 /* First look for an exact match */
813 wl_list_for_each(mode, &output->base.mode_list, link)
814 if (mode->width == ref_mode->width &&
815 mode->height == ref_mode->height &&
816 mode->refresh == ref_mode->refresh)
817 return mode;
818
819 /* If we can't find an exact match, ignore refresh and try again */
820 wl_list_for_each(mode, &output->base.mode_list, link)
821 if (mode->width == ref_mode->width &&
822 mode->height == ref_mode->height)
823 return mode;
824
825 /* Yeah, we failed */
826 return NULL;
827}
828
829enum mode_status {
830 MODE_STATUS_UNKNOWN,
831 MODE_STATUS_SUCCESS,
832 MODE_STATUS_FAIL,
833 MODE_STATUS_CANCEL,
834};
835
836static void
837mode_feedback_successful(void *data,
838 struct _wl_fullscreen_shell_mode_feedback *fb)
839{
840 enum mode_status *value = data;
841
842 printf("Mode switch successful\n");
843
844 *value = MODE_STATUS_SUCCESS;
845}
846
847static void
848mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
849{
850 enum mode_status *value = data;
851
852 printf("Mode switch failed\n");
853
854 *value = MODE_STATUS_FAIL;
855}
856
857static void
858mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
859{
860 enum mode_status *value = data;
861
862 printf("Mode switch cancelled\n");
863
864 *value = MODE_STATUS_CANCEL;
865}
866
867struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
868 mode_feedback_successful,
869 mode_feedback_failed,
870 mode_feedback_cancelled,
871};
872
873static int
874wayland_output_switch_mode(struct weston_output *output_base,
875 struct weston_mode *mode)
876{
877 struct wayland_output *output = (struct wayland_output *) output_base;
878 struct wayland_compositor *c;
879 struct wl_surface *old_surface;
880 struct weston_mode *old_mode;
881 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
882 enum mode_status mode_status;
883 int ret = 0;
884
885 if (output_base == NULL) {
886 weston_log("output is NULL.\n");
887 return -1;
888 }
889
890 if (mode == NULL) {
891 weston_log("mode is NULL.\n");
892 return -1;
893 }
894
895 c = (struct wayland_compositor *)output_base->compositor;
896
897 if (output->parent.shell_surface || !c->parent.fshell)
898 return -1;
899
900 mode = wayland_output_choose_mode(output, mode);
901 if (mode == NULL)
902 return -1;
903
904 if (output->base.current_mode == mode)
905 return 0;
906
907 old_mode = output->base.current_mode;
908 old_surface = output->parent.surface;
909 output->base.current_mode = mode;
910 output->parent.surface =
911 wl_compositor_create_surface(c->parent.compositor);
912 wl_surface_set_user_data(output->parent.surface, output);
913
914 /* Blow the old buffers because we changed size/surfaces */
915 wayland_output_resize_surface(output);
916
917 mode_feedback =
918 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
919 output->parent.surface,
920 output->parent.output,
921 mode->refresh);
922 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
923 &mode_feedback_listener,
924 &mode_status);
925
926 /* This should kick-start things again */
927 output->parent.draw_initial_frame = 1;
928 wayland_output_start_repaint_loop(&output->base);
929
930 mode_status = MODE_STATUS_UNKNOWN;
931 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
932 ret = wl_display_dispatch(c->parent.wl_display);
933
934 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
935
936 if (mode_status == MODE_STATUS_FAIL) {
937 output->base.current_mode = old_mode;
938 wl_surface_destroy(output->parent.surface);
939 output->parent.surface = old_surface;
940 wayland_output_resize_surface(output);
941
942 return -1;
943 }
944
945 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
946 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
947
948 if (c->use_pixman) {
949 pixman_renderer_output_destroy(output_base);
950 if (wayland_output_init_pixman_renderer(output) < 0)
951 goto err_output;
952 } else {
953 gl_renderer->output_destroy(output_base);
954 wl_egl_window_destroy(output->gl.egl_window);
955 if (wayland_output_init_gl_renderer(output) < 0)
956 goto err_output;
957 }
958 wl_surface_destroy(old_surface);
959
960 weston_output_schedule_repaint(&output->base);
961
962 return 0;
963
964err_output:
965 /* XXX */
966 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600967}
968
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500969static struct wayland_output *
970wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600971 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500972 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100973{
974 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500975 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600976
977 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
978 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100979
Peter Huttererf3d62272013-08-08 11:57:05 +1000980 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100981 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500982 return NULL;
983
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600984 output->name = name ? strdup(name) : NULL;
985 output->base.make = "waywayland";
986 output->base.model = "none";
987
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500988 output_width = width * scale;
989 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100990
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600991 output->parent.surface =
992 wl_compositor_create_surface(c->parent.compositor);
993 if (!output->parent.surface)
994 goto err_name;
995 wl_surface_set_user_data(output->parent.surface, output);
996
997 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600998
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500999 if (c->parent.shell) {
1000 output->parent.shell_surface =
1001 wl_shell_get_shell_surface(c->parent.shell,
1002 output->parent.surface);
1003 if (!output->parent.shell_surface)
1004 goto err_surface;
1005 wl_shell_surface_add_listener(output->parent.shell_surface,
1006 &shell_surface_listener, output);
1007 }
1008
1009 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001010 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1011 0, 0, NULL);
1012 wl_display_roundtrip(c->parent.wl_display);
1013 if (!width)
1014 output_width = output->parent.configure_width;
1015 if (!height)
1016 output_height = output->parent.configure_height;
1017 }
1018
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001019 output->mode.flags =
1020 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001021 output->mode.width = output_width;
1022 output->mode.height = output_height;
1023 output->mode.refresh = 60000;
1024 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001025 wl_list_init(&output->base.mode_list);
1026 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001027 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001028
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001029 wl_list_init(&output->shm.buffers);
1030 wl_list_init(&output->shm.free_buffers);
1031
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001032 weston_output_init(&output->base, &c->base, x, y, width, height,
1033 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001034
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001035 if (c->use_pixman) {
1036 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001037 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001038 output->base.repaint = wayland_output_repaint_pixman;
1039 } else {
1040 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001041 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001042 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001043 }
1044
Jonas Ådahle5a12252013-04-05 23:07:11 +02001045 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001046 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001047 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001048 output->base.set_backlight = NULL;
1049 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001050 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001051
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001052 wl_list_insert(c->base.output_list.prev, &output->base.link);
1053
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001054 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001055
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001056err_output:
1057 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001058 if (output->parent.shell_surface)
1059 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001060err_surface:
1061 wl_surface_destroy(output->parent.surface);
1062err_name:
1063 free(output->name);
1064
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001065 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001066 free(output);
1067
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001068 return NULL;
1069}
1070
1071static struct wayland_output *
1072wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001073 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001074 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001075 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001076{
1077 struct wayland_output *output;
1078 char *mode, *t, *name, *str;
1079 int width, height, scale;
1080 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001081 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001082
1083 weston_config_section_get_string(config_section, "name", &name, NULL);
1084 if (name) {
1085 slen = strlen(name);
1086 slen += strlen(WINDOW_TITLE " - ");
1087 str = malloc(slen + 1);
1088 if (str)
1089 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1090 free(name);
1091 name = str;
1092 }
1093 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001094 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001095
1096 weston_config_section_get_string(config_section,
1097 "mode", &mode, "1024x600");
1098 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1099 weston_log("Invalid mode \"%s\" for output %s\n",
1100 mode, name);
1101 width = 1024;
1102 height = 640;
1103 }
1104 free(mode);
1105
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001106 if (option_width)
1107 width = option_width;
1108 if (option_height)
1109 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001110
1111 weston_config_section_get_int(config_section, "scale", &scale, 1);
1112
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001113 if (option_scale)
1114 scale = option_scale;
1115
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001116 weston_config_section_get_string(config_section,
1117 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001118 if (weston_parse_transform(t, &transform) < 0)
1119 weston_log("Invalid transform \"%s\" for output %s\n",
1120 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001121 free(t);
1122
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001123 output = wayland_output_create(c, x, y, width, height, name, 0,
1124 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001125 free(name);
1126
1127 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001128}
1129
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001130static struct wayland_output *
1131wayland_output_create_for_parent_output(struct wayland_compositor *c,
1132 struct wayland_parent_output *poutput)
1133{
1134 struct wayland_output *output;
1135 struct weston_mode *mode;
1136 int32_t x;
1137
1138 if (poutput->current_mode) {
1139 mode = poutput->current_mode;
1140 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001141 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001142 } else if (!wl_list_empty(&poutput->mode_list)) {
1143 mode = container_of(poutput->mode_list.next,
1144 struct weston_mode, link);
1145 } else {
1146 weston_log("No valid modes found. Skipping output");
1147 return NULL;
1148 }
1149
1150 if (!wl_list_empty(&c->base.output_list)) {
1151 output = container_of(c->base.output_list.prev,
1152 struct wayland_output, base.link);
1153 x = output->base.x + output->base.current_mode->width;
1154 } else {
1155 x = 0;
1156 }
1157
1158 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1159 NULL, 0,
1160 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1161 if (!output)
1162 return NULL;
1163
1164 output->parent.output = poutput->global;
1165
1166 output->base.make = poutput->physical.make;
1167 output->base.model = poutput->physical.model;
1168 wl_list_init(&output->base.mode_list);
1169 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1170 wl_list_init(&poutput->mode_list);
1171
1172 wayland_output_set_fullscreen(output,
1173 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1174 mode->refresh, poutput->global);
1175
1176 if (output->parent.shell_surface) {
1177 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1178 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1179 mode->refresh, poutput->global);
1180 } else if (c->parent.fshell) {
1181 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1182 output->parent.surface,
1183 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1184 poutput->global);
1185 _wl_fullscreen_shell_mode_feedback_destroy(
1186 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1187 output->parent.surface,
1188 poutput->global,
1189 mode->refresh));
1190 }
1191
1192 return output;
1193}
1194
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001195static void
1196shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1197 uint32_t serial)
1198{
1199 wl_shell_surface_pong(shell_surface, serial);
1200}
1201
1202static void
1203shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1204 uint32_t edges, int32_t width, int32_t height)
1205{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001206 struct wayland_output *output = data;
1207
1208 output->parent.configure_width = width;
1209 output->parent.configure_height = height;
1210
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001211 /* FIXME: implement resizing */
1212}
1213
1214static void
1215shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1216{
1217}
1218
1219static const struct wl_shell_surface_listener shell_surface_listener = {
1220 shell_surface_ping,
1221 shell_surface_configure,
1222 shell_surface_popup_done
1223};
1224
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001225/* Events received from the wayland-server this compositor is client of: */
1226
Jason Ekstrand7744f712013-10-27 22:24:55 -05001227/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001228static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001229input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001230{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001231
Jason Ekstrand7744f712013-10-27 22:24:55 -05001232 struct wl_buffer *buffer;
1233 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001234
Jason Ekstrand7744f712013-10-27 22:24:55 -05001235 if (!input->compositor->cursor)
1236 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001237
Jason Ekstrand7744f712013-10-27 22:24:55 -05001238 image = input->compositor->cursor->images[0];
1239 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001240 if (!buffer)
1241 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001242
1243 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1244 input->parent.cursor.surface,
1245 image->hotspot_x, image->hotspot_y);
1246
1247 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1248 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1249 image->width, image->height);
1250 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001251}
1252
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001253static void
Daniel Stone37816df2012-05-16 18:45:18 +01001254input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1255 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001256 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001257{
1258 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001259 int32_t fx, fy;
1260 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001261
Daniel Stone50692802012-06-22 13:21:41 +01001262 /* XXX: If we get a modifier event immediately before the focus,
1263 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001264 input->enter_serial = serial;
1265 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001266
1267 if (input->output->frame) {
1268 location = frame_pointer_enter(input->output->frame, input,
1269 wl_fixed_to_int(x),
1270 wl_fixed_to_int(y));
1271 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1272 x -= wl_fixed_from_int(fx);
1273 y -= wl_fixed_from_int(fy);
1274
1275 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1276 weston_output_schedule_repaint(&input->output->base);
1277 } else {
1278 location = THEME_LOCATION_CLIENT_AREA;
1279 }
1280
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001281 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001282
1283 if (location == THEME_LOCATION_CLIENT_AREA) {
1284 input->focus = 1;
1285 notify_pointer_focus(&input->base, &input->output->base, x, y);
1286 wl_pointer_set_cursor(input->parent.pointer,
1287 input->enter_serial, NULL, 0, 0);
1288 } else {
1289 input->focus = 0;
1290 notify_pointer_focus(&input->base, NULL, 0, 0);
1291 input_set_cursor(input);
1292 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001293}
1294
1295static void
Daniel Stone37816df2012-05-16 18:45:18 +01001296input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1297 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001298{
1299 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001300
Jason Ekstrand7744f712013-10-27 22:24:55 -05001301 if (input->output->frame) {
1302 frame_pointer_leave(input->output->frame, input);
1303
1304 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1305 weston_output_schedule_repaint(&input->output->base);
1306 }
1307
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001308 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001309 input->output = NULL;
1310 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001311}
1312
1313static void
Daniel Stone37816df2012-05-16 18:45:18 +01001314input_handle_motion(void *data, struct wl_pointer *pointer,
1315 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1316{
1317 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001318 int32_t fx, fy;
1319 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001320
Jason Ekstrand7744f712013-10-27 22:24:55 -05001321 if (input->output->frame) {
1322 location = frame_pointer_motion(input->output->frame, input,
1323 wl_fixed_to_int(x),
1324 wl_fixed_to_int(y));
1325 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1326 x -= wl_fixed_from_int(fx);
1327 y -= wl_fixed_from_int(fy);
1328
1329 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1330 weston_output_schedule_repaint(&input->output->base);
1331 } else {
1332 location = THEME_LOCATION_CLIENT_AREA;
1333 }
1334
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001335 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336
1337 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1338 input_set_cursor(input);
1339 notify_pointer_focus(&input->base, NULL, 0, 0);
1340 input->focus = 0;
1341 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1342 wl_pointer_set_cursor(input->parent.pointer,
1343 input->enter_serial, NULL, 0, 0);
1344 notify_pointer_focus(&input->base, &input->output->base, x, y);
1345 input->focus = 1;
1346 }
1347
1348 if (location == THEME_LOCATION_CLIENT_AREA)
1349 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001350}
1351
1352static void
1353input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001354 uint32_t serial, uint32_t time, uint32_t button,
1355 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001356{
1357 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001358 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001359 enum frame_button_state fstate;
1360 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001361
Jason Ekstrand7744f712013-10-27 22:24:55 -05001362 if (input->output->frame) {
1363 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1364 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1365
1366 location = frame_pointer_button(input->output->frame, input,
1367 button, fstate);
1368
1369 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1370
1371 wl_shell_surface_move(input->output->parent.shell_surface,
1372 input->parent.seat, serial);
1373 frame_status_clear(input->output->frame,
1374 FRAME_STATUS_MOVE);
1375 return;
1376 }
1377
1378 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1379 wl_display_terminate(input->compositor->base.wl_display);
1380
1381 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1382 weston_output_schedule_repaint(&input->output->base);
1383 } else {
1384 location = THEME_LOCATION_CLIENT_AREA;
1385 }
1386
1387 if (location == THEME_LOCATION_CLIENT_AREA)
1388 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001389}
1390
1391static void
1392input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001393 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001394{
1395 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001396
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001397 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001398}
1399
1400static const struct wl_pointer_listener pointer_listener = {
1401 input_handle_pointer_enter,
1402 input_handle_pointer_leave,
1403 input_handle_motion,
1404 input_handle_button,
1405 input_handle_axis,
1406};
1407
1408static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001409input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1410 int fd, uint32_t size)
1411{
1412 struct wayland_input *input = data;
1413 struct xkb_keymap *keymap;
1414 char *map_str;
1415
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001416 if (!data) {
1417 close(fd);
1418 return;
1419 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001420
1421 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1422 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1423 if (map_str == MAP_FAILED) {
1424 weston_log("mmap failed: %m\n");
1425 goto error;
1426 }
1427
Ran Benita2e1968f2014-08-19 23:59:51 +03001428 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1429 map_str,
1430 XKB_KEYMAP_FORMAT_TEXT_V1,
1431 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001432 munmap(map_str, size);
1433
1434 if (!keymap) {
1435 weston_log("failed to compile keymap\n");
1436 goto error;
1437 }
1438
1439 input->keyboard_state_update = STATE_UPDATE_NONE;
1440 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1441 weston_log("No keymap provided; falling back to defalt\n");
1442 keymap = NULL;
1443 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1444 } else {
1445 weston_log("Invalid keymap\n");
1446 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001447 }
1448
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001449 close(fd);
1450
Rui Matos0c194ce2013-10-10 19:44:21 +02001451 if (input->base.keyboard)
1452 weston_seat_update_keymap(&input->base, keymap);
1453 else
1454 weston_seat_init_keyboard(&input->base, keymap);
1455
Ran Benitac9c74152014-08-19 23:59:52 +03001456 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001457
1458 return;
1459
1460error:
1461 wl_keyboard_release(input->parent.keyboard);
1462 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001463}
1464
1465static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001466input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001467 struct wl_keyboard *keyboard,
1468 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001469 struct wl_surface *surface,
1470 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001471{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001472 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001473 struct wayland_output *focus;
1474
1475 focus = input->keyboard_focus;
1476 if (focus) {
1477 /* This shouldn't happen */
1478 focus->keyboard_count--;
1479 if (!focus->keyboard_count && focus->frame)
1480 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1481 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1482 weston_output_schedule_repaint(&focus->base);
1483 }
1484
1485 input->keyboard_focus = wl_surface_get_user_data(surface);
1486 input->keyboard_focus->keyboard_count++;
1487
1488 focus = input->keyboard_focus;
1489 if (focus->frame) {
1490 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1491 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1492 weston_output_schedule_repaint(&focus->base);
1493 }
1494
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001495
Daniel Stone50692802012-06-22 13:21:41 +01001496 /* XXX: If we get a modifier event immediately before the focus,
1497 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001498 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001499 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001500}
1501
1502static void
1503input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001504 struct wl_keyboard *keyboard,
1505 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001506 struct wl_surface *surface)
1507{
1508 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001509 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001510
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001511 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001512
1513 focus = input->keyboard_focus;
1514 if (!focus)
1515 return; /* This shouldn't happen */
1516
1517 focus->keyboard_count--;
1518 if (!focus->keyboard_count && focus->frame) {
1519 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1520 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1521 weston_output_schedule_repaint(&focus->base);
1522 }
1523
1524 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001525}
1526
Daniel Stone37816df2012-05-16 18:45:18 +01001527static void
1528input_handle_key(void *data, struct wl_keyboard *keyboard,
1529 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1530{
1531 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001532
Daniel Stone50692802012-06-22 13:21:41 +01001533 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001534 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001535 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001536 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001537 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001538}
1539
Daniel Stone351eb612012-05-31 15:27:47 -04001540static void
1541input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001542 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001543 uint32_t mods_latched, uint32_t mods_locked,
1544 uint32_t group)
1545{
Daniel Stone50692802012-06-22 13:21:41 +01001546 struct wayland_input *input = data;
1547 struct wayland_compositor *c = input->compositor;
1548 uint32_t serial_out;
1549
1550 /* If we get a key event followed by a modifier event with the
1551 * same serial number, then we try to preserve those semantics by
1552 * reusing the same serial number on the way out too. */
1553 if (serial_in == input->key_serial)
1554 serial_out = wl_display_get_serial(c->base.wl_display);
1555 else
1556 serial_out = wl_display_next_serial(c->base.wl_display);
1557
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001558 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001559 mods_depressed, mods_latched,
1560 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001561 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001562}
1563
Jonny Lamb497994a2014-08-12 14:58:26 +02001564static void
1565input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1566 int32_t rate, int32_t delay)
1567{
1568 struct wayland_input *input = data;
1569 struct wayland_compositor *c = input->compositor;
1570
1571 c->base.kb_repeat_rate = rate;
1572 c->base.kb_repeat_delay = delay;
1573}
1574
Daniel Stone37816df2012-05-16 18:45:18 +01001575static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001576 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001577 input_handle_keyboard_enter,
1578 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001579 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001580 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001581 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001582};
1583
1584static void
Daniel Stone37816df2012-05-16 18:45:18 +01001585input_handle_capabilities(void *data, struct wl_seat *seat,
1586 enum wl_seat_capability caps)
1587{
1588 struct wayland_input *input = data;
1589
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001590 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1591 input->parent.pointer = wl_seat_get_pointer(seat);
1592 wl_pointer_set_user_data(input->parent.pointer, input);
1593 wl_pointer_add_listener(input->parent.pointer,
1594 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001595 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001596 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1597 wl_pointer_destroy(input->parent.pointer);
1598 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001599 }
1600
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001601 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1602 input->parent.keyboard = wl_seat_get_keyboard(seat);
1603 wl_keyboard_set_user_data(input->parent.keyboard, input);
1604 wl_keyboard_add_listener(input->parent.keyboard,
1605 &keyboard_listener, input);
1606 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1607 wl_keyboard_destroy(input->parent.keyboard);
1608 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001609 }
1610}
1611
Jonny Lamb497994a2014-08-12 14:58:26 +02001612static void
1613input_handle_name(void *data, struct wl_seat *seat,
1614 const char *name)
1615{
1616}
1617
Daniel Stone37816df2012-05-16 18:45:18 +01001618static const struct wl_seat_listener seat_listener = {
1619 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001620 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001621};
1622
1623static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001624display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001625{
1626 struct wayland_input *input;
1627
Peter Huttererf3d62272013-08-08 11:57:05 +10001628 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001629 if (input == NULL)
1630 return;
1631
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001632 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001633 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001634 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001635 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001636 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001637
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001638 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1639 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001640
1641 input->parent.cursor.surface =
1642 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001643}
1644
1645static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001646wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1647 int32_t x, int32_t y,
1648 int32_t physical_width, int32_t physical_height,
1649 int32_t subpixel, const char *make,
1650 const char *model, int32_t transform)
1651{
1652 struct wayland_parent_output *output = data;
1653
1654 output->x = x;
1655 output->y = y;
1656 output->physical.width = physical_width;
1657 output->physical.height = physical_height;
1658 output->physical.subpixel = subpixel;
1659
1660 free(output->physical.make);
1661 output->physical.make = strdup(make);
1662 free(output->physical.model);
1663 output->physical.model = strdup(model);
1664
1665 output->transform = transform;
1666}
1667
1668static struct weston_mode *
1669find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1670{
1671 struct weston_mode *mode;
1672
1673 wl_list_for_each(mode, list, link) {
1674 if (mode->width == width && mode->height == height &&
1675 mode->refresh == refresh)
1676 return mode;
1677 }
1678
1679 mode = zalloc(sizeof *mode);
1680 if (!mode)
1681 return NULL;
1682
1683 mode->width = width;
1684 mode->height = height;
1685 mode->refresh = refresh;
1686 wl_list_insert(list, &mode->link);
1687
1688 return mode;
1689}
1690
1691static void
1692wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1693 uint32_t flags, int32_t width, int32_t height,
1694 int32_t refresh)
1695{
1696 struct wayland_parent_output *output = data;
1697 struct weston_mode *mode;
1698
1699 if (output->output) {
1700 mode = find_mode(&output->output->base.mode_list,
1701 width, height, refresh);
1702 if (!mode)
1703 return;
1704 mode->flags = flags;
1705 /* Do a mode-switch on current mode change? */
1706 } else {
1707 mode = find_mode(&output->mode_list, width, height, refresh);
1708 if (!mode)
1709 return;
1710 mode->flags = flags;
1711 if (flags & WL_OUTPUT_MODE_CURRENT)
1712 output->current_mode = mode;
1713 if (flags & WL_OUTPUT_MODE_PREFERRED)
1714 output->preferred_mode = mode;
1715 }
1716}
1717
1718static const struct wl_output_listener output_listener = {
1719 wayland_parent_output_geometry,
1720 wayland_parent_output_mode
1721};
1722
1723static void
1724wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1725{
1726 struct wayland_parent_output *output;
1727
1728 output = zalloc(sizeof *output);
1729 if (!output)
1730 return;
1731
1732 output->id = id;
1733 output->global = wl_registry_bind(c->parent.registry, id,
1734 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001735 if (!output->global) {
1736 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001737 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001738 }
1739
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001740 wl_output_add_listener(output->global, &output_listener, output);
1741
1742 output->scale = 0;
1743 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1744 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1745 wl_list_init(&output->mode_list);
1746 wl_list_insert(&c->parent.output_list, &output->link);
1747
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001748 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001749 wl_display_roundtrip(c->parent.wl_display);
1750 wayland_output_create_for_parent_output(c, output);
1751 }
1752}
1753
1754static void
1755wayland_parent_output_destroy(struct wayland_parent_output *output)
1756{
1757 struct weston_mode *mode, *next;
1758
1759 if (output->output)
1760 wayland_output_destroy(&output->output->base);
1761
1762 wl_output_destroy(output->global);
1763 free(output->physical.make);
1764 free(output->physical.model);
1765
1766 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1767 wl_list_remove(&mode->link);
1768 free(mode);
1769 }
1770}
1771
1772static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001773registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1774 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001775{
1776 struct wayland_compositor *c = data;
1777
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001778 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001779 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001780 wl_registry_bind(registry, name,
1781 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001782 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001783 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001784 wl_registry_bind(registry, name,
1785 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001786 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1787 c->parent.fshell =
1788 wl_registry_bind(registry, name,
1789 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001790 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001791 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001792 } else if (strcmp(interface, "wl_output") == 0) {
1793 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001794 } else if (strcmp(interface, "wl_shm") == 0) {
1795 c->parent.shm =
1796 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001797 }
1798}
1799
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001800static void
1801registry_handle_global_remove(void *data, struct wl_registry *registry,
1802 uint32_t name)
1803{
1804 struct wayland_compositor *c = data;
1805 struct wayland_parent_output *output;
1806
1807 wl_list_for_each(output, &c->parent.output_list, link)
1808 if (output->id == name)
1809 wayland_parent_output_destroy(output);
1810}
1811
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001812static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001813 registry_handle_global,
1814 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001815};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001816
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001817static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001818wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1819{
1820 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001821 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001822
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001823 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1824 wl_display_terminate(c->base.wl_display);
1825 return 0;
1826 }
1827
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001828 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001829 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001830 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001831 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001832
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001833 if (mask == 0) {
1834 count = wl_display_dispatch_pending(c->parent.wl_display);
1835 wl_display_flush(c->parent.wl_display);
1836 }
1837
1838 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001839}
1840
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001841static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001842wayland_restore(struct weston_compositor *ec)
1843{
1844}
1845
1846static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001847wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001848{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001849 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1850
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001851 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001852
Jonas Ådahle5a12252013-04-05 23:07:11 +02001853 if (c->parent.shm)
1854 wl_shm_destroy(c->parent.shm);
1855
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001856 free(ec);
1857}
1858
Jason Ekstrand7744f712013-10-27 22:24:55 -05001859static const char *left_ptrs[] = {
1860 "left_ptr",
1861 "default",
1862 "top_left_arrow",
1863 "left-arrow"
1864};
1865
1866static void
1867create_cursor(struct wayland_compositor *c, struct weston_config *config)
1868{
1869 struct weston_config_section *s;
1870 int size;
1871 char *theme = NULL;
1872 unsigned int i;
1873
1874 s = weston_config_get_section(config, "shell", NULL, NULL);
1875 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1876 weston_config_section_get_int(s, "cursor-size", &size, 32);
1877
1878 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001879 if (!c->cursor_theme) {
1880 fprintf(stderr, "could not load cursor theme\n");
1881 return;
1882 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001883
U. Artie Eoffff755002014-01-17 12:36:58 -08001884 free(theme);
1885
Jason Ekstrand7744f712013-10-27 22:24:55 -05001886 c->cursor = NULL;
1887 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1888 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1889 left_ptrs[i]);
1890 if (!c->cursor) {
1891 fprintf(stderr, "could not load left cursor\n");
1892 return;
1893 }
1894}
1895
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001896static void
1897fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1898 void *data)
1899{
1900 struct wayland_compositor *c = data;
1901 struct wayland_input *input = NULL;
1902
1903 wl_list_for_each(input, &c->input_list, link)
1904 if (&input->base == seat_base)
1905 break;
1906
1907 if (!input || !input->output)
1908 return;
1909
1910 if (input->output->frame)
1911 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1912 else
1913 wayland_output_set_windowed(input->output);
1914
1915 weston_output_schedule_repaint(&input->output->base);
1916}
1917
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001918static struct wayland_compositor *
1919wayland_compositor_create(struct wl_display *display, int use_pixman,
1920 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001921 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001922{
1923 struct wayland_compositor *c;
1924 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001925 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001926
Peter Huttererf3d62272013-08-08 11:57:05 +10001927 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001928 if (c == NULL)
1929 return NULL;
1930
Daniel Stone725c2c32012-06-22 14:04:36 +01001931 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001932 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001933 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001934
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001935 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1936 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001937
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001938 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001939 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001940 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001941 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001942 }
1943
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001944 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001945 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001946 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1947 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001948 wl_display_roundtrip(c->parent.wl_display);
1949
1950 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001951
1952 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001953
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001954 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001955
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001956 if (!c->use_pixman) {
1957 gl_renderer = weston_load_module("gl-renderer.so",
1958 "gl_renderer_interface");
1959 if (!gl_renderer)
1960 c->use_pixman = 1;
1961 }
1962
1963 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001964 if (gl_renderer->create(&c->base, c->parent.wl_display,
1965 gl_renderer->alpha_attribs,
1966 NULL) < 0) {
1967 weston_log("Failed to initialize the GL renderer; "
1968 "falling back to pixman.\n");
1969 c->use_pixman = 1;
1970 }
1971 }
1972
1973 if (c->use_pixman) {
1974 if (pixman_renderer_init(&c->base) < 0) {
1975 weston_log("Failed to initialize pixman renderer\n");
1976 goto err_display;
1977 }
1978 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001979
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001980 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001981 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001982
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001983 loop = wl_display_get_event_loop(c->base.wl_display);
1984
1985 fd = wl_display_get_fd(c->parent.wl_display);
1986 c->parent.wl_source =
1987 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1988 wayland_compositor_handle_event, c);
1989 if (c->parent.wl_source == NULL)
1990 goto err_renderer;
1991
1992 wl_event_source_check(c->parent.wl_source);
1993
1994 return c;
1995err_renderer:
1996 c->base.renderer->destroy(&c->base);
1997err_display:
1998 wl_display_disconnect(c->parent.wl_display);
1999err_compositor:
2000 weston_compositor_shutdown(&c->base);
2001err_free:
2002 free(c);
2003 return NULL;
2004}
2005
2006static void
2007wayland_compositor_destroy(struct wayland_compositor *c)
2008{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002009 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002010
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002011 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002012 wayland_output_destroy(output);
2013
2014 c->base.renderer->destroy(&c->base);
2015 wl_display_disconnect(c->parent.wl_display);
2016
2017 if (c->theme)
2018 theme_destroy(c->theme);
2019 if (c->frame_device)
2020 cairo_device_destroy(c->frame_device);
2021 wl_cursor_theme_destroy(c->cursor_theme);
2022
2023 weston_compositor_shutdown(&c->base);
2024 free(c);
2025}
2026
2027WL_EXPORT struct weston_compositor *
2028backend_init(struct wl_display *display, int *argc, char *argv[],
2029 struct weston_config *config)
2030{
2031 struct wayland_compositor *c;
2032 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002033 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002034 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002035 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002036 const char *section_name, *display_name;
2037 char *name;
2038
2039 const struct weston_option wayland_options[] = {
2040 { WESTON_OPTION_INTEGER, "width", 0, &width },
2041 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002042 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002043 { WESTON_OPTION_STRING, "display", 0, &display_name },
2044 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2045 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002046 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002047 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002048 };
2049
2050 width = 0;
2051 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002052 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002053 display_name = NULL;
2054 use_pixman = 0;
2055 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002056 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002057 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002058 parse_options(wayland_options,
2059 ARRAY_LENGTH(wayland_options), argc, argv);
2060
2061 c = wayland_compositor_create(display, use_pixman, display_name,
2062 argc, argv, config);
2063 if (!c)
2064 return NULL;
2065
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002066 if (sprawl || c->parent.fshell) {
2067 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002068 wl_display_roundtrip(c->parent.wl_display);
2069
2070 wl_list_for_each(poutput, &c->parent.output_list, link)
2071 wayland_output_create_for_parent_output(c, poutput);
2072
2073 return &c->base;
2074 }
2075
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002076 if (fullscreen) {
2077 output = wayland_output_create(c, 0, 0, width, height,
2078 NULL, 1, 0, 1);
2079 if (!output)
2080 goto err_outputs;
2081
Axel Davydd8b88d2013-11-17 21:34:16 +01002082 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002083 return &c->base;
2084 }
2085
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002086 section = NULL;
2087 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002088 while (weston_config_next_section(config, &section, &section_name)) {
2089 if (!section_name || strcmp(section_name, "output") != 0)
2090 continue;
2091 weston_config_section_get_string(section, "name", &name, NULL);
2092 if (name == NULL)
2093 continue;
2094
2095 if (name[0] != 'W' || name[1] != 'L') {
2096 free(name);
2097 continue;
2098 }
2099 free(name);
2100
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002101 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002102 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002103 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002104 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002105 if (wayland_output_set_windowed(output))
2106 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002107
2108 x += output->base.width;
2109 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002110 }
2111
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002112 if (!width)
2113 width = 1024;
2114 if (!height)
2115 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002116 if (!scale)
2117 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002118 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002119 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002120 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002121 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002122 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002123 if (wayland_output_set_windowed(output))
2124 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002125
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002126 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002127 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002128 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002129
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002130 weston_compositor_add_key_binding(&c->base, KEY_F,
2131 MODIFIER_CTRL | MODIFIER_ALT,
2132 fullscreen_binding, c);
2133
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002134 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002135
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002136err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002137 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002138 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002139}