blob: 4f792b1494c41a5ecaf8171edca769e1fd7072df [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
Jonny Lamb70eba3f2015-03-20 15:26:50 +010048#include <EGL/eglext.h>
49
Jason Ekstrand48ce4212013-10-27 22:25:02 -050050#define WINDOW_TITLE "Weston Compositor"
51
Jonny Lamb70eba3f2015-03-20 15:26:50 +010052#ifndef EGL_PLATFORM_WAYLAND_KHR
53#define EGL_PLATFORM_WAYLAND_KHR 0x31D8
54#endif
55
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050057 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058
59 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040060 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040061 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_compositor *compositor;
63 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050064 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020065 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010066
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050067 struct wl_list output_list;
68
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069 struct wl_event_source *wl_source;
70 uint32_t event_mask;
71 } parent;
72
Jason Ekstrandff2fd462013-10-27 22:24:58 -050073 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050074 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050075
Jason Ekstrand7744f712013-10-27 22:24:55 -050076 struct theme *theme;
77 cairo_device_t *frame_device;
78 struct wl_cursor_theme *cursor_theme;
79 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050080
Jason Ekstrand06ced802013-11-07 20:13:29 -060081 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010082};
83
84struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050085 struct weston_output base;
86
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010087 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050088 int draw_initial_frame;
89 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050090
91 struct wl_output *output;
92 uint32_t global_id;
93
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050094 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060095 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050097
Jason Ekstrand7744f712013-10-27 22:24:55 -050098 int keyboard_count;
99
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600100 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500101 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500102
Jason Ekstrand7744f712013-10-27 22:24:55 -0500103 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500104 struct wl_egl_window *egl_window;
105 struct {
106 cairo_surface_t *top;
107 cairo_surface_t *left;
108 cairo_surface_t *right;
109 cairo_surface_t *bottom;
110 } border;
111 } gl;
112
113 struct {
114 struct wl_list buffers;
115 struct wl_list free_buffers;
116 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500117
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500118 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500119 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100120};
121
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500122struct wayland_parent_output {
123 struct wayland_output *output;
124 struct wl_list link;
125
126 struct wl_output *global;
127 uint32_t id;
128
129 struct {
130 char *make;
131 char *model;
132 int32_t width, height;
133 uint32_t subpixel;
134 } physical;
135
136 int32_t x, y;
137 uint32_t transform;
138 uint32_t scale;
139
140 struct wl_list mode_list;
141 struct weston_mode *preferred_mode;
142 struct weston_mode *current_mode;
143};
144
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500145struct wayland_shm_buffer {
146 struct wayland_output *output;
147 struct wl_list link;
148 struct wl_list free_link;
149
150 struct wl_buffer *buffer;
151 void *data;
152 size_t size;
153 pixman_region32_t damage;
154 int frame_damaged;
155
156 pixman_image_t *pm_image;
157 cairo_surface_t *c_surface;
158};
159
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100160struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400161 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100162 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100163 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500164
165 struct {
166 struct wl_seat *seat;
167 struct wl_pointer *pointer;
168 struct wl_keyboard *keyboard;
169 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500170
171 struct {
172 struct wl_surface *surface;
173 int32_t hx, hy;
174 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500175 } parent;
176
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500177 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100178 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400179 uint32_t enter_serial;
180 int focus;
181 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500182 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100183};
184
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300185struct gl_renderer_interface *gl_renderer;
186
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500187static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500188wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
189{
190 cairo_surface_destroy(buffer->c_surface);
191 pixman_image_unref(buffer->pm_image);
192
193 wl_buffer_destroy(buffer->buffer);
194 munmap(buffer->data, buffer->size);
195
196 pixman_region32_fini(&buffer->damage);
197
198 wl_list_remove(&buffer->link);
199 wl_list_remove(&buffer->free_link);
200 free(buffer);
201}
202
203static void
204buffer_release(void *data, struct wl_buffer *buffer)
205{
206 struct wayland_shm_buffer *sb = data;
207
208 if (sb->output) {
209 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
210 } else {
211 wayland_shm_buffer_destroy(sb);
212 }
213}
214
215static const struct wl_buffer_listener buffer_listener = {
216 buffer_release
217};
218
219static struct wayland_shm_buffer *
220wayland_output_get_shm_buffer(struct wayland_output *output)
221{
222 struct wayland_compositor *c =
223 (struct wayland_compositor *) output->base.compositor;
224 struct wl_shm *shm = c->parent.shm;
225 struct wayland_shm_buffer *sb;
226
227 struct wl_shm_pool *pool;
228 int width, height, stride;
229 int32_t fx, fy;
230 int fd;
231 unsigned char *data;
232
233 if (!wl_list_empty(&output->shm.free_buffers)) {
234 sb = container_of(output->shm.free_buffers.next,
235 struct wayland_shm_buffer, free_link);
236 wl_list_remove(&sb->free_link);
237 wl_list_init(&sb->free_link);
238
239 return sb;
240 }
241
242 if (output->frame) {
243 width = frame_width(output->frame);
244 height = frame_height(output->frame);
245 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500246 width = output->base.current_mode->width;
247 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500248 }
249
250 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
251
252 fd = os_create_anonymous_file(height * stride);
253 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000254 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500255 return NULL;
256 }
257
258 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
259 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000260 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500261 close(fd);
262 return NULL;
263 }
264
265 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000266 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200267 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000268 close(fd);
269 free(data);
270 return NULL;
271 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500272
273 sb->output = output;
274 wl_list_init(&sb->free_link);
275 wl_list_insert(&output->shm.buffers, &sb->link);
276
277 pixman_region32_init_rect(&sb->damage, 0, 0,
278 output->base.width, output->base.height);
279 sb->frame_damaged = 1;
280
281 sb->data = data;
282 sb->size = height * stride;
283
284 pool = wl_shm_create_pool(shm, fd, sb->size);
285
286 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
287 width, height,
288 stride,
289 WL_SHM_FORMAT_ARGB8888);
290 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
291 wl_shm_pool_destroy(pool);
292 close(fd);
293
294 memset(data, 0, sb->size);
295
296 sb->c_surface =
297 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
298 width, height, stride);
299
300 fx = 0;
301 fy = 0;
302 if (output->frame)
303 frame_interior(output->frame, &fx, &fy, 0, 0);
304 sb->pm_image =
305 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
306 (uint32_t *)(data + fy * stride) + fx,
307 stride);
308
309 return sb;
310}
311
312static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500313frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500315 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400316 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400317
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500318 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400319
320 /* XXX: use the presentation extension for proper timings */
321 ts.tv_sec = time / 1000;
322 ts.tv_nsec = (time % 1000) * 1000000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200323 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400324}
325
326static const struct wl_callback_listener frame_listener = {
327 frame_done
328};
329
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500330static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200331draw_initial_frame(struct wayland_output *output)
332{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500333 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200334
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500335 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200336
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500337 /* If we are rendering with GL, then orphan it so that it gets
338 * destroyed immediately */
339 if (output->gl.egl_window)
340 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500341
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500342 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500343 wl_surface_damage(output->parent.surface, 0, 0,
344 output->base.current_mode->width,
345 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200346}
347
348static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500349wayland_output_update_gl_border(struct wayland_output *output)
350{
351 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
352 cairo_t *cr;
353
354 if (!output->frame)
355 return;
356 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
357 return;
358
359 fwidth = frame_width(output->frame);
360 fheight = frame_height(output->frame);
361 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
362
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500363 if (!output->gl.border.top)
364 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500365 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
366 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500367 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500368 frame_repaint(output->frame, cr);
369 cairo_destroy(cr);
370 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
371 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500372 cairo_image_surface_get_stride(output->gl.border.top) / 4,
373 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500374
375
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500376 if (!output->gl.border.left)
377 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500378 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
379 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500380 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500381 cairo_translate(cr, 0, -iy);
382 frame_repaint(output->frame, cr);
383 cairo_destroy(cr);
384 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
385 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500386 cairo_image_surface_get_stride(output->gl.border.left) / 4,
387 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500388
389
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500390 if (!output->gl.border.right)
391 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500392 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
393 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500395 cairo_translate(cr, -(iwidth + ix), -iy);
396 frame_repaint(output->frame, cr);
397 cairo_destroy(cr);
398 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
399 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500400 cairo_image_surface_get_stride(output->gl.border.right) / 4,
401 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500402
403
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500404 if (!output->gl.border.bottom)
405 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500406 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
407 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409 cairo_translate(cr, 0, -(iy + iheight));
410 frame_repaint(output->frame, cr);
411 cairo_destroy(cr);
412 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
413 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500414 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
415 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500416}
417
418static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200419wayland_output_start_repaint_loop(struct weston_output *output_base)
420{
421 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500422 struct wayland_compositor *wc =
423 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200424 struct wl_callback *callback;
425
426 /* If this is the initial frame, we need to attach a buffer so that
427 * the compositor can map the surface and include it in its render
428 * loop. If the surface doesn't end up in the render loop, the frame
429 * callback won't be invoked. The buffer is transparent and of the
430 * same size as the future real output buffer. */
431 if (output->parent.draw_initial_frame) {
432 output->parent.draw_initial_frame = 0;
433
434 draw_initial_frame(output);
435 }
436
437 callback = wl_surface_frame(output->parent.surface);
438 wl_callback_add_listener(callback, &frame_listener, output);
439 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500440 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200441}
442
David Herrmann1edf44c2013-10-22 17:11:26 +0200443static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500444wayland_output_repaint_gl(struct weston_output *output_base,
445 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400446{
447 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400448 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400449 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600450
Kristian Høgsberg33418202011-08-16 23:01:28 -0400451 callback = wl_surface_frame(output->parent.surface);
452 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100453
Jason Ekstrand7744f712013-10-27 22:24:55 -0500454 wayland_output_update_gl_border(output);
455
Pekka Paalanenbc106382012-10-10 12:49:31 +0300456 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200457
458 pixman_region32_subtract(&ec->primary_plane.damage,
459 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200460 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100461}
462
Matt Roper361d2ad2011-08-29 13:52:23 -0700463static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500464wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
465{
466 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
467 cairo_t *cr;
468
469 if (!buffer->output->frame || !buffer->frame_damaged)
470 return;
471
472 cr = cairo_create(buffer->c_surface);
473
474 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
475 fwidth = frame_width(buffer->output->frame);
476 fheight = frame_height(buffer->output->frame);
477
478 /* Set the clip so we don't unnecisaraly damage the surface */
479 cairo_move_to(cr, ix, iy);
480 cairo_rel_line_to(cr, iwidth, 0);
481 cairo_rel_line_to(cr, 0, iheight);
482 cairo_rel_line_to(cr, -iwidth, 0);
483 cairo_line_to(cr, ix, iy);
484 cairo_line_to(cr, 0, iy);
485 cairo_line_to(cr, 0, fheight);
486 cairo_line_to(cr, fwidth, fheight);
487 cairo_line_to(cr, fwidth, 0);
488 cairo_line_to(cr, 0, 0);
489 cairo_line_to(cr, 0, iy);
490 cairo_close_path(cr);
491 cairo_clip(cr);
492
493 /* Draw using a pattern so that the final result gets clipped */
494 cairo_push_group(cr);
495 frame_repaint(buffer->output->frame, cr);
496 cairo_pop_group_to_source(cr);
497 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
498 cairo_paint(cr);
499
500 cairo_destroy(cr);
501}
502
503static void
504wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
505{
506 pixman_region32_t damage;
507 pixman_box32_t *rects;
508 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
509 int i, n;
510
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500511 pixman_region32_init(&damage);
512 weston_transformed_region(sb->output->base.width,
513 sb->output->base.height,
514 sb->output->base.transform,
515 sb->output->base.current_scale,
516 &sb->damage, &damage);
517
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500518 if (sb->output->frame) {
519 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
520 fwidth = frame_width(sb->output->frame);
521 fheight = frame_height(sb->output->frame);
522
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500523 pixman_region32_translate(&damage, ix, iy);
524
525 if (sb->frame_damaged) {
526 pixman_region32_union_rect(&damage, &damage,
527 0, 0, fwidth, iy);
528 pixman_region32_union_rect(&damage, &damage,
529 0, iy, ix, iheight);
530 pixman_region32_union_rect(&damage, &damage,
531 ix + iwidth, iy,
532 fwidth - (ix + iwidth), iheight);
533 pixman_region32_union_rect(&damage, &damage,
534 0, iy + iheight,
535 fwidth, fheight - (iy + iheight));
536 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500537 }
538
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500539 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500540 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
541 for (i = 0; i < n; ++i)
542 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
543 rects[i].y1, rects[i].x2 - rects[i].x1,
544 rects[i].y2 - rects[i].y1);
545
546 if (sb->output->frame)
547 pixman_region32_fini(&damage);
548}
549
550static int
551wayland_output_repaint_pixman(struct weston_output *output_base,
552 pixman_region32_t *damage)
553{
554 struct wayland_output *output = (struct wayland_output *) output_base;
555 struct wayland_compositor *c =
556 (struct wayland_compositor *)output->base.compositor;
557 struct wl_callback *callback;
558 struct wayland_shm_buffer *sb;
559
560 if (output->frame) {
561 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
562 wl_list_for_each(sb, &output->shm.buffers, link)
563 sb->frame_damaged = 1;
564 }
565
566 wl_list_for_each(sb, &output->shm.buffers, link)
567 pixman_region32_union(&sb->damage, &sb->damage, damage);
568
569 sb = wayland_output_get_shm_buffer(output);
570
571 wayland_output_update_shm_border(sb);
572 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
573 c->base.renderer->repaint_output(output_base, &sb->damage);
574
575 wayland_shm_buffer_attach(sb);
576
577 callback = wl_surface_frame(output->parent.surface);
578 wl_callback_add_listener(callback, &frame_listener, output);
579 wl_surface_commit(output->parent.surface);
580 wl_display_flush(c->parent.wl_display);
581
582 pixman_region32_fini(&sb->damage);
583 pixman_region32_init(&sb->damage);
584 sb->frame_damaged = 0;
585
586 pixman_region32_subtract(&c->base.primary_plane.damage,
587 &c->base.primary_plane.damage, damage);
588 return 0;
589}
590
591static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500592wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700593{
594 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500595 struct wayland_compositor *c =
596 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700597
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500598 if (c->use_pixman) {
599 pixman_renderer_output_destroy(output_base);
600 } else {
601 gl_renderer->output_destroy(output_base);
602 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100603
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500604 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500605 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500606 if (output->parent.shell_surface)
607 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500608
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600609 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500610 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600611
612 cairo_surface_destroy(output->gl.border.top);
613 cairo_surface_destroy(output->gl.border.left);
614 cairo_surface_destroy(output->gl.border.right);
615 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500616
617 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700618 free(output);
619
620 return;
621}
622
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300623static const struct wl_shell_surface_listener shell_surface_listener;
624
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200625static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500626wayland_output_init_gl_renderer(struct wayland_output *output)
627{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500628 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500629
630 if (output->frame) {
631 fwidth = frame_width(output->frame);
632 fheight = frame_height(output->frame);
633 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500634 fwidth = output->base.current_mode->width;
635 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500636 }
637
638 output->gl.egl_window =
639 wl_egl_window_create(output->parent.surface,
640 fwidth, fheight);
641 if (!output->gl.egl_window) {
642 weston_log("failure to create wl_egl_window\n");
643 return -1;
644 }
645
646 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000647 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100648 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000649 gl_renderer->alpha_attribs,
650 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500651 goto cleanup_window;
652
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500653 return 0;
654
655cleanup_window:
656 wl_egl_window_destroy(output->gl.egl_window);
657 return -1;
658}
659
660static int
661wayland_output_init_pixman_renderer(struct wayland_output *output)
662{
663 return pixman_renderer_output_create(&output->base);
664}
665
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600666static void
667wayland_output_resize_surface(struct wayland_output *output)
668{
669 struct wayland_compositor *c =
670 (struct wayland_compositor *)output->base.compositor;
671 struct wayland_shm_buffer *buffer, *next;
672 int32_t ix, iy, iwidth, iheight;
673 int32_t width, height;
674 struct wl_region *region;
675
676 width = output->base.current_mode->width;
677 height = output->base.current_mode->height;
678
679 if (output->frame) {
680 frame_resize_inside(output->frame, width, height);
681
682 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
683 region = wl_compositor_create_region(c->parent.compositor);
684 wl_region_add(region, ix, iy, iwidth, iheight);
685 wl_surface_set_input_region(output->parent.surface, region);
686 wl_region_destroy(region);
687
688 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
689 region = wl_compositor_create_region(c->parent.compositor);
690 wl_region_add(region, ix, iy, iwidth, iheight);
691 wl_surface_set_opaque_region(output->parent.surface, region);
692 wl_region_destroy(region);
693
694 width = frame_width(output->frame);
695 height = frame_height(output->frame);
696 } else {
697 region = wl_compositor_create_region(c->parent.compositor);
698 wl_region_add(region, 0, 0, width, height);
699 wl_surface_set_input_region(output->parent.surface, region);
700 wl_region_destroy(region);
701
702 region = wl_compositor_create_region(c->parent.compositor);
703 wl_region_add(region, 0, 0, width, height);
704 wl_surface_set_opaque_region(output->parent.surface, region);
705 wl_region_destroy(region);
706 }
707
708 if (output->gl.egl_window) {
709 wl_egl_window_resize(output->gl.egl_window,
710 width, height, 0, 0);
711
712 /* These will need to be re-created due to the resize */
713 gl_renderer->output_set_border(&output->base,
714 GL_RENDERER_BORDER_TOP,
715 0, 0, 0, NULL);
716 cairo_surface_destroy(output->gl.border.top);
717 output->gl.border.top = NULL;
718 gl_renderer->output_set_border(&output->base,
719 GL_RENDERER_BORDER_LEFT,
720 0, 0, 0, NULL);
721 cairo_surface_destroy(output->gl.border.left);
722 output->gl.border.left = NULL;
723 gl_renderer->output_set_border(&output->base,
724 GL_RENDERER_BORDER_RIGHT,
725 0, 0, 0, NULL);
726 cairo_surface_destroy(output->gl.border.right);
727 output->gl.border.right = NULL;
728 gl_renderer->output_set_border(&output->base,
729 GL_RENDERER_BORDER_BOTTOM,
730 0, 0, 0, NULL);
731 cairo_surface_destroy(output->gl.border.bottom);
732 output->gl.border.bottom = NULL;
733 }
734
735 /* Throw away any remaining SHM buffers */
736 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
737 wayland_shm_buffer_destroy(buffer);
738 /* These will get thrown away when they get released */
739 wl_list_for_each(buffer, &output->shm.buffers, link)
740 buffer->output = NULL;
741}
742
743static int
744wayland_output_set_windowed(struct wayland_output *output)
745{
746 struct wayland_compositor *c =
747 (struct wayland_compositor *)output->base.compositor;
748 int tlen;
749 char *title;
750
751 if (output->frame)
752 return 0;
753
754 if (output->name) {
755 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
756 title = malloc(tlen + 1);
757 if (!title)
758 return -1;
759
760 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
761 } else {
762 title = strdup(WINDOW_TITLE);
763 }
764
765 if (!c->theme) {
766 c->theme = theme_create();
767 if (!c->theme) {
768 free(title);
769 return -1;
770 }
771 }
772 output->frame = frame_create(c->theme, 100, 100,
773 FRAME_BUTTON_CLOSE, title);
774 free(title);
775 if (!output->frame)
776 return -1;
777
778 if (output->keyboard_count)
779 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
780
781 wayland_output_resize_surface(output);
782
783 wl_shell_surface_set_toplevel(output->parent.shell_surface);
784
785 return 0;
786}
787
788static void
789wayland_output_set_fullscreen(struct wayland_output *output,
790 enum wl_shell_surface_fullscreen_method method,
791 uint32_t framerate, struct wl_output *target)
792{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500793 struct wayland_compositor *c =
794 (struct wayland_compositor *)output->base.compositor;
795
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600796 if (output->frame) {
797 frame_destroy(output->frame);
798 output->frame = NULL;
799 }
800
801 wayland_output_resize_surface(output);
802
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500803 if (output->parent.shell_surface) {
804 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
805 method, framerate, target);
806 } else if (c->parent.fshell) {
807 _wl_fullscreen_shell_present_surface(c->parent.fshell,
808 output->parent.surface,
809 method, target);
810 }
811}
812
813static struct weston_mode *
814wayland_output_choose_mode(struct wayland_output *output,
815 struct weston_mode *ref_mode)
816{
817 struct weston_mode *mode;
818
819 /* First look for an exact match */
820 wl_list_for_each(mode, &output->base.mode_list, link)
821 if (mode->width == ref_mode->width &&
822 mode->height == ref_mode->height &&
823 mode->refresh == ref_mode->refresh)
824 return mode;
825
826 /* If we can't find an exact match, ignore refresh and try again */
827 wl_list_for_each(mode, &output->base.mode_list, link)
828 if (mode->width == ref_mode->width &&
829 mode->height == ref_mode->height)
830 return mode;
831
832 /* Yeah, we failed */
833 return NULL;
834}
835
836enum mode_status {
837 MODE_STATUS_UNKNOWN,
838 MODE_STATUS_SUCCESS,
839 MODE_STATUS_FAIL,
840 MODE_STATUS_CANCEL,
841};
842
843static void
844mode_feedback_successful(void *data,
845 struct _wl_fullscreen_shell_mode_feedback *fb)
846{
847 enum mode_status *value = data;
848
849 printf("Mode switch successful\n");
850
851 *value = MODE_STATUS_SUCCESS;
852}
853
854static void
855mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
856{
857 enum mode_status *value = data;
858
859 printf("Mode switch failed\n");
860
861 *value = MODE_STATUS_FAIL;
862}
863
864static void
865mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
866{
867 enum mode_status *value = data;
868
869 printf("Mode switch cancelled\n");
870
871 *value = MODE_STATUS_CANCEL;
872}
873
874struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
875 mode_feedback_successful,
876 mode_feedback_failed,
877 mode_feedback_cancelled,
878};
879
880static int
881wayland_output_switch_mode(struct weston_output *output_base,
882 struct weston_mode *mode)
883{
884 struct wayland_output *output = (struct wayland_output *) output_base;
885 struct wayland_compositor *c;
886 struct wl_surface *old_surface;
887 struct weston_mode *old_mode;
888 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
889 enum mode_status mode_status;
890 int ret = 0;
891
892 if (output_base == NULL) {
893 weston_log("output is NULL.\n");
894 return -1;
895 }
896
897 if (mode == NULL) {
898 weston_log("mode is NULL.\n");
899 return -1;
900 }
901
902 c = (struct wayland_compositor *)output_base->compositor;
903
904 if (output->parent.shell_surface || !c->parent.fshell)
905 return -1;
906
907 mode = wayland_output_choose_mode(output, mode);
908 if (mode == NULL)
909 return -1;
910
911 if (output->base.current_mode == mode)
912 return 0;
913
914 old_mode = output->base.current_mode;
915 old_surface = output->parent.surface;
916 output->base.current_mode = mode;
917 output->parent.surface =
918 wl_compositor_create_surface(c->parent.compositor);
919 wl_surface_set_user_data(output->parent.surface, output);
920
921 /* Blow the old buffers because we changed size/surfaces */
922 wayland_output_resize_surface(output);
923
924 mode_feedback =
925 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
926 output->parent.surface,
927 output->parent.output,
928 mode->refresh);
929 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
930 &mode_feedback_listener,
931 &mode_status);
932
933 /* This should kick-start things again */
934 output->parent.draw_initial_frame = 1;
935 wayland_output_start_repaint_loop(&output->base);
936
937 mode_status = MODE_STATUS_UNKNOWN;
938 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
939 ret = wl_display_dispatch(c->parent.wl_display);
940
941 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
942
943 if (mode_status == MODE_STATUS_FAIL) {
944 output->base.current_mode = old_mode;
945 wl_surface_destroy(output->parent.surface);
946 output->parent.surface = old_surface;
947 wayland_output_resize_surface(output);
948
949 return -1;
950 }
951
952 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
953 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
954
955 if (c->use_pixman) {
956 pixman_renderer_output_destroy(output_base);
957 if (wayland_output_init_pixman_renderer(output) < 0)
958 goto err_output;
959 } else {
960 gl_renderer->output_destroy(output_base);
961 wl_egl_window_destroy(output->gl.egl_window);
962 if (wayland_output_init_gl_renderer(output) < 0)
963 goto err_output;
964 }
965 wl_surface_destroy(old_surface);
966
967 weston_output_schedule_repaint(&output->base);
968
969 return 0;
970
971err_output:
972 /* XXX */
973 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600974}
975
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500976static struct wayland_output *
977wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600978 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500979 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100980{
981 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500982 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600983
984 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
985 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100986
Peter Huttererf3d62272013-08-08 11:57:05 +1000987 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100988 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500989 return NULL;
990
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600991 output->name = name ? strdup(name) : NULL;
992 output->base.make = "waywayland";
993 output->base.model = "none";
994
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500995 output_width = width * scale;
996 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100997
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600998 output->parent.surface =
999 wl_compositor_create_surface(c->parent.compositor);
1000 if (!output->parent.surface)
1001 goto err_name;
1002 wl_surface_set_user_data(output->parent.surface, output);
1003
1004 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001005
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001006 if (c->parent.shell) {
1007 output->parent.shell_surface =
1008 wl_shell_get_shell_surface(c->parent.shell,
1009 output->parent.surface);
1010 if (!output->parent.shell_surface)
1011 goto err_surface;
1012 wl_shell_surface_add_listener(output->parent.shell_surface,
1013 &shell_surface_listener, output);
1014 }
1015
1016 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001017 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1018 0, 0, NULL);
1019 wl_display_roundtrip(c->parent.wl_display);
1020 if (!width)
1021 output_width = output->parent.configure_width;
1022 if (!height)
1023 output_height = output->parent.configure_height;
1024 }
1025
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001026 output->mode.flags =
1027 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001028 output->mode.width = output_width;
1029 output->mode.height = output_height;
1030 output->mode.refresh = 60000;
1031 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001032 wl_list_init(&output->base.mode_list);
1033 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001034 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001035
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001036 wl_list_init(&output->shm.buffers);
1037 wl_list_init(&output->shm.free_buffers);
1038
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001039 weston_output_init(&output->base, &c->base, x, y, width, height,
1040 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001041
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001042 if (c->use_pixman) {
1043 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001044 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001045 output->base.repaint = wayland_output_repaint_pixman;
1046 } else {
1047 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001048 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001049 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001050 }
1051
Jonas Ådahle5a12252013-04-05 23:07:11 +02001052 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001053 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001054 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001055 output->base.set_backlight = NULL;
1056 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001057 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001058
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001059 wl_list_insert(c->base.output_list.prev, &output->base.link);
1060
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001061 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001062
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001063err_output:
1064 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001065 if (output->parent.shell_surface)
1066 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001067err_surface:
1068 wl_surface_destroy(output->parent.surface);
1069err_name:
1070 free(output->name);
1071
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001072 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001073 free(output);
1074
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001075 return NULL;
1076}
1077
1078static struct wayland_output *
1079wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001080 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001081 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001082 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001083{
1084 struct wayland_output *output;
1085 char *mode, *t, *name, *str;
1086 int width, height, scale;
1087 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001088 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001089
1090 weston_config_section_get_string(config_section, "name", &name, NULL);
1091 if (name) {
1092 slen = strlen(name);
1093 slen += strlen(WINDOW_TITLE " - ");
1094 str = malloc(slen + 1);
1095 if (str)
1096 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1097 free(name);
1098 name = str;
1099 }
1100 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001101 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001102
1103 weston_config_section_get_string(config_section,
1104 "mode", &mode, "1024x600");
1105 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1106 weston_log("Invalid mode \"%s\" for output %s\n",
1107 mode, name);
1108 width = 1024;
1109 height = 640;
1110 }
1111 free(mode);
1112
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001113 if (option_width)
1114 width = option_width;
1115 if (option_height)
1116 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001117
1118 weston_config_section_get_int(config_section, "scale", &scale, 1);
1119
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001120 if (option_scale)
1121 scale = option_scale;
1122
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001123 weston_config_section_get_string(config_section,
1124 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001125 if (weston_parse_transform(t, &transform) < 0)
1126 weston_log("Invalid transform \"%s\" for output %s\n",
1127 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001128 free(t);
1129
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001130 output = wayland_output_create(c, x, y, width, height, name, 0,
1131 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001132 free(name);
1133
1134 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001135}
1136
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001137static struct wayland_output *
1138wayland_output_create_for_parent_output(struct wayland_compositor *c,
1139 struct wayland_parent_output *poutput)
1140{
1141 struct wayland_output *output;
1142 struct weston_mode *mode;
1143 int32_t x;
1144
1145 if (poutput->current_mode) {
1146 mode = poutput->current_mode;
1147 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001148 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001149 } else if (!wl_list_empty(&poutput->mode_list)) {
1150 mode = container_of(poutput->mode_list.next,
1151 struct weston_mode, link);
1152 } else {
1153 weston_log("No valid modes found. Skipping output");
1154 return NULL;
1155 }
1156
1157 if (!wl_list_empty(&c->base.output_list)) {
1158 output = container_of(c->base.output_list.prev,
1159 struct wayland_output, base.link);
1160 x = output->base.x + output->base.current_mode->width;
1161 } else {
1162 x = 0;
1163 }
1164
1165 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1166 NULL, 0,
1167 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1168 if (!output)
1169 return NULL;
1170
1171 output->parent.output = poutput->global;
1172
1173 output->base.make = poutput->physical.make;
1174 output->base.model = poutput->physical.model;
1175 wl_list_init(&output->base.mode_list);
1176 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1177 wl_list_init(&poutput->mode_list);
1178
1179 wayland_output_set_fullscreen(output,
1180 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1181 mode->refresh, poutput->global);
1182
1183 if (output->parent.shell_surface) {
1184 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1185 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1186 mode->refresh, poutput->global);
1187 } else if (c->parent.fshell) {
1188 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1189 output->parent.surface,
1190 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1191 poutput->global);
1192 _wl_fullscreen_shell_mode_feedback_destroy(
1193 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1194 output->parent.surface,
1195 poutput->global,
1196 mode->refresh));
1197 }
1198
1199 return output;
1200}
1201
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001202static void
1203shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1204 uint32_t serial)
1205{
1206 wl_shell_surface_pong(shell_surface, serial);
1207}
1208
1209static void
1210shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1211 uint32_t edges, int32_t width, int32_t height)
1212{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001213 struct wayland_output *output = data;
1214
1215 output->parent.configure_width = width;
1216 output->parent.configure_height = height;
1217
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001218 /* FIXME: implement resizing */
1219}
1220
1221static void
1222shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1223{
1224}
1225
1226static const struct wl_shell_surface_listener shell_surface_listener = {
1227 shell_surface_ping,
1228 shell_surface_configure,
1229 shell_surface_popup_done
1230};
1231
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001232/* Events received from the wayland-server this compositor is client of: */
1233
Jason Ekstrand7744f712013-10-27 22:24:55 -05001234/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001235static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001236input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001237{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001238
Jason Ekstrand7744f712013-10-27 22:24:55 -05001239 struct wl_buffer *buffer;
1240 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001241
Jason Ekstrand7744f712013-10-27 22:24:55 -05001242 if (!input->compositor->cursor)
1243 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001244
Jason Ekstrand7744f712013-10-27 22:24:55 -05001245 image = input->compositor->cursor->images[0];
1246 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001247 if (!buffer)
1248 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001249
1250 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1251 input->parent.cursor.surface,
1252 image->hotspot_x, image->hotspot_y);
1253
1254 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1255 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1256 image->width, image->height);
1257 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001258}
1259
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001260static void
Daniel Stone37816df2012-05-16 18:45:18 +01001261input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1262 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001263 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001264{
1265 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001266 int32_t fx, fy;
1267 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001268
Daniel Stone50692802012-06-22 13:21:41 +01001269 /* XXX: If we get a modifier event immediately before the focus,
1270 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001271 input->enter_serial = serial;
1272 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001273
1274 if (input->output->frame) {
1275 location = frame_pointer_enter(input->output->frame, input,
1276 wl_fixed_to_int(x),
1277 wl_fixed_to_int(y));
1278 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1279 x -= wl_fixed_from_int(fx);
1280 y -= wl_fixed_from_int(fy);
1281
1282 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1283 weston_output_schedule_repaint(&input->output->base);
1284 } else {
1285 location = THEME_LOCATION_CLIENT_AREA;
1286 }
1287
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001288 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001289
1290 if (location == THEME_LOCATION_CLIENT_AREA) {
1291 input->focus = 1;
1292 notify_pointer_focus(&input->base, &input->output->base, x, y);
1293 wl_pointer_set_cursor(input->parent.pointer,
1294 input->enter_serial, NULL, 0, 0);
1295 } else {
1296 input->focus = 0;
1297 notify_pointer_focus(&input->base, NULL, 0, 0);
1298 input_set_cursor(input);
1299 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001300}
1301
1302static void
Daniel Stone37816df2012-05-16 18:45:18 +01001303input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1304 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001305{
1306 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001307
Jason Ekstrand7744f712013-10-27 22:24:55 -05001308 if (input->output->frame) {
1309 frame_pointer_leave(input->output->frame, input);
1310
1311 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1312 weston_output_schedule_repaint(&input->output->base);
1313 }
1314
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001315 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001316 input->output = NULL;
1317 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001318}
1319
1320static void
Daniel Stone37816df2012-05-16 18:45:18 +01001321input_handle_motion(void *data, struct wl_pointer *pointer,
1322 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1323{
1324 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001325 int32_t fx, fy;
1326 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001327
Jason Ekstrand7744f712013-10-27 22:24:55 -05001328 if (input->output->frame) {
1329 location = frame_pointer_motion(input->output->frame, input,
1330 wl_fixed_to_int(x),
1331 wl_fixed_to_int(y));
1332 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1333 x -= wl_fixed_from_int(fx);
1334 y -= wl_fixed_from_int(fy);
1335
1336 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1337 weston_output_schedule_repaint(&input->output->base);
1338 } else {
1339 location = THEME_LOCATION_CLIENT_AREA;
1340 }
1341
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001342 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001343
1344 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1345 input_set_cursor(input);
1346 notify_pointer_focus(&input->base, NULL, 0, 0);
1347 input->focus = 0;
1348 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1349 wl_pointer_set_cursor(input->parent.pointer,
1350 input->enter_serial, NULL, 0, 0);
1351 notify_pointer_focus(&input->base, &input->output->base, x, y);
1352 input->focus = 1;
1353 }
1354
1355 if (location == THEME_LOCATION_CLIENT_AREA)
1356 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001357}
1358
1359static void
1360input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001361 uint32_t serial, uint32_t time, uint32_t button,
1362 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001363{
1364 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001365 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001366 enum frame_button_state fstate;
1367 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001368
Jason Ekstrand7744f712013-10-27 22:24:55 -05001369 if (input->output->frame) {
1370 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1371 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1372
1373 location = frame_pointer_button(input->output->frame, input,
1374 button, fstate);
1375
1376 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1377
1378 wl_shell_surface_move(input->output->parent.shell_surface,
1379 input->parent.seat, serial);
1380 frame_status_clear(input->output->frame,
1381 FRAME_STATUS_MOVE);
1382 return;
1383 }
1384
1385 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1386 wl_display_terminate(input->compositor->base.wl_display);
1387
1388 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1389 weston_output_schedule_repaint(&input->output->base);
1390 } else {
1391 location = THEME_LOCATION_CLIENT_AREA;
1392 }
1393
1394 if (location == THEME_LOCATION_CLIENT_AREA)
1395 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001396}
1397
1398static void
1399input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001400 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001401{
1402 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001403
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001404 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001405}
1406
1407static const struct wl_pointer_listener pointer_listener = {
1408 input_handle_pointer_enter,
1409 input_handle_pointer_leave,
1410 input_handle_motion,
1411 input_handle_button,
1412 input_handle_axis,
1413};
1414
1415static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001416input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1417 int fd, uint32_t size)
1418{
1419 struct wayland_input *input = data;
1420 struct xkb_keymap *keymap;
1421 char *map_str;
1422
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001423 if (!data) {
1424 close(fd);
1425 return;
1426 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001427
1428 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1429 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1430 if (map_str == MAP_FAILED) {
1431 weston_log("mmap failed: %m\n");
1432 goto error;
1433 }
1434
Ran Benita2e1968f2014-08-19 23:59:51 +03001435 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1436 map_str,
1437 XKB_KEYMAP_FORMAT_TEXT_V1,
1438 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001439 munmap(map_str, size);
1440
1441 if (!keymap) {
1442 weston_log("failed to compile keymap\n");
1443 goto error;
1444 }
1445
1446 input->keyboard_state_update = STATE_UPDATE_NONE;
1447 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1448 weston_log("No keymap provided; falling back to defalt\n");
1449 keymap = NULL;
1450 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1451 } else {
1452 weston_log("Invalid keymap\n");
1453 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001454 }
1455
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001456 close(fd);
1457
Rui Matos0c194ce2013-10-10 19:44:21 +02001458 if (input->base.keyboard)
1459 weston_seat_update_keymap(&input->base, keymap);
1460 else
1461 weston_seat_init_keyboard(&input->base, keymap);
1462
Ran Benitac9c74152014-08-19 23:59:52 +03001463 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001464
1465 return;
1466
1467error:
1468 wl_keyboard_release(input->parent.keyboard);
1469 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001470}
1471
1472static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001473input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001474 struct wl_keyboard *keyboard,
1475 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001476 struct wl_surface *surface,
1477 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001478{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001479 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001480 struct wayland_output *focus;
1481
1482 focus = input->keyboard_focus;
1483 if (focus) {
1484 /* This shouldn't happen */
1485 focus->keyboard_count--;
1486 if (!focus->keyboard_count && focus->frame)
1487 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1488 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1489 weston_output_schedule_repaint(&focus->base);
1490 }
1491
1492 input->keyboard_focus = wl_surface_get_user_data(surface);
1493 input->keyboard_focus->keyboard_count++;
1494
1495 focus = input->keyboard_focus;
1496 if (focus->frame) {
1497 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1498 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1499 weston_output_schedule_repaint(&focus->base);
1500 }
1501
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001502
Daniel Stone50692802012-06-22 13:21:41 +01001503 /* XXX: If we get a modifier event immediately before the focus,
1504 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001505 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001506 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001507}
1508
1509static void
1510input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001511 struct wl_keyboard *keyboard,
1512 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001513 struct wl_surface *surface)
1514{
1515 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001516 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001517
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001518 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001519
1520 focus = input->keyboard_focus;
1521 if (!focus)
1522 return; /* This shouldn't happen */
1523
1524 focus->keyboard_count--;
1525 if (!focus->keyboard_count && focus->frame) {
1526 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1527 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1528 weston_output_schedule_repaint(&focus->base);
1529 }
1530
1531 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001532}
1533
Daniel Stone37816df2012-05-16 18:45:18 +01001534static void
1535input_handle_key(void *data, struct wl_keyboard *keyboard,
1536 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1537{
1538 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001539
Daniel Stone50692802012-06-22 13:21:41 +01001540 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001541 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001542 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001543 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001544 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001545}
1546
Daniel Stone351eb612012-05-31 15:27:47 -04001547static void
1548input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001549 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001550 uint32_t mods_latched, uint32_t mods_locked,
1551 uint32_t group)
1552{
Daniel Stone50692802012-06-22 13:21:41 +01001553 struct wayland_input *input = data;
1554 struct wayland_compositor *c = input->compositor;
1555 uint32_t serial_out;
1556
1557 /* If we get a key event followed by a modifier event with the
1558 * same serial number, then we try to preserve those semantics by
1559 * reusing the same serial number on the way out too. */
1560 if (serial_in == input->key_serial)
1561 serial_out = wl_display_get_serial(c->base.wl_display);
1562 else
1563 serial_out = wl_display_next_serial(c->base.wl_display);
1564
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001565 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001566 mods_depressed, mods_latched,
1567 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001568 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001569}
1570
Jonny Lamb497994a2014-08-12 14:58:26 +02001571static void
1572input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1573 int32_t rate, int32_t delay)
1574{
1575 struct wayland_input *input = data;
1576 struct wayland_compositor *c = input->compositor;
1577
1578 c->base.kb_repeat_rate = rate;
1579 c->base.kb_repeat_delay = delay;
1580}
1581
Daniel Stone37816df2012-05-16 18:45:18 +01001582static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001583 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001584 input_handle_keyboard_enter,
1585 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001586 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001587 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001588 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001589};
1590
1591static void
Daniel Stone37816df2012-05-16 18:45:18 +01001592input_handle_capabilities(void *data, struct wl_seat *seat,
1593 enum wl_seat_capability caps)
1594{
1595 struct wayland_input *input = data;
1596
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001597 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1598 input->parent.pointer = wl_seat_get_pointer(seat);
1599 wl_pointer_set_user_data(input->parent.pointer, input);
1600 wl_pointer_add_listener(input->parent.pointer,
1601 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001602 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001603 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1604 wl_pointer_destroy(input->parent.pointer);
1605 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001606 }
1607
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001608 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1609 input->parent.keyboard = wl_seat_get_keyboard(seat);
1610 wl_keyboard_set_user_data(input->parent.keyboard, input);
1611 wl_keyboard_add_listener(input->parent.keyboard,
1612 &keyboard_listener, input);
1613 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1614 wl_keyboard_destroy(input->parent.keyboard);
1615 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001616 }
1617}
1618
Jonny Lamb497994a2014-08-12 14:58:26 +02001619static void
1620input_handle_name(void *data, struct wl_seat *seat,
1621 const char *name)
1622{
1623}
1624
Daniel Stone37816df2012-05-16 18:45:18 +01001625static const struct wl_seat_listener seat_listener = {
1626 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001627 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001628};
1629
1630static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001631display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001632{
1633 struct wayland_input *input;
1634
Peter Huttererf3d62272013-08-08 11:57:05 +10001635 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001636 if (input == NULL)
1637 return;
1638
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001639 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001640 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001641 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001642 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001643 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001644
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001645 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1646 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001647
1648 input->parent.cursor.surface =
1649 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001650}
1651
1652static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001653wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1654 int32_t x, int32_t y,
1655 int32_t physical_width, int32_t physical_height,
1656 int32_t subpixel, const char *make,
1657 const char *model, int32_t transform)
1658{
1659 struct wayland_parent_output *output = data;
1660
1661 output->x = x;
1662 output->y = y;
1663 output->physical.width = physical_width;
1664 output->physical.height = physical_height;
1665 output->physical.subpixel = subpixel;
1666
1667 free(output->physical.make);
1668 output->physical.make = strdup(make);
1669 free(output->physical.model);
1670 output->physical.model = strdup(model);
1671
1672 output->transform = transform;
1673}
1674
1675static struct weston_mode *
1676find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1677{
1678 struct weston_mode *mode;
1679
1680 wl_list_for_each(mode, list, link) {
1681 if (mode->width == width && mode->height == height &&
1682 mode->refresh == refresh)
1683 return mode;
1684 }
1685
1686 mode = zalloc(sizeof *mode);
1687 if (!mode)
1688 return NULL;
1689
1690 mode->width = width;
1691 mode->height = height;
1692 mode->refresh = refresh;
1693 wl_list_insert(list, &mode->link);
1694
1695 return mode;
1696}
1697
1698static void
1699wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1700 uint32_t flags, int32_t width, int32_t height,
1701 int32_t refresh)
1702{
1703 struct wayland_parent_output *output = data;
1704 struct weston_mode *mode;
1705
1706 if (output->output) {
1707 mode = find_mode(&output->output->base.mode_list,
1708 width, height, refresh);
1709 if (!mode)
1710 return;
1711 mode->flags = flags;
1712 /* Do a mode-switch on current mode change? */
1713 } else {
1714 mode = find_mode(&output->mode_list, width, height, refresh);
1715 if (!mode)
1716 return;
1717 mode->flags = flags;
1718 if (flags & WL_OUTPUT_MODE_CURRENT)
1719 output->current_mode = mode;
1720 if (flags & WL_OUTPUT_MODE_PREFERRED)
1721 output->preferred_mode = mode;
1722 }
1723}
1724
1725static const struct wl_output_listener output_listener = {
1726 wayland_parent_output_geometry,
1727 wayland_parent_output_mode
1728};
1729
1730static void
1731wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1732{
1733 struct wayland_parent_output *output;
1734
1735 output = zalloc(sizeof *output);
1736 if (!output)
1737 return;
1738
1739 output->id = id;
1740 output->global = wl_registry_bind(c->parent.registry, id,
1741 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001742 if (!output->global) {
1743 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001744 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001745 }
1746
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001747 wl_output_add_listener(output->global, &output_listener, output);
1748
1749 output->scale = 0;
1750 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1751 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1752 wl_list_init(&output->mode_list);
1753 wl_list_insert(&c->parent.output_list, &output->link);
1754
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001755 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001756 wl_display_roundtrip(c->parent.wl_display);
1757 wayland_output_create_for_parent_output(c, output);
1758 }
1759}
1760
1761static void
1762wayland_parent_output_destroy(struct wayland_parent_output *output)
1763{
1764 struct weston_mode *mode, *next;
1765
1766 if (output->output)
1767 wayland_output_destroy(&output->output->base);
1768
1769 wl_output_destroy(output->global);
1770 free(output->physical.make);
1771 free(output->physical.model);
1772
1773 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1774 wl_list_remove(&mode->link);
1775 free(mode);
1776 }
1777}
1778
1779static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001780registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1781 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001782{
1783 struct wayland_compositor *c = data;
1784
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001785 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001786 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001787 wl_registry_bind(registry, name,
1788 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001789 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001790 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001791 wl_registry_bind(registry, name,
1792 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001793 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1794 c->parent.fshell =
1795 wl_registry_bind(registry, name,
1796 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001797 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001798 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001799 } else if (strcmp(interface, "wl_output") == 0) {
1800 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001801 } else if (strcmp(interface, "wl_shm") == 0) {
1802 c->parent.shm =
1803 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001804 }
1805}
1806
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001807static void
1808registry_handle_global_remove(void *data, struct wl_registry *registry,
1809 uint32_t name)
1810{
1811 struct wayland_compositor *c = data;
1812 struct wayland_parent_output *output;
1813
1814 wl_list_for_each(output, &c->parent.output_list, link)
1815 if (output->id == name)
1816 wayland_parent_output_destroy(output);
1817}
1818
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001819static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001820 registry_handle_global,
1821 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001822};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001823
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001824static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001825wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1826{
1827 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001828 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001829
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001830 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1831 wl_display_terminate(c->base.wl_display);
1832 return 0;
1833 }
1834
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001835 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001836 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001837 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001838 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001839
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001840 if (mask == 0) {
1841 count = wl_display_dispatch_pending(c->parent.wl_display);
1842 wl_display_flush(c->parent.wl_display);
1843 }
1844
1845 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001846}
1847
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001848static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001849wayland_restore(struct weston_compositor *ec)
1850{
1851}
1852
1853static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001854wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001855{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001856 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1857
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001858 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001859
Jonas Ådahle5a12252013-04-05 23:07:11 +02001860 if (c->parent.shm)
1861 wl_shm_destroy(c->parent.shm);
1862
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001863 free(ec);
1864}
1865
Jason Ekstrand7744f712013-10-27 22:24:55 -05001866static const char *left_ptrs[] = {
1867 "left_ptr",
1868 "default",
1869 "top_left_arrow",
1870 "left-arrow"
1871};
1872
1873static void
1874create_cursor(struct wayland_compositor *c, struct weston_config *config)
1875{
1876 struct weston_config_section *s;
1877 int size;
1878 char *theme = NULL;
1879 unsigned int i;
1880
1881 s = weston_config_get_section(config, "shell", NULL, NULL);
1882 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1883 weston_config_section_get_int(s, "cursor-size", &size, 32);
1884
1885 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001886 if (!c->cursor_theme) {
1887 fprintf(stderr, "could not load cursor theme\n");
1888 return;
1889 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001890
U. Artie Eoffff755002014-01-17 12:36:58 -08001891 free(theme);
1892
Jason Ekstrand7744f712013-10-27 22:24:55 -05001893 c->cursor = NULL;
1894 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1895 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1896 left_ptrs[i]);
1897 if (!c->cursor) {
1898 fprintf(stderr, "could not load left cursor\n");
1899 return;
1900 }
1901}
1902
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001903static void
1904fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1905 void *data)
1906{
1907 struct wayland_compositor *c = data;
1908 struct wayland_input *input = NULL;
1909
1910 wl_list_for_each(input, &c->input_list, link)
1911 if (&input->base == seat_base)
1912 break;
1913
1914 if (!input || !input->output)
1915 return;
1916
1917 if (input->output->frame)
1918 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1919 else
1920 wayland_output_set_windowed(input->output);
1921
1922 weston_output_schedule_repaint(&input->output->base);
1923}
1924
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001925static struct wayland_compositor *
1926wayland_compositor_create(struct wl_display *display, int use_pixman,
1927 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001928 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001929{
1930 struct wayland_compositor *c;
1931 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001932 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001933
Peter Huttererf3d62272013-08-08 11:57:05 +10001934 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001935 if (c == NULL)
1936 return NULL;
1937
Daniel Stone725c2c32012-06-22 14:04:36 +01001938 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001939 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001940 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001941
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001942 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1943 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001944
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001945 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001946 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001947 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001948 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001949 }
1950
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001951 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001952 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001953 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1954 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001955 wl_display_roundtrip(c->parent.wl_display);
1956
1957 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001958
1959 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001960
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001961 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001962
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001963 if (!c->use_pixman) {
1964 gl_renderer = weston_load_module("gl-renderer.so",
1965 "gl_renderer_interface");
1966 if (!gl_renderer)
1967 c->use_pixman = 1;
1968 }
1969
1970 if (!c->use_pixman) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01001971 if (!gl_renderer->supports ||
1972 gl_renderer->supports(&c->base, "wayland") < 0) {
1973 weston_log("No support for "
1974 "EGL_{KHR,EXT,MESA}_platform_wayland; "
1975 "falling back to pixman.\n");
1976 c->use_pixman = 1;
1977 } else if (gl_renderer->create(&c->base,
1978 EGL_PLATFORM_WAYLAND_KHR,
1979 c->parent.wl_display,
1980 gl_renderer->alpha_attribs,
1981 NULL) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001982 weston_log("Failed to initialize the GL renderer; "
1983 "falling back to pixman.\n");
1984 c->use_pixman = 1;
1985 }
1986 }
1987
1988 if (c->use_pixman) {
1989 if (pixman_renderer_init(&c->base) < 0) {
1990 weston_log("Failed to initialize pixman renderer\n");
1991 goto err_display;
1992 }
1993 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001994
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001995 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001996 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001997
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001998 loop = wl_display_get_event_loop(c->base.wl_display);
1999
2000 fd = wl_display_get_fd(c->parent.wl_display);
2001 c->parent.wl_source =
2002 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2003 wayland_compositor_handle_event, c);
2004 if (c->parent.wl_source == NULL)
2005 goto err_renderer;
2006
2007 wl_event_source_check(c->parent.wl_source);
2008
2009 return c;
2010err_renderer:
2011 c->base.renderer->destroy(&c->base);
2012err_display:
2013 wl_display_disconnect(c->parent.wl_display);
2014err_compositor:
2015 weston_compositor_shutdown(&c->base);
2016err_free:
2017 free(c);
2018 return NULL;
2019}
2020
2021static void
2022wayland_compositor_destroy(struct wayland_compositor *c)
2023{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002024 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002025
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002026 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002027 wayland_output_destroy(output);
2028
2029 c->base.renderer->destroy(&c->base);
2030 wl_display_disconnect(c->parent.wl_display);
2031
2032 if (c->theme)
2033 theme_destroy(c->theme);
2034 if (c->frame_device)
2035 cairo_device_destroy(c->frame_device);
2036 wl_cursor_theme_destroy(c->cursor_theme);
2037
2038 weston_compositor_shutdown(&c->base);
2039 free(c);
2040}
2041
2042WL_EXPORT struct weston_compositor *
2043backend_init(struct wl_display *display, int *argc, char *argv[],
2044 struct weston_config *config)
2045{
2046 struct wayland_compositor *c;
2047 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002048 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002049 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002050 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002051 const char *section_name, *display_name;
2052 char *name;
2053
2054 const struct weston_option wayland_options[] = {
2055 { WESTON_OPTION_INTEGER, "width", 0, &width },
2056 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002057 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002058 { WESTON_OPTION_STRING, "display", 0, &display_name },
2059 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2060 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002061 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002062 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002063 };
2064
2065 width = 0;
2066 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002067 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002068 display_name = NULL;
2069 use_pixman = 0;
2070 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002071 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002072 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002073 parse_options(wayland_options,
2074 ARRAY_LENGTH(wayland_options), argc, argv);
2075
2076 c = wayland_compositor_create(display, use_pixman, display_name,
2077 argc, argv, config);
2078 if (!c)
2079 return NULL;
2080
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002081 if (sprawl || c->parent.fshell) {
2082 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002083 wl_display_roundtrip(c->parent.wl_display);
2084
2085 wl_list_for_each(poutput, &c->parent.output_list, link)
2086 wayland_output_create_for_parent_output(c, poutput);
2087
2088 return &c->base;
2089 }
2090
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002091 if (fullscreen) {
2092 output = wayland_output_create(c, 0, 0, width, height,
2093 NULL, 1, 0, 1);
2094 if (!output)
2095 goto err_outputs;
2096
Axel Davydd8b88d2013-11-17 21:34:16 +01002097 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002098 return &c->base;
2099 }
2100
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002101 section = NULL;
2102 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002103 while (weston_config_next_section(config, &section, &section_name)) {
2104 if (!section_name || strcmp(section_name, "output") != 0)
2105 continue;
2106 weston_config_section_get_string(section, "name", &name, NULL);
2107 if (name == NULL)
2108 continue;
2109
2110 if (name[0] != 'W' || name[1] != 'L') {
2111 free(name);
2112 continue;
2113 }
2114 free(name);
2115
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002116 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002117 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002118 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002119 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002120 if (wayland_output_set_windowed(output))
2121 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002122
2123 x += output->base.width;
2124 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002125 }
2126
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002127 if (!width)
2128 width = 1024;
2129 if (!height)
2130 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002131 if (!scale)
2132 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002133 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002134 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002135 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002137 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002138 if (wayland_output_set_windowed(output))
2139 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002140
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002141 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002142 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002143 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002144
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002145 weston_compositor_add_key_binding(&c->base, KEY_F,
2146 MODIFIER_CTRL | MODIFIER_ALT,
2147 fullscreen_binding, c);
2148
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002149 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002150
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002151err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002152 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002153 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002154}