blob: 7e59ecd5c3fdc7f659b4ae86c41bf7ac979f69a5 [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,
Jonny Lamb671148f2015-03-20 15:26:52 +0100642 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000643 gl_renderer->alpha_attribs,
644 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500645 goto cleanup_window;
646
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500647 return 0;
648
649cleanup_window:
650 wl_egl_window_destroy(output->gl.egl_window);
651 return -1;
652}
653
654static int
655wayland_output_init_pixman_renderer(struct wayland_output *output)
656{
657 return pixman_renderer_output_create(&output->base);
658}
659
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600660static void
661wayland_output_resize_surface(struct wayland_output *output)
662{
663 struct wayland_compositor *c =
664 (struct wayland_compositor *)output->base.compositor;
665 struct wayland_shm_buffer *buffer, *next;
666 int32_t ix, iy, iwidth, iheight;
667 int32_t width, height;
668 struct wl_region *region;
669
670 width = output->base.current_mode->width;
671 height = output->base.current_mode->height;
672
673 if (output->frame) {
674 frame_resize_inside(output->frame, width, height);
675
676 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
677 region = wl_compositor_create_region(c->parent.compositor);
678 wl_region_add(region, ix, iy, iwidth, iheight);
679 wl_surface_set_input_region(output->parent.surface, region);
680 wl_region_destroy(region);
681
682 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
683 region = wl_compositor_create_region(c->parent.compositor);
684 wl_region_add(region, ix, iy, iwidth, iheight);
685 wl_surface_set_opaque_region(output->parent.surface, region);
686 wl_region_destroy(region);
687
688 width = frame_width(output->frame);
689 height = frame_height(output->frame);
690 } else {
691 region = wl_compositor_create_region(c->parent.compositor);
692 wl_region_add(region, 0, 0, width, height);
693 wl_surface_set_input_region(output->parent.surface, region);
694 wl_region_destroy(region);
695
696 region = wl_compositor_create_region(c->parent.compositor);
697 wl_region_add(region, 0, 0, width, height);
698 wl_surface_set_opaque_region(output->parent.surface, region);
699 wl_region_destroy(region);
700 }
701
702 if (output->gl.egl_window) {
703 wl_egl_window_resize(output->gl.egl_window,
704 width, height, 0, 0);
705
706 /* These will need to be re-created due to the resize */
707 gl_renderer->output_set_border(&output->base,
708 GL_RENDERER_BORDER_TOP,
709 0, 0, 0, NULL);
710 cairo_surface_destroy(output->gl.border.top);
711 output->gl.border.top = NULL;
712 gl_renderer->output_set_border(&output->base,
713 GL_RENDERER_BORDER_LEFT,
714 0, 0, 0, NULL);
715 cairo_surface_destroy(output->gl.border.left);
716 output->gl.border.left = NULL;
717 gl_renderer->output_set_border(&output->base,
718 GL_RENDERER_BORDER_RIGHT,
719 0, 0, 0, NULL);
720 cairo_surface_destroy(output->gl.border.right);
721 output->gl.border.right = NULL;
722 gl_renderer->output_set_border(&output->base,
723 GL_RENDERER_BORDER_BOTTOM,
724 0, 0, 0, NULL);
725 cairo_surface_destroy(output->gl.border.bottom);
726 output->gl.border.bottom = NULL;
727 }
728
729 /* Throw away any remaining SHM buffers */
730 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
731 wayland_shm_buffer_destroy(buffer);
732 /* These will get thrown away when they get released */
733 wl_list_for_each(buffer, &output->shm.buffers, link)
734 buffer->output = NULL;
735}
736
737static int
738wayland_output_set_windowed(struct wayland_output *output)
739{
740 struct wayland_compositor *c =
741 (struct wayland_compositor *)output->base.compositor;
742 int tlen;
743 char *title;
744
745 if (output->frame)
746 return 0;
747
748 if (output->name) {
749 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
750 title = malloc(tlen + 1);
751 if (!title)
752 return -1;
753
754 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
755 } else {
756 title = strdup(WINDOW_TITLE);
757 }
758
759 if (!c->theme) {
760 c->theme = theme_create();
761 if (!c->theme) {
762 free(title);
763 return -1;
764 }
765 }
766 output->frame = frame_create(c->theme, 100, 100,
767 FRAME_BUTTON_CLOSE, title);
768 free(title);
769 if (!output->frame)
770 return -1;
771
772 if (output->keyboard_count)
773 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
774
775 wayland_output_resize_surface(output);
776
777 wl_shell_surface_set_toplevel(output->parent.shell_surface);
778
779 return 0;
780}
781
782static void
783wayland_output_set_fullscreen(struct wayland_output *output,
784 enum wl_shell_surface_fullscreen_method method,
785 uint32_t framerate, struct wl_output *target)
786{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500787 struct wayland_compositor *c =
788 (struct wayland_compositor *)output->base.compositor;
789
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600790 if (output->frame) {
791 frame_destroy(output->frame);
792 output->frame = NULL;
793 }
794
795 wayland_output_resize_surface(output);
796
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500797 if (output->parent.shell_surface) {
798 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
799 method, framerate, target);
800 } else if (c->parent.fshell) {
801 _wl_fullscreen_shell_present_surface(c->parent.fshell,
802 output->parent.surface,
803 method, target);
804 }
805}
806
807static struct weston_mode *
808wayland_output_choose_mode(struct wayland_output *output,
809 struct weston_mode *ref_mode)
810{
811 struct weston_mode *mode;
812
813 /* First look for an exact match */
814 wl_list_for_each(mode, &output->base.mode_list, link)
815 if (mode->width == ref_mode->width &&
816 mode->height == ref_mode->height &&
817 mode->refresh == ref_mode->refresh)
818 return mode;
819
820 /* If we can't find an exact match, ignore refresh and try again */
821 wl_list_for_each(mode, &output->base.mode_list, link)
822 if (mode->width == ref_mode->width &&
823 mode->height == ref_mode->height)
824 return mode;
825
826 /* Yeah, we failed */
827 return NULL;
828}
829
830enum mode_status {
831 MODE_STATUS_UNKNOWN,
832 MODE_STATUS_SUCCESS,
833 MODE_STATUS_FAIL,
834 MODE_STATUS_CANCEL,
835};
836
837static void
838mode_feedback_successful(void *data,
839 struct _wl_fullscreen_shell_mode_feedback *fb)
840{
841 enum mode_status *value = data;
842
843 printf("Mode switch successful\n");
844
845 *value = MODE_STATUS_SUCCESS;
846}
847
848static void
849mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
850{
851 enum mode_status *value = data;
852
853 printf("Mode switch failed\n");
854
855 *value = MODE_STATUS_FAIL;
856}
857
858static void
859mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
860{
861 enum mode_status *value = data;
862
863 printf("Mode switch cancelled\n");
864
865 *value = MODE_STATUS_CANCEL;
866}
867
868struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
869 mode_feedback_successful,
870 mode_feedback_failed,
871 mode_feedback_cancelled,
872};
873
874static int
875wayland_output_switch_mode(struct weston_output *output_base,
876 struct weston_mode *mode)
877{
878 struct wayland_output *output = (struct wayland_output *) output_base;
879 struct wayland_compositor *c;
880 struct wl_surface *old_surface;
881 struct weston_mode *old_mode;
882 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
883 enum mode_status mode_status;
884 int ret = 0;
885
886 if (output_base == NULL) {
887 weston_log("output is NULL.\n");
888 return -1;
889 }
890
891 if (mode == NULL) {
892 weston_log("mode is NULL.\n");
893 return -1;
894 }
895
896 c = (struct wayland_compositor *)output_base->compositor;
897
898 if (output->parent.shell_surface || !c->parent.fshell)
899 return -1;
900
901 mode = wayland_output_choose_mode(output, mode);
902 if (mode == NULL)
903 return -1;
904
905 if (output->base.current_mode == mode)
906 return 0;
907
908 old_mode = output->base.current_mode;
909 old_surface = output->parent.surface;
910 output->base.current_mode = mode;
911 output->parent.surface =
912 wl_compositor_create_surface(c->parent.compositor);
913 wl_surface_set_user_data(output->parent.surface, output);
914
915 /* Blow the old buffers because we changed size/surfaces */
916 wayland_output_resize_surface(output);
917
918 mode_feedback =
919 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
920 output->parent.surface,
921 output->parent.output,
922 mode->refresh);
923 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
924 &mode_feedback_listener,
925 &mode_status);
926
927 /* This should kick-start things again */
928 output->parent.draw_initial_frame = 1;
929 wayland_output_start_repaint_loop(&output->base);
930
931 mode_status = MODE_STATUS_UNKNOWN;
932 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
933 ret = wl_display_dispatch(c->parent.wl_display);
934
935 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
936
937 if (mode_status == MODE_STATUS_FAIL) {
938 output->base.current_mode = old_mode;
939 wl_surface_destroy(output->parent.surface);
940 output->parent.surface = old_surface;
941 wayland_output_resize_surface(output);
942
943 return -1;
944 }
945
946 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
947 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
948
949 if (c->use_pixman) {
950 pixman_renderer_output_destroy(output_base);
951 if (wayland_output_init_pixman_renderer(output) < 0)
952 goto err_output;
953 } else {
954 gl_renderer->output_destroy(output_base);
955 wl_egl_window_destroy(output->gl.egl_window);
956 if (wayland_output_init_gl_renderer(output) < 0)
957 goto err_output;
958 }
959 wl_surface_destroy(old_surface);
960
961 weston_output_schedule_repaint(&output->base);
962
963 return 0;
964
965err_output:
966 /* XXX */
967 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600968}
969
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500970static struct wayland_output *
971wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600972 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500973 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100974{
975 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500976 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600977
978 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
979 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100980
Peter Huttererf3d62272013-08-08 11:57:05 +1000981 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100982 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500983 return NULL;
984
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600985 output->name = name ? strdup(name) : NULL;
986 output->base.make = "waywayland";
987 output->base.model = "none";
988
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500989 output_width = width * scale;
990 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100991
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600992 output->parent.surface =
993 wl_compositor_create_surface(c->parent.compositor);
994 if (!output->parent.surface)
995 goto err_name;
996 wl_surface_set_user_data(output->parent.surface, output);
997
998 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600999
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001000 if (c->parent.shell) {
1001 output->parent.shell_surface =
1002 wl_shell_get_shell_surface(c->parent.shell,
1003 output->parent.surface);
1004 if (!output->parent.shell_surface)
1005 goto err_surface;
1006 wl_shell_surface_add_listener(output->parent.shell_surface,
1007 &shell_surface_listener, output);
1008 }
1009
1010 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001011 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1012 0, 0, NULL);
1013 wl_display_roundtrip(c->parent.wl_display);
1014 if (!width)
1015 output_width = output->parent.configure_width;
1016 if (!height)
1017 output_height = output->parent.configure_height;
1018 }
1019
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001020 output->mode.flags =
1021 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001022 output->mode.width = output_width;
1023 output->mode.height = output_height;
1024 output->mode.refresh = 60000;
1025 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001026 wl_list_init(&output->base.mode_list);
1027 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001028 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001029
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001030 wl_list_init(&output->shm.buffers);
1031 wl_list_init(&output->shm.free_buffers);
1032
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001033 weston_output_init(&output->base, &c->base, x, y, width, height,
1034 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001035
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001036 if (c->use_pixman) {
1037 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001038 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001039 output->base.repaint = wayland_output_repaint_pixman;
1040 } else {
1041 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001042 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001043 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001044 }
1045
Jonas Ådahle5a12252013-04-05 23:07:11 +02001046 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001047 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001048 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001049 output->base.set_backlight = NULL;
1050 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001051 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001052
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001053 wl_list_insert(c->base.output_list.prev, &output->base.link);
1054
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001055 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001056
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001057err_output:
1058 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001059 if (output->parent.shell_surface)
1060 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001061err_surface:
1062 wl_surface_destroy(output->parent.surface);
1063err_name:
1064 free(output->name);
1065
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001066 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001067 free(output);
1068
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001069 return NULL;
1070}
1071
1072static struct wayland_output *
1073wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001074 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001075 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001076 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001077{
1078 struct wayland_output *output;
1079 char *mode, *t, *name, *str;
1080 int width, height, scale;
1081 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001082 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001083
1084 weston_config_section_get_string(config_section, "name", &name, NULL);
1085 if (name) {
1086 slen = strlen(name);
1087 slen += strlen(WINDOW_TITLE " - ");
1088 str = malloc(slen + 1);
1089 if (str)
1090 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1091 free(name);
1092 name = str;
1093 }
1094 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001095 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001096
1097 weston_config_section_get_string(config_section,
1098 "mode", &mode, "1024x600");
1099 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1100 weston_log("Invalid mode \"%s\" for output %s\n",
1101 mode, name);
1102 width = 1024;
1103 height = 640;
1104 }
1105 free(mode);
1106
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001107 if (option_width)
1108 width = option_width;
1109 if (option_height)
1110 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001111
1112 weston_config_section_get_int(config_section, "scale", &scale, 1);
1113
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001114 if (option_scale)
1115 scale = option_scale;
1116
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001117 weston_config_section_get_string(config_section,
1118 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001119 if (weston_parse_transform(t, &transform) < 0)
1120 weston_log("Invalid transform \"%s\" for output %s\n",
1121 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001122 free(t);
1123
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001124 output = wayland_output_create(c, x, y, width, height, name, 0,
1125 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001126 free(name);
1127
1128 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001129}
1130
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001131static struct wayland_output *
1132wayland_output_create_for_parent_output(struct wayland_compositor *c,
1133 struct wayland_parent_output *poutput)
1134{
1135 struct wayland_output *output;
1136 struct weston_mode *mode;
1137 int32_t x;
1138
1139 if (poutput->current_mode) {
1140 mode = poutput->current_mode;
1141 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001142 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001143 } else if (!wl_list_empty(&poutput->mode_list)) {
1144 mode = container_of(poutput->mode_list.next,
1145 struct weston_mode, link);
1146 } else {
1147 weston_log("No valid modes found. Skipping output");
1148 return NULL;
1149 }
1150
1151 if (!wl_list_empty(&c->base.output_list)) {
1152 output = container_of(c->base.output_list.prev,
1153 struct wayland_output, base.link);
1154 x = output->base.x + output->base.current_mode->width;
1155 } else {
1156 x = 0;
1157 }
1158
1159 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1160 NULL, 0,
1161 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1162 if (!output)
1163 return NULL;
1164
1165 output->parent.output = poutput->global;
1166
1167 output->base.make = poutput->physical.make;
1168 output->base.model = poutput->physical.model;
1169 wl_list_init(&output->base.mode_list);
1170 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1171 wl_list_init(&poutput->mode_list);
1172
1173 wayland_output_set_fullscreen(output,
1174 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1175 mode->refresh, poutput->global);
1176
1177 if (output->parent.shell_surface) {
1178 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1179 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1180 mode->refresh, poutput->global);
1181 } else if (c->parent.fshell) {
1182 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1183 output->parent.surface,
1184 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1185 poutput->global);
1186 _wl_fullscreen_shell_mode_feedback_destroy(
1187 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1188 output->parent.surface,
1189 poutput->global,
1190 mode->refresh));
1191 }
1192
1193 return output;
1194}
1195
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001196static void
1197shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1198 uint32_t serial)
1199{
1200 wl_shell_surface_pong(shell_surface, serial);
1201}
1202
1203static void
1204shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1205 uint32_t edges, int32_t width, int32_t height)
1206{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001207 struct wayland_output *output = data;
1208
1209 output->parent.configure_width = width;
1210 output->parent.configure_height = height;
1211
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001212 /* FIXME: implement resizing */
1213}
1214
1215static void
1216shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1217{
1218}
1219
1220static const struct wl_shell_surface_listener shell_surface_listener = {
1221 shell_surface_ping,
1222 shell_surface_configure,
1223 shell_surface_popup_done
1224};
1225
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001226/* Events received from the wayland-server this compositor is client of: */
1227
Jason Ekstrand7744f712013-10-27 22:24:55 -05001228/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001229static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001230input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001231{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001232
Jason Ekstrand7744f712013-10-27 22:24:55 -05001233 struct wl_buffer *buffer;
1234 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001235
Jason Ekstrand7744f712013-10-27 22:24:55 -05001236 if (!input->compositor->cursor)
1237 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001238
Jason Ekstrand7744f712013-10-27 22:24:55 -05001239 image = input->compositor->cursor->images[0];
1240 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001241 if (!buffer)
1242 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001243
1244 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1245 input->parent.cursor.surface,
1246 image->hotspot_x, image->hotspot_y);
1247
1248 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1249 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1250 image->width, image->height);
1251 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001252}
1253
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001254static void
Daniel Stone37816df2012-05-16 18:45:18 +01001255input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1256 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001257 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001258{
1259 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001260 int32_t fx, fy;
1261 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001262
Daniel Stone50692802012-06-22 13:21:41 +01001263 /* XXX: If we get a modifier event immediately before the focus,
1264 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001265 input->enter_serial = serial;
1266 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001267
1268 if (input->output->frame) {
1269 location = frame_pointer_enter(input->output->frame, input,
1270 wl_fixed_to_int(x),
1271 wl_fixed_to_int(y));
1272 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1273 x -= wl_fixed_from_int(fx);
1274 y -= wl_fixed_from_int(fy);
1275
1276 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1277 weston_output_schedule_repaint(&input->output->base);
1278 } else {
1279 location = THEME_LOCATION_CLIENT_AREA;
1280 }
1281
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001282 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001283
1284 if (location == THEME_LOCATION_CLIENT_AREA) {
1285 input->focus = 1;
1286 notify_pointer_focus(&input->base, &input->output->base, x, y);
1287 wl_pointer_set_cursor(input->parent.pointer,
1288 input->enter_serial, NULL, 0, 0);
1289 } else {
1290 input->focus = 0;
1291 notify_pointer_focus(&input->base, NULL, 0, 0);
1292 input_set_cursor(input);
1293 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001294}
1295
1296static void
Daniel Stone37816df2012-05-16 18:45:18 +01001297input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1298 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001299{
1300 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001301
Jason Ekstrand7744f712013-10-27 22:24:55 -05001302 if (input->output->frame) {
1303 frame_pointer_leave(input->output->frame, input);
1304
1305 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1306 weston_output_schedule_repaint(&input->output->base);
1307 }
1308
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001309 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001310 input->output = NULL;
1311 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001312}
1313
1314static void
Daniel Stone37816df2012-05-16 18:45:18 +01001315input_handle_motion(void *data, struct wl_pointer *pointer,
1316 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1317{
1318 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001319 int32_t fx, fy;
1320 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001321
Jason Ekstrand7744f712013-10-27 22:24:55 -05001322 if (input->output->frame) {
1323 location = frame_pointer_motion(input->output->frame, input,
1324 wl_fixed_to_int(x),
1325 wl_fixed_to_int(y));
1326 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1327 x -= wl_fixed_from_int(fx);
1328 y -= wl_fixed_from_int(fy);
1329
1330 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1331 weston_output_schedule_repaint(&input->output->base);
1332 } else {
1333 location = THEME_LOCATION_CLIENT_AREA;
1334 }
1335
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001336 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001337
1338 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1339 input_set_cursor(input);
1340 notify_pointer_focus(&input->base, NULL, 0, 0);
1341 input->focus = 0;
1342 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1343 wl_pointer_set_cursor(input->parent.pointer,
1344 input->enter_serial, NULL, 0, 0);
1345 notify_pointer_focus(&input->base, &input->output->base, x, y);
1346 input->focus = 1;
1347 }
1348
1349 if (location == THEME_LOCATION_CLIENT_AREA)
1350 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001351}
1352
1353static void
1354input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001355 uint32_t serial, uint32_t time, uint32_t button,
1356 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001357{
1358 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001359 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001360 enum frame_button_state fstate;
1361 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001362
Jason Ekstrand7744f712013-10-27 22:24:55 -05001363 if (input->output->frame) {
1364 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1365 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1366
1367 location = frame_pointer_button(input->output->frame, input,
1368 button, fstate);
1369
1370 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1371
1372 wl_shell_surface_move(input->output->parent.shell_surface,
1373 input->parent.seat, serial);
1374 frame_status_clear(input->output->frame,
1375 FRAME_STATUS_MOVE);
1376 return;
1377 }
1378
1379 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1380 wl_display_terminate(input->compositor->base.wl_display);
1381
1382 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1383 weston_output_schedule_repaint(&input->output->base);
1384 } else {
1385 location = THEME_LOCATION_CLIENT_AREA;
1386 }
1387
1388 if (location == THEME_LOCATION_CLIENT_AREA)
1389 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001390}
1391
1392static void
1393input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001394 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001395{
1396 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001397
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001398 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001399}
1400
1401static const struct wl_pointer_listener pointer_listener = {
1402 input_handle_pointer_enter,
1403 input_handle_pointer_leave,
1404 input_handle_motion,
1405 input_handle_button,
1406 input_handle_axis,
1407};
1408
1409static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001410input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1411 int fd, uint32_t size)
1412{
1413 struct wayland_input *input = data;
1414 struct xkb_keymap *keymap;
1415 char *map_str;
1416
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001417 if (!data) {
1418 close(fd);
1419 return;
1420 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001421
1422 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1423 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1424 if (map_str == MAP_FAILED) {
1425 weston_log("mmap failed: %m\n");
1426 goto error;
1427 }
1428
Ran Benita2e1968f2014-08-19 23:59:51 +03001429 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1430 map_str,
1431 XKB_KEYMAP_FORMAT_TEXT_V1,
1432 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001433 munmap(map_str, size);
1434
1435 if (!keymap) {
1436 weston_log("failed to compile keymap\n");
1437 goto error;
1438 }
1439
1440 input->keyboard_state_update = STATE_UPDATE_NONE;
1441 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1442 weston_log("No keymap provided; falling back to defalt\n");
1443 keymap = NULL;
1444 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1445 } else {
1446 weston_log("Invalid keymap\n");
1447 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001448 }
1449
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001450 close(fd);
1451
Rui Matos0c194ce2013-10-10 19:44:21 +02001452 if (input->base.keyboard)
1453 weston_seat_update_keymap(&input->base, keymap);
1454 else
1455 weston_seat_init_keyboard(&input->base, keymap);
1456
Ran Benitac9c74152014-08-19 23:59:52 +03001457 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001458
1459 return;
1460
1461error:
1462 wl_keyboard_release(input->parent.keyboard);
1463 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001464}
1465
1466static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001467input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001468 struct wl_keyboard *keyboard,
1469 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001470 struct wl_surface *surface,
1471 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001472{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001473 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001474 struct wayland_output *focus;
1475
1476 focus = input->keyboard_focus;
1477 if (focus) {
1478 /* This shouldn't happen */
1479 focus->keyboard_count--;
1480 if (!focus->keyboard_count && focus->frame)
1481 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1482 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1483 weston_output_schedule_repaint(&focus->base);
1484 }
1485
1486 input->keyboard_focus = wl_surface_get_user_data(surface);
1487 input->keyboard_focus->keyboard_count++;
1488
1489 focus = input->keyboard_focus;
1490 if (focus->frame) {
1491 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1492 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1493 weston_output_schedule_repaint(&focus->base);
1494 }
1495
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001496
Daniel Stone50692802012-06-22 13:21:41 +01001497 /* XXX: If we get a modifier event immediately before the focus,
1498 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001499 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001500 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001501}
1502
1503static void
1504input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001505 struct wl_keyboard *keyboard,
1506 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001507 struct wl_surface *surface)
1508{
1509 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001510 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001511
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001512 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001513
1514 focus = input->keyboard_focus;
1515 if (!focus)
1516 return; /* This shouldn't happen */
1517
1518 focus->keyboard_count--;
1519 if (!focus->keyboard_count && focus->frame) {
1520 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1521 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1522 weston_output_schedule_repaint(&focus->base);
1523 }
1524
1525 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001526}
1527
Daniel Stone37816df2012-05-16 18:45:18 +01001528static void
1529input_handle_key(void *data, struct wl_keyboard *keyboard,
1530 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1531{
1532 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001533
Daniel Stone50692802012-06-22 13:21:41 +01001534 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001535 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001536 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001537 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001538 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001539}
1540
Daniel Stone351eb612012-05-31 15:27:47 -04001541static void
1542input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001543 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001544 uint32_t mods_latched, uint32_t mods_locked,
1545 uint32_t group)
1546{
Daniel Stone50692802012-06-22 13:21:41 +01001547 struct wayland_input *input = data;
1548 struct wayland_compositor *c = input->compositor;
1549 uint32_t serial_out;
1550
1551 /* If we get a key event followed by a modifier event with the
1552 * same serial number, then we try to preserve those semantics by
1553 * reusing the same serial number on the way out too. */
1554 if (serial_in == input->key_serial)
1555 serial_out = wl_display_get_serial(c->base.wl_display);
1556 else
1557 serial_out = wl_display_next_serial(c->base.wl_display);
1558
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001559 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001560 mods_depressed, mods_latched,
1561 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001562 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001563}
1564
Jonny Lamb497994a2014-08-12 14:58:26 +02001565static void
1566input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1567 int32_t rate, int32_t delay)
1568{
1569 struct wayland_input *input = data;
1570 struct wayland_compositor *c = input->compositor;
1571
1572 c->base.kb_repeat_rate = rate;
1573 c->base.kb_repeat_delay = delay;
1574}
1575
Daniel Stone37816df2012-05-16 18:45:18 +01001576static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001577 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001578 input_handle_keyboard_enter,
1579 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001580 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001581 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001582 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001583};
1584
1585static void
Daniel Stone37816df2012-05-16 18:45:18 +01001586input_handle_capabilities(void *data, struct wl_seat *seat,
1587 enum wl_seat_capability caps)
1588{
1589 struct wayland_input *input = data;
1590
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001591 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1592 input->parent.pointer = wl_seat_get_pointer(seat);
1593 wl_pointer_set_user_data(input->parent.pointer, input);
1594 wl_pointer_add_listener(input->parent.pointer,
1595 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001596 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001597 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1598 wl_pointer_destroy(input->parent.pointer);
1599 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001600 }
1601
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001602 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1603 input->parent.keyboard = wl_seat_get_keyboard(seat);
1604 wl_keyboard_set_user_data(input->parent.keyboard, input);
1605 wl_keyboard_add_listener(input->parent.keyboard,
1606 &keyboard_listener, input);
1607 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1608 wl_keyboard_destroy(input->parent.keyboard);
1609 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001610 }
1611}
1612
Jonny Lamb497994a2014-08-12 14:58:26 +02001613static void
1614input_handle_name(void *data, struct wl_seat *seat,
1615 const char *name)
1616{
1617}
1618
Daniel Stone37816df2012-05-16 18:45:18 +01001619static const struct wl_seat_listener seat_listener = {
1620 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001621 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001622};
1623
1624static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001625display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001626{
1627 struct wayland_input *input;
1628
Peter Huttererf3d62272013-08-08 11:57:05 +10001629 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001630 if (input == NULL)
1631 return;
1632
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001633 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001634 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001635 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001636 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001637 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001638
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001639 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1640 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001641
1642 input->parent.cursor.surface =
1643 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001644}
1645
1646static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001647wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1648 int32_t x, int32_t y,
1649 int32_t physical_width, int32_t physical_height,
1650 int32_t subpixel, const char *make,
1651 const char *model, int32_t transform)
1652{
1653 struct wayland_parent_output *output = data;
1654
1655 output->x = x;
1656 output->y = y;
1657 output->physical.width = physical_width;
1658 output->physical.height = physical_height;
1659 output->physical.subpixel = subpixel;
1660
1661 free(output->physical.make);
1662 output->physical.make = strdup(make);
1663 free(output->physical.model);
1664 output->physical.model = strdup(model);
1665
1666 output->transform = transform;
1667}
1668
1669static struct weston_mode *
1670find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1671{
1672 struct weston_mode *mode;
1673
1674 wl_list_for_each(mode, list, link) {
1675 if (mode->width == width && mode->height == height &&
1676 mode->refresh == refresh)
1677 return mode;
1678 }
1679
1680 mode = zalloc(sizeof *mode);
1681 if (!mode)
1682 return NULL;
1683
1684 mode->width = width;
1685 mode->height = height;
1686 mode->refresh = refresh;
1687 wl_list_insert(list, &mode->link);
1688
1689 return mode;
1690}
1691
1692static void
1693wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1694 uint32_t flags, int32_t width, int32_t height,
1695 int32_t refresh)
1696{
1697 struct wayland_parent_output *output = data;
1698 struct weston_mode *mode;
1699
1700 if (output->output) {
1701 mode = find_mode(&output->output->base.mode_list,
1702 width, height, refresh);
1703 if (!mode)
1704 return;
1705 mode->flags = flags;
1706 /* Do a mode-switch on current mode change? */
1707 } else {
1708 mode = find_mode(&output->mode_list, width, height, refresh);
1709 if (!mode)
1710 return;
1711 mode->flags = flags;
1712 if (flags & WL_OUTPUT_MODE_CURRENT)
1713 output->current_mode = mode;
1714 if (flags & WL_OUTPUT_MODE_PREFERRED)
1715 output->preferred_mode = mode;
1716 }
1717}
1718
1719static const struct wl_output_listener output_listener = {
1720 wayland_parent_output_geometry,
1721 wayland_parent_output_mode
1722};
1723
1724static void
1725wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1726{
1727 struct wayland_parent_output *output;
1728
1729 output = zalloc(sizeof *output);
1730 if (!output)
1731 return;
1732
1733 output->id = id;
1734 output->global = wl_registry_bind(c->parent.registry, id,
1735 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001736 if (!output->global) {
1737 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001738 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001739 }
1740
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001741 wl_output_add_listener(output->global, &output_listener, output);
1742
1743 output->scale = 0;
1744 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1745 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1746 wl_list_init(&output->mode_list);
1747 wl_list_insert(&c->parent.output_list, &output->link);
1748
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001749 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001750 wl_display_roundtrip(c->parent.wl_display);
1751 wayland_output_create_for_parent_output(c, output);
1752 }
1753}
1754
1755static void
1756wayland_parent_output_destroy(struct wayland_parent_output *output)
1757{
1758 struct weston_mode *mode, *next;
1759
1760 if (output->output)
1761 wayland_output_destroy(&output->output->base);
1762
1763 wl_output_destroy(output->global);
1764 free(output->physical.make);
1765 free(output->physical.model);
1766
1767 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1768 wl_list_remove(&mode->link);
1769 free(mode);
1770 }
1771}
1772
1773static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001774registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1775 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001776{
1777 struct wayland_compositor *c = data;
1778
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001779 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001780 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001781 wl_registry_bind(registry, name,
1782 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001783 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001784 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001785 wl_registry_bind(registry, name,
1786 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001787 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1788 c->parent.fshell =
1789 wl_registry_bind(registry, name,
1790 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001791 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001792 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001793 } else if (strcmp(interface, "wl_output") == 0) {
1794 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001795 } else if (strcmp(interface, "wl_shm") == 0) {
1796 c->parent.shm =
1797 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001798 }
1799}
1800
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001801static void
1802registry_handle_global_remove(void *data, struct wl_registry *registry,
1803 uint32_t name)
1804{
1805 struct wayland_compositor *c = data;
1806 struct wayland_parent_output *output;
1807
1808 wl_list_for_each(output, &c->parent.output_list, link)
1809 if (output->id == name)
1810 wayland_parent_output_destroy(output);
1811}
1812
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001813static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001814 registry_handle_global,
1815 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001816};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001817
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001818static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001819wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1820{
1821 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001822 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001823
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001824 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1825 wl_display_terminate(c->base.wl_display);
1826 return 0;
1827 }
1828
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001829 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001830 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001831 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001832 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001833
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001834 if (mask == 0) {
1835 count = wl_display_dispatch_pending(c->parent.wl_display);
1836 wl_display_flush(c->parent.wl_display);
1837 }
1838
1839 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001840}
1841
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001842static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001843wayland_restore(struct weston_compositor *ec)
1844{
1845}
1846
1847static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001848wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001849{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001850 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1851
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001852 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001853
Jonas Ådahle5a12252013-04-05 23:07:11 +02001854 if (c->parent.shm)
1855 wl_shm_destroy(c->parent.shm);
1856
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001857 free(ec);
1858}
1859
Jason Ekstrand7744f712013-10-27 22:24:55 -05001860static const char *left_ptrs[] = {
1861 "left_ptr",
1862 "default",
1863 "top_left_arrow",
1864 "left-arrow"
1865};
1866
1867static void
1868create_cursor(struct wayland_compositor *c, struct weston_config *config)
1869{
1870 struct weston_config_section *s;
1871 int size;
1872 char *theme = NULL;
1873 unsigned int i;
1874
1875 s = weston_config_get_section(config, "shell", NULL, NULL);
1876 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1877 weston_config_section_get_int(s, "cursor-size", &size, 32);
1878
1879 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001880 if (!c->cursor_theme) {
1881 fprintf(stderr, "could not load cursor theme\n");
1882 return;
1883 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001884
U. Artie Eoffff755002014-01-17 12:36:58 -08001885 free(theme);
1886
Jason Ekstrand7744f712013-10-27 22:24:55 -05001887 c->cursor = NULL;
1888 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1889 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1890 left_ptrs[i]);
1891 if (!c->cursor) {
1892 fprintf(stderr, "could not load left cursor\n");
1893 return;
1894 }
1895}
1896
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001897static void
1898fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1899 void *data)
1900{
1901 struct wayland_compositor *c = data;
1902 struct wayland_input *input = NULL;
1903
1904 wl_list_for_each(input, &c->input_list, link)
1905 if (&input->base == seat_base)
1906 break;
1907
1908 if (!input || !input->output)
1909 return;
1910
1911 if (input->output->frame)
1912 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1913 else
1914 wayland_output_set_windowed(input->output);
1915
1916 weston_output_schedule_repaint(&input->output->base);
1917}
1918
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001919static struct wayland_compositor *
1920wayland_compositor_create(struct wl_display *display, int use_pixman,
1921 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001922 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001923{
1924 struct wayland_compositor *c;
1925 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001926 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001927
Peter Huttererf3d62272013-08-08 11:57:05 +10001928 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001929 if (c == NULL)
1930 return NULL;
1931
Daniel Stone725c2c32012-06-22 14:04:36 +01001932 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001933 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001934 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001935
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001936 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1937 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001938
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001939 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001940 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001941 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001942 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001943 }
1944
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001945 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001946 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001947 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1948 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001949 wl_display_roundtrip(c->parent.wl_display);
1950
1951 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001952
1953 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001954
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001955 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001956
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001957 if (!c->use_pixman) {
1958 gl_renderer = weston_load_module("gl-renderer.so",
1959 "gl_renderer_interface");
1960 if (!gl_renderer)
1961 c->use_pixman = 1;
1962 }
1963
1964 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001965 if (gl_renderer->create(&c->base,
Jonny Lamb70eba3f2015-03-20 15:26:50 +01001966 EGL_PLATFORM_WAYLAND_KHR,
1967 c->parent.wl_display,
1968 gl_renderer->alpha_attribs,
1969 NULL) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001970 weston_log("Failed to initialize the GL renderer; "
1971 "falling back to pixman.\n");
1972 c->use_pixman = 1;
1973 }
1974 }
1975
1976 if (c->use_pixman) {
1977 if (pixman_renderer_init(&c->base) < 0) {
1978 weston_log("Failed to initialize pixman renderer\n");
1979 goto err_display;
1980 }
1981 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001982
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001983 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001984 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001985
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001986 loop = wl_display_get_event_loop(c->base.wl_display);
1987
1988 fd = wl_display_get_fd(c->parent.wl_display);
1989 c->parent.wl_source =
1990 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1991 wayland_compositor_handle_event, c);
1992 if (c->parent.wl_source == NULL)
1993 goto err_renderer;
1994
1995 wl_event_source_check(c->parent.wl_source);
1996
1997 return c;
1998err_renderer:
1999 c->base.renderer->destroy(&c->base);
2000err_display:
2001 wl_display_disconnect(c->parent.wl_display);
2002err_compositor:
2003 weston_compositor_shutdown(&c->base);
2004err_free:
2005 free(c);
2006 return NULL;
2007}
2008
2009static void
2010wayland_compositor_destroy(struct wayland_compositor *c)
2011{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002012 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002013
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002014 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002015 wayland_output_destroy(output);
2016
2017 c->base.renderer->destroy(&c->base);
2018 wl_display_disconnect(c->parent.wl_display);
2019
2020 if (c->theme)
2021 theme_destroy(c->theme);
2022 if (c->frame_device)
2023 cairo_device_destroy(c->frame_device);
2024 wl_cursor_theme_destroy(c->cursor_theme);
2025
2026 weston_compositor_shutdown(&c->base);
2027 free(c);
2028}
2029
2030WL_EXPORT struct weston_compositor *
2031backend_init(struct wl_display *display, int *argc, char *argv[],
2032 struct weston_config *config)
2033{
2034 struct wayland_compositor *c;
2035 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002036 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002037 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002038 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002039 const char *section_name, *display_name;
2040 char *name;
2041
2042 const struct weston_option wayland_options[] = {
2043 { WESTON_OPTION_INTEGER, "width", 0, &width },
2044 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002045 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002046 { WESTON_OPTION_STRING, "display", 0, &display_name },
2047 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2048 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002049 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002050 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002051 };
2052
2053 width = 0;
2054 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002055 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002056 display_name = NULL;
2057 use_pixman = 0;
2058 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002059 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002060 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002061 parse_options(wayland_options,
2062 ARRAY_LENGTH(wayland_options), argc, argv);
2063
2064 c = wayland_compositor_create(display, use_pixman, display_name,
2065 argc, argv, config);
2066 if (!c)
2067 return NULL;
2068
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002069 if (sprawl || c->parent.fshell) {
2070 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002071 wl_display_roundtrip(c->parent.wl_display);
2072
2073 wl_list_for_each(poutput, &c->parent.output_list, link)
2074 wayland_output_create_for_parent_output(c, poutput);
2075
2076 return &c->base;
2077 }
2078
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002079 if (fullscreen) {
2080 output = wayland_output_create(c, 0, 0, width, height,
2081 NULL, 1, 0, 1);
2082 if (!output)
2083 goto err_outputs;
2084
Axel Davydd8b88d2013-11-17 21:34:16 +01002085 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002086 return &c->base;
2087 }
2088
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002089 section = NULL;
2090 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002091 while (weston_config_next_section(config, &section, &section_name)) {
2092 if (!section_name || strcmp(section_name, "output") != 0)
2093 continue;
2094 weston_config_section_get_string(section, "name", &name, NULL);
2095 if (name == NULL)
2096 continue;
2097
2098 if (name[0] != 'W' || name[1] != 'L') {
2099 free(name);
2100 continue;
2101 }
2102 free(name);
2103
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002104 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002105 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002106 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002107 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002108 if (wayland_output_set_windowed(output))
2109 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002110
2111 x += output->base.width;
2112 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002113 }
2114
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002115 if (!width)
2116 width = 1024;
2117 if (!height)
2118 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002119 if (!scale)
2120 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002121 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002122 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002123 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002124 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002125 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002126 if (wayland_output_set_windowed(output))
2127 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002128
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002129 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002130 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002131 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002132
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002133 weston_compositor_add_key_binding(&c->base, KEY_F,
2134 MODIFIER_CTRL | MODIFIER_ALT,
2135 fullscreen_binding, c);
2136
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002137 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002138
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002139err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002140 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002141 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002142}