blob: 6c333619f05321b4c1cfc91a1e3440b00319bce2 [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,
651 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500652 goto cleanup_window;
653
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500654 return 0;
655
656cleanup_window:
657 wl_egl_window_destroy(output->gl.egl_window);
658 return -1;
659}
660
661static int
662wayland_output_init_pixman_renderer(struct wayland_output *output)
663{
664 return pixman_renderer_output_create(&output->base);
665}
666
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600667static void
668wayland_output_resize_surface(struct wayland_output *output)
669{
670 struct wayland_compositor *c =
671 (struct wayland_compositor *)output->base.compositor;
672 struct wayland_shm_buffer *buffer, *next;
673 int32_t ix, iy, iwidth, iheight;
674 int32_t width, height;
675 struct wl_region *region;
676
677 width = output->base.current_mode->width;
678 height = output->base.current_mode->height;
679
680 if (output->frame) {
681 frame_resize_inside(output->frame, width, height);
682
683 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
684 region = wl_compositor_create_region(c->parent.compositor);
685 wl_region_add(region, ix, iy, iwidth, iheight);
686 wl_surface_set_input_region(output->parent.surface, region);
687 wl_region_destroy(region);
688
689 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
690 region = wl_compositor_create_region(c->parent.compositor);
691 wl_region_add(region, ix, iy, iwidth, iheight);
692 wl_surface_set_opaque_region(output->parent.surface, region);
693 wl_region_destroy(region);
694
695 width = frame_width(output->frame);
696 height = frame_height(output->frame);
697 } else {
698 region = wl_compositor_create_region(c->parent.compositor);
699 wl_region_add(region, 0, 0, width, height);
700 wl_surface_set_input_region(output->parent.surface, region);
701 wl_region_destroy(region);
702
703 region = wl_compositor_create_region(c->parent.compositor);
704 wl_region_add(region, 0, 0, width, height);
705 wl_surface_set_opaque_region(output->parent.surface, region);
706 wl_region_destroy(region);
707 }
708
709 if (output->gl.egl_window) {
710 wl_egl_window_resize(output->gl.egl_window,
711 width, height, 0, 0);
712
713 /* These will need to be re-created due to the resize */
714 gl_renderer->output_set_border(&output->base,
715 GL_RENDERER_BORDER_TOP,
716 0, 0, 0, NULL);
717 cairo_surface_destroy(output->gl.border.top);
718 output->gl.border.top = NULL;
719 gl_renderer->output_set_border(&output->base,
720 GL_RENDERER_BORDER_LEFT,
721 0, 0, 0, NULL);
722 cairo_surface_destroy(output->gl.border.left);
723 output->gl.border.left = NULL;
724 gl_renderer->output_set_border(&output->base,
725 GL_RENDERER_BORDER_RIGHT,
726 0, 0, 0, NULL);
727 cairo_surface_destroy(output->gl.border.right);
728 output->gl.border.right = NULL;
729 gl_renderer->output_set_border(&output->base,
730 GL_RENDERER_BORDER_BOTTOM,
731 0, 0, 0, NULL);
732 cairo_surface_destroy(output->gl.border.bottom);
733 output->gl.border.bottom = NULL;
734 }
735
736 /* Throw away any remaining SHM buffers */
737 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
738 wayland_shm_buffer_destroy(buffer);
739 /* These will get thrown away when they get released */
740 wl_list_for_each(buffer, &output->shm.buffers, link)
741 buffer->output = NULL;
742}
743
744static int
745wayland_output_set_windowed(struct wayland_output *output)
746{
747 struct wayland_compositor *c =
748 (struct wayland_compositor *)output->base.compositor;
749 int tlen;
750 char *title;
751
752 if (output->frame)
753 return 0;
754
755 if (output->name) {
756 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
757 title = malloc(tlen + 1);
758 if (!title)
759 return -1;
760
761 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
762 } else {
763 title = strdup(WINDOW_TITLE);
764 }
765
766 if (!c->theme) {
767 c->theme = theme_create();
768 if (!c->theme) {
769 free(title);
770 return -1;
771 }
772 }
773 output->frame = frame_create(c->theme, 100, 100,
774 FRAME_BUTTON_CLOSE, title);
775 free(title);
776 if (!output->frame)
777 return -1;
778
779 if (output->keyboard_count)
780 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
781
782 wayland_output_resize_surface(output);
783
784 wl_shell_surface_set_toplevel(output->parent.shell_surface);
785
786 return 0;
787}
788
789static void
790wayland_output_set_fullscreen(struct wayland_output *output,
791 enum wl_shell_surface_fullscreen_method method,
792 uint32_t framerate, struct wl_output *target)
793{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500794 struct wayland_compositor *c =
795 (struct wayland_compositor *)output->base.compositor;
796
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600797 if (output->frame) {
798 frame_destroy(output->frame);
799 output->frame = NULL;
800 }
801
802 wayland_output_resize_surface(output);
803
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500804 if (output->parent.shell_surface) {
805 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
806 method, framerate, target);
807 } else if (c->parent.fshell) {
808 _wl_fullscreen_shell_present_surface(c->parent.fshell,
809 output->parent.surface,
810 method, target);
811 }
812}
813
814static struct weston_mode *
815wayland_output_choose_mode(struct wayland_output *output,
816 struct weston_mode *ref_mode)
817{
818 struct weston_mode *mode;
819
820 /* First look for an exact match */
821 wl_list_for_each(mode, &output->base.mode_list, link)
822 if (mode->width == ref_mode->width &&
823 mode->height == ref_mode->height &&
824 mode->refresh == ref_mode->refresh)
825 return mode;
826
827 /* If we can't find an exact match, ignore refresh and try again */
828 wl_list_for_each(mode, &output->base.mode_list, link)
829 if (mode->width == ref_mode->width &&
830 mode->height == ref_mode->height)
831 return mode;
832
833 /* Yeah, we failed */
834 return NULL;
835}
836
837enum mode_status {
838 MODE_STATUS_UNKNOWN,
839 MODE_STATUS_SUCCESS,
840 MODE_STATUS_FAIL,
841 MODE_STATUS_CANCEL,
842};
843
844static void
845mode_feedback_successful(void *data,
846 struct _wl_fullscreen_shell_mode_feedback *fb)
847{
848 enum mode_status *value = data;
849
850 printf("Mode switch successful\n");
851
852 *value = MODE_STATUS_SUCCESS;
853}
854
855static void
856mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
857{
858 enum mode_status *value = data;
859
860 printf("Mode switch failed\n");
861
862 *value = MODE_STATUS_FAIL;
863}
864
865static void
866mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
867{
868 enum mode_status *value = data;
869
870 printf("Mode switch cancelled\n");
871
872 *value = MODE_STATUS_CANCEL;
873}
874
875struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
876 mode_feedback_successful,
877 mode_feedback_failed,
878 mode_feedback_cancelled,
879};
880
881static int
882wayland_output_switch_mode(struct weston_output *output_base,
883 struct weston_mode *mode)
884{
885 struct wayland_output *output = (struct wayland_output *) output_base;
886 struct wayland_compositor *c;
887 struct wl_surface *old_surface;
888 struct weston_mode *old_mode;
889 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
890 enum mode_status mode_status;
891 int ret = 0;
892
893 if (output_base == NULL) {
894 weston_log("output is NULL.\n");
895 return -1;
896 }
897
898 if (mode == NULL) {
899 weston_log("mode is NULL.\n");
900 return -1;
901 }
902
903 c = (struct wayland_compositor *)output_base->compositor;
904
905 if (output->parent.shell_surface || !c->parent.fshell)
906 return -1;
907
908 mode = wayland_output_choose_mode(output, mode);
909 if (mode == NULL)
910 return -1;
911
912 if (output->base.current_mode == mode)
913 return 0;
914
915 old_mode = output->base.current_mode;
916 old_surface = output->parent.surface;
917 output->base.current_mode = mode;
918 output->parent.surface =
919 wl_compositor_create_surface(c->parent.compositor);
920 wl_surface_set_user_data(output->parent.surface, output);
921
922 /* Blow the old buffers because we changed size/surfaces */
923 wayland_output_resize_surface(output);
924
925 mode_feedback =
926 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
927 output->parent.surface,
928 output->parent.output,
929 mode->refresh);
930 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
931 &mode_feedback_listener,
932 &mode_status);
933
934 /* This should kick-start things again */
935 output->parent.draw_initial_frame = 1;
936 wayland_output_start_repaint_loop(&output->base);
937
938 mode_status = MODE_STATUS_UNKNOWN;
939 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
940 ret = wl_display_dispatch(c->parent.wl_display);
941
942 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
943
944 if (mode_status == MODE_STATUS_FAIL) {
945 output->base.current_mode = old_mode;
946 wl_surface_destroy(output->parent.surface);
947 output->parent.surface = old_surface;
948 wayland_output_resize_surface(output);
949
950 return -1;
951 }
952
953 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
954 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
955
956 if (c->use_pixman) {
957 pixman_renderer_output_destroy(output_base);
958 if (wayland_output_init_pixman_renderer(output) < 0)
959 goto err_output;
960 } else {
961 gl_renderer->output_destroy(output_base);
962 wl_egl_window_destroy(output->gl.egl_window);
963 if (wayland_output_init_gl_renderer(output) < 0)
964 goto err_output;
965 }
966 wl_surface_destroy(old_surface);
967
968 weston_output_schedule_repaint(&output->base);
969
970 return 0;
971
972err_output:
973 /* XXX */
974 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600975}
976
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500977static struct wayland_output *
978wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600979 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500980 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100981{
982 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500983 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600984
985 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
986 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100987
Peter Huttererf3d62272013-08-08 11:57:05 +1000988 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100989 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500990 return NULL;
991
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600992 output->name = name ? strdup(name) : NULL;
993 output->base.make = "waywayland";
994 output->base.model = "none";
995
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500996 output_width = width * scale;
997 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100998
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600999 output->parent.surface =
1000 wl_compositor_create_surface(c->parent.compositor);
1001 if (!output->parent.surface)
1002 goto err_name;
1003 wl_surface_set_user_data(output->parent.surface, output);
1004
1005 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001006
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001007 if (c->parent.shell) {
1008 output->parent.shell_surface =
1009 wl_shell_get_shell_surface(c->parent.shell,
1010 output->parent.surface);
1011 if (!output->parent.shell_surface)
1012 goto err_surface;
1013 wl_shell_surface_add_listener(output->parent.shell_surface,
1014 &shell_surface_listener, output);
1015 }
1016
1017 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001018 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1019 0, 0, NULL);
1020 wl_display_roundtrip(c->parent.wl_display);
1021 if (!width)
1022 output_width = output->parent.configure_width;
1023 if (!height)
1024 output_height = output->parent.configure_height;
1025 }
1026
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001027 output->mode.flags =
1028 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001029 output->mode.width = output_width;
1030 output->mode.height = output_height;
1031 output->mode.refresh = 60000;
1032 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001033 wl_list_init(&output->base.mode_list);
1034 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001035 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001036
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001037 wl_list_init(&output->shm.buffers);
1038 wl_list_init(&output->shm.free_buffers);
1039
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001040 weston_output_init(&output->base, &c->base, x, y, width, height,
1041 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001042
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001043 if (c->use_pixman) {
1044 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001045 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001046 output->base.repaint = wayland_output_repaint_pixman;
1047 } else {
1048 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001049 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001050 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001051 }
1052
Jonas Ådahle5a12252013-04-05 23:07:11 +02001053 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001054 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001055 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001056 output->base.set_backlight = NULL;
1057 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001058 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001059
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001060 wl_list_insert(c->base.output_list.prev, &output->base.link);
1061
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001062 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001063
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001064err_output:
1065 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001066 if (output->parent.shell_surface)
1067 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001068err_surface:
1069 wl_surface_destroy(output->parent.surface);
1070err_name:
1071 free(output->name);
1072
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001073 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001074 free(output);
1075
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001076 return NULL;
1077}
1078
1079static struct wayland_output *
1080wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001081 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001082 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001083 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001084{
1085 struct wayland_output *output;
1086 char *mode, *t, *name, *str;
1087 int width, height, scale;
1088 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001089 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001090
1091 weston_config_section_get_string(config_section, "name", &name, NULL);
1092 if (name) {
1093 slen = strlen(name);
1094 slen += strlen(WINDOW_TITLE " - ");
1095 str = malloc(slen + 1);
1096 if (str)
1097 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1098 free(name);
1099 name = str;
1100 }
1101 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001102 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001103
1104 weston_config_section_get_string(config_section,
1105 "mode", &mode, "1024x600");
1106 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1107 weston_log("Invalid mode \"%s\" for output %s\n",
1108 mode, name);
1109 width = 1024;
1110 height = 640;
1111 }
1112 free(mode);
1113
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001114 if (option_width)
1115 width = option_width;
1116 if (option_height)
1117 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001118
1119 weston_config_section_get_int(config_section, "scale", &scale, 1);
1120
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001121 if (option_scale)
1122 scale = option_scale;
1123
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001124 weston_config_section_get_string(config_section,
1125 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001126 if (weston_parse_transform(t, &transform) < 0)
1127 weston_log("Invalid transform \"%s\" for output %s\n",
1128 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001129 free(t);
1130
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001131 output = wayland_output_create(c, x, y, width, height, name, 0,
1132 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001133 free(name);
1134
1135 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001136}
1137
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001138static struct wayland_output *
1139wayland_output_create_for_parent_output(struct wayland_compositor *c,
1140 struct wayland_parent_output *poutput)
1141{
1142 struct wayland_output *output;
1143 struct weston_mode *mode;
1144 int32_t x;
1145
1146 if (poutput->current_mode) {
1147 mode = poutput->current_mode;
1148 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001149 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001150 } else if (!wl_list_empty(&poutput->mode_list)) {
1151 mode = container_of(poutput->mode_list.next,
1152 struct weston_mode, link);
1153 } else {
1154 weston_log("No valid modes found. Skipping output");
1155 return NULL;
1156 }
1157
1158 if (!wl_list_empty(&c->base.output_list)) {
1159 output = container_of(c->base.output_list.prev,
1160 struct wayland_output, base.link);
1161 x = output->base.x + output->base.current_mode->width;
1162 } else {
1163 x = 0;
1164 }
1165
1166 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1167 NULL, 0,
1168 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1169 if (!output)
1170 return NULL;
1171
1172 output->parent.output = poutput->global;
1173
1174 output->base.make = poutput->physical.make;
1175 output->base.model = poutput->physical.model;
1176 wl_list_init(&output->base.mode_list);
1177 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1178 wl_list_init(&poutput->mode_list);
1179
1180 wayland_output_set_fullscreen(output,
1181 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1182 mode->refresh, poutput->global);
1183
1184 if (output->parent.shell_surface) {
1185 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1186 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1187 mode->refresh, poutput->global);
1188 } else if (c->parent.fshell) {
1189 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1190 output->parent.surface,
1191 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1192 poutput->global);
1193 _wl_fullscreen_shell_mode_feedback_destroy(
1194 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1195 output->parent.surface,
1196 poutput->global,
1197 mode->refresh));
1198 }
1199
1200 return output;
1201}
1202
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001203static void
1204shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1205 uint32_t serial)
1206{
1207 wl_shell_surface_pong(shell_surface, serial);
1208}
1209
1210static void
1211shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1212 uint32_t edges, int32_t width, int32_t height)
1213{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001214 struct wayland_output *output = data;
1215
1216 output->parent.configure_width = width;
1217 output->parent.configure_height = height;
1218
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001219 /* FIXME: implement resizing */
1220}
1221
1222static void
1223shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1224{
1225}
1226
1227static const struct wl_shell_surface_listener shell_surface_listener = {
1228 shell_surface_ping,
1229 shell_surface_configure,
1230 shell_surface_popup_done
1231};
1232
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001233/* Events received from the wayland-server this compositor is client of: */
1234
Jason Ekstrand7744f712013-10-27 22:24:55 -05001235/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001236static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001237input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001238{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001239
Jason Ekstrand7744f712013-10-27 22:24:55 -05001240 struct wl_buffer *buffer;
1241 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001242
Jason Ekstrand7744f712013-10-27 22:24:55 -05001243 if (!input->compositor->cursor)
1244 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001245
Jason Ekstrand7744f712013-10-27 22:24:55 -05001246 image = input->compositor->cursor->images[0];
1247 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001248 if (!buffer)
1249 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001250
1251 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1252 input->parent.cursor.surface,
1253 image->hotspot_x, image->hotspot_y);
1254
1255 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1256 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1257 image->width, image->height);
1258 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001259}
1260
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001261static void
Daniel Stone37816df2012-05-16 18:45:18 +01001262input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1263 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001264 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001265{
1266 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001267 int32_t fx, fy;
1268 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001269
Daniel Stone50692802012-06-22 13:21:41 +01001270 /* XXX: If we get a modifier event immediately before the focus,
1271 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001272 input->enter_serial = serial;
1273 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001274
1275 if (input->output->frame) {
1276 location = frame_pointer_enter(input->output->frame, input,
1277 wl_fixed_to_int(x),
1278 wl_fixed_to_int(y));
1279 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1280 x -= wl_fixed_from_int(fx);
1281 y -= wl_fixed_from_int(fy);
1282
1283 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1284 weston_output_schedule_repaint(&input->output->base);
1285 } else {
1286 location = THEME_LOCATION_CLIENT_AREA;
1287 }
1288
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001289 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001290
1291 if (location == THEME_LOCATION_CLIENT_AREA) {
1292 input->focus = 1;
1293 notify_pointer_focus(&input->base, &input->output->base, x, y);
1294 wl_pointer_set_cursor(input->parent.pointer,
1295 input->enter_serial, NULL, 0, 0);
1296 } else {
1297 input->focus = 0;
1298 notify_pointer_focus(&input->base, NULL, 0, 0);
1299 input_set_cursor(input);
1300 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001301}
1302
1303static void
Daniel Stone37816df2012-05-16 18:45:18 +01001304input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1305 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001306{
1307 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001308
Jason Ekstrand7744f712013-10-27 22:24:55 -05001309 if (input->output->frame) {
1310 frame_pointer_leave(input->output->frame, input);
1311
1312 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1313 weston_output_schedule_repaint(&input->output->base);
1314 }
1315
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001316 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001317 input->output = NULL;
1318 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001319}
1320
1321static void
Daniel Stone37816df2012-05-16 18:45:18 +01001322input_handle_motion(void *data, struct wl_pointer *pointer,
1323 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1324{
1325 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001326 int32_t fx, fy;
1327 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001328
Jason Ekstrand7744f712013-10-27 22:24:55 -05001329 if (input->output->frame) {
1330 location = frame_pointer_motion(input->output->frame, input,
1331 wl_fixed_to_int(x),
1332 wl_fixed_to_int(y));
1333 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1334 x -= wl_fixed_from_int(fx);
1335 y -= wl_fixed_from_int(fy);
1336
1337 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1338 weston_output_schedule_repaint(&input->output->base);
1339 } else {
1340 location = THEME_LOCATION_CLIENT_AREA;
1341 }
1342
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001343 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001344
1345 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1346 input_set_cursor(input);
1347 notify_pointer_focus(&input->base, NULL, 0, 0);
1348 input->focus = 0;
1349 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1350 wl_pointer_set_cursor(input->parent.pointer,
1351 input->enter_serial, NULL, 0, 0);
1352 notify_pointer_focus(&input->base, &input->output->base, x, y);
1353 input->focus = 1;
1354 }
1355
1356 if (location == THEME_LOCATION_CLIENT_AREA)
1357 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001358}
1359
1360static void
1361input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001362 uint32_t serial, uint32_t time, uint32_t button,
1363 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001364{
1365 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001366 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001367 enum frame_button_state fstate;
1368 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001369
Jason Ekstrand7744f712013-10-27 22:24:55 -05001370 if (input->output->frame) {
1371 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1372 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1373
1374 location = frame_pointer_button(input->output->frame, input,
1375 button, fstate);
1376
1377 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1378
1379 wl_shell_surface_move(input->output->parent.shell_surface,
1380 input->parent.seat, serial);
1381 frame_status_clear(input->output->frame,
1382 FRAME_STATUS_MOVE);
1383 return;
1384 }
1385
1386 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1387 wl_display_terminate(input->compositor->base.wl_display);
1388
1389 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1390 weston_output_schedule_repaint(&input->output->base);
1391 } else {
1392 location = THEME_LOCATION_CLIENT_AREA;
1393 }
1394
1395 if (location == THEME_LOCATION_CLIENT_AREA)
1396 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001397}
1398
1399static void
1400input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001401 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001402{
1403 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001404
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001405 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001406}
1407
1408static const struct wl_pointer_listener pointer_listener = {
1409 input_handle_pointer_enter,
1410 input_handle_pointer_leave,
1411 input_handle_motion,
1412 input_handle_button,
1413 input_handle_axis,
1414};
1415
1416static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001417input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1418 int fd, uint32_t size)
1419{
1420 struct wayland_input *input = data;
1421 struct xkb_keymap *keymap;
1422 char *map_str;
1423
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001424 if (!data) {
1425 close(fd);
1426 return;
1427 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001428
1429 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1430 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1431 if (map_str == MAP_FAILED) {
1432 weston_log("mmap failed: %m\n");
1433 goto error;
1434 }
1435
Ran Benita2e1968f2014-08-19 23:59:51 +03001436 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1437 map_str,
1438 XKB_KEYMAP_FORMAT_TEXT_V1,
1439 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001440 munmap(map_str, size);
1441
1442 if (!keymap) {
1443 weston_log("failed to compile keymap\n");
1444 goto error;
1445 }
1446
1447 input->keyboard_state_update = STATE_UPDATE_NONE;
1448 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1449 weston_log("No keymap provided; falling back to defalt\n");
1450 keymap = NULL;
1451 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1452 } else {
1453 weston_log("Invalid keymap\n");
1454 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001455 }
1456
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001457 close(fd);
1458
Rui Matos0c194ce2013-10-10 19:44:21 +02001459 if (input->base.keyboard)
1460 weston_seat_update_keymap(&input->base, keymap);
1461 else
1462 weston_seat_init_keyboard(&input->base, keymap);
1463
Ran Benitac9c74152014-08-19 23:59:52 +03001464 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001465
1466 return;
1467
1468error:
1469 wl_keyboard_release(input->parent.keyboard);
1470 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001471}
1472
1473static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001474input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001475 struct wl_keyboard *keyboard,
1476 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001477 struct wl_surface *surface,
1478 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001479{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001480 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001481 struct wayland_output *focus;
1482
1483 focus = input->keyboard_focus;
1484 if (focus) {
1485 /* This shouldn't happen */
1486 focus->keyboard_count--;
1487 if (!focus->keyboard_count && focus->frame)
1488 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1489 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1490 weston_output_schedule_repaint(&focus->base);
1491 }
1492
1493 input->keyboard_focus = wl_surface_get_user_data(surface);
1494 input->keyboard_focus->keyboard_count++;
1495
1496 focus = input->keyboard_focus;
1497 if (focus->frame) {
1498 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1499 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1500 weston_output_schedule_repaint(&focus->base);
1501 }
1502
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001503
Daniel Stone50692802012-06-22 13:21:41 +01001504 /* XXX: If we get a modifier event immediately before the focus,
1505 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001506 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001507 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001508}
1509
1510static void
1511input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001512 struct wl_keyboard *keyboard,
1513 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001514 struct wl_surface *surface)
1515{
1516 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001517 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001518
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001519 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001520
1521 focus = input->keyboard_focus;
1522 if (!focus)
1523 return; /* This shouldn't happen */
1524
1525 focus->keyboard_count--;
1526 if (!focus->keyboard_count && focus->frame) {
1527 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1528 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1529 weston_output_schedule_repaint(&focus->base);
1530 }
1531
1532 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001533}
1534
Daniel Stone37816df2012-05-16 18:45:18 +01001535static void
1536input_handle_key(void *data, struct wl_keyboard *keyboard,
1537 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1538{
1539 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001540
Daniel Stone50692802012-06-22 13:21:41 +01001541 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001542 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001543 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001544 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001545 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001546}
1547
Daniel Stone351eb612012-05-31 15:27:47 -04001548static void
1549input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001550 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001551 uint32_t mods_latched, uint32_t mods_locked,
1552 uint32_t group)
1553{
Daniel Stone50692802012-06-22 13:21:41 +01001554 struct wayland_input *input = data;
1555 struct wayland_compositor *c = input->compositor;
1556 uint32_t serial_out;
1557
1558 /* If we get a key event followed by a modifier event with the
1559 * same serial number, then we try to preserve those semantics by
1560 * reusing the same serial number on the way out too. */
1561 if (serial_in == input->key_serial)
1562 serial_out = wl_display_get_serial(c->base.wl_display);
1563 else
1564 serial_out = wl_display_next_serial(c->base.wl_display);
1565
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001566 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001567 mods_depressed, mods_latched,
1568 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001569 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001570}
1571
Jonny Lamb497994a2014-08-12 14:58:26 +02001572static void
1573input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1574 int32_t rate, int32_t delay)
1575{
1576 struct wayland_input *input = data;
1577 struct wayland_compositor *c = input->compositor;
1578
1579 c->base.kb_repeat_rate = rate;
1580 c->base.kb_repeat_delay = delay;
1581}
1582
Daniel Stone37816df2012-05-16 18:45:18 +01001583static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001584 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001585 input_handle_keyboard_enter,
1586 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001587 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001588 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001589 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001590};
1591
1592static void
Daniel Stone37816df2012-05-16 18:45:18 +01001593input_handle_capabilities(void *data, struct wl_seat *seat,
1594 enum wl_seat_capability caps)
1595{
1596 struct wayland_input *input = data;
1597
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001598 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1599 input->parent.pointer = wl_seat_get_pointer(seat);
1600 wl_pointer_set_user_data(input->parent.pointer, input);
1601 wl_pointer_add_listener(input->parent.pointer,
1602 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001603 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001604 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1605 wl_pointer_destroy(input->parent.pointer);
1606 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001607 }
1608
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001609 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1610 input->parent.keyboard = wl_seat_get_keyboard(seat);
1611 wl_keyboard_set_user_data(input->parent.keyboard, input);
1612 wl_keyboard_add_listener(input->parent.keyboard,
1613 &keyboard_listener, input);
1614 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1615 wl_keyboard_destroy(input->parent.keyboard);
1616 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001617 }
1618}
1619
Jonny Lamb497994a2014-08-12 14:58:26 +02001620static void
1621input_handle_name(void *data, struct wl_seat *seat,
1622 const char *name)
1623{
1624}
1625
Daniel Stone37816df2012-05-16 18:45:18 +01001626static const struct wl_seat_listener seat_listener = {
1627 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001628 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001629};
1630
1631static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001632display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001633{
1634 struct wayland_input *input;
1635
Peter Huttererf3d62272013-08-08 11:57:05 +10001636 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001637 if (input == NULL)
1638 return;
1639
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001640 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001641 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001642 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001643 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001644 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001645
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001646 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1647 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001648
1649 input->parent.cursor.surface =
1650 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001651}
1652
1653static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001654wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1655 int32_t x, int32_t y,
1656 int32_t physical_width, int32_t physical_height,
1657 int32_t subpixel, const char *make,
1658 const char *model, int32_t transform)
1659{
1660 struct wayland_parent_output *output = data;
1661
1662 output->x = x;
1663 output->y = y;
1664 output->physical.width = physical_width;
1665 output->physical.height = physical_height;
1666 output->physical.subpixel = subpixel;
1667
1668 free(output->physical.make);
1669 output->physical.make = strdup(make);
1670 free(output->physical.model);
1671 output->physical.model = strdup(model);
1672
1673 output->transform = transform;
1674}
1675
1676static struct weston_mode *
1677find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1678{
1679 struct weston_mode *mode;
1680
1681 wl_list_for_each(mode, list, link) {
1682 if (mode->width == width && mode->height == height &&
1683 mode->refresh == refresh)
1684 return mode;
1685 }
1686
1687 mode = zalloc(sizeof *mode);
1688 if (!mode)
1689 return NULL;
1690
1691 mode->width = width;
1692 mode->height = height;
1693 mode->refresh = refresh;
1694 wl_list_insert(list, &mode->link);
1695
1696 return mode;
1697}
1698
1699static void
1700wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1701 uint32_t flags, int32_t width, int32_t height,
1702 int32_t refresh)
1703{
1704 struct wayland_parent_output *output = data;
1705 struct weston_mode *mode;
1706
1707 if (output->output) {
1708 mode = find_mode(&output->output->base.mode_list,
1709 width, height, refresh);
1710 if (!mode)
1711 return;
1712 mode->flags = flags;
1713 /* Do a mode-switch on current mode change? */
1714 } else {
1715 mode = find_mode(&output->mode_list, width, height, refresh);
1716 if (!mode)
1717 return;
1718 mode->flags = flags;
1719 if (flags & WL_OUTPUT_MODE_CURRENT)
1720 output->current_mode = mode;
1721 if (flags & WL_OUTPUT_MODE_PREFERRED)
1722 output->preferred_mode = mode;
1723 }
1724}
1725
1726static const struct wl_output_listener output_listener = {
1727 wayland_parent_output_geometry,
1728 wayland_parent_output_mode
1729};
1730
1731static void
1732wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1733{
1734 struct wayland_parent_output *output;
1735
1736 output = zalloc(sizeof *output);
1737 if (!output)
1738 return;
1739
1740 output->id = id;
1741 output->global = wl_registry_bind(c->parent.registry, id,
1742 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001743 if (!output->global) {
1744 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001745 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001746 }
1747
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001748 wl_output_add_listener(output->global, &output_listener, output);
1749
1750 output->scale = 0;
1751 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1752 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1753 wl_list_init(&output->mode_list);
1754 wl_list_insert(&c->parent.output_list, &output->link);
1755
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001756 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001757 wl_display_roundtrip(c->parent.wl_display);
1758 wayland_output_create_for_parent_output(c, output);
1759 }
1760}
1761
1762static void
1763wayland_parent_output_destroy(struct wayland_parent_output *output)
1764{
1765 struct weston_mode *mode, *next;
1766
1767 if (output->output)
1768 wayland_output_destroy(&output->output->base);
1769
1770 wl_output_destroy(output->global);
1771 free(output->physical.make);
1772 free(output->physical.model);
1773
1774 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1775 wl_list_remove(&mode->link);
1776 free(mode);
1777 }
1778}
1779
1780static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001781registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1782 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001783{
1784 struct wayland_compositor *c = data;
1785
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001786 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001787 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001788 wl_registry_bind(registry, name,
1789 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001790 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001791 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001792 wl_registry_bind(registry, name,
1793 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001794 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1795 c->parent.fshell =
1796 wl_registry_bind(registry, name,
1797 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001798 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001799 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001800 } else if (strcmp(interface, "wl_output") == 0) {
1801 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001802 } else if (strcmp(interface, "wl_shm") == 0) {
1803 c->parent.shm =
1804 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001805 }
1806}
1807
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001808static void
1809registry_handle_global_remove(void *data, struct wl_registry *registry,
1810 uint32_t name)
1811{
1812 struct wayland_compositor *c = data;
1813 struct wayland_parent_output *output;
1814
1815 wl_list_for_each(output, &c->parent.output_list, link)
1816 if (output->id == name)
1817 wayland_parent_output_destroy(output);
1818}
1819
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001820static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001821 registry_handle_global,
1822 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001823};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001824
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001825static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001826wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1827{
1828 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001829 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001830
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001831 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1832 wl_display_terminate(c->base.wl_display);
1833 return 0;
1834 }
1835
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001836 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001837 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001838 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001839 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001840
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001841 if (mask == 0) {
1842 count = wl_display_dispatch_pending(c->parent.wl_display);
1843 wl_display_flush(c->parent.wl_display);
1844 }
1845
1846 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001847}
1848
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001849static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001850wayland_restore(struct weston_compositor *ec)
1851{
1852}
1853
1854static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001855wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001856{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001857 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1858
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001859 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001860
Jonas Ådahle5a12252013-04-05 23:07:11 +02001861 if (c->parent.shm)
1862 wl_shm_destroy(c->parent.shm);
1863
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001864 free(ec);
1865}
1866
Jason Ekstrand7744f712013-10-27 22:24:55 -05001867static const char *left_ptrs[] = {
1868 "left_ptr",
1869 "default",
1870 "top_left_arrow",
1871 "left-arrow"
1872};
1873
1874static void
1875create_cursor(struct wayland_compositor *c, struct weston_config *config)
1876{
1877 struct weston_config_section *s;
1878 int size;
1879 char *theme = NULL;
1880 unsigned int i;
1881
1882 s = weston_config_get_section(config, "shell", NULL, NULL);
1883 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1884 weston_config_section_get_int(s, "cursor-size", &size, 32);
1885
1886 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001887 if (!c->cursor_theme) {
1888 fprintf(stderr, "could not load cursor theme\n");
1889 return;
1890 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001891
U. Artie Eoffff755002014-01-17 12:36:58 -08001892 free(theme);
1893
Jason Ekstrand7744f712013-10-27 22:24:55 -05001894 c->cursor = NULL;
1895 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1896 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1897 left_ptrs[i]);
1898 if (!c->cursor) {
1899 fprintf(stderr, "could not load left cursor\n");
1900 return;
1901 }
1902}
1903
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001904static void
1905fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1906 void *data)
1907{
1908 struct wayland_compositor *c = data;
1909 struct wayland_input *input = NULL;
1910
1911 wl_list_for_each(input, &c->input_list, link)
1912 if (&input->base == seat_base)
1913 break;
1914
1915 if (!input || !input->output)
1916 return;
1917
1918 if (input->output->frame)
1919 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1920 else
1921 wayland_output_set_windowed(input->output);
1922
1923 weston_output_schedule_repaint(&input->output->base);
1924}
1925
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001926static struct wayland_compositor *
1927wayland_compositor_create(struct wl_display *display, int use_pixman,
1928 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001929 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001930{
1931 struct wayland_compositor *c;
1932 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001933 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001934
Peter Huttererf3d62272013-08-08 11:57:05 +10001935 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001936 if (c == NULL)
1937 return NULL;
1938
Daniel Stone725c2c32012-06-22 14:04:36 +01001939 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001940 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001941 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001942
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001943 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1944 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001945
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001946 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001947 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001948 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001949 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001950 }
1951
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001952 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001953 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001954 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1955 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001956 wl_display_roundtrip(c->parent.wl_display);
1957
1958 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001959
1960 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001961
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001962 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001963
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001964 if (!c->use_pixman) {
1965 gl_renderer = weston_load_module("gl-renderer.so",
1966 "gl_renderer_interface");
1967 if (!gl_renderer)
1968 c->use_pixman = 1;
1969 }
1970
1971 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001972 if (gl_renderer->create(&c->base,
Jonny Lamb70eba3f2015-03-20 15:26:50 +01001973 EGL_PLATFORM_WAYLAND_KHR,
1974 c->parent.wl_display,
1975 gl_renderer->alpha_attribs,
1976 NULL) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001977 weston_log("Failed to initialize the GL renderer; "
1978 "falling back to pixman.\n");
1979 c->use_pixman = 1;
1980 }
1981 }
1982
1983 if (c->use_pixman) {
1984 if (pixman_renderer_init(&c->base) < 0) {
1985 weston_log("Failed to initialize pixman renderer\n");
1986 goto err_display;
1987 }
1988 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001989
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001990 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001991 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001992
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001993 loop = wl_display_get_event_loop(c->base.wl_display);
1994
1995 fd = wl_display_get_fd(c->parent.wl_display);
1996 c->parent.wl_source =
1997 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1998 wayland_compositor_handle_event, c);
1999 if (c->parent.wl_source == NULL)
2000 goto err_renderer;
2001
2002 wl_event_source_check(c->parent.wl_source);
2003
2004 return c;
2005err_renderer:
2006 c->base.renderer->destroy(&c->base);
2007err_display:
2008 wl_display_disconnect(c->parent.wl_display);
2009err_compositor:
2010 weston_compositor_shutdown(&c->base);
2011err_free:
2012 free(c);
2013 return NULL;
2014}
2015
2016static void
2017wayland_compositor_destroy(struct wayland_compositor *c)
2018{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002019 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002020
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002021 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002022 wayland_output_destroy(output);
2023
2024 c->base.renderer->destroy(&c->base);
2025 wl_display_disconnect(c->parent.wl_display);
2026
2027 if (c->theme)
2028 theme_destroy(c->theme);
2029 if (c->frame_device)
2030 cairo_device_destroy(c->frame_device);
2031 wl_cursor_theme_destroy(c->cursor_theme);
2032
2033 weston_compositor_shutdown(&c->base);
2034 free(c);
2035}
2036
2037WL_EXPORT struct weston_compositor *
2038backend_init(struct wl_display *display, int *argc, char *argv[],
2039 struct weston_config *config)
2040{
2041 struct wayland_compositor *c;
2042 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002043 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002044 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002045 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002046 const char *section_name, *display_name;
2047 char *name;
2048
2049 const struct weston_option wayland_options[] = {
2050 { WESTON_OPTION_INTEGER, "width", 0, &width },
2051 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002052 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002053 { WESTON_OPTION_STRING, "display", 0, &display_name },
2054 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2055 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002056 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002057 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002058 };
2059
2060 width = 0;
2061 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002062 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002063 display_name = NULL;
2064 use_pixman = 0;
2065 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002066 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002067 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002068 parse_options(wayland_options,
2069 ARRAY_LENGTH(wayland_options), argc, argv);
2070
2071 c = wayland_compositor_create(display, use_pixman, display_name,
2072 argc, argv, config);
2073 if (!c)
2074 return NULL;
2075
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002076 if (sprawl || c->parent.fshell) {
2077 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002078 wl_display_roundtrip(c->parent.wl_display);
2079
2080 wl_list_for_each(poutput, &c->parent.output_list, link)
2081 wayland_output_create_for_parent_output(c, poutput);
2082
2083 return &c->base;
2084 }
2085
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002086 if (fullscreen) {
2087 output = wayland_output_create(c, 0, 0, width, height,
2088 NULL, 1, 0, 1);
2089 if (!output)
2090 goto err_outputs;
2091
Axel Davydd8b88d2013-11-17 21:34:16 +01002092 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002093 return &c->base;
2094 }
2095
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002096 section = NULL;
2097 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002098 while (weston_config_next_section(config, &section, &section_name)) {
2099 if (!section_name || strcmp(section_name, "output") != 0)
2100 continue;
2101 weston_config_section_get_string(section, "name", &name, NULL);
2102 if (name == NULL)
2103 continue;
2104
2105 if (name[0] != 'W' || name[1] != 'L') {
2106 free(name);
2107 continue;
2108 }
2109 free(name);
2110
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002111 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002112 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002113 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002114 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002115 if (wayland_output_set_windowed(output))
2116 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002117
2118 x += output->base.width;
2119 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002120 }
2121
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002122 if (!width)
2123 width = 1024;
2124 if (!height)
2125 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002126 if (!scale)
2127 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002128 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002129 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002130 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002131 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002132 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002133 if (wayland_output_set_windowed(output))
2134 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002135
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002136 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002137 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002138 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002139
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002140 weston_compositor_add_key_binding(&c->base, KEY_F,
2141 MODIFIER_CTRL | MODIFIER_ALT,
2142 fullscreen_binding, c);
2143
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002144 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002145
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002146err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002147 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002148 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002149}