blob: c9983e012fc33ca471d404b3d0eb500936e8d383 [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
Jason Ekstrand7744f712013-10-27 22:24:55 -05001310 if (input->output->frame) {
1311 frame_pointer_leave(input->output->frame, input);
1312
1313 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1314 weston_output_schedule_repaint(&input->output->base);
1315 }
1316
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001317 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001318 input->output = NULL;
1319 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001320}
1321
1322static void
Daniel Stone37816df2012-05-16 18:45:18 +01001323input_handle_motion(void *data, struct wl_pointer *pointer,
1324 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1325{
1326 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001327 int32_t fx, fy;
1328 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001329
Jason Ekstrand7744f712013-10-27 22:24:55 -05001330 if (input->output->frame) {
1331 location = frame_pointer_motion(input->output->frame, input,
1332 wl_fixed_to_int(x),
1333 wl_fixed_to_int(y));
1334 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1335 x -= wl_fixed_from_int(fx);
1336 y -= wl_fixed_from_int(fy);
1337
1338 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1339 weston_output_schedule_repaint(&input->output->base);
1340 } else {
1341 location = THEME_LOCATION_CLIENT_AREA;
1342 }
1343
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001344 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001345
1346 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1347 input_set_cursor(input);
1348 notify_pointer_focus(&input->base, NULL, 0, 0);
1349 input->focus = 0;
1350 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1351 wl_pointer_set_cursor(input->parent.pointer,
1352 input->enter_serial, NULL, 0, 0);
1353 notify_pointer_focus(&input->base, &input->output->base, x, y);
1354 input->focus = 1;
1355 }
1356
1357 if (location == THEME_LOCATION_CLIENT_AREA)
1358 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001359}
1360
1361static void
1362input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001363 uint32_t serial, uint32_t time, uint32_t button,
1364 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001365{
1366 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001367 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001368 enum frame_button_state fstate;
1369 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001370
Jason Ekstrand7744f712013-10-27 22:24:55 -05001371 if (input->output->frame) {
1372 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1373 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1374
1375 location = frame_pointer_button(input->output->frame, input,
1376 button, fstate);
1377
1378 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1379
1380 wl_shell_surface_move(input->output->parent.shell_surface,
1381 input->parent.seat, serial);
1382 frame_status_clear(input->output->frame,
1383 FRAME_STATUS_MOVE);
1384 return;
1385 }
1386
1387 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1388 wl_display_terminate(input->compositor->base.wl_display);
1389
1390 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1391 weston_output_schedule_repaint(&input->output->base);
1392 } else {
1393 location = THEME_LOCATION_CLIENT_AREA;
1394 }
1395
1396 if (location == THEME_LOCATION_CLIENT_AREA)
1397 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001398}
1399
1400static void
1401input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001402 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001403{
1404 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001405
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001406 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001407}
1408
1409static const struct wl_pointer_listener pointer_listener = {
1410 input_handle_pointer_enter,
1411 input_handle_pointer_leave,
1412 input_handle_motion,
1413 input_handle_button,
1414 input_handle_axis,
1415};
1416
1417static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001418input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1419 int fd, uint32_t size)
1420{
1421 struct wayland_input *input = data;
1422 struct xkb_keymap *keymap;
1423 char *map_str;
1424
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001425 if (!data) {
1426 close(fd);
1427 return;
1428 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001429
1430 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1431 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1432 if (map_str == MAP_FAILED) {
1433 weston_log("mmap failed: %m\n");
1434 goto error;
1435 }
1436
Ran Benita2e1968f2014-08-19 23:59:51 +03001437 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1438 map_str,
1439 XKB_KEYMAP_FORMAT_TEXT_V1,
1440 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001441 munmap(map_str, size);
1442
1443 if (!keymap) {
1444 weston_log("failed to compile keymap\n");
1445 goto error;
1446 }
1447
1448 input->keyboard_state_update = STATE_UPDATE_NONE;
1449 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1450 weston_log("No keymap provided; falling back to defalt\n");
1451 keymap = NULL;
1452 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1453 } else {
1454 weston_log("Invalid keymap\n");
1455 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001456 }
1457
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001458 close(fd);
1459
Rui Matos0c194ce2013-10-10 19:44:21 +02001460 if (input->base.keyboard)
1461 weston_seat_update_keymap(&input->base, keymap);
1462 else
1463 weston_seat_init_keyboard(&input->base, keymap);
1464
Ran Benitac9c74152014-08-19 23:59:52 +03001465 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001466
1467 return;
1468
1469error:
1470 wl_keyboard_release(input->parent.keyboard);
1471 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001472}
1473
1474static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001475input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001476 struct wl_keyboard *keyboard,
1477 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001478 struct wl_surface *surface,
1479 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001480{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001481 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001482 struct wayland_output *focus;
1483
1484 focus = input->keyboard_focus;
1485 if (focus) {
1486 /* This shouldn't happen */
1487 focus->keyboard_count--;
1488 if (!focus->keyboard_count && focus->frame)
1489 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1490 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1491 weston_output_schedule_repaint(&focus->base);
1492 }
1493
1494 input->keyboard_focus = wl_surface_get_user_data(surface);
1495 input->keyboard_focus->keyboard_count++;
1496
1497 focus = input->keyboard_focus;
1498 if (focus->frame) {
1499 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1500 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1501 weston_output_schedule_repaint(&focus->base);
1502 }
1503
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001504
Daniel Stone50692802012-06-22 13:21:41 +01001505 /* XXX: If we get a modifier event immediately before the focus,
1506 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001507 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001508 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001509}
1510
1511static void
1512input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001513 struct wl_keyboard *keyboard,
1514 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001515 struct wl_surface *surface)
1516{
1517 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001518 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001519
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001520 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001521
1522 focus = input->keyboard_focus;
1523 if (!focus)
1524 return; /* This shouldn't happen */
1525
1526 focus->keyboard_count--;
1527 if (!focus->keyboard_count && focus->frame) {
1528 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1529 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1530 weston_output_schedule_repaint(&focus->base);
1531 }
1532
1533 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001534}
1535
Daniel Stone37816df2012-05-16 18:45:18 +01001536static void
1537input_handle_key(void *data, struct wl_keyboard *keyboard,
1538 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1539{
1540 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001541
Daniel Stone50692802012-06-22 13:21:41 +01001542 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001543 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001544 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001545 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001546 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001547}
1548
Daniel Stone351eb612012-05-31 15:27:47 -04001549static void
1550input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001551 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001552 uint32_t mods_latched, uint32_t mods_locked,
1553 uint32_t group)
1554{
Daniel Stone50692802012-06-22 13:21:41 +01001555 struct wayland_input *input = data;
1556 struct wayland_compositor *c = input->compositor;
1557 uint32_t serial_out;
1558
1559 /* If we get a key event followed by a modifier event with the
1560 * same serial number, then we try to preserve those semantics by
1561 * reusing the same serial number on the way out too. */
1562 if (serial_in == input->key_serial)
1563 serial_out = wl_display_get_serial(c->base.wl_display);
1564 else
1565 serial_out = wl_display_next_serial(c->base.wl_display);
1566
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001567 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001568 mods_depressed, mods_latched,
1569 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001570 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001571}
1572
Jonny Lamb497994a2014-08-12 14:58:26 +02001573static void
1574input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1575 int32_t rate, int32_t delay)
1576{
1577 struct wayland_input *input = data;
1578 struct wayland_compositor *c = input->compositor;
1579
1580 c->base.kb_repeat_rate = rate;
1581 c->base.kb_repeat_delay = delay;
1582}
1583
Daniel Stone37816df2012-05-16 18:45:18 +01001584static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001585 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001586 input_handle_keyboard_enter,
1587 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001588 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001589 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001590 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001591};
1592
1593static void
Daniel Stone37816df2012-05-16 18:45:18 +01001594input_handle_capabilities(void *data, struct wl_seat *seat,
1595 enum wl_seat_capability caps)
1596{
1597 struct wayland_input *input = data;
1598
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001599 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1600 input->parent.pointer = wl_seat_get_pointer(seat);
1601 wl_pointer_set_user_data(input->parent.pointer, input);
1602 wl_pointer_add_listener(input->parent.pointer,
1603 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001604 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001605 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1606 wl_pointer_destroy(input->parent.pointer);
1607 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001608 }
1609
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001610 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1611 input->parent.keyboard = wl_seat_get_keyboard(seat);
1612 wl_keyboard_set_user_data(input->parent.keyboard, input);
1613 wl_keyboard_add_listener(input->parent.keyboard,
1614 &keyboard_listener, input);
1615 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1616 wl_keyboard_destroy(input->parent.keyboard);
1617 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001618 }
1619}
1620
Jonny Lamb497994a2014-08-12 14:58:26 +02001621static void
1622input_handle_name(void *data, struct wl_seat *seat,
1623 const char *name)
1624{
1625}
1626
Daniel Stone37816df2012-05-16 18:45:18 +01001627static const struct wl_seat_listener seat_listener = {
1628 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001629 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001630};
1631
1632static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001633display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001634{
1635 struct wayland_input *input;
1636
Peter Huttererf3d62272013-08-08 11:57:05 +10001637 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001638 if (input == NULL)
1639 return;
1640
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001641 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001642 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001643 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001644 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001645 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001646
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001647 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1648 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001649
1650 input->parent.cursor.surface =
1651 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001652}
1653
1654static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001655wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1656 int32_t x, int32_t y,
1657 int32_t physical_width, int32_t physical_height,
1658 int32_t subpixel, const char *make,
1659 const char *model, int32_t transform)
1660{
1661 struct wayland_parent_output *output = data;
1662
1663 output->x = x;
1664 output->y = y;
1665 output->physical.width = physical_width;
1666 output->physical.height = physical_height;
1667 output->physical.subpixel = subpixel;
1668
1669 free(output->physical.make);
1670 output->physical.make = strdup(make);
1671 free(output->physical.model);
1672 output->physical.model = strdup(model);
1673
1674 output->transform = transform;
1675}
1676
1677static struct weston_mode *
1678find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1679{
1680 struct weston_mode *mode;
1681
1682 wl_list_for_each(mode, list, link) {
1683 if (mode->width == width && mode->height == height &&
1684 mode->refresh == refresh)
1685 return mode;
1686 }
1687
1688 mode = zalloc(sizeof *mode);
1689 if (!mode)
1690 return NULL;
1691
1692 mode->width = width;
1693 mode->height = height;
1694 mode->refresh = refresh;
1695 wl_list_insert(list, &mode->link);
1696
1697 return mode;
1698}
1699
1700static void
1701wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1702 uint32_t flags, int32_t width, int32_t height,
1703 int32_t refresh)
1704{
1705 struct wayland_parent_output *output = data;
1706 struct weston_mode *mode;
1707
1708 if (output->output) {
1709 mode = find_mode(&output->output->base.mode_list,
1710 width, height, refresh);
1711 if (!mode)
1712 return;
1713 mode->flags = flags;
1714 /* Do a mode-switch on current mode change? */
1715 } else {
1716 mode = find_mode(&output->mode_list, width, height, refresh);
1717 if (!mode)
1718 return;
1719 mode->flags = flags;
1720 if (flags & WL_OUTPUT_MODE_CURRENT)
1721 output->current_mode = mode;
1722 if (flags & WL_OUTPUT_MODE_PREFERRED)
1723 output->preferred_mode = mode;
1724 }
1725}
1726
1727static const struct wl_output_listener output_listener = {
1728 wayland_parent_output_geometry,
1729 wayland_parent_output_mode
1730};
1731
1732static void
1733wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1734{
1735 struct wayland_parent_output *output;
1736
1737 output = zalloc(sizeof *output);
1738 if (!output)
1739 return;
1740
1741 output->id = id;
1742 output->global = wl_registry_bind(c->parent.registry, id,
1743 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001744 if (!output->global) {
1745 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001746 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001747 }
1748
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001749 wl_output_add_listener(output->global, &output_listener, output);
1750
1751 output->scale = 0;
1752 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1753 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1754 wl_list_init(&output->mode_list);
1755 wl_list_insert(&c->parent.output_list, &output->link);
1756
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001757 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001758 wl_display_roundtrip(c->parent.wl_display);
1759 wayland_output_create_for_parent_output(c, output);
1760 }
1761}
1762
1763static void
1764wayland_parent_output_destroy(struct wayland_parent_output *output)
1765{
1766 struct weston_mode *mode, *next;
1767
1768 if (output->output)
1769 wayland_output_destroy(&output->output->base);
1770
1771 wl_output_destroy(output->global);
1772 free(output->physical.make);
1773 free(output->physical.model);
1774
1775 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1776 wl_list_remove(&mode->link);
1777 free(mode);
1778 }
1779}
1780
1781static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001782registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1783 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001784{
1785 struct wayland_compositor *c = data;
1786
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001787 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001788 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001789 wl_registry_bind(registry, name,
1790 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001791 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001792 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001793 wl_registry_bind(registry, name,
1794 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001795 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1796 c->parent.fshell =
1797 wl_registry_bind(registry, name,
1798 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001799 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001800 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001801 } else if (strcmp(interface, "wl_output") == 0) {
1802 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001803 } else if (strcmp(interface, "wl_shm") == 0) {
1804 c->parent.shm =
1805 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001806 }
1807}
1808
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001809static void
1810registry_handle_global_remove(void *data, struct wl_registry *registry,
1811 uint32_t name)
1812{
1813 struct wayland_compositor *c = data;
1814 struct wayland_parent_output *output;
1815
1816 wl_list_for_each(output, &c->parent.output_list, link)
1817 if (output->id == name)
1818 wayland_parent_output_destroy(output);
1819}
1820
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001821static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001822 registry_handle_global,
1823 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001824};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001825
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001826static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001827wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1828{
1829 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001830 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001831
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001832 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1833 wl_display_terminate(c->base.wl_display);
1834 return 0;
1835 }
1836
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001837 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001838 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001839 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001840 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001841
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001842 if (mask == 0) {
1843 count = wl_display_dispatch_pending(c->parent.wl_display);
1844 wl_display_flush(c->parent.wl_display);
1845 }
1846
1847 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001848}
1849
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001850static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001851wayland_restore(struct weston_compositor *ec)
1852{
1853}
1854
1855static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001856wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001857{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001858 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1859
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001860 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001861
Jonas Ådahle5a12252013-04-05 23:07:11 +02001862 if (c->parent.shm)
1863 wl_shm_destroy(c->parent.shm);
1864
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001865 free(ec);
1866}
1867
Jason Ekstrand7744f712013-10-27 22:24:55 -05001868static const char *left_ptrs[] = {
1869 "left_ptr",
1870 "default",
1871 "top_left_arrow",
1872 "left-arrow"
1873};
1874
1875static void
1876create_cursor(struct wayland_compositor *c, struct weston_config *config)
1877{
1878 struct weston_config_section *s;
1879 int size;
1880 char *theme = NULL;
1881 unsigned int i;
1882
1883 s = weston_config_get_section(config, "shell", NULL, NULL);
1884 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1885 weston_config_section_get_int(s, "cursor-size", &size, 32);
1886
1887 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001888 if (!c->cursor_theme) {
1889 fprintf(stderr, "could not load cursor theme\n");
1890 return;
1891 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001892
U. Artie Eoffff755002014-01-17 12:36:58 -08001893 free(theme);
1894
Jason Ekstrand7744f712013-10-27 22:24:55 -05001895 c->cursor = NULL;
1896 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1897 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1898 left_ptrs[i]);
1899 if (!c->cursor) {
1900 fprintf(stderr, "could not load left cursor\n");
1901 return;
1902 }
1903}
1904
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001905static void
1906fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1907 void *data)
1908{
1909 struct wayland_compositor *c = data;
1910 struct wayland_input *input = NULL;
1911
1912 wl_list_for_each(input, &c->input_list, link)
1913 if (&input->base == seat_base)
1914 break;
1915
1916 if (!input || !input->output)
1917 return;
1918
1919 if (input->output->frame)
1920 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1921 else
1922 wayland_output_set_windowed(input->output);
1923
1924 weston_output_schedule_repaint(&input->output->base);
1925}
1926
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001927static struct wayland_compositor *
1928wayland_compositor_create(struct wl_display *display, int use_pixman,
1929 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001930 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001931{
1932 struct wayland_compositor *c;
1933 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001934 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001935
Peter Huttererf3d62272013-08-08 11:57:05 +10001936 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001937 if (c == NULL)
1938 return NULL;
1939
Daniel Stone725c2c32012-06-22 14:04:36 +01001940 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001941 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001942 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001943
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001944 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1945 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001946
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001947 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001948 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001949 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001950 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001951 }
1952
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001953 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001954 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001955 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1956 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001957 wl_display_roundtrip(c->parent.wl_display);
1958
1959 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001960
1961 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001962
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001963 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001964
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001965 if (!c->use_pixman) {
1966 gl_renderer = weston_load_module("gl-renderer.so",
1967 "gl_renderer_interface");
1968 if (!gl_renderer)
1969 c->use_pixman = 1;
1970 }
1971
1972 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001973 if (gl_renderer->create(&c->base,
Derek Foremane76f1852015-05-15 12:12:39 -05001974 EGL_PLATFORM_WAYLAND_KHR,
1975 c->parent.wl_display,
1976 gl_renderer->alpha_attribs,
1977 NULL,
1978 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001979 weston_log("Failed to initialize the GL renderer; "
1980 "falling back to pixman.\n");
1981 c->use_pixman = 1;
1982 }
1983 }
1984
1985 if (c->use_pixman) {
1986 if (pixman_renderer_init(&c->base) < 0) {
1987 weston_log("Failed to initialize pixman renderer\n");
1988 goto err_display;
1989 }
1990 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001991
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001992 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001993 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001994
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001995 loop = wl_display_get_event_loop(c->base.wl_display);
1996
1997 fd = wl_display_get_fd(c->parent.wl_display);
1998 c->parent.wl_source =
1999 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2000 wayland_compositor_handle_event, c);
2001 if (c->parent.wl_source == NULL)
2002 goto err_renderer;
2003
2004 wl_event_source_check(c->parent.wl_source);
2005
2006 return c;
2007err_renderer:
2008 c->base.renderer->destroy(&c->base);
2009err_display:
2010 wl_display_disconnect(c->parent.wl_display);
2011err_compositor:
2012 weston_compositor_shutdown(&c->base);
2013err_free:
2014 free(c);
2015 return NULL;
2016}
2017
2018static void
2019wayland_compositor_destroy(struct wayland_compositor *c)
2020{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002021 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002022
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002023 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002024 wayland_output_destroy(output);
2025
2026 c->base.renderer->destroy(&c->base);
2027 wl_display_disconnect(c->parent.wl_display);
2028
2029 if (c->theme)
2030 theme_destroy(c->theme);
2031 if (c->frame_device)
2032 cairo_device_destroy(c->frame_device);
2033 wl_cursor_theme_destroy(c->cursor_theme);
2034
2035 weston_compositor_shutdown(&c->base);
2036 free(c);
2037}
2038
2039WL_EXPORT struct weston_compositor *
2040backend_init(struct wl_display *display, int *argc, char *argv[],
2041 struct weston_config *config)
2042{
2043 struct wayland_compositor *c;
2044 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002045 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002046 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002047 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002048 const char *section_name, *display_name;
2049 char *name;
2050
2051 const struct weston_option wayland_options[] = {
2052 { WESTON_OPTION_INTEGER, "width", 0, &width },
2053 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002054 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002055 { WESTON_OPTION_STRING, "display", 0, &display_name },
2056 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2057 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002058 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002059 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002060 };
2061
2062 width = 0;
2063 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002064 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002065 display_name = NULL;
2066 use_pixman = 0;
2067 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002068 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002069 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002070 parse_options(wayland_options,
2071 ARRAY_LENGTH(wayland_options), argc, argv);
2072
2073 c = wayland_compositor_create(display, use_pixman, display_name,
2074 argc, argv, config);
2075 if (!c)
2076 return NULL;
2077
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002078 if (sprawl || c->parent.fshell) {
2079 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002080 wl_display_roundtrip(c->parent.wl_display);
2081
2082 wl_list_for_each(poutput, &c->parent.output_list, link)
2083 wayland_output_create_for_parent_output(c, poutput);
2084
2085 return &c->base;
2086 }
2087
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002088 if (fullscreen) {
2089 output = wayland_output_create(c, 0, 0, width, height,
2090 NULL, 1, 0, 1);
2091 if (!output)
2092 goto err_outputs;
2093
Axel Davydd8b88d2013-11-17 21:34:16 +01002094 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002095 return &c->base;
2096 }
2097
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002098 section = NULL;
2099 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002100 while (weston_config_next_section(config, &section, &section_name)) {
2101 if (!section_name || strcmp(section_name, "output") != 0)
2102 continue;
2103 weston_config_section_get_string(section, "name", &name, NULL);
2104 if (name == NULL)
2105 continue;
2106
2107 if (name[0] != 'W' || name[1] != 'L') {
2108 free(name);
2109 continue;
2110 }
2111 free(name);
2112
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002113 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002114 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002115 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002116 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002117 if (wayland_output_set_windowed(output))
2118 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002119
2120 x += output->base.width;
2121 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002122 }
2123
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002124 if (!width)
2125 width = 1024;
2126 if (!height)
2127 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002128 if (!scale)
2129 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002130 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002131 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002132 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002133 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002134 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002135 if (wayland_output_set_windowed(output))
2136 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002137
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002138 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002139 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002140 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002141
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002142 weston_compositor_add_key_binding(&c->base, KEY_F,
2143 MODIFIER_CTRL | MODIFIER_ALT,
2144 fullscreen_binding, c);
2145
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002146 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002147
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002148err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002149 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002150 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002151}