blob: 935701ac21f585c3bb2ffe459c69f8fda2bb0b64 [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 */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300315
316 /*
317 * This is the fallback case, where Presentation extension is not
318 * available from the parent compositor. We do not know the base for
319 * 'time', so we cannot feed it to finish_frame(). Do the only thing
320 * we can, and pretend finish_frame time is when we process this
321 * event.
322 */
323 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200324 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400325}
326
327static const struct wl_callback_listener frame_listener = {
328 frame_done
329};
330
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500331static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200332draw_initial_frame(struct wayland_output *output)
333{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500334 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200335
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500336 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200337
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500338 /* If we are rendering with GL, then orphan it so that it gets
339 * destroyed immediately */
340 if (output->gl.egl_window)
341 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500342
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500343 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500344 wl_surface_damage(output->parent.surface, 0, 0,
345 output->base.current_mode->width,
346 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200347}
348
349static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500350wayland_output_update_gl_border(struct wayland_output *output)
351{
352 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
353 cairo_t *cr;
354
355 if (!output->frame)
356 return;
357 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
358 return;
359
360 fwidth = frame_width(output->frame);
361 fheight = frame_height(output->frame);
362 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
363
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500364 if (!output->gl.border.top)
365 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500366 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
367 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500369 frame_repaint(output->frame, cr);
370 cairo_destroy(cr);
371 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
372 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500373 cairo_image_surface_get_stride(output->gl.border.top) / 4,
374 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500375
376
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500377 if (!output->gl.border.left)
378 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500379 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
380 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500381 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382 cairo_translate(cr, 0, -iy);
383 frame_repaint(output->frame, cr);
384 cairo_destroy(cr);
385 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
386 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500387 cairo_image_surface_get_stride(output->gl.border.left) / 4,
388 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389
390
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500391 if (!output->gl.border.right)
392 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500393 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
394 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 cairo_translate(cr, -(iwidth + ix), -iy);
397 frame_repaint(output->frame, cr);
398 cairo_destroy(cr);
399 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
400 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500401 cairo_image_surface_get_stride(output->gl.border.right) / 4,
402 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500403
404
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500405 if (!output->gl.border.bottom)
406 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500407 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
408 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410 cairo_translate(cr, 0, -(iy + iheight));
411 frame_repaint(output->frame, cr);
412 cairo_destroy(cr);
413 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
414 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500415 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
416 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500417}
418
419static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200420wayland_output_start_repaint_loop(struct weston_output *output_base)
421{
422 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500423 struct wayland_compositor *wc =
424 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200425 struct wl_callback *callback;
426
427 /* If this is the initial frame, we need to attach a buffer so that
428 * the compositor can map the surface and include it in its render
429 * loop. If the surface doesn't end up in the render loop, the frame
430 * callback won't be invoked. The buffer is transparent and of the
431 * same size as the future real output buffer. */
432 if (output->parent.draw_initial_frame) {
433 output->parent.draw_initial_frame = 0;
434
435 draw_initial_frame(output);
436 }
437
438 callback = wl_surface_frame(output->parent.surface);
439 wl_callback_add_listener(callback, &frame_listener, output);
440 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500441 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200442}
443
David Herrmann1edf44c2013-10-22 17:11:26 +0200444static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500445wayland_output_repaint_gl(struct weston_output *output_base,
446 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400447{
448 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400449 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400450 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600451
Kristian Høgsberg33418202011-08-16 23:01:28 -0400452 callback = wl_surface_frame(output->parent.surface);
453 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100454
Jason Ekstrand7744f712013-10-27 22:24:55 -0500455 wayland_output_update_gl_border(output);
456
Pekka Paalanenbc106382012-10-10 12:49:31 +0300457 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200458
459 pixman_region32_subtract(&ec->primary_plane.damage,
460 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200461 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100462}
463
Matt Roper361d2ad2011-08-29 13:52:23 -0700464static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500465wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
466{
467 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
468 cairo_t *cr;
469
470 if (!buffer->output->frame || !buffer->frame_damaged)
471 return;
472
473 cr = cairo_create(buffer->c_surface);
474
475 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
476 fwidth = frame_width(buffer->output->frame);
477 fheight = frame_height(buffer->output->frame);
478
479 /* Set the clip so we don't unnecisaraly damage the surface */
480 cairo_move_to(cr, ix, iy);
481 cairo_rel_line_to(cr, iwidth, 0);
482 cairo_rel_line_to(cr, 0, iheight);
483 cairo_rel_line_to(cr, -iwidth, 0);
484 cairo_line_to(cr, ix, iy);
485 cairo_line_to(cr, 0, iy);
486 cairo_line_to(cr, 0, fheight);
487 cairo_line_to(cr, fwidth, fheight);
488 cairo_line_to(cr, fwidth, 0);
489 cairo_line_to(cr, 0, 0);
490 cairo_line_to(cr, 0, iy);
491 cairo_close_path(cr);
492 cairo_clip(cr);
493
494 /* Draw using a pattern so that the final result gets clipped */
495 cairo_push_group(cr);
496 frame_repaint(buffer->output->frame, cr);
497 cairo_pop_group_to_source(cr);
498 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
499 cairo_paint(cr);
500
501 cairo_destroy(cr);
502}
503
504static void
505wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
506{
507 pixman_region32_t damage;
508 pixman_box32_t *rects;
509 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
510 int i, n;
511
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500512 pixman_region32_init(&damage);
513 weston_transformed_region(sb->output->base.width,
514 sb->output->base.height,
515 sb->output->base.transform,
516 sb->output->base.current_scale,
517 &sb->damage, &damage);
518
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500519 if (sb->output->frame) {
520 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
521 fwidth = frame_width(sb->output->frame);
522 fheight = frame_height(sb->output->frame);
523
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500524 pixman_region32_translate(&damage, ix, iy);
525
526 if (sb->frame_damaged) {
527 pixman_region32_union_rect(&damage, &damage,
528 0, 0, fwidth, iy);
529 pixman_region32_union_rect(&damage, &damage,
530 0, iy, ix, iheight);
531 pixman_region32_union_rect(&damage, &damage,
532 ix + iwidth, iy,
533 fwidth - (ix + iwidth), iheight);
534 pixman_region32_union_rect(&damage, &damage,
535 0, iy + iheight,
536 fwidth, fheight - (iy + iheight));
537 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500538 }
539
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500540 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500541 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
542 for (i = 0; i < n; ++i)
543 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
544 rects[i].y1, rects[i].x2 - rects[i].x1,
545 rects[i].y2 - rects[i].y1);
546
547 if (sb->output->frame)
548 pixman_region32_fini(&damage);
549}
550
551static int
552wayland_output_repaint_pixman(struct weston_output *output_base,
553 pixman_region32_t *damage)
554{
555 struct wayland_output *output = (struct wayland_output *) output_base;
556 struct wayland_compositor *c =
557 (struct wayland_compositor *)output->base.compositor;
558 struct wl_callback *callback;
559 struct wayland_shm_buffer *sb;
560
561 if (output->frame) {
562 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
563 wl_list_for_each(sb, &output->shm.buffers, link)
564 sb->frame_damaged = 1;
565 }
566
567 wl_list_for_each(sb, &output->shm.buffers, link)
568 pixman_region32_union(&sb->damage, &sb->damage, damage);
569
570 sb = wayland_output_get_shm_buffer(output);
571
572 wayland_output_update_shm_border(sb);
573 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
574 c->base.renderer->repaint_output(output_base, &sb->damage);
575
576 wayland_shm_buffer_attach(sb);
577
578 callback = wl_surface_frame(output->parent.surface);
579 wl_callback_add_listener(callback, &frame_listener, output);
580 wl_surface_commit(output->parent.surface);
581 wl_display_flush(c->parent.wl_display);
582
583 pixman_region32_fini(&sb->damage);
584 pixman_region32_init(&sb->damage);
585 sb->frame_damaged = 0;
586
587 pixman_region32_subtract(&c->base.primary_plane.damage,
588 &c->base.primary_plane.damage, damage);
589 return 0;
590}
591
592static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700594{
595 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500596 struct wayland_compositor *c =
597 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700598
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500599 if (c->use_pixman) {
600 pixman_renderer_output_destroy(output_base);
601 } else {
602 gl_renderer->output_destroy(output_base);
603 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100604
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500605 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500606 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500607 if (output->parent.shell_surface)
608 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500609
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600610 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500611 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600612
613 cairo_surface_destroy(output->gl.border.top);
614 cairo_surface_destroy(output->gl.border.left);
615 cairo_surface_destroy(output->gl.border.right);
616 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500617
618 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700619 free(output);
620
621 return;
622}
623
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300624static const struct wl_shell_surface_listener shell_surface_listener;
625
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200626static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500627wayland_output_init_gl_renderer(struct wayland_output *output)
628{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500629 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630
631 if (output->frame) {
632 fwidth = frame_width(output->frame);
633 fheight = frame_height(output->frame);
634 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500635 fwidth = output->base.current_mode->width;
636 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500637 }
638
639 output->gl.egl_window =
640 wl_egl_window_create(output->parent.surface,
641 fwidth, fheight);
642 if (!output->gl.egl_window) {
643 weston_log("failure to create wl_egl_window\n");
644 return -1;
645 }
646
647 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000648 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100649 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000650 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500651 NULL,
652 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500653 goto cleanup_window;
654
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500655 return 0;
656
657cleanup_window:
658 wl_egl_window_destroy(output->gl.egl_window);
659 return -1;
660}
661
662static int
663wayland_output_init_pixman_renderer(struct wayland_output *output)
664{
665 return pixman_renderer_output_create(&output->base);
666}
667
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600668static void
669wayland_output_resize_surface(struct wayland_output *output)
670{
671 struct wayland_compositor *c =
672 (struct wayland_compositor *)output->base.compositor;
673 struct wayland_shm_buffer *buffer, *next;
674 int32_t ix, iy, iwidth, iheight;
675 int32_t width, height;
676 struct wl_region *region;
677
678 width = output->base.current_mode->width;
679 height = output->base.current_mode->height;
680
681 if (output->frame) {
682 frame_resize_inside(output->frame, width, height);
683
684 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
685 region = wl_compositor_create_region(c->parent.compositor);
686 wl_region_add(region, ix, iy, iwidth, iheight);
687 wl_surface_set_input_region(output->parent.surface, region);
688 wl_region_destroy(region);
689
690 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
691 region = wl_compositor_create_region(c->parent.compositor);
692 wl_region_add(region, ix, iy, iwidth, iheight);
693 wl_surface_set_opaque_region(output->parent.surface, region);
694 wl_region_destroy(region);
695
696 width = frame_width(output->frame);
697 height = frame_height(output->frame);
698 } else {
699 region = wl_compositor_create_region(c->parent.compositor);
700 wl_region_add(region, 0, 0, width, height);
701 wl_surface_set_input_region(output->parent.surface, region);
702 wl_region_destroy(region);
703
704 region = wl_compositor_create_region(c->parent.compositor);
705 wl_region_add(region, 0, 0, width, height);
706 wl_surface_set_opaque_region(output->parent.surface, region);
707 wl_region_destroy(region);
708 }
709
710 if (output->gl.egl_window) {
711 wl_egl_window_resize(output->gl.egl_window,
712 width, height, 0, 0);
713
714 /* These will need to be re-created due to the resize */
715 gl_renderer->output_set_border(&output->base,
716 GL_RENDERER_BORDER_TOP,
717 0, 0, 0, NULL);
718 cairo_surface_destroy(output->gl.border.top);
719 output->gl.border.top = NULL;
720 gl_renderer->output_set_border(&output->base,
721 GL_RENDERER_BORDER_LEFT,
722 0, 0, 0, NULL);
723 cairo_surface_destroy(output->gl.border.left);
724 output->gl.border.left = NULL;
725 gl_renderer->output_set_border(&output->base,
726 GL_RENDERER_BORDER_RIGHT,
727 0, 0, 0, NULL);
728 cairo_surface_destroy(output->gl.border.right);
729 output->gl.border.right = NULL;
730 gl_renderer->output_set_border(&output->base,
731 GL_RENDERER_BORDER_BOTTOM,
732 0, 0, 0, NULL);
733 cairo_surface_destroy(output->gl.border.bottom);
734 output->gl.border.bottom = NULL;
735 }
736
737 /* Throw away any remaining SHM buffers */
738 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
739 wayland_shm_buffer_destroy(buffer);
740 /* These will get thrown away when they get released */
741 wl_list_for_each(buffer, &output->shm.buffers, link)
742 buffer->output = NULL;
743}
744
745static int
746wayland_output_set_windowed(struct wayland_output *output)
747{
748 struct wayland_compositor *c =
749 (struct wayland_compositor *)output->base.compositor;
750 int tlen;
751 char *title;
752
753 if (output->frame)
754 return 0;
755
756 if (output->name) {
757 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
758 title = malloc(tlen + 1);
759 if (!title)
760 return -1;
761
762 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
763 } else {
764 title = strdup(WINDOW_TITLE);
765 }
766
767 if (!c->theme) {
768 c->theme = theme_create();
769 if (!c->theme) {
770 free(title);
771 return -1;
772 }
773 }
774 output->frame = frame_create(c->theme, 100, 100,
775 FRAME_BUTTON_CLOSE, title);
776 free(title);
777 if (!output->frame)
778 return -1;
779
780 if (output->keyboard_count)
781 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
782
783 wayland_output_resize_surface(output);
784
785 wl_shell_surface_set_toplevel(output->parent.shell_surface);
786
787 return 0;
788}
789
790static void
791wayland_output_set_fullscreen(struct wayland_output *output,
792 enum wl_shell_surface_fullscreen_method method,
793 uint32_t framerate, struct wl_output *target)
794{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500795 struct wayland_compositor *c =
796 (struct wayland_compositor *)output->base.compositor;
797
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600798 if (output->frame) {
799 frame_destroy(output->frame);
800 output->frame = NULL;
801 }
802
803 wayland_output_resize_surface(output);
804
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500805 if (output->parent.shell_surface) {
806 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
807 method, framerate, target);
808 } else if (c->parent.fshell) {
809 _wl_fullscreen_shell_present_surface(c->parent.fshell,
810 output->parent.surface,
811 method, target);
812 }
813}
814
815static struct weston_mode *
816wayland_output_choose_mode(struct wayland_output *output,
817 struct weston_mode *ref_mode)
818{
819 struct weston_mode *mode;
820
821 /* First look for an exact match */
822 wl_list_for_each(mode, &output->base.mode_list, link)
823 if (mode->width == ref_mode->width &&
824 mode->height == ref_mode->height &&
825 mode->refresh == ref_mode->refresh)
826 return mode;
827
828 /* If we can't find an exact match, ignore refresh and try again */
829 wl_list_for_each(mode, &output->base.mode_list, link)
830 if (mode->width == ref_mode->width &&
831 mode->height == ref_mode->height)
832 return mode;
833
834 /* Yeah, we failed */
835 return NULL;
836}
837
838enum mode_status {
839 MODE_STATUS_UNKNOWN,
840 MODE_STATUS_SUCCESS,
841 MODE_STATUS_FAIL,
842 MODE_STATUS_CANCEL,
843};
844
845static void
846mode_feedback_successful(void *data,
847 struct _wl_fullscreen_shell_mode_feedback *fb)
848{
849 enum mode_status *value = data;
850
851 printf("Mode switch successful\n");
852
853 *value = MODE_STATUS_SUCCESS;
854}
855
856static void
857mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
858{
859 enum mode_status *value = data;
860
861 printf("Mode switch failed\n");
862
863 *value = MODE_STATUS_FAIL;
864}
865
866static void
867mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
868{
869 enum mode_status *value = data;
870
871 printf("Mode switch cancelled\n");
872
873 *value = MODE_STATUS_CANCEL;
874}
875
876struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
877 mode_feedback_successful,
878 mode_feedback_failed,
879 mode_feedback_cancelled,
880};
881
882static int
883wayland_output_switch_mode(struct weston_output *output_base,
884 struct weston_mode *mode)
885{
886 struct wayland_output *output = (struct wayland_output *) output_base;
887 struct wayland_compositor *c;
888 struct wl_surface *old_surface;
889 struct weston_mode *old_mode;
890 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
891 enum mode_status mode_status;
892 int ret = 0;
893
894 if (output_base == NULL) {
895 weston_log("output is NULL.\n");
896 return -1;
897 }
898
899 if (mode == NULL) {
900 weston_log("mode is NULL.\n");
901 return -1;
902 }
903
904 c = (struct wayland_compositor *)output_base->compositor;
905
906 if (output->parent.shell_surface || !c->parent.fshell)
907 return -1;
908
909 mode = wayland_output_choose_mode(output, mode);
910 if (mode == NULL)
911 return -1;
912
913 if (output->base.current_mode == mode)
914 return 0;
915
916 old_mode = output->base.current_mode;
917 old_surface = output->parent.surface;
918 output->base.current_mode = mode;
919 output->parent.surface =
920 wl_compositor_create_surface(c->parent.compositor);
921 wl_surface_set_user_data(output->parent.surface, output);
922
923 /* Blow the old buffers because we changed size/surfaces */
924 wayland_output_resize_surface(output);
925
926 mode_feedback =
927 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
928 output->parent.surface,
929 output->parent.output,
930 mode->refresh);
931 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
932 &mode_feedback_listener,
933 &mode_status);
934
935 /* This should kick-start things again */
936 output->parent.draw_initial_frame = 1;
937 wayland_output_start_repaint_loop(&output->base);
938
939 mode_status = MODE_STATUS_UNKNOWN;
940 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
941 ret = wl_display_dispatch(c->parent.wl_display);
942
943 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
944
945 if (mode_status == MODE_STATUS_FAIL) {
946 output->base.current_mode = old_mode;
947 wl_surface_destroy(output->parent.surface);
948 output->parent.surface = old_surface;
949 wayland_output_resize_surface(output);
950
951 return -1;
952 }
953
954 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
955 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
956
957 if (c->use_pixman) {
958 pixman_renderer_output_destroy(output_base);
959 if (wayland_output_init_pixman_renderer(output) < 0)
960 goto err_output;
961 } else {
962 gl_renderer->output_destroy(output_base);
963 wl_egl_window_destroy(output->gl.egl_window);
964 if (wayland_output_init_gl_renderer(output) < 0)
965 goto err_output;
966 }
967 wl_surface_destroy(old_surface);
968
969 weston_output_schedule_repaint(&output->base);
970
971 return 0;
972
973err_output:
974 /* XXX */
975 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600976}
977
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500978static struct wayland_output *
979wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600980 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500981 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100982{
983 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500984 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600985
986 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
987 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100988
Peter Huttererf3d62272013-08-08 11:57:05 +1000989 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100990 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500991 return NULL;
992
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600993 output->name = name ? strdup(name) : NULL;
994 output->base.make = "waywayland";
995 output->base.model = "none";
996
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500997 output_width = width * scale;
998 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100999
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001000 output->parent.surface =
1001 wl_compositor_create_surface(c->parent.compositor);
1002 if (!output->parent.surface)
1003 goto err_name;
1004 wl_surface_set_user_data(output->parent.surface, output);
1005
1006 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001007
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001008 if (c->parent.shell) {
1009 output->parent.shell_surface =
1010 wl_shell_get_shell_surface(c->parent.shell,
1011 output->parent.surface);
1012 if (!output->parent.shell_surface)
1013 goto err_surface;
1014 wl_shell_surface_add_listener(output->parent.shell_surface,
1015 &shell_surface_listener, output);
1016 }
1017
1018 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001019 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1020 0, 0, NULL);
1021 wl_display_roundtrip(c->parent.wl_display);
1022 if (!width)
1023 output_width = output->parent.configure_width;
1024 if (!height)
1025 output_height = output->parent.configure_height;
1026 }
1027
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001028 output->mode.flags =
1029 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001030 output->mode.width = output_width;
1031 output->mode.height = output_height;
1032 output->mode.refresh = 60000;
1033 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001034 wl_list_init(&output->base.mode_list);
1035 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001036 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001037
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001038 wl_list_init(&output->shm.buffers);
1039 wl_list_init(&output->shm.free_buffers);
1040
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001041 weston_output_init(&output->base, &c->base, x, y, width, height,
1042 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001043
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001044 if (c->use_pixman) {
1045 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001046 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001047 output->base.repaint = wayland_output_repaint_pixman;
1048 } else {
1049 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001050 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001051 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001052 }
1053
Jonas Ådahle5a12252013-04-05 23:07:11 +02001054 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001055 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001056 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001057 output->base.set_backlight = NULL;
1058 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001059 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001060
Giulio Camuffob1147152015-05-06 21:41:57 +03001061 weston_compositor_add_output(&c->base, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001062
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001063 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001064
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001065err_output:
1066 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001067 if (output->parent.shell_surface)
1068 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001069err_surface:
1070 wl_surface_destroy(output->parent.surface);
1071err_name:
1072 free(output->name);
1073
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001074 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001075 free(output);
1076
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001077 return NULL;
1078}
1079
1080static struct wayland_output *
1081wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001082 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001083 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001084 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001085{
1086 struct wayland_output *output;
1087 char *mode, *t, *name, *str;
1088 int width, height, scale;
1089 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001090 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001091
1092 weston_config_section_get_string(config_section, "name", &name, NULL);
1093 if (name) {
1094 slen = strlen(name);
1095 slen += strlen(WINDOW_TITLE " - ");
1096 str = malloc(slen + 1);
1097 if (str)
1098 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1099 free(name);
1100 name = str;
1101 }
1102 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001103 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001104
1105 weston_config_section_get_string(config_section,
1106 "mode", &mode, "1024x600");
1107 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1108 weston_log("Invalid mode \"%s\" for output %s\n",
1109 mode, name);
1110 width = 1024;
1111 height = 640;
1112 }
1113 free(mode);
1114
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001115 if (option_width)
1116 width = option_width;
1117 if (option_height)
1118 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001119
1120 weston_config_section_get_int(config_section, "scale", &scale, 1);
1121
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001122 if (option_scale)
1123 scale = option_scale;
1124
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001125 weston_config_section_get_string(config_section,
1126 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001127 if (weston_parse_transform(t, &transform) < 0)
1128 weston_log("Invalid transform \"%s\" for output %s\n",
1129 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001130 free(t);
1131
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001132 output = wayland_output_create(c, x, y, width, height, name, 0,
1133 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001134 free(name);
1135
1136 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001137}
1138
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001139static struct wayland_output *
1140wayland_output_create_for_parent_output(struct wayland_compositor *c,
1141 struct wayland_parent_output *poutput)
1142{
1143 struct wayland_output *output;
1144 struct weston_mode *mode;
1145 int32_t x;
1146
1147 if (poutput->current_mode) {
1148 mode = poutput->current_mode;
1149 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001150 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001151 } else if (!wl_list_empty(&poutput->mode_list)) {
1152 mode = container_of(poutput->mode_list.next,
1153 struct weston_mode, link);
1154 } else {
1155 weston_log("No valid modes found. Skipping output");
1156 return NULL;
1157 }
1158
1159 if (!wl_list_empty(&c->base.output_list)) {
1160 output = container_of(c->base.output_list.prev,
1161 struct wayland_output, base.link);
1162 x = output->base.x + output->base.current_mode->width;
1163 } else {
1164 x = 0;
1165 }
1166
1167 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1168 NULL, 0,
1169 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1170 if (!output)
1171 return NULL;
1172
1173 output->parent.output = poutput->global;
1174
1175 output->base.make = poutput->physical.make;
1176 output->base.model = poutput->physical.model;
1177 wl_list_init(&output->base.mode_list);
1178 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1179 wl_list_init(&poutput->mode_list);
1180
1181 wayland_output_set_fullscreen(output,
1182 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1183 mode->refresh, poutput->global);
1184
1185 if (output->parent.shell_surface) {
1186 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1187 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1188 mode->refresh, poutput->global);
1189 } else if (c->parent.fshell) {
1190 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1191 output->parent.surface,
1192 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1193 poutput->global);
1194 _wl_fullscreen_shell_mode_feedback_destroy(
1195 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1196 output->parent.surface,
1197 poutput->global,
1198 mode->refresh));
1199 }
1200
1201 return output;
1202}
1203
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001204static void
1205shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1206 uint32_t serial)
1207{
1208 wl_shell_surface_pong(shell_surface, serial);
1209}
1210
1211static void
1212shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1213 uint32_t edges, int32_t width, int32_t height)
1214{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001215 struct wayland_output *output = data;
1216
1217 output->parent.configure_width = width;
1218 output->parent.configure_height = height;
1219
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001220 /* FIXME: implement resizing */
1221}
1222
1223static void
1224shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1225{
1226}
1227
1228static const struct wl_shell_surface_listener shell_surface_listener = {
1229 shell_surface_ping,
1230 shell_surface_configure,
1231 shell_surface_popup_done
1232};
1233
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001234/* Events received from the wayland-server this compositor is client of: */
1235
Jason Ekstrand7744f712013-10-27 22:24:55 -05001236/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001237static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001238input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001239{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001240
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241 struct wl_buffer *buffer;
1242 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001243
Jason Ekstrand7744f712013-10-27 22:24:55 -05001244 if (!input->compositor->cursor)
1245 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001246
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247 image = input->compositor->cursor->images[0];
1248 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001249 if (!buffer)
1250 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001251
1252 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1253 input->parent.cursor.surface,
1254 image->hotspot_x, image->hotspot_y);
1255
1256 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1257 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1258 image->width, image->height);
1259 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001260}
1261
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001262static void
Daniel Stone37816df2012-05-16 18:45:18 +01001263input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1264 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001265 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001266{
1267 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001268 int32_t fx, fy;
1269 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001270
Daniel Stone50692802012-06-22 13:21:41 +01001271 /* XXX: If we get a modifier event immediately before the focus,
1272 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001273 input->enter_serial = serial;
1274 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001275
1276 if (input->output->frame) {
1277 location = frame_pointer_enter(input->output->frame, input,
1278 wl_fixed_to_int(x),
1279 wl_fixed_to_int(y));
1280 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1281 x -= wl_fixed_from_int(fx);
1282 y -= wl_fixed_from_int(fy);
1283
1284 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1285 weston_output_schedule_repaint(&input->output->base);
1286 } else {
1287 location = THEME_LOCATION_CLIENT_AREA;
1288 }
1289
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001290 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001291
1292 if (location == THEME_LOCATION_CLIENT_AREA) {
1293 input->focus = 1;
1294 notify_pointer_focus(&input->base, &input->output->base, x, y);
1295 wl_pointer_set_cursor(input->parent.pointer,
1296 input->enter_serial, NULL, 0, 0);
1297 } else {
1298 input->focus = 0;
1299 notify_pointer_focus(&input->base, NULL, 0, 0);
1300 input_set_cursor(input);
1301 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001302}
1303
1304static void
Daniel Stone37816df2012-05-16 18:45:18 +01001305input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1306 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001307{
1308 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001309
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001310 if (!input->output)
1311 return;
1312
Jason Ekstrand7744f712013-10-27 22:24:55 -05001313 if (input->output->frame) {
1314 frame_pointer_leave(input->output->frame, input);
1315
1316 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1317 weston_output_schedule_repaint(&input->output->base);
1318 }
1319
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001320 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001321 input->output = NULL;
1322 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001323}
1324
1325static void
Daniel Stone37816df2012-05-16 18:45:18 +01001326input_handle_motion(void *data, struct wl_pointer *pointer,
1327 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1328{
1329 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001330 int32_t fx, fy;
1331 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001332
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001333 if (!input->output)
1334 return;
1335
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336 if (input->output->frame) {
1337 location = frame_pointer_motion(input->output->frame, input,
1338 wl_fixed_to_int(x),
1339 wl_fixed_to_int(y));
1340 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1341 x -= wl_fixed_from_int(fx);
1342 y -= wl_fixed_from_int(fy);
1343
1344 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1345 weston_output_schedule_repaint(&input->output->base);
1346 } else {
1347 location = THEME_LOCATION_CLIENT_AREA;
1348 }
1349
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001350 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001351
1352 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1353 input_set_cursor(input);
1354 notify_pointer_focus(&input->base, NULL, 0, 0);
1355 input->focus = 0;
1356 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1357 wl_pointer_set_cursor(input->parent.pointer,
1358 input->enter_serial, NULL, 0, 0);
1359 notify_pointer_focus(&input->base, &input->output->base, x, y);
1360 input->focus = 1;
1361 }
1362
1363 if (location == THEME_LOCATION_CLIENT_AREA)
1364 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001365}
1366
1367static void
1368input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001369 uint32_t serial, uint32_t time, uint32_t button,
1370 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001371{
1372 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001373 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001374 enum frame_button_state fstate;
1375 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001376
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001377 if (!input->output)
1378 return;
1379
Jason Ekstrand7744f712013-10-27 22:24:55 -05001380 if (input->output->frame) {
1381 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1382 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1383
1384 location = frame_pointer_button(input->output->frame, input,
1385 button, fstate);
1386
1387 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1388
1389 wl_shell_surface_move(input->output->parent.shell_surface,
1390 input->parent.seat, serial);
1391 frame_status_clear(input->output->frame,
1392 FRAME_STATUS_MOVE);
1393 return;
1394 }
1395
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001396 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1397 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001398 input->output = NULL;
1399 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001400
1401 if (wl_list_empty(&input->compositor->base.output_list))
1402 wl_display_terminate(input->compositor->base.wl_display);
1403
1404 return;
1405 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001406
1407 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1408 weston_output_schedule_repaint(&input->output->base);
1409 } else {
1410 location = THEME_LOCATION_CLIENT_AREA;
1411 }
1412
1413 if (location == THEME_LOCATION_CLIENT_AREA)
1414 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001415}
1416
1417static void
1418input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001419 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001420{
1421 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001422
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001423 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001424}
1425
1426static const struct wl_pointer_listener pointer_listener = {
1427 input_handle_pointer_enter,
1428 input_handle_pointer_leave,
1429 input_handle_motion,
1430 input_handle_button,
1431 input_handle_axis,
1432};
1433
1434static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001435input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1436 int fd, uint32_t size)
1437{
1438 struct wayland_input *input = data;
1439 struct xkb_keymap *keymap;
1440 char *map_str;
1441
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001442 if (!data) {
1443 close(fd);
1444 return;
1445 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001446
1447 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1448 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1449 if (map_str == MAP_FAILED) {
1450 weston_log("mmap failed: %m\n");
1451 goto error;
1452 }
1453
Ran Benita2e1968f2014-08-19 23:59:51 +03001454 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1455 map_str,
1456 XKB_KEYMAP_FORMAT_TEXT_V1,
1457 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001458 munmap(map_str, size);
1459
1460 if (!keymap) {
1461 weston_log("failed to compile keymap\n");
1462 goto error;
1463 }
1464
1465 input->keyboard_state_update = STATE_UPDATE_NONE;
1466 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1467 weston_log("No keymap provided; falling back to defalt\n");
1468 keymap = NULL;
1469 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1470 } else {
1471 weston_log("Invalid keymap\n");
1472 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001473 }
1474
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001475 close(fd);
1476
Rui Matos0c194ce2013-10-10 19:44:21 +02001477 if (input->base.keyboard)
1478 weston_seat_update_keymap(&input->base, keymap);
1479 else
1480 weston_seat_init_keyboard(&input->base, keymap);
1481
Ran Benitac9c74152014-08-19 23:59:52 +03001482 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001483
1484 return;
1485
1486error:
1487 wl_keyboard_release(input->parent.keyboard);
1488 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001489}
1490
1491static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001492input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001493 struct wl_keyboard *keyboard,
1494 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001495 struct wl_surface *surface,
1496 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001497{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001498 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001499 struct wayland_output *focus;
1500
1501 focus = input->keyboard_focus;
1502 if (focus) {
1503 /* This shouldn't happen */
1504 focus->keyboard_count--;
1505 if (!focus->keyboard_count && focus->frame)
1506 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1507 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1508 weston_output_schedule_repaint(&focus->base);
1509 }
1510
1511 input->keyboard_focus = wl_surface_get_user_data(surface);
1512 input->keyboard_focus->keyboard_count++;
1513
1514 focus = input->keyboard_focus;
1515 if (focus->frame) {
1516 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1517 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1518 weston_output_schedule_repaint(&focus->base);
1519 }
1520
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001521
Daniel Stone50692802012-06-22 13:21:41 +01001522 /* XXX: If we get a modifier event immediately before the focus,
1523 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001524 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001525 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001526}
1527
1528static void
1529input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001530 struct wl_keyboard *keyboard,
1531 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001532 struct wl_surface *surface)
1533{
1534 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001535 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001536
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001537 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001538
1539 focus = input->keyboard_focus;
1540 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001541 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001542
1543 focus->keyboard_count--;
1544 if (!focus->keyboard_count && focus->frame) {
1545 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1546 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1547 weston_output_schedule_repaint(&focus->base);
1548 }
1549
1550 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001551}
1552
Daniel Stone37816df2012-05-16 18:45:18 +01001553static void
1554input_handle_key(void *data, struct wl_keyboard *keyboard,
1555 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1556{
1557 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001558
Daniel Stone50692802012-06-22 13:21:41 +01001559 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001560 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001561 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001562 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001563 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001564}
1565
Daniel Stone351eb612012-05-31 15:27:47 -04001566static void
1567input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001568 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001569 uint32_t mods_latched, uint32_t mods_locked,
1570 uint32_t group)
1571{
Daniel Stone50692802012-06-22 13:21:41 +01001572 struct wayland_input *input = data;
1573 struct wayland_compositor *c = input->compositor;
1574 uint32_t serial_out;
1575
1576 /* If we get a key event followed by a modifier event with the
1577 * same serial number, then we try to preserve those semantics by
1578 * reusing the same serial number on the way out too. */
1579 if (serial_in == input->key_serial)
1580 serial_out = wl_display_get_serial(c->base.wl_display);
1581 else
1582 serial_out = wl_display_next_serial(c->base.wl_display);
1583
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001584 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001585 mods_depressed, mods_latched,
1586 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001587 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001588}
1589
Jonny Lamb497994a2014-08-12 14:58:26 +02001590static void
1591input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1592 int32_t rate, int32_t delay)
1593{
1594 struct wayland_input *input = data;
1595 struct wayland_compositor *c = input->compositor;
1596
1597 c->base.kb_repeat_rate = rate;
1598 c->base.kb_repeat_delay = delay;
1599}
1600
Daniel Stone37816df2012-05-16 18:45:18 +01001601static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001602 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001603 input_handle_keyboard_enter,
1604 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001605 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001606 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001607 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001608};
1609
1610static void
Daniel Stone37816df2012-05-16 18:45:18 +01001611input_handle_capabilities(void *data, struct wl_seat *seat,
1612 enum wl_seat_capability caps)
1613{
1614 struct wayland_input *input = data;
1615
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001616 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1617 input->parent.pointer = wl_seat_get_pointer(seat);
1618 wl_pointer_set_user_data(input->parent.pointer, input);
1619 wl_pointer_add_listener(input->parent.pointer,
1620 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001621 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001622 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1623 wl_pointer_destroy(input->parent.pointer);
1624 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001625 }
1626
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001627 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1628 input->parent.keyboard = wl_seat_get_keyboard(seat);
1629 wl_keyboard_set_user_data(input->parent.keyboard, input);
1630 wl_keyboard_add_listener(input->parent.keyboard,
1631 &keyboard_listener, input);
1632 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1633 wl_keyboard_destroy(input->parent.keyboard);
1634 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001635 }
1636}
1637
Jonny Lamb497994a2014-08-12 14:58:26 +02001638static void
1639input_handle_name(void *data, struct wl_seat *seat,
1640 const char *name)
1641{
1642}
1643
Daniel Stone37816df2012-05-16 18:45:18 +01001644static const struct wl_seat_listener seat_listener = {
1645 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001646 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001647};
1648
1649static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001650display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001651{
1652 struct wayland_input *input;
1653
Peter Huttererf3d62272013-08-08 11:57:05 +10001654 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001655 if (input == NULL)
1656 return;
1657
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001658 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001659 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001660 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001661 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001662 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001663
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001664 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1665 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001666
1667 input->parent.cursor.surface =
1668 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001669}
1670
1671static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001672wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1673 int32_t x, int32_t y,
1674 int32_t physical_width, int32_t physical_height,
1675 int32_t subpixel, const char *make,
1676 const char *model, int32_t transform)
1677{
1678 struct wayland_parent_output *output = data;
1679
1680 output->x = x;
1681 output->y = y;
1682 output->physical.width = physical_width;
1683 output->physical.height = physical_height;
1684 output->physical.subpixel = subpixel;
1685
1686 free(output->physical.make);
1687 output->physical.make = strdup(make);
1688 free(output->physical.model);
1689 output->physical.model = strdup(model);
1690
1691 output->transform = transform;
1692}
1693
1694static struct weston_mode *
1695find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1696{
1697 struct weston_mode *mode;
1698
1699 wl_list_for_each(mode, list, link) {
1700 if (mode->width == width && mode->height == height &&
1701 mode->refresh == refresh)
1702 return mode;
1703 }
1704
1705 mode = zalloc(sizeof *mode);
1706 if (!mode)
1707 return NULL;
1708
1709 mode->width = width;
1710 mode->height = height;
1711 mode->refresh = refresh;
1712 wl_list_insert(list, &mode->link);
1713
1714 return mode;
1715}
1716
1717static void
1718wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1719 uint32_t flags, int32_t width, int32_t height,
1720 int32_t refresh)
1721{
1722 struct wayland_parent_output *output = data;
1723 struct weston_mode *mode;
1724
1725 if (output->output) {
1726 mode = find_mode(&output->output->base.mode_list,
1727 width, height, refresh);
1728 if (!mode)
1729 return;
1730 mode->flags = flags;
1731 /* Do a mode-switch on current mode change? */
1732 } else {
1733 mode = find_mode(&output->mode_list, width, height, refresh);
1734 if (!mode)
1735 return;
1736 mode->flags = flags;
1737 if (flags & WL_OUTPUT_MODE_CURRENT)
1738 output->current_mode = mode;
1739 if (flags & WL_OUTPUT_MODE_PREFERRED)
1740 output->preferred_mode = mode;
1741 }
1742}
1743
1744static const struct wl_output_listener output_listener = {
1745 wayland_parent_output_geometry,
1746 wayland_parent_output_mode
1747};
1748
1749static void
1750wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1751{
1752 struct wayland_parent_output *output;
1753
1754 output = zalloc(sizeof *output);
1755 if (!output)
1756 return;
1757
1758 output->id = id;
1759 output->global = wl_registry_bind(c->parent.registry, id,
1760 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001761 if (!output->global) {
1762 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001763 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001764 }
1765
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001766 wl_output_add_listener(output->global, &output_listener, output);
1767
1768 output->scale = 0;
1769 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1770 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1771 wl_list_init(&output->mode_list);
1772 wl_list_insert(&c->parent.output_list, &output->link);
1773
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001774 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001775 wl_display_roundtrip(c->parent.wl_display);
1776 wayland_output_create_for_parent_output(c, output);
1777 }
1778}
1779
1780static void
1781wayland_parent_output_destroy(struct wayland_parent_output *output)
1782{
1783 struct weston_mode *mode, *next;
1784
1785 if (output->output)
1786 wayland_output_destroy(&output->output->base);
1787
1788 wl_output_destroy(output->global);
1789 free(output->physical.make);
1790 free(output->physical.model);
1791
1792 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1793 wl_list_remove(&mode->link);
1794 free(mode);
1795 }
1796}
1797
1798static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001799registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1800 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001801{
1802 struct wayland_compositor *c = data;
1803
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001804 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001805 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001806 wl_registry_bind(registry, name,
1807 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001808 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001809 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001810 wl_registry_bind(registry, name,
1811 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001812 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1813 c->parent.fshell =
1814 wl_registry_bind(registry, name,
1815 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001816 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001817 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001818 } else if (strcmp(interface, "wl_output") == 0) {
1819 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001820 } else if (strcmp(interface, "wl_shm") == 0) {
1821 c->parent.shm =
1822 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001823 }
1824}
1825
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001826static void
1827registry_handle_global_remove(void *data, struct wl_registry *registry,
1828 uint32_t name)
1829{
1830 struct wayland_compositor *c = data;
1831 struct wayland_parent_output *output;
1832
1833 wl_list_for_each(output, &c->parent.output_list, link)
1834 if (output->id == name)
1835 wayland_parent_output_destroy(output);
1836}
1837
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001838static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001839 registry_handle_global,
1840 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001841};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001842
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001843static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001844wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1845{
1846 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001847 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001848
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001849 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1850 wl_display_terminate(c->base.wl_display);
1851 return 0;
1852 }
1853
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001854 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001855 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001856 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001857 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001858
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001859 if (mask == 0) {
1860 count = wl_display_dispatch_pending(c->parent.wl_display);
1861 wl_display_flush(c->parent.wl_display);
1862 }
1863
1864 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001865}
1866
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001867static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001868wayland_restore(struct weston_compositor *ec)
1869{
1870}
1871
1872static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001874{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001875 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1876
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001877 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001878
Jonas Ådahle5a12252013-04-05 23:07:11 +02001879 if (c->parent.shm)
1880 wl_shm_destroy(c->parent.shm);
1881
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001882 free(ec);
1883}
1884
Jason Ekstrand7744f712013-10-27 22:24:55 -05001885static const char *left_ptrs[] = {
1886 "left_ptr",
1887 "default",
1888 "top_left_arrow",
1889 "left-arrow"
1890};
1891
1892static void
1893create_cursor(struct wayland_compositor *c, struct weston_config *config)
1894{
1895 struct weston_config_section *s;
1896 int size;
1897 char *theme = NULL;
1898 unsigned int i;
1899
1900 s = weston_config_get_section(config, "shell", NULL, NULL);
1901 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1902 weston_config_section_get_int(s, "cursor-size", &size, 32);
1903
1904 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001905 if (!c->cursor_theme) {
1906 fprintf(stderr, "could not load cursor theme\n");
1907 return;
1908 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001909
U. Artie Eoffff755002014-01-17 12:36:58 -08001910 free(theme);
1911
Jason Ekstrand7744f712013-10-27 22:24:55 -05001912 c->cursor = NULL;
1913 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1914 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1915 left_ptrs[i]);
1916 if (!c->cursor) {
1917 fprintf(stderr, "could not load left cursor\n");
1918 return;
1919 }
1920}
1921
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001922static void
1923fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1924 void *data)
1925{
1926 struct wayland_compositor *c = data;
1927 struct wayland_input *input = NULL;
1928
1929 wl_list_for_each(input, &c->input_list, link)
1930 if (&input->base == seat_base)
1931 break;
1932
1933 if (!input || !input->output)
1934 return;
1935
1936 if (input->output->frame)
1937 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1938 else
1939 wayland_output_set_windowed(input->output);
1940
1941 weston_output_schedule_repaint(&input->output->base);
1942}
1943
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001944static struct wayland_compositor *
1945wayland_compositor_create(struct wl_display *display, int use_pixman,
1946 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001947 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001948{
1949 struct wayland_compositor *c;
1950 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001951 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001952
Peter Huttererf3d62272013-08-08 11:57:05 +10001953 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001954 if (c == NULL)
1955 return NULL;
1956
Daniel Stone725c2c32012-06-22 14:04:36 +01001957 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001958 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001959 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001960
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001961 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1962 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001963
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001964 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001965 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001966 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001967 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001968 }
1969
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001970 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001971 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001972 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1973 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001974 wl_display_roundtrip(c->parent.wl_display);
1975
1976 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001977
1978 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001979
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001980 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001981
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001982 if (!c->use_pixman) {
1983 gl_renderer = weston_load_module("gl-renderer.so",
1984 "gl_renderer_interface");
1985 if (!gl_renderer)
1986 c->use_pixman = 1;
1987 }
1988
1989 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001990 if (gl_renderer->create(&c->base,
Derek Foremane76f1852015-05-15 12:12:39 -05001991 EGL_PLATFORM_WAYLAND_KHR,
1992 c->parent.wl_display,
1993 gl_renderer->alpha_attribs,
1994 NULL,
1995 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001996 weston_log("Failed to initialize the GL renderer; "
1997 "falling back to pixman.\n");
1998 c->use_pixman = 1;
1999 }
2000 }
2001
2002 if (c->use_pixman) {
2003 if (pixman_renderer_init(&c->base) < 0) {
2004 weston_log("Failed to initialize pixman renderer\n");
2005 goto err_display;
2006 }
2007 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002008
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002009 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002010 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002011
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002012 loop = wl_display_get_event_loop(c->base.wl_display);
2013
2014 fd = wl_display_get_fd(c->parent.wl_display);
2015 c->parent.wl_source =
2016 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2017 wayland_compositor_handle_event, c);
2018 if (c->parent.wl_source == NULL)
2019 goto err_renderer;
2020
2021 wl_event_source_check(c->parent.wl_source);
2022
2023 return c;
2024err_renderer:
2025 c->base.renderer->destroy(&c->base);
2026err_display:
2027 wl_display_disconnect(c->parent.wl_display);
2028err_compositor:
2029 weston_compositor_shutdown(&c->base);
2030err_free:
2031 free(c);
2032 return NULL;
2033}
2034
2035static void
2036wayland_compositor_destroy(struct wayland_compositor *c)
2037{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002038 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002039
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002040 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002041 wayland_output_destroy(output);
2042
2043 c->base.renderer->destroy(&c->base);
2044 wl_display_disconnect(c->parent.wl_display);
2045
2046 if (c->theme)
2047 theme_destroy(c->theme);
2048 if (c->frame_device)
2049 cairo_device_destroy(c->frame_device);
2050 wl_cursor_theme_destroy(c->cursor_theme);
2051
2052 weston_compositor_shutdown(&c->base);
2053 free(c);
2054}
2055
2056WL_EXPORT struct weston_compositor *
2057backend_init(struct wl_display *display, int *argc, char *argv[],
2058 struct weston_config *config)
2059{
2060 struct wayland_compositor *c;
2061 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002062 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002063 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002064 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002065 const char *section_name, *display_name;
2066 char *name;
2067
2068 const struct weston_option wayland_options[] = {
2069 { WESTON_OPTION_INTEGER, "width", 0, &width },
2070 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002071 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002072 { WESTON_OPTION_STRING, "display", 0, &display_name },
2073 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2074 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002075 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002076 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002077 };
2078
2079 width = 0;
2080 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002081 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002082 display_name = NULL;
2083 use_pixman = 0;
2084 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002085 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002086 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002087 parse_options(wayland_options,
2088 ARRAY_LENGTH(wayland_options), argc, argv);
2089
2090 c = wayland_compositor_create(display, use_pixman, display_name,
2091 argc, argv, config);
2092 if (!c)
2093 return NULL;
2094
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002095 if (sprawl || c->parent.fshell) {
2096 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002097 wl_display_roundtrip(c->parent.wl_display);
2098
2099 wl_list_for_each(poutput, &c->parent.output_list, link)
2100 wayland_output_create_for_parent_output(c, poutput);
2101
2102 return &c->base;
2103 }
2104
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002105 if (fullscreen) {
2106 output = wayland_output_create(c, 0, 0, width, height,
2107 NULL, 1, 0, 1);
2108 if (!output)
2109 goto err_outputs;
2110
Axel Davydd8b88d2013-11-17 21:34:16 +01002111 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002112 return &c->base;
2113 }
2114
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002115 section = NULL;
2116 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002117 while (weston_config_next_section(config, &section, &section_name)) {
2118 if (!section_name || strcmp(section_name, "output") != 0)
2119 continue;
2120 weston_config_section_get_string(section, "name", &name, NULL);
2121 if (name == NULL)
2122 continue;
2123
2124 if (name[0] != 'W' || name[1] != 'L') {
2125 free(name);
2126 continue;
2127 }
2128 free(name);
2129
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002130 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002131 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002132 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002133 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002134 if (wayland_output_set_windowed(output))
2135 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136
2137 x += output->base.width;
2138 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002139 }
2140
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002141 if (!width)
2142 width = 1024;
2143 if (!height)
2144 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002145 if (!scale)
2146 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002147 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002148 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002149 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002150 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002151 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002152 if (wayland_output_set_windowed(output))
2153 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002154
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002155 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002156 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002157 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002158
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002159 weston_compositor_add_key_binding(&c->base, KEY_F,
2160 MODIFIER_CTRL | MODIFIER_ALT,
2161 fullscreen_binding, c);
2162
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002163 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002164
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002165err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002166 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002167 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002168}